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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
/*
* Copyright (c) 2014 Balabit
* Copyright (c) 2014 Laszlo Budai
*
* 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 <criterion/criterion.h>
#include <criterion/parameterized.h>
#include "value-pairs/value-pairs.h"
#include "logmsg/logmsg.h"
#include "apphook.h"
#include "cfg.h"
#include "plugin.h"
#include "msg-format.h"
#include <stdlib.h>
gboolean success = TRUE;
gboolean
vp_keys_foreach(const gchar *name, LogMessageValueType type, const gchar *value,
gsize value_len, gpointer user_data)
{
gpointer *args = (gpointer *) user_data;
GList **keys = (GList **) args[0];
gboolean *test_key_found = (gboolean *) args[1];
if (strcmp(name, "test.key") != 0)
*keys = g_list_insert_sorted(*keys, g_strdup(name), (GCompareFunc) strcmp);
else
*test_key_found = TRUE;
return FALSE;
}
void
cat_keys_foreach(const gchar *name, gpointer user_data)
{
GString *res = (GString *) user_data;
if (res->len > 0)
g_string_append_c(res, ',');
g_string_append(res, name);
}
MsgFormatOptions parse_options;
LogTemplateOptions template_options;
LogMessage *
create_message(void)
{
LogMessage *msg;
const gchar *text =
"<134>1 2009-10-16T11:51:56+02:00 exchange.macartney.esbjerg MSExchange_ADAccess 20208 _MSGID_ [origin ip=\"exchange.macartney.esbjerg\"][meta sequenceId=\"191732\" sysUpTime=\"68807696\"][EventData@18372.4 Data=\"MSEXCHANGEOWAAPPPOOL.CONFIG\\\" -W \\\"\\\" -M 1 -AP \\\"MSEXCHANGEOWAAPPPOOL5244fileserver.macartney.esbjerg CDG 1 7 7 1 0 1 1 7 1 mail.macartney.esbjerg CDG 1 7 7 1 0 1 1 7 1 maindc.macartney.esbjerg CD- 1 6 6 0 0 1 1 6 1 \"][Keywords@18372.4 Keyword=\"Classic\"] ApplicationMSExchangeADAccess: message";
const gchar *unset_nvpair = "unset_value";
msg = msg_format_parse(&parse_options, (const guchar *) text, strlen(text));
log_msg_set_tag_by_name(msg, "almafa");
log_msg_set_value_by_name(msg, unset_nvpair, "value that has been unset", -1);
log_msg_unset_value_by_name(msg, unset_nvpair);
return msg;
}
static LogTemplate *
create_template(const gchar *type_hint_string, const gchar *template_string)
{
LogTemplate *template;
template = log_template_new(configuration, NULL);
cr_assert(log_template_compile(template, template_string, NULL));
log_template_set_type_hint(template, type_hint_string, NULL);
return template;
}
static void
assert_keys_match_expected(const gchar *scope, GList *vp_keys_list, const gchar *expected)
{
gchar **expected_list = g_strsplit(expected, ",", -1);
GList *l = vp_keys_list;
gint i = 0;
for (i = 0; l && expected_list[i]; l = l->next, i++)
{
cr_assert_str_eq((const gchar *) l->data, expected_list[i],
"Expected element at index %d mismatch %s <> %s, scope=%s",
i, (const gchar *) l->data, expected_list[i], scope);
}
cr_assert(l == NULL && expected_list[i] == NULL);
g_strfreev(expected_list);
}
void
testcase(const gchar *scope, const gchar *exclude, const gchar *expected)
{
ValuePairs *vp;
GList *vp_keys_list = NULL;
LogMessage *msg = create_message();
gpointer args[2];
gboolean test_key_found = FALSE;
LogTemplate *template;
vp = value_pairs_new(configuration);
value_pairs_add_scope(vp, scope);
if (exclude)
value_pairs_add_glob_pattern(vp, exclude, FALSE);
template = create_template("string", "$MESSAGE");
value_pairs_add_pair(vp, "test.key", template);
log_template_unref(template);
args[0] = &vp_keys_list;
args[1] = &test_key_found;
LogTemplateEvalOptions options = {&template_options, LTZ_LOCAL, 11, NULL, LM_VT_STRING};
value_pairs_foreach(vp, vp_keys_foreach, msg, &options, args);
cr_expect(test_key_found, "test.key is not found in the result set");
assert_keys_match_expected(scope, vp_keys_list, expected);
g_list_foreach(vp_keys_list, (GFunc) g_free, NULL);
g_list_free(vp_keys_list);
log_msg_unref(msg);
value_pairs_unref(vp);
}
void
transformers_testcase(const gchar *scope, const gchar *transformed_keys, const gchar *expected, GPtrArray *transformers)
{
ValuePairs *vp;
GList *vp_keys_list = NULL;
LogMessage *msg = create_message();
gpointer args[2];
gboolean test_key_found = FALSE;
vp = value_pairs_new(configuration);
value_pairs_add_scope(vp, scope);
if (transformers)
{
gint i;
ValuePairsTransformSet *vpts = value_pairs_transform_set_new(transformed_keys ? : "*");
for (i = 0; i < transformers->len; i++)
value_pairs_transform_set_add_func(vpts, g_ptr_array_index(transformers, i));
value_pairs_add_transforms(vp, vpts);
}
args[0] = &vp_keys_list;
args[1] = &test_key_found;
LogTemplateEvalOptions options = {&template_options, LTZ_LOCAL, 11, NULL, LM_VT_STRING};
value_pairs_foreach(vp, vp_keys_foreach, msg, &options, args);
assert_keys_match_expected(scope, vp_keys_list, expected);
g_list_foreach(vp_keys_list, (GFunc) g_free, NULL);
g_list_free(vp_keys_list);
log_msg_unref(msg);
value_pairs_unref(vp);
}
struct value_pairs_params
{
gchar *scope;
gchar *exclude;
gchar *expected;
GPtrArray *transformers;
};
ParameterizedTestParameters(value_pairs, test)
{
static const struct value_pairs_params params[] =
{
{ "rfc3164", NULL, "DATE,FACILITY,HOST,MESSAGE,PID,PRIORITY,PROGRAM", NULL },
{"rfc3164", NULL, "DATE,FACILITY,HOST,MESSAGE,PID,PRIORITY,PROGRAM", NULL },
{"core", NULL, "DATE,FACILITY,HOST,MESSAGE,PID,PRIORITY,PROGRAM", NULL },
{"base", NULL, "DATE,FACILITY,HOST,MESSAGE,PID,PRIORITY,PROGRAM", NULL },
{"rfc5424", NULL, ".SDATA.EventData@18372.4.Data,.SDATA.Keywords@18372.4.Keyword,.SDATA.meta.sequenceId,.SDATA.meta.sysUpTime,.SDATA.origin.ip,DATE,FACILITY,HOST,MESSAGE,MSGID,PID,PRIORITY,PROGRAM", NULL },
{"syslog-proto", NULL, ".SDATA.EventData@18372.4.Data,.SDATA.Keywords@18372.4.Keyword,.SDATA.meta.sequenceId,.SDATA.meta.sysUpTime,.SDATA.origin.ip,DATE,FACILITY,HOST,MESSAGE,MSGID,PID,PRIORITY,PROGRAM", NULL },
{"selected-macros", NULL, "DATE,FACILITY,HOST,MESSAGE,PID,PRIORITY,PROGRAM,SEQNUM,SOURCEIP,TAGS", NULL },
{"nv-pairs", NULL, "HOST,MESSAGE,MSGFORMAT,MSGID,PID,PROGRAM", NULL },
{"dot-nv-pairs", NULL, ".SDATA.EventData@18372.4.Data,.SDATA.Keywords@18372.4.Keyword,.SDATA.meta.sequenceId,.SDATA.meta.sysUpTime,.SDATA.origin.ip", NULL },
{"sdata", NULL, ".SDATA.EventData@18372.4.Data,.SDATA.Keywords@18372.4.Keyword,.SDATA.meta.sequenceId,.SDATA.meta.sysUpTime,.SDATA.origin.ip", NULL },
{"all-nv-pairs", NULL, ".SDATA.EventData@18372.4.Data,.SDATA.Keywords@18372.4.Keyword,.SDATA.meta.sequenceId,.SDATA.meta.sysUpTime,.SDATA.origin.ip,HOST,MESSAGE,MSGFORMAT,MSGID,PID,PROGRAM", NULL },
{"everything", NULL, ".SDATA.EventData@18372.4.Data,.SDATA.Keywords@18372.4.Keyword,.SDATA.meta.sequenceId,.SDATA.meta.sysUpTime,.SDATA.origin.ip,AMPM,BSDTAG,C_AMPM,C_DATE,C_DAY,C_FULLDATE,C_HOUR,C_HOUR12,C_ISODATE,C_ISOWEEK,C_MIN,C_MONTH,C_MONTH_ABBREV,C_MONTH_NAME,C_MONTH_WEEK,C_MSEC,C_SEC,C_STAMP,C_TZ,C_TZOFFSET,C_UNIXTIME,C_USEC,C_WEEK,C_WEEKDAY,C_WEEK_DAY,C_WEEK_DAY_ABBREV,C_WEEK_DAY_NAME,C_YE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Z,TZOFFSET,UNIXTIME,USEC,WEEK,WEEKDAY,WEEK_DAY,WEEK_DAY_ABBREV,WEEK_DAY_NAME,YEAR,YEAR_DAY", NULL },
{"nv-pairs", ".SDATA.*", "HOST,MESSAGE,MSGFORMAT,MSGID,PID,PROGRAM", NULL },
/* tests that the exclude patterns do not affect explicitly added
* keys. The testcase function adds a "test.key" and then checks if
* it is indeed present. Even if it would be excluded it still has
* to be in the result set. */
{"rfc3164", "test.*", "DATE,FACILITY,HOST,MESSAGE,PID,PRIORITY,PROGRAM", NULL },
/* tests that excluding works even when the key would be in the
* default set. */
{"nv-pairs", "MESSAGE", "HOST,MSGFORMAT,MSGID,PID,PROGRAM", NULL }
};
return cr_make_param_array(struct value_pairs_params, params, sizeof (params) / sizeof(params[0]));
}
ParameterizedTest(struct value_pairs_params *param, value_pairs, test)
{
testcase(param->scope, param->exclude, param->expected);
}
Test(value_pairs, test_transformers)
{
/* test the value-pair transformators */
GPtrArray *transformers = g_ptr_array_new();
g_ptr_array_add(transformers, value_pairs_new_transform_add_prefix("__"));
g_ptr_array_add(transformers, value_pairs_new_transform_shift(2));
g_ptr_array_add(transformers, value_pairs_new_transform_replace_prefix("C_", "CC_"));
transformers_testcase("everything", NULL,
".SDATA.EventData@18372.4.Data,.SDATA.Keywords@18372.4.Keyword,"
".SDATA.meta.sequenceId,.SDATA.meta.sysUpTime,.SDATA.origin.ip,"
"AMPM,BSDTAG,CC_AMPM,CC_DATE,CC_DAY,CC_FULLDATE,CC_HOUR,CC_HOUR12,"
"CC_ISODATE,CC_ISOWEEK,CC_MIN,CC_MONTH,CC_MONTH_ABBREV,CC_MONTH_NAME,"
"CC_MONTH_WEEK,CC_MSEC,CC_SEC,CC_STAMP,CC_TZ,CC_TZOFFSET,"
"CC_UNIXTIME,CC_USEC,CC_WEEK,CC_WEEKDAY,CC_WEEK_DAY,"
"CC_WEEK_DAY_ABBREV,CC_WEEK_DAY_NAME,CC_YEAR,CC_YEAR_DAY,"
"DATE,DAY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AR,S_YEAR_DAY,TAG,TAGS,TZ,TZOFFSET,UNIXTIME,USEC,WEEK,WEEKDAY,"
"WEEK_DAY,WEEK_DAY_ABBREV,WEEK_DAY_NAME,YEAR,YEAR_DAY",
transformers);
g_ptr_array_free(transformers, TRUE);
}
Test(value_pairs, test_transformer_shift_levels)
{
/* test the value-pair transformators */
GPtrArray *transformers = g_ptr_array_new();
/* remove . from before .SDATA prefix */
g_ptr_array_add(transformers, value_pairs_new_transform_shift_levels(1));
/* add a new prefix, would become .foo.bar.baz.SDATA */
g_ptr_array_add(transformers, value_pairs_new_transform_add_prefix(".foo.bar.baz."));
/* remove just added prefix, should start with SDATA without leading dot */
g_ptr_array_add(transformers, value_pairs_new_transform_shift_levels(4));
transformers_testcase("sdata", ".SDATA.meta.*",
".SDATA.EventData@18372.4.Data,.SDATA.Keywords@18372.4.Keyword,.SDATA.origin.ip,SDATA.meta.sequenceId,SDATA.meta.sysUpTime",
transformers);
g_ptr_array_free(transformers, TRUE);
}
Test(value_pairs, test_transformer_lower)
{
/* test the value-pair transformators */
GPtrArray *transformers = g_ptr_array_new();
g_ptr_array_add(transformers, value_pairs_new_transform_lower());
transformers_testcase("sdata", ".SDATA.meta.sequenceId",
".SDATA.EventData@18372.4.Data,.SDATA.Keywords@18372.4.Keyword,.SDATA.meta.sysUpTime,.SDATA.origin.ip,.sdata.meta.sequenceid",
transformers);
g_ptr_array_free(transformers, TRUE);
}
Test(value_pairs, test_transformer_upper)
{
/* test the value-pair transformators */
GPtrArray *transformers = g_ptr_array_new();
g_ptr_array_add(transformers, value_pairs_new_transform_upper());
transformers_testcase("sdata", ".SDATA.meta.sequenceId",
".SDATA.EventData@18372.4.Data,.SDATA.Keywords@18372.4.Keyword,.SDATA.META.SEQUENCEID,.SDATA.meta.sysUpTime,.SDATA.origin.ip",
transformers);
g_ptr_array_free(transformers, TRUE);
}
static ValuePairs *
_create_value_pairs_for_include_bytes_tc(void)
{
ValuePairs *vp = value_pairs_new(configuration);
value_pairs_add_scope(vp, "nv-pairs");
LogTemplate *template;
template = create_template(NULL, "$bytes");
value_pairs_add_pair(vp, "custom_bytes", template);
log_template_unref(template);
template = create_template(NULL, "$protobuf");
value_pairs_add_pair(vp, "custom_protobuf", template);
log_template_unref(template);
template = create_template("bytes", "$bytes");
value_pairs_add_pair(vp, "custom_explicit_bytes", template);
log_template_unref(template);
template = create_template("protobuf", "$protobuf");
value_pairs_add_pair(vp, "custom_explicit_protobuf", template);
log_template_unref(template);
return vp;
}
static gboolean
asserts_for_no_include_bytes_v4(const gchar *name, LogMessageValueType type, const gchar *value,
gsize value_len, gpointer user_data)
{
if (strcmp(name, "custom_bytes") == 0)
{
cr_assert_eq(type, LM_VT_NULL);
cr_assert_eq(value_len, 0);
}
else if (strcmp(name, "custom_protobuf") == 0)
{
cr_assert_eq(type, LM_VT_NULL);
cr_assert_eq(value_len, 0);
}
else
{
cr_assert(FALSE, "%s not expected", name);
}
return FALSE;
}
static gboolean
asserts_for_include_bytes_v4(const gchar *name, LogMessageValueType type, const gchar *value,
gsize value_len, gpointer user_data)
{
if (strcmp(name, "bytes") == 0)
{
cr_assert_eq(type, LM_VT_BYTES);
cr_assert_eq(value_len, 4);
cr_assert_eq(memcmp(value, "\0\1\2\3", 4), 0);
}
else if (strcmp(name, "protobuf") == 0)
{
cr_assert_eq(type, LM_VT_PROTOBUF);
cr_assert_eq(value_len, 4);
cr_assert_eq(memcmp(value, "\4\5\6\7", 4), 0);
}
else if (strcmp(name, "custom_bytes") == 0)
{
cr_assert_eq(type, LM_VT_NULL);
cr_assert_eq(value_len, 0);
}
else if (strcmp(name, "custom_protobuf") == 0)
{
cr_assert_eq(type, LM_VT_NULL);
cr_assert_eq(value_len, 0);
}
else if (strcmp(name, "custom_explicit_bytes") == 0)
{
cr_assert_eq(type, LM_VT_BYTES);
cr_assert_eq(value_len, 4);
cr_assert_eq(memcmp(value, "\0\1\2\3", 4), 0);
}
else if (strcmp(name, "custom_explicit_protobuf") == 0)
{
cr_assert_eq(type, LM_VT_PROTOBUF);
cr_assert_eq(value_len, 4);
cr_assert_eq(memcmp(value, "\4\5\6\7", 4), 0);
}
else
{
cr_assert(FALSE, "%s not expected", name);
}
return FALSE;
}
Test(value_pairs, test_include_bytes_v4)
{
cfg_set_version_without_validation(configuration, VERSION_VALUE_4_0);
ValuePairs *vp = _create_value_pairs_for_include_bytes_tc();
LogMessage *msg = log_msg_new_empty();
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);
LogTemplateEvalOptions options = {&template_options, LTZ_LOCAL, 11, NULL, LM_VT_STRING};
value_pairs_foreach(vp, asserts_for_no_include_bytes_v4, msg, &options, NULL);
value_pairs_set_include_bytes(vp, TRUE);
value_pairs_foreach(vp, asserts_for_include_bytes_v4, msg, &options, NULL);
log_msg_unref(msg);
value_pairs_unref(vp);
}
static gboolean
asserts_for_no_include_bytes_v3(const gchar *name, LogMessageValueType type, const gchar *value,
gsize value_len, gpointer user_data)
{
if (strcmp(name, "custom_bytes") == 0)
{
cr_assert_eq(type, LM_VT_STRING);
cr_assert_eq(value_len, 0);
}
else if (strcmp(name, "custom_protobuf") == 0)
{
cr_assert_eq(type, LM_VT_STRING);
cr_assert_eq(value_len, 0);
}
else
{
cr_assert(FALSE, "%s not expected", name);
}
return FALSE;
}
static gboolean
asserts_for_include_bytes_v3(const gchar *name, LogMessageValueType type, const gchar *value,
gsize value_len, gpointer user_data)
{
if (strcmp(name, "bytes") == 0)
{
cr_assert_eq(type, LM_VT_STRING);
cr_assert_eq(value_len, 4);
cr_assert_eq(memcmp(value, "\0\1\2\3", 4), 0);
}
else if (strcmp(name, "protobuf") == 0)
{
cr_assert_eq(type, LM_VT_STRING);
cr_assert_eq(value_len, 4);
cr_assert_eq(memcmp(value, "\4\5\6\7", 4), 0);
}
else if (strcmp(name, "custom_bytes") == 0)
{
cr_assert_eq(type, LM_VT_STRING);
cr_assert_eq(value_len, 0);
}
else if (strcmp(name, "custom_protobuf") == 0)
{
cr_assert_eq(type, LM_VT_STRING);
cr_assert_eq(value_len, 0);
}
else if (strcmp(name, "custom_explicit_bytes") == 0)
{
cr_assert_eq(type, LM_VT_BYTES);
cr_assert_eq(value_len, 4);
cr_assert_eq(memcmp(value, "\0\1\2\3", 4), 0);
}
else if (strcmp(name, "custom_explicit_protobuf") == 0)
{
cr_assert_eq(type, LM_VT_PROTOBUF);
cr_assert_eq(value_len, 4);
cr_assert_eq(memcmp(value, "\4\5\6\7", 4), 0);
}
else
{
cr_assert(FALSE, "%s not expected", name);
}
return FALSE;
}
Test(value_pairs, test_include_bytes_v3)
{
cfg_set_version_without_validation(configuration, VERSION_VALUE_3_38);
ValuePairs *vp = _create_value_pairs_for_include_bytes_tc();
LogMessage *msg = log_msg_new_empty();
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);
LogTemplateEvalOptions options = {&template_options, LTZ_LOCAL, 11, NULL, LM_VT_STRING};
value_pairs_foreach(vp, asserts_for_no_include_bytes_v3, msg, &options, NULL);
value_pairs_set_include_bytes(vp, TRUE);
value_pairs_foreach(vp, asserts_for_include_bytes_v3, msg, &options, NULL);
log_msg_unref(msg);
value_pairs_unref(vp);
}
void
setup(void)
{
app_startup();
setenv("TZ", "MET-1METDST", TRUE);
tzset();
configuration = cfg_new_snippet();
cfg_load_module(configuration, "syslogformat");
msg_format_options_defaults(&parse_options);
msg_format_options_init(&parse_options, configuration);
parse_options.flags |= LP_SYSLOG_PROTOCOL;
}
void
teardown(void)
{
cfg_free(configuration);
app_shutdown();
}
TestSuite(value_pairs, .init = setup, .fini = teardown);
|