1 /*
2  * Copyright (c) 2016 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 
26 #include "msg_parse_lib.h"
27 #include "syslog-ng.h"
28 #include "logmsg/logmsg.h"
29 #include "serialize.h"
30 #include "apphook.h"
31 #include "gsockaddr.h"
32 #include "logpipe.h"
33 #include "cfg.h"
34 #include "plugin.h"
35 
36 #include <time.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 
41 MsgFormatOptions parse_options;
42 
43 void
assert_new_log_message_attributes(LogMessage * log_message)44 assert_new_log_message_attributes(LogMessage *log_message)
45 {
46   assert_log_message_value(log_message, LM_V_HOST, "newhost");
47   assert_log_message_value(log_message, LM_V_HOST_FROM, "newhost");
48   assert_log_message_value(log_message, LM_V_MESSAGE, "newmsg");
49   assert_log_message_value(log_message, LM_V_PROGRAM, "newprogram");
50   assert_log_message_value(log_message, LM_V_PID, "newpid");
51   assert_log_message_value(log_message, LM_V_MSGID, "newmsgid");
52   assert_log_message_value(log_message, LM_V_SOURCE, "newsource");
53   assert_log_message_value(log_message, log_msg_get_value_handle("newvalue"), "newvalue");
54 }
55 
56 void
set_new_log_message_attributes(LogMessage * log_message)57 set_new_log_message_attributes(LogMessage *log_message)
58 {
59   log_msg_set_value(log_message, LM_V_HOST, "newhost", -1);
60   log_msg_set_value(log_message, LM_V_HOST_FROM, "newhost", -1);
61   log_msg_set_value(log_message, LM_V_MESSAGE, "newmsg", -1);
62   log_msg_set_value(log_message, LM_V_PROGRAM, "newprogram", -1);
63   log_msg_set_value(log_message, LM_V_PID, "newpid", -1);
64   log_msg_set_value(log_message, LM_V_MSGID, "newmsgid", -1);
65   log_msg_set_value(log_message, LM_V_SOURCE, "newsource", -1);
66   log_msg_set_value_by_name(log_message, "newvalue", "newvalue", -1);
67 }
68 
69 
70 void
setup(void)71 setup(void)
72 {
73   app_startup();
74   init_parse_options_and_load_syslogformat(&parse_options);
75 }
76 
77 void
teardown(void)78 teardown(void)
79 {
80   deinit_syslogformat_module();
81   app_shutdown();
82 }
83 
84 TestSuite(clone_logmsg, .init = setup, .fini = teardown);
85 
ParameterizedTestParameters(clone_logmsg,test_cloning_with_log_message)86 ParameterizedTestParameters(clone_logmsg, test_cloning_with_log_message)
87 {
88   const gchar *messages[] =
89   {
90     "<7>1 2006-10-29T01:59:59.156+01:00 mymachine.example.com evntslog - ID47 [exampleSDID@0 iut=\"3\" eventSource=\"Application\" eventID=\"1011\"][examplePriority@0 class=\"high\"] BOMAn application event log entry...",
91     "<132>1 2006-10-29T01:59:59.156+01:00 mymachine evntslog - - [exampleSDID@0 iut=\"3\"] [eventSource=\"Application\" eventID=\"1011\"][examplePriority@0 class=\"high\"] BOMAn application event log entry...",
92   };
93 
94   return cr_make_param_array(const gchar *, messages, sizeof(messages) / sizeof(const gchar *));
95 }
96 
ParameterizedTest(const gchar * msg,clone_logmsg,test_cloning_with_log_message)97 ParameterizedTest(const gchar *msg, clone_logmsg, test_cloning_with_log_message)
98 {
99   LogMessage *original_log_message, *log_message, *cloned_log_message;
100   regex_t bad_hostname;
101   GSockAddr *addr = g_sockaddr_inet_new("10.10.10.10", 1010);
102   LogPathOptions path_options = LOG_PATH_OPTIONS_INIT;
103 
104   parse_options.flags = LP_SYSLOG_PROTOCOL;
105   parse_options.bad_hostname = &bad_hostname;
106 
107   original_log_message = log_msg_new(msg, strlen(msg), &parse_options);
108   log_msg_set_saddr(original_log_message, addr);
109   log_message = log_msg_new(msg, strlen(msg), &parse_options);
110   log_msg_set_saddr(log_message, addr);
111 
112   log_msg_set_tag_by_name(log_message, "newtag");
113   path_options.ack_needed = FALSE;
114 
115   cloned_log_message = log_msg_clone_cow(log_message, &path_options);
116   assert_log_messages_equal(cloned_log_message, original_log_message);
117 
118   set_new_log_message_attributes(cloned_log_message);
119 
120   assert_log_messages_equal(log_message, original_log_message);
121   assert_new_log_message_attributes(cloned_log_message);
122   assert_log_message_has_tag(cloned_log_message, "newtag");
123 
124   g_sockaddr_unref(addr);
125   log_msg_unref(cloned_log_message);
126   log_msg_unref(log_message);
127   log_msg_unref(original_log_message);
128 }
129