#include #include #include #include #include "ttail_check.h" #include "ttail.h" #include "ttail_init.h" /* * 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_CHECK_START("ttail init checks", "logfile init checks") TTAIL_SET_FIXTURE(setup_ttail_empty, teardown_ttail); TTAIL_ADD_TEST(test_init_empty_logfilename); TTAIL_ADD_TEST(test_init_nonexist_logfilename); TTAIL_ADD_TEST(test_init_exist_logfilename); TTAIL_ADD_TEST(test_init_same_exist_logfilename); TTAIL_ADD_TEST(test_init_same_nonexist_logfilename); TTAIL_ADD_TEST(test_init_multiple_logfilename); TTAIL_CHECK_END