|
@@ -1,8 +1,16 @@
|
1
|
1
|
#include <check.h>
|
|
2
|
+#include <errno.h>
|
|
3
|
+#include <stdio.h>
|
|
4
|
+#include <unistd.h>
|
2
|
5
|
|
3
|
|
-#include "../src/include/ttail.h"
|
|
6
|
+
|
|
7
|
+#include "ttail.h"
|
4
|
8
|
|
5
|
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];
|
6
|
14
|
|
7
|
15
|
void teardown_ttail(void)
|
8
|
16
|
{
|
|
@@ -56,11 +64,54 @@ END_TEST
|
56
|
64
|
* file argument parsing
|
57
|
65
|
*/
|
58
|
66
|
|
59
|
|
-START_TEST (test_init_file_non_exist)
|
|
67
|
+void setup_fname(void)
|
|
68
|
+{
|
|
69
|
+ int i;
|
|
70
|
+ FILE *fp;
|
|
71
|
+ for(i=0;i<__Fname_sz;i++)
|
|
72
|
+ {
|
|
73
|
+ fname[i] = NULL;
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ for(i=0; i<__Fname_sz; i++)
|
|
77
|
+ {
|
|
78
|
+ fname[i] = tempnam(NULL, "ttail_check");
|
|
79
|
+ if(i%2)
|
|
80
|
+ {
|
|
81
|
+ if((fp=fopen(fname[i], "w+")) == NULL)
|
|
82
|
+ {
|
|
83
|
+ perror("Unable to create file for testing");
|
|
84
|
+ ck_abort_msg("Unable to create file for testing");
|
|
85
|
+ } else { printf("Created : %s\n", fname[i]);}
|
|
86
|
+ if(fclose(fp))
|
|
87
|
+ {
|
|
88
|
+ perror("Unable to close file for testing");
|
|
89
|
+ ck_abort_msg("Unable to close file for testing");
|
|
90
|
+
|
|
91
|
+ }
|
|
92
|
+ }
|
|
93
|
+ }
|
|
94
|
+}
|
|
95
|
+
|
|
96
|
+void teardown_fname(void)
|
|
97
|
+{
|
|
98
|
+ int i;
|
|
99
|
+ for(i=0; i<__Fname_sz; i++)
|
|
100
|
+ {
|
|
101
|
+ unlink(fname[i]);
|
|
102
|
+ if(fname[i] != NULL)
|
|
103
|
+ {
|
|
104
|
+ free(fname[i]);
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+}
|
|
108
|
+
|
|
109
|
+START_TEST (test_argparse_file_non_exist)
|
60
|
110
|
{
|
61
|
111
|
ttail_t *t;
|
62
|
|
- char *args[3] = {"foo", "-f", "__non__existing__"};
|
|
112
|
+ char *args[3] = {"foo", "-f", NULL};
|
63
|
113
|
|
|
114
|
+ args[2] = fname[FNAME_NO_EXIST];
|
64
|
115
|
t = ttail_init(3, args);
|
65
|
116
|
|
66
|
117
|
ck_assert_msg(t->logfile_sz == 1,
|
|
@@ -75,15 +126,73 @@ START_TEST (test_init_file_non_exist)
|
75
|
126
|
}
|
76
|
127
|
END_TEST
|
77
|
128
|
|
|
129
|
+START_TEST (test_argparse_file_exist)
|
|
130
|
+{
|
|
131
|
+ ttail_t *t;
|
|
132
|
+ char *args[3] = {"foo", "-f", NULL};
|
|
133
|
+
|
|
134
|
+ args[2] = fname[FNAME_EXIST];
|
|
135
|
+
|
|
136
|
+ t = ttail_init(3, args);
|
|
137
|
+
|
|
138
|
+ ck_assert_msg(t->logfile_sz == 1,
|
|
139
|
+ "ttail_t.logfile_sz should be 1");
|
|
140
|
+ ck_assert_msg(strcmp(t->logfile_name[0], args[2]) == 0,
|
|
141
|
+ "ttail_t.logfile_name[0] does not contain the filename");
|
|
142
|
+ ck_assert_msg(t->logfile[0] != NULL,
|
|
143
|
+ "ttail_t.logfile[0] shouldn't be NULL");
|
|
144
|
+
|
|
145
|
+ ttail_free(t);
|
|
146
|
+
|
|
147
|
+}
|
|
148
|
+END_TEST
|
|
149
|
+
|
|
150
|
+START_TEST (test_argparse_file_multiple)
|
|
151
|
+{
|
|
152
|
+ printf("MULTIPLE\n");
|
|
153
|
+ ttail_t *t;
|
|
154
|
+ int i;
|
|
155
|
+ char *args[11] = {"foo", "-f", NULL, "-f", NULL,
|
|
156
|
+ "-f", NULL, "-f", NULL, "-f", NULL};
|
|
157
|
+
|
|
158
|
+ for(i=0; i<__Fname_sz; i++)
|
|
159
|
+ {
|
|
160
|
+ args[(i*2)+2] = fname[i];
|
|
161
|
+ }
|
|
162
|
+
|
|
163
|
+ t = ttail_init(11, args);
|
|
164
|
+
|
|
165
|
+ ck_assert_msg(t->logfile_sz == __Fname_sz,
|
|
166
|
+ "ttail_t.logfile_sz doesn't have the good value");
|
|
167
|
+
|
|
168
|
+ for(i=0;i<__Fname_sz; i++)
|
|
169
|
+ {
|
|
170
|
+ ck_assert_msg(strcmp(t->logfile_name[i], args[(i*2)+2]) == 0,
|
|
171
|
+ "some filename corrupted in ttail_t.logfile_name");
|
|
172
|
+ if(i%2)
|
|
173
|
+ {
|
|
174
|
+ ck_assert_msg(t->logfile[i] != NULL,
|
|
175
|
+ "logfile not opened");
|
|
176
|
+ } else {
|
|
177
|
+ ck_assert_msg(t->logfile[i] == NULL,
|
|
178
|
+ "logfile should be NULL");
|
|
179
|
+
|
|
180
|
+ }
|
|
181
|
+ }
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+}
|
|
185
|
+END_TEST
|
|
186
|
+
|
78
|
187
|
Suite * ttail_init_suite(void)
|
79
|
188
|
{
|
80
|
189
|
Suite *s;
|
81
|
190
|
TCase *tc_argparse_empty;
|
82
|
|
- TCase *tc_init_files;
|
|
191
|
+ TCase *tc_argparse_files;
|
83
|
192
|
|
84
|
|
- s = suite_create("ttail init methods tests");
|
|
193
|
+ s = suite_create("ttail argument parsing checks");
|
85
|
194
|
|
86
|
|
- tc_argparse_empty = tcase_create("Empty arguments parsing");
|
|
195
|
+ tc_argparse_empty = tcase_create("empty arguments parsing");
|
87
|
196
|
tcase_add_checked_fixture(tc_argparse_empty,
|
88
|
197
|
setup_ttail_empty, teardown_ttail);
|
89
|
198
|
tcase_add_test(tc_argparse_empty, test_argparse_empty_logfilename);
|
|
@@ -93,10 +202,14 @@ Suite * ttail_init_suite(void)
|
93
|
202
|
tcase_add_test(tc_argparse_empty, test_argparse_empty_fmt);
|
94
|
203
|
tcase_add_test(tc_argparse_empty, test_argparse_empty_verbose);
|
95
|
204
|
|
96
|
|
- tc_init_files = tcase_create("files options handling");
|
97
|
|
- tcase_add_test(tc_init_files, test_init_file_non_exist);
|
|
205
|
+ tc_argparse_files = tcase_create("files options parse");
|
|
206
|
+ tcase_add_checked_fixture(tc_argparse_files,
|
|
207
|
+ setup_fname, teardown_fname);
|
|
208
|
+ tcase_add_test(tc_argparse_files, test_argparse_file_non_exist);
|
|
209
|
+ tcase_add_test(tc_argparse_files, test_argparse_file_exist);
|
|
210
|
+ tcase_add_test(tc_argparse_files, test_argparse_file_multiple);
|
98
|
211
|
|
99
|
|
- suite_add_tcase(s, tc_init_files);
|
|
212
|
+ suite_add_tcase(s, tc_argparse_files);
|
100
|
213
|
suite_add_tcase(s, tc_argparse_empty);
|
101
|
214
|
return s;
|
102
|
215
|
}
|
|
@@ -107,6 +220,7 @@ int main(void)
|
107
|
220
|
SRunner *sr;
|
108
|
221
|
|
109
|
222
|
sr = srunner_create(ttail_init_suite());
|
|
223
|
+ srunner_set_fork_status(sr, CK_FORK);
|
110
|
224
|
|
111
|
225
|
srunner_run_all(sr,CK_VERBOSE);
|
112
|
226
|
number_failed = srunner_ntests_failed(sr);
|