diff --git a/test/test_logging.c b/test/test_logging.c index 6a676d6..1dfaaf9 100644 --- a/test/test_logging.c +++ b/test/test_logging.c @@ -7,12 +7,26 @@ #include #include "ucore_logging.h" -const char *file_name = "logfile_test.log"; +#define FILE_NAME "logfile_test.log" +#define SUBDIR "log_subdir" +#define SUBDIR_FILE_NAME SUBDIR "/" FILE_NAME //for cleaning the created log files.. static void logging_rm_files(void) { - remove(file_name); + remove(FILE_NAME); +} + +//setting up subdir +static void logging_setup_subdir(void) +{ + mkdir(SUBDIR, 0750); +} + +static void logging_rm_subdir(void) +{ + remove(SUBDIR_FILE_NAME); + remove(SUBDIR); } START_TEST (test_logfile_location) @@ -33,14 +47,14 @@ START_TEST (test_logfile_location) uc_log_init(&mods); - dest = uc_log_new_file(file_name, UC_LL_DEBUG, 1); + 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); + 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 @@ -63,14 +77,14 @@ START_TEST (test_logfile_no_location) uc_log_init(&mods); - dest = uc_log_new_file(file_name, UC_LL_DEBUG, 0); + 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); + 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 @@ -95,13 +109,13 @@ START_TEST (test_logfile_loglevel_surpressed_dest) //file destination have UC_LL_WARNING, so nothing should be logged - dest = uc_log_new_file(file_name, UC_LL_WARNING, 10); + 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); + 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 @@ -127,13 +141,13 @@ START_TEST (test_logfile_loglevel_surpressed_module) uc_log_init(&mods); - dest = uc_log_new_file(file_name, UC_LL_DEBUG, 1); + 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); + 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 @@ -157,18 +171,18 @@ START_TEST (test_logfile_reopen_files) uc_log_init(&mods); - dest = uc_log_new_file(file_name, UC_LL_DEBUG, 1); + 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); + 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); + rc = remove(FILE_NAME); fail_if(rc != 0, "Remove failed %s\n", strerror(errno)); rc = uc_log_reopen_files(); @@ -176,7 +190,7 @@ START_TEST (test_logfile_reopen_files) UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); - rc = stat(file_name, &st); + 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 @@ -201,7 +215,7 @@ START_TEST (test_logfile_remove_dest) uc_log_init(&mods); - dest1 = uc_log_new_file(file_name, UC_LL_DEBUG, 1); + dest1 = uc_log_new_file(FILE_NAME, UC_LL_DEBUG, 1); fail_if(dest1 == NULL); dest2 = uc_log_new_stderr(UC_LL_WARNING, 1); @@ -213,7 +227,7 @@ START_TEST (test_logfile_remove_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); + 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 @@ -221,27 +235,120 @@ START_TEST (test_logfile_remove_dest) UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); //should not be logged now - rc = stat(file_name, &st); + 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 +START_TEST (test_logfile_full_filesystem) +//logging to a full filesystem shouldn't crash + 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 uc_log_destination *dest; + + uc_log_init(&mods); + + dest = uc_log_new_file("/dev/full", 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_WARNING, 0, "Lorum ipson %d\n", 1); + UC_LOGF(UC_LL_INFO, 0, "Lorum ipson %d\n", 1); + +END_TEST + +START_TEST (test_logfile_invalid_file) +//check behavior when a log file cannot be opened + 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 uc_log_destination *dest; + + uc_log_init(&mods); + + dest = uc_log_new_file("PATH/to/nonexisting.log", UC_LL_DEBUG, 1); + fail_if(dest != NULL); + +END_TEST + +START_TEST (test_logfile_invalid_file_after_repoen) +//check behavior when a log file cannot be re-opened + 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 uc_log_destination *dest; + int rc; + + uc_log_init(&mods); + printf("SUBDIR_FILE_NAME= " SUBDIR_FILE_NAME " \n"); + + dest = uc_log_new_file(SUBDIR_FILE_NAME, UC_LL_DEBUG, 1); + fail_if(dest == NULL); + + logging_rm_subdir(); + + rc = access(SUBDIR_FILE_NAME, W_OK); + fail_if(rc == NULL, "Test setup is wrong. Can still access" SUBDIR_FILE_NAME "\n"); + + UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); + UC_LOGF(UC_LL_WARNING, 0, "Lorum ipson %d\n", 1); + UC_LOGF(UC_LL_INFO, 0, "Lorum ipson %d\n", 1); + + uc_log_reopen_files(); + +//this should not crash now. + UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); + UC_LOGF(UC_LL_WARNING, 0, "Lorum ipson %d\n", 1); + UC_LOGF(UC_LL_INFO, 0, "Lorum ipson %d\n", 1); + +END_TEST + + Suite *logging_suite(void) { Suite *s = suite_create("logging"); - TCase *tc = tcase_create("ucore_logging"); + TCase *tc1 = tcase_create("ucore_logging1"); + TCase *tc2 = tcase_create("ucore_logging2"); - 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_test(tc1, test_logfile_location); + tcase_add_test(tc1, test_logfile_no_location); + tcase_add_test(tc1, test_logfile_loglevel_surpressed_dest); + tcase_add_test(tc1, test_logfile_loglevel_surpressed_module); + tcase_add_test(tc1, test_logfile_reopen_files); + tcase_add_test(tc1, test_logfile_remove_dest); + tcase_add_test(tc1, test_logfile_full_filesystem); + tcase_add_test(tc1, test_logfile_invalid_file); + tcase_add_test(tc2, test_logfile_invalid_file_after_repoen); - tcase_add_checked_fixture(tc, logging_rm_files, logging_rm_files); + tcase_add_checked_fixture(tc1, logging_rm_files, logging_rm_files); + tcase_add_checked_fixture(tc2, logging_setup_subdir, logging_rm_subdir); - suite_add_tcase(s, tc); + suite_add_tcase(s, tc1); + suite_add_tcase(s, tc2); return s; }