A shell that runs x86_64 assembly
c
x86-64
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

tests_logger.c 5.3KB

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