Revision 9b0a0ba9

View differences:

Makefile.in
22 22

  
23 23
client=$(addprefix $(exedir)/,@CLIENT@)
24 24
daemon=$(exedir)/bird
25
protocols  = @protocols@
25
protocols=@protocols@
26 26

  
27 27
prefix=@prefix@
28 28
exec_prefix=@exec_prefix@
......
49 49
endif
50 50

  
51 51
# Meta rules
52
cleangoals := clean distclean
53 52
docgoals := docs userdocs progdocs
54
.PHONY: all daemon cli $(cleangoals) $(docgoals) tags
53
testgoals := check test tests tests_run
54
cleangoals := clean distclean testsclean
55
.PHONY: all daemon cli $(docgoals) $(testgoals) $(cleangoals) tags
55 56
all: daemon cli
56 57

  
57 58
daemon: $(daemon)
58 59
cli: $(client)
59 60

  
60 61
# Include directories
61
dirs := client conf doc filter lib nest $(addprefix proto/,$(protocols)) @sysdep_dirs@
62
dirs := client conf doc filter lib nest test $(addprefix proto/,$(protocols)) @sysdep_dirs@
62 63

  
63 64
conf-y-targets := $(addprefix $(objdir)/conf/,cf-parse.y keywords.h commands.h)
64 65
cf-local = $(conf-y-targets): $(s)config.Y
65 66

  
66 67
src-o-files = $(patsubst %.c,$(o)%.o,$(src))
68
tests-target-files = $(patsubst %.c,$(o)%,$(tests_src))
67 69

  
68
all-daemon = $(exedir)/bird: $(obj)
69
all-client = $(exedir)/birdc $(exedir)/birdcl: $(obj)
70
all-daemon = $(daemon): $(obj)
71
all-client = $(client): $(obj)
70 72

  
71 73
s = $(dir $(lastword $(MAKEFILE_LIST)))
72 74
ifeq ($(srcdir),.)
......
109 111
	echo >>$@ "#define PATH_CONTROL_SOCKET \"@CONTROL_SOCKET@\""
110 112
	if test -n "@iproutedir@" ; then echo >>$@ "#define PATH_IPROUTE_DIR \"@iproutedir@\"" ; fi
111 113

  
114
# Unit tests rules
115

  
116
tests_targets_ok = $(addsuffix .ok,$(tests_targets))
117

  
118
$(tests_targets): %: %.o $(tests_objs)
119
	$(E)echo LD $(LDFLAGS) -o $@ $^ $(LIBS)
120
	$(Q)$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
121

  
122
$(tests_targets_ok): %.ok: %
123
	$(Q)$* 2>/dev/null && touch $*.ok
124

  
125
test: testsclean check
126
check: tests tests_run
127
tests: $(tests_targets)
128
tests_run: $(tests_targets_ok)
129

  
112 130
# Finally include the computed dependencies
113 131

  
114 132
ifneq ($(filter-out $(cleangoals),$(MAKECMDGOALS)),)
......
147 165
	rm -f $(addprefix $(exedir)/,bird birdc birdcl)
148 166
	find $(objdir) -name "*.[od]" -exec rm -f '{}' '+'
149 167

  
168
testsclean:
169
	rm -f $(tests_targets_ok)
170

  
150 171
ifeq ($(objdir),obj)
151 172
distclean: clean
152 173
	rm -rf $(objdir)
conf/Makefile
3 3

  
4 4
$(all-daemon)
5 5

  
6
tests_objs := $(tests_objs) $(src-o-files)
7

  
6 8
ifdef DEBUG
7 9
BISON_DEBUG=-t
8 10
#FLEX_DEBUG=-d
conf/conf.c
49 49
#include "nest/route.h"
50 50
#include "nest/protocol.h"
51 51
#include "nest/iface.h"
52
#include "lib/resource.h"
53
#include "lib/string.h"
54 52
#include "lib/event.h"
55 53
#include "sysdep/unix/timer.h"
56 54
#include "conf/conf.h"
57
#include "filter/filter.h"
55

  
58 56

  
59 57
static jmp_buf conf_jmpbuf;
60 58

  
......
85 83
 * further use. Returns a pointer to the structure.
86 84
 */
87 85
struct config *
88
config_alloc(const byte *name)
86
config_alloc(const char *name)
89 87
{
90 88
  pool *p = rp_new(&root_pool, "Config");
91 89
  linpool *l = lp_new(p, 4080);
......
96 94
  char *ndup = lp_allocu(l, nlen);
97 95
  memcpy(ndup, name, nlen);
98 96

  
97
  init_list(&c->tests);
99 98
  c->mrtdump_file = -1; /* Hack, this should be sysdep-specific */
100 99
  c->pool = p;
101 100
  c->mem = l;
conf/conf.h
9 9
#ifndef _BIRD_CONF_H_
10 10
#define _BIRD_CONF_H_
11 11

  
12
#include "sysdep/config.h"
13
#include "lib/ip.h"
12 14
#include "lib/resource.h"
13 15
#include "sysdep/unix/timer.h"
14 16

  
......
21 23
  list protos;				/* Configured protocol instances (struct proto_config) */
22 24
  list tables;				/* Configured routing tables (struct rtable_config) */
23 25
  list logfiles;			/* Configured log files (sysdep) */
26
  list tests;				/* Configured unit tests */
24 27

  
25 28
  int mrtdump_file;			/* Configured MRTDump file (sysdep, fd in unix) */
26 29
  char *syslog_name;			/* Name used for syslog (NULL -> no syslog) */
......
60 63
extern struct config *config;		/* Currently active configuration */
61 64
extern struct config *new_config;	/* Configuration being parsed */
62 65

  
63
struct config *config_alloc(const byte *name);
66
struct config *config_alloc(const char *name);
64 67
int config_parse(struct config *);
65 68
int cli_parse(struct config *);
66 69
void config_free(struct config *);
......
161 164

  
162 165
/* Parser */
163 166

  
167
extern char *cf_text;
164 168
int cf_parse(void);
165 169

  
166 170
/* Sysdep hooks */
configure.in
56 56
	bird_cflags_default=yes
57 57
fi
58 58

  
59
AC_PROG_CC
59
AC_PROG_CC_C99
60 60
if test -z "$GCC" ; then
61 61
	AC_MSG_ERROR([This program requires the GNU C Compiler.])
62 62
fi
......
220 220

  
221 221
if test "$enable_debug" = yes ; then
222 222
	AC_DEFINE(DEBUGGING)
223
	AC_CHECK_HEADER(execinfo.h, [AC_SEARCH_LIBS([backtrace, backtrace_symbols], [c execinfo], [AC_DEFINE(HAVE_EXECINFO_H)])])
224
	LDFLAGS="$LDFLAGS -rdynamic"
225
	CFLAGS="$CFLAGS -O0 -ggdb -g3 -gdwarf-4"
223 226
	if test "$enable_memcheck" = yes ; then
224 227
		AC_CHECK_LIB(dmalloc, dmalloc_debug)
225 228
		if test $ac_cv_lib_dmalloc_dmalloc_debug != yes ; then
doc/bird.sgml
1253 1253
	<cf/!&tilde;/ membership operators) can be used to modify or test
1254 1254
	eclists, with ECs instead of pairs as arguments.
1255 1255

  
1256
	<tag/lclist/
1256
	<tag><label id="type-lclist">lclist/</tag>
1257 1257
	Lclist is a data type used for BGP large community lists. Like eclists,
1258 1258
	lclists are very similar to clists, but they are sets of LCs instead of
1259 1259
	pairs. The same operations (like <cf/add/, <cf/delete/ or <cf/&tilde;/
filter/Makefile
2 2
obj := $(src-o-files)
3 3
$(all-daemon)
4 4
$(cf-local)
5

  
6
tests_src := tree_test.c filter_test.c trie_test.c
7
tests_targets := $(tests_targets) $(tests-target-files)
8
tests_objs := $(tests_objs) $(src-o-files)
filter/config.Y
323 323
  return rv;
324 324
}
325 325

  
326
/*
327
 * Remove all new lines and doubled whitespaces
328
 * and convert all tabulators to spaces
329
 * and return a copy of string
330
 */
331
char *
332
assert_copy_expr(const char *start, size_t len)
333
{
334
  /* XXX: Allocates maybe a little more memory than we really finally need */
335
  char *str = cfg_alloc(len + 1);
336

  
337
  char *dst = str;
338
  const char *src = start - 1;
339
  const char *end = start + len;
340
  while (++src < end)
341
  {
342
    if (*src == '\n')
343
      continue;
344

  
345
    /* Skip doubled whitespaces */
346
    if (src != start)
347
    {
348
      const char *prev = src - 1;
349
      if ((*src == ' ' || *src == '\t') && (*prev == ' ' || *prev == '\t'))
350
	continue;
351
    }
352

  
353
    if (*src == '\t')
354
      *dst = ' ';
355
    else
356
      *dst = *src;
357

  
358
    dst++;
359
  }
360
  *dst = '\0';
361

  
362
  return str;
363
}
364

  
365
/*
366
 * assert_done - create f_instruction of bt_assert
367
 * @expr: expression in bt_assert()
368
 * @start: pointer to first char of test expression
369
 * @end: pointer to the last char of test expression
370
 */
371
static struct f_inst *
372
assert_done(struct f_inst *expr, const char *start, const char *end)
373
{
374
  struct f_inst *i;
375
  i = f_new_inst();
376
  i->code = P('a','s');
377
  i->a1.p = expr;
378

  
379
  if (end >= start)
380
  {
381
    i->a2.p = assert_copy_expr(start, end - start + 1);
382
  }
383
  else
384
  {
385
    /* this is a break of lexer buffer */
386
    i->a2.p = "???";
387
  }
326 388

  
389
  return i;
390
}
327 391

  
328 392
CF_DECLS
329 393

  
......
341 405
	ADD, DELETE, CONTAINS, RESET,
342 406
	PREPEND, FIRST, LAST, LAST_NONAGGREGATED, MATCH,
343 407
	EMPTY,
344
	FILTER, WHERE, EVAL)
408
	FILTER, WHERE, EVAL,
409
	BT_ASSERT, BT_TEST_SUITE)
345 410

  
346 411
%nonassoc THEN
347 412
%nonassoc ELSE
348 413

  
349
%type <x> term block cmds cmds_int cmd function_body constant constructor print_one print_list var_list var_listn dynamic_attr static_attr function_call symbol bgp_path_expr
414
%type <x> term block cmds cmds_int cmd function_body constant constructor print_one print_list var_list var_listn dynamic_attr static_attr function_call symbol bgp_path_expr bt_assert
350 415
%type <f> filter filter_body where_filter
351 416
%type <i> type break_command ec_kind
352 417
%type <i32> cnum
......
356 421
%type <px> fprefix
357 422
%type <s> decls declsn one_decl function_params
358 423
%type <h> bgp_path bgp_path_tail1 bgp_path_tail2
424
%type <t> get_cf_position
359 425

  
360 426
CF_GRAMMAR
361 427

  
......
375 441
   EVAL term { f_eval_int($2); }
376 442
 ;
377 443

  
444
CF_ADDTO(conf, bt_test_suite)
445
bt_test_suite:
446
 BT_TEST_SUITE '(' SYM ',' text ')' {
447
  if (!($3->class & SYM_FUNCTION))
448
    cf_error("Function expected");
449

  
450
  struct f_bt_test_suite *t = cfg_alloc(sizeof(struct f_bt_test_suite));
451
  t->fn = $3->def;
452
  t->fn_name = $3->name;
453
  t->dsc = $5;
454

  
455
  add_tail(&new_config->tests, &t->n);
456
 }
457
 ;
458

  
378 459
type:
379 460
   INT { $$ = T_INT; }
380 461
 | BOOL { $$ = T_BOOL; }
......
835 916
 | ROA_CHECK '(' rtable ')' { $$ = f_generate_roa_check($3, NULL, NULL); }
836 917
 | ROA_CHECK '(' rtable ',' term ',' term ')' { $$ = f_generate_roa_check($3, $5, $7); }
837 918

  
919
 | bt_assert { $$ = $1; }
920

  
838 921
/* | term '.' LEN { $$->code = P('P','l'); } */
839 922

  
840 923
/* function_call is inlined here */
......
966 1049
      $$->a1.p = $2;
967 1050
      $$->a2.p = build_tree( $4 );
968 1051
   }
1052
 | bt_assert ';' { $$ = $1; }
969 1053

  
970 1054

  
971 1055
 | rtadot dynamic_attr '.' EMPTY ';' { $$ = f_generate_empty($2); }
......
975 1059
 | rtadot dynamic_attr '.' FILTER '(' term ')' ';'    { $$ = f_generate_complex( P('C','a'), 'f', $2, $6 ); }
976 1060
 ;
977 1061

  
1062
 bt_assert:
1063
   BT_ASSERT '(' get_cf_position term get_cf_position ')' { $$ = assert_done($4, $3 + 1, $5 - 1); }
1064
 ;
1065

  
1066
get_cf_position:
1067
{
1068
  $$ = cf_text;
1069
};
1070

  
1071

  
978 1072
CF_END
filter/filter.c
52 52

  
53 53
#define CMP_ERROR 999
54 54

  
55
void (*bt_assert_hook)(int result, struct f_inst *assert);
56

  
55 57
static struct adata *
56 58
adata_empty(struct linpool *pool, int l)
57 59
{
......
563 565

  
564 566
static struct tbf rl_runtime_err = TBF_DEFAULT_LOG_LIMITS;
565 567

  
566
#define runtime(x) do { \
567
    log_rl(&rl_runtime_err, L_ERR "filters, line %d: %s", what->lineno, x); \
568
#define runtime(fmt, ...) do { \
569
    log_rl(&rl_runtime_err, L_ERR "filters, line %d: " fmt, what->lineno, ##__VA_ARGS__); \
568 570
    res.type = T_RETURN; \
569 571
    res.val.i = F_ERROR; \
570 572
    return res; \
......
1475 1477

  
1476 1478
    break;
1477 1479

  
1480
  case P('a', 's'):	/* Birdtest Assert */
1481
    ONEARG;
1482

  
1483
    if (v1.type != T_BOOL)
1484
      runtime("Should be boolean value");
1485

  
1486
    res.type = v1.type;
1487
    res.val = v1.val;
1488

  
1489
    CALL(bt_assert_hook, res.val.i, what);
1490
    break;
1478 1491

  
1479 1492
  default:
1480 1493
    bug( "Unknown instruction %d (%c)", what->code, what->code & 0xff);
filter/filter.h
16 16

  
17 17
struct f_inst {		/* Instruction */
18 18
  struct f_inst *next;	/* Structure is 16 bytes, anyway */
19
  u16 code;
20
  u16 aux;
19
  u16 code;		/* Instruction code, see the interpret() function and P() macro */
20
  u16 aux;		/* Extension to instruction code, T_*, EA_*, EAF_*  */
21 21
  union {
22 22
    int i;
23 23
    void *p;
24
  } a1;
24
  } a1;			/* The first argument */
25 25
  union {
26 26
    int i;
27 27
    void *p;
28
  } a2;
28
  } a2;			/* The second argument */
29 29
  int lineno;
30 30
};
31 31

  
......
55 55
};
56 56

  
57 57
struct f_val {
58
  int type;
58
  int type;		/* T_*  */
59 59
  union {
60 60
    uint i;
61 61
    u64 ec;
......
205 205

  
206 206
#define FF_FORCE_TMPATTR 1		/* Force all attributes to be temporary */
207 207

  
208
/* Bird Tests */
209
struct f_bt_test_suite {
210
  node n;			/* Node in config->tests */
211
  struct f_inst *fn;		/* Root of function */
212
  const char *fn_name;		/* Name of test */
213
  const char *dsc;		/* Description */
214
};
215

  
216
/* Hook for call bt_assert() function in configuration */
217
extern void (*bt_assert_hook)(int result, struct f_inst *assert);
218

  
208 219
#endif
filter/filter_test.c
1
/*
2
 *	Filters: Tests
3
 *
4
 *	(c) 2015 CZ.NIC z.s.p.o.
5
 *
6
 *	Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

  
9
#ifndef _GNU_SOURCE
10
#define _GNU_SOURCE
11
#endif
12

  
13
#include <string.h>
14
#include <stdlib.h>
15

  
16
#include "test/birdtest.h"
17
#include "test/bt-utils.h"
18

  
19
#include "filter/filter.h"
20
#include "conf/conf.h"
21

  
22
#define BT_CONFIG_FILE "filter/test.conf"
23

  
24

  
25
static struct config *
26
parse_config_file(const void *filename_void)
27
{
28
  bt_bird_init();
29

  
30
  size_t fn_size = strlen((const char *) filename_void) + 1;
31
  char *filename = alloca(fn_size);
32
  strncpy(filename, filename_void, fn_size);
33

  
34
  struct config *c = bt_config_file_parse(filename);
35
  bt_bird_cleanup();
36

  
37
  return c;
38
}
39

  
40
static int
41
run_function(const void *parsed_fn_def)
42
{
43
  /* XXX: const -> non-const */
44
  struct f_inst *f = (struct f_inst *) parsed_fn_def;
45

  
46
  linpool *tmp = lp_new(&root_pool, 4096);
47
  struct f_val res = f_eval(f, tmp);
48
  rfree(tmp);
49

  
50
  if (res.type == T_RETURN && res.val.i >= F_REJECT)
51
    return BT_FAILURE;
52

  
53
  return BT_SUCCESS;
54
}
55

  
56
static void
57
bt_assert_filter(int result, struct f_inst *assert)
58
{
59
  int bt_suit_case_result = BT_SUCCESS;
60
  if (!result)
61
  {
62
    bt_result = BT_FAILURE;
63
    bt_suite_result = BT_FAILURE;
64
    bt_suit_case_result = BT_FAILURE;
65
  }
66

  
67
  bt_log_suite_case_result(bt_suit_case_result, "Assertion at line %d (%s)", assert->lineno, (char *) assert->a2.p);
68
}
69

  
70
int
71
main(int argc, char *argv[])
72
{
73
  bt_init(argc, argv);
74

  
75
  struct config *c = parse_config_file(BT_CONFIG_FILE);
76

  
77
  if (c)
78
  {
79
    bt_assert_hook = bt_assert_filter;
80

  
81
    struct f_bt_test_suite *t;
82
    WALK_LIST(t, c->tests)
83
      bt_test_suite_base(run_function, t->fn_name, t->fn, BT_FORKING, BT_TIMEOUT, "%s", t->dsc);
84
  }
85

  
86
  return bt_exit_value();
87
}
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);
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff