diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/ecore/src/tests/ecore_test_ecore_con.c | 258 |
1 files changed, 258 insertions, 0 deletions
diff --git a/libraries/ecore/src/tests/ecore_test_ecore_con.c b/libraries/ecore/src/tests/ecore_test_ecore_con.c new file mode 100644 index 0000000..45c1f69 --- /dev/null +++ b/libraries/ecore/src/tests/ecore_test_ecore_con.c | |||
@@ -0,0 +1,258 @@ | |||
1 | #ifdef HAVE_CONFIG_H | ||
2 | # include <config.h> | ||
3 | #endif | ||
4 | |||
5 | #include <Ecore_Con.h> | ||
6 | |||
7 | #include "ecore_suite.h" | ||
8 | |||
9 | #include <stdio.h> | ||
10 | #include <Ecore.h> | ||
11 | #include <Ecore_Con.h> | ||
12 | |||
13 | Eina_Bool | ||
14 | _add(void *data, int type __UNUSED__, void *ev) | ||
15 | { | ||
16 | fail_if (type != ECORE_CON_EVENT_CLIENT_ADD && | ||
17 | type != ECORE_CON_EVENT_SERVER_ADD); | ||
18 | |||
19 | /* Server */ | ||
20 | if (type == ECORE_CON_EVENT_CLIENT_ADD) | ||
21 | { | ||
22 | Ecore_Con_Event_Client_Add *event = ev; | ||
23 | |||
24 | fail_if (data != (void *) 1); | ||
25 | fail_if (!event->client); | ||
26 | |||
27 | printf("Client with ip %s, port %d, connected = %d!\n", | ||
28 | ecore_con_client_ip_get(event->client), | ||
29 | ecore_con_client_port_get(event->client), | ||
30 | ecore_con_client_connected_get(event->client)); | ||
31 | |||
32 | ecore_con_client_timeout_set(event->client, 10); | ||
33 | |||
34 | } | ||
35 | else if (type == ECORE_CON_EVENT_SERVER_ADD) | ||
36 | { | ||
37 | Ecore_Con_Event_Server_Add *event = ev; | ||
38 | const char ping[] = "PING"; | ||
39 | int ret; | ||
40 | |||
41 | fail_if (data != (void *) 2); | ||
42 | fail_if (!event->server); | ||
43 | printf("Server with ip %s, name %s, port %d, connected = %d!\n", | ||
44 | ecore_con_server_ip_get(event->server), | ||
45 | ecore_con_server_name_get(event->server), | ||
46 | ecore_con_server_port_get(event->server), | ||
47 | ecore_con_server_connected_get(event->server)); | ||
48 | ret = ecore_con_server_send(event->server, ping, sizeof(ping)); | ||
49 | fail_if (ret != sizeof(ping)); | ||
50 | ecore_con_server_flush(event->server); | ||
51 | } | ||
52 | |||
53 | return ECORE_CALLBACK_RENEW; | ||
54 | } | ||
55 | |||
56 | Eina_Bool | ||
57 | _del(void *data , int type __UNUSED__, void *ev) | ||
58 | { | ||
59 | fail_if (type != ECORE_CON_EVENT_CLIENT_DEL && | ||
60 | type != ECORE_CON_EVENT_SERVER_DEL); | ||
61 | |||
62 | /* Server */ | ||
63 | if (type == ECORE_CON_EVENT_CLIENT_DEL) | ||
64 | { | ||
65 | Ecore_Con_Event_Client_Del *event = ev; | ||
66 | |||
67 | fail_if (data != (void *) 1); | ||
68 | fail_if (!event->client); | ||
69 | |||
70 | printf("Lost client with ip %s!\n", ecore_con_client_ip_get(event->client)); | ||
71 | printf("Client was connected for %0.3f seconds.\n", | ||
72 | ecore_con_client_uptime_get(event->client)); | ||
73 | |||
74 | ecore_con_client_del(event->client); | ||
75 | } | ||
76 | else if (type == ECORE_CON_EVENT_SERVER_DEL) | ||
77 | { | ||
78 | Ecore_Con_Event_Server_Del *event = ev; | ||
79 | |||
80 | fail_if (!event->server); | ||
81 | |||
82 | fail_if (data != (void *) 2); | ||
83 | |||
84 | printf("Lost server with ip %s!\n", ecore_con_server_ip_get(event->server)); | ||
85 | |||
86 | ecore_con_server_del(event->server); | ||
87 | } | ||
88 | fail (); | ||
89 | |||
90 | return ECORE_CALLBACK_RENEW; | ||
91 | } | ||
92 | |||
93 | Eina_Bool | ||
94 | _data(void *data, int type __UNUSED__, void *ev) | ||
95 | { | ||
96 | |||
97 | fail_if (type != ECORE_CON_EVENT_CLIENT_DATA && | ||
98 | type != ECORE_CON_EVENT_SERVER_DATA); | ||
99 | |||
100 | /* Server */ | ||
101 | if (type == ECORE_CON_EVENT_CLIENT_DATA) | ||
102 | { | ||
103 | Ecore_Con_Event_Client_Data *event = ev; | ||
104 | const char pong[] = "PONG"; | ||
105 | int ret; | ||
106 | |||
107 | char fmt[128]; | ||
108 | fail_if (data != (void *) 1); | ||
109 | |||
110 | snprintf(fmt, sizeof(fmt), | ||
111 | "Received %i bytes from client %s port %d:\n" | ||
112 | ">>>>>\n" | ||
113 | "%%.%is\n" | ||
114 | ">>>>>\n", | ||
115 | event->size, ecore_con_client_ip_get(event->client), | ||
116 | ecore_con_client_port_get(event->client), event->size); | ||
117 | |||
118 | printf(fmt, event->data); | ||
119 | fail_if (event->size != sizeof("PING")); | ||
120 | fail_if (memcmp (event->data, "PING", sizeof("PING")) != 0); | ||
121 | |||
122 | ret = ecore_con_client_send(event->client, pong, sizeof(pong)); | ||
123 | fail_if (ret != sizeof(pong)); | ||
124 | ecore_con_client_flush(event->client); | ||
125 | } | ||
126 | else if (type == ECORE_CON_EVENT_SERVER_DATA) | ||
127 | { | ||
128 | Ecore_Con_Event_Server_Data *event = ev; | ||
129 | char fmt[128]; | ||
130 | |||
131 | fail_if (data != (void *) 2); | ||
132 | |||
133 | snprintf(fmt, sizeof(fmt), | ||
134 | "Received %i bytes from server:\n" | ||
135 | ">>>>>\n" | ||
136 | "%%.%is\n" | ||
137 | ">>>>>\n", | ||
138 | event->size, event->size); | ||
139 | |||
140 | printf(fmt, event->data); | ||
141 | fail_if (event->size != sizeof("PONG")); | ||
142 | fail_if (memcmp (event->data, "PONG", sizeof("PONG")) != 0); | ||
143 | ecore_main_loop_quit(); | ||
144 | } | ||
145 | |||
146 | return ECORE_CALLBACK_RENEW; | ||
147 | } | ||
148 | |||
149 | START_TEST(ecore_test_ecore_con_server) | ||
150 | { | ||
151 | Ecore_Con_Server *server; | ||
152 | Ecore_Con_Server *client; | ||
153 | Ecore_Con_Client *cl; | ||
154 | const Eina_List *clients, *l; | ||
155 | Ecore_Event_Handler *handlers[6]; | ||
156 | void *server_data = malloc (1); | ||
157 | void *client_data = malloc (1); | ||
158 | int ret; | ||
159 | void *del_ret; | ||
160 | |||
161 | ret = eina_init(); | ||
162 | fail_if(ret != 1); | ||
163 | ret = ecore_init(); | ||
164 | fail_if(ret != 1); | ||
165 | ret = ecore_con_init(); | ||
166 | fail_if(ret != 1); | ||
167 | |||
168 | handlers[0] = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD, | ||
169 | _add, (void *) 1); | ||
170 | fail_if(handlers[0] == NULL); | ||
171 | handlers[1] = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DEL, | ||
172 | _del, (void *) 1); | ||
173 | fail_if(handlers[1] == NULL); | ||
174 | handlers[2] = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DATA, | ||
175 | _data, (void *) 1); | ||
176 | fail_if(handlers[2] == NULL); | ||
177 | |||
178 | handlers[3] = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, | ||
179 | _add, (void *) 2); | ||
180 | fail_if(handlers[3] == NULL); | ||
181 | handlers[4] = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL, | ||
182 | _del, (void *) 2); | ||
183 | fail_if(handlers[4] == NULL); | ||
184 | handlers[5] = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA, | ||
185 | _data, (void *) 2); | ||
186 | fail_if(handlers[5] == NULL); | ||
187 | |||
188 | server = ecore_con_server_add(ECORE_CON_REMOTE_TCP, "::1", 8080, | ||
189 | server_data); | ||
190 | fail_if (server == NULL); | ||
191 | |||
192 | ecore_con_server_timeout_set(server, 10); | ||
193 | ecore_con_server_client_limit_set(server, 1, 0); | ||
194 | |||
195 | client = ecore_con_server_connect(ECORE_CON_REMOTE_TCP, "::1", 8080, | ||
196 | client_data); | ||
197 | fail_if (client == NULL); | ||
198 | |||
199 | ecore_main_loop_begin(); | ||
200 | |||
201 | clients = ecore_con_server_clients_get(server); | ||
202 | printf("Clients connected to this server when exiting: %d\n", | ||
203 | eina_list_count(clients)); | ||
204 | EINA_LIST_FOREACH(clients, l, cl) | ||
205 | { | ||
206 | printf("%s\n", ecore_con_client_ip_get(cl)); | ||
207 | } | ||
208 | |||
209 | printf("Server was up for %0.3f seconds\n", | ||
210 | ecore_con_server_uptime_get(server)); | ||
211 | |||
212 | del_ret = ecore_con_server_del(server); | ||
213 | fail_if (del_ret != server_data); | ||
214 | free (server_data); | ||
215 | del_ret = ecore_con_server_del(client); | ||
216 | fail_if (del_ret != client_data); | ||
217 | free (client_data); | ||
218 | |||
219 | del_ret = ecore_event_handler_del (handlers[0]); | ||
220 | fail_if (del_ret != (void *) 1); | ||
221 | del_ret = ecore_event_handler_del (handlers[1]); | ||
222 | fail_if (del_ret != (void *) 1); | ||
223 | del_ret = ecore_event_handler_del (handlers[2]); | ||
224 | fail_if (del_ret != (void *) 1); | ||
225 | |||
226 | del_ret = ecore_event_handler_del (handlers[3]); | ||
227 | fail_if (del_ret != (void *) 2); | ||
228 | del_ret = ecore_event_handler_del (handlers[4]); | ||
229 | fail_if (del_ret != (void *) 2); | ||
230 | del_ret = ecore_event_handler_del (handlers[5]); | ||
231 | fail_if (del_ret != (void *) 2); | ||
232 | |||
233 | ret = ecore_con_shutdown(); | ||
234 | fail_if(ret != 0); | ||
235 | ret = ecore_shutdown(); | ||
236 | fail_if(ret != 0); | ||
237 | ret = eina_shutdown(); | ||
238 | fail_if(ret != 0); | ||
239 | } | ||
240 | END_TEST | ||
241 | |||
242 | START_TEST(ecore_test_ecore_con_init) | ||
243 | { | ||
244 | int ret; | ||
245 | |||
246 | ret = ecore_con_init(); | ||
247 | fail_if(ret != 1); | ||
248 | |||
249 | ret = ecore_con_shutdown(); | ||
250 | fail_if(ret != 0); | ||
251 | } | ||
252 | END_TEST | ||
253 | |||
254 | void ecore_test_ecore_con(TCase *tc) | ||
255 | { | ||
256 | tcase_add_test(tc, ecore_test_ecore_con_init); | ||
257 | tcase_add_test(tc, ecore_test_ecore_con_server); | ||
258 | } | ||