123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- #include <check.h>
- #include <errno.h>
- #include <stdio.h>
- #include <unistd.h>
-
- #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);
- unlink(fname);
- 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);
- unlink(fname);
- 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++)
- {
- unlink(fname[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
|