A shell that runs x86_64 assembly
c
x86-64
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

tests_logger.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #define _GNU_SOURCE
  2. #include <check.h>
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include "asmsh_check.h"
  8. #include "logger.h"
  9. START_TEST(test_logger_new)
  10. {
  11. asmsh_logger_t *logger;
  12. asmsh_loglevel_t lvls[] = {ASMSH_DEBUG, ASMSH_WARN, ASMSH_FATAL};
  13. for(int i = 0; i<sizeof(lvls) / sizeof(*lvls); i++)
  14. {
  15. logger = asmsh_logger_new(lvls[i]);
  16. ck_assert_ptr_nonnull(logger);
  17. ck_assert_int_eq((logger->min_level), lvls[i]);
  18. ck_assert_uint_eq((logger->msgs_sz), 0);
  19. ck_assert_uint_eq((logger->msgs_alloc), ASMSH_LOG_BUFFER_ALLOC);
  20. asmsh_logger_free(logger);
  21. }
  22. }
  23. END_TEST
  24. START_TEST(test_log_default_fmt)
  25. {
  26. asmsh_log_msg_t msg;
  27. char buf[4096];
  28. int ret;
  29. msg.level = ASMSH_DEBUG;
  30. msg.timestamp = 0;
  31. msg.caller = "TestCaller";
  32. msg.msg = "foo bar";
  33. msg.nxt = &msg;
  34. ret = asmsh_log_default_fmt(&msg, buf, sizeof(buf));
  35. if(ret == -1)
  36. {
  37. dprintf(2, "log fmt error : '%s'\n", buf);
  38. }
  39. ck_assert_uint_eq(strlen(buf), ret);
  40. ck_assert_str_eq(buf, "1970-01-01T00:00:00+00:00 [DEBUG](TestCaller) : foo bar\n");
  41. }
  42. END_TEST
  43. START_TEST(test_logger_setup)
  44. {
  45. int ret = asmsh_logger_setup(NULL);
  46. ck_assert_int_eq(ret, 0);
  47. }
  48. END_TEST
  49. START_TEST(test_log_function_lvl_nolog)
  50. {
  51. asmsh_logger_t *logger;
  52. asmsh_log_msg_t *msg;
  53. logger = asmsh_logger_new(ASMSH_INFO);
  54. ck_assert_int_eq(asmsh_log(logger, ASMSH_DEBUG, "caller", "msg"), 0);
  55. ck_assert_uint_eq(logger->msgs_alloc, ASMSH_LOG_BUFFER_ALLOC);
  56. ck_assert_ptr_nonnull(logger->msgs);
  57. ck_assert_ptr_eq(logger->msgs, logger->nxt);
  58. ck_assert_int_ne(asmsh_logger_empty(logger), 0);
  59. }
  60. END_TEST
  61. static const char *msg_samples[][2] = {
  62. {"caller", "msg"},
  63. {"superfoofun", "super \"long\" message :P"},
  64. {"...", "woot"},
  65. };
  66. static const int msg_samples_sz = sizeof(msg_samples)/sizeof(*msg_samples);
  67. START_TEST(test_log_function_lvl_singlelog)
  68. {
  69. asmsh_log_msg_t *msg;
  70. asmsh_logger_t *logger;
  71. const char *caller, *testmsg;
  72. size_t total_len;
  73. caller = msg_samples[_i][0];
  74. testmsg = msg_samples[_i][1];
  75. total_len = sizeof(asmsh_log_msg_t);
  76. total_len += strlen(caller) + strlen(testmsg) + 2;
  77. logger = asmsh_logger_new(ASMSH_INFO);
  78. ck_assert_int_eq(asmsh_log(logger, ASMSH_ERR, caller, testmsg), 0);
  79. ck_assert_uint_eq(logger->msgs_alloc, ASMSH_LOG_BUFFER_ALLOC);
  80. ck_assert_ptr_nonnull(logger->msgs);
  81. msg = (asmsh_log_msg_t*)logger->msgs;
  82. ck_assert_ptr_nonnull(msg->caller);
  83. ck_assert_ptr_nonnull(msg->msg);
  84. ck_assert_str_eq(caller, msg->caller);
  85. ck_assert_str_eq(testmsg, msg->msg);
  86. ck_assert_uint_eq(logger->msgs_sz, total_len);
  87. asmsh_logger_free(logger);
  88. }
  89. END_TEST
  90. START_TEST(test_log_function_lvl_logs)
  91. {
  92. asmsh_log_msg_t *msg;
  93. asmsh_logger_t *logger;
  94. logger = asmsh_logger_new(ASMSH_INFO);
  95. const char *caller, *testmsg;
  96. size_t total_len = 0;
  97. for(int i=0; i<msg_samples_sz; i++)
  98. {
  99. caller = msg_samples[i][0];
  100. testmsg = msg_samples[i][1];
  101. total_len += sizeof(asmsh_log_msg_t);
  102. total_len += strlen(caller) + strlen(testmsg) + 2;
  103. ck_assert_int_eq(asmsh_log(logger, ASMSH_INFO, caller, testmsg), 0);
  104. }
  105. ck_assert_ptr_nonnull(logger->msgs);
  106. msg = (asmsh_log_msg_t*)logger->msgs;
  107. for(int i=0; i<msg_samples_sz; i++)
  108. {
  109. caller = msg_samples[i][0];
  110. testmsg = msg_samples[i][1];
  111. ck_assert_ptr_nonnull(msg->caller);
  112. ck_assert_ptr_nonnull(msg->msg);
  113. ck_assert_int_eq(msg->level, ASMSH_INFO);
  114. ck_assert_str_eq(caller, msg->caller);
  115. ck_assert_str_eq(testmsg, msg->msg);
  116. msg = msg->nxt;
  117. }
  118. ck_assert_uint_eq(logger->msgs_sz, total_len);
  119. asmsh_logger_free(logger);
  120. }
  121. END_TEST
  122. START_TEST(test_log_dprint)
  123. {
  124. asmsh_log_msg_t *msg;
  125. asmsh_logger_t *logger;
  126. logger = asmsh_logger_new(ASMSH_INFO);
  127. const char *caller, *testmsg;
  128. char *exptr, *expt, *res;
  129. size_t total_len = 0;
  130. int tmp_fd;
  131. const char _tmpname[] = "/tmp/dprintXXXXXX";
  132. char *tmpname = strdupa(_tmpname);
  133. for(int i=0; i<msg_samples_sz; i++)
  134. {
  135. caller = msg_samples[i][0];
  136. testmsg = msg_samples[i][1];
  137. total_len += sizeof(asmsh_log_msg_t);
  138. total_len += strlen(caller) + strlen(testmsg) + 2;
  139. ck_assert_int_eq(asmsh_log(logger, ASMSH_INFO, caller, testmsg), 0);
  140. }
  141. ck_assert_ptr_nonnull(logger->msgs);
  142. // forcing time to 1970-01-01T00:00:00+00:00
  143. msg = logger->msgs;
  144. while(msg != logger->nxt)
  145. {
  146. msg->timestamp = 0;
  147. msg = msg->nxt;
  148. }
  149. if((expt = malloc(logger->msgs_alloc)) == NULL)
  150. {
  151. perror("Unable to allocate expected result");
  152. ck_abort_msg("Unable to allocate expt");
  153. }
  154. *expt = '\0';
  155. exptr = expt;
  156. for(int i=0; i<msg_samples_sz; i++)
  157. {
  158. caller = msg_samples[i][0];
  159. testmsg = msg_samples[i][1];
  160. int ret = sprintf(exptr,
  161. "1970-01-01T00:00:00+00:00 [INFO](%s) : %s\n",
  162. caller, testmsg);
  163. exptr += ret;
  164. if(exptr-expt > logger->msgs_alloc)
  165. {
  166. ck_abort_msg("WTF");
  167. }
  168. }
  169. tmp_fd = mkstemp(tmpname);
  170. int printed_sz = asmsh_logger_dprint(tmp_fd, logger);
  171. lseek(tmp_fd, SEEK_SET, 0);
  172. if((res = malloc(printed_sz+16)) == NULL)
  173. {
  174. perror("Unable to allocate result");
  175. ck_abort_msg("unable to allocate result");
  176. }
  177. printed_sz = read(tmp_fd, res, printed_sz+15);
  178. res[printed_sz] = '\0';
  179. close(tmp_fd);
  180. unlink(tmpname);
  181. ck_assert_str_eq(expt, res);
  182. asmsh_logger_free(logger);
  183. }
  184. END_TEST
  185. ASMSH_CHECK_START("logger tests", "unit tests various logger function")
  186. ASMSH_ADD_TEST(test_logger_new);
  187. ASMSH_ADD_TEST(test_log_default_fmt);
  188. ASMSH_ADD_TEST(test_logger_setup);
  189. ASMSH_ADD_TEST(test_log_function_lvl_nolog);
  190. ASMSH_ADD_LOOP_TEST(test_log_function_lvl_singlelog, 0, msg_samples_sz);
  191. ASMSH_ADD_TEST(test_log_function_lvl_logs);
  192. ASMSH_ADD_TEST(test_log_dprint);
  193. ASMSH_CHECK_END