blob: 023d7e6c6866be144c1efdf0aeaf92d8aadb16f3 [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"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035#include <grpc/support/log.h>
36#include <grpc/support/sync.h>
37#include <grpc/support/time.h>
Craig Tiller9533d042016-03-25 17:11:06 -070038#include "src/core/lib/iomgr/executor.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include "test/core/util/test_config.h"
40
Craig Tillera82950e2015-09-22 12:33:20 -070041static gpr_timespec test_deadline(void) {
42 return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(100);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043}
44
Craig Tillerf707d622016-05-06 14:26:12 -070045typedef struct args_struct {
46 gpr_event ev;
47 grpc_resolved_addresses *addrs;
48} args_struct;
49
50void args_init(args_struct *args) {
51 gpr_event_init(&args->ev);
52 args->addrs = NULL;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053}
54
Craig Tillerf707d622016-05-06 14:26:12 -070055void args_finish(args_struct *args) {
56 GPR_ASSERT(gpr_event_wait(&args->ev, test_deadline()));
57 grpc_resolved_addresses_destroy(args->addrs);
58}
59
60static void must_succeed(grpc_exec_ctx *exec_ctx, void *argsp,
61 grpc_error *err) {
62 args_struct *args = argsp;
63 GPR_ASSERT(err == GRPC_ERROR_NONE);
64 GPR_ASSERT(args->addrs != NULL);
65 GPR_ASSERT(args->addrs->naddrs > 1);
66 gpr_event_set(&args->ev, (void *)1);
67}
68
69static void must_fail(grpc_exec_ctx *exec_ctx, void *argsp, grpc_error *err) {
70 args_struct *args = argsp;
71 GPR_ASSERT(err != GRPC_ERROR_NONE);
72 gpr_event_set(&args->ev, (void *)1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073}
74
Craig Tillera82950e2015-09-22 12:33:20 -070075static void test_localhost(void) {
Craig Tillerf707d622016-05-06 14:26:12 -070076 args_struct args;
77 args_init(&args);
Craig Tillere06a81f2016-04-21 22:58:58 -070078 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerf707d622016-05-06 14:26:12 -070079 grpc_resolve_address(&exec_ctx, "localhost:1", NULL,
80 grpc_closure_create(must_succeed, &args), &args.addrs);
Craig Tillere06a81f2016-04-21 22:58:58 -070081 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerf707d622016-05-06 14:26:12 -070082 args_finish(&args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083}
84
Craig Tillera82950e2015-09-22 12:33:20 -070085static void test_default_port(void) {
Craig Tillerf707d622016-05-06 14:26:12 -070086 args_struct args;
87 args_init(&args);
Craig Tillere06a81f2016-04-21 22:58:58 -070088 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerf707d622016-05-06 14:26:12 -070089 grpc_resolve_address(&exec_ctx, "localhost", "1",
90 grpc_closure_create(must_succeed, &args), &args.addrs);
Craig Tillere06a81f2016-04-21 22:58:58 -070091 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerf707d622016-05-06 14:26:12 -070092 args_finish(&args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080093}
94
Craig Tillera82950e2015-09-22 12:33:20 -070095static void test_missing_default_port(void) {
Craig Tillerf707d622016-05-06 14:26:12 -070096 args_struct args;
97 args_init(&args);
Craig Tillere06a81f2016-04-21 22:58:58 -070098 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerf707d622016-05-06 14:26:12 -070099 grpc_resolve_address(&exec_ctx, "localhost", NULL,
100 grpc_closure_create(must_fail, &args), &args.addrs);
Craig Tillere06a81f2016-04-21 22:58:58 -0700101 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerf707d622016-05-06 14:26:12 -0700102 args_finish(&args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800103}
104
Craig Tillera82950e2015-09-22 12:33:20 -0700105static void test_ipv6_with_port(void) {
Craig Tillerf707d622016-05-06 14:26:12 -0700106 args_struct args;
107 args_init(&args);
Craig Tillere06a81f2016-04-21 22:58:58 -0700108 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerf707d622016-05-06 14:26:12 -0700109 grpc_resolve_address(&exec_ctx, "[2001:db8::1]:1", NULL,
110 grpc_closure_create(must_succeed, &args), &args.addrs);
Craig Tillere06a81f2016-04-21 22:58:58 -0700111 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerf707d622016-05-06 14:26:12 -0700112 args_finish(&args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800113}
114
Craig Tillera82950e2015-09-22 12:33:20 -0700115static void test_ipv6_without_port(void) {
Craig Tiller45724b32015-09-22 10:42:19 -0700116 const char *const kCases[] = {
Craig Tillera82950e2015-09-22 12:33:20 -0700117 "2001:db8::1", "2001:db8::1.2.3.4", "[2001:db8::1]",
Craig Tiller8ad8a412015-02-25 08:36:40 -0800118 };
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100119 unsigned i;
Craig Tillera82950e2015-09-22 12:33:20 -0700120 for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) {
Craig Tillerf707d622016-05-06 14:26:12 -0700121 args_struct args;
122 args_init(&args);
Craig Tillere06a81f2016-04-21 22:58:58 -0700123 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerf707d622016-05-06 14:26:12 -0700124 grpc_resolve_address(&exec_ctx, kCases[i], "80",
125 grpc_closure_create(must_succeed, &args), &args.addrs);
Craig Tillere06a81f2016-04-21 22:58:58 -0700126 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerf707d622016-05-06 14:26:12 -0700127 args_finish(&args);
Craig Tillera82950e2015-09-22 12:33:20 -0700128 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800129}
130
Craig Tillera82950e2015-09-22 12:33:20 -0700131static void test_invalid_ip_addresses(void) {
Craig Tiller45724b32015-09-22 10:42:19 -0700132 const char *const kCases[] = {
Craig Tillera82950e2015-09-22 12:33:20 -0700133 "293.283.1238.3:1", "[2001:db8::11111]:1",
Craig Tiller8ad8a412015-02-25 08:36:40 -0800134 };
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100135 unsigned i;
Craig Tillera82950e2015-09-22 12:33:20 -0700136 for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) {
Craig Tillerf707d622016-05-06 14:26:12 -0700137 args_struct args;
138 args_init(&args);
Craig Tillere06a81f2016-04-21 22:58:58 -0700139 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerf707d622016-05-06 14:26:12 -0700140 grpc_resolve_address(&exec_ctx, kCases[i], NULL,
141 grpc_closure_create(must_fail, &args), &args.addrs);
Craig Tillere06a81f2016-04-21 22:58:58 -0700142 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerf707d622016-05-06 14:26:12 -0700143 args_finish(&args);
Craig Tillera82950e2015-09-22 12:33:20 -0700144 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800145}
146
Craig Tillera82950e2015-09-22 12:33:20 -0700147static void test_unparseable_hostports(void) {
Craig Tiller45724b32015-09-22 10:42:19 -0700148 const char *const kCases[] = {
Craig Tillera82950e2015-09-22 12:33:20 -0700149 "[", "[::1", "[::1]bad", "[1.2.3.4]", "[localhost]", "[localhost]:1",
Craig Tiller8ad8a412015-02-25 08:36:40 -0800150 };
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100151 unsigned i;
Craig Tillera82950e2015-09-22 12:33:20 -0700152 for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) {
Craig Tillerf707d622016-05-06 14:26:12 -0700153 args_struct args;
154 args_init(&args);
Craig Tillere06a81f2016-04-21 22:58:58 -0700155 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerf707d622016-05-06 14:26:12 -0700156 grpc_resolve_address(&exec_ctx, kCases[i], "1",
157 grpc_closure_create(must_fail, &args), &args.addrs);
Craig Tillere06a81f2016-04-21 22:58:58 -0700158 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerf707d622016-05-06 14:26:12 -0700159 args_finish(&args);
Craig Tillera82950e2015-09-22 12:33:20 -0700160 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800161}
162
Craig Tillera82950e2015-09-22 12:33:20 -0700163int main(int argc, char **argv) {
164 grpc_test_init(argc, argv);
David Garcia Quintas4bc34632015-10-07 16:12:35 -0700165 grpc_executor_init();
David Garcia Quintas661ad7f2015-10-13 15:51:31 -0700166 grpc_iomgr_init();
Craig Tillera82950e2015-09-22 12:33:20 -0700167 test_localhost();
168 test_default_port();
169 test_missing_default_port();
170 test_ipv6_with_port();
171 test_ipv6_without_port();
172 test_invalid_ip_addresses();
173 test_unparseable_hostports();
David Garcia Quintas661ad7f2015-10-13 15:51:31 -0700174 grpc_iomgr_shutdown();
David Garcia Quintas4bc34632015-10-07 16:12:35 -0700175 grpc_executor_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800176 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800177}