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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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_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_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_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_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. memcpy(&(ttail->date_min), &tm, sizeof(tm));
  293. ttail->flag |= TTAIL_FLAG_DATE_MIN;
  294. ret = ttail_search_files_init(ttail);
  295. ck_assert_int_eq(ret,0);
  296. ret = _ttail_search_closest_files(ttail);
  297. ck_assert_int_eq(ret, 0);
  298. ck_assert(ttail->session->file.off_min.id == 0);
  299. ck_assert(ttail->session->file.off_min.off == 0);
  300. }
  301. END_TEST
  302. START_TEST (test_search_files2)
  303. {
  304. int ret;
  305. size_t i;
  306. struct tm tm;
  307. memset(&tm, 0, sizeof(tm));
  308. for(i=1;i<ttail->logfile_sz;i++)
  309. {
  310. fclose(ttail->logfile[i]);
  311. ttail->logfile[i] = NULL;
  312. }
  313. ttail->flag |= TTAIL_FLAG_PREFIX;
  314. ttail->prefix_sz = 0;
  315. ttail_set_fmt(ttail, "%B%n%d %H:%M");
  316. tm.tm_year = -1;
  317. tm.tm_mon = 2;
  318. tm.tm_mday = 6;
  319. tm.tm_hour = 0;
  320. tm.tm_min = 29;
  321. tm.tm_sec = -1;
  322. memcpy(&(ttail->date_min), &tm, sizeof(tm));
  323. ttail->flag |= TTAIL_FLAG_DATE_MIN;
  324. ret = ttail_search_files_init(ttail);
  325. ck_assert_int_eq(ret,0);
  326. ret = _ttail_search_closest_files(ttail);
  327. ck_assert_int_eq(ret, 0);
  328. ck_assert(ttail->session->file.off_min.id == 0);
  329. ck_assert_int_eq(ttail->session->file.off_min.off, 221);
  330. }
  331. END_TEST
  332. START_TEST (test_search_files3)
  333. {
  334. int ret;
  335. size_t i;
  336. struct tm tm;
  337. memset(&tm, 0, sizeof(tm));
  338. for(i=1;i<ttail->logfile_sz-1;i++)
  339. {
  340. fclose(ttail->logfile[i]);
  341. ttail->logfile[i] = NULL;
  342. }
  343. ttail->flag |= TTAIL_FLAG_PREFIX;
  344. ttail->prefix_sz = 0;
  345. ttail_set_fmt(ttail, "%B%n%d %H:%M");
  346. tm.tm_year = -1;
  347. tm.tm_mon = 2;
  348. tm.tm_mday = 6;
  349. tm.tm_hour = 1;
  350. tm.tm_min = 0;
  351. tm.tm_sec = -1;
  352. memcpy(&(ttail->date_min), &tm, sizeof(tm));
  353. ttail->flag |= TTAIL_FLAG_DATE_MIN;
  354. ret = ttail_search_files_init(ttail);
  355. ck_assert_int_eq(ret,0);
  356. ret = _ttail_search_closest_files(ttail);
  357. ck_assert_int_eq(ret, 0);
  358. ck_assert_int_eq(ttail->session->file.off_min.id, 5);
  359. ck_assert_int_eq(ttail->session->file.off_min.off, 0);
  360. }
  361. END_TEST
  362. Suite * ttail_search_files_suite(void)
  363. {
  364. Suite *s;
  365. TCase *tc_search_closest_fileinit, *tc_file_line,
  366. *tc_file_minmax;
  367. TCase *tc_search_subst, *tc_search_logline2date;
  368. TCase *tc_search_files;
  369. s = suite_create("ttail search_files checks");
  370. tc_search_closest_fileinit = tcase_create("\
  371. ttail_logline_closest_files_init() checks");
  372. tcase_add_checked_fixture(tc_search_closest_fileinit,
  373. setup_closest_fileinit, teardown_closest_fileinit);
  374. tcase_add_test(tc_search_closest_fileinit, test_search_closest_init);
  375. tcase_add_test(tc_search_closest_fileinit,
  376. test_search_closest_init_filesz);
  377. tcase_add_test(tc_search_closest_fileinit,
  378. test_search_closest_init_vfile);
  379. tc_file_line = tcase_create("ttail_file_*line*() checks");
  380. tcase_add_checked_fixture(tc_file_line,
  381. setup_file_line, teardown_file_line);
  382. tcase_add_test(tc_file_line, test_file_line_next);
  383. tcase_add_test(tc_file_line, test_file_line_start);
  384. tc_file_minmax = tcase_create("ttail_file_minmax() checks");
  385. tcase_add_checked_fixture(tc_file_minmax,
  386. setup_closest_fileinit, teardown_closest_fileinit);
  387. tcase_add_test(tc_file_minmax, test_file_minmax1);
  388. tc_search_subst = tcase_create("ttail_logline_subst() checks");
  389. tcase_add_checked_fixture(tc_search_subst,
  390. setup_closest_fileinit, teardown_closest_fileinit);
  391. tcase_add_test(tc_search_subst, test_search_subst_const1);
  392. tcase_add_test(tc_search_subst, test_search_subst_const2);
  393. tcase_add_test(tc_search_subst, test_search_subst_re1);
  394. tcase_add_test(tc_search_subst, test_search_subst_re2);
  395. tcase_add_test(tc_search_subst, test_search_subst_re_nomatch);
  396. tc_search_logline2date = tcase_create("ttail_logline2date() checks");
  397. tcase_add_checked_fixture(tc_search_logline2date,
  398. setup_closest_fileinit, teardown_closest_fileinit);
  399. tcase_add_test(tc_search_subst, test_search_log2date1);
  400. tcase_add_test(tc_search_subst, test_search_log2date_failpref);
  401. tcase_add_test(tc_search_subst, test_search_log2date_faildate);
  402. tc_search_files = tcase_create("ttail_logline2date() checks");
  403. tcase_add_checked_fixture(tc_search_files,
  404. setup_closest_fileinit, teardown_closest_fileinit);
  405. tcase_add_test(tc_search_files, test_search_files1);
  406. tcase_add_test(tc_search_files, test_search_files2);
  407. tcase_add_test(tc_search_files, test_search_files3);
  408. suite_add_tcase(s, tc_search_closest_fileinit);
  409. suite_add_tcase(s, tc_file_line);
  410. suite_add_tcase(s, tc_file_minmax);
  411. suite_add_tcase(s, tc_search_subst);
  412. suite_add_tcase(s, tc_search_logline2date);
  413. suite_add_tcase(s, tc_search_files);
  414. return s;
  415. }
  416. int main(int argc, char **argv)
  417. {
  418. int number_failed = 0;
  419. SRunner *sr;
  420. chdir(dirname(argv[0])); /* move in ./tests dir */
  421. sr = srunner_create(ttail_search_files_suite());
  422. srunner_set_fork_status(sr, CK_FORK);
  423. srunner_run_all(sr,CK_VERBOSE);
  424. number_failed = srunner_ntests_failed(sr);
  425. srunner_free(sr);
  426. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  427. }