summaryrefslogtreecommitdiff
path: root/src/tests/ecore_con
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>2016-12-12 10:54:20 -0200
committerGustavo Sverzut Barbieri <barbieri@profusion.mobi>2016-12-12 11:22:16 -0200
commit8415626397573e69b2ca8c42a72625c14607d0c7 (patch)
treef30f035aa09bf960ef9940bdfb4159fc67926e47 /src/tests/ecore_con
parent7e648c46df3a5792cd3b4b81c9a0301b943f6bc3 (diff)
test efl_net_ip_address: check if /etc/hosts has '::1 localhost'
it seems that the bot and some users do not have ::1 localhost in /etc/hosts, check for that and print it out. be more verbose on resolve errors, showing the results and error code. also allow more time for resolution, otherwise the local timeout may expire before getaddrinfo() returns with failures.
Diffstat (limited to 'src/tests/ecore_con')
-rw-r--r--src/tests/ecore_con/ecore_con_test_efl_net_ip_address.c202
1 files changed, 159 insertions, 43 deletions
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
index d46e31b..4f76f09 100644
--- 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
@@ -19,6 +19,8 @@
19# include <Evil.h> 19# include <Evil.h>
20#endif 20#endif
21 21
22#include <ctype.h>
23
22#include "ecore_con_suite.h" 24#include "ecore_con_suite.h"
23 25
24struct log_ctx { 26struct log_ctx {
@@ -155,7 +157,7 @@ _resolve_found(const struct resolve_ctx *ctx, const char *string)
155 unsigned int i; 157 unsigned int i;
156 const Efl_Net_Ip_Address *o; 158 const Efl_Net_Ip_Address *o;
157 159
158 mark_point(); 160 ck_assert_ptr_ne(ctx->results, NULL);
159 161
160 EINA_ARRAY_ITER_NEXT(ctx->results, i, o, it) 162 EINA_ARRAY_ITER_NEXT(ctx->results, i, o, it)
161 { 163 {
@@ -166,6 +168,41 @@ _resolve_found(const struct resolve_ctx *ctx, const char *string)
166 return EINA_FALSE; 168 return EINA_FALSE;
167} 169}
168 170
171#define _assert_found(...) _assert_found_internal(__FILE__, __LINE__, __VA_ARGS__)
172static void
173_assert_found_internal(const char *file, int line, const struct resolve_ctx *ctx, const char *string, Eina_Bool expected, Eina_Error err)
174{
175 Eina_Bool found;
176 Eina_Array_Iterator it;
177 unsigned int i;
178 const Efl_Net_Ip_Address *o;
179
180 if (ctx->err != err)
181 _ck_assert_failed(file, line, "Failed",
182 "Expected error=%d (%s), got %d (%s) resolving=%s",
183 err, err ? eina_error_msg_get(err) : "success",
184 ctx->err, ctx->err ? eina_error_msg_get(ctx->err) : "success",
185 string,
186 NULL);
187
188 if (err) return;
189
190 found = _resolve_found(ctx, string);
191 if (found == expected) return;
192
193 fprintf(stderr, "ERROR: did%s expect '%s' in results:\n",
194 expected ? "" : " NOT", string);
195
196 EINA_ARRAY_ITER_NEXT(ctx->results, i, o, it)
197 fprintf(stderr, "result %u: %s\n", i, efl_net_ip_address_string_get(o));
198
199 _ck_assert_failed(file, line, "Failed",
200 "Expected found=%hhu, got %hhu resolving=%s",
201 expected, found,
202 string,
203 NULL);
204}
205
169static void 206static void
170_resolve_done(void *data, const Efl_Event *event) 207_resolve_done(void *data, const Efl_Event *event)
171{ 208{
@@ -214,7 +251,7 @@ _resolve(struct resolve_ctx *ctx, const char *address, int family, int flags)
214 ck_assert_ptr_ne(ctx->future, NULL); 251 ck_assert_ptr_ne(ctx->future, NULL);
215 efl_future_then(ctx->future, _resolve_done, _resolve_failed, NULL, ctx); 252 efl_future_then(ctx->future, _resolve_done, _resolve_failed, NULL, ctx);
216 253
217 LOOP_WITH_TIMEOUT(5); 254 LOOP_WITH_TIMEOUT(10);
218} 255}
219 256
220/* IPv4 *****************************************************************/ 257/* IPv4 *****************************************************************/
@@ -560,38 +597,31 @@ START_TEST(ecore_test_efl_net_ip_address_ipv4_resolve_ok)
560 ecore_con_init(); 597 ecore_con_init();
561 598
562 _resolve(&ctx, "localhost:http", 0, 0); 599 _resolve(&ctx, "localhost:http", 0, 0);
563 ck_assert_int_eq(ctx.err, 0); 600 _assert_found(&ctx, "127.0.0.1:80", EINA_TRUE, 0);
564 ck_assert_int_eq(_resolve_found(&ctx, "127.0.0.1:80"), EINA_TRUE);
565 _resolve_cleanup(&ctx); 601 _resolve_cleanup(&ctx);
566 602
567 _resolve(&ctx, "localhost", 0, 0); 603 _resolve(&ctx, "localhost", 0, 0);
568 ck_assert_int_eq(ctx.err, 0); 604 _assert_found(&ctx, "127.0.0.1", EINA_TRUE, 0);
569 ck_assert_int_eq(_resolve_found(&ctx, "127.0.0.1"), EINA_TRUE);
570 _resolve_cleanup(&ctx); 605 _resolve_cleanup(&ctx);
571 606
572 _resolve(&ctx, "127.0.0.1", 0, 0); 607 _resolve(&ctx, "127.0.0.1", 0, 0);
573 ck_assert_int_eq(ctx.err, 0); 608 _assert_found(&ctx, "127.0.0.1", EINA_TRUE, 0);
574 ck_assert_int_eq(_resolve_found(&ctx, "127.0.0.1"), EINA_TRUE);
575 _resolve_cleanup(&ctx); 609 _resolve_cleanup(&ctx);
576 610
577 _resolve(&ctx, "127.0.0.1:http", 0, 0); 611 _resolve(&ctx, "127.0.0.1:http", 0, 0);
578 ck_assert_int_eq(ctx.err, 0); 612 _assert_found(&ctx, "127.0.0.1:80", EINA_TRUE, 0);
579 ck_assert_int_eq(_resolve_found(&ctx, "127.0.0.1:80"), EINA_TRUE);
580 _resolve_cleanup(&ctx); 613 _resolve_cleanup(&ctx);
581 614
582 _resolve(&ctx, "127.0.0.1:80", 0, 0); 615 _resolve(&ctx, "127.0.0.1:80", 0, 0);
583 ck_assert_int_eq(ctx.err, 0); 616 _assert_found(&ctx, "127.0.0.1:80", EINA_TRUE, 0);
584 ck_assert_int_eq(_resolve_found(&ctx, "127.0.0.1:80"), EINA_TRUE);
585 _resolve_cleanup(&ctx); 617 _resolve_cleanup(&ctx);
586 618
587 _resolve(&ctx, "localhost:80", 0, 0); 619 _resolve(&ctx, "localhost:80", 0, 0);
588 ck_assert_int_eq(ctx.err, 0); 620 _assert_found(&ctx, "127.0.0.1:80", EINA_TRUE, 0);
589 ck_assert_int_eq(_resolve_found(&ctx, "127.0.0.1:80"), EINA_TRUE);
590 _resolve_cleanup(&ctx); 621 _resolve_cleanup(&ctx);
591 622
592 _resolve(&ctx, "localhost:http", AF_INET, 0); 623 _resolve(&ctx, "localhost:http", AF_INET, 0);
593 ck_assert_int_eq(ctx.err, 0); 624 _assert_found(&ctx, "[::1]:80", EINA_FALSE, 0);
594 ck_assert_int_eq(_resolve_found(&ctx, "[::1]:80"), EINA_FALSE);
595 _resolve_cleanup(&ctx); 625 _resolve_cleanup(&ctx);
596 626
597 ecore_con_shutdown(); 627 ecore_con_shutdown();
@@ -617,11 +647,11 @@ START_TEST(ecore_test_efl_net_ip_address_ipv4_resolve_fail)
617 TRAP_ERRORS_FINISH(1); 647 TRAP_ERRORS_FINISH(1);
618 648
619 _resolve(&ctx, "xxlocalhost:xxhttp", 0, 0); 649 _resolve(&ctx, "xxlocalhost:xxhttp", 0, 0);
620 ck_assert_int_eq(ctx.err, EFL_NET_ERROR_COULDNT_RESOLVE_HOST); 650 _assert_found(&ctx, "xxlocalhost:xxhttp", EINA_FALSE, EFL_NET_ERROR_COULDNT_RESOLVE_HOST);
621 _resolve_cleanup(&ctx); 651 _resolve_cleanup(&ctx);
622 652
623 _resolve(&ctx, "[::1]:http", AF_INET, 0); 653 _resolve(&ctx, "[::1]:http", AF_INET, 0);
624 ck_assert_int_eq(ctx.err, EFL_NET_ERROR_COULDNT_RESOLVE_HOST); 654 _assert_found(&ctx, "[::1]:http", EINA_FALSE, EFL_NET_ERROR_COULDNT_RESOLVE_HOST);
625 _resolve_cleanup(&ctx); 655 _resolve_cleanup(&ctx);
626 656
627 ecore_con_shutdown(); 657 ecore_con_shutdown();
@@ -1004,53 +1034,139 @@ START_TEST(ecore_test_efl_net_ip_address_ipv6_parse_fail)
1004} 1034}
1005END_TEST 1035END_TEST
1006 1036
1037static Eina_Bool
1038_ipv6_localhost_check(void)
1039{
1040#ifndef ETC_HOSTS
1041#ifdef _WIN32
1042#define ETC_HOSTS "C:\\Windows\\System32\\Drivers\\etc\\hosts"
1043#else
1044#define ETC_HOSTS "/etc/hosts"
1045#endif
1046#endif
1047 Eina_File *f;
1048 Eina_Bool has = EINA_FALSE;
1049 Eina_Iterator *it;
1050 Eina_File_Line *line;
1051
1052 f = eina_file_open(ETC_HOSTS, EINA_FALSE);
1053 if (!f)
1054 {
1055 fprintf(stderr, "WARNING: your system misses %s: %s\n", ETC_HOSTS, eina_error_msg_get(eina_error_get() ? : errno));
1056 return EINA_FALSE;
1057 }
1058
1059 it = eina_file_map_lines(f);
1060 if (!it)
1061 {
1062 fprintf(stderr, "ERROR: could not map lines of %s\n", ETC_HOSTS);
1063 goto end;
1064 }
1065 EINA_ITERATOR_FOREACH(it, line)
1066 {
1067 const char *p;
1068
1069 if (line->length < strlen("::1 localhost")) continue;
1070
1071 for (p = line->start; p < line->end; p++)
1072 if (!isspace(p[0])) break;
1073
1074 if (p >= line->end) continue;
1075 if (p[0] == '#') continue;
1076 if ((size_t)(line->end - p) < strlen("::1 localhost")) continue;
1077 if (memcmp(p, "::1", strlen("::1")) != 0) continue;
1078
1079 p += strlen("::1");
1080
1081 while (p < line->end)
1082 {
1083 const char *e;
1084
1085 if (!isspace(p[0]))
1086 {
1087 p = line->end;
1088 break;
1089 }
1090 p++;
1091
1092 for (; p < line->end; p++)
1093 if (!isspace(p[0])) break;
1094 if (p >= line->end) break;
1095 if (p[0] == '#')
1096 {
1097 p = line->end;
1098 break;
1099 }
1100
1101 for (e = p; e < line->end; e++)
1102 if (isspace(e[0])) break;
1103
1104 if (e > p)
1105 {
1106 size_t len = e - p;
1107 if ((len == strlen("localhost")) && (memcmp(p, "localhost", strlen("localhost")) == 0))
1108 has = EINA_TRUE;
1109 }
1110 p = e;
1111 }
1112 if (has) break;
1113 }
1114 eina_iterator_free(it);
1115
1116 end:
1117 if (!has) fprintf(stderr, "WARNING: your system miss '::1 localhost' in %s\n", ETC_HOSTS);
1118 eina_file_close(f);
1119 return has;
1120}
1121
1007START_TEST(ecore_test_efl_net_ip_address_ipv6_resolve_ok) 1122START_TEST(ecore_test_efl_net_ip_address_ipv6_resolve_ok)
1008{ 1123{
1009 struct resolve_ctx ctx = { }; 1124 struct resolve_ctx ctx = { };
1125 Eina_Bool has_localhost_ipv6;
1010 1126
1011 ecore_con_init(); 1127 ecore_con_init();
1012 1128
1013 _resolve(&ctx, "localhost:http", 0, 0); 1129 has_localhost_ipv6 = _ipv6_localhost_check();
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 1130
1018 _resolve(&ctx, "localhost", 0, 0); 1131 if (has_localhost_ipv6)
1019 ck_assert_int_eq(ctx.err, 0); 1132 {
1020 ck_assert_int_eq(_resolve_found(&ctx, "[::1]"), EINA_TRUE); 1133 _resolve(&ctx, "localhost:http", 0, 0);
1021 _resolve_cleanup(&ctx); 1134 _assert_found(&ctx, "[::1]:80", EINA_TRUE, 0);
1135 _resolve_cleanup(&ctx);
1136
1137 _resolve(&ctx, "localhost", 0, 0);
1138 _assert_found(&ctx, "[::1]", EINA_TRUE, 0);
1139 _resolve_cleanup(&ctx);
1140 }
1022 1141
1023 _resolve(&ctx, "::1", 0, 0); 1142 _resolve(&ctx, "::1", 0, 0);
1024 ck_assert_int_eq(ctx.err, 0); 1143 _assert_found(&ctx, "[::1]", EINA_TRUE, 0);
1025 ck_assert_int_eq(_resolve_found(&ctx, "[::1]"), EINA_TRUE);
1026 _resolve_cleanup(&ctx); 1144 _resolve_cleanup(&ctx);
1027 1145
1028#if defined(AI_V4MAPPED) && (AI_V4MAPPED > 0) 1146#if defined(AI_V4MAPPED) && (AI_V4MAPPED > 0)
1029 _resolve(&ctx, "127.0.0.1", AF_INET6, AI_V4MAPPED); 1147 _resolve(&ctx, "127.0.0.1", AF_INET6, AI_V4MAPPED);
1030 ck_assert_int_eq(ctx.err, 0); 1148 _assert_found(&ctx, "[::ffff:127.0.0.1]", EINA_TRUE, 0);
1031 ck_assert_int_eq(_resolve_found(&ctx, "[::ffff:127.0.0.1]"), EINA_TRUE);
1032 _resolve_cleanup(&ctx); 1149 _resolve_cleanup(&ctx);
1033#endif 1150#endif
1034 1151
1035 _resolve(&ctx, "[::1]:http", 0, 0); 1152 _resolve(&ctx, "[::1]:http", 0, 0);
1036 ck_assert_int_eq(ctx.err, 0); 1153 _assert_found(&ctx, "[::1]:80", EINA_TRUE, 0);
1037 ck_assert_int_eq(_resolve_found(&ctx, "[::1]:80"), EINA_TRUE);
1038 _resolve_cleanup(&ctx); 1154 _resolve_cleanup(&ctx);
1039 1155
1040 _resolve(&ctx, "[::1]:80", 0, 0); 1156 _resolve(&ctx, "[::1]:80", 0, 0);
1041 ck_assert_int_eq(ctx.err, 0); 1157 _assert_found(&ctx, "[::1]:80", EINA_TRUE, 0);
1042 ck_assert_int_eq(_resolve_found(&ctx, "[::1]:80"), EINA_TRUE);
1043 _resolve_cleanup(&ctx); 1158 _resolve_cleanup(&ctx);
1044 1159
1045 _resolve(&ctx, "localhost:80", 0, 0); 1160 if (has_localhost_ipv6)
1046 ck_assert_int_eq(ctx.err, 0); 1161 {
1047 ck_assert_int_eq(_resolve_found(&ctx, "[::1]:80"), EINA_TRUE); 1162 _resolve(&ctx, "localhost:80", 0, 0);
1048 _resolve_cleanup(&ctx); 1163 _assert_found(&ctx, "[::1]:80", EINA_TRUE, 0);
1164 _resolve_cleanup(&ctx);
1049 1165
1050 _resolve(&ctx, "localhost:http", AF_INET6, 0); 1166 _resolve(&ctx, "localhost:http", AF_INET6, 0);
1051 ck_assert_int_eq(ctx.err, 0); 1167 _assert_found(&ctx, "127.0.0.1:80", EINA_FALSE, 0);
1052 ck_assert_int_eq(_resolve_found(&ctx, "127.0.0.1:80"), EINA_FALSE); 1168 _resolve_cleanup(&ctx);
1053 _resolve_cleanup(&ctx); 1169 }
1054 1170
1055 ecore_con_shutdown(); 1171 ecore_con_shutdown();
1056} 1172}
@@ -1066,7 +1182,7 @@ START_TEST(ecore_test_efl_net_ip_address_ipv6_resolve_fail)
1066 1182
1067#if defined(AI_V4MAPPED) && (AI_V4MAPPED > 0) 1183#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 */ 1184 _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); 1185 _assert_found(&ctx, "127.0.0.1:http", EINA_FALSE, EFL_NET_ERROR_COULDNT_RESOLVE_HOST);
1070 _resolve_cleanup(&ctx); 1186 _resolve_cleanup(&ctx);
1071#endif 1187#endif
1072 1188