From c194692e46103f1c0205d23bb0814df8ecac41f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20V=C3=A1rady?= Date: Fri, 12 Aug 2016 15:43:33 +0200 Subject: [PATCH] logmsg/tests/test_log_message: switch to Criterion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: László Várady --- lib/logmsg/tests/Makefile.am | 11 +- lib/logmsg/tests/test_log_message.c | 340 +++++++++++++--------------- 2 files changed, 158 insertions(+), 193 deletions(-) diff --git a/lib/logmsg/tests/Makefile.am b/lib/logmsg/tests/Makefile.am index 2fe7076a70a..24c277aaa5a 100644 --- a/lib/logmsg/tests/Makefile.am +++ b/lib/logmsg/tests/Makefile.am @@ -1,5 +1,4 @@ lib_logmsg_tests_TESTS = \ - lib/logmsg/tests/test_log_message \ lib/logmsg/tests/test_logmsg_serialize \ lib/logmsg/tests/test_timestamp_serialize \ lib/logmsg/tests/test_tags @@ -7,10 +6,6 @@ lib_logmsg_tests_TESTS = \ check_PROGRAMS += ${lib_logmsg_tests_TESTS} -lib_logmsg_tests_test_log_message_CFLAGS = $(TEST_CFLAGS) -lib_logmsg_tests_test_log_message_LDADD = $(TEST_LDADD) - - lib_logmsg_tests_test_timestamp_serialize_CFLAGS = $(TEST_CFLAGS) lib_logmsg_tests_test_timestamp_serialize_LDADD = $(TEST_LDADD) @@ -24,7 +19,8 @@ if ENABLE_CRITERION lib_logmsg_tests_TESTS += \ lib/logmsg/tests/test_nvtable \ - lib/logmsg/tests/test_gsockaddr_serialize + lib/logmsg/tests/test_gsockaddr_serialize \ + lib/logmsg/tests/test_log_message lib_logmsg_tests_test_nvtable_CFLAGS = $(TEST_CFLAGS) lib_logmsg_tests_test_nvtable_LDADD = $(TEST_LDADD) @@ -32,4 +28,7 @@ lib_logmsg_tests_test_nvtable_LDADD = $(TEST_LDADD) lib_logmsg_tests_test_gsockaddr_serialize_CFLAGS = $(TEST_CFLAGS) lib_logmsg_tests_test_gsockaddr_serialize_LDADD = $(TEST_LDADD) +lib_logmsg_tests_test_log_message_CFLAGS = $(TEST_CFLAGS) +lib_logmsg_tests_test_log_message_LDADD = $(TEST_LDADD) + endif diff --git a/lib/logmsg/tests/test_log_message.c b/lib/logmsg/tests/test_log_message.c index 4563080e0d2..d088655c9af 100644 --- a/lib/logmsg/tests/test_log_message.c +++ b/lib/logmsg/tests/test_log_message.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2013 Balabit + * Copyright (c) 2002-2016 Balabit * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,6 +20,9 @@ * COPYING for details. * */ + +#include + #include "msg_parse_lib.h" #include "apphook.h" #include "logpipe.h" @@ -28,8 +31,17 @@ #include -LogMessage * -construct_log_message(void) +typedef struct _LogMessageTestParams +{ + LogMessage *message; + LogMessage *cloned_message; + NVHandle nv_handle; + NVHandle sd_handle; + const gchar *tag_name; +} LogMessageTestParams; + +static LogMessage * +_construct_log_message(void) { const gchar *raw_msg = "foo"; LogMessage *msg; @@ -39,149 +51,171 @@ construct_log_message(void) return msg; } -LogMessage * -clone_cow_log_message(LogMessage *msg) +static LogMessage * +_construct_merge_base_message(void) { - LogPathOptions path_options = LOG_PATH_OPTIONS_INIT; + LogMessage *msg; - return log_msg_clone_cow(msg, &path_options); + msg = log_msg_new_empty(); + log_msg_set_value_by_name(msg, "base", "basevalue", -1); + log_msg_set_tag_by_name(msg, "basetag"); + return msg; } -static void -test_log_message_can_be_created_and_freed(void) +static LogMessage * +_construct_merged_message(const gchar *name, const gchar *value) { - LogMessage *msg = construct_log_message(); - log_msg_unref(msg); -} + LogMessage *msg; -NVHandle nv_handle; -NVHandle sd_handle; -const gchar *tag_name = "tag"; + msg = log_msg_new_empty(); + log_msg_set_value_by_name(msg, name, value, -1); + log_msg_set_tag_by_name(msg, "mergedtag"); + return msg; +} -LogMessage * -construct_log_message_with_all_bells_and_whistles(void) +static void +assert_log_msg_clear_clears_all_properties(LogMessage *message, NVHandle nv_handle, + NVHandle sd_handle, const gchar *tag_name) { - LogMessage *msg = construct_log_message(); + log_msg_clear(message); - nv_handle = log_msg_get_value_handle("foo"); - sd_handle = log_msg_get_value_handle(".SDATA.foo.bar"); + cr_assert_str_empty(log_msg_get_value(message, nv_handle, NULL), + "Message still contains value after log_msg_clear"); - log_msg_set_value(msg, nv_handle, "value", -1); - log_msg_set_value(msg, sd_handle, "value", -1); - msg->saddr = g_sockaddr_inet_new("1.2.3.4", 5050); - log_msg_set_tag_by_name(msg, tag_name); - return msg; + cr_assert_str_empty(log_msg_get_value(message, sd_handle, NULL), + "Message still contains sdata value after log_msg_clear"); + + cr_assert_null(message->saddr, "Message still contains an saddr after log_msg_clear"); + cr_assert_not(log_msg_is_tag_by_name(message, tag_name), + "Message still contains a valid tag after log_msg_clear"); } -void -assert_log_msg_clear_clears_all_properties(LogMessage *msg) +static void +assert_sdata_value_with_seqnum_equals(LogMessage *msg, guint32 seq_num, const gchar *expected) { - log_msg_clear(msg); + GString *result = g_string_sized_new(0); - assert_string("", log_msg_get_value(msg, nv_handle, NULL), "Message still contains value after log_msg_clear"); - assert_string("", log_msg_get_value(msg, sd_handle, NULL), "Message still contains sdata value after log_msg_clear"); - assert_true(msg->saddr == NULL, "Message still contains an saddr after log_msg_clear"); - assert_false(log_msg_is_tag_by_name(msg, tag_name), "Message still contains a valid tag after log_msg_clear"); + log_msg_append_format_sdata(msg, result, seq_num); + cr_assert_str_eq(result->str, expected, "SDATA value does not match"); + g_string_free(result, TRUE); } static void -test_log_message_can_be_cleared(void) +assert_sdata_value_equals(LogMessage *msg, const gchar *expected) { - LogMessage *msg, *cloned; + assert_sdata_value_with_seqnum_equals(msg, 0, expected); +} - msg = construct_log_message_with_all_bells_and_whistles(); - cloned = clone_cow_log_message(msg); +static LogMessageTestParams * +log_message_test_params_new(void) +{ + LogMessageTestParams *params = g_new0(LogMessageTestParams, 1); - assert_log_msg_clear_clears_all_properties(cloned); - log_msg_unref(cloned); + params->tag_name = "tag"; + params->message = _construct_log_message(); - assert_log_msg_clear_clears_all_properties(msg); - log_msg_unref(msg); + params->nv_handle = log_msg_get_value_handle("foo"); + params->sd_handle = log_msg_get_value_handle(".SDATA.foo.bar"); + + log_msg_set_value(params->message, params->nv_handle, "value", -1); + log_msg_set_value(params->message, params->sd_handle, "value", -1); + params->message->saddr = g_sockaddr_inet_new("1.2.3.4", 5050); + log_msg_set_tag_by_name(params->message, params->tag_name); + + return params; } +void +log_message_test_params_free(LogMessageTestParams *params) +{ + log_msg_unref(params->message); -PersistState *state; + if (params->cloned_message) + log_msg_unref(params->cloned_message); -static void -setup_rcptid_test(void) + g_free(params); +} + +LogMessage * +log_message_test_params_clone_message(LogMessageTestParams *params) { - state = clean_and_create_persist_state_for_test("test_values.persist"); - rcptid_init(state, TRUE); + LogPathOptions path_options = LOG_PATH_OPTIONS_INIT; + params->cloned_message = log_msg_clone_cow(params->message, &path_options); + + return params->cloned_message; } -static void -teardown_rcptid_test(void) + +void +setup(void) { - commit_and_destroy_persist_state(state); - rcptid_deinit(); + app_startup(); + init_and_load_syslogformat_module(); } -static void -test_rcptid_is_automatically_assigned_to_a_newly_created_log_message(void) +void +teardown(void) { - LogMessage *msg; - - setup_rcptid_test(); - msg = log_msg_new_empty(); - assert_guint64(msg->rcptid, 1, "rcptid is not automatically set"); - log_msg_unref(msg); - teardown_rcptid_test(); + deinit_syslogformat_module(); + app_shutdown(); } +TestSuite(log_message, .init = setup, .fini = teardown); -void -test_log_message(void) +Test(log_message, test_log_message_can_be_created_and_freed) { - MSG_TESTCASE(test_log_message_can_be_created_and_freed); - MSG_TESTCASE(test_log_message_can_be_cleared); - MSG_TESTCASE(test_rcptid_is_automatically_assigned_to_a_newly_created_log_message); + LogMessage *msg = _construct_log_message(); + log_msg_unref(msg); } -static LogMessage * -construct_merge_base_message(void) +Test(log_message, test_log_message_can_be_cleared) { - LogMessage *msg; + LogMessageTestParams *params = log_message_test_params_new(); - msg = log_msg_new_empty(); - log_msg_set_value_by_name(msg, "base", "basevalue", -1); - log_msg_set_tag_by_name(msg, "basetag"); - return msg; + log_message_test_params_clone_message(params); + + assert_log_msg_clear_clears_all_properties(params->message, params->nv_handle, + params->sd_handle, params->tag_name); + assert_log_msg_clear_clears_all_properties(params->cloned_message, params->nv_handle, + params->sd_handle, params->tag_name); + + log_message_test_params_free(params); } -static LogMessage * -construct_merged_message(const gchar *name, const gchar *value) +Test(log_message, test_rcptid_is_automatically_assigned_to_a_newly_created_log_message) { LogMessage *msg; + PersistState *state = clean_and_create_persist_state_for_test("test_values.persist"); + rcptid_init(state, TRUE); msg = log_msg_new_empty(); - log_msg_set_value_by_name(msg, name, value, -1); - log_msg_set_tag_by_name(msg, "mergedtag"); - return msg; + cr_assert_eq(msg->rcptid, 1, "rcptid is not automatically set"); + log_msg_unref(msg); + + commit_and_destroy_persist_state(state); + rcptid_deinit(); } -static void -test_log_message_merge_with_empty_context(void) +Test(log_message, test_log_message_merge_with_empty_context) { - LogMessage *msg, *msg_clone; + LogMessageTestParams *params = log_message_test_params_new(); LogMessage *context[] = {}; - msg = construct_log_message_with_all_bells_and_whistles(); - msg_clone = clone_cow_log_message(msg); - log_msg_merge_context(msg, context, 0); - log_msg_unref(msg); - assert_log_messages_equal(msg, msg_clone); - log_msg_unref(msg_clone); -} + log_message_test_params_clone_message(params); + log_msg_merge_context(params->message, context, 0); + assert_log_messages_equal(params->message, params->cloned_message); -static void -test_log_message_merge_unset_value(void) + log_message_test_params_free(params); +} + +Test(log_message, test_log_message_merge_unset_value) { LogMessage *msg; GPtrArray *context = g_ptr_array_sized_new(0); - msg = construct_merge_base_message(); - g_ptr_array_add(context, construct_merged_message("merged", "mergedvalue")); + msg = _construct_merge_base_message(); + g_ptr_array_add(context, _construct_merged_message("merged", "mergedvalue")); log_msg_merge_context(msg, (LogMessage **) context->pdata, context->len); assert_log_message_value_by_name(msg, "base", "basevalue"); @@ -191,14 +225,13 @@ test_log_message_merge_unset_value(void) log_msg_unref(msg); } -static void -test_log_message_merge_doesnt_overwrite_already_set_values(void) +Test(log_message, test_log_message_merge_doesnt_overwrite_already_set_values) { LogMessage *msg; GPtrArray *context = g_ptr_array_sized_new(0); - msg = construct_merge_base_message(); - g_ptr_array_add(context, construct_merged_message("base", "mergedvalue")); + msg = _construct_merge_base_message(); + g_ptr_array_add(context, _construct_merged_message("base", "mergedvalue")); log_msg_merge_context(msg, (LogMessage **) context->pdata, context->len); assert_log_message_value_by_name(msg, "base", "basevalue"); @@ -207,15 +240,14 @@ test_log_message_merge_doesnt_overwrite_already_set_values(void) log_msg_unref(msg); } -static void -test_log_message_merge_merges_the_closest_value_in_the_context(void) +Test(log_message, test_log_message_merge_merges_the_closest_value_in_the_context) { LogMessage *msg; GPtrArray *context = g_ptr_array_sized_new(0); - msg = construct_merge_base_message(); - g_ptr_array_add(context, construct_merged_message("merged", "mergedvalue1")); - g_ptr_array_add(context, construct_merged_message("merged", "mergedvalue2")); + msg = _construct_merge_base_message(); + g_ptr_array_add(context, _construct_merged_message("merged", "mergedvalue1")); + g_ptr_array_add(context, _construct_merged_message("merged", "mergedvalue2")); log_msg_merge_context(msg, (LogMessage **) context->pdata, context->len); assert_log_message_value_by_name(msg, "merged", "mergedvalue2"); @@ -224,16 +256,15 @@ test_log_message_merge_merges_the_closest_value_in_the_context(void) log_msg_unref(msg); } -static void -test_log_message_merge_merges_from_all_messages_in_the_context(void) +Test(log_message, test_log_message_merge_merges_from_all_messages_in_the_context) { LogMessage *msg; GPtrArray *context = g_ptr_array_sized_new(0); - msg = construct_merge_base_message(); - g_ptr_array_add(context, construct_merged_message("merged1", "mergedvalue1")); - g_ptr_array_add(context, construct_merged_message("merged2", "mergedvalue2")); - g_ptr_array_add(context, construct_merged_message("merged3", "mergedvalue3")); + msg = _construct_merge_base_message(); + g_ptr_array_add(context, _construct_merged_message("merged1", "mergedvalue1")); + g_ptr_array_add(context, _construct_merged_message("merged2", "mergedvalue2")); + g_ptr_array_add(context, _construct_merged_message("merged3", "mergedvalue3")); log_msg_merge_context(msg, (LogMessage **) context->pdata, context->len); assert_log_message_value_by_name(msg, "merged1", "mergedvalue1"); @@ -244,14 +275,13 @@ test_log_message_merge_merges_from_all_messages_in_the_context(void) log_msg_unref(msg); } -static void -test_log_message_merge_leaves_base_tags_intact(void) +Test(log_message, test_log_message_merge_leaves_base_tags_intact) { LogMessage *msg; GPtrArray *context = g_ptr_array_sized_new(0); - msg = construct_merge_base_message(); - g_ptr_array_add(context, construct_merged_message("merged1", "mergedvalue1")); + msg = _construct_merge_base_message(); + g_ptr_array_add(context, _construct_merged_message("merged1", "mergedvalue1")); log_msg_merge_context(msg, (LogMessage **) context->pdata, context->len); assert_log_message_has_tag(msg, "basetag"); @@ -261,31 +291,19 @@ test_log_message_merge_leaves_base_tags_intact(void) log_msg_unref(msg); } -static void -test_log_message_merge(void) +Test(log_message, test_log_msg_set_value_indirect_with_self_referencing_handle_results_in_a_nonindirect_value) { - MSG_TESTCASE(test_log_message_merge_with_empty_context); - MSG_TESTCASE(test_log_message_merge_unset_value); - MSG_TESTCASE(test_log_message_merge_doesnt_overwrite_already_set_values); - MSG_TESTCASE(test_log_message_merge_merges_the_closest_value_in_the_context); - MSG_TESTCASE(test_log_message_merge_merges_from_all_messages_in_the_context); - MSG_TESTCASE(test_log_message_merge_leaves_base_tags_intact); -} - -static void -test_log_msg_set_value_indirect_with_self_referencing_handle_results_in_a_nonindirect_value(void) -{ - LogMessage *msg; + LogMessageTestParams *params = log_message_test_params_new(); gssize value_len; - msg = construct_log_message_with_all_bells_and_whistles(); - log_msg_set_value_indirect(msg, nv_handle, nv_handle, 0, 0, 5); - assert_string(log_msg_get_value(msg, nv_handle, &value_len), "value", "indirect self-reference value doesn't match"); - log_msg_unref(msg); + log_msg_set_value_indirect(params->message, params->nv_handle, params->nv_handle, 0, 0, 5); + cr_assert_str_eq(log_msg_get_value(params->message, params->nv_handle, &value_len), "value", + "indirect self-reference value doesn't match"); + + log_message_test_params_free(params); } -static void -test_log_msg_get_value_with_time_related_macro(void) +Test(log_message, test_log_msg_get_value_with_time_related_macro) { LogMessage *msg; gssize value_len; @@ -297,48 +315,22 @@ test_log_msg_get_value_with_time_related_macro(void) handle = log_msg_get_value_handle("ISODATE"); date_value = log_msg_get_value(msg, handle, &value_len); - assert_string(date_value, "2014-01-15T10:57:23-00:00", "ISODATE macro value does not match!"); + cr_assert_str_eq(date_value, "2014-01-15T10:57:23-00:00", "ISODATE macro value does not match!"); log_msg_unref(msg); } -static void -test_local_logmsg_created_with_the_right_flags_and_timestamps(void) +Test(log_message, test_local_logmsg_created_with_the_right_flags_and_timestamps) { LogMessage *msg = log_msg_new_local(); gboolean are_equals = log_stamp_eq(&msg->timestamps[LM_TS_STAMP], &msg->timestamps[LM_TS_RECVD]); - assert_true((msg->flags & LF_LOCAL) != 0, "LogMessage created by log_msg_new_local() should have LF_LOCAL flag set"); - assert_true(are_equals, "The timestamps in a LogMessage created by log_msg_new_local() should be equals"); -} - -static void -test_misc_stuff(void) -{ - MSG_TESTCASE(test_log_msg_get_value_with_time_related_macro); - MSG_TESTCASE(test_log_msg_set_value_indirect_with_self_referencing_handle_results_in_a_nonindirect_value); - MSG_TESTCASE(test_local_logmsg_created_with_the_right_flags_and_timestamps); -} - -static void -assert_sdata_value_with_seqnum_equals(LogMessage *msg, guint32 seq_num, const gchar *expected) -{ - GString *result = g_string_sized_new(0); - - log_msg_append_format_sdata(msg, result, seq_num); - assert_string(result->str, expected, "SDATA value does not match"); - g_string_free(result, TRUE); -} - -static void -assert_sdata_value_equals(LogMessage *msg, const gchar *expected) -{ - assert_sdata_value_with_seqnum_equals(msg, 0, expected); + cr_assert_neq((msg->flags & LF_LOCAL), 0, "LogMessage created by log_msg_new_local() should have LF_LOCAL flag set"); + cr_assert(are_equals, "The timestamps in a LogMessage created by log_msg_new_local() should be equals"); } -static void -test_sdata_value_is_updated_by_sdata_name_value_pairs(void) +Test(log_message, test_sdata_value_is_updated_by_sdata_name_value_pairs) { LogMessage *msg; @@ -356,8 +348,7 @@ test_sdata_value_is_updated_by_sdata_name_value_pairs(void) log_msg_unref(msg); } -static void -test_sdata_seqnum_adds_meta_sequence_id(void) +Test(log_message, test_sdata_seqnum_adds_meta_sequence_id) { LogMessage *msg; @@ -371,8 +362,7 @@ test_sdata_seqnum_adds_meta_sequence_id(void) log_msg_unref(msg); } -static void -test_sdata_value_omits_unset_values(void) +Test(log_message, test_sdata_value_omits_unset_values) { LogMessage *msg; @@ -388,27 +378,3 @@ test_sdata_value_omits_unset_values(void) assert_sdata_value_equals(msg, ""); log_msg_unref(msg); } - -static void -test_sdata_format(void) -{ - MSG_TESTCASE(test_sdata_value_is_updated_by_sdata_name_value_pairs); - MSG_TESTCASE(test_sdata_value_omits_unset_values); - MSG_TESTCASE(test_sdata_seqnum_adds_meta_sequence_id); -} - -int -main(int argc G_GNUC_UNUSED, char *argv[] G_GNUC_UNUSED) -{ - app_startup(); - - init_and_load_syslogformat_module(); - - test_log_message(); - test_log_message_merge(); - test_misc_stuff(); - test_sdata_format(); - - app_shutdown(); - return 0; -}