1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
/*
* Copyright (c) 2012-2018 Balabit
* Copyright (c) 2012 Balázs Scheidler
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "cr_template.h"
#include "stopwatch.h"
#include "msg_parse_lib.h"
#include "timeutils/misc.h"
#include "timeutils/cache.h"
#include "logmsg/logmsg.h"
#include "gsockaddr.h"
#include "cfg.h"
#include <string.h>
#include <criterion/criterion.h>
static MsgFormatOptions parse_options;
void
init_template_tests(void)
{
init_parse_options_and_load_syslogformat(&parse_options);
}
void
deinit_template_tests(void)
{
deinit_syslogformat_module();
}
LogMessage *
message_from_list(va_list ap)
{
char *key, *value;
LogMessage *msg = create_empty_message();
if (!msg)
return NULL;
key = va_arg(ap, char *);
while (key)
{
value = va_arg(ap, char *);
if (!value)
return msg;
log_msg_set_value_by_name(msg, key, value, -1);
key = va_arg(ap, char *);
}
return msg;
}
LogMessage *
create_empty_message(void)
{
LogMessage *msg;
const gchar *msg_str = "<155>2006-02-11T10:34:56+01:00 bzorp syslog-ng[23323]:árvíztűrőtükörfúrógép";
msg = msg_format_parse(&parse_options, (const guchar *) msg_str, strlen(msg_str));
log_msg_set_saddr_ref(msg, g_sockaddr_inet_new("10.11.12.13", 1010));
log_msg_set_daddr_ref(msg, g_sockaddr_inet_new("127.0.0.5", 6514));
log_msg_set_match(msg, 0, "whole-match", -1);
log_msg_set_match(msg, 1, "first-match", -1);
log_msg_set_tag_by_name(msg, "alma");
log_msg_set_tag_by_name(msg, "korte");
log_msg_clear_tag_by_name(msg, "narancs");
log_msg_set_tag_by_name(msg, "citrom");
log_msg_set_tag_by_name(msg, "tag,containing,comma");
msg->rcptid = 555;
msg->host_id = 0xcafebabe;
msg->proto = 33;
/* fix some externally or automatically defined values */
log_msg_set_value(msg, LM_V_HOST_FROM, "kismacska", -1);
msg->timestamps[LM_TS_RECVD].ut_sec = 1139684315;
msg->timestamps[LM_TS_RECVD].ut_usec = 639000;
msg->timestamps[LM_TS_RECVD].ut_gmtoff = get_local_timezone_ofs(1139684315);
return msg;
}
LogMessage *
create_sample_message(void)
{
LogMessage *msg = create_empty_message();
log_msg_set_value_by_name(msg, "APP.VALUE", "value", -1);
log_msg_set_value_by_name(msg, "APP.VALUE2", "value", -1);
log_msg_set_value_by_name(msg, "APP.VALUE3", "value", -1);
log_msg_set_value_by_name(msg, "APP.VALUE4", "value", -1);
log_msg_set_value_by_name(msg, "APP.VALUE5", "value", -1);
log_msg_set_value_by_name(msg, "APP.VALUE6", "value", -1);
log_msg_set_value_by_name(msg, "APP.VALUE7", "value", -1);
log_msg_set_value_by_name(msg, "APP.STRIP1", " value", -1);
log_msg_set_value_by_name(msg, "APP.STRIP2", "value ", -1);
log_msg_set_value_by_name(msg, "APP.STRIP3", " value ", -1);
log_msg_set_value_by_name(msg, "APP.STRIP4", "value", -1);
log_msg_set_value_by_name(msg, "APP.STRIP5", "", -1);
log_msg_set_value_by_name(msg, "APP.QVALUE", "\"value\"", -1);
log_msg_set_value_by_name(msg, ".unix.uid", "1000", -1);
log_msg_set_value_by_name(msg, ".unix.gid", "1000", -1);
log_msg_set_value_by_name(msg, ".unix.cmd", "command", -1);
log_msg_set_value_by_name(msg, ".json.foo", "bar", -1);
log_msg_set_value_by_name(msg, ".json.sub.value1", "subvalue1", -1);
log_msg_set_value_by_name(msg, ".json.sub.value2", "subvalue2", -1);
log_msg_set_value_by_name(msg, "escaping", "binary stuff follows \"\xad árvíztűrőtükörfúrógép", -1);
log_msg_set_value_by_name(msg, "escaping2", "\xc3", -1);
log_msg_set_value_by_name(msg, "null", "binary\0stuff", 12);
log_msg_set_value_by_name(msg, "comma_value", "value,with,a,comma", -1);
log_msg_set_value_by_name(msg, "empty_value", "", -1);
log_msg_set_value_by_name(msg, "template_name", "dummy", -1);
log_msg_set_value_by_name_with_type(msg, "number1", "123", -1, LM_VT_INTEGER);
log_msg_set_value_by_name_with_type(msg, "number2", "456", -1, LM_VT_INTEGER);
log_msg_set_value_by_name_with_type(msg, "bytes", "\0\1\2\3", 4, LM_VT_BYTES);
log_msg_set_value_by_name_with_type(msg, "protobuf", "\4\5\6\7", 4, LM_VT_PROTOBUF);
return msg;
}
LogTemplate *
compile_template_with_escaping(const gchar *template, gboolean escaping)
{
LogTemplate *templ = log_template_new(configuration, NULL);
gboolean success;
GError *error = NULL;
log_template_set_escape(templ, escaping);
success = log_template_compile(templ, template, &error);
cr_assert(success, "template expected to compile cleanly,"
" but it didn't, template=%s, error=%s",
template, error ? error->message : "(none)");
g_clear_error(&error);
return templ;
}
LogTemplate *
compile_template(const gchar *template)
{
return compile_template_with_escaping(template, FALSE);
}
LogTemplate *
compile_escaped_template(const gchar *template)
{
return compile_template_with_escaping(template, TRUE);
}
void
assert_template_format_with_escaping_and_context_msgs(const gchar *template, gboolean escaping,
const gchar *expected, gssize expected_len,
LogMessageValueType expected_type,
LogMessage **msgs, gint num_messages)
{
LogTemplate *templ = compile_template_with_escaping(template, escaping);
const gchar *prefix = "somevoodooprefix/";
LogMessageValueType type;
gint prefix_len = strlen(prefix);
if (!templ)
return;
GString *res = g_string_new(prefix);
const gchar *context_id = "test-context-id";
LogTemplateEvalOptions options = {NULL, LTZ_LOCAL, 999, context_id, LM_VT_STRING};
log_template_append_format_value_and_type_with_context(templ, msgs, num_messages, &options, res, &type);
cr_assert(strncmp(res->str, prefix, prefix_len) == 0,
"the prefix was overwritten by the template, template=%s, res=%s, expected_prefix=%s",
template, res->str, prefix);
expected_len = (expected_len >= 0 ? expected_len : strlen(expected));
cr_assert_eq(res->len - prefix_len, expected_len,
"context template test failed, expected length mismatch, template=%s, actual=%.*s, expected=%.*s",
template, (gint) res->len - prefix_len, res->str + prefix_len, (gint) expected_len, expected);
cr_assert_arr_eq(res->str + prefix_len, expected, expected_len,
"context template test failed, template=%s, actual=%.*s, expected=%.*s",
template, (gint) res->len - prefix_len, res->str + prefix_len, (gint) expected_len, expected);
if (expected_type != LM_VT_NONE)
cr_assert_eq(type, expected_type,
"expected type does not match template=%s, type=%d, expected_type=%d (value was %.*s)",
template, type, expected_type, (gint) expected_len, expected);
log_template_unref(templ);
g_string_free(res, TRUE);
}
void
assert_template_format_with_context_msgs(const gchar *template, const gchar *expected, LogMessage **msgs,
gint num_messages)
{
assert_template_format_with_escaping_and_context_msgs(template, FALSE, expected, -1, LM_VT_NONE, msgs, num_messages);
}
void
assert_template_format_with_escaping_msg(const gchar *template, gboolean escaping,
const gchar *expected,
LogMessage *msg)
{
assert_template_format_with_escaping_and_context_msgs(template, escaping, expected, -1, LM_VT_NONE, &msg, 1);
}
void
assert_template_format_with_escaping(const gchar *template, gboolean escaping, const gchar *expected)
{
LogMessage *msg = create_sample_message();
assert_template_format_with_escaping_msg(template, escaping, expected, msg);
log_msg_unref(msg);
}
void
assert_template_format_msg(const gchar *template, const gchar *expected, LogMessage *msg)
{
assert_template_format_with_escaping_msg(template, FALSE, expected, msg);
}
void
assert_template_format(const gchar *template, const gchar *expected)
{
assert_template_format_with_escaping(template, FALSE, expected);
}
void
assert_template_format_value_and_type(const gchar *template, const gchar *expected, LogMessageValueType expected_type)
{
LogMessage *msg = create_sample_message();
assert_template_format_with_escaping_and_context_msgs(template, FALSE, expected, -1, expected_type, &msg, 1);
log_msg_unref(msg);
}
void
assert_template_format_value_and_type_msg(const gchar *template, const gchar *expected,
LogMessageValueType expected_type, LogMessage *msg)
{
assert_template_format_with_escaping_and_context_msgs(template, FALSE, expected, -1, expected_type, &msg, 1);
}
void
assert_template_format_value_and_type_with_escaping(const gchar *template, gboolean escaping, const gchar *expected,
LogMessageValueType expected_type)
{
LogMessage *msg = create_sample_message();
assert_template_format_with_escaping_and_context_msgs(template, escaping, expected, -1, expected_type, &msg, 1);
log_msg_unref(msg);
}
void
assert_template_format_with_context(const gchar *template, const gchar *expected)
{
LogMessage *msg;
LogMessage *context[2];
msg = create_sample_message();
context[0] = context[1] = msg;
assert_template_format_with_context_msgs(template, expected, context, 2);
log_msg_unref(msg);
}
void
assert_template_format_with_len(const gchar *template, const gchar *expected, gssize expected_len)
{
LogMessage *msg = create_sample_message();
assert_template_format_with_escaping_and_context_msgs(template, FALSE, expected, expected_len, LM_VT_NONE, &msg, 1);
log_msg_unref(msg);
}
void
assert_template_failure(const gchar *template, const gchar *expected_error)
{
LogTemplate *templ = log_template_new(configuration, NULL);
GError *error = NULL;
cr_assert_not(log_template_compile(templ, template, &error),
"compilation failure expected to template,"
" but success was returned, template=%s, expected_error=%s\n",
template, expected_error);
cr_assert(strstr(error ? error->message : "", expected_error) != NULL,
"FAIL: compilation error doesn't match, error=%s, expected_error=%s\n",
error->message, expected_error);
g_clear_error(&error);
log_template_unref(templ);
}
#define BENCHMARK_COUNT 100000
void
perftest_template(gchar *template)
{
LogTemplate *templ;
LogMessage *msg;
GString *res = g_string_sized_new(1024);
gint i;
GError *error = NULL;
templ = log_template_new(configuration, NULL);
if (!log_template_compile(templ, template, &error))
{
cr_assert(FALSE, "template expected to compile cleanly,"
" but it didn't, template=%s, error=%s",
template, error ? error->message : "(none)");
return;
}
msg = create_sample_message();
start_stopwatch();
for (i = 0; i < BENCHMARK_COUNT; i++)
{
log_template_format(templ, msg, &DEFAULT_TEMPLATE_EVAL_OPTIONS, res);
}
stop_stopwatch_and_display_result(BENCHMARK_COUNT,
" %-90.*s",
(int) strlen(template) - 1,
template);
log_template_unref(templ);
g_string_free(res, TRUE);
log_msg_unref(msg);
}
|