summaryrefslogtreecommitdiff
path: root/src/tests/eio/eio_test_manager_xattr.c
blob: 77aa30c2322990be323a6df01baa24876ea298b0 (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

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

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

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


#ifndef XATTR_TEST_DIR
#define DISABLE_XATTR_TEST
#define XATTR_TEST_DIR ""
#endif

static const char *attribute[] =
  {
     "user.comment1",
     "user.comment2",
     "user.comment3"
  };
static const char *attr_data[] =
  {
     "This is a test file",
     "This line is a comment",
     "This file has extra attributes"
  };

int total_attributes = sizeof(attribute)/sizeof(attribute[0]);

static void
_filter_cb(void *data EINA_UNUSED, const Eo_Event *event)
{
   Eio_Filter_Name_Data *event_info = event->info;

   event_info->filter = EINA_TRUE;
}

static void
_main_cb(void *data, void *v)
{
   const char* attr = v;
   int *num_of_attr = (int *)data;
   unsigned int i;

   for (i = 0; i < sizeof (attribute) / sizeof (attribute[0]); ++i)
     if (strcmp(attr, attribute[i]) == 0)
       {
          (*num_of_attr)++;
          break;
       }

}

static void
_done_cb(void *data, void *value EINA_UNUSED)

{
   int *num_of_attr = (int *)data;

   fail_if(*num_of_attr != total_attributes);

   ecore_main_loop_quit();
}

static void
_done_get_cb(void *data EINA_UNUSED, void* v)
{
   Eina_Iterator** it = (Eina_Iterator**)v;
   int i = 0;
   Eio_Xattr_Data *get_data;

   while (eina_iterator_next((*it), (void**)&get_data))
     {
        fail_if(!get_data);
        fail_if(strcmp(get_data->data, attr_data[i]) != 0);
        i++;
     }

   fail_if(i != total_attributes);

   ecore_main_loop_quit();
}

static void
_done_set_cb(void *data, void* v)
{
   Eina_Iterator** it = (Eina_Iterator**)v;
   int *placeholder;
   int *num_of_attr = data;
   while(eina_iterator_next((*it), (void**)&placeholder))
     *num_of_attr += 1;

   fail_if(*num_of_attr != total_attributes);

   ecore_main_loop_quit();
}

static void
_error_cb(void *data EINA_UNUSED, Eina_Error error)

{
   fprintf(stderr, "Something has gone wrong:%s\n", eina_error_msg_get(error));
   abort();

   ecore_main_loop_quit();
}

START_TEST(eio_test_job_xattr_set)
{
   char *filename = "eio-tmpfile";
   Eina_Tmpstr *test_file_path;
   int num_of_attr = 0, fd;
   unsigned int i;
   Eo *job;
   Eina_Promise *list_promise = NULL;
   Eina_Promise **attrib_promises = NULL;

   ecore_init();
   eina_init();
   eio_init();

   fprintf(stderr, "eio_test_job_xattr_set\n"); fflush(stderr);

   job = eo_add(EFL_IO_MANAGER_CLASS, NULL);

   test_file_path = get_full_path(XATTR_TEST_DIR, filename);
   fd = open(test_file_path,
             O_WRONLY | O_CREAT | O_TRUNC,
             S_IRWXU | S_IRWXG | S_IRWXO);
   fail_if(fd == 0);

   attrib_promises = (Eina_Promise**)calloc(total_attributes + 1, sizeof(Eina_Promise*));
   attrib_promises[total_attributes] = NULL;

   for (i = 0; i < sizeof(attribute) / sizeof(attribute[0]); ++i)
     {
        attrib_promises[i] = efl_io_manager_file_xattr_set
          (job, test_file_path, attribute[i],
           attr_data[i], strlen(attr_data[i]),
           EINA_XATTR_INSERT);

        fail_if(num_of_attr != 0); // test asynchronous
     }
   eina_promise_then(eina_promise_all(eina_carray_iterator_new((void**)attrib_promises)),
         &_done_set_cb, _error_cb, &num_of_attr);

   ecore_main_loop_begin();

   free(attrib_promises);

   num_of_attr = 0;

   attrib_promises = (Eina_Promise**)calloc(total_attributes + 1, sizeof(Eina_Promise*));
   attrib_promises[total_attributes] = NULL;

   for (i = 0; i < sizeof(attribute) / sizeof(attribute[0]); ++i)
   {
     attrib_promises[i] = efl_io_manager_file_xattr_get(job, test_file_path, attribute[i]);
   }

   eina_promise_then(eina_promise_all(eina_carray_iterator_new((void**)attrib_promises)),
         _done_get_cb, _error_cb, &num_of_attr);

   ecore_main_loop_begin();

   num_of_attr = 0;

   efl_event_callback_add(job, EFL_IO_MANAGER_EVENT_XATTR, _filter_cb, NULL);
   list_promise = efl_io_manager_file_xattr_list_get(job, test_file_path);
   eina_promise_progress_cb_add(list_promise, _main_cb, &num_of_attr, NULL);
   eina_promise_then(list_promise, _done_cb, _error_cb, &num_of_attr);

   fail_if(num_of_attr != 0);

   ecore_main_loop_begin();

   free(attrib_promises);

   eo_unref(job);
   close(fd);
   unlink(test_file_path);
   eina_tmpstr_del(test_file_path);
   eio_shutdown();
   eina_shutdown();
   ecore_shutdown();
}
END_TEST

void eio_test_job_xattr(TCase *tc)
{
#ifndef DISABLE_XATTR_TEST
   tcase_add_test(tc, eio_test_job_xattr_set);
#else
   (void)eio_test_job_xattr_set;
   (void)tc;
#endif
}