summaryrefslogtreecommitdiff
path: root/src/tests/eio/eio_test_manager.c
blob: fb6c7f40542a096e4871d73eaa9a302b1319049c (plain) (blame)
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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

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

#include "eio_suite.h"
#include "eio_test_common.h"

#ifndef O_BINARY
# define O_BINARY 0
#endif

static uint64_t test_count = 0;
static Eina_Bool direct = EINA_FALSE;

#define DONE_CALLED 0xdeadbeef

static void
_access_cb(void *data, Eina_Array *paths)
{
   uint64_t *number_of_listed_files = data;

   *number_of_listed_files += eina_array_count(paths);
}

static void
_progress_cb(void *data, Eina_Array *entries)
{
   uint64_t *number_of_listed_files = data;

   (*number_of_listed_files) += eina_array_count(entries);
}

static Eina_Value
_future_cb(void *data,
           const Eina_Value file,
           const Eina_Future *dead EINA_UNUSED)
{
   if (file.type == EINA_VALUE_TYPE_ERROR)
     {
        Eina_Error err;

        eina_value_get(&file, &err);
        fprintf(stderr, "Something has gone wrong: %s\n", eina_error_msg_get(err));
        abort();
     }
   if (file.type == EINA_VALUE_TYPE_UINT64)
     {
        uint64_t *number_of_listed_files = data;
        uint64_t value;

        eina_value_get(&file, &value);

        fail_if((*number_of_listed_files) != test_count);
        fail_if(value != test_count);
        *number_of_listed_files = DONE_CALLED;
     }

   ecore_main_loop_quit();

   return file;
}

static Eina_Value
_done_cb(void *data,
         const Eina_Value file,
         const Eina_Future *dead EINA_UNUSED)
{
   uint64_t *number_of_listed_files = data;

   if (eina_value_type_get(&file) == EINA_VALUE_TYPE_ERROR)
     {
        Eina_Error err = 0;
        const char *msg;

        eina_value_error_get(&file, &err);
        msg = eina_error_msg_get(err);

        EINA_LOG_ERR("error: %s", msg);
        abort();
     }
   else
     {
        Eina_Value convert = EINA_VALUE_EMPTY;
        unsigned long files_count = 0;

        eina_value_setup(&convert, EINA_VALUE_TYPE_ULONG);
        eina_value_convert(&file, &convert);
        eina_value_ulong_get(&convert, &files_count);

        fail_if((*number_of_listed_files) != test_count);
        fail_if(files_count != test_count);

        *number_of_listed_files = DONE_CALLED;
     }

   ecore_main_loop_quit();

   return file;
}

static Eina_Value
_open_done_cb(void *data,
              const Eina_Value file,
              const Eina_Future *dead EINA_UNUSED)
{
   if (file.type == EINA_VALUE_TYPE_ERROR)
     {
        Eina_Error err;

        eina_value_get(&file, &err);
        fprintf(stderr, "Something has gone wrong: %s\n", eina_error_msg_get(err));
        abort();
     }
   if (file.type == EINA_VALUE_TYPE_FILE)
     {
        Eina_Bool *opened = (Eina_Bool *)data;

        *opened = EINA_TRUE;
     }
   ecore_main_loop_quit();

   return file;
}

static Eina_Value
_stat_done_cb(void *data,
              const Eina_Value st,
              const Eina_Future *dead EINA_UNUSED)
{
   Eina_Bool *is_dir = data;
   unsigned int rights;

   if (st.type == EINA_VALUE_TYPE_ERROR)
     {
        Eina_Error err;
        eina_value_get(&st, &err);
        fprintf(stderr, "Something has gone wrong: %s\n", eina_error_msg_get(err));
        abort();
     }

   if (st.type == EINA_VALUE_TYPE_STRUCT)
     {
        unsigned int mode = 0;

        fail_if(!eina_value_struct_get(&st, "mode", &mode));
        fail_if(S_ISDIR(mode) != *is_dir);
        /* no symbolic link on Windows */
#ifndef _WIN32
        fail_if(S_ISLNK(mode));
#endif

        rights = mode & (S_IRWXU | S_IRWXG | S_IRWXO);
        fail_if(rights != default_rights);
     }

   ecore_main_loop_quit();

   return st;
}

static void
_test_ls(Eina_Future *(*func)(const Eo *obj, const char *path, Eina_Bool recursive,
                              void *info_data, EflIoDirectInfo info, Eina_Free_Cb info_free_cb),
         uint64_t expected_test_count,
         const char* test_dirname)
{
   Efl_Io_Manager *job = efl_add(EFL_IO_MANAGER_CLASS, efl_main_loop_get());
   Eina_Future *f = NULL;
   uint64_t main_files = 0;

   fail_if(!job);

   f = func(job, test_dirname, EINA_FALSE, &main_files, _progress_cb, NULL);
   fail_if(!f);
   test_count = expected_test_count;
   eina_future_then(f, &_done_cb, &main_files, NULL);

   ecore_main_loop_begin();

   fail_if(main_files != DONE_CALLED);
   main_files = 0;

   f = func(job, test_dirname, EINA_TRUE, &main_files, _progress_cb, NULL);
   test_count = expected_test_count + 4;
   eina_future_then(f, &_done_cb, &main_files, NULL);

   ecore_main_loop_begin();

   fail_if(main_files != DONE_CALLED);

   efl_del(job);
}

EFL_START_TEST(efl_io_manager_test_stat)
{
   Eina_Tmpstr *test_dirname;
   Eina_Tmpstr *nested_dirname;
   Eina_Tmpstr *nested_filename;
   Efl_Io_Manager *job;
   Eina_Future *f;
   Eina_Bool is_dir = EINA_TRUE;
   int ret;

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

   test_dirname = get_eio_test_file_tmp_dir();
   nested_dirname = create_test_dirs(test_dirname);
   nested_filename = get_full_path(test_dirname, files[3]);

   job = efl_add(EFL_IO_MANAGER_CLASS, efl_main_loop_get());
   fail_if(!job);

   // Start testing
   f = efl_io_manager_stat(job, nested_dirname);
   eina_future_then(f, _stat_done_cb, &is_dir, NULL);
   ecore_main_loop_begin();

   is_dir = EINA_FALSE;
   f = efl_io_manager_stat(job, nested_filename);
   eina_future_then(f, _stat_done_cb, &is_dir, NULL);
   ecore_main_loop_begin();

   // Cleanup
   efl_del(job);
   fail_if(!ecore_file_recursive_rm(test_dirname));

   eina_tmpstr_del(nested_dirname);
   eina_tmpstr_del(test_dirname);
   eina_tmpstr_del(nested_filename);
   ecore_file_shutdown();
}
EFL_END_TEST

EFL_START_TEST(efl_io_manager_test_ls)
{
   Eina_Tmpstr *test_dirname;
   Eina_Tmpstr *nested_dirname;
   Eina_Tmpstr *nested_filename;
   Efl_Io_Manager *job;
   Eina_Future *f;
   uint64_t main_files = 0;
   int ret;

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

   test_dirname = get_eio_test_file_tmp_dir();
   nested_dirname = create_test_dirs(test_dirname);
   nested_filename = get_full_path(test_dirname, files[3]);

   // Start testing
   job = efl_add(EFL_IO_MANAGER_CLASS, efl_main_loop_get());
   fail_if(!job);

   f = efl_io_manager_ls(job, test_dirname, &main_files, _access_cb, NULL);
   test_count = 6;
   eina_future_then(f, _future_cb, &main_files, NULL);

   ecore_main_loop_begin();

   fail_if(main_files != DONE_CALLED);

   // No recursion for efl_io_manager_ls, should I fix that ?
   /* _test_ls(&efl_io_manager_ls, 5, test_dirname); */
   direct = EINA_TRUE;
   _test_ls(&efl_io_manager_stat_ls, 6, test_dirname);
   _test_ls(&efl_io_manager_direct_ls, 6, test_dirname);

   // Cleanup
   efl_del(job);
   fail_if(!ecore_file_recursive_rm(test_dirname));

   eina_tmpstr_del(nested_dirname);
   eina_tmpstr_del(test_dirname);
   eina_tmpstr_del(nested_filename);
   ecore_file_shutdown();
}
EFL_END_TEST

EFL_START_TEST(efl_io_manager_test_open)
{
   Eina_Tmpstr *test_dirname;
   Eina_Tmpstr *nested_dirname;
   Eina_Tmpstr *nested_filename;
   Efl_Io_Manager *job;
   Eina_Future *f;
   Eina_Bool opened_file = EINA_FALSE;
   int ret;

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

   test_dirname = get_eio_test_file_tmp_dir();
   nested_dirname = create_test_dirs(test_dirname);
   nested_filename = get_full_path(test_dirname, files[3]);

   job = efl_add(EFL_IO_MANAGER_CLASS, efl_main_loop_get());

   f = efl_io_manager_open(job, nested_filename, EINA_FALSE);
   eina_future_then(f, _open_done_cb, &opened_file, NULL);
   ecore_main_loop_begin();

   fail_if(!opened_file);

   // Cleanup
   efl_del(job);
   fail_if(!ecore_file_recursive_rm(test_dirname));

   eina_tmpstr_del(nested_dirname);
   eina_tmpstr_del(test_dirname);

   eina_tmpstr_del(nested_filename);
   ecore_file_shutdown();
}
EFL_END_TEST

EFL_START_TEST(efl_io_test_instantiated)
{
   Efl_Io_Manager *manager;

   ck_assert_int_eq(eio_shutdown(), 0);
   ecore_init();
   fail_if(efl_provider_find(efl_main_loop_get(), EFL_IO_MANAGER_CLASS) != NULL);
   ecore_shutdown();
   ck_assert_int_eq(eio_init(), 1);

   manager = efl_provider_find(efl_main_loop_get(), EFL_IO_MANAGER_CLASS);
   fail_if(manager == NULL);
   fail_if(!efl_isa(manager, EFL_IO_MANAGER_CLASS));
}
EFL_END_TEST

void
eio_test_job(TCase *tc)
{
    tcase_add_test(tc, efl_io_manager_test_ls);
    tcase_add_test(tc, efl_io_manager_test_stat);
    tcase_add_test(tc, efl_io_manager_test_open);
    tcase_add_test(tc, efl_io_test_instantiated);
}