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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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[6] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
  9. "./samples/4.log", "./samples/5.log", "./samples/1.1.log"};
  10. off_t samples_sz[6] = { 442, 0, 893, 2587, 2310, 220 };
  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<6; 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<6;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<6;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. 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. /*
  155. * ttail_logline_subst() tests
  156. */
  157. START_TEST (test_search_subst_const1)
  158. {
  159. char expl[] = "Hello world !";
  160. const char *res;
  161. ttail->flag |= TTAIL_FLAG_PREFIX;
  162. ttail->prefix_sz = 4;
  163. res = ttail_logline_subst(ttail, expl);
  164. ck_assert(res != NULL);
  165. ck_assert(res == expl+4);
  166. }
  167. END_TEST
  168. START_TEST (test_search_subst_const2)
  169. {
  170. char expl[] = "Hello world !";
  171. const char *res;
  172. ttail->flag |= TTAIL_FLAG_PREFIX;
  173. ttail->prefix_sz = 0;
  174. res = ttail_logline_subst(ttail, expl);
  175. ck_assert(res != NULL);
  176. ck_assert(res == expl);
  177. }
  178. END_TEST
  179. START_TEST (test_search_subst_re1)
  180. {
  181. char expl[] = "1337 Foo Bar - Hello world !";
  182. char re[] = "^1337 Fo* Bar - ";
  183. const char *res;
  184. int ret;
  185. ret = ttail_set_prefix(ttail, re);
  186. ck_assert_int_eq(ret,0);
  187. res = ttail_logline_subst(ttail, expl);
  188. ck_assert(res != NULL);
  189. ck_assert_str_eq(res, "Hello world !");
  190. }
  191. END_TEST
  192. START_TEST (test_search_subst_re2)
  193. {
  194. char expl[] = "1337 Foo Bar - Hello world !";
  195. char re[] = "^[0-9]+ Fo{2} Bar - ";
  196. const char *res;
  197. int ret;
  198. ttail->flag |= TTAIL_FLAG_EXTENDED_RE;
  199. ret = ttail_set_prefix(ttail, re);
  200. ck_assert_int_eq(ret,0);
  201. res = ttail_logline_subst(ttail, expl);
  202. ck_assert(res != NULL);
  203. ck_assert_str_eq(res, "Hello world !");
  204. }
  205. END_TEST
  206. START_TEST (test_search_subst_re_nomatch)
  207. {
  208. char expl[] = "1337 Foo Bar - Hello world !";
  209. char re[] = "Never match m*";
  210. const char *res;
  211. int ret;
  212. ret = ttail_set_prefix(ttail, re);
  213. ck_assert_int_eq(ret,0);
  214. res = ttail_logline_subst(ttail, expl);
  215. ck_assert(res == NULL);
  216. }
  217. END_TEST
  218. /*
  219. * ttail_logline2date() checks
  220. */
  221. START_TEST (test_search_log2date1)
  222. {
  223. char re[] = "^[0-9]+ ";
  224. char fmt[] = "%Y-%m-%d:%H:%M";
  225. struct tm tm;
  226. int ret;
  227. ttail_set_flag_re_ex(ttail);
  228. ttail_set_prefix(ttail, re);
  229. ttail_set_fmt(ttail, fmt);
  230. ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
  231. ck_assert_int_eq(ret, 0);
  232. ck_assert_int_eq(tm.tm_year, 88);
  233. ck_assert_int_eq(tm.tm_mon, 9);
  234. ck_assert_int_eq(tm.tm_mday, 22);
  235. ck_assert_int_eq(tm.tm_hour, 22);
  236. ck_assert_int_eq(tm.tm_min, 10);
  237. }
  238. END_TEST
  239. START_TEST (test_search_log2date_failpref)
  240. {
  241. char re[] = "^[0-9]+aa ";
  242. char fmt[] = "%Y-%m-%d:%H:%M";
  243. struct tm tm;
  244. int ret;
  245. ttail_set_flag_re_ex(ttail);
  246. ttail_set_prefix(ttail, re);
  247. ttail_set_fmt(ttail, fmt);
  248. ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
  249. ck_assert_int_eq(ret, 1);
  250. ck_assert_int_eq(tm.tm_year, 0);
  251. ck_assert_int_eq(tm.tm_mon, 0);
  252. ck_assert_int_eq(tm.tm_mday, 0);
  253. ck_assert_int_eq(tm.tm_hour, 0);
  254. ck_assert_int_eq(tm.tm_min, 0);
  255. }
  256. END_TEST
  257. START_TEST (test_search_log2date_faildate)
  258. {
  259. char re[] = "^[0-9]+ ";
  260. char fmt[] = "%y-%m-%d:%H:%M";
  261. struct tm tm;
  262. int ret;
  263. ttail_set_flag_re_ex(ttail);
  264. ttail_set_prefix(ttail, re);
  265. ttail_set_fmt(ttail, fmt);
  266. ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
  267. ck_assert_int_eq(ret, 2);
  268. ck_assert_int_eq(tm.tm_year, 0);
  269. ck_assert_int_eq(tm.tm_mon, 0);
  270. ck_assert_int_eq(tm.tm_mday, 0);
  271. ck_assert_int_eq(tm.tm_hour, 0);
  272. ck_assert_int_eq(tm.tm_min, 0);
  273. }
  274. END_TEST
  275. /*
  276. * tests ttail_search_closest_files()
  277. */
  278. START_TEST (test_search_files1)
  279. {
  280. int ret;
  281. size_t i;
  282. struct tm tm;
  283. memset(&tm, 0, sizeof(tm));
  284. for(i=1;i<ttail->logfile_sz;i++)
  285. {
  286. fclose(ttail->logfile[i]);
  287. ttail->logfile[i] = NULL;
  288. }
  289. ttail->flag |= TTAIL_FLAG_PREFIX;
  290. ttail->prefix_sz = 0;
  291. ttail_set_fmt(ttail, "%B%n%d %H:%M");
  292. ret = _ttail_search_closest_files(ttail, &tm);
  293. ck_assert_int_eq(ret, 0);
  294. ck_assert(ttail->session->file.id == 0);
  295. ck_assert(ttail->session->file.off == 0);
  296. }
  297. END_TEST
  298. START_TEST (test_search_files2)
  299. {
  300. int ret;
  301. size_t i;
  302. struct tm tm;
  303. memset(&tm, 0, sizeof(tm));
  304. for(i=1;i<ttail->logfile_sz;i++)
  305. {
  306. fclose(ttail->logfile[i]);
  307. ttail->logfile[i] = NULL;
  308. }
  309. ttail->flag |= TTAIL_FLAG_PREFIX;
  310. ttail->prefix_sz = 0;
  311. ttail_set_fmt(ttail, "%B%n%d %H:%M");
  312. tm.tm_year = -1;
  313. tm.tm_mon = 2;
  314. tm.tm_mday = 6;
  315. tm.tm_hour = 0;
  316. tm.tm_min = 29;
  317. tm.tm_sec = -1;
  318. ret = _ttail_search_closest_files(ttail, &tm);
  319. ck_assert_int_eq(ret, 0);
  320. ck_assert(ttail->session->file.id == 0);
  321. ck_assert_int_eq(ttail->session->file.off, 221);
  322. }
  323. END_TEST
  324. START_TEST (test_search_files3)
  325. {
  326. int ret;
  327. size_t i;
  328. struct tm tm;
  329. memset(&tm, 0, sizeof(tm));
  330. for(i=1;i<ttail->logfile_sz-1;i++)
  331. {
  332. fclose(ttail->logfile[i]);
  333. ttail->logfile[i] = NULL;
  334. }
  335. ttail->flag |= TTAIL_FLAG_PREFIX;
  336. ttail->prefix_sz = 0;
  337. ttail_set_fmt(ttail, "%B%n%d %H:%M");
  338. tm.tm_year = -1;
  339. tm.tm_mon = 2;
  340. tm.tm_mday = 6;
  341. tm.tm_hour = 1;
  342. tm.tm_min = 0;
  343. tm.tm_sec = -1;
  344. ret = _ttail_search_closest_files(ttail, &tm);
  345. ck_assert_int_eq(ret, 0);
  346. ck_assert_int_eq(ttail->session->file.id, 5);
  347. ck_assert_int_eq(ttail->session->file.off, 0);
  348. }
  349. END_TEST
  350. Suite * ttail_search_files_suite(void)
  351. {
  352. Suite *s;
  353. TCase *tc_search_closest_fileinit, *tc_file_line,
  354. *tc_file_minmax;
  355. TCase *tc_search_subst, *tc_search_logline2date;
  356. TCase *tc_search_files;
  357. s = suite_create("ttail search_files checks");
  358. tc_search_closest_fileinit = tcase_create("\
  359. ttail_logline_closest_files_init() checks");
  360. tcase_add_checked_fixture(tc_search_closest_fileinit,
  361. setup_closest_fileinit, teardown_closest_fileinit);
  362. tcase_add_test(tc_search_closest_fileinit, test_search_closest_init);
  363. tcase_add_test(tc_search_closest_fileinit,
  364. test_search_closest_init_filesz);
  365. tcase_add_test(tc_search_closest_fileinit,
  366. test_search_closest_init_vfile);
  367. tc_file_line = tcase_create("ttail_file_*line*() checks");
  368. tcase_add_checked_fixture(tc_file_line,
  369. setup_file_line, teardown_file_line);
  370. tcase_add_test(tc_file_line, test_file_line_next);
  371. tcase_add_test(tc_file_line, test_file_line_start);
  372. tc_file_minmax = tcase_create("ttail_file_minmax() checks");
  373. tcase_add_checked_fixture(tc_file_minmax,
  374. setup_closest_fileinit, teardown_closest_fileinit);
  375. tcase_add_test(tc_file_minmax, test_file_minmax1);
  376. tc_search_subst = tcase_create("ttail_logline_subst() checks");
  377. tcase_add_checked_fixture(tc_search_subst,
  378. setup_closest_fileinit, teardown_closest_fileinit);
  379. tcase_add_test(tc_search_subst, test_search_subst_const1);
  380. tcase_add_test(tc_search_subst, test_search_subst_const2);
  381. tcase_add_test(tc_search_subst, test_search_subst_re1);
  382. tcase_add_test(tc_search_subst, test_search_subst_re2);
  383. tcase_add_test(tc_search_subst, test_search_subst_re_nomatch);
  384. tc_search_logline2date = tcase_create("ttail_logline2date() checks");
  385. tcase_add_checked_fixture(tc_search_logline2date,
  386. setup_closest_fileinit, teardown_closest_fileinit);
  387. tcase_add_test(tc_search_subst, test_search_log2date1);
  388. tcase_add_test(tc_search_subst, test_search_log2date_failpref);
  389. tcase_add_test(tc_search_subst, test_search_log2date_faildate);
  390. tc_search_files = tcase_create("ttail_logline2date() checks");
  391. tcase_add_checked_fixture(tc_search_files,
  392. setup_closest_fileinit, teardown_closest_fileinit);
  393. tcase_add_test(tc_search_files, test_search_files1);
  394. tcase_add_test(tc_search_files, test_search_files2);
  395. tcase_add_test(tc_search_files, test_search_files3);
  396. suite_add_tcase(s, tc_search_closest_fileinit);
  397. suite_add_tcase(s, tc_file_line);
  398. suite_add_tcase(s, tc_file_minmax);
  399. suite_add_tcase(s, tc_search_subst);
  400. suite_add_tcase(s, tc_search_logline2date);
  401. suite_add_tcase(s, tc_search_files);
  402. return s;
  403. }
  404. int main(int argc, char **argv)
  405. {
  406. int number_failed = 0;
  407. SRunner *sr;
  408. chdir(dirname(argv[0])); /* move in ./tests dir */
  409. sr = srunner_create(ttail_search_files_suite());
  410. srunner_set_fork_status(sr, CK_FORK);
  411. srunner_run_all(sr,CK_VERBOSE);
  412. number_failed = srunner_ntests_failed(sr);
  413. srunner_free(sr);
  414. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  415. }