summaryrefslogtreecommitdiff
path: root/src/tests/ecore/ecore_test_ecore_file.c
blob: ba82223b1f77a6a15e9514d0a1d62e28c4a3ef7b (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libgen.h>

#include <Eina.h>
#include <Ecore.h>
#include <Ecore_File.h>

#include "ecore_suite.h"

#ifndef O_BINARY
# define O_BINARY 0
#endif

#define MAXSIZE 256

void
_writeToFile(const char *filePath, char *text)
{
   FILE *f = fopen(filePath, "rb+");
   if (f == NULL)
     f = fopen(filePath, "wb");
   fail_if(f == NULL);
   fprintf(f, "%s\n", text);
   fclose(f);
}

static char *
get_tmp_dir(void)
{
   Eina_Tmpstr *tmp_dir;
   char *realpath;

   Eina_Bool created = eina_file_mkdtemp("EcoreFileTestXXXXXX", &tmp_dir);
   if (!created) return NULL;

   realpath = ecore_file_realpath(tmp_dir);
   eina_tmpstr_del(tmp_dir);
   return realpath;
}

static char *
get_tmp_file(void)
{
   Eina_Tmpstr *tmp_file;
   char *realpath;

   int fd = eina_file_mkstemp("EcoreFileTestXXXXXX", &tmp_file);
   if (fd < 0) return NULL;
   close(fd);

   realpath = ecore_file_realpath(tmp_file);
   eina_tmpstr_del(tmp_file);
   return realpath;
}

static void
file_monitor_cb(void *data EINA_UNUSED, Ecore_File_Monitor *em EINA_UNUSED,
                Ecore_File_Event event, const char *path)
{
   switch (event)
     {
        case ECORE_FILE_EVENT_NONE:
        case ECORE_FILE_EVENT_CREATED_FILE:
          fprintf(stderr, "File created in %s", path);
          break;
        case ECORE_FILE_EVENT_DELETED_FILE:
          fprintf(stderr, "File deleted in %s", path);
          break;
        case ECORE_FILE_EVENT_MODIFIED:
          fprintf(stderr, "File modified in %s", path);
          break;
        case ECORE_FILE_EVENT_CLOSED:
          fprintf(stderr, "File closed in %s", path);
          break;
        case ECORE_FILE_EVENT_DELETED_DIRECTORY:
          fprintf(stderr, "Directory deleted in %s", path);
          break;
        case ECORE_FILE_EVENT_CREATED_DIRECTORY:
          fprintf(stderr, "Directory created in %s", path);
          break;
        case ECORE_FILE_EVENT_DELETED_SELF:
          fprintf(stderr, "Path %s deleted", path);
          break;
     }
}

void
completion_cb(void *data EINA_UNUSED, const char *file EINA_UNUSED, int status)
{
   fprintf(stderr, "Done (status: %d)\n", status);
   ecore_main_loop_quit();
}

void
err_completion_cb(void *data EINA_UNUSED, const char *file EINA_UNUSED, int status)
{
   fail_if(status != 1);
   ecore_main_loop_quit();
}

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)
{
   fprintf(stderr, "Progress: %ld/%ld\n", dlnow, dltotal);
   return ECORE_FILE_PROGRESS_CONTINUE;
}

START_TEST(ecore_test_ecore_file_init)
{
   int ret;

   ret = ecore_file_init();
   fail_if(ret != 1);

   ret = ecore_file_shutdown();
   fail_if(ret != 0);
}
END_TEST

START_TEST(ecore_test_ecore_file_operations)
{
   const char* dirs[] = {"b", "b/c", "b/c/d", "d", 0};
   char *dirs2[] = {"a2", "b2", "c2", 0};
   const char *src_dir, *src_file, *dest_file;
   const char *not_exist_file;
   const char *tmpdir = NULL;
   char *random_text = "This is random test String";
   char *escaped_text = "This\\ is\\ random\\ test\\ String";
   char *exe_cmd = "test.sh --opt1=a --opt2=b";
   char *exe = "test.sh";
   char dir[MAXSIZE] = {'\0'};
   unsigned int ret;
   int fd;
   int i;
   Eina_Bool res;
   Eina_List *list;

   tmpdir = ecore_file_realpath(eina_environment_tmp_get());

   ret = ecore_file_init();
   fail_if(ret != 1);

   src_dir = get_tmp_dir();
   fail_if(!src_dir);

   src_file = get_tmp_file();
   fail_if(!src_file);

   res = ecore_file_exists(src_file);
   fail_if(res != EINA_TRUE);
   res = ecore_file_is_dir(src_file);
   fail_if(res != EINA_FALSE);
   res = ecore_file_remove(src_file);
   fail_if(res != EINA_TRUE);
   res = ecore_file_exists(src_file);
   fail_if(res != EINA_FALSE);

   res = ecore_file_is_dir(src_dir);
   fail_if(res != EINA_TRUE);
   ret = ecore_file_dir_is_empty(src_dir);
   fail_if(ret != 1);
   res = ecore_file_mkdir(src_dir);
   fail_if(res != EINA_FALSE);
   res = ecore_file_remove(src_dir);
   fail_if(res != EINA_TRUE);
   res = ecore_file_is_dir(src_dir);
   fail_if(res != EINA_FALSE);

   src_dir = get_tmp_dir();
   fail_if(!src_dir);
   ret = ecore_file_mksubdirs(src_dir, dirs);
   fail_if(ret != 4);
   list = ecore_file_ls(src_dir);
   fail_if(!list);
   fail_if(eina_list_count(list) != 2);
   res = ecore_file_recursive_rm(src_dir);
   fail_if(res != EINA_TRUE);

   src_dir = get_tmp_dir();
   fail_if(!src_dir);
   strcat(dir, src_dir);
   strcat(dir, "/");
   strcat(dir, dirs[2]);
   res = ecore_file_mkpath(dir);
   fail_if(res != EINA_TRUE);
   res = ecore_file_recursive_rm(src_dir);
   fail_if(res != EINA_TRUE);

   src_file = get_tmp_file();
   fail_if(!src_file);
   _writeToFile(src_file, random_text);

   ret = ecore_file_size(src_file);
   fail_if(ret != strlen(random_text)+1);
   ret = ecore_file_is_dir(src_file);
   fail_if(ret != EINA_FALSE);

   dest_file = get_tmp_file();
   fail_if(!dest_file);
   res = ecore_file_cp(src_file, dest_file);
   fail_if(res != EINA_TRUE);
   res = ecore_file_exists(dest_file);
   fail_if(res != EINA_TRUE);
   fail_if(ecore_file_size(src_file) != ecore_file_size(dest_file));
   res = ecore_file_remove(dest_file);
   fail_if(res != EINA_TRUE);

   res = ecore_file_symlink(src_file, dest_file);
   fail_if(res != EINA_TRUE);
   ck_assert_str_eq(ecore_file_readlink(dest_file), src_file);
   ck_assert_str_eq(ecore_file_realpath(dest_file), src_file);
   res = ecore_file_unlink(dest_file);
   fail_if(res != EINA_TRUE);

   dest_file = get_tmp_file();
   fail_if(!dest_file);
   res = ecore_file_mv(src_file, dest_file);
   fail_if(res != EINA_TRUE);
   res = ecore_file_exists(dest_file);
   fail_if(res != EINA_TRUE);
   res = ecore_file_exists(src_file);
   fail_if(res != EINA_FALSE);

   ck_assert_str_eq(ecore_file_dir_get(dest_file), tmpdir);
   ck_assert_str_eq(ecore_file_realpath(dest_file), dest_file);
   fail_if(ecore_file_mod_time(dest_file) == 0);
   fail_if(ecore_file_can_read(dest_file) != EINA_TRUE);
   fail_if(ecore_file_can_write(dest_file) != EINA_TRUE);
   fail_if(ecore_file_can_exec(dest_file) != EINA_FALSE);
   fail_if(ecore_file_remove(dest_file) != EINA_TRUE);

   ck_assert_str_eq(ecore_file_app_exe_get(exe_cmd), exe);
   ck_assert_str_eq(ecore_file_escape_name(random_text), escaped_text);
   ck_assert_str_eq(ecore_file_strip_ext(exe), "test");
   res = ecore_file_app_installed(random_text);
   fail_if(res != EINA_FALSE);
   src_file = get_tmp_file();
   fail_if(!src_file);
   fail_if(ecore_file_remove(src_file) != EINA_TRUE);
   fd = open(src_file, O_RDWR | O_BINARY | O_CREAT, 0700);
   fail_if(fd < 0);
   fail_if(close(fd) != 0);
   fail_if(ecore_file_can_exec(src_file) != EINA_TRUE);

   src_dir = get_tmp_dir();
   fail_if(!src_dir);
   strcpy(dir, src_dir);
   strcat(dir, "/");
   strcat(dir, dirs[0]);
   fail_if(ecore_file_mkdir(dir) != EINA_TRUE);

   fail_if(ecore_file_mkdirs(NULL) != -1);
   for (i = 0; i < 3; i++)
     {
        char tmp[PATH_MAX];
        strcpy(tmp, src_dir);
        strcat(tmp, "/");
        strcat(tmp, dirs2[i]);
        dirs2[i] = strdup(tmp);
     }
   fail_if(ecore_file_mkdirs((const char **)dirs2) != 3);
   for (i = 0; i < 3; i++)
     free(dirs2[i]);

   fail_if(ecore_file_mksubdirs(src_dir, NULL) != -1);
   fail_if(ecore_file_mksubdirs(NULL, dirs) != -1);
   fail_if(ecore_file_mksubdirs("", dirs) != -1);
   fail_if(ecore_file_mksubdirs(src_file, dirs) != 0);
   fail_if(ecore_file_remove(src_file) != EINA_TRUE);
   fail_if(ecore_file_recursive_rm(src_dir) != EINA_TRUE);

   src_dir = get_tmp_dir();
   fail_if(!src_dir);
   fail_if(ecore_file_rmdir(src_dir) != EINA_TRUE);
   fail_if(ecore_file_dir_is_empty(src_dir) != -1);
   fail_if(ecore_file_ls(src_dir) != NULL);

   not_exist_file = get_tmp_file();
   fail_if(!not_exist_file);
   fail_if(ecore_file_remove(not_exist_file) != EINA_TRUE);
   fail_if(ecore_file_exists(not_exist_file) != EINA_FALSE);
   fail_if(ecore_file_mod_time(not_exist_file) != 0);
   fail_if(ecore_file_size(not_exist_file) != 0);

   ck_assert_str_eq(ecore_file_realpath(NULL), "");
   ck_assert_str_eq(ecore_file_realpath(not_exist_file), "");

   src_file = get_tmp_file();
   fail_if(!src_file);
   fail_if(ecore_file_remove(src_file) != EINA_TRUE);
   fd = open(src_file, O_RDWR | O_BINARY | O_CREAT, 0400);
   fail_if(fd < 0);
   fail_if(close(fd) != 0);
   fail_if(ecore_file_can_read(src_file) != EINA_TRUE);
   fail_if(ecore_file_can_write(src_file) != EINA_FALSE);
   fail_if(ecore_file_can_exec(src_file) != EINA_FALSE);
   fail_if(ecore_file_cp(src_file, src_file) != EINA_FALSE);
   fail_if(ecore_file_remove(src_file) != EINA_TRUE);

   src_file = get_tmp_file();
   fail_if(!src_file);
   fail_if(ecore_file_remove(src_file) != EINA_TRUE);
   fd = open(src_file, O_RDWR | O_BINARY | O_CREAT, 0200);
   fail_if(fd < 0);
   fail_if(close(fd) != 0);
   fail_if(ecore_file_can_read(src_file) != EINA_FALSE);
   fail_if(ecore_file_can_write(src_file) != EINA_TRUE);
   fail_if(ecore_file_can_exec(src_file) != EINA_FALSE);
   fail_if(ecore_file_remove(src_file) != EINA_TRUE);

   src_file = get_tmp_file();
   fail_if(!src_file);
   fail_if(ecore_file_remove(src_file) != EINA_TRUE);
   fd = open(src_file, O_RDWR | O_BINARY | O_CREAT, 0100);
   fail_if(fd < 0);
   fail_if(close(fd) != 0);
   fail_if(ecore_file_can_read(src_file) != EINA_FALSE);
   fail_if(ecore_file_can_write(src_file) != EINA_FALSE);
   fail_if(ecore_file_can_exec(src_file) != EINA_TRUE);
   fail_if(ecore_file_remove(src_file) != EINA_TRUE);

   fail_if(ecore_file_unlink(not_exist_file) != EINA_FALSE);
   fail_if(ecore_file_remove(not_exist_file) != EINA_FALSE);
   fail_if(ecore_file_cp(not_exist_file, "test_file") != EINA_FALSE);
   fail_if(ecore_file_mv(not_exist_file, "test_file") != EINA_FALSE);

   chdir(eina_environment_tmp_get());
   fail_if(ecore_file_mkpath(src_dir) != EINA_TRUE);
   fail_if(ecore_file_rmdir(src_dir) != EINA_TRUE);
   fail_if(ecore_file_mkpath(NULL) != EINA_FALSE);
   fail_if(ecore_file_mkpaths(dirs) != 4);
   for (i = 0; dirs[i]; i++)
     if (ecore_file_is_dir(dirs[i]))
       fail_if(ecore_file_recursive_rm(dirs[i]) != EINA_TRUE);
   fail_if(ecore_file_mkpaths(NULL) != -1);

   fail_if(ecore_file_dir_get(NULL) != NULL);
   fail_if(ecore_file_strip_ext(NULL) != NULL);
   fail_if(ecore_file_escape_name(NULL) != NULL);

   ret = ecore_file_shutdown();
   fail_if(ret != 0);

}
END_TEST

START_TEST(ecore_test_ecore_file_path)
{
   const char *src_dir, *src_file, *dest_file;
   char *dup_dir, *path;
   unsigned int ret;
   int fd;
   Eina_Bool res;
   Eina_List *list, *l;

   src_file = get_tmp_file();
   fail_if(!src_file);
   fail_if(ecore_file_remove(src_file) != EINA_TRUE);
   fd = open(src_file, O_RDWR | O_BINARY | O_CREAT, 0700);
   fail_if(fd < 0);
   fail_if(close(fd) != 0);
   fail_if(ecore_file_can_exec(src_file) != EINA_TRUE);
   dup_dir = strdup(src_file);
   fail_if(!dup_dir);
   dest_file = basename(dup_dir);
   dup_dir = strdup(src_file);

   src_dir = getenv("PATH");
   fail_if(!src_dir);
   path = malloc(strlen(src_dir) + strlen(dup_dir) + 1);
   *path = '\0';
   strcat(path, src_dir);
   strcat(path, ":");
   strcat(path, dirname(dup_dir));
   ret = setenv("PATH", path, 1);
   fail_if(ret != 0);
   free(dup_dir);
   free(path);

   ret = ecore_file_init();

   res = ecore_file_app_installed(dest_file);
   fail_if(res != EINA_TRUE);
   res = ecore_file_app_installed(src_file);
   fail_if(res != EINA_TRUE);
   list = NULL;
   list = ecore_file_app_list();
   fd = 0;
   EINA_LIST_FOREACH(list, l, path)
     {
        if (strcmp(path, src_file) == 0)
          {
             fd = 1;
             break;
          }
     }
   fail_if(fd == 0);
   EINA_LIST_FREE(list, dup_dir)
     free(dup_dir);
   ret = setenv("PATH", src_dir, 1);
   fail_if(ret != 0);

   fail_if(ecore_file_remove(src_file) != EINA_TRUE);
}
END_TEST

START_TEST(ecore_test_ecore_file_monitor)
{
   Ecore_File_Monitor *mon;
   const char *src_dir;
   const char *file = "EcoreFileDest";
   const char *sub_dir[] = {"subdir1", 0};
   char dir_name[MAXSIZE] = {'\0'};
   char file_name[MAXSIZE] = {'\0'};
   char *random_text = "This is random test String";
   char *realp;
   Eina_Bool res;
   int ret;

   ret = ecore_file_init();
   fail_if(ret != 1);

   src_dir = get_tmp_dir();
   fail_if(!src_dir);

   realp = ecore_file_realpath(src_dir);
   fail_if(!realp);
   mon = ecore_file_monitor_add(realp, file_monitor_cb, NULL);
   fail_if(mon == NULL);

   strcat(file_name, src_dir);
   strcat(file_name, "/");
   strcat(file_name, file);
   _writeToFile(file_name, random_text);
   _writeToFile(file_name, random_text);

   ck_assert_str_eq(ecore_file_monitor_path_get(mon), realp);

   ret = ecore_file_mksubdirs(src_dir, sub_dir);
   fail_if(ret != 1);

   res = ecore_file_remove(file_name);
   fail_if(res != EINA_TRUE);

   strcat(dir_name, src_dir);
   strcat(dir_name, "/");
   strcat(dir_name, sub_dir[0]);
   res = ecore_file_rmdir(dir_name);
   fail_if(res != EINA_TRUE);

   res = ecore_file_recursive_rm(src_dir);
   fail_if(res != EINA_TRUE);

   ecore_file_monitor_del(mon);

   ret = ecore_file_shutdown();
   fail_if(ret != 0);

}
END_TEST

START_TEST(ecore_test_ecore_file_download)
{
   const char *download_dir;
   const char *download_file;
   const char *download_url = "http://check.sourceforge.net/xml/check_unittest.xslt";
   char dest_name[MAXSIZE] = {'\0'};
   Eina_Bool res;
   Eina_Hash *headers;
   Ecore_File_Download_Job *job = NULL;
   int ret;

   ret = ecore_file_init();
   fail_if(ret != 1);

   download_dir = get_tmp_dir();
   fail_if(!download_dir);
   download_file = ecore_file_file_get(download_url);
   fail_if(!download_file);
   fail_if(!ecore_file_download_protocol_available("http://"));
   strcat(dest_name, download_dir);
   strcat(dest_name, "/");
   strcat(dest_name, download_file);
   res = ecore_file_download(download_url, dest_name, completion_cb,
                             progress_cb, NULL, &job);
   fail_if(res != EINA_TRUE);
   fail_if(!job);
   ecore_main_loop_begin();
   fprintf(stderr, "Downloaded %lld bytes\n", ecore_file_size(dest_name));
   res = ecore_file_exists(dest_name);
   fail_if(res != EINA_TRUE);
   res = ecore_file_unlink(dest_name);
   fail_if(res != EINA_TRUE);

   res = ecore_file_download("xxyyzz", dest_name, completion_cb,
                             progress_cb, NULL, &job);
   fail_if(res != EINA_FALSE);

   res = ecore_file_download(download_url, dest_name, err_completion_cb,
                             progress_cb, NULL, &job);
   fail_if(res != EINA_TRUE);
   fail_if(!job);
   ecore_file_download_abort(job);
   ecore_main_loop_begin();
   res = ecore_file_remove(dest_name);
   fail_if(res != EINA_TRUE);

   headers = eina_hash_string_small_new(NULL);
   eina_hash_add(headers, "Content-type", "text/xml");

   res = ecore_file_download_full(download_url, dest_name, completion_cb,
                                  progress_cb, NULL, NULL, headers);
   fail_if(res != EINA_TRUE);
   ecore_main_loop_begin();
   fprintf(stderr, "Downloaded %lld bytes\n", ecore_file_size(dest_name));
   res = ecore_file_exists(dest_name);
   fail_if(res != EINA_TRUE);

   res = ecore_file_recursive_rm(download_dir);
   fail_if(res != EINA_TRUE);

   ret = ecore_file_shutdown();
   fail_if(ret != 0);
}
END_TEST

void ecore_test_ecore_file(TCase *tc)
{
   tcase_add_test(tc, ecore_test_ecore_file_init);
   tcase_add_test(tc, ecore_test_ecore_file_operations);
   tcase_add_test(tc, ecore_test_ecore_file_monitor);
   tcase_add_test(tc, ecore_test_ecore_file_download);
   tcase_add_test(tc, ecore_test_ecore_file_path);
}