summaryrefslogtreecommitdiff
path: root/src/tests/ecore_con
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>2016-12-12 02:23:29 -0200
committerGustavo Sverzut Barbieri <barbieri@profusion.mobi>2016-12-12 02:30:33 -0200
commit633ec445b8f167339fa451350319f47a4b01900b (patch)
treea7a6bd9aff14ec02fe3fabfe1f3d5648289103f6 /src/tests/ecore_con
parent96eccc27535e2d2a0de3dae46dbdedb5450e1174 (diff)
efl_net: add Efl.Net.Ip_Address
This is a string parser, serializer and asynchronous resolver. It's purpose is to convert to and from the strings we use in our dialers and servers, such as "127.0.0.1:1234" or "[::1]:1234", properties allow to check the family, port, address bytes (slice) and even get a struct sockaddr pointer to use with bind()/connect() in outside code. It will also offer some utilities present in netinet/in.h in an easy to use way, after all IN6_IS_ADDR_LOOPBACK() works one way, while there is no IN_LOOPBACK and comparing with INADDR_LOOPBACK will lead to errors since it's in network order. Last but not least, it will do asynchronous resolve of host and port names using an internal thread and getaddrinfo(). The results are delivered using a Future with an array of objects.
Diffstat (limited to 'src/tests/ecore_con')
-rw-r--r--src/tests/ecore_con/ecore_con_suite.c1
-rw-r--r--src/tests/ecore_con/ecore_con_suite.h1
-rw-r--r--src/tests/ecore_con/ecore_con_test_efl_net_ip_address.c1154
3 files changed, 1156 insertions, 0 deletions
diff --git a/src/tests/ecore_con/ecore_con_suite.c b/src/tests/ecore_con/ecore_con_suite.c
index a3ef616..21585ae 100644
--- a/src/tests/ecore_con/ecore_con_suite.c
+++ b/src/tests/ecore_con/ecore_con_suite.c
@@ -9,6 +9,7 @@ static const Efl_Test_Case etc[] = {
9 { "Ecore_Con", ecore_con_test_ecore_con }, 9 { "Ecore_Con", ecore_con_test_ecore_con },
10 { "Ecore_Con_Url", ecore_con_test_ecore_con_url }, 10 { "Ecore_Con_Url", ecore_con_test_ecore_con_url },
11 { "Ecore_Con_Eet", ecore_con_test_ecore_con_eet }, 11 { "Ecore_Con_Eet", ecore_con_test_ecore_con_eet },
12 { "Efl_Net_Ip_Address", ecore_con_test_efl_net_ip_address },
12 { NULL, NULL } 13 { NULL, NULL }
13}; 14};
14 15
diff --git a/src/tests/ecore_con/ecore_con_suite.h b/src/tests/ecore_con/ecore_con_suite.h
index 5d18647..133b68a 100644
--- a/src/tests/ecore_con/ecore_con_suite.h
+++ b/src/tests/ecore_con/ecore_con_suite.h
@@ -6,5 +6,6 @@
6void ecore_con_test_ecore_con(TCase *tc); 6void ecore_con_test_ecore_con(TCase *tc);
7void ecore_con_test_ecore_con_url(TCase *tc); 7void ecore_con_test_ecore_con_url(TCase *tc);
8void ecore_con_test_ecore_con_eet(TCase *tc); 8void ecore_con_test_ecore_con_eet(TCase *tc);
9void ecore_con_test_efl_net_ip_address(TCase *tc);
9 10
10#endif /* _ECORE_CON_SUITE_H */ 11#endif /* _ECORE_CON_SUITE_H */
diff --git a/src/tests/ecore_con/ecore_con_test_efl_net_ip_address.c b/src/tests/ecore_con/ecore_con_test_efl_net_ip_address.c
new file mode 100644
index 0000000..d46e31b
--- /dev/null
+++ b/src/tests/ecore_con/ecore_con_test_efl_net_ip_address.c
@@ -0,0 +1,1154 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <stdio.h>
6
7#include <Ecore.h>
8#include <Ecore_Con.h>
9
10#ifdef HAVE_NETINET_IN_H
11# include <netinet/in.h>
12#endif
13
14#ifdef HAVE_ARPA_INET_H
15# include <arpa/inet.h>
16#endif
17
18#ifdef HAVE_EVIL
19# include <Evil.h>
20#endif
21
22#include "ecore_con_suite.h"
23
24struct log_ctx {
25 const char *dom;
26 const char *msg;
27 int level;
28 unsigned int did;
29};
30
31/* tests should not output on success, just uncomment this for debugging */
32//#define SHOW_LOG 1
33
34static void
35_eina_test_safety_print_cb(const Eina_Log_Domain *d, Eina_Log_Level level, const char *file, const char *fnc, int line, const char *fmt, void *data, va_list args EINA_UNUSED)
36{
37 struct log_ctx *ctx = data;
38 const char *str;
39
40 if (ctx->level != level) goto log;
41
42 if (strcmp(fmt, "%s") != 0)
43 str = fmt;
44 else
45 {
46 va_list cp_args;
47 va_copy(cp_args, args);
48 str = va_arg(cp_args, const char *);
49 va_end(cp_args);
50 }
51
52 if (ctx->dom)
53 {
54 if ((!d->name) || (strcmp(ctx->dom, d->name) != 0))
55 goto log;
56 }
57
58 if (ctx->msg)
59 {
60 if (strcmp(ctx->msg, str) != 0)
61 goto log;
62 }
63
64 ctx->did++;
65
66#ifndef SHOW_LOG
67 return;
68#endif
69 log:
70
71 eina_log_print_cb_stderr(d, level, file, fnc, line, fmt, NULL, args);
72}
73
74#define TRAP_ERRORS_BEGIN(_dom, _level, _msg) \
75 do \
76 { \
77 struct log_ctx _log_ctx = { \
78 .dom = #_dom, \
79 .level = EINA_LOG_LEVEL_ ## _level, \
80 .msg = _msg, \
81 }; \
82 eina_log_print_cb_set(_eina_test_safety_print_cb, &_log_ctx)
83
84#define TRAP_ERRORS_FINISH(cnt) \
85 eina_log_print_cb_set(eina_log_print_cb_stderr, NULL); \
86 if (_log_ctx.did != cnt) \
87 { \
88 ck_abort_msg("Expected error %u (did: %u) messages to be logged to domain=%s, level=%d, mesage='%s'", cnt, _log_ctx.did, _log_ctx.dom, _log_ctx.level, _log_ctx.msg); \
89 } \
90 } \
91 while (0)
92
93static void
94_timeout(void *data, const Efl_Event *event EINA_UNUSED)
95{
96 Eina_Bool *did = data;
97 *did = EINA_TRUE;
98 ck_abort_msg("timed out!");
99}
100
101#define LOOP_WITH_TIMEOUT(t) \
102 do \
103 { \
104 Eina_Bool _did_timeout = EINA_FALSE; \
105 Efl_Future *_timeout_future = efl_loop_timeout(ecore_main_loop_get(), t, &_did_timeout); \
106 efl_future_then(_timeout_future, _timeout, NULL, NULL, &_did_timeout); \
107 mark_point(); \
108 ecore_main_loop_begin(); \
109 if (!_did_timeout) efl_future_cancel(_timeout_future); \
110 else ck_abort_msg("Timed out!"); \
111 } \
112 while (0)
113
114
115struct resolve_ctx {
116 Eina_Array *results;
117 Eina_Stringshare *canonical_name;
118 Eina_Stringshare *request_address;
119 Efl_Future *future;
120 Eina_Error err;
121};
122
123static void
124_resolve_cleanup(struct resolve_ctx *ctx)
125{
126 Eina_Array_Iterator it;
127 unsigned int i;
128 const Efl_Net_Ip_Address *o;
129
130 mark_point();
131
132 if (ctx->results)
133 {
134 EINA_ARRAY_ITER_NEXT(ctx->results, i, o, it)
135 efl_unref(o);
136 eina_array_free(ctx->results);
137 ctx->results = NULL;
138 }
139
140 ctx->err = 0;
141 eina_stringshare_replace(&ctx->canonical_name, NULL);
142 eina_stringshare_replace(&ctx->request_address, NULL);
143
144 if (ctx->future)
145 {
146 efl_future_cancel(ctx->future);
147 ctx->future = NULL;
148 }
149}
150
151static Eina_Bool
152_resolve_found(const struct resolve_ctx *ctx, const char *string)
153{
154 Eina_Array_Iterator it;
155 unsigned int i;
156 const Efl_Net_Ip_Address *o;
157
158 mark_point();
159
160 EINA_ARRAY_ITER_NEXT(ctx->results, i, o, it)
161 {
162 if (strcmp(string, efl_net_ip_address_string_get(o)) == 0)
163 return EINA_TRUE;
164 }
165
166 return EINA_FALSE;
167}
168
169static void
170_resolve_done(void *data, const Efl_Event *event)
171{
172 struct resolve_ctx *ctx = data;
173 Efl_Future_Event_Success *f = event->info;
174 Efl_Net_Ip_Address_Resolve_Results *r = f->value;
175 Eina_Array_Iterator it;
176 unsigned int i;
177 Efl_Net_Ip_Address *o;
178
179 ck_assert_ptr_eq(ctx->results, NULL);
180 ctx->results = eina_array_new(32);
181
182 ctx->canonical_name = eina_stringshare_ref(r->canonical_name);
183 ctx->request_address = eina_stringshare_ref(r->request_address);
184
185 EINA_ARRAY_ITER_NEXT(r->results, i, o, it)
186 eina_array_push(ctx->results, efl_ref(o));
187
188 ctx->future = NULL;
189 ecore_main_loop_quit();
190
191 mark_point();
192}
193
194static void
195_resolve_failed(void *data, const Efl_Event *event)
196{
197 struct resolve_ctx *ctx = data;
198 Efl_Future_Event_Failure *f = event->info;
199
200 mark_point();
201
202 ctx->err = f->error;
203 ctx->future = NULL;
204 ecore_main_loop_quit();
205
206 mark_point();
207}
208
209static void
210_resolve(struct resolve_ctx *ctx, const char *address, int family, int flags)
211{
212 ctx->future = efl_net_ip_address_resolve(EFL_NET_IP_ADDRESS_CLASS,
213 address, family, flags);
214 ck_assert_ptr_ne(ctx->future, NULL);
215 efl_future_then(ctx->future, _resolve_done, _resolve_failed, NULL, ctx);
216
217 LOOP_WITH_TIMEOUT(5);
218}
219
220/* IPv4 *****************************************************************/
221
222static void
223_ipv4_check(Eo *o, const struct sockaddr_in *addr)
224{
225 Eina_Slice slice = { .mem = &addr->sin_addr, .len = sizeof(addr->sin_addr) };
226 const Eina_Slice *rs;
227 char buf[INET_ADDRSTRLEN + sizeof(":65536")] = "";
228
229 ck_assert_ptr_ne(o, NULL);
230
231 ck_assert_int_eq(efl_net_ip_address_family_get(o), AF_INET);
232 ck_assert_int_eq(efl_net_ip_address_port_get(o), ntohs(addr->sin_port));
233
234 rs = efl_net_ip_address_get(o);
235 ck_assert_ptr_ne(rs, NULL);
236 ck_assert_int_eq(eina_slice_compare(*rs, slice), 0);
237
238 inet_ntop(AF_INET, slice.mem, buf, INET_ADDRSTRLEN);
239
240 if (addr->sin_port)
241 {
242 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
243 ":%hu", htons(addr->sin_port));
244 }
245 ck_assert_ptr_ne(efl_net_ip_address_string_get(o), NULL);
246 ck_assert_str_eq(efl_net_ip_address_string_get(o), buf);
247}
248
249START_TEST(ecore_test_efl_net_ip_address_ipv4_manual_ok)
250{
251 struct sockaddr_in addr = {
252 .sin_family = AF_INET,
253 };
254 Eina_Slice slice = { .mem = &addr.sin_addr, .len = sizeof(addr.sin_addr) };
255 Eo *o;
256
257 ecore_con_init();
258
259 addr.sin_port = htons(12345);
260 addr.sin_addr.s_addr = htonl(0xabcdefafU);
261 o = efl_add(EFL_NET_IP_ADDRESS_CLASS, NULL,
262 efl_net_ip_address_family_set(efl_added, AF_INET),
263 efl_net_ip_address_port_set(efl_added, ntohs(addr.sin_port)),
264 efl_net_ip_address_set(efl_added, &slice));
265 _ipv4_check(o, &addr);
266 efl_del(o);
267
268 addr.sin_port = htons(8081);
269 addr.sin_addr.s_addr = htonl(0);
270 o = efl_add(EFL_NET_IP_ADDRESS_CLASS, NULL,
271 efl_net_ip_address_family_set(efl_added, AF_INET),
272 efl_net_ip_address_port_set(efl_added, ntohs(addr.sin_port)),
273 efl_net_ip_address_set(efl_added, &slice));
274 _ipv4_check(o, &addr);
275 efl_del(o);
276
277 addr.sin_port = htons(0);
278 addr.sin_addr.s_addr = htonl(0x12345678);
279 o = efl_add(EFL_NET_IP_ADDRESS_CLASS, NULL,
280 efl_net_ip_address_family_set(efl_added, AF_INET),
281 efl_net_ip_address_set(efl_added, &slice));
282 _ipv4_check(o, &addr);
283 efl_del(o);
284
285 ecore_con_shutdown();
286}
287END_TEST
288
289START_TEST(ecore_test_efl_net_ip_address_ipv4_manual_fail)
290{
291 uint8_t c = 123;
292 Eina_Slice wrong_slice = { .mem = &c, .len = 1 };
293 struct sockaddr_in addr = {
294 .sin_family = AF_INET,
295 };
296 Eina_Slice slice = { .mem = &addr.sin_addr, .len = sizeof(addr.sin_addr) };
297 Eo *o;
298
299 ecore_con_init();
300
301 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: pd->addr.sa_family == 0 is true");
302 o = efl_add(EFL_NET_IP_ADDRESS_CLASS, NULL);
303 ck_assert_ptr_eq(o, NULL);
304 TRAP_ERRORS_FINISH(1);
305
306 TRAP_ERRORS_BEGIN(eina_safety, ERR, NULL);
307 o = efl_add(EFL_NET_IP_ADDRESS_CLASS, NULL,
308 efl_net_ip_address_family_set(efl_added, 12345));
309 ck_assert_ptr_eq(o, NULL);
310 TRAP_ERRORS_FINISH(2);
311
312 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: pd->addr.sa_family == 0 is true");
313 o = efl_add(EFL_NET_IP_ADDRESS_CLASS, NULL,
314 efl_net_ip_address_set(efl_added, &wrong_slice));
315 ck_assert_ptr_eq(o, NULL);
316 TRAP_ERRORS_FINISH(2);
317
318 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: pd->addr.sa_family == 0 is true");
319 o = efl_add(EFL_NET_IP_ADDRESS_CLASS, NULL,
320 efl_net_ip_address_port_set(efl_added, 1234));
321 ck_assert_ptr_eq(o, NULL);
322 TRAP_ERRORS_FINISH(2);
323
324 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: pd->addr.sa_family == 0 is true");
325 o = efl_add(EFL_NET_IP_ADDRESS_CLASS, NULL,
326 efl_net_ip_address_port_set(efl_added, 1234),
327 efl_net_ip_address_set(efl_added, &wrong_slice));
328 ck_assert_ptr_eq(o, NULL);
329 TRAP_ERRORS_FINISH(3);
330
331 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: rw_slice.len != address->len is true");
332 o = efl_add(EFL_NET_IP_ADDRESS_CLASS, NULL,
333 efl_net_ip_address_family_set(efl_added, AF_INET),
334 efl_net_ip_address_set(efl_added, &wrong_slice));
335 _ipv4_check(o, &addr);
336 efl_del(o);
337 TRAP_ERRORS_FINISH(1);
338
339 addr.sin_port = htons(12345);
340 addr.sin_addr.s_addr = htonl(0xabcdefafU);
341 o = efl_add(EFL_NET_IP_ADDRESS_CLASS, NULL,
342 efl_net_ip_address_family_set(efl_added, AF_INET),
343 efl_net_ip_address_port_set(efl_added, ntohs(addr.sin_port)),
344 efl_net_ip_address_set(efl_added, &slice));
345 _ipv4_check(o, &addr);
346
347 TRAP_ERRORS_BEGIN(ecore_con, ERR, "port already set to %hu, new %hu");
348 efl_net_ip_address_port_set(o, ntohs(addr.sin_port));
349 TRAP_ERRORS_FINISH(0);
350
351 TRAP_ERRORS_BEGIN(ecore_con, ERR, "port already set to %hu, new %hu");
352 efl_net_ip_address_port_set(o, 999);
353 TRAP_ERRORS_FINISH(1);
354
355 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: address == NULL");
356 efl_net_ip_address_set(o, NULL);
357 TRAP_ERRORS_FINISH(1);
358
359 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: rw_slice.len != address->len is true");
360 slice.len = 1;
361 efl_net_ip_address_set(o, &slice);
362 TRAP_ERRORS_FINISH(1);
363
364 TRAP_ERRORS_BEGIN(ecore_con, ERR, "address already set to %s, new %s");
365 slice.len = sizeof(addr.sin_addr.s_addr);
366 efl_net_ip_address_set(o, &slice);
367 TRAP_ERRORS_FINISH(0);
368
369 TRAP_ERRORS_BEGIN(ecore_con, ERR, "address already set to %s, new %s");
370 addr.sin_addr.s_addr = htonl(0x12345678);
371 slice.len = sizeof(addr.sin_addr.s_addr);
372 efl_net_ip_address_set(o, &slice);
373 TRAP_ERRORS_FINISH(1);
374
375 efl_del(o);
376
377 ecore_con_shutdown();
378}
379END_TEST
380
381START_TEST(ecore_test_efl_net_ip_address_ipv4_create_ok)
382{
383 struct sockaddr_in addr = {
384 .sin_family = AF_INET,
385 };
386 Eina_Slice slice = { .mem = &addr.sin_addr, .len = sizeof(addr.sin_addr) };
387 Eo *o;
388
389 ecore_con_init();
390
391 addr.sin_port = htons(12345);
392 addr.sin_addr.s_addr = htonl(0xabcdefafU);
393 o = efl_net_ip_address_create(EFL_NET_IP_ADDRESS_CLASS,
394 ntohs(addr.sin_port),
395 slice);
396 _ipv4_check(o, &addr);
397 efl_del(o);
398
399 addr.sin_port = htons(8081);
400 addr.sin_addr.s_addr = htonl(0);
401 o = efl_net_ip_address_create(EFL_NET_IP_ADDRESS_CLASS,
402 ntohs(addr.sin_port),
403 slice);
404 _ipv4_check(o, &addr);
405 efl_del(o);
406
407 addr.sin_port = htons(0);
408 addr.sin_addr.s_addr = htonl(0x12345678);
409 o = efl_net_ip_address_create(EFL_NET_IP_ADDRESS_CLASS,
410 ntohs(addr.sin_port),
411 slice);
412 _ipv4_check(o, &addr);
413 efl_del(o);
414
415 ecore_con_shutdown();
416}
417END_TEST
418
419START_TEST(ecore_test_efl_net_ip_address_ipv4_create_fail)
420{
421 uint8_t c = 123;
422 Eina_Slice wrong_slice = { .mem = &c, .len = 1 };
423 Eo *o;
424
425 ecore_con_init();
426
427 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: address.len != 4 && address.len != 16 is true");
428 o = efl_net_ip_address_create(EFL_NET_IP_ADDRESS_CLASS,
429 1234,
430 wrong_slice);
431 ck_assert_ptr_eq(o, NULL);
432 TRAP_ERRORS_FINISH(1);
433
434 ecore_con_shutdown();
435}
436END_TEST
437
438START_TEST(ecore_test_efl_net_ip_address_ipv4_create_sockaddr_ok)
439{
440 struct sockaddr_in addr = {
441 .sin_family = AF_INET,
442 };
443 Eo *o;
444
445 ecore_con_init();
446
447 addr.sin_port = htons(12345);
448 addr.sin_addr.s_addr = htonl(0xabcdefafU);
449 o = efl_net_ip_address_create_sockaddr(EFL_NET_IP_ADDRESS_CLASS, &addr);
450 ck_assert_ptr_ne(&addr, efl_net_ip_address_sockaddr_get(o));
451 _ipv4_check(o, &addr);
452 efl_del(o);
453
454 addr.sin_port = htons(0);
455 addr.sin_addr.s_addr = htonl(0);
456 o = efl_net_ip_address_create_sockaddr(EFL_NET_IP_ADDRESS_CLASS, &addr);
457 ck_assert_ptr_ne(&addr, efl_net_ip_address_sockaddr_get(o));
458 _ipv4_check(o, &addr);
459 efl_del(o);
460
461 ecore_con_shutdown();
462}
463END_TEST
464
465START_TEST(ecore_test_efl_net_ip_address_ipv4_create_sockaddr_fail)
466{
467 struct sockaddr_in addr = {
468 .sin_family = 1234,
469 };
470 Eo *o;
471
472 ecore_con_init();
473
474 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: sockaddr == NULL");
475 o = efl_net_ip_address_create_sockaddr(EFL_NET_IP_ADDRESS_CLASS, NULL);
476 ck_assert_ptr_eq(o, NULL);
477 TRAP_ERRORS_FINISH(1);
478
479 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: (sockaddr->sa_family != AF_INET) && (sockaddr->sa_family != AF_INET6) is true");
480 o = efl_net_ip_address_create_sockaddr(EFL_NET_IP_ADDRESS_CLASS, &addr);
481 ck_assert_ptr_eq(o, NULL);
482 TRAP_ERRORS_FINISH(1);
483
484 ecore_con_shutdown();
485}
486END_TEST
487
488START_TEST(ecore_test_efl_net_ip_address_ipv4_parse_ok)
489{
490 Eo *o;
491
492 ecore_con_init();
493
494 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "127.0.0.1:12345");
495 ck_assert_ptr_ne(o, NULL);
496 ck_assert_str_eq(efl_net_ip_address_string_get(o), "127.0.0.1:12345");
497 efl_del(o);
498
499 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "127.0.0.1:0");
500 ck_assert_ptr_ne(o, NULL);
501 ck_assert_str_eq(efl_net_ip_address_string_get(o), "127.0.0.1");
502 efl_del(o);
503
504 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "127.0.0.1");
505 ck_assert_ptr_ne(o, NULL);
506 ck_assert_str_eq(efl_net_ip_address_string_get(o), "127.0.0.1");
507 efl_del(o);
508
509 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "192.168.0.123:80");
510 ck_assert_ptr_ne(o, NULL);
511 ck_assert_str_eq(efl_net_ip_address_string_get(o), "192.168.0.123:80");
512 efl_del(o);
513
514 ecore_con_shutdown();
515}
516END_TEST
517
518START_TEST(ecore_test_efl_net_ip_address_ipv4_parse_fail)
519{
520 Eo *o;
521
522 ecore_con_init();
523
524 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: numeric_address == NULL");
525 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, NULL);
526 ck_assert_ptr_eq(o, NULL);
527 TRAP_ERRORS_FINISH(1);
528
529 /* incomplete numbers */
530 TRAP_ERRORS_BEGIN(eina_safety, ERR, NULL);
531 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "127.");
532 ck_assert_ptr_eq(o, NULL);
533 TRAP_ERRORS_FINISH(0); /* no error messages! */
534
535 TRAP_ERRORS_BEGIN(eina_safety, ERR, NULL);
536 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "127.0.0.");
537 ck_assert_ptr_eq(o, NULL);
538 TRAP_ERRORS_FINISH(0); /* no error messages! */
539
540 /* hostnames are not numeric, shouldn't return an object */
541 TRAP_ERRORS_BEGIN(eina_safety, ERR, NULL);
542 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "google.com");
543 ck_assert_ptr_eq(o, NULL);
544 TRAP_ERRORS_FINISH(0); /* no error messages! */
545
546 /* port names are not numeric, shouldn't return an object */
547 TRAP_ERRORS_BEGIN(eina_safety, ERR, NULL);
548 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "127.0.0.1:http");
549 ck_assert_ptr_eq(o, NULL);
550 TRAP_ERRORS_FINISH(0); /* no error messages! */
551
552 ecore_con_shutdown();
553}
554END_TEST
555
556START_TEST(ecore_test_efl_net_ip_address_ipv4_resolve_ok)
557{
558 struct resolve_ctx ctx = { };
559
560 ecore_con_init();
561
562 _resolve(&ctx, "localhost:http", 0, 0);
563 ck_assert_int_eq(ctx.err, 0);
564 ck_assert_int_eq(_resolve_found(&ctx, "127.0.0.1:80"), EINA_TRUE);
565 _resolve_cleanup(&ctx);
566
567 _resolve(&ctx, "localhost", 0, 0);
568 ck_assert_int_eq(ctx.err, 0);
569 ck_assert_int_eq(_resolve_found(&ctx, "127.0.0.1"), EINA_TRUE);
570 _resolve_cleanup(&ctx);
571
572 _resolve(&ctx, "127.0.0.1", 0, 0);
573 ck_assert_int_eq(ctx.err, 0);
574 ck_assert_int_eq(_resolve_found(&ctx, "127.0.0.1"), EINA_TRUE);
575 _resolve_cleanup(&ctx);
576
577 _resolve(&ctx, "127.0.0.1:http", 0, 0);
578 ck_assert_int_eq(ctx.err, 0);
579 ck_assert_int_eq(_resolve_found(&ctx, "127.0.0.1:80"), EINA_TRUE);
580 _resolve_cleanup(&ctx);
581
582 _resolve(&ctx, "127.0.0.1:80", 0, 0);
583 ck_assert_int_eq(ctx.err, 0);
584 ck_assert_int_eq(_resolve_found(&ctx, "127.0.0.1:80"), EINA_TRUE);
585 _resolve_cleanup(&ctx);
586
587 _resolve(&ctx, "localhost:80", 0, 0);
588 ck_assert_int_eq(ctx.err, 0);
589 ck_assert_int_eq(_resolve_found(&ctx, "127.0.0.1:80"), EINA_TRUE);
590 _resolve_cleanup(&ctx);
591
592 _resolve(&ctx, "localhost:http", AF_INET, 0);
593 ck_assert_int_eq(ctx.err, 0);
594 ck_assert_int_eq(_resolve_found(&ctx, "[::1]:80"), EINA_FALSE);
595 _resolve_cleanup(&ctx);
596
597 ecore_con_shutdown();
598}
599END_TEST
600
601START_TEST(ecore_test_efl_net_ip_address_ipv4_resolve_fail)
602{
603 struct resolve_ctx ctx = { };
604
605 ecore_con_init();
606
607 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: address == NULL");
608 ctx.future = efl_net_ip_address_resolve(EFL_NET_IP_ADDRESS_CLASS,
609 NULL, 0, 0);
610 ck_assert_ptr_eq(ctx.future, NULL);
611 TRAP_ERRORS_FINISH(1);
612
613 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: (family != AF_UNSPEC) && (family != AF_INET) && (family != AF_INET6) is true");
614 ctx.future = efl_net_ip_address_resolve(EFL_NET_IP_ADDRESS_CLASS,
615 "localhost", 1234, 0);
616 ck_assert_ptr_eq(ctx.future, NULL);
617 TRAP_ERRORS_FINISH(1);
618
619 _resolve(&ctx, "xxlocalhost:xxhttp", 0, 0);
620 ck_assert_int_eq(ctx.err, EFL_NET_ERROR_COULDNT_RESOLVE_HOST);
621 _resolve_cleanup(&ctx);
622
623 _resolve(&ctx, "[::1]:http", AF_INET, 0);
624 ck_assert_int_eq(ctx.err, EFL_NET_ERROR_COULDNT_RESOLVE_HOST);
625 _resolve_cleanup(&ctx);
626
627 ecore_con_shutdown();
628}
629END_TEST
630
631START_TEST(ecore_test_efl_net_ip_address_ipv4_checks)
632{
633 const struct test {
634 uint32_t addr;
635 Eina_Bool is_a, is_b, is_c, is_d, is_multicast, is_loopback, is_any;
636 } *itr, tests[] = {
637#define TEST(_addr) \
638 { .addr = _addr, \
639 .is_a = IN_CLASSA(_addr), \
640 .is_b = IN_CLASSB(_addr), \
641 .is_c = IN_CLASSC(_addr), \
642 .is_d = IN_CLASSD(_addr), \
643 .is_multicast = IN_MULTICAST(_addr) , \
644 .is_loopback = _addr == INADDR_LOOPBACK, \
645 .is_any = _addr == INADDR_ANY, \
646 }
647 TEST(INADDR_LOOPBACK),
648 TEST(INADDR_ANY),
649 TEST(0x0a000001),
650 TEST(0x80000002),
651 TEST(0xc0000003),
652 TEST(0xe0000004),
653#undef TEST
654 };
655
656 ecore_con_init();
657
658 for (itr = tests; itr < tests + sizeof(tests)/sizeof(tests[0]); itr++)
659 {
660 struct sockaddr_in a = {
661 .sin_family = AF_INET,
662 .sin_port = 0,
663 .sin_addr.s_addr = htonl(itr->addr),
664 };
665 Eo *o = efl_net_ip_address_create_sockaddr(EFL_NET_IP_ADDRESS_CLASS, &a);
666 ck_assert_ptr_ne(o, NULL);
667
668 ck_assert_int_eq(efl_net_ip_address_ipv4_class_a_check(o), itr->is_a);
669 ck_assert_int_eq(efl_net_ip_address_ipv4_class_b_check(o), itr->is_b);
670 ck_assert_int_eq(efl_net_ip_address_ipv4_class_c_check(o), itr->is_c);
671 ck_assert_int_eq(efl_net_ip_address_ipv4_class_d_check(o), itr->is_d);
672 ck_assert_int_eq(efl_net_ip_address_multicast_check(o), itr->is_multicast);
673 ck_assert_int_eq(efl_net_ip_address_loopback_check(o), itr->is_loopback);
674 ck_assert_int_eq(efl_net_ip_address_any_check(o), itr->is_any);
675
676 ck_assert_int_eq(efl_net_ip_address_ipv6_v4mapped_check(o), EINA_FALSE);
677 ck_assert_int_eq(efl_net_ip_address_ipv6_v4compat_check(o), EINA_FALSE);
678 ck_assert_int_eq(efl_net_ip_address_ipv6_local_link_check(o), EINA_FALSE);
679 ck_assert_int_eq(efl_net_ip_address_ipv6_local_site_check(o), EINA_FALSE);
680
681 efl_del(o);
682 }
683
684 ecore_con_shutdown();
685}
686END_TEST
687
688/* IPv6 *****************************************************************/
689
690static void
691_ipv6_check(Eo *o, const struct sockaddr_in6 *addr)
692{
693 Eina_Slice slice = { .mem = &addr->sin6_addr, .len = sizeof(addr->sin6_addr) };
694 const Eina_Slice *rs;
695 char buf[INET6_ADDRSTRLEN + sizeof("[]:65536")] = "";
696
697 ck_assert_ptr_ne(o, NULL);
698
699 ck_assert_int_eq(efl_net_ip_address_family_get(o), AF_INET6);
700 ck_assert_int_eq(efl_net_ip_address_port_get(o), ntohs(addr->sin6_port));
701
702 rs = efl_net_ip_address_get(o);
703 ck_assert_ptr_ne(rs, NULL);
704 ck_assert_int_eq(eina_slice_compare(*rs, slice), 0);
705
706 buf[0] = '[';
707 inet_ntop(AF_INET6, slice.mem, buf + 1, INET6_ADDRSTRLEN);
708 buf[strlen(buf)] = ']';
709
710 if (addr->sin6_port)
711 {
712 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
713 ":%hu", htons(addr->sin6_port));
714 }
715
716 ck_assert_ptr_ne(efl_net_ip_address_string_get(o), NULL);
717 ck_assert_str_eq(efl_net_ip_address_string_get(o), buf);
718}
719
720static void
721_ipv6_set(struct sockaddr_in6 *addr, uint16_t s1, uint16_t s2, uint16_t s3, uint16_t s4, uint16_t s5, uint16_t s6, uint16_t s7, uint16_t s8)
722{
723 uint16_t s[8] = { s1, s2, s3, s4, s5, s6, s7, s8 };
724 memcpy(&addr->sin6_addr, s, 16);
725}
726
727START_TEST(ecore_test_efl_net_ip_address_ipv6_manual_ok)
728{
729 struct sockaddr_in6 addr = {
730 .sin6_family = AF_INET6,
731 };
732 Eina_Slice slice = { .mem = &addr.sin6_addr, .len = sizeof(addr.sin6_addr) };
733 Eo *o;
734
735 ecore_con_init();
736
737 addr.sin6_port = htons(12345);
738 _ipv6_set(&addr, 1, 2, 3, 4, 5, 6, 7, 8);
739 o = efl_add(EFL_NET_IP_ADDRESS_CLASS, NULL,
740 efl_net_ip_address_family_set(efl_added, AF_INET6),
741 efl_net_ip_address_port_set(efl_added, ntohs(addr.sin6_port)),
742 efl_net_ip_address_set(efl_added, &slice));
743 _ipv6_check(o, &addr);
744 efl_del(o);
745
746 addr.sin6_port = htons(8081);
747 _ipv6_set(&addr, 0, 0, 0, 0, 0, 0, 0, 0);
748 o = efl_add(EFL_NET_IP_ADDRESS_CLASS, NULL,
749 efl_net_ip_address_family_set(efl_added, AF_INET6),
750 efl_net_ip_address_port_set(efl_added, ntohs(addr.sin6_port)),
751 efl_net_ip_address_set(efl_added, &slice));
752 _ipv6_check(o, &addr);
753 efl_del(o);
754
755 ecore_con_shutdown();
756}
757END_TEST
758
759START_TEST(ecore_test_efl_net_ip_address_ipv6_manual_fail)
760{
761 uint8_t c = 123;
762 Eina_Slice wrong_slice = { .mem = &c, .len = 1 };
763 struct sockaddr_in6 addr = {
764 .sin6_family = AF_INET6,
765 };
766 Eina_Slice slice = { .mem = &addr.sin6_addr, .len = sizeof(addr.sin6_addr) };
767 Eo *o;
768
769 ecore_con_init();
770
771 /* generic errors checked at ecore_test_efl_net_ip_address_ipv4_manual_fail */
772
773 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: rw_slice.len != address->len is true");
774 o = efl_add(EFL_NET_IP_ADDRESS_CLASS, NULL,
775 efl_net_ip_address_family_set(efl_added, AF_INET6),
776 efl_net_ip_address_set(efl_added, &wrong_slice));
777 _ipv6_check(o, &addr);
778 efl_del(o);
779 TRAP_ERRORS_FINISH(1);
780
781 addr.sin6_port = htons(12345);
782 _ipv6_set(&addr, 0, 0, 0, 0, 0, 0, 0, 1);
783 o = efl_add(EFL_NET_IP_ADDRESS_CLASS, NULL,
784 efl_net_ip_address_family_set(efl_added, AF_INET6),
785 efl_net_ip_address_port_set(efl_added, ntohs(addr.sin6_port)),
786 efl_net_ip_address_set(efl_added, &slice));
787 _ipv6_check(o, &addr);
788
789 TRAP_ERRORS_BEGIN(ecore_con, ERR, "port already set to %hu, new %hu");
790 efl_net_ip_address_port_set(o, ntohs(addr.sin6_port));
791 TRAP_ERRORS_FINISH(0);
792
793 TRAP_ERRORS_BEGIN(ecore_con, ERR, "port already set to %hu, new %hu");
794 efl_net_ip_address_port_set(o, 999);
795 TRAP_ERRORS_FINISH(1);
796
797 TRAP_ERRORS_BEGIN(eina_safety, ERR, "safety check failed: rw_slice.len != address->len is true");
798 slice.len = 1;
799 efl_net_ip_address_set(o, &slice);
800 TRAP_ERRORS_FINISH(1);
801
802 TRAP_ERRORS_BEGIN(ecore_con, ERR, "address already set to %s, new %s");
803 slice.len = sizeof(addr.sin6_addr);
804 efl_net_ip_address_set(o, &slice);
805 TRAP_ERRORS_FINISH(0);
806
807 TRAP_ERRORS_BEGIN(ecore_con, ERR, "address already set to %s, new %s");
808 _ipv6_set(&addr, 1, 2, 3, 4, 5, 6, 7, 8);
809 slice.len = sizeof(addr.sin6_addr);
810 efl_net_ip_address_set(o, &slice);
811 TRAP_ERRORS_FINISH(1);
812
813 efl_del(o);
814
815 ecore_con_shutdown();
816}
817END_TEST
818
819START_TEST(ecore_test_efl_net_ip_address_ipv6_create_ok)
820{
821 struct sockaddr_in6 addr = {
822 .sin6_family = AF_INET6,
823 };
824 Eina_Slice slice = { .mem = &addr.sin6_addr, .len = sizeof(addr.sin6_addr) };
825 Eo *o;
826
827 ecore_con_init();
828
829 addr.sin6_port = htons(12365);
830 _ipv6_set(&addr, 1, 2, 3, 4, 5, 6, 7, 8);
831 o = efl_net_ip_address_create(EFL_NET_IP_ADDRESS_CLASS,
832 ntohs(addr.sin6_port),
833 slice);
834 _ipv6_check(o, &addr);
835 efl_del(o);
836
837 addr.sin6_port = htons(8081);
838 _ipv6_set(&addr, 0, 0, 0, 0, 0, 0, 0, 0);
839 o = efl_net_ip_address_create(EFL_NET_IP_ADDRESS_CLASS,
840 ntohs(addr.sin6_port),
841 slice);
842 _ipv6_check(o, &addr);
843 efl_del(o);
844
845 addr.sin6_port = htons(0);
846 _ipv6_set(&addr, 0, 0, 0, 0, 0, 0, 0, 1);
847 o = efl_net_ip_address_create(EFL_NET_IP_ADDRESS_CLASS,
848 ntohs(addr.sin6_port),
849 slice);
850 _ipv6_check(o, &addr);
851 efl_del(o);
852
853 ecore_con_shutdown();
854}
855END_TEST
856
857START_TEST(ecore_test_efl_net_ip_address_ipv6_create_sockaddr_ok)
858{
859 struct sockaddr_in6 addr = {
860 .sin6_family = AF_INET6,
861 };
862 Eo *o;
863
864 ecore_con_init();
865
866 addr.sin6_port = htons(12345);
867 _ipv6_set(&addr, 1, 2, 3, 4, 5, 6, 7, 8);
868 o = efl_net_ip_address_create_sockaddr(EFL_NET_IP_ADDRESS_CLASS, &addr);
869 ck_assert_ptr_ne(&addr, efl_net_ip_address_sockaddr_get(o));
870 _ipv6_check(o, &addr);
871 efl_del(o);
872
873 addr.sin6_port = htons(0);
874 _ipv6_set(&addr, 0, 0, 0, 0, 0, 0, 0, 0);
875 o = efl_net_ip_address_create_sockaddr(EFL_NET_IP_ADDRESS_CLASS, &addr);
876 ck_assert_ptr_ne(&addr, efl_net_ip_address_sockaddr_get(o));
877 _ipv6_check(o, &addr);
878 efl_del(o);
879
880 ecore_con_shutdown();
881}
882END_TEST
883
884START_TEST(ecore_test_efl_net_ip_address_ipv6_parse_ok)
885{
886 Eo *o;
887
888 ecore_con_init();
889
890 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[::1]:12345");
891 ck_assert_ptr_ne(o, NULL);
892 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[::1]:12345");
893 efl_del(o);
894
895 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[::1]:0");
896 ck_assert_ptr_ne(o, NULL);
897 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[::1]");
898 efl_del(o);
899
900 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[::1]");
901 ck_assert_ptr_ne(o, NULL);
902 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[::1]");
903 efl_del(o);
904
905 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "::1");
906 ck_assert_ptr_ne(o, NULL);
907 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[::1]");
908 efl_del(o);
909
910 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[::]:12345");
911 ck_assert_ptr_ne(o, NULL);
912 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[::]:12345");
913 efl_del(o);
914
915 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[::]:0");
916 ck_assert_ptr_ne(o, NULL);
917 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[::]");
918 efl_del(o);
919
920 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[::]");
921 ck_assert_ptr_ne(o, NULL);
922 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[::]");
923 efl_del(o);
924
925 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "::");
926 ck_assert_ptr_ne(o, NULL);
927 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[::]");
928 efl_del(o);
929
930 /* IPv4 Mapped */
931 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[::ffff:192.168.0.1]:12345");
932 ck_assert_ptr_ne(o, NULL);
933 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[::ffff:192.168.0.1]:12345");
934 efl_del(o);
935
936 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[::ffff:192.168.0.1]");
937 ck_assert_ptr_ne(o, NULL);
938 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[::ffff:192.168.0.1]");
939 efl_del(o);
940
941 /* IPv4 Compatible */
942 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[::192.168.0.1]:12345");
943 ck_assert_ptr_ne(o, NULL);
944 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[::192.168.0.1]:12345");
945 efl_del(o);
946
947 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[::192.168.0.1]");
948 ck_assert_ptr_ne(o, NULL);
949 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[::192.168.0.1]");
950 efl_del(o);
951
952 /* Link Local */
953 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[fe80::1]:12345");
954 ck_assert_ptr_ne(o, NULL);
955 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[fe80::1]:12345");
956 efl_del(o);
957
958 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[fe80::1]");
959 ck_assert_ptr_ne(o, NULL);
960 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[fe80::1]");
961 efl_del(o);
962
963 /* Site Local */
964 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[fc00::1]:12345");
965 ck_assert_ptr_ne(o, NULL);
966 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[fc00::1]:12345");
967 efl_del(o);
968
969 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[fc00::1]");
970 ck_assert_ptr_ne(o, NULL);
971 ck_assert_str_eq(efl_net_ip_address_string_get(o), "[fc00::1]");
972 efl_del(o);
973
974 ecore_con_shutdown();
975}
976END_TEST
977
978START_TEST(ecore_test_efl_net_ip_address_ipv6_parse_fail)
979{
980 Eo *o;
981
982 ecore_con_init();
983
984 /* generic error (null ptr) checked in ipv4_parse_fail */
985
986 /* incomplete numbers */
987 TRAP_ERRORS_BEGIN(eina_safety, ERR, NULL);
988 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "::9999999");
989 ck_assert_ptr_eq(o, NULL);
990 TRAP_ERRORS_FINISH(0); /* no error messages! */
991
992 TRAP_ERRORS_BEGIN(eina_safety, ERR, NULL);
993 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "ab:cd:ef:gh");
994 ck_assert_ptr_eq(o, NULL);
995 TRAP_ERRORS_FINISH(0); /* no error messages! */
996
997 /* port names are not numeric, shouldn't return an object */
998 TRAP_ERRORS_BEGIN(eina_safety, ERR, NULL);
999 o = efl_net_ip_address_parse(EFL_NET_IP_ADDRESS_CLASS, "[::1]:http");
1000 ck_assert_ptr_eq(o, NULL);
1001 TRAP_ERRORS_FINISH(0); /* no error messages! */
1002
1003 ecore_con_shutdown();
1004}
1005END_TEST
1006
1007START_TEST(ecore_test_efl_net_ip_address_ipv6_resolve_ok)
1008{
1009 struct resolve_ctx ctx = { };
1010
1011 ecore_con_init();
1012
1013 _resolve(&ctx, "localhost:http", 0, 0);
1014 ck_assert_int_eq(ctx.err, 0);
1015 ck_assert_int_eq(_resolve_found(&ctx, "[::1]:80"), EINA_TRUE);
1016 _resolve_cleanup(&ctx);
1017
1018 _resolve(&ctx, "localhost", 0, 0);
1019 ck_assert_int_eq(ctx.err, 0);
1020 ck_assert_int_eq(_resolve_found(&ctx, "[::1]"), EINA_TRUE);
1021 _resolve_cleanup(&ctx);
1022
1023 _resolve(&ctx, "::1", 0, 0);
1024 ck_assert_int_eq(ctx.err, 0);
1025 ck_assert_int_eq(_resolve_found(&ctx, "[::1]"), EINA_TRUE);
1026 _resolve_cleanup(&ctx);
1027
1028#if defined(AI_V4MAPPED) && (AI_V4MAPPED > 0)
1029 _resolve(&ctx, "127.0.0.1", AF_INET6, AI_V4MAPPED);
1030 ck_assert_int_eq(ctx.err, 0);
1031 ck_assert_int_eq(_resolve_found(&ctx, "[::ffff:127.0.0.1]"), EINA_TRUE);
1032 _resolve_cleanup(&ctx);
1033#endif
1034
1035 _resolve(&ctx, "[::1]:http", 0, 0);
1036 ck_assert_int_eq(ctx.err, 0);
1037 ck_assert_int_eq(_resolve_found(&ctx, "[::1]:80"), EINA_TRUE);
1038 _resolve_cleanup(&ctx);
1039
1040 _resolve(&ctx, "[::1]:80", 0, 0);
1041 ck_assert_int_eq(ctx.err, 0);
1042 ck_assert_int_eq(_resolve_found(&ctx, "[::1]:80"), EINA_TRUE);
1043 _resolve_cleanup(&ctx);
1044
1045 _resolve(&ctx, "localhost:80", 0, 0);
1046 ck_assert_int_eq(ctx.err, 0);
1047 ck_assert_int_eq(_resolve_found(&ctx, "[::1]:80"), EINA_TRUE);
1048 _resolve_cleanup(&ctx);
1049
1050 _resolve(&ctx, "localhost:http", AF_INET6, 0);
1051 ck_assert_int_eq(ctx.err, 0);
1052 ck_assert_int_eq(_resolve_found(&ctx, "127.0.0.1:80"), EINA_FALSE);
1053 _resolve_cleanup(&ctx);
1054
1055 ecore_con_shutdown();
1056}
1057END_TEST
1058
1059START_TEST(ecore_test_efl_net_ip_address_ipv6_resolve_fail)
1060{
1061 struct resolve_ctx ctx = { };
1062
1063 ecore_con_init();
1064
1065 /* generic checks at ipv4_resolve_fail */
1066
1067#if defined(AI_V4MAPPED) && (AI_V4MAPPED > 0)
1068 _resolve(&ctx, "127.0.0.1:http", AF_INET6, AI_CANONNAME); /* do NOT set V4MAPPED, but use non-zero */
1069 ck_assert_int_eq(ctx.err, EFL_NET_ERROR_COULDNT_RESOLVE_HOST);
1070 _resolve_cleanup(&ctx);
1071#endif
1072
1073 ecore_con_shutdown();
1074}
1075END_TEST
1076
1077START_TEST(ecore_test_efl_net_ip_address_ipv6_checks)
1078{
1079 const struct test {
1080 const char *str;
1081 } *itr, tests[] = {
1082#define TEST(_addr) { .str = _addr }
1083 TEST("::1"),
1084 TEST("::"),
1085 TEST("1:2:3:4:5:6:7:8"),
1086 TEST("::ffff:192.168.0.1"),
1087 TEST("::192.168.0.1"),
1088 TEST("fe80::1"),
1089 TEST("fc00::2"),
1090#undef TEST
1091 };
1092
1093 ecore_con_init();
1094
1095 for (itr = tests; itr < tests + sizeof(tests)/sizeof(tests[0]); itr++)
1096 {
1097 struct sockaddr_in6 a = {
1098 .sin6_family = AF_INET6,
1099 .sin6_port = 0,
1100 };
1101 const struct in6_addr any = { };
1102 struct in6_addr *ia = &a.sin6_addr;
1103 int r;
1104
1105 r = inet_pton(AF_INET6, itr->str, ia);
1106 ck_assert_int_eq(r, 1);
1107
1108 Eo *o = efl_net_ip_address_create_sockaddr(EFL_NET_IP_ADDRESS_CLASS, &a);
1109 ck_assert_ptr_ne(o, NULL);
1110
1111 ck_assert_int_eq(efl_net_ip_address_ipv4_class_a_check(o), EINA_FALSE);
1112 ck_assert_int_eq(efl_net_ip_address_ipv4_class_b_check(o), EINA_FALSE);
1113 ck_assert_int_eq(efl_net_ip_address_ipv4_class_c_check(o), EINA_FALSE);
1114 ck_assert_int_eq(efl_net_ip_address_ipv4_class_d_check(o), EINA_FALSE);
1115
1116 ck_assert_int_eq(efl_net_ip_address_multicast_check(o), IN6_IS_ADDR_MULTICAST(ia));
1117 ck_assert_int_eq(efl_net_ip_address_loopback_check(o), IN6_IS_ADDR_LOOPBACK(ia));
1118 ck_assert_int_eq(efl_net_ip_address_any_check(o), memcmp(ia, &any, 16) == 0);
1119
1120 ck_assert_int_eq(efl_net_ip_address_ipv6_v4mapped_check(o), IN6_IS_ADDR_V4MAPPED(ia));
1121 ck_assert_int_eq(efl_net_ip_address_ipv6_v4compat_check(o), IN6_IS_ADDR_V4COMPAT(ia));
1122 ck_assert_int_eq(efl_net_ip_address_ipv6_local_link_check(o), IN6_IS_ADDR_LINKLOCAL(ia));
1123 ck_assert_int_eq(efl_net_ip_address_ipv6_local_site_check(o), IN6_IS_ADDR_SITELOCAL(ia));
1124
1125 efl_del(o);
1126 }
1127
1128 ecore_con_shutdown();
1129}
1130END_TEST
1131
1132void ecore_con_test_efl_net_ip_address(TCase *tc)
1133{
1134 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv4_manual_ok);
1135 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv4_manual_fail);
1136 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv4_create_ok);
1137 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv4_create_fail);
1138 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv4_create_sockaddr_ok);
1139 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv4_create_sockaddr_fail);
1140 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv4_parse_ok);
1141 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv4_parse_fail);
1142 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv4_resolve_ok);
1143 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv4_resolve_fail);
1144 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv4_checks);
1145 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv6_manual_ok);
1146 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv6_manual_fail);
1147 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv6_create_ok);
1148 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv6_create_sockaddr_ok);
1149 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv6_parse_ok);
1150 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv6_parse_fail);
1151 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv6_resolve_ok);
1152 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv6_resolve_fail);
1153 tcase_add_test(tc, ecore_test_efl_net_ip_address_ipv6_checks);
1154}