2 * This is unit testing configuration file for testing filters
4 * FIXME: add all examples from docs here.
9 /* We have to setup any protocol */
10 protocol static { ipv4; }
16 * Common definitions and functions
17 * --------------------------------
32 * Testing boolean expressions
33 * ---------------------------
43 bt_assert(format(true) = "TRUE");
44 bt_assert(format(false) = "FALSE");
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));
61 bt_test_suite(t_bool, "Testing boolean expressions");
72 define xyzzy = (120+10);
73 define '1a-a1' = (xyzzy-100);
78 bt_assert(xyzzy = 130);
79 bt_assert('1a-a1' = 30);
82 i = 12*100 + 60/2 + i;
86 bt_assert(format(i) = "1234");
89 bt_assert(i = 4200000000);
90 bt_assert(i > 4100000000);
91 bt_assert(!(i > 4250000000));
100 bt_assert(1 != (0,1));
104 bt_assert(!(1234 < 1234));
107 bt_test_suite(t_int, "Testing integers");
113 * Testing sets of integers
114 * ------------------------
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];
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 ]);
130 is = [ 2, 3, 4, 7..11 ];
137 bt_assert((one+2) ~ is1);
143 bt_assert(10 !~ is1);
144 bt_assert(10 !~ is2);
145 bt_assert(10 !~ is3);
149 bt_assert(18 !~ is1);
150 bt_assert(18 !~ is2);
151 bt_assert(18 !~ is3);
155 bt_assert(20 !~ is1);
156 bt_assert(20 !~ is2);
157 bt_assert(20 !~ is3);
159 bt_assert([1,2] != [1,3]);
160 bt_assert([1,4..10,20] = [1,4..10,20]);
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]");
165 bt_test_suite(t_int_set, "Testing sets of integers");
171 * Testing string matching
172 * -----------------------
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*");
187 bt_test_suite(t_string, "Testing string matching");
197 function 'mkpair-a'(int a)
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)));
215 bt_test_suite(t_pair, "Testing pairs");
221 * Testing sets of pairs
222 * ---------------------
225 function t_pair_set()
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)]");
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);
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);
262 bt_test_suite(t_pair_set, "Testing sets of pairs");
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);
281 bt_test_suite(t_quad, "Testing quads");
287 * Testing sets of quads
288 * ---------------------
291 function t_quad_set()
295 bt_assert(qq ~ [1.2.3.4, 5.6.7.8]);
296 bt_assert(qq !~ [1.2.1.1, 1.2.3.5]);
299 bt_test_suite(t_quad_set, "Testing sets of quads");
309 define onetwo = 1.2.3.4;
316 bt_assert(p.mask(8) = 127.0.0.0);
317 bt_assert(1.2.3.4 = 1.2.3.4);
318 bt_assert(1.2.3.4 = onetwo);
319 bt_assert(format(p) = "127.1.2.3");
321 p = ::fffe:6:c0c:936d:88c7:35d3;
323 bt_assert(format(p) = "::fffe:6:c0c:936d:88c7:35d3");
327 bt_assert(p.mask(24) = 1234:5600::);
330 bt_test_suite(t_ip, "Testing ip address");
336 * Testing sets of ip address
337 * --------------------------
340 define ip1222 = 1.2.2.2;
345 ips = [ 1.1.1.0 .. 1.1.1.255, ip1222];
346 bt_assert(format(ips) = "[1.1.1.0..1.1.1.255, 1.2.2.2]");
347 bt_assert(1.1.1.0 ~ ips);
348 bt_assert(1.1.1.100 ~ ips);
349 bt_assert(1.2.2.2 ~ ips);
350 bt_assert(1.1.0.255 !~ ips);
351 bt_assert(1.1.2.0 !~ ips);
352 bt_assert(1.2.2.3 !~ ips);
353 bt_assert(192.168.1.1 !~ ips);
355 bt_assert(1.2.3.4 !~ [ 1.2.3.3, 1.2.3.5 ]);
356 bt_assert(1.2.3.4 ~ [ 1.2.3.3..1.2.3.5 ]);
359 bt_test_suite(t_ip_set, "Testing sets of ip address");
371 bt_assert(format(RTS_DUMMY) = "(enum 30)0");
372 bt_assert(format(RTS_STATIC) = "(enum 30)1");
373 bt_assert(RTS_STATIC ~ [RTS_STATIC, RTS_DEVICE]);
374 bt_assert(RTS_BGP !~ [RTS_STATIC, RTS_DEVICE]);
377 bt_test_suite(t_enum, "Testing enums");
387 define netdoc = 2001:db8::/32;
393 bt_assert(format(px) = "1.2.0.0/18");
394 bt_assert(192.168.0.0/16 ~ 192.168.0.0/16);
395 bt_assert(192.168.0.0/17 ~ 192.168.0.0/16);
396 bt_assert(192.168.254.0/24 ~ 192.168.0.0/16);
397 bt_assert(netdoc ~ 2001::/16);
398 bt_assert(192.168.0.0/15 !~ 192.168.0.0/16);
399 bt_assert(192.160.0.0/17 !~ 192.168.0.0/16);
400 bt_assert(px !~ netdoc);
402 bt_assert(1.2.3.4 ~ 1.0.0.0/8);
403 bt_assert(1.0.0.0/8 ~ 1.0.0.0/8);
406 bt_test_suite(t_prefix, "Testing prefixes");
412 * Testing prefix sets
413 * -------------------
416 define net10 = 10.0.0.0/8;
417 define pxs2 = [ 10.0.0.0/16{8,12}, 20.0.0.0/16{24,28} ];
419 function test_pxset(prefix set pxs)
421 bt_assert(net10 ~ pxs);
422 bt_assert(10.0.0.0/10 ~ pxs);
423 bt_assert(10.0.0.0/12 ~ pxs);
424 bt_assert(20.0.0.0/24 ~ pxs);
425 bt_assert(20.0.40.0/24 ~ pxs);
426 bt_assert(20.0.0.0/26 ~ pxs);
427 bt_assert(20.0.100.0/26 ~ pxs);
428 bt_assert(20.0.0.0/28 ~ pxs);
429 bt_assert(20.0.255.0/28 ~ pxs);
431 bt_assert(10.0.0.0/7 !~ pxs);
432 bt_assert(10.0.0.0/13 !~ pxs);
433 bt_assert(10.0.0.0/16 !~ pxs);
434 bt_assert(20.0.0.0/16 !~ pxs);
435 bt_assert(20.0.0.0/23 !~ pxs);
436 bt_assert(20.0.0.0/29 !~ pxs);
437 bt_assert(11.0.0.0/10 !~ pxs);
438 bt_assert(20.1.0.0/26 !~ pxs);
440 bt_assert(1.0.0.0/8 ~ [ 1.0.0.0/8+ ]);
441 bt_assert(1.0.0.0/9 !~ [ 1.0.0.0/8- ]);
442 bt_assert(1.2.0.0/17 !~ [ 1.0.0.0/8{ 15 , 16 } ]);
444 bt_assert([ 10.0.0.0/8{ 15 , 17 } ] = [ 10.0.0.0/8{ 15 , 17 } ]);
447 function t_prefix_set()
450 pxs = [ 1.2.0.0/16, 1.4.0.0/16+, 44.66.88.64/30{24,28}, 12.34.56.0/24{8,16} ];
451 bt_assert(format(pxs) = "[1.2.0.0/112{::0.1.0.0}, 1.4.0.0/112{::0.1.255.255}, 12.34.0.0/112{::1.255.0.0}, 44.66.88.64/124{::1f0}]");
452 bt_assert(1.2.0.0/16 ~ pxs);
453 bt_assert(1.4.0.0/16 ~ pxs);
454 bt_assert(1.4.0.0/18 ~ pxs);
455 bt_assert(1.4.0.0/32 ~ pxs);
456 bt_assert(1.1.0.0/16 !~ pxs);
457 bt_assert(1.3.0.0/16 !~ pxs);
458 bt_assert(1.2.0.0/15 !~ pxs);
459 bt_assert(1.2.0.0/17 !~ pxs);
460 bt_assert(1.2.0.0/32 !~ pxs);
461 bt_assert(1.4.0.0/15 !~ pxs);
464 test_pxset([ 10.0.0.0/16{8,12}, 20.0.0.0/16{24,28} ]);
466 bt_assert(1.2.0.0/16 ~ [ 1.0.0.0/8{ 15 , 17 } ]);
467 bt_assert([ 10.0.0.0/8{ 15 , 17 } ] != [ 11.0.0.0/8{ 15 , 17 } ]);
470 bt_test_suite(t_prefix_set, "Testing prefix sets");
476 * Testing Prefix IPv6
477 * -------------------
484 bt_assert(format(px) = "1020::/18");
485 bt_assert(1020:3040:5060:: ~ 1020:3040:5000::/40);
486 bt_assert(1020:3040::/32 ~ 1020:3040::/32);
487 bt_assert(1020:3040::/33 ~ 1020:3040::/32);
488 bt_assert(1020:3040:5060::/48 ~ 1020:3040::/32);
489 bt_assert(1020:3040::/31 !~ 1020:3040::/32);
490 bt_assert(1020:3041::/33 !~ 1020:3040::/32);
493 bt_test_suite(t_prefix6, "Testing prefix IPv6");
499 * Testing prefix IPv6 sets
500 * ------------------------
503 function t_prefix6_set()
506 bt_assert(1180::/16 ~ [ 1100::/8{15, 17} ]);
507 bt_assert(12::34 = 12::34);
508 bt_assert(12::34 ~ [ 12::33..12::35 ]);
509 bt_assert(1020::34 ~ 1000::/8);
510 bt_assert(1000::/8 ~ 1000::/8);
511 bt_assert(1000::/8 ~ [ 1000::/8+ ]);
512 bt_assert(12::34 !~ [ 12::33, 12::35 ]);
513 bt_assert(1000::/9 !~ [ 1000::/8- ]);
514 bt_assert(1000::/17 !~ [ 1000::/8{15, 16} ]);
516 pxs = [ 1102::/16, 1104::/16+];
517 bt_assert(1102::/16 ~ pxs);
518 bt_assert(1104::/16 ~ pxs);
519 bt_assert(1104::/18 ~ pxs);
520 bt_assert(1104::/32 ~ pxs);
521 bt_assert(1101::/16 !~ pxs);
522 bt_assert(1103::/16 !~ pxs);
523 bt_assert(1102::/15 !~ pxs);
524 bt_assert(1102::/17 !~ pxs);
525 bt_assert(1102::/32 !~ pxs);
526 bt_assert(1104::/15 !~ pxs);
528 pxs = ([ 1000::/16{8,12}, 2000::/16{24,28} ]);
529 bt_assert(format(pxs) = "[1000::/12{1f0::}, 2000::/16{0:1f0::}]");
530 bt_assert(1000::/8 ~ pxs);
531 bt_assert(1000::/10 ~ pxs);
532 bt_assert(1000::/12 ~ pxs);
533 bt_assert(2000::/24 ~ pxs);
534 bt_assert(2000:4000::/24 ~ pxs);
535 bt_assert(2000::/26 ~ pxs);
536 bt_assert(2000:8000::/26 ~ pxs);
537 bt_assert(2000::/28 ~ pxs);
538 bt_assert(2000:FFF0::/28 ~ pxs);
539 bt_assert(1000::/7 !~ pxs);
540 bt_assert(1000::/13 !~ pxs);
541 bt_assert(1000::/16 !~ pxs);
542 bt_assert(2000::/16 !~ pxs);
543 bt_assert(2000::/23 !~ pxs);
544 bt_assert(2000::/29 !~ pxs);
545 bt_assert(1100::/10 !~ pxs);
546 bt_assert(2010::/26 !~ pxs);
549 bt_test_suite(t_prefix6_set, "Testing prefix IPv6 sets");
554 function t_flowspec()
557 p = flow4 { dst 10.0.0.0/8; };
558 bt_assert(p !~ [ 10.0.0.0/8 ] );
560 bt_assert(format(flow4 { dst 10.0.0.0/8; proto = 23; }) = "flow4 { dst 10.0.0.0/8; proto 23; }");
561 bt_assert(format(flow6 { dst ::1/128; src ::2/127; }) = "flow6 { dst ::1/128; src ::2/127; }");
562 bt_assert(format(flow6 { next header false 42; }) = "flow6 { next header false 42; }");
563 bt_assert(format(flow6 { port 80; }) = "flow6 { port 80; }");
564 bt_assert(format(flow6 { dport > 24 && < 30 || 40..50,60..70,80 && >= 90; }) = "flow6 { dport > 24 && < 30 || 40..50,60..70,80 && >= 90; }");
565 bt_assert(format(flow6 { sport 0..0x400; }) = "flow6 { sport 0..1024; }");
566 bt_assert(format(flow6 { icmp type 80; }) = "flow6 { icmp type 80; }");
567 bt_assert(format(flow6 { icmp code 90; }) = "flow6 { icmp code 90; }");
568 bt_assert(format(flow6 { tcp flags 0x03/0x0f; }) = "flow6 { tcp flags 0x3/0x3,0x0/0xc; }");
569 bt_assert(format(flow6 { length 0..65535; }) = "flow6 { length 0..65535; }");
570 bt_assert(format(flow6 { dscp = 63; }) = "flow6 { dscp 63; }");
571 bt_assert(format(flow6 { fragment is_fragment || !first_fragment; }) = "flow6 { fragment is_fragment || !first_fragment; }");
572 bt_assert(format(flow6 { }) = "flow6 { }");
575 bt_test_suite(t_flowspec, "Testing flowspec routes");
585 function mkpath(int a; int b)
587 return [= a b 3 2 1 =];
598 bt_assert(pm1 = pm2);
599 bt_assert(format(pm2) = "[= 4 3 2 1 =]");
601 bt_assert(+empty+ = +empty+);
602 bt_assert(10 !~ +empty+);
604 p2 = prepend( + empty +, 1 );
605 p2 = prepend( p2, 2 );
606 p2 = prepend( p2, 3 );
607 p2 = prepend( p2, 4 );
609 bt_assert(format(p2) = "(path 4 3 2 1)");
610 bt_assert(p2.len = 4);
614 bt_assert(p2 ~ [2, 10..20]);
615 bt_assert(p2 ~ [4, 10..20]);
618 bt_assert(p2 !~ pm1);
619 bt_assert(p2 !~ pm2);
621 bt_assert(p2 !~ [8, ten..(2*ten)]);
622 bt_assert(p2 ~ / ? 4 3 2 1 /);
623 bt_assert(p2 ~ [= * 4 3 * 1 =]);
624 bt_assert(p2 ~ [= (3+2) (2*2) 3 2 1 =]);
625 bt_assert(p2 ~ mkpath(5, 4));
627 bt_assert(p2.len = 5);
628 bt_assert(p2.first = 5);
629 bt_assert(p2.last = 1);
631 bt_assert(p2.len = 5);
632 bt_assert(delete(p2, 3) = prepend(prepend(prepend(prepend(+empty+, 1), 2), 4), 5));
633 bt_assert(filter(p2, [1..3]) = prepend(prepend(prepend(+empty+, 1), 2), 3));
635 pm1 = [= 1 2 * 3 4 5 =];
636 p2 = prepend( + empty +, 5 );
637 p2 = prepend( p2, 4 );
638 p2 = prepend( p2, 3 );
639 p2 = prepend( p2, 3 );
640 p2 = prepend( p2, 2 );
641 p2 = prepend( p2, 1 );
644 bt_assert(delete(p2, 3) = prepend(prepend(prepend(prepend(+empty+, 5), 4), 2), 1));
645 bt_assert(delete(p2, [4..5]) = prepend(prepend(prepend(prepend(+empty+, 3), 3), 2), 1));
648 bt_test_suite(t_path, "Testing paths");
654 * Testing Community List
655 * ----------------------
666 bt_assert(l !~ [(*,*)]);
667 bt_assert((l ~ [(*,*)]) != (l !~ [(*,*)]));
669 bt_assert(-empty- = -empty-);
671 l = add( l, (one,2) );
672 bt_assert(l ~ [(*,*)]);
673 l = add( l, (2,one+2) );
674 bt_assert(format(l) = "(clist (1,2) (2,3))");
676 bt_assert((2,3) ~ l);
677 bt_assert(l ~ [(1,*)]);
678 bt_assert(l ~ [p23]);
679 bt_assert(l ~ [(2,2..3)]);
680 bt_assert(l ~ [(1,1..2)]);
681 bt_assert(l ~ [(1,1)..(1,2)]);
686 l = add(l, (one,one));
687 l = delete(l, [(5,1),(6,one),(one,1)]);
688 l = delete(l, [(5,one),(6,one)]);
689 l = filter(l, [(1,*)]);
690 bt_assert(l = add(-empty-, (1,2)));
692 bt_assert((2,3) !~ l);
693 bt_assert(l !~ [(2,*)]);
694 bt_assert(l !~ [(one,3..6)]);
695 bt_assert(l ~ [(*,*)]);
698 l = add(l, (one+one+one,one+one));
702 l2 = filter(l, [(3,*)]);
703 l = delete(l, [(3,2..4)]);
704 bt_assert(l = add(add(add(-empty-, (1,2)), (3,1)), (3,5)));
705 bt_assert(l.len = 3);
709 bt_assert(l = add(add(add(add(add(-empty-, (1,2)), (3,1)), (3,5)), (3,2)), (4,5)));
711 bt_assert(l.len = 5);
712 bt_assert(l ~ [(*,2)]);
713 bt_assert(l ~ [(*,5)]);
714 bt_assert(l ~ [(*, one)]);
715 bt_assert(l !~ [(*,3)]);
716 bt_assert(l !~ [(*,(one+6))]);
717 bt_assert(l !~ [(*, (one+one+one))]);
719 l = delete(l, [(*,(one+onef(3)))]);
720 l = delete(l, [(*,(4+one))]);
721 bt_assert(l = add(-empty-, (3,1)));
723 l = delete(l, [(*,(onef(5)))]);
724 bt_assert(l = -empty-);
727 l = filter(l2, [(3,1..4)]);
728 l2 = filter(l2, [(3,3..6)]);
731 bt_assert(l = add(add(add(add(-empty-, (3,1)), (3,2)), (3,3)), (3,4)));
732 bt_assert(format(l) = "(clist (3,1) (3,2) (3,3) (3,4))");
735 bt_assert(l2 = add(add(add(add(-empty-, (3,3)), (3,4)), (3,5)), (3,6)));
736 bt_assert(format(l2) = "(clist (3,3) (3,4) (3,5) (3,6))");
740 bt_assert(r = add(add(add(add(add(add(-empty-, (3,1)), (3,2)), (3,3)), (3,4)), (3,5)), (3,6)));
741 bt_assert(format(r) = "(clist (3,1) (3,2) (3,3) (3,4) (3,5) (3,6))");
745 bt_assert(r = add(add(-empty-, (3,3)), (3,4)));
746 bt_assert(format(r) = "(clist (3,3) (3,4))");
750 bt_assert(r = add(add(-empty-, (3,1)), (3,2)));
751 bt_assert(format(r) = "(clist (3,1) (3,2))");
754 r = filter(l, [(3,1), (*,2)]);
755 bt_assert(r = add(add(-empty-, (3,1)), (3,2)));
756 bt_assert(format(r) = "(clist (3,1) (3,2))");
759 bt_test_suite(t_clist, "Testing lists of communities");
765 * Testing Extended Communities
766 * ----------------------------
772 cc = (rt, 12345, 200000);
773 bt_assert(format(cc) = "(rt, 12345, 200000)");
775 bt_assert(cc = (rt, 12345, 200000));
776 bt_assert(cc < (rt, 12345, 200010));
777 bt_assert(cc != (rt, 12346, 200000));
778 bt_assert(cc != (ro, 12345, 200000));
779 bt_assert(!(cc > (rt, 12345, 200010)));
781 bt_assert(format((ro, 100000, 20000)) = "(ro, 100000, 20000)");
784 bt_test_suite(t_ec, "Testing extended communities");
790 * Testing Extended Community List
791 * -------------------------------
800 el = add(el, (rt, 10, 20));
801 el = add(el, (ro, 10.20.30.40, 100));
802 el = add(el, (ro, 11.21.31.41.mask(16), 200));
804 bt_assert(--empty-- = --empty--);
805 bt_assert(((rt, 10, 20)) !~ --empty--);
807 bt_assert(format(el) = "(eclist (rt, 10, 20) (ro, 10.20.30.40, 100) (ro, 11.21.0.0, 200))");
808 bt_assert(el.len = 3);
809 el = delete(el, (rt, 10, 20));
810 el = delete(el, (rt, 10, 30));
811 bt_assert(el = add(add(--empty--, (ro, 10.20.30.40, 100)), (ro, 11.21.0.0, 200)));
812 el = add(el, (unknown 2, ten, 1));
813 el = add(el, (unknown 5, ten, 1));
814 el = add(el, (rt, ten, one+one));
815 el = add(el, (rt, 10, 3));
816 el = add(el, (rt, 10, 4));
817 el = add(el, (rt, 10, 5));
818 el = add(el, (generic, 0x2000a, 3*ten));
819 el = delete(el, [(rt, 10, 2..ten)]);
820 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)));
822 el = filter(el, [(rt, 10, *)]);
823 bt_assert(el = add(add(--empty--, (rt, 10, 1)), (rt, 10, 30)));
824 bt_assert((rt, 10, 1) ~ el);
825 bt_assert(el ~ [(rt, 10, ten..40)]);
826 bt_assert((rt, 10, 20) !~ el);
827 bt_assert((ro, 10.20.30.40, 100) !~ el);
828 bt_assert(el !~ [(rt, 10, 35..40)]);
829 bt_assert(el !~ [(ro, 10, *)]);
831 el = add(el, (rt, 10, 40));
832 el2 = filter(el, [(rt, 10, 20..40)] );
833 el2 = add(el2, (rt, 10, 50));
836 bt_assert(el = add(add(add(--empty--, (rt, 10, 1)), (rt, 10, 30)), (rt, 10, 40)));
837 bt_assert(format(el) = "(eclist (rt, 10, 1) (rt, 10, 30) (rt, 10, 40))");
839 # eclist B (30,40,50)
840 bt_assert(el2 = add(add(add(--empty--, (rt, 10, 30)), (rt, 10, 40)), (rt, 10, 50)));
841 bt_assert(format(el2) = "(eclist (rt, 10, 30) (rt, 10, 40) (rt, 10, 50))");
845 bt_assert(r = add(add(add(add(--empty--, (rt, 10, 30)), (rt, 10, 40)), (rt, 10, 50)), (rt, 10, 1)));
846 bt_assert(format(r) = "(eclist (rt, 10, 30) (rt, 10, 40) (rt, 10, 50) (rt, 10, 1))");
850 bt_assert(r = add(add(--empty--, (rt, 10, 30)), (rt, 10, 40)));
851 bt_assert(format(r) = "(eclist (rt, 10, 30) (rt, 10, 40))");
855 bt_assert(r = add(--empty--, (rt, 10, 1)));
856 bt_assert(format(r) = "(eclist (rt, 10, 1))");
859 r = filter(el, [(rt, 10, 1), (rt, 10, 25..30), (ro, 10, 40)]);
860 bt_assert(r = add(add(--empty--, (rt, 10, 1)), (rt, 10, 30)));
861 bt_assert(format(r) = "(eclist (rt, 10, 1) (rt, 10, 30))");
864 bt_test_suite(t_eclist, "Testing lists of extended communities");
870 * Testing sets of Extended Communities
871 * ------------------------------------
874 define ecs2 = [(rt, ten, (one+onef(0))*10), (ro, 100000, 100..200), (rt, 12345, *)];
879 ecs = [(rt, ten, (one+onef(0))*10), (ro, 100000, 100..200), (rt, 12345, *)];
880 bt_assert(format(ecs) = "[(rt, 10, 20), (rt, 12345, 0)..(rt, 12345, 4294967295), (ro, 100000, 100)..(ro, 100000, 200)]");
881 bt_assert(format(ecs2) = "[(rt, 10, 20), (rt, 12345, 0)..(rt, 12345, 4294967295), (ro, 100000, 100)..(ro, 100000, 200)]");
883 bt_assert((rt, 10, 20) ~ ecs);
884 bt_assert((ro, 100000, 100) ~ ecs);
885 bt_assert((ro, 100000, 128) ~ ecs);
886 bt_assert((ro, 100000, 200) ~ ecs);
887 bt_assert((rt, 12345, 0) ~ ecs);
888 bt_assert((rt, 12345, 200000) ~ ecs);
889 bt_assert((rt, 12345, 4000000) ~ ecs);
890 bt_assert((ro, 10, 20) !~ ecs);
891 bt_assert((rt, 10, 21) !~ ecs);
892 bt_assert((ro, 100000, 99) !~ ecs);
893 bt_assert((ro, 12345, 10) !~ ecs);
894 bt_assert((rt, 12346, 0) !~ ecs);
895 bt_assert((ro, 0.1.134.160, 150) !~ ecs);
898 bt_test_suite(t_ec_set, "Testing sets of extended communities");
904 * Testing Large Communities
905 * -------------------------
908 function mktrip(int a)
910 return (a, 2*a, 3*a);
918 bt_assert(---empty--- = ---empty---);
919 bt_assert((10, 20, 30) !~ ---empty---);
922 ll = add(ll, (ten, 20, 30));
923 ll = add(ll, (1000, 2000, 3000));
924 ll = add(ll, mktrip(100000));
925 bt_assert(format(ll) = "(lclist (10, 20, 30) (1000, 2000, 3000) (100000, 200000, 300000))");
926 bt_assert(ll.len = 3);
927 bt_assert(ll = add(add(add(---empty---, (10, 20, 30)), (1000, 2000, 3000)), (100000, 200000, 300000)));
929 bt_assert(mktrip(1000) ~ ll);
930 bt_assert(mktrip(100) !~ ll);
933 ll = add(ll, (10, 10, 10));
934 ll = add(ll, (20, 20, 20));
935 ll = add(ll, (30, 30, 30));
938 ll2 = add(ll2, (20, 20, 20));
939 ll2 = add(ll2, (30, 30, 30));
940 ll2 = add(ll2, (40, 40, 40));
942 # lclist A (10, 20, 30)
943 bt_assert(format(ll) = "(lclist (10, 10, 10) (20, 20, 20) (30, 30, 30))");
945 # lclist B (20, 30, 40)
946 bt_assert(format(ll2) = "(lclist (20, 20, 20) (30, 30, 30) (40, 40, 40))");
950 bt_assert(r = add(add(add(add(---empty---, (10,10,10)), (20,20,20)), (30,30,30)), (40,40,40)));
951 bt_assert(format(r) = "(lclist (10, 10, 10) (20, 20, 20) (30, 30, 30) (40, 40, 40))");
955 bt_assert(r = add(add(---empty---, (20, 20, 20)), (30, 30, 30)));
956 bt_assert(format(r) = "(lclist (20, 20, 20) (30, 30, 30))");
960 bt_assert(r = add(---empty---, (10, 10, 10)));
961 bt_assert(format(r) = "(lclist (10, 10, 10))");
964 r = filter(ll, [(5..15, *, *), (20, 15..25, *)]);
965 bt_assert(r = add(add(---empty---, (10, 10, 10)), (20, 20, 20)));
966 bt_assert(format(r) = "(lclist (10, 10, 10) (20, 20, 20))");
969 bt_test_suite(t_lclist, "Testing lists of large communities");
975 * Testing sets of Large Communities
976 * ---------------------------------
979 function t_lclist_set()
984 ll = add(ll, (10, 20, 30));
985 ll = add(ll, (1000, 2000, 3000));
986 ll = add(ll, mktrip(100000));
988 bt_assert(ll ~ [(5,10,15), (10,20,30)]);
989 bt_assert(ll ~ [(10,15..25,*)]);
990 bt_assert(ll ~ [(ten, *, *)]);
992 bt_assert(ll !~ [(5,10,15), (10,21,30)]);
993 bt_assert(ll !~ [(10,21..25,*)]);
994 bt_assert(ll !~ [(11, *, *)]);
996 lls = [(10, 10, 10), (20, 20, 15..25), (30, 30, *), (40, 35..45, *), (50, *, *), (55..65, *, *)];
997 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)]");
998 bt_assert((10, 10, 10) ~ lls);
999 bt_assert((20, 20, 25) ~ lls);
1000 bt_assert((20, 20, 26) !~ lls);
1001 bt_assert((30, 30, 0) ~ lls);
1002 bt_assert((40, 35, 40) ~ lls);
1003 bt_assert((40, 34, 40) !~ lls);
1004 bt_assert((50, 0, 0) ~ lls);
1005 bt_assert((60, 60, 60) ~ lls);
1006 bt_assert((70, 60, 60) !~ lls);
1009 bt_test_suite(t_lclist_set, "Testing sets of large communities");
1015 * Testing defined() function
1016 * --------------------------
1019 function test_undef(int a)
1024 bt_assert(defined(b));
1027 bt_assert(!defined(b));
1038 bt_assert(defined(1));
1039 bt_assert(defined(1.2.3.4));
1042 bt_test_suite(t_define, "Testing defined() function");
1048 * Testing calling functions
1049 * -------------------------
1052 function callme(int arg1; int arg2)
1057 else: return arg1 * arg2;
1068 function t_call_function()
1070 bt_assert(fifteen() = 15);
1072 bt_assert(callme(1, 2) = 42);
1073 bt_assert(callme(42, 2) = 42);
1075 bt_assert(callme(2, 2) = 4);
1076 bt_assert(callme(3, 2) = 6);
1077 bt_assert(callme(4, 4) = 16);
1078 bt_assert(callme(7, 2) = 14);
1081 bt_test_suite(t_call_function, "Testing calling functions");
1087 * Test including another config file
1088 * ----------------------------------
1091 function t_include()
1095 include "test.conf.inc";
1099 bt_test_suite(t_include, "Testing including another config file");
1105 * Test if-else statement
1106 * ----------------------
1109 function t_if_else()
1123 bt_test_suite(t_if_else, "Testing if-else statement");
1129 * Unused functions -- testing only parsing
1130 * ----------------------------------------
1135 if source ~ [ RTS_BGP, RTS_STATIC ] then {
1136 # ospf_metric1 = 65535;
1137 # ospf_metric2 = 1000;
1138 ospf_tag = 0x12345678;
1146 if source ~ [ RTS_BGP, RTS_STATIC ] then {
1147 # ospf_metric1 = 65535;
1148 # ospf_metric2 = 1000;
1149 ospf_tag = 0x12345678;
1158 print "Heya, filtering route to ", net.ip, " prefixlen ", net.len, " source ", source;
1159 print "This route was from ", from;
1162 if rip_metric > 15 then {
1163 reject "RIP Metric is more than infinity";
1168 accept "ok I take that";
1173 if net ~ [ 10.0.0.0/8{16,24}, 2000::/3{16,96} ] then {
1184 roa4 { table r4; import filter roa_filter; };
1185 route 10.110.0.0/16 max 16 as 1000 blackhole;
1186 route 10.120.0.0/16 max 24 as 1000 blackhole ;
1187 route 10.130.0.0/16 max 24 as 2000 blackhole;
1188 route 10.130.128.0/18 max 24 as 3000 blackhole;
1193 roa6 { table r6; import filter roa_filter; };
1194 route 2001:0db8:85a3:8a2e::/64 max 96 as 1000 blackhole;
1197 function test_roa_check()
1200 # cannot be tested in __startup(), sorry
1201 bt_assert(roa_check(r4, 10.10.0.0/16, 1000) = ROA_UNKNOWN);
1202 bt_assert(roa_check(r4, 10.0.0.0/8, 1000) = ROA_UNKNOWN);
1203 bt_assert(roa_check(r4, 10.110.0.0/16, 1000) = ROA_VALID);
1204 bt_assert(roa_check(r4, 10.110.0.0/16, 2000) = ROA_INVALID);
1205 bt_assert(roa_check(r4, 10.110.32.0/20, 1000) = ROA_INVALID);
1206 bt_assert(roa_check(r4, 10.120.32.0/20, 1000) = ROA_VALID);
1207 bt_assert(roa_check(r4, 10.120.32.0/20, 2000) = ROA_INVALID);
1208 bt_assert(roa_check(r4, 10.120.32.32/28, 1000) = ROA_INVALID);
1209 bt_assert(roa_check(r4, 10.130.130.0/24, 1000) = ROA_INVALID);
1210 bt_assert(roa_check(r4, 10.130.130.0/24, 2000) = ROA_VALID);
1211 bt_assert(roa_check(r4, 10.130.30.0/24, 3000) = ROA_INVALID);
1212 bt_assert(roa_check(r4, 10.130.130.0/24, 3000) = ROA_VALID);
1214 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e:1234::/80, 1000) = ROA_VALID);
1215 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e:1234::/97, 1000) = ROA_INVALID);
1216 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e::/64, 1000) = ROA_VALID);
1217 bt_assert(roa_check(r6, 2001:0db8:85a3::/48, 1000) = ROA_UNKNOWN);
1219 bt_assert(roa_check(r4, 10.10.0.0/16, 1000) = ROA_UNKNOWN);
1220 bt_assert(roa_check(r4, 10.0.0.0/8, 1000) = ROA_UNKNOWN);
1221 bt_assert(roa_check(r4, 10.110.0.0/16, 1000) = ROA_VALID);
1222 bt_assert(roa_check(r4, 10.110.0.0/16, 2000) = ROA_INVALID);
1223 bt_assert(roa_check(r4, 10.110.32.0/20, 1000) = ROA_INVALID);
1224 bt_assert(roa_check(r4, 10.120.32.0/20, 1000) = ROA_VALID);
1226 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e:1234::/80, 1000) = ROA_VALID);
1227 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e:1234::/97, 1000) = ROA_INVALID);
1228 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e::/64, 1000) = ROA_VALID);
1229 bt_assert(roa_check(r6, 2001:0db8:85a3::/48, 1000) = ROA_UNKNOWN);
1231 bt_assert(roa_check(r4, 2001:0db8:85a3:8a2e:1234::/97, 1000) = ROA_UNKNOWN);
1232 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e:1234::/97, 1000) = ROA_INVALID);
1234 bt_assert(roa_check(r4, 2001:0db8:85a3:8a2e:1234::/80, 1000) = ROA_UNKNOWN);
1235 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e:1234::/80, 1000) = ROA_VALID);
1236 bt_assert(roa_check(r4, 2001:0db8:85a3::/48, 1000) = ROA_UNKNOWN);
1237 bt_assert(roa_check(r6, 2001:0db8:85a3::/48, 1000) = ROA_UNKNOWN);
1239 bt_assert(10.130.130.0/24 ~ 0.0.0.0/0);
1240 bt_assert(2001:0db8:85a3:8a2e::/64 ~ ::/0);
1241 bt_assert(10.130.130.0/24 !~ ::/0);
1242 bt_assert(2001:0db8:85a3:8a2e::/64 !~ 0.0.0.0/0);
1244 pfx = 12.13.0.0/16 max 24 as 1234;
1245 bt_assert(pfx.len = 16);
1246 bt_assert(pfx.maxlen = 24);
1247 bt_assert(pfx.asn = 1234);
1249 pfx = 1000::/8 max 32 as 1234;
1250 bt_assert(pfx.len = 8);
1251 bt_assert(pfx.maxlen = 32);
1252 bt_assert(pfx.asn = 1234);
1255 bt_test_suite(test_roa_check, "Testing ROA");
1258 * Testing Mixed Net Types
1259 * -----------------------
1262 function t_mixed_prefix()
1266 pxs = [ 98.45.0.0/16, 128.128.0.0/12+, 2200::/42-, ::ffff:d000:0/100{98,102}];
1267 bt_assert(format(pxs) = "[::/0, ::/2{c000::}, 98.45.0.0/112{::0.1.0.0}, 128.128.0.0/108{::0.31.255.255}, 208.0.0.0/100{::124.0.0.0}, 2200::/42{ffff:ffff:ffc0::}]");
1268 bt_assert(::fe00:0:0/88 !~ pxs);
1269 bt_assert(::fffe:0:0/95 !~ pxs);
1270 bt_assert(::ffff:d800:0/101 ~ pxs);
1271 bt_assert(216.0.0.0/5 ~ pxs);
1272 bt_assert(212.0.0.0/6 ~ pxs);
1273 bt_assert(212.0.0.0/7 !~ pxs);
1274 bt_assert(::ffff:8080:8080/121 ~ pxs);
1275 bt_assert(::/0 ~ pxs);
1276 bt_assert(0.0.0.0/0 !~ pxs);
1277 bt_assert(128.135.64.17/32 ~ pxs);
1279 # pxt = [ 0:1:2 10.1.10.0/24, 0:5:10000 10.1.10.0/24 ];
1282 bt_assert(format(NET_IP4) = "(enum 36)1"); ## if (net.type = NET_IP4) ...
1283 bt_assert(format(NET_VPN6) = "(enum 36)4");
1284 bt_assert(format(0:1:2) = "0:1:2");
1287 bt_test_suite(t_mixed_prefix, "Testing mixed net types");
1292 bt_assert(format(net) = "0:1:2 10.1.10.0/24");
1293 bt_assert(net.type = NET_VPN4);
1294 bt_assert(net.type != NET_IP4);
1295 bt_assert(net.type != NET_IP6);
1296 bt_assert(net.rd = 0:1:2);
1299 NET_IP4: print "IPV4";
1300 NET_IP6: print "IPV6";
1311 vpn4 { table v4; import filter vpn_filter; };
1312 route 0:1:2 10.1.10.0/24 unreachable;