summaryrefslogtreecommitdiff
path: root/src/tests/ecore_con/ecore_con_test_ecore_con_eet.c
blob: edf5185fc2478b146673f2e4dfd5b08d49104240 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "ecore_con_suite.h"

#include <Eet.h>
#include <Ecore_Con_Eet.h>

#define TEST_STREAM "test_stream"
#define SRV_MSG "Server Message"
#define CLI_MSG "Client Message"

static Eet_Data_Descriptor *_ece_test_descriptor;
static char eet_data[] = "test_data";

typedef struct
{
   int id;
   char *message;
}ECE_Test;

static void
_ece_test_descriptor_init(void)
{
   Eet_Data_Descriptor_Class eddc;

   EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, ECE_Test);
   _ece_test_descriptor = eet_data_descriptor_stream_new(&eddc);

#define ECE_TEST_ADD_BASIC(member, eet_type) \
  EET_DATA_DESCRIPTOR_ADD_BASIC             \
    (_ece_test_descriptor, ECE_Test, # member, member, eet_type)

   ECE_TEST_ADD_BASIC(id, EET_T_INT);
   ECE_TEST_ADD_BASIC(message, EET_T_STRING);
#undef ECE_TEST_ADD_BASIC
}

static void
_ece_test_descriptor_shutdown(void)
{
   eet_data_descriptor_free(_ece_test_descriptor);
}

static Eina_Bool
_eet_client_discnct_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply EINA_UNUSED, Ecore_Con_Client *conn EINA_UNUSED)
{
   ecore_main_loop_quit();

   return EINA_FALSE;
}

static Eina_Bool
_eet_client_cnct_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply, Ecore_Con_Client *conn EINA_UNUSED)
{
   const char *ec_eet_data;
   Ecore_Con_Eet *ec_eet = ecore_con_eet_reply(reply);
   fail_if(!ec_eet);

   ec_eet_data = ecore_con_eet_data_get(ec_eet);
   fail_if(strcmp(ec_eet_data, eet_data));

   return EINA_TRUE;
}

static void
_eet_data_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply, const char *protocol_name, void *value)
{
   char toSend[] = SRV_MSG;
   ECE_Test *received = value;

   fprintf(stderr, "id: %d\n message: %s\n",
           received->id, received->message);
   fail_if(strcmp(received->message, CLI_MSG));
   fail_if(received->id != 1);

   ecore_con_eet_raw_send(reply, protocol_name, "message", toSend,
                          strlen(toSend));
}

static Eina_Bool
_eet_svr_cnct_cb(void *data, Ecore_Con_Reply *reply, Ecore_Con_Client *conn EINA_UNUSED)
{
   int *i = data;
   ECE_Test toSend = {1, CLI_MSG};
   char rawMsg[] = CLI_MSG;

   if (*i)
     ecore_con_eet_send(reply, TEST_STREAM, &toSend);
   else
     ecore_con_eet_raw_send(reply, TEST_STREAM, "message", rawMsg,
                            strlen(rawMsg) + 1);

   return EINA_TRUE;
}

static void
_eet_raw_data_cb(void *data, Ecore_Con_Reply *reply, const char *protocol_name EINA_UNUSED, const char *section, void *value, size_t length)
{
   Ecore_Con_Server *cl = data;
   Ecore_Con_Eet *ece_cl = ecore_con_eet_reply(reply);

   fprintf(stderr, "Section: %s\n", section);
   fprintf(stderr, "Value: %s\n", (char *) value);
   fail_if(strncmp((char *) value, SRV_MSG, length));

   ecore_con_eet_raw_data_callback_del(ece_cl, TEST_STREAM);
   ecore_con_eet_server_connect_callback_del(ece_cl, _eet_svr_cnct_cb, NULL);

   ecore_con_eet_server_free(ece_cl);
   ecore_con_server_del(cl);
}

START_TEST(ecore_con_test_ecore_con_eet_svr_cl)
{
   Ecore_Con_Eet *ece_svr, *ece_cl;
   Ecore_Con_Server *svr, *cl;
   int *cb_data = (int *) malloc(sizeof(int));
   *cb_data = 1;

   eina_init();
   eet_init();
   ecore_con_init();
   _ece_test_descriptor_init();

   svr = ecore_con_server_add(ECORE_CON_REMOTE_TCP, "127.0.0.1",
                              8080, NULL);
   fail_if(!svr);

   ece_svr = ecore_con_eet_server_new(svr);
   fail_if(!ece_svr);

   ecore_con_eet_data_set(ece_svr, eet_data);

   ecore_con_eet_register(ece_svr, TEST_STREAM, _ece_test_descriptor);
   ecore_con_eet_data_callback_add(ece_svr, TEST_STREAM, _eet_data_cb,
                                   NULL);
   ecore_con_eet_client_connect_callback_add(ece_svr, _eet_client_cnct_cb,
                                             NULL);
   ecore_con_eet_client_disconnect_callback_add(ece_svr,
                                                _eet_client_discnct_cb, NULL);

   cl = ecore_con_server_connect(ECORE_CON_REMOTE_TCP, "127.0.0.1",
                                 8080, NULL);
   fail_if(!cl);

   ece_cl = ecore_con_eet_client_new(cl);
   fail_if(!ece_cl);

   ecore_con_eet_register(ece_cl, TEST_STREAM, _ece_test_descriptor);
   ecore_con_eet_raw_data_callback_add(ece_cl, TEST_STREAM,
                                       _eet_raw_data_cb, cl);
   ecore_con_eet_server_connect_callback_add(ece_cl, _eet_svr_cnct_cb,
                                             cb_data);

   ecore_main_loop_begin();


   ecore_con_eet_data_callback_del(ece_svr, TEST_STREAM);
   ecore_con_eet_client_connect_callback_del(ece_svr, _eet_client_cnct_cb,
                                             NULL);
   ecore_con_eet_client_disconnect_callback_del(ece_svr,
                                                _eet_client_discnct_cb, NULL);

   free(cb_data);
   ecore_con_eet_server_free(ece_svr);
   _ece_test_descriptor_shutdown();
   ecore_con_server_del(svr);
   ecore_con_shutdown();
   eet_shutdown();
   eina_shutdown();
}
END_TEST

static Eina_Bool
_eet_svr_discnct_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply EINA_UNUSED, Ecore_Con_Client *conn EINA_UNUSED)
{
   ecore_main_loop_quit();
   return EINA_FALSE;
}

static void
_eet_svr_raw_data_cb(void *data, Ecore_Con_Reply *reply, const char *protocol_name EINA_UNUSED, const char *section, void *value, size_t length)
{
   Ecore_Con_Server *svr = data;
   Ecore_Con_Eet *ece_svr = ecore_con_eet_reply(reply);

   fprintf(stderr, "Section: %s\n", section);
   fprintf(stderr, "Value: %s\n", (char *) value);
   fail_if(strncmp((char *) value, CLI_MSG, length));

   ecore_con_eet_raw_data_callback_del(ece_svr, TEST_STREAM);

   ecore_con_eet_server_free(ece_svr);
   ecore_con_server_del(svr);
}

START_TEST(ecore_con_test_ecore_con_eet_svr_del)
{
   Ecore_Con_Eet *ece_svr, *ece_cl;
   Ecore_Con_Server *svr, *cl;
   int *cb_data = (int *) malloc(sizeof(int));
   *cb_data = 0;

   eina_init();
   eet_init();
   ecore_con_init();
   _ece_test_descriptor_init();

   svr = ecore_con_server_add(ECORE_CON_REMOTE_TCP, "127.0.0.1",
                              8080, NULL);
   fail_if(!svr);

   ece_svr = ecore_con_eet_server_new(svr);
   fail_if(!ece_svr);

   ecore_con_eet_data_set(ece_svr, eet_data);

   ecore_con_eet_register(ece_svr, TEST_STREAM, _ece_test_descriptor);
   ecore_con_eet_raw_data_callback_add(ece_svr, TEST_STREAM,
                                  _eet_svr_raw_data_cb, svr);

   cl = ecore_con_server_connect(ECORE_CON_REMOTE_TCP, "127.0.0.1",
                                 8080, NULL);
   fail_if(!cl);

   ece_cl = ecore_con_eet_client_new(cl);
   fail_if(!ece_cl);

   ecore_con_eet_register(ece_cl, TEST_STREAM, _ece_test_descriptor);
   ecore_con_eet_server_connect_callback_add(ece_cl, _eet_svr_cnct_cb,
                                             cb_data);
   ecore_con_eet_server_disconnect_callback_add(ece_cl,
                                                _eet_svr_discnct_cb, NULL);

   ecore_main_loop_begin();


   ecore_con_eet_server_connect_callback_del(ece_cl, _eet_client_cnct_cb,
                                             NULL);
   ecore_con_eet_server_disconnect_callback_del(ece_cl,
                                                _eet_client_discnct_cb, NULL);

   free(cb_data);
   ecore_con_eet_server_free(ece_cl);
   _ece_test_descriptor_shutdown();
   ecore_con_server_del(cl);
   ecore_con_shutdown();
   eet_shutdown();
   eina_shutdown();
}
END_TEST

void ecore_con_test_ecore_con_eet(TCase *tc)
{
   tcase_add_test(tc, ecore_con_test_ecore_con_eet_svr_cl);
   tcase_add_test(tc, ecore_con_test_ecore_con_eet_svr_del);
}