#include #include #include #include #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; jlogfile_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; jlogfile_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; }