1 /*
2 * Copyright (c) 2018 Balabit
3 * Copyright (c) 2019 Szemere
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * As an additional exemption you are allowed to compile & link against the
20 * OpenSSL libraries as published by the OpenSSL project. See the file
21 * COPYING for details.
22 *
23 */
24 #include "filter/filter-op.h"
25 #include "filter/filter-expr.h"
26 #include "filter/filter-pri.h"
27 #include "filter/filter-expr-parser.h"
28 #include "test_filters_common.h"
29 #include "cfg-lexer.h"
30 #include "apphook.h"
31
32 #include <criterion/criterion.h>
33 #include <criterion/parameterized.h>
34
35 static FilterExprNode *
_compile_standalone_filter(gchar * config_snippet)36 _compile_standalone_filter(gchar *config_snippet)
37 {
38 GlobalConfig *cfg = cfg_new_snippet();
39 FilterExprNode *tmp;
40
41 CfgLexer *lexer = cfg_lexer_new_buffer(cfg, config_snippet, strlen(config_snippet));
42 cr_assert(lexer, "Couldn't initialize a buffer for CfgLexer");
43
44 cr_assert(cfg_run_parser(cfg, lexer, &filter_expr_parser, (gpointer *) &tmp, NULL));
45
46 cfg_free(cfg);
47
48 return tmp;
49 }
50
51 typedef struct _FilterParams
52 {
53 gchar *config_snippet;
54 gboolean expected_result;
55 } FilterParams;
56
ParameterizedTestParameters(filter_op,test_or_evaluation)57 ParameterizedTestParameters(filter_op, test_or_evaluation)
58 {
59 static FilterParams test_data_list[] =
60 {
61 // Filters inside evaluates to TRUE
62 {.config_snippet = " facility(2) or facility(2)", .expected_result = TRUE },
63 {.config_snippet = " facility(2) or not facility(2)", .expected_result = TRUE },
64 {.config_snippet = "not facility(2) or facility(2)", .expected_result = TRUE },
65 {.config_snippet = "not facility(2) or not facility(2)", .expected_result = FALSE },
66 // note: The above expression evaluated in the following way: not (TRUE or (not TRUE))
67 // The expression below has the same expected result, but for a different reason.
68 {.config_snippet = "(not facility(2)) or (not facility(2))", .expected_result = FALSE },
69
70 // Filters inside evaluates to FALSE
71 {.config_snippet = " facility(3) or facility(3)", .expected_result = FALSE },
72 {.config_snippet = " facility(3) or not facility(3)", .expected_result = TRUE },
73 {.config_snippet = "not facility(3) or facility(3)", .expected_result = TRUE },
74 {.config_snippet = "not facility(3) or not facility(3)", .expected_result = TRUE },
75 // same as before
76 {.config_snippet = "(not facility(3)) or (not facility(3))", .expected_result = TRUE },
77
78 // Mixed TRUE and FALSE evaluations
79 {.config_snippet = " facility(2) or facility(3)", .expected_result = TRUE },
80 {.config_snippet = " facility(2) or not facility(3)", .expected_result = TRUE },
81 {.config_snippet = "not facility(2) or facility(3)", .expected_result = FALSE },
82 {.config_snippet = "not facility(2) or not facility(3)", .expected_result = TRUE },
83 // same as before
84 {.config_snippet = "(not facility(2)) or (not facility(3))", .expected_result = TRUE },
85 };
86
87 return cr_make_param_array(FilterParams, test_data_list, G_N_ELEMENTS(test_data_list));
88 }
89
ParameterizedTest(FilterParams * params,filter_op,test_or_evaluation)90 ParameterizedTest(FilterParams *params, filter_op, test_or_evaluation)
91 {
92 const gchar *msg = "<16> openvpn[2499]: PTHREAD support initialized";
93 FilterExprNode *filter = _compile_standalone_filter(params->config_snippet);
94 testcase(msg, filter, params->expected_result);
95 }
96
97 TestSuite(filter_op, .init = setup, .fini = teardown);
98