123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #include <check.h>
- #include <errno.h>
- #include <stdio.h>
- #include <unistd.h>
-
- #include "ttail_check.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];
-
- /*
- * 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");
- }
- 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[4] = {"foo", "-d", "Mar 6 13:37", NULL};
-
- args[3] = fname[FNAME_NO_EXIST];
- t = ttail_init(4, args);
- ck_assert_msg(t != NULL, "init failed");
-
- ck_assert_msg(t->logfile_sz == 1,
- "ttail_t.logfile_sz should be 1");
- ck_assert_msg(strcmp(t->logfile_name[0], args[3]) == 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[4] = {"foo", "-d", "Mar 10 13:37", NULL};
-
- args[3] = fname[FNAME_EXIST];
-
- t = ttail_init(4, args);
- ck_assert_msg(t != NULL, "init failed");
-
- ck_assert_msg(t->logfile_sz == 1,
- "ttail_t.logfile_sz should be 1");
- ck_assert_msg(strcmp(t->logfile_name[0], args[3]) == 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)
- {
- ttail_t *t;
- int i;
- char *args[8] = {"foo", "-d", "mar 10 13:37", NULL, NULL, NULL, NULL,
- NULL};
-
- for(i=0; i<__Fname_sz; i++)
- {
- args[i+3] = fname[i];
- }
-
- t = ttail_init(8, args);
- ck_assert_msg(t != NULL, "init failed");
-
- 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+3]) == 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_files;
-
- s = suite_create("ttail argument parsing checks");
-
- 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);
- return s;
- }
-
- TTAIL_CHECK_MAIN(ttail_init_suite)
|