From 2a1d341e8b10275afb84ff4126a31bb560f6742c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Sun, 4 Nov 2012 23:24:02 +0100 Subject: [PATCH] Added tests for logging --- test/test_logging.c | 242 ++++++++++++++++++++++++++++++++++++++++++++ test/test_runner.c | 2 + 2 files changed, 244 insertions(+) create mode 100644 test/test_logging.c diff --git a/test/test_logging.c b/test/test_logging.c new file mode 100644 index 0000000..fd59c12 --- /dev/null +++ b/test/test_logging.c @@ -0,0 +1,242 @@ +#include +#include +#include +#include +#include +#include +#include +#include "ucore_logging.h" + +const char *file_name = "logfile_test.log"; + +//for cleaning the created log files.. +static void logging_rm_files(void) +{ + remove(file_name); +} + +START_TEST (test_logfile_location) + + struct uc_log_module m = { + .id = 0, + .short_name = "MOD1", + .long_name = "Module 1", + .log_level = UC_LL_DEBUG, + }; + struct uc_log_modules mods = { + .mods = &m, + .cnt = 1, + }; + struct stat st; + struct uc_log_destination *dest; + int rc; + + uc_log_init(&mods); + + dest = uc_log_new_file(file_name, UC_LL_DEBUG, 1); + fail_if(dest == NULL); + uc_log_add_destination(dest); + + UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); + UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); + + rc = stat(file_name, &st); + fail_if(rc != 0, "stat failed: %s\n", strerror(errno)); + fail_if(st.st_size != 79*2, "was %ld\n", (long)st.st_size);//should be 82 chars per log statement +END_TEST + +START_TEST (test_logfile_no_location) + + struct uc_log_module m = { + .id = 0, + .short_name = "MOD1", + .long_name = "Module 1", + .log_level = UC_LL_DEBUG, + }; + struct uc_log_modules mods = { + .mods = &m, + .cnt = 1, + }; + struct stat st; + struct uc_log_destination *dest; + int rc; + + uc_log_init(&mods); + + dest = uc_log_new_file(file_name, UC_LL_DEBUG, 0); + fail_if(dest == NULL); + uc_log_add_destination(dest); + + UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); + UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); + + rc = stat(file_name, &st); + fail_if(rc != 0, "stat failed: %s\n", strerror(errno)); + fail_if(st.st_size != 55*2, "was %ld\n", (long)st.st_size);//should be 82 chars per log statement +END_TEST + +START_TEST (test_logfile_loglevel_surpressed_dest) + + struct uc_log_module m = { + .id = 0, + .short_name = "MOD1", + .long_name = "Module 1", + .log_level = UC_LL_DEBUG, + }; + struct uc_log_modules mods = { + .mods = &m, + .cnt = 1, + }; + struct stat st; + struct uc_log_destination *dest; + int rc; + + uc_log_init(&mods); + + + //file destination have UC_LL_WARNING, so nothing should be logged + dest = uc_log_new_file(file_name, UC_LL_WARNING, 10); + fail_if(dest == NULL); + uc_log_add_destination(dest); + + UC_LOGF(UC_LL_INFO, 1, "Lorum ipson %d", 1); + + rc = stat(file_name, &st); + fail_if(rc != 0, "stat failed: %s\n", strerror(errno)); + + fail_if(st.st_size != 0, "was %ld\n", (long)st.st_size);//should be 82 chars per log statement +END_TEST + +START_TEST (test_logfile_loglevel_surpressed_module) + + struct uc_log_module m = { + .id = 0, + .short_name = "MOD1", + .long_name = "Module 1", + //file destination have UC_LL_WARNING, so nothing should be logged + .log_level = UC_LL_WARNING, + }; + struct uc_log_modules mods = { + .mods = &m, + .cnt = 1, + }; + struct stat st; + struct uc_log_destination *dest; + int rc; + + uc_log_init(&mods); + + + dest = uc_log_new_file(file_name, UC_LL_DEBUG, 1); + fail_if(dest == NULL); + uc_log_add_destination(dest); + + UC_LOGF(UC_LL_INFO, 0, "Lorum ipson %d", 100); + + rc = stat(file_name, &st); + fail_if(rc != 0, "stat failed: %s\n", strerror(errno)); + + fail_if(st.st_size != 0, "was %ld\n", (long)st.st_size);//should be 82 chars per log statement +END_TEST + +START_TEST (test_logfile_reopen_files) + + struct uc_log_module m = { + .id = 0, + .short_name = "MOD1", + .long_name = "Module 1", + .log_level = UC_LL_DEBUG, + }; + struct uc_log_modules mods = { + .mods = &m, + .cnt = 1, + }; + struct stat st; + struct uc_log_destination *dest; + int rc; + + uc_log_init(&mods); + + dest = uc_log_new_file(file_name, UC_LL_DEBUG, 1); + fail_if(dest == NULL); + uc_log_add_destination(dest); + + UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); + UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); + + rc = stat(file_name, &st); + fail_if(rc != 0, "stat failed: %s\n", strerror(errno)); + fail_if(st.st_size != 80*2, "was %ld\n", (long)st.st_size);//should be 82 chars per log statement + + rc = remove(file_name); + fail_if(rc != 0, "Remove failed %s\n", strerror(errno)); + + rc = uc_log_reopen_files(); + fail_if(rc != 0, "uc_log_reopen_files failed: %d\n", rc); + + UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); + + rc = stat(file_name, &st); + fail_if(rc != 0, "2. stat failed: %s\n", strerror(errno)); + fail_if(st.st_size != 80, "was %ld\n", (long)st.st_size);//should only one line there now + +END_TEST + +START_TEST (test_logfile_remove_dest) + + struct uc_log_module m = { + .id = 0, + .short_name = "MOD1", + .long_name = "Module 1", + .log_level = UC_LL_DEBUG, + }; + struct uc_log_modules mods = { + .mods = &m, + .cnt = 1, + }; + struct stat st; + struct uc_log_destination *dest; + int rc; + + uc_log_init(&mods); + + dest = uc_log_new_file(file_name, UC_LL_DEBUG, 1); + fail_if(dest == NULL); + uc_log_add_destination(dest); + + UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); + UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); + + rc = stat(file_name, &st); + fail_if(rc != 0, "stat failed: %s\n", strerror(errno)); + fail_if(st.st_size != 80*2, "was %ld\n", (long)st.st_size);//should be 82 chars per log statement + + uc_log_remove_destination(dest); + + UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); //should not be logged now + + rc = stat(file_name, &st); + fail_if(rc != 0, "2. stat failed: %s\n", strerror(errno)); + fail_if(st.st_size != 80*2, "was %ld\n", (long)st.st_size);//should be same size + +END_TEST + +Suite *logging_suite(void) +{ + Suite *s = suite_create("logging"); + TCase *tc = tcase_create("ucore_logging"); + + tcase_add_test(tc, test_logfile_location); + tcase_add_test(tc, test_logfile_no_location); + tcase_add_test(tc, test_logfile_loglevel_surpressed_dest); + tcase_add_test(tc, test_logfile_loglevel_surpressed_module); + tcase_add_test(tc, test_logfile_reopen_files); + tcase_add_test(tc, test_logfile_remove_dest); + + tcase_add_checked_fixture(tc, logging_rm_files, logging_rm_files); + + suite_add_tcase(s, tc); + + return s; +} + diff --git a/test/test_runner.c b/test/test_runner.c index 7157462..a587196 100644 --- a/test/test_runner.c +++ b/test/test_runner.c @@ -9,6 +9,7 @@ extern Suite *hex_suite(void); extern Suite *container_of_suite(void); extern Suite *read_file_suite(void); extern Suite *pack_suite(void); +extern Suite *logging_suite(void); int main (int argc, char *argv[]) @@ -27,6 +28,7 @@ main (int argc, char *argv[]) srunner_add_suite(sr, container_of_suite()); srunner_add_suite(sr, read_file_suite()); srunner_add_suite(sr, pack_suite()); + srunner_add_suite(sr, logging_suite()); srunner_run_all (sr, CK_VERBOSE);