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 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. * _ttail_file_minmax() tests
  131. */
  132. START_TEST (test_file_minmax1)
  133. {
  134. /**@todo complete the testcase */
  135. int r;
  136. struct tm tm[2];
  137. printf("%ld\n", ttail->logfile_sz);
  138. ttail->flag |= TTAIL_FLAG_PREFIX;
  139. ttail->prefix_sz = 0;
  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. r = _ttail_file_minmax(ttail, 0, tm);
  144. ck_assert_int_eq(r, 0);
  145. ck_assert_int_eq(tm[0].tm_mon, 2);
  146. ck_assert_int_eq(tm[0].tm_mday, 6);
  147. ck_assert_int_eq(tm[0].tm_hour, 0);
  148. ck_assert_int_eq(tm[0].tm_min, 1);
  149. ck_assert_int_eq(tm[1].tm_mon, 2);
  150. ck_assert_int_eq(tm[1].tm_mday, 6);
  151. ck_assert_int_eq(tm[1].tm_hour, 0);
  152. ck_assert_int_eq(tm[1].tm_min, 29);
  153. }
  154. END_TEST
  155. /*
  156. * ttail_logline_subst() tests
  157. */
  158. START_TEST (test_search_subst_const1)
  159. {
  160. char expl[] = "Hello world !";
  161. const char *res;
  162. ttail->flag |= TTAIL_FLAG_PREFIX;
  163. ttail->prefix_sz = 4;
  164. res = ttail_logline_subst(ttail, expl);
  165. ck_assert(res != NULL);
  166. ck_assert(res == expl+4);
  167. }
  168. END_TEST
  169. START_TEST (test_search_subst_const2)
  170. {
  171. char expl[] = "Hello world !";
  172. const char *res;
  173. ttail->flag |= TTAIL_FLAG_PREFIX;
  174. ttail->prefix_sz = 0;
  175. res = ttail_logline_subst(ttail, expl);
  176. ck_assert(res != NULL);
  177. ck_assert(res == expl);
  178. }
  179. END_TEST
  180. START_TEST (test_search_subst_re1)
  181. {
  182. char expl[] = "1337 Foo Bar - Hello world !";
  183. char re[] = "^1337 Fo* Bar - ";
  184. const char *res;
  185. int ret;
  186. ret = ttail_set_prefix(ttail, re);
  187. ck_assert_int_eq(ret,0);
  188. res = ttail_logline_subst(ttail, expl);
  189. ck_assert(res != NULL);
  190. ck_assert_str_eq(res, "Hello world !");
  191. }
  192. END_TEST
  193. START_TEST (test_search_subst_re2)
  194. {
  195. char expl[] = "1337 Foo Bar - Hello world !";
  196. char re[] = "^[0-9]+ Fo{2} Bar - ";
  197. const char *res;
  198. int ret;
  199. ttail->flag |= TTAIL_FLAG_EXTENDED_RE;
  200. ret = ttail_set_prefix(ttail, re);
  201. ck_assert_int_eq(ret,0);
  202. res = ttail_logline_subst(ttail, expl);
  203. ck_assert(res != NULL);
  204. ck_assert_str_eq(res, "Hello world !");
  205. }
  206. END_TEST
  207. START_TEST (test_search_subst_re_nomatch)
  208. {
  209. char expl[] = "1337 Foo Bar - Hello world !";
  210. char re[] = "Never match m*";
  211. const char *res;
  212. int ret;
  213. ret = ttail_set_prefix(ttail, re);
  214. ck_assert_int_eq(ret,0);
  215. res = ttail_logline_subst(ttail, expl);
  216. ck_assert(res == NULL);
  217. }
  218. END_TEST
  219. /*
  220. * ttail_logline2date() checks
  221. */
  222. START_TEST (test_search_log2date1)
  223. {
  224. char re[] = "^[0-9]+ ";
  225. char fmt[] = "%Y-%m-%d:%H:%M";
  226. struct tm tm;
  227. int ret;
  228. ttail_set_flag_re_ex(ttail);
  229. ttail_set_prefix(ttail, re);
  230. ttail_set_fmt(ttail, fmt);
  231. ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
  232. ck_assert_int_eq(ret, 0);
  233. ck_assert_int_eq(tm.tm_year, 88);
  234. ck_assert_int_eq(tm.tm_mon, 9);
  235. ck_assert_int_eq(tm.tm_mday, 22);
  236. ck_assert_int_eq(tm.tm_hour, 22);
  237. ck_assert_int_eq(tm.tm_min, 10);
  238. }
  239. END_TEST
  240. START_TEST (test_search_log2date_failpref)
  241. {
  242. char re[] = "^[0-9]+aa ";
  243. char fmt[] = "%Y-%m-%d:%H:%M";
  244. struct tm tm;
  245. int ret;
  246. ttail_set_flag_re_ex(ttail);
  247. ttail_set_prefix(ttail, re);
  248. ttail_set_fmt(ttail, fmt);
  249. ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
  250. ck_assert_int_eq(ret, 1);
  251. ck_assert_int_eq(tm.tm_year, 0);
  252. ck_assert_int_eq(tm.tm_mon, 0);
  253. ck_assert_int_eq(tm.tm_mday, 0);
  254. ck_assert_int_eq(tm.tm_hour, 0);
  255. ck_assert_int_eq(tm.tm_min, 0);
  256. }
  257. END_TEST
  258. START_TEST (test_search_log2date_faildate)
  259. {
  260. char re[] = "^[0-9]+ ";
  261. char fmt[] = "%y-%m-%d:%H:%M";
  262. struct tm tm;
  263. int ret;
  264. ttail_set_flag_re_ex(ttail);
  265. ttail_set_prefix(ttail, re);
  266. ttail_set_fmt(ttail, fmt);
  267. ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
  268. ck_assert_int_eq(ret, 2);
  269. ck_assert_int_eq(tm.tm_year, 0);
  270. ck_assert_int_eq(tm.tm_mon, 0);
  271. ck_assert_int_eq(tm.tm_mday, 0);
  272. ck_assert_int_eq(tm.tm_hour, 0);
  273. ck_assert_int_eq(tm.tm_min, 0);
  274. }
  275. END_TEST
  276. Suite * ttail_search_files_suite(void)
  277. {
  278. Suite *s;
  279. TCase *tc_search_closest_fileinit, *tc_file_line,
  280. *tc_file_minmax;
  281. TCase *tc_search_subst, *tc_search_logline2date;
  282. s = suite_create("ttail search_files checks");
  283. tc_search_closest_fileinit = tcase_create("\
  284. ttail_logline_closest_files_init() checks");
  285. tcase_add_checked_fixture(tc_search_closest_fileinit,
  286. setup_closest_fileinit, teardown_closest_fileinit);
  287. tcase_add_test(tc_search_closest_fileinit, test_search_closest_init);
  288. tcase_add_test(tc_search_closest_fileinit,
  289. test_search_closest_init_filesz);
  290. tcase_add_test(tc_search_closest_fileinit,
  291. test_search_closest_init_vfile);
  292. tc_file_line = tcase_create("ttail_file_*line*() checks");
  293. tcase_add_checked_fixture(tc_file_line,
  294. setup_file_line, teardown_file_line);
  295. tcase_add_test(tc_file_line, test_file_line_next);
  296. tcase_add_test(tc_file_line, test_file_line_start);
  297. tc_file_minmax = tcase_create("ttail_file_minmax() checks");
  298. tcase_add_checked_fixture(tc_file_minmax,
  299. setup_closest_fileinit, teardown_closest_fileinit);
  300. tcase_add_test(tc_file_minmax, test_file_minmax1);
  301. tc_search_subst = tcase_create("ttail_logline_subst() checks");
  302. tcase_add_checked_fixture(tc_search_subst,
  303. setup_closest_fileinit, teardown_closest_fileinit);
  304. tcase_add_test(tc_search_subst, test_search_subst_const1);
  305. tcase_add_test(tc_search_subst, test_search_subst_const2);
  306. tcase_add_test(tc_search_subst, test_search_subst_re1);
  307. tcase_add_test(tc_search_subst, test_search_subst_re2);
  308. tcase_add_test(tc_search_subst, test_search_subst_re_nomatch);
  309. tc_search_logline2date = tcase_create("ttail_logline2date() checks");
  310. tcase_add_checked_fixture(tc_search_logline2date,
  311. setup_closest_fileinit, teardown_closest_fileinit);
  312. tcase_add_test(tc_search_subst, test_search_log2date1);
  313. tcase_add_test(tc_search_subst, test_search_log2date_failpref);
  314. tcase_add_test(tc_search_subst, test_search_log2date_faildate);
  315. suite_add_tcase(s, tc_search_closest_fileinit);
  316. suite_add_tcase(s, tc_file_line);
  317. suite_add_tcase(s, tc_file_minmax);
  318. suite_add_tcase(s, tc_search_subst);
  319. suite_add_tcase(s, tc_search_logline2date);
  320. return s;
  321. }
  322. int main(int argc, char **argv)
  323. {
  324. int number_failed = 0;
  325. SRunner *sr;
  326. chdir(dirname(argv[0])); /* move in ./tests dir */
  327. sr = srunner_create(ttail_search_files_suite());
  328. srunner_set_fork_status(sr, CK_FORK);
  329. srunner_run_all(sr,CK_VERBOSE);
  330. number_failed = srunner_ntests_failed(sr);
  331. srunner_free(sr);
  332. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  333. }