timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ttail_search_check.c 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #include <check.h>
  2. #include <stdio.h>
  3. #include <libgen.h>
  4. #include "ttail.h"
  5. #include "ttail_init.h"
  6. #include "ttail_search.h"
  7. ttail_t *ttail;
  8. char *samples[5] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
  9. "./samples/4.log", "./samples/5.log"};
  10. off_t samples_sz[5] = { 442, 0, 893, 2587, 2310 };
  11. FILE *fpl;
  12. void setup_file_line(void)
  13. {
  14. fpl = fopen(samples[0], "r");
  15. if(!fpl)
  16. {
  17. perror("Unable to open file for testing :");
  18. ck_abort();
  19. }
  20. }
  21. void teardown_file_line(void)
  22. {
  23. fclose(fpl);
  24. }
  25. void teardown_closest(void)
  26. {
  27. ttail_free(ttail);
  28. }
  29. void setup_closest(void)
  30. {
  31. ttail = ttail_init(1, (char**)&"foo");
  32. }
  33. void setup_closest_fileinit(void)
  34. {
  35. size_t i;
  36. setup_closest();
  37. for(i=0; i<5; i++)
  38. {
  39. ttail_add_logfile(ttail, samples[i]);
  40. }
  41. }
  42. void teardown_closest_fileinit(void)
  43. {
  44. _ttail_search_file_free(ttail);
  45. teardown_closest();
  46. }
  47. START_TEST (test_search_closest_init)
  48. {
  49. int ret;
  50. ret = _ttail_search_closest_files_init(ttail);
  51. ck_assert_int_eq(ret,0);
  52. ck_assert(ttail->session->file.vpos == 0);
  53. #ifdef HUGEFILE
  54. ck_assert_int_eq(ttail->session->file.sz_div,0);
  55. #endif
  56. }
  57. END_TEST
  58. START_TEST (test_search_closest_init_filesz)
  59. {
  60. size_t i;
  61. _ttail_search_closest_files_init(ttail);
  62. for(i=0;i<5;i++)
  63. {
  64. ck_assert(samples_sz[i] == ttail->session->file.file_sz[i]);
  65. }
  66. }
  67. END_TEST
  68. START_TEST (test_search_closest_init_vfile)
  69. {
  70. size_t i;
  71. off_t full_sz;
  72. _ttail_search_closest_files_init(ttail);
  73. full_sz = 0;
  74. for(i=0;i<5;i++)
  75. {
  76. ck_assert(full_sz == ttail->session->file.vfile[i]);
  77. full_sz += samples_sz[i];
  78. }
  79. ck_assert(full_sz == ttail->session->file.vsz);
  80. }
  81. END_TEST
  82. /*
  83. * _ttail_file_next_line() & _ttail_file_line_start() tests
  84. */
  85. START_TEST (test_file_line_next)
  86. {
  87. long res;
  88. res = _ttail_file_next_line(fpl);
  89. ck_assert(res == 77);
  90. res = _ttail_file_next_line(fpl);
  91. ck_assert(res == 136);
  92. res = _ttail_file_next_line(fpl);
  93. ck_assert(res == 221);
  94. res = _ttail_file_next_line(fpl);
  95. ck_assert(res == 298);
  96. res = _ttail_file_next_line(fpl);
  97. ck_assert(res == 357);
  98. res = _ttail_file_next_line(fpl);
  99. ck_assert(res == 0);
  100. res = _ttail_file_next_line(fpl);
  101. ck_assert(res == -1);
  102. }
  103. END_TEST
  104. START_TEST (test_file_line_start)
  105. {
  106. long res;
  107. fseek(fpl, -1, SEEK_END);
  108. res = _ttail_file_start_line(fpl);
  109. ck_assert(res == 357);
  110. res = _ttail_file_start_line(fpl);
  111. ck_assert(res == 357);
  112. fseek(fpl, -1, SEEK_CUR);
  113. res = _ttail_file_start_line(fpl);
  114. ck_assert(res == 298);
  115. fseek(fpl, -1, SEEK_CUR);
  116. res = _ttail_file_start_line(fpl);
  117. ck_assert(res == 221);
  118. fseek(fpl, -1, SEEK_CUR);
  119. res = _ttail_file_start_line(fpl);
  120. ck_assert(res == 136);
  121. fseek(fpl, -1, SEEK_CUR);
  122. res = _ttail_file_start_line(fpl);
  123. ck_assert(res == 77);
  124. fseek(fpl, -1, SEEK_CUR);
  125. res = _ttail_file_start_line(fpl);
  126. ck_assert(res == 0);
  127. }
  128. END_TEST
  129. /*
  130. TODO : complete the test + debug
  131. */
  132. START_TEST (test_file_minmax1)
  133. {
  134. int r;
  135. struct tm tm[2];
  136. printf("%ld\n", ttail->logfile_sz);
  137. ttail->flag |= TTAIL_FLAG_PREFIX;
  138. ttail->prefix_sz = 0;
  139. ttail_set_fmt(ttail, "%b%n%d %H:%M");
  140. r = _ttail_search_closest_files_init(ttail);
  141. ck_assert_int_eq(r, 0);
  142. r = _ttail_file_minmax(ttail, 0, tm);
  143. ck_assert_int_eq(r, 0);
  144. ck_assert_int_eq(tm[0].tm_mon, 2);
  145. ck_assert_int_eq(tm[0].tm_mday, 6);
  146. ck_assert_int_eq(tm[0].tm_hour, 0);
  147. ck_assert_int_eq(tm[0].tm_min, 1);
  148. ck_assert_int_eq(tm[1].tm_mon, 2);
  149. ck_assert_int_eq(tm[1].tm_mday, 6);
  150. ck_assert_int_eq(tm[1].tm_hour, 0);
  151. ck_assert_int_eq(tm[1].tm_min, 29);
  152. }
  153. END_TEST
  154. START_TEST (test_search_subst_const1)
  155. {
  156. char expl[] = "Hello world !";
  157. const char *res;
  158. ttail->flag |= TTAIL_FLAG_PREFIX;
  159. ttail->prefix_sz = 4;
  160. res = ttail_logline_subst(ttail, expl);
  161. ck_assert(res != NULL);
  162. ck_assert(res == expl+4);
  163. }
  164. END_TEST
  165. START_TEST (test_search_subst_const2)
  166. {
  167. char expl[] = "Hello world !";
  168. const char *res;
  169. ttail->flag |= TTAIL_FLAG_PREFIX;
  170. ttail->prefix_sz = 0;
  171. res = ttail_logline_subst(ttail, expl);
  172. ck_assert(res != NULL);
  173. ck_assert(res == expl);
  174. }
  175. END_TEST
  176. START_TEST (test_search_subst_re1)
  177. {
  178. char expl[] = "1337 Foo Bar - Hello world !";
  179. char re[] = "^1337 Fo* Bar - ";
  180. const char *res;
  181. int ret;
  182. ret = ttail_set_prefix(ttail, re);
  183. ck_assert_int_eq(ret,0);
  184. res = ttail_logline_subst(ttail, expl);
  185. ck_assert(res != NULL);
  186. ck_assert_str_eq(res, "Hello world !");
  187. }
  188. END_TEST
  189. START_TEST (test_search_subst_re2)
  190. {
  191. char expl[] = "1337 Foo Bar - Hello world !";
  192. char re[] = "^[0-9]+ Fo{2} Bar - ";
  193. const char *res;
  194. int ret;
  195. ttail->flag |= TTAIL_FLAG_EXTENDED_RE;
  196. ret = ttail_set_prefix(ttail, re);
  197. ck_assert_int_eq(ret,0);
  198. res = ttail_logline_subst(ttail, expl);
  199. ck_assert(res != NULL);
  200. ck_assert_str_eq(res, "Hello world !");
  201. }
  202. END_TEST
  203. START_TEST (test_search_subst_re_nomatch)
  204. {
  205. char expl[] = "1337 Foo Bar - Hello world !";
  206. char re[] = "Never match m*";
  207. const char *res;
  208. int ret;
  209. ret = ttail_set_prefix(ttail, re);
  210. ck_assert_int_eq(ret,0);
  211. res = ttail_logline_subst(ttail, expl);
  212. ck_assert(res == NULL);
  213. }
  214. END_TEST
  215. Suite * ttail_search_files_suite(void)
  216. {
  217. Suite *s;
  218. TCase *tc_search_subst;
  219. TCase *tc_search_closest_fileinit, *tc_file_line,
  220. *tc_file_minmax;
  221. s = suite_create("ttail search_files checks");
  222. tc_search_closest_fileinit = tcase_create("\
  223. ttail_logline_closest_files_init() checks");
  224. tcase_add_checked_fixture(tc_search_closest_fileinit,
  225. setup_closest_fileinit, teardown_closest_fileinit);
  226. tcase_add_test(tc_search_closest_fileinit, test_search_closest_init);
  227. tcase_add_test(tc_search_closest_fileinit,
  228. test_search_closest_init_filesz);
  229. tcase_add_test(tc_search_closest_fileinit,
  230. test_search_closest_init_vfile);
  231. tc_file_line = tcase_create("ttail_file_*line*() checks");
  232. tcase_add_checked_fixture(tc_file_line,
  233. setup_file_line, teardown_file_line);
  234. tcase_add_test(tc_file_line, test_file_line_next);
  235. tcase_add_test(tc_file_line, test_file_line_start);
  236. tc_file_minmax = tcase_create("ttail_file_minmax() checks");
  237. tcase_add_checked_fixture(tc_file_minmax,
  238. setup_closest_fileinit, teardown_closest_fileinit);
  239. tcase_add_test(tc_file_minmax, test_file_minmax1);
  240. tc_search_subst = tcase_create("ttail_logline_subst() checks");
  241. tcase_add_checked_fixture(tc_search_subst,
  242. setup_closest_fileinit, teardown_closest_fileinit);
  243. tcase_add_test(tc_search_subst, test_search_subst_const1);
  244. tcase_add_test(tc_search_subst, test_search_subst_const2);
  245. tcase_add_test(tc_search_subst, test_search_subst_re1);
  246. tcase_add_test(tc_search_subst, test_search_subst_re2);
  247. tcase_add_test(tc_search_subst, test_search_subst_re_nomatch);
  248. suite_add_tcase(s, tc_search_subst);
  249. suite_add_tcase(s, tc_search_closest_fileinit);
  250. suite_add_tcase(s, tc_file_line);
  251. suite_add_tcase(s, tc_file_minmax);
  252. return s;
  253. }
  254. int main(int argc, char **argv)
  255. {
  256. int number_failed = 0;
  257. SRunner *sr;
  258. chdir(dirname(argv[0])); /* move in ./tests dir */
  259. sr = srunner_create(ttail_search_files_suite());
  260. srunner_set_fork_status(sr, CK_FORK);
  261. srunner_run_all(sr,CK_VERBOSE);
  262. number_failed = srunner_ntests_failed(sr);
  263. srunner_free(sr);
  264. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  265. }