]> git.ipfire.org Git - thirdparty/bird.git/blame - filter/filter_test.c
Filters: split the large filter.h file to smaller files.
[thirdparty/bird.git] / filter / filter_test.c
CommitLineData
9b0a0ba9
OZ
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"
8bdb05ed
MM
20#include "filter/f-util.h"
21#include "filter/f-inst.h"
9b0a0ba9
OZ
22#include "conf/conf.h"
23
24#define BT_CONFIG_FILE "filter/test.conf"
25
26
c0e958e0
MM
27struct parse_config_file_arg {
28 struct config **cp;
29 const char *filename;
30};
9b0a0ba9 31
c0e958e0
MM
32static int
33parse_config_file(const void *argv)
34{
35 const struct parse_config_file_arg *arg = argv;
36 size_t fn_size = strlen(arg->filename) + 1;
9b0a0ba9 37 char *filename = alloca(fn_size);
c0e958e0
MM
38 strncpy(filename, arg->filename, fn_size);
39
40 *(arg->cp) = bt_config_file_parse(filename);
41 return !!*(arg->cp);
9b0a0ba9
OZ
42}
43
44static int
45run_function(const void *parsed_fn_def)
46{
4c553c5a 47 const struct f_line *f = (const struct f_line *) parsed_fn_def;
9b0a0ba9 48
05d47bd5 49 linpool *tmp = lp_new_default(&root_pool);
7afa1438
JMM
50 struct f_val res;
51 enum filter_return fret = f_eval(f, tmp, &res);
9b0a0ba9
OZ
52 rfree(tmp);
53
7afa1438 54 return (fret < F_REJECT);
9b0a0ba9
OZ
55}
56
57static void
4c553c5a 58bt_assert_filter(int result, const struct f_line_item *assert)
9b0a0ba9 59{
5e3cd0e5 60 int bt_suit_case_result = 1;
9b0a0ba9
OZ
61 if (!result)
62 {
5e3cd0e5
PT
63 bt_result = 0;
64 bt_suite_result = 0;
65 bt_suit_case_result = 0;
9b0a0ba9
OZ
66 }
67
4c553c5a 68 bt_log_suite_case_result(bt_suit_case_result, "Assertion at line %d (%s)", assert->lineno, assert->s);
9b0a0ba9
OZ
69}
70
71int
72main(int argc, char *argv[])
73{
74 bt_init(argc, argv);
c0e958e0
MM
75 bt_bird_init();
76
77 bt_assert_hook = bt_assert_filter;
9b0a0ba9 78
c0e958e0
MM
79 struct config *c = NULL;
80 struct parse_config_file_arg pcfa = { .cp = &c, .filename = BT_CONFIG_FILE };
81 bt_test_suite_base(parse_config_file, "conf", (const void *) &pcfa, 0, 0, "parse config file");
82
83 bt_bird_cleanup();
9b0a0ba9
OZ
84
85 if (c)
86 {
9b0a0ba9
OZ
87 struct f_bt_test_suite *t;
88 WALK_LIST(t, c->tests)
89 bt_test_suite_base(run_function, t->fn_name, t->fn, BT_FORKING, BT_TIMEOUT, "%s", t->dsc);
90 }
91
92 return bt_exit_value();
93}