blob: beab685ed4b6bf4aee42e1e4293ece3bdd515111 [file] [log] [blame]
paul1f377b02005-09-10 18:00:11 +00001#include <zebra.h>
2
3#include "vty.h"
4#include "stream.h"
5#include "privs.h"
6
7#include "bgpd/bgpd.h"
8#include "bgpd/bgp_aspath.h"
9
10/* need these to link in libbgp */
11struct zebra_privs_t *bgpd_privs = NULL;
12struct thread_master *master = NULL;
13
14static int failed = 0;
15
16/* specification for a test - what the results should be */
17struct test_spec
18{
19 const char *shouldbe; /* the string the path should parse to */
20 const char *shouldbe_delete_confed; /* ditto, but once confeds are deleted */
paul84771ee2005-10-11 03:48:28 +000021 const unsigned int hops; /* aspath_count_hops result */
22 const unsigned int confeds; /* aspath_count_confeds */
paul1f377b02005-09-10 18:00:11 +000023 const int private_as; /* whether the private_as check should pass or fail */
24#define NOT_ALL_PRIVATE 0
25#define ALL_PRIVATE 1
26 const as_t does_loop; /* an ASN which should trigger loop-check */
27 const as_t doesnt_loop; /* one which should not */
28 const as_t first; /* the first ASN, if there is one */
29#define NULL_ASN 0
30};
31
32
33/* test segments to parse and validate, and use for other tests */
34static struct test_segment {
35 const char *name;
36 const char *desc;
37 const u_char asdata[1024];
38 int len;
39 struct test_spec sp;
40} test_segments [] =
41{
42 { /* 0 */
43 "seq1",
44 "seq(8466,3,52737,4096)",
45 { 0x2,0x4, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00 },
46 10,
47 { "8466 3 52737 4096",
48 "8466 3 52737 4096",
49 4, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
50 },
51 { /* 1 */
52 "seq2",
53 "seq(8722) seq(4)",
54 { 0x2,0x1, 0x22,0x12,
55 0x2,0x1, 0x00,0x04 },
56 8,
57 { "8722 4",
58 "8722 4",
59 2, 0, NOT_ALL_PRIVATE, 4, 5, 8722, },
60 },
61 { /* 2 */
62 "seq3",
63 "seq(8466,3,52737,4096,8722,4)",
64 { 0x2,0x6, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00,
65 0x22,0x12, 0x00,0x04},
66 14,
67 { "8466 3 52737 4096 8722 4",
68 "8466 3 52737 4096 8722 4",
69 6, 0, NOT_ALL_PRIVATE, 3, 5, 8466 },
70 },
71 { /* 3 */
72 "seqset",
73 "seq(8482,51457) set(5204)",
74 { 0x2,0x2, 0x21,0x22, 0xc9,0x01,
75 0x1,0x1, 0x14,0x54 },
76 10,
77 { "8482 51457 {5204}",
78 "8482 51457 {5204}",
79 3, 0, NOT_ALL_PRIVATE, 5204, 51457, 8482},
80 },
81 { /* 4 */
82 "seqset2",
83 "seq(8467, 59649) set(4196,48658) set(17322,30745)",
84 { 0x2,0x2, 0x21,0x13, 0xe9,0x01,
85 0x1,0x2, 0x10,0x64, 0xbe,0x12,
86 0x1,0x2, 0x43,0xaa, 0x78,0x19 },
87 18,
88 { "8467 59649 {4196,48658} {17322,30745}",
89 "8467 59649 {4196,48658} {17322,30745}",
90 4, 0, NOT_ALL_PRIVATE, 48658, 1, 8467},
91 },
92 { /* 5 */
93 "multi",
94 "seq(6435,59408,21665) set(2457,61697,4369), seq(1842,41590,51793)",
95 { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
96 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
97 0x2,0x3, 0x07,0x32, 0xa2,0x76, 0xca,0x51 },
98 24,
99 { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
100 "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
101 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
102 },
103 { /* 6 */
104 "confed",
105 "confseq(123,456,789)",
106 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15 },
107 8,
108 { "(123 456 789)",
109 "",
110 0, 3, NOT_ALL_PRIVATE, 789, 1, NULL_ASN },
111 },
112 { /* 7 */
113 "confed2",
114 "confseq(123,456,789) confseq(111,222)",
115 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
116 0x3,0x2, 0x00,0x6f, 0x00,0xde },
117 14,
118 { "(123 456 789) (111 222)",
119 "",
120 0, 5, NOT_ALL_PRIVATE, 111, 1, NULL_ASN },
121 },
122 { /* 8 */
123 "confset",
124 "confset(456,123,789)",
125 { 0x4,0x3, 0x01,0xc8, 0x00,0x7b, 0x03,0x15 },
126 8,
127 { "[123,456,789]",
128 "[123,456,789]",
129 0, 1, NOT_ALL_PRIVATE, 123, 1, NULL_ASN },
130 },
131 { /* 9 */
132 "confmulti",
133 "confseq(123,456,789) confset(222,111) seq(8722) set(4196,48658)",
134 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
135 0x4,0x2, 0x00,0xde, 0x00,0x6f,
136 0x2,0x1, 0x22,0x12,
137 0x1,0x2, 0x10,0x64, 0xbe,0x12 },
138 24,
139 { "(123 456 789) [111,222] 8722 {4196,48658}",
140 "8722 {4196,48658}",
141 2, 4, NOT_ALL_PRIVATE, 123, 1, NULL_ASN },
142 },
143 { /* 10 */
144 "seq4",
145 "seq(8466,2,52737,4096,8722,4)",
146 { 0x2,0x6, 0x21,0x12, 0x00,0x02, 0xce,0x01, 0x10,0x00,
147 0x22,0x12, 0x00,0x04},
148 14,
149 { "8466 2 52737 4096 8722 4",
150 "8466 2 52737 4096 8722 4",
151 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
152 },
153 { /* 11 */
154 "tripleseq1",
155 "seq(8466,2,52737) seq(4096,8722,4) seq(8722)",
156 { 0x2,0x3, 0x21,0x12, 0x00,0x02, 0xce,0x01,
157 0x2,0x3, 0x10,0x00, 0x22,0x12, 0x00,0x04,
158 0x2,0x1, 0x22,0x12},
159 20,
160 { "8466 2 52737 4096 8722 4 8722",
161 "8466 2 52737 4096 8722 4 8722",
162 7, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
163 },
164 { /* 12 */
165 "someprivate",
166 "seq(8466,64512,52737,65535)",
167 { 0x2,0x4, 0x21,0x12, 0xfc,0x00, 0xce,0x01, 0xff,0xff },
168 10,
169 { "8466 64512 52737 65535",
170 "8466 64512 52737 65535",
171 4, 0, NOT_ALL_PRIVATE, 65535, 4, 8466 },
172 },
173 { /* 13 */
174 "allprivate",
175 "seq(65534,64512,64513,65535)",
176 { 0x2,0x4, 0xff,0xfe, 0xfc,0x00, 0xfc,0x01, 0xff,0xff },
177 10,
178 { "65534 64512 64513 65535",
179 "65534 64512 64513 65535",
180 4, 0, ALL_PRIVATE, 65534, 4, 65534 },
181 },
182 { /* 14 */
183 "long",
184 "seq(8466,3,52737,4096,34285,<repeated 49 more times>)",
185 { 0x2,0xfa, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
186 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
187 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
188 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
189 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
190 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
191 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
192 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 502,
236 { "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
237 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
238 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
239 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
240 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
241 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
242 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
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
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 "
268 "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 250, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
288 },
paul59064762005-11-23 02:48:14 +0000289 { /* 15 */
290 "seq1extra",
291 "seq(8466,3,52737,4096,3456)",
292 { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
293 12,
294 { "8466 3 52737 4096 3456",
295 "8466 3 52737 4096 3456",
296 5, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
297 },
298 { /* 16 */
299 "empty",
300 "<empty>",
301 {},
302 0,
303 { "", "", 0, 0, 0, 0, 0, 0 },
304 },
paul1f377b02005-09-10 18:00:11 +0000305 { NULL, NULL, {0}, 0, { NULL, 0, 0 } }
306};
307
308/* prepending tests */
309static struct tests {
310 const struct test_segment *test1;
311 const struct test_segment *test2;
312 struct test_spec sp;
313} prepend_tests[] =
314{
315 { &test_segments[0], &test_segments[1],
316 { "8466 3 52737 4096 8722 4",
317 "8466 3 52737 4096 8722 4",
318 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
319 },
320 { &test_segments[1], &test_segments[3],
321 { "8722 4 8482 51457 {5204}",
322 "8722 4 8482 51457 {5204}",
323 5, 0, NOT_ALL_PRIVATE, 5204, 1, 8722 }
324 },
325 { &test_segments[3], &test_segments[4],
326 { "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
327 "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
328 7, 0, NOT_ALL_PRIVATE, 5204, 1, 8482 },
329 },
330 { &test_segments[4], &test_segments[5],
331 { "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
332 " {2457,4369,61697} 1842 41590 51793",
333 "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
334 " {2457,4369,61697} 1842 41590 51793",
335 11, 0, NOT_ALL_PRIVATE, 61697, 1, 8467 }
336 },
337 { &test_segments[5], &test_segments[6],
338 { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793 (123 456 789)",
339 "6435 59408 21665 {2457,4369,61697} 1842 41590 51793 (123 456 789)",
340 7, 3, NOT_ALL_PRIVATE, 123, 1, 6435 },
341 },
342 { &test_segments[6], &test_segments[7],
343 { "(123 456 789) (123 456 789) (111 222)",
344 "",
345 0, 8, NOT_ALL_PRIVATE, 111, 1, 0 }
346 },
347 { &test_segments[7], &test_segments[8],
348 { "(123 456 789) (111 222) [123,456,789]",
349 "",
350 0, 6, NOT_ALL_PRIVATE, 111, 1, 0 }
351 },
352 { &test_segments[8], &test_segments[9],
353 { "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
354 "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
355 2, 5, NOT_ALL_PRIVATE, 456, 1, NULL_ASN },
356 },
357 { &test_segments[9], &test_segments[8],
358 { "(123 456 789) [111,222] 8722 {4196,48658} [123,456,789]",
359 "8722 {4196,48658} [123,456,789]",
360 2, 5, NOT_ALL_PRIVATE, 48658, 1, NULL_ASN },
361 },
362 { &test_segments[14], &test_segments[11],
363 { "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
364 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
365 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
366 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
367 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
368 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
369 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
370 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
371 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
372 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
373 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
374 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
375 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
376 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
377 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
378 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
379 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
380 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
381 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
382 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
383 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
384 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
385 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
386 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
387 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
388 "8466 2 52737 4096 8722 4 8722",
389
390 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
391 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
392 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
393 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
394 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
395 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
396 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
397 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
398 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
399 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
400 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
401 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
402 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
403 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
404 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
405 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
406 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
407 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
408 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
409 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
410 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
411 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
412 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
413 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
414 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
415 "8466 2 52737 4096 8722 4 8722",
416 257, 0, NOT_ALL_PRIVATE, 4096, 1000, 8466 },
417 },
418 { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
419};
420
421struct tests aggregate_tests[] =
422{
423 { &test_segments[0], &test_segments[1],
424 { "{3,4,4096,8466,8722,52737}",
425 "{3,4,4096,8466,8722,52737}",
426 1, 0, NOT_ALL_PRIVATE, 52737, 1, NULL_ASN },
427 },
428 { &test_segments[0], &test_segments[2],
429 { "8466 3 52737 4096 {4,8722}",
430 "8466 3 52737 4096 {4,8722}",
431 5, 0, NOT_ALL_PRIVATE, 4, 1, 8466 },
432 },
433 { &test_segments[2], &test_segments[0],
434 { "8466 3 52737 4096 {4,8722}",
435 "8466 3 52737 4096 {4,8722}",
436 5, 0, NOT_ALL_PRIVATE, 8722, 1, 8466 },
437 },
438 { &test_segments[2], &test_segments[10],
439 { "8466 {2,3,4,4096,8722,52737}",
440 "8466 {2,3,4,4096,8722,52737}",
441 2, 0, NOT_ALL_PRIVATE, 8722, 5, 8466 },
442 },
443 { &test_segments[10], &test_segments[2],
444 { "8466 {2,3,4,4096,8722,52737}",
445 "8466 {2,3,4,4096,8722,52737}",
446 2, 0, NOT_ALL_PRIVATE, 2, 20000, 8466 },
447 },
448 { NULL, NULL, { NULL, 0, 0} },
449};
450
451struct compare_tests
452{
453 int test_index1;
454 int test_index2;
455#define CMP_RES_YES 1
456#define CMP_RES_NO 0
457 char shouldbe_cmp;
458 char shouldbe_confed;
459} left_compare [] =
460{
461 { 0, 1, CMP_RES_NO, CMP_RES_NO },
462 { 0, 2, CMP_RES_YES, CMP_RES_NO },
463 { 0, 11, CMP_RES_YES, CMP_RES_NO },
paul59064762005-11-23 02:48:14 +0000464 { 0, 15, CMP_RES_YES, CMP_RES_NO },
465 { 0, 16, CMP_RES_NO, CMP_RES_NO },
paul1f377b02005-09-10 18:00:11 +0000466 { 1, 11, CMP_RES_NO, CMP_RES_NO },
467 { 6, 7, CMP_RES_NO, CMP_RES_YES },
468 { 6, 8, CMP_RES_NO, CMP_RES_NO },
469 { 7, 8, CMP_RES_NO, CMP_RES_NO },
470 { 1, 9, CMP_RES_YES, CMP_RES_NO },
471 { 0, 9, CMP_RES_NO, CMP_RES_NO },
472 { 3, 9, CMP_RES_NO, CMP_RES_NO },
473 { 0, 6, CMP_RES_NO, CMP_RES_NO },
474 { 1, 6, CMP_RES_NO, CMP_RES_NO },
475 { 0, 8, CMP_RES_NO, CMP_RES_NO },
476 { 1, 8, CMP_RES_NO, CMP_RES_NO },
477 { 11, 6, CMP_RES_NO, CMP_RES_NO },
478 { 11, 7, CMP_RES_NO, CMP_RES_NO },
479 { 11, 8, CMP_RES_NO, CMP_RES_NO },
480 { 9, 6, CMP_RES_NO, CMP_RES_YES },
481 { 9, 7, CMP_RES_NO, CMP_RES_YES },
482 { 9, 8, CMP_RES_NO, CMP_RES_NO },
483};
484
485/* make an aspath from a data stream */
486static struct aspath *
487make_aspath (const u_char *data, size_t len)
488{
489 struct stream *s = NULL;
490 struct aspath *as;
491
492 if (len)
493 {
494 s = stream_new (len);
495 stream_put (s, data, len);
496 }
497 as = aspath_parse (s, len);
paul59064762005-11-23 02:48:14 +0000498
499 if (s)
500 stream_free (s);
paul1f377b02005-09-10 18:00:11 +0000501
502 return as;
503}
504
505static void
506printbytes (const u_char *bytes, int len)
507{
508 int i = 0;
509 while (i < len)
510 {
511 if (i % 2)
512 printf ("%02hhx%s", bytes[i], " ");
513 else
514 printf ("0x%02hhx", bytes[i]);
515 i++;
516 }
517 printf ("\n");
518}
519
520/* validate the given aspath */
521static int
522validate (struct aspath *as, const struct test_spec *sp)
523{
524 size_t bytes;
525 int fails = 0;
526 const u_char *out;
527 struct aspath *asinout, *asconfeddel, *asstr;
528
529 out = aspath_snmp_pathseg (as, &bytes);
530 asinout = make_aspath (out, bytes);
531
532 asstr = aspath_str2aspath (sp->shouldbe);
533
534 asconfeddel = aspath_delete_confed_seq (aspath_dup (asinout));
535
536 /* the parsed path should match the specified 'shouldbe' string.
537 * We should pass the "eat our own dog food" test, be able to output
538 * this path and then input it again. Ie the path resulting from:
539 *
540 * aspath_parse(aspath_put(as))
541 *
542 * should:
543 *
544 * - also match the specified 'shouldbe' value
545 * - hash to same value as original path
546 * - have same hops and confed counts as original, and as the
547 * the specified counts
548 *
549 * aspath_str2aspath() and shouldbe should match
550 *
551 * Confederation related tests:
552 * - aspath_delete_confed_seq(aspath) should match shouldbe_confed
553 * - aspath_delete_confed_seq should be idempotent.
554 */
555 if (strcmp(aspath_print (as), sp->shouldbe)
556 /* hash validation */
557 || (aspath_key_make (as) != aspath_key_make (asinout))
558 /* by string */
559 || strcmp(aspath_print (asinout), sp->shouldbe)
560 /* by various path counts */
561 || (aspath_count_hops (as) != sp->hops)
562 || (aspath_count_confeds (as) != sp->confeds)
563 || (aspath_count_hops (asinout) != sp->hops)
564 || (aspath_count_confeds (asinout) != sp->confeds))
565 {
566 failed++;
567 fails++;
568 printf ("shouldbe:\n%s\n", sp->shouldbe);
569 printf ("got:\n%s\n", aspath_print(as));
570 printf ("hash keys: in: %d out->in: %d\n",
571 aspath_key_make (as), aspath_key_make (asinout));
572 printf ("hops: %d, counted %d %d\n", sp->hops,
573 aspath_count_hops (as),
574 aspath_count_hops (asinout) );
575 printf ("confeds: %d, counted %d %d\n", sp->confeds,
576 aspath_count_confeds (as),
577 aspath_count_confeds (asinout));
578 printf ("out->in:\n%s\nbytes: ", aspath_print(asinout));
579 printbytes (out, bytes);
580 }
581 /* basic confed related tests */
582 if ((aspath_print (asconfeddel) == NULL
583 && sp->shouldbe_delete_confed != NULL)
584 || (aspath_print (asconfeddel) != NULL
585 && sp->shouldbe_delete_confed == NULL)
586 || strcmp(aspath_print (asconfeddel), sp->shouldbe_delete_confed)
587 /* delete_confed_seq should be idempotent */
588 || (aspath_key_make (asconfeddel)
589 != aspath_key_make (aspath_delete_confed_seq (asconfeddel))))
590 {
591 failed++;
592 fails++;
593 printf ("confed_del: %s\n", aspath_print (asconfeddel));
594 printf ("should be: %s\n", sp->shouldbe_delete_confed);
595 }
596 /* aspath_str2aspath test */
597 if ((aspath_print (asstr) == NULL && sp->shouldbe != NULL)
598 || (aspath_print (asstr) != NULL && sp->shouldbe == NULL)
599 || strcmp(aspath_print (asstr), sp->shouldbe))
600 {
601 failed++;
602 fails++;
603 printf ("asstr: %s\n", aspath_print (asstr));
604 }
605
606 /* loop, private and first as checks */
607 if (aspath_loop_check (as, sp->does_loop)
608 || aspath_loop_check (as, sp->doesnt_loop)
609 || (aspath_private_as_check (as) != sp->private_as)
610 || (aspath_firstas_check (as,sp->first)
611 && sp->first == 0))
612 {
613 failed++;
614 fails++;
615 printf ("firstas: %d, got %d\n", sp->first,
616 aspath_firstas_check (as,sp->first));
617 printf ("loop does: %d %d, doesnt: %d %d\n",
618 sp->does_loop, aspath_loop_check (as, sp->does_loop),
619 sp->doesnt_loop, aspath_loop_check (as, sp->doesnt_loop));
620 printf ("private check: %d %d\n", sp->private_as,
621 aspath_private_as_check (as));
622 }
623 aspath_unintern (asinout);
paul6726f952006-01-16 12:06:00 +0000624 aspath_free (asconfeddel);
625 aspath_free (asstr);
626
paul1f377b02005-09-10 18:00:11 +0000627 return fails;
628
629}
630
631static void
632empty_get_test ()
633{
634 struct aspath *as = aspath_empty_get ();
635 struct test_spec sp = { "", "", 0, 0, 0, 0, 0, 0 };
636
637 printf ("empty_get_test, as: %s\n",aspath_print (as));
638 if (!validate (as, &sp))
639 printf ("OK\n");
640 else
641 printf ("failed!\n");
642
643 printf ("\n");
paul6726f952006-01-16 12:06:00 +0000644
645 aspath_free (as);
paul1f377b02005-09-10 18:00:11 +0000646}
647
648/* basic parsing test */
649static void
650parse_test (struct test_segment *t)
651{
652 struct aspath *asp;
653
654 printf ("%s: %s\n", t->name, t->desc);
655
656 asp = make_aspath (t->asdata, t->len);
657
658 printf ("aspath: %s\nvalidating...:\n", aspath_print (asp));
659
660 if (!validate (asp, &t->sp))
661 printf ("OK\n");
662 else
663 printf ("failed\n");
664
665 printf ("\n");
666 aspath_unintern (asp);
667}
668
669/* prepend testing */
670void
671prepend_test (struct tests *t)
672{
673 struct aspath *asp1, *asp2, *ascratch;
674
675 printf ("prepend %s: %s\n", t->test1->name, t->test1->desc);
676 printf ("to %s: %s\n", t->test2->name, t->test2->desc);
677
678 asp1 = make_aspath (t->test1->asdata, t->test1->len);
679 asp2 = make_aspath (t->test2->asdata, t->test2->len);
680
681 ascratch = aspath_dup (asp2);
682 aspath_unintern (asp2);
683
684 asp2 = aspath_prepend (asp1, ascratch);
685
686 printf ("aspath: %s\n", aspath_print (asp2));
687
688 if (!validate (asp2, &t->sp))
689 printf ("OK\n");
690 else
691 printf ("failed!\n");
692
693 printf ("\n");
694 aspath_unintern (asp1);
695 aspath_free (asp2);
696}
697
698/* empty-prepend testing */
699void
700empty_prepend_test (struct test_segment *t)
701{
702 struct aspath *asp1, *asp2, *ascratch;
703
704 printf ("empty prepend %s: %s\n", t->name, t->desc);
705
706 asp1 = make_aspath (t->asdata, t->len);
707 asp2 = aspath_empty ();
708
709 ascratch = aspath_dup (asp2);
710 aspath_unintern (asp2);
711
712 asp2 = aspath_prepend (asp1, ascratch);
713
714 printf ("aspath: %s\n", aspath_print (asp2));
715
716 if (!validate (asp2, &t->sp))
717 printf ("OK\n");
718 else
719 printf ("failed!\n");
720
721 printf ("\n");
722 aspath_unintern (asp1);
723 aspath_free (asp2);
724}
725
726/* aggregation testing */
727void
728aggregate_test (struct tests *t)
729{
730 struct aspath *asp1, *asp2, *ascratch;
731
732 printf ("aggregate %s: %s\n", t->test1->name, t->test1->desc);
733 printf ("with %s: %s\n", t->test2->name, t->test2->desc);
734
735 asp1 = make_aspath (t->test1->asdata, t->test1->len);
736 asp2 = make_aspath (t->test2->asdata, t->test2->len);
737
738 ascratch = aspath_aggregate (asp1, asp2);
739
740 printf ("aspath: %s\n", aspath_print (ascratch));
741
742 if (!validate (ascratch, &t->sp))
743 printf ("OK\n");
744 else
745 printf ("failed!\n");
746
747 printf ("\n");
748 aspath_unintern (asp1);
749 aspath_unintern (asp2);
750 aspath_free (ascratch);
751/* aspath_unintern (ascratch);*/
752}
753
754/* cmp_left tests */
755static void
756cmp_test ()
757{
paul84771ee2005-10-11 03:48:28 +0000758 unsigned int i;
paul1f377b02005-09-10 18:00:11 +0000759#define CMP_TESTS_MAX \
760 (sizeof(left_compare) / sizeof (struct compare_tests))
761
762 for (i = 0; i < CMP_TESTS_MAX; i++)
763 {
764 struct test_segment *t1 = &test_segments[left_compare[i].test_index1];
765 struct test_segment *t2 = &test_segments[left_compare[i].test_index2];
766 struct aspath *asp1, *asp2;
767
768 printf ("left cmp %s: %s\n", t1->name, t1->desc);
769 printf ("and %s: %s\n", t2->name, t2->desc);
770
771 asp1 = make_aspath (t1->asdata, t1->len);
772 asp2 = make_aspath (t2->asdata, t2->len);
773
774 if (aspath_cmp_left (asp1, asp2) != left_compare[i].shouldbe_cmp
775 || aspath_cmp_left (asp2, asp1) != left_compare[i].shouldbe_cmp
776 || aspath_cmp_left_confed (asp1, asp2)
777 != left_compare[i].shouldbe_confed
778 || aspath_cmp_left_confed (asp2, asp1)
779 != left_compare[i].shouldbe_confed)
780 {
781 printf ("failed\n");
782 printf ("result should be: cmp: %d, confed: %d\n",
783 left_compare[i].shouldbe_cmp,
784 left_compare[i].shouldbe_confed);
785 printf ("got: cmp %d, cmp_confed: %d\n",
786 aspath_cmp_left (asp1, asp2),
787 aspath_cmp_left_confed (asp1, asp2));
788 printf("path1: %s\npath2: %s\n", aspath_print (asp1),
789 aspath_print (asp2));
790 failed++;
791 }
792 else
793 printf ("OK\n");
794
795 printf ("\n");
796 aspath_unintern (asp1);
797 aspath_unintern (asp2);
798 }
799}
800
801int
802main (void)
803{
804 int i = 0;
805 aspath_init();
806 while (test_segments[i].name)
807 {
808 parse_test (&test_segments[i]);
809 empty_prepend_test (&test_segments[i++]);
810 }
811
812 i = 0;
813 while (prepend_tests[i].test1)
814 prepend_test (&prepend_tests[i++]);
815
816 i = 0;
817 while (aggregate_tests[i].test1)
818 aggregate_test (&aggregate_tests[i++]);
819
820 cmp_test();
821
822 i = 0;
823
824 empty_get_test();
825
826 printf ("failures: %d\n", failed);
827 printf ("aspath count: %ld\n", aspath_count());
828
829 return (failed + aspath_count());
830}