]>
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 "conf/conf.h"
16 #define PREFIXES_NUM 10
17 #define PREFIX_TESTS_NUM 10000
19 #define BIG_BUFFER_SIZE 10000
21 /* Wrapping structure for storing f_prefixes structures in list */
22 struct f_prefix_node
{
24 struct f_prefix prefix
;
30 return ( bt_random () % max
);
34 is_prefix_included ( list
* prefixes
, struct f_prefix
* needle
)
36 struct f_prefix_node
* n
;
37 WALK_LIST ( n
, * prefixes
)
39 ip6_addr cmask
= ip6_mkmask ( MIN ( n
-> prefix
. net
. pxlen
, needle
-> net
. pxlen
));
41 ip6_addr ip
= net6_prefix (& n
-> prefix
. net
);
42 ip6_addr needle_ip
= net6_prefix (& needle
-> net
);
44 if (( ipa_compare ( ipa_and ( ip
, cmask
), ipa_and ( needle_ip
, cmask
)) == 0 ) &&
45 ( n
-> prefix
. lo
<= needle
-> net
. pxlen
) && ( needle
-> net
. pxlen
<= n
-> prefix
. hi
))
47 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
);
54 static struct f_prefix
55 get_random_ip6_prefix ( void )
58 u8 pxlen
= xrandom ( 120 )+ 8 ;
59 ip6_addr ip6
= ip6_build ( bt_random (), bt_random (), bt_random (), bt_random ());
60 net_addr_ip6 net6
= NET_ADDR_IP6 ( ip6
, pxlen
);
62 p
. net
= *(( net_addr
*) & net6
);
72 p
. hi
= net_max_prefix_length
[ p
. net
. type
];
79 generate_random_ipv6_prefixes ( list
* prefixes
)
82 for ( i
= 0 ; i
< PREFIXES_NUM
; i
++)
84 struct f_prefix f
= get_random_ip6_prefix ();
86 struct f_prefix_node
* px
= calloc ( 1 , sizeof ( struct f_prefix_node
));
89 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
);
90 add_tail ( prefixes
, & px
-> n
);
98 bt_config_parse ( BT_CONFIG_SIMPLE
);
101 for ( round
= 0 ; round
< TESTS_NUM
; round
++)
103 list prefixes
; /* of structs f_extended_prefix */
104 init_list (& prefixes
);
105 struct f_trie
* trie
= f_new_trie ( config
-> mem
, sizeof ( struct f_trie_node
));
107 generate_random_ipv6_prefixes (& prefixes
);
108 struct f_prefix_node
* n
;
109 WALK_LIST ( n
, prefixes
)
111 trie_add_prefix ( trie
, & n
-> prefix
. net
, n
-> prefix
. lo
, n
-> prefix
. hi
);
115 for ( i
= 0 ; i
< PREFIX_TESTS_NUM
; i
++)
117 struct f_prefix f
= get_random_ip6_prefix ();
118 bt_debug ( "TEST \t " PRIip6
"/%d \n " , ARGip6 ( net6_prefix (& f
. net
)), f
. net
. pxlen
);
120 int should_be
= is_prefix_included (& prefixes
, & f
);
121 int is_there
= trie_match_net ( trie
, & f
. net
);
122 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" ));
125 struct f_prefix_node
* nxt
;
126 WALK_LIST_DELSAFE ( n
, nxt
, prefixes
)
140 bt_config_parse ( BT_CONFIG_SIMPLE
);
143 for ( round
= 0 ; round
< TESTS_NUM
* 4 ; round
++)
145 struct f_trie
* trie1
= f_new_trie ( config
-> mem
, sizeof ( struct f_trie_node
));
146 struct f_trie
* trie2
= f_new_trie ( config
-> mem
, sizeof ( struct f_trie_node
));
148 list prefixes
; /* a list of f_extended_prefix structures */
149 init_list (& prefixes
);
151 for ( i
= 0 ; i
< 100 ; i
++)
152 generate_random_ipv6_prefixes (& prefixes
);
154 struct f_prefix_node
* n
;
155 WALK_LIST ( n
, prefixes
)
157 trie_add_prefix ( trie1
, & n
-> prefix
. net
, n
-> prefix
. lo
, n
-> prefix
. hi
);
159 WALK_LIST_BACKWARDS ( n
, prefixes
)
161 trie_add_prefix ( trie2
, & n
-> prefix
. net
, n
-> prefix
. lo
, n
-> prefix
. hi
);
164 bt_assert ( trie_same ( trie1
, trie2
));
166 struct f_prefix_node
* nxt
;
167 WALK_LIST_DELSAFE ( n
, nxt
, prefixes
)
177 main ( int argc
, char * argv
[])
181 bt_test_suite ( t_match_net
, "Testing random prefix matching" );
182 bt_test_suite ( t_trie_same
, "A trie filled forward should be same with a trie filled backward." );
184 return bt_exit_value ();