123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- #include <check.h>
- #include <stdio.h>
- #include <libgen.h>
-
- #include "ttail.h"
- #include "ttail_init.h"
- #include "ttail_search.h"
-
- ttail_t *ttail;
-
- char *samples[6] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
- "./samples/4.log", "./samples/5.log", "./samples/1.1.log"};
- off_t samples_sz[6] = { 442, 0, 893, 2587, 2310, 220 };
- FILE *fpl;
-
- void setup_file_line(void)
- {
- fpl = fopen(samples[0], "r");
- if(!fpl)
- {
- perror("Unable to open file for testing :");
- ck_abort();
- }
- }
-
- void teardown_file_line(void)
- {
- fclose(fpl);
- }
-
- void teardown_closest(void)
- {
-
- ttail_free(ttail);
- }
-
- void setup_closest(void)
- {
- ttail = ttail_init(1, (char**)&"foo");
- }
-
- void setup_closest_fileinit(void)
- {
- size_t i;
- setup_closest();
- for(i=0; i<6; i++)
- {
- ttail_add_logfile(ttail, samples[i]);
- }
- }
-
- void teardown_closest_fileinit(void)
- {
- _ttail_search_file_free(ttail);
- teardown_closest();
- }
-
- START_TEST (test_search_closest_init)
- {
- int ret;
-
- ret = _ttail_search_closest_files_init(ttail);
- ck_assert_int_eq(ret,0);
- ck_assert(ttail->session->file.vpos == 0);
- #ifdef HUGEFILE
- ck_assert_int_eq(ttail->session->file.sz_div,0);
- #endif
- }
- END_TEST
-
- START_TEST (test_search_closest_init_filesz)
- {
- size_t i;
- _ttail_search_closest_files_init(ttail);
- for(i=0;i<6;i++)
- {
- ck_assert(samples_sz[i] == ttail->session->file.file_sz[i]);
- }
-
- }
- END_TEST
-
- START_TEST (test_search_closest_init_vfile)
- {
- size_t i;
- off_t full_sz;
-
- _ttail_search_closest_files_init(ttail);
-
- full_sz = 0;
- for(i=0;i<6;i++)
- {
- ck_assert(full_sz == ttail->session->file.vfile[i]);
- full_sz += samples_sz[i];
- }
- ck_assert(full_sz == ttail->session->file.vsz);
- }
- END_TEST
-
- /*
- * _ttail_file_next_line() & _ttail_file_line_start() tests
- */
- START_TEST (test_file_line_next)
- {
- long res;
- res = _ttail_file_next_line(fpl);
- ck_assert(res == 77);
- res = _ttail_file_next_line(fpl);
- ck_assert(res == 136);
- res = _ttail_file_next_line(fpl);
- ck_assert(res == 221);
- res = _ttail_file_next_line(fpl);
- ck_assert(res == 298);
- res = _ttail_file_next_line(fpl);
- ck_assert(res == 357);
- res = _ttail_file_next_line(fpl);
- ck_assert(res == 0);
- res = _ttail_file_next_line(fpl);
- ck_assert(res == -1);
- }
- END_TEST
-
- START_TEST (test_file_line_start)
- {
- long res;
- fseek(fpl, -1, SEEK_END);
- res = _ttail_file_start_line(fpl);
- ck_assert(res == 357);
- res = _ttail_file_start_line(fpl);
- ck_assert(res == 357);
- fseek(fpl, -1, SEEK_CUR);
- res = _ttail_file_start_line(fpl);
- ck_assert(res == 298);
- fseek(fpl, -1, SEEK_CUR);
- res = _ttail_file_start_line(fpl);
- ck_assert(res == 221);
- fseek(fpl, -1, SEEK_CUR);
- res = _ttail_file_start_line(fpl);
- ck_assert(res == 136);
- fseek(fpl, -1, SEEK_CUR);
- res = _ttail_file_start_line(fpl);
- ck_assert(res == 77);
- fseek(fpl, -1, SEEK_CUR);
- res = _ttail_file_start_line(fpl);
- ck_assert(res == 0);
- }
- END_TEST
-
- /*
- * _ttail_file_minmax() tests
- */
- START_TEST (test_file_minmax1)
- {
- /**@todo complete the testcase */
- int r;
- struct tm tm[2];
- ttail->flag |= TTAIL_FLAG_PREFIX;
- ttail->prefix_sz = 0;
- ttail_set_fmt(ttail, "%b%n%d %H:%M");
- r = _ttail_search_closest_files_init(ttail);
- ck_assert_int_eq(r, 0);
- r = _ttail_file_minmax(ttail, 0, tm);
- ck_assert_int_eq(r, 0);
- ck_assert_int_eq(tm[0].tm_mon, 2);
- ck_assert_int_eq(tm[0].tm_mday, 6);
- ck_assert_int_eq(tm[0].tm_hour, 0);
- ck_assert_int_eq(tm[0].tm_min, 1);
- ck_assert_int_eq(tm[1].tm_mon, 2);
- ck_assert_int_eq(tm[1].tm_mday, 6);
- ck_assert_int_eq(tm[1].tm_hour, 0);
- ck_assert_int_eq(tm[1].tm_min, 29);
- }
- END_TEST
-
- /*
- * ttail_logline_subst() tests
- */
- START_TEST (test_search_subst_const1)
- {
- char expl[] = "Hello world !";
- const char *res;
- ttail->flag |= TTAIL_FLAG_PREFIX;
- ttail->prefix_sz = 4;
- res = ttail_logline_subst(ttail, expl);
- ck_assert(res != NULL);
- ck_assert(res == expl+4);
- }
- END_TEST
-
- START_TEST (test_search_subst_const2)
- {
- char expl[] = "Hello world !";
- const char *res;
- ttail->flag |= TTAIL_FLAG_PREFIX;
- ttail->prefix_sz = 0;
- res = ttail_logline_subst(ttail, expl);
- ck_assert(res != NULL);
- ck_assert(res == expl);
- }
- END_TEST
-
- START_TEST (test_search_subst_re1)
- {
- char expl[] = "1337 Foo Bar - Hello world !";
- char re[] = "^1337 Fo* Bar - ";
- const char *res;
- int ret;
- ret = ttail_set_prefix(ttail, re);
- ck_assert_int_eq(ret,0);
- res = ttail_logline_subst(ttail, expl);
- ck_assert(res != NULL);
- ck_assert_str_eq(res, "Hello world !");
- }
- END_TEST
-
- START_TEST (test_search_subst_re2)
- {
- char expl[] = "1337 Foo Bar - Hello world !";
- char re[] = "^[0-9]+ Fo{2} Bar - ";
- const char *res;
- int ret;
- ttail->flag |= TTAIL_FLAG_EXTENDED_RE;
- ret = ttail_set_prefix(ttail, re);
- ck_assert_int_eq(ret,0);
- res = ttail_logline_subst(ttail, expl);
- ck_assert(res != NULL);
- ck_assert_str_eq(res, "Hello world !");
- }
- END_TEST
-
- START_TEST (test_search_subst_re_nomatch)
- {
- char expl[] = "1337 Foo Bar - Hello world !";
- char re[] = "Never match m*";
- const char *res;
- int ret;
- ret = ttail_set_prefix(ttail, re);
- ck_assert_int_eq(ret,0);
- res = ttail_logline_subst(ttail, expl);
- ck_assert(res == NULL);
- }
- END_TEST
-
- /*
- * ttail_logline2date() checks
- */
- START_TEST (test_search_log2date1)
- {
- char re[] = "^[0-9]+ ";
- char fmt[] = "%Y-%m-%d:%H:%M";
- struct tm tm;
- int ret;
- ttail_set_flag_re_ex(ttail);
- ttail_set_prefix(ttail, re);
- ttail_set_fmt(ttail, fmt);
- ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
- ck_assert_int_eq(ret, 0);
- ck_assert_int_eq(tm.tm_year, 88);
- ck_assert_int_eq(tm.tm_mon, 9);
- ck_assert_int_eq(tm.tm_mday, 22);
- ck_assert_int_eq(tm.tm_hour, 22);
- ck_assert_int_eq(tm.tm_min, 10);
- }
- END_TEST
-
- START_TEST (test_search_log2date_failpref)
- {
- char re[] = "^[0-9]+aa ";
- char fmt[] = "%Y-%m-%d:%H:%M";
- struct tm tm;
- int ret;
- ttail_set_flag_re_ex(ttail);
- ttail_set_prefix(ttail, re);
- ttail_set_fmt(ttail, fmt);
- ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
- ck_assert_int_eq(ret, 1);
- ck_assert_int_eq(tm.tm_year, 0);
- ck_assert_int_eq(tm.tm_mon, 0);
- ck_assert_int_eq(tm.tm_mday, 0);
- ck_assert_int_eq(tm.tm_hour, 0);
- ck_assert_int_eq(tm.tm_min, 0);
- }
- END_TEST
-
- START_TEST (test_search_log2date_faildate)
- {
- char re[] = "^[0-9]+ ";
- char fmt[] = "%y-%m-%d:%H:%M";
- struct tm tm;
- int ret;
- ttail_set_flag_re_ex(ttail);
- ttail_set_prefix(ttail, re);
- ttail_set_fmt(ttail, fmt);
- ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
- ck_assert_int_eq(ret, 2);
- ck_assert_int_eq(tm.tm_year, 0);
- ck_assert_int_eq(tm.tm_mon, 0);
- ck_assert_int_eq(tm.tm_mday, 0);
- ck_assert_int_eq(tm.tm_hour, 0);
- ck_assert_int_eq(tm.tm_min, 0);
- }
- END_TEST
-
- /*
- * tests ttail_search_closest_files()
- */
-
- START_TEST (test_search_files1)
- {
- int ret;
- size_t i;
- struct tm tm;
- memset(&tm, 0, sizeof(tm));
- for(i=1;i<ttail->logfile_sz;i++)
- {
- fclose(ttail->logfile[i]);
- ttail->logfile[i] = NULL;
- }
- ttail->flag |= TTAIL_FLAG_PREFIX;
- ttail->prefix_sz = 0;
- ttail_set_fmt(ttail, "%B%n%d %H:%M");
- memcpy(&(ttail->date_min), &tm, sizeof(tm));
- ttail->flag |= TTAIL_FLAG_DATE_MIN;
-
- ret = _ttail_search_closest_files(ttail);
- ck_assert_int_eq(ret, 0);
- ck_assert(ttail->session->file.off_min.id == 0);
- ck_assert(ttail->session->file.off_min.off == 0);
- }
- END_TEST
-
- START_TEST (test_search_files2)
- {
- int ret;
- size_t i;
- struct tm tm;
- memset(&tm, 0, sizeof(tm));
- for(i=1;i<ttail->logfile_sz;i++)
- {
- fclose(ttail->logfile[i]);
- ttail->logfile[i] = NULL;
- }
- ttail->flag |= TTAIL_FLAG_PREFIX;
- ttail->prefix_sz = 0;
- ttail_set_fmt(ttail, "%B%n%d %H:%M");
-
- tm.tm_year = -1;
- tm.tm_mon = 2;
- tm.tm_mday = 6;
- tm.tm_hour = 0;
- tm.tm_min = 29;
- tm.tm_sec = -1;
- memcpy(&(ttail->date_min), &tm, sizeof(tm));
- ttail->flag |= TTAIL_FLAG_DATE_MIN;
-
- ret = _ttail_search_closest_files(ttail);
- ck_assert_int_eq(ret, 0);
- ck_assert(ttail->session->file.off_min.id == 0);
- ck_assert_int_eq(ttail->session->file.off_min.off, 221);
- }
- END_TEST
-
- START_TEST (test_search_files3)
- {
- int ret;
- size_t i;
- struct tm tm;
- memset(&tm, 0, sizeof(tm));
- for(i=1;i<ttail->logfile_sz-1;i++)
- {
- fclose(ttail->logfile[i]);
- ttail->logfile[i] = NULL;
- }
- ttail->flag |= TTAIL_FLAG_PREFIX;
- ttail->prefix_sz = 0;
- ttail_set_fmt(ttail, "%B%n%d %H:%M");
-
- tm.tm_year = -1;
- tm.tm_mon = 2;
- tm.tm_mday = 6;
- tm.tm_hour = 1;
- tm.tm_min = 0;
- tm.tm_sec = -1;
- memcpy(&(ttail->date_min), &tm, sizeof(tm));
- ttail->flag |= TTAIL_FLAG_DATE_MIN;
-
- ret = _ttail_search_closest_files(ttail);
- ck_assert_int_eq(ret, 0);
- ck_assert_int_eq(ttail->session->file.off_min.id, 5);
- ck_assert_int_eq(ttail->session->file.off_min.off, 0);
- }
- END_TEST
-
- Suite * ttail_search_files_suite(void)
- {
- Suite *s;
- TCase *tc_search_closest_fileinit, *tc_file_line,
- *tc_file_minmax;
- TCase *tc_search_subst, *tc_search_logline2date;
- TCase *tc_search_files;
-
- s = suite_create("ttail search_files checks");
- tc_search_closest_fileinit = tcase_create("\
- ttail_logline_closest_files_init() checks");
- tcase_add_checked_fixture(tc_search_closest_fileinit,
- setup_closest_fileinit, teardown_closest_fileinit);
- tcase_add_test(tc_search_closest_fileinit, test_search_closest_init);
- tcase_add_test(tc_search_closest_fileinit,
- test_search_closest_init_filesz);
- tcase_add_test(tc_search_closest_fileinit,
- test_search_closest_init_vfile);
-
- tc_file_line = tcase_create("ttail_file_*line*() checks");
- tcase_add_checked_fixture(tc_file_line,
- setup_file_line, teardown_file_line);
- tcase_add_test(tc_file_line, test_file_line_next);
- tcase_add_test(tc_file_line, test_file_line_start);
-
- tc_file_minmax = tcase_create("ttail_file_minmax() checks");
- tcase_add_checked_fixture(tc_file_minmax,
- setup_closest_fileinit, teardown_closest_fileinit);
- tcase_add_test(tc_file_minmax, test_file_minmax1);
-
- tc_search_subst = tcase_create("ttail_logline_subst() checks");
- tcase_add_checked_fixture(tc_search_subst,
- setup_closest_fileinit, teardown_closest_fileinit);
- tcase_add_test(tc_search_subst, test_search_subst_const1);
- tcase_add_test(tc_search_subst, test_search_subst_const2);
- tcase_add_test(tc_search_subst, test_search_subst_re1);
- tcase_add_test(tc_search_subst, test_search_subst_re2);
- tcase_add_test(tc_search_subst, test_search_subst_re_nomatch);
-
- tc_search_logline2date = tcase_create("ttail_logline2date() checks");
- tcase_add_checked_fixture(tc_search_logline2date,
- setup_closest_fileinit, teardown_closest_fileinit);
- tcase_add_test(tc_search_subst, test_search_log2date1);
- tcase_add_test(tc_search_subst, test_search_log2date_failpref);
- tcase_add_test(tc_search_subst, test_search_log2date_faildate);
-
- tc_search_files = tcase_create("ttail_logline2date() checks");
- tcase_add_checked_fixture(tc_search_files,
- setup_closest_fileinit, teardown_closest_fileinit);
- tcase_add_test(tc_search_files, test_search_files1);
- tcase_add_test(tc_search_files, test_search_files2);
- tcase_add_test(tc_search_files, test_search_files3);
-
-
- suite_add_tcase(s, tc_search_closest_fileinit);
- suite_add_tcase(s, tc_file_line);
- suite_add_tcase(s, tc_file_minmax);
- suite_add_tcase(s, tc_search_subst);
- suite_add_tcase(s, tc_search_logline2date);
- suite_add_tcase(s, tc_search_files);
- return s;
- }
-
- int main(int argc, char **argv)
- {
- int number_failed = 0;
- SRunner *sr;
-
- chdir(dirname(argv[0])); /* move in ./tests dir */
-
- sr = srunner_create(ttail_search_files_suite());
- srunner_set_fork_status(sr, CK_FORK);
- srunner_run_all(sr,CK_VERBOSE);
- number_failed = srunner_ntests_failed(sr);
- srunner_free(sr);
- return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
-
- }
|