]> git.ipfire.org Git - thirdparty/bird.git/blob - filter/test.conf
Extends set operations in filters.
[thirdparty/bird.git] / filter / test.conf
1
2 /*
3 * This is an example configuration file.
4 * FIXME: add all examples from docs here.
5 */
6
7 # Yet another comment
8
9 router id 62.168.0.1;
10
11 define xyzzy = (120+10);
12 define '1a-a1' = (20+10);
13 define one = 1;
14 define ten = 10;
15
16 function onef(int a)
17 {
18 return 1;
19 }
20
21
22 function 'mkpair-a'(int a)
23 {
24 return (1, a);
25 }
26
27 function mkpath(int a; int b)
28 {
29 return [= a b 3 2 1 =];
30 }
31
32 function callme(int arg1; int arg2)
33 int local1;
34 int local2;
35 int i;
36 {
37 printn "Function callme called arguments ", arg1, " and ", arg2, ": " ;
38 i = arg2;
39
40 case arg1 {
41 11, 1, 111: printn "jedna, "; printn "jedna";
42 (one+onef(2)): printn "dva, "; printn "jeste jednou dva";
43 (2+one) .. 5: if arg2 < 3 then printn "tri az pet";
44 else: printn "neco jineho";
45 }
46 print;
47 }
48
49 function fifteen()
50 {
51 print "fifteen called";
52 return 15;
53 }
54
55 function paths()
56 bgpmask pm1;
57 bgpmask pm2;
58 bgppath p2;
59 clist l;
60 clist l2;
61 eclist el;
62 eclist el2;
63 {
64 pm1 = / 4 3 2 1 /;
65 pm2 = [= 4 3 2 1 =];
66 print "Testing path masks: ", pm1, " ", pm2;
67 p2 = prepend( + empty +, 1 );
68 p2 = prepend( p2, 2 );
69 p2 = prepend( p2, 3 );
70 p2 = prepend( p2, 4 );
71 print "Testing paths: ", p2;
72 print "Should be true: ", p2 ~ pm1, " ", p2 ~ pm2, " ", 3 ~ p2;
73 print "4 = ", p2.len;
74 p2 = prepend( p2, 5 );
75 print "Should be false: ", p2 ~ pm1, " ", p2 ~ pm2, " ", 10 ~ p2;
76 print "Should be true: ", p2 ~ / ? 4 3 2 1 /, " ", p2, " ", / ? 4 3 2 1 /;
77 print "Should be true: ", p2 ~ [= * 4 3 * 1 =], " ", p2, " ", [= * 4 3 * 1 =];
78 print "Should be true: ", p2 ~ [= (3+2) (2*2) 3 2 1 =], " ", p2 ~ mkpath(5, 4);
79 print "Should be true: ", p2.len = 5, " ", p2.first = 5, " ", p2.last = 1;
80 print "5 = ", p2.len;
81
82 pm1 = [= 1 2 * 3 4 5 =];
83 p2 = prepend( + empty +, 5 );
84 p2 = prepend( p2, 4 );
85 p2 = prepend( p2, 3 );
86 p2 = prepend( p2, 3 );
87 p2 = prepend( p2, 2 );
88 p2 = prepend( p2, 1 );
89 print "Should be true: ", p2 ~ pm1, " ", p2, " ", pm1;
90
91 l = - empty -;
92 print "Should be false in this special case: ", l ~ [(*,*)];
93 l = add( l, (one,2) );
94 print "Should be always true: ", l ~ [(*,*)];
95 l = add( l, (2,one+2) );
96 print "Community list (1,2) (2,3) ", l;
97 print "Should be true: ", (2,3) ~ l, " ", l ~ [(1,*)], " ", l ~ [(2,3)]," ", l ~ [(2,2..3)], " ", l ~ [(1,1..2)], " ", l ~ [(1,1)..(1,2)];
98 l = add( l, (2,5) );
99 l = add( l, (5,one) );
100 l = add( l, (6,one) );
101 l = add( l, (one,one) );
102 l = delete( l, [(5,1),(6,one),(one,1)] );
103 l = delete( l, [(5,one),(6,one)] );
104 l = filter( l, [(1,*)] );
105 print "Community list (1,2) ", l;
106 print "Should be false: ", (2,3) ~ l, " ", l ~ [(2,*)], " ", l ~ [(one,3..6)];
107 print "Should be always true: ", l ~ [(*,*)];
108 l = add( l, (3,one) );
109 l = add( l, (one+one+one,one+one) );
110 l = add( l, (3,3) );
111 l = add( l, (3,4) );
112 l = add( l, (3,5) );
113 l2 = filter( l, [(3,*)] );
114 l = delete( l, [(3,2..4)] );
115 print "Community list (1,2) (3,1) (3,5) ", l;
116 l = add( l, (3,2) );
117 l = add( l, (4,5) );
118 print "Community list (1,2) (3,1) (3,2) (3,5) (4,5) ", l;
119 print "Should be true: ", l ~ [(*,2)], " ", l ~ [(*,5)], " ", l ~ [(*, one)];
120 print "Should be false: ", l ~ [(*,3)], " ", l ~ [(*,(one+6))], " ", l ~ [(*, (one+one+one))];
121 l = delete( l, [(*,(one+onef(3)))] );
122 l = delete( l, [(*,(4+one))] );
123 print "Community list (3,1) ", l;
124 l = delete( l, [(*,(onef(5)))] );
125 print "Community list empty ", l;
126 l2 = add( l2, (3,6) );
127 l = filter( l2, [(3,1..4)] );
128 l2 = filter( l2, [(3,3..6)] );
129 print "clist A (1..4): ", l;
130 print "clist B (3..6): ", l2;
131 print "clist A union B: ", add( l2, l );
132 print "clist A isect B: ", filter( l, l2 );
133 print "clist A \ B: ", delete( l, l2 );
134
135 el = -- empty --;
136 el = add(el, (rt, 10, 20));
137 el = add(el, (ro, 10.20.30.40, 100));
138 el = add(el, (ro, 11.21.31.41.mask(16), 200));
139 print "EC list (rt, 10, 20) (ro, 10.20.30.40, 100) (ro, 11.21.0.0, 200):";
140 print el;
141 el = delete(el, (rt, 10, 20));
142 el = delete(el, (rt, 10, 30));
143 el = add(el, (unknown 2, ten, 1));
144 el = add(el, (unknown 5, ten, 1));
145 el = add(el, (rt, ten, one+one));
146 el = add(el, (rt, 10, 3));
147 el = add(el, (rt, 10, 4));
148 el = add(el, (rt, 10, 5));
149 el = add(el, (generic, 0x2000a, 3*ten));
150 el = delete(el, [(rt, 10, 2..ten)]);
151 print "EC list (ro, 10.20.30.40, 100) (ro, 11.21.0.0, 200) (rt, 10, 1) (unknown 0x5, 10, 1) (rt, 10, 30):";
152 print el;
153 el = filter(el, [(rt, 10, *)]);
154 print "EC list (rt, 10, 1) (rt, 10, 30): ", el;
155 print "Testing EC list, true: ", (rt, 10, 1) ~ el, " ", el ~ [(rt, 10, ten..40)];
156 print "Testing EC list, false: ", (rt, 10, 20) ~ el, " ", (ro, 10.20.30.40, 100) ~ el, " ", el ~ [(rt, 10, 35..40)], " ", el ~ [(ro, 10, *)];
157 el = add(el, (rt, 10, 40));
158 el2 = filter(el, [(rt, 10, 20..40)] );
159 el2 = add(el2, (rt, 10, 50));
160 print "eclist A (1,30,40): ", el;
161 print "eclist B (30,40,50): ", el2;
162 print "eclist A union B: ", add( el2, el );
163 print "eclist A isect B: ", filter( el, el2 );
164 print "eclist A \ B: ", delete( el, el2 );
165
166
167 }
168
169 function bla()
170 {
171 print "fifteen called";
172 return 15;
173 }
174
175 define four=4;
176 define onetwo=1.2.3.4;
177
178 function __test1()
179 {
180 if source ~ [ RTS_BGP, RTS_STATIC ] then {
181 # ospf_metric1 = 65535;
182 # ospf_metric2 = 1000;
183 ospf_tag = 0x12345678;
184 accept;
185 } reject;
186 }
187
188 function __test2()
189 {
190 if source ~ [ RTS_BGP, RTS_STATIC ] then {
191 # ospf_metric1 = 65535;
192 # ospf_metric2 = 1000;
193 ospf_tag = 0x12345678;
194 accept;
195 } reject;
196 }
197
198 function test_pxset(prefix set pxs)
199 {
200 print pxs;
201 print " must be true: ", 10.0.0.0/8 ~ pxs, ",", 10.0.0.0/10 ~ pxs, ",", 10.0.0.0/12 ~ pxs, ",",
202 20.0.0.0/24 ~ pxs, ",", 20.0.40.0/24 ~ pxs, ",", 20.0.0.0/26 ~ pxs, ",",
203 20.0.100.0/26 ~ pxs, ",", 20.0.0.0/28 ~ pxs, ",", 20.0.255.0/28 ~ pxs;
204 print " must be false: ", 10.0.0.0/7 ~ pxs, ",", 10.0.0.0/13 ~ pxs, ",", 10.0.0.0/16 ~ pxs, ",",
205 20.0.0.0/16 ~ pxs, ",", 20.0.0.0/23 ~ pxs, ",", 20.0.0.0/29 ~ pxs, ",",
206 11.0.0.0/10 ~ pxs, ",", 20.1.0.0/26 ~ pxs;
207 }
208
209 function test_undef(int a)
210 int b;
211 {
212 if a = 3
213 then b = 4;
214 print "Defined: ", a, " ", b, " ", defined(b);
215 }
216
217 function __startup()
218 int i;
219 bool b;
220 prefix px;
221 ip p;
222 pair pp;
223 quad qq;
224 ec cc;
225 int set is;
226 int set is1;
227 int set is2;
228 int set is3;
229 pair set ps;
230 ec set ecs;
231 prefix set pxs;
232 string s;
233 {
234 print "1a-a1 = 30: ", '1a-a1';
235 print "Testing filter language:";
236 i = four;
237 i = 12*100 + 60/2 + i;
238 i = ( i + 0 );
239 print " arithmetics: 1234 = ", i;
240 printn " if statements ";
241 print "what happens here?";
242 printn ".";
243 if (i = 4) then { print "*** FAIL: if 0"; quitbird; } else printn ".";
244 # if !(i = 3) then { print "*** FAIL: if 0"; quitbird; } else printn ".";
245 if 1234 = i then printn "."; else { print "*** FAIL: if 1 else"; }
246 # if 1 <= 1 then printn "."; else { print "*** FAIL: test 3"; }
247 if 1234 < 1234 then { print "*** FAIL: test 4"; quitbird; } else print "ok";
248 is = [ 2, 3, 4, 7..11 ];
249 print " must be true: ", 1.2.0.0/16 ~ [ 1.0.0.0/8{ 15 , 17 } ];
250 print " data types; must be true: ", 1.2.3.4 = 1.2.3.4, ",", 1 ~ [1,2,3], ",", 5 ~ [1..20], ",", 10 ~ is, ",", 2 ~ [ 1, 2, 3 ], ",", 5 ~ [ 4 .. 7 ], ",", 1.2.3.4 ~ [ 1.2.3.3..1.2.3.5 ], ",", 1.2.3.4 ~ 1.0.0.0/8, ",", 1.0.0.0/8 ~ 1.0.0.0/8, ",", 1.0.0.0/8 ~ [ 1.0.0.0/8+ ];
251 print " must be true: ", true && true, ",", true || false, ",", ! false && ! false && true, ",", 1 < 2 && 1 != 3, ",", true && true && ! false, ",", true || 1+"a", ",", !(false && 1+"a");
252
253 print " must be true: ", defined(1), ",", defined(1.2.3.4), ",", 1 != 2, ",", 1 <= 2;
254 print " data types: must be false: ", 1 ~ [ 2, 3, 4 ], ",", 5 ~ is, ",", 1.2.3.4 ~ [ 1.2.3.3, 1.2.3.5 ], ",", (1,2) > (2,2), ",", (1,1) > (1,1), ",", 1.0.0.0/9 ~ [ 1.0.0.0/8- ], ",", 1.2.0.0/17 ~ [ 1.0.0.0/8{ 15 , 16 } ], ",", true && false;
255
256 is1 = [ 1, 5, 8, 11, 15, 17, 19];
257
258 is1 = [ one, (2+1), (6-one), 8, 11, 15, 17, 19];
259 is2 = [(17+2), 17, 15, 11, 8, 5, 3, 2];
260 is3 = [5, 17, 2, 11, 8, 15, 3, 19];
261
262 print " must be true: ", 1 ~ is1, " ", 3 ~ is1, " ", 5 ~ is1;
263 print " must be true: ", (one+2) ~ is1, " ", 2 ~ is2, " ", 2 ~ is3;
264 print " must be false: ", 4 ~ is1, " ", 4 ~ is2, " ", 4 ~ is3;
265 print " must be false: ", 10 ~ is1, " ", 10 ~ is2, " ", 10 ~ is3;
266 print " must be true: ", 15 ~ is1, " ", 15 ~ is2, " ", 15 ~ is3;
267 print " must be false: ", 18 ~ is1, " ", 18 ~ is2, " ", 18 ~ is3;
268 print " must be true: ", 19 ~ is1, " ", 19 ~ is2, " ", 19 ~ is3;
269 print " must be false: ", 20 ~ is1, " ", 20 ~ is2, " ", 20 ~ is3;
270
271 px = 1.2.0.0/18;
272 print "Testing prefixes: 1.2.0.0/18 = ", px;
273 print " must be true: ", 192.168.0.0/16 ~ 192.168.0.0/16, " ", 192.168.0.0/17 ~ 192.168.0.0/16, " ", 192.168.254.0/24 ~ 192.168.0.0/16;
274 print " must be false: ", 192.168.0.0/15 ~ 192.168.0.0/16, " ", 192.160.0.0/17 ~ 192.168.0.0/16;
275
276 p = 127.1.2.3;
277 print "Testing mask : 127.0.0.0 = ", p.mask(8);
278
279 pp = (1, 2);
280 print "Testing pairs: (1,2) = ", (1,2), " = ", pp, " = ", (1,1+1), " = ", 'mkpair-a'(2);
281 print " must be true: ", (1,2) = (1,1+1);
282 print "Testing enums: ", RTS_DUMMY, " ", RTS_STATIC, " ",
283 ", true: ", RTS_STATIC ~ [RTS_STATIC, RTS_DEVICE],
284 ", false: ", RTS_BGP ~ [RTS_STATIC, RTS_DEVICE];
285
286 ps = [(1,(one+one)), (3,4)..(4,8), (5,*), (6,3..6)];
287 print "Pair set: ", ps;
288 print "Testing pair set, true: ", pp ~ ps, " ", (3,5) ~ ps, " ", (4,1) ~ ps, " ", (5,4) ~ ps, " ", (5,65535) ~ ps, " ", (6,4) ~ ps, " ", (3, 10000) ~ ps;
289 print "Testing pair set, false: ", (3,3) ~ ps, " ", (4,9) ~ ps, " ", (4,65535) ~ ps, " ", (6,2) ~ ps, " ", (6,6+one) ~ ps, " ", ((one+6),2) ~ ps, " ", (1,1) ~ ps;
290
291 ps = [(20..150, 200..300), (50100..50200, 1000..50000), (*, 5+5)];
292 print "Pair set: .. too long ..";
293 print "Testing pair set, true: ", (100,200) ~ ps, " ", (150,300) ~ ps, " ", (50180,1200) ~ ps, " ", (50110,49000) ~ ps, " ", (0,10) ~ ps, " ", (64000,10) ~ ps;
294 print "Testing pair set, false: ", (20,199) ~ ps, " ", (151,250) ~ ps, " ", (50050,2000) ~ ps, " ", (50150,50050) ~ ps, " ", (10,9) ~ ps, " ", (65535,11) ~ ps ;
295
296 qq = 1.2.3.4;
297 print "Testinq quad: 1.2.3.4 = ", qq,
298 ", true: ", qq = 1.2.3.4, " ", qq ~ [1.2.3.4, 5.6.7.8],
299 ", false: ", qq = 4.3.2.1, " ", qq ~ [1.2.1.1, 1.2.3.5];
300
301 cc = (rt, 12345, 200000);
302 print "Testing EC: (rt, 12345, 200000) = ", cc;
303 print "Testing EC: (ro, 100000, 20000) = ", (ro, 100000, 20000);
304 print "Testing EC: (rt, 10.20.30.40, 20000) = ", (rt, 10.20.30.40, 20000);
305 print " true: ", cc = (rt, 12345, 200000), " ", cc < (rt, 12345, 200010),
306 ", false: ", cc = (rt, 12346, 200000), " ", cc = (ro, 12345, 200000), " ", cc > (rt, 12345, 200010);
307
308 ecs = [(rt, ten, (one+onef(0))*10), (ro, 100000, 100..200), (rt, 12345, *)];
309 print "EC set: ", ecs;
310 print "Testing EC set, true: ", (rt, 10, 20) ~ ecs, " ", (ro, 100000, 100) ~ ecs, " ", (ro, 100000, 200) ~ ecs,
311 " ", (rt, 12345, 0) ~ ecs, " ", cc ~ ecs, " ", (rt, 12345, 4000000) ~ ecs;
312 print "Testing EC set, false: ", (ro, 10, 20) ~ ecs, " ", (rt, 10, 21) ~ ecs, " ", (ro, 100000, 99) ~ ecs,
313 " ", (ro, 12345, 10) ~ ecs, " ", (rt, 12346, 0) ~ ecs, " ", (ro, 0.1.134.160, 150) ~ ecs;
314
315 s = "Hello";
316 print "Testing string: ", s, " true: ", s ~ "Hell*", " false: ", s ~ "ell*";
317
318 b = true;
319 print "Testing bool: ", b, ", ", !b;
320
321 if ( b = true ) then print "Testing bool comparison b = true: ", b;
322 else { print "*** FAIL: TRUE test failed" ; quitbird; }
323
324 pxs = [ 1.2.0.0/16, 1.4.0.0/16+];
325 print "Testing prefix sets: ";
326 print pxs;
327 print " must be true: ", 1.2.0.0/16 ~ pxs, ",", 1.4.0.0/16 ~ pxs, ",", 1.4.0.0/18 ~ pxs, ",", 1.4.0.0/32 ~ pxs;
328 print " must be false: ", 1.1.0.0/16 ~ pxs, ",", 1.3.0.0/16 ~ pxs, ",", 1.2.0.0/15 ~ pxs, ",", 1.2.0.0/17 ~ pxs, ",",
329 1.2.0.0/32 ~ pxs, ",", 1.4.0.0/15 ~ pxs;
330
331 test_pxset([ 10.0.0.0/16{8,12}, 20.0.0.0/16{24,28} ]);
332 print "What will this do? ", [ 1, 2, 1, 1, 1, 3, 4, 1, 1, 1, 5 ];
333
334 print "Testing functions...";
335 callme ( 1, 2 );
336 callme ( 2, 2 );
337 callme ( 2, 2 );
338 callme ( 3, 2 );
339 callme ( 4, 4 );
340 callme ( 7, 2 );
341
342 i = fifteen();
343 print "Testing function calls: 15 = ", i;
344
345 paths();
346
347 print "1.2.3.4 = ", onetwo;
348
349 test_undef(2);
350 test_undef(3);
351 test_undef(2);
352
353 print "Testing include";
354 include "test.conf.inc";
355
356 print "done";
357 quitbird;
358 # print "*** FAIL: this is unreachable";
359 }
360
361 filter testf
362 int j;
363 {
364 print "Heya, filtering route to ", net.ip, " prefixlen ", net.len, " source ", source;
365 print "This route was from ", from;
366 j = 7;
367 j = 17;
368 if rip_metric > 15 then {
369 reject "RIP Metric is more than infinity";
370 }
371 rip_metric = 14;
372 unset(rip_metric);
373
374 accept "ok I take that";
375 }
376
377 eval __startup();