1 /*
2 * Copyright (c) 2017 Balabit
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation, or (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 *
17 * As an additional exemption you are allowed to compile & link against the
18 * OpenSSL libraries as published by the OpenSSL project. See the file
19 * COPYING for details.
20 */
21
22
23 #include <criterion/criterion.h>
24 #include <criterion/parameterized.h>
25 #include "tags-parser.h"
26 #include "apphook.h"
27 #include "msg_parse_lib.h"
28 #include "libtest/cr_template.h"
29
30 void
setup(void)31 setup(void)
32 {
33 configuration = cfg_new_snippet();
34 app_startup();
35 }
36
37 void
teardown(void)38 teardown(void)
39 {
40 app_shutdown();
41 cfg_free(configuration);
42 }
43
44 TestSuite(tagsparser, .init = setup, .fini = teardown);
45
Test(tagsparser,input_list_is_transferred_to_a_list_of_tags)46 Test(tagsparser, input_list_is_transferred_to_a_list_of_tags)
47 {
48 LogParser *tags_parser = tags_parser_new(configuration);
49
50 log_pipe_init((LogPipe *)tags_parser);
51
52 LogMessage *msg = log_msg_new_empty();
53 log_msg_set_value(msg, LM_V_MESSAGE, "foo,bar,baz", -1);
54 log_msg_set_tag_by_name(msg, "tag-already-set");
55
56 LogPathOptions path_options = LOG_PATH_OPTIONS_INIT;
57 cr_assert(log_parser_process_message(tags_parser, &msg, &path_options));
58
59 assert_log_message_has_tag(msg, "foo");
60 assert_log_message_has_tag(msg, "bar");
61 assert_log_message_has_tag(msg, "baz");
62 assert_log_message_has_tag(msg, "tag-already-set");
63
64 log_pipe_deinit((LogPipe *)tags_parser);
65 log_pipe_unref((LogPipe *)tags_parser);
66 log_msg_unref(msg);
67 }
68
Test(tagsparser,template_argument_uses_that_instead_of_MSG)69 Test(tagsparser, template_argument_uses_that_instead_of_MSG)
70 {
71 LogParser *tags_parser = tags_parser_new(configuration);
72 LogTemplate *template = compile_template("${PROGRAM}", FALSE);
73
74 log_pipe_init((LogPipe *) tags_parser);
75
76 LogMessage *msg = log_msg_new_empty();
77 log_msg_set_value(msg, LM_V_PROGRAM, "foo,bar,baz", -1);
78
79 log_parser_set_template(tags_parser, template);
80 LogPathOptions path_options = LOG_PATH_OPTIONS_INIT;
81 cr_assert(log_parser_process_message(tags_parser, &msg, &path_options));
82
83 assert_log_message_has_tag(msg, "foo");
84 assert_log_message_has_tag(msg, "bar");
85 assert_log_message_has_tag(msg, "baz");
86
87 log_pipe_deinit((LogPipe *)tags_parser);
88 log_pipe_unref((LogPipe *)tags_parser);
89 log_msg_unref(msg);
90 }
91