#include #include #include #include #include "ttail.h" ttail_t *ttail; #define __Fname_sz 5 #define FNAME_NO_EXIST 0 #define FNAME_EXIST 1 char *fname[__Fname_sz]; void teardown_ttail(void) { ttail_free(ttail); } /* * Empty argument parsing test case */ void setup_ttail_empty(void) { ttail = ttail_init(1, (char**)&"foo"); } START_TEST (test_argparse_empty_logfilename) { ck_assert_msg(ttail->logfile_name == NULL, "ttail_t.logfile_name should be NULL"); } END_TEST START_TEST (test_argparse_empty_logfile) { ck_assert_msg(ttail->logfile == NULL, "ttail_t.logfile should be NULL"); } END_TEST START_TEST (test_argparse_empty_logfilesz) { ck_assert_msg(ttail->logfile_sz == 0, "ttail_t.logfile_sz should be 0"); } END_TEST START_TEST (test_argparse_empty_prefix_flag) { ck_assert_msg(ttail->date_prefix_flag == 0, "ttail_t.date_prefix_flag should be 0"); } END_TEST START_TEST (test_argparse_empty_fmt) { ck_assert_msg(ttail->fmt == NULL, "ttail_t.fmt should be NULL"); } END_TEST START_TEST (test_argparse_empty_verbose) { ck_assert_msg(ttail->verbose == 0, "ttail_t.verbose should be 0"); } END_TEST /* * file argument parsing */ void setup_fname(void) { int i; FILE *fp; for(i=0;i<__Fname_sz;i++) { fname[i] = NULL; } for(i=0; i<__Fname_sz; i++) { fname[i] = tempnam(NULL, "ttail_check"); if(i%2) { if((fp=fopen(fname[i], "w+")) == NULL) { perror("Unable to create file for testing"); ck_abort_msg("Unable to create file for testing"); } else { printf("Created : %s\n", fname[i]);} if(fclose(fp)) { perror("Unable to close file for testing"); ck_abort_msg("Unable to close file for testing"); } } } } void teardown_fname(void) { int i; for(i=0; i<__Fname_sz; i++) { unlink(fname[i]); if(fname[i] != NULL) { free(fname[i]); } } } START_TEST (test_argparse_file_non_exist) { ttail_t *t; char *args[3] = {"foo", "-f", NULL}; args[2] = fname[FNAME_NO_EXIST]; t = ttail_init(3, args); ck_assert_msg(t->logfile_sz == 1, "ttail_t.logfile_sz should be 1"); ck_assert_msg(strcmp(t->logfile_name[0], args[2]) == 0, "ttail_t.logfile_name[0] does not contain the filename"); ck_assert_msg(t->logfile[0] == NULL, "ttail_t.logfile[0] should be NULL"); ttail_free(t); } END_TEST START_TEST (test_argparse_file_exist) { ttail_t *t; char *args[3] = {"foo", "-f", NULL}; args[2] = fname[FNAME_EXIST]; t = ttail_init(3, args); ck_assert_msg(t->logfile_sz == 1, "ttail_t.logfile_sz should be 1"); ck_assert_msg(strcmp(t->logfile_name[0], args[2]) == 0, "ttail_t.logfile_name[0] does not contain the filename"); ck_assert_msg(t->logfile[0] != NULL, "ttail_t.logfile[0] shouldn't be NULL"); ttail_free(t); } END_TEST START_TEST (test_argparse_file_multiple) { printf("MULTIPLE\n"); ttail_t *t; int i; char *args[11] = {"foo", "-f", NULL, "-f", NULL, "-f", NULL, "-f", NULL, "-f", NULL}; for(i=0; i<__Fname_sz; i++) { args[(i*2)+2] = fname[i]; } t = ttail_init(11, args); ck_assert_msg(t->logfile_sz == __Fname_sz, "ttail_t.logfile_sz doesn't have the good value"); for(i=0;i<__Fname_sz; i++) { ck_assert_msg(strcmp(t->logfile_name[i], args[(i*2)+2]) == 0, "some filename corrupted in ttail_t.logfile_name"); if(i%2) { ck_assert_msg(t->logfile[i] != NULL, "logfile not opened"); } else { ck_assert_msg(t->logfile[i] == NULL, "logfile should be NULL"); } } } END_TEST Suite * ttail_init_suite(void) { Suite *s; TCase *tc_argparse_empty; TCase *tc_argparse_files; s = suite_create("ttail argument parsing checks"); tc_argparse_empty = tcase_create("empty arguments parsing"); tcase_add_checked_fixture(tc_argparse_empty, setup_ttail_empty, teardown_ttail); tcase_add_test(tc_argparse_empty, test_argparse_empty_logfilename); tcase_add_test(tc_argparse_empty, test_argparse_empty_logfile); tcase_add_test(tc_argparse_empty, test_argparse_empty_logfilesz); tcase_add_test(tc_argparse_empty, test_argparse_empty_prefix_flag); tcase_add_test(tc_argparse_empty, test_argparse_empty_fmt); tcase_add_test(tc_argparse_empty, test_argparse_empty_verbose); tc_argparse_files = tcase_create("files options parse"); tcase_add_checked_fixture(tc_argparse_files, setup_fname, teardown_fname); tcase_add_test(tc_argparse_files, test_argparse_file_non_exist); tcase_add_test(tc_argparse_files, test_argparse_file_exist); tcase_add_test(tc_argparse_files, test_argparse_file_multiple); suite_add_tcase(s, tc_argparse_files); suite_add_tcase(s, tc_argparse_empty); 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; }