]> git.proxmox.com Git - mirror_frr.git/blob - tests/aspath_test.c
lib, bgpd, tests: Refactor FILTER_X in zebra.h
[mirror_frr.git] / tests / aspath_test.c
1 /*
2 * Copyright (C) 2005 Sun Microsystems, Inc.
3 *
4 * This file is part of Quagga.
5 *
6 * Quagga is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * Quagga is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Quagga; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22 #include <zebra.h>
23
24 #include "vty.h"
25 #include "stream.h"
26 #include "privs.h"
27 #include "queue.h"
28 #include "filter.h"
29
30 #include "bgpd/bgpd.h"
31 #include "bgpd/bgp_aspath.h"
32 #include "bgpd/bgp_attr.h"
33
34 #define VT100_RESET "\x1b[0m"
35 #define VT100_RED "\x1b[31m"
36 #define VT100_GREEN "\x1b[32m"
37 #define VT100_YELLOW "\x1b[33m"
38 #define OK VT100_GREEN "OK" VT100_RESET
39 #define FAILED VT100_RED "failed" VT100_RESET
40
41 /* need these to link in libbgp */
42 struct zebra_privs_t *bgpd_privs = NULL;
43 struct thread_master *master = NULL;
44
45 static int failed = 0;
46
47 /* specification for a test - what the results should be */
48 struct test_spec
49 {
50 const char *shouldbe; /* the string the path should parse to */
51 const char *shouldbe_delete_confed; /* ditto, but once confeds are deleted */
52 const unsigned int hops; /* aspath_count_hops result */
53 const unsigned int confeds; /* aspath_count_confeds */
54 const int private_as; /* whether the private_as check should pass or fail */
55 #define NOT_ALL_PRIVATE 0
56 #define ALL_PRIVATE 1
57 const as_t does_loop; /* an ASN which should trigger loop-check */
58 const as_t doesnt_loop; /* one which should not */
59 const as_t first; /* the first ASN, if there is one */
60 #define NULL_ASN 0
61 };
62
63
64 /* test segments to parse and validate, and use for other tests */
65 static struct test_segment {
66 const char *name;
67 const char *desc;
68 const u_char asdata[1024];
69 int len;
70 struct test_spec sp;
71 } test_segments [] =
72 {
73 { /* 0 */
74 "seq1",
75 "seq(8466,3,52737,4096)",
76 { 0x2,0x4, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00 },
77 10,
78 { "8466 3 52737 4096",
79 "8466 3 52737 4096",
80 4, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
81 },
82 { /* 1 */
83 "seq2",
84 "seq(8722) seq(4)",
85 { 0x2,0x1, 0x22,0x12,
86 0x2,0x1, 0x00,0x04 },
87 8,
88 { "8722 4",
89 "8722 4",
90 2, 0, NOT_ALL_PRIVATE, 4, 5, 8722, },
91 },
92 { /* 2 */
93 "seq3",
94 "seq(8466,3,52737,4096,8722,4)",
95 { 0x2,0x6, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00,
96 0x22,0x12, 0x00,0x04},
97 14,
98 { "8466 3 52737 4096 8722 4",
99 "8466 3 52737 4096 8722 4",
100 6, 0, NOT_ALL_PRIVATE, 3, 5, 8466 },
101 },
102 { /* 3 */
103 "seqset",
104 "seq(8482,51457) set(5204)",
105 { 0x2,0x2, 0x21,0x22, 0xc9,0x01,
106 0x1,0x1, 0x14,0x54 },
107 10,
108 { "8482 51457 {5204}",
109 "8482 51457 {5204}",
110 3, 0, NOT_ALL_PRIVATE, 5204, 51456, 8482},
111 },
112 { /* 4 */
113 "seqset2",
114 "seq(8467, 59649) set(4196,48658) set(17322,30745)",
115 { 0x2,0x2, 0x21,0x13, 0xe9,0x01,
116 0x1,0x2, 0x10,0x64, 0xbe,0x12,
117 0x1,0x2, 0x43,0xaa, 0x78,0x19 },
118 18,
119 { "8467 59649 {4196,48658} {17322,30745}",
120 "8467 59649 {4196,48658} {17322,30745}",
121 4, 0, NOT_ALL_PRIVATE, 48658, 1, 8467},
122 },
123 { /* 5 */
124 "multi",
125 "seq(6435,59408,21665) set(2457,61697,4369), seq(1842,41590,51793)",
126 { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
127 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
128 0x2,0x3, 0x07,0x32, 0xa2,0x76, 0xca,0x51 },
129 24,
130 { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
131 "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
132 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
133 },
134 { /* 6 */
135 "confed",
136 "confseq(123,456,789)",
137 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15 },
138 8,
139 { "(123 456 789)",
140 "",
141 0, 3, NOT_ALL_PRIVATE, 789, 1, NULL_ASN },
142 },
143 { /* 7 */
144 "confed2",
145 "confseq(123,456,789) confseq(111,222)",
146 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
147 0x3,0x2, 0x00,0x6f, 0x00,0xde },
148 14,
149 { "(123 456 789) (111 222)",
150 "",
151 0, 5, NOT_ALL_PRIVATE, 111, 1, NULL_ASN },
152 },
153 { /* 8 */
154 "confset",
155 "confset(456,123,789)",
156 { 0x4,0x3, 0x01,0xc8, 0x00,0x7b, 0x03,0x15 },
157 8,
158 { "[123,456,789]",
159 "",
160 0, 1, NOT_ALL_PRIVATE, 123, 1, NULL_ASN },
161 },
162 { /* 9 */
163 "confmulti",
164 "confseq(123,456,789) confset(222,111) seq(8722) set(4196,48658)",
165 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
166 0x4,0x2, 0x00,0xde, 0x00,0x6f,
167 0x2,0x1, 0x22,0x12,
168 0x1,0x2, 0x10,0x64, 0xbe,0x12 },
169 24,
170 { "(123 456 789) [111,222] 8722 {4196,48658}",
171 "8722 {4196,48658}",
172 2, 4, NOT_ALL_PRIVATE, 123, 1, NULL_ASN },
173 },
174 { /* 10 */
175 "seq4",
176 "seq(8466,2,52737,4096,8722,4)",
177 { 0x2,0x6, 0x21,0x12, 0x00,0x02, 0xce,0x01, 0x10,0x00,
178 0x22,0x12, 0x00,0x04},
179 14,
180 { "8466 2 52737 4096 8722 4",
181 "8466 2 52737 4096 8722 4",
182 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
183 },
184 { /* 11 */
185 "tripleseq1",
186 "seq(8466,2,52737) seq(4096,8722,4) seq(8722)",
187 { 0x2,0x3, 0x21,0x12, 0x00,0x02, 0xce,0x01,
188 0x2,0x3, 0x10,0x00, 0x22,0x12, 0x00,0x04,
189 0x2,0x1, 0x22,0x12},
190 20,
191 { "8466 2 52737 4096 8722 4 8722",
192 "8466 2 52737 4096 8722 4 8722",
193 7, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
194 },
195 { /* 12 */
196 "someprivate",
197 "seq(8466,64512,52737,65535)",
198 { 0x2,0x4, 0x21,0x12, 0xfc,0x00, 0xce,0x01, 0xff,0xff },
199 10,
200 { "8466 64512 52737 65535",
201 "8466 64512 52737 65535",
202 4, 0, NOT_ALL_PRIVATE, 65535, 4, 8466 },
203 },
204 { /* 13 */
205 "allprivate",
206 "seq(65534,64512,64513,65535)",
207 { 0x2,0x4, 0xff,0xfe, 0xfc,0x00, 0xfc,0x01, 0xff,0xff },
208 10,
209 { "65534 64512 64513 65535",
210 "65534 64512 64513 65535",
211 4, 0, ALL_PRIVATE, 65534, 4, 65534 },
212 },
213 { /* 14 */
214 "long",
215 "seq(8466,3,52737,4096,34285,<repeated 49 more times>)",
216 { 0x2,0xfa, 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 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
243 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
244 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
245 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
246 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
247 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
248 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
249 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
250 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
251 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
252 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
253 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
254 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
255 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
256 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
257 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
258 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
259 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
260 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
261 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
262 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
263 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
264 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
265 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, },
266 502,
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 "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
293 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
294 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
295 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
296 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
297 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
298 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
299 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
300 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
301 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
302 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
303 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
304 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
305 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
306 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
307 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
308 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
309 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
310 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
311 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
312 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
313 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
314 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
315 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
316 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
317 "8466 3 52737 4096 34285 8466 3 52737 4096 34285",
318 250, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
319 },
320 { /* 15 */
321 "seq1extra",
322 "seq(8466,3,52737,4096,3456)",
323 { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
324 12,
325 { "8466 3 52737 4096 3456",
326 "8466 3 52737 4096 3456",
327 5, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
328 },
329 { /* 16 */
330 "empty",
331 "<empty>",
332 {},
333 0,
334 { "", "", 0, 0, 0, 0, 0, 0 },
335 },
336 { /* 17 */
337 "redundantset",
338 "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153)",
339 { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
340 0x1,0x4, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9 },
341 22,
342 {
343 /* We shouldn't ever /generate/ such paths. However, we should
344 * cope with them fine.
345 */
346 "8466 3 52737 4096 3456 {7099,8153}",
347 "8466 3 52737 4096 3456 {7099,8153}",
348 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
349 },
350 { /* 18 */
351 "reconcile_lead_asp",
352 "seq(6435,59408,21665) set(23456,23456,23456), seq(23456,23456,23456)",
353 { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
354 0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
355 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
356 24,
357 { "6435 59408 21665 {23456} 23456 23456 23456",
358 "6435 59408 21665 {23456} 23456 23456 23456",
359 7, 0, NOT_ALL_PRIVATE, 23456, 1, 6435 },
360 },
361 { /* 19 */
362 "reconcile_new_asp",
363 "set(2457,61697,4369), seq(1842,41591,51793)",
364 {
365 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
366 0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51 },
367 16,
368 { "{2457,4369,61697} 1842 41591 51793",
369 "{2457,4369,61697} 1842 41591 51793",
370 4, 0, NOT_ALL_PRIVATE, 51793, 1, 2457 },
371 },
372 { /* 20 */
373 "reconcile_confed",
374 "confseq(123,456,789) confset(456,124,788) seq(6435,59408,21665)"
375 " set(23456,23456,23456), seq(23456,23456,23456)",
376 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
377 0x4,0x3, 0x01,0xc8, 0x00,0x7c, 0x03,0x14,
378 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
379 0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
380 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
381 40,
382 { "(123 456 789) [124,456,788] 6435 59408 21665"
383 " {23456} 23456 23456 23456",
384 "6435 59408 21665 {23456} 23456 23456 23456",
385 7, 4, NOT_ALL_PRIVATE, 23456, 1, 6435 },
386 },
387 { /* 21 */
388 "reconcile_start_trans",
389 "seq(23456,23456,23456) seq(6435,59408,21665)",
390 { 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
391 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, },
392 16,
393 { "23456 23456 23456 6435 59408 21665",
394 "23456 23456 23456 6435 59408 21665",
395 6, 0, NOT_ALL_PRIVATE, 21665, 1, 23456 },
396 },
397 { /* 22 */
398 "reconcile_start_trans4",
399 "seq(1842,41591,51793) seq(6435,59408,21665)",
400 { 0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51,
401 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, },
402 16,
403 { "1842 41591 51793 6435 59408 21665",
404 "1842 41591 51793 6435 59408 21665",
405 6, 0, NOT_ALL_PRIVATE, 41591, 1, 1842 },
406 },
407 { /* 23 */
408 "reconcile_start_trans_error",
409 "seq(23456,23456,23456) seq(6435,59408)",
410 { 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
411 0x2,0x2, 0x19,0x23, 0xe8,0x10, },
412 14,
413 { "23456 23456 23456 6435 59408",
414 "23456 23456 23456 6435 59408",
415 5, 0, NOT_ALL_PRIVATE, 59408, 1, 23456 },
416 },
417 { /* 24 */
418 "redundantset2",
419 "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153,7099)",
420 { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
421 0x1,0x5, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9, 0x1b,0xbb,},
422 24,
423 {
424 /* We should weed out duplicate set members. */
425 "8466 3 52737 4096 3456 {7099,8153}",
426 "8466 3 52737 4096 3456 {7099,8153}",
427 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
428 },
429 { /* 25 */
430 "zero-size overflow",
431 "#ASNs = 0, data = seq(8466 3 52737 4096 3456)",
432 { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
433 12,
434 { NULL, NULL,
435 0, 0, 0, 0, 0, 0 },
436 },
437 { /* 26 */
438 "zero-size overflow + valid segment",
439 "seq(#AS=0:8466 3 52737),seq(4096 3456)",
440 { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01,
441 0x2,0x2, 0x10,0x00, 0x0d,0x80 },
442 14
443 ,
444 { NULL, NULL,
445 0, 0, 0, 0, 0, 0 },
446 },
447 { /* 27 */
448 "invalid segment type",
449 "type=8(4096 3456)",
450 { 0x8,0x2, 0x10,0x00, 0x0d,0x80 },
451 14
452 ,
453 { NULL, NULL,
454 0, 0, 0, 0, 0, 0 },
455 }, { NULL, NULL, {0}, 0, { NULL, 0, 0 } }
456 };
457
458 #define COMMON_ATTRS \
459 BGP_ATTR_FLAG_TRANS, \
460 BGP_ATTR_ORIGIN, \
461 1, \
462 BGP_ORIGIN_EGP, \
463 BGP_ATTR_FLAG_TRANS, \
464 BGP_ATTR_NEXT_HOP, \
465 4, 192, 0, 2, 0
466 #define COMMON_ATTR_SIZE 11
467
468 /* */
469 static struct aspath_tests {
470 const char *desc;
471 const struct test_segment *segment;
472 const char *shouldbe; /* String it should evaluate to */
473 const enum as4 { AS4_DATA, AS2_DATA }
474 as4; /* whether data should be as4 or not (ie as2) */
475 const int result; /* expected result for bgp_attr_parse */
476 const int cap; /* capabilities to set for peer */
477 const char attrheader [1024];
478 size_t len;
479 const struct test_segment *old_segment;
480 } aspath_tests [] =
481 {
482 /* 0 */
483 {
484 "basic test",
485 &test_segments[0],
486 "8466 3 52737 4096",
487 AS2_DATA, 0,
488 0,
489 { COMMON_ATTRS,
490 BGP_ATTR_FLAG_TRANS,
491 BGP_ATTR_AS_PATH,
492 10,
493 },
494 COMMON_ATTR_SIZE + 3,
495 },
496 /* 1 */
497 {
498 "length too short",
499 &test_segments[0],
500 "8466 3 52737 4096",
501 AS2_DATA, -1,
502 0,
503 { COMMON_ATTRS,
504 BGP_ATTR_FLAG_TRANS,
505 BGP_ATTR_AS_PATH,
506 8,
507 },
508 COMMON_ATTR_SIZE + 3,
509 },
510 /* 2 */
511 {
512 "length too long",
513 &test_segments[0],
514 "8466 3 52737 4096",
515 AS2_DATA, -1,
516 0,
517 { COMMON_ATTRS,
518 BGP_ATTR_FLAG_TRANS,
519 BGP_ATTR_AS_PATH,
520 12,
521 },
522 COMMON_ATTR_SIZE + 3,
523 },
524 /* 3 */
525 {
526 "incorrect flag",
527 &test_segments[0],
528 "8466 3 52737 4096",
529 AS2_DATA, -1,
530 0,
531 { COMMON_ATTRS,
532 BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
533 BGP_ATTR_AS_PATH,
534 10,
535 },
536 COMMON_ATTR_SIZE + 3,
537 },
538 /* 4 */
539 {
540 "as4_path, with as2 format data",
541 &test_segments[0],
542 "8466 3 52737 4096",
543 AS2_DATA, -1,
544 0,
545 { COMMON_ATTRS,
546 BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
547 BGP_ATTR_AS4_PATH,
548 10,
549 },
550 COMMON_ATTR_SIZE + 3,
551 },
552 /* 5 */
553 {
554 "as4, with incorrect attr length",
555 &test_segments[0],
556 "8466 3 52737 4096",
557 AS4_DATA, -1,
558 PEER_CAP_AS4_RCV,
559 { COMMON_ATTRS,
560 BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
561 BGP_ATTR_AS4_PATH,
562 10,
563 },
564 COMMON_ATTR_SIZE + 3,
565 },
566 /* 6 */
567 {
568 "basic 4-byte as-path",
569 &test_segments[0],
570 "8466 3 52737 4096",
571 AS4_DATA, 0,
572 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
573 { COMMON_ATTRS,
574 BGP_ATTR_FLAG_TRANS,
575 BGP_ATTR_AS_PATH,
576 18,
577 },
578 COMMON_ATTR_SIZE + 3,
579 },
580 /* 7 */
581 {
582 "4b AS_PATH: too short",
583 &test_segments[0],
584 "8466 3 52737 4096",
585 AS4_DATA, -1,
586 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
587 { COMMON_ATTRS,
588 BGP_ATTR_FLAG_TRANS,
589 BGP_ATTR_AS_PATH,
590 16,
591 },
592 COMMON_ATTR_SIZE + 3,
593 },
594 /* 8 */
595 {
596 "4b AS_PATH: too long",
597 &test_segments[0],
598 "8466 3 52737 4096",
599 AS4_DATA, -1,
600 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
601 { COMMON_ATTRS,
602 BGP_ATTR_FLAG_TRANS,
603 BGP_ATTR_AS_PATH,
604 20,
605 },
606 COMMON_ATTR_SIZE + 3,
607 },
608 /* 9 */
609 {
610 "4b AS_PATH: too long2",
611 &test_segments[0],
612 "8466 3 52737 4096",
613 AS4_DATA, -1,
614 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
615 { COMMON_ATTRS,
616 BGP_ATTR_FLAG_TRANS,
617 BGP_ATTR_AS_PATH,
618 22,
619 },
620 COMMON_ATTR_SIZE + 3,
621 },
622 /* 10 */
623 {
624 "4b AS_PATH: bad flags",
625 &test_segments[0],
626 "8466 3 52737 4096",
627 AS4_DATA, -1,
628 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
629 { COMMON_ATTRS,
630 BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
631 BGP_ATTR_AS_PATH,
632 18,
633 },
634 COMMON_ATTR_SIZE + 3,
635 },
636 /* 11 */
637 {
638 "4b AS4_PATH w/o AS_PATH",
639 &test_segments[6],
640 NULL,
641 AS4_DATA, -1,
642 PEER_CAP_AS4_ADV,
643 { COMMON_ATTRS,
644 BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
645 BGP_ATTR_AS4_PATH,
646 14,
647 },
648 COMMON_ATTR_SIZE + 3,
649 },
650 /* 12 */
651 {
652 "4b AS4_PATH: confed",
653 &test_segments[6],
654 "8466 3 52737 4096 (123 456 789)",
655 AS4_DATA, 0,
656 PEER_CAP_AS4_ADV,
657 { COMMON_ATTRS,
658 BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
659 BGP_ATTR_AS4_PATH,
660 14,
661 },
662 COMMON_ATTR_SIZE + 3,
663 &test_segments[0],
664 },
665 { NULL, NULL, NULL, 0, 0, 0, { 0 }, 0 },
666 };
667
668 /* prepending tests */
669 static struct tests {
670 const struct test_segment *test1;
671 const struct test_segment *test2;
672 struct test_spec sp;
673 } prepend_tests[] =
674 {
675 /* 0 */
676 { &test_segments[0], &test_segments[1],
677 { "8466 3 52737 4096 8722 4",
678 "8466 3 52737 4096 8722 4",
679 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
680 },
681 /* 1 */
682 { &test_segments[1], &test_segments[3],
683 { "8722 4 8482 51457 {5204}",
684 "8722 4 8482 51457 {5204}",
685 5, 0, NOT_ALL_PRIVATE, 5204, 1, 8722 }
686 },
687 /* 2 */
688 { &test_segments[3], &test_segments[4],
689 { "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
690 "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
691 7, 0, NOT_ALL_PRIVATE, 5204, 1, 8482 },
692 },
693 /* 3 */
694 { &test_segments[4], &test_segments[5],
695 { "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
696 " {2457,4369,61697} 1842 41590 51793",
697 "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
698 " {2457,4369,61697} 1842 41590 51793",
699 11, 0, NOT_ALL_PRIVATE, 61697, 1, 8467 }
700 },
701 /* 4 */
702 { &test_segments[5], &test_segments[6],
703 { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
704 "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
705 7, 0, NOT_ALL_PRIVATE, 1842, 1, 6435 },
706 },
707 /* 5 */
708 { &test_segments[6], &test_segments[7],
709 { "(123 456 789) (123 456 789) (111 222)",
710 "",
711 0, 8, NOT_ALL_PRIVATE, 111, 1, 0 }
712 },
713 { &test_segments[7], &test_segments[8],
714 { "(123 456 789) (111 222) [123,456,789]",
715 "",
716 0, 6, NOT_ALL_PRIVATE, 111, 1, 0 }
717 },
718 { &test_segments[8], &test_segments[9],
719 { "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
720 "8722 {4196,48658}",
721 2, 5, NOT_ALL_PRIVATE, 456, 1, NULL_ASN },
722 },
723 { &test_segments[9], &test_segments[8],
724 { "(123 456 789) [111,222] 8722 {4196,48658} [123,456,789]",
725 "8722 {4196,48658}",
726 2, 5, NOT_ALL_PRIVATE, 48658, 1, NULL_ASN },
727 },
728 { &test_segments[14], &test_segments[11],
729 { "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
730 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
731 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
732 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
733 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
734 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
735 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
736 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
737 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
738 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
739 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
740 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
741 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
742 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
743 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
744 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
745 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
746 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
747 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
748 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
749 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
750 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
751 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
752 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
753 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
754 "8466 2 52737 4096 8722 4 8722",
755
756 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
757 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
758 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
759 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
760 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
761 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
762 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
763 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
764 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
765 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
766 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
767 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
768 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
769 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
770 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
771 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
772 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
773 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
774 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
775 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
776 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
777 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
778 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
779 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
780 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
781 "8466 2 52737 4096 8722 4 8722",
782 257, 0, NOT_ALL_PRIVATE, 4096, 1000, 8466 },
783 },
784 { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
785 };
786
787 struct tests reconcile_tests[] =
788 {
789 { &test_segments[18], &test_segments[19],
790 { "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
791 "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
792 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
793 },
794 { &test_segments[19], &test_segments[18],
795 /* AS_PATH (19) has more hops than NEW_AS_PATH,
796 * so just AS_PATH should be used (though, this practice
797 * is bad imho).
798 */
799 { "{2457,4369,61697} 1842 41591 51793 6435 59408 21665 {23456} 23456 23456 23456",
800 "{2457,4369,61697} 1842 41591 51793 6435 59408 21665 {23456} 23456 23456 23456",
801 11, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
802 },
803 { &test_segments[20], &test_segments[19],
804 { "(123 456 789) [124,456,788] 6435 59408 21665"
805 " {2457,4369,61697} 1842 41591 51793",
806 "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
807 7, 4, NOT_ALL_PRIVATE, 51793, 1, 6435 },
808 },
809 { &test_segments[21], &test_segments[22],
810 { "1842 41591 51793 6435 59408 21665",
811 "1842 41591 51793 6435 59408 21665",
812 6, 0, NOT_ALL_PRIVATE, 51793, 1, 1842 },
813 },
814 { &test_segments[23], &test_segments[22],
815 { "23456 23456 23456 6435 59408 1842 41591 51793 6435 59408 21665",
816 "23456 23456 23456 6435 59408 1842 41591 51793 6435 59408 21665",
817 11, 0, NOT_ALL_PRIVATE, 51793, 1, 1842 },
818 },
819 { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
820 };
821
822 struct tests aggregate_tests[] =
823 {
824 { &test_segments[0], &test_segments[2],
825 { "8466 3 52737 4096 {4,8722}",
826 "8466 3 52737 4096 {4,8722}",
827 5, 0, NOT_ALL_PRIVATE, 4, 1, 8466 },
828 },
829 { &test_segments[2], &test_segments[0],
830 { "8466 3 52737 4096 {4,8722}",
831 "8466 3 52737 4096 {4,8722}",
832 5, 0, NOT_ALL_PRIVATE, 8722, 1, 8466 },
833 },
834 { &test_segments[2], &test_segments[10],
835 { "8466 {2,3,4,4096,8722,52737}",
836 "8466 {2,3,4,4096,8722,52737}",
837 2, 0, NOT_ALL_PRIVATE, 8722, 5, 8466 },
838 },
839 { &test_segments[10], &test_segments[2],
840 { "8466 {2,3,4,4096,8722,52737}",
841 "8466 {2,3,4,4096,8722,52737}",
842 2, 0, NOT_ALL_PRIVATE, 2, 20000, 8466 },
843 },
844
845 { &test_segments[5], &test_segments[18],
846 { "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
847 "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
848 4, 0, NOT_ALL_PRIVATE, 41590, 1, 6435 },
849 },
850
851 { NULL, NULL, { NULL, 0, 0} },
852 };
853
854 struct compare_tests
855 {
856 int test_index1;
857 int test_index2;
858 #define CMP_RES_YES 1
859 #define CMP_RES_NO 0
860 char shouldbe_cmp;
861 char shouldbe_confed;
862 } left_compare [] =
863 {
864 { 0, 1, CMP_RES_NO, CMP_RES_NO },
865 { 0, 2, CMP_RES_YES, CMP_RES_NO },
866 { 0, 11, CMP_RES_YES, CMP_RES_NO },
867 { 0, 15, CMP_RES_YES, CMP_RES_NO },
868 { 0, 16, CMP_RES_NO, CMP_RES_NO },
869 { 1, 11, CMP_RES_NO, CMP_RES_NO },
870 { 6, 7, CMP_RES_NO, CMP_RES_YES },
871 { 6, 8, CMP_RES_NO, CMP_RES_NO },
872 { 7, 8, CMP_RES_NO, CMP_RES_NO },
873 { 1, 9, CMP_RES_YES, CMP_RES_NO },
874 { 0, 9, CMP_RES_NO, CMP_RES_NO },
875 { 3, 9, CMP_RES_NO, CMP_RES_NO },
876 { 0, 6, CMP_RES_NO, CMP_RES_NO },
877 { 1, 6, CMP_RES_NO, CMP_RES_NO },
878 { 0, 8, CMP_RES_NO, CMP_RES_NO },
879 { 1, 8, CMP_RES_NO, CMP_RES_NO },
880 { 11, 6, CMP_RES_NO, CMP_RES_NO },
881 { 11, 7, CMP_RES_NO, CMP_RES_NO },
882 { 11, 8, CMP_RES_NO, CMP_RES_NO },
883 { 9, 6, CMP_RES_NO, CMP_RES_YES },
884 { 9, 7, CMP_RES_NO, CMP_RES_YES },
885 { 9, 8, CMP_RES_NO, CMP_RES_NO },
886 };
887
888 /* make an aspath from a data stream */
889 static struct aspath *
890 make_aspath (const u_char *data, size_t len, int use32bit)
891 {
892 struct stream *s = NULL;
893 struct aspath *as;
894
895 if (len)
896 {
897 s = stream_new (len);
898 stream_put (s, data, len);
899 }
900 as = aspath_parse (s, len, use32bit);
901
902 if (s)
903 stream_free (s);
904
905 return as;
906 }
907
908 static void
909 printbytes (const u_char *bytes, int len)
910 {
911 int i = 0;
912 while (i < len)
913 {
914 if (i % 2)
915 printf ("%02hhx%s", bytes[i], " ");
916 else
917 printf ("0x%02hhx", bytes[i]);
918 i++;
919 }
920 printf ("\n");
921 }
922
923 /* validate the given aspath */
924 static int
925 validate (struct aspath *as, const struct test_spec *sp)
926 {
927 size_t bytes, bytes4;
928 int fails = 0;
929 const u_char *out;
930 static struct stream *s;
931 struct aspath *asinout, *asconfeddel, *asstr, *as4;
932
933 if (as == NULL && sp->shouldbe == NULL)
934 {
935 printf ("Correctly failed to parse\n");
936 return fails;
937 }
938
939 out = aspath_snmp_pathseg (as, &bytes);
940 asinout = make_aspath (out, bytes, 0);
941
942 /* Excercise AS4 parsing a bit, with a dogfood test */
943 if (!s)
944 s = stream_new (4096);
945 bytes4 = aspath_put (s, as, 1);
946 as4 = make_aspath (STREAM_DATA(s), bytes4, 1);
947
948 asstr = aspath_str2aspath (sp->shouldbe);
949
950 asconfeddel = aspath_delete_confed_seq (aspath_dup (asinout));
951
952 printf ("got: %s\n", aspath_print(as));
953
954 /* the parsed path should match the specified 'shouldbe' string.
955 * We should pass the "eat our own dog food" test, be able to output
956 * this path and then input it again. Ie the path resulting from:
957 *
958 * aspath_parse(aspath_put(as))
959 *
960 * should:
961 *
962 * - also match the specified 'shouldbe' value
963 * - hash to same value as original path
964 * - have same hops and confed counts as original, and as the
965 * the specified counts
966 *
967 * aspath_str2aspath() and shouldbe should match
968 *
969 * We do the same for:
970 *
971 * aspath_parse(aspath_put(as,USE32BIT))
972 *
973 * Confederation related tests:
974 * - aspath_delete_confed_seq(aspath) should match shouldbe_confed
975 * - aspath_delete_confed_seq should be idempotent.
976 */
977 if (strcmp(aspath_print (as), sp->shouldbe)
978 /* hash validation */
979 || (aspath_key_make (as) != aspath_key_make (asinout))
980 /* by string */
981 || strcmp(aspath_print (asinout), sp->shouldbe)
982 /* By 4-byte parsing */
983 || strcmp(aspath_print (as4), sp->shouldbe)
984 /* by various path counts */
985 || (aspath_count_hops (as) != sp->hops)
986 || (aspath_count_confeds (as) != sp->confeds)
987 || (aspath_count_hops (asinout) != sp->hops)
988 || (aspath_count_confeds (asinout) != sp->confeds))
989 {
990 failed++;
991 fails++;
992 printf ("shouldbe:\n%s\n", sp->shouldbe);
993 printf ("as4:\n%s\n", aspath_print (as4));
994 printf ("hash keys: in: %d out->in: %d\n",
995 aspath_key_make (as), aspath_key_make (asinout));
996 printf ("hops: %d, counted %d %d\n", sp->hops,
997 aspath_count_hops (as),
998 aspath_count_hops (asinout) );
999 printf ("confeds: %d, counted %d %d\n", sp->confeds,
1000 aspath_count_confeds (as),
1001 aspath_count_confeds (asinout));
1002 printf ("out->in:\n%s\nbytes: ", aspath_print(asinout));
1003 printbytes (out, bytes);
1004 }
1005 /* basic confed related tests */
1006 if ((aspath_print (asconfeddel) == NULL
1007 && sp->shouldbe_delete_confed != NULL)
1008 || (aspath_print (asconfeddel) != NULL
1009 && sp->shouldbe_delete_confed == NULL)
1010 || strcmp(aspath_print (asconfeddel), sp->shouldbe_delete_confed)
1011 /* delete_confed_seq should be idempotent */
1012 || (aspath_key_make (asconfeddel)
1013 != aspath_key_make (aspath_delete_confed_seq (asconfeddel))))
1014 {
1015 failed++;
1016 fails++;
1017 printf ("as-path minus confeds is: %s\n", aspath_print (asconfeddel));
1018 printf ("as-path minus confeds should be: %s\n", sp->shouldbe_delete_confed);
1019 }
1020 /* aspath_str2aspath test */
1021 if ((aspath_print (asstr) == NULL && sp->shouldbe != NULL)
1022 || (aspath_print (asstr) != NULL && sp->shouldbe == NULL)
1023 || strcmp(aspath_print (asstr), sp->shouldbe))
1024 {
1025 failed++;
1026 fails++;
1027 printf ("asstr: %s\n", aspath_print (asstr));
1028 }
1029
1030 /* loop, private and first as checks */
1031 if ((sp->does_loop && aspath_loop_check (as, sp->does_loop) == 0)
1032 || (sp->doesnt_loop && aspath_loop_check (as, sp->doesnt_loop) != 0)
1033 || (aspath_private_as_check (as) != sp->private_as)
1034 || (aspath_firstas_check (as,sp->first)
1035 && sp->first == 0))
1036 {
1037 failed++;
1038 fails++;
1039 printf ("firstas: %d, got %d\n", sp->first,
1040 aspath_firstas_check (as,sp->first));
1041 printf ("loop does: %d %d, doesnt: %d %d\n",
1042 sp->does_loop, aspath_loop_check (as, sp->does_loop),
1043 sp->doesnt_loop, aspath_loop_check (as, sp->doesnt_loop));
1044 printf ("private check: %d %d\n", sp->private_as,
1045 aspath_private_as_check (as));
1046 }
1047 aspath_unintern (&asinout);
1048 aspath_unintern (&as4);
1049
1050 aspath_free (asconfeddel);
1051 aspath_free (asstr);
1052 stream_reset (s);
1053
1054 return fails;
1055 }
1056
1057 static void
1058 empty_get_test ()
1059 {
1060 struct aspath *as = aspath_empty_get ();
1061 struct test_spec sp = { "", "", 0, 0, 0, 0, 0, 0 };
1062
1063 printf ("empty_get_test, as: %s\n",aspath_print (as));
1064 if (!validate (as, &sp))
1065 printf ("%s\n", OK);
1066 else
1067 printf ("%s!\n", FAILED);
1068
1069 printf ("\n");
1070
1071 aspath_free (as);
1072 }
1073
1074 /* basic parsing test */
1075 static void
1076 parse_test (struct test_segment *t)
1077 {
1078 struct aspath *asp;
1079
1080 printf ("%s: %s\n", t->name, t->desc);
1081
1082 asp = make_aspath (t->asdata, t->len, 0);
1083
1084 printf ("aspath: %s\nvalidating...:\n", aspath_print (asp));
1085
1086 if (!validate (asp, &t->sp))
1087 printf (OK "\n");
1088 else
1089 printf (FAILED "\n");
1090
1091 printf ("\n");
1092
1093 if (asp)
1094 aspath_unintern (&asp);
1095 }
1096
1097 /* prepend testing */
1098 static void
1099 prepend_test (struct tests *t)
1100 {
1101 struct aspath *asp1, *asp2, *ascratch;
1102
1103 printf ("prepend %s: %s\n", t->test1->name, t->test1->desc);
1104 printf ("to %s: %s\n", t->test2->name, t->test2->desc);
1105
1106 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
1107 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
1108
1109 ascratch = aspath_dup (asp2);
1110 aspath_unintern (&asp2);
1111
1112 asp2 = aspath_prepend (asp1, ascratch);
1113
1114 printf ("aspath: %s\n", aspath_print (asp2));
1115
1116 if (!validate (asp2, &t->sp))
1117 printf ("%s\n", OK);
1118 else
1119 printf ("%s!\n", FAILED);
1120
1121 printf ("\n");
1122 aspath_unintern (&asp1);
1123 aspath_free (asp2);
1124 }
1125
1126 /* empty-prepend testing */
1127 static void
1128 empty_prepend_test (struct test_segment *t)
1129 {
1130 struct aspath *asp1, *asp2, *ascratch;
1131
1132 printf ("empty prepend %s: %s\n", t->name, t->desc);
1133
1134 asp1 = make_aspath (t->asdata, t->len, 0);
1135 asp2 = aspath_empty ();
1136
1137 ascratch = aspath_dup (asp2);
1138 aspath_unintern (&asp2);
1139
1140 asp2 = aspath_prepend (asp1, ascratch);
1141
1142 printf ("aspath: %s\n", aspath_print (asp2));
1143
1144 if (!validate (asp2, &t->sp))
1145 printf (OK "\n");
1146 else
1147 printf (FAILED "!\n");
1148
1149 printf ("\n");
1150 if (asp1)
1151 aspath_unintern (&asp1);
1152 aspath_free (asp2);
1153 }
1154
1155 /* as2+as4 reconciliation testing */
1156 static void
1157 as4_reconcile_test (struct tests *t)
1158 {
1159 struct aspath *asp1, *asp2, *ascratch;
1160
1161 printf ("reconciling %s:\n %s\n", t->test1->name, t->test1->desc);
1162 printf ("with %s:\n %s\n", t->test2->name, t->test2->desc);
1163
1164 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
1165 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
1166
1167 ascratch = aspath_reconcile_as4 (asp1, asp2);
1168
1169 if (!validate (ascratch, &t->sp))
1170 printf (OK "\n");
1171 else
1172 printf (FAILED "!\n");
1173
1174 printf ("\n");
1175 aspath_unintern (&asp1);
1176 aspath_unintern (&asp2);
1177 aspath_free (ascratch);
1178 }
1179
1180
1181 /* aggregation testing */
1182 static void
1183 aggregate_test (struct tests *t)
1184 {
1185 struct aspath *asp1, *asp2, *ascratch;
1186
1187 printf ("aggregate %s: %s\n", t->test1->name, t->test1->desc);
1188 printf ("with %s: %s\n", t->test2->name, t->test2->desc);
1189
1190 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
1191 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
1192
1193 ascratch = aspath_aggregate (asp1, asp2);
1194
1195 if (!validate (ascratch, &t->sp))
1196 printf (OK "\n");
1197 else
1198 printf (FAILED "!\n");
1199
1200 printf ("\n");
1201 aspath_unintern (&asp1);
1202 aspath_unintern (&asp2);
1203 aspath_free (ascratch);
1204 /* aspath_unintern (ascratch);*/
1205 }
1206
1207 /* cmp_left tests */
1208 static void
1209 cmp_test ()
1210 {
1211 unsigned int i;
1212 #define CMP_TESTS_MAX \
1213 (sizeof(left_compare) / sizeof (struct compare_tests))
1214
1215 for (i = 0; i < CMP_TESTS_MAX; i++)
1216 {
1217 struct test_segment *t1 = &test_segments[left_compare[i].test_index1];
1218 struct test_segment *t2 = &test_segments[left_compare[i].test_index2];
1219 struct aspath *asp1, *asp2;
1220
1221 printf ("left cmp %s: %s\n", t1->name, t1->desc);
1222 printf ("and %s: %s\n", t2->name, t2->desc);
1223
1224 asp1 = make_aspath (t1->asdata, t1->len, 0);
1225 asp2 = make_aspath (t2->asdata, t2->len, 0);
1226
1227 if (aspath_cmp_left (asp1, asp2) != left_compare[i].shouldbe_cmp
1228 || aspath_cmp_left (asp2, asp1) != left_compare[i].shouldbe_cmp
1229 || aspath_cmp_left_confed (asp1, asp2)
1230 != left_compare[i].shouldbe_confed
1231 || aspath_cmp_left_confed (asp2, asp1)
1232 != left_compare[i].shouldbe_confed)
1233 {
1234 failed++;
1235 printf (FAILED "\n");
1236 printf ("result should be: cmp: %d, confed: %d\n",
1237 left_compare[i].shouldbe_cmp,
1238 left_compare[i].shouldbe_confed);
1239 printf ("got: cmp %d, cmp_confed: %d\n",
1240 aspath_cmp_left (asp1, asp2),
1241 aspath_cmp_left_confed (asp1, asp2));
1242 printf("path1: %s\npath2: %s\n", aspath_print (asp1),
1243 aspath_print (asp2));
1244 }
1245 else
1246 printf (OK "\n");
1247
1248 printf ("\n");
1249 aspath_unintern (&asp1);
1250 aspath_unintern (&asp2);
1251 }
1252 }
1253
1254 static int
1255 handle_attr_test (struct aspath_tests *t)
1256 {
1257 struct bgp bgp = { 0 };
1258 struct peer peer = { 0 };
1259 struct attr attr = { 0 };
1260 int ret;
1261 int initfail = failed;
1262 struct aspath *asp;
1263 size_t datalen;
1264
1265 asp = make_aspath (t->segment->asdata, t->segment->len, 0);
1266
1267 peer.ibuf = stream_new (BGP_MAX_PACKET_SIZE);
1268 peer.obuf = stream_fifo_new ();
1269 peer.bgp = &bgp;
1270 peer.host = (char *)"none";
1271 peer.fd = -1;
1272 peer.cap = t->cap;
1273
1274 stream_write (peer.ibuf, t->attrheader, t->len);
1275 datalen = aspath_put (peer.ibuf, asp, t->as4 == AS4_DATA);
1276 if (t->old_segment)
1277 {
1278 char dummyaspath[] = { BGP_ATTR_FLAG_TRANS, BGP_ATTR_AS_PATH,
1279 t->old_segment->len };
1280 stream_write (peer.ibuf, dummyaspath, sizeof (dummyaspath));
1281 stream_write (peer.ibuf, t->old_segment->asdata, t->old_segment->len);
1282 datalen += sizeof (dummyaspath) + t->old_segment->len;
1283 }
1284
1285 ret = bgp_attr_parse (&peer, &attr, t->len + datalen, NULL, NULL);
1286
1287 if (ret != t->result)
1288 {
1289 printf ("bgp_attr_parse returned %d, expected %d\n", ret, t->result);
1290 printf ("datalen %zd\n", datalen);
1291 failed++;
1292 }
1293 if (ret != 0)
1294 goto out;
1295
1296 if (t->shouldbe && attr.aspath == NULL)
1297 {
1298 printf ("aspath is NULL, but should be: %s\n", t->shouldbe);
1299 failed++;
1300 }
1301 if (t->shouldbe && attr.aspath && strcmp (attr.aspath->str, t->shouldbe))
1302 {
1303 printf ("attr str and 'shouldbe' mismatched!\n"
1304 "attr str: %s\n"
1305 "shouldbe: %s\n",
1306 attr.aspath->str, t->shouldbe);
1307 failed++;
1308 }
1309 if (!t->shouldbe && attr.aspath)
1310 {
1311 printf ("aspath should be NULL, but is: %s\n", attr.aspath->str);
1312 failed++;
1313 }
1314
1315 out:
1316 if (attr.aspath)
1317 aspath_unintern (&attr.aspath);
1318 if (asp)
1319 aspath_unintern (&asp);
1320 return failed - initfail;
1321 }
1322
1323 static void
1324 attr_test (struct aspath_tests *t)
1325 {
1326 printf ("%s\n", t->desc);
1327 printf ("%s\n\n", handle_attr_test (t) ? FAILED : OK);
1328 }
1329
1330 int
1331 main (void)
1332 {
1333 int i = 0;
1334 bgp_master_init ();
1335 master = bm->master;
1336 bgp_option_set (BGP_OPT_NO_LISTEN);
1337 bgp_attr_init ();
1338
1339 while (test_segments[i].name)
1340 {
1341 printf ("test %u\n", i);
1342 parse_test (&test_segments[i]);
1343 empty_prepend_test (&test_segments[i++]);
1344 }
1345
1346 i = 0;
1347 while (prepend_tests[i].test1)
1348 {
1349 printf ("prepend test %u\n", i);
1350 prepend_test (&prepend_tests[i++]);
1351 }
1352
1353 i = 0;
1354 while (aggregate_tests[i].test1)
1355 {
1356 printf ("aggregate test %u\n", i);
1357 aggregate_test (&aggregate_tests[i++]);
1358 }
1359
1360 i = 0;
1361
1362 while (reconcile_tests[i].test1)
1363 {
1364 printf ("reconcile test %u\n", i);
1365 as4_reconcile_test (&reconcile_tests[i++]);
1366 }
1367
1368 i = 0;
1369
1370 cmp_test();
1371
1372 i = 0;
1373
1374 empty_get_test();
1375
1376 i = 0;
1377
1378 while (aspath_tests[i].desc)
1379 {
1380 printf ("aspath_attr test %d\n", i);
1381 attr_test (&aspath_tests[i++]);
1382 }
1383
1384 printf ("failures: %d\n", failed);
1385 printf ("aspath count: %ld\n", aspath_count());
1386
1387 return (failed + aspath_count());
1388 }