blob: 9170455fbdcb6d20b7d9087c5422a2f6715d83ac [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,
Paul Jakmab881c702010-11-23 16:35:42 +0000411 { NULL, NULL,
Paul Jakmad38e8d62008-02-23 16:57:03 +0000412 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 ,
Paul Jakmab881c702010-11-23 16:35:42 +0000421 { NULL, NULL,
Paul Jakmad38e8d62008-02-23 16:57:03 +0000422 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 ,
Paul Jakmab881c702010-11-23 16:35:42 +0000430 { NULL, NULL,
Paul Jakma50ef5652010-11-27 23:14:02 +0000431 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
Paul Jakmab881c702010-11-23 16:35:42 +0000872 if (as == NULL && sp->shouldbe == NULL)
873 {
874 printf ("Correctly failed to parse\n");
875 return fails;
876 }
877
paul1f377b02005-09-10 18:00:11 +0000878 out = aspath_snmp_pathseg (as, &bytes);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000879 asinout = make_aspath (out, bytes, 0);
880
881 /* Excercise AS4 parsing a bit, with a dogfood test */
882 if (!s)
883 s = stream_new (4096);
884 bytes4 = aspath_put (s, as, 1);
885 as4 = make_aspath (STREAM_DATA(s), bytes4, 1);
paul1f377b02005-09-10 18:00:11 +0000886
887 asstr = aspath_str2aspath (sp->shouldbe);
888
889 asconfeddel = aspath_delete_confed_seq (aspath_dup (asinout));
890
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000891 printf ("got: %s\n", aspath_print(as));
892
paul1f377b02005-09-10 18:00:11 +0000893 /* the parsed path should match the specified 'shouldbe' string.
894 * We should pass the "eat our own dog food" test, be able to output
895 * this path and then input it again. Ie the path resulting from:
896 *
897 * aspath_parse(aspath_put(as))
898 *
899 * should:
900 *
901 * - also match the specified 'shouldbe' value
902 * - hash to same value as original path
903 * - have same hops and confed counts as original, and as the
904 * the specified counts
905 *
906 * aspath_str2aspath() and shouldbe should match
907 *
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000908 * We do the same for:
909 *
910 * aspath_parse(aspath_put(as,USE32BIT))
911 *
paul1f377b02005-09-10 18:00:11 +0000912 * Confederation related tests:
913 * - aspath_delete_confed_seq(aspath) should match shouldbe_confed
914 * - aspath_delete_confed_seq should be idempotent.
915 */
916 if (strcmp(aspath_print (as), sp->shouldbe)
917 /* hash validation */
918 || (aspath_key_make (as) != aspath_key_make (asinout))
919 /* by string */
920 || strcmp(aspath_print (asinout), sp->shouldbe)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000921 /* By 4-byte parsing */
922 || strcmp(aspath_print (as4), sp->shouldbe)
paul1f377b02005-09-10 18:00:11 +0000923 /* by various path counts */
924 || (aspath_count_hops (as) != sp->hops)
925 || (aspath_count_confeds (as) != sp->confeds)
926 || (aspath_count_hops (asinout) != sp->hops)
927 || (aspath_count_confeds (asinout) != sp->confeds))
928 {
929 failed++;
930 fails++;
931 printf ("shouldbe:\n%s\n", sp->shouldbe);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000932 printf ("as4:\n%s\n", aspath_print (as4));
paul1f377b02005-09-10 18:00:11 +0000933 printf ("hash keys: in: %d out->in: %d\n",
934 aspath_key_make (as), aspath_key_make (asinout));
935 printf ("hops: %d, counted %d %d\n", sp->hops,
936 aspath_count_hops (as),
937 aspath_count_hops (asinout) );
938 printf ("confeds: %d, counted %d %d\n", sp->confeds,
939 aspath_count_confeds (as),
940 aspath_count_confeds (asinout));
941 printf ("out->in:\n%s\nbytes: ", aspath_print(asinout));
942 printbytes (out, bytes);
943 }
944 /* basic confed related tests */
945 if ((aspath_print (asconfeddel) == NULL
946 && sp->shouldbe_delete_confed != NULL)
947 || (aspath_print (asconfeddel) != NULL
948 && sp->shouldbe_delete_confed == NULL)
949 || strcmp(aspath_print (asconfeddel), sp->shouldbe_delete_confed)
950 /* delete_confed_seq should be idempotent */
951 || (aspath_key_make (asconfeddel)
952 != aspath_key_make (aspath_delete_confed_seq (asconfeddel))))
953 {
954 failed++;
955 fails++;
956 printf ("confed_del: %s\n", aspath_print (asconfeddel));
957 printf ("should be: %s\n", sp->shouldbe_delete_confed);
958 }
959 /* aspath_str2aspath test */
960 if ((aspath_print (asstr) == NULL && sp->shouldbe != NULL)
961 || (aspath_print (asstr) != NULL && sp->shouldbe == NULL)
962 || strcmp(aspath_print (asstr), sp->shouldbe))
963 {
964 failed++;
965 fails++;
966 printf ("asstr: %s\n", aspath_print (asstr));
967 }
968
969 /* loop, private and first as checks */
Paul Jakma1f742f22006-08-06 15:52:11 +0000970 if ((sp->does_loop && aspath_loop_check (as, sp->does_loop) == 0)
971 || (sp->doesnt_loop && aspath_loop_check (as, sp->doesnt_loop) != 0)
paul1f377b02005-09-10 18:00:11 +0000972 || (aspath_private_as_check (as) != sp->private_as)
973 || (aspath_firstas_check (as,sp->first)
974 && sp->first == 0))
975 {
976 failed++;
977 fails++;
978 printf ("firstas: %d, got %d\n", sp->first,
979 aspath_firstas_check (as,sp->first));
980 printf ("loop does: %d %d, doesnt: %d %d\n",
981 sp->does_loop, aspath_loop_check (as, sp->does_loop),
982 sp->doesnt_loop, aspath_loop_check (as, sp->doesnt_loop));
983 printf ("private check: %d %d\n", sp->private_as,
984 aspath_private_as_check (as));
985 }
Paul Jakma1dba2542012-05-01 16:20:33 +0100986 aspath_unintern (&asinout);
987 aspath_unintern (&as4);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000988
paul6726f952006-01-16 12:06:00 +0000989 aspath_free (asconfeddel);
990 aspath_free (asstr);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000991 stream_reset (s);
paul6726f952006-01-16 12:06:00 +0000992
paul1f377b02005-09-10 18:00:11 +0000993 return fails;
paul1f377b02005-09-10 18:00:11 +0000994}
995
996static void
997empty_get_test ()
998{
999 struct aspath *as = aspath_empty_get ();
1000 struct test_spec sp = { "", "", 0, 0, 0, 0, 0, 0 };
1001
1002 printf ("empty_get_test, as: %s\n",aspath_print (as));
1003 if (!validate (as, &sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001004 printf ("%s\n", OK);
paul1f377b02005-09-10 18:00:11 +00001005 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001006 printf ("%s!\n", FAILED);
paul1f377b02005-09-10 18:00:11 +00001007
1008 printf ("\n");
paul6726f952006-01-16 12:06:00 +00001009
1010 aspath_free (as);
paul1f377b02005-09-10 18:00:11 +00001011}
1012
1013/* basic parsing test */
1014static void
1015parse_test (struct test_segment *t)
1016{
1017 struct aspath *asp;
1018
1019 printf ("%s: %s\n", t->name, t->desc);
1020
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001021 asp = make_aspath (t->asdata, t->len, 0);
Paul Jakmab881c702010-11-23 16:35:42 +00001022
paul1f377b02005-09-10 18:00:11 +00001023 printf ("aspath: %s\nvalidating...:\n", aspath_print (asp));
1024
1025 if (!validate (asp, &t->sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001026 printf (OK "\n");
paul1f377b02005-09-10 18:00:11 +00001027 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001028 printf (FAILED "\n");
paul1f377b02005-09-10 18:00:11 +00001029
1030 printf ("\n");
Paul Jakmab881c702010-11-23 16:35:42 +00001031
1032 if (asp)
Paul Jakma1dba2542012-05-01 16:20:33 +01001033 aspath_unintern (&asp);
paul1f377b02005-09-10 18:00:11 +00001034}
1035
1036/* prepend testing */
Paul Jakma5d932f62006-03-30 13:42:50 +00001037static void
paul1f377b02005-09-10 18:00:11 +00001038prepend_test (struct tests *t)
1039{
1040 struct aspath *asp1, *asp2, *ascratch;
1041
1042 printf ("prepend %s: %s\n", t->test1->name, t->test1->desc);
1043 printf ("to %s: %s\n", t->test2->name, t->test2->desc);
1044
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001045 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
1046 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
paul1f377b02005-09-10 18:00:11 +00001047
1048 ascratch = aspath_dup (asp2);
Paul Jakma1dba2542012-05-01 16:20:33 +01001049 aspath_unintern (&asp2);
paul1f377b02005-09-10 18:00:11 +00001050
1051 asp2 = aspath_prepend (asp1, ascratch);
1052
1053 printf ("aspath: %s\n", aspath_print (asp2));
1054
1055 if (!validate (asp2, &t->sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001056 printf ("%s\n", OK);
paul1f377b02005-09-10 18:00:11 +00001057 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001058 printf ("%s!\n", FAILED);
paul1f377b02005-09-10 18:00:11 +00001059
1060 printf ("\n");
Paul Jakma1dba2542012-05-01 16:20:33 +01001061 aspath_unintern (&asp1);
paul1f377b02005-09-10 18:00:11 +00001062 aspath_free (asp2);
1063}
1064
1065/* empty-prepend testing */
Paul Jakma5d932f62006-03-30 13:42:50 +00001066static void
paul1f377b02005-09-10 18:00:11 +00001067empty_prepend_test (struct test_segment *t)
1068{
1069 struct aspath *asp1, *asp2, *ascratch;
1070
1071 printf ("empty prepend %s: %s\n", t->name, t->desc);
1072
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001073 asp1 = make_aspath (t->asdata, t->len, 0);
paul1f377b02005-09-10 18:00:11 +00001074 asp2 = aspath_empty ();
1075
1076 ascratch = aspath_dup (asp2);
Paul Jakma1dba2542012-05-01 16:20:33 +01001077 aspath_unintern (&asp2);
paul1f377b02005-09-10 18:00:11 +00001078
1079 asp2 = aspath_prepend (asp1, ascratch);
1080
1081 printf ("aspath: %s\n", aspath_print (asp2));
1082
1083 if (!validate (asp2, &t->sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001084 printf (OK "\n");
paul1f377b02005-09-10 18:00:11 +00001085 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001086 printf (FAILED "!\n");
paul1f377b02005-09-10 18:00:11 +00001087
1088 printf ("\n");
Paul Jakmab881c702010-11-23 16:35:42 +00001089 if (asp1)
Paul Jakma1dba2542012-05-01 16:20:33 +01001090 aspath_unintern (&asp1);
paul1f377b02005-09-10 18:00:11 +00001091 aspath_free (asp2);
1092}
1093
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001094/* as2+as4 reconciliation testing */
1095static void
1096as4_reconcile_test (struct tests *t)
1097{
1098 struct aspath *asp1, *asp2, *ascratch;
1099
1100 printf ("reconciling %s:\n %s\n", t->test1->name, t->test1->desc);
1101 printf ("with %s:\n %s\n", t->test2->name, t->test2->desc);
1102
1103 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
1104 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
1105
1106 ascratch = aspath_reconcile_as4 (asp1, asp2);
1107
1108 if (!validate (ascratch, &t->sp))
1109 printf (OK "\n");
1110 else
1111 printf (FAILED "!\n");
1112
1113 printf ("\n");
Paul Jakma1dba2542012-05-01 16:20:33 +01001114 aspath_unintern (&asp1);
1115 aspath_unintern (&asp2);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001116 aspath_free (ascratch);
1117}
1118
1119
paul1f377b02005-09-10 18:00:11 +00001120/* aggregation testing */
Paul Jakma5d932f62006-03-30 13:42:50 +00001121static void
paul1f377b02005-09-10 18:00:11 +00001122aggregate_test (struct tests *t)
1123{
1124 struct aspath *asp1, *asp2, *ascratch;
1125
1126 printf ("aggregate %s: %s\n", t->test1->name, t->test1->desc);
1127 printf ("with %s: %s\n", t->test2->name, t->test2->desc);
1128
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001129 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
1130 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
paul1f377b02005-09-10 18:00:11 +00001131
1132 ascratch = aspath_aggregate (asp1, asp2);
1133
paul1f377b02005-09-10 18:00:11 +00001134 if (!validate (ascratch, &t->sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001135 printf (OK "\n");
paul1f377b02005-09-10 18:00:11 +00001136 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001137 printf (FAILED "!\n");
paul1f377b02005-09-10 18:00:11 +00001138
1139 printf ("\n");
Paul Jakma1dba2542012-05-01 16:20:33 +01001140 aspath_unintern (&asp1);
1141 aspath_unintern (&asp2);
paul1f377b02005-09-10 18:00:11 +00001142 aspath_free (ascratch);
1143/* aspath_unintern (ascratch);*/
1144}
1145
1146/* cmp_left tests */
1147static void
1148cmp_test ()
1149{
paul84771ee2005-10-11 03:48:28 +00001150 unsigned int i;
paul1f377b02005-09-10 18:00:11 +00001151#define CMP_TESTS_MAX \
1152 (sizeof(left_compare) / sizeof (struct compare_tests))
1153
1154 for (i = 0; i < CMP_TESTS_MAX; i++)
1155 {
1156 struct test_segment *t1 = &test_segments[left_compare[i].test_index1];
1157 struct test_segment *t2 = &test_segments[left_compare[i].test_index2];
1158 struct aspath *asp1, *asp2;
1159
1160 printf ("left cmp %s: %s\n", t1->name, t1->desc);
1161 printf ("and %s: %s\n", t2->name, t2->desc);
1162
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001163 asp1 = make_aspath (t1->asdata, t1->len, 0);
1164 asp2 = make_aspath (t2->asdata, t2->len, 0);
paul1f377b02005-09-10 18:00:11 +00001165
1166 if (aspath_cmp_left (asp1, asp2) != left_compare[i].shouldbe_cmp
1167 || aspath_cmp_left (asp2, asp1) != left_compare[i].shouldbe_cmp
1168 || aspath_cmp_left_confed (asp1, asp2)
1169 != left_compare[i].shouldbe_confed
1170 || aspath_cmp_left_confed (asp2, asp1)
1171 != left_compare[i].shouldbe_confed)
1172 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001173 failed++;
1174 printf (FAILED "\n");
paul1f377b02005-09-10 18:00:11 +00001175 printf ("result should be: cmp: %d, confed: %d\n",
1176 left_compare[i].shouldbe_cmp,
1177 left_compare[i].shouldbe_confed);
1178 printf ("got: cmp %d, cmp_confed: %d\n",
1179 aspath_cmp_left (asp1, asp2),
1180 aspath_cmp_left_confed (asp1, asp2));
1181 printf("path1: %s\npath2: %s\n", aspath_print (asp1),
1182 aspath_print (asp2));
paul1f377b02005-09-10 18:00:11 +00001183 }
1184 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001185 printf (OK "\n");
paul1f377b02005-09-10 18:00:11 +00001186
1187 printf ("\n");
Paul Jakma1dba2542012-05-01 16:20:33 +01001188 aspath_unintern (&asp1);
1189 aspath_unintern (&asp2);
paul1f377b02005-09-10 18:00:11 +00001190 }
1191}
Paul Jakma50ef5652010-11-27 23:14:02 +00001192
1193static int
1194handle_attr_test (struct aspath_tests *t)
1195{
1196 struct bgp bgp = { 0 };
1197 struct peer peer = { 0 };
1198 struct attr attr = { 0 };
1199 int ret;
1200 int initfail = failed;
1201 struct aspath *asp;
1202 size_t datalen;
1203
1204 asp = make_aspath (t->segment->asdata, t->segment->len, 0);
1205
1206 peer.ibuf = stream_new (BGP_MAX_PACKET_SIZE);
1207 peer.obuf = stream_fifo_new ();
1208 peer.bgp = &bgp;
1209 peer.host = (char *)"none";
1210 peer.fd = -1;
1211 peer.cap = t->cap;
1212
1213 stream_write (peer.ibuf, t->attrheader, t->len);
1214 datalen = aspath_put (peer.ibuf, asp, t->as4 == AS4_DATA);
1215
1216 ret = bgp_attr_parse (&peer, &attr, t->len + datalen, NULL, NULL);
1217
1218 if (ret != t->result)
1219 {
1220 printf ("bgp_attr_parse returned %d, expected %d\n", ret, t->result);
Paul Jakma1dba2542012-05-01 16:20:33 +01001221 printf ("datalen %zd\n", datalen);
Paul Jakma50ef5652010-11-27 23:14:02 +00001222 failed++;
1223 }
1224 if (ret != 0)
1225 goto out;
1226
1227 if (attr.aspath == NULL)
1228 {
1229 printf ("aspath is NULL!\n");
1230 failed++;
1231 }
1232 if (attr.aspath && strcmp (attr.aspath->str, t->shouldbe))
1233 {
1234 printf ("attr str and 'shouldbe' mismatched!\n"
1235 "attr str: %s\n"
1236 "shouldbe: %s\n",
1237 attr.aspath->str, t->shouldbe);
1238 failed++;
1239 }
1240
1241out:
1242 if (attr.aspath)
Paul Jakma1dba2542012-05-01 16:20:33 +01001243 aspath_unintern (&attr.aspath);
Paul Jakma50ef5652010-11-27 23:14:02 +00001244 if (asp)
Paul Jakma1dba2542012-05-01 16:20:33 +01001245 aspath_unintern (&asp);
Paul Jakma50ef5652010-11-27 23:14:02 +00001246 return failed - initfail;
1247}
1248
1249static void
1250attr_test (struct aspath_tests *t)
1251{
1252 printf ("%s\n", t->desc);
1253 printf ("%s\n\n", handle_attr_test (t) ? FAILED : OK);
1254}
1255
paul1f377b02005-09-10 18:00:11 +00001256int
1257main (void)
1258{
1259 int i = 0;
Paul Jakma50ef5652010-11-27 23:14:02 +00001260 bgp_master_init ();
1261 master = bm->master;
Paul Jakmac9e4f862012-06-14 10:42:39 +01001262 bgp_option_set (BGP_OPT_NO_LISTEN);
Paul Jakma50ef5652010-11-27 23:14:02 +00001263 bgp_attr_init ();
1264
paul1f377b02005-09-10 18:00:11 +00001265 while (test_segments[i].name)
1266 {
Paul Jakma50ef5652010-11-27 23:14:02 +00001267 printf ("test %u\n", i);
paul1f377b02005-09-10 18:00:11 +00001268 parse_test (&test_segments[i]);
1269 empty_prepend_test (&test_segments[i++]);
1270 }
1271
1272 i = 0;
1273 while (prepend_tests[i].test1)
Paul Jakma50ef5652010-11-27 23:14:02 +00001274 {
1275 printf ("prepend test %u\n", i);
1276 prepend_test (&prepend_tests[i++]);
1277 }
paul1f377b02005-09-10 18:00:11 +00001278
1279 i = 0;
1280 while (aggregate_tests[i].test1)
Paul Jakma50ef5652010-11-27 23:14:02 +00001281 {
1282 printf ("aggregate test %u\n", i);
1283 aggregate_test (&aggregate_tests[i++]);
1284 }
paul1f377b02005-09-10 18:00:11 +00001285
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001286 i = 0;
1287
1288 while (reconcile_tests[i].test1)
Paul Jakma50ef5652010-11-27 23:14:02 +00001289 {
1290 printf ("reconcile test %u\n", i);
1291 as4_reconcile_test (&reconcile_tests[i++]);
1292 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001293
1294 i = 0;
1295
paul1f377b02005-09-10 18:00:11 +00001296 cmp_test();
1297
1298 i = 0;
1299
1300 empty_get_test();
1301
Paul Jakma50ef5652010-11-27 23:14:02 +00001302 i = 0;
1303
1304 while (aspath_tests[i].desc)
1305 {
1306 printf ("aspath_attr test %d\n", i);
1307 attr_test (&aspath_tests[i++]);
1308 }
1309
paul1f377b02005-09-10 18:00:11 +00001310 printf ("failures: %d\n", failed);
1311 printf ("aspath count: %ld\n", aspath_count());
1312
1313 return (failed + aspath_count());
1314}