blob: a695010a1d0be17d24ca0e50e7b7521c252e95c1 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/iomgr/resolve_address.h"
Yuchen Zengb4080a22016-11-17 22:03:56 -080035#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036#include <grpc/support/log.h>
37#include <grpc/support/sync.h>
Yuchen Zeng15521de2016-11-17 20:39:27 -080038#include <grpc/support/thd.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <grpc/support/time.h>
Craig Tiller9533d042016-03-25 17:11:06 -070040#include "src/core/lib/iomgr/executor.h"
murgatroid9922d50e92016-10-12 09:54:49 -070041#include "src/core/lib/iomgr/iomgr.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042#include "test/core/util/test_config.h"
43
Craig Tillera82950e2015-09-22 12:33:20 -070044static gpr_timespec test_deadline(void) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050045 return grpc_timeout_seconds_to_deadline(100);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046}
47
Craig Tillerf707d622016-05-06 14:26:12 -070048typedef struct args_struct {
49 gpr_event ev;
50 grpc_resolved_addresses *addrs;
Yuchen Zeng15521de2016-11-17 20:39:27 -080051 gpr_atm done_atm;
52 gpr_mu *mu;
53 grpc_pollset *pollset;
54 grpc_pollset_set *pollset_set;
Craig Tillerf707d622016-05-06 14:26:12 -070055} args_struct;
56
Yuchen Zengb4080a22016-11-17 22:03:56 -080057static void do_nothing(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {}
58
59void args_init(grpc_exec_ctx *exec_ctx, args_struct *args) {
Craig Tillerf707d622016-05-06 14:26:12 -070060 gpr_event_init(&args->ev);
Yuchen Zeng47de64c2017-02-22 19:04:38 -080061 args->pollset = gpr_zalloc(grpc_pollset_size());
Yuchen Zeng15521de2016-11-17 20:39:27 -080062 grpc_pollset_init(args->pollset, &args->mu);
63 args->pollset_set = grpc_pollset_set_create();
Yuchen Zengb4080a22016-11-17 22:03:56 -080064 grpc_pollset_set_add_pollset(exec_ctx, args->pollset_set, args->pollset);
Craig Tillerf707d622016-05-06 14:26:12 -070065 args->addrs = NULL;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080066}
67
Yuchen Zengb4080a22016-11-17 22:03:56 -080068void args_finish(grpc_exec_ctx *exec_ctx, args_struct *args) {
Craig Tillerf707d622016-05-06 14:26:12 -070069 GPR_ASSERT(gpr_event_wait(&args->ev, test_deadline()));
70 grpc_resolved_addresses_destroy(args->addrs);
Yuchen Zengb4080a22016-11-17 22:03:56 -080071 grpc_pollset_set_del_pollset(exec_ctx, args->pollset_set, args->pollset);
Craig Tiller9e5ac1b2017-02-14 22:25:50 -080072 grpc_pollset_set_destroy(exec_ctx, args->pollset_set);
Yuchen Zengb4080a22016-11-17 22:03:56 -080073 grpc_closure do_nothing_cb;
Craig Tiller91031da2016-12-28 15:44:25 -080074 grpc_closure_init(&do_nothing_cb, do_nothing, NULL,
75 grpc_schedule_on_exec_ctx);
Yuchen Zengb4080a22016-11-17 22:03:56 -080076 grpc_pollset_shutdown(exec_ctx, args->pollset, &do_nothing_cb);
Yuchen Zeng8fe21f22016-11-21 13:34:16 -080077 // exec_ctx needs to be flushed before calling grpc_pollset_destroy()
Yuchen Zengb4080a22016-11-17 22:03:56 -080078 grpc_exec_ctx_flush(exec_ctx);
Yuchen Zeng15521de2016-11-17 20:39:27 -080079 grpc_pollset_destroy(args->pollset);
Yuchen Zengb4080a22016-11-17 22:03:56 -080080 gpr_free(args->pollset);
Yuchen Zeng15521de2016-11-17 20:39:27 -080081}
82
83static gpr_timespec n_sec_deadline(int seconds) {
84 return gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
85 gpr_time_from_seconds(seconds, GPR_TIMESPAN));
86}
87
88static void actually_poll(void *argsp) {
89 args_struct *args = argsp;
90 gpr_timespec deadline = n_sec_deadline(10);
Yuchen Zeng15521de2016-11-17 20:39:27 -080091 while (true) {
92 bool done = gpr_atm_acq_load(&args->done_atm) != 0;
93 if (done) {
94 break;
95 }
96 gpr_timespec time_left =
97 gpr_time_sub(deadline, gpr_now(GPR_CLOCK_REALTIME));
98 gpr_log(GPR_DEBUG, "done=%d, time_left=%" PRId64 ".%09d", done,
99 time_left.tv_sec, time_left.tv_nsec);
100 GPR_ASSERT(gpr_time_cmp(time_left, gpr_time_0(GPR_TIMESPAN)) >= 0);
Yuchen Zengb4080a22016-11-17 22:03:56 -0800101 grpc_pollset_worker *worker = NULL;
Yuchen Zeng15521de2016-11-17 20:39:27 -0800102 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
103 gpr_mu_lock(args->mu);
104 GRPC_LOG_IF_ERROR(
105 "pollset_work",
106 grpc_pollset_work(&exec_ctx, args->pollset, &worker,
107 gpr_now(GPR_CLOCK_REALTIME), n_sec_deadline(1)));
108 gpr_mu_unlock(args->mu);
109 grpc_exec_ctx_finish(&exec_ctx);
110 }
111 gpr_event_set(&args->ev, (void *)1);
112}
113
114static void poll_pollset_until_request_done(args_struct *args) {
115 gpr_atm_rel_store(&args->done_atm, 0);
116 gpr_thd_id id;
117 gpr_thd_new(&id, actually_poll, args, NULL);
Craig Tillerf707d622016-05-06 14:26:12 -0700118}
119
120static void must_succeed(grpc_exec_ctx *exec_ctx, void *argsp,
121 grpc_error *err) {
122 args_struct *args = argsp;
123 GPR_ASSERT(err == GRPC_ERROR_NONE);
124 GPR_ASSERT(args->addrs != NULL);
David Garcia Quintasd5520c12016-05-10 16:03:12 -0700125 GPR_ASSERT(args->addrs->naddrs > 0);
Yuchen Zeng15521de2016-11-17 20:39:27 -0800126 gpr_atm_rel_store(&args->done_atm, 1);
Craig Tillerf707d622016-05-06 14:26:12 -0700127}
128
129static void must_fail(grpc_exec_ctx *exec_ctx, void *argsp, grpc_error *err) {
130 args_struct *args = argsp;
131 GPR_ASSERT(err != GRPC_ERROR_NONE);
Yuchen Zeng15521de2016-11-17 20:39:27 -0800132 gpr_atm_rel_store(&args->done_atm, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800133}
134
Craig Tillera82950e2015-09-22 12:33:20 -0700135static void test_localhost(void) {
Craig Tillere06a81f2016-04-21 22:58:58 -0700136 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Yuchen Zengb4080a22016-11-17 22:03:56 -0800137 args_struct args;
138 args_init(&exec_ctx, &args);
Yuchen Zeng15521de2016-11-17 20:39:27 -0800139 poll_pollset_until_request_done(&args);
Craig Tiller91031da2016-12-28 15:44:25 -0800140 grpc_resolve_address(
141 &exec_ctx, "localhost:1", NULL, args.pollset_set,
142 grpc_closure_create(must_succeed, &args, grpc_schedule_on_exec_ctx),
143 &args.addrs);
Yuchen Zengb4080a22016-11-17 22:03:56 -0800144 args_finish(&exec_ctx, &args);
Craig Tillere06a81f2016-04-21 22:58:58 -0700145 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146}
147
Craig Tillera82950e2015-09-22 12:33:20 -0700148static void test_default_port(void) {
Craig Tillere06a81f2016-04-21 22:58:58 -0700149 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Yuchen Zengb4080a22016-11-17 22:03:56 -0800150 args_struct args;
151 args_init(&exec_ctx, &args);
Yuchen Zeng15521de2016-11-17 20:39:27 -0800152 poll_pollset_until_request_done(&args);
Craig Tiller91031da2016-12-28 15:44:25 -0800153 grpc_resolve_address(
154 &exec_ctx, "localhost", "1", args.pollset_set,
155 grpc_closure_create(must_succeed, &args, grpc_schedule_on_exec_ctx),
156 &args.addrs);
Yuchen Zengb4080a22016-11-17 22:03:56 -0800157 args_finish(&exec_ctx, &args);
Craig Tillere06a81f2016-04-21 22:58:58 -0700158 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800159}
160
Craig Tillera82950e2015-09-22 12:33:20 -0700161static void test_missing_default_port(void) {
Craig Tillere06a81f2016-04-21 22:58:58 -0700162 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Yuchen Zengb4080a22016-11-17 22:03:56 -0800163 args_struct args;
164 args_init(&exec_ctx, &args);
Yuchen Zeng15521de2016-11-17 20:39:27 -0800165 poll_pollset_until_request_done(&args);
Craig Tiller91031da2016-12-28 15:44:25 -0800166 grpc_resolve_address(
167 &exec_ctx, "localhost", NULL, args.pollset_set,
168 grpc_closure_create(must_fail, &args, grpc_schedule_on_exec_ctx),
169 &args.addrs);
Yuchen Zengb4080a22016-11-17 22:03:56 -0800170 args_finish(&exec_ctx, &args);
Craig Tillere06a81f2016-04-21 22:58:58 -0700171 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800172}
173
Craig Tillera82950e2015-09-22 12:33:20 -0700174static void test_ipv6_with_port(void) {
Craig Tillere06a81f2016-04-21 22:58:58 -0700175 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Yuchen Zengb4080a22016-11-17 22:03:56 -0800176 args_struct args;
177 args_init(&exec_ctx, &args);
Yuchen Zeng15521de2016-11-17 20:39:27 -0800178 poll_pollset_until_request_done(&args);
Craig Tiller91031da2016-12-28 15:44:25 -0800179 grpc_resolve_address(
180 &exec_ctx, "[2001:db8::1]:1", NULL, args.pollset_set,
181 grpc_closure_create(must_succeed, &args, grpc_schedule_on_exec_ctx),
182 &args.addrs);
Yuchen Zengb4080a22016-11-17 22:03:56 -0800183 args_finish(&exec_ctx, &args);
Craig Tillere06a81f2016-04-21 22:58:58 -0700184 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800185}
186
Craig Tillera82950e2015-09-22 12:33:20 -0700187static void test_ipv6_without_port(void) {
Craig Tiller45724b32015-09-22 10:42:19 -0700188 const char *const kCases[] = {
Craig Tillera82950e2015-09-22 12:33:20 -0700189 "2001:db8::1", "2001:db8::1.2.3.4", "[2001:db8::1]",
Craig Tiller8ad8a412015-02-25 08:36:40 -0800190 };
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100191 unsigned i;
Craig Tillera82950e2015-09-22 12:33:20 -0700192 for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) {
Craig Tillere06a81f2016-04-21 22:58:58 -0700193 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Yuchen Zengb4080a22016-11-17 22:03:56 -0800194 args_struct args;
195 args_init(&exec_ctx, &args);
Yuchen Zeng15521de2016-11-17 20:39:27 -0800196 poll_pollset_until_request_done(&args);
Craig Tiller91031da2016-12-28 15:44:25 -0800197 grpc_resolve_address(
198 &exec_ctx, kCases[i], "80", args.pollset_set,
199 grpc_closure_create(must_succeed, &args, grpc_schedule_on_exec_ctx),
200 &args.addrs);
Yuchen Zengb4080a22016-11-17 22:03:56 -0800201 args_finish(&exec_ctx, &args);
Craig Tillere06a81f2016-04-21 22:58:58 -0700202 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -0700203 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800204}
205
Craig Tillera82950e2015-09-22 12:33:20 -0700206static void test_invalid_ip_addresses(void) {
Craig Tiller45724b32015-09-22 10:42:19 -0700207 const char *const kCases[] = {
Craig Tillera82950e2015-09-22 12:33:20 -0700208 "293.283.1238.3:1", "[2001:db8::11111]:1",
Craig Tiller8ad8a412015-02-25 08:36:40 -0800209 };
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100210 unsigned i;
Craig Tillera82950e2015-09-22 12:33:20 -0700211 for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) {
Craig Tillere06a81f2016-04-21 22:58:58 -0700212 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Yuchen Zengb4080a22016-11-17 22:03:56 -0800213 args_struct args;
214 args_init(&exec_ctx, &args);
Yuchen Zeng15521de2016-11-17 20:39:27 -0800215 poll_pollset_until_request_done(&args);
Craig Tiller91031da2016-12-28 15:44:25 -0800216 grpc_resolve_address(
217 &exec_ctx, kCases[i], NULL, args.pollset_set,
218 grpc_closure_create(must_fail, &args, grpc_schedule_on_exec_ctx),
219 &args.addrs);
Yuchen Zengb4080a22016-11-17 22:03:56 -0800220 args_finish(&exec_ctx, &args);
Craig Tillere06a81f2016-04-21 22:58:58 -0700221 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -0700222 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800223}
224
Craig Tillera82950e2015-09-22 12:33:20 -0700225static void test_unparseable_hostports(void) {
Craig Tiller45724b32015-09-22 10:42:19 -0700226 const char *const kCases[] = {
Craig Tillera82950e2015-09-22 12:33:20 -0700227 "[", "[::1", "[::1]bad", "[1.2.3.4]", "[localhost]", "[localhost]:1",
Craig Tiller8ad8a412015-02-25 08:36:40 -0800228 };
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100229 unsigned i;
Craig Tillera82950e2015-09-22 12:33:20 -0700230 for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) {
Craig Tillere06a81f2016-04-21 22:58:58 -0700231 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Yuchen Zengb4080a22016-11-17 22:03:56 -0800232 args_struct args;
233 args_init(&exec_ctx, &args);
Yuchen Zeng15521de2016-11-17 20:39:27 -0800234 poll_pollset_until_request_done(&args);
Craig Tiller91031da2016-12-28 15:44:25 -0800235 grpc_resolve_address(
236 &exec_ctx, kCases[i], "1", args.pollset_set,
237 grpc_closure_create(must_fail, &args, grpc_schedule_on_exec_ctx),
238 &args.addrs);
Yuchen Zengb4080a22016-11-17 22:03:56 -0800239 args_finish(&exec_ctx, &args);
Craig Tillere06a81f2016-04-21 22:58:58 -0700240 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -0700241 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800242}
243
Craig Tillera82950e2015-09-22 12:33:20 -0700244int main(int argc, char **argv) {
245 grpc_test_init(argc, argv);
David Garcia Quintas4bc34632015-10-07 16:12:35 -0700246 grpc_executor_init();
David Garcia Quintas661ad7f2015-10-13 15:51:31 -0700247 grpc_iomgr_init();
Craig Tillera82950e2015-09-22 12:33:20 -0700248 test_localhost();
249 test_default_port();
250 test_missing_default_port();
251 test_ipv6_with_port();
252 test_ipv6_without_port();
253 test_invalid_ip_addresses();
254 test_unparseable_hostports();
Craig Tiller3cf79222016-11-14 08:02:45 -0800255 {
256 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
257 grpc_executor_shutdown(&exec_ctx);
258 grpc_iomgr_shutdown(&exec_ctx);
259 grpc_exec_ctx_finish(&exec_ctx);
260 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800261 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800262}