]>
git.ipfire.org Git - thirdparty/bird.git/blob - filter/trie_test.c
2 * Filters: Utility Functions Tests
4 * (c) 2015 CZ.NIC z.s.p.o.
6 * Can be freely distributed and used under the terms of the GNU GPL.
9 #include "test/birdtest.h"
10 #include "test/bt-utils.h"
12 #include "filter/filter.h"
13 #include "filter/data.h"
14 #include "conf/conf.h"
17 #define PREFIXES_NUM 10
18 #define PREFIX_TESTS_NUM 10000
20 #define BIG_BUFFER_SIZE 10000
22 /* Wrapping structure for storing f_prefixes structures in list */
23 struct f_prefix_node
{
25 struct f_prefix prefix
;
31 return ( bt_random () % max
);
35 is_prefix_included ( list
* prefixes
, struct f_prefix
* needle
)
37 struct f_prefix_node
* n
;
38 WALK_LIST ( n
, * prefixes
)
40 ip6_addr cmask
= ip6_mkmask ( MIN ( n
-> prefix
. net
. pxlen
, needle
-> net
. pxlen
));
42 ip6_addr ip
= net6_prefix (& n
-> prefix
. net
);
43 ip6_addr needle_ip
= net6_prefix (& needle
-> net
);
45 if (( ipa_compare ( ipa_and ( ip
, cmask
), ipa_and ( needle_ip
, cmask
)) == 0 ) &&
46 ( n
-> prefix
. lo
<= needle
-> net
. pxlen
) && ( needle
-> net
. pxlen
<= n
-> prefix
. hi
))
48 bt_debug ( "FOUND \t " PRIip6
"/%d %d-%d \n " , ARGip6 ( net6_prefix (& n
-> prefix
. net
)), n
-> prefix
. net
. pxlen
, n
-> prefix
. lo
, n
-> prefix
. hi
);
55 static struct f_prefix
56 get_random_ip6_prefix ( void )
59 u8 pxlen
= xrandom ( 120 )+ 8 ;
60 ip6_addr ip6
= ip6_build ( bt_random (), bt_random (), bt_random (), bt_random ());
61 net_addr_ip6 net6
= NET_ADDR_IP6 ( ip6
, pxlen
);
63 p
. net
= *(( net_addr
*) & net6
);
73 p
. hi
= net_max_prefix_length
[ p
. net
. type
];
80 generate_random_ipv6_prefixes ( list
* prefixes
)
83 for ( i
= 0 ; i
< PREFIXES_NUM
; i
++)
85 struct f_prefix f
= get_random_ip6_prefix ();
87 struct f_prefix_node
* px
= calloc ( 1 , sizeof ( struct f_prefix_node
));
90 bt_debug ( "ADD \t " PRIip6
"/%d %d-%d \n " , ARGip6 ( net6_prefix (& px
-> prefix
. net
)), px
-> prefix
. net
. pxlen
, px
-> prefix
. lo
, px
-> prefix
. hi
);
91 add_tail ( prefixes
, & px
-> n
);
99 bt_config_parse ( BT_CONFIG_SIMPLE
);
102 for ( round
= 0 ; round
< TESTS_NUM
; round
++)
104 list prefixes
; /* of structs f_extended_prefix */
105 init_list (& prefixes
);
106 struct f_trie
* trie
= f_new_trie ( config
-> mem
, 0 );
108 generate_random_ipv6_prefixes (& prefixes
);
109 struct f_prefix_node
* n
;
110 WALK_LIST ( n
, prefixes
)
112 trie_add_prefix ( trie
, & n
-> prefix
. net
, n
-> prefix
. lo
, n
-> prefix
. hi
);
116 for ( i
= 0 ; i
< PREFIX_TESTS_NUM
; i
++)
118 struct f_prefix f
= get_random_ip6_prefix ();
119 bt_debug ( "TEST \t " PRIip6
"/%d \n " , ARGip6 ( net6_prefix (& f
. net
)), f
. net
. pxlen
);
121 int should_be
= is_prefix_included (& prefixes
, & f
);
122 int is_there
= trie_match_net ( trie
, & f
. net
);
123 bt_assert_msg ( should_be
== is_there
, "Prefix " PRIip6
"/%d %s" , ARGip6 ( net6_prefix (& f
. net
)), f
. net
. pxlen
, ( should_be
? "should be found in trie" : "should not be found in trie" ));
126 struct f_prefix_node
* nxt
;
127 WALK_LIST_DELSAFE ( n
, nxt
, prefixes
)
141 bt_config_parse ( BT_CONFIG_SIMPLE
);
144 for ( round
= 0 ; round
< TESTS_NUM
* 4 ; round
++)
146 struct f_trie
* trie1
= f_new_trie ( config
-> mem
, 0 );
147 struct f_trie
* trie2
= f_new_trie ( config
-> mem
, 0 );
149 list prefixes
; /* a list of f_extended_prefix structures */
150 init_list (& prefixes
);
152 for ( i
= 0 ; i
< 100 ; i
++)
153 generate_random_ipv6_prefixes (& prefixes
);
155 struct f_prefix_node
* n
;
156 WALK_LIST ( n
, prefixes
)
158 trie_add_prefix ( trie1
, & n
-> prefix
. net
, n
-> prefix
. lo
, n
-> prefix
. hi
);
160 WALK_LIST_BACKWARDS ( n
, prefixes
)
162 trie_add_prefix ( trie2
, & n
-> prefix
. net
, n
-> prefix
. lo
, n
-> prefix
. hi
);
165 bt_assert ( trie_same ( trie1
, trie2
));
167 struct f_prefix_node
* nxt
;
168 WALK_LIST_DELSAFE ( n
, nxt
, prefixes
)
178 main ( int argc
, char * argv
[])
182 bt_test_suite ( t_match_net
, "Testing random prefix matching" );
183 bt_test_suite ( t_trie_same
, "A trie filled forward should be same with a trie filled backward." );
185 return bt_exit_value ();