timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ttail_argparse_check.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include <check.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include "ttail.h"
  6. ttail_t *ttail;
  7. #define __Fname_sz 5
  8. #define FNAME_NO_EXIST 0
  9. #define FNAME_EXIST 1
  10. char *fname[__Fname_sz];
  11. void teardown_ttail(void)
  12. {
  13. ttail_free(ttail);
  14. }
  15. /*
  16. * Empty argument parsing test case
  17. */
  18. void setup_ttail_empty(void)
  19. {
  20. ttail = ttail_init(1, (char**)&"foo");
  21. }
  22. START_TEST (test_argparse_empty_logfilename)
  23. {
  24. ck_assert_msg(ttail->logfile_name == NULL,
  25. "ttail_t.logfile_name should be NULL");
  26. }
  27. END_TEST
  28. START_TEST (test_argparse_empty_logfile)
  29. {
  30. ck_assert_msg(ttail->logfile == NULL,
  31. "ttail_t.logfile should be NULL");
  32. }
  33. END_TEST
  34. START_TEST (test_argparse_empty_logfilesz)
  35. {
  36. ck_assert_msg(ttail->logfile_sz == 0,
  37. "ttail_t.logfile_sz should be 0");
  38. }
  39. END_TEST
  40. START_TEST (test_argparse_empty_flag)
  41. {
  42. ck_assert_msg(ttail->flag == 0,
  43. "ttail_t.flag should be 0");
  44. }
  45. END_TEST
  46. START_TEST (test_argparse_empty_fmt)
  47. {
  48. ck_assert_msg(ttail->fmt == NULL,
  49. "ttail_t.fmt should be NULL");
  50. }
  51. END_TEST
  52. START_TEST (test_argparse_empty_verbose)
  53. {
  54. ck_assert_msg(ttail->verbose == 0,
  55. "ttail_t.verbose should be 0");
  56. }
  57. END_TEST
  58. /*
  59. * file argument parsing
  60. */
  61. void setup_fname(void)
  62. {
  63. int i;
  64. FILE *fp;
  65. for(i=0;i<__Fname_sz;i++)
  66. {
  67. fname[i] = NULL;
  68. }
  69. for(i=0; i<__Fname_sz; i++)
  70. {
  71. fname[i] = tempnam(NULL, "ttail_check");
  72. if(i%2)
  73. {
  74. if((fp=fopen(fname[i], "w+")) == NULL)
  75. {
  76. perror("Unable to create file for testing");
  77. ck_abort_msg("Unable to create file for testing");
  78. }
  79. if(fclose(fp))
  80. {
  81. perror("Unable to close file for testing");
  82. ck_abort_msg("Unable to close file for testing");
  83. }
  84. }
  85. }
  86. }
  87. void teardown_fname(void)
  88. {
  89. int i;
  90. for(i=0; i<__Fname_sz; i++)
  91. {
  92. unlink(fname[i]);
  93. if(fname[i] != NULL)
  94. {
  95. free(fname[i]);
  96. }
  97. }
  98. }
  99. START_TEST (test_argparse_file_non_exist)
  100. {
  101. ttail_t *t;
  102. char *args[3] = {"foo", "-l", NULL};
  103. args[2] = fname[FNAME_NO_EXIST];
  104. t = ttail_init(3, args);
  105. ck_assert_msg(t != NULL, "init failed");
  106. ck_assert_msg(t->logfile_sz == 1,
  107. "ttail_t.logfile_sz should be 1");
  108. ck_assert_msg(strcmp(t->logfile_name[0], args[2]) == 0,
  109. "ttail_t.logfile_name[0] does not contain the filename");
  110. ck_assert_msg(t->logfile[0] == NULL,
  111. "ttail_t.logfile[0] should be NULL");
  112. ttail_free(t);
  113. }
  114. END_TEST
  115. START_TEST (test_argparse_file_exist)
  116. {
  117. ttail_t *t;
  118. char *args[3] = {"foo", "-l", NULL};
  119. args[2] = fname[FNAME_EXIST];
  120. t = ttail_init(3, args);
  121. ck_assert_msg(t != NULL, "init failed");
  122. ck_assert_msg(t->logfile_sz == 1,
  123. "ttail_t.logfile_sz should be 1");
  124. ck_assert_msg(strcmp(t->logfile_name[0], args[2]) == 0,
  125. "ttail_t.logfile_name[0] does not contain the filename");
  126. ck_assert_msg(t->logfile[0] != NULL,
  127. "ttail_t.logfile[0] shouldn't be NULL");
  128. ttail_free(t);
  129. }
  130. END_TEST
  131. START_TEST (test_argparse_file_multiple)
  132. {
  133. ttail_t *t;
  134. int i;
  135. char *args[11] = {"foo", "-l", NULL, "-l", NULL,
  136. "-l", NULL, "-l", NULL, "-l", NULL};
  137. for(i=0; i<__Fname_sz; i++)
  138. {
  139. args[(i*2)+2] = fname[i];
  140. }
  141. t = ttail_init(11, args);
  142. ck_assert_msg(t != NULL, "init failed");
  143. ck_assert_msg(t->logfile_sz == __Fname_sz,
  144. "ttail_t.logfile_sz doesn't have the good value");
  145. for(i=0;i<__Fname_sz; i++)
  146. {
  147. ck_assert_msg(strcmp(t->logfile_name[i], args[(i*2)+2]) == 0,
  148. "some filename corrupted in ttail_t.logfile_name");
  149. if(i%2)
  150. {
  151. ck_assert_msg(t->logfile[i] != NULL,
  152. "logfile not opened");
  153. } else {
  154. ck_assert_msg(t->logfile[i] == NULL,
  155. "logfile should be NULL");
  156. }
  157. }
  158. }
  159. END_TEST
  160. Suite * ttail_init_suite(void)
  161. {
  162. Suite *s;
  163. TCase *tc_argparse_empty;
  164. TCase *tc_argparse_files;
  165. s = suite_create("ttail argument parsing checks");
  166. tc_argparse_empty = tcase_create("empty arguments parsing");
  167. tcase_add_checked_fixture(tc_argparse_empty,
  168. setup_ttail_empty, teardown_ttail);
  169. tcase_add_test(tc_argparse_empty, test_argparse_empty_logfilename);
  170. tcase_add_test(tc_argparse_empty, test_argparse_empty_logfile);
  171. tcase_add_test(tc_argparse_empty, test_argparse_empty_logfilesz);
  172. tcase_add_test(tc_argparse_empty, test_argparse_empty_flag);
  173. tcase_add_test(tc_argparse_empty, test_argparse_empty_fmt);
  174. tcase_add_test(tc_argparse_empty, test_argparse_empty_verbose);
  175. tc_argparse_files = tcase_create("files options parse");
  176. tcase_add_checked_fixture(tc_argparse_files,
  177. setup_fname, teardown_fname);
  178. tcase_add_test(tc_argparse_files, test_argparse_file_non_exist);
  179. tcase_add_test(tc_argparse_files, test_argparse_file_exist);
  180. tcase_add_test(tc_argparse_files, test_argparse_file_multiple);
  181. suite_add_tcase(s, tc_argparse_files);
  182. suite_add_tcase(s, tc_argparse_empty);
  183. return s;
  184. }
  185. int main(void)
  186. {
  187. int number_failed = 0;
  188. SRunner *sr;
  189. sr = srunner_create(ttail_init_suite());
  190. srunner_set_fork_status(sr, CK_FORK);
  191. srunner_run_all(sr,CK_VERBOSE);
  192. number_failed = srunner_ntests_failed(sr);
  193. srunner_free(sr);
  194. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  195. }