summaryrefslogtreecommitdiff
path: root/src/tests/eio/eio_test_manager_xattr.c
blob: db8142fa7642198001d1b98d944d4fba43cdca70 (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
#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
_main_cb(void *data, Eina_Array *array)
{
   const char* attr;
   int *num_of_attr = (int *)data;
   Eina_Array_Iterator iterator;
   unsigned int j;

   EINA_ARRAY_ITER_NEXT(array, j, attr, iterator)
     {
        unsigned int i;

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

static Eina_Value
_future_done_cb(void *data EINA_UNUSED,
                const Eina_Value array,
                const Eina_Future *dead EINA_UNUSED)
{
   Eina_Error err;
   unsigned int i, len;
   Eina_Value v = EINA_VALUE_EMPTY;
   Eina_Binbuf *buf;

   if (array.type == EINA_VALUE_TYPE_ERROR)
     {
        eina_value_get(&array, &err);
        fprintf(stderr, "Something has gone wrong: %s\n", eina_error_msg_get(err));
        abort();
     }
   EINA_VALUE_ARRAY_FOREACH(&array, len, i, v)
     {
        buf = eina_value_to_binbuf(&v);
        fail_if(!buf);
        fail_if(strcmp((const char*) eina_binbuf_string_get(buf),
                       attr_data[i]) != 0);
     }

   fail_if((int) i != total_attributes);

   ecore_main_loop_quit();
   return array;
}

static Eina_Value
_future_all_cb(void *data,
               const Eina_Value array,
               const Eina_Future *dead EINA_UNUSED)
{
   Eina_Error err;
   unsigned int i, len;
   Eina_Value v = EINA_VALUE_EMPTY;
   int *num_of_attr = (int *)data;

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

   fail_if(*num_of_attr != (int) len);
   return array;
}

EFL_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_Future **futures = NULL;

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

   job = efl_add(EFL_IO_MANAGER_CLASS, efl_main_loop_get());

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

   futures = calloc(total_attributes + 1, sizeof(Eina_Future*));
   futures[total_attributes] = EINA_FUTURE_SENTINEL;

   for (i = 0; i < sizeof(attribute) / sizeof(attribute[0]); ++i)
     {
        Eina_Binbuf *buf;

        buf = eina_binbuf_manage_new((const unsigned char*) attr_data[i], strlen(attr_data[i]), EINA_TRUE);
        futures[i] = efl_io_manager_xattr_set(job, test_file_path, attribute[i], buf, EINA_XATTR_INSERT);
        eina_binbuf_free(buf);

        fail_if(num_of_attr != 0); // test asynchronous
     }
   eina_future_then(eina_future_all_array(futures),
                    _future_all_cb, &num_of_attr);

   ecore_main_loop_begin();

   free(futures);

   num_of_attr = 0;

   futures = calloc(total_attributes + 1, sizeof(Eina_Future*));
   futures[total_attributes] = NULL;

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

   eina_future_then(eina_future_all_array(futures),
                   _future_done_cb, &num_of_attr);

   ecore_main_loop_begin();

   num_of_attr = 0;

   eina_future_then(efl_io_manager_xattr_ls(job, test_file_path, &num_of_attr, _main_cb, NULL),
                    _future_done_cb, &num_of_attr);

   fail_if(num_of_attr != 0);

   ecore_main_loop_begin();

   free(futures);

   efl_unref(job);
   close(fd);
   unlink(test_file_path);
   eina_tmpstr_del(test_file_path);
   eio_shutdown();
   eina_shutdown();
   ecore_shutdown();
}
EFL_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
}