blob: ade60c67c57931ea7b37ca3b71c08d8bc1ed831c [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"
Paul Jakma50ef5652010-11-27 23:14:02 +00009#include "bgpd/bgp_attr.h"
paul1f377b02005-09-10 18:00:11 +000010
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000011#define VT100_RESET "\x1b[0m"
12#define VT100_RED "\x1b[31m"
13#define VT100_GREEN "\x1b[32m"
14#define VT100_YELLOW "\x1b[33m"
15#define OK VT100_GREEN "OK" VT100_RESET
16#define FAILED VT100_RED "failed" VT100_RESET
17
paul1f377b02005-09-10 18:00:11 +000018/* need these to link in libbgp */
19struct zebra_privs_t *bgpd_privs = NULL;
20struct thread_master *master = NULL;
21
22static int failed = 0;
23
24/* specification for a test - what the results should be */
25struct test_spec
26{
27 const char *shouldbe; /* the string the path should parse to */
28 const char *shouldbe_delete_confed; /* ditto, but once confeds are deleted */
paul84771ee2005-10-11 03:48:28 +000029 const unsigned int hops; /* aspath_count_hops result */
30 const unsigned int confeds; /* aspath_count_confeds */
paul1f377b02005-09-10 18:00:11 +000031 const int private_as; /* whether the private_as check should pass or fail */
32#define NOT_ALL_PRIVATE 0
33#define ALL_PRIVATE 1
34 const as_t does_loop; /* an ASN which should trigger loop-check */
35 const as_t doesnt_loop; /* one which should not */
36 const as_t first; /* the first ASN, if there is one */
37#define NULL_ASN 0
38};
39
40
41/* test segments to parse and validate, and use for other tests */
42static struct test_segment {
43 const char *name;
44 const char *desc;
45 const u_char asdata[1024];
46 int len;
47 struct test_spec sp;
48} test_segments [] =
49{
50 { /* 0 */
51 "seq1",
52 "seq(8466,3,52737,4096)",
53 { 0x2,0x4, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00 },
54 10,
55 { "8466 3 52737 4096",
56 "8466 3 52737 4096",
57 4, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
58 },
59 { /* 1 */
60 "seq2",
61 "seq(8722) seq(4)",
62 { 0x2,0x1, 0x22,0x12,
63 0x2,0x1, 0x00,0x04 },
64 8,
65 { "8722 4",
66 "8722 4",
67 2, 0, NOT_ALL_PRIVATE, 4, 5, 8722, },
68 },
69 { /* 2 */
70 "seq3",
71 "seq(8466,3,52737,4096,8722,4)",
72 { 0x2,0x6, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00,
73 0x22,0x12, 0x00,0x04},
74 14,
75 { "8466 3 52737 4096 8722 4",
76 "8466 3 52737 4096 8722 4",
77 6, 0, NOT_ALL_PRIVATE, 3, 5, 8466 },
78 },
79 { /* 3 */
80 "seqset",
81 "seq(8482,51457) set(5204)",
82 { 0x2,0x2, 0x21,0x22, 0xc9,0x01,
83 0x1,0x1, 0x14,0x54 },
84 10,
85 { "8482 51457 {5204}",
86 "8482 51457 {5204}",
Paul Jakma1f742f22006-08-06 15:52:11 +000087 3, 0, NOT_ALL_PRIVATE, 5204, 51456, 8482},
paul1f377b02005-09-10 18:00:11 +000088 },
89 { /* 4 */
90 "seqset2",
91 "seq(8467, 59649) set(4196,48658) set(17322,30745)",
92 { 0x2,0x2, 0x21,0x13, 0xe9,0x01,
93 0x1,0x2, 0x10,0x64, 0xbe,0x12,
94 0x1,0x2, 0x43,0xaa, 0x78,0x19 },
95 18,
96 { "8467 59649 {4196,48658} {17322,30745}",
97 "8467 59649 {4196,48658} {17322,30745}",
98 4, 0, NOT_ALL_PRIVATE, 48658, 1, 8467},
99 },
100 { /* 5 */
101 "multi",
102 "seq(6435,59408,21665) set(2457,61697,4369), seq(1842,41590,51793)",
103 { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
104 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
105 0x2,0x3, 0x07,0x32, 0xa2,0x76, 0xca,0x51 },
106 24,
107 { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
108 "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
109 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
110 },
111 { /* 6 */
112 "confed",
113 "confseq(123,456,789)",
114 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15 },
115 8,
116 { "(123 456 789)",
117 "",
118 0, 3, NOT_ALL_PRIVATE, 789, 1, NULL_ASN },
119 },
120 { /* 7 */
121 "confed2",
122 "confseq(123,456,789) confseq(111,222)",
123 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
124 0x3,0x2, 0x00,0x6f, 0x00,0xde },
125 14,
126 { "(123 456 789) (111 222)",
127 "",
128 0, 5, NOT_ALL_PRIVATE, 111, 1, NULL_ASN },
129 },
130 { /* 8 */
131 "confset",
132 "confset(456,123,789)",
133 { 0x4,0x3, 0x01,0xc8, 0x00,0x7b, 0x03,0x15 },
134 8,
135 { "[123,456,789]",
136 "[123,456,789]",
137 0, 1, NOT_ALL_PRIVATE, 123, 1, NULL_ASN },
138 },
139 { /* 9 */
140 "confmulti",
141 "confseq(123,456,789) confset(222,111) seq(8722) set(4196,48658)",
142 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
143 0x4,0x2, 0x00,0xde, 0x00,0x6f,
144 0x2,0x1, 0x22,0x12,
145 0x1,0x2, 0x10,0x64, 0xbe,0x12 },
146 24,
147 { "(123 456 789) [111,222] 8722 {4196,48658}",
148 "8722 {4196,48658}",
149 2, 4, NOT_ALL_PRIVATE, 123, 1, NULL_ASN },
150 },
151 { /* 10 */
152 "seq4",
153 "seq(8466,2,52737,4096,8722,4)",
154 { 0x2,0x6, 0x21,0x12, 0x00,0x02, 0xce,0x01, 0x10,0x00,
155 0x22,0x12, 0x00,0x04},
156 14,
157 { "8466 2 52737 4096 8722 4",
158 "8466 2 52737 4096 8722 4",
159 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
160 },
161 { /* 11 */
162 "tripleseq1",
163 "seq(8466,2,52737) seq(4096,8722,4) seq(8722)",
164 { 0x2,0x3, 0x21,0x12, 0x00,0x02, 0xce,0x01,
165 0x2,0x3, 0x10,0x00, 0x22,0x12, 0x00,0x04,
166 0x2,0x1, 0x22,0x12},
167 20,
168 { "8466 2 52737 4096 8722 4 8722",
169 "8466 2 52737 4096 8722 4 8722",
170 7, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
171 },
172 { /* 12 */
173 "someprivate",
174 "seq(8466,64512,52737,65535)",
175 { 0x2,0x4, 0x21,0x12, 0xfc,0x00, 0xce,0x01, 0xff,0xff },
176 10,
177 { "8466 64512 52737 65535",
178 "8466 64512 52737 65535",
179 4, 0, NOT_ALL_PRIVATE, 65535, 4, 8466 },
180 },
181 { /* 13 */
182 "allprivate",
183 "seq(65534,64512,64513,65535)",
184 { 0x2,0x4, 0xff,0xfe, 0xfc,0x00, 0xfc,0x01, 0xff,0xff },
185 10,
186 { "65534 64512 64513 65535",
187 "65534 64512 64513 65535",
188 4, 0, ALL_PRIVATE, 65534, 4, 65534 },
189 },
190 { /* 14 */
191 "long",
192 "seq(8466,3,52737,4096,34285,<repeated 49 more times>)",
193 { 0x2,0xfa, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
194 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
195 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
196 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
197 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
198 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
199 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
200 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
201 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
202 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
203 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
204 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
205 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
206 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
207 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
208 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
209 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
210 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
211 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
212 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
213 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
214 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
215 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
216 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
217 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
218 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
219 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
220 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
221 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
222 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
223 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
224 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
225 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
226 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
227 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
228 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
229 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
230 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
231 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
232 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
233 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
234 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
235 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
236 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
237 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
238 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
239 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
240 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
241 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
242 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, },
243 502,
244 { "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
245 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
246 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
247 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
248 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
249 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
250 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
251 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
252 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
253 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
254 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
255 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
256 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
257 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
258 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
259 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
260 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
261 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
262 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
263 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
264 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
265 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
266 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
267 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
268 "8466 3 52737 4096 34285 8466 3 52737 4096 34285",
269
270 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
271 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
272 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
273 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
274 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
275 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
276 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
277 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
278 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
279 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
280 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
281 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
282 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
283 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
284 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
285 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
286 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
287 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
288 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
289 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
290 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
291 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
292 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
293 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
294 "8466 3 52737 4096 34285 8466 3 52737 4096 34285",
295 250, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
296 },
paul59064762005-11-23 02:48:14 +0000297 { /* 15 */
298 "seq1extra",
299 "seq(8466,3,52737,4096,3456)",
300 { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
301 12,
302 { "8466 3 52737 4096 3456",
303 "8466 3 52737 4096 3456",
304 5, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
305 },
306 { /* 16 */
307 "empty",
308 "<empty>",
309 {},
310 0,
311 { "", "", 0, 0, 0, 0, 0, 0 },
312 },
Paul Jakma5d932f62006-03-30 13:42:50 +0000313 { /* 17 */
314 "redundantset",
315 "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153)",
316 { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
317 0x1,0x4, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9 },
318 22,
319 {
320 /* We shouldn't ever /generate/ such paths. However, we should
321 * cope with them fine.
322 */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000323 "8466 3 52737 4096 3456 {7099,8153}",
324 "8466 3 52737 4096 3456 {7099,8153}",
325 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
326 },
327 { /* 18 */
328 "reconcile_lead_asp",
329 "seq(6435,59408,21665) set(23456,23456,23456), seq(23456,23456,23456)",
330 { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
331 0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
332 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
333 24,
334 { "6435 59408 21665 {23456} 23456 23456 23456",
335 "6435 59408 21665 {23456} 23456 23456 23456",
336 7, 0, NOT_ALL_PRIVATE, 23456, 1, 6435 },
337 },
338 { /* 19 */
339 "reconcile_new_asp",
340 "set(2457,61697,4369), seq(1842,41591,51793)",
341 {
342 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
343 0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51 },
344 16,
345 { "{2457,4369,61697} 1842 41591 51793",
346 "{2457,4369,61697} 1842 41591 51793",
347 4, 0, NOT_ALL_PRIVATE, 51793, 1, 2457 },
348 },
349 { /* 20 */
350 "reconcile_confed",
351 "confseq(123,456,789) confset(456,124,788) seq(6435,59408,21665)"
352 " set(23456,23456,23456), seq(23456,23456,23456)",
353 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
354 0x4,0x3, 0x01,0xc8, 0x00,0x7c, 0x03,0x14,
355 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
356 0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
357 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
358 40,
359 { "(123 456 789) [124,456,788] 6435 59408 21665"
360 " {23456} 23456 23456 23456",
361 "6435 59408 21665 {23456} 23456 23456 23456",
362 7, 4, NOT_ALL_PRIVATE, 23456, 1, 6435 },
363 },
364 { /* 21 */
365 "reconcile_start_trans",
366 "seq(23456,23456,23456) seq(6435,59408,21665)",
367 { 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
368 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, },
369 16,
370 { "23456 23456 23456 6435 59408 21665",
371 "23456 23456 23456 6435 59408 21665",
372 6, 0, NOT_ALL_PRIVATE, 21665, 1, 23456 },
373 },
374 { /* 22 */
375 "reconcile_start_trans4",
376 "seq(1842,41591,51793) seq(6435,59408,21665)",
377 { 0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51,
378 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, },
379 16,
380 { "1842 41591 51793 6435 59408 21665",
381 "1842 41591 51793 6435 59408 21665",
382 6, 0, NOT_ALL_PRIVATE, 41591, 1, 1842 },
383 },
384 { /* 23 */
385 "reconcile_start_trans_error",
386 "seq(23456,23456,23456) seq(6435,59408)",
387 { 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
388 0x2,0x2, 0x19,0x23, 0xe8,0x10, },
389 14,
390 { "23456 23456 23456 6435 59408",
391 "23456 23456 23456 6435 59408",
392 5, 0, NOT_ALL_PRIVATE, 59408, 1, 23456 },
393 },
394 { /* 24 */
395 "redundantset2",
396 "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153,7099)",
397 { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
398 0x1,0x5, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9, 0x1b,0xbb,},
399 24,
400 {
401 /* We should weed out duplicate set members. */
402 "8466 3 52737 4096 3456 {7099,8153}",
403 "8466 3 52737 4096 3456 {7099,8153}",
Paul Jakma5d932f62006-03-30 13:42:50 +0000404 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
405 },
Paul Jakmad38e8d62008-02-23 16:57:03 +0000406 { /* 25 */
407 "zero-size overflow",
408 "#ASNs = 0, data = seq(8466 3 52737 4096 3456)",
409 { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
410 12,
411 { "", "",
412 0, 0, 0, 0, 0, 0 },
413 },
414 { /* 26 */
415 "zero-size overflow + valid segment",
416 "seq(#AS=0:8466 3 52737),seq(4096 3456)",
417 { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01,
418 0x2,0x2, 0x10,0x00, 0x0d,0x80 },
419 14
420 ,
421 { "", "",
422 0, 0, 0, 0, 0, 0 },
423 },
Paul Jakma50ef5652010-11-27 23:14:02 +0000424 { /* 27 */
425 "invalid segment type",
426 "type=8(4096 3456)",
427 { 0x8,0x2, 0x10,0x00, 0x0d,0x80 },
428 14
429 ,
430 { "", "",
431 0, 0, 0, 0, 0, 0 },
432 }, { NULL, NULL, {0}, 0, { NULL, 0, 0 } }
433};
434
435/* */
436static struct aspath_tests {
437 const char *desc;
438 const struct test_segment *segment;
439 const char *shouldbe; /* String it should evaluate to */
440 const enum as4 { AS4_DATA, AS2_DATA }
441 as4; /* whether data should be as4 or not (ie as2) */
442 const int result; /* expected result for bgp_attr_parse */
443 const int cap; /* capabilities to set for peer */
444 const char attrheader [1024];
445 size_t len;
446} aspath_tests [] =
447{
448 /* 0 */
449 {
450 "basic test",
451 &test_segments[0],
452 "8466 3 52737 4096",
453 AS2_DATA, 0,
454 0,
455 { BGP_ATTR_FLAG_TRANS,
456 BGP_ATTR_AS_PATH,
457 10,
458 },
459 3,
460 },
461 /* 1 */
462 {
463 "length too short",
464 &test_segments[0],
465 "8466 3 52737 4096",
466 AS2_DATA, -1,
467 0,
468 { BGP_ATTR_FLAG_TRANS,
469 BGP_ATTR_AS_PATH,
470 8,
471 },
472 3,
473 },
474 /* 2 */
475 {
476 "length too long",
477 &test_segments[0],
478 "8466 3 52737 4096",
479 AS2_DATA, -1,
480 0,
481 { BGP_ATTR_FLAG_TRANS,
482 BGP_ATTR_AS_PATH,
483 12,
484 },
485 3,
486 },
487 /* 3 */
488 {
489 "incorrect flag",
490 &test_segments[0],
491 "8466 3 52737 4096",
492 AS2_DATA, -1,
493 0,
494 { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
495 BGP_ATTR_AS_PATH,
496 10,
497 },
498 3,
499 },
500 /* 4 */
501 {
502 "as4_path, with as2 format data",
503 &test_segments[0],
504 "8466 3 52737 4096",
505 AS2_DATA, -1,
506 0,
507 { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
508 BGP_ATTR_AS4_PATH,
509 10,
510 },
511 3,
512 },
513 /* 5 */
514 {
515 "as4, with incorrect attr length",
516 &test_segments[0],
517 "8466 3 52737 4096",
518 AS4_DATA, -1,
519 PEER_CAP_AS4_RCV,
520 { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
521 BGP_ATTR_AS4_PATH,
522 10,
523 },
524 3,
525 },
526 /* 6 */
527 {
528 "basic 4-byte as-path",
529 &test_segments[0],
530 "8466 3 52737 4096",
531 AS4_DATA, 0,
532 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
533 { BGP_ATTR_FLAG_TRANS,
534 BGP_ATTR_AS_PATH,
535 18,
536 },
537 3,
538 },
539 /* 7 */
540 {
541 "4b AS_PATH: too short",
542 &test_segments[0],
543 "8466 3 52737 4096",
544 AS4_DATA, -1,
545 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
546 { BGP_ATTR_FLAG_TRANS,
547 BGP_ATTR_AS_PATH,
548 16,
549 },
550 3,
551 },
552 /* 8 */
553 {
554 "4b AS_PATH: too long",
555 &test_segments[0],
556 "8466 3 52737 4096",
557 AS4_DATA, -1,
558 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
559 { BGP_ATTR_FLAG_TRANS,
560 BGP_ATTR_AS_PATH,
561 20,
562 },
563 3,
564 },
565 /* 9 */
566 {
567 "4b AS_PATH: too long2",
568 &test_segments[0],
569 "8466 3 52737 4096",
570 AS4_DATA, -1,
571 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
572 { BGP_ATTR_FLAG_TRANS,
573 BGP_ATTR_AS_PATH,
574 22,
575 },
576 3,
577 },
578 /* 10 */
579 {
580 "4b AS_PATH: bad flags",
581 &test_segments[0],
582 "8466 3 52737 4096",
583 AS4_DATA, -1,
584 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
585 { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
586 BGP_ATTR_AS_PATH,
587 18,
588 },
589 3,
590 },
591 /* 11 */
592 {
593 "4b AS_PATH: confed",
594 &test_segments[6],
595 "8466 3 52737 4096",
596 AS4_DATA, -1,
597 PEER_CAP_AS4_ADV,
598 { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
599 BGP_ATTR_AS4_PATH,
600 14,
601 },
602 3,
603 },
604 { NULL, NULL, NULL, 0, 0, 0, { 0 }, 0 },
paul1f377b02005-09-10 18:00:11 +0000605};
606
607/* prepending tests */
608static struct tests {
609 const struct test_segment *test1;
610 const struct test_segment *test2;
611 struct test_spec sp;
612} prepend_tests[] =
613{
Paul Jakma50ef5652010-11-27 23:14:02 +0000614 /* 0 */
paul1f377b02005-09-10 18:00:11 +0000615 { &test_segments[0], &test_segments[1],
616 { "8466 3 52737 4096 8722 4",
617 "8466 3 52737 4096 8722 4",
618 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
619 },
Paul Jakma50ef5652010-11-27 23:14:02 +0000620 /* 1 */
paul1f377b02005-09-10 18:00:11 +0000621 { &test_segments[1], &test_segments[3],
622 { "8722 4 8482 51457 {5204}",
623 "8722 4 8482 51457 {5204}",
624 5, 0, NOT_ALL_PRIVATE, 5204, 1, 8722 }
625 },
Paul Jakma50ef5652010-11-27 23:14:02 +0000626 /* 2 */
paul1f377b02005-09-10 18:00:11 +0000627 { &test_segments[3], &test_segments[4],
628 { "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
629 "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
630 7, 0, NOT_ALL_PRIVATE, 5204, 1, 8482 },
631 },
Paul Jakma50ef5652010-11-27 23:14:02 +0000632 /* 3 */
paul1f377b02005-09-10 18:00:11 +0000633 { &test_segments[4], &test_segments[5],
634 { "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
635 " {2457,4369,61697} 1842 41590 51793",
636 "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
637 " {2457,4369,61697} 1842 41590 51793",
638 11, 0, NOT_ALL_PRIVATE, 61697, 1, 8467 }
639 },
Paul Jakma50ef5652010-11-27 23:14:02 +0000640 /* 4 */
paul1f377b02005-09-10 18:00:11 +0000641 { &test_segments[5], &test_segments[6],
Paul Jakma50ef5652010-11-27 23:14:02 +0000642 { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
643 "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
644 7, 0, NOT_ALL_PRIVATE, 1842, 1, 6435 },
paul1f377b02005-09-10 18:00:11 +0000645 },
Paul Jakma50ef5652010-11-27 23:14:02 +0000646 /* 5 */
paul1f377b02005-09-10 18:00:11 +0000647 { &test_segments[6], &test_segments[7],
648 { "(123 456 789) (123 456 789) (111 222)",
649 "",
650 0, 8, NOT_ALL_PRIVATE, 111, 1, 0 }
651 },
652 { &test_segments[7], &test_segments[8],
653 { "(123 456 789) (111 222) [123,456,789]",
654 "",
655 0, 6, NOT_ALL_PRIVATE, 111, 1, 0 }
656 },
657 { &test_segments[8], &test_segments[9],
658 { "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
659 "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
660 2, 5, NOT_ALL_PRIVATE, 456, 1, NULL_ASN },
661 },
662 { &test_segments[9], &test_segments[8],
663 { "(123 456 789) [111,222] 8722 {4196,48658} [123,456,789]",
664 "8722 {4196,48658} [123,456,789]",
665 2, 5, NOT_ALL_PRIVATE, 48658, 1, NULL_ASN },
666 },
667 { &test_segments[14], &test_segments[11],
668 { "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
669 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
670 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
671 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
672 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
673 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
674 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
675 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
676 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
677 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
678 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
679 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
680 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
681 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
682 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
683 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
684 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
685 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
686 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
687 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
688 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
689 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
690 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
691 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
692 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
693 "8466 2 52737 4096 8722 4 8722",
694
695 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
696 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
697 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
698 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
699 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
700 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
701 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
702 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
703 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
704 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
705 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
706 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
707 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
708 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
709 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
710 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
711 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
712 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
713 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
714 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
715 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
716 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
717 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
718 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
719 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
720 "8466 2 52737 4096 8722 4 8722",
721 257, 0, NOT_ALL_PRIVATE, 4096, 1000, 8466 },
722 },
723 { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
724};
725
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000726struct tests reconcile_tests[] =
727{
728 { &test_segments[18], &test_segments[19],
729 { "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
730 "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
731 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
732 },
733 { &test_segments[19], &test_segments[18],
734 /* AS_PATH (19) has more hops than NEW_AS_PATH,
735 * so just AS_PATH should be used (though, this practice
736 * is bad imho).
737 */
738 { "{2457,4369,61697} 1842 41591 51793 6435 59408 21665 {23456} 23456 23456 23456",
739 "{2457,4369,61697} 1842 41591 51793 6435 59408 21665 {23456} 23456 23456 23456",
740 11, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
741 },
742 { &test_segments[20], &test_segments[19],
743 { "(123 456 789) [124,456,788] 6435 59408 21665"
744 " {2457,4369,61697} 1842 41591 51793",
745 "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
746 7, 4, NOT_ALL_PRIVATE, 51793, 1, 6435 },
747 },
748 { &test_segments[21], &test_segments[22],
749 { "1842 41591 51793 6435 59408 21665",
750 "1842 41591 51793 6435 59408 21665",
751 6, 0, NOT_ALL_PRIVATE, 51793, 1, 1842 },
752 },
753 { &test_segments[23], &test_segments[22],
754 { "23456 23456 23456 6435 59408 1842 41591 51793 6435 59408 21665",
755 "23456 23456 23456 6435 59408 1842 41591 51793 6435 59408 21665",
756 11, 0, NOT_ALL_PRIVATE, 51793, 1, 1842 },
757 },
758 { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
759};
760
paul1f377b02005-09-10 18:00:11 +0000761struct tests aggregate_tests[] =
762{
paul1f377b02005-09-10 18:00:11 +0000763 { &test_segments[0], &test_segments[2],
764 { "8466 3 52737 4096 {4,8722}",
765 "8466 3 52737 4096 {4,8722}",
766 5, 0, NOT_ALL_PRIVATE, 4, 1, 8466 },
767 },
768 { &test_segments[2], &test_segments[0],
769 { "8466 3 52737 4096 {4,8722}",
770 "8466 3 52737 4096 {4,8722}",
771 5, 0, NOT_ALL_PRIVATE, 8722, 1, 8466 },
772 },
773 { &test_segments[2], &test_segments[10],
774 { "8466 {2,3,4,4096,8722,52737}",
775 "8466 {2,3,4,4096,8722,52737}",
776 2, 0, NOT_ALL_PRIVATE, 8722, 5, 8466 },
777 },
778 { &test_segments[10], &test_segments[2],
779 { "8466 {2,3,4,4096,8722,52737}",
780 "8466 {2,3,4,4096,8722,52737}",
781 2, 0, NOT_ALL_PRIVATE, 2, 20000, 8466 },
782 },
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000783
784 { &test_segments[5], &test_segments[18],
785 { "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
786 "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
787 4, 0, NOT_ALL_PRIVATE, 41590, 1, 6435 },
788 },
789
paul1f377b02005-09-10 18:00:11 +0000790 { NULL, NULL, { NULL, 0, 0} },
791};
792
793struct compare_tests
794{
795 int test_index1;
796 int test_index2;
797#define CMP_RES_YES 1
798#define CMP_RES_NO 0
799 char shouldbe_cmp;
800 char shouldbe_confed;
801} left_compare [] =
802{
803 { 0, 1, CMP_RES_NO, CMP_RES_NO },
804 { 0, 2, CMP_RES_YES, CMP_RES_NO },
805 { 0, 11, CMP_RES_YES, CMP_RES_NO },
paul59064762005-11-23 02:48:14 +0000806 { 0, 15, CMP_RES_YES, CMP_RES_NO },
807 { 0, 16, CMP_RES_NO, CMP_RES_NO },
paul1f377b02005-09-10 18:00:11 +0000808 { 1, 11, CMP_RES_NO, CMP_RES_NO },
809 { 6, 7, CMP_RES_NO, CMP_RES_YES },
810 { 6, 8, CMP_RES_NO, CMP_RES_NO },
811 { 7, 8, CMP_RES_NO, CMP_RES_NO },
812 { 1, 9, CMP_RES_YES, CMP_RES_NO },
813 { 0, 9, CMP_RES_NO, CMP_RES_NO },
814 { 3, 9, CMP_RES_NO, CMP_RES_NO },
815 { 0, 6, CMP_RES_NO, CMP_RES_NO },
816 { 1, 6, CMP_RES_NO, CMP_RES_NO },
817 { 0, 8, CMP_RES_NO, CMP_RES_NO },
818 { 1, 8, CMP_RES_NO, CMP_RES_NO },
819 { 11, 6, CMP_RES_NO, CMP_RES_NO },
820 { 11, 7, CMP_RES_NO, CMP_RES_NO },
821 { 11, 8, CMP_RES_NO, CMP_RES_NO },
822 { 9, 6, CMP_RES_NO, CMP_RES_YES },
823 { 9, 7, CMP_RES_NO, CMP_RES_YES },
824 { 9, 8, CMP_RES_NO, CMP_RES_NO },
825};
826
827/* make an aspath from a data stream */
828static struct aspath *
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000829make_aspath (const u_char *data, size_t len, int use32bit)
paul1f377b02005-09-10 18:00:11 +0000830{
831 struct stream *s = NULL;
832 struct aspath *as;
833
834 if (len)
835 {
836 s = stream_new (len);
837 stream_put (s, data, len);
838 }
Paul Jakma50ef5652010-11-27 23:14:02 +0000839 as = aspath_parse (s, len, use32bit);
paul59064762005-11-23 02:48:14 +0000840
841 if (s)
842 stream_free (s);
paul1f377b02005-09-10 18:00:11 +0000843
844 return as;
845}
846
847static void
848printbytes (const u_char *bytes, int len)
849{
850 int i = 0;
851 while (i < len)
852 {
853 if (i % 2)
854 printf ("%02hhx%s", bytes[i], " ");
855 else
856 printf ("0x%02hhx", bytes[i]);
857 i++;
858 }
859 printf ("\n");
860}
861
862/* validate the given aspath */
863static int
864validate (struct aspath *as, const struct test_spec *sp)
865{
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000866 size_t bytes, bytes4;
paul1f377b02005-09-10 18:00:11 +0000867 int fails = 0;
868 const u_char *out;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000869 static struct stream *s;
870 struct aspath *asinout, *asconfeddel, *asstr, *as4;
paul1f377b02005-09-10 18:00:11 +0000871
872 out = aspath_snmp_pathseg (as, &bytes);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000873 asinout = make_aspath (out, bytes, 0);
874
875 /* Excercise AS4 parsing a bit, with a dogfood test */
876 if (!s)
877 s = stream_new (4096);
878 bytes4 = aspath_put (s, as, 1);
879 as4 = make_aspath (STREAM_DATA(s), bytes4, 1);
paul1f377b02005-09-10 18:00:11 +0000880
881 asstr = aspath_str2aspath (sp->shouldbe);
882
883 asconfeddel = aspath_delete_confed_seq (aspath_dup (asinout));
884
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000885 printf ("got: %s\n", aspath_print(as));
886
paul1f377b02005-09-10 18:00:11 +0000887 /* the parsed path should match the specified 'shouldbe' string.
888 * We should pass the "eat our own dog food" test, be able to output
889 * this path and then input it again. Ie the path resulting from:
890 *
891 * aspath_parse(aspath_put(as))
892 *
893 * should:
894 *
895 * - also match the specified 'shouldbe' value
896 * - hash to same value as original path
897 * - have same hops and confed counts as original, and as the
898 * the specified counts
899 *
900 * aspath_str2aspath() and shouldbe should match
901 *
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000902 * We do the same for:
903 *
904 * aspath_parse(aspath_put(as,USE32BIT))
905 *
paul1f377b02005-09-10 18:00:11 +0000906 * Confederation related tests:
907 * - aspath_delete_confed_seq(aspath) should match shouldbe_confed
908 * - aspath_delete_confed_seq should be idempotent.
909 */
910 if (strcmp(aspath_print (as), sp->shouldbe)
911 /* hash validation */
912 || (aspath_key_make (as) != aspath_key_make (asinout))
913 /* by string */
914 || strcmp(aspath_print (asinout), sp->shouldbe)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000915 /* By 4-byte parsing */
916 || strcmp(aspath_print (as4), sp->shouldbe)
paul1f377b02005-09-10 18:00:11 +0000917 /* by various path counts */
918 || (aspath_count_hops (as) != sp->hops)
919 || (aspath_count_confeds (as) != sp->confeds)
920 || (aspath_count_hops (asinout) != sp->hops)
921 || (aspath_count_confeds (asinout) != sp->confeds))
922 {
923 failed++;
924 fails++;
925 printf ("shouldbe:\n%s\n", sp->shouldbe);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000926 printf ("as4:\n%s\n", aspath_print (as4));
paul1f377b02005-09-10 18:00:11 +0000927 printf ("hash keys: in: %d out->in: %d\n",
928 aspath_key_make (as), aspath_key_make (asinout));
929 printf ("hops: %d, counted %d %d\n", sp->hops,
930 aspath_count_hops (as),
931 aspath_count_hops (asinout) );
932 printf ("confeds: %d, counted %d %d\n", sp->confeds,
933 aspath_count_confeds (as),
934 aspath_count_confeds (asinout));
935 printf ("out->in:\n%s\nbytes: ", aspath_print(asinout));
936 printbytes (out, bytes);
937 }
938 /* basic confed related tests */
939 if ((aspath_print (asconfeddel) == NULL
940 && sp->shouldbe_delete_confed != NULL)
941 || (aspath_print (asconfeddel) != NULL
942 && sp->shouldbe_delete_confed == NULL)
943 || strcmp(aspath_print (asconfeddel), sp->shouldbe_delete_confed)
944 /* delete_confed_seq should be idempotent */
945 || (aspath_key_make (asconfeddel)
946 != aspath_key_make (aspath_delete_confed_seq (asconfeddel))))
947 {
948 failed++;
949 fails++;
950 printf ("confed_del: %s\n", aspath_print (asconfeddel));
951 printf ("should be: %s\n", sp->shouldbe_delete_confed);
952 }
953 /* aspath_str2aspath test */
954 if ((aspath_print (asstr) == NULL && sp->shouldbe != NULL)
955 || (aspath_print (asstr) != NULL && sp->shouldbe == NULL)
956 || strcmp(aspath_print (asstr), sp->shouldbe))
957 {
958 failed++;
959 fails++;
960 printf ("asstr: %s\n", aspath_print (asstr));
961 }
962
963 /* loop, private and first as checks */
Paul Jakma1f742f22006-08-06 15:52:11 +0000964 if ((sp->does_loop && aspath_loop_check (as, sp->does_loop) == 0)
965 || (sp->doesnt_loop && aspath_loop_check (as, sp->doesnt_loop) != 0)
paul1f377b02005-09-10 18:00:11 +0000966 || (aspath_private_as_check (as) != sp->private_as)
967 || (aspath_firstas_check (as,sp->first)
968 && sp->first == 0))
969 {
970 failed++;
971 fails++;
972 printf ("firstas: %d, got %d\n", sp->first,
973 aspath_firstas_check (as,sp->first));
974 printf ("loop does: %d %d, doesnt: %d %d\n",
975 sp->does_loop, aspath_loop_check (as, sp->does_loop),
976 sp->doesnt_loop, aspath_loop_check (as, sp->doesnt_loop));
977 printf ("private check: %d %d\n", sp->private_as,
978 aspath_private_as_check (as));
979 }
980 aspath_unintern (asinout);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000981 aspath_unintern (as4);
982
paul6726f952006-01-16 12:06:00 +0000983 aspath_free (asconfeddel);
984 aspath_free (asstr);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000985 stream_reset (s);
paul6726f952006-01-16 12:06:00 +0000986
paul1f377b02005-09-10 18:00:11 +0000987 return fails;
paul1f377b02005-09-10 18:00:11 +0000988}
989
990static void
991empty_get_test ()
992{
993 struct aspath *as = aspath_empty_get ();
994 struct test_spec sp = { "", "", 0, 0, 0, 0, 0, 0 };
995
996 printf ("empty_get_test, as: %s\n",aspath_print (as));
997 if (!validate (as, &sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000998 printf ("%s\n", OK);
paul1f377b02005-09-10 18:00:11 +0000999 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001000 printf ("%s!\n", FAILED);
paul1f377b02005-09-10 18:00:11 +00001001
1002 printf ("\n");
paul6726f952006-01-16 12:06:00 +00001003
1004 aspath_free (as);
paul1f377b02005-09-10 18:00:11 +00001005}
1006
1007/* basic parsing test */
1008static void
1009parse_test (struct test_segment *t)
1010{
1011 struct aspath *asp;
1012
1013 printf ("%s: %s\n", t->name, t->desc);
1014
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001015 asp = make_aspath (t->asdata, t->len, 0);
paul1f377b02005-09-10 18:00:11 +00001016
1017 printf ("aspath: %s\nvalidating...:\n", aspath_print (asp));
1018
1019 if (!validate (asp, &t->sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001020 printf (OK "\n");
paul1f377b02005-09-10 18:00:11 +00001021 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001022 printf (FAILED "\n");
paul1f377b02005-09-10 18:00:11 +00001023
1024 printf ("\n");
1025 aspath_unintern (asp);
1026}
1027
1028/* prepend testing */
Paul Jakma5d932f62006-03-30 13:42:50 +00001029static void
paul1f377b02005-09-10 18:00:11 +00001030prepend_test (struct tests *t)
1031{
1032 struct aspath *asp1, *asp2, *ascratch;
1033
1034 printf ("prepend %s: %s\n", t->test1->name, t->test1->desc);
1035 printf ("to %s: %s\n", t->test2->name, t->test2->desc);
1036
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001037 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
1038 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
paul1f377b02005-09-10 18:00:11 +00001039
1040 ascratch = aspath_dup (asp2);
1041 aspath_unintern (asp2);
1042
1043 asp2 = aspath_prepend (asp1, ascratch);
1044
1045 printf ("aspath: %s\n", aspath_print (asp2));
1046
1047 if (!validate (asp2, &t->sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001048 printf ("%s\n", OK);
paul1f377b02005-09-10 18:00:11 +00001049 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001050 printf ("%s!\n", FAILED);
paul1f377b02005-09-10 18:00:11 +00001051
1052 printf ("\n");
1053 aspath_unintern (asp1);
1054 aspath_free (asp2);
1055}
1056
1057/* empty-prepend testing */
Paul Jakma5d932f62006-03-30 13:42:50 +00001058static void
paul1f377b02005-09-10 18:00:11 +00001059empty_prepend_test (struct test_segment *t)
1060{
1061 struct aspath *asp1, *asp2, *ascratch;
1062
1063 printf ("empty prepend %s: %s\n", t->name, t->desc);
1064
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001065 asp1 = make_aspath (t->asdata, t->len, 0);
paul1f377b02005-09-10 18:00:11 +00001066 asp2 = aspath_empty ();
1067
1068 ascratch = aspath_dup (asp2);
1069 aspath_unintern (asp2);
1070
1071 asp2 = aspath_prepend (asp1, ascratch);
1072
1073 printf ("aspath: %s\n", aspath_print (asp2));
1074
1075 if (!validate (asp2, &t->sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001076 printf (OK "\n");
paul1f377b02005-09-10 18:00:11 +00001077 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001078 printf (FAILED "!\n");
paul1f377b02005-09-10 18:00:11 +00001079
1080 printf ("\n");
1081 aspath_unintern (asp1);
1082 aspath_free (asp2);
1083}
1084
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001085/* as2+as4 reconciliation testing */
1086static void
1087as4_reconcile_test (struct tests *t)
1088{
1089 struct aspath *asp1, *asp2, *ascratch;
1090
1091 printf ("reconciling %s:\n %s\n", t->test1->name, t->test1->desc);
1092 printf ("with %s:\n %s\n", t->test2->name, t->test2->desc);
1093
1094 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
1095 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
1096
1097 ascratch = aspath_reconcile_as4 (asp1, asp2);
1098
1099 if (!validate (ascratch, &t->sp))
1100 printf (OK "\n");
1101 else
1102 printf (FAILED "!\n");
1103
1104 printf ("\n");
1105 aspath_unintern (asp1);
1106 aspath_unintern (asp2);
1107 aspath_free (ascratch);
1108}
1109
1110
paul1f377b02005-09-10 18:00:11 +00001111/* aggregation testing */
Paul Jakma5d932f62006-03-30 13:42:50 +00001112static void
paul1f377b02005-09-10 18:00:11 +00001113aggregate_test (struct tests *t)
1114{
1115 struct aspath *asp1, *asp2, *ascratch;
1116
1117 printf ("aggregate %s: %s\n", t->test1->name, t->test1->desc);
1118 printf ("with %s: %s\n", t->test2->name, t->test2->desc);
1119
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001120 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
1121 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
paul1f377b02005-09-10 18:00:11 +00001122
1123 ascratch = aspath_aggregate (asp1, asp2);
1124
paul1f377b02005-09-10 18:00:11 +00001125 if (!validate (ascratch, &t->sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001126 printf (OK "\n");
paul1f377b02005-09-10 18:00:11 +00001127 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001128 printf (FAILED "!\n");
paul1f377b02005-09-10 18:00:11 +00001129
1130 printf ("\n");
1131 aspath_unintern (asp1);
1132 aspath_unintern (asp2);
1133 aspath_free (ascratch);
1134/* aspath_unintern (ascratch);*/
1135}
1136
1137/* cmp_left tests */
1138static void
1139cmp_test ()
1140{
paul84771ee2005-10-11 03:48:28 +00001141 unsigned int i;
paul1f377b02005-09-10 18:00:11 +00001142#define CMP_TESTS_MAX \
1143 (sizeof(left_compare) / sizeof (struct compare_tests))
1144
1145 for (i = 0; i < CMP_TESTS_MAX; i++)
1146 {
1147 struct test_segment *t1 = &test_segments[left_compare[i].test_index1];
1148 struct test_segment *t2 = &test_segments[left_compare[i].test_index2];
1149 struct aspath *asp1, *asp2;
1150
1151 printf ("left cmp %s: %s\n", t1->name, t1->desc);
1152 printf ("and %s: %s\n", t2->name, t2->desc);
1153
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001154 asp1 = make_aspath (t1->asdata, t1->len, 0);
1155 asp2 = make_aspath (t2->asdata, t2->len, 0);
paul1f377b02005-09-10 18:00:11 +00001156
1157 if (aspath_cmp_left (asp1, asp2) != left_compare[i].shouldbe_cmp
1158 || aspath_cmp_left (asp2, asp1) != left_compare[i].shouldbe_cmp
1159 || aspath_cmp_left_confed (asp1, asp2)
1160 != left_compare[i].shouldbe_confed
1161 || aspath_cmp_left_confed (asp2, asp1)
1162 != left_compare[i].shouldbe_confed)
1163 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001164 failed++;
1165 printf (FAILED "\n");
paul1f377b02005-09-10 18:00:11 +00001166 printf ("result should be: cmp: %d, confed: %d\n",
1167 left_compare[i].shouldbe_cmp,
1168 left_compare[i].shouldbe_confed);
1169 printf ("got: cmp %d, cmp_confed: %d\n",
1170 aspath_cmp_left (asp1, asp2),
1171 aspath_cmp_left_confed (asp1, asp2));
1172 printf("path1: %s\npath2: %s\n", aspath_print (asp1),
1173 aspath_print (asp2));
paul1f377b02005-09-10 18:00:11 +00001174 }
1175 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001176 printf (OK "\n");
paul1f377b02005-09-10 18:00:11 +00001177
1178 printf ("\n");
1179 aspath_unintern (asp1);
1180 aspath_unintern (asp2);
1181 }
1182}
Paul Jakma50ef5652010-11-27 23:14:02 +00001183
1184static int
1185handle_attr_test (struct aspath_tests *t)
1186{
1187 struct bgp bgp = { 0 };
1188 struct peer peer = { 0 };
1189 struct attr attr = { 0 };
1190 int ret;
1191 int initfail = failed;
1192 struct aspath *asp;
1193 size_t datalen;
1194
1195 asp = make_aspath (t->segment->asdata, t->segment->len, 0);
1196
1197 peer.ibuf = stream_new (BGP_MAX_PACKET_SIZE);
1198 peer.obuf = stream_fifo_new ();
1199 peer.bgp = &bgp;
1200 peer.host = (char *)"none";
1201 peer.fd = -1;
1202 peer.cap = t->cap;
1203
1204 stream_write (peer.ibuf, t->attrheader, t->len);
1205 datalen = aspath_put (peer.ibuf, asp, t->as4 == AS4_DATA);
1206
1207 ret = bgp_attr_parse (&peer, &attr, t->len + datalen, NULL, NULL);
1208
1209 if (ret != t->result)
1210 {
1211 printf ("bgp_attr_parse returned %d, expected %d\n", ret, t->result);
1212 printf ("datalen %d\n", datalen);
1213 failed++;
1214 }
1215 if (ret != 0)
1216 goto out;
1217
1218 if (attr.aspath == NULL)
1219 {
1220 printf ("aspath is NULL!\n");
1221 failed++;
1222 }
1223 if (attr.aspath && strcmp (attr.aspath->str, t->shouldbe))
1224 {
1225 printf ("attr str and 'shouldbe' mismatched!\n"
1226 "attr str: %s\n"
1227 "shouldbe: %s\n",
1228 attr.aspath->str, t->shouldbe);
1229 failed++;
1230 }
1231
1232out:
1233 if (attr.aspath)
1234 aspath_unintern (attr.aspath);
1235 if (asp)
1236 aspath_unintern (asp);
1237 return failed - initfail;
1238}
1239
1240static void
1241attr_test (struct aspath_tests *t)
1242{
1243 printf ("%s\n", t->desc);
1244 printf ("%s\n\n", handle_attr_test (t) ? FAILED : OK);
1245}
1246
paul1f377b02005-09-10 18:00:11 +00001247int
1248main (void)
1249{
1250 int i = 0;
Paul Jakma50ef5652010-11-27 23:14:02 +00001251 bgp_master_init ();
1252 master = bm->master;
1253 bgp_attr_init ();
1254
paul1f377b02005-09-10 18:00:11 +00001255 while (test_segments[i].name)
1256 {
Paul Jakma50ef5652010-11-27 23:14:02 +00001257 printf ("test %u\n", i);
paul1f377b02005-09-10 18:00:11 +00001258 parse_test (&test_segments[i]);
1259 empty_prepend_test (&test_segments[i++]);
1260 }
1261
1262 i = 0;
1263 while (prepend_tests[i].test1)
Paul Jakma50ef5652010-11-27 23:14:02 +00001264 {
1265 printf ("prepend test %u\n", i);
1266 prepend_test (&prepend_tests[i++]);
1267 }
paul1f377b02005-09-10 18:00:11 +00001268
1269 i = 0;
1270 while (aggregate_tests[i].test1)
Paul Jakma50ef5652010-11-27 23:14:02 +00001271 {
1272 printf ("aggregate test %u\n", i);
1273 aggregate_test (&aggregate_tests[i++]);
1274 }
paul1f377b02005-09-10 18:00:11 +00001275
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001276 i = 0;
1277
1278 while (reconcile_tests[i].test1)
Paul Jakma50ef5652010-11-27 23:14:02 +00001279 {
1280 printf ("reconcile test %u\n", i);
1281 as4_reconcile_test (&reconcile_tests[i++]);
1282 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001283
1284 i = 0;
1285
paul1f377b02005-09-10 18:00:11 +00001286 cmp_test();
1287
1288 i = 0;
1289
1290 empty_get_test();
1291
Paul Jakma50ef5652010-11-27 23:14:02 +00001292 i = 0;
1293
1294 while (aspath_tests[i].desc)
1295 {
1296 printf ("aspath_attr test %d\n", i);
1297 attr_test (&aspath_tests[i++]);
1298 }
1299
paul1f377b02005-09-10 18:00:11 +00001300 printf ("failures: %d\n", failed);
1301 printf ("aspath count: %ld\n", aspath_count());
1302
1303 return (failed + aspath_count());
1304}