From f35ed2a01d56cd0cb2390240cd4e97de318335c5 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Tue, 28 Aug 2018 12:45:23 +0300 Subject: [PATCH] lib: Add unit test for event log prefix handling Some of the tests are commented out until they're fixed in a following commit. --- src/lib/Makefile.am | 1 + src/lib/test-event-log.c | 187 +++++++++++++++++++++++++++++++++++++++ src/lib/test-lib.inc | 1 + 3 files changed, 189 insertions(+) create mode 100644 src/lib/test-event-log.c diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 36b51b2ffa..b8e2c8cea2 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -340,6 +340,7 @@ test_lib_SOURCES = \ test-crc32.c \ test-data-stack.c \ test-event-filter.c \ + test-event-log.c \ test-failures.c \ test-file-create-locked.c \ test-guid.c \ diff --git a/src/lib/test-event-log.c b/src/lib/test-event-log.c new file mode 100644 index 0000000000..e0f4803773 --- /dev/null +++ b/src/lib/test-event-log.c @@ -0,0 +1,187 @@ +/* Copyright (c) 2018 Dovecot authors, see the included COPYING file */ + +#include "test-lib.h" +#include "ioloop.h" +#include "str.h" +#include "failures-private.h" + +enum test_log_prefix_type { + TYPE_END, + TYPE_APPEND, + TYPE_REPLACE, + TYPE_SKIP, +}; + +struct test_log_prefix { + enum test_log_prefix_type type; + const char *str; +}; + +struct test_log { + const struct test_log_prefix *prefixes; + const char *global_log_prefix; + const char *result; +}; + +static char *test_output; + +static void ATTR_FORMAT(2, 0) +info_handler(const struct failure_context *ctx, + const char *format, va_list args) +{ + size_t prefix_len; + + i_assert(ctx->type == LOG_TYPE_INFO); + + i_free(test_output); + T_BEGIN { + string_t *str = failure_handler.v->format(ctx, &prefix_len, + format, args); + test_output = i_strdup(str_c(str)); + } T_END; +} + +static void test_event_log_prefix(void) +{ + struct test_log tests[] = { + { + .prefixes = (const struct test_log_prefix []) { + { .type = TYPE_END } + }, + .global_log_prefix = "global1.", + .result = "global1.Info: TEXT", + }, + { + .prefixes = (const struct test_log_prefix []) { + { TYPE_REPLACE, "replaced1," }, + { .type = TYPE_END } + }, + .result = "replaced1,Info: TEXT", + }, + { + .prefixes = (const struct test_log_prefix []) { + { TYPE_REPLACE, "replaced1," }, + { TYPE_REPLACE, "replaced2." }, + { .type = TYPE_END } + }, + .result = "replaced2.Info: TEXT", + }, + /*{ + .prefixes = (const struct test_log_prefix []) { + { TYPE_REPLACE, "replaced1," }, + { TYPE_APPEND, "appended2." }, + { .type = TYPE_END } + }, + .result = "replaced1,Info: appended2.TEXT", + },*/ + { + .prefixes = (const struct test_log_prefix []) { + { TYPE_APPEND, "appended1," }, + { TYPE_REPLACE, "replaced1," }, + { .type = TYPE_END } + }, + .result = "replaced1,Info: TEXT", + }, + { + .prefixes = (const struct test_log_prefix []) { + { TYPE_APPEND, "appended1," }, + { .type = TYPE_END } + }, + .global_log_prefix = "global2.", + .result = "global2.Info: appended1,TEXT", + }, + { + .prefixes = (const struct test_log_prefix []) { + { TYPE_APPEND, "appended1," }, + { TYPE_APPEND, "appended2." }, + { .type = TYPE_END } + }, + .global_log_prefix = "global3.", + .result = "global3.Info: appended1,appended2.TEXT", + }, + /*{ + .prefixes = (const struct test_log_prefix []) { + { TYPE_APPEND, "appended1," }, + { TYPE_REPLACE, "replaced2." }, + { TYPE_APPEND, "appended3#" }, + { .type = TYPE_END } + }, + .result = "replaced2.Info: appended3#TEXT", + },*/ + { + .prefixes = (const struct test_log_prefix []) { + { TYPE_APPEND, "appended1," }, + { TYPE_REPLACE, "replaced2." }, + { TYPE_APPEND, "appended3#" }, + { TYPE_REPLACE, "replaced4;" }, + { .type = TYPE_END } + }, + .result = "replaced4;Info: TEXT", + }, + /*{ + .prefixes = (const struct test_log_prefix []) { + { TYPE_APPEND, "appended1," }, + { TYPE_REPLACE, "replaced2." }, + { TYPE_APPEND, "appended3#" }, + { TYPE_REPLACE, "replaced4;" }, + { TYPE_APPEND, "appended5-" }, + { .type = TYPE_END } + }, + .result = "replaced4;Info: appended5-TEXT", + },*/ + }; + const struct event_log_params params = { + .log_type = LOG_TYPE_INFO, + }; + + test_begin("event log prefixes"); + + failure_callback_t *orig_fatal, *orig_error, *orig_info, *orig_debug; + i_get_failure_handlers(&orig_fatal, &orig_error, &orig_info, &orig_debug); + i_set_info_handler(info_handler); + for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) { + const struct test_log *test = &tests[i]; + + if (test->global_log_prefix != NULL) + i_set_failure_prefix("%s", test->global_log_prefix); + else + i_set_failure_prefix("UNEXPECTED GLOBAL PREFIX"); + + struct event *event, *parent; + event = parent = event_create(NULL); + for (unsigned int j = 0; test->prefixes[j].type != TYPE_END; j++) { + if (event == NULL) { + struct event *child = event_create(parent); + event_unref(&parent); + event = parent = child; + } + switch (test->prefixes[j].type) { + case TYPE_END: + i_unreached(); + case TYPE_APPEND: + event_set_append_log_prefix(event, test->prefixes[j].str); + break; + case TYPE_REPLACE: + event_replace_log_prefix(event, test->prefixes[j].str); + break; + case TYPE_SKIP: + break; + } + event = NULL; + } + event = parent; + event_log(event, ¶ms, "TEXT"); + + test_assert_strcmp(test->result, test_output); + event_unref(&event); + } + i_set_info_handler(orig_info); + i_unset_failure_prefix(); + i_free(test_output); + test_end(); +} + +void test_event_log(void) +{ + test_event_log_prefix(); +} diff --git a/src/lib/test-lib.inc b/src/lib/test-lib.inc index 74ea8a692b..bf97c95842 100644 --- a/src/lib/test-lib.inc +++ b/src/lib/test-lib.inc @@ -15,6 +15,7 @@ TEST(test_crc32) TEST(test_data_stack) FATAL(fatal_data_stack) TEST(test_event_filter) +TEST(test_event_log) TEST(test_failures) TEST(test_file_create_locked) TEST(test_guid)