123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597 |
- #include <check.h>
- #include <errno.h>
- #include <stdio.h>
- #include <unistd.h>
-
- #include "ttail.h"
- #include "ttail_init.h"
-
- ttail_t *ttail;
- #define __Fname_sz 5
- #define FNAME_NO_EXIST 0
- #define FNAME_EXIST 1
-
- char *fname[__Fname_sz];
- /*date formats*/
- char *fmt[] = TTAIL_DEFAULT_FORMATS;
-
- void teardown_ttail(void)
- {
- ttail_free(ttail);
- }
- void setup_ttail_empty(void)
- {
- ttail = ttail_init(1, (char**)&"foo");
- }
- /*
- * ttail_add_logfile() tests
- */
- START_TEST (test_init_empty_logfilename)
- {
- ck_assert_msg(ttail->logfile_name == NULL,
- "ttail_t.logfile_name should be NULL");
- }
- END_TEST
-
- START_TEST (test_init_nonexist_logfilename)
- {
- char *fname;
- int ret;
- /** @todo replace by mkstemp */
- fname = tempnam(NULL, "ttail_check");
-
- ret = ttail_add_logfile(ttail, fname);
- ck_assert_int_eq(ret, 1);
- ck_assert(ttail->logfile_name != NULL);
- ck_assert_str_eq(ttail->logfile_name[0], fname);
- ck_assert(ttail->logfile[0] == NULL);
- free(fname);
- }
- END_TEST
-
- START_TEST (test_init_exist_logfilename)
- {
- char *fname;
- int ret;
- FILE *fp;
- fname = tempnam(NULL, "ttail_check");
- if(!(fp = fopen(fname, "w+")))
- {
- perror("Unable to create file for testing");
- ck_abort_msg("Unable to create file for testing");
- }
- if(fclose(fp))
- {
- perror("Unable to close file for testing");
- ck_abort_msg("Unabe to close file for testing");
- }
-
- ret = ttail_add_logfile(ttail, fname);
- ck_assert_int_eq(ret, 0);
- ck_assert(ttail->logfile_name != NULL);
- ck_assert_str_eq(ttail->logfile_name[0], fname);
- ck_assert(ttail->logfile[0] != NULL);
- free(fname);
- }
- END_TEST
-
- START_TEST (test_init_same_exist_logfilename)
- {
- char *fname;
- int ret;
- FILE *fp;
- fname = tempnam(NULL, "ttail_check");
- if(!(fp = fopen(fname, "w+")))
- {
- perror("Unable to create file for testing");
- ck_abort_msg("Unable to create file for testing");
- }
- if(fclose(fp))
- {
- perror("Unable to close file for testing");
- ck_abort_msg("Unabe to close file for testing");
- }
-
- ret = ttail_add_logfile(ttail, fname);
- ck_assert_int_eq(ret, 0);
- ret = ttail_add_logfile(ttail, fname);
- ck_assert_int_eq(ret, -1);
- ck_assert_int_eq(ttail->logfile_sz, 1);
- ck_assert(ttail->logfile_name != NULL);
- ck_assert_str_eq(ttail->logfile_name[0], fname);
- ck_assert(ttail->logfile[0] != NULL);
- free(fname);
- }
- END_TEST
-
- START_TEST (test_init_same_nonexist_logfilename)
- {
- char *fname;
- int ret;
- fname = tempnam(NULL, "ttail_check");
-
- ret = ttail_add_logfile(ttail, fname);
- ck_assert_int_eq(ret, 1);
- ret = ttail_add_logfile(ttail, fname);
- ck_assert_int_eq(ret, -1);
- ck_assert_int_eq(ttail->logfile_sz, 1);
- ck_assert(ttail->logfile_name != NULL);
- ck_assert_str_eq(ttail->logfile_name[0], fname);
- ck_assert(ttail->logfile[0] == NULL);
- free(fname);
- }
- END_TEST
-
- START_TEST (test_init_multiple_logfilename)
- {
- char *fname[20];
- int ret;
- int i, j;
- for(i=0;i<20;i++)
- {
- fname[i] = tempnam(NULL, "ttail_check");
- }
-
- for(i=0; i<20; i++)
- {
- for(j=0; j<i; j++)
- {
- ret = ttail_add_logfile(ttail, fname[j]);
- ck_assert_int_eq(ret, -1);
- ck_assert_int_eq(ttail->logfile_sz, i);
- }
- ret = ttail_add_logfile(ttail, fname[i]);
- ck_assert_int_eq(ret, 1);
- ck_assert_int_eq(ttail->logfile_sz, i+1);
- for(j=0; j<i+1; j++)
- {
- ck_assert_str_eq(ttail->logfile_name[j], fname[j]);
- ck_assert(ttail->logfile[j] == NULL);
- }
- }
- for(i=0; i<20; i++)
- {
- free(fname[i]);
- }
- }
- END_TEST
-
- /*
- * ttail_set_prefix() tests
- */
- START_TEST (test_init_prefix)
- {
- int ret;
- ret = ttail_set_prefix(ttail, "^[^ ]+ ");
- ck_assert_int_eq(ret, 0);
- ck_assert((ttail->flag & TTAIL_FLAG_PREFIX) == TTAIL_FLAG_PREFIX);
- }
- END_TEST
-
- START_TEST (test_init_bad_prefix)
- {
- int ret;
- ret = ttail_set_prefix(ttail, "^[^ + ");
- ck_assert_int_eq(ret, -1);
- ck_assert((ttail->flag & TTAIL_FLAG_PREFIX) == 0);
- }
- END_TEST
-
- START_TEST (test_init_again_prefix)
- {
- int ret;
- ret = ttail_set_prefix(ttail, "^[^ ]+ ");
- ck_assert_int_eq(ret, 0);
- ck_assert((ttail->flag & TTAIL_FLAG_PREFIX) == TTAIL_FLAG_PREFIX);
- ret = ttail_set_prefix(ttail, "^[^ + ");
- ck_assert_int_eq(ret, 1);
- ck_assert((ttail->flag & TTAIL_FLAG_PREFIX) == TTAIL_FLAG_PREFIX);
- }
- END_TEST
-
- START_TEST (test_init_againbad_prefix)
- {
- int ret;
- ret = ttail_set_prefix(ttail, "^[^ + ");
- ck_assert_int_eq(ret, -1);
- ck_assert((ttail->flag & TTAIL_FLAG_PREFIX) == 0);
-
- ret = ttail_set_prefix(ttail, "^[^ ]+ ");
- ck_assert_int_eq(ret, 0);
- ck_assert((ttail->flag & TTAIL_FLAG_PREFIX) == TTAIL_FLAG_PREFIX);
- }
- END_TEST
-
- /*
- * ttail_set_fmt() checks
- */
- START_TEST (test_init_fmt)
- {
- int ret;
- ret = ttail_set_fmt(ttail, "%B");
- ck_assert_int_eq(ret, 0);
- ck_assert_int_eq(ttail->flag & TTAIL_FLAG_FORMAT, TTAIL_FLAG_FORMAT);
- ck_assert_str_eq(ttail->fmt, "%B");
- }
- END_TEST
-
- START_TEST (test_init_fmt_again)
- {
- int ret;
- ttail_set_fmt(ttail, "%B");
- ret = ttail_set_fmt(ttail, "%b");
- ck_assert_int_eq(ret, -1);
- ck_assert_int_eq(ttail->flag & TTAIL_FLAG_FORMAT, TTAIL_FLAG_FORMAT);
- ck_assert_str_eq(ttail->fmt, "%B");
- }
- END_TEST
-
- /*
- * ttail_format_guess() tests
- */
- START_TEST (test_init_guess_date)
- {
- int ret;
- struct tm tm;
- char res[] = "%Y/%m/%d";
- ret = ttail_format_guess(ttail, "1988/10/22", &tm);
- ck_assert_int_ne(ret, -1);
- ck_assert_str_eq(fmt[ret], res);
- ck_assert(ttail->fmt != NULL);
- ck_assert_str_eq(ttail->fmt, res);
- ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == TTAIL_FLAG_FORMAT);
- 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, 0);
- ck_assert_int_eq(tm.tm_min, 0);
- ck_assert_int_eq(tm.tm_sec, 0);
- }
- END_TEST
-
- START_TEST (test_init_guess_datetime)
- {
- int ret;
- struct tm tm;
- char res[] = "%Y/%m/%d:%H:%M";
- ret = ttail_format_guess(ttail, "1988/10/22:22:10:00", &tm);
- ck_assert_int_ne(ret, -1);
- ck_assert_str_eq(fmt[ret], res);
- ck_assert(ttail->fmt != NULL);
- ck_assert_str_eq(ttail->fmt, res);
- ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == TTAIL_FLAG_FORMAT);
- 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);
- ck_assert_int_eq(tm.tm_sec, 0);
- }
- END_TEST
-
- START_TEST (test_init_guess_datetime2)
- {
- int ret;
- struct tm tm;
- char res[] = "%B%n%d %H:%M:%S";
- ret = ttail_format_guess(ttail, "Mar 6 00:01:39 pilgrim dhclient", &tm);
- ck_assert_int_ne(ret, -1);
- ck_assert_str_eq(fmt[ret], res);
- ck_assert(ttail->fmt != NULL);
- ck_assert_str_eq(ttail->fmt, res);
- ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == TTAIL_FLAG_FORMAT);
- ck_assert_int_eq(tm.tm_year, 0);
- ck_assert_int_eq(tm.tm_mon, 2);
- ck_assert_int_eq(tm.tm_mday, 6);
- ck_assert_int_eq(tm.tm_hour, 0);
- ck_assert_int_eq(tm.tm_min, 1);
- ck_assert_int_eq(tm.tm_sec, 39);
- }
- END_TEST
-
- START_TEST (test_init_guess_noguess)
- {
- int ret;
- struct tm tm;
- ret = ttail_format_guess(ttail, "notadate", &tm);
- ck_assert_int_eq(ret, -1);
- ck_assert(ttail->fmt == NULL);
- ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == 0);
- 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);
- ck_assert_int_eq(tm.tm_sec, 0);
- }
- END_TEST
-
- START_TEST (test_init_guess_date_notm)
- {
- int ret;
- char res[] = "%Y/%m/%d";
- ret = ttail_format_guess(ttail, "1988/10/22", NULL);
- ck_assert_int_ne(ret, -1);
- ck_assert_str_eq(fmt[ret], res);
- ck_assert(ttail->fmt != NULL);
- ck_assert_str_eq(ttail->fmt, res);
- ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == TTAIL_FLAG_FORMAT);
- }
- END_TEST
-
- START_TEST (test_init_guess_noguess_notm)
- {
- int ret;
- struct tm tm;
- ret = ttail_format_guess(ttail, "notadate", &tm);
- ck_assert_int_eq(ret, -1);
- ck_assert(ttail->fmt == NULL);
- ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == 0);
- }
- END_TEST
-
- START_TEST (test_init_guess_fmt_set)
- {
- int ret;
- char res[] = "%Y/%m/%d";
- ret = ttail_format_guess(ttail, "1988/10/22", NULL);
- ck_assert_str_eq(fmt[ret], res);
- ret = ttail_format_guess(ttail, "1988/10/22", NULL);
- ck_assert_int_eq(ret, -2);
- ck_assert_str_eq(ttail->fmt, res);
- ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == TTAIL_FLAG_FORMAT);
- }
- END_TEST
-
- /*
- * ttail_set_dates() tests
- */
-
- START_TEST (test_init_setdates_nothing)
- {
- int ret;
- char *arg[] = {NULL, NULL};
- struct tm zero;
-
- memset(&zero, -1, sizeof(struct tm));
-
- ret = ttail_set_dates(ttail, arg);
- ck_assert_int_eq(ret, 0);
- ck_assert((ttail->flag & TTAIL_FLAG_DATE_MIN) == 0);
- ck_assert((ttail->flag & TTAIL_FLAG_DATE_MAX) == 0);
- ck_assert(memcmp(&zero, &(ttail->date_min), sizeof(struct tm)) == 0);
- ck_assert(memcmp(&zero, &(ttail->date_max), sizeof(struct tm)) == 0);
- }
- END_TEST
-
- START_TEST (test_init_setdates_nofmt_min)
- {
- int ret;
- char *arg[] = {"88/10/22", NULL};
- struct tm zero;
- char *arg0;
-
- arg0 = malloc(sizeof(char)*(strlen(arg[0])+1));
- if(!arg0)
- {
- perror("Malloc failed for argument");
- ck_abort_msg("Unable to allocate memory");
- }
- strcpy(arg0, arg[0]);
- arg[0]=arg0;
-
- memset(&zero, -1, sizeof(struct tm));
-
- ret = ttail_set_dates(ttail, arg);
- ck_assert_int_eq(ret, 0);
- ck_assert((ttail->flag & TTAIL_FLAG_DATE_MIN) == TTAIL_FLAG_DATE_MIN);
- ck_assert((ttail->flag & TTAIL_FLAG_DATE_MAX) == 0);
- ck_assert(memcmp(&zero, &(ttail->date_max), sizeof(struct tm)) == 0);
-
- ck_assert_int_eq(ttail->date_min.tm_year, 88);
- ck_assert_int_eq(ttail->date_min.tm_mon, 9);
- ck_assert_int_eq(ttail->date_min.tm_mday, 22);
- ck_assert_int_eq(ttail->date_min.tm_hour, 0);
- ck_assert_int_eq(ttail->date_min.tm_min, 0);
- ck_assert_int_eq(ttail->date_min.tm_sec, 0);
- }
- END_TEST
-
- START_TEST (test_init_setdates_nofmt_max)
- {
- int ret;
- char *arg[] = {NULL, "88/10/22"};
- struct tm zero;
- char *arg1;
-
- arg1 = malloc(sizeof(char)*(strlen(arg[1])+1));
- if(!arg1)
- {
- perror("Malloc failed for argument");
- ck_abort_msg("Unable to allocate memory");
- }
- strcpy(arg1, arg[1]);
- arg[1]=arg1;
-
- memset(&zero, -1, sizeof(struct tm));
-
- ret = ttail_set_dates(ttail, arg);
- ck_assert_int_eq(ret, 0);
- ck_assert((ttail->flag & TTAIL_FLAG_DATE_MIN) == 0);
- ck_assert((ttail->flag & TTAIL_FLAG_DATE_MAX) == TTAIL_FLAG_DATE_MAX);
- ck_assert(memcmp(&zero, &(ttail->date_min), sizeof(struct tm)) == 0);
-
- ck_assert_int_eq(ttail->date_max.tm_year, 88);
- ck_assert_int_eq(ttail->date_max.tm_mon, 9);
- ck_assert_int_eq(ttail->date_max.tm_mday, 22);
- ck_assert_int_eq(ttail->date_max.tm_hour, 0);
- ck_assert_int_eq(ttail->date_max.tm_min, 0);
- ck_assert_int_eq(ttail->date_max.tm_sec, 0);
- }
- END_TEST
-
- START_TEST (test_init_setdates_nofmt_both)
- {
- int ret;
- char *arg[] = {"1988/10/22", "2088/10/22"};
- struct tm zero;
- char *arg0, *arg1;
-
- arg0 = malloc(sizeof(char)*(strlen(arg[0])+1));
- if(!arg0)
- {
- perror("Malloc failed for argument");
- ck_abort_msg("Unable to allocate memory");
- }
- strcpy(arg0, arg[0]);
- arg[0]=arg0;
-
- arg1 = malloc(sizeof(char)*(strlen(arg[1])+1));
- if(!arg1)
- {
- perror("Malloc failed for argument");
- ck_abort_msg("Unable to allocate memory");
- }
- strcpy(arg1, arg[1]);
- arg[1]=arg1;
-
-
- memset(&zero, 0, sizeof(struct tm));
-
- ret = ttail_set_dates(ttail, arg);
- ck_assert_int_eq(ret, 0);
- ck_assert((ttail->flag & TTAIL_FLAG_DATE_MIN) == TTAIL_FLAG_DATE_MIN);
- ck_assert((ttail->flag & TTAIL_FLAG_DATE_MAX) == TTAIL_FLAG_DATE_MAX);
-
- ck_assert_int_eq(ttail->date_min.tm_year, 88);
- ck_assert_int_eq(ttail->date_min.tm_mon, 9);
- ck_assert_int_eq(ttail->date_min.tm_mday, 22);
- ck_assert_int_eq(ttail->date_min.tm_hour, 0);
- ck_assert_int_eq(ttail->date_min.tm_min, 0);
- ck_assert_int_eq(ttail->date_min.tm_sec, 0);
-
- ck_assert_int_eq(ttail->date_max.tm_year, 188);
- ck_assert_int_eq(ttail->date_max.tm_mon, 9);
- ck_assert_int_eq(ttail->date_max.tm_mday, 22);
- ck_assert_int_eq(ttail->date_max.tm_hour, -1);
- ck_assert_int_eq(ttail->date_max.tm_min, -1);
- ck_assert_int_eq(ttail->date_max.tm_sec, -1);
- }
- END_TEST
-
- START_TEST (test_init_check_bad1)
- {
- ck_assert_int_eq(ttail_init_check(ttail), -1);
- }
- END_TEST
-
- START_TEST (test_init_check_bad2)
- {
- ttail->flag |= TTAIL_FLAG_DATE_MAX;
- ck_assert_int_eq(ttail_init_check(ttail), -1);
- }
- END_TEST
-
- START_TEST (test_init_check1)
- {
- char *arg[] = {"88/10/22", NULL};
- struct tm zero;
- char *arg0;
-
- arg0 = malloc(sizeof(char)*(strlen(arg[0])+1));
- if(!arg0)
- {
- perror("Malloc failed for argument");
- ck_abort_msg("Unable to allocate memory");
- }
- strcpy(arg0, arg[0]);
- arg[0]=arg0;
-
- memset(&zero, 0, sizeof(struct tm));
-
- ttail_set_dates(ttail, arg);
- ck_assert_int_eq(ttail_init_check(ttail), 0);
- }
- END_TEST
-
- Suite * ttail_init_suite(void)
- {
- Suite *s;
- TCase *tc_init_logfile, *tc_init_prefix , *tc_init_fmt, \
- *tc_init_fmt_guess, *tc_init_set_dates, *tc_init_check;
-
- s = suite_create("ttail init checks");
-
- tc_init_logfile = tcase_create("logfile init checks");
- tcase_add_checked_fixture(tc_init_logfile,
- setup_ttail_empty, teardown_ttail);
- tcase_add_test(tc_init_logfile, test_init_empty_logfilename);
- tcase_add_test(tc_init_logfile, test_init_nonexist_logfilename);
- tcase_add_test(tc_init_logfile, test_init_exist_logfilename);
- tcase_add_test(tc_init_logfile, test_init_same_exist_logfilename);
- tcase_add_test(tc_init_logfile, test_init_same_nonexist_logfilename);
- tcase_add_test(tc_init_logfile, test_init_multiple_logfilename);
-
- tc_init_prefix = tcase_create("date prefix regex init checks");
- tcase_add_checked_fixture(tc_init_prefix,
- setup_ttail_empty, teardown_ttail);
- tcase_add_test(tc_init_prefix, test_init_prefix);
- tcase_add_test(tc_init_prefix, test_init_bad_prefix);
- tcase_add_test(tc_init_prefix, test_init_again_prefix);
- tcase_add_test(tc_init_prefix, test_init_againbad_prefix);
-
- tc_init_fmt = tcase_create("date format init checks");
- tcase_add_checked_fixture(tc_init_fmt,
- setup_ttail_empty, teardown_ttail);
- tcase_add_test(tc_init_fmt, test_init_fmt);
- tcase_add_test(tc_init_fmt, test_init_fmt_again);
-
- tc_init_fmt_guess = tcase_create("date format guess init checks");
- tcase_add_checked_fixture(tc_init_fmt_guess,
- setup_ttail_empty, teardown_ttail);
- tcase_add_test(tc_init_fmt_guess, test_init_guess_date);
- tcase_add_test(tc_init_fmt_guess, test_init_guess_datetime);
- tcase_add_test(tc_init_fmt_guess, test_init_guess_datetime2);
- tcase_add_test(tc_init_fmt_guess, test_init_guess_noguess);
- tcase_add_test(tc_init_fmt_guess, test_init_guess_date_notm);
- tcase_add_test(tc_init_fmt_guess, test_init_guess_noguess_notm);
- tcase_add_test(tc_init_fmt_guess, test_init_guess_fmt_set);
-
- tc_init_set_dates = tcase_create("dates min/max init checks");
- tcase_add_checked_fixture(tc_init_set_dates,
- setup_ttail_empty, teardown_ttail);
- tcase_add_test(tc_init_set_dates, test_init_setdates_nothing);
- tcase_add_test(tc_init_set_dates, test_init_setdates_nofmt_min);
- tcase_add_test(tc_init_set_dates, test_init_setdates_nofmt_max);
- tcase_add_test(tc_init_set_dates, test_init_setdates_nofmt_both);
-
- tc_init_check = tcase_create("ttail_init_check() checks");
- tcase_add_checked_fixture(tc_init_check,
- setup_ttail_empty, teardown_ttail);
- tcase_add_test(tc_init_fmt_guess, test_init_check_bad1);
- tcase_add_test(tc_init_fmt_guess, test_init_check_bad2);
- tcase_add_test(tc_init_fmt_guess, test_init_check1);
-
- suite_add_tcase(s, tc_init_logfile);
- suite_add_tcase(s, tc_init_prefix);
- suite_add_tcase(s, tc_init_fmt);
- suite_add_tcase(s, tc_init_fmt_guess);
- suite_add_tcase(s, tc_init_set_dates);
- suite_add_tcase(s, tc_init_check);
- return s;
- }
-
- int main(void)
- {
- int number_failed = 0;
- SRunner *sr;
-
- sr = srunner_create(ttail_init_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;
-
- }
|