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