|
@@ -0,0 +1,150 @@
|
|
1
|
+#include <check.h>
|
|
2
|
+#include <errno.h>
|
|
3
|
+#include <stdio.h>
|
|
4
|
+#include <unistd.h>
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+#include "ttail.h"
|
|
8
|
+
|
|
9
|
+ttail_t *ttail;
|
|
10
|
+#define __Fname_sz 5
|
|
11
|
+#define FNAME_NO_EXIST 0
|
|
12
|
+#define FNAME_EXIST 1
|
|
13
|
+char *fname[__Fname_sz];
|
|
14
|
+
|
|
15
|
+void teardown_ttail(void)
|
|
16
|
+{
|
|
17
|
+ ttail_free(ttail);
|
|
18
|
+}
|
|
19
|
+void setup_ttail_empty(void)
|
|
20
|
+{
|
|
21
|
+ ttail = ttail_init(1, (char**)&"foo");
|
|
22
|
+}
|
|
23
|
+START_TEST (test_init_empty_logfilename)
|
|
24
|
+{
|
|
25
|
+ ck_assert_msg(ttail->logfile_name == NULL,
|
|
26
|
+ "ttail_t.logfile_name should be NULL");
|
|
27
|
+}
|
|
28
|
+END_TEST
|
|
29
|
+
|
|
30
|
+START_TEST (test_init_nonexist_logfilename)
|
|
31
|
+{
|
|
32
|
+ char *fname;
|
|
33
|
+ int ret;
|
|
34
|
+ fname = tempnam(NULL, "ttail_check");
|
|
35
|
+
|
|
36
|
+ ret = ttail_add_logfile(ttail, fname);
|
|
37
|
+ ck_assert_int_eq(ret, 1);
|
|
38
|
+ ck_assert(ttail->logfile_name != NULL);
|
|
39
|
+ ck_assert_str_eq(ttail->logfile_name[0], fname);
|
|
40
|
+ ck_assert(ttail->logfile[0] == NULL);
|
|
41
|
+ free(fname);
|
|
42
|
+}
|
|
43
|
+END_TEST
|
|
44
|
+
|
|
45
|
+START_TEST (test_init_exist_logfilename)
|
|
46
|
+{
|
|
47
|
+ char *fname;
|
|
48
|
+ int ret;
|
|
49
|
+ FILE *fp;
|
|
50
|
+ fname = tempnam(NULL, "ttail_check");
|
|
51
|
+ if(!(fp = fopen(fname, "w+")))
|
|
52
|
+ {
|
|
53
|
+ perror("Unable to create file for testing");
|
|
54
|
+ ck_abort_msg("Unable to create file for testing");
|
|
55
|
+ }
|
|
56
|
+ if(fclose(fp))
|
|
57
|
+ {
|
|
58
|
+ perror("Unable to close file for testing");
|
|
59
|
+ ck_abort_msg("Unabe to close file for testing");
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ ret = ttail_add_logfile(ttail, fname);
|
|
63
|
+ ck_assert_int_eq(ret, 0);
|
|
64
|
+ ck_assert(ttail->logfile_name != NULL);
|
|
65
|
+ ck_assert_str_eq(ttail->logfile_name[0], fname);
|
|
66
|
+ ck_assert(ttail->logfile[0] != NULL);
|
|
67
|
+ free(fname);
|
|
68
|
+}
|
|
69
|
+END_TEST
|
|
70
|
+
|
|
71
|
+START_TEST (test_init_same_exist_logfilename)
|
|
72
|
+{
|
|
73
|
+ char *fname;
|
|
74
|
+ int ret;
|
|
75
|
+ FILE *fp;
|
|
76
|
+ fname = tempnam(NULL, "ttail_check");
|
|
77
|
+ if(!(fp = fopen(fname, "w+")))
|
|
78
|
+ {
|
|
79
|
+ perror("Unable to create file for testing");
|
|
80
|
+ ck_abort_msg("Unable to create file for testing");
|
|
81
|
+ }
|
|
82
|
+ if(fclose(fp))
|
|
83
|
+ {
|
|
84
|
+ perror("Unable to close file for testing");
|
|
85
|
+ ck_abort_msg("Unabe to close file for testing");
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ ret = ttail_add_logfile(ttail, fname);
|
|
89
|
+ ck_assert_int_eq(ret, 0);
|
|
90
|
+ ret = ttail_add_logfile(ttail, fname);
|
|
91
|
+ ck_assert_int_eq(ret, -1);
|
|
92
|
+ ck_assert_int_eq(ttail->logfile_sz, 1);
|
|
93
|
+ ck_assert(ttail->logfile_name != NULL);
|
|
94
|
+ ck_assert_str_eq(ttail->logfile_name[0], fname);
|
|
95
|
+ ck_assert(ttail->logfile[0] != NULL);
|
|
96
|
+ free(fname);
|
|
97
|
+}
|
|
98
|
+END_TEST
|
|
99
|
+
|
|
100
|
+START_TEST (test_init_same_nonexist_logfilename)
|
|
101
|
+{
|
|
102
|
+ char *fname;
|
|
103
|
+ int ret;
|
|
104
|
+ fname = tempnam(NULL, "ttail_check");
|
|
105
|
+
|
|
106
|
+ ret = ttail_add_logfile(ttail, fname);
|
|
107
|
+ ck_assert_int_eq(ret, 1);
|
|
108
|
+ ret = ttail_add_logfile(ttail, fname);
|
|
109
|
+ ck_assert_int_eq(ret, -1);
|
|
110
|
+ ck_assert_int_eq(ttail->logfile_sz, 1);
|
|
111
|
+ ck_assert(ttail->logfile_name != NULL);
|
|
112
|
+ ck_assert_str_eq(ttail->logfile_name[0], fname);
|
|
113
|
+ ck_assert(ttail->logfile[0] == NULL);
|
|
114
|
+ free(fname);
|
|
115
|
+}
|
|
116
|
+END_TEST
|
|
117
|
+
|
|
118
|
+Suite * ttail_init_suite(void)
|
|
119
|
+{
|
|
120
|
+ Suite *s;
|
|
121
|
+ TCase *tc_init_logfile;
|
|
122
|
+
|
|
123
|
+ s = suite_create("ttail init checks");
|
|
124
|
+
|
|
125
|
+ tc_init_logfile = tcase_create("logfile init checks");
|
|
126
|
+ tcase_add_checked_fixture(tc_init_logfile,
|
|
127
|
+ setup_ttail_empty, teardown_ttail);
|
|
128
|
+ tcase_add_test(tc_init_logfile, test_init_empty_logfilename);
|
|
129
|
+ tcase_add_test(tc_init_logfile, test_init_nonexist_logfilename);
|
|
130
|
+ tcase_add_test(tc_init_logfile, test_init_exist_logfilename);
|
|
131
|
+ tcase_add_test(tc_init_logfile, test_init_same_exist_logfilename);
|
|
132
|
+ tcase_add_test(tc_init_logfile, test_init_same_nonexist_logfilename);
|
|
133
|
+ suite_add_tcase(s, tc_init_logfile);
|
|
134
|
+ return s;
|
|
135
|
+}
|
|
136
|
+
|
|
137
|
+int main(void)
|
|
138
|
+{
|
|
139
|
+ int number_failed = 0;
|
|
140
|
+ SRunner *sr;
|
|
141
|
+
|
|
142
|
+ sr = srunner_create(ttail_init_suite());
|
|
143
|
+ srunner_set_fork_status(sr, CK_FORK);
|
|
144
|
+
|
|
145
|
+ srunner_run_all(sr,CK_VERBOSE);
|
|
146
|
+ number_failed = srunner_ntests_failed(sr);
|
|
147
|
+ srunner_free(sr);
|
|
148
|
+ return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
149
|
+
|
|
150
|
+}
|