123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- #include <check.h>
- #include <errno.h>
- #include <stdio.h>
- #include <unistd.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];
-
- 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_flag)
- {
- ck_assert_msg(ttail->flag == 0,
- "ttail_t.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
- START_TEST (test_argparse_empty_prefixsz)
- {
- ck_assert_msg(ttail->prefix_sz == -1,
- "ttail_t.prefix_sz should be NULL");
- }
- END_TEST
- START_TEST (test_argparse_empty_session)
- {
- ck_assert_msg(ttail->session == NULL,
- "ttail_t.session should be NULL");
- }
- END_TEST
-
- /*
- * Bad arguments
- */
-
- START_TEST (test_argparse_bad1)
- {
- ttail_t *t;
- char *args[] = {"foo", "--sdfsdf"};
- t = ttail_init(2, args);
- ck_assert(t == NULL);
- }
- END_TEST
-
- START_TEST (test_argparse_bad2)
- {
- ttail_t *t;
- char *args[] = {"foo", "-x"};
- t = ttail_init(2, args);
- ck_assert(t == NULL);
- }
- END_TEST
-
- START_TEST (test_argparse_bad3)
- {
- ttail_t *t;
- char *args[] = {"foo", "-p"};
- t = ttail_init(2, args);
- ck_assert(t == NULL);
- }
- END_TEST
-
- START_TEST (test_argparse_bad4)
- {
- ttail_t *t;
- char *args[] = {"foo", "-f"};
- t = ttail_init(2, args);
- ck_assert(t == NULL);
- }
- END_TEST
-
- START_TEST (test_argparse_bad5)
- {
- ttail_t *t;
- char *args[] = {"foo", "-d"};
- t = ttail_init(2, args);
- ck_assert(t == NULL);
- }
- END_TEST
-
- START_TEST (test_argparse_bad6)
- {
- ttail_t *t;
- char *args[] = {"foo", "-m"};
- t = ttail_init(2, args);
- ck_assert(t == NULL);
- }
- END_TEST
-
- START_TEST (test_argparse_bad7)
- {
- ttail_t *t;
- char *args[] = {"foo", "-l"};
- t = ttail_init(2, args);
- ck_assert(t == NULL);
- }
- 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");
- }
- 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", "-l", NULL};
-
- args[2] = fname[FNAME_NO_EXIST];
- t = ttail_init(3, 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[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", "-l", NULL};
-
- args[2] = fname[FNAME_EXIST];
-
- t = ttail_init(3, 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[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)
- {
- ttail_t *t;
- int i;
- char *args[11] = {"foo", "-l", NULL, "-l", NULL,
- "-l", NULL, "-l", NULL, "-l", NULL};
-
- for(i=0; i<__Fname_sz; i++)
- {
- args[(i*2)+2] = fname[i];
- }
-
- t = ttail_init(11, 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*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
-
- START_TEST (test_argparse_reprefix_short)
- {
- ttail_t *t;
- char *args[] = {"foo", "-r", "^[^ ]+ "};
- t = ttail_init(3, args);
- ck_assert(t != NULL);
- ck_assert((t->flag & TTAIL_FLAG_PREFIX) == TTAIL_FLAG_PREFIX);
- }
- END_TEST
-
- START_TEST (test_argparse_reprefix_long)
- {
- ttail_t *t;
- char *args[] = {"foo", "--re-prefix", "^[^ ]+ "};
- t = ttail_init(3, args);
- ck_assert(t != NULL);
- ck_assert((t->flag & TTAIL_FLAG_PREFIX) == TTAIL_FLAG_PREFIX);
- }
- END_TEST
-
- START_TEST (test_argparse_prefixlen_short)
- {
- ttail_t *t;
- char *args[] = {"foo", "-p", "10"};
- t = ttail_init(3, args);
- ck_assert(t != NULL);
- ck_assert((t->flag & TTAIL_FLAG_PREFIX) == TTAIL_FLAG_PREFIX);
- ck_assert_int_eq(t->prefix_sz, 10);
- }
- END_TEST
-
- START_TEST (test_argparse_prefixlen_long)
- {
- ttail_t *t;
- char *args[] = {"foo", "--prefix-len", "42"};
- t = ttail_init(3, args);
- ck_assert(t != NULL);
- ck_assert((t->flag & TTAIL_FLAG_PREFIX) == TTAIL_FLAG_PREFIX);
- ck_assert_int_eq(t->prefix_sz, 42);
- }
- END_TEST
-
-
- START_TEST (test_argparse_prefix_multiple)
- {
- ttail_t *t;
- char *args[] = {"foo", "--re-prefix", "^[^ ]+ ", "-p", "20"};
- t = ttail_init(5, args);
- ck_assert(t == NULL);
- }
- END_TEST
-
- START_TEST (test_argparse_fmt_short)
- {
- ttail_t *t;
- char *args[] = {"foo", "-f", "%m"};
- t = ttail_init(3, args);
- ck_assert(t != NULL);
- }
- END_TEST
-
- START_TEST (test_argparse_fmt_long)
- {
- ttail_t *t;
- char *args[] = {"foo", "--date-format", "%m"};
- t = ttail_init(3, args);
- ck_assert(t != NULL);
- }
- END_TEST
-
- START_TEST (test_argparse_fmt_multiple)
- {
- ttail_t *t;
- char *args[] = {"foo", "-f", "%m", "-f", "%d"};
- t = ttail_init(5, args);
- ck_assert(t == NULL);
- }
- END_TEST
-
- Suite * ttail_init_suite(void)
- {
- Suite *s;
- TCase *tc_argparse_empty;
- TCase *tc_argparse_badarg;
- TCase *tc_argparse_files;
- TCase *tc_argparse_prefix;
- TCase *tc_argparse_fmt;
-
- 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_flag);
- tcase_add_test(tc_argparse_empty, test_argparse_empty_fmt);
- tcase_add_test(tc_argparse_empty, test_argparse_empty_verbose);
- tcase_add_test(tc_argparse_empty, test_argparse_empty_prefixsz);
- tcase_add_test(tc_argparse_empty, test_argparse_empty_session);
-
- tc_argparse_badarg = tcase_create("bad arguments parsing");
- tcase_add_test(tc_argparse_badarg, test_argparse_bad1);
- tcase_add_test(tc_argparse_badarg, test_argparse_bad2);
- tcase_add_test(tc_argparse_badarg, test_argparse_bad3);
- tcase_add_test(tc_argparse_badarg, test_argparse_bad4);
- tcase_add_test(tc_argparse_badarg, test_argparse_bad5);
- tcase_add_test(tc_argparse_badarg, test_argparse_bad6);
- tcase_add_test(tc_argparse_badarg, test_argparse_bad7);
-
- 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);
-
- tc_argparse_prefix = tcase_create("re-prefix arguments parsing");
- tcase_add_test(tc_argparse_prefix, test_argparse_reprefix_short);
- tcase_add_test(tc_argparse_prefix, test_argparse_reprefix_long);
- tcase_add_test(tc_argparse_prefix, test_argparse_prefixlen_short);
- tcase_add_test(tc_argparse_prefix, test_argparse_prefixlen_long);
- tcase_add_test(tc_argparse_prefix, test_argparse_prefix_multiple);
-
- tc_argparse_fmt = tcase_create("date format arguments parsing");
- tcase_add_test(tc_argparse_fmt, test_argparse_fmt_short);
- tcase_add_test(tc_argparse_fmt, test_argparse_fmt_long);
- tcase_add_test(tc_argparse_fmt, test_argparse_fmt_multiple);
-
- suite_add_tcase(s, tc_argparse_empty);
- suite_add_tcase(s, tc_argparse_badarg);
- suite_add_tcase(s, tc_argparse_files);
- suite_add_tcase(s, tc_argparse_prefix);
- suite_add_tcase(s, tc_argparse_fmt);
- 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;
-
- }
|