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 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <libgen.h>
11 
12 #ifdef _WIN32
13 # include <evil_private.h> /* setenv */
14 #endif
15 
16 #include <Eina.h>
17 #include <Ecore.h>
18 #include <Ecore_File.h>
19 
20 #include "ecore_suite.h"
21 
22 #ifndef O_BINARY
23 # define O_BINARY 0
24 #endif
25 
26 #define MAXSIZE 256
27 
28 void
_writeToFile(const char * filePath,char * text)29 _writeToFile(const char *filePath, char *text)
30 {
31    FILE *f = fopen(filePath, "rb+");
32    if (f == NULL)
33      f = fopen(filePath, "wb");
34    fail_if(f == NULL);
35    fprintf(f, "%s\n", text);
36    fclose(f);
37 }
38 
39 static char *
get_tmp_dir(void)40 get_tmp_dir(void)
41 {
42    Eina_Tmpstr *tmp_dir;
43    char *realpath;
44 
45    Eina_Bool created = eina_file_mkdtemp("EcoreFileTestXXXXXX", &tmp_dir);
46    if (!created) return NULL;
47 
48    realpath = ecore_file_realpath(tmp_dir);
49    eina_tmpstr_del(tmp_dir);
50    return realpath;
51 }
52 
53 static char *
get_tmp_file(void)54 get_tmp_file(void)
55 {
56    Eina_Tmpstr *tmp_file;
57    char *realpath;
58 
59    int fd = eina_file_mkstemp("EcoreFileTestXXXXXX", &tmp_file);
60    if (fd < 0) return NULL;
61    close(fd);
62 
63    realpath = ecore_file_realpath(tmp_file);
64    eina_tmpstr_del(tmp_file);
65    return realpath;
66 }
67 
68 static void
file_monitor_cb(void * data EINA_UNUSED,Ecore_File_Monitor * em EINA_UNUSED,Ecore_File_Event event,const char * path)69 file_monitor_cb(void *data EINA_UNUSED, Ecore_File_Monitor *em EINA_UNUSED,
70                 Ecore_File_Event event, const char *path)
71 {
72    switch (event)
73      {
74         case ECORE_FILE_EVENT_NONE:
75         case ECORE_FILE_EVENT_CREATED_FILE:
76           fprintf(stderr, "File created in %s", path);
77           break;
78         case ECORE_FILE_EVENT_DELETED_FILE:
79           fprintf(stderr, "File deleted in %s", path);
80           break;
81         case ECORE_FILE_EVENT_MODIFIED:
82           fprintf(stderr, "File modified in %s", path);
83           break;
84         case ECORE_FILE_EVENT_CLOSED:
85           fprintf(stderr, "File closed in %s", path);
86           break;
87         case ECORE_FILE_EVENT_DELETED_DIRECTORY:
88           fprintf(stderr, "Directory deleted in %s", path);
89           break;
90         case ECORE_FILE_EVENT_CREATED_DIRECTORY:
91           fprintf(stderr, "Directory created in %s", path);
92           break;
93         case ECORE_FILE_EVENT_DELETED_SELF:
94           fprintf(stderr, "Path %s deleted", path);
95           break;
96      }
97 }
98 
99 void
completion_cb(void * data EINA_UNUSED,const char * file EINA_UNUSED,int status)100 completion_cb(void *data EINA_UNUSED, const char *file EINA_UNUSED, int status)
101 {
102    fprintf(stderr, "Done (status: %d)\n", status);
103    ecore_main_loop_quit();
104 }
105 
106 void
err_completion_cb(void * data,const char * file EINA_UNUSED,int status)107 err_completion_cb(void *data, const char *file EINA_UNUSED, int status)
108 {
109    if (data)
110      *((int*) data) = status;
111    fail_if(status != 1);
112    // NOP if called from outside main loop. Keep it here if abort fails and
113    // we get called from there.
114    ecore_main_loop_quit();
115 }
116 
117 int
progress_cb(void * data EINA_UNUSED,const char * file EINA_UNUSED,long int dltotal,long int dlnow,long int ultotal EINA_UNUSED,long int ulnow EINA_UNUSED)118 progress_cb(void *data EINA_UNUSED, const char *file EINA_UNUSED,
119             long int dltotal, long int dlnow,
120             long int ultotal EINA_UNUSED, long int ulnow EINA_UNUSED)
121 {
122    fprintf(stderr, "Progress: %ld/%ld\n", dlnow, dltotal);
123    return ECORE_FILE_PROGRESS_CONTINUE;
124 }
125 
EFL_START_TEST(ecore_test_ecore_file_init)126 EFL_START_TEST(ecore_test_ecore_file_init)
127 {
128    int ret;
129 
130    ret = ecore_file_init();
131    fail_if(ret != 1);
132 
133    ret = ecore_file_shutdown();
134    fail_if(ret != 0);
135 }
136 EFL_END_TEST
137 
EFL_START_TEST(ecore_test_ecore_file_operations)138 EFL_START_TEST(ecore_test_ecore_file_operations)
139 {
140    const char* dirs[] = {"b", "b/c", "b/c/d", "d", 0};
141    char *dirs2[] = {"a2", "b2", "c2", 0};
142    const char *src_dir, *src_file, *dest_file;
143    const char *not_exist_file;
144    const char *tmpdir = NULL;
145    char *random_text = "This is random test String";
146    char *escaped_text = "This\\ is\\ random\\ test\\ String";
147    char *exe_cmd = "test.sh --opt1=a --opt2=b";
148    char *exe = "test.sh";
149    char dir[MAXSIZE] = {'\0'};
150    unsigned int ret;
151    int fd;
152    int i;
153    Eina_Bool res;
154    Eina_List *list;
155 
156    tmpdir = ecore_file_realpath(eina_environment_tmp_get());
157 
158    ret = ecore_file_init();
159    fail_if(ret != 1);
160 
161    src_dir = get_tmp_dir();
162    fail_if(!src_dir);
163 
164    src_file = get_tmp_file();
165    fail_if(!src_file);
166 
167    res = ecore_file_exists(src_file);
168    fail_if(res != EINA_TRUE);
169    res = ecore_file_is_dir(src_file);
170    fail_if(res != EINA_FALSE);
171    res = ecore_file_remove(src_file);
172    fail_if(res != EINA_TRUE);
173    res = ecore_file_exists(src_file);
174    fail_if(res != EINA_FALSE);
175 
176    res = ecore_file_is_dir(src_dir);
177    fail_if(res != EINA_TRUE);
178    ret = ecore_file_dir_is_empty(src_dir);
179    fail_if(ret != 1);
180    res = ecore_file_mkdir(src_dir);
181    fail_if(res != EINA_FALSE);
182    res = ecore_file_remove(src_dir);
183    fail_if(res != EINA_TRUE);
184    res = ecore_file_is_dir(src_dir);
185    fail_if(res != EINA_FALSE);
186 
187    src_dir = get_tmp_dir();
188    fail_if(!src_dir);
189    ret = ecore_file_mksubdirs(src_dir, dirs);
190    fail_if(ret != 4);
191    list = ecore_file_ls(src_dir);
192    fail_if(!list);
193    fail_if(eina_list_count(list) != 2);
194    res = ecore_file_recursive_rm(src_dir);
195    fail_if(res != EINA_TRUE);
196 
197    src_dir = get_tmp_dir();
198    fail_if(!src_dir);
199    snprintf(dir, sizeof(dir), "%s/%s", src_dir, dirs[2]);
200    res = ecore_file_mkpath(dir);
201    fail_if(res != EINA_TRUE);
202    res = ecore_file_recursive_rm(src_dir);
203    fail_if(res != EINA_TRUE);
204 
205    src_file = get_tmp_file();
206    fail_if(!src_file);
207    _writeToFile(src_file, random_text);
208 
209    ret = ecore_file_size(src_file);
210    fail_if(ret != strlen(random_text)+1);
211    ret = ecore_file_is_dir(src_file);
212    fail_if(ret != EINA_FALSE);
213 
214    dest_file = get_tmp_file();
215    fail_if(!dest_file);
216    res = ecore_file_cp(src_file, dest_file);
217    fail_if(res != EINA_TRUE);
218    res = ecore_file_exists(dest_file);
219    fail_if(res != EINA_TRUE);
220    fail_if(ecore_file_size(src_file) != ecore_file_size(dest_file));
221    res = ecore_file_remove(dest_file);
222    fail_if(res != EINA_TRUE);
223 
224    /* On Windows, symlink/readlink are not supported */
225 #ifndef _WIN32
226    res = ecore_file_symlink(src_file, dest_file);
227    fail_if(res != EINA_TRUE);
228    ck_assert_str_eq(ecore_file_readlink(dest_file), src_file);
229    ck_assert_str_eq(ecore_file_realpath(dest_file), src_file);
230    res = ecore_file_unlink(dest_file);
231    fail_if(res != EINA_TRUE);
232 #endif
233 
234    dest_file = get_tmp_file();
235    fail_if(!dest_file);
236    res = ecore_file_mv(src_file, dest_file);
237    fail_if(res != EINA_TRUE);
238    res = ecore_file_exists(dest_file);
239    fail_if(res != EINA_TRUE);
240    res = ecore_file_exists(src_file);
241    fail_if(res != EINA_FALSE);
242 
243    ck_assert_str_eq(ecore_file_dir_get(dest_file), tmpdir);
244    ck_assert_str_eq(ecore_file_realpath(dest_file), dest_file);
245    fail_if(ecore_file_mod_time(dest_file) == 0);
246    fail_if(ecore_file_can_read(dest_file) != EINA_TRUE);
247    fail_if(ecore_file_can_write(dest_file) != EINA_TRUE);
248    fail_if(ecore_file_can_exec(dest_file) != EINA_FALSE);
249    fail_if(ecore_file_remove(dest_file) != EINA_TRUE);
250 
251    ck_assert_str_eq(ecore_file_app_exe_get(exe_cmd), exe);
252    ck_assert_str_eq(ecore_file_escape_name(random_text), escaped_text);
253    ck_assert_str_eq(ecore_file_strip_ext(exe), "test");
254    res = ecore_file_app_installed(random_text);
255    fail_if(res != EINA_FALSE);
256    src_file = get_tmp_file();
257    fail_if(!src_file);
258    fail_if(ecore_file_remove(src_file) != EINA_TRUE);
259    fd = open(src_file, O_RDWR | O_BINARY | O_CREAT, 0700);
260    fail_if(fd < 0);
261    fail_if(close(fd) != 0);
262    fail_if(ecore_file_can_exec(src_file) != EINA_TRUE);
263 
264    src_dir = get_tmp_dir();
265    fail_if(!src_dir);
266    snprintf(dir, sizeof(dir), "%s/%s", src_dir, dirs[0]);
267    fail_if(ecore_file_mkdir(dir) != EINA_TRUE);
268 
269    fail_if(ecore_file_mkdirs(NULL) != -1);
270    for (i = 0; i < 3; i++)
271      {
272         char tmp[PATH_MAX];
273         snprintf(tmp, sizeof(tmp), "%s/%s", src_dir, dirs2[i]);
274         dirs2[i] = strdup(tmp);
275      }
276    fail_if(ecore_file_mkdirs((const char **)dirs2) != 3);
277    for (i = 0; i < 3; i++)
278      free(dirs2[i]);
279 
280    fail_if(ecore_file_mksubdirs(src_dir, NULL) != -1);
281    fail_if(ecore_file_mksubdirs(NULL, dirs) != -1);
282    fail_if(ecore_file_mksubdirs("", dirs) != -1);
283    fail_if(ecore_file_mksubdirs(src_file, dirs) != 0);
284    fail_if(ecore_file_remove(src_file) != EINA_TRUE);
285    fail_if(ecore_file_recursive_rm(src_dir) != EINA_TRUE);
286 
287    src_dir = get_tmp_dir();
288    fail_if(!src_dir);
289    fail_if(ecore_file_rmdir(src_dir) != EINA_TRUE);
290    fail_if(ecore_file_dir_is_empty(src_dir) != -1);
291    fail_if(ecore_file_ls(src_dir) != NULL);
292 
293    not_exist_file = get_tmp_file();
294    fail_if(!not_exist_file);
295    fail_if(ecore_file_remove(not_exist_file) != EINA_TRUE);
296    fail_if(ecore_file_exists(not_exist_file) != EINA_FALSE);
297    fail_if(ecore_file_mod_time(not_exist_file) != 0);
298    fail_if(ecore_file_size(not_exist_file) != 0);
299 
300    ck_assert_str_eq(ecore_file_realpath(NULL), "");
301    ck_assert_str_eq(ecore_file_realpath(not_exist_file), "");
302 
303    src_file = get_tmp_file();
304    fail_if(!src_file);
305    fail_if(ecore_file_remove(src_file) != EINA_TRUE);
306    fd = open(src_file, O_RDWR | O_BINARY | O_CREAT, 0400);
307    fail_if(fd < 0);
308    fail_if(close(fd) != 0);
309    fail_if(ecore_file_can_read(src_file) != EINA_TRUE);
310 #if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
311    if (getuid() || geteuid())
312 #endif
313      {
314         fail_if(ecore_file_can_write(src_file) != EINA_FALSE);
315         fail_if(ecore_file_can_exec(src_file) != EINA_FALSE);
316      }
317    fail_if(ecore_file_cp(src_file, src_file) != EINA_FALSE);
318    fail_if(ecore_file_remove(src_file) != EINA_TRUE);
319 
320    src_file = get_tmp_file();
321    fail_if(!src_file);
322    fail_if(ecore_file_remove(src_file) != EINA_TRUE);
323    fd = open(src_file, O_RDWR | O_BINARY | O_CREAT, 0200);
324    fail_if(fd < 0);
325    fail_if(close(fd) != 0);
326 #if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
327    if (getuid() || geteuid())
328 #endif
329      {
330         fail_if(ecore_file_can_read(src_file) != EINA_FALSE);
331      }
332    fail_if(ecore_file_can_exec(src_file) != EINA_FALSE);
333    fail_if(ecore_file_can_write(src_file) != EINA_TRUE);
334    fail_if(ecore_file_remove(src_file) != EINA_TRUE);
335 
336    src_file = get_tmp_file();
337    fail_if(!src_file);
338    fail_if(ecore_file_remove(src_file) != EINA_TRUE);
339    fd = open(src_file, O_RDWR | O_BINARY | O_CREAT, 0100);
340    fail_if(fd < 0);
341    fail_if(close(fd) != 0);
342 #if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
343    if (getuid() || geteuid())
344 #endif
345      {
346         fail_if(ecore_file_can_read(src_file) != EINA_FALSE);
347         fail_if(ecore_file_can_write(src_file) != EINA_FALSE);
348      }
349    fail_if(ecore_file_can_exec(src_file) != EINA_TRUE);
350    fail_if(ecore_file_remove(src_file) != EINA_TRUE);
351 
352    fail_if(ecore_file_unlink(not_exist_file) != EINA_FALSE);
353    fail_if(ecore_file_remove(not_exist_file) != EINA_FALSE);
354    fail_if(ecore_file_cp(not_exist_file, "test_file") != EINA_FALSE);
355    fail_if(ecore_file_mv(not_exist_file, "test_file") != EINA_FALSE);
356 
357    ck_assert_int_eq(chdir(eina_environment_tmp_get()), 0);
358    fail_if(ecore_file_mkpath(src_dir) != EINA_TRUE);
359    fail_if(ecore_file_rmdir(src_dir) != EINA_TRUE);
360    fail_if(ecore_file_mkpath(NULL) != EINA_FALSE);
361    fail_if(ecore_file_mkpaths(dirs) != 4);
362    for (i = 0; dirs[i]; i++)
363      if (ecore_file_is_dir(dirs[i]))
364        fail_if(ecore_file_recursive_rm(dirs[i]) != EINA_TRUE);
365    fail_if(ecore_file_mkpaths(NULL) != -1);
366 
367    fail_if(ecore_file_dir_get(NULL) != NULL);
368    fail_if(ecore_file_strip_ext(NULL) != NULL);
369    fail_if(ecore_file_escape_name(NULL) != NULL);
370 
371    ret = ecore_file_shutdown();
372    fail_if(ret != 0);
373 
374 }
375 EFL_END_TEST
376 
EFL_START_TEST(ecore_test_ecore_file_path)377 EFL_START_TEST(ecore_test_ecore_file_path)
378 {
379    const char *src_dir, *dest_file;
380    char *dup_dir, *path, *src_file;
381    unsigned int ret;
382    int fd;
383    Eina_Bool res;
384    Eina_List *list, *l;
385 
386    src_file = get_tmp_file();
387    fail_if(!src_file);
388    fail_if(ecore_file_remove(src_file) != EINA_TRUE);
389    fd = open(src_file, O_RDWR | O_BINARY | O_CREAT, 0700);
390    fail_if(fd < 0);
391    fail_if(close(fd) != 0);
392    fail_if(ecore_file_can_exec(src_file) != EINA_TRUE);
393    dup_dir = strdup(src_file);
394    fail_if(!dup_dir);
395    dest_file = basename(dup_dir);
396    dup_dir = strdup(src_file);
397 
398    src_dir = getenv("PATH");
399    fail_if(!src_dir);
400    path = malloc(strlen(src_dir) + strlen(dup_dir) + 2);
401    snprintf(path, strlen(src_dir) + strlen(dup_dir) + 2, "%s:%s", src_dir, dirname(dup_dir));
402    ret = setenv("PATH", path, 1);
403    fail_if(ret != 0);
404    free(dup_dir);
405    free(path);
406 
407    ret = ecore_file_init();
408 
409    res = ecore_file_app_installed(dest_file);
410    if (!res)
411      {
412         /* attempt to mitigate cascading failures */
413         ret = setenv("PATH", src_dir, 1);
414         fail_if(ret != 0);
415      }
416    fail_if(res != EINA_TRUE);
417    res = ecore_file_app_installed(src_file);
418    if (!res)
419      {
420         /* attempt to mitigate cascading failures */
421         ret = setenv("PATH", src_dir, 1);
422         fail_if(ret != 0);
423      }
424    fail_if(res != EINA_TRUE);
425    list = NULL;
426    list = ecore_file_app_list();
427    ret = setenv("PATH", src_dir, 1);
428    fail_if(ret != 0);
429    fd = 0;
430    EINA_LIST_FOREACH(list, l, path)
431      {
432         if (strcmp(path, src_file) == 0)
433           {
434              fd = 1;
435              break;
436           }
437      }
438    fail_if(fd == 0);
439    EINA_LIST_FREE(list, dup_dir)
440      free(dup_dir);
441 
442    fail_if(ecore_file_remove(src_file) != EINA_TRUE);
443 
444    ret = ecore_file_shutdown();
445    fail_if(ret != 0);
446    free(src_file);
447 }
448 EFL_END_TEST
449 
EFL_START_TEST(ecore_test_ecore_file_monitor)450 EFL_START_TEST(ecore_test_ecore_file_monitor)
451 {
452    Ecore_File_Monitor *mon;
453    char *src_dir;
454    const char *file = "EcoreFileDest";
455    const char *sub_dir[] = {"subdir1", 0};
456    char dir_name[MAXSIZE] = {'\0'};
457    char file_name[MAXSIZE] = {'\0'};
458    char *random_text = "This is random test String";
459    char *realp;
460    Eina_Bool res;
461    int ret;
462 
463    ret = ecore_file_init();
464    fail_if(ret != 1);
465 
466    src_dir = get_tmp_dir();
467    fail_if(!src_dir);
468 
469    realp = ecore_file_realpath(src_dir);
470    fail_if(!realp);
471    mon = ecore_file_monitor_add(realp, file_monitor_cb, NULL);
472    fail_if(mon == NULL);
473 
474    snprintf(file_name, sizeof(file_name), "%s/%s", src_dir, file);
475    _writeToFile(file_name, random_text);
476    _writeToFile(file_name, random_text);
477 
478    ck_assert_str_eq(ecore_file_monitor_path_get(mon), realp);
479    free(realp);
480 
481    ret = ecore_file_mksubdirs(src_dir, sub_dir);
482    fail_if(ret != 1);
483 
484    res = ecore_file_remove(file_name);
485    fail_if(res != EINA_TRUE);
486 
487    snprintf(dir_name, sizeof(dir_name), "%s/%s", src_dir, sub_dir[0]);
488    res = ecore_file_rmdir(dir_name);
489    fail_if(res != EINA_TRUE);
490 
491    res = ecore_file_recursive_rm(src_dir);
492    fail_if(res != EINA_TRUE);
493 
494    ecore_file_monitor_del(mon);
495 
496    ret = ecore_file_shutdown();
497    fail_if(ret != 0);
498    free(src_dir);
499 }
500 EFL_END_TEST
501 
EFL_START_TEST(ecore_test_ecore_file_download)502 EFL_START_TEST(ecore_test_ecore_file_download)
503 {
504    const char *download_dir;
505    const char *download_file;
506    const char *download_url = "http://example.com";
507    char dest_name[MAXSIZE] = {'\0'};
508    Eina_Bool res;
509    Eina_Hash *headers;
510    Ecore_File_Download_Job *job = NULL;
511    int ret;
512 
513    ret = ecore_file_init();
514    fail_if(ret != 1);
515 
516    download_dir = get_tmp_dir();
517    fail_if(!download_dir);
518    download_file = ecore_file_file_get(download_url); //example.com
519    fail_if(!download_file);
520    fail_if(!ecore_file_download_protocol_available("http://"));
521    snprintf(dest_name, sizeof(dest_name), "%s/%s", download_dir, download_file);
522 
523    res = ecore_file_download("xxyyzz", dest_name, completion_cb,
524                              progress_cb, NULL, &job);
525    fail_if(res != EINA_FALSE);
526 
527    int status = 0;
528    res = ecore_file_download(download_url, dest_name, err_completion_cb,
529                              progress_cb, &status, &job);
530    fail_if(res != EINA_TRUE);
531    fail_if(!job);
532    ecore_file_download_abort(job);
533    fail_if(status != 1);
534    if (!status)
535      ecore_main_loop_begin();
536    if (timeout_reached) goto end;
537    res = ecore_file_remove(dest_name);
538    fail_if(res != EINA_TRUE);
539 
540    headers = eina_hash_string_small_new(NULL);
541    eina_hash_add(headers, "Content-type", "text/html");
542 
543    res = ecore_file_download_full(download_url, dest_name, completion_cb,
544                                   progress_cb, NULL, &job, headers);
545    fail_if(res != EINA_TRUE);
546    fail_if(!job);
547    ecore_main_loop_begin();
548    if (timeout_reached) goto end;
549    fprintf(stderr, "Downloaded %lld bytes\n", ecore_file_size(dest_name));
550    res = ecore_file_exists(dest_name);
551    fail_if(res != EINA_TRUE);
552 end:
553    res = ecore_file_recursive_rm(download_dir);
554    fail_if(res != EINA_TRUE);
555 
556    ret = ecore_file_shutdown();
557    fail_if(ret != 0);
558 }
559 EFL_END_TEST
560 
ecore_test_ecore_file(TCase * tc)561 void ecore_test_ecore_file(TCase *tc)
562 {
563    tcase_add_test(tc, ecore_test_ecore_file_init);
564    tcase_add_test(tc, ecore_test_ecore_file_operations);
565    tcase_add_test(tc, ecore_test_ecore_file_monitor);
566    tcase_add_test(tc, ecore_test_ecore_file_download);
567    tcase_add_test(tc, ecore_test_ecore_file_path);
568 }
569