blob: 0e57c535d28f45f017566682728ad30385d2384a [file] [log] [blame]
Paul Jakma010ebbb2014-09-16 11:53:49 +01001/*
2 * Copyright (C) 2005 Sun Microsystems, Inc.
3 *
4 * This file is part of Quagga.
5 *
6 * Quagga is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * Quagga is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Quagga; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
paul1f377b02005-09-10 18:00:11 +000022#include <zebra.h>
23
24#include "vty.h"
25#include "stream.h"
26#include "privs.h"
27
28#include "bgpd/bgpd.h"
29#include "bgpd/bgp_aspath.h"
Paul Jakma50ef5652010-11-27 23:14:02 +000030#include "bgpd/bgp_attr.h"
paul1f377b02005-09-10 18:00:11 +000031
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000032#define VT100_RESET "\x1b[0m"
33#define VT100_RED "\x1b[31m"
34#define VT100_GREEN "\x1b[32m"
35#define VT100_YELLOW "\x1b[33m"
36#define OK VT100_GREEN "OK" VT100_RESET
37#define FAILED VT100_RED "failed" VT100_RESET
38
paul1f377b02005-09-10 18:00:11 +000039/* need these to link in libbgp */
40struct zebra_privs_t *bgpd_privs = NULL;
41struct thread_master *master = NULL;
42
43static int failed = 0;
44
45/* specification for a test - what the results should be */
46struct test_spec
47{
48 const char *shouldbe; /* the string the path should parse to */
49 const char *shouldbe_delete_confed; /* ditto, but once confeds are deleted */
paul84771ee2005-10-11 03:48:28 +000050 const unsigned int hops; /* aspath_count_hops result */
51 const unsigned int confeds; /* aspath_count_confeds */
paul1f377b02005-09-10 18:00:11 +000052 const int private_as; /* whether the private_as check should pass or fail */
53#define NOT_ALL_PRIVATE 0
54#define ALL_PRIVATE 1
55 const as_t does_loop; /* an ASN which should trigger loop-check */
56 const as_t doesnt_loop; /* one which should not */
57 const as_t first; /* the first ASN, if there is one */
58#define NULL_ASN 0
59};
60
61
62/* test segments to parse and validate, and use for other tests */
63static struct test_segment {
64 const char *name;
65 const char *desc;
66 const u_char asdata[1024];
67 int len;
68 struct test_spec sp;
69} test_segments [] =
70{
71 { /* 0 */
72 "seq1",
73 "seq(8466,3,52737,4096)",
74 { 0x2,0x4, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00 },
75 10,
76 { "8466 3 52737 4096",
77 "8466 3 52737 4096",
78 4, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
79 },
80 { /* 1 */
81 "seq2",
82 "seq(8722) seq(4)",
83 { 0x2,0x1, 0x22,0x12,
84 0x2,0x1, 0x00,0x04 },
85 8,
86 { "8722 4",
87 "8722 4",
88 2, 0, NOT_ALL_PRIVATE, 4, 5, 8722, },
89 },
90 { /* 2 */
91 "seq3",
92 "seq(8466,3,52737,4096,8722,4)",
93 { 0x2,0x6, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00,
94 0x22,0x12, 0x00,0x04},
95 14,
96 { "8466 3 52737 4096 8722 4",
97 "8466 3 52737 4096 8722 4",
98 6, 0, NOT_ALL_PRIVATE, 3, 5, 8466 },
99 },
100 { /* 3 */
101 "seqset",
102 "seq(8482,51457) set(5204)",
103 { 0x2,0x2, 0x21,0x22, 0xc9,0x01,
104 0x1,0x1, 0x14,0x54 },
105 10,
106 { "8482 51457 {5204}",
107 "8482 51457 {5204}",
Paul Jakma1f742f22006-08-06 15:52:11 +0000108 3, 0, NOT_ALL_PRIVATE, 5204, 51456, 8482},
paul1f377b02005-09-10 18:00:11 +0000109 },
110 { /* 4 */
111 "seqset2",
112 "seq(8467, 59649) set(4196,48658) set(17322,30745)",
113 { 0x2,0x2, 0x21,0x13, 0xe9,0x01,
114 0x1,0x2, 0x10,0x64, 0xbe,0x12,
115 0x1,0x2, 0x43,0xaa, 0x78,0x19 },
116 18,
117 { "8467 59649 {4196,48658} {17322,30745}",
118 "8467 59649 {4196,48658} {17322,30745}",
119 4, 0, NOT_ALL_PRIVATE, 48658, 1, 8467},
120 },
121 { /* 5 */
122 "multi",
123 "seq(6435,59408,21665) set(2457,61697,4369), seq(1842,41590,51793)",
124 { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
125 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
126 0x2,0x3, 0x07,0x32, 0xa2,0x76, 0xca,0x51 },
127 24,
128 { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
129 "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
130 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
131 },
132 { /* 6 */
133 "confed",
134 "confseq(123,456,789)",
135 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15 },
136 8,
137 { "(123 456 789)",
138 "",
139 0, 3, NOT_ALL_PRIVATE, 789, 1, NULL_ASN },
140 },
141 { /* 7 */
142 "confed2",
143 "confseq(123,456,789) confseq(111,222)",
144 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
145 0x3,0x2, 0x00,0x6f, 0x00,0xde },
146 14,
147 { "(123 456 789) (111 222)",
148 "",
149 0, 5, NOT_ALL_PRIVATE, 111, 1, NULL_ASN },
150 },
151 { /* 8 */
152 "confset",
153 "confset(456,123,789)",
154 { 0x4,0x3, 0x01,0xc8, 0x00,0x7b, 0x03,0x15 },
155 8,
156 { "[123,456,789]",
157 "[123,456,789]",
158 0, 1, NOT_ALL_PRIVATE, 123, 1, NULL_ASN },
159 },
160 { /* 9 */
161 "confmulti",
162 "confseq(123,456,789) confset(222,111) seq(8722) set(4196,48658)",
163 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
164 0x4,0x2, 0x00,0xde, 0x00,0x6f,
165 0x2,0x1, 0x22,0x12,
166 0x1,0x2, 0x10,0x64, 0xbe,0x12 },
167 24,
168 { "(123 456 789) [111,222] 8722 {4196,48658}",
169 "8722 {4196,48658}",
170 2, 4, NOT_ALL_PRIVATE, 123, 1, NULL_ASN },
171 },
172 { /* 10 */
173 "seq4",
174 "seq(8466,2,52737,4096,8722,4)",
175 { 0x2,0x6, 0x21,0x12, 0x00,0x02, 0xce,0x01, 0x10,0x00,
176 0x22,0x12, 0x00,0x04},
177 14,
178 { "8466 2 52737 4096 8722 4",
179 "8466 2 52737 4096 8722 4",
180 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
181 },
182 { /* 11 */
183 "tripleseq1",
184 "seq(8466,2,52737) seq(4096,8722,4) seq(8722)",
185 { 0x2,0x3, 0x21,0x12, 0x00,0x02, 0xce,0x01,
186 0x2,0x3, 0x10,0x00, 0x22,0x12, 0x00,0x04,
187 0x2,0x1, 0x22,0x12},
188 20,
189 { "8466 2 52737 4096 8722 4 8722",
190 "8466 2 52737 4096 8722 4 8722",
191 7, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
192 },
193 { /* 12 */
194 "someprivate",
195 "seq(8466,64512,52737,65535)",
196 { 0x2,0x4, 0x21,0x12, 0xfc,0x00, 0xce,0x01, 0xff,0xff },
197 10,
198 { "8466 64512 52737 65535",
199 "8466 64512 52737 65535",
200 4, 0, NOT_ALL_PRIVATE, 65535, 4, 8466 },
201 },
202 { /* 13 */
203 "allprivate",
204 "seq(65534,64512,64513,65535)",
205 { 0x2,0x4, 0xff,0xfe, 0xfc,0x00, 0xfc,0x01, 0xff,0xff },
206 10,
207 { "65534 64512 64513 65535",
208 "65534 64512 64513 65535",
209 4, 0, ALL_PRIVATE, 65534, 4, 65534 },
210 },
211 { /* 14 */
212 "long",
213 "seq(8466,3,52737,4096,34285,<repeated 49 more times>)",
214 { 0x2,0xfa, 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 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
245 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
246 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
247 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
248 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
249 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
250 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
251 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
252 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
253 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
254 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
255 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
256 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
257 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
258 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
259 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
260 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
261 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
262 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
263 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, },
264 502,
265 { "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
266 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
267 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
268 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
269 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
270 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
271 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
272 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
273 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
274 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
275 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
276 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
277 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
278 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
279 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
280 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
281 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
282 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
283 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
284 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
285 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
286 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
287 "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
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 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
297 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
298 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
299 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
300 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
301 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
302 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
303 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
304 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
305 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
306 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
307 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
308 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
309 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
310 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
311 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
312 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
313 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
314 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
315 "8466 3 52737 4096 34285 8466 3 52737 4096 34285",
316 250, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
317 },
paul59064762005-11-23 02:48:14 +0000318 { /* 15 */
319 "seq1extra",
320 "seq(8466,3,52737,4096,3456)",
321 { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
322 12,
323 { "8466 3 52737 4096 3456",
324 "8466 3 52737 4096 3456",
325 5, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
326 },
327 { /* 16 */
328 "empty",
329 "<empty>",
330 {},
331 0,
332 { "", "", 0, 0, 0, 0, 0, 0 },
333 },
Paul Jakma5d932f62006-03-30 13:42:50 +0000334 { /* 17 */
335 "redundantset",
336 "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153)",
337 { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
338 0x1,0x4, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9 },
339 22,
340 {
341 /* We shouldn't ever /generate/ such paths. However, we should
342 * cope with them fine.
343 */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000344 "8466 3 52737 4096 3456 {7099,8153}",
345 "8466 3 52737 4096 3456 {7099,8153}",
346 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
347 },
348 { /* 18 */
349 "reconcile_lead_asp",
350 "seq(6435,59408,21665) set(23456,23456,23456), seq(23456,23456,23456)",
351 { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
352 0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
353 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
354 24,
355 { "6435 59408 21665 {23456} 23456 23456 23456",
356 "6435 59408 21665 {23456} 23456 23456 23456",
357 7, 0, NOT_ALL_PRIVATE, 23456, 1, 6435 },
358 },
359 { /* 19 */
360 "reconcile_new_asp",
361 "set(2457,61697,4369), seq(1842,41591,51793)",
362 {
363 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
364 0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51 },
365 16,
366 { "{2457,4369,61697} 1842 41591 51793",
367 "{2457,4369,61697} 1842 41591 51793",
368 4, 0, NOT_ALL_PRIVATE, 51793, 1, 2457 },
369 },
370 { /* 20 */
371 "reconcile_confed",
372 "confseq(123,456,789) confset(456,124,788) seq(6435,59408,21665)"
373 " set(23456,23456,23456), seq(23456,23456,23456)",
374 { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
375 0x4,0x3, 0x01,0xc8, 0x00,0x7c, 0x03,0x14,
376 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
377 0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
378 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
379 40,
380 { "(123 456 789) [124,456,788] 6435 59408 21665"
381 " {23456} 23456 23456 23456",
382 "6435 59408 21665 {23456} 23456 23456 23456",
383 7, 4, NOT_ALL_PRIVATE, 23456, 1, 6435 },
384 },
385 { /* 21 */
386 "reconcile_start_trans",
387 "seq(23456,23456,23456) seq(6435,59408,21665)",
388 { 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
389 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, },
390 16,
391 { "23456 23456 23456 6435 59408 21665",
392 "23456 23456 23456 6435 59408 21665",
393 6, 0, NOT_ALL_PRIVATE, 21665, 1, 23456 },
394 },
395 { /* 22 */
396 "reconcile_start_trans4",
397 "seq(1842,41591,51793) seq(6435,59408,21665)",
398 { 0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51,
399 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, },
400 16,
401 { "1842 41591 51793 6435 59408 21665",
402 "1842 41591 51793 6435 59408 21665",
403 6, 0, NOT_ALL_PRIVATE, 41591, 1, 1842 },
404 },
405 { /* 23 */
406 "reconcile_start_trans_error",
407 "seq(23456,23456,23456) seq(6435,59408)",
408 { 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
409 0x2,0x2, 0x19,0x23, 0xe8,0x10, },
410 14,
411 { "23456 23456 23456 6435 59408",
412 "23456 23456 23456 6435 59408",
413 5, 0, NOT_ALL_PRIVATE, 59408, 1, 23456 },
414 },
415 { /* 24 */
416 "redundantset2",
417 "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153,7099)",
418 { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
419 0x1,0x5, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9, 0x1b,0xbb,},
420 24,
421 {
422 /* We should weed out duplicate set members. */
423 "8466 3 52737 4096 3456 {7099,8153}",
424 "8466 3 52737 4096 3456 {7099,8153}",
Paul Jakma5d932f62006-03-30 13:42:50 +0000425 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
426 },
Paul Jakmad38e8d62008-02-23 16:57:03 +0000427 { /* 25 */
428 "zero-size overflow",
429 "#ASNs = 0, data = seq(8466 3 52737 4096 3456)",
430 { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
431 12,
Paul Jakmab881c702010-11-23 16:35:42 +0000432 { NULL, NULL,
Paul Jakmad38e8d62008-02-23 16:57:03 +0000433 0, 0, 0, 0, 0, 0 },
434 },
435 { /* 26 */
436 "zero-size overflow + valid segment",
437 "seq(#AS=0:8466 3 52737),seq(4096 3456)",
438 { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01,
439 0x2,0x2, 0x10,0x00, 0x0d,0x80 },
440 14
441 ,
Paul Jakmab881c702010-11-23 16:35:42 +0000442 { NULL, NULL,
Paul Jakmad38e8d62008-02-23 16:57:03 +0000443 0, 0, 0, 0, 0, 0 },
444 },
Paul Jakma50ef5652010-11-27 23:14:02 +0000445 { /* 27 */
446 "invalid segment type",
447 "type=8(4096 3456)",
448 { 0x8,0x2, 0x10,0x00, 0x0d,0x80 },
449 14
450 ,
Paul Jakmab881c702010-11-23 16:35:42 +0000451 { NULL, NULL,
Paul Jakma50ef5652010-11-27 23:14:02 +0000452 0, 0, 0, 0, 0, 0 },
453 }, { NULL, NULL, {0}, 0, { NULL, 0, 0 } }
454};
455
456/* */
457static struct aspath_tests {
458 const char *desc;
459 const struct test_segment *segment;
460 const char *shouldbe; /* String it should evaluate to */
461 const enum as4 { AS4_DATA, AS2_DATA }
462 as4; /* whether data should be as4 or not (ie as2) */
463 const int result; /* expected result for bgp_attr_parse */
464 const int cap; /* capabilities to set for peer */
465 const char attrheader [1024];
466 size_t len;
David Lamparter1cb9cf02013-01-22 23:39:17 +0100467 const struct test_segment *old_segment;
Paul Jakma50ef5652010-11-27 23:14:02 +0000468} aspath_tests [] =
469{
470 /* 0 */
471 {
472 "basic test",
473 &test_segments[0],
474 "8466 3 52737 4096",
475 AS2_DATA, 0,
476 0,
477 { BGP_ATTR_FLAG_TRANS,
478 BGP_ATTR_AS_PATH,
479 10,
480 },
481 3,
482 },
483 /* 1 */
484 {
485 "length too short",
486 &test_segments[0],
487 "8466 3 52737 4096",
488 AS2_DATA, -1,
489 0,
490 { BGP_ATTR_FLAG_TRANS,
491 BGP_ATTR_AS_PATH,
492 8,
493 },
494 3,
495 },
496 /* 2 */
497 {
498 "length too long",
499 &test_segments[0],
500 "8466 3 52737 4096",
501 AS2_DATA, -1,
502 0,
503 { BGP_ATTR_FLAG_TRANS,
504 BGP_ATTR_AS_PATH,
505 12,
506 },
507 3,
508 },
509 /* 3 */
510 {
511 "incorrect flag",
512 &test_segments[0],
513 "8466 3 52737 4096",
514 AS2_DATA, -1,
515 0,
516 { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
517 BGP_ATTR_AS_PATH,
518 10,
519 },
520 3,
521 },
522 /* 4 */
523 {
524 "as4_path, with as2 format data",
525 &test_segments[0],
526 "8466 3 52737 4096",
527 AS2_DATA, -1,
528 0,
529 { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
530 BGP_ATTR_AS4_PATH,
531 10,
532 },
533 3,
534 },
535 /* 5 */
536 {
537 "as4, with incorrect attr length",
538 &test_segments[0],
539 "8466 3 52737 4096",
540 AS4_DATA, -1,
541 PEER_CAP_AS4_RCV,
542 { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
543 BGP_ATTR_AS4_PATH,
544 10,
545 },
546 3,
547 },
548 /* 6 */
549 {
550 "basic 4-byte as-path",
551 &test_segments[0],
552 "8466 3 52737 4096",
553 AS4_DATA, 0,
554 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
555 { BGP_ATTR_FLAG_TRANS,
556 BGP_ATTR_AS_PATH,
557 18,
558 },
559 3,
560 },
561 /* 7 */
562 {
563 "4b AS_PATH: too short",
564 &test_segments[0],
565 "8466 3 52737 4096",
566 AS4_DATA, -1,
567 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
568 { BGP_ATTR_FLAG_TRANS,
569 BGP_ATTR_AS_PATH,
570 16,
571 },
572 3,
573 },
574 /* 8 */
575 {
576 "4b AS_PATH: too long",
577 &test_segments[0],
578 "8466 3 52737 4096",
579 AS4_DATA, -1,
580 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
581 { BGP_ATTR_FLAG_TRANS,
582 BGP_ATTR_AS_PATH,
583 20,
584 },
585 3,
586 },
587 /* 9 */
588 {
589 "4b AS_PATH: too long2",
590 &test_segments[0],
591 "8466 3 52737 4096",
592 AS4_DATA, -1,
593 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
594 { BGP_ATTR_FLAG_TRANS,
595 BGP_ATTR_AS_PATH,
596 22,
597 },
598 3,
599 },
600 /* 10 */
601 {
602 "4b AS_PATH: bad flags",
603 &test_segments[0],
604 "8466 3 52737 4096",
605 AS4_DATA, -1,
606 PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
607 { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
608 BGP_ATTR_AS_PATH,
609 18,
610 },
611 3,
612 },
613 /* 11 */
614 {
David Lamparter1cb9cf02013-01-22 23:39:17 +0100615 "4b AS4_PATH w/o AS_PATH",
Paul Jakma50ef5652010-11-27 23:14:02 +0000616 &test_segments[6],
David Lamparter1cb9cf02013-01-22 23:39:17 +0100617 NULL,
618 AS4_DATA, 0,
Paul Jakma50ef5652010-11-27 23:14:02 +0000619 PEER_CAP_AS4_ADV,
620 { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
621 BGP_ATTR_AS4_PATH,
622 14,
623 },
624 3,
625 },
David Lamparter1cb9cf02013-01-22 23:39:17 +0100626 /* 12 */
627 {
628 "4b AS4_PATH: confed",
629 &test_segments[6],
630 "8466 3 52737 4096 (123 456 789)",
631 AS4_DATA, 0,
632 PEER_CAP_AS4_ADV,
633 { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
634 BGP_ATTR_AS4_PATH,
635 14,
636 },
637 3,
638 &test_segments[0],
639 },
Paul Jakma50ef5652010-11-27 23:14:02 +0000640 { NULL, NULL, NULL, 0, 0, 0, { 0 }, 0 },
paul1f377b02005-09-10 18:00:11 +0000641};
642
643/* prepending tests */
644static struct tests {
645 const struct test_segment *test1;
646 const struct test_segment *test2;
647 struct test_spec sp;
648} prepend_tests[] =
649{
Paul Jakma50ef5652010-11-27 23:14:02 +0000650 /* 0 */
paul1f377b02005-09-10 18:00:11 +0000651 { &test_segments[0], &test_segments[1],
652 { "8466 3 52737 4096 8722 4",
653 "8466 3 52737 4096 8722 4",
654 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
655 },
Paul Jakma50ef5652010-11-27 23:14:02 +0000656 /* 1 */
paul1f377b02005-09-10 18:00:11 +0000657 { &test_segments[1], &test_segments[3],
658 { "8722 4 8482 51457 {5204}",
659 "8722 4 8482 51457 {5204}",
660 5, 0, NOT_ALL_PRIVATE, 5204, 1, 8722 }
661 },
Paul Jakma50ef5652010-11-27 23:14:02 +0000662 /* 2 */
paul1f377b02005-09-10 18:00:11 +0000663 { &test_segments[3], &test_segments[4],
664 { "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
665 "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
666 7, 0, NOT_ALL_PRIVATE, 5204, 1, 8482 },
667 },
Paul Jakma50ef5652010-11-27 23:14:02 +0000668 /* 3 */
paul1f377b02005-09-10 18:00:11 +0000669 { &test_segments[4], &test_segments[5],
670 { "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
671 " {2457,4369,61697} 1842 41590 51793",
672 "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
673 " {2457,4369,61697} 1842 41590 51793",
674 11, 0, NOT_ALL_PRIVATE, 61697, 1, 8467 }
675 },
Paul Jakma50ef5652010-11-27 23:14:02 +0000676 /* 4 */
paul1f377b02005-09-10 18:00:11 +0000677 { &test_segments[5], &test_segments[6],
Paul Jakma50ef5652010-11-27 23:14:02 +0000678 { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
679 "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
680 7, 0, NOT_ALL_PRIVATE, 1842, 1, 6435 },
paul1f377b02005-09-10 18:00:11 +0000681 },
Paul Jakma50ef5652010-11-27 23:14:02 +0000682 /* 5 */
paul1f377b02005-09-10 18:00:11 +0000683 { &test_segments[6], &test_segments[7],
684 { "(123 456 789) (123 456 789) (111 222)",
685 "",
686 0, 8, NOT_ALL_PRIVATE, 111, 1, 0 }
687 },
688 { &test_segments[7], &test_segments[8],
689 { "(123 456 789) (111 222) [123,456,789]",
690 "",
691 0, 6, NOT_ALL_PRIVATE, 111, 1, 0 }
692 },
693 { &test_segments[8], &test_segments[9],
694 { "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
695 "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
696 2, 5, NOT_ALL_PRIVATE, 456, 1, NULL_ASN },
697 },
698 { &test_segments[9], &test_segments[8],
699 { "(123 456 789) [111,222] 8722 {4196,48658} [123,456,789]",
700 "8722 {4196,48658} [123,456,789]",
701 2, 5, NOT_ALL_PRIVATE, 48658, 1, NULL_ASN },
702 },
703 { &test_segments[14], &test_segments[11],
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 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 2 52737 4096 8722 4 8722",
730
731 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
732 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
733 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
734 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
735 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
736 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
737 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
738 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
739 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
740 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
741 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
742 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
743 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
744 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
745 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
746 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
747 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
748 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
749 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
750 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
751 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
752 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
753 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
754 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
755 "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
756 "8466 2 52737 4096 8722 4 8722",
757 257, 0, NOT_ALL_PRIVATE, 4096, 1000, 8466 },
758 },
759 { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
760};
761
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000762struct tests reconcile_tests[] =
763{
764 { &test_segments[18], &test_segments[19],
765 { "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
766 "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
767 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
768 },
769 { &test_segments[19], &test_segments[18],
770 /* AS_PATH (19) has more hops than NEW_AS_PATH,
771 * so just AS_PATH should be used (though, this practice
772 * is bad imho).
773 */
774 { "{2457,4369,61697} 1842 41591 51793 6435 59408 21665 {23456} 23456 23456 23456",
775 "{2457,4369,61697} 1842 41591 51793 6435 59408 21665 {23456} 23456 23456 23456",
776 11, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
777 },
778 { &test_segments[20], &test_segments[19],
779 { "(123 456 789) [124,456,788] 6435 59408 21665"
780 " {2457,4369,61697} 1842 41591 51793",
781 "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
782 7, 4, NOT_ALL_PRIVATE, 51793, 1, 6435 },
783 },
784 { &test_segments[21], &test_segments[22],
785 { "1842 41591 51793 6435 59408 21665",
786 "1842 41591 51793 6435 59408 21665",
787 6, 0, NOT_ALL_PRIVATE, 51793, 1, 1842 },
788 },
789 { &test_segments[23], &test_segments[22],
790 { "23456 23456 23456 6435 59408 1842 41591 51793 6435 59408 21665",
791 "23456 23456 23456 6435 59408 1842 41591 51793 6435 59408 21665",
792 11, 0, NOT_ALL_PRIVATE, 51793, 1, 1842 },
793 },
794 { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
795};
796
paul1f377b02005-09-10 18:00:11 +0000797struct tests aggregate_tests[] =
798{
paul1f377b02005-09-10 18:00:11 +0000799 { &test_segments[0], &test_segments[2],
800 { "8466 3 52737 4096 {4,8722}",
801 "8466 3 52737 4096 {4,8722}",
802 5, 0, NOT_ALL_PRIVATE, 4, 1, 8466 },
803 },
804 { &test_segments[2], &test_segments[0],
805 { "8466 3 52737 4096 {4,8722}",
806 "8466 3 52737 4096 {4,8722}",
807 5, 0, NOT_ALL_PRIVATE, 8722, 1, 8466 },
808 },
809 { &test_segments[2], &test_segments[10],
810 { "8466 {2,3,4,4096,8722,52737}",
811 "8466 {2,3,4,4096,8722,52737}",
812 2, 0, NOT_ALL_PRIVATE, 8722, 5, 8466 },
813 },
814 { &test_segments[10], &test_segments[2],
815 { "8466 {2,3,4,4096,8722,52737}",
816 "8466 {2,3,4,4096,8722,52737}",
817 2, 0, NOT_ALL_PRIVATE, 2, 20000, 8466 },
818 },
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000819
820 { &test_segments[5], &test_segments[18],
821 { "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
822 "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
823 4, 0, NOT_ALL_PRIVATE, 41590, 1, 6435 },
824 },
825
paul1f377b02005-09-10 18:00:11 +0000826 { NULL, NULL, { NULL, 0, 0} },
827};
828
829struct compare_tests
830{
831 int test_index1;
832 int test_index2;
833#define CMP_RES_YES 1
834#define CMP_RES_NO 0
835 char shouldbe_cmp;
836 char shouldbe_confed;
837} left_compare [] =
838{
839 { 0, 1, CMP_RES_NO, CMP_RES_NO },
840 { 0, 2, CMP_RES_YES, CMP_RES_NO },
841 { 0, 11, CMP_RES_YES, CMP_RES_NO },
paul59064762005-11-23 02:48:14 +0000842 { 0, 15, CMP_RES_YES, CMP_RES_NO },
843 { 0, 16, CMP_RES_NO, CMP_RES_NO },
paul1f377b02005-09-10 18:00:11 +0000844 { 1, 11, CMP_RES_NO, CMP_RES_NO },
845 { 6, 7, CMP_RES_NO, CMP_RES_YES },
846 { 6, 8, CMP_RES_NO, CMP_RES_NO },
847 { 7, 8, CMP_RES_NO, CMP_RES_NO },
848 { 1, 9, CMP_RES_YES, CMP_RES_NO },
849 { 0, 9, CMP_RES_NO, CMP_RES_NO },
850 { 3, 9, CMP_RES_NO, CMP_RES_NO },
851 { 0, 6, CMP_RES_NO, CMP_RES_NO },
852 { 1, 6, CMP_RES_NO, CMP_RES_NO },
853 { 0, 8, CMP_RES_NO, CMP_RES_NO },
854 { 1, 8, CMP_RES_NO, CMP_RES_NO },
855 { 11, 6, CMP_RES_NO, CMP_RES_NO },
856 { 11, 7, CMP_RES_NO, CMP_RES_NO },
857 { 11, 8, CMP_RES_NO, CMP_RES_NO },
858 { 9, 6, CMP_RES_NO, CMP_RES_YES },
859 { 9, 7, CMP_RES_NO, CMP_RES_YES },
860 { 9, 8, CMP_RES_NO, CMP_RES_NO },
861};
862
863/* make an aspath from a data stream */
864static struct aspath *
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000865make_aspath (const u_char *data, size_t len, int use32bit)
paul1f377b02005-09-10 18:00:11 +0000866{
867 struct stream *s = NULL;
868 struct aspath *as;
869
870 if (len)
871 {
872 s = stream_new (len);
873 stream_put (s, data, len);
874 }
Paul Jakma50ef5652010-11-27 23:14:02 +0000875 as = aspath_parse (s, len, use32bit);
paul59064762005-11-23 02:48:14 +0000876
877 if (s)
878 stream_free (s);
paul1f377b02005-09-10 18:00:11 +0000879
880 return as;
881}
882
883static void
884printbytes (const u_char *bytes, int len)
885{
886 int i = 0;
887 while (i < len)
888 {
889 if (i % 2)
890 printf ("%02hhx%s", bytes[i], " ");
891 else
892 printf ("0x%02hhx", bytes[i]);
893 i++;
894 }
895 printf ("\n");
896}
897
898/* validate the given aspath */
899static int
900validate (struct aspath *as, const struct test_spec *sp)
901{
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000902 size_t bytes, bytes4;
paul1f377b02005-09-10 18:00:11 +0000903 int fails = 0;
904 const u_char *out;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000905 static struct stream *s;
906 struct aspath *asinout, *asconfeddel, *asstr, *as4;
paul1f377b02005-09-10 18:00:11 +0000907
Paul Jakmab881c702010-11-23 16:35:42 +0000908 if (as == NULL && sp->shouldbe == NULL)
909 {
910 printf ("Correctly failed to parse\n");
911 return fails;
912 }
913
paul1f377b02005-09-10 18:00:11 +0000914 out = aspath_snmp_pathseg (as, &bytes);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000915 asinout = make_aspath (out, bytes, 0);
916
917 /* Excercise AS4 parsing a bit, with a dogfood test */
918 if (!s)
919 s = stream_new (4096);
920 bytes4 = aspath_put (s, as, 1);
921 as4 = make_aspath (STREAM_DATA(s), bytes4, 1);
paul1f377b02005-09-10 18:00:11 +0000922
923 asstr = aspath_str2aspath (sp->shouldbe);
924
925 asconfeddel = aspath_delete_confed_seq (aspath_dup (asinout));
926
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000927 printf ("got: %s\n", aspath_print(as));
928
paul1f377b02005-09-10 18:00:11 +0000929 /* the parsed path should match the specified 'shouldbe' string.
930 * We should pass the "eat our own dog food" test, be able to output
931 * this path and then input it again. Ie the path resulting from:
932 *
933 * aspath_parse(aspath_put(as))
934 *
935 * should:
936 *
937 * - also match the specified 'shouldbe' value
938 * - hash to same value as original path
939 * - have same hops and confed counts as original, and as the
940 * the specified counts
941 *
942 * aspath_str2aspath() and shouldbe should match
943 *
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000944 * We do the same for:
945 *
946 * aspath_parse(aspath_put(as,USE32BIT))
947 *
paul1f377b02005-09-10 18:00:11 +0000948 * Confederation related tests:
949 * - aspath_delete_confed_seq(aspath) should match shouldbe_confed
950 * - aspath_delete_confed_seq should be idempotent.
951 */
952 if (strcmp(aspath_print (as), sp->shouldbe)
953 /* hash validation */
954 || (aspath_key_make (as) != aspath_key_make (asinout))
955 /* by string */
956 || strcmp(aspath_print (asinout), sp->shouldbe)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000957 /* By 4-byte parsing */
958 || strcmp(aspath_print (as4), sp->shouldbe)
paul1f377b02005-09-10 18:00:11 +0000959 /* by various path counts */
960 || (aspath_count_hops (as) != sp->hops)
961 || (aspath_count_confeds (as) != sp->confeds)
962 || (aspath_count_hops (asinout) != sp->hops)
963 || (aspath_count_confeds (asinout) != sp->confeds))
964 {
965 failed++;
966 fails++;
967 printf ("shouldbe:\n%s\n", sp->shouldbe);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000968 printf ("as4:\n%s\n", aspath_print (as4));
paul1f377b02005-09-10 18:00:11 +0000969 printf ("hash keys: in: %d out->in: %d\n",
970 aspath_key_make (as), aspath_key_make (asinout));
971 printf ("hops: %d, counted %d %d\n", sp->hops,
972 aspath_count_hops (as),
973 aspath_count_hops (asinout) );
974 printf ("confeds: %d, counted %d %d\n", sp->confeds,
975 aspath_count_confeds (as),
976 aspath_count_confeds (asinout));
977 printf ("out->in:\n%s\nbytes: ", aspath_print(asinout));
978 printbytes (out, bytes);
979 }
980 /* basic confed related tests */
981 if ((aspath_print (asconfeddel) == NULL
982 && sp->shouldbe_delete_confed != NULL)
983 || (aspath_print (asconfeddel) != NULL
984 && sp->shouldbe_delete_confed == NULL)
985 || strcmp(aspath_print (asconfeddel), sp->shouldbe_delete_confed)
986 /* delete_confed_seq should be idempotent */
987 || (aspath_key_make (asconfeddel)
988 != aspath_key_make (aspath_delete_confed_seq (asconfeddel))))
989 {
990 failed++;
991 fails++;
992 printf ("confed_del: %s\n", aspath_print (asconfeddel));
993 printf ("should be: %s\n", sp->shouldbe_delete_confed);
994 }
995 /* aspath_str2aspath test */
996 if ((aspath_print (asstr) == NULL && sp->shouldbe != NULL)
997 || (aspath_print (asstr) != NULL && sp->shouldbe == NULL)
998 || strcmp(aspath_print (asstr), sp->shouldbe))
999 {
1000 failed++;
1001 fails++;
1002 printf ("asstr: %s\n", aspath_print (asstr));
1003 }
1004
1005 /* loop, private and first as checks */
Paul Jakma1f742f22006-08-06 15:52:11 +00001006 if ((sp->does_loop && aspath_loop_check (as, sp->does_loop) == 0)
1007 || (sp->doesnt_loop && aspath_loop_check (as, sp->doesnt_loop) != 0)
paul1f377b02005-09-10 18:00:11 +00001008 || (aspath_private_as_check (as) != sp->private_as)
1009 || (aspath_firstas_check (as,sp->first)
1010 && sp->first == 0))
1011 {
1012 failed++;
1013 fails++;
1014 printf ("firstas: %d, got %d\n", sp->first,
1015 aspath_firstas_check (as,sp->first));
1016 printf ("loop does: %d %d, doesnt: %d %d\n",
1017 sp->does_loop, aspath_loop_check (as, sp->does_loop),
1018 sp->doesnt_loop, aspath_loop_check (as, sp->doesnt_loop));
1019 printf ("private check: %d %d\n", sp->private_as,
1020 aspath_private_as_check (as));
1021 }
Paul Jakma1dba2542012-05-01 16:20:33 +01001022 aspath_unintern (&asinout);
1023 aspath_unintern (&as4);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001024
paul6726f952006-01-16 12:06:00 +00001025 aspath_free (asconfeddel);
1026 aspath_free (asstr);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001027 stream_reset (s);
paul6726f952006-01-16 12:06:00 +00001028
paul1f377b02005-09-10 18:00:11 +00001029 return fails;
paul1f377b02005-09-10 18:00:11 +00001030}
1031
1032static void
1033empty_get_test ()
1034{
1035 struct aspath *as = aspath_empty_get ();
1036 struct test_spec sp = { "", "", 0, 0, 0, 0, 0, 0 };
1037
1038 printf ("empty_get_test, as: %s\n",aspath_print (as));
1039 if (!validate (as, &sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001040 printf ("%s\n", OK);
paul1f377b02005-09-10 18:00:11 +00001041 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001042 printf ("%s!\n", FAILED);
paul1f377b02005-09-10 18:00:11 +00001043
1044 printf ("\n");
paul6726f952006-01-16 12:06:00 +00001045
1046 aspath_free (as);
paul1f377b02005-09-10 18:00:11 +00001047}
1048
1049/* basic parsing test */
1050static void
1051parse_test (struct test_segment *t)
1052{
1053 struct aspath *asp;
1054
1055 printf ("%s: %s\n", t->name, t->desc);
1056
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001057 asp = make_aspath (t->asdata, t->len, 0);
Paul Jakmab881c702010-11-23 16:35:42 +00001058
paul1f377b02005-09-10 18:00:11 +00001059 printf ("aspath: %s\nvalidating...:\n", aspath_print (asp));
1060
1061 if (!validate (asp, &t->sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001062 printf (OK "\n");
paul1f377b02005-09-10 18:00:11 +00001063 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001064 printf (FAILED "\n");
paul1f377b02005-09-10 18:00:11 +00001065
1066 printf ("\n");
Paul Jakmab881c702010-11-23 16:35:42 +00001067
1068 if (asp)
Paul Jakma1dba2542012-05-01 16:20:33 +01001069 aspath_unintern (&asp);
paul1f377b02005-09-10 18:00:11 +00001070}
1071
1072/* prepend testing */
Paul Jakma5d932f62006-03-30 13:42:50 +00001073static void
paul1f377b02005-09-10 18:00:11 +00001074prepend_test (struct tests *t)
1075{
1076 struct aspath *asp1, *asp2, *ascratch;
1077
1078 printf ("prepend %s: %s\n", t->test1->name, t->test1->desc);
1079 printf ("to %s: %s\n", t->test2->name, t->test2->desc);
1080
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001081 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
1082 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
paul1f377b02005-09-10 18:00:11 +00001083
1084 ascratch = aspath_dup (asp2);
Paul Jakma1dba2542012-05-01 16:20:33 +01001085 aspath_unintern (&asp2);
paul1f377b02005-09-10 18:00:11 +00001086
1087 asp2 = aspath_prepend (asp1, ascratch);
1088
1089 printf ("aspath: %s\n", aspath_print (asp2));
1090
1091 if (!validate (asp2, &t->sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001092 printf ("%s\n", OK);
paul1f377b02005-09-10 18:00:11 +00001093 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001094 printf ("%s!\n", FAILED);
paul1f377b02005-09-10 18:00:11 +00001095
1096 printf ("\n");
Paul Jakma1dba2542012-05-01 16:20:33 +01001097 aspath_unintern (&asp1);
paul1f377b02005-09-10 18:00:11 +00001098 aspath_free (asp2);
1099}
1100
1101/* empty-prepend testing */
Paul Jakma5d932f62006-03-30 13:42:50 +00001102static void
paul1f377b02005-09-10 18:00:11 +00001103empty_prepend_test (struct test_segment *t)
1104{
1105 struct aspath *asp1, *asp2, *ascratch;
1106
1107 printf ("empty prepend %s: %s\n", t->name, t->desc);
1108
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001109 asp1 = make_aspath (t->asdata, t->len, 0);
paul1f377b02005-09-10 18:00:11 +00001110 asp2 = aspath_empty ();
1111
1112 ascratch = aspath_dup (asp2);
Paul Jakma1dba2542012-05-01 16:20:33 +01001113 aspath_unintern (&asp2);
paul1f377b02005-09-10 18:00:11 +00001114
1115 asp2 = aspath_prepend (asp1, ascratch);
1116
1117 printf ("aspath: %s\n", aspath_print (asp2));
1118
1119 if (!validate (asp2, &t->sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001120 printf (OK "\n");
paul1f377b02005-09-10 18:00:11 +00001121 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001122 printf (FAILED "!\n");
paul1f377b02005-09-10 18:00:11 +00001123
1124 printf ("\n");
Paul Jakmab881c702010-11-23 16:35:42 +00001125 if (asp1)
Paul Jakma1dba2542012-05-01 16:20:33 +01001126 aspath_unintern (&asp1);
paul1f377b02005-09-10 18:00:11 +00001127 aspath_free (asp2);
1128}
1129
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001130/* as2+as4 reconciliation testing */
1131static void
1132as4_reconcile_test (struct tests *t)
1133{
1134 struct aspath *asp1, *asp2, *ascratch;
1135
1136 printf ("reconciling %s:\n %s\n", t->test1->name, t->test1->desc);
1137 printf ("with %s:\n %s\n", t->test2->name, t->test2->desc);
1138
1139 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
1140 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
1141
1142 ascratch = aspath_reconcile_as4 (asp1, asp2);
1143
1144 if (!validate (ascratch, &t->sp))
1145 printf (OK "\n");
1146 else
1147 printf (FAILED "!\n");
1148
1149 printf ("\n");
Paul Jakma1dba2542012-05-01 16:20:33 +01001150 aspath_unintern (&asp1);
1151 aspath_unintern (&asp2);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001152 aspath_free (ascratch);
1153}
1154
1155
paul1f377b02005-09-10 18:00:11 +00001156/* aggregation testing */
Paul Jakma5d932f62006-03-30 13:42:50 +00001157static void
paul1f377b02005-09-10 18:00:11 +00001158aggregate_test (struct tests *t)
1159{
1160 struct aspath *asp1, *asp2, *ascratch;
1161
1162 printf ("aggregate %s: %s\n", t->test1->name, t->test1->desc);
1163 printf ("with %s: %s\n", t->test2->name, t->test2->desc);
1164
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001165 asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
1166 asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
paul1f377b02005-09-10 18:00:11 +00001167
1168 ascratch = aspath_aggregate (asp1, asp2);
1169
paul1f377b02005-09-10 18:00:11 +00001170 if (!validate (ascratch, &t->sp))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001171 printf (OK "\n");
paul1f377b02005-09-10 18:00:11 +00001172 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001173 printf (FAILED "!\n");
paul1f377b02005-09-10 18:00:11 +00001174
1175 printf ("\n");
Paul Jakma1dba2542012-05-01 16:20:33 +01001176 aspath_unintern (&asp1);
1177 aspath_unintern (&asp2);
paul1f377b02005-09-10 18:00:11 +00001178 aspath_free (ascratch);
1179/* aspath_unintern (ascratch);*/
1180}
1181
1182/* cmp_left tests */
1183static void
1184cmp_test ()
1185{
paul84771ee2005-10-11 03:48:28 +00001186 unsigned int i;
paul1f377b02005-09-10 18:00:11 +00001187#define CMP_TESTS_MAX \
1188 (sizeof(left_compare) / sizeof (struct compare_tests))
1189
1190 for (i = 0; i < CMP_TESTS_MAX; i++)
1191 {
1192 struct test_segment *t1 = &test_segments[left_compare[i].test_index1];
1193 struct test_segment *t2 = &test_segments[left_compare[i].test_index2];
1194 struct aspath *asp1, *asp2;
1195
1196 printf ("left cmp %s: %s\n", t1->name, t1->desc);
1197 printf ("and %s: %s\n", t2->name, t2->desc);
1198
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001199 asp1 = make_aspath (t1->asdata, t1->len, 0);
1200 asp2 = make_aspath (t2->asdata, t2->len, 0);
paul1f377b02005-09-10 18:00:11 +00001201
1202 if (aspath_cmp_left (asp1, asp2) != left_compare[i].shouldbe_cmp
1203 || aspath_cmp_left (asp2, asp1) != left_compare[i].shouldbe_cmp
1204 || aspath_cmp_left_confed (asp1, asp2)
1205 != left_compare[i].shouldbe_confed
1206 || aspath_cmp_left_confed (asp2, asp1)
1207 != left_compare[i].shouldbe_confed)
1208 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001209 failed++;
1210 printf (FAILED "\n");
paul1f377b02005-09-10 18:00:11 +00001211 printf ("result should be: cmp: %d, confed: %d\n",
1212 left_compare[i].shouldbe_cmp,
1213 left_compare[i].shouldbe_confed);
1214 printf ("got: cmp %d, cmp_confed: %d\n",
1215 aspath_cmp_left (asp1, asp2),
1216 aspath_cmp_left_confed (asp1, asp2));
1217 printf("path1: %s\npath2: %s\n", aspath_print (asp1),
1218 aspath_print (asp2));
paul1f377b02005-09-10 18:00:11 +00001219 }
1220 else
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001221 printf (OK "\n");
paul1f377b02005-09-10 18:00:11 +00001222
1223 printf ("\n");
Paul Jakma1dba2542012-05-01 16:20:33 +01001224 aspath_unintern (&asp1);
1225 aspath_unintern (&asp2);
paul1f377b02005-09-10 18:00:11 +00001226 }
1227}
Paul Jakma50ef5652010-11-27 23:14:02 +00001228
1229static int
1230handle_attr_test (struct aspath_tests *t)
1231{
1232 struct bgp bgp = { 0 };
1233 struct peer peer = { 0 };
1234 struct attr attr = { 0 };
1235 int ret;
1236 int initfail = failed;
1237 struct aspath *asp;
1238 size_t datalen;
1239
1240 asp = make_aspath (t->segment->asdata, t->segment->len, 0);
1241
1242 peer.ibuf = stream_new (BGP_MAX_PACKET_SIZE);
1243 peer.obuf = stream_fifo_new ();
1244 peer.bgp = &bgp;
1245 peer.host = (char *)"none";
1246 peer.fd = -1;
1247 peer.cap = t->cap;
1248
1249 stream_write (peer.ibuf, t->attrheader, t->len);
1250 datalen = aspath_put (peer.ibuf, asp, t->as4 == AS4_DATA);
David Lamparter1cb9cf02013-01-22 23:39:17 +01001251 if (t->old_segment)
1252 {
1253 char dummyaspath[] = { BGP_ATTR_FLAG_TRANS, BGP_ATTR_AS_PATH,
1254 t->old_segment->len };
1255 stream_write (peer.ibuf, dummyaspath, sizeof (dummyaspath));
1256 stream_write (peer.ibuf, t->old_segment->asdata, t->old_segment->len);
1257 datalen += sizeof (dummyaspath) + t->old_segment->len;
1258 }
Paul Jakma50ef5652010-11-27 23:14:02 +00001259
1260 ret = bgp_attr_parse (&peer, &attr, t->len + datalen, NULL, NULL);
1261
1262 if (ret != t->result)
1263 {
1264 printf ("bgp_attr_parse returned %d, expected %d\n", ret, t->result);
Paul Jakma1dba2542012-05-01 16:20:33 +01001265 printf ("datalen %zd\n", datalen);
Paul Jakma50ef5652010-11-27 23:14:02 +00001266 failed++;
1267 }
1268 if (ret != 0)
1269 goto out;
1270
David Lamparter1cb9cf02013-01-22 23:39:17 +01001271 if (t->shouldbe && attr.aspath == NULL)
Paul Jakma50ef5652010-11-27 23:14:02 +00001272 {
David Lamparter1cb9cf02013-01-22 23:39:17 +01001273 printf ("aspath is NULL, but should be: %s\n", t->shouldbe);
Paul Jakma50ef5652010-11-27 23:14:02 +00001274 failed++;
1275 }
David Lamparter1cb9cf02013-01-22 23:39:17 +01001276 if (t->shouldbe && attr.aspath && strcmp (attr.aspath->str, t->shouldbe))
Paul Jakma50ef5652010-11-27 23:14:02 +00001277 {
1278 printf ("attr str and 'shouldbe' mismatched!\n"
1279 "attr str: %s\n"
1280 "shouldbe: %s\n",
1281 attr.aspath->str, t->shouldbe);
1282 failed++;
1283 }
David Lamparter1cb9cf02013-01-22 23:39:17 +01001284 if (!t->shouldbe && attr.aspath)
1285 {
1286 printf ("aspath should be NULL, but is: %s\n", attr.aspath->str);
1287 failed++;
1288 }
Paul Jakma50ef5652010-11-27 23:14:02 +00001289
1290out:
1291 if (attr.aspath)
Paul Jakma1dba2542012-05-01 16:20:33 +01001292 aspath_unintern (&attr.aspath);
Paul Jakma50ef5652010-11-27 23:14:02 +00001293 if (asp)
Paul Jakma1dba2542012-05-01 16:20:33 +01001294 aspath_unintern (&asp);
Paul Jakma50ef5652010-11-27 23:14:02 +00001295 return failed - initfail;
1296}
1297
1298static void
1299attr_test (struct aspath_tests *t)
1300{
1301 printf ("%s\n", t->desc);
1302 printf ("%s\n\n", handle_attr_test (t) ? FAILED : OK);
1303}
1304
paul1f377b02005-09-10 18:00:11 +00001305int
1306main (void)
1307{
1308 int i = 0;
Paul Jakma50ef5652010-11-27 23:14:02 +00001309 bgp_master_init ();
1310 master = bm->master;
Paul Jakmac9e4f862012-06-14 10:42:39 +01001311 bgp_option_set (BGP_OPT_NO_LISTEN);
Paul Jakma50ef5652010-11-27 23:14:02 +00001312 bgp_attr_init ();
1313
paul1f377b02005-09-10 18:00:11 +00001314 while (test_segments[i].name)
1315 {
Paul Jakma50ef5652010-11-27 23:14:02 +00001316 printf ("test %u\n", i);
paul1f377b02005-09-10 18:00:11 +00001317 parse_test (&test_segments[i]);
1318 empty_prepend_test (&test_segments[i++]);
1319 }
1320
1321 i = 0;
1322 while (prepend_tests[i].test1)
Paul Jakma50ef5652010-11-27 23:14:02 +00001323 {
1324 printf ("prepend test %u\n", i);
1325 prepend_test (&prepend_tests[i++]);
1326 }
paul1f377b02005-09-10 18:00:11 +00001327
1328 i = 0;
1329 while (aggregate_tests[i].test1)
Paul Jakma50ef5652010-11-27 23:14:02 +00001330 {
1331 printf ("aggregate test %u\n", i);
1332 aggregate_test (&aggregate_tests[i++]);
1333 }
paul1f377b02005-09-10 18:00:11 +00001334
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001335 i = 0;
1336
1337 while (reconcile_tests[i].test1)
Paul Jakma50ef5652010-11-27 23:14:02 +00001338 {
1339 printf ("reconcile test %u\n", i);
1340 as4_reconcile_test (&reconcile_tests[i++]);
1341 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001342
1343 i = 0;
1344
paul1f377b02005-09-10 18:00:11 +00001345 cmp_test();
1346
1347 i = 0;
1348
1349 empty_get_test();
1350
Paul Jakma50ef5652010-11-27 23:14:02 +00001351 i = 0;
1352
1353 while (aspath_tests[i].desc)
1354 {
1355 printf ("aspath_attr test %d\n", i);
1356 attr_test (&aspath_tests[i++]);
1357 }
1358
paul1f377b02005-09-10 18:00:11 +00001359 printf ("failures: %d\n", failed);
1360 printf ("aspath count: %ld\n", aspath_count());
1361
1362 return (failed + aspath_count());
1363}