blob: a66edc9df2b49bac1003d5a505c77bc5dfce25d0 [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 Tillera82950e2015-09-22 12:33:20 -070045static void must_succeed(grpc_exec_ctx *exec_ctx, void *evp,
46 grpc_resolved_addresses *p) {
47 GPR_ASSERT(p);
48 GPR_ASSERT(p->naddrs >= 1);
49 grpc_resolved_addresses_destroy(p);
50 gpr_event_set(evp, (void *)1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051}
52
Craig Tillera82950e2015-09-22 12:33:20 -070053static void must_fail(grpc_exec_ctx *exec_ctx, void *evp,
54 grpc_resolved_addresses *p) {
55 GPR_ASSERT(!p);
56 gpr_event_set(evp, (void *)1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057}
58
Craig Tillera82950e2015-09-22 12:33:20 -070059static void test_localhost(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060 gpr_event ev;
Craig Tillera82950e2015-09-22 12:33:20 -070061 gpr_event_init(&ev);
62 grpc_resolve_address("localhost:1", NULL, must_succeed, &ev);
63 GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064}
65
Craig Tillera82950e2015-09-22 12:33:20 -070066static void test_default_port(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067 gpr_event ev;
Craig Tillera82950e2015-09-22 12:33:20 -070068 gpr_event_init(&ev);
69 grpc_resolve_address("localhost", "1", must_succeed, &ev);
70 GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071}
72
Craig Tillera82950e2015-09-22 12:33:20 -070073static void test_missing_default_port(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074 gpr_event ev;
Craig Tillera82950e2015-09-22 12:33:20 -070075 gpr_event_init(&ev);
76 grpc_resolve_address("localhost", NULL, must_fail, &ev);
77 GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078}
79
Craig Tillera82950e2015-09-22 12:33:20 -070080static void test_ipv6_with_port(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081 gpr_event ev;
Craig Tillera82950e2015-09-22 12:33:20 -070082 gpr_event_init(&ev);
83 grpc_resolve_address("[2001:db8::1]:1", NULL, must_succeed, &ev);
84 GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080085}
86
Craig Tillera82950e2015-09-22 12:33:20 -070087static void test_ipv6_without_port(void) {
Craig Tiller45724b32015-09-22 10:42:19 -070088 const char *const kCases[] = {
Craig Tillera82950e2015-09-22 12:33:20 -070089 "2001:db8::1", "2001:db8::1.2.3.4", "[2001:db8::1]",
Craig Tiller8ad8a412015-02-25 08:36:40 -080090 };
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +010091 unsigned i;
Craig Tillera82950e2015-09-22 12:33:20 -070092 for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) {
93 gpr_event ev;
94 gpr_event_init(&ev);
95 grpc_resolve_address(kCases[i], "80", must_succeed, &ev);
96 GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
97 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080098}
99
Craig Tillera82950e2015-09-22 12:33:20 -0700100static void test_invalid_ip_addresses(void) {
Craig Tiller45724b32015-09-22 10:42:19 -0700101 const char *const kCases[] = {
Craig Tillera82950e2015-09-22 12:33:20 -0700102 "293.283.1238.3:1", "[2001:db8::11111]:1",
Craig Tiller8ad8a412015-02-25 08:36:40 -0800103 };
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100104 unsigned i;
Craig Tillera82950e2015-09-22 12:33:20 -0700105 for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) {
106 gpr_event ev;
107 gpr_event_init(&ev);
108 grpc_resolve_address(kCases[i], NULL, must_fail, &ev);
109 GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
110 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800111}
112
Craig Tillera82950e2015-09-22 12:33:20 -0700113static void test_unparseable_hostports(void) {
Craig Tiller45724b32015-09-22 10:42:19 -0700114 const char *const kCases[] = {
Craig Tillera82950e2015-09-22 12:33:20 -0700115 "[", "[::1", "[::1]bad", "[1.2.3.4]", "[localhost]", "[localhost]:1",
Craig Tiller8ad8a412015-02-25 08:36:40 -0800116 };
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100117 unsigned i;
Craig Tillera82950e2015-09-22 12:33:20 -0700118 for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) {
119 gpr_event ev;
120 gpr_event_init(&ev);
121 grpc_resolve_address(kCases[i], "1", must_fail, &ev);
122 GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
123 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800124}
125
Craig Tillera82950e2015-09-22 12:33:20 -0700126int main(int argc, char **argv) {
127 grpc_test_init(argc, argv);
David Garcia Quintas4bc34632015-10-07 16:12:35 -0700128 grpc_executor_init();
David Garcia Quintas661ad7f2015-10-13 15:51:31 -0700129 grpc_iomgr_init();
Craig Tillera82950e2015-09-22 12:33:20 -0700130 test_localhost();
131 test_default_port();
132 test_missing_default_port();
133 test_ipv6_with_port();
134 test_ipv6_without_port();
135 test_invalid_ip_addresses();
136 test_unparseable_hostports();
David Garcia Quintas661ad7f2015-10-13 15:51:31 -0700137 grpc_iomgr_shutdown();
David Garcia Quintas4bc34632015-10-07 16:12:35 -0700138 grpc_executor_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800139 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800140}