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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. /*
  136. struct tm tm[2];
  137. */
  138. printf("%ld\n", ttail->logfile_sz);
  139. ttail->flag |= TTAIL_FLAG_FORMAT;
  140. ttail_set_fmt(ttail, "%B%n%d %H:%M");
  141. r = _ttail_search_closest_files_init(ttail);
  142. ck_assert_int_eq(r, 0);
  143. /*
  144. r = _ttail_file_minmax(ttail, 0, tm);
  145. */
  146. }
  147. END_TEST
  148. START_TEST (test_search_subst_const1)
  149. {
  150. char expl[] = "Hello world !";
  151. const char *res;
  152. ttail->flag |= TTAIL_FLAG_PREFIX;
  153. ttail->prefix_sz = 4;
  154. res = ttail_logline_subst(ttail, expl);
  155. ck_assert(res != NULL);
  156. ck_assert(res == expl+4);
  157. }
  158. END_TEST
  159. START_TEST (test_search_subst_const2)
  160. {
  161. char expl[] = "Hello world !";
  162. const char *res;
  163. ttail->flag |= TTAIL_FLAG_PREFIX;
  164. ttail->prefix_sz = 0;
  165. res = ttail_logline_subst(ttail, expl);
  166. ck_assert(res != NULL);
  167. ck_assert(res == expl);
  168. }
  169. END_TEST
  170. START_TEST (test_search_subst_re1)
  171. {
  172. char expl[] = "1337 Foo Bar - Hello world !";
  173. char re[] = "^1337 Fo* Bar - ";
  174. const char *res;
  175. int ret;
  176. ret = ttail_set_prefix(ttail, re);
  177. ck_assert_int_eq(ret,0);
  178. res = ttail_logline_subst(ttail, expl);
  179. ck_assert(res != NULL);
  180. ck_assert_str_eq(res, "Hello world !");
  181. }
  182. END_TEST
  183. START_TEST (test_search_subst_re2)
  184. {
  185. char expl[] = "1337 Foo Bar - Hello world !";
  186. char re[] = "^[0-9]+ Fo{2} Bar - ";
  187. const char *res;
  188. int ret;
  189. ttail->flag |= TTAIL_FLAG_EXTENDED_RE;
  190. ret = ttail_set_prefix(ttail, re);
  191. ck_assert_int_eq(ret,0);
  192. res = ttail_logline_subst(ttail, expl);
  193. ck_assert(res != NULL);
  194. ck_assert_str_eq(res, "Hello world !");
  195. }
  196. END_TEST
  197. START_TEST (test_search_subst_re_nomatch)
  198. {
  199. char expl[] = "1337 Foo Bar - Hello world !";
  200. char re[] = "Never match m*";
  201. const char *res;
  202. int ret;
  203. ret = ttail_set_prefix(ttail, re);
  204. ck_assert_int_eq(ret,0);
  205. res = ttail_logline_subst(ttail, expl);
  206. ck_assert(res == NULL);
  207. }
  208. END_TEST
  209. Suite * ttail_search_files_suite(void)
  210. {
  211. Suite *s;
  212. TCase *tc_search_subst;
  213. TCase *tc_search_closest_fileinit, *tc_file_line,
  214. *tc_file_minmax;
  215. s = suite_create("ttail search_files checks");
  216. tc_search_closest_fileinit = tcase_create("\
  217. ttail_logline_closest_files_init() checks");
  218. tcase_add_checked_fixture(tc_search_closest_fileinit,
  219. setup_closest_fileinit, teardown_closest_fileinit);
  220. tcase_add_test(tc_search_closest_fileinit, test_search_closest_init);
  221. tcase_add_test(tc_search_closest_fileinit,
  222. test_search_closest_init_filesz);
  223. tcase_add_test(tc_search_closest_fileinit,
  224. test_search_closest_init_vfile);
  225. tc_file_line = tcase_create("ttail_file_*line*() checks");
  226. tcase_add_checked_fixture(tc_file_line,
  227. setup_file_line, teardown_file_line);
  228. tcase_add_test(tc_file_line, test_file_line_next);
  229. tcase_add_test(tc_file_line, test_file_line_start);
  230. tc_file_minmax = tcase_create("ttail_file_minmax() checks");
  231. tcase_add_checked_fixture(tc_file_minmax,
  232. setup_closest_fileinit, teardown_closest_fileinit);
  233. tcase_add_test(tc_file_minmax, test_file_minmax1);
  234. tc_search_subst = tcase_create("ttail_logline_subst() checks");
  235. tcase_add_checked_fixture(tc_search_subst,
  236. setup_closest_fileinit, teardown_closest_fileinit);
  237. tcase_add_test(tc_search_subst, test_search_subst_const1);
  238. tcase_add_test(tc_search_subst, test_search_subst_const2);
  239. tcase_add_test(tc_search_subst, test_search_subst_re1);
  240. tcase_add_test(tc_search_subst, test_search_subst_re2);
  241. tcase_add_test(tc_search_subst, test_search_subst_re_nomatch);
  242. suite_add_tcase(s, tc_search_subst);
  243. suite_add_tcase(s, tc_search_closest_fileinit);
  244. suite_add_tcase(s, tc_file_line);
  245. suite_add_tcase(s, tc_file_minmax);
  246. return s;
  247. }
  248. int main(int argc, char **argv)
  249. {
  250. int number_failed = 0;
  251. SRunner *sr;
  252. chdir(dirname(argv[0])); /* move in ./tests dir */
  253. sr = srunner_create(ttail_search_files_suite());
  254. srunner_set_fork_status(sr, CK_FORK);
  255. srunner_run_all(sr,CK_VERBOSE);
  256. number_failed = srunner_ntests_failed(sr);
  257. srunner_free(sr);
  258. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  259. }