This source file includes following definitions.
- mock_opendir
- mock_readdir
- setup
- teardown
- count_entries
- START_TEST
- START_TEST
- START_TEST
- START_TEST
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 #define TEST_SUITE_NAME "/src/vfs/local"
27
28 #include "tests/mctest.h"
29
30 #include <dirent.h>
31 #include <unistd.h>
32
33 #include "lib/strutil.h"
34
35
36
37
38
39 static DIR *mock_opendir (const char *name);
40 static struct dirent *mock_readdir (DIR *dirp);
41
42
43 #define opendir(name) mock_opendir (name)
44 #define readdir(dirp) mock_readdir (dirp)
45 #include "src/vfs/local/local.c"
46 #undef opendir
47 #undef readdir
48
49
50 static int mock_opendir__eintr_count = 0;
51
52
53 static DIR *
54 mock_opendir (const char *name)
55 {
56 if (mock_opendir__eintr_count > 0)
57 {
58 mock_opendir__eintr_count--;
59 errno = EINTR;
60 return NULL;
61 }
62
63 return opendir (name);
64 }
65
66
67 static int mock_readdir__eintr_count = 0;
68
69
70 static gboolean mock_readdir__eof = FALSE;
71
72
73 static int mock_readdir__fail_after = -1;
74
75
76 static struct dirent *
77 mock_readdir (DIR *dirp)
78 {
79 if (mock_readdir__eintr_count > 0)
80 {
81 mock_readdir__eintr_count--;
82 errno = EINTR;
83 return NULL;
84 }
85
86 if (mock_readdir__eof)
87 return NULL;
88
89 if (mock_readdir__fail_after < 0)
90 return readdir (dirp);
91
92
93 for (; mock_readdir__fail_after > 0; mock_readdir__fail_after--)
94 (void) readdir (dirp);
95 mock_readdir__fail_after = -1;
96
97 errno = EIO;
98 return NULL;
99 }
100
101
102
103
104 static void
105 setup (void)
106 {
107 str_init_strings (NULL);
108
109 vfs_init ();
110 vfs_init_localfs ();
111 }
112
113
114
115
116 static void
117 teardown (void)
118 {
119 vfs_shut ();
120 str_uninit_strings ();
121 }
122
123
124
125 static int
126 count_entries (const vfs_path_t *vpath)
127 {
128 void *info;
129 struct vfs_dirent *entry;
130 int count = 0;
131
132 info = local_opendir (vpath);
133 mctest_assert_not_null (info);
134
135 while ((entry = local_readdir (info)) != NULL)
136 {
137 vfs_dirent_free (entry);
138 count++;
139 }
140
141 local_closedir (info);
142
143 return count;
144 }
145
146
147
148
149 START_TEST (test_local_opendir_stale_eintr)
150 {
151 char *dir;
152 vfs_path_t *vpath;
153 void *info;
154
155
156
157 dir = g_dir_make_tmp ("mctest-XXXXXX", NULL);
158 mctest_assert_not_null (dir);
159 vpath = vfs_path_from_str (dir);
160 mock_readdir__eof = TRUE;
161 errno = EINTR;
162
163
164 info = local_opendir (vpath);
165
166
167 mctest_assert_not_null (info);
168
169
170 local_closedir (info);
171 mock_readdir__eof = FALSE;
172 vfs_path_free (vpath, TRUE);
173 rmdir (dir);
174 g_free (dir);
175 }
176 END_TEST
177
178
179
180
181 START_TEST (test_local_opendir_readdir_error)
182 {
183 char *dir;
184 vfs_path_t *vpath;
185 int expected;
186 int actual;
187
188
189
190 dir = g_dir_make_tmp ("mctest-XXXXXX", NULL);
191 mctest_assert_not_null (dir);
192 vpath = vfs_path_from_str (dir);
193 expected = count_entries (vpath);
194 ck_assert_int_gt (expected, 0);
195 mock_readdir__fail_after = 1;
196
197
198 actual = count_entries (vpath);
199
200
201 ck_assert_int_eq (actual, expected);
202
203
204 vfs_path_free (vpath, TRUE);
205 rmdir (dir);
206 g_free (dir);
207 }
208 END_TEST
209
210
211
212
213 START_TEST (test_local_opendir_opendir_eintr)
214 {
215 char *dir;
216 vfs_path_t *vpath;
217 void *info;
218
219
220 dir = g_dir_make_tmp ("mctest-XXXXXX", NULL);
221 mctest_assert_not_null (dir);
222 vpath = vfs_path_from_str (dir);
223 mock_opendir__eintr_count = 1;
224
225
226 info = local_opendir (vpath);
227
228
229 mctest_assert_not_null (info);
230
231
232 local_closedir (info);
233 vfs_path_free (vpath, TRUE);
234 rmdir (dir);
235 g_free (dir);
236 }
237 END_TEST
238
239
240
241
242 START_TEST (test_local_readdir_eintr)
243 {
244 char *dir;
245 vfs_path_t *vpath;
246 void *info;
247 struct vfs_dirent *entry;
248 int expected;
249 int actual;
250
251
252
253 dir = g_dir_make_tmp ("mctest-XXXXXX", NULL);
254 mctest_assert_not_null (dir);
255 vpath = vfs_path_from_str (dir);
256 expected = count_entries (vpath);
257 ck_assert_int_gt (expected, 1);
258 info = local_opendir (vpath);
259 mctest_assert_not_null (info);
260 entry = local_readdir (info);
261 mctest_assert_not_null (entry);
262 vfs_dirent_free (entry);
263 mock_readdir__eintr_count = 1;
264
265
266 for (actual = 1; (entry = local_readdir (info)) != NULL; actual++)
267 vfs_dirent_free (entry);
268
269
270 ck_assert_int_eq (actual, expected);
271
272
273 local_closedir (info);
274 vfs_path_free (vpath, TRUE);
275 rmdir (dir);
276 g_free (dir);
277 }
278 END_TEST
279
280
281
282 int
283 main (void)
284 {
285 TCase *tc_core;
286
287 tc_core = tcase_create ("Core");
288
289 tcase_add_checked_fixture (tc_core, setup, teardown);
290
291
292 tcase_add_test (tc_core, test_local_opendir_stale_eintr);
293 tcase_add_test (tc_core, test_local_opendir_readdir_error);
294 tcase_add_test (tc_core, test_local_opendir_opendir_eintr);
295 tcase_add_test (tc_core, test_local_readdir_eintr);
296
297
298 return mctest_run_all (tc_core);
299 }
300
301