2 * BIRD -- Path Operations 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 "nest/route.h"
13 #include "nest/attrs.h"
14 #include "lib/resource.h"
15 #include "filter/data.h"
18 #define AS_PATH_LENGTH 1000
20 #if AS_PATH_LENGTH > AS_PATH_MAXLEN
21 #warning "AS_PATH_LENGTH should be <= AS_PATH_MAXLEN"
28 for (round
= 0; round
< TESTS_NUM
; round
++)
30 struct adata empty_as_path
= {};
31 struct adata
*as_path
= &empty_as_path
;
32 u32 first_prepended
, last_prepended
;
33 first_prepended
= last_prepended
= 0;
35 struct f_path_mask
*mask
= alloca(sizeof(struct f_path_mask
) + AS_PATH_LENGTH
* sizeof(struct f_path_mask_item
));
36 mask
->len
= AS_PATH_LENGTH
;
37 for (int i
= AS_PATH_LENGTH
- 1; i
>= 0; i
--)
39 u32 val
= bt_random();
40 as_path
= as_path_prepend(tmp_linpool
, as_path
, val
);
41 bt_debug("Prepending ASN: %10u \n", val
);
45 if (i
== AS_PATH_LENGTH
-1)
46 first_prepended
= val
;
48 mask
->item
[i
].kind
= PM_ASN
;
49 mask
->item
[i
].asn
= val
;
52 bt_assert_msg(as_path_match(as_path
, mask
), "Mask should match with AS path");
56 bt_assert(as_path_get_first(as_path
, &asn
));
57 bt_assert_msg(asn
== last_prepended
, "as_path_get_first() should return the last prepended ASN");
59 bt_assert(as_path_get_last(as_path
, &asn
));
60 bt_assert_msg(asn
== first_prepended
, "as_path_get_last() should return the first prepended ASN");
71 struct adata empty_as_path
= {};
72 struct adata
*as_path
= &empty_as_path
;
75 for (i
= 4294967285; i
<= 4294967294; i
++)
77 as_path
= as_path_prepend(tmp_linpool
, as_path
, i
);
78 bt_debug("Prepending ASN: %10u \n", i
);
81 #define BUFFER_SIZE 120
82 byte buf
[BUFFER_SIZE
] = {};
84 as_path_format(&empty_as_path
, buf
, BUFFER_SIZE
);
85 bt_assert_msg(strcmp(buf
, "") == 0, "Buffer(%zu): '%s'", strlen(buf
), buf
);
87 as_path_format(as_path
, buf
, BUFFER_SIZE
);
88 bt_assert_msg(strcmp(buf
, "4294967294 4294967293 4294967292 4294967291 4294967290 4294967289 4294967288 4294967287 4294967286 4294967285") == 0, "Buffer(%zu): '%s'", strlen(buf
), buf
);
90 #define SMALL_BUFFER_SIZE 25
91 byte buf2
[SMALL_BUFFER_SIZE
] = {};
92 as_path_format(as_path
, buf2
, SMALL_BUFFER_SIZE
);
93 bt_assert_msg(strcmp(buf2
, "4294967294 42...") == 0, "Small Buffer(%zu): '%s'", strlen(buf2
), buf2
);
101 count_asn_in_array(const u32
*array
, u32 asn
)
103 int counts_of_contains
= 0;
105 for (u
= 0; u
< AS_PATH_LENGTH
; u
++)
107 counts_of_contains
++;
108 return counts_of_contains
;
114 struct adata empty_as_path
= {};
115 struct adata
*as_path
= &empty_as_path
;
117 u32 as_nums
[AS_PATH_LENGTH
] = {};
119 for (i
= 0; i
< AS_PATH_LENGTH
; i
++)
121 u32 val
= bt_random();
123 as_path
= as_path_prepend(tmp_linpool
, as_path
, val
);
126 for (i
= 0; i
< AS_PATH_LENGTH
; i
++)
128 int counts_of_contains
= count_asn_in_array(as_nums
, as_nums
[i
]);
129 bt_assert_msg(as_path_contains(as_path
, as_nums
[i
], counts_of_contains
), "AS Path should contains %d-times number %d", counts_of_contains
, as_nums
[i
]);
131 struct f_val v
= { .type
= T_INT
, .val
.i
= as_nums
[i
] };
132 bt_assert(as_path_filter(tmp_linpool
, as_path
, &v
, 0) != NULL
);
133 bt_assert(as_path_filter(tmp_linpool
, as_path
, &v
, 1) != NULL
);
136 for (i
= 0; i
< 10000; i
++)
138 u32 test_val
= bt_random();
139 int counts_of_contains
= count_asn_in_array(as_nums
, test_val
);
140 int result
= as_path_contains(as_path
, test_val
, (counts_of_contains
== 0 ? 1 : counts_of_contains
));
142 if (counts_of_contains
)
143 bt_assert_msg(result
, "As path should contain %d-times the number %u", counts_of_contains
, test_val
);
145 bt_assert_msg(result
== 0, "As path should not contain the number %u", test_val
);
155 t_as_path_converting(void)
157 struct adata empty_as_path
= {};
158 struct adata
*as_path
= &empty_as_path
;
159 #define AS_PATH_LENGTH_FOR_CONVERTING_TEST 10
162 for (i
= 0; i
< AS_PATH_LENGTH_FOR_CONVERTING_TEST
; i
++)
163 as_path
= as_path_prepend(tmp_linpool
, as_path
, i
);
165 bt_debug("data length: %u \n", as_path
->length
);
167 byte buffer
[100] = {};
168 int used_size
= as_path_convert_to_new(as_path
, buffer
, AS_PATH_LENGTH_FOR_CONVERTING_TEST
-1);
169 bt_debug("as_path_convert_to_new: len %d \n%s\n", used_size
, buffer
);
170 for (i
= 0; i
< used_size
; i
++)
172 bt_debug("\\03%d", buffer
[i
]);
175 bt_assert(memcmp(buffer
,
176 "\032\039\030\030\030\030\030\030\030\039\030\030\030\030\030\030\030\038\030\030\030\030\030\030"
177 "\030\037\030\030\030\030\030\030\030\036\030\030\030\030",
180 bzero(buffer
, sizeof(buffer
));
182 used_size
= as_path_convert_to_old(as_path
, buffer
, &new_used
);
183 bt_debug("as_path_convert_to_old: len %d, new_used: %d \n", used_size
, new_used
);
184 for (i
= 0; i
< used_size
; i
++)
186 bt_debug("\\03%d", buffer
[i
]);
189 bt_assert(memcmp(buffer
,
190 "\032\0310\030\039\030\038\030\037\030\036\030\035\030\034\030\033\030\032\030\031\030\030",
198 main(int argc
, char *argv
[])
202 bt_test_suite(t_as_path_match
, "Testing AS path matching and some a-path utilities.");
203 bt_test_suite(t_path_format
, "Testing formating as path into byte buffer");
204 bt_test_suite(t_path_include
, "Testing including a AS number in AS path");
205 // bt_test_suite(t_as_path_converting, "Testing as_path_convert_to_*() output constancy");
207 return bt_exit_value();