]> git.ipfire.org Git - thirdparty/bird.git/blob - filter/filter_test.c
Filter: fix filter comparison test
[thirdparty/bird.git] / 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 "filter/data.h"
21 #include "filter/f-inst.h"
22 #include "conf/conf.h"
23
24 #define BT_CONFIG_FILE "filter/test.conf"
25
26
27 struct parse_config_file_arg {
28 struct config **cp;
29 const char *filename;
30 };
31
32 static int
33 parse_config_file(const void *argv)
34 {
35 const struct parse_config_file_arg *arg = argv;
36 size_t fn_size = strlen(arg->filename) + 1;
37 char *filename = alloca(fn_size);
38 memcpy(filename, arg->filename, fn_size);
39
40 *(arg->cp) = bt_config_file_parse(filename);
41 return !!*(arg->cp);
42 }
43
44 static int
45 run_function(const void *arg)
46 {
47 const struct f_bt_test_suite *t = arg;
48
49 if (t->cmp)
50 return t->result == f_same(t->fn, t->cmp);
51
52 linpool *tmp = lp_new_default(&root_pool);
53 enum filter_return fret = f_eval(t->fn, tmp, NULL);
54 rfree(tmp);
55
56 return (fret < F_REJECT);
57 }
58
59 static void
60 bt_assert_filter(int result, const struct f_line_item *assert)
61 {
62 int bt_suit_case_result = 1;
63 if (!result)
64 {
65 bt_result = 0;
66 bt_suite_result = 0;
67 bt_suit_case_result = 0;
68 }
69
70 bt_log_suite_case_result(bt_suit_case_result, "Assertion at line %d (%s)",
71 assert->lineno, assert->i_FI_ASSERT.s);
72 }
73
74 int
75 main(int argc, char *argv[])
76 {
77 bt_init(argc, argv);
78
79 bt_bird_init();
80
81 bt_assert_hook = bt_assert_filter;
82
83 struct config *c = NULL;
84 struct parse_config_file_arg pcfa = { .cp = &c, .filename = BT_CONFIG_FILE };
85
86 bt_test_suite_base(parse_config_file, "conf", (const void *) &pcfa, 0, 0, "parse config file");
87 bt_assert(c);
88
89 bt_test_suite_base(parse_config_file, "reconf", (const void *) &pcfa, 0, 0, "reconfigure with the same file");
90 bt_assert(c);
91
92 struct symbol *s;
93 WALK_LIST(s, c->symbols)
94 if ((s->class == SYM_FUNCTION) || (s->class == SYM_FILTER))
95 bt_assert_msg((s->flags & SYM_FLAG_SAME), "Symbol %s same check", s->name);
96
97 struct f_bt_test_suite *t;
98 WALK_LIST(t, c->tests)
99 bt_test_suite_base(run_function, t->fn_name, t, BT_FORKING, BT_TIMEOUT, "%s", t->dsc);
100
101 bt_bird_cleanup();
102 return bt_exit_value();
103 }