summaryrefslogtreecommitdiff
path: root/src/tests/eio/eio_test_manager.c
blob: c1ac397c8ae4c6c93619bb0ba63f96366a68fb44 (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
#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 int test_count = 0;

static int DONE_CALLED = 0xdeadbeef;

static void
_filter_direct_cb(void *data EINA_UNUSED, const Eo_Event *event)
{
   Eio_Filter_Direct_Data *event_info = event->info;
   char *last_slash = strrchr(event_info->info->path, '/');

   //Check if it is a hidden file
   if (last_slash != NULL && strlen(last_slash) > 1 && last_slash[1] == '.')
     event_info->filter = EINA_FALSE;
   else
     event_info->filter = EINA_TRUE;
}

static void
_main_direct_cb(void *data, const Eina_File_Direct_Info *info)
{
   int *number_of_listed_files = (int *)data;

   fprintf(stderr, "Processing file:%s\n", info->path);
   (*number_of_listed_files)++;
}

static void
_filter_cb(void *data EINA_UNUSED, const Eo_Event *event)
{
   Eio_Filter_Name_Data *event_info = event->info;
   char *last_slash = strrchr(event_info->file, '/');

   //Check if it is a hidden file
   if (last_slash != NULL && strlen(last_slash) > 1 && last_slash[1] == '.')
     event_info->filter = EINA_FALSE;
   else
     event_info->filter = EINA_TRUE;
}

static void
_main_cb(void *data, const char *file)
{
   int *number_of_listed_files = (int *)data;

   fprintf(stderr, "Processing file:%s\n", file);
   (*number_of_listed_files)++;
}

static void
_done_cb(void *data, void* value EINA_UNUSED)
{
   int *number_of_listed_files = (int *)data;
   fail_if((*number_of_listed_files) != test_count);
   *number_of_listed_files = DONE_CALLED;
   ecore_main_loop_quit();
}

static void
_error_cb(void *data EINA_UNUSED, Eina_Error error)
{
   const char *msg = eina_error_msg_get(error);
   EINA_LOG_ERR("error: %s", msg);
   ecore_main_loop_quit();
}

static void
_open_done_cb(void *data, void *file_value)
{
   Eina_Bool *opened = (Eina_Bool *)data;
   *opened = EINA_TRUE;
   Eina_File* file = eina_file_dup(file_value);
   eina_file_close(file);
   ecore_main_loop_quit();
}

static void
_stat_done_cb(void *data, void *value)
{
   Eina_Stat const* stat = value;
   Eina_Bool *is_dir = data;
   unsigned int rights;
   fail_if(eio_file_is_dir(stat) != *is_dir);
   fail_if(eio_file_is_lnk(stat));
   rights = stat->mode & (S_IRWXU | S_IRWXG | S_IRWXO);
   fail_if(rights != default_rights);
   ecore_main_loop_quit();
}

typedef Eina_Promise* (*Efl_Io_Manager_Test_Stat_Ls_Func)(Eo *job, const char *path);

static void
_do_ls_test(Efl_Io_Manager_Test_Stat_Ls_Func ls_func,
      const Efl_Event_Description *event,
      Efl_Event_Cb filter_cb,
      Eina_Promise_Progress_Cb progress_cb,
      int expected_test_count,
      const char* test_dirname)
{
   int main_files = 0;

   Efl_Io_Manager *job = efl_add(EFL_IO_MANAGER_CLASS, NULL);
   Eina_Promise *promise = NULL;

   efl_event_callback_add(job, event, filter_cb, NULL);
   promise = ls_func(job, test_dirname);
   test_count = expected_test_count;
   eina_promise_progress_cb_add(promise, progress_cb, &main_files, NULL);
   eina_promise_then(promise, &_done_cb, &_error_cb, &main_files);

   ecore_main_loop_begin();

   fail_if(main_files != DONE_CALLED);

   efl_unref(job);
}

static void
_do_direct_ls_test(Efl_Io_Manager_Test_Stat_Ls_Func ls_func,
      int expected_test_count,
      const char *test_dirname)
{
   _do_ls_test(ls_func,
               EFL_IO_MANAGER_EVENT_FILTER_DIRECT,
               (Efl_Event_Cb)&_filter_direct_cb,
               (Eina_Promise_Progress_Cb)&_main_direct_cb,
               expected_test_count,
               test_dirname);
}

START_TEST(efl_io_manager_test_file_direct_stat)
{
   Eina_Bool is_dir;
   int ret;

   ret = ecore_init();
   fail_if(ret < 1);
   ret = eio_init();
   fail_if(ret < 1);
   ret = eina_init();
   fail_if(ret < 1);
   ret = ecore_file_init();
   fail_if(ret < 1);

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

   Efl_Io_Manager *job = efl_add(EFL_IO_MANAGER_CLASS, NULL);
   Eina_Promise *promise = NULL;
   // Start testing
   is_dir = EINA_TRUE;

   promise = efl_io_manager_file_direct_stat(job, nested_dirname);
   eina_promise_then(promise, &_stat_done_cb, &_error_cb, &is_dir);
   ecore_main_loop_begin();

   is_dir = EINA_FALSE;
   promise = efl_io_manager_file_direct_stat(job, nested_filename);
   eina_promise_then(promise, &_stat_done_cb, &_error_cb, &is_dir);
   ecore_main_loop_begin();
   efl_unref(job);

   // Cleanup
   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();
   eina_shutdown();
   eio_shutdown();
   ecore_shutdown();
}
END_TEST

START_TEST(efl_io_manager_test_ls_funcs)
{
   int ret;

   ret = ecore_init();
   fail_if(ret < 1);
   ret = eio_init();
   fail_if(ret < 1);
   ret = eina_init();
   fail_if(ret < 1);
   ret = ecore_file_init();
   fail_if(ret < 1);

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

   // Start testing

   _do_ls_test(&efl_io_manager_file_ls,
         EFL_IO_MANAGER_EVENT_FILTER_NAME,
         (Efl_Event_Cb)&_filter_cb,
         (Eina_Promise_Progress_Cb)&_main_cb,
         5,
         test_dirname);

   _do_direct_ls_test(&efl_io_manager_file_stat_ls, 5, test_dirname);

   _do_direct_ls_test(&efl_io_manager_file_direct_ls, 5, test_dirname);

   _do_direct_ls_test(&efl_io_manager_dir_stat_ls, 8, test_dirname);

   _do_direct_ls_test(&efl_io_manager_dir_direct_ls, 8, test_dirname);

   // Cleanup
   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();
   eina_shutdown();
   eio_shutdown();
   ecore_shutdown();
}
END_TEST

START_TEST(efl_io_manager_test_open)
{
   Eina_Bool opened_file;
   int ret;

   ret = ecore_init();
   fail_if(ret < 1);
   ret = eina_init();
   fail_if(ret < 1);
   ret = ecore_file_init();
   fail_if(ret < 1);
   ret = eio_init();
   fail_if(ret < 1);

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

   opened_file = EINA_FALSE;
   Efl_Io_Manager *job = efl_add(EFL_IO_MANAGER_CLASS, NULL);
   Eina_Promise *promise = NULL;

   promise = efl_io_manager_file_open(job, nested_filename, EINA_FALSE);
   eina_promise_then(promise, &_open_done_cb, &_error_cb, &opened_file);
   ecore_main_loop_begin();
   efl_unref(job);
   fail_if(!opened_file);

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

   eina_tmpstr_del(nested_dirname);
   eina_tmpstr_del(test_dirname);

   eio_shutdown();
   eina_tmpstr_del(nested_filename);
   ecore_file_shutdown();
   eina_shutdown();
   ecore_shutdown();
}
END_TEST

START_TEST(efl_io_instantiated)
{
   Efl_Io_Manager *manager;

   ecore_init();

   fail_if(efl_provider_find(ecore_main_loop_get(), EFL_IO_MANAGER_CLASS) != NULL);

   eio_init();

   manager = efl_provider_find(ecore_main_loop_get(), EFL_IO_MANAGER_CLASS);
   fail_if(manager == NULL);
   fail_if(!efl_isa(manager, EFL_IO_MANAGER_CLASS));

   eio_shutdown();

   ecore_shutdown();
}
END_TEST

void
eio_test_job(TCase *tc)
{
    tcase_add_test(tc, efl_io_manager_test_ls_funcs);
    tcase_add_test(tc, efl_io_manager_test_file_direct_stat);
    tcase_add_test(tc, efl_io_manager_test_open);
    tcase_add_test(tc, efl_io_instantiated);
}