Revision 9b0a0ba9 filter/test.conf

View differences:

filter/test.conf
1

  
2 1
/*
3
 *	This is an example configuration file.
2
 *	This is unit testing configuration file for testing filters
3
 *
4 4
 *	FIXME: add all examples from docs here.
5 5
 */
6 6

  
7
# Yet another comment
8

  
9 7
router id 62.168.0.1;
10 8

  
11
define xyzzy = (120+10);
12
define '1a-a1' = (20+10);
13
define one = 1;
14
define ten = 10;
9
/* We have to setup any protocol */
10
protocol static { ipv4; }
11

  
15 12

  
16
define p23 = (2, 3);
17
define ip1222 = 1.2.2.2;
18 13

  
19
define net10 = 10.0.0.0/8;
20
define netdoc = 2001:db8::/32;
21 14

  
15
/*
16
 * 	Common definitions and functions
17
 * 	--------------------------------
18
 */
19

  
20
define one = 1;
21
define ten = 10;
22 22

  
23 23
function onef(int a)
24 24
{
......
26 26
}
27 27

  
28 28

  
29
function 'mkpair-a'(int a)
30
{
31
	return (1, a);
32
}
33 29

  
34
function mktrip(int a)
35
{
36
	return (a, 2*a, 3*a);
37
}
38 30

  
39
function mkpath(int a; int b)
40
{
41
	return [= a b 3 2 1 =];
42
}
31
/*
32
 * 	Testing empty lists/paths
33
 * 	-------------------------
34
 */
43 35

  
44
function callme(int arg1; int arg2)
45
int local1;
46
int local2;
47
int i;
36
function t_empty()
48 37
{
49
	printn "Function callme called arguments ", arg1, " and ", arg2, ": " ;
50
	i = arg2;
38
	bt_assert(+empty+ = +empty+);
39
	bt_assert(+empty+ != -empty-);
40
	bt_assert(+empty+ != --empty--);
51 41

  
52
	case arg1 {
53
	11, 1, 111: printn "jedna, "; printn "jedna";
54
	(one+onef(2)): printn "dva, "; printn "jeste jednou dva";
55
	(2+one) .. 5: if arg2 < 3 then printn "tri az pet";
56
	else: printn "neco jineho";
57
	}
58
	print;
59
}
42
	bt_assert(-empty- = -empty-);
43
	bt_assert(-empty- != --empty--);
60 44

  
61
function fifteen()
62
{
63
	print "fifteen called";
64
	return 15;
45
	bt_assert(--empty-- = --empty--);
65 46
}
66 47

  
48
bt_test_suite(t_empty, "Testing +empty+, -empty-, --empty--");
49

  
50

  
51

  
52

  
67 53
/*
68
roa table rl
69
{
70
	roa 10.110.0.0/16 max 16 as 1000;
71
	roa 10.120.0.0/16 max 24 as 1000;
72
	roa 10.130.0.0/16 max 24 as 2000;
73
	roa 10.130.128.0/18 max 24 as 3000;
74
}
54
 * 	Testing Paths
55
 * 	-------------
56
 */
75 57

  
76
function test_roa()
58
function mkpath(int a; int b)
77 59
{
78
	# cannot be tested in __startup(), sorry
79
	print "Testing ROA";
80
	print "Should be true: ", roa_check(rl, 10.10.0.0/16, 1000) = ROA_UNKNOWN,
81
	      " ", roa_check(rl, 10.0.0.0/8, 1000) = ROA_UNKNOWN,
82
	      " ", roa_check(rl, 10.110.0.0/16, 1000) = ROA_VALID,
83
	      " ", roa_check(rl, 10.110.0.0/16, 2000) = ROA_INVALID,
84
	      " ", roa_check(rl, 10.110.32.0/20, 1000) = ROA_INVALID,
85
	      " ", roa_check(rl, 10.120.32.0/20, 1000) = ROA_VALID;
86
	print "Should be true: ", roa_check(rl, 10.120.32.0/20, 2000) = ROA_INVALID,
87
	      " ", roa_check(rl, 10.120.32.32/28, 1000) = ROA_INVALID,
88
	      " ", roa_check(rl, 10.130.130.0/24, 1000) = ROA_INVALID,
89
	      " ", roa_check(rl, 10.130.130.0/24, 2000) = ROA_VALID,
90
	      " ", roa_check(rl, 10.130.30.0/24, 3000) = ROA_INVALID,
91
	      " ", roa_check(rl, 10.130.130.0/24, 3000) = ROA_VALID;
60
	return [= a b 3 2 1 =];
92 61
}
93
*/
94 62

  
95
function path_test()
63
function t_path()
96 64
bgpmask pm1;
97 65
bgpmask pm2;
98 66
bgppath p2;
99
clist l;
100
clist l2;
101
eclist el;
102
eclist el2;
103
lclist ll;
104
lclist ll2;
105 67
{
106
	print "Entering path test...";
107 68
	pm1 =  / 4 3 2 1 /;
108
	pm2 = [= 3..6 3 2 1..2 =];
109
	print "Testing path masks: ", pm1, " ", pm2;
69
	pm2 = [= 4 3 2 1 =];
70

  
110 71
	p2 = prepend( + empty +, 1 );
111 72
	p2 = prepend( p2, 2 );
112 73
	p2 = prepend( p2, 3 );
113 74
	p2 = prepend( p2, 4 );
114
	print "Testing path: (4 3 2 1) = ", p2;
115
	print "Should be true: ", p2 ~ pm1, " ", p2 ~ pm2, " ", 3 ~ p2, " ", p2 ~ [2, 10..20], " ", p2 ~ [4, 10..20];
116
	print "4 = ", p2.len;
117
	p2 = prepend( p2, 5 );
118
	print "Testing path: (5 4 3 2 1) = ", p2;
119
	print "Should be false: ", p2 ~ pm1, " ", p2 ~ pm2, " ", 10 ~ p2, " ", p2 ~ [8, ten..(2*ten)], " ", p2 ~ [= 1..4 4 3 2 1 =], " ",  p2 ~ [= 5 4 4..100 2 1 =];
120
	print "Should be true: ", p2 ~  / ? 4 3 2 1 /,  " ", p2, " ",  / ? 4 3 2 1 /;
121
	print "Should be true: ", p2 ~ [= * 4 3 * 1 =], " ", p2, " ", [= * 4 3 * 1 =];
122
	print "Should be true: ", p2 ~ [= 5..6 4..10 1..3 1..3 1..65536 =];
123
	print "Should be true: ", p2 ~ [= (3+2) (2*2) 3 2 1 =], " ", p2 ~ mkpath(5, 4);
124
	print "Should be true: ", p2.len = 5, " ", p2.first = 5, " ", p2.last = 1;
125
	print "Should be true: ", pm1 = [= 4 3 2 1 =], " ", pm1 != [= 4 3 1 2 =], " ",
126
				pm2 = [= 3..6 3 2 1..2 =], " ", pm2 != [= 3..6 3 2 1..3 =], " ",
127
				[= 1 2 (1+2) =] = [= 1 2 (1+2) =], " ", [= 1 2 (1+2) =] != [= 1 2 (2+1) =];
128
	print "5 = ", p2.len;
129
	print "Delete 3:   ", delete(p2, 3);
130
	print "Filter 1-3: ", filter(p2, [1..3]);
75

  
76
	print "Testing paths: ", p2;
77
	bt_assert(p2.len = 4);
78
	bt_assert(p2 ~ pm1);
79
	bt_assert(p2 ~ pm2);
80
	bt_assert(3 ~ p2);
81
	bt_assert(p2 ~ [2, 10..20]);
82
	bt_assert(p2 ~ [4, 10..20]);
83

  
84
	p2 = prepend(p2, 5);
85
	bt_assert(p2 !~ pm1);
86
	bt_assert(p2 !~ pm2);
87
	bt_assert(10 !~ p2);
88
	bt_assert(p2 !~ [8, ten..(2*ten)]);
89
	bt_assert(p2 ~  / ? 4 3 2 1 /);
90
	bt_assert(p2 ~ [= * 4 3 * 1 =]);
91
	bt_assert(p2 ~ [= (3+2) (2*2) 3 2 1 =]);
92
	bt_assert(p2 ~ mkpath(5, 4));
93

  
94
	bt_assert(p2.len = 5);
95
	bt_assert(p2.first = 5);
96
	bt_assert(p2.last = 1);
97

  
98
	bt_assert(p2.len = 5);
99
	bt_assert(delete(p2, 3) = prepend(prepend(prepend(prepend(+empty+, 1), 2), 4), 5));
100
	bt_assert(filter(p2, [1..3]) = prepend(prepend(prepend(+empty+, 1), 2), 3));
131 101

  
132 102
	pm1 = [= 1 2 * 3 4 5 =];
133 103
	p2 = prepend( + empty +, 5 );
......
136 106
	p2 = prepend( p2, 3 );
137 107
	p2 = prepend( p2, 2 );
138 108
	p2 = prepend( p2, 1 );
139
	print "Should be true: ", p2 ~ pm1, " ", p2, " ", pm1;
140
	print "Delete 3:   ", delete(p2, 3);
141
	print "Delete 4-5: ", delete(p2, [4..5]);
109

  
110
	bt_assert(p2 ~ pm1);
111
	bt_assert(delete(p2, 3) = prepend(prepend(prepend(prepend(+empty+, 5), 4), 2), 1));
112
	bt_assert(delete(p2, [4..5]) = prepend(prepend(prepend(prepend(+empty+, 3), 3), 2), 1));
113
}
114

  
115
bt_test_suite(t_path, "Testing paths");
116

  
117

  
118

  
119

  
120
/*
121
 * 	Testing Community List
122
 *	----------------------
123
 */
124

  
125
define p23 = (2, 3);
126

  
127
function t_community_list()
128
clist l;
129
clist l2;
130
{
131
	/* XXX: add((x,y)) works as prepend */
142 132

  
143 133
	l = - empty -;
144
	print "Should be false in this special case: ", l ~ [(*,*)];
134
	bt_assert(l !~ [(*,*)]);
135
	bt_assert((l ~ [(*,*)]) != (l !~ [(*,*)]));
136

  
145 137
	l = add( l, (one,2) );
146
	print "Should be always true: ", l ~ [(*,*)];
138
	bt_assert(l ~ [(*,*)]);
147 139
	l = add( l, (2,one+2) );
148 140
	print "Community list (1,2) (2,3) ", l;
149
	print "Should be true: ", (2,3) ~ l, " ", l ~ [(1,*)], " ", l ~ [p23]," ", l ~ [(2,2..3)], " ", l ~ [(1,1..2)], " ", l ~ [(1,1)..(1,2)];
150
	l = add( l, (2,5) );
151
	l = add( l, (5,one) );
152
	l = add( l, (6,one) );
153
	l = add( l, (one,one) );
154
	l = delete( l, [(5,1),(6,one),(one,1)] );
155
	l = delete( l, [(5,one),(6,one)] );
156
	l = filter( l, [(1,*)] );
157
	print "Community list (1,2) ", l;
158
	print "Should be false: ", (2,3) ~ l, " ", l ~ [(2,*)], " ", l ~ [(one,3..6)];
159
	print "Should be always true: ", l ~ [(*,*)];
160
	l = add( l, (3,one) );
161
	l = add( l, (one+one+one,one+one) );
162
	l = add( l, (3,3) );
163
	l = add( l, (3,4) );
164
	l = add( l, (3,5) );
165
	l2 = filter( l, [(3,*)] );
166
	l = delete( l, [(3,2..4)] );
167
	print "Community list (1,2) (3,1) (3,5) ", l, " len: ", l.len;
168
	l = add( l, (3,2) );
169
	l = add( l, (4,5) );
170
	print "Community list (1,2) (3,1) (3,5) (3,2) (4,5) ", l, " len: ", l.len;
171
	print "Should be true: ", l ~ [(*,2)], " ", l ~ [(*,5)], " ", l ~ [(*, one)];
172
	print "Should be false: ", l ~ [(*,3)], " ", l ~ [(*,(one+6))], " ", l ~ [(*, (one+one+one))];
173
	l = delete( l, [(*,(one+onef(3)))] );
174
	l = delete( l, [(*,(4+one))] );
175
	print "Community list (3,1) ", l;
176
	l = delete( l, [(*,(onef(5)))] );
177
	print "Community list empty ", l;
178
	l2 = add( l2, (3,6) );
179
	l = filter( l2, [(3,1..4)] );
180
	l2 = filter( l2, [(3,3..6)] );
141

  
142
	bt_assert((2,3) ~ l);
143
	bt_assert(l ~ [(1,*)]);
144
	bt_assert(l ~ [p23]);
145
	bt_assert(l ~ [(2,2..3)]);
146
	bt_assert(l ~ [(1,1..2)]);
147
	bt_assert(l ~ [(1,1)..(1,2)]);
148

  
149
	l = add(l, (2,5));
150
	l = add(l, (5,one));
151
	l = add(l, (6,one));
152
	l = add(l, (one,one));
153
	l = delete(l, [(5,1),(6,one),(one,1)]);
154
	l = delete(l, [(5,one),(6,one)]);
155
	l = filter(l, [(1,*)]);
156
	bt_assert(l = add(-empty-, (1,2)));
157

  
158
	bt_assert((2,3) !~ l);
159
	bt_assert(l !~ [(2,*)]);
160
	bt_assert(l !~ [(one,3..6)]);
161
	bt_assert(l ~ [(*,*)]);
162

  
163
	l = add(l, (3,one));
164
	l = add(l, (one+one+one,one+one));
165
	l = add(l, (3,3));
166
	l = add(l, (3,4));
167
	l = add(l, (3,5));
168
	l2 = filter(l, [(3,*)]);
169
	l = delete(l, [(3,2..4)]);
170
	bt_assert(l = add(add(add(-empty-, (1,2)), (3,1)), (3,5)));
171
	bt_assert(l.len = 3);
172

  
173
	l = add(l, (3,2));
174
	l = add(l, (4,5));
175
	bt_assert(l = add(add(add(add(add(-empty-, (1,2)), (3,1)), (3,5)), (3,2)), (4,5)));
176

  
177
	bt_assert(l.len = 5);
178
	bt_assert(l ~ [(*,2)]);
179
	bt_assert(l ~ [(*,5)]);
180
	bt_assert(l ~ [(*, one)]);
181
	bt_assert(l !~ [(*,3)]);
182
	bt_assert(l !~ [(*,(one+6))]);
183
	bt_assert(l !~ [(*, (one+one+one))]);
184

  
185
	l = delete(l, [(*,(one+onef(3)))]);
186
	l = delete(l, [(*,(4+one))]);
187
	bt_assert(l = add(-empty-, (3,1)));
188

  
189
	l = delete(l, [(*,(onef(5)))]);
190
	bt_assert(l = -empty-);
191

  
192
	l2 = add(l2, (3,6));
193
	l = filter(l2, [(3,1..4)]);
194
	l2 = filter(l2, [(3,3..6)]);
195

  
181 196
	print "clist A (1..4): ", l;
197
	bt_assert(l = add(add(add(add(-empty-, (3,1)), (3,2)), (3,3)), (3,4)));
198

  
182 199
	print "clist B (3..6): ", l2;
183
	print "clist A union B: ", add( l2, l );
200
	bt_assert(l2 = add(add(add(add(-empty-, (3,3)), (3,4)), (3,5)), (3,6)));
201

  
202
	print "clist A union B: ", add( l, l2 );
203
	bt_assert(add(l, l2) = add(add(add(add(add(add(-empty-, (3,1)), (3,2)), (3,3)), (3,4)), (3,5)), (3,6)));
204

  
184 205
	print "clist A isect B: ", filter( l, l2 );
206
	bt_assert(filter(l, l2) = add(add(-empty-, (3,3)), (3,4)));
207

  
185 208
	print "clist A \ B: ", delete( l, l2 );
209
	bt_assert(delete(l, l2) = add(add(-empty-, (3,1)), (3,2)));
210
}
211

  
212
bt_test_suite(t_community_list, "Testing communities and lists");
213

  
214

  
215

  
216

  
217
/*
218
 * 	Testing Extended Community List
219
 * 	-------------------------------
220
 */
186 221

  
222
function t_extended_community_list()
223
eclist el;
224
eclist el2;
225
{
187 226
	el = -- empty --;
188 227
	el = add(el, (rt, 10, 20));
189 228
	el = add(el, (ro, 10.20.30.40, 100));
190 229
	el = add(el, (ro, 11.21.31.41.mask(16), 200));
230

  
191 231
	print "EC list (rt, 10, 20) (ro, 10.20.30.40, 100) (ro, 11.21.0.0, 200):";
192 232
	print el;
193
	print "EC len: ", el.len;
233
	bt_assert(el.len = 3);
194 234
	el = delete(el, (rt, 10, 20));
195 235
	el = delete(el, (rt, 10, 30));
236
	bt_assert(el = add(add(--empty--, (ro, 10.20.30.40, 100)), (ro, 11.21.0.0, 200)));
196 237
	el = add(el, (unknown 2, ten, 1));
197 238
	el = add(el, (unknown 5, ten, 1));
198 239
	el = add(el, (rt, ten, one+one));
......
201 242
	el = add(el, (rt, 10, 5));
202 243
	el = add(el, (generic, 0x2000a, 3*ten));
203 244
	el = delete(el, [(rt, 10, 2..ten)]);
204
	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):";
205
	print el;
245
	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)));
246

  
206 247
	el = filter(el, [(rt, 10, *)]);
207
	print "EC list (rt, 10, 1) (rt, 10, 30): ", el;
208
	print "Testing EC list, true: ", (rt, 10, 1) ~ el, " ", el ~ [(rt, 10, ten..40)];
209
	print "Testing EC list, false: ", (rt, 10, 20) ~ el, " ", (ro, 10.20.30.40, 100) ~ el, " ", el ~ [(rt, 10, 35..40)], " ", el ~ [(ro, 10, *)];
248
	bt_assert(el = add(add(--empty--, (rt, 10, 1)), (rt, 10, 30)));
249
	bt_assert((rt, 10, 1) ~ el);
250
	bt_assert(el ~ [(rt, 10, ten..40)]);
251
	bt_assert((rt, 10, 20) !~ el);
252
	bt_assert((ro, 10.20.30.40, 100) !~ el);
253
	bt_assert(el !~ [(rt, 10, 35..40)]);
254
	bt_assert(el !~ [(ro, 10, *)]);
255

  
210 256
	el = add(el, (rt, 10, 40));
211 257
	el2 = filter(el, [(rt, 10, 20..40)] );
212 258
	el2 = add(el2, (rt, 10, 50));
259

  
213 260
	print "eclist A (1,30,40): ", el;
261
	bt_assert(el = add(add(add(--empty--, (rt, 10, 1)), (rt, 10, 30)), (rt, 10, 40)));
262

  
214 263
	print "eclist B (30,40,50): ", el2;
215
	print "eclist A union B: ", add( el2, el );
216
	print "eclist A isect B: ", filter( el, el2 );
217
	print "eclist A \ B: ", delete( el, el2 );
218

  
219
	ll = --- empty ---;
220
	ll = add(ll, (ten, 20, 30));
221
	ll = add(ll, (1000, 2000, 3000));
222
	ll = add(ll, mktrip(100000));
223
	print "LC list (10, 20, 30) (1000, 2000, 3000) (100000, 200000, 300000):";
224
	print ll;
225
	print "LC len: ", el.len;
226
	print "Should be true: ", mktrip(1000) ~ ll, " ", ll ~ [(5,10,15), (10,20,30)], " ", ll ~ [(10,15..25,*)], " ", ll ~ [(ten, *, *)];
227
	print "Should be false: ", mktrip(100) ~ ll, " ", ll ~ [(5,10,15), (10,21,30)], " ", ll ~ [(10,21..25,*)], " ", ll ~ [(11, *, *)];
228
	print "LC filtered: ", filter(ll, [(5..15, *, *), (100000, 500..500000, *)]);
229

  
230
	ll = --- empty ---;
231
	ll = add(ll, (10, 10, 10));
232
	ll = add(ll, (20, 20, 20));
233
	ll = add(ll, (30, 30, 30));
234

  
235
	ll2 = --- empty ---;
236
	ll2 = add(ll2, (20, 20, 20));
237
	ll2 = add(ll2, (30, 30, 30));
238
	ll2 = add(ll2, (40, 40, 40));
239

  
240
	print "lclist A (10,20,30): ", ll;
241
	print "lclist B (20,30,40): ", ll2;
242
	print "lclist A union B: ", add(ll, ll2);
243
	print "lclist A isect B: ", filter(ll, ll2);
244
	print "lclist A \ B: ", delete(ll, ll2);
245

  
246
#	test_roa();
247
}
248

  
249
function bla()
250
{
251
	print "fifteen called";
252
	return 15;
264
	bt_assert(el2 = add(add(add(--empty--, (rt, 10, 30)), (rt, 10, 40)), (rt, 10, 50)));
265

  
266
	print "eclist A union B: ", add(el2, el);
267
	bt_assert(add(el2, el) =  add(add(add(add(--empty--, (rt, 10, 30)), (rt, 10, 40)), (rt, 10, 50)), (rt, 10, 1)));
268

  
269
	print "eclist A isect B: ", filter(el, el2);
270
	bt_assert(filter(el, el2) = add(add(--empty--, (rt, 10, 30)), (rt, 10, 40)));
271

  
272
	print "eclist A \ B: ", delete(el, el2);
273
	bt_assert(delete(el, el2) = add(--empty--, (rt, 10, 1)));
253 274
}
254 275

  
255
define four=4;
256
define onetwo=1.2.3.4;
276
bt_test_suite(t_extended_community_list, "Testing extended communities and lists");
257 277

  
258
function __test1()
278

  
279

  
280

  
281
/*
282
 * 	Testing defined() function
283
 * 	--------------------------
284
 */
285

  
286
function test_undef(int a)
287
int b;
259 288
{
260
	if source ~ [ RTS_BGP, RTS_STATIC ] then {
261
#		ospf_metric1 = 65535;
262
#		ospf_metric2 = 1000;
263
		ospf_tag = 0x12345678;
264
		accept;
289
	if a = 3 then {
290
		b = 4;
291
		bt_assert(defined(b));
292
	}
293
	else {
294
		bt_assert(!defined(b));
265 295
	}
266
	reject;
267 296
}
268 297

  
269
function __test2()
298
function t_define()
299
int i;
270 300
{
271
	if source ~ [ RTS_BGP, RTS_STATIC ] then {
272
#		ospf_metric1 = 65535;
273
#		ospf_metric2 = 1000;
274
		ospf_tag = 0x12345678;
275
		accept;
276
	}
277
	reject;
301
	test_undef(2);
302
	test_undef(3);
303
	test_undef(2);
304

  
305
	bt_assert(defined(1));
306
	bt_assert(defined(1.2.3.4));
278 307
}
279 308

  
309
bt_test_suite(t_define, "Testing defined() function");
280 310

  
281
function test_pxset(prefix set pxs)
311

  
312

  
313

  
314
/*
315
 * 	Testing quads
316
 * 	-------------
317
 */
318

  
319
function t_quad()
320
quad qq;
282 321
{
283
	print pxs;
284
	print "  must be true:  ",	net10  ~ pxs, ",", 10.0.0.0/10  ~ pxs, ",", 10.0.0.0/12 ~ pxs, ",",
285
					20.0.0.0/24 ~ pxs, ",", 20.0.40.0/24 ~ pxs, ",", 20.0.0.0/26 ~ pxs, ",",
286
					20.0.100.0/26 ~ pxs, ",", 20.0.0.0/28 ~ pxs, ",", 20.0.255.0/28 ~ pxs;
287
	print "  must be false: ",	10.0.0.0/7 ~ pxs,  ",", 10.0.0.0/13 ~ pxs, ",", 10.0.0.0/16 ~ pxs, ",",
288
					20.0.0.0/16 ~ pxs, ",", 20.0.0.0/23 ~ pxs, ",", 20.0.0.0/29 ~ pxs, ",",
289
					11.0.0.0/10 ~ pxs, ",", 20.1.0.0/26 ~ pxs;
322
	qq = 1.2.3.4;
323
	print "Testinq quad: 1.2.3.4 = ", qq;
324
	bt_assert(qq = 1.2.3.4);
325
	bt_assert(qq != 4.3.2.1);
290 326
}
291 327

  
292
function test_undef(int a)
293
int b;
328
bt_test_suite(t_quad, "Testing quads");
329

  
330

  
331

  
332

  
333
/*
334
 * 	Testing sets of quads
335
 * 	---------------------
336
 */
337

  
338
function t_quad_set()
339
quad qq;
294 340
{
295
	if a = 3
296
	then b = 4;
297
	print "Defined: ", a, " ", b, " ", defined(b);
341
	qq = 1.2.3.4;
342
	bt_assert(qq ~ [1.2.3.4, 5.6.7.8]);
343
	bt_assert(qq !~ [1.2.1.1, 1.2.3.5]);
298 344
}
299 345

  
300
define is1 = [ one, (2+1), (6-one), 8, 11, 15, 17, 19];
301
define is2 = [(17+2), 17, 15, 11, 8, 5, 3, 2];
302
define is3 = [5, 17, 2, 11, 8, 15, 3, 19];
346
bt_test_suite(t_quad_set, "Testing sets of quads");
303 347

  
304
define pxs2 = [ 10.0.0.0/16{8,12}, 20.0.0.0/16{24,28} ];
348

  
349

  
350

  
351
/*
352
 * 	Testing Extended Communities
353
 * 	----------------------------
354
 */
355

  
356
function t_ec()
357
ec cc;
358
{
359
	cc = (rt, 12345, 200000);
360
	print "Testing EC: ", cc;
361

  
362
	bt_assert(cc = (rt, 12345, 200000));
363
	bt_assert(cc < (rt, 12345, 200010));
364
	bt_assert(cc != (rt, 12346, 200000));
365
	bt_assert(cc != (ro, 12345, 200000));
366
	bt_assert(!(cc > (rt, 12345, 200010)));
367

  
368
	print "Testing EC: (ro, 100000, 20000) = ", (ro, 100000, 20000);
369
}
370

  
371
bt_test_suite(t_ec, "Testing Extended Communities");
372

  
373

  
374

  
375

  
376
/*
377
 * 	Testing sets of Extended Communities
378
 * 	------------------------------------
379
 */
305 380

  
306 381
define ecs2 = [(rt, ten, (one+onef(0))*10), (ro, 100000, 100..200), (rt, 12345, *)];
307 382

  
383
function t_ec_set()
384
ec set ecs;
385
{
386
	ecs = [(rt, ten, (one+onef(0))*10), (ro, 100000, 100..200), (rt, 12345, *)];
387
	print "EC set (rt, 10, 20) (ro, 100000, 100..200), (rt, 12345, *):";
388
	print ecs;
389
	print ecs2;
390

  
391
	bt_assert((rt, 10, 20) ~ ecs);
392
	bt_assert((ro, 100000, 100) ~ ecs);
393
	bt_assert((ro, 100000, 128) ~ ecs);
394
	bt_assert((ro, 100000, 200) ~ ecs);
395
	bt_assert((rt, 12345, 0) ~ ecs);
396
	bt_assert((rt, 12345, 200000) ~ ecs);
397
	bt_assert((rt, 12345, 4000000) ~ ecs);
398
	bt_assert((ro, 10, 20) !~ ecs);
399
	bt_assert((rt, 10, 21) !~ ecs);
400
	bt_assert((ro, 100000, 99) !~ ecs);
401
	bt_assert((ro, 12345, 10) !~ ecs);
402
	bt_assert((rt, 12346, 0) !~ ecs);
403
	bt_assert((ro, 0.1.134.160, 150) !~ ecs);
404
}
405

  
406
bt_test_suite(t_ec_set, "Testing sets of Extended Communities");
407

  
408

  
409

  
410

  
411
/*
412
 *	Testing integers
413
 *	----------------
414
 */
415

  
416
define four = 4;
417
define xyzzy = (120+10);
418
define '1a-a1' = (xyzzy-100);
308 419

  
309
function __startup() 
420
function t_int()
310 421
int i;
311
bool b;
312
prefix px;
313
ip p;
314
pair pp;
315
quad qq;
316
ec cc;
422
{
423
	bt_assert(xyzzy = 130);
424
	bt_assert('1a-a1' = 30);
425

  
426
	i = four;
427
	i = 12*100 + 60/2 + i;
428
	i = (i + 0);
429
	bt_assert(i = 1234);
430

  
431
	if (i = 4) then
432
		bt_assert(false);
433
	else
434
		bt_assert(true);
435

  
436
	if !(i = 3) then
437
		bt_assert(true);
438
	else
439
		bt_assert(false);
440

  
441
	if 1234 = i then
442
		bt_assert(true);
443
	else
444
		bt_assert(false);
445

  
446
	if 1 <= 1 then
447
		bt_assert(true);
448
	else
449
		bt_assert(false);
450

  
451
	if 1234 < 1234 then
452
		bt_assert(false);
453
	else
454
		bt_assert(true);
455

  
456
	i = 4200000000;
457
	bt_assert(i = 4200000000);
458
	bt_assert(i > 4100000000);
459
	bt_assert(!(i > 4250000000));
460

  
461
	bt_assert(1 = 1);
462
	bt_assert(!(1 != 1));
463

  
464
	bt_assert(1 != 2);
465
	bt_assert(1 <= 2);
466

  
467
	bt_assert(1 != "a");
468
	bt_assert(1 != (0,1));
469
}
470

  
471
bt_test_suite(t_int, "Testing integers");
472

  
473

  
474

  
475

  
476
/*
477
 * 	Testing sets of integers
478
 * 	------------------------
479
 */
480

  
481
define is1 = [ one, (2+1), (6-one), 8, 11, 15, 17, 19];
482
define is2 = [(17+2), 17, 15, 11, 8, 5, 3, 2];
483
define is3 = [5, 17, 2, 11, 8, 15, 3, 19];
484

  
485
function t_int_set()
317 486
int set is;
318
pair set ps;
319
ec set ecs;
320
ip set ips;
321
prefix set pxs;
322
string st;
323 487
{
324
	print "1a-a1 = 30: ", '1a-a1'; 
325
	print "Testing filter language:";
326
	i = four; 
327
	i = 12*100 + 60/2 + i; 
328
	i = ( i + 0 );
329
	print "  arithmetics: 1234 = ", i;
330
	printn "  if statements ";
331
	print "what happens here?";
332
	printn ".";
333
	if (i = 4) then { print "*** FAIL: if 0"; quitbird; } else printn ".";
334
#	if !(i = 3) then { print "*** FAIL: if 0"; quitbird; } else printn ".";
335
	if 1234 = i then printn "."; else { print "*** FAIL: if 1 else"; }
336
#	if 1 <= 1 then printn "."; else { print "*** FAIL: test 3"; }
337
	if 1234 < 1234 then { print "*** FAIL: test 4"; quitbird; } else print "ok";
488
	bt_assert(1 ~ [1,2,3]);
489
	bt_assert(5 ~ [1..20]);
490
	bt_assert(2 ~ [ 1, 2, 3 ]);
491
	bt_assert(5 ~ [ 4 .. 7 ]);
492
	bt_assert(1 !~ [ 2, 3, 4 ]);
493

  
338 494
	is = [ 2, 3, 4, 7..11 ];
495
	bt_assert(10 ~ is);
496
	bt_assert(5 !~ is);
497

  
498
	bt_assert(1 ~ is1);
499
	bt_assert(3 ~ is1);
500
	bt_assert(5 ~ is1);
501
	bt_assert((one+2) ~ is1);
502
	bt_assert(2 ~ is2);
503
	bt_assert(2 ~ is3);
504
	bt_assert(4 !~ is1);
505
	bt_assert(4 !~ is2);
506
	bt_assert(4 !~ is3);
507
	bt_assert(10 !~ is1);
508
	bt_assert(10 !~ is2);
509
	bt_assert(10 !~ is3);
510
	bt_assert(15 ~ is1);
511
	bt_assert(15 ~ is2);
512
	bt_assert(15 ~ is3);
513
	bt_assert(18 !~ is1);
514
	bt_assert(18 !~ is2);
515
	bt_assert(18 !~ is3);
516
	bt_assert(19 ~ is1);
517
	bt_assert(19 ~ is2);
518
	bt_assert(19 ~ is3);
519
	bt_assert(20 !~ is1);
520
	bt_assert(20 !~ is2);
521
	bt_assert(20 !~ is3);
522

  
523
	bt_assert([1,2] != [1,3]);
524
	bt_assert([1,4..10,20] = [1,4..10,20]);
339 525

  
340
	print "must be true:  ", 1 = 1, " ", 1 != (0,1), " ", 1 != "a", " ", +empty+ = +empty+, " ", -empty- = -empty-, " ", --empty-- = --empty-- ,
341
			" ", [1,4..10,20] = [1,4..10,20] , " ", [ 10.0.0.0/8{ 15 , 17 } ] = [ 10.0.0.0/8{ 15 , 17 } ];
342
	print "must be false: ", 1 != 1, " ", 1 = (0,1), " ", 1 = "a", " ", +empty+ = -empty-, " ", -empty- = --empty--, " ", --empty-- = +empty+ ,
343
			" ", [1,2] = [1,3], " ", [ 10.0.0.0/8{ 15 , 17 } ] = [ 11.0.0.0/8{ 15 , 17 } ];
526
	print "What will this do? ", [ 1, 2, 1, 1, 1, 3, 4, 1, 1, 1, 5 ];
527
}
344 528

  
345
	print "  must be true: ", 1.2.0.0/16 ~ [ 1.0.0.0/8{ 15 , 17 } ];
346
	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+ ];
347
	print "  must be true: ", true && true, ",", true || false, ",", ! false && ! false && true, ",", 1 < 2 && 1 != 3, ",", true && true && ! false, ",", true || 1+"a", ",", !(false && 1+"a");
529
bt_test_suite(t_int_set, "Testing sets of integers");
348 530

  
349
	print "  must be true: ", defined(1), ",", defined(1.2.3.4), ",", 1 != 2, ",", 1 <= 2;
350
	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;
351 531

  
352 532

  
353
	print "  must be true:  ", 1 ~ is1, "  ", 3 ~ is1, "  ", 5 ~ is1;
354
	print "  must be true:  ", (one+2) ~ is1, "  ", 2 ~ is2, "  ", 2 ~ is3;
355
	print "  must be false: ", 4 ~ is1, " ", 4 ~ is2, " ", 4 ~ is3;
356
	print "  must be false: ", 10 ~ is1, " ", 10 ~ is2, " ", 10 ~ is3;
357
	print "  must be true:  ", 15 ~ is1, "  ", 15 ~ is2, "  ", 15 ~ is3;
358
	print "  must be false: ", 18 ~ is1, " ", 18 ~ is2, " ", 18 ~ is3;
359
	print "  must be true:  ", 19 ~ is1, "  ", 19 ~ is2, "  ", 19 ~ is3;
360
	print "  must be false: ", 20 ~ is1, " ", 20 ~ is2, " ", 20 ~ is3;
361 533

  
362
	px = 1.2.0.0/18;
363
	print "Testing prefixes: 1.2.0.0/18 = ", px;
364
	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, " ", netdoc ~ 2001::/16;
365
	print "  must be false: ",	192.168.0.0/15 ~ 192.168.0.0/16, " ", 192.160.0.0/17 ~ 192.168.0.0/16, " ", px ~ netdoc;
534
/*
535
 * 	Testing ip address
536
 * 	------------------
537
 */
538

  
539
define onetwo = 1.2.3.4;
366 540

  
541
function t_ip()
542
ip p;
543
{
367 544
	p = 127.1.2.3;
368
	print "Testing mask : 127.0.0.0 = ", p.mask(8);
545
	bt_assert(p.mask(8) = 127.0.0.0);
546
	bt_assert(1.2.3.4 = 1.2.3.4);
547
	bt_assert(1.2.3.4 = onetwo);
548
	print "1.2.3.4 = ", onetwo;
549
}
550

  
551
bt_test_suite(t_ip, "Testing ip address");
552

  
553

  
554

  
555

  
556
/*
557
 * 	Testing sets of ip address
558
 * 	--------------------------
559
 */
560

  
561
function t_ip_set()
562
{
563
	bt_assert(1.2.3.4 !~ [ 1.2.3.3, 1.2.3.5 ]);
564
	bt_assert(1.2.3.4 ~ [ 1.2.3.3..1.2.3.5 ]);
565
}
566

  
567
bt_test_suite(t_ip_set, "Testing sets of ip address");
568

  
569

  
369 570

  
571

  
572
/*
573
 *	Testing enums
574
 *	-------------
575
 */
576

  
577
function t_enum()
578
{
579
	print "Testing enums: ", RTS_DUMMY, " ", RTS_STATIC;
580
	bt_assert(RTS_STATIC ~ [RTS_STATIC, RTS_DEVICE]);
581
	bt_assert(RTS_BGP !~ [RTS_STATIC, RTS_DEVICE]);
582
}
583

  
584
bt_test_suite(t_enum, "Testing enums");
585

  
586

  
587

  
588

  
589
/*
590
 * 	Testing pairs
591
 * 	-------------
592
 */
593

  
594
function 'mkpair-a'(int a)
595
{
596
	return (1, a);
597
}
598

  
599
function t_pair()
600
pair pp;
601
{
370 602
	pp = (1, 2);
371
	print "Testing pairs: (1,2) = ", (1,2), " = ", pp, " = ", (1,1+1), " = ", 'mkpair-a'(2);
372
	print "  must be true:  ", (1,2) = (1,1+1);
373
	print "Testing enums: ", RTS_DUMMY, " ", RTS_STATIC, " ",
374
		", true: ", RTS_STATIC ~ [RTS_STATIC, RTS_DEVICE],
375
		", false: ", RTS_BGP ~ [RTS_STATIC, RTS_DEVICE];
603
	bt_assert((1,2) = pp);
604
	bt_assert((1,1+1) = pp);
605
	bt_assert('mkpair-a'(2) = pp);
606
	bt_assert((1,2) = (1,1+1));
607
	bt_assert(((1,2) < (2,2)));
608
	bt_assert(!((1,1) > (1,1)));
609
}
610

  
611
bt_test_suite(t_pair, "Testing pairs");
612

  
613

  
376 614

  
615

  
616
/*
617
 * 	Testing sets of pairs
618
 * 	---------------------
619
 */
620

  
621
function t_pair_set()
622
pair pp;
623
pair set ps;
624
{
625
	pp = (1, 2);
377 626
	ps = [(1,(one+one)), (3,4)..(4,8), (5,*), (6,3..6)];
378 627
	print "Pair set: ", ps;
379
	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;
380
	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;
628
	bt_assert(pp ~ ps);
629
	bt_assert((3,5) ~ ps);
630
	bt_assert((4,1) ~ ps);
631
	bt_assert((5,4) ~ ps);
632
	bt_assert((5,65535) ~ ps);
633
	bt_assert((6,4) ~ ps);
634
	bt_assert((3, 10000) ~ ps);
635
	bt_assert((3,3) !~ ps);
636
	bt_assert((4,9) !~ ps);
637
	bt_assert((4,65535) !~ ps);
638
	bt_assert((6,2) !~ ps);
639
	bt_assert((6,6+one) !~ ps);
640
	bt_assert(((one+6),2) !~ ps);
641
	bt_assert((1,1) !~ ps);
381 642

  
382 643
	ps = [(20..150, 200..300), (50100..50200, 1000..50000), (*, 5+5)];
383
	print "Pair set: .. too long ..";
384
	print "Testing pair set, true:  ", (100,200) ~ ps,  "  ", (150,300) ~ ps, "  ", (50180,1200) ~ ps, "  ", (50110,49000) ~ ps, "  ", (0,10) ~ ps, "  ", (64000,10) ~ ps;
385
	print "Testing pair set, false: ", (20,199) ~ ps, " ", (151,250) ~ ps, " ", (50050,2000) ~ ps, " ", (50150,50050) ~ ps, " ", (10,9) ~ ps, " ", (65535,11) ~ ps ;
644
	print "Pair set: ", ps;
645
	bt_assert((100,200) ~ ps);
646
	bt_assert((150,300) ~ ps);
647
	bt_assert((50180,1200) ~ ps);
648
	bt_assert((50110,49000) ~ ps);
649
	bt_assert((0,10) ~ ps);
650
	bt_assert((64000,10) ~ ps);
651
	bt_assert((20,199) !~ ps);
652
	bt_assert((151,250) !~ ps);
653
	bt_assert((50050,2000) !~ ps);
654
	bt_assert((50150,50050) !~ ps);
655
	bt_assert((10,9) !~ ps);
656
	bt_assert((65535,11) !~ ps);
657
}
386 658

  
387
	qq = 1.2.3.4;
388
	print "Testinq quad: 1.2.3.4 = ", qq,
389
		", true: ", qq = 1.2.3.4, " ", qq ~ [1.2.3.4, 5.6.7.8],
390
		", false: ", qq = 4.3.2.1, " ", qq ~ [1.2.1.1, 1.2.3.5];
659
bt_test_suite(t_pair_set, "Testing sets of pairs");
391 660

  
392
	cc = (rt, 12345, 200000);
393
	print "Testing EC: (rt, 12345, 200000) = ", cc;
394
	print "Testing EC: (ro, 100000, 20000) = ", (ro, 100000, 20000);
395
	print "Testing EC: (rt, 10.20.30.40, 20000) = ", (rt, 10.20.30.40, 20000);
396
	print "  true: ", cc = (rt, 12345, 200000), " ", cc < (rt, 12345, 200010),
397
		", false: ", cc = (rt, 12346, 200000), " ", cc = (ro, 12345, 200000), " ",  cc > (rt, 12345, 200010);
398 661

  
399
	ecs = [(rt, ten, (one+onef(0))*10), (ro, 100000, 100..200), (rt, 12345, *)];
400
	print "EC set: ", ecs;
401
	print "EC set: ", ecs2;
402
	print "Testing EC set, true:  ",  (rt, 10, 20) ~ ecs, "  ", (ro, 100000, 100) ~ ecs, "  ", (ro, 100000, 200) ~ ecs,
403
		"  ", (rt, 12345, 0) ~ ecs, "  ", cc ~ ecs,  "  ", (rt, 12345, 4000000) ~ ecs;
404
	print "Testing EC set, false: ", (ro, 10, 20) ~ ecs, " ", (rt, 10, 21) ~ ecs, " ", (ro, 100000, 99) ~ ecs,
405
		" ", (ro, 12345, 10) ~ ecs, " ", (rt, 12346, 0) ~ ecs, " ", (ro, 0.1.134.160, 150) ~ ecs;
406 662

  
663

  
664
/*
665
 * 	Testing string matching
666
 * 	-----------------------
667
 */
668

  
669
function t_string()
670
string st;
671
{
407 672
	st = "Hello";
408
	print "Testing string: ", st, " true: ", st ~ "Hell*", " false: ", st ~ "ell*";
409
	
673
	print "Testing string: ", st;
674
	bt_assert(st ~ "Hell*");
675
	bt_assert(st ~ "?ello");
676
	bt_assert(st ~ "Hello");
677
	bt_assert(st ~ "Hell?");
678
	bt_assert(st !~ "ell*");
679
}
680

  
681
bt_test_suite(t_string, "Testing string matching");
682

  
683

  
684

  
685

  
686
/*
687
 * 	Testing boolean expressions
688
 * 	---------------------------
689
 */
690

  
691
function t_bool()
692
bool b;
693
{
410 694
	b = true;
411
	print "Testing bool: ", b, ", ", !b;
695
	bt_assert(b);
696
	bt_assert(!!b);
697

  
698
	if ( b = true ) then
699
		bt_assert(b);
700
	else
701
		bt_assert(false);
702

  
703
	bt_assert(true && true);
704
	bt_assert(true || false);
705
	bt_assert(! false && ! false && true);
706
	bt_assert(1 < 2 && 1 != 3);
707
	bt_assert(true && true && ! false);
708
	bt_assert(true || 1+"a");
709
	bt_assert(!(false && 1+"a"));
710
	bt_assert(!(true && false));
711
}
412 712

  
413
	if ( b = true ) then print "Testing bool comparison b = true: ", b;
414
	else { print "*** FAIL: TRUE test failed" ; quitbird; }
415
	
416
	ips = [ 1.1.1.0 .. 1.1.1.255, ip1222];
417
	print "Testing IP sets: ";
418
	print ips;
419
	print "  must be true:  ",	1.1.1.0 ~ ips, ",", 1.1.1.100 ~ ips, ",", 1.2.2.2 ~ ips;
420
	print "  must be false: ",	1.1.0.255 ~ ips, ",", 1.1.2.0  ~ ips, ",", 1.2.2.3 ~ ips, ",", 192.168.1.1 ~ ips;
713
bt_test_suite(t_bool, "Testing boolean expressions");
421 714

  
422
	pxs = [ 1.2.0.0/16, 1.4.0.0/16+];
423
	print "Testing prefix sets: ";
715

  
716

  
717

  
718
/*
719
 * 	Testing prefixes
720
 * 	----------------
721
 */
722

  
723
define netdoc = 2001:db8::/32;
724

  
725
function t_prefix()
726
prefix px;
727
{
728
	px = 1.2.0.0/18;
729
	print "Testing prefixes: 1.2.0.0/18 = ", px;
730
	bt_assert(192.168.0.0/16 ~ 192.168.0.0/16);
731
	bt_assert(192.168.0.0/17 ~ 192.168.0.0/16);
732
	bt_assert(192.168.254.0/24 ~ 192.168.0.0/16);
733
	bt_assert(netdoc ~ 2001::/16);
734
	bt_assert(192.168.0.0/15 !~ 192.168.0.0/16);
735
	bt_assert(192.160.0.0/17 !~ 192.168.0.0/16);
736
	bt_assert(px !~ netdoc);
737

  
738
	bt_assert(1.2.3.4 ~ 1.0.0.0/8);
739
	bt_assert(1.0.0.0/8 ~ 1.0.0.0/8);
740
}
741

  
742
bt_test_suite(t_prefix, "Testing prefixes");
743

  
744

  
745

  
746

  
747
/*
748
 *	Testing prefix sets
749
 *	-------------------
750
 */
751

  
752
define net10 = 10.0.0.0/8;
753
define pxs2 = [ 10.0.0.0/16{8,12}, 20.0.0.0/16{24,28} ];
754

  
755
function test_pxset(prefix set pxs)
756
{
424 757
	print pxs;
425
	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;
426
	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, ",",
427
					1.2.0.0/32 ~ pxs, ",", 1.4.0.0/15 ~ pxs;
758

  
759
	bt_assert(net10  ~ pxs);
760
	bt_assert(10.0.0.0/10  ~ pxs);
761
	bt_assert(10.0.0.0/12 ~ pxs);
762
	bt_assert(20.0.0.0/24 ~ pxs);
763
	bt_assert(20.0.40.0/24 ~ pxs);
764
	bt_assert(20.0.0.0/26 ~ pxs);
765
	bt_assert(20.0.100.0/26 ~ pxs);
766
	bt_assert(20.0.0.0/28 ~ pxs);
767
	bt_assert(20.0.255.0/28 ~ pxs);
768

  
769
	bt_assert(10.0.0.0/7 !~ pxs);
770
	bt_assert(10.0.0.0/13 !~ pxs);
771
	bt_assert(10.0.0.0/16 !~ pxs);
772
	bt_assert(20.0.0.0/16 !~ pxs);
773
	bt_assert(20.0.0.0/23 !~ pxs);
774
	bt_assert(20.0.0.0/29 !~ pxs);
775
	bt_assert(11.0.0.0/10 !~ pxs);
776
	bt_assert(20.1.0.0/26 !~ pxs);
777

  
778
	bt_assert(1.0.0.0/8 ~ [ 1.0.0.0/8+ ]);
779
	bt_assert(1.0.0.0/9 !~ [ 1.0.0.0/8- ]);
780
	bt_assert(1.2.0.0/17 !~ [ 1.0.0.0/8{ 15 , 16 } ]);
781

  
782
	bt_assert([ 10.0.0.0/8{ 15 , 17 } ] = [ 10.0.0.0/8{ 15 , 17 } ]);
783
}
784

  
785
function t_prefix_sets()
786
prefix set pxs;
787
{
788
	pxs = [ 1.2.0.0/16, 1.4.0.0/16+];
789
	print "Testing prefix sets: ", pxs;
790
	bt_assert(1.2.0.0/16 ~ pxs);
791
	bt_assert(1.4.0.0/16 ~ pxs);
792
	bt_assert(1.4.0.0/18 ~ pxs);
793
	bt_assert(1.4.0.0/32 ~ pxs);
794
	bt_assert(1.1.0.0/16 !~ pxs);
795
	bt_assert(1.3.0.0/16 !~ pxs);
796
	bt_assert(1.2.0.0/15 !~ pxs);
797
	bt_assert(1.2.0.0/17 !~ pxs);
798
	bt_assert(1.2.0.0/32 !~ pxs);
799
	bt_assert(1.4.0.0/15 !~ pxs);
428 800

  
429 801
	test_pxset(pxs2);
430 802
	test_pxset([ 10.0.0.0/16{8,12}, 20.0.0.0/16{24,28} ]);
431
	print "What will this do? ", [ 1, 2, 1, 1, 1, 3, 4, 1, 1, 1, 5 ];
432 803

  
433
	print "Testing functions...";
434
	callme ( 1, 2 );
435
	callme ( 2, 2 );
436
	callme ( 2, 2 );
437
	callme ( 3, 2 );
438
	callme ( 4, 4 );
439
	callme ( 7, 2 );
804
	bt_assert(1.2.0.0/16 ~ [ 1.0.0.0/8{ 15 , 17 } ]);
805
	bt_assert([ 10.0.0.0/8{ 15 , 17 } ] != [ 11.0.0.0/8{ 15 , 17 } ]);
806
}
440 807

  
441
	i = fifteen();
442
	print "Testing function calls: 15 = ", i;
808
bt_test_suite(t_prefix_sets, "Testing prefix sets");
443 809

  
444
	path_test();
445 810

  
446
	print "1.2.3.4 = ", onetwo;
447 811

  
448
	i = 4200000000;
449
	print "4200000000 = ", i, "    true: ", i = 4200000000, " ", i > 4100000000, "   false: ", i > 4250000000;
450 812

  
451
	test_undef(2);
452
	test_undef(3);
453
	test_undef(2);
813
/*
814
 * 	Testing IP sets
815
 * 	---------------
816
 */
454 817

  
455
	print "Testing include";
456
	include "test.conf.inc";
818
define ip1222 = 1.2.2.2;
457 819

  
458
	print "done";
459
	quitbird;
460
#	print "*** FAIL: this is unreachable"; 
820
function t_ip_sets()
821
ip set ips;
822
{
823
	ips = [ 1.1.1.0 .. 1.1.1.255, ip1222];
824
	print "Testing IP sets: ", ips;
825
	bt_assert(1.1.1.0 ~ ips);
826
	bt_assert(1.1.1.100 ~ ips);
827
	bt_assert(1.2.2.2 ~ ips);
828
	bt_assert(1.1.0.255 !~ ips);
829
	bt_assert(1.1.2.0  !~ ips);
830
	bt_assert(1.2.2.3 !~ ips);
831
	bt_assert(192.168.1.1 !~ ips);
461 832
}
462 833

  
463
filter testf 
464
int j; 
465
{ 
834
bt_test_suite(t_ip_sets, "Testing IP sets");
835

  
836

  
837

  
838

  
839
/*
840
 *	 Testing calling functions
841
 *	 -------------------------
842
 */
843

  
844
function callme(int arg1; int arg2)
845
int i;
846
{
847
	case arg1 {
848
	1, 42: return 42;
849
	else: return arg1 * arg2;
850
	}
851

  
852
	return 0;
853
}
854

  
855
function fifteen()
856
{
857
	return 15;
858
}
859

  
860
function t_call_function()
861
{
862
	bt_assert(fifteen() = 15);
863

  
864
	bt_assert(callme(1, 2) = 42);
865
	bt_assert(callme(42, 2) = 42);
866

  
867
	bt_assert(callme(2, 2) = 4);
868
	bt_assert(callme(3, 2) = 6);
869
	bt_assert(callme(4, 4) = 16);
870
	bt_assert(callme(7, 2) = 14);
871
}
872

  
873
bt_test_suite(t_call_function, "Testing calling functions");
874

  
875

  
876

  
877

  
878
/*
879
 * 	Test including another config file
880
 * 	----------------------------------
881
 */
882

  
883
function t_include()
884
int i;
885
{
886
  print "Testing include";
887
  i = 1;
888
  include "test.conf.inc";
889
  bt_assert(i = 42);
890
}
891

  
892
bt_test_suite(t_include, "Test including another config file");
893

  
894

  
895

  
896

  
897
/*
898
 *	Unused functions -- testing only parsing
899
 *	----------------------------------------
900
 */
901

  
902
function __test1()
903
{
904
	if source ~ [ RTS_BGP, RTS_STATIC ] then {
905
#		ospf_metric1 = 65535;
906
#		ospf_metric2 = 1000;
907
		ospf_tag = 0x12345678;
908
		accept;
909
	}
910
	reject;
911
}
912

  
913
function __test2()
914
{
915
	if source ~ [ RTS_BGP, RTS_STATIC ] then {
916
#		ospf_metric1 = 65535;
917
#		ospf_metric2 = 1000;
918
		ospf_tag = 0x12345678;
919
		accept;
920
	}
921
	reject;
922
}
923

  
924
filter testf
925
int j;
926
{
466 927
	print "Heya, filtering route to ", net.ip, " prefixlen ", net.len, " source ", source;
467 928
	print "This route was from ", from;
468 929
	j = 7;
......
472 933
	}
473 934
	rip_metric = 14;
474 935
	unset(rip_metric);
475
		
936

  
476 937
	accept "ok I take that";
477 938
}
478 939

  
479
eval __startup();
940
/*
941
roa table rl
942
{
943
	roa 10.110.0.0/16 max 16 as 1000;
944
	roa 10.120.0.0/16 max 24 as 1000;
945
	roa 10.130.0.0/16 max 24 as 2000;
946
	roa 10.130.128.0/18 max 24 as 3000;
947
}
948

  
949
function test_roa()
950
{
951
	# cannot be tested in __startup(), sorry
952
	print "Testing ROA";
953
	print "Should be true: ", roa_check(rl, 10.10.0.0/16, 1000) = ROA_UNKNOWN,
954
	      " ", roa_check(rl, 10.0.0.0/8, 1000) = ROA_UNKNOWN,
955
	      " ", roa_check(rl, 10.110.0.0/16, 1000) = ROA_VALID,
956
	      " ", roa_check(rl, 10.110.0.0/16, 2000) = ROA_INVALID,
957
	      " ", roa_check(rl, 10.110.32.0/20, 1000) = ROA_INVALID,
958
	      " ", roa_check(rl, 10.120.32.0/20, 1000) = ROA_VALID;
959
	print "Should be true: ", roa_check(rl, 10.120.32.0/20, 2000) = ROA_INVALID,
960
	      " ", roa_check(rl, 10.120.32.32/28, 1000) = ROA_INVALID,
961
	      " ", roa_check(rl, 10.130.130.0/24, 1000) = ROA_INVALID,
962
	      " ", roa_check(rl, 10.130.130.0/24, 2000) = ROA_VALID,
963
	      " ", roa_check(rl, 10.130.30.0/24, 3000) = ROA_INVALID,
964
	      " ", roa_check(rl, 10.130.130.0/24, 3000) = ROA_VALID;
965
}
966
*/

Also available in: Unified diff