efl

Форк
0
/
eio_test_eet.c 
212 строк · 4.8 Кб
1
#ifdef HAVE_CONFIG_H
2
# include <config.h>
3
#endif
4

5
#include <stdio.h>
6
#include <unistd.h>
7
#include <string.h>
8
#include <sys/stat.h>
9
#include <fcntl.h>
10

11
#include <Ecore.h>
12
#include <Eio.h>
13
#include <Eet.h>
14

15
#include "eio_suite.h"
16

17
Eet_File *ee;
18

19
static void
20
_open_cb(void *data EINA_UNUSED, Eio_File *handler EINA_UNUSED, Eet_File *ef)
21
{
22
   ee = ef;
23
   ecore_main_loop_quit();
24
}
25

26
static void
27
_done_cb(void *data EINA_UNUSED, Eio_File *handler EINA_UNUSED)
28
{
29
   ecore_main_loop_quit();
30
}
31

32
static void
33
_write_done_cb(void *data EINA_UNUSED, Eio_File *handler EINA_UNUSED, int i)
34
{
35
   fail_if (i <= 0);
36
   ecore_main_loop_quit();
37
}
38

39
static void
40
_read_done_cb(void *data, Eio_File *handler EINA_UNUSED, void *read_data,
41
              unsigned int size)
42
{
43
   char *str = data;
44
   char *read_str = read_data;
45

46
   fail_if(!read_str);
47
   fail_if(size != strlen(str) + 1);
48
   fail_if(memcmp(str, read_str, strlen(str) + 1) != 0);
49

50
   ecore_main_loop_quit();
51
}
52

53
static void
54
_error_cb(void *data EINA_UNUSED, Eio_File *handler EINA_UNUSED, int error)
55
{
56
   fprintf(stderr, "Error:%s\n", strerror(error));
57
   fail();
58
   ecore_main_loop_quit();
59
}
60

61
static void
62
_eet_error_cb(void *data EINA_UNUSED, Eio_File *handler EINA_UNUSED, Eet_Error err EINA_UNUSED)
63
{
64
   fail();
65
   ecore_main_loop_quit();
66
}
67

68
EFL_START_TEST(eio_test_eet_cipher_decipher)
69
{
70
   int ret;
71
   char *data = "This is the data to save in file";
72
   const char *key = "This is a secret key";
73
   Eio_File *ef;
74
   Eina_Tmpstr *file;
75

76
   eet_init();
77

78
   ret = eina_file_mkstemp("eio_eet_example_XXXXXX", &file);
79
   fail_if(ret == -1);
80

81
   ef = eio_eet_open(file, EET_FILE_MODE_WRITE, _open_cb, _error_cb, NULL);
82
   ecore_main_loop_begin();
83
   fail_if(!ef);
84

85
   ef = eio_eet_write_cipher(ee, "keys/tests", data, strlen(data) + 1, 0,
86
                             key, _write_done_cb, _error_cb, NULL);
87
   ecore_main_loop_begin();
88
   fail_if(!ef);
89

90
   ef = eio_eet_close(ee, _done_cb, _eet_error_cb, NULL);
91
   ecore_main_loop_begin();
92
   fail_if(!ef);
93

94
   ef = eio_eet_open(file, EET_FILE_MODE_READ, _open_cb, _error_cb, NULL);
95
   ecore_main_loop_begin();
96
   fail_if(!ef);
97

98
   ef = eio_eet_read_cipher(ee, "keys/tests", key, _read_done_cb,
99
                            _error_cb, data);
100
   ecore_main_loop_begin();
101
   fail_if(!ef);
102

103
   ef = eio_eet_close(ee, _done_cb, _eet_error_cb, NULL);
104
   ecore_main_loop_begin();
105
   fail_if(!ef);
106

107
   unlink(file);
108
   eina_tmpstr_del(file);
109
   eet_shutdown();
110
}
111
EFL_END_TEST
112

113
typedef struct
114
{
115
   unsigned int id;
116
   const char *name;
117
} Test_Struct;
118

119
static Eet_Data_Descriptor *_test_struct_descriptor;
120

121
static void
122
_test_struct_descriptor_init(void)
123
{
124
   Eet_Data_Descriptor_Class eddc;
125

126
   EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, Test_Struct);
127
   _test_struct_descriptor = eet_data_descriptor_stream_new(&eddc);
128

129
   EET_DATA_DESCRIPTOR_ADD_BASIC(_test_struct_descriptor, Test_Struct,
130
                                 "id", id, EET_T_INT);
131
   EET_DATA_DESCRIPTOR_ADD_BASIC(_test_struct_descriptor, Test_Struct,
132
                                 "name", name, EET_T_STRING);
133
}
134

135
static Test_Struct *
136
_test_struct_new(void)
137
{
138
   Test_Struct *tc = calloc(1, sizeof(Test_Struct));
139
   fail_if(!tc);
140

141
   tc->id = 1234;
142
   tc->name = "eio_eet_test";
143

144
   return tc;
145
}
146

147
static void
148
_data_read_done_cb(void *data, Eio_File *handler EINA_UNUSED, void *decoded)
149
{
150
   Test_Struct *res = decoded;
151
   Test_Struct *orig = data;
152

153
   fail_if(!res);
154
   fail_if(res->id != orig->id);
155
   fail_if(strcmp(res->name, orig->name) != 0);
156
   ecore_main_loop_quit();
157
}
158

159
EFL_START_TEST(eio_test_eet_data_cipher_decipher)
160
{
161
   int ret;
162
   const char *key = "This is a secret key";
163
   Eio_File *ef;
164
   Test_Struct *tc;
165
   Eina_Tmpstr *file;
166

167
   eet_init();
168

169
   _test_struct_descriptor_init();
170
   tc = _test_struct_new();
171

172
   ret = eina_file_mkstemp("eio_eet_example_XXXXXX", &file);
173
   fail_if(ret == -1);
174

175
   ef = eio_eet_open(file, EET_FILE_MODE_WRITE, _open_cb, _error_cb, NULL);
176
   ecore_main_loop_begin();
177
   fail_if(!ef);
178

179
   ef = eio_eet_data_write_cipher(ee, _test_struct_descriptor, "test",
180
                                  key, tc, 0, _write_done_cb, _error_cb, NULL);
181
   ecore_main_loop_begin();
182
   fail_if(!ef);
183

184
   ef = eio_eet_close(ee, _done_cb, _eet_error_cb, NULL);
185
   ecore_main_loop_begin();
186
   fail_if(!ef);
187

188
   ef = eio_eet_open(file, EET_FILE_MODE_READ, _open_cb, _error_cb, NULL);
189
   ecore_main_loop_begin();
190
   fail_if(!ef);
191

192
   ef = eio_eet_data_read_cipher(ee, _test_struct_descriptor, "test",
193
                                 key, _data_read_done_cb, _error_cb, tc);
194
   ecore_main_loop_begin();
195
   fail_if(!ef);
196

197
   ef = eio_eet_close(ee, _done_cb, _eet_error_cb, NULL);
198
   ecore_main_loop_begin();
199
   fail_if(!ef);
200

201
   unlink(file);
202
   eina_tmpstr_del(file);
203
   eet_shutdown();
204
}
205
EFL_END_TEST
206

207
void
208
eio_test_eet(TCase *tc)
209
{
210
   tcase_add_test(tc, eio_test_eet_cipher_decipher);
211
   tcase_add_test(tc, eio_test_eet_data_cipher_decipher);
212
}
213

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.