ospf6d: fix warnings from recent prefix bit commit
[jleu-quagga.git] / tests / aspath_test.c
blobfb504f317f494ab5c804c3776f12ded7b239ad58
1 #include <zebra.h>
3 #include "vty.h"
4 #include "stream.h"
5 #include "privs.h"
7 #include "bgpd/bgpd.h"
8 #include "bgpd/bgp_aspath.h"
10 #define VT100_RESET "\x1b[0m"
11 #define VT100_RED "\x1b[31m"
12 #define VT100_GREEN "\x1b[32m"
13 #define VT100_YELLOW "\x1b[33m"
14 #define OK VT100_GREEN "OK" VT100_RESET
15 #define FAILED VT100_RED "failed" VT100_RESET
17 /* need these to link in libbgp */
18 struct zebra_privs_t *bgpd_privs = NULL;
19 struct thread_master *master = NULL;
21 static int failed = 0;
23 /* specification for a test - what the results should be */
24 struct test_spec
26 const char *shouldbe; /* the string the path should parse to */
27 const char *shouldbe_delete_confed; /* ditto, but once confeds are deleted */
28 const unsigned int hops; /* aspath_count_hops result */
29 const unsigned int confeds; /* aspath_count_confeds */
30 const int private_as; /* whether the private_as check should pass or fail */
31 #define NOT_ALL_PRIVATE 0
32 #define ALL_PRIVATE 1
33 const as_t does_loop; /* an ASN which should trigger loop-check */
34 const as_t doesnt_loop; /* one which should not */
35 const as_t first; /* the first ASN, if there is one */
36 #define NULL_ASN 0
40 /* test segments to parse and validate, and use for other tests */
41 static struct test_segment {
42 const char *name;
43 const char *desc;
44 const u_char asdata[1024];
45 int len;
46 struct test_spec sp;
47 } test_segments [] =
49 { /* 0 */
50 "seq1",
51 "seq(8466,3,52737,4096)",
52 { 0x2,0x4, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00 },
53 10,
54 { "8466 3 52737 4096",
55 "8466 3 52737 4096",
56 4, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
58 { /* 1 */
59 "seq2",
60 "seq(8722) seq(4)",
61 { 0x2,0x1, 0x22,0x12,
62 0x2,0x1, 0x00,0x04 },
64 { "8722 4",
65 "8722 4",
66 2, 0, NOT_ALL_PRIVATE, 4, 5, 8722, },
68 { /* 2 */
69 "seq3",
70 "seq(8466,3,52737,4096,8722,4)",
71 { 0x2,0x6, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00,
72 0x22,0x12, 0x00,0x04},
73 14,
74 { "8466 3 52737 4096 8722 4",
75 "8466 3 52737 4096 8722 4",
76 6, 0, NOT_ALL_PRIVATE, 3, 5, 8466 },
78 { /* 3 */
79 "seqset",
80 "seq(8482,51457) set(5204)",
81 { 0x2,0x2, 0x21,0x22, 0xc9,0x01,
82 0x1,0x1, 0x14,0x54 },
83 10,
84 { "8482 51457 {5204}",
85 "8482 51457 {5204}",
86 3, 0, NOT_ALL_PRIVATE, 5204, 51456, 8482},
88 { /* 4 */
89 "seqset2",
90 "seq(8467, 59649) set(4196,48658) set(17322,30745)",
91 { 0x2,0x2, 0x21,0x13, 0xe9,0x01,
92 0x1,0x2, 0x10,0x64, 0xbe,0x12,
93 0x1,0x2, 0x43,0xaa, 0x78,0x19 },
94 18,
95 { "8467 59649 {4196,48658} {17322,30745}",
96 "8467 59649 {4196,48658} {17322,30745}",
97 4, 0, NOT_ALL_PRIVATE, 48658, 1, 8467},
99 { /* 5 */
100 "multi",
101 "seq(6435,59408,21665) set(2457,61697,4369), seq(1842,41590,51793)",
102 { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
103 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
104 0x2,0x3, 0x07,0x32, 0xa2,0x76, 0xca,0x51 },
106 { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
107 "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
108 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
110 { /* 6 */
111 "confed",
112 "confseq(123,456,789)",
113 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15 },
115 { "(123 456 789)",
117 0, 3, NOT_ALL_PRIVATE, 789, 1, NULL_ASN },
119 { /* 7 */
120 "confed2",
121 "confseq(123,456,789) confseq(111,222)",
122 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
123 0x3,0x2, 0x00,0x6f, 0x00,0xde },
125 { "(123 456 789) (111 222)",
127 0, 5, NOT_ALL_PRIVATE, 111, 1, NULL_ASN },
129 { /* 8 */
130 "confset",
131 "confset(456,123,789)",
132 { 0x4,0x3, 0x01,0xc8, 0x00,0x7b, 0x03,0x15 },
134 { "[123,456,789]",
135 "[123,456,789]",
136 0, 1, NOT_ALL_PRIVATE, 123, 1, NULL_ASN },
138 { /* 9 */
139 "confmulti",
140 "confseq(123,456,789) confset(222,111) seq(8722) set(4196,48658)",
141 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
142 0x4,0x2, 0x00,0xde, 0x00,0x6f,
143 0x2,0x1, 0x22,0x12,
144 0x1,0x2, 0x10,0x64, 0xbe,0x12 },
146 { "(123 456 789) [111,222] 8722 {4196,48658}",
147 "8722 {4196,48658}",
148 2, 4, NOT_ALL_PRIVATE, 123, 1, NULL_ASN },
150 { /* 10 */
151 "seq4",
152 "seq(8466,2,52737,4096,8722,4)",
153 { 0x2,0x6, 0x21,0x12, 0x00,0x02, 0xce,0x01, 0x10,0x00,
154 0x22,0x12, 0x00,0x04},
156 { "8466 2 52737 4096 8722 4",
157 "8466 2 52737 4096 8722 4",
158 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
160 { /* 11 */
161 "tripleseq1",
162 "seq(8466,2,52737) seq(4096,8722,4) seq(8722)",
163 { 0x2,0x3, 0x21,0x12, 0x00,0x02, 0xce,0x01,
164 0x2,0x3, 0x10,0x00, 0x22,0x12, 0x00,0x04,
165 0x2,0x1, 0x22,0x12},
167 { "8466 2 52737 4096 8722 4 8722",
168 "8466 2 52737 4096 8722 4 8722",
169 7, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
171 { /* 12 */
172 "someprivate",
173 "seq(8466,64512,52737,65535)",
174 { 0x2,0x4, 0x21,0x12, 0xfc,0x00, 0xce,0x01, 0xff,0xff },
176 { "8466 64512 52737 65535",
177 "8466 64512 52737 65535",
178 4, 0, NOT_ALL_PRIVATE, 65535, 4, 8466 },
180 { /* 13 */
181 "allprivate",
182 "seq(65534,64512,64513,65535)",
183 { 0x2,0x4, 0xff,0xfe, 0xfc,0x00, 0xfc,0x01, 0xff,0xff },
185 { "65534 64512 64513 65535",
186 "65534 64512 64513 65535",
187 4, 0, ALL_PRIVATE, 65534, 4, 65534 },
189 { /* 14 */
190 "long",
191 "seq(8466,3,52737,4096,34285,<repeated 49 more times>)",
192 { 0x2,0xfa, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
193 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
194 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
195 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
196 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
197 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
198 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
199 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
200 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
201 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
202 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
203 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
204 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
205 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
206 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
207 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
208 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
209 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
210 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
211 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
212 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
213 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
214 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
215 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
216 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
217 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
218 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
219 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
220 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
221 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
222 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
223 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
224 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
225 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
226 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
227 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
228 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
229 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
230 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
231 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
232 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
233 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
234 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
235 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
236 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
237 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
238 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
239 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
240 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
241 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, },
242 502,
243 { "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
244 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
245 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
246 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
247 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
248 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
249 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
250 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
251 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
252 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
253 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
254 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
255 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
256 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
257 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
258 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
259 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
260 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
261 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
262 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
263 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
264 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
265 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
266 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
267 "8466 3 52737 4096 34285 8466 3 52737 4096 34285",
269 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
270 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
271 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
272 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
273 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
274 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
275 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
276 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
277 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
278 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
279 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
280 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
281 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
282 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
283 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
284 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
285 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
286 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
287 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
288 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
289 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
290 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
291 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
292 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
293 "8466 3 52737 4096 34285 8466 3 52737 4096 34285",
294 250, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
296 { /* 15 */
297 "seq1extra",
298 "seq(8466,3,52737,4096,3456)",
299 { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
301 { "8466 3 52737 4096 3456",
302 "8466 3 52737 4096 3456",
303 5, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
305 { /* 16 */
306 "empty",
307 "<empty>",
310 { "", "", 0, 0, 0, 0, 0, 0 },
312 { /* 17 */
313 "redundantset",
314 "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153)",
315 { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
316 0x1,0x4, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9 },
319 /* We shouldn't ever /generate/ such paths. However, we should
320 * cope with them fine.
322 "8466 3 52737 4096 3456 {7099,8153}",
323 "8466 3 52737 4096 3456 {7099,8153}",
324 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
326 { /* 18 */
327 "reconcile_lead_asp",
328 "seq(6435,59408,21665) set(23456,23456,23456), seq(23456,23456,23456)",
329 { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
330 0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
331 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
333 { "6435 59408 21665 {23456} 23456 23456 23456",
334 "6435 59408 21665 {23456} 23456 23456 23456",
335 7, 0, NOT_ALL_PRIVATE, 23456, 1, 6435 },
337 { /* 19 */
338 "reconcile_new_asp",
339 "set(2457,61697,4369), seq(1842,41591,51793)",
341 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
342 0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51 },
344 { "{2457,4369,61697} 1842 41591 51793",
345 "{2457,4369,61697} 1842 41591 51793",
346 4, 0, NOT_ALL_PRIVATE, 51793, 1, 2457 },
348 { /* 20 */
349 "reconcile_confed",
350 "confseq(123,456,789) confset(456,124,788) seq(6435,59408,21665)"
351 " set(23456,23456,23456), seq(23456,23456,23456)",
352 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
353 0x4,0x3, 0x01,0xc8, 0x00,0x7c, 0x03,0x14,
354 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
355 0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
356 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
358 { "(123 456 789) [124,456,788] 6435 59408 21665"
359 " {23456} 23456 23456 23456",
360 "6435 59408 21665 {23456} 23456 23456 23456",
361 7, 4, NOT_ALL_PRIVATE, 23456, 1, 6435 },
363 { /* 21 */
364 "reconcile_start_trans",
365 "seq(23456,23456,23456) seq(6435,59408,21665)",
366 { 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
367 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, },
369 { "23456 23456 23456 6435 59408 21665",
370 "23456 23456 23456 6435 59408 21665",
371 6, 0, NOT_ALL_PRIVATE, 21665, 1, 23456 },
373 { /* 22 */
374 "reconcile_start_trans4",
375 "seq(1842,41591,51793) seq(6435,59408,21665)",
376 { 0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51,
377 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, },
379 { "1842 41591 51793 6435 59408 21665",
380 "1842 41591 51793 6435 59408 21665",
381 6, 0, NOT_ALL_PRIVATE, 41591, 1, 1842 },
383 { /* 23 */
384 "reconcile_start_trans_error",
385 "seq(23456,23456,23456) seq(6435,59408)",
386 { 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
387 0x2,0x2, 0x19,0x23, 0xe8,0x10, },
389 { "23456 23456 23456 6435 59408",
390 "23456 23456 23456 6435 59408",
391 5, 0, NOT_ALL_PRIVATE, 59408, 1, 23456 },
393 { /* 24 */
394 "redundantset2",
395 "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153,7099)",
396 { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
397 0x1,0x5, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9, 0x1b,0xbb,},
400 /* We should weed out duplicate set members. */
401 "8466 3 52737 4096 3456 {7099,8153}",
402 "8466 3 52737 4096 3456 {7099,8153}",
403 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
405 { /* 25 */
406 "zero-size overflow",
407 "#ASNs = 0, data = seq(8466 3 52737 4096 3456)",
408 { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
410 { "", "",
411 0, 0, 0, 0, 0, 0 },
413 { /* 26 */
414 "zero-size overflow + valid segment",
415 "seq(#AS=0:8466 3 52737),seq(4096 3456)",
416 { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01,
417 0x2,0x2, 0x10,0x00, 0x0d,0x80 },
420 { "", "",
421 0, 0, 0, 0, 0, 0 },
423 { NULL, NULL, {0}, 0, { NULL, 0, 0 } }
426 /* prepending tests */
427 static struct tests {
428 const struct test_segment *test1;
429 const struct test_segment *test2;
430 struct test_spec sp;
431 } prepend_tests[] =
433 { &test_segments[0], &test_segments[1],
434 { "8466 3 52737 4096 8722 4",
435 "8466 3 52737 4096 8722 4",
436 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
438 { &test_segments[1], &test_segments[3],
439 { "8722 4 8482 51457 {5204}",
440 "8722 4 8482 51457 {5204}",
441 5, 0, NOT_ALL_PRIVATE, 5204, 1, 8722 }
443 { &test_segments[3], &test_segments[4],
444 { "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
445 "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
446 7, 0, NOT_ALL_PRIVATE, 5204, 1, 8482 },
448 { &test_segments[4], &test_segments[5],
449 { "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
450 " {2457,4369,61697} 1842 41590 51793",
451 "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
452 " {2457,4369,61697} 1842 41590 51793",
453 11, 0, NOT_ALL_PRIVATE, 61697, 1, 8467 }
455 { &test_segments[5], &test_segments[6],
456 { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793 (123 456 789)",
457 "6435 59408 21665 {2457,4369,61697} 1842 41590 51793 (123 456 789)",
458 7, 3, NOT_ALL_PRIVATE, 123, 1, 6435 },
460 { &test_segments[6], &test_segments[7],
461 { "(123 456 789) (123 456 789) (111 222)",
463 0, 8, NOT_ALL_PRIVATE, 111, 1, 0 }
465 { &test_segments[7], &test_segments[8],
466 { "(123 456 789) (111 222) [123,456,789]",
468 0, 6, NOT_ALL_PRIVATE, 111, 1, 0 }
470 { &test_segments[8], &test_segments[9],
471 { "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
472 "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
473 2, 5, NOT_ALL_PRIVATE, 456, 1, NULL_ASN },
475 { &test_segments[9], &test_segments[8],
476 { "(123 456 789) [111,222] 8722 {4196,48658} [123,456,789]",
477 "8722 {4196,48658} [123,456,789]",
478 2, 5, NOT_ALL_PRIVATE, 48658, 1, NULL_ASN },
480 { &test_segments[14], &test_segments[11],
481 { "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
482 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
483 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
484 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
485 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
486 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
487 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
488 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
489 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
490 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
491 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
492 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
493 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
494 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
495 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
496 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
497 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
498 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
499 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
500 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
501 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
502 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
503 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
504 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
505 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
506 "8466 2 52737 4096 8722 4 8722",
508 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
509 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
510 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
511 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
512 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
513 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
514 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
515 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
516 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
517 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
518 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
519 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
520 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
521 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
522 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
523 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
524 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
525 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
526 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
527 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
528 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
529 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
530 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
531 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
532 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
533 "8466 2 52737 4096 8722 4 8722",
534 257, 0, NOT_ALL_PRIVATE, 4096, 1000, 8466 },
536 { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
539 struct tests reconcile_tests[] =
541 { &test_segments[18], &test_segments[19],
542 { "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
543 "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
544 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
546 { &test_segments[19], &test_segments[18],
547 /* AS_PATH (19) has more hops than NEW_AS_PATH,
548 * so just AS_PATH should be used (though, this practice
549 * is bad imho).
551 { "{2457,4369,61697} 1842 41591 51793 6435 59408 21665 {23456} 23456 23456 23456",
552 "{2457,4369,61697} 1842 41591 51793 6435 59408 21665 {23456} 23456 23456 23456",
553 11, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
555 { &test_segments[20], &test_segments[19],
556 { "(123 456 789) [124,456,788] 6435 59408 21665"
557 " {2457,4369,61697} 1842 41591 51793",
558 "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
559 7, 4, NOT_ALL_PRIVATE, 51793, 1, 6435 },
561 { &test_segments[21], &test_segments[22],
562 { "1842 41591 51793 6435 59408 21665",
563 "1842 41591 51793 6435 59408 21665",
564 6, 0, NOT_ALL_PRIVATE, 51793, 1, 1842 },
566 { &test_segments[23], &test_segments[22],
567 { "23456 23456 23456 6435 59408 1842 41591 51793 6435 59408 21665",
568 "23456 23456 23456 6435 59408 1842 41591 51793 6435 59408 21665",
569 11, 0, NOT_ALL_PRIVATE, 51793, 1, 1842 },
571 { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
574 struct tests aggregate_tests[] =
576 { &test_segments[0], &test_segments[2],
577 { "8466 3 52737 4096 {4,8722}",
578 "8466 3 52737 4096 {4,8722}",
579 5, 0, NOT_ALL_PRIVATE, 4, 1, 8466 },
581 { &test_segments[2], &test_segments[0],
582 { "8466 3 52737 4096 {4,8722}",
583 "8466 3 52737 4096 {4,8722}",
584 5, 0, NOT_ALL_PRIVATE, 8722, 1, 8466 },
586 { &test_segments[2], &test_segments[10],
587 { "8466 {2,3,4,4096,8722,52737}",
588 "8466 {2,3,4,4096,8722,52737}",
589 2, 0, NOT_ALL_PRIVATE, 8722, 5, 8466 },
591 { &test_segments[10], &test_segments[2],
592 { "8466 {2,3,4,4096,8722,52737}",
593 "8466 {2,3,4,4096,8722,52737}",
594 2, 0, NOT_ALL_PRIVATE, 2, 20000, 8466 },
597 { &test_segments[5], &test_segments[18],
598 { "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
599 "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
600 4, 0, NOT_ALL_PRIVATE, 41590, 1, 6435 },
603 { NULL, NULL, { NULL, 0, 0} },
606 struct compare_tests
608 int test_index1;
609 int test_index2;
610 #define CMP_RES_YES 1
611 #define CMP_RES_NO 0
612 char shouldbe_cmp;
613 char shouldbe_confed;
614 } left_compare [] =
616 { 0, 1, CMP_RES_NO, CMP_RES_NO },
617 { 0, 2, CMP_RES_YES, CMP_RES_NO },
618 { 0, 11, CMP_RES_YES, CMP_RES_NO },
619 { 0, 15, CMP_RES_YES, CMP_RES_NO },
620 { 0, 16, CMP_RES_NO, CMP_RES_NO },
621 { 1, 11, CMP_RES_NO, CMP_RES_NO },
622 { 6, 7, CMP_RES_NO, CMP_RES_YES },
623 { 6, 8, CMP_RES_NO, CMP_RES_NO },
624 { 7, 8, CMP_RES_NO, CMP_RES_NO },
625 { 1, 9, CMP_RES_YES, CMP_RES_NO },
626 { 0, 9, CMP_RES_NO, CMP_RES_NO },
627 { 3, 9, CMP_RES_NO, CMP_RES_NO },
628 { 0, 6, CMP_RES_NO, CMP_RES_NO },
629 { 1, 6, CMP_RES_NO, CMP_RES_NO },
630 { 0, 8, CMP_RES_NO, CMP_RES_NO },
631 { 1, 8, CMP_RES_NO, CMP_RES_NO },
632 { 11, 6, CMP_RES_NO, CMP_RES_NO },
633 { 11, 7, CMP_RES_NO, CMP_RES_NO },
634 { 11, 8, CMP_RES_NO, CMP_RES_NO },
635 { 9, 6, CMP_RES_NO, CMP_RES_YES },
636 { 9, 7, CMP_RES_NO, CMP_RES_YES },
637 { 9, 8, CMP_RES_NO, CMP_RES_NO },
640 /* make an aspath from a data stream */
641 static struct aspath *
642 make_aspath (const u_char *data, size_t len, int use32bit)
644 struct stream *s = NULL;
645 struct aspath *as;
647 if (len)
649 s = stream_new (len);
650 stream_put (s, data, len);
652 as = aspath_parse (s, len, use32bit);
654 if (s)
655 stream_free (s);
657 return as;
660 static void
661 printbytes (const u_char *bytes, int len)
663 int i = 0;
664 while (i < len)
666 if (i % 2)
667 printf ("%02hhx%s", bytes[i], " ");
668 else
669 printf ("0x%02hhx", bytes[i]);
670 i++;
672 printf ("\n");
675 /* validate the given aspath */
676 static int
677 validate (struct aspath *as, const struct test_spec *sp)
679 size_t bytes, bytes4;
680 int fails = 0;
681 const u_char *out;
682 static struct stream *s;
683 struct aspath *asinout, *asconfeddel, *asstr, *as4;
685 out = aspath_snmp_pathseg (as, &bytes);
686 asinout = make_aspath (out, bytes, 0);
688 /* Excercise AS4 parsing a bit, with a dogfood test */
689 if (!s)
690 s = stream_new (4096);
691 bytes4 = aspath_put (s, as, 1);
692 as4 = make_aspath (STREAM_DATA(s), bytes4, 1);
694 asstr = aspath_str2aspath (sp->shouldbe);
696 asconfeddel = aspath_delete_confed_seq (aspath_dup (asinout));
698 printf ("got: %s\n", aspath_print(as));
700 /* the parsed path should match the specified 'shouldbe' string.
701 * We should pass the "eat our own dog food" test, be able to output
702 * this path and then input it again. Ie the path resulting from:
704 * aspath_parse(aspath_put(as))
706 * should:
708 * - also match the specified 'shouldbe' value
709 * - hash to same value as original path
710 * - have same hops and confed counts as original, and as the
711 * the specified counts
713 * aspath_str2aspath() and shouldbe should match
715 * We do the same for:
717 * aspath_parse(aspath_put(as,USE32BIT))
719 * Confederation related tests:
720 * - aspath_delete_confed_seq(aspath) should match shouldbe_confed
721 * - aspath_delete_confed_seq should be idempotent.
723 if (strcmp(aspath_print (as), sp->shouldbe)
724 /* hash validation */
725 || (aspath_key_make (as) != aspath_key_make (asinout))
726 /* by string */
727 || strcmp(aspath_print (asinout), sp->shouldbe)
728 /* By 4-byte parsing */
729 || strcmp(aspath_print (as4), sp->shouldbe)
730 /* by various path counts */
731 || (aspath_count_hops (as) != sp->hops)
732 || (aspath_count_confeds (as) != sp->confeds)
733 || (aspath_count_hops (asinout) != sp->hops)
734 || (aspath_count_confeds (asinout) != sp->confeds))
736 failed++;
737 fails++;
738 printf ("shouldbe:\n%s\n", sp->shouldbe);
739 printf ("as4:\n%s\n", aspath_print (as4));
740 printf ("hash keys: in: %d out->in: %d\n",
741 aspath_key_make (as), aspath_key_make (asinout));
742 printf ("hops: %d, counted %d %d\n", sp->hops,
743 aspath_count_hops (as),
744 aspath_count_hops (asinout) );
745 printf ("confeds: %d, counted %d %d\n", sp->confeds,
746 aspath_count_confeds (as),
747 aspath_count_confeds (asinout));
748 printf ("out->in:\n%s\nbytes: ", aspath_print(asinout));
749 printbytes (out, bytes);
751 /* basic confed related tests */
752 if ((aspath_print (asconfeddel) == NULL
753 && sp->shouldbe_delete_confed != NULL)
754 || (aspath_print (asconfeddel) != NULL
755 && sp->shouldbe_delete_confed == NULL)
756 || strcmp(aspath_print (asconfeddel), sp->shouldbe_delete_confed)
757 /* delete_confed_seq should be idempotent */
758 || (aspath_key_make (asconfeddel)
759 != aspath_key_make (aspath_delete_confed_seq (asconfeddel))))
761 failed++;
762 fails++;
763 printf ("confed_del: %s\n", aspath_print (asconfeddel));
764 printf ("should be: %s\n", sp->shouldbe_delete_confed);
766 /* aspath_str2aspath test */
767 if ((aspath_print (asstr) == NULL && sp->shouldbe != NULL)
768 || (aspath_print (asstr) != NULL && sp->shouldbe == NULL)
769 || strcmp(aspath_print (asstr), sp->shouldbe))
771 failed++;
772 fails++;
773 printf ("asstr: %s\n", aspath_print (asstr));
776 /* loop, private and first as checks */
777 if ((sp->does_loop && aspath_loop_check (as, sp->does_loop) == 0)
778 || (sp->doesnt_loop && aspath_loop_check (as, sp->doesnt_loop) != 0)
779 || (aspath_private_as_check (as) != sp->private_as)
780 || (aspath_firstas_check (as,sp->first)
781 && sp->first == 0))
783 failed++;
784 fails++;
785 printf ("firstas: %d, got %d\n", sp->first,
786 aspath_firstas_check (as,sp->first));
787 printf ("loop does: %d %d, doesnt: %d %d\n",
788 sp->does_loop, aspath_loop_check (as, sp->does_loop),
789 sp->doesnt_loop, aspath_loop_check (as, sp->doesnt_loop));
790 printf ("private check: %d %d\n", sp->private_as,
791 aspath_private_as_check (as));
793 aspath_unintern (asinout);
794 aspath_unintern (as4);
796 aspath_free (asconfeddel);
797 aspath_free (asstr);
798 stream_reset (s);
800 return fails;
803 static void
804 empty_get_test ()
806 struct aspath *as = aspath_empty_get ();
807 struct test_spec sp = { "", "", 0, 0, 0, 0, 0, 0 };
809 printf ("empty_get_test, as: %s\n",aspath_print (as));
810 if (!validate (as, &sp))
811 printf ("%s\n", OK);
812 else
813 printf ("%s!\n", FAILED);
815 printf ("\n");
817 aspath_free (as);
820 /* basic parsing test */
821 static void
822 parse_test (struct test_segment *t)
824 struct aspath *asp;
826 printf ("%s: %s\n", t->name, t->desc);
828 asp = make_aspath (t->asdata, t->len, 0);
830 printf ("aspath: %s\nvalidating...:\n", aspath_print (asp));
832 if (!validate (asp, &t->sp))
833 printf (OK "\n");
834 else
835 printf (FAILED "\n");
837 printf ("\n");
838 aspath_unintern (asp);
841 /* prepend testing */
842 static void
843 prepend_test (struct tests *t)
845 struct aspath *asp1, *asp2, *ascratch;
847 printf ("prepend %s: %s\n", t->test1->name, t->test1->desc);
848 printf ("to %s: %s\n", t->test2->name, t->test2->desc);
850 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
851 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
853 ascratch = aspath_dup (asp2);
854 aspath_unintern (asp2);
856 asp2 = aspath_prepend (asp1, ascratch);
858 printf ("aspath: %s\n", aspath_print (asp2));
860 if (!validate (asp2, &t->sp))
861 printf ("%s\n", OK);
862 else
863 printf ("%s!\n", FAILED);
865 printf ("\n");
866 aspath_unintern (asp1);
867 aspath_free (asp2);
870 /* empty-prepend testing */
871 static void
872 empty_prepend_test (struct test_segment *t)
874 struct aspath *asp1, *asp2, *ascratch;
876 printf ("empty prepend %s: %s\n", t->name, t->desc);
878 asp1 = make_aspath (t->asdata, t->len, 0);
879 asp2 = aspath_empty ();
881 ascratch = aspath_dup (asp2);
882 aspath_unintern (asp2);
884 asp2 = aspath_prepend (asp1, ascratch);
886 printf ("aspath: %s\n", aspath_print (asp2));
888 if (!validate (asp2, &t->sp))
889 printf (OK "\n");
890 else
891 printf (FAILED "!\n");
893 printf ("\n");
894 aspath_unintern (asp1);
895 aspath_free (asp2);
898 /* as2+as4 reconciliation testing */
899 static void
900 as4_reconcile_test (struct tests *t)
902 struct aspath *asp1, *asp2, *ascratch;
904 printf ("reconciling %s:\n %s\n", t->test1->name, t->test1->desc);
905 printf ("with %s:\n %s\n", t->test2->name, t->test2->desc);
907 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
908 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
910 ascratch = aspath_reconcile_as4 (asp1, asp2);
912 if (!validate (ascratch, &t->sp))
913 printf (OK "\n");
914 else
915 printf (FAILED "!\n");
917 printf ("\n");
918 aspath_unintern (asp1);
919 aspath_unintern (asp2);
920 aspath_free (ascratch);
924 /* aggregation testing */
925 static void
926 aggregate_test (struct tests *t)
928 struct aspath *asp1, *asp2, *ascratch;
930 printf ("aggregate %s: %s\n", t->test1->name, t->test1->desc);
931 printf ("with %s: %s\n", t->test2->name, t->test2->desc);
933 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
934 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
936 ascratch = aspath_aggregate (asp1, asp2);
938 if (!validate (ascratch, &t->sp))
939 printf (OK "\n");
940 else
941 printf (FAILED "!\n");
943 printf ("\n");
944 aspath_unintern (asp1);
945 aspath_unintern (asp2);
946 aspath_free (ascratch);
947 /* aspath_unintern (ascratch);*/
950 /* cmp_left tests */
951 static void
952 cmp_test ()
954 unsigned int i;
955 #define CMP_TESTS_MAX \
956 (sizeof(left_compare) / sizeof (struct compare_tests))
958 for (i = 0; i < CMP_TESTS_MAX; i++)
960 struct test_segment *t1 = &test_segments[left_compare[i].test_index1];
961 struct test_segment *t2 = &test_segments[left_compare[i].test_index2];
962 struct aspath *asp1, *asp2;
964 printf ("left cmp %s: %s\n", t1->name, t1->desc);
965 printf ("and %s: %s\n", t2->name, t2->desc);
967 asp1 = make_aspath (t1->asdata, t1->len, 0);
968 asp2 = make_aspath (t2->asdata, t2->len, 0);
970 if (aspath_cmp_left (asp1, asp2) != left_compare[i].shouldbe_cmp
971 || aspath_cmp_left (asp2, asp1) != left_compare[i].shouldbe_cmp
972 || aspath_cmp_left_confed (asp1, asp2)
973 != left_compare[i].shouldbe_confed
974 || aspath_cmp_left_confed (asp2, asp1)
975 != left_compare[i].shouldbe_confed)
977 failed++;
978 printf (FAILED "\n");
979 printf ("result should be: cmp: %d, confed: %d\n",
980 left_compare[i].shouldbe_cmp,
981 left_compare[i].shouldbe_confed);
982 printf ("got: cmp %d, cmp_confed: %d\n",
983 aspath_cmp_left (asp1, asp2),
984 aspath_cmp_left_confed (asp1, asp2));
985 printf("path1: %s\npath2: %s\n", aspath_print (asp1),
986 aspath_print (asp2));
988 else
989 printf (OK "\n");
991 printf ("\n");
992 aspath_unintern (asp1);
993 aspath_unintern (asp2);
998 main (void)
1000 int i = 0;
1001 aspath_init();
1002 while (test_segments[i].name)
1004 parse_test (&test_segments[i]);
1005 empty_prepend_test (&test_segments[i++]);
1008 i = 0;
1009 while (prepend_tests[i].test1)
1010 prepend_test (&prepend_tests[i++]);
1012 i = 0;
1013 while (aggregate_tests[i].test1)
1014 aggregate_test (&aggregate_tests[i++]);
1016 i = 0;
1018 while (reconcile_tests[i].test1)
1019 as4_reconcile_test (&reconcile_tests[i++]);
1021 i = 0;
1023 cmp_test();
1025 i = 0;
1027 empty_get_test();
1029 printf ("failures: %d\n", failed);
1030 printf ("aspath count: %ld\n", aspath_count());
1032 return (failed + aspath_count());