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 =];
596 bt_assert(format(pm1) = "[= 4 3 2 1 =]");
598 bt_assert(+empty+ = +empty+);
599 bt_assert(10 !~ +empty+);
601 p2 = prepend( + empty +, 1 );
602 p2 = prepend( p2, 2 );
603 p2 = prepend( p2, 3 );
604 p2 = prepend( p2, 4 );
606 bt_assert(format(p2) = "(path 4 3 2 1)");
607 bt_assert(p2.len = 4);
610 bt_assert(p2 ~ [2, 10..20]);
611 bt_assert(p2 ~ [4, 10..20]);
614 bt_assert(p2 !~ pm1);
616 bt_assert(p2 !~ [8, ten..(2*ten)]);
617 bt_assert(p2 ~ [= * 4 3 * 1 =]);
618 bt_assert(p2 ~ [= (3+2) (2*2) 3 2 1 =]);
619 bt_assert(p2 ~ mkpath(5, 4));
621 bt_assert(p2.len = 5);
622 bt_assert(p2.first = 5);
623 bt_assert(p2.last = 1);
625 bt_assert(p2.len = 5);
626 bt_assert(delete(p2, 3) = prepend(prepend(prepend(prepend(+empty+, 1), 2), 4), 5));
627 bt_assert(filter(p2, [1..3]) = prepend(prepend(prepend(+empty+, 1), 2), 3));
629 pm1 = [= 1 2 * 3 4 5 =];
630 p2 = prepend( + empty +, 5 );
631 p2 = prepend( p2, 4 );
632 p2 = prepend( p2, 3 );
633 p2 = prepend( p2, 3 );
634 p2 = prepend( p2, 2 );
635 p2 = prepend( p2, 1 );
638 bt_assert(delete(p2, 3) = prepend(prepend(prepend(prepend(+empty+, 5), 4), 2), 1));
639 bt_assert(delete(p2, [4..5]) = prepend(prepend(prepend(prepend(+empty+, 3), 3), 2), 1));
642 bt_test_suite(t_path, "Testing paths");
648 * Testing Community List
649 * ----------------------
660 bt_assert(l !~ [(*,*)]);
661 bt_assert((l ~ [(*,*)]) != (l !~ [(*,*)]));
663 bt_assert(-empty- = -empty-);
665 l = add( l, (one,2) );
666 bt_assert(l ~ [(*,*)]);
667 l = add( l, (2,one+2) );
668 bt_assert(format(l) = "(clist (1,2) (2,3))");
670 bt_assert((2,3) ~ l);
671 bt_assert(l ~ [(1,*)]);
672 bt_assert(l ~ [p23]);
673 bt_assert(l ~ [(2,2..3)]);
674 bt_assert(l ~ [(1,1..2)]);
675 bt_assert(l ~ [(1,1)..(1,2)]);
680 l = add(l, (one,one));
681 l = delete(l, [(5,1),(6,one),(one,1)]);
682 l = delete(l, [(5,one),(6,one)]);
683 l = filter(l, [(1,*)]);
684 bt_assert(l = add(-empty-, (1,2)));
686 bt_assert((2,3) !~ l);
687 bt_assert(l !~ [(2,*)]);
688 bt_assert(l !~ [(one,3..6)]);
689 bt_assert(l ~ [(*,*)]);
692 l = add(l, (one+one+one,one+one));
696 l2 = filter(l, [(3,*)]);
697 l = delete(l, [(3,2..4)]);
698 bt_assert(l = add(add(add(-empty-, (1,2)), (3,1)), (3,5)));
699 bt_assert(l.len = 3);
703 bt_assert(l = add(add(add(add(add(-empty-, (1,2)), (3,1)), (3,5)), (3,2)), (4,5)));
705 bt_assert(l.len = 5);
706 bt_assert(l ~ [(*,2)]);
707 bt_assert(l ~ [(*,5)]);
708 bt_assert(l ~ [(*, one)]);
709 bt_assert(l !~ [(*,3)]);
710 bt_assert(l !~ [(*,(one+6))]);
711 bt_assert(l !~ [(*, (one+one+one))]);
713 l = delete(l, [(*,(one+onef(3)))]);
714 l = delete(l, [(*,(4+one))]);
715 bt_assert(l = add(-empty-, (3,1)));
717 l = delete(l, [(*,(onef(5)))]);
718 bt_assert(l = -empty-);
721 l = filter(l2, [(3,1..4)]);
722 l2 = filter(l2, [(3,3..6)]);
725 bt_assert(l = add(add(add(add(-empty-, (3,1)), (3,2)), (3,3)), (3,4)));
726 bt_assert(format(l) = "(clist (3,1) (3,2) (3,3) (3,4))");
729 bt_assert(l2 = add(add(add(add(-empty-, (3,3)), (3,4)), (3,5)), (3,6)));
730 bt_assert(format(l2) = "(clist (3,3) (3,4) (3,5) (3,6))");
734 bt_assert(r = add(add(add(add(add(add(-empty-, (3,1)), (3,2)), (3,3)), (3,4)), (3,5)), (3,6)));
735 bt_assert(format(r) = "(clist (3,1) (3,2) (3,3) (3,4) (3,5) (3,6))");
739 bt_assert(r = add(add(-empty-, (3,3)), (3,4)));
740 bt_assert(format(r) = "(clist (3,3) (3,4))");
744 bt_assert(r = add(add(-empty-, (3,1)), (3,2)));
745 bt_assert(format(r) = "(clist (3,1) (3,2))");
748 r = filter(l, [(3,1), (*,2)]);
749 bt_assert(r = add(add(-empty-, (3,1)), (3,2)));
750 bt_assert(format(r) = "(clist (3,1) (3,2))");
753 bt_test_suite(t_clist, "Testing lists of communities");
759 * Testing Extended Communities
760 * ----------------------------
766 cc = (rt, 12345, 200000);
767 bt_assert(format(cc) = "(rt, 12345, 200000)");
769 bt_assert(cc = (rt, 12345, 200000));
770 bt_assert(cc < (rt, 12345, 200010));
771 bt_assert(cc != (rt, 12346, 200000));
772 bt_assert(cc != (ro, 12345, 200000));
773 bt_assert(!(cc > (rt, 12345, 200010)));
775 bt_assert(format((ro, 100000, 20000)) = "(ro, 100000, 20000)");
778 bt_test_suite(t_ec, "Testing extended communities");
784 * Testing Extended Community List
785 * -------------------------------
794 el = add(el, (rt, 10, 20));
795 el = add(el, (ro, 10.20.30.40, 100));
796 el = add(el, (ro, 11.21.31.41.mask(16), 200));
798 bt_assert(--empty-- = --empty--);
799 bt_assert(((rt, 10, 20)) !~ --empty--);
801 bt_assert(format(el) = "(eclist (rt, 10, 20) (ro, 10.20.30.40, 100) (ro, 11.21.0.0, 200))");
802 bt_assert(el.len = 3);
803 el = delete(el, (rt, 10, 20));
804 el = delete(el, (rt, 10, 30));
805 bt_assert(el = add(add(--empty--, (ro, 10.20.30.40, 100)), (ro, 11.21.0.0, 200)));
806 el = add(el, (unknown 2, ten, 1));
807 el = add(el, (unknown 5, ten, 1));
808 el = add(el, (rt, ten, one+one));
809 el = add(el, (rt, 10, 3));
810 el = add(el, (rt, 10, 4));
811 el = add(el, (rt, 10, 5));
812 el = add(el, (generic, 0x2000a, 3*ten));
813 el = delete(el, [(rt, 10, 2..ten)]);
814 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)));
816 el = filter(el, [(rt, 10, *)]);
817 bt_assert(el = add(add(--empty--, (rt, 10, 1)), (rt, 10, 30)));
818 bt_assert((rt, 10, 1) ~ el);
819 bt_assert(el ~ [(rt, 10, ten..40)]);
820 bt_assert((rt, 10, 20) !~ el);
821 bt_assert((ro, 10.20.30.40, 100) !~ el);
822 bt_assert(el !~ [(rt, 10, 35..40)]);
823 bt_assert(el !~ [(ro, 10, *)]);
825 el = add(el, (rt, 10, 40));
826 el2 = filter(el, [(rt, 10, 20..40)] );
827 el2 = add(el2, (rt, 10, 50));
830 bt_assert(el = add(add(add(--empty--, (rt, 10, 1)), (rt, 10, 30)), (rt, 10, 40)));
831 bt_assert(format(el) = "(eclist (rt, 10, 1) (rt, 10, 30) (rt, 10, 40))");
833 # eclist B (30,40,50)
834 bt_assert(el2 = add(add(add(--empty--, (rt, 10, 30)), (rt, 10, 40)), (rt, 10, 50)));
835 bt_assert(format(el2) = "(eclist (rt, 10, 30) (rt, 10, 40) (rt, 10, 50))");
839 bt_assert(r = add(add(add(add(--empty--, (rt, 10, 30)), (rt, 10, 40)), (rt, 10, 50)), (rt, 10, 1)));
840 bt_assert(format(r) = "(eclist (rt, 10, 30) (rt, 10, 40) (rt, 10, 50) (rt, 10, 1))");
844 bt_assert(r = add(add(--empty--, (rt, 10, 30)), (rt, 10, 40)));
845 bt_assert(format(r) = "(eclist (rt, 10, 30) (rt, 10, 40))");
849 bt_assert(r = add(--empty--, (rt, 10, 1)));
850 bt_assert(format(r) = "(eclist (rt, 10, 1))");
853 r = filter(el, [(rt, 10, 1), (rt, 10, 25..30), (ro, 10, 40)]);
854 bt_assert(r = add(add(--empty--, (rt, 10, 1)), (rt, 10, 30)));
855 bt_assert(format(r) = "(eclist (rt, 10, 1) (rt, 10, 30))");
858 bt_test_suite(t_eclist, "Testing lists of extended communities");
864 * Testing sets of Extended Communities
865 * ------------------------------------
868 define ecs2 = [(rt, ten, (one+onef(0))*10), (ro, 100000, 100..200), (rt, 12345, *)];
873 ecs = [(rt, ten, (one+onef(0))*10), (ro, 100000, 100..200), (rt, 12345, *)];
874 bt_assert(format(ecs) = "[(rt, 10, 20), (rt, 12345, 0)..(rt, 12345, 4294967295), (ro, 100000, 100)..(ro, 100000, 200)]");
875 bt_assert(format(ecs2) = "[(rt, 10, 20), (rt, 12345, 0)..(rt, 12345, 4294967295), (ro, 100000, 100)..(ro, 100000, 200)]");
877 bt_assert((rt, 10, 20) ~ ecs);
878 bt_assert((ro, 100000, 100) ~ ecs);
879 bt_assert((ro, 100000, 128) ~ ecs);
880 bt_assert((ro, 100000, 200) ~ ecs);
881 bt_assert((rt, 12345, 0) ~ ecs);
882 bt_assert((rt, 12345, 200000) ~ ecs);
883 bt_assert((rt, 12345, 4000000) ~ ecs);
884 bt_assert((ro, 10, 20) !~ ecs);
885 bt_assert((rt, 10, 21) !~ ecs);
886 bt_assert((ro, 100000, 99) !~ ecs);
887 bt_assert((ro, 12345, 10) !~ ecs);
888 bt_assert((rt, 12346, 0) !~ ecs);
889 bt_assert((ro, 0.1.134.160, 150) !~ ecs);
892 bt_test_suite(t_ec_set, "Testing sets of extended communities");
898 * Testing Large Communities
899 * -------------------------
902 function mktrip(int a)
904 return (a, 2*a, 3*a);
912 bt_assert(---empty--- = ---empty---);
913 bt_assert((10, 20, 30) !~ ---empty---);
916 ll = add(ll, (ten, 20, 30));
917 ll = add(ll, (1000, 2000, 3000));
918 ll = add(ll, mktrip(100000));
919 bt_assert(format(ll) = "(lclist (10, 20, 30) (1000, 2000, 3000) (100000, 200000, 300000))");
920 bt_assert(ll.len = 3);
921 bt_assert(ll = add(add(add(---empty---, (10, 20, 30)), (1000, 2000, 3000)), (100000, 200000, 300000)));
923 bt_assert(mktrip(1000) ~ ll);
924 bt_assert(mktrip(100) !~ ll);
927 ll = add(ll, (10, 10, 10));
928 ll = add(ll, (20, 20, 20));
929 ll = add(ll, (30, 30, 30));
932 ll2 = add(ll2, (20, 20, 20));
933 ll2 = add(ll2, (30, 30, 30));
934 ll2 = add(ll2, (40, 40, 40));
936 # lclist A (10, 20, 30)
937 bt_assert(format(ll) = "(lclist (10, 10, 10) (20, 20, 20) (30, 30, 30))");
939 # lclist B (20, 30, 40)
940 bt_assert(format(ll2) = "(lclist (20, 20, 20) (30, 30, 30) (40, 40, 40))");
944 bt_assert(r = add(add(add(add(---empty---, (10,10,10)), (20,20,20)), (30,30,30)), (40,40,40)));
945 bt_assert(format(r) = "(lclist (10, 10, 10) (20, 20, 20) (30, 30, 30) (40, 40, 40))");
949 bt_assert(r = add(add(---empty---, (20, 20, 20)), (30, 30, 30)));
950 bt_assert(format(r) = "(lclist (20, 20, 20) (30, 30, 30))");
954 bt_assert(r = add(---empty---, (10, 10, 10)));
955 bt_assert(format(r) = "(lclist (10, 10, 10))");
958 r = filter(ll, [(5..15, *, *), (20, 15..25, *)]);
959 bt_assert(r = add(add(---empty---, (10, 10, 10)), (20, 20, 20)));
960 bt_assert(format(r) = "(lclist (10, 10, 10) (20, 20, 20))");
963 bt_test_suite(t_lclist, "Testing lists of large communities");
969 * Testing sets of Large Communities
970 * ---------------------------------
973 function t_lclist_set()
978 ll = add(ll, (10, 20, 30));
979 ll = add(ll, (1000, 2000, 3000));
980 ll = add(ll, mktrip(100000));
982 bt_assert(ll ~ [(5,10,15), (10,20,30)]);
983 bt_assert(ll ~ [(10,15..25,*)]);
984 bt_assert(ll ~ [(ten, *, *)]);
986 bt_assert(ll !~ [(5,10,15), (10,21,30)]);
987 bt_assert(ll !~ [(10,21..25,*)]);
988 bt_assert(ll !~ [(11, *, *)]);
990 lls = [(10, 10, 10), (20, 20, 15..25), (30, 30, *), (40, 35..45, *), (50, *, *), (55..65, *, *)];
991 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)]");
992 bt_assert((10, 10, 10) ~ lls);
993 bt_assert((20, 20, 25) ~ lls);
994 bt_assert((20, 20, 26) !~ lls);
995 bt_assert((30, 30, 0) ~ lls);
996 bt_assert((40, 35, 40) ~ lls);
997 bt_assert((40, 34, 40) !~ lls);
998 bt_assert((50, 0, 0) ~ lls);
999 bt_assert((60, 60, 60) ~ lls);
1000 bt_assert((70, 60, 60) !~ lls);
1003 bt_test_suite(t_lclist_set, "Testing sets of large communities");
1009 * Testing defined() function
1010 * --------------------------
1013 function test_undef(int a)
1018 bt_assert(defined(b));
1021 bt_assert(!defined(b));
1032 bt_assert(defined(1));
1033 bt_assert(defined(1.2.3.4));
1036 bt_test_suite(t_define, "Testing defined() function");
1042 * Testing calling functions
1043 * -------------------------
1046 function callme(int arg1; int arg2)
1051 else: return arg1 * arg2;
1062 function t_call_function()
1064 bt_assert(fifteen() = 15);
1066 bt_assert(callme(1, 2) = 42);
1067 bt_assert(callme(42, 2) = 42);
1069 bt_assert(callme(2, 2) = 4);
1070 bt_assert(callme(3, 2) = 6);
1071 bt_assert(callme(4, 4) = 16);
1072 bt_assert(callme(7, 2) = 14);
1075 bt_test_suite(t_call_function, "Testing calling functions");
1081 * Test including another config file
1082 * ----------------------------------
1085 function t_include()
1089 include "test.conf.inc";
1093 bt_test_suite(t_include, "Testing including another config file");
1099 * Test if-else statement
1100 * ----------------------
1103 function t_if_else()
1117 bt_test_suite(t_if_else, "Testing if-else statement");
1123 * Unused functions -- testing only parsing
1124 * ----------------------------------------
1129 if source ~ [ RTS_BGP, RTS_STATIC ] then {
1130 # ospf_metric1 = 65535;
1131 # ospf_metric2 = 1000;
1132 ospf_tag = 0x12345678;
1140 if source ~ [ RTS_BGP, RTS_STATIC ] then {
1141 # ospf_metric1 = 65535;
1142 # ospf_metric2 = 1000;
1143 ospf_tag = 0x12345678;
1152 print "Heya, filtering route to ", net.ip, " prefixlen ", net.len, " source ", source;
1153 print "This route was from ", from;
1156 if rip_metric > 15 then {
1157 reject "RIP Metric is more than infinity";
1162 accept "ok I take that";
1167 if net ~ [ 10.0.0.0/8{16,24}, 2000::/3{16,96} ] then {
1178 roa4 { table r4; import filter roa_filter; };
1179 route 10.110.0.0/16 max 16 as 1000;
1180 route 10.120.0.0/16 max 24 as 1000;
1181 route 10.130.0.0/16 max 24 as 2000;
1182 route 10.130.128.0/18 max 24 as 3000;
1187 roa6 { table r6; import filter roa_filter; };
1188 route 2001:0db8:85a3:8a2e::/64 max 96 as 1000;
1191 function test_roa_check()
1194 # cannot be tested in __startup(), sorry
1195 bt_assert(roa_check(r4, 10.10.0.0/16, 1000) = ROA_UNKNOWN);
1196 bt_assert(roa_check(r4, 10.0.0.0/8, 1000) = ROA_UNKNOWN);
1197 bt_assert(roa_check(r4, 10.110.0.0/16, 1000) = ROA_VALID);
1198 bt_assert(roa_check(r4, 10.110.0.0/16, 2000) = ROA_INVALID);
1199 bt_assert(roa_check(r4, 10.110.32.0/20, 1000) = ROA_INVALID);
1200 bt_assert(roa_check(r4, 10.120.32.0/20, 1000) = ROA_VALID);
1201 bt_assert(roa_check(r4, 10.120.32.0/20, 2000) = ROA_INVALID);
1202 bt_assert(roa_check(r4, 10.120.32.32/28, 1000) = ROA_INVALID);
1203 bt_assert(roa_check(r4, 10.130.130.0/24, 1000) = ROA_INVALID);
1204 bt_assert(roa_check(r4, 10.130.130.0/24, 2000) = ROA_VALID);
1205 bt_assert(roa_check(r4, 10.130.30.0/24, 3000) = ROA_INVALID);
1206 bt_assert(roa_check(r4, 10.130.130.0/24, 3000) = ROA_VALID);
1208 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e:1234::/80, 1000) = ROA_VALID);
1209 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e:1234::/97, 1000) = ROA_INVALID);
1210 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e::/64, 1000) = ROA_VALID);
1211 bt_assert(roa_check(r6, 2001:0db8:85a3::/48, 1000) = ROA_UNKNOWN);
1213 bt_assert(roa_check(r4, 10.10.0.0/16, 1000) = ROA_UNKNOWN);
1214 bt_assert(roa_check(r4, 10.0.0.0/8, 1000) = ROA_UNKNOWN);
1215 bt_assert(roa_check(r4, 10.110.0.0/16, 1000) = ROA_VALID);
1216 bt_assert(roa_check(r4, 10.110.0.0/16, 2000) = ROA_INVALID);
1217 bt_assert(roa_check(r4, 10.110.32.0/20, 1000) = ROA_INVALID);
1218 bt_assert(roa_check(r4, 10.120.32.0/20, 1000) = ROA_VALID);
1220 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e:1234::/80, 1000) = ROA_VALID);
1221 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e:1234::/97, 1000) = ROA_INVALID);
1222 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e::/64, 1000) = ROA_VALID);
1223 bt_assert(roa_check(r6, 2001:0db8:85a3::/48, 1000) = ROA_UNKNOWN);
1225 bt_assert(roa_check(r4, 2001:0db8:85a3:8a2e:1234::/97, 1000) = ROA_UNKNOWN);
1226 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e:1234::/97, 1000) = ROA_INVALID);
1228 bt_assert(roa_check(r4, 2001:0db8:85a3:8a2e:1234::/80, 1000) = ROA_UNKNOWN);
1229 bt_assert(roa_check(r6, 2001:0db8:85a3:8a2e:1234::/80, 1000) = ROA_VALID);
1230 bt_assert(roa_check(r4, 2001:0db8:85a3::/48, 1000) = ROA_UNKNOWN);
1231 bt_assert(roa_check(r6, 2001:0db8:85a3::/48, 1000) = ROA_UNKNOWN);
1233 bt_assert(10.130.130.0/24 ~ 0.0.0.0/0);
1234 bt_assert(2001:0db8:85a3:8a2e::/64 ~ ::/0);
1235 bt_assert(10.130.130.0/24 !~ ::/0);
1236 bt_assert(2001:0db8:85a3:8a2e::/64 !~ 0.0.0.0/0);
1238 pfx = 12.13.0.0/16 max 24 as 1234;
1239 bt_assert(pfx.len = 16);
1240 bt_assert(pfx.maxlen = 24);
1241 bt_assert(pfx.asn = 1234);
1243 pfx = 1000::/8 max 32 as 1234;
1244 bt_assert(pfx.len = 8);
1245 bt_assert(pfx.maxlen = 32);
1246 bt_assert(pfx.asn = 1234);
1249 bt_test_suite(test_roa_check, "Testing ROA");
1252 * Testing Mixed Net Types
1253 * -----------------------
1256 function t_mixed_prefix()
1260 pxs = [ 98.45.0.0/16, 128.128.0.0/12+, 2200::/42-, ::ffff:d000:0/100{98,102}];
1261 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::}]");
1262 bt_assert(::fe00:0:0/88 !~ pxs);
1263 bt_assert(::fffe:0:0/95 !~ pxs);
1264 bt_assert(::ffff:d800:0/101 ~ pxs);
1265 bt_assert(216.0.0.0/5 ~ pxs);
1266 bt_assert(212.0.0.0/6 ~ pxs);
1267 bt_assert(212.0.0.0/7 !~ pxs);
1268 bt_assert(::ffff:8080:8080/121 ~ pxs);
1269 bt_assert(::/0 ~ pxs);
1270 bt_assert(0.0.0.0/0 !~ pxs);
1271 bt_assert(128.135.64.17/32 ~ pxs);
1273 # pxt = [ 0:1:2 10.1.10.0/24, 0:5:10000 10.1.10.0/24 ];
1276 bt_assert(format(NET_IP4) = "(enum 36)1"); ## if (net.type = NET_IP4) ...
1277 bt_assert(format(NET_VPN6) = "(enum 36)4");
1278 bt_assert(format(0:1:2) = "1:2");
1281 bt_test_suite(t_mixed_prefix, "Testing mixed net types");
1286 bt_assert(format(net) = "0:1:2 10.1.10.0/24");
1287 bt_assert(net.type = NET_VPN4);
1288 bt_assert(net.type != NET_IP4);
1289 bt_assert(net.type != NET_IP6);
1290 bt_assert(net.rd = 0:1:2);
1293 NET_IP4: print "IPV4";
1294 NET_IP6: print "IPV6";
1305 vpn4 { table v4; import filter vpn_filter; };
1306 route 0:1:2 10.1.10.0/24 unreachable;