blob: 5d510ccac38818e52520ad6cef046cae6d46e120 [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 },
Paul Jakma5d932f62006-03-30 13:42:50 +0000305 { /* 17 */
306 "redundantset",
307 "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153)",
308 { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
309 0x1,0x4, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9 },
310 22,
311 {
312 /* We shouldn't ever /generate/ such paths. However, we should
313 * cope with them fine.
314 */
315 "8466 3 52737 4096 3456 {7099,8153,8153,8153}",
316 "8466 3 52737 4096 3456 {7099,8153,8153,8153}",
317 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
318 },
paul1f377b02005-09-10 18:00:11 +0000319 { NULL, NULL, {0}, 0, { NULL, 0, 0 } }
320};
321
322/* prepending tests */
323static struct tests {
324 const struct test_segment *test1;
325 const struct test_segment *test2;
326 struct test_spec sp;
327} prepend_tests[] =
328{
329 { &test_segments[0], &test_segments[1],
330 { "8466 3 52737 4096 8722 4",
331 "8466 3 52737 4096 8722 4",
332 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
333 },
334 { &test_segments[1], &test_segments[3],
335 { "8722 4 8482 51457 {5204}",
336 "8722 4 8482 51457 {5204}",
337 5, 0, NOT_ALL_PRIVATE, 5204, 1, 8722 }
338 },
339 { &test_segments[3], &test_segments[4],
340 { "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
341 "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
342 7, 0, NOT_ALL_PRIVATE, 5204, 1, 8482 },
343 },
344 { &test_segments[4], &test_segments[5],
345 { "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
346 " {2457,4369,61697} 1842 41590 51793",
347 "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
348 " {2457,4369,61697} 1842 41590 51793",
349 11, 0, NOT_ALL_PRIVATE, 61697, 1, 8467 }
350 },
351 { &test_segments[5], &test_segments[6],
352 { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793 (123 456 789)",
353 "6435 59408 21665 {2457,4369,61697} 1842 41590 51793 (123 456 789)",
354 7, 3, NOT_ALL_PRIVATE, 123, 1, 6435 },
355 },
356 { &test_segments[6], &test_segments[7],
357 { "(123 456 789) (123 456 789) (111 222)",
358 "",
359 0, 8, NOT_ALL_PRIVATE, 111, 1, 0 }
360 },
361 { &test_segments[7], &test_segments[8],
362 { "(123 456 789) (111 222) [123,456,789]",
363 "",
364 0, 6, NOT_ALL_PRIVATE, 111, 1, 0 }
365 },
366 { &test_segments[8], &test_segments[9],
367 { "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
368 "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
369 2, 5, NOT_ALL_PRIVATE, 456, 1, NULL_ASN },
370 },
371 { &test_segments[9], &test_segments[8],
372 { "(123 456 789) [111,222] 8722 {4196,48658} [123,456,789]",
373 "8722 {4196,48658} [123,456,789]",
374 2, 5, NOT_ALL_PRIVATE, 48658, 1, NULL_ASN },
375 },
376 { &test_segments[14], &test_segments[11],
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 3 52737 4096 34285 8466 3 52737 4096 34285 "
389 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
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 2 52737 4096 8722 4 8722",
403
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 3 52737 4096 34285 8466 3 52737 4096 34285 "
416 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
417 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
418 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
419 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
420 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
421 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
422 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
423 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
424 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
425 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
426 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
427 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
428 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
429 "8466 2 52737 4096 8722 4 8722",
430 257, 0, NOT_ALL_PRIVATE, 4096, 1000, 8466 },
431 },
432 { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
433};
434
435struct tests aggregate_tests[] =
436{
437 { &test_segments[0], &test_segments[1],
438 { "{3,4,4096,8466,8722,52737}",
439 "{3,4,4096,8466,8722,52737}",
440 1, 0, NOT_ALL_PRIVATE, 52737, 1, NULL_ASN },
441 },
442 { &test_segments[0], &test_segments[2],
443 { "8466 3 52737 4096 {4,8722}",
444 "8466 3 52737 4096 {4,8722}",
445 5, 0, NOT_ALL_PRIVATE, 4, 1, 8466 },
446 },
447 { &test_segments[2], &test_segments[0],
448 { "8466 3 52737 4096 {4,8722}",
449 "8466 3 52737 4096 {4,8722}",
450 5, 0, NOT_ALL_PRIVATE, 8722, 1, 8466 },
451 },
452 { &test_segments[2], &test_segments[10],
453 { "8466 {2,3,4,4096,8722,52737}",
454 "8466 {2,3,4,4096,8722,52737}",
455 2, 0, NOT_ALL_PRIVATE, 8722, 5, 8466 },
456 },
457 { &test_segments[10], &test_segments[2],
458 { "8466 {2,3,4,4096,8722,52737}",
459 "8466 {2,3,4,4096,8722,52737}",
460 2, 0, NOT_ALL_PRIVATE, 2, 20000, 8466 },
461 },
462 { NULL, NULL, { NULL, 0, 0} },
463};
464
465struct compare_tests
466{
467 int test_index1;
468 int test_index2;
469#define CMP_RES_YES 1
470#define CMP_RES_NO 0
471 char shouldbe_cmp;
472 char shouldbe_confed;
473} left_compare [] =
474{
475 { 0, 1, CMP_RES_NO, CMP_RES_NO },
476 { 0, 2, CMP_RES_YES, CMP_RES_NO },
477 { 0, 11, CMP_RES_YES, CMP_RES_NO },
paul59064762005-11-23 02:48:14 +0000478 { 0, 15, CMP_RES_YES, CMP_RES_NO },
479 { 0, 16, CMP_RES_NO, CMP_RES_NO },
paul1f377b02005-09-10 18:00:11 +0000480 { 1, 11, CMP_RES_NO, CMP_RES_NO },
481 { 6, 7, CMP_RES_NO, CMP_RES_YES },
482 { 6, 8, CMP_RES_NO, CMP_RES_NO },
483 { 7, 8, CMP_RES_NO, CMP_RES_NO },
484 { 1, 9, CMP_RES_YES, CMP_RES_NO },
485 { 0, 9, CMP_RES_NO, CMP_RES_NO },
486 { 3, 9, CMP_RES_NO, CMP_RES_NO },
487 { 0, 6, CMP_RES_NO, CMP_RES_NO },
488 { 1, 6, CMP_RES_NO, CMP_RES_NO },
489 { 0, 8, CMP_RES_NO, CMP_RES_NO },
490 { 1, 8, CMP_RES_NO, CMP_RES_NO },
491 { 11, 6, CMP_RES_NO, CMP_RES_NO },
492 { 11, 7, CMP_RES_NO, CMP_RES_NO },
493 { 11, 8, CMP_RES_NO, CMP_RES_NO },
494 { 9, 6, CMP_RES_NO, CMP_RES_YES },
495 { 9, 7, CMP_RES_NO, CMP_RES_YES },
496 { 9, 8, CMP_RES_NO, CMP_RES_NO },
497};
498
499/* make an aspath from a data stream */
500static struct aspath *
501make_aspath (const u_char *data, size_t len)
502{
503 struct stream *s = NULL;
504 struct aspath *as;
505
506 if (len)
507 {
508 s = stream_new (len);
509 stream_put (s, data, len);
510 }
511 as = aspath_parse (s, len);
paul59064762005-11-23 02:48:14 +0000512
513 if (s)
514 stream_free (s);
paul1f377b02005-09-10 18:00:11 +0000515
516 return as;
517}
518
519static void
520printbytes (const u_char *bytes, int len)
521{
522 int i = 0;
523 while (i < len)
524 {
525 if (i % 2)
526 printf ("%02hhx%s", bytes[i], " ");
527 else
528 printf ("0x%02hhx", bytes[i]);
529 i++;
530 }
531 printf ("\n");
532}
533
534/* validate the given aspath */
535static int
536validate (struct aspath *as, const struct test_spec *sp)
537{
538 size_t bytes;
539 int fails = 0;
540 const u_char *out;
541 struct aspath *asinout, *asconfeddel, *asstr;
542
543 out = aspath_snmp_pathseg (as, &bytes);
544 asinout = make_aspath (out, bytes);
545
546 asstr = aspath_str2aspath (sp->shouldbe);
547
548 asconfeddel = aspath_delete_confed_seq (aspath_dup (asinout));
549
550 /* the parsed path should match the specified 'shouldbe' string.
551 * We should pass the "eat our own dog food" test, be able to output
552 * this path and then input it again. Ie the path resulting from:
553 *
554 * aspath_parse(aspath_put(as))
555 *
556 * should:
557 *
558 * - also match the specified 'shouldbe' value
559 * - hash to same value as original path
560 * - have same hops and confed counts as original, and as the
561 * the specified counts
562 *
563 * aspath_str2aspath() and shouldbe should match
564 *
565 * Confederation related tests:
566 * - aspath_delete_confed_seq(aspath) should match shouldbe_confed
567 * - aspath_delete_confed_seq should be idempotent.
568 */
569 if (strcmp(aspath_print (as), sp->shouldbe)
570 /* hash validation */
571 || (aspath_key_make (as) != aspath_key_make (asinout))
572 /* by string */
573 || strcmp(aspath_print (asinout), sp->shouldbe)
574 /* by various path counts */
575 || (aspath_count_hops (as) != sp->hops)
576 || (aspath_count_confeds (as) != sp->confeds)
577 || (aspath_count_hops (asinout) != sp->hops)
578 || (aspath_count_confeds (asinout) != sp->confeds))
579 {
580 failed++;
581 fails++;
582 printf ("shouldbe:\n%s\n", sp->shouldbe);
583 printf ("got:\n%s\n", aspath_print(as));
584 printf ("hash keys: in: %d out->in: %d\n",
585 aspath_key_make (as), aspath_key_make (asinout));
586 printf ("hops: %d, counted %d %d\n", sp->hops,
587 aspath_count_hops (as),
588 aspath_count_hops (asinout) );
589 printf ("confeds: %d, counted %d %d\n", sp->confeds,
590 aspath_count_confeds (as),
591 aspath_count_confeds (asinout));
592 printf ("out->in:\n%s\nbytes: ", aspath_print(asinout));
593 printbytes (out, bytes);
594 }
595 /* basic confed related tests */
596 if ((aspath_print (asconfeddel) == NULL
597 && sp->shouldbe_delete_confed != NULL)
598 || (aspath_print (asconfeddel) != NULL
599 && sp->shouldbe_delete_confed == NULL)
600 || strcmp(aspath_print (asconfeddel), sp->shouldbe_delete_confed)
601 /* delete_confed_seq should be idempotent */
602 || (aspath_key_make (asconfeddel)
603 != aspath_key_make (aspath_delete_confed_seq (asconfeddel))))
604 {
605 failed++;
606 fails++;
607 printf ("confed_del: %s\n", aspath_print (asconfeddel));
608 printf ("should be: %s\n", sp->shouldbe_delete_confed);
609 }
610 /* aspath_str2aspath test */
611 if ((aspath_print (asstr) == NULL && sp->shouldbe != NULL)
612 || (aspath_print (asstr) != NULL && sp->shouldbe == NULL)
613 || strcmp(aspath_print (asstr), sp->shouldbe))
614 {
615 failed++;
616 fails++;
617 printf ("asstr: %s\n", aspath_print (asstr));
618 }
619
620 /* loop, private and first as checks */
621 if (aspath_loop_check (as, sp->does_loop)
622 || aspath_loop_check (as, sp->doesnt_loop)
623 || (aspath_private_as_check (as) != sp->private_as)
624 || (aspath_firstas_check (as,sp->first)
625 && sp->first == 0))
626 {
627 failed++;
628 fails++;
629 printf ("firstas: %d, got %d\n", sp->first,
630 aspath_firstas_check (as,sp->first));
631 printf ("loop does: %d %d, doesnt: %d %d\n",
632 sp->does_loop, aspath_loop_check (as, sp->does_loop),
633 sp->doesnt_loop, aspath_loop_check (as, sp->doesnt_loop));
634 printf ("private check: %d %d\n", sp->private_as,
635 aspath_private_as_check (as));
636 }
637 aspath_unintern (asinout);
paul6726f952006-01-16 12:06:00 +0000638 aspath_free (asconfeddel);
639 aspath_free (asstr);
640
paul1f377b02005-09-10 18:00:11 +0000641 return fails;
642
643}
644
645static void
646empty_get_test ()
647{
648 struct aspath *as = aspath_empty_get ();
649 struct test_spec sp = { "", "", 0, 0, 0, 0, 0, 0 };
650
651 printf ("empty_get_test, as: %s\n",aspath_print (as));
652 if (!validate (as, &sp))
653 printf ("OK\n");
654 else
655 printf ("failed!\n");
656
657 printf ("\n");
paul6726f952006-01-16 12:06:00 +0000658
659 aspath_free (as);
paul1f377b02005-09-10 18:00:11 +0000660}
661
662/* basic parsing test */
663static void
664parse_test (struct test_segment *t)
665{
666 struct aspath *asp;
667
668 printf ("%s: %s\n", t->name, t->desc);
669
670 asp = make_aspath (t->asdata, t->len);
671
672 printf ("aspath: %s\nvalidating...:\n", aspath_print (asp));
673
674 if (!validate (asp, &t->sp))
675 printf ("OK\n");
676 else
677 printf ("failed\n");
678
679 printf ("\n");
680 aspath_unintern (asp);
681}
682
683/* prepend testing */
Paul Jakma5d932f62006-03-30 13:42:50 +0000684static void
paul1f377b02005-09-10 18:00:11 +0000685prepend_test (struct tests *t)
686{
687 struct aspath *asp1, *asp2, *ascratch;
688
689 printf ("prepend %s: %s\n", t->test1->name, t->test1->desc);
690 printf ("to %s: %s\n", t->test2->name, t->test2->desc);
691
692 asp1 = make_aspath (t->test1->asdata, t->test1->len);
693 asp2 = make_aspath (t->test2->asdata, t->test2->len);
694
695 ascratch = aspath_dup (asp2);
696 aspath_unintern (asp2);
697
698 asp2 = aspath_prepend (asp1, ascratch);
699
700 printf ("aspath: %s\n", aspath_print (asp2));
701
702 if (!validate (asp2, &t->sp))
703 printf ("OK\n");
704 else
705 printf ("failed!\n");
706
707 printf ("\n");
708 aspath_unintern (asp1);
709 aspath_free (asp2);
710}
711
712/* empty-prepend testing */
Paul Jakma5d932f62006-03-30 13:42:50 +0000713static void
paul1f377b02005-09-10 18:00:11 +0000714empty_prepend_test (struct test_segment *t)
715{
716 struct aspath *asp1, *asp2, *ascratch;
717
718 printf ("empty prepend %s: %s\n", t->name, t->desc);
719
720 asp1 = make_aspath (t->asdata, t->len);
721 asp2 = aspath_empty ();
722
723 ascratch = aspath_dup (asp2);
724 aspath_unintern (asp2);
725
726 asp2 = aspath_prepend (asp1, ascratch);
727
728 printf ("aspath: %s\n", aspath_print (asp2));
729
730 if (!validate (asp2, &t->sp))
731 printf ("OK\n");
732 else
733 printf ("failed!\n");
734
735 printf ("\n");
736 aspath_unintern (asp1);
737 aspath_free (asp2);
738}
739
740/* aggregation testing */
Paul Jakma5d932f62006-03-30 13:42:50 +0000741static void
paul1f377b02005-09-10 18:00:11 +0000742aggregate_test (struct tests *t)
743{
744 struct aspath *asp1, *asp2, *ascratch;
745
746 printf ("aggregate %s: %s\n", t->test1->name, t->test1->desc);
747 printf ("with %s: %s\n", t->test2->name, t->test2->desc);
748
749 asp1 = make_aspath (t->test1->asdata, t->test1->len);
750 asp2 = make_aspath (t->test2->asdata, t->test2->len);
751
752 ascratch = aspath_aggregate (asp1, asp2);
753
754 printf ("aspath: %s\n", aspath_print (ascratch));
755
756 if (!validate (ascratch, &t->sp))
757 printf ("OK\n");
758 else
759 printf ("failed!\n");
760
761 printf ("\n");
762 aspath_unintern (asp1);
763 aspath_unintern (asp2);
764 aspath_free (ascratch);
765/* aspath_unintern (ascratch);*/
766}
767
768/* cmp_left tests */
769static void
770cmp_test ()
771{
paul84771ee2005-10-11 03:48:28 +0000772 unsigned int i;
paul1f377b02005-09-10 18:00:11 +0000773#define CMP_TESTS_MAX \
774 (sizeof(left_compare) / sizeof (struct compare_tests))
775
776 for (i = 0; i < CMP_TESTS_MAX; i++)
777 {
778 struct test_segment *t1 = &test_segments[left_compare[i].test_index1];
779 struct test_segment *t2 = &test_segments[left_compare[i].test_index2];
780 struct aspath *asp1, *asp2;
781
782 printf ("left cmp %s: %s\n", t1->name, t1->desc);
783 printf ("and %s: %s\n", t2->name, t2->desc);
784
785 asp1 = make_aspath (t1->asdata, t1->len);
786 asp2 = make_aspath (t2->asdata, t2->len);
787
788 if (aspath_cmp_left (asp1, asp2) != left_compare[i].shouldbe_cmp
789 || aspath_cmp_left (asp2, asp1) != left_compare[i].shouldbe_cmp
790 || aspath_cmp_left_confed (asp1, asp2)
791 != left_compare[i].shouldbe_confed
792 || aspath_cmp_left_confed (asp2, asp1)
793 != left_compare[i].shouldbe_confed)
794 {
795 printf ("failed\n");
796 printf ("result should be: cmp: %d, confed: %d\n",
797 left_compare[i].shouldbe_cmp,
798 left_compare[i].shouldbe_confed);
799 printf ("got: cmp %d, cmp_confed: %d\n",
800 aspath_cmp_left (asp1, asp2),
801 aspath_cmp_left_confed (asp1, asp2));
802 printf("path1: %s\npath2: %s\n", aspath_print (asp1),
803 aspath_print (asp2));
804 failed++;
805 }
806 else
807 printf ("OK\n");
808
809 printf ("\n");
810 aspath_unintern (asp1);
811 aspath_unintern (asp2);
812 }
813}
814
815int
816main (void)
817{
818 int i = 0;
819 aspath_init();
820 while (test_segments[i].name)
821 {
822 parse_test (&test_segments[i]);
823 empty_prepend_test (&test_segments[i++]);
824 }
825
826 i = 0;
827 while (prepend_tests[i].test1)
828 prepend_test (&prepend_tests[i++]);
829
830 i = 0;
831 while (aggregate_tests[i].test1)
832 aggregate_test (&aggregate_tests[i++]);
833
834 cmp_test();
835
836 i = 0;
837
838 empty_get_test();
839
840 printf ("failures: %d\n", failed);
841 printf ("aspath count: %ld\n", aspath_count());
842
843 return (failed + aspath_count());
844}