#ifndef _ttail_check_h__ #define _ttail_check_h__ #include #include #include #include #include #include #include #include #include "ttail.h" #include "ttail_init.h" ttail_t *ttail; #define __Fname_sz 5 #define FNAME_NO_EXIST 0 #define FNAME_EXIST 1 /**@brief Start a ttail test *@param const char* suite_str test suite string *@param const char* tc_str test case string */ #define TTAIL_CHECK_START(suite_str, tc_str) \ Suite * ttail_test_suite(void) \ {\ Suite *s;\ TCase *tc;\ s = suite_create(suite_str);\ tc = tcase_create(tc_str); /**@brief Set the setup & teardown fixture *@param void (*setup)() *@param void (*teardown)() */ #define TTAIL_SET_FIXTURE(setup, teardown) \ tcase_add_checked_fixture(tc, setup, teardown) /**@brief Add a test function defined using START_TEST(name) { ... } END_TEST */ #define TTAIL_ADD_TEST(test) tcase_add_test(tc, test) /**@brief End a ttail test */ #define TTAIL_CHECK_END \ suite_add_tcase(s, tc);\ return s;\ }\ \ int main(int argc, char **argv) {\ int n_fail;\ SRunner *sr;\ n_fail = chdir(dirname(argv[0])); /* move in ./tests dir */ \ if(n_fail < 0)\ {\ perror("Unable to chdir in tests folder");\ }\ sr = srunner_create(ttail_test_suite());\ srunner_set_fork_status(sr, CK_FORK);\ srunner_run_all(sr, CK_VERBOSE);\ n_fail = srunner_ntests_failed(sr);\ srunner_free(sr);\ return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;\ } #define ck_assert_printf(test, format, ...) {\ char _err_msg[512];\ snprintf(_err_msg, sizeof(char) * 512,format, __VA_ARGS__);\ ck_assert_msg(test, _err_msg);\ } #define close_stdpipe() {\ close(STDPIPE);\ STDPIPE = -1;\ } #define send_sample_stdpipe(id) {\ int ret;\ char buff[1024];\ while((ret= read(samples_fd[id], buff, 1024)) > 0)\ {\ ret = write(STDPIPE, buff, strlen(buff));\ if(ret < 0)\ {\ perror("Unable to write through pipe");\ ck_abort_msg("Unable to write through pipe");\ }\ }\ if(ret < 0)\ {\ perror("Unable to read through pipe");\ ck_abort_msg("Unable to read through pipe");\ }\ } char *fname[__Fname_sz]; /*date formats*/ char *fmt[] = TTAIL_DEFAULT_FORMATS; /* logfiles samples */ char *samples[6] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\ "./samples/4.log", "./samples/5.log", "./samples/1.1.log"}; off_t samples_sz[6] = { 442, 0, 893, 2587, 2310, 220 }; int samples_fd[6]; int STDPIPE; void teardown_ttail(void) { ttail_free(ttail); } void setup_ttail_empty(void) { ttail = ttail_init(1, (char**)&"foo"); } 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]); } } } void setup_file_line(void) { setup_ttail_empty(); ttail_add_logfile(ttail, samples[0]); } void teardown_closest(void) { ttail_free(ttail); } void setup_closest(void) { ttail = ttail_init(1, (char**)&"foo"); } void setup_closest_stdin(void) { int ret, pipes[2], i; ret = pipe(pipes); if(ret < 0) { perror("fail to open pipe"); ck_abort_msg("fail to open pipe"); } _ttail_set_stdin(pipes[0]); STDPIPE = pipes[1]; for(i=0; i 0) { close(STDPIPE); } teardown_closest(); } void setup_closest_fileinit(void) { size_t i; setup_closest(); for(i=0; i<6; i++) { ttail_add_logfile(ttail, samples[i]); } } void teardown_closest_fileinit(void) { _ttail_search_file_free(ttail); teardown_closest(); } #endif