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

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