Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * 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 :
34 : #include "src/core/iomgr/resolve_address.h"
35 : #include "src/core/iomgr/iomgr.h"
36 : #include <grpc/support/log.h>
37 : #include <grpc/support/sync.h>
38 : #include <grpc/support/time.h>
39 : #include "test/core/util/test_config.h"
40 :
41 15 : static gpr_timespec test_deadline(void) {
42 15 : return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(100);
43 : }
44 :
45 6 : static void must_succeed(grpc_exec_ctx *exec_ctx, void *evp,
46 : grpc_resolved_addresses *p) {
47 6 : GPR_ASSERT(p);
48 6 : GPR_ASSERT(p->naddrs >= 1);
49 6 : grpc_resolved_addresses_destroy(p);
50 6 : gpr_event_set(evp, (void *)1);
51 6 : }
52 :
53 9 : static void must_fail(grpc_exec_ctx *exec_ctx, void *evp,
54 : grpc_resolved_addresses *p) {
55 9 : GPR_ASSERT(!p);
56 9 : gpr_event_set(evp, (void *)1);
57 9 : }
58 :
59 1 : static void test_localhost(void) {
60 : gpr_event ev;
61 1 : gpr_event_init(&ev);
62 1 : grpc_resolve_address("localhost:1", NULL, must_succeed, &ev);
63 1 : GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
64 1 : }
65 :
66 1 : static void test_default_port(void) {
67 : gpr_event ev;
68 1 : gpr_event_init(&ev);
69 1 : grpc_resolve_address("localhost", "1", must_succeed, &ev);
70 1 : GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
71 1 : }
72 :
73 1 : static void test_missing_default_port(void) {
74 : gpr_event ev;
75 1 : gpr_event_init(&ev);
76 1 : grpc_resolve_address("localhost", NULL, must_fail, &ev);
77 1 : GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
78 1 : }
79 :
80 1 : static void test_ipv6_with_port(void) {
81 : gpr_event ev;
82 1 : gpr_event_init(&ev);
83 1 : grpc_resolve_address("[2001:db8::1]:1", NULL, must_succeed, &ev);
84 1 : GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
85 1 : }
86 :
87 1 : static void test_ipv6_without_port(void) {
88 1 : const char *const kCases[] = {
89 : "2001:db8::1", "2001:db8::1.2.3.4", "[2001:db8::1]",
90 : };
91 : unsigned i;
92 4 : for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) {
93 : gpr_event ev;
94 3 : gpr_event_init(&ev);
95 3 : grpc_resolve_address(kCases[i], "80", must_succeed, &ev);
96 3 : GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
97 : }
98 1 : }
99 :
100 1 : static void test_invalid_ip_addresses(void) {
101 1 : const char *const kCases[] = {
102 : "293.283.1238.3:1", "[2001:db8::11111]:1",
103 : };
104 : unsigned i;
105 3 : for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) {
106 : gpr_event ev;
107 2 : gpr_event_init(&ev);
108 2 : grpc_resolve_address(kCases[i], NULL, must_fail, &ev);
109 2 : GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
110 : }
111 1 : }
112 :
113 1 : static void test_unparseable_hostports(void) {
114 1 : const char *const kCases[] = {
115 : "[", "[::1", "[::1]bad", "[1.2.3.4]", "[localhost]", "[localhost]:1",
116 : };
117 : unsigned i;
118 7 : for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) {
119 : gpr_event ev;
120 6 : gpr_event_init(&ev);
121 6 : grpc_resolve_address(kCases[i], "1", must_fail, &ev);
122 6 : GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
123 : }
124 1 : }
125 :
126 1 : int main(int argc, char **argv) {
127 1 : grpc_test_init(argc, argv);
128 1 : grpc_iomgr_init();
129 1 : test_localhost();
130 1 : test_default_port();
131 1 : test_missing_default_port();
132 1 : test_ipv6_with_port();
133 1 : test_ipv6_without_port();
134 1 : test_invalid_ip_addresses();
135 1 : test_unparseable_hostports();
136 1 : grpc_iomgr_shutdown();
137 1 : return 0;
138 : }
|