blob: de67990b00461b1c7075ac05f5091f200db24a2f [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);
624 /* aspath_unintern (asconfeddel);*/
625 return fails;
626
627}
628
629static void
630empty_get_test ()
631{
632 struct aspath *as = aspath_empty_get ();
633 struct test_spec sp = { "", "", 0, 0, 0, 0, 0, 0 };
634
635 printf ("empty_get_test, as: %s\n",aspath_print (as));
636 if (!validate (as, &sp))
637 printf ("OK\n");
638 else
639 printf ("failed!\n");
640
641 printf ("\n");
642}
643
644/* basic parsing test */
645static void
646parse_test (struct test_segment *t)
647{
648 struct aspath *asp;
649
650 printf ("%s: %s\n", t->name, t->desc);
651
652 asp = make_aspath (t->asdata, t->len);
653
654 printf ("aspath: %s\nvalidating...:\n", aspath_print (asp));
655
656 if (!validate (asp, &t->sp))
657 printf ("OK\n");
658 else
659 printf ("failed\n");
660
661 printf ("\n");
662 aspath_unintern (asp);
663}
664
665/* prepend testing */
666void
667prepend_test (struct tests *t)
668{
669 struct aspath *asp1, *asp2, *ascratch;
670
671 printf ("prepend %s: %s\n", t->test1->name, t->test1->desc);
672 printf ("to %s: %s\n", t->test2->name, t->test2->desc);
673
674 asp1 = make_aspath (t->test1->asdata, t->test1->len);
675 asp2 = make_aspath (t->test2->asdata, t->test2->len);
676
677 ascratch = aspath_dup (asp2);
678 aspath_unintern (asp2);
679
680 asp2 = aspath_prepend (asp1, ascratch);
681
682 printf ("aspath: %s\n", aspath_print (asp2));
683
684 if (!validate (asp2, &t->sp))
685 printf ("OK\n");
686 else
687 printf ("failed!\n");
688
689 printf ("\n");
690 aspath_unintern (asp1);
691 aspath_free (asp2);
692}
693
694/* empty-prepend testing */
695void
696empty_prepend_test (struct test_segment *t)
697{
698 struct aspath *asp1, *asp2, *ascratch;
699
700 printf ("empty prepend %s: %s\n", t->name, t->desc);
701
702 asp1 = make_aspath (t->asdata, t->len);
703 asp2 = aspath_empty ();
704
705 ascratch = aspath_dup (asp2);
706 aspath_unintern (asp2);
707
708 asp2 = aspath_prepend (asp1, ascratch);
709
710 printf ("aspath: %s\n", aspath_print (asp2));
711
712 if (!validate (asp2, &t->sp))
713 printf ("OK\n");
714 else
715 printf ("failed!\n");
716
717 printf ("\n");
718 aspath_unintern (asp1);
719 aspath_free (asp2);
720}
721
722/* aggregation testing */
723void
724aggregate_test (struct tests *t)
725{
726 struct aspath *asp1, *asp2, *ascratch;
727
728 printf ("aggregate %s: %s\n", t->test1->name, t->test1->desc);
729 printf ("with %s: %s\n", t->test2->name, t->test2->desc);
730
731 asp1 = make_aspath (t->test1->asdata, t->test1->len);
732 asp2 = make_aspath (t->test2->asdata, t->test2->len);
733
734 ascratch = aspath_aggregate (asp1, asp2);
735
736 printf ("aspath: %s\n", aspath_print (ascratch));
737
738 if (!validate (ascratch, &t->sp))
739 printf ("OK\n");
740 else
741 printf ("failed!\n");
742
743 printf ("\n");
744 aspath_unintern (asp1);
745 aspath_unintern (asp2);
746 aspath_free (ascratch);
747/* aspath_unintern (ascratch);*/
748}
749
750/* cmp_left tests */
751static void
752cmp_test ()
753{
paul84771ee2005-10-11 03:48:28 +0000754 unsigned int i;
paul1f377b02005-09-10 18:00:11 +0000755#define CMP_TESTS_MAX \
756 (sizeof(left_compare) / sizeof (struct compare_tests))
757
758 for (i = 0; i < CMP_TESTS_MAX; i++)
759 {
760 struct test_segment *t1 = &test_segments[left_compare[i].test_index1];
761 struct test_segment *t2 = &test_segments[left_compare[i].test_index2];
762 struct aspath *asp1, *asp2;
763
764 printf ("left cmp %s: %s\n", t1->name, t1->desc);
765 printf ("and %s: %s\n", t2->name, t2->desc);
766
767 asp1 = make_aspath (t1->asdata, t1->len);
768 asp2 = make_aspath (t2->asdata, t2->len);
769
770 if (aspath_cmp_left (asp1, asp2) != left_compare[i].shouldbe_cmp
771 || aspath_cmp_left (asp2, asp1) != left_compare[i].shouldbe_cmp
772 || aspath_cmp_left_confed (asp1, asp2)
773 != left_compare[i].shouldbe_confed
774 || aspath_cmp_left_confed (asp2, asp1)
775 != left_compare[i].shouldbe_confed)
776 {
777 printf ("failed\n");
778 printf ("result should be: cmp: %d, confed: %d\n",
779 left_compare[i].shouldbe_cmp,
780 left_compare[i].shouldbe_confed);
781 printf ("got: cmp %d, cmp_confed: %d\n",
782 aspath_cmp_left (asp1, asp2),
783 aspath_cmp_left_confed (asp1, asp2));
784 printf("path1: %s\npath2: %s\n", aspath_print (asp1),
785 aspath_print (asp2));
786 failed++;
787 }
788 else
789 printf ("OK\n");
790
791 printf ("\n");
792 aspath_unintern (asp1);
793 aspath_unintern (asp2);
794 }
795}
796
797int
798main (void)
799{
800 int i = 0;
801 aspath_init();
802 while (test_segments[i].name)
803 {
804 parse_test (&test_segments[i]);
805 empty_prepend_test (&test_segments[i++]);
806 }
807
808 i = 0;
809 while (prepend_tests[i].test1)
810 prepend_test (&prepend_tests[i++]);
811
812 i = 0;
813 while (aggregate_tests[i].test1)
814 aggregate_test (&aggregate_tests[i++]);
815
816 cmp_test();
817
818 i = 0;
819
820 empty_get_test();
821
822 printf ("failures: %d\n", failed);
823 printf ("aspath count: %ld\n", aspath_count());
824
825 return (failed + aspath_count());
826}