#define _GNU_SOURCE #include #include #include #include #include #include "asmsh_check.h" #include "logger.h" START_TEST(test_logger_new) { asmsh_logger_t *logger; asmsh_loglevel_t lvls[] = {ASMSH_DEBUG, ASMSH_WARN, ASMSH_FATAL}; for(int i = 0; imin_level), lvls[i]); ck_assert_uint_eq((logger->msgs_sz), 0); ck_assert_uint_eq((logger->msgs_alloc), ASMSH_LOG_BUFFER_ALLOC); asmsh_logger_free(logger); } } END_TEST START_TEST(test_log_default_fmt) { asmsh_log_msg_t msg; char buf[4096]; int ret; msg.level = ASMSH_DEBUG; msg.timestamp = 0; msg.caller = "TestCaller"; msg.msg = "foo bar"; msg.nxt = &msg; ret = asmsh_log_default_fmt(&msg, buf, sizeof(buf)); if(ret == -1) { dprintf(2, "log fmt error : '%s'\n", buf); } ck_assert_uint_eq(strlen(buf), ret); ck_assert_str_eq(buf, "1970-01-01T00:00:00+00:00 [DEBUG](TestCaller) : foo bar\n"); } END_TEST START_TEST(test_logger_setup) { int ret = asmsh_logger_setup(NULL); ck_assert_int_eq(ret, 0); } END_TEST START_TEST(test_log_function_lvl_nolog) { asmsh_logger_t *logger; asmsh_log_msg_t *msg; logger = asmsh_logger_new(ASMSH_INFO); ck_assert_int_eq(asmsh_log(logger, ASMSH_DEBUG, "caller", "msg"), 0); ck_assert_uint_eq(logger->msgs_alloc, ASMSH_LOG_BUFFER_ALLOC); ck_assert_ptr_nonnull(logger->msgs); ck_assert_ptr_eq(logger->msgs, logger->nxt); ck_assert_int_ne(asmsh_logger_empty(logger), 0); } END_TEST static const char *msg_samples[][2] = { {"caller", "msg"}, {"superfoofun", "super \"long\" message :P"}, {"...", "woot"}, }; static const int msg_samples_sz = sizeof(msg_samples)/sizeof(*msg_samples); START_TEST(test_log_function_lvl_singlelog) { asmsh_log_msg_t *msg; asmsh_logger_t *logger; const char *caller, *testmsg; size_t total_len; caller = msg_samples[_i][0]; testmsg = msg_samples[_i][1]; total_len = sizeof(asmsh_log_msg_t); total_len += strlen(caller) + strlen(testmsg) + 2; logger = asmsh_logger_new(ASMSH_INFO); ck_assert_int_eq(asmsh_log(logger, ASMSH_ERR, caller, testmsg), 0); ck_assert_uint_eq(logger->msgs_alloc, ASMSH_LOG_BUFFER_ALLOC); ck_assert_ptr_nonnull(logger->msgs); msg = (asmsh_log_msg_t*)logger->msgs; ck_assert_ptr_nonnull(msg->caller); ck_assert_ptr_nonnull(msg->msg); ck_assert_str_eq(caller, msg->caller); ck_assert_str_eq(testmsg, msg->msg); ck_assert_uint_eq(logger->msgs_sz, total_len); asmsh_logger_free(logger); } END_TEST START_TEST(test_log_function_lvl_logs) { asmsh_log_msg_t *msg; asmsh_logger_t *logger; logger = asmsh_logger_new(ASMSH_INFO); const char *caller, *testmsg; size_t total_len = 0; for(int i=0; imsgs); msg = (asmsh_log_msg_t*)logger->msgs; for(int i=0; icaller); ck_assert_ptr_nonnull(msg->msg); ck_assert_int_eq(msg->level, ASMSH_INFO); ck_assert_str_eq(caller, msg->caller); ck_assert_str_eq(testmsg, msg->msg); msg = msg->nxt; } ck_assert_uint_eq(logger->msgs_sz, total_len); asmsh_logger_free(logger); } END_TEST START_TEST(test_log_dprint) { asmsh_log_msg_t *msg; asmsh_logger_t *logger; logger = asmsh_logger_new(ASMSH_INFO); const char *caller, *testmsg; char *exptr, *expt, *res; size_t total_len = 0; int tmp_fd; const char _tmpname[] = "/tmp/dprintXXXXXX"; char *tmpname = strdupa(_tmpname); for(int i=0; imsgs); // forcing time to 1970-01-01T00:00:00+00:00 msg = logger->msgs; while(msg != logger->nxt) { msg->timestamp = 0; msg = msg->nxt; } if((expt = malloc(logger->msgs_alloc)) == NULL) { perror("Unable to allocate expected result"); ck_abort_msg("Unable to allocate expt"); } *expt = '\0'; exptr = expt; for(int i=0; i logger->msgs_alloc) { ck_abort_msg("WTF"); } } tmp_fd = mkstemp(tmpname); int printed_sz = asmsh_logger_dprint(tmp_fd, logger); lseek(tmp_fd, SEEK_SET, 0); if((res = malloc(printed_sz+16)) == NULL) { perror("Unable to allocate result"); ck_abort_msg("unable to allocate result"); } printed_sz = read(tmp_fd, res, printed_sz+15); res[printed_sz] = '\0'; close(tmp_fd); unlink(tmpname); ck_assert_str_eq(expt, res); asmsh_logger_free(logger); } END_TEST ASMSH_CHECK_START("logger tests", "unit tests various logger function") ASMSH_ADD_TEST(test_logger_new); ASMSH_ADD_TEST(test_log_default_fmt); ASMSH_ADD_TEST(test_logger_setup); ASMSH_ADD_TEST(test_log_function_lvl_nolog); ASMSH_ADD_LOOP_TEST(test_log_function_lvl_singlelog, 0, msg_samples_sz); ASMSH_ADD_TEST(test_log_function_lvl_logs); ASMSH_ADD_TEST(test_log_dprint); ASMSH_CHECK_END