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;
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");
320 p = ::fffe:6:c0c:936d:88c7:35d3;
321 bt_assert(format(p) = "::fffe:6:c0c:936d:88c7:35d3");
324 bt_assert(p.mask(24) = 1234:5600::);
327 bt_test_suite(t_ip, "Testing ip address");
333 * Testing sets of ip address
334 * --------------------------
337 define ip1222 = 1.2.2.2;
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);
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 ]);
356 bt_test_suite(t_ip_set, "Testing sets of ip address");
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]);
374 bt_test_suite(t_enum, "Testing enums");
384 define netdoc = 2001:db8::/32;
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);
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);
403 bt_test_suite(t_prefix, "Testing prefixes");
409 * Testing prefix sets
410 * -------------------
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} ];
416 function test_pxset(prefix set pxs)
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);
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);
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 } ]);
441 bt_assert([ 10.0.0.0/8{ 15 , 17 } ] = [ 10.0.0.0/8{ 15 , 17 } ]);
444 function t_prefix_set()
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);
461 test_pxset([ 10.0.0.0/16{8,12}, 20.0.0.0/16{24,28} ]);
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 } ]);
467 bt_test_suite(t_prefix_set, "Testing prefix sets");
473 * Testing Prefix IPv6
474 * -------------------
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);
490 bt_test_suite(t_prefix6, "Testing prefix IPv6");
496 * Testing prefix IPv6 sets
497 * ------------------------
500 function t_prefix6_set()
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} ]);
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);
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);
546 bt_test_suite(t_prefix6_set, "Testing prefix IPv6 sets");
551 function t_flowspec()
554 p = flow4 { dst 10.0.0.0/8; };
555 bt_assert(p !~ [ 10.0.0.0/8 ] );
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 { }");
572 bt_test_suite(t_flowspec, "Testing flowspec routes");
582 function mkpath(int a; int b)
584 return [= a b 3 2 1 =];
595 bt_assert(pm1 = pm2);
596 bt_assert(format(pm2) = "[= 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);
611 bt_assert(p2 ~ [2, 10..20]);
612 bt_assert(p2 ~ [4, 10..20]);
615 bt_assert(p2 !~ pm1);
616 bt_assert(p2 !~ pm2);
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));
624 bt_assert(p2.len = 5);
625 bt_assert(p2.first = 5);
626 bt_assert(p2.last = 1);
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));
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 );
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));
645 bt_test_suite(t_path, "Testing paths");
651 * Testing Community List
652 * ----------------------
663 bt_assert(l !~ [(*,*)]);
664 bt_assert((l ~ [(*,*)]) != (l !~ [(*,*)]));
666 bt_assert(-empty- = -empty-);
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))");
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)]);
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)));
689 bt_assert((2,3) !~ l);
690 bt_assert(l !~ [(2,*)]);
691 bt_assert(l !~ [(one,3..6)]);
692 bt_assert(l ~ [(*,*)]);
695 l = add(l, (one+one+one,one+one));
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);
706 bt_assert(l = add(add(add(add(add(-empty-, (1,2)), (3,1)), (3,5)), (3,2)), (4,5)));
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))]);
716 l = delete(l, [(*,(one+onef(3)))]);
717 l = delete(l, [(*,(4+one))]);
718 bt_assert(l = add(-empty-, (3,1)));
720 l = delete(l, [(*,(onef(5)))]);
721 bt_assert(l = -empty-);
724 l = filter(l2, [(3,1..4)]);
725 l2 = filter(l2, [(3,3..6)]);
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))");
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))");
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))");
742 bt_assert(r = add(add(-empty-, (3,3)), (3,4)));
743 bt_assert(format(r) = "(clist (3,3) (3,4))");
747 bt_assert(r = add(add(-empty-, (3,1)), (3,2)));
748 bt_assert(format(r) = "(clist (3,1) (3,2))");
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))");
756 bt_test_suite(t_clist, "Testing lists of communities");
762 * Testing Extended Communities
763 * ----------------------------
769 cc = (rt, 12345, 200000);
770 bt_assert(format(cc) = "(rt, 12345, 200000)");
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)));
778 bt_assert(format((ro, 100000, 20000)) = "(ro, 100000, 20000)");
781 bt_test_suite(t_ec, "Testing extended communities");
787 * Testing Extended Community List
788 * -------------------------------
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));
801 bt_assert(--empty-- = --empty--);
802 bt_assert(((rt, 10, 20)) !~ --empty--);
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)));
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, *)]);
828 el = add(el, (rt, 10, 40));
829 el2 = filter(el, [(rt, 10, 20..40)] );
830 el2 = add(el2, (rt, 10, 50));
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))");
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))");
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))");
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))");
852 bt_assert(r = add(--empty--, (rt, 10, 1)));
853 bt_assert(format(r) = "(eclist (rt, 10, 1))");
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))");
861 bt_test_suite(t_eclist, "Testing lists of extended communities");
867 * Testing sets of Extended Communities
868 * ------------------------------------
871 define ecs2 = [(rt, ten, (one+onef(0))*10), (ro, 100000, 100..200), (rt, 12345, *)];
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)]");
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);
895 bt_test_suite(t_ec_set, "Testing sets of extended communities");
901 * Testing Large Communities
902 * -------------------------
905 function mktrip(int a)
907 return (a, 2*a, 3*a);
915 bt_assert(---empty--- = ---empty---);
916 bt_assert((10, 20, 30) !~ ---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)));
926 bt_assert(mktrip(1000) ~ ll);
927 bt_assert(mktrip(100) !~ ll);
930 ll = add(ll, (10, 10, 10));
931 ll = add(ll, (20, 20, 20));
932 ll = add(ll, (30, 30, 30));
935 ll2 = add(ll2, (20, 20, 20));
936 ll2 = add(ll2, (30, 30, 30));
937 ll2 = add(ll2, (40, 40, 40));
939 # lclist A (10, 20, 30)
940 bt_assert(format(ll) = "(lclist (10, 10, 10) (20, 20, 20) (30, 30, 30))");
942 # lclist B (20, 30, 40)
943 bt_assert(format(ll2) = "(lclist (20, 20, 20) (30, 30, 30) (40, 40, 40))");
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))");
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))");
957 bt_assert(r = add(---empty---, (10, 10, 10)));
958 bt_assert(format(r) = "(lclist (10, 10, 10))");
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))");
966 bt_test_suite(t_lclist, "Testing lists of large communities");
972 * Testing sets of Large Communities
973 * ---------------------------------
976 function t_lclist_set()
981 ll = add(ll, (10, 20, 30));
982 ll = add(ll, (1000, 2000, 3000));
983 ll = add(ll, mktrip(100000));
985 bt_assert(ll ~ [(5,10,15), (10,20,30)]);
986 bt_assert(ll ~ [(10,15..25,*)]);
987 bt_assert(ll ~ [(ten, *, *)]);
989 bt_assert(ll !~ [(5,10,15), (10,21,30)]);
990 bt_assert(ll !~ [(10,21..25,*)]);
991 bt_assert(ll !~ [(11, *, *)]);
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);
1006 bt_test_suite(t_lclist_set, "Testing sets of large communities");
1012 * Testing defined() function
1013 * --------------------------
1016 function test_undef(int a)
1021 bt_assert(defined(b));
1024 bt_assert(!defined(b));
1035 bt_assert(defined(1));
1036 bt_assert(defined(1.2.3.4));
1039 bt_test_suite(t_define, "Testing defined() function");
1045 * Testing calling functions
1046 * -------------------------
1049 function callme(int arg1; int arg2)
1054 else: return arg1 * arg2;
1065 function t_call_function()
1067 bt_assert(fifteen() = 15);
1069 bt_assert(callme(1, 2) = 42);
1070 bt_assert(callme(42, 2) = 42);
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);
1078 bt_test_suite(t_call_function, "Testing calling functions");
1084 * Test including another config file
1085 * ----------------------------------
1088 function t_include()
1092 include "test.conf.inc";
1096 bt_test_suite(t_include, "Testing including another config file");
1102 * Test if-else statement
1103 * ----------------------
1106 function t_if_else()
1120 bt_test_suite(t_if_else, "Testing if-else statement");
1126 * Unused functions -- testing only parsing
1127 * ----------------------------------------
1132 if source ~ [ RTS_BGP, RTS_STATIC ] then {
1133 # ospf_metric1 = 65535;
1134 # ospf_metric2 = 1000;
1135 ospf_tag = 0x12345678;
1143 if source ~ [ RTS_BGP, RTS_STATIC ] then {
1144 # ospf_metric1 = 65535;
1145 # ospf_metric2 = 1000;
1146 ospf_tag = 0x12345678;
1155 print "Heya, filtering route to ", net.ip, " prefixlen ", net.len, " source ", source;
1156 print "This route was from ", from;
1159 if rip_metric > 15 then {
1160 reject "RIP Metric is more than infinity";
1165 accept "ok I take that";
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;
1183 route 2001:0db8:85a3:8a2e::/64 max 96 as 1000 blackhole;
1186 function test_roa_check()
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;
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;
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;
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;
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;
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;
1232 function roa_operators_test()
1235 print "Testing ROA prefix operators '.maxlen' and '.asn':";
1237 pfx = 12.13.0.0/16 max 24 as 1234;
1239 print "Should be true: ", pfx.len = 16, " ", pfx.maxlen = 24, " ", pfx.asn = 1234;
1241 pfx = 1000::/8 max 32 as 1234;
1243 print "Should be true: ", pfx.len = 8, " ", pfx.maxlen = 32, " ", pfx.asn = 1234;