summaryrefslogtreecommitdiff
path: root/src/tests/eio
diff options
context:
space:
mode:
authorplamot <pierre.lamot@openwide.fr>2015-02-23 14:44:48 +0100
committerCedric BAIL <cedric@osg.samsung.com>2015-03-12 07:43:58 +0100
commit19200a878ad53a37b6d6533037553fe9e6945473 (patch)
tree68150dde15eab2e2a26a5bd36d42f73849daec04 /src/tests/eio
parentf6d870b4c13a1cf44bf35f1e66126ff7054d5957 (diff)
eio: add unit tests for eio file and directory monitoring.
this patch adds units test to check the behavior of eio file and directory monitoring module Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
Diffstat (limited to 'src/tests/eio')
-rw-r--r--src/tests/eio/eio_suite.c107
-rw-r--r--src/tests/eio/eio_suite.h8
-rw-r--r--src/tests/eio/eio_test_monitor.c452
3 files changed, 567 insertions, 0 deletions
diff --git a/src/tests/eio/eio_suite.c b/src/tests/eio/eio_suite.c
new file mode 100644
index 0000000000..08deb05803
--- /dev/null
+++ b/src/tests/eio/eio_suite.c
@@ -0,0 +1,107 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <stdlib.h>
6#include <stdio.h>
7
8#include <Eio.h>
9
10#include "eio_suite.h"
11
12typedef struct _Eio_Test_Case Eio_Test_Case;
13
14struct _Eio_Test_Case
15{
16 const char *test_case;
17 void (*build)(TCase *tc);
18};
19
20static const Eio_Test_Case etc[] = {
21 {"Eio_Monitor", eio_test_monitor},
22 {NULL, NULL}
23};
24
25static void
26_list_tests(void)
27{
28 const Eio_Test_Case *itr;
29
30 itr = etc;
31 fputs("Available Test Cases:\n", stderr);
32 for (; itr->test_case; itr++)
33 fprintf(stderr, "\t%s\n", itr->test_case);
34}
35
36static Eina_Bool
37_use_test(int argc, const char **argv, const char *test_case)
38{
39 if (argc < 1)
40 return 1;
41
42 for (; argc > 0; argc--, argv++)
43 if (strcmp(test_case, *argv) == 0)
44 return 1;
45 return 0;
46}
47
48static Suite *
49eio_suite_build(int argc, const char **argv)
50{
51 TCase *tc;
52 Suite *s;
53 int i;
54
55 s = suite_create("Eio");
56
57 for (i = 0; etc[i].test_case; ++i)
58 {
59 if (!_use_test(argc, argv, etc[i].test_case)) continue;
60 tc = tcase_create(etc[i].test_case);
61
62 etc[i].build(tc);
63
64 suite_add_tcase(s, tc);
65 tcase_set_timeout(tc, 0);
66 }
67
68 return s;
69}
70
71
72int
73main(int argc, char **argv)
74{
75 Suite *s;
76 SRunner *sr;
77 int i, failed_count;
78
79 for (i = 1; i < argc; i++)
80 if ((strcmp(argv[i], "-h") == 0) ||
81 (strcmp(argv[i], "--help") == 0))
82 {
83 fprintf(stderr, "Usage:\n\t%s [test_case1 .. [test_caseN]]\n",
84 argv[0]);
85 _list_tests();
86 return 0;
87 }
88 else if ((strcmp(argv[i], "-l") == 0) ||
89 (strcmp(argv[i], "--list") == 0))
90 {
91 _list_tests();
92 return 0;
93 }
94
95 putenv("EFL_RUN_IN_TREE=1");
96
97 s = eio_suite_build(argc - 1, (const char **)argv + 1);
98 sr = srunner_create(s);
99
100 srunner_set_xml(sr, TESTS_BUILD_DIR "/check-results.xml");
101
102 srunner_run_all(sr, CK_ENV);
103 failed_count = srunner_ntests_failed(sr);
104 srunner_free(sr);
105
106 return (failed_count == 0) ? 0 : 255;
107}
diff --git a/src/tests/eio/eio_suite.h b/src/tests/eio/eio_suite.h
new file mode 100644
index 0000000000..2212f8eafb
--- /dev/null
+++ b/src/tests/eio/eio_suite.h
@@ -0,0 +1,8 @@
1#ifndef _EIO_SUITE_H
2#define _EIO_SUITE_H
3
4#include <check.h>
5
6void eio_test_monitor(TCase *tc);
7
8#endif /* _EIO_SUITE_H */
diff --git a/src/tests/eio/eio_test_monitor.c b/src/tests/eio/eio_test_monitor.c
new file mode 100644
index 0000000000..c1f10a45b1
--- /dev/null
+++ b/src/tests/eio/eio_test_monitor.c
@@ -0,0 +1,452 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <stdio.h>
6#include <unistd.h>
7#include <string.h>
8
9#include <Eio.h>
10#include <Ecore.h>
11#include <Ecore_File.h>
12
13#include "eio_suite.h"
14
15/////////////////timeout function
16
17#define TEST_TIMEOUT_SEC 10
18#define TEST_OPERATION_DELAY 0.5
19
20static Ecore_Timer * test_timeout_timer;
21
22static Eina_Bool _test_timeout_cb(void* data EINA_UNUSED)
23{
24 ck_abort_msg("test timeout");
25 ecore_main_loop_quit();
26 return ECORE_CALLBACK_CANCEL;
27}
28
29static void _cancel_timeout()
30{
31 if (test_timeout_timer != NULL)
32 {
33 ecore_timer_del (test_timeout_timer);
34 test_timeout_timer = NULL;
35 }
36}
37
38///////////////// file and directory operations
39
40typedef struct {
41 const char* src;
42 const char* dst;
43} RenameOperation;
44
45static Eina_Bool _delete_directory(void* data)
46{
47 const char* dirname = (const char*)data;
48 if (ecore_file_is_dir(dirname))
49 {
50 ecore_file_recursive_rm(dirname);
51 }
52 return ECORE_CALLBACK_CANCEL;
53}
54
55static Eina_Bool _create_directory(void* data)
56{
57 const char* dirname = (const char*)data;
58 ecore_file_mkpath(dirname);
59 return ECORE_CALLBACK_CANCEL;
60}
61
62
63static Eina_Bool _create_file(void* data)
64{
65 FILE* fd = fopen((const char*)data, "w+");
66 ck_assert_ptr_ne(fd, NULL);
67 fprintf(fd, "test test");
68 fclose(fd);
69 return ECORE_CALLBACK_CANCEL;
70}
71
72static Eina_Bool _delete_file(void* data)
73{
74 Eina_Bool file_removed = ecore_file_remove((const char*)data);
75 ck_assert(file_removed);
76 return ECORE_CALLBACK_CANCEL;
77}
78
79static Eina_Bool _modify_file(void* data)
80{
81 FILE* fd = fopen((const char*)data, "a");
82 ck_assert_ptr_ne(fd, NULL);
83 fprintf(fd, "appened");
84 fclose(fd);
85 return ECORE_CALLBACK_CANCEL;
86}
87
88static Eina_Bool _modify_attrib_file(void* data)
89{
90 int ret = chmod((const char*)data, 0666);
91 ck_assert_int_eq(ret, 0);
92 return ECORE_CALLBACK_CANCEL;
93}
94
95/////// helper functions
96
97static void _check_event_path(void* data, void* event)
98{
99 const char* expected_path = ecore_file_realpath((const char*)data);
100 const char* actual_path = ecore_file_realpath(((Eio_Monitor_Event*)event)->filename);
101 ck_assert_str_eq(actual_path, expected_path);
102}
103
104static Eina_Tmpstr* _common_init()
105{
106 Eina_Tmpstr* dirname;
107 fail_if(eio_init() != 1);
108 ecore_file_init();
109
110 //test timeout
111 test_timeout_timer = ecore_timer_add(TEST_TIMEOUT_SEC, _test_timeout_cb, NULL);
112
113 eina_file_mkdtemp("checkFileCreationXXXXXX", &dirname);
114 return dirname;
115}
116
117static void _common_shutdown(Eina_Tmpstr* dirname)
118{
119 _delete_directory((void*)dirname);
120 ecore_file_shutdown();
121 fail_if(eio_shutdown() != 0);
122 eina_tmpstr_del(dirname);
123}
124
125/////// tests monitoring a directory
126
127static void _file_created_cb(void* data, int type, void* event)
128{
129 ck_assert_int_eq(type, (int)EIO_MONITOR_FILE_CREATED);
130 _check_event_path(data, event);
131 _cancel_timeout();
132 ecore_main_loop_quit();
133}
134
135
136START_TEST(eio_test_monitor_directory_file_created_notify)
137{
138 Eina_Tmpstr* dirname = _common_init();
139 Eina_Stringshare* filename;
140
141 filename = eina_stringshare_printf("%s/filecreated", dirname);
142
143 //monitor directory
144 eio_monitor_add(dirname);
145 ecore_event_handler_add(EIO_MONITOR_FILE_CREATED, (Ecore_Event_Handler_Cb)_file_created_cb, filename);
146
147 ecore_timer_add(TEST_OPERATION_DELAY, _create_file, filename);
148
149 ecore_main_loop_begin();
150
151 _common_shutdown(dirname);
152}
153END_TEST
154
155static void _file_deleted_cb(void* data, int type, void* event)
156{
157 ck_assert_int_eq(type, (int)EIO_MONITOR_FILE_DELETED);
158 _check_event_path(data, event);
159 _cancel_timeout();
160 ecore_main_loop_quit();
161}
162
163START_TEST(eio_test_monitor_directory_file_deleted_notify)
164{
165 Eina_Tmpstr* dirname = _common_init();
166 Eina_Stringshare* filename;
167
168 filename = eina_stringshare_printf("%s/filecreated", dirname);
169 _create_file((void*)filename);
170
171 //monitor directory
172 eio_monitor_add(dirname);
173 ecore_event_handler_add(EIO_MONITOR_FILE_DELETED, (Ecore_Event_Handler_Cb)_file_deleted_cb, filename);
174
175 ecore_timer_add(TEST_OPERATION_DELAY, _delete_file, filename);
176
177 ecore_main_loop_begin();
178
179 _common_shutdown(dirname);
180}
181END_TEST
182
183static void _file_modified_cb(void* data, int type, void* event)
184{
185 ck_assert_int_eq(type, (int)EIO_MONITOR_FILE_MODIFIED);
186 _check_event_path(data, event);
187 _cancel_timeout();
188 ecore_main_loop_quit();
189}
190
191START_TEST(eio_test_monitor_directory_file_modified_notify)
192{
193 Eina_Tmpstr* dirname = _common_init();
194 Eina_Stringshare* filename;
195
196 filename = eina_stringshare_printf("%s/filecreated", dirname);
197 _create_file((void*)filename);
198
199 //monitor directory
200 eio_monitor_add(dirname);
201 ecore_event_handler_add(EIO_MONITOR_FILE_MODIFIED, (Ecore_Event_Handler_Cb)_file_modified_cb, filename);
202
203 ecore_timer_add(TEST_OPERATION_DELAY, _modify_file, filename);
204
205 ecore_main_loop_begin();
206
207 //cleanup
208 _common_shutdown(dirname);
209}
210END_TEST
211
212static void _file_closed_cb(void* data, int type, void* event)
213{
214 ck_assert_int_eq(type, (int)EIO_MONITOR_FILE_CLOSED);
215 _check_event_path(data, event);
216 _cancel_timeout();
217 ecore_main_loop_quit();
218}
219
220START_TEST(eio_test_monitor_directory_file_closed_notify)
221{
222 Eina_Tmpstr* dirname = _common_init();
223 Eina_Stringshare* filename;
224
225 filename = eina_stringshare_printf("%s/filecreated", dirname);
226 _create_file((void*)filename);
227
228 //monitor directory
229 eio_monitor_add(dirname);
230 ecore_event_handler_add(EIO_MONITOR_FILE_CLOSED, (Ecore_Event_Handler_Cb)_file_closed_cb, filename);
231 ecore_timer_add(TEST_OPERATION_DELAY, _modify_file, filename);
232
233 ecore_main_loop_begin();
234
235 _common_shutdown(dirname);
236}
237END_TEST
238
239static void _directory_created_cb(void* data, int type, void* event)
240{
241 ck_assert_int_eq(type, (int)EIO_MONITOR_DIRECTORY_CREATED);
242 _check_event_path(data, event);
243 _cancel_timeout();
244 ecore_main_loop_quit();
245}
246
247START_TEST(eio_test_monitor_directory_directory_created_notify)
248{
249 Eina_Tmpstr* dirname = _common_init();
250 Eina_Stringshare* filename;
251
252 filename = eina_stringshare_printf("%s/dircreated", dirname);
253
254 //monitor directory
255 eio_monitor_add(dirname);
256 ecore_event_handler_add(EIO_MONITOR_DIRECTORY_CREATED, (Ecore_Event_Handler_Cb)_directory_created_cb, filename);
257
258 ecore_timer_add(TEST_OPERATION_DELAY, _create_directory, filename);
259
260 ecore_main_loop_begin();
261
262 _common_shutdown(dirname);
263}
264END_TEST
265
266static void _directory_deleted_cb(void* data, int type, void* event)
267{
268 ck_assert_int_eq(type, (int)EIO_MONITOR_DIRECTORY_DELETED);
269 _check_event_path(data, event);
270 _cancel_timeout();
271 ecore_main_loop_quit();
272}
273
274START_TEST(eio_test_monitor_directory_directory_deleted_notify)
275{
276 Eina_Tmpstr* dirname = _common_init();
277 Eina_Stringshare* filename;
278
279 filename = eina_stringshare_printf("%s/dircreated", dirname);
280 _create_directory((void*)filename);
281
282 //monitor directory
283 eio_monitor_add(dirname);
284 ecore_event_handler_add(EIO_MONITOR_DIRECTORY_DELETED, (Ecore_Event_Handler_Cb)_directory_deleted_cb, filename);
285
286 ecore_timer_add(TEST_OPERATION_DELAY, _delete_directory, filename);
287
288 ecore_main_loop_begin();
289
290 _common_shutdown(dirname);
291}
292END_TEST
293
294static void _directory_modified_cb(void* data, int type, void* event)
295{
296 ck_assert_int_eq(type, (int)EIO_MONITOR_DIRECTORY_MODIFIED);
297 _check_event_path(data, event);
298 _cancel_timeout();
299 ecore_main_loop_quit();
300}
301
302
303START_TEST(eio_test_monitor_directory_directory_modified_notify)
304{
305 Eina_Tmpstr* dirname = _common_init();
306 Eina_Stringshare* filename;
307
308 filename = eina_stringshare_printf("%s/filecreated", dirname);
309 _create_directory((void*)filename);
310
311 //monitor directory
312 eio_monitor_add(dirname);
313 ecore_event_handler_add(EIO_MONITOR_DIRECTORY_MODIFIED, (Ecore_Event_Handler_Cb)_directory_modified_cb, filename);
314
315 ecore_timer_add(TEST_OPERATION_DELAY, _modify_attrib_file, filename);
316
317 ecore_main_loop_begin();
318
319 _common_shutdown(dirname);
320}
321END_TEST
322
323
324static void _directory_self_deleted_cb(void* data, int type, void* event)
325{
326 ck_assert_int_eq(type, (int)EIO_MONITOR_SELF_DELETED);
327 _check_event_path(data, event);
328 _cancel_timeout();
329 ecore_main_loop_quit();
330}
331
332
333START_TEST(eio_test_monitor_directory_directory_self_deleted_notify)
334{
335 Eina_Tmpstr* dirname = _common_init();
336
337 //monitor directory
338 eio_monitor_add(dirname);
339 ecore_event_handler_add(EIO_MONITOR_SELF_DELETED, (Ecore_Event_Handler_Cb)_directory_self_deleted_cb, dirname);
340
341 ecore_timer_add(TEST_OPERATION_DELAY, _delete_directory, dirname);
342
343 ecore_main_loop_begin();
344
345 _common_shutdown(dirname);
346}
347END_TEST
348
349//////// test monitoring a single file
350
351START_TEST(eio_test_monitor_file_file_modified_notify)
352{
353 Eina_Tmpstr* dirname = _common_init();
354 Eina_Stringshare* filename;
355
356 filename = eina_stringshare_printf("%s/filecreated", dirname);
357 _create_file((void*)filename);
358
359 //monitor file
360 eio_monitor_add(filename);
361 ecore_event_handler_add(EIO_MONITOR_FILE_MODIFIED, (Ecore_Event_Handler_Cb)_file_modified_cb, filename);
362
363 ecore_timer_add(TEST_OPERATION_DELAY, _modify_file, filename);
364
365 ecore_main_loop_begin();
366
367 //cleanup
368 _common_shutdown(dirname);
369}
370END_TEST
371
372START_TEST(eio_test_monitor_file_file_attrib_modified_notify)
373{
374 Eina_Tmpstr* dirname = _common_init();
375 Eina_Stringshare* filename;
376
377 filename = eina_stringshare_printf("%s/filecreated", dirname);
378 _create_file((void*)filename);
379
380 //monitor file
381 eio_monitor_add(filename);
382 ecore_event_handler_add(EIO_MONITOR_FILE_MODIFIED, (Ecore_Event_Handler_Cb)_file_modified_cb, filename);
383
384 ecore_timer_add(TEST_OPERATION_DELAY, _modify_attrib_file, filename);
385
386 ecore_main_loop_begin();
387
388 _common_shutdown(dirname);
389}
390END_TEST
391
392
393START_TEST(eio_test_monitor_file_file_closed_notify)
394{
395 Eina_Tmpstr* dirname = _common_init();
396 Eina_Stringshare* filename;
397
398 filename = eina_stringshare_printf("%s/filecreated", dirname);
399 _create_file((void*)filename);
400
401 //monitor file
402 eio_monitor_add(dirname);
403 ecore_event_handler_add(EIO_MONITOR_FILE_CLOSED, (Ecore_Event_Handler_Cb)_file_closed_cb, filename);
404 ecore_timer_add(TEST_OPERATION_DELAY, _modify_file, filename);
405
406 ecore_main_loop_begin();
407
408 _common_shutdown(dirname);
409}
410END_TEST
411
412START_TEST(eio_test_monitor_file_file_self_deleted_notify)
413{
414 Eina_Tmpstr* dirname = _common_init();
415 Eina_Stringshare* filename;
416
417 filename = eina_stringshare_printf("%s/filecreated", dirname);
418 _create_file((void*)filename);
419
420 //monitor file
421 eio_monitor_add(filename);
422 ecore_event_handler_add(EIO_MONITOR_SELF_DELETED, (Ecore_Event_Handler_Cb)_directory_self_deleted_cb, filename);
423
424 ecore_timer_add(TEST_OPERATION_DELAY, _delete_file, filename);
425
426 ecore_main_loop_begin();
427
428 _common_shutdown(dirname);
429}
430END_TEST
431
432void eio_test_monitor(TCase *tc)
433{
434 tcase_add_test(tc, eio_test_monitor_directory_file_created_notify);
435 tcase_add_test(tc, eio_test_monitor_directory_file_deleted_notify);
436 tcase_add_test(tc, eio_test_monitor_directory_file_modified_notify);
437#ifndef _WIN32
438 tcase_add_test(tc, eio_test_monitor_directory_file_closed_notify);
439#endif
440 tcase_add_test(tc, eio_test_monitor_directory_directory_created_notify);
441 tcase_add_test(tc, eio_test_monitor_directory_directory_deleted_notify);
442 tcase_add_test(tc, eio_test_monitor_directory_directory_modified_notify);
443 tcase_add_test(tc, eio_test_monitor_directory_directory_self_deleted_notify);
444
445 tcase_add_test(tc, eio_test_monitor_file_file_modified_notify);
446 tcase_add_test(tc, eio_test_monitor_file_file_attrib_modified_notify);
447#ifndef _WIN32
448 tcase_add_test(tc, eio_test_monitor_file_file_closed_notify);
449#endif
450 tcase_add_test(tc, eio_test_monitor_file_file_self_deleted_notify);
451
452}