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

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

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

#include "eio_suite.h"

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

Eina_Tmpstr*
get_file_path(const char* tmpdirname, const char* filename)
{
    char file_path[PATH_MAX] = "";
    eina_str_join(file_path, sizeof(file_path), '/', tmpdirname, filename);
    return eina_tmpstr_add(file_path);
}

static Eina_Bool
_filter_cb(void *data EINA_UNUSED, Eio_File *handler EINA_UNUSED, const char *attr EINA_UNUSED)
{
   return EINA_TRUE;
}

static void
_main_cb(void *data EINA_UNUSED, Eio_File *handler EINA_UNUSED, const char *attr)
{
   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)
       break;

   fail_if(i == sizeof (attribute) / sizeof (attribute[0]));
   (*num_of_attr)++;
}

static void
_done_cb(void *data, Eio_File *handler EINA_UNUSED)

{
   int *num_of_attr = (int *)data;

   (*num_of_attr)++;

   if (((*num_of_attr) + 1) == (sizeof (attribute) / sizeof (attribute[0])))
     ecore_main_loop_quit();
}

static void
_done_once_cb(void *data EINA_UNUSED, Eio_File *handler EINA_UNUSED)
{
   ecore_main_loop_quit();
}

static void
_done_get_cb(void *data, Eio_File *handler EINA_UNUSED, const char *name, unsigned int len EINA_UNUSED)

{
   int i = (int)(uintptr_t)data;

   fail_if(strcmp(name, attr_data[i]) != 0);

   if ((i + 1) == (sizeof (attribute) / sizeof (attribute[0])))
     ecore_main_loop_quit();
}

static void
_error_cb(void *data EINA_UNUSED, Eio_File *handler EINA_UNUSED, int error)

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

   ecore_main_loop_quit();
}

#ifdef XATTR_TEST_DIR
START_TEST(eio_test_xattr_set)
{
   char *filename = "eio-tmpfile";
   Eina_Tmpstr *test_file_path;
   Eio_File *fp;
   int num_of_attr = 0, fd;
   unsigned int i;

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

   test_file_path = get_file_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);

   for (i = 0; i < sizeof(attribute) / sizeof(attribute[0]); ++i)
     {
        fp = eio_file_xattr_set(test_file_path, attribute[i],
                                attr_data[i], strlen(attr_data[i]),
                                EINA_XATTR_INSERT,
                                _done_cb, _error_cb, &num_of_attr);

        fail_if(num_of_attr != 0); // test asynchronous
        fail_if(!fp);
     }

   ecore_main_loop_begin();

   for (i = 0; i < sizeof(attribute) / sizeof(attribute[0]); ++i)
     {
        fp = eio_file_xattr_get(test_file_path, attribute[i],
                                _done_get_cb, _error_cb, (void*)(uintptr_t) i);
        fail_if(!fp);
     }

   ecore_main_loop_begin();

   num_of_attr = 0;
   fp = eio_file_xattr(test_file_path,
                       _filter_cb, _main_cb, _done_once_cb, _error_cb,
                       &num_of_attr);
   fail_if(num_of_attr != 0);
   fail_if(!fp);

   ecore_main_loop_begin();

   fail_if(num_of_attr != sizeof (attribute)  / sizeof (attribute[0]));

   close(fd);
   unlink(test_file_path);
   eio_shutdown();
   ecore_shutdown();
}
END_TEST
#endif

void eio_test_xattr(TCase *tc)
{
#ifdef XATTR_TEST_DIR
   tcase_add_test(tc, eio_test_xattr_set);
#else
   (void)tc
#endif
}