Test(date, test_date_with_additional_text_at_the_end) { const gchar *msg = "2015-01-26T16:14:49+0300 Disappointing log file"; LogParser *parser = _construct_parser(NULL, NULL, LM_TS_STAMP); LogMessage *logmsg = _construct_logmsg(msg); gboolean success = log_parser_process(parser, &logmsg, NULL, log_msg_get_value(logmsg, LM_V_MESSAGE, NULL), -1); cr_assert_not(success, "successfully parsed but expected failure, msg=%s", msg); log_pipe_unref(&parser->super); log_msg_unref(logmsg); }
static void testcase(gchar *msg, gchar *timezone, gchar *format, gchar *expected) { LogTemplate *templ; LogMessage *logmsg; LogParser *parser; gboolean success; GString *res = g_string_sized_new(128); parser = date_parser_new (configuration); if (format != NULL) date_parser_set_format(parser, format); if (timezone != NULL) date_parser_set_timezone(parser, timezone); log_pipe_init(&parser->super); logmsg = log_msg_new_empty(); logmsg->timestamps[LM_TS_RECVD].tv_sec = 1451473200; /* Dec 30 2015 */ log_msg_set_value(logmsg, log_msg_get_value_handle("MESSAGE"), msg, -1); success = log_parser_process(parser, &logmsg, NULL, log_msg_get_value(logmsg, LM_V_MESSAGE, NULL), -1); if (!success && expected) { fprintf(stderr, "unable to parse format=%s msg=%s\n", format, msg); exit(1); } else if (success && !expected) { fprintf(stderr, "successfully parsed but expected failure, format=%s msg=%s\n", format, msg); exit(1); } else if (expected) { /* Convert to ISODATE */ templ = compile_template("${ISODATE}", FALSE); log_template_format(templ, logmsg, NULL, LTZ_LOCAL, -1, NULL, res); assert_nstring(res->str, res->len, expected, strlen(expected), "incorrect date parsed msg=%s format=%s", msg, format); log_template_unref(templ); } g_string_free(res, TRUE); log_pipe_unref(&parser->super); log_msg_unref(logmsg); return; }
static LogMessage * parse_kv_into_log_message_no_check(const gchar *kv) { LogMessage *msg; LogPathOptions path_options = LOG_PATH_OPTIONS_INIT; LogParser *cloned_parser; cloned_parser = (LogParser *) log_pipe_clone(&kv_parser->super); msg = log_msg_new_empty(); if (!log_parser_process(cloned_parser, &msg, &path_options, kv, strlen(kv))) { log_msg_unref(msg); log_pipe_unref(&cloned_parser->super); return NULL; } log_pipe_unref(&cloned_parser->super); return msg; }
static void testcase(gchar *msg, goffset offset, gchar *timezone, gchar *format, gchar *expected) { LogTemplate *templ; LogMessage *logmsg; NVTable *nvtable; GlobalConfig *cfg = cfg_new (0x302); LogParser *parser = date_parser_new (cfg); gboolean success; const gchar *context_id = "test-context-id"; GString *res = g_string_sized_new(128); date_parser_set_offset(parser, offset); if (format != NULL) date_parser_set_format(parser, format); if (timezone != NULL) date_parser_set_timezone(parser, timezone); parse_options.flags = 0; logmsg = log_msg_new_empty(); logmsg->timestamps[LM_TS_RECVD].tv_sec = 1438793384; /* Wed Aug 5 2015 */ log_msg_set_value(logmsg, log_msg_get_value_handle("MESSAGE"), msg, -1); nvtable = nv_table_ref(logmsg->payload); success = log_parser_process(parser, &logmsg, NULL, log_msg_get_value(logmsg, LM_V_MESSAGE, NULL), -1); nv_table_unref(nvtable); if (!success) { fprintf(stderr, "unable to parse offset=%d format=%s msg=%s\n", offset, format, msg); exit(1); } /* Convert to ISODATE */ templ = compile_template("${ISODATE}", FALSE); log_template_format(templ, logmsg, NULL, LTZ_LOCAL, 999, context_id, res); assert_nstring(res->str, res->len, expected, strlen(expected), "incorrect date parsed msg=%s offset=%d format=%s", msg, offset, format); log_template_unref(templ); g_string_free(res, TRUE); log_pipe_unref(&parser->super); log_msg_unref(logmsg); return; }
static void iterate_pattern(LogParser *p, const gchar *input) { LogMessage *msg; GTimeVal start, end; gint i; msg = _construct_msg(input); g_get_current_time(&start); for (i = 0; i < 100000; i++) { log_parser_process(p, &msg, NULL, log_msg_get_value(msg, LM_V_MESSAGE, NULL), -1); } log_msg_unref(msg); g_get_current_time(&end); printf(" %-90.*s speed: %12.3f msg/sec\n", (int) strlen(input), input, i * 1e6 / g_time_val_diff(&end, &start)); }
Test(date, test_date_with_guess_timezone) { const gchar *msg = "2015-12-30T12:00:00+05:00"; GString *res = g_string_sized_new(128); LogParser *parser = _construct_parser(NULL, NULL, LM_TS_STAMP); date_parser_process_flag(parser, "guess-timezone"); LogMessage *logmsg = _construct_logmsg(msg); gboolean success = log_parser_process(parser, &logmsg, NULL, log_msg_get_value(logmsg, LM_V_MESSAGE, NULL), -1); cr_assert(success, "failed to parse timestamp, msg=%s", msg); append_format_unix_time(&logmsg->timestamps[LM_TS_STAMP], res, TS_FMT_ISO, -1, 0); /* this should fix up the timezone */ cr_assert_str_eq(res->str, "2015-12-30T12:00:00+01:00", "incorrect date parsed msg=%s result=%s", msg, res->str); log_pipe_unref(&parser->super); log_msg_unref(logmsg); g_string_free(res, TRUE); }
int testcase(gchar *msg, guint parse_flags, gint max_columns, guint32 flags, gchar *delimiters, gchar *quotes, gchar *null_value, gchar *first_value, ...) { LogMessage *logmsg; LogColumnParser *p; gchar *expected_value; gint i; va_list va; NVTable *nvtable; const gchar *column_array[] = { "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13", "C14", "C15", "C16", "C17", "C18", "C19", "C20", "C21", "C22", "C23", "C24", "C25", "C26", "C27", "C28", "C29", "C30", NULL }; gboolean success; if (max_columns != -1) { g_assert(max_columns < (sizeof(column_array) / sizeof(column_array[0]))); column_array[max_columns] = NULL; } parse_options.flags = parse_flags; logmsg = log_msg_new(msg, strlen(msg), NULL, &parse_options); p = log_csv_parser_new(); log_csv_parser_set_flags(p, flags); log_column_parser_set_columns(p, string_array_to_list(column_array)); if (delimiters) log_csv_parser_set_delimiters(p, delimiters); if (quotes) log_csv_parser_set_quote_pairs(p, quotes); if (null_value) log_csv_parser_set_null_value(p, null_value); nvtable = nv_table_ref(logmsg->payload); success = log_parser_process(&p->super, &logmsg, NULL, log_msg_get_value(logmsg, LM_V_MESSAGE, NULL), -1); nv_table_unref(nvtable); if (success && !first_value) { fprintf(stderr, "unexpected match; msg=%s\n", msg); exit(1); } if (!success && first_value) { fprintf(stderr, "unexpected non-match; msg=%s\n", msg); exit(1); } log_pipe_unref(&p->super.super); va_start(va, first_value); expected_value = first_value; i = 0; while (expected_value && column_array[i]) { const gchar *value; gssize value_len; value = log_msg_get_value(logmsg, log_msg_get_value_handle(column_array[i]), &value_len); if (expected_value && expected_value[0]) { TEST_ASSERT(value && value[0], "expected value set, but no actual value"); TEST_ASSERT(strlen(expected_value) == value_len, "value length doesn't match actual length"); TEST_ASSERT(strncmp(value, expected_value, value_len) == 0, "value does not match expected value"); } else { TEST_ASSERT(!(value && value[0]), "expected unset, but actual value present"); } expected_value = va_arg(va, char *); i++; } log_msg_unref(logmsg); return 1; }
void setup(void) { app_startup(); setlocale (LC_ALL, "C"); setenv("TZ", "CET-1", TRUE); tzset(); configuration = cfg_new_snippet(); /* year heuristics depends on the current time */ /* Dec 30 2015 */ GTimeVal faked_time = { .tv_sec = 1451473200, .tv_usec = 0 }; set_cached_time(&faked_time); } void teardown(void) { app_shutdown(); } TestSuite(date, .init = setup, .fini = teardown); ParameterizedTestParameters(date, test_date_parser) { static struct date_params params[] = { { "2015-01-26T16:14:49+03:00", NULL, NULL, LM_TS_RECVD, "2015-01-26T16:14:49+03:00" }, /* Various ISO8601 formats */ { "2015-01-26T16:14:49+0300", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+03:00" }, { "2015-01-26T16:14:49+0330", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+03:30" }, { "2015-01-26T16:14:49+0200", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+02:00" }, { "2015-01-26T16:14:49+03:00", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+03:00" }, { "2015-01-26T16:14:49+03:30", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+03:30" }, { "2015-01-26T16:14:49+02:00", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+02:00" }, { "2015-01-26T16:14:49Z", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+00:00" }, { "2015-01-26T16:14:49A", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49-01:00" }, { "2015-01-26T16:14:49B", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49-02:00" }, { "2015-01-26T16:14:49N", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+01:00" }, { "2015-01-26T16:14:49O", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+02:00" }, { "2015-01-26T16:14:49GMT", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+00:00" }, { "2015-01-26T16:14:49PDT", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49-07:00" }, /* RFC 2822 */ { "Tue, 27 Jan 2015 11:48:46 +0200", NULL, "%a, %d %b %Y %T %z", LM_TS_STAMP, "2015-01-27T11:48:46+02:00" }, /* Apache-like */ { "21/Jan/2015:14:40:07 +0500", NULL, "%d/%b/%Y:%T %z", LM_TS_STAMP, "2015-01-21T14:40:07+05:00" }, /* Dates without timezones. America/Phoenix has no DST */ { "Tue, 27 Jan 2015 11:48:46", NULL, "%a, %d %b %Y %T", LM_TS_STAMP, "2015-01-27T11:48:46+01:00" }, { "Tue, 27 Jan 2015 11:48:46", "America/Phoenix", "%a, %d %b %Y %T", LM_TS_STAMP, "2015-01-27T11:48:46-07:00" }, { "Tue, 27 Jan 2015 11:48:46", "+05:00", "%a, %d %b %Y %T", LM_TS_STAMP, "2015-01-27T11:48:46+05:00" }, /* Try without the year. */ { "01/Jan:00:40:07 +0500", NULL, "%d/%b:%T %z", LM_TS_STAMP, "2016-01-01T00:40:07+05:00" }, { "01/Aug:00:40:07 +0500", NULL, "%d/%b:%T %z", LM_TS_STAMP, "2015-08-01T00:40:07+05:00" }, { "01/Sep:00:40:07 +0500", NULL, "%d/%b:%T %z", LM_TS_STAMP, "2015-09-01T00:40:07+05:00" }, { "01/Oct:00:40:07 +0500", NULL, "%d/%b:%T %z", LM_TS_STAMP, "2015-10-01T00:40:07+05:00" }, { "01/Nov:00:40:07 +0500", NULL, "%d/%b:%T %z", LM_TS_STAMP, "2015-11-01T00:40:07+05:00" }, { "1446128356 +01:00", NULL, "%s %z", LM_TS_STAMP, "2015-10-29T15:19:16+01:00" }, { "1446128356", "Europe/Budapest", "%s", LM_TS_STAMP, "2015-10-29T15:19:16+01:00" }, }; return cr_make_param_array(struct date_params, params, sizeof(params) / sizeof(struct date_params)); } ParameterizedTest(struct date_params *params, date, test_date_parser) { LogMessage *logmsg; LogParser *parser = _construct_parser(params->timezone_, params->format, params->time_stamp); gboolean success; GString *res = g_string_sized_new(128); logmsg = _construct_logmsg(params->msg); success = log_parser_process(parser, &logmsg, NULL, log_msg_get_value(logmsg, LM_V_MESSAGE, NULL), -1); cr_assert(success, "unable to parse format=%s msg=%s", params->format, params->msg); append_format_unix_time(&logmsg->timestamps[params->time_stamp], res, TS_FMT_ISO, -1, 0); cr_assert_str_eq(res->str, params->expected, "incorrect date parsed msg=%s format=%s, result=%s, expected=%s", params->msg, params->format, res->str, params->expected); g_string_free(res, TRUE); log_pipe_unref(&parser->super); log_msg_unref(logmsg); }