summaryrefslogtreecommitdiff
path: root/src/tests/ecore/ecore_test_ecore_file.c
blob: 8b3c567c76ea7063f2ad940cdcfdd470ae3c8677 (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <unistd.h>
#include <string.h>

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

#include "ecore_suite.h"

#define MAXSIZE 256

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

Eina_Tmpstr*
get_tmp_dir()
{
   Eina_Tmpstr *tmp_dir;

   Eina_Bool created = eina_file_mkdtemp("EcoreFileTestXXXXXX", &tmp_dir);

   if (!created)
     {
        return NULL;
     }

   return tmp_dir;
}

Eina_Tmpstr*
get_tmp_file()
{
   Eina_Tmpstr *tmp_file;

   Eina_Bool created = eina_file_mkstemp("EcoreFileTestXXXXXX", &tmp_file);

   if (!created)
     {
        return NULL;
     }

   return tmp_file;
}

int
_compare_path(const char *filePathA, const char *filePathB)
{
   char realFilePathA[MAXSIZE];
   char realFilePathB[MAXSIZE];
#ifdef _WIN32
   GetFullPathName(filePathA, MAXSIZE, realFilePathA, NULL);
   GetFullPathName(filePathB, MAXSIZE, realFilePathB, NULL);
#else
   realpath(filePathA, realFilePathA);
   realpath(filePathB, realFilePathB);
#endif
   return strcmp(realFilePathA, realFilePathB);
}

#define assert_path_eq(X, Y)                                            \
  do {                                                                  \
     const char* _ck_x = (X);                                           \
     const char* _ck_y = (Y);                                           \
     ck_assert_msg(0 == _compare_path(_ck_y, _ck_x),                    \
                   "Assertion '%s' failed: %s==\"%s\", %s==\"%s\"", #X"=="#Y, #X, _ck_x, #Y, _ck_y); \
  } while (0)

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();
}

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};
   const char *src_dir, *src_file, *dest_file;
   const char *tmpdir = NULL;
   char *random_text = "This is random test String";
   char dir[MAXSIZE] = {'\0'};
   unsigned int ret;
   Eina_Bool res;
   Eina_List *list;

#ifndef HAVE_EVIL
#if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
   if (getuid() == geteuid())
#endif
     {
        tmpdir = getenv("TMPDIR");
        if (!tmpdir) tmpdir = getenv("XDG_RUNTIME_DIR");
     }
   if (!tmpdir) tmpdir = "/tmp";
#else
   tmpdir = (char *)evil_tmpdir_get();
#endif /* ! HAVE_EVIL */

   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_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);

   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);
   assert_path_eq(ecore_file_readlink(dest_file), src_file);
   assert_path_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);

   assert_path_eq(ecore_file_dir_get(dest_file), tmpdir);
   assert_path_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);
   res = ecore_file_remove(dest_file);
   fail_if(res != EINA_TRUE);

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

}
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);

   assert_path_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;
   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);
   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, NULL);
   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_unlink(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);

   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);
}