* eet: Add Eet_Connection.
[framework/uifw/eet.git] / src / tests / eet_suite.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <strings.h>
6 #include <stdio.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <pthread.h>
10
11 #include <check.h>
12
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16
17 #include <Eina.h>
18
19 #include "eet_suite.h"
20
21 START_TEST(eet_test_init)
22 {
23    int ret;
24
25    ret = eet_init();
26    fail_if(ret != 1);
27
28    ret = eet_shutdown();
29    fail_if(ret != 0);
30 }
31 END_TEST
32
33 typedef struct _Eet_Test_Basic_Type Eet_Test_Basic_Type;
34 struct _Eet_Test_Basic_Type
35 {
36    char c;
37    short s;
38    int i;
39    long long l;
40    char *str;
41    char *istr;
42    float f1;
43    float f2;
44    double d;
45    unsigned char uc;
46    unsigned short us;
47    unsigned int ui;
48    unsigned long long ul;
49    Eet_Test_Basic_Type *empty;
50    Eet_Test_Basic_Type *with;
51 };
52
53 #define EET_TEST_CHAR 0x42
54 #define EET_TEST_SHORT 0x4224
55 #define EET_TEST_INT 0x42211224
56 #define EET_TEST_LONG_LONG 0x84CB42211224BC48
57 #define EET_TEST_STRING "my little test with escape \\\""
58 #define EET_TEST_KEY1 "key1"
59 #define EET_TEST_KEY2 "key2"
60 #define EET_TEST_FLOAT 123.45689
61 #define EET_TEST_FLOAT2 1.0
62 #define EET_TEST_FLOAT3 0.25
63 #define EET_TEST_FLOAT4 0.0001234
64 #define EET_TEST_DOUBLE 123456789.9876543210
65 #define EET_TEST_DOUBLE2 1.0
66 #define EET_TEST_DOUBLE3 0.25
67 #define EET_TEST_FILE_KEY1 "keys/data/1"
68 #define EET_TEST_FILE_KEY2 "keys/data/2"
69 #define EET_TEST_FILE_IMAGE "keys/images/"
70
71 typedef struct _Eet_Test_Image Eet_Test_Image;
72 struct _Eet_Test_Image
73 {
74    unsigned int w;
75    unsigned int h;
76    int alpha;
77    unsigned int color[64];
78 };
79
80 static const Eet_Test_Image test_noalpha = {
81   8, 8, 0,
82   {
83     0x00AA0000, 0x0000AA00, 0x000000AA, 0x00110000, 0x00AA0000, 0x0000AA00, 0x000000AA, 0x00110000,
84     0x0000AA00, 0x000000AA, 0x00110000, 0x00AA0000, 0x0000AA00, 0x000000AA, 0x00110000, 0x00AA0000,
85     0x000000AA, 0x00110000, 0x00AA0000, 0x0000AA00, 0x000000AA, 0x00110000, 0x00AA0000, 0x0000AA00,
86     0x00110000, 0x00AA0000, 0x0000AA00, 0x000000AA, 0x00110000, 0x00AA0000, 0x0000AA00, 0x000000AA,
87     0x00AA0000, 0x0000AA00, 0x000000AA, 0x00110000, 0x00AA0000, 0x0000AA00, 0x000000AA, 0x00110000,
88     0x0000AA00, 0x000000AA, 0x00110000, 0x00AA0000, 0x0000AA00, 0x000000AA, 0x00110000, 0x00AA0000,
89     0x000000AA, 0x00110000, 0x00AA0000, 0x0000AA00, 0x000000AA, 0x00110000, 0x00AA0000, 0x0000AA00,
90     0x00110000, 0x00AA0000, 0x0000AA00, 0x000000AA, 0x00110000, 0x00AA0000, 0x0000AA00, 0x000000AA
91   }
92 };
93
94 static const Eet_Test_Image test_alpha = {
95   8, 8, 1,
96   {
97     0x0FAA0000, 0x0000AA00, 0x000000AA, 0x00110000, 0x00AA0000, 0x0000AA00, 0x000000AA, 0x0F110000,
98     0x0000AA00, 0x0F0000AA, 0x00110000, 0x00AA0000, 0x0000AA00, 0x000000AA, 0x0F110000, 0x00AA0000,
99     0x000000AA, 0x00110000, 0x0FAA0000, 0x0000AA00, 0x000000AA, 0x0F110000, 0x00AA0000, 0x0000AA00,
100     0x00110000, 0x00AA0000, 0x0000AA00, 0x0F0000AA, 0x0F110000, 0x00AA0000, 0x0000AA00, 0x000000AA,
101     0x00AA0000, 0x0000AA00, 0x000000AA, 0x0F110000, 0x0FAA0000, 0x0000AA00, 0x000000AA, 0x00110000,
102     0x0000AA00, 0x000000AA, 0x0F110000, 0x00AA0000, 0x0000AA00, 0x0F0000AA, 0x00110000, 0x00AA0000,
103     0x000000AA, 0x0F110000, 0x00AA0000, 0x0000AA00, 0x000000AA, 0x00110000, 0x0FAA0000, 0x0000AA00,
104     0x0F110000, 0x00AA0000, 0x0000AA00, 0x000000AA, 0x00110000, 0x00AA0000, 0x0000AA00, 0x0F0000AA
105   }
106 };
107
108 static void
109 _eet_test_basic_set(Eet_Test_Basic_Type *res, int i)
110 {
111    res->c = EET_TEST_CHAR;
112    res->s = EET_TEST_SHORT;
113    res->i = EET_TEST_INT + i;
114    res->l = EET_TEST_LONG_LONG;
115    res->str = EET_TEST_STRING;
116    res->istr = EET_TEST_STRING;
117    res->f1 = - EET_TEST_FLOAT;
118    res->d = - EET_TEST_DOUBLE;
119    res->f2 = EET_TEST_FLOAT4;
120    res->uc = EET_TEST_CHAR;
121    res->us = EET_TEST_SHORT;
122    res->ui = EET_TEST_INT;
123    res->ul = EET_TEST_LONG_LONG;
124    res->empty = NULL;
125    res->with = NULL;
126
127    if (i == 0)
128      {
129         Eet_Test_Basic_Type *tmp;
130
131         tmp = malloc(sizeof (Eet_Test_Basic_Type));
132         fail_if(!tmp);
133
134         res->with = tmp;
135         tmp->c = EET_TEST_CHAR;
136         tmp->s = EET_TEST_SHORT;
137         tmp->i = EET_TEST_INT + i + 1;
138         tmp->l = EET_TEST_LONG_LONG;
139         tmp->str = EET_TEST_STRING;
140         tmp->istr = EET_TEST_STRING;
141         tmp->f1 = - EET_TEST_FLOAT;
142         tmp->d = - EET_TEST_DOUBLE;
143         tmp->f2 = EET_TEST_FLOAT4;
144         tmp->uc = EET_TEST_CHAR;
145         tmp->us = EET_TEST_SHORT;
146         tmp->ui = EET_TEST_INT;
147         tmp->ul = EET_TEST_LONG_LONG;
148         tmp->empty = NULL;
149         tmp->with = NULL;
150      }
151 }
152
153 static void
154 _eet_test_basic_check(Eet_Test_Basic_Type *result, int i)
155 {
156    float tmp;
157
158    fail_if(result->c != EET_TEST_CHAR);
159    fail_if(result->s != EET_TEST_SHORT);
160    fail_if(result->i != EET_TEST_INT + i);
161    fail_if(result->l != (long long) EET_TEST_LONG_LONG);
162    fail_if(strcmp(result->str, EET_TEST_STRING) != 0);
163    fail_if(strcmp(result->istr, EET_TEST_STRING) != 0);
164    fail_if(result->uc != EET_TEST_CHAR);
165    fail_if(result->us != EET_TEST_SHORT);
166    fail_if(result->ui != EET_TEST_INT);
167    fail_if(result->ul != EET_TEST_LONG_LONG);
168
169    tmp = (result->f1 + EET_TEST_FLOAT);
170    if (tmp < 0) tmp = -tmp;
171    fail_if(tmp > 0.005);
172
173    tmp = (result->f2 - EET_TEST_FLOAT4);
174    if (tmp < 0) tmp = -tmp;
175    fail_if(tmp > 0.005);
176
177    tmp = (result->d + EET_TEST_DOUBLE);
178    if (tmp < 0) tmp = -tmp;
179    fail_if(tmp > 0.00005);
180
181    fail_if(result->empty != NULL);
182    if (i == 0)
183      {
184         Eet_Test_Basic_Type *tmp;
185
186         tmp = result->with;
187         fail_if(tmp == NULL);
188
189         fail_if(tmp->c != EET_TEST_CHAR);
190         fail_if(tmp->s != EET_TEST_SHORT);
191         fail_if(tmp->i != EET_TEST_INT + i + 1);
192         fail_if(tmp->l != (long long) EET_TEST_LONG_LONG);
193         fail_if(strcmp(tmp->str, EET_TEST_STRING) != 0);
194         fail_if(strcmp(tmp->istr, EET_TEST_STRING) != 0);
195         fail_if(tmp->uc != EET_TEST_CHAR);
196         fail_if(tmp->us != EET_TEST_SHORT);
197         fail_if(tmp->ui != EET_TEST_INT);
198         fail_if(tmp->ul != EET_TEST_LONG_LONG);
199      }
200    else
201      fail_if(result->with != NULL);
202 }
203
204 static void
205 _eet_build_basic_descriptor(Eet_Data_Descriptor *edd)
206 {
207    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Basic_Type, "c", c, EET_T_CHAR);
208    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Basic_Type, "s", s, EET_T_SHORT);
209    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Basic_Type, "i", i, EET_T_INT);
210    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Basic_Type, "l", l, EET_T_LONG_LONG);
211    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Basic_Type, "str", str, EET_T_STRING);
212    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Basic_Type, "istr", istr, EET_T_INLINED_STRING);
213    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Basic_Type, "f1", f1, EET_T_FLOAT);
214    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Basic_Type, "f2", f2, EET_T_FLOAT);
215    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Basic_Type, "d", d, EET_T_DOUBLE);
216    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Basic_Type, "uc", uc, EET_T_UCHAR);
217    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Basic_Type, "us", us, EET_T_USHORT);
218    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Basic_Type, "ui", ui, EET_T_UINT);
219    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Basic_Type, "ul", ul, EET_T_ULONG_LONG);
220
221    EET_DATA_DESCRIPTOR_ADD_SUB(edd, Eet_Test_Basic_Type, "empty", empty, edd);
222    EET_DATA_DESCRIPTOR_ADD_SUB(edd, Eet_Test_Basic_Type, "with", with, edd);
223 }
224
225 START_TEST(eet_test_basic_data_type_encoding_decoding)
226 {
227    Eet_Data_Descriptor *edd;
228    Eet_Test_Basic_Type *result;
229    Eet_Data_Descriptor_Class eddc;
230    Eet_Test_Basic_Type etbt;
231    void *transfert;
232    int size;
233
234    eet_init();
235
236    _eet_test_basic_set(&etbt, 0);
237
238    eet_test_setup_eddc(&eddc);
239    eddc.name = "Eet_Test_Basic_Type";
240    eddc.size = sizeof(Eet_Test_Basic_Type);
241
242    edd = eet_data_descriptor_stream_new(&eddc);
243    fail_if(!edd);
244
245    _eet_build_basic_descriptor(edd);
246
247    transfert = eet_data_descriptor_encode(edd, &etbt, &size);
248    fail_if(!transfert || size <= 0);
249
250    result = eet_data_descriptor_decode(edd, transfert, size);
251    fail_if(!result);
252
253    _eet_test_basic_check(result, 0);
254
255    free(result->str);
256    free(result);
257
258    eet_data_descriptor_free(edd);
259
260    eet_shutdown();
261 }
262 END_TEST
263
264 typedef struct _Eet_Test_Ex_Type Eet_Test_Ex_Type;
265 struct _Eet_Test_Ex_Type
266 {
267    char c;
268    short s;
269    int i;
270    unsigned long long l;
271    char *str;
272    char *istr;
273    float f1;
274    float f2;
275    float f3;
276    float f4;
277    double d1;
278    double d2;
279    double d3;
280    double d4;
281    Eina_List *list;
282    Eina_Hash *hash;
283    Eina_List *ilist;
284    Eina_List *slist;
285    Eina_Hash *ihash;
286    Eina_Hash *shash;
287    Eet_Test_Basic_Type sarray1[10];
288    unsigned int sarray2[5];
289    unsigned int varray1_count;
290    unsigned int *varray1;
291    unsigned int varray2_count;
292    Eet_Test_Basic_Type *varray2;
293    unsigned char uc;
294    unsigned short us;
295    unsigned int ui;
296    unsigned long long ul;
297    char *charray[10];
298 };
299
300 static int i42 = 42;
301 static int i7 = 7;
302
303
304 static void
305 _eet_build_ex_descriptor(Eet_Data_Descriptor *edd)
306 {
307    Eet_Data_Descriptor_Class eddc;
308    Eet_Test_Ex_Type etbt;
309    Eet_Data_Descriptor *eddb;
310
311    eet_test_setup_eddc(&eddc);
312    eddc.name = "Eet_Test_Basic_Type";
313    eddc.size = sizeof(Eet_Test_Basic_Type);
314    eddb = eet_data_descriptor_file_new(&eddc);
315    fail_if(!eddb);
316
317    _eet_build_basic_descriptor(eddb);
318
319    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "c", c, EET_T_CHAR);
320    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "s", s, EET_T_SHORT);
321    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "i", i, EET_T_INT);
322    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "l", l, EET_T_LONG_LONG);
323    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "str", str, EET_T_STRING);
324    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "istr", istr, EET_T_INLINED_STRING);
325    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "f1", f1, EET_T_FLOAT);
326    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "f2", f2, EET_T_FLOAT);
327    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "f3", f3, EET_T_FLOAT);
328    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "f4", f4, EET_T_FLOAT);
329    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "d1", d1, EET_T_DOUBLE);
330    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "d2", d2, EET_T_DOUBLE);
331    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "d3", d3, EET_T_DOUBLE);
332    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "d4", d4, EET_T_DOUBLE);
333    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "uc", uc, EET_T_UCHAR);
334    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "us", us, EET_T_USHORT);
335    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "ui", ui, EET_T_UINT);
336    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eet_Test_Ex_Type, "ul", ul, EET_T_ULONG_LONG);
337    EET_DATA_DESCRIPTOR_ADD_ARRAY(edd, Eet_Test_Ex_Type, "sarray1", sarray1, eddb);
338    EET_DATA_DESCRIPTOR_ADD_VAR_ARRAY(edd, Eet_Test_Ex_Type, "varray2", varray2, eddb);
339    eet_data_descriptor_element_add(edd, "varray1", EET_T_INT, EET_G_VAR_ARRAY,
340                                    (char *)(&(etbt.varray1)) - (char *)(&(etbt)),
341                                    (char *)(&(etbt.varray1_count)) - (char *)(&(etbt)), /* 0,  */NULL, NULL);
342    eet_data_descriptor_element_add(edd, "sarray2", EET_T_INT, EET_G_ARRAY,
343                                    (char *)(&(etbt.sarray2)) - (char *)(&(etbt)),
344                                    /* 0,  */sizeof(etbt.sarray2)/sizeof(etbt.sarray2[0]), NULL, NULL);
345    eet_data_descriptor_element_add(edd, "charray", EET_T_STRING, EET_G_ARRAY,
346                                    (char *)(&(etbt.charray)) - (char *)(&(etbt)),
347                                    /* 0,  */sizeof(etbt.charray)/sizeof(etbt.charray[0]), NULL, NULL);
348    EET_DATA_DESCRIPTOR_ADD_LIST(edd, Eet_Test_Ex_Type, "list", list, edd);
349    EET_DATA_DESCRIPTOR_ADD_HASH(edd, Eet_Test_Ex_Type, "hash", hash, edd);
350    eet_data_descriptor_element_add(edd, "ilist", EET_T_INT, EET_G_LIST,
351                                    (char *)(&(etbt.ilist)) - (char *)(&(etbt)),
352                                    0, /* 0,  */NULL, NULL);
353    eet_data_descriptor_element_add(edd, "ihash", EET_T_INT, EET_G_HASH,
354                                    (char *)(&(etbt.ihash)) - (char *)(&(etbt)),
355                                    0, /* 0,  */NULL, NULL);
356    eet_data_descriptor_element_add(edd, "slist", EET_T_STRING, EET_G_LIST,
357                                    (char *)(&(etbt.slist)) - (char *)(&(etbt)),
358                                    0, /* 0,  */NULL, NULL);
359    eet_data_descriptor_element_add(edd, "shash", EET_T_STRING, EET_G_HASH,
360                                    (char *)(&(etbt.shash)) - (char *)(&(etbt)),
361                                    0, /* 0,  */NULL, NULL);
362 }
363
364 static Eet_Test_Ex_Type*
365 _eet_test_ex_set(Eet_Test_Ex_Type *res, int offset)
366 {
367    int i;
368
369    if (!res) res = malloc( sizeof(Eet_Test_Ex_Type));
370    if (!res) return NULL;
371
372    res->c = EET_TEST_CHAR + offset;
373    res->s = EET_TEST_SHORT + offset;
374    res->i = EET_TEST_INT + offset;
375    res->l = EET_TEST_LONG_LONG + offset;
376    res->str = EET_TEST_STRING;
377    res->istr = EET_TEST_STRING;
378    res->f1 = EET_TEST_FLOAT + offset;
379    res->f2 = -(EET_TEST_FLOAT2 + offset);
380    res->f3 = EET_TEST_FLOAT3 + offset;
381    res->f4 = EET_TEST_FLOAT2 + offset;
382    res->d1 = EET_TEST_DOUBLE + offset;
383    res->d2 = -(EET_TEST_DOUBLE2 + offset);
384    res->d3 = EET_TEST_DOUBLE3 + offset;
385    res->d4 = EET_TEST_DOUBLE2 + offset;
386    res->list = NULL;
387    res->hash = NULL;
388    res->ilist = NULL;
389    res->ihash = NULL;
390    res->slist = NULL;
391    res->shash = NULL;
392    for (i = 0; i < sizeof(res->charray)/sizeof(res->charray[0]); ++i)
393      res->charray[i] = NULL;
394
395    res->varray2 = malloc(sizeof (Eet_Test_Basic_Type) * 10);
396    res->varray1 = malloc(sizeof (int) * 5);
397    fail_if(!res->varray1 || !res->varray2);
398    for (i = 0; i < 10; ++i)
399      {
400         _eet_test_basic_set(res->sarray1 + i, i);
401         _eet_test_basic_set(res->varray2 + i, i);
402      }
403    res->varray2_count = 10;
404    for (i = 0; i < 5; ++i)
405      {
406         res->sarray2[i] = i * 42 + 1;
407         res->varray1[i] = i * 42 + 1;
408      }
409    res->varray1_count = 5;
410
411    res->uc = EET_TEST_CHAR + offset;
412    res->us = EET_TEST_SHORT + offset;
413    res->ui = EET_TEST_INT + offset;
414    res->ul = EET_TEST_LONG_LONG + offset;
415
416    return res;
417 }
418
419 static int
420 _eet_test_ex_check(Eet_Test_Ex_Type *stuff, int offset)
421 {
422    double tmp;
423    unsigned int i;
424
425    if (!stuff) return 1;
426
427    if (stuff->c != EET_TEST_CHAR + offset) return 1;
428    if (stuff->s != EET_TEST_SHORT + offset) return 1;
429    if (stuff->i != EET_TEST_INT + offset) return 1;
430    if (stuff->l != EET_TEST_LONG_LONG + offset) return 1;
431    if (strcmp(stuff->str, EET_TEST_STRING) != 0) return 1;
432    if (strcmp(stuff->istr, EET_TEST_STRING) != 0) return 1;
433
434    tmp = stuff->f1 - (EET_TEST_FLOAT + offset);
435    if (tmp < 0) tmp = -tmp;
436    if (tmp > 0.005) return 1;
437
438    tmp = stuff->d1 - (EET_TEST_DOUBLE + offset);
439    if (tmp < 0) tmp = -tmp;
440    if (tmp > 0.00005) return 1;
441
442    if (stuff->f2 != - (EET_TEST_FLOAT2 + offset)) return 1;
443    if (stuff->d2 != - (EET_TEST_DOUBLE2 + offset)) return 1;
444
445    if (stuff->f3 != EET_TEST_FLOAT3 + offset) return 1;
446    if (stuff->d3 != EET_TEST_DOUBLE3 + offset) return 1;
447
448    if (stuff->f4 != EET_TEST_FLOAT2 + offset) return 1;
449    if (stuff->d4 != EET_TEST_DOUBLE2 + offset) return 1;
450
451    if (stuff->uc != EET_TEST_CHAR + offset) return 1;
452    if (stuff->us != EET_TEST_SHORT + offset) return 1;
453    if (stuff->ui != (unsigned int) EET_TEST_INT + offset) return 1;
454    if (stuff->ul != EET_TEST_LONG_LONG + offset) return 1;
455
456    if (stuff->varray1_count != 5) return 1;
457    if (stuff->varray2_count != 10) return 1;
458
459    for (i = 0; i < 5; ++i)
460      if (stuff->sarray2[i] != i * 42 + 1 && stuff->varray1[i] != i * 42 + 1)
461        return 1;
462
463    for (i = 0; i < 10; ++i)
464      {
465         _eet_test_basic_check(stuff->sarray1 + i, i);
466         _eet_test_basic_check(stuff->varray2 + i, i);
467      }
468
469    return 0;
470 }
471
472 static Eina_Bool
473 func(__UNUSED__ const Eina_Hash *hash, const void *key, void *data, void *fdata)
474 {
475    int *res = fdata;
476
477    if (strcmp(key, EET_TEST_KEY1) != 0
478        && strcmp(key, EET_TEST_KEY2) != 0) *res = 1;
479    if (_eet_test_ex_check(data, 2)) *res = 1;
480
481    return EINA_TRUE;
482 }
483
484 static Eina_Bool
485 func7(__UNUSED__ const Eina_Hash *hash, __UNUSED__ const void *key, void *data, void *fdata)
486 {
487    int *res = fdata;
488    int *val;
489
490    val = data;
491    if (!val) *res = 1;
492    if (*val != 7) *res = 1;
493
494    return EINA_TRUE;
495 }
496
497 START_TEST(eet_test_data_type_encoding_decoding)
498 {
499    Eet_Data_Descriptor *edd;
500    Eet_Test_Ex_Type *result;
501    void *transfert;
502    Eet_Data_Descriptor_Class eddc;
503    Eet_Test_Ex_Type etbt;
504    int size;
505    int test;
506
507    eet_init();
508
509    _eet_test_ex_set(&etbt, 0);
510    etbt.list = eina_list_prepend(etbt.list, _eet_test_ex_set(NULL, 1));
511    etbt.hash = eina_hash_string_superfast_new(NULL);
512    eina_hash_add(etbt.hash, EET_TEST_KEY1, _eet_test_ex_set(NULL, 2));
513    etbt.ilist = eina_list_prepend(etbt.ilist, &i42);
514    etbt.ihash = eina_hash_string_superfast_new(NULL);
515    eina_hash_add(etbt.ihash, EET_TEST_KEY1, &i7);
516    etbt.slist = eina_list_prepend(NULL, "test");
517    etbt.shash = eina_hash_string_superfast_new(NULL);
518    eina_hash_add(etbt.shash, EET_TEST_KEY1, "test");
519    memset(&etbt.charray, 0, sizeof(etbt.charray));
520    etbt.charray[0] = "test";
521    etbt.charray[5] = "plouf";
522
523    eet_test_setup_eddc(&eddc);
524    eddc.name = "Eet_Test_Ex_Type";
525    eddc.size = sizeof(Eet_Test_Ex_Type);
526
527    edd = eet_data_descriptor_file_new(&eddc);
528    fail_if(!edd);
529
530    _eet_build_ex_descriptor(edd);
531
532    transfert = eet_data_descriptor_encode(edd, &etbt, &size);
533    fail_if(!transfert || size <= 0);
534
535    result = eet_data_descriptor_decode(edd, transfert, size);
536    fail_if(!result);
537
538    fail_if(_eet_test_ex_check(result, 0) != 0);
539    fail_if(_eet_test_ex_check(eina_list_data_get(result->list), 1) != 0);
540    fail_if(eina_list_data_get(result->ilist) == NULL);
541    fail_if(*((int*)eina_list_data_get(result->ilist)) != 42);
542    fail_if(eina_list_data_get(result->slist) == NULL);
543    fail_if(strcmp(eina_list_data_get(result->slist), "test") != 0);
544    fail_if(eina_hash_find(result->shash, EET_TEST_KEY1) == NULL);
545    fail_if(strcmp(eina_hash_find(result->shash, EET_TEST_KEY1), "test") != 0);
546    fail_if(strcmp(result->charray[0], "test") != 0);
547    fail_if(strcmp(result->charray[5], "plouf") != 0);
548
549    test = 0;
550    if (result->hash) eina_hash_foreach(result->hash, func, &test);
551    fail_if(test != 0);
552    if (result->ihash) eina_hash_foreach(result->ihash, func7, &test);
553    fail_if(test != 0);
554
555    eet_shutdown();
556 }
557 END_TEST
558
559 static void
560 append_string(void *data, const char *str)
561 {
562    char **string = data;
563    int length;
564
565    if (!data) return ;
566
567    length = *string ? strlen(*string) : 0;
568    *string = realloc(*string, strlen(str) + length + 1);
569
570    memcpy((*string) + length, str, strlen(str) + 1);
571 }
572
573 START_TEST(eet_test_data_type_dump_undump)
574 {
575    Eet_Data_Descriptor *edd;
576    Eet_Test_Ex_Type *result;
577    Eet_Data_Descriptor_Class eddc;
578    Eet_Test_Ex_Type etbt;
579    char *transfert1;
580    char *transfert2;
581    char *string1;
582    char *string2;
583    int size1;
584    int size2;
585    int test;
586
587    eet_init();
588
589    _eet_test_ex_set(&etbt, 0);
590    etbt.list = eina_list_prepend(etbt.list, _eet_test_ex_set(NULL, 1));
591    etbt.list = eina_list_prepend(etbt.list, _eet_test_ex_set(NULL, 1));
592    etbt.hash = eina_hash_string_superfast_new(NULL);
593    eina_hash_add(etbt.hash, EET_TEST_KEY1, _eet_test_ex_set(NULL, 2));
594    etbt.hash = eina_hash_string_superfast_new(NULL);
595    eina_hash_add(etbt.hash, EET_TEST_KEY2, _eet_test_ex_set(NULL, 2));
596    etbt.ilist = eina_list_prepend(etbt.ilist, &i42);
597    etbt.ilist = eina_list_prepend(etbt.ilist, &i42);
598    etbt.ihash = eina_hash_string_superfast_new(NULL);
599    eina_hash_add(etbt.ihash, EET_TEST_KEY1, &i7);
600    etbt.ihash = eina_hash_string_superfast_new(NULL);
601    eina_hash_add(etbt.ihash, EET_TEST_KEY2, &i7);
602    etbt.slist = eina_list_prepend(NULL, "test");
603    etbt.shash = eina_hash_string_superfast_new(NULL);
604    eina_hash_add(etbt.shash, EET_TEST_KEY1, "test");
605    memset(&etbt.charray, 0, sizeof(etbt.charray));
606    etbt.charray[0] = "test";
607
608    eet_test_setup_eddc(&eddc);
609    eddc.name = "Eet_Test_Ex_Type";
610    eddc.size = sizeof(Eet_Test_Ex_Type);
611
612    edd = eet_data_descriptor_file_new(&eddc);
613    fail_if(!edd);
614
615    _eet_build_ex_descriptor(edd);
616
617    transfert1 = eet_data_descriptor_encode(edd, &etbt, &size1);
618    fail_if(!transfert1 || size1 <= 0);
619
620    string1 = NULL;
621    eet_data_text_dump(transfert1, size1, append_string, &string1);
622    fail_if(!string1);
623
624    transfert2 = eet_data_text_undump(string1, string1 ? strlen(string1) : 0, &size2);
625    fail_if(!transfert2 && size2 <= 0);
626
627    string2 = NULL;
628    eet_data_text_dump(transfert2, size2, append_string, &string2);
629    fail_if(!string2);
630
631    fail_if(strlen(string2) != strlen(string1));
632
633    result = eet_data_descriptor_decode(edd, transfert2, size2);
634    fail_if(!result);
635
636    fail_if(_eet_test_ex_check(result, 0) != 0);
637    fail_if(_eet_test_ex_check(eina_list_data_get(result->list), 1) != 0);
638    fail_if(eina_list_data_get(result->ilist) == NULL);
639    fail_if(*((int*)eina_list_data_get(result->ilist)) != 42);
640    fail_if(eina_list_data_get(result->slist) == NULL);
641    fail_if(strcmp(eina_list_data_get(result->slist), "test") != 0);
642    fail_if(eina_hash_find(result->shash, EET_TEST_KEY1) == NULL);
643    fail_if(strcmp(eina_hash_find(result->shash, EET_TEST_KEY1), "test") != 0);
644    fail_if(strcmp(result->charray[0], "test") != 0);
645
646    test = 0;
647    if (result->hash) eina_hash_foreach(result->hash, func, &test);
648    fail_if(test != 0);
649    if (result->ihash) eina_hash_foreach(result->ihash, func7, &test);
650    fail_if(test != 0);
651
652    eet_shutdown();
653 }
654 END_TEST
655
656 START_TEST(eet_file_simple_write)
657 {
658    const char *buffer = "Here is a string of data to save !";
659    Eet_File *ef;
660    char *test;
661    char *file = strdup("/tmp/eet_suite_testXXXXXX");
662    int size;
663
664    eet_init();
665
666    fail_if(!mktemp(file));
667
668    fail_if(eet_mode_get(NULL) != EET_FILE_MODE_INVALID);
669
670    ef = eet_open(file, EET_FILE_MODE_WRITE);
671    fail_if(!ef);
672
673    fail_if(!eet_write(ef, "keys/tests", buffer, strlen(buffer) + 1, 1));
674
675    fail_if(eet_mode_get(ef) != EET_FILE_MODE_WRITE);
676
677    fail_if(eet_list(ef, "*", &size) != NULL);
678    fail_if(eet_num_entries(ef) != -1);
679
680    eet_close(ef);
681
682    /* Test read of simple file */
683    ef = eet_open(file, EET_FILE_MODE_READ);
684    fail_if(!ef);
685
686    test = eet_read(ef, "keys/tests", &size);
687    fail_if(!test);
688    fail_if(size != (int) strlen(buffer) + 1);
689
690    fail_if(memcmp(test, buffer, strlen(buffer) + 1) != 0);
691
692    fail_if(eet_mode_get(ef) != EET_FILE_MODE_READ);
693    fail_if(eet_num_entries(ef) != 1);
694
695    eet_close(ef);
696
697    /* Test eet cache system */
698    ef = eet_open(file, EET_FILE_MODE_READ);
699    fail_if(!ef);
700
701    test = eet_read(ef, "keys/tests", &size);
702    fail_if(!test);
703    fail_if(size != (int) strlen(buffer) + 1);
704
705    fail_if(memcmp(test, buffer, strlen(buffer) + 1) != 0);
706
707    eet_close(ef);
708
709    fail_if(unlink(file) != 0);
710
711    eet_shutdown();
712 }
713 END_TEST
714
715 START_TEST(eet_file_data_test)
716 {
717    Eet_Data_Descriptor *edd;
718    Eet_Test_Ex_Type *result;
719    Eet_Dictionary *ed;
720    Eet_File *ef;
721    char **list;
722    char *file = strdup("/tmp/eet_suite_testXXXXXX");
723    Eet_Data_Descriptor_Class eddc;
724    Eet_Test_Ex_Type etbt;
725    int size;
726    int test;
727
728    eet_init();
729
730    _eet_test_ex_set(&etbt, 0);
731    etbt.list = eina_list_prepend(etbt.list, _eet_test_ex_set(NULL, 1));
732    etbt.list = eina_list_prepend(etbt.list, _eet_test_ex_set(NULL, 1));
733    etbt.hash = eina_hash_string_superfast_new(NULL);
734    eina_hash_add(etbt.hash, EET_TEST_KEY1, _eet_test_ex_set(NULL, 2));
735    etbt.hash = eina_hash_string_superfast_new(NULL);
736    eina_hash_add(etbt.hash, EET_TEST_KEY2, _eet_test_ex_set(NULL, 2));
737    etbt.ilist = eina_list_prepend(etbt.ilist, &i42);
738    etbt.ilist = eina_list_prepend(etbt.ilist, &i42);
739    etbt.ihash = eina_hash_string_superfast_new(NULL);
740    eina_hash_add(etbt.ihash, EET_TEST_KEY1, &i7);
741    etbt.ihash = eina_hash_string_superfast_new(NULL);
742    eina_hash_add(etbt.ihash, EET_TEST_KEY2, &i7);
743    etbt.slist = eina_list_prepend(NULL, "test");
744    etbt.shash = eina_hash_string_superfast_new(NULL);
745    eina_hash_add(etbt.shash, EET_TEST_KEY1, "test");
746    memset(&etbt.charray, 0, sizeof(etbt.charray));
747    etbt.charray[0] = "test";
748
749    eet_test_setup_eddc(&eddc);
750    eddc.name = "Eet_Test_Ex_Type";
751    eddc.size = sizeof(Eet_Test_Ex_Type);
752
753    edd = eet_data_descriptor_file_new(&eddc);
754    fail_if(!edd);
755
756    _eet_build_ex_descriptor(edd);
757
758    fail_if(!mktemp(file));
759
760    /* Insert an error in etbt. */
761    etbt.i = 0;
762
763    /* Save the encoded data in a file. */
764    ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
765    fail_if(!ef);
766
767    fail_if(!eet_data_write(ef, edd, EET_TEST_FILE_KEY1, &etbt, 0));
768
769    result = eet_data_read(ef, edd, EET_TEST_FILE_KEY1);
770    fail_if(!result);
771
772    fail_if(eet_mode_get(ef) != EET_FILE_MODE_READ_WRITE);
773
774    /* Test string space. */
775    ed = eet_dictionary_get(ef);
776
777    fail_if(!eet_dictionary_string_check(ed, result->str));
778    fail_if(eet_dictionary_string_check(ed, result->istr));
779
780    eet_close(ef);
781
782    /* Attempt to replace etbt by the correct one. */
783    etbt.i = EET_TEST_INT;
784
785    ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
786    fail_if(!ef);
787
788    fail_if(!eet_data_write(ef, edd, EET_TEST_FILE_KEY1, &etbt, 0));
789
790    eet_close(ef);
791
792    /* Read back the data. */
793    ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
794    fail_if(!ef);
795
796    fail_if(!eet_data_write(ef, edd, EET_TEST_FILE_KEY2, &etbt, 0));
797
798    result = eet_data_read(ef, edd, EET_TEST_FILE_KEY1);
799    fail_if(!result);
800
801    /* Test string space. */
802    ed = eet_dictionary_get(ef);
803    fail_if(!ed);
804
805    fail_if(!eet_dictionary_string_check(ed, result->str));
806    fail_if(eet_dictionary_string_check(ed, result->istr));
807
808    /* Test the resulting data. */
809    fail_if(_eet_test_ex_check(result, 0) != 0);
810    fail_if(_eet_test_ex_check(eina_list_data_get(result->list), 1) != 0);
811    fail_if(eina_list_data_get(result->ilist) == NULL);
812    fail_if(*((int*)eina_list_data_get(result->ilist)) != 42);
813    fail_if(eina_list_data_get(result->slist) == NULL);
814    fail_if(strcmp(eina_list_data_get(result->slist), "test") != 0);
815    fail_if(eina_hash_find(result->shash, EET_TEST_KEY1) == NULL);
816    fail_if(strcmp(eina_hash_find(result->shash, EET_TEST_KEY1), "test") != 0);
817    fail_if(strcmp(result->charray[0], "test") != 0);
818
819    test = 0;
820    if (result->hash) eina_hash_foreach(result->hash, func, &test);
821    fail_if(test != 0);
822    if (result->ihash) eina_hash_foreach(result->ihash, func7, &test);
823    fail_if(test != 0);
824
825    list = eet_list(ef, "keys/*", &size);
826    fail_if(eet_num_entries(ef) != 2);
827    fail_if(size != 2);
828    fail_if(!(strcmp(list[0], EET_TEST_FILE_KEY1) == 0 && strcmp(list[1], EET_TEST_FILE_KEY2) == 0)
829            && !(strcmp(list[0], EET_TEST_FILE_KEY2) == 0 && strcmp(list[1], EET_TEST_FILE_KEY1) == 0));
830    free(list);
831
832    fail_if(eet_delete(ef, NULL) != 0);
833    fail_if(eet_delete(NULL, EET_TEST_FILE_KEY1) != 0);
834    fail_if(eet_delete(ef, EET_TEST_FILE_KEY1) == 0);
835
836    list = eet_list(ef, "keys/*", &size);
837    fail_if(size != 1);
838    fail_if(eet_num_entries(ef) != 1);
839
840    /* Test some more wrong case */
841    fail_if(eet_data_read(ef, edd, "plop") != NULL);
842    fail_if(eet_data_read(ef, edd, EET_TEST_FILE_KEY1) != NULL);
843
844    /* Reinsert and reread data */
845    fail_if(!eet_data_write(ef, edd, EET_TEST_FILE_KEY1, &etbt, 0));
846    fail_if(eet_data_read(ef, edd, EET_TEST_FILE_KEY1) == NULL);
847    fail_if(eet_read_direct(ef, EET_TEST_FILE_KEY1, &size) == NULL);
848
849    eet_close(ef);
850
851    fail_if(unlink(file) != 0);
852
853    eet_shutdown();
854 }
855 END_TEST
856
857 START_TEST(eet_file_data_dump_test)
858 {
859    Eet_Data_Descriptor *edd;
860    Eet_Test_Ex_Type *result;
861    Eet_Data_Descriptor_Class eddc;
862    Eet_Test_Ex_Type etbt;
863    Eet_File *ef;
864    char *string1;
865    char *file = strdup("/tmp/eet_suite_testXXXXXX");
866    int test;
867
868    eet_init();
869
870    _eet_test_ex_set(&etbt, 0);
871    etbt.list = eina_list_prepend(etbt.list, _eet_test_ex_set(NULL, 1));
872    etbt.list = eina_list_prepend(etbt.list, _eet_test_ex_set(NULL, 1));
873    etbt.hash = eina_hash_string_superfast_new(NULL);
874    eina_hash_add(etbt.hash, EET_TEST_KEY1, _eet_test_ex_set(NULL, 2));
875    eina_hash_add(etbt.hash, EET_TEST_KEY2, _eet_test_ex_set(NULL, 2));
876    etbt.ilist = eina_list_prepend(etbt.ilist, &i42);
877    etbt.ilist = eina_list_prepend(etbt.ilist, &i42);
878    etbt.ihash = eina_hash_string_superfast_new(NULL);
879    eina_hash_add(etbt.ihash, EET_TEST_KEY1, &i7);
880    eina_hash_add(etbt.ihash, EET_TEST_KEY2, &i7);
881    etbt.slist = eina_list_prepend(NULL, "test");
882    etbt.shash = eina_hash_string_superfast_new(NULL);
883    eina_hash_add(etbt.shash, EET_TEST_KEY1, "test");
884    memset(&etbt.charray, 0, sizeof(etbt.charray));
885    etbt.charray[0] = "test";
886
887    eet_eina_file_data_descriptor_class_set(&eddc, "Eet_Test_Ex_Type", sizeof(Eet_Test_Ex_Type));
888
889    edd = eet_data_descriptor_file_new(&eddc);
890    fail_if(!edd);
891
892    _eet_build_ex_descriptor(edd);
893
894    fail_if(!mktemp(file));
895
896    /* Save the encoded data in a file. */
897    ef = eet_open(file, EET_FILE_MODE_WRITE);
898    fail_if(!ef);
899
900    fail_if(!eet_data_write(ef, edd, EET_TEST_FILE_KEY1, &etbt, 0));
901
902    eet_close(ef);
903
904    /* Use dump/undump in the middle */
905    ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
906    fail_if(!ef);
907
908    string1 = NULL;
909    fail_if(eet_data_dump(ef, EET_TEST_FILE_KEY1, append_string, &string1) != 1);
910    fail_if(eet_delete(ef, EET_TEST_FILE_KEY1) == 0);
911    fail_if(!eet_data_undump(ef, EET_TEST_FILE_KEY1, string1, strlen(string1), 1));
912
913    eet_close(ef);
914
915    /* Test the correctness of the reinsertion. */
916    ef = eet_open(file, EET_FILE_MODE_READ);
917    fail_if(!ef);
918
919    result = eet_data_read(ef, edd, EET_TEST_FILE_KEY1);
920    fail_if(!result);
921
922    eet_close(ef);
923
924    /* Test the resulting data. */
925    fail_if(_eet_test_ex_check(result, 0) != 0);
926    fail_if(_eet_test_ex_check(eina_list_data_get(result->list), 1) != 0);
927    fail_if(eina_list_data_get(result->ilist) == NULL);
928    fail_if(*((int*)eina_list_data_get(result->ilist)) != 42);
929    fail_if(eina_list_data_get(result->slist) == NULL);
930    fail_if(strcmp(eina_list_data_get(result->slist), "test") != 0);
931    fail_if(eina_hash_find(result->shash, EET_TEST_KEY1) == NULL);
932    fail_if(strcmp(eina_hash_find(result->shash, EET_TEST_KEY1), "test") != 0);
933    fail_if(strcmp(result->charray[0], "test") != 0);
934
935    test = 0;
936    if (result->hash) eina_hash_foreach(result->hash, func, &test);
937    fail_if(test != 0);
938    if (result->ihash) eina_hash_foreach(result->ihash, func7, &test);
939    fail_if(test != 0);
940
941    fail_if(unlink(file) != 0);
942
943    eet_shutdown();
944 }
945 END_TEST
946
947 START_TEST(eet_image)
948 {
949    Eet_File *ef;
950    char *file = strdup("/tmp/eet_suite_testXXXXXX");
951    unsigned int *data;
952    int compress;
953    int quality;
954    int result;
955    int lossy;
956    int alpha;
957    unsigned int w;
958    unsigned int h;
959
960    fail_if(!mktemp(file));
961
962    /* Save the encoded data in a file. */
963    ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
964    fail_if(!ef);
965
966    result = eet_data_image_write(ef, EET_TEST_FILE_IMAGE "0", test_noalpha.color,
967                                  test_noalpha.w, test_noalpha.h, test_noalpha.alpha,
968                                  0, 100, 0);
969    fail_if(result == 0);
970
971    result = eet_data_image_write(ef, EET_TEST_FILE_IMAGE "1", test_noalpha.color,
972                                  test_noalpha.w, test_noalpha.h, test_noalpha.alpha,
973                                  5, 100, 0);
974    fail_if(result == 0);
975
976    result = eet_data_image_write(ef, EET_TEST_FILE_IMAGE "2", test_noalpha.color,
977                                  test_noalpha.w, test_noalpha.h, test_noalpha.alpha,
978                                  9, 100, 0);
979    fail_if(result == 0);
980
981    result = eet_data_image_write(ef, EET_TEST_FILE_IMAGE "3", test_noalpha.color,
982                                  test_noalpha.w, test_noalpha.h, test_noalpha.alpha,
983                                  0, 100, 1);
984    fail_if(result == 0);
985
986    result = eet_data_image_write(ef, EET_TEST_FILE_IMAGE "4", test_noalpha.color,
987                                  test_noalpha.w, test_noalpha.h, test_noalpha.alpha,
988                                  0, 60, 1);
989    fail_if(result == 0);
990
991    result = eet_data_image_write(ef, EET_TEST_FILE_IMAGE "5", test_noalpha.color,
992                                  test_noalpha.w, test_noalpha.h, test_noalpha.alpha,
993                                  0, 10, 1);
994    fail_if(result == 0);
995
996    result = eet_data_image_write(ef, EET_TEST_FILE_IMAGE "6", test_noalpha.color,
997                                  test_noalpha.w, test_noalpha.h, test_noalpha.alpha,
998                                  0, 0, 1);
999    fail_if(result == 0);
1000
1001    result = eet_data_image_write(ef, EET_TEST_FILE_IMAGE "7", test_alpha.color,
1002                                  test_alpha.w, test_alpha.h, test_alpha.alpha,
1003                                  9, 100, 0);
1004    fail_if(result == 0);
1005
1006    result = eet_data_image_write(ef, EET_TEST_FILE_IMAGE "8", test_alpha.color,
1007                                  test_alpha.w, test_alpha.h, test_alpha.alpha,
1008                                  0, 80, 1);
1009    fail_if(result == 0);
1010
1011    result = eet_data_image_write(ef, EET_TEST_FILE_IMAGE "9", test_alpha.color,
1012                                  test_alpha.w, test_alpha.h, test_alpha.alpha,
1013                                  0, 100, 1);
1014    fail_if(result == 0);
1015
1016    data = eet_data_image_read(ef, EET_TEST_FILE_IMAGE "2", &w, &h, &alpha, &compress, &quality, &lossy);
1017    fail_if(data == NULL);
1018    fail_if(w != test_noalpha.w);
1019    fail_if(h != test_noalpha.h);
1020    fail_if(alpha != test_noalpha.alpha);
1021    fail_if(compress != 9);
1022    fail_if(lossy != 0);
1023    fail_if(data[0] != test_noalpha.color[0]);
1024    free(data);
1025
1026    result = eet_data_image_header_read(ef, EET_TEST_FILE_IMAGE "2", &w, &h, &alpha, &compress, &quality, &lossy);
1027    fail_if(result == 0);
1028    fail_if(w != test_noalpha.w);
1029    fail_if(h != test_noalpha.h);
1030    fail_if(alpha != test_noalpha.alpha);
1031    fail_if(compress != 9);
1032    fail_if(lossy != 0);
1033
1034    eet_close(ef);
1035
1036    /* Test read of image */
1037    ef = eet_open(file, EET_FILE_MODE_READ);
1038    fail_if(!ef);
1039
1040    result = eet_data_image_header_read(ef, EET_TEST_FILE_IMAGE "0", &w, &h, &alpha, &compress, &quality, &lossy);
1041    fail_if(result == 0);
1042    fail_if(w != test_noalpha.w);
1043    fail_if(h != test_noalpha.h);
1044    fail_if(alpha != test_noalpha.alpha);
1045    fail_if(compress != 0);
1046    fail_if(lossy != 0);
1047
1048    data = malloc(w * h * 4);
1049    fail_if(data == NULL);
1050    result = eet_data_image_read_to_surface(ef, EET_TEST_FILE_IMAGE "0", 4, 4, data, 2, 2, w * 4, &alpha, &compress, &quality, &lossy);
1051    fail_if(result != 1);
1052    fail_if(alpha != test_noalpha.alpha);
1053    fail_if(compress != 0);
1054    fail_if(quality != 100);
1055    fail_if(lossy != 0);
1056    fail_if(data[0] != test_noalpha.color[4 + 4 * w]);
1057    free(data);
1058
1059    data = malloc(w * h * 4);
1060    fail_if(data == NULL);
1061    result = eet_data_image_read_to_surface(ef, EET_TEST_FILE_IMAGE "0", 0, 0, data, w, h, w * 4, &alpha, &compress, &quality, &lossy);
1062    fail_if(result != 1);
1063    fail_if(alpha != test_noalpha.alpha);
1064    fail_if(compress != 0);
1065    fail_if(quality != 100);
1066    fail_if(lossy != 0);
1067    fail_if(data[0] != test_noalpha.color[0]);
1068    free(data);
1069
1070    data = eet_data_image_read(ef, EET_TEST_FILE_IMAGE "1", &w, &h, &alpha, &compress, &quality, &lossy);
1071    fail_if(data == NULL);
1072    fail_if(w != test_noalpha.w);
1073    fail_if(h != test_noalpha.h);
1074    fail_if(alpha != test_noalpha.alpha);
1075    fail_if(compress != 5);
1076    fail_if(quality != 100);
1077    fail_if(lossy != 0);
1078    fail_if(data[0] != test_noalpha.color[0]);
1079    free(data);
1080
1081    data = eet_data_image_read(ef, EET_TEST_FILE_IMAGE "2", &w, &h, &alpha, &compress, &quality, &lossy);
1082    fail_if(data == NULL);
1083    fail_if(w != test_noalpha.w);
1084    fail_if(h != test_noalpha.h);
1085    fail_if(alpha != test_noalpha.alpha);
1086    fail_if(compress != 9);
1087    fail_if(lossy != 0);
1088    fail_if(data[0] != test_noalpha.color[0]);
1089    free(data);
1090
1091    data = eet_data_image_read(ef, EET_TEST_FILE_IMAGE "3", &w, &h, &alpha, &compress, &quality, &lossy);
1092    fail_if(data == NULL);
1093    fail_if(w != test_noalpha.w);
1094    fail_if(h != test_noalpha.h);
1095    fail_if(alpha != test_noalpha.alpha);
1096    fail_if(lossy != 1);
1097    free(data);
1098
1099    data = eet_data_image_read(ef, EET_TEST_FILE_IMAGE "5", &w, &h, &alpha, &compress, &quality, &lossy);
1100    fail_if(data == NULL);
1101    fail_if(w != test_noalpha.w);
1102    fail_if(h != test_noalpha.h);
1103    fail_if(alpha != test_noalpha.alpha);
1104    fail_if(lossy != 1);
1105    free(data);
1106
1107    data = eet_data_image_read(ef, EET_TEST_FILE_IMAGE "6", &w, &h, &alpha, &compress, &quality, &lossy);
1108    fail_if(data == NULL);
1109    fail_if(w != test_noalpha.w);
1110    fail_if(h != test_noalpha.h);
1111    fail_if(alpha != test_noalpha.alpha);
1112    fail_if(lossy != 1);
1113    free(data);
1114
1115    result = eet_data_image_header_read(ef, EET_TEST_FILE_IMAGE "7", &w, &h, &alpha, &compress, &quality, &lossy);
1116    fail_if(result == 0);
1117    fail_if(w != test_alpha.w);
1118    fail_if(h != test_alpha.h);
1119    fail_if(alpha != test_alpha.alpha);
1120    fail_if(compress != 9);
1121    fail_if(lossy != 0);
1122
1123    data = eet_data_image_read(ef, EET_TEST_FILE_IMAGE "7", &w, &h, &alpha, &compress, &quality, &lossy);
1124    fail_if(data == NULL);
1125    fail_if(w != test_alpha.w);
1126    fail_if(h != test_alpha.h);
1127    fail_if(alpha != test_alpha.alpha);
1128    fail_if(compress != 9);
1129    fail_if(lossy != 0);
1130    fail_if(data[0] != test_alpha.color[0]);
1131    free(data);
1132
1133    result = eet_data_image_header_read(ef, EET_TEST_FILE_IMAGE "9", &w, &h, &alpha, &compress, &quality, &lossy);
1134    fail_if(result == 0);
1135    fail_if(w != test_alpha.w);
1136    fail_if(h != test_alpha.h);
1137    fail_if(alpha != test_alpha.alpha);
1138    fail_if(lossy != 1);
1139
1140    data = eet_data_image_read(ef, EET_TEST_FILE_IMAGE "9", &w, &h, &alpha, &compress, &quality, &lossy);
1141    fail_if(data == NULL);
1142    fail_if(w != test_alpha.w);
1143    fail_if(h != test_alpha.h);
1144    fail_if(alpha != test_alpha.alpha);
1145    fail_if(lossy != 1);
1146    free(data);
1147
1148    eet_close(ef);
1149
1150    eet_shutdown();
1151 }
1152 END_TEST
1153
1154 #define IM0 0x00112233
1155 #define IM1 0x44556677
1156 #define IM2 0x8899aabb
1157 #define IM3 0xccddeeff
1158
1159 START_TEST(eet_small_image)
1160 {
1161    char *file = strdup("/tmp/eet_suite_testXXXXXX");
1162    unsigned int image[4];
1163    unsigned int *data;
1164    Eet_File *ef;
1165    unsigned int w;
1166    unsigned int h;
1167    int alpha;
1168    int compression;
1169    int quality;
1170    int lossy;
1171    int result;
1172
1173    image[0] = IM0;
1174    image[1] = IM1;
1175    image[2] = IM2;
1176    image[3] = IM3;
1177
1178    eet_init();
1179
1180    fail_if(!mktemp(file));
1181
1182    ef = eet_open(file, EET_FILE_MODE_WRITE);
1183    fail_if(!ef);
1184
1185    result = eet_data_image_write(ef, "/images/test", image, 2, 2, 1, 9, 100, 0);
1186    fail_if(result == 0);
1187
1188    eet_close(ef);
1189
1190    ef = eet_open(file, EET_FILE_MODE_READ);
1191    fail_if(!ef);
1192
1193    data = (unsigned int*) eet_data_image_read(ef, "/images/test", &w, &h, &alpha, &compression, &quality, &lossy);
1194    fail_if(data == NULL);
1195
1196    eet_close(ef);
1197
1198    fail_if(data[0] != IM0);
1199    fail_if(data[1] != IM1);
1200    fail_if(data[2] != IM2);
1201    fail_if(data[3] != IM3);
1202
1203    free(data);
1204
1205    eet_shutdown();
1206 }
1207 END_TEST
1208
1209 START_TEST(eet_identity_simple)
1210 {
1211    const char *buffer = "Here is a string of data to save !";
1212    const void *tmp;
1213    Eet_File *ef;
1214    Eet_Key *k;
1215    char *test;
1216    char *file = strdup("/tmp/eet_suite_testXXXXXX");
1217    int size;
1218    int fd;
1219
1220    eet_init();
1221
1222    fail_if(!mktemp(file));
1223    fail_if(chdir("src/tests"));
1224
1225    /* Sign an eet file. */
1226    ef = eet_open(file, EET_FILE_MODE_WRITE);
1227    fail_if(!ef);
1228
1229    fail_if(!eet_write(ef, "keys/tests", buffer, strlen(buffer) + 1, 0));
1230
1231    k = eet_identity_open("cert.pem", "key.pem", NULL);
1232    fail_if(!k);
1233
1234    fail_if(eet_identity_set(ef, k) != EET_ERROR_NONE);
1235
1236    eet_close(ef);
1237
1238    /* Open a signed file. */
1239    ef = eet_open(file, EET_FILE_MODE_READ);
1240    fail_if(!ef);
1241
1242    test = eet_read(ef, "keys/tests", &size);
1243    fail_if(!test);
1244    fail_if(size != (int) strlen(buffer) + 1);
1245
1246    fail_if(memcmp(test, buffer, strlen(buffer) + 1) != 0);
1247
1248    tmp = eet_identity_x509(ef, &size);
1249    fail_if(tmp == NULL);
1250
1251    eet_close(ef);
1252
1253    /* As we are changing file contain in less than 1s, this could get unnoticed
1254       by eet cache system. */
1255    eet_clearcache();
1256
1257    /* Corrupting the file. */
1258    fd = open(file, O_WRONLY);
1259    fail_if(fd < 0);
1260
1261    fail_if(lseek(fd, 200, SEEK_SET) != 200);
1262    fail_if(write(fd, "42", 2) != 2);
1263    fail_if(lseek(fd, 50, SEEK_SET) != 50);
1264    fail_if(write(fd, "42", 2) != 2);
1265    fail_if(lseek(fd, 88, SEEK_SET) != 88);
1266    fail_if(write(fd, "42", 2) != 2);
1267
1268    close(fd);
1269
1270    /* Attempt to open a modified file. */
1271    ef = eet_open(file, EET_FILE_MODE_READ);
1272    fail_if(ef);
1273
1274    fail_if(unlink(file) != 0);
1275
1276    eet_shutdown();
1277 }
1278 END_TEST
1279
1280 START_TEST(eet_identity_open_simple)
1281 {
1282    Eet_Key *k = NULL;
1283
1284    eet_init();
1285
1286    fail_if(chdir("src/tests"));
1287
1288    k = eet_identity_open("cert.pem", "key.pem", NULL);
1289    fail_if(!k);
1290
1291    if (k) eet_identity_close(k);
1292
1293    eet_shutdown();
1294 }
1295 END_TEST
1296
1297 START_TEST(eet_identity_open_pkcs8)
1298 {
1299    Eet_Key *k = NULL;
1300
1301    eet_init();
1302
1303    fail_if(chdir("src/tests"));
1304
1305    k = eet_identity_open("cert.pem", "key_enc_none.pem", NULL);
1306    fail_if(!k);
1307
1308    if (k) eet_identity_close(k);
1309
1310    eet_shutdown();
1311 }
1312 END_TEST
1313
1314 static int pass_get(char *pass, int size, __UNUSED__ int rwflags, __UNUSED__ void *u)
1315 {
1316    memset(pass, 0, size);
1317
1318    if (strlen("password") > size)
1319      return 0;
1320    snprintf(pass, size, "%s", "password");
1321    return strlen(pass);
1322 }
1323
1324 static int badpass_get(char *pass, int size, __UNUSED__ int rwflags, __UNUSED__ void *u)
1325 {
1326    memset(pass, 0, size);
1327
1328    if (strlen("bad password") > size)
1329      return 0;
1330    snprintf(pass, size, "%s", "bad password");
1331    return strlen(pass);
1332 }
1333
1334
1335 START_TEST(eet_identity_open_pkcs8_enc)
1336 {
1337    Eet_Key *k = NULL;
1338
1339    eet_init();
1340
1341    fail_if(chdir("src/tests"));
1342
1343    k = eet_identity_open("cert.pem", "key_enc.pem", NULL);
1344    fail_if(k);
1345
1346    if (k) eet_identity_close(k);
1347
1348    k = eet_identity_open("cert.pem", "key_enc.pem", &badpass_get);
1349    fail_if(k);
1350
1351    if (k) eet_identity_close(k);
1352
1353    k = eet_identity_open("cert.pem", "key_enc.pem", &pass_get);
1354    fail_if(!k);
1355
1356    if (k) eet_identity_close(k);
1357
1358    eet_shutdown();
1359 }
1360 END_TEST
1361
1362 START_TEST(eet_cipher_decipher_simple)
1363 {
1364    const char *buffer = "Here is a string of data to save !";
1365    const char *key = "This is a crypto key";
1366    const char *key_bad = "This is another crypto key";
1367    Eet_File *ef;
1368    char *test;
1369    char *file = strdup("/tmp/eet_suite_testXXXXXX");
1370    int size;
1371
1372    eet_init();
1373
1374    fail_if(!mktemp(file));
1375    fail_if(chdir("src/tests"));
1376
1377    /* Crypt an eet file. */
1378    ef = eet_open(file, EET_FILE_MODE_WRITE);
1379    fail_if(!ef);
1380
1381    fail_if(!eet_write_cipher(ef, "keys/tests", buffer, strlen(buffer) + 1, 0, key));
1382
1383    eet_close(ef);
1384
1385    /* Decrypt an eet file. */
1386    ef = eet_open(file, EET_FILE_MODE_READ);
1387    fail_if(!ef);
1388
1389    test = eet_read_cipher(ef, "keys/tests", &size, key);
1390    fail_if(!test);
1391    fail_if(size != (int) strlen(buffer) + 1);
1392
1393    fail_if(memcmp(test, buffer, strlen(buffer) + 1) != 0);
1394
1395    eet_close(ef);
1396
1397    /* Decrypt an eet file. */
1398    ef = eet_open(file, EET_FILE_MODE_READ);
1399    fail_if(!ef);
1400
1401    test = eet_read_cipher(ef, "keys/tests", &size, key_bad);
1402
1403    if (size == (int) strlen(buffer) + 1)
1404      fail_if(memcmp(test, buffer, strlen(buffer) + 1) == 0);
1405
1406    eet_close(ef);
1407
1408    fail_if(unlink(file) != 0);
1409
1410    eet_shutdown();
1411 }
1412 END_TEST
1413
1414 static Eina_Bool open_worker_stop;
1415 static void*
1416 open_close_worker(void* path)
1417 {
1418    while (!open_worker_stop)
1419      {
1420         Eet_File* ef = eet_open((char const*)path, EET_FILE_MODE_READ);
1421         if (ef == NULL)
1422           {
1423              pthread_exit("eet_open() failed");
1424           }
1425         else
1426           {
1427              Eet_Error err_code = eet_close(ef);
1428              if (err_code != EET_ERROR_NONE)
1429                pthread_exit("eet_close() failed");
1430           }
1431      }
1432
1433    pthread_exit(NULL);
1434 }
1435
1436 START_TEST(eet_cache_concurrency)
1437 {
1438    char *file = strdup("/tmp/eet_suite_testXXXXXX");
1439    const char *buffer = "test data";
1440    Eet_File *ef;
1441    void *thread_ret;
1442    unsigned int n;
1443
1444    eet_init();
1445
1446    /* create a file to test with */
1447    fail_if(!mktemp(file));
1448    ef = eet_open(file, EET_FILE_MODE_WRITE);
1449    fail_if(!ef);
1450    fail_if(!eet_write(ef, "keys/tests", buffer, strlen(buffer) + 1, 0));
1451
1452    /* start a thread that repeatedly opens and closes a file */
1453    open_worker_stop = 0;
1454    pthread_t thread;
1455    pthread_create(&thread, NULL, open_close_worker, file);
1456
1457    /* clear the cache repeatedly in this thread */
1458    for (n = 0; n < 50000; ++n)
1459      {
1460         eet_clearcache();
1461      }
1462
1463    /* join the other thread, and fail if it returned an error message */
1464    open_worker_stop = 1;
1465    fail_if(pthread_join(thread, &thread_ret) != 0);
1466    fail_unless(thread_ret == NULL, (char const*)thread_ret);
1467
1468    fail_if(unlink(file) != 0);
1469    eet_shutdown();
1470 }
1471 END_TEST
1472
1473 typedef struct _Eet_Connection_Data Eet_Connection_Data;
1474 struct _Eet_Connection_Data
1475 {
1476    Eet_Connection *conn;
1477    Eet_Data_Descriptor *edd;
1478    Eina_Bool test;
1479 };
1480
1481 static Eina_Bool
1482 _eet_connection_read(const void *eet_data, size_t size, void *user_data)
1483 {
1484    Eet_Connection_Data *dt = user_data;
1485    Eet_Test_Ex_Type *result;
1486    Eet_Node *node;
1487    int test;
1488
1489    result = eet_data_descriptor_decode(dt->edd, eet_data, size);
1490    node = eet_data_node_decode_cipher(eet_data, NULL, size);
1491
1492    /* Test the resulting data. */
1493    fail_if(!node);
1494    fail_if(_eet_test_ex_check(result, 0) != 0);
1495    fail_if(_eet_test_ex_check(eina_list_data_get(result->list), 1) != 0);
1496    fail_if(eina_list_data_get(result->ilist) == NULL);
1497    fail_if(*((int*)eina_list_data_get(result->ilist)) != 42);
1498    fail_if(eina_list_data_get(result->slist) == NULL);
1499    fail_if(strcmp(eina_list_data_get(result->slist), "test") != 0);
1500    fail_if(eina_hash_find(result->shash, EET_TEST_KEY1) == NULL);
1501    fail_if(strcmp(eina_hash_find(result->shash, EET_TEST_KEY1), "test") != 0);
1502    fail_if(strcmp(result->charray[0], "test") != 0);
1503
1504    test = 0;
1505    if (result->hash) eina_hash_foreach(result->hash, func, &test);
1506    fail_if(test != 0);
1507    if (result->ihash) eina_hash_foreach(result->ihash, func7, &test);
1508    fail_if(test != 0);
1509
1510    if (!dt->test)
1511      {
1512         dt->test = EINA_TRUE;
1513         fail_if(!eet_connection_node_send(dt->conn, node, NULL));
1514      }
1515
1516    return EINA_TRUE;
1517 }
1518
1519 static Eina_Bool
1520 _eet_connection_write(const void *data, size_t size, void *user_data)
1521 {
1522    Eet_Connection_Data *dt = user_data;
1523    int still;
1524
1525    if (!dt->test)
1526      {
1527         int step = size / 3;
1528
1529         eet_connection_received(dt->conn, data, step);
1530         eet_connection_received(dt->conn, (char*) data + step, step);
1531         size -= 2 * step;
1532         still = eet_connection_received(dt->conn, (char*) data + 2 * step, size);
1533      }
1534    else
1535      still = eet_connection_received(dt->conn, data, size);
1536    fail_if(still);
1537
1538    return EINA_TRUE;
1539 }
1540
1541 START_TEST(eet_connection_check)
1542 {
1543    Eet_Connection *conn;
1544    Eet_Data_Descriptor *edd;
1545    Eet_Data_Descriptor_Class eddc;
1546    Eet_Connection_Data ecd;
1547    Eet_Test_Ex_Type etbt;
1548    Eina_Bool on_going;
1549
1550    eet_init();
1551
1552    _eet_test_ex_set(&etbt, 0);
1553    etbt.list = eina_list_prepend(etbt.list, _eet_test_ex_set(NULL, 1));
1554    etbt.list = eina_list_prepend(etbt.list, _eet_test_ex_set(NULL, 1));
1555    etbt.hash = eina_hash_string_superfast_new(NULL);
1556    eina_hash_add(etbt.hash, EET_TEST_KEY1, _eet_test_ex_set(NULL, 2));
1557    eina_hash_add(etbt.hash, EET_TEST_KEY2, _eet_test_ex_set(NULL, 2));
1558    etbt.ilist = eina_list_prepend(etbt.ilist, &i42);
1559    etbt.ilist = eina_list_prepend(etbt.ilist, &i42);
1560    etbt.ihash = eina_hash_string_superfast_new(NULL);
1561    eina_hash_add(etbt.ihash, EET_TEST_KEY1, &i7);
1562    eina_hash_add(etbt.ihash, EET_TEST_KEY2, &i7);
1563    etbt.slist = eina_list_prepend(NULL, "test");
1564    etbt.shash = eina_hash_string_superfast_new(NULL);
1565    eina_hash_add(etbt.shash, EET_TEST_KEY1, "test");
1566    memset(&etbt.charray, 0, sizeof(etbt.charray));
1567    etbt.charray[0] = "test";
1568
1569    eet_eina_file_data_descriptor_class_set(&eddc, "Eet_Test_Ex_Type", sizeof(Eet_Test_Ex_Type));
1570
1571    edd = eet_data_descriptor_file_new(&eddc);
1572    fail_if(!edd);
1573
1574    _eet_build_ex_descriptor(edd);
1575
1576    /* Create a connection. */
1577    conn = eet_connection_new(_eet_connection_read, _eet_connection_write, &ecd);
1578    fail_if(!conn);
1579
1580    /* Init context. */
1581    ecd.test = EINA_FALSE;
1582    ecd.conn = conn;
1583    ecd.edd = edd;
1584
1585    /* Test the connection. */
1586    fail_if(!eet_connection_send(conn, edd, &etbt, NULL));
1587
1588    fail_if(!ecd.test);
1589
1590    fail_if(!eet_connection_close(conn, &on_going));
1591
1592    fail_if(on_going);
1593
1594    eet_shutdown();
1595 }
1596 END_TEST
1597
1598 Suite *
1599 eet_suite(void)
1600 {
1601    Suite *s;
1602    TCase *tc;
1603
1604    s = suite_create("Eet");
1605
1606    tc = tcase_create("Eet_Init");
1607    tcase_add_test(tc, eet_test_init);
1608    suite_add_tcase(s, tc);
1609
1610    tc = tcase_create("Eet Data Encoding/Decoding");
1611    tcase_add_test(tc, eet_test_basic_data_type_encoding_decoding);
1612    tcase_add_test(tc, eet_test_data_type_encoding_decoding);
1613    tcase_add_test(tc, eet_test_data_type_dump_undump);
1614    suite_add_tcase(s, tc);
1615
1616    tc = tcase_create("Eet File");
1617    tcase_add_test(tc, eet_file_simple_write);
1618    tcase_add_test(tc, eet_file_data_test);
1619    tcase_add_test(tc, eet_file_data_dump_test);
1620    suite_add_tcase(s, tc);
1621
1622    tc = tcase_create("Eet Image");
1623    tcase_add_test(tc, eet_image);
1624    tcase_add_test(tc, eet_small_image);
1625    suite_add_tcase(s, tc);
1626
1627 #ifdef HAVE_SIGNATURE
1628    tc = tcase_create("Eet Identity");
1629    tcase_add_test(tc, eet_identity_simple);
1630    tcase_add_test(tc, eet_identity_open_simple);
1631    tcase_add_test(tc, eet_identity_open_pkcs8);
1632    tcase_add_test(tc, eet_identity_open_pkcs8_enc);
1633    suite_add_tcase(s, tc);
1634 #endif
1635
1636 #ifdef HAVE_CIPHER
1637    tc = tcase_create("Eet Cipher");
1638    tcase_add_test(tc, eet_cipher_decipher_simple);
1639    suite_add_tcase(s, tc);
1640 #endif
1641
1642    tc = tcase_create("Eet Cache");
1643    tcase_add_test(tc, eet_cache_concurrency);
1644    suite_add_tcase(s, tc);
1645
1646    tc = tcase_create("Eet Connection");
1647    tcase_add_test(tc, eet_connection_check);
1648    suite_add_tcase(s, tc);
1649
1650    return s;
1651 }
1652
1653 int
1654 main(void)
1655 {
1656    Suite   *s;
1657    SRunner *sr;
1658    int      failed_count;
1659
1660    s = eet_suite();
1661    sr = srunner_create(s);
1662    srunner_run_all(sr, CK_NORMAL);
1663    failed_count = srunner_ntests_failed(sr);
1664    srunner_free(sr);
1665
1666    return (failed_count == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
1667 }