123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- #define _GNU_SOURCE
- #include <check.h>
- #include <errno.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
-
- #include "asmsh_check.h"
- #include "logger.h"
-
- START_TEST(test_logger_new)
- {
- asmsh_loglevel_t lvls[] = {ASMSH_DEBUG, ASMSH_WARN, ASMSH_FATAL};
-
- for(int i = 0; i<sizeof(lvls) / sizeof(*lvls); i++)
- {
- asmsh_logger_t *logger = asmsh_logger_new(lvls[i]);
-
- ck_assert_ptr_nonnull(logger);
- ck_assert_int_eq((logger->min_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;
-
- 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; i<msg_samples_sz; i++)
- {
-
- caller = msg_samples[i][0];
- testmsg = msg_samples[i][1];
- total_len += sizeof(asmsh_log_msg_t);
- total_len += strlen(caller) + strlen(testmsg) + 2;
-
- ck_assert_int_eq(asmsh_log(logger, ASMSH_INFO, caller, testmsg), 0);
- }
-
- ck_assert_ptr_nonnull(logger->msgs);
-
- msg = (asmsh_log_msg_t*)logger->msgs;
- for(int i=0; i<msg_samples_sz; i++)
- {
-
- caller = msg_samples[i][0];
- testmsg = msg_samples[i][1];
-
- ck_assert_ptr_nonnull(msg->caller);
- 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;
- int tmp_fd;
- const char _tmpname[] = "/tmp/dprintXXXXXX";
- char *tmpname = strdupa(_tmpname);
-
- for(int i=0; i<msg_samples_sz; i++)
- {
-
- caller = msg_samples[i][0];
- testmsg = msg_samples[i][1];
-
- ck_assert_int_eq(asmsh_log(logger, ASMSH_INFO, caller, testmsg), 0);
- }
- ck_assert_ptr_nonnull(logger->msgs);
-
- // 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<msg_samples_sz; i++)
- {
-
- caller = msg_samples[i][0];
- testmsg = msg_samples[i][1];
- int ret = sprintf(exptr,
- "1970-01-01T00:00:00+00:00 [INFO](%s) : %s\n",
- caller, testmsg);
- exptr += ret;
- if(exptr-expt > 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
|