]> git.ipfire.org Git - thirdparty/bird.git/blob - filter/filter_test.c
Filter refactoring: Changed arguments from separate unions to an array
[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 "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_default(&root_pool);
47 struct f_val res;
48 enum filter_return fret = f_eval(f, tmp, &res);
49 rfree(tmp);
50
51 return (fret < F_REJECT);
52 }
53
54 static void
55 bt_assert_filter(int result, struct f_inst *assert)
56 {
57 int bt_suit_case_result = 1;
58 if (!result)
59 {
60 bt_result = 0;
61 bt_suite_result = 0;
62 bt_suit_case_result = 0;
63 }
64
65 bt_log_suite_case_result(bt_suit_case_result, "Assertion at line %d (%s)", assert->lineno, (char *) assert->a[1].p);
66 }
67
68 int
69 main(int argc, char *argv[])
70 {
71 bt_init(argc, argv);
72
73 struct config *c = parse_config_file(BT_CONFIG_FILE);
74
75 if (c)
76 {
77 bt_assert_hook = bt_assert_filter;
78
79 struct f_bt_test_suite *t;
80 WALK_LIST(t, c->tests)
81 bt_test_suite_base(run_function, t->fn_name, t->fn, BT_FORKING, BT_TIMEOUT, "%s", t->dsc);
82 }
83
84 return bt_exit_value();
85 }