]> git.ipfire.org Git - thirdparty/bird.git/blob - filter/test.conf
BGP: Update RFC references
[thirdparty/bird.git] / filter / test.conf
1 /*
2 * This is unit testing configuration file for testing filters
3 *
4 * FIXME: add all examples from docs here.
5 */
6
7 router id 62.168.0.1;
8
9 /* We have to setup any protocol */
10 protocol static { ipv4; }
11
12
13
14
15 /*
16 * Common definitions and functions
17 * --------------------------------
18 */
19
20 define one = 1;
21 define ten = 10;
22
23 function onef(int a)
24 {
25 return 1;
26 }
27
28
29
30
31 /*
32 * Testing boolean expressions
33 * ---------------------------
34 */
35
36 function t_bool()
37 bool b;
38 {
39 b = true;
40 bt_assert(b);
41 bt_assert(!!b);
42
43 bt_assert(format(true) = "TRUE");
44 bt_assert(format(false) = "FALSE");
45
46 if ( b = true ) then
47 bt_assert(b);
48 else
49 bt_assert(false);
50
51 bt_assert(true && true);
52 bt_assert(true || false);
53 bt_assert(! false && ! false && true);
54 bt_assert(1 < 2 && 1 != 3);
55 bt_assert(true && true && ! false);
56 bt_assert(true || 1+"a");
57 bt_assert(!(false && 1+"a"));
58 bt_assert(!(true && false));
59 }
60
61 bt_test_suite(t_bool, "Testing boolean expressions");
62
63
64
65
66 /*
67 * Testing integers
68 * ----------------
69 */
70
71 define four = 4;
72 define xyzzy = (120+10);
73 define '1a-a1' = (xyzzy-100);
74
75 function t_int()
76 int i;
77 {
78 bt_assert(xyzzy = 130);
79 bt_assert('1a-a1' = 30);
80
81 i = four;
82 i = 12*100 + 60/2 + i;
83 i = (i + 0);
84 bt_assert(i = 1234);
85
86 bt_assert(format(i) = "1234");
87
88 i = 4200000000;
89 bt_assert(i = 4200000000);
90 bt_assert(i > 4100000000);
91 bt_assert(!(i > 4250000000));
92
93 bt_assert(1 = 1);
94 bt_assert(!(1 != 1));
95
96 bt_assert(1 != 2);
97 bt_assert(1 <= 2);
98
99 bt_assert(1 != "a");
100 bt_assert(1 != (0,1));
101
102 bt_assert(!(i = 4));
103 bt_assert(1 <= 1);
104 bt_assert(!(1234 < 1234));
105 }
106
107 bt_test_suite(t_int, "Testing integers");
108
109
110
111
112 /*
113 * Testing sets of integers
114 * ------------------------
115 */
116
117 define is1 = [ one, (2+1), (6-one), 8, 11, 15, 17, 19];
118 define is2 = [(17+2), 17, 15, 11, 8, 5, 3, 2];
119 define is3 = [5, 17, 2, 11, 8, 15, 3, 19];
120
121 function t_int_set()
122 int set is;
123 {
124 bt_assert(1 ~ [1,2,3]);
125 bt_assert(5 ~ [1..20]);
126 bt_assert(2 ~ [ 1, 2, 3 ]);
127 bt_assert(5 ~ [ 4 .. 7 ]);
128 bt_assert(1 !~ [ 2, 3, 4 ]);
129
130 is = [ 2, 3, 4, 7..11 ];
131 bt_assert(10 ~ is);
132 bt_assert(5 !~ is);
133
134 bt_assert(1 ~ is1);
135 bt_assert(3 ~ is1);
136 bt_assert(5 ~ is1);
137 bt_assert((one+2) ~ is1);
138 bt_assert(2 ~ is2);
139 bt_assert(2 ~ is3);
140 bt_assert(4 !~ is1);
141 bt_assert(4 !~ is2);
142 bt_assert(4 !~ is3);
143 bt_assert(10 !~ is1);
144 bt_assert(10 !~ is2);
145 bt_assert(10 !~ is3);
146 bt_assert(15 ~ is1);
147 bt_assert(15 ~ is2);
148 bt_assert(15 ~ is3);
149 bt_assert(18 !~ is1);
150 bt_assert(18 !~ is2);
151 bt_assert(18 !~ is3);
152 bt_assert(19 ~ is1);
153 bt_assert(19 ~ is2);
154 bt_assert(19 ~ is3);
155 bt_assert(20 !~ is1);
156 bt_assert(20 !~ is2);
157 bt_assert(20 !~ is3);
158
159 bt_assert([1,2] != [1,3]);
160 bt_assert([1,4..10,20] = [1,4..10,20]);
161
162 bt_assert(format([ 1, 2, 1, 1, 1, 3, 4, 1, 1, 1, 5 ]) = "[1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5]");
163 }
164
165 bt_test_suite(t_int_set, "Testing sets of integers");
166
167
168
169
170 /*
171 * Testing string matching
172 * -----------------------
173 */
174
175 function t_string()
176 string st;
177 {
178 st = "Hello";
179 bt_assert(format(st) = "Hello");
180 bt_assert(st ~ "Hell*");
181 bt_assert(st ~ "?ello");
182 bt_assert(st ~ "Hello");
183 bt_assert(st ~ "Hell?");
184 bt_assert(st !~ "ell*");
185 }
186
187 bt_test_suite(t_string, "Testing string matching");
188
189
190
191
192 /*
193 * Testing pairs
194 * -------------
195 */
196
197 function 'mkpair-a'(int a)
198 {
199 return (1, a);
200 }
201
202 function t_pair()
203 pair pp;
204 {
205 pp = (1, 2);
206 bt_assert(format(pp) = "(1,2)");
207 bt_assert((1,2) = pp);
208 bt_assert((1,1+1) = pp);
209 bt_assert('mkpair-a'(2) = pp);
210 bt_assert((1,2) = (1,1+1));
211 bt_assert(((1,2) < (2,2)));
212 bt_assert(!((1,1) > (1,1)));
213 }
214
215 bt_test_suite(t_pair, "Testing pairs");
216
217
218
219
220 /*
221 * Testing sets of pairs
222 * ---------------------
223 */
224
225 function t_pair_set()
226 pair pp;
227 pair set ps;
228 {
229 pp = (1, 2);
230 ps = [(1,(one+one)), (3,4)..(4,8), (5,*), (6,3..6)];
231 bt_assert(format(ps) = "[(1,2), (3,4)..(4,8), (5,0)..(5,65535), (6,3)..(6,6)]");
232 bt_assert(pp ~ ps);
233 bt_assert((3,5) ~ ps);
234 bt_assert((4,1) ~ ps);
235 bt_assert((5,4) ~ ps);
236 bt_assert((5,65535) ~ ps);
237 bt_assert((6,4) ~ ps);
238 bt_assert((3, 10000) ~ ps);
239 bt_assert((3,3) !~ ps);
240 bt_assert((4,9) !~ ps);
241 bt_assert((4,65535) !~ ps);
242 bt_assert((6,2) !~ ps);
243 bt_assert((6,6+one) !~ ps);
244 bt_assert(((one+6),2) !~ ps);
245 bt_assert((1,1) !~ ps);
246
247 ps = [(20..150, 200..300), (50100..50200, 1000..50000), (*, 5+5)];
248 bt_assert((100,200) ~ ps);
249 bt_assert((150,300) ~ ps);
250 bt_assert((50180,1200) ~ ps);
251 bt_assert((50110,49000) ~ ps);
252 bt_assert((0,10) ~ ps);
253 bt_assert((64000,10) ~ ps);
254 bt_assert((20,199) !~ ps);
255 bt_assert((151,250) !~ ps);
256 bt_assert((50050,2000) !~ ps);
257 bt_assert((50150,50050) !~ ps);
258 bt_assert((10,9) !~ ps);
259 bt_assert((65535,11) !~ ps);
260 }
261
262 bt_test_suite(t_pair_set, "Testing sets of pairs");
263
264
265
266
267 /*
268 * Testing quads
269 * -------------
270 */
271
272 function t_quad()
273 quad qq;
274 {
275 qq = 1.2.3.4;
276 bt_assert(format(qq) = "1.2.3.4");
277 bt_assert(qq = 1.2.3.4);
278 bt_assert(qq != 4.3.2.1);
279 }
280
281 bt_test_suite(t_quad, "Testing quads");
282
283
284
285
286 /*
287 * Testing sets of quads
288 * ---------------------
289 */
290
291 function t_quad_set()
292 quad qq;
293 {
294 qq = 1.2.3.4;
295 bt_assert(qq ~ [1.2.3.4, 5.6.7.8]);
296 bt_assert(qq !~ [1.2.1.1, 1.2.3.5]);
297 }
298
299 bt_test_suite(t_quad_set, "Testing sets of quads");
300
301
302
303
304 /*
305 * Testing ip address
306 * ------------------
307 */
308
309 define onetwo = 1.2.3.4;
310
311 function t_ip()
312 ip p;
313 {
314 p = 127.1.2.3;
315 bt_assert(p.mask(8) = 127.0.0.0);
316 bt_assert(1.2.3.4 = 1.2.3.4);
317 bt_assert(1.2.3.4 = onetwo);
318 bt_assert(format(p) = "127.1.2.3");
319
320 p = ::fffe:6:c0c:936d:88c7:35d3;
321 bt_assert(format(p) = "::fffe:6:c0c:936d:88c7:35d3");
322
323 p = 1234:5678::;
324 bt_assert(p.mask(24) = 1234:5600::);
325 }
326
327 bt_test_suite(t_ip, "Testing ip address");
328
329
330
331
332 /*
333 * Testing sets of ip address
334 * --------------------------
335 */
336
337 define ip1222 = 1.2.2.2;
338
339 function t_ip_set()
340 ip set ips;
341 {
342 ips = [ 1.1.1.0 .. 1.1.1.255, ip1222];
343 bt_assert(format(ips) = "[1.1.1.0..1.1.1.255, 1.2.2.2]");
344 bt_assert(1.1.1.0 ~ ips);
345 bt_assert(1.1.1.100 ~ ips);
346 bt_assert(1.2.2.2 ~ ips);
347 bt_assert(1.1.0.255 !~ ips);
348 bt_assert(1.1.2.0 !~ ips);
349 bt_assert(1.2.2.3 !~ ips);
350 bt_assert(192.168.1.1 !~ ips);
351
352 bt_assert(1.2.3.4 !~ [ 1.2.3.3, 1.2.3.5 ]);
353 bt_assert(1.2.3.4 ~ [ 1.2.3.3..1.2.3.5 ]);
354 }
355
356 bt_test_suite(t_ip_set, "Testing sets of ip address");
357
358
359
360
361 /*
362 * Testing enums
363 * -------------
364 */
365
366 function t_enum()
367 {
368 bt_assert(format(RTS_DUMMY) = "(enum 30)0");
369 bt_assert(format(RTS_STATIC) = "(enum 30)1");
370 bt_assert(RTS_STATIC ~ [RTS_STATIC, RTS_DEVICE]);
371 bt_assert(RTS_BGP !~ [RTS_STATIC, RTS_DEVICE]);
372 }
373
374 bt_test_suite(t_enum, "Testing enums");
375
376
377
378
379 /*
380 * Testing prefixes
381 * ----------------
382 */
383
384 define netdoc = 2001:db8::/32;
385
386 function t_prefix()
387 prefix px;
388 {
389 px = 1.2.0.0/18;
390 bt_assert(format(px) = "1.2.0.0/18");
391 bt_assert(192.168.0.0/16 ~ 192.168.0.0/16);
392 bt_assert(192.168.0.0/17 ~ 192.168.0.0/16);
393 bt_assert(192.168.254.0/24 ~ 192.168.0.0/16);
394 bt_assert(netdoc ~ 2001::/16);
395 bt_assert(192.168.0.0/15 !~ 192.168.0.0/16);
396 bt_assert(192.160.0.0/17 !~ 192.168.0.0/16);
397 bt_assert(px !~ netdoc);
398
399 bt_assert(1.2.3.4 ~ 1.0.0.0/8);
400 bt_assert(1.0.0.0/8 ~ 1.0.0.0/8);
401 }
402
403 bt_test_suite(t_prefix, "Testing prefixes");
404
405
406
407
408 /*
409 * Testing prefix sets
410 * -------------------
411 */
412
413 define net10 = 10.0.0.0/8;
414 define pxs2 = [ 10.0.0.0/16{8,12}, 20.0.0.0/16{24,28} ];
415
416 function test_pxset(prefix set pxs)
417 {
418 bt_assert(net10 ~ pxs);
419 bt_assert(10.0.0.0/10 ~ pxs);
420 bt_assert(10.0.0.0/12 ~ pxs);
421 bt_assert(20.0.0.0/24 ~ pxs);
422 bt_assert(20.0.40.0/24 ~ pxs);
423 bt_assert(20.0.0.0/26 ~ pxs);
424 bt_assert(20.0.100.0/26 ~ pxs);
425 bt_assert(20.0.0.0/28 ~ pxs);
426 bt_assert(20.0.255.0/28 ~ pxs);
427
428 bt_assert(10.0.0.0/7 !~ pxs);
429 bt_assert(10.0.0.0/13 !~ pxs);
430 bt_assert(10.0.0.0/16 !~ pxs);
431 bt_assert(20.0.0.0/16 !~ pxs);
432 bt_assert(20.0.0.0/23 !~ pxs);
433 bt_assert(20.0.0.0/29 !~ pxs);
434 bt_assert(11.0.0.0/10 !~ pxs);
435 bt_assert(20.1.0.0/26 !~ pxs);
436
437 bt_assert(1.0.0.0/8 ~ [ 1.0.0.0/8+ ]);
438 bt_assert(1.0.0.0/9 !~ [ 1.0.0.0/8- ]);
439 bt_assert(1.2.0.0/17 !~ [ 1.0.0.0/8{ 15 , 16 } ]);
440
441 bt_assert([ 10.0.0.0/8{ 15 , 17 } ] = [ 10.0.0.0/8{ 15 , 17 } ]);
442 }
443
444 function t_prefix_set()
445 prefix set pxs;
446 {
447 pxs = [ 1.2.0.0/16, 1.4.0.0/16+];
448 bt_assert(format(pxs) = "[1.2.0.0/112{::0.1.0.0}, 1.4.0.0/112{::0.1.255.255}]");
449 bt_assert(1.2.0.0/16 ~ pxs);
450 bt_assert(1.4.0.0/16 ~ pxs);
451 bt_assert(1.4.0.0/18 ~ pxs);
452 bt_assert(1.4.0.0/32 ~ pxs);
453 bt_assert(1.1.0.0/16 !~ pxs);
454 bt_assert(1.3.0.0/16 !~ pxs);
455 bt_assert(1.2.0.0/15 !~ pxs);
456 bt_assert(1.2.0.0/17 !~ pxs);
457 bt_assert(1.2.0.0/32 !~ pxs);
458 bt_assert(1.4.0.0/15 !~ pxs);
459
460 test_pxset(pxs2);
461 test_pxset([ 10.0.0.0/16{8,12}, 20.0.0.0/16{24,28} ]);
462
463 bt_assert(1.2.0.0/16 ~ [ 1.0.0.0/8{ 15 , 17 } ]);
464 bt_assert([ 10.0.0.0/8{ 15 , 17 } ] != [ 11.0.0.0/8{ 15 , 17 } ]);
465 }
466
467 bt_test_suite(t_prefix_set, "Testing prefix sets");
468
469
470
471
472 /*
473 * Testing Prefix IPv6
474 * -------------------
475 */
476
477 function t_prefix6()
478 prefix px;
479 {
480 px = 1020::/18;
481 bt_assert(format(px) = "1020::/18");
482 bt_assert(1020:3040:5060:: ~ 1020:3040:5000::/40);
483 bt_assert(1020:3040::/32 ~ 1020:3040::/32);
484 bt_assert(1020:3040::/33 ~ 1020:3040::/32);
485 bt_assert(1020:3040:5060::/48 ~ 1020:3040::/32);
486 bt_assert(1020:3040::/31 !~ 1020:3040::/32);
487 bt_assert(1020:3041::/33 !~ 1020:3040::/32);
488 }
489
490 bt_test_suite(t_prefix6, "Testing prefix IPv6");
491
492
493
494
495 /*
496 * Testing prefix IPv6 sets
497 * ------------------------
498 */
499
500 function t_prefix6_set()
501 prefix set pxs;
502 {
503 bt_assert(1180::/16 ~ [ 1100::/8{15, 17} ]);
504 bt_assert(12::34 = 12::34);
505 bt_assert(12::34 ~ [ 12::33..12::35 ]);
506 bt_assert(1020::34 ~ 1000::/8);
507 bt_assert(1000::/8 ~ 1000::/8);
508 bt_assert(1000::/8 ~ [ 1000::/8+ ]);
509 bt_assert(12::34 !~ [ 12::33, 12::35 ]);
510 bt_assert(1000::/9 !~ [ 1000::/8- ]);
511 bt_assert(1000::/17 !~ [ 1000::/8{15, 16} ]);
512
513 pxs = [ 1102::/16, 1104::/16+];
514 bt_assert(1102::/16 ~ pxs);
515 bt_assert(1104::/16 ~ pxs);
516 bt_assert(1104::/18 ~ pxs);
517 bt_assert(1104::/32 ~ pxs);
518 bt_assert(1101::/16 !~ pxs);
519 bt_assert(1103::/16 !~ pxs);
520 bt_assert(1102::/15 !~ pxs);
521 bt_assert(1102::/17 !~ pxs);
522 bt_assert(1102::/32 !~ pxs);
523 bt_assert(1104::/15 !~ pxs);
524
525 pxs = ([ 1000::/16{8,12}, 2000::/16{24,28} ]);
526 bt_assert(format(pxs) = "[1000::/12{1f0::}, 2000::/16{0:1f0::}]");
527 bt_assert(1000::/8 ~ pxs);
528 bt_assert(1000::/10 ~ pxs);
529 bt_assert(1000::/12 ~ pxs);
530 bt_assert(2000::/24 ~ pxs);
531 bt_assert(2000:4000::/24 ~ pxs);
532 bt_assert(2000::/26 ~ pxs);
533 bt_assert(2000:8000::/26 ~ pxs);
534 bt_assert(2000::/28 ~ pxs);
535 bt_assert(2000:FFF0::/28 ~ pxs);
536 bt_assert(1000::/7 !~ pxs);
537 bt_assert(1000::/13 !~ pxs);
538 bt_assert(1000::/16 !~ pxs);
539 bt_assert(2000::/16 !~ pxs);
540 bt_assert(2000::/23 !~ pxs);
541 bt_assert(2000::/29 !~ pxs);
542 bt_assert(1100::/10 !~ pxs);
543 bt_assert(2010::/26 !~ pxs);
544 }
545
546 bt_test_suite(t_prefix6_set, "Testing prefix IPv6 sets");
547
548
549
550
551 function t_flowspec()
552 prefix p;
553 {
554 p = flow4 { dst 10.0.0.0/8; };
555 bt_assert(p !~ [ 10.0.0.0/8 ] );
556
557 bt_assert(format(flow4 { dst 10.0.0.0/8; proto = 23; }) = "flow4 { dst 10.0.0.0/8; proto 23; }");
558 bt_assert(format(flow6 { dst ::1/128; src ::2/127; }) = "flow6 { dst ::1/128; src ::2/127; }");
559 bt_assert(format(flow6 { next header false 42; }) = "flow6 { next header false 42; }");
560 bt_assert(format(flow6 { port 80; }) = "flow6 { port 80; }");
561 bt_assert(format(flow6 { dport > 24 && < 30 || 40..50,60..70,80 && >= 90; }) = "flow6 { dport > 24 && < 30 || 40..50,60..70,80 && >= 90; }");
562 bt_assert(format(flow6 { sport 0..0x400; }) = "flow6 { sport 0..1024; }");
563 bt_assert(format(flow6 { icmp type 80; }) = "flow6 { icmp type 80; }");
564 bt_assert(format(flow6 { icmp code 90; }) = "flow6 { icmp code 90; }");
565 bt_assert(format(flow6 { tcp flags 0x03/0x0f; }) = "flow6 { tcp flags 0x3/0x3,0x0/0xc; }");
566 bt_assert(format(flow6 { length 0..65535; }) = "flow6 { length 0..65535; }");
567 bt_assert(format(flow6 { dscp = 63; }) = "flow6 { dscp 63; }");
568 bt_assert(format(flow6 { fragment is_fragment || !first_fragment; }) = "flow6 { fragment is_fragment || !first_fragment; }");
569 bt_assert(format(flow6 { }) = "flow6 { }");
570 }
571
572 bt_test_suite(t_flowspec, "Testing flowspec routes");
573
574
575
576
577 /*
578 * Testing Paths
579 * -------------
580 */
581
582 function mkpath(int a; int b)
583 {
584 return [= a b 3 2 1 =];
585 }
586
587 function t_path()
588 bgpmask pm1;
589 bgpmask pm2;
590 bgppath p2;
591 {
592 pm1 = / 4 3 2 1 /;
593 pm2 = [= 4 3 2 1 =];
594
595 bt_assert(pm1 = pm2);
596 bt_assert(format(pm2) = "[= 4 3 2 1 =]");
597
598 bt_assert(+empty+ = +empty+);
599 bt_assert(10 !~ +empty+);
600
601 p2 = prepend( + empty +, 1 );
602 p2 = prepend( p2, 2 );
603 p2 = prepend( p2, 3 );
604 p2 = prepend( p2, 4 );
605
606 bt_assert(format(p2) = "(path 4 3 2 1)");
607 bt_assert(p2.len = 4);
608 bt_assert(p2 ~ pm1);
609 bt_assert(p2 ~ pm2);
610 bt_assert(3 ~ p2);
611 bt_assert(p2 ~ [2, 10..20]);
612 bt_assert(p2 ~ [4, 10..20]);
613
614 p2 = prepend(p2, 5);
615 bt_assert(p2 !~ pm1);
616 bt_assert(p2 !~ pm2);
617 bt_assert(10 !~ p2);
618 bt_assert(p2 !~ [8, ten..(2*ten)]);
619 bt_assert(p2 ~ / ? 4 3 2 1 /);
620 bt_assert(p2 ~ [= * 4 3 * 1 =]);
621 bt_assert(p2 ~ [= (3+2) (2*2) 3 2 1 =]);
622 bt_assert(p2 ~ mkpath(5, 4));
623
624 bt_assert(p2.len = 5);
625 bt_assert(p2.first = 5);
626 bt_assert(p2.last = 1);
627
628 bt_assert(p2.len = 5);
629 bt_assert(delete(p2, 3) = prepend(prepend(prepend(prepend(+empty+, 1), 2), 4), 5));
630 bt_assert(filter(p2, [1..3]) = prepend(prepend(prepend(+empty+, 1), 2), 3));
631
632 pm1 = [= 1 2 * 3 4 5 =];
633 p2 = prepend( + empty +, 5 );
634 p2 = prepend( p2, 4 );
635 p2 = prepend( p2, 3 );
636 p2 = prepend( p2, 3 );
637 p2 = prepend( p2, 2 );
638 p2 = prepend( p2, 1 );
639
640 bt_assert(p2 ~ pm1);
641 bt_assert(delete(p2, 3) = prepend(prepend(prepend(prepend(+empty+, 5), 4), 2), 1));
642 bt_assert(delete(p2, [4..5]) = prepend(prepend(prepend(prepend(+empty+, 3), 3), 2), 1));
643 }
644
645 bt_test_suite(t_path, "Testing paths");
646
647
648
649
650 /*
651 * Testing Community List
652 * ----------------------
653 */
654
655 define p23 = (2, 3);
656
657 function t_clist()
658 clist l;
659 clist l2;
660 clist r;
661 {
662 l = - empty -;
663 bt_assert(l !~ [(*,*)]);
664 bt_assert((l ~ [(*,*)]) != (l !~ [(*,*)]));
665
666 bt_assert(-empty- = -empty-);
667
668 l = add( l, (one,2) );
669 bt_assert(l ~ [(*,*)]);
670 l = add( l, (2,one+2) );
671 bt_assert(format(l) = "(clist (1,2) (2,3))");
672
673 bt_assert((2,3) ~ l);
674 bt_assert(l ~ [(1,*)]);
675 bt_assert(l ~ [p23]);
676 bt_assert(l ~ [(2,2..3)]);
677 bt_assert(l ~ [(1,1..2)]);
678 bt_assert(l ~ [(1,1)..(1,2)]);
679
680 l = add(l, (2,5));
681 l = add(l, (5,one));
682 l = add(l, (6,one));
683 l = add(l, (one,one));
684 l = delete(l, [(5,1),(6,one),(one,1)]);
685 l = delete(l, [(5,one),(6,one)]);
686 l = filter(l, [(1,*)]);
687 bt_assert(l = add(-empty-, (1,2)));
688
689 bt_assert((2,3) !~ l);
690 bt_assert(l !~ [(2,*)]);
691 bt_assert(l !~ [(one,3..6)]);
692 bt_assert(l ~ [(*,*)]);
693
694 l = add(l, (3,one));
695 l = add(l, (one+one+one,one+one));
696 l = add(l, (3,3));
697 l = add(l, (3,4));
698 l = add(l, (3,5));
699 l2 = filter(l, [(3,*)]);
700 l = delete(l, [(3,2..4)]);
701 bt_assert(l = add(add(add(-empty-, (1,2)), (3,1)), (3,5)));
702 bt_assert(l.len = 3);
703
704 l = add(l, (3,2));
705 l = add(l, (4,5));
706 bt_assert(l = add(add(add(add(add(-empty-, (1,2)), (3,1)), (3,5)), (3,2)), (4,5)));
707
708 bt_assert(l.len = 5);
709 bt_assert(l ~ [(*,2)]);
710 bt_assert(l ~ [(*,5)]);
711 bt_assert(l ~ [(*, one)]);
712 bt_assert(l !~ [(*,3)]);
713 bt_assert(l !~ [(*,(one+6))]);
714 bt_assert(l !~ [(*, (one+one+one))]);
715
716 l = delete(l, [(*,(one+onef(3)))]);
717 l = delete(l, [(*,(4+one))]);
718 bt_assert(l = add(-empty-, (3,1)));
719
720 l = delete(l, [(*,(onef(5)))]);
721 bt_assert(l = -empty-);
722
723 l2 = add(l2, (3,6));
724 l = filter(l2, [(3,1..4)]);
725 l2 = filter(l2, [(3,3..6)]);
726
727 # clist A (10,20,30)
728 bt_assert(l = add(add(add(add(-empty-, (3,1)), (3,2)), (3,3)), (3,4)));
729 bt_assert(format(l) = "(clist (3,1) (3,2) (3,3) (3,4))");
730
731 # clist B (30,40,50)
732 bt_assert(l2 = add(add(add(add(-empty-, (3,3)), (3,4)), (3,5)), (3,6)));
733 bt_assert(format(l2) = "(clist (3,3) (3,4) (3,5) (3,6))");
734
735 # clist A union B
736 r = add(l, l2);
737 bt_assert(r = add(add(add(add(add(add(-empty-, (3,1)), (3,2)), (3,3)), (3,4)), (3,5)), (3,6)));
738 bt_assert(format(r) = "(clist (3,1) (3,2) (3,3) (3,4) (3,5) (3,6))");
739
740 # clist A isect B
741 r = filter(l, l2);
742 bt_assert(r = add(add(-empty-, (3,3)), (3,4)));
743 bt_assert(format(r) = "(clist (3,3) (3,4))");
744
745 # clist A \ B
746 r = delete(l, l2);
747 bt_assert(r = add(add(-empty-, (3,1)), (3,2)));
748 bt_assert(format(r) = "(clist (3,1) (3,2))");
749
750 # clist in c set
751 r = filter(l, [(3,1), (*,2)]);
752 bt_assert(r = add(add(-empty-, (3,1)), (3,2)));
753 bt_assert(format(r) = "(clist (3,1) (3,2))");
754 }
755
756 bt_test_suite(t_clist, "Testing lists of communities");
757
758
759
760
761 /*
762 * Testing Extended Communities
763 * ----------------------------
764 */
765
766 function t_ec()
767 ec cc;
768 {
769 cc = (rt, 12345, 200000);
770 bt_assert(format(cc) = "(rt, 12345, 200000)");
771
772 bt_assert(cc = (rt, 12345, 200000));
773 bt_assert(cc < (rt, 12345, 200010));
774 bt_assert(cc != (rt, 12346, 200000));
775 bt_assert(cc != (ro, 12345, 200000));
776 bt_assert(!(cc > (rt, 12345, 200010)));
777
778 bt_assert(format((ro, 100000, 20000)) = "(ro, 100000, 20000)");
779 }
780
781 bt_test_suite(t_ec, "Testing extended communities");
782
783
784
785
786 /*
787 * Testing Extended Community List
788 * -------------------------------
789 */
790
791 function t_eclist()
792 eclist el;
793 eclist el2;
794 eclist r;
795 {
796 el = -- empty --;
797 el = add(el, (rt, 10, 20));
798 el = add(el, (ro, 10.20.30.40, 100));
799 el = add(el, (ro, 11.21.31.41.mask(16), 200));
800
801 bt_assert(--empty-- = --empty--);
802 bt_assert(((rt, 10, 20)) !~ --empty--);
803
804 bt_assert(format(el) = "(eclist (rt, 10, 20) (ro, 10.20.30.40, 100) (ro, 11.21.0.0, 200))");
805 bt_assert(el.len = 3);
806 el = delete(el, (rt, 10, 20));
807 el = delete(el, (rt, 10, 30));
808 bt_assert(el = add(add(--empty--, (ro, 10.20.30.40, 100)), (ro, 11.21.0.0, 200)));
809 el = add(el, (unknown 2, ten, 1));
810 el = add(el, (unknown 5, ten, 1));
811 el = add(el, (rt, ten, one+one));
812 el = add(el, (rt, 10, 3));
813 el = add(el, (rt, 10, 4));
814 el = add(el, (rt, 10, 5));
815 el = add(el, (generic, 0x2000a, 3*ten));
816 el = delete(el, [(rt, 10, 2..ten)]);
817 bt_assert(el = add(add(add(add(add(--empty--, (ro, 10.20.30.40, 100)), (ro, 11.21.0.0, 200)), (rt, 10, 1)), (unknown 5, 10, 1)), (rt, 10, 30)));
818
819 el = filter(el, [(rt, 10, *)]);
820 bt_assert(el = add(add(--empty--, (rt, 10, 1)), (rt, 10, 30)));
821 bt_assert((rt, 10, 1) ~ el);
822 bt_assert(el ~ [(rt, 10, ten..40)]);
823 bt_assert((rt, 10, 20) !~ el);
824 bt_assert((ro, 10.20.30.40, 100) !~ el);
825 bt_assert(el !~ [(rt, 10, 35..40)]);
826 bt_assert(el !~ [(ro, 10, *)]);
827
828 el = add(el, (rt, 10, 40));
829 el2 = filter(el, [(rt, 10, 20..40)] );
830 el2 = add(el2, (rt, 10, 50));
831
832 # eclist A (1,30,40)
833 bt_assert(el = add(add(add(--empty--, (rt, 10, 1)), (rt, 10, 30)), (rt, 10, 40)));
834 bt_assert(format(el) = "(eclist (rt, 10, 1) (rt, 10, 30) (rt, 10, 40))");
835
836 # eclist B (30,40,50)
837 bt_assert(el2 = add(add(add(--empty--, (rt, 10, 30)), (rt, 10, 40)), (rt, 10, 50)));
838 bt_assert(format(el2) = "(eclist (rt, 10, 30) (rt, 10, 40) (rt, 10, 50))");
839
840 # eclist A union B
841 r = add(el2, el);
842 bt_assert(r = add(add(add(add(--empty--, (rt, 10, 30)), (rt, 10, 40)), (rt, 10, 50)), (rt, 10, 1)));
843 bt_assert(format(r) = "(eclist (rt, 10, 30) (rt, 10, 40) (rt, 10, 50) (rt, 10, 1))");
844
845 # eclist A isect B
846 r = filter(el, el2);
847 bt_assert(r = add(add(--empty--, (rt, 10, 30)), (rt, 10, 40)));
848 bt_assert(format(r) = "(eclist (rt, 10, 30) (rt, 10, 40))");
849
850 # eclist A \ B
851 r = delete(el, el2);
852 bt_assert(r = add(--empty--, (rt, 10, 1)));
853 bt_assert(format(r) = "(eclist (rt, 10, 1))");
854
855 # eclist in ec set
856 r = filter(el, [(rt, 10, 1), (rt, 10, 25..30), (ro, 10, 40)]);
857 bt_assert(r = add(add(--empty--, (rt, 10, 1)), (rt, 10, 30)));
858 bt_assert(format(r) = "(eclist (rt, 10, 1) (rt, 10, 30))");
859 }
860
861 bt_test_suite(t_eclist, "Testing lists of extended communities");
862
863
864
865
866 /*
867 * Testing sets of Extended Communities
868 * ------------------------------------
869 */
870
871 define ecs2 = [(rt, ten, (one+onef(0))*10), (ro, 100000, 100..200), (rt, 12345, *)];
872
873 function t_ec_set()
874 ec set ecs;
875 {
876 ecs = [(rt, ten, (one+onef(0))*10), (ro, 100000, 100..200), (rt, 12345, *)];
877 bt_assert(format(ecs) = "[(rt, 10, 20), (rt, 12345, 0)..(rt, 12345, 4294967295), (ro, 100000, 100)..(ro, 100000, 200)]");
878 bt_assert(format(ecs2) = "[(rt, 10, 20), (rt, 12345, 0)..(rt, 12345, 4294967295), (ro, 100000, 100)..(ro, 100000, 200)]");
879
880 bt_assert((rt, 10, 20) ~ ecs);
881 bt_assert((ro, 100000, 100) ~ ecs);
882 bt_assert((ro, 100000, 128) ~ ecs);
883 bt_assert((ro, 100000, 200) ~ ecs);
884 bt_assert((rt, 12345, 0) ~ ecs);
885 bt_assert((rt, 12345, 200000) ~ ecs);
886 bt_assert((rt, 12345, 4000000) ~ ecs);
887 bt_assert((ro, 10, 20) !~ ecs);
888 bt_assert((rt, 10, 21) !~ ecs);
889 bt_assert((ro, 100000, 99) !~ ecs);
890 bt_assert((ro, 12345, 10) !~ ecs);
891 bt_assert((rt, 12346, 0) !~ ecs);
892 bt_assert((ro, 0.1.134.160, 150) !~ ecs);
893 }
894
895 bt_test_suite(t_ec_set, "Testing sets of extended communities");
896
897
898
899
900 /*
901 * Testing Large Communities
902 * -------------------------
903 */
904
905 function mktrip(int a)
906 {
907 return (a, 2*a, 3*a);
908 }
909
910 function t_lclist()
911 lclist ll;
912 lclist ll2;
913 lclist r;
914 {
915 bt_assert(---empty--- = ---empty---);
916 bt_assert((10, 20, 30) !~ ---empty---);
917
918 ll = --- empty ---;
919 ll = add(ll, (ten, 20, 30));
920 ll = add(ll, (1000, 2000, 3000));
921 ll = add(ll, mktrip(100000));
922 bt_assert(format(ll) = "(lclist (10, 20, 30) (1000, 2000, 3000) (100000, 200000, 300000))");
923 bt_assert(ll.len = 3);
924 bt_assert(ll = add(add(add(---empty---, (10, 20, 30)), (1000, 2000, 3000)), (100000, 200000, 300000)));
925
926 bt_assert(mktrip(1000) ~ ll);
927 bt_assert(mktrip(100) !~ ll);
928
929 ll = --- empty ---;
930 ll = add(ll, (10, 10, 10));
931 ll = add(ll, (20, 20, 20));
932 ll = add(ll, (30, 30, 30));
933
934 ll2 = --- empty ---;
935 ll2 = add(ll2, (20, 20, 20));
936 ll2 = add(ll2, (30, 30, 30));
937 ll2 = add(ll2, (40, 40, 40));
938
939 # lclist A (10, 20, 30)
940 bt_assert(format(ll) = "(lclist (10, 10, 10) (20, 20, 20) (30, 30, 30))");
941
942 # lclist B (20, 30, 40)
943 bt_assert(format(ll2) = "(lclist (20, 20, 20) (30, 30, 30) (40, 40, 40))");
944
945 # lclist A union B
946 r = add(ll, ll2);
947 bt_assert(r = add(add(add(add(---empty---, (10,10,10)), (20,20,20)), (30,30,30)), (40,40,40)));
948 bt_assert(format(r) = "(lclist (10, 10, 10) (20, 20, 20) (30, 30, 30) (40, 40, 40))");
949
950 # lclist A isect B
951 r = filter(ll, ll2);
952 bt_assert(r = add(add(---empty---, (20, 20, 20)), (30, 30, 30)));
953 bt_assert(format(r) = "(lclist (20, 20, 20) (30, 30, 30))");
954
955 # lclist A \ B
956 r = delete(ll, ll2);
957 bt_assert(r = add(---empty---, (10, 10, 10)));
958 bt_assert(format(r) = "(lclist (10, 10, 10))");
959
960 # lclist in lc set
961 r = filter(ll, [(5..15, *, *), (20, 15..25, *)]);
962 bt_assert(r = add(add(---empty---, (10, 10, 10)), (20, 20, 20)));
963 bt_assert(format(r) = "(lclist (10, 10, 10) (20, 20, 20))");
964 }
965
966 bt_test_suite(t_lclist, "Testing lists of large communities");
967
968
969
970
971 /*
972 * Testing sets of Large Communities
973 * ---------------------------------
974 */
975
976 function t_lclist_set()
977 lclist ll;
978 lc set lls;
979 {
980 ll = --- empty ---;
981 ll = add(ll, (10, 20, 30));
982 ll = add(ll, (1000, 2000, 3000));
983 ll = add(ll, mktrip(100000));
984
985 bt_assert(ll ~ [(5,10,15), (10,20,30)]);
986 bt_assert(ll ~ [(10,15..25,*)]);
987 bt_assert(ll ~ [(ten, *, *)]);
988
989 bt_assert(ll !~ [(5,10,15), (10,21,30)]);
990 bt_assert(ll !~ [(10,21..25,*)]);
991 bt_assert(ll !~ [(11, *, *)]);
992
993 lls = [(10, 10, 10), (20, 20, 15..25), (30, 30, *), (40, 35..45, *), (50, *, *), (55..65, *, *)];
994 bt_assert(format(lls) = "[(10, 10, 10), (20, 20, 15)..(20, 20, 25), (30, 30, 0)..(30, 30, 4294967295), (40, 35, 0)..(40, 45, 4294967295), (50, 0, 0)..(50, 4294967295, 4294967295), (55, 0, 0)..(65, 4294967295, 4294967295)]");
995 bt_assert((10, 10, 10) ~ lls);
996 bt_assert((20, 20, 25) ~ lls);
997 bt_assert((20, 20, 26) !~ lls);
998 bt_assert((30, 30, 0) ~ lls);
999 bt_assert((40, 35, 40) ~ lls);
1000 bt_assert((40, 34, 40) !~ lls);
1001 bt_assert((50, 0, 0) ~ lls);
1002 bt_assert((60, 60, 60) ~ lls);
1003 bt_assert((70, 60, 60) !~ lls);
1004 }
1005
1006 bt_test_suite(t_lclist_set, "Testing sets of large communities");
1007
1008
1009
1010
1011 /*
1012 * Testing defined() function
1013 * --------------------------
1014 */
1015
1016 function test_undef(int a)
1017 int b;
1018 {
1019 if a = 3 then {
1020 b = 4;
1021 bt_assert(defined(b));
1022 }
1023 else {
1024 bt_assert(!defined(b));
1025 }
1026 }
1027
1028 function t_define()
1029 int i;
1030 {
1031 test_undef(2);
1032 test_undef(3);
1033 test_undef(2);
1034
1035 bt_assert(defined(1));
1036 bt_assert(defined(1.2.3.4));
1037 }
1038
1039 bt_test_suite(t_define, "Testing defined() function");
1040
1041
1042
1043
1044 /*
1045 * Testing calling functions
1046 * -------------------------
1047 */
1048
1049 function callme(int arg1; int arg2)
1050 int i;
1051 {
1052 case arg1 {
1053 1, 42: return 42;
1054 else: return arg1 * arg2;
1055 }
1056
1057 return 0;
1058 }
1059
1060 function fifteen()
1061 {
1062 return 15;
1063 }
1064
1065 function t_call_function()
1066 {
1067 bt_assert(fifteen() = 15);
1068
1069 bt_assert(callme(1, 2) = 42);
1070 bt_assert(callme(42, 2) = 42);
1071
1072 bt_assert(callme(2, 2) = 4);
1073 bt_assert(callme(3, 2) = 6);
1074 bt_assert(callme(4, 4) = 16);
1075 bt_assert(callme(7, 2) = 14);
1076 }
1077
1078 bt_test_suite(t_call_function, "Testing calling functions");
1079
1080
1081
1082
1083 /*
1084 * Test including another config file
1085 * ----------------------------------
1086 */
1087
1088 function t_include()
1089 int i;
1090 {
1091 i = 1;
1092 include "test.conf.inc";
1093 bt_assert(i = 42);
1094 }
1095
1096 bt_test_suite(t_include, "Testing including another config file");
1097
1098
1099
1100
1101 /*
1102 * Test if-else statement
1103 * ----------------------
1104 */
1105
1106 function t_if_else()
1107 int i;
1108 {
1109 if true then
1110 bt_assert(true);
1111
1112 if false then
1113 bt_assert(false);
1114 else if true then
1115 bt_assert(true);
1116 else
1117 bt_assert(false);
1118 }
1119
1120 bt_test_suite(t_if_else, "Testing if-else statement");
1121
1122
1123
1124
1125 /*
1126 * Unused functions -- testing only parsing
1127 * ----------------------------------------
1128 */
1129
1130 function __test1()
1131 {
1132 if source ~ [ RTS_BGP, RTS_STATIC ] then {
1133 # ospf_metric1 = 65535;
1134 # ospf_metric2 = 1000;
1135 ospf_tag = 0x12345678;
1136 accept;
1137 }
1138 reject;
1139 }
1140
1141 function __test2()
1142 {
1143 if source ~ [ RTS_BGP, RTS_STATIC ] then {
1144 # ospf_metric1 = 65535;
1145 # ospf_metric2 = 1000;
1146 ospf_tag = 0x12345678;
1147 accept;
1148 }
1149 reject;
1150 }
1151
1152 filter testf
1153 int j;
1154 {
1155 print "Heya, filtering route to ", net.ip, " prefixlen ", net.len, " source ", source;
1156 print "This route was from ", from;
1157 j = 7;
1158 j = 17;
1159 if rip_metric > 15 then {
1160 reject "RIP Metric is more than infinity";
1161 }
1162 rip_metric = 14;
1163 unset(rip_metric);
1164
1165 accept "ok I take that";
1166 }
1167
1168 roa4 table r4;
1169 roa6 table r6;
1170
1171 protocol static
1172 {
1173 roa4 { table r4; };
1174 route 10.110.0.0/16 max 16 as 1000 blackhole;
1175 route 10.120.0.0/16 max 24 as 1000 blackhole ;
1176 route 10.130.0.0/16 max 24 as 2000 blackhole;
1177 route 10.130.128.0/18 max 24 as 3000 blackhole;
1178 }
1179
1180 protocol static
1181 {
1182 roa6 { table r6; };
1183 route 2001:0db8:85a3:8a2e::/64 max 96 as 1000 blackhole;
1184 }
1185
1186 function test_roa_check()
1187 {
1188 # cannot be tested in __startup(), sorry
1189 print "Should be true: ", roa_check(r4, 10.10.0.0/16, 1000) = ROA_UNKNOWN,
1190 " ", roa_check(r4, 10.0.0.0/8, 1000) = ROA_UNKNOWN,
1191 " ", roa_check(r4, 10.110.0.0/16, 1000) = ROA_VALID,
1192 " ", roa_check(r4, 10.110.0.0/16, 2000) = ROA_INVALID,
1193 " ", roa_check(r4, 10.110.32.0/20, 1000) = ROA_INVALID,
1194 " ", roa_check(r4, 10.120.32.0/20, 1000) = ROA_VALID;
1195 print "Should be true: ", roa_check(r4, 10.120.32.0/20, 2000) = ROA_INVALID,
1196 " ", roa_check(r4, 10.120.32.32/28, 1000) = ROA_INVALID,
1197 " ", roa_check(r4, 10.130.130.0/24, 1000) = ROA_INVALID,
1198 " ", roa_check(r4, 10.130.130.0/24, 2000) = ROA_VALID,
1199 " ", roa_check(r4, 10.130.30.0/24, 3000) = ROA_INVALID,
1200 " ", roa_check(r4, 10.130.130.0/24, 3000) = ROA_VALID;
1201 print "Should be true: ", roa_check(r6, 2001:0db8:85a3:8a2e:1234::/80, 1000) = ROA_VALID,
1202 " ", roa_check(r6, 2001:0db8:85a3:8a2e:1234::/97, 1000) = ROA_INVALID,
1203 " ", roa_check(r6, 2001:0db8:85a3:8a2e::/64, 1000) = ROA_VALID,
1204 " ", roa_check(r6, 2001:0db8:85a3::/48, 1000) = ROA_UNKNOWN;
1205
1206 print "Should be true: ", roa_check(r4, 10.10.0.0/16, 1000) = ROA_UNKNOWN,
1207 " ", roa_check(r4, 10.0.0.0/8, 1000) = ROA_UNKNOWN,
1208 " ", roa_check(r4, 10.110.0.0/16, 1000) = ROA_VALID,
1209 " ", roa_check(r4, 10.110.0.0/16, 2000) = ROA_INVALID,
1210 " ", roa_check(r4, 10.110.32.0/20, 1000) = ROA_INVALID,
1211 " ", roa_check(r4, 10.120.32.0/20, 1000) = ROA_VALID;
1212
1213 print "Should be true: ", roa_check(r6, 2001:0db8:85a3:8a2e:1234::/80, 1000) = ROA_VALID,
1214 " ", roa_check(r6, 2001:0db8:85a3:8a2e:1234::/97, 1000) = ROA_INVALID,
1215 " ", roa_check(r6, 2001:0db8:85a3:8a2e::/64, 1000) = ROA_VALID,
1216 " ", roa_check(r6, 2001:0db8:85a3::/48, 1000) = ROA_UNKNOWN;
1217
1218 print "Should be true: ", roa_check(r4, 2001:0db8:85a3:8a2e:1234::/97, 1000) = ROA_INVALID ||
1219 roa_check(r6, 2001:0db8:85a3:8a2e:1234::/97, 1000) = ROA_INVALID;
1220
1221 print "Should be false: ", roa_check(r4, 2001:0db8:85a3:8a2e:1234::/80, 1000) = ROA_INVALID ||
1222 roa_check(r6, 2001:0db8:85a3:8a2e:1234::/80, 1000) = ROA_INVALID,
1223 " ", roa_check(r4, 2001:0db8:85a3::/48, 1000) = ROA_INVALID ||
1224 roa_check(r6, 2001:0db8:85a3::/48, 1000) = ROA_INVALID;
1225
1226 print "Should be true: ", 10.130.130.0/24 ~ 0.0.0.0/0,
1227 " ", 2001:0db8:85a3:8a2e::/64 ~ ::/0;
1228 print "Should be false: ", 10.130.130.0/24 ~ ::/0,
1229 " ", 2001:0db8:85a3:8a2e::/64 ~ 0.0.0.0/0;
1230 }
1231
1232 function roa_operators_test()
1233 prefix pfx;
1234 {
1235 print "Testing ROA prefix operators '.maxlen' and '.asn':";
1236
1237 pfx = 12.13.0.0/16 max 24 as 1234;
1238 print pfx;
1239 print "Should be true: ", pfx.len = 16, " ", pfx.maxlen = 24, " ", pfx.asn = 1234;
1240
1241 pfx = 1000::/8 max 32 as 1234;
1242 print pfx;
1243 print "Should be true: ", pfx.len = 8, " ", pfx.maxlen = 32, " ", pfx.asn = 1234;
1244 }