1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "src/core/lib/iomgr/port.h"
20
21 // This test won't work except with posix sockets enabled
22 #ifdef GRPC_POSIX_SOCKET_EV
23
24 #include <string.h>
25
26 #include <vector>
27
28 #include "absl/strings/str_format.h"
29 #include "absl/strings/str_join.h"
30 #include "absl/strings/str_split.h"
31 #include "absl/strings/string_view.h"
32
33 #include <grpc/grpc.h>
34 #include <grpc/support/alloc.h>
35 #include <grpc/support/log.h>
36 #include <grpc/support/string_util.h>
37
38 #include "src/core/lib/gpr/string.h"
39 #include "src/core/lib/gprpp/host_port.h"
40 #include "src/core/lib/iomgr/error.h"
41 #include "src/core/lib/iomgr/resolve_address.h"
42 #include "src/core/lib/iomgr/sockaddr_utils.h"
43 #include "src/core/lib/iomgr/socket_utils_posix.h"
44 #include "src/core/lib/slice/slice_string_helpers.h"
45 #include "test/core/end2end/cq_verifier.h"
46 #include "test/core/util/port.h"
47 #include "test/core/util/test_config.h"
48
49 /* This test exercises IPv4, IPv6, and dualstack sockets in various ways. */
50
tag(intptr_t i)51 static void* tag(intptr_t i) { return (void*)i; }
52
drain_cq(grpc_completion_queue * cq)53 static void drain_cq(grpc_completion_queue* cq) {
54 grpc_event ev;
55 do {
56 ev = grpc_completion_queue_next(
57 cq, grpc_timeout_milliseconds_to_deadline(5000), nullptr);
58 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
59 }
60
log_resolved_addrs(const char * label,const char * hostname)61 static void log_resolved_addrs(const char* label, const char* hostname) {
62 grpc_resolved_addresses* res = nullptr;
63 grpc_error* error = grpc_blocking_resolve_address(hostname, "80", &res);
64 if (error != GRPC_ERROR_NONE || res == nullptr) {
65 GRPC_LOG_IF_ERROR(hostname, error);
66 return;
67 }
68 for (size_t i = 0; i < res->naddrs; ++i) {
69 gpr_log(GPR_INFO, "%s: %s", label,
70 grpc_sockaddr_to_uri(&res->addrs[i]).c_str());
71 }
72 grpc_resolved_addresses_destroy(res);
73 }
74
test_connect(const char * server_host,const char * client_host,int port,int expect_ok)75 void test_connect(const char* server_host, const char* client_host, int port,
76 int expect_ok) {
77 grpc_channel* client;
78 grpc_server* server;
79 grpc_completion_queue* cq;
80 grpc_completion_queue* shutdown_cq;
81 grpc_call* c;
82 grpc_call* s;
83 cq_verifier* cqv;
84 gpr_timespec deadline;
85 int got_port;
86 grpc_op ops[6];
87 grpc_op* op;
88 grpc_metadata_array initial_metadata_recv;
89 grpc_metadata_array trailing_metadata_recv;
90 grpc_metadata_array request_metadata_recv;
91 grpc_status_code status;
92 grpc_call_error error;
93 grpc_slice details;
94 int was_cancelled = 2;
95 grpc_call_details call_details;
96 char* peer;
97 int picked_port = 0;
98
99 if (port == 0) {
100 port = grpc_pick_unused_port_or_die();
101 picked_port = 1;
102 }
103
104 std::string server_hostport = grpc_core::JoinHostPort(server_host, port);
105
106 grpc_metadata_array_init(&initial_metadata_recv);
107 grpc_metadata_array_init(&trailing_metadata_recv);
108 grpc_metadata_array_init(&request_metadata_recv);
109 grpc_call_details_init(&call_details);
110
111 /* Create server. */
112 cq = grpc_completion_queue_create_for_next(nullptr);
113 server = grpc_server_create(nullptr, nullptr);
114 grpc_server_register_completion_queue(server, cq, nullptr);
115 GPR_ASSERT((got_port = grpc_server_add_insecure_http2_port(
116 server, server_hostport.c_str())) > 0);
117 if (port == 0) {
118 port = got_port;
119 } else {
120 GPR_ASSERT(port == got_port);
121 }
122 grpc_server_start(server);
123 cqv = cq_verifier_create(cq);
124
125 /* Create client. */
126 std::string client_hostport;
127 if (client_host[0] == 'i') {
128 /* for ipv4:/ipv6: addresses, concatenate the port to each of the parts */
129 std::vector<absl::string_view> uri_parts =
130 absl::StrSplit(client_host, ",", absl::SkipEmpty());
131 std::vector<std::string> hosts_with_port;
132 hosts_with_port.reserve(uri_parts.size());
133 for (const absl::string_view& uri_part : uri_parts) {
134 hosts_with_port.push_back(absl::StrFormat("%s:%d", uri_part, port));
135 }
136 client_hostport = absl::StrJoin(hosts_with_port, ",");
137 } else {
138 client_hostport = grpc_core::JoinHostPort(client_host, port);
139 }
140 client =
141 grpc_insecure_channel_create(client_hostport.c_str(), nullptr, nullptr);
142
143 gpr_log(GPR_INFO, "Testing with server=%s client=%s (expecting %s)",
144 server_hostport.c_str(), client_hostport.c_str(),
145 expect_ok ? "success" : "failure");
146 log_resolved_addrs("server resolved addr", server_host);
147 log_resolved_addrs("client resolved addr", client_host);
148
149 if (expect_ok) {
150 /* Normal deadline, shouldn't be reached. */
151 deadline = grpc_timeout_milliseconds_to_deadline(60000);
152 } else {
153 /* Give up faster when failure is expected.
154 BUG: Setting this to 1000 reveals a memory leak (b/18608927). */
155 deadline = grpc_timeout_milliseconds_to_deadline(8000);
156 }
157
158 /* Send a trivial request. */
159 grpc_slice host = grpc_slice_from_static_string("foo.test.google.fr");
160 c = grpc_channel_create_call(client, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
161 grpc_slice_from_static_string("/foo"), &host,
162 deadline, nullptr);
163 GPR_ASSERT(c);
164
165 memset(ops, 0, sizeof(ops));
166 op = ops;
167 op->op = GRPC_OP_SEND_INITIAL_METADATA;
168 op->data.send_initial_metadata.count = 0;
169 op->flags = expect_ok ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0;
170 op->reserved = nullptr;
171 op++;
172 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
173 op->flags = 0;
174 op->reserved = nullptr;
175 op++;
176 op->op = GRPC_OP_RECV_INITIAL_METADATA;
177 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
178 op->flags = 0;
179 op->reserved = nullptr;
180 op++;
181 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
182 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
183 op->data.recv_status_on_client.status = &status;
184 op->data.recv_status_on_client.status_details = &details;
185 op->flags = 0;
186 op->reserved = nullptr;
187 op++;
188 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
189 nullptr);
190 GPR_ASSERT(GRPC_CALL_OK == error);
191
192 if (expect_ok) {
193 /* Check for a successful request. */
194 error = grpc_server_request_call(server, &s, &call_details,
195 &request_metadata_recv, cq, cq, tag(101));
196 GPR_ASSERT(GRPC_CALL_OK == error);
197 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
198 cq_verify(cqv);
199
200 memset(ops, 0, sizeof(ops));
201 op = ops;
202 op->op = GRPC_OP_SEND_INITIAL_METADATA;
203 op->data.send_initial_metadata.count = 0;
204 op->flags = 0;
205 op++;
206 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
207 op->data.send_status_from_server.trailing_metadata_count = 0;
208 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
209 grpc_slice status_details = grpc_slice_from_static_string("xyz");
210 op->data.send_status_from_server.status_details = &status_details;
211 op->flags = 0;
212 op++;
213 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
214 op->data.recv_close_on_server.cancelled = &was_cancelled;
215 op->flags = 0;
216 op++;
217 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops),
218 tag(102), nullptr);
219 GPR_ASSERT(GRPC_CALL_OK == error);
220
221 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
222 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
223 cq_verify(cqv);
224
225 peer = grpc_call_get_peer(c);
226 gpr_log(GPR_DEBUG, "got peer: '%s'", peer);
227 gpr_free(peer);
228
229 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
230 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
231 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
232 GPR_ASSERT(0 ==
233 grpc_slice_str_cmp(call_details.host, "foo.test.google.fr"));
234 GPR_ASSERT(was_cancelled == 0);
235
236 grpc_call_unref(s);
237 } else {
238 /* Check for a failed connection. */
239 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
240 cq_verify(cqv);
241
242 gpr_log(GPR_INFO, "status: %d (expected: %d)", status,
243 GRPC_STATUS_UNAVAILABLE);
244 GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
245 }
246
247 grpc_call_unref(c);
248
249 cq_verifier_destroy(cqv);
250
251 /* Destroy client. */
252 grpc_channel_destroy(client);
253
254 /* Destroy server. */
255 shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
256 grpc_server_shutdown_and_notify(server, shutdown_cq, tag(1000));
257 GPR_ASSERT(grpc_completion_queue_pluck(shutdown_cq, tag(1000),
258 grpc_timeout_seconds_to_deadline(5),
259 nullptr)
260 .type == GRPC_OP_COMPLETE);
261 grpc_server_destroy(server);
262 grpc_completion_queue_destroy(shutdown_cq);
263 grpc_completion_queue_shutdown(cq);
264 drain_cq(cq);
265 grpc_completion_queue_destroy(cq);
266
267 grpc_metadata_array_destroy(&initial_metadata_recv);
268 grpc_metadata_array_destroy(&trailing_metadata_recv);
269 grpc_metadata_array_destroy(&request_metadata_recv);
270
271 grpc_call_details_destroy(&call_details);
272 grpc_slice_unref(details);
273 if (picked_port) {
274 grpc_recycle_unused_port(port);
275 }
276 }
277
external_dns_works(const char * host)278 int external_dns_works(const char* host) {
279 grpc_resolved_addresses* res = nullptr;
280 grpc_error* error = grpc_blocking_resolve_address(host, "80", &res);
281 GRPC_ERROR_UNREF(error);
282 if (res != nullptr) {
283 grpc_resolved_addresses_destroy(res);
284 return 1;
285 }
286 return 0;
287 }
288
main(int argc,char ** argv)289 int main(int argc, char** argv) {
290 int do_ipv6 = 1;
291
292 grpc::testing::TestEnvironment env(argc, argv);
293 grpc_init();
294
295 if (!grpc_ipv6_loopback_available()) {
296 gpr_log(GPR_INFO, "Can't bind to ::1. Skipping IPv6 tests.");
297 do_ipv6 = 0;
298 }
299
300 /* For coverage, test with and without dualstack sockets. */
301 for (grpc_forbid_dualstack_sockets_for_testing = 0;
302 grpc_forbid_dualstack_sockets_for_testing <= 1;
303 grpc_forbid_dualstack_sockets_for_testing++) {
304 /* :: and 0.0.0.0 are handled identically. */
305 test_connect("::", "127.0.0.1", 0, 1);
306 test_connect("::", "::ffff:127.0.0.1", 0, 1);
307 test_connect("::", "ipv4:127.0.0.1", 0, 1);
308 test_connect("::", "ipv6:[::ffff:127.0.0.1]", 0, 1);
309 test_connect("::", "localhost", 0, 1);
310 test_connect("0.0.0.0", "127.0.0.1", 0, 1);
311 test_connect("0.0.0.0", "::ffff:127.0.0.1", 0, 1);
312 test_connect("0.0.0.0", "ipv4:127.0.0.1", 0, 1);
313 test_connect("0.0.0.0", "ipv4:127.0.0.1,127.0.0.2,127.0.0.3", 0, 1);
314 test_connect("0.0.0.0", "ipv6:[::ffff:127.0.0.1],[::ffff:127.0.0.2]", 0, 1);
315 test_connect("0.0.0.0", "localhost", 0, 1);
316 if (do_ipv6) {
317 test_connect("::", "::1", 0, 1);
318 test_connect("0.0.0.0", "::1", 0, 1);
319 test_connect("::", "ipv6:[::1]", 0, 1);
320 test_connect("0.0.0.0", "ipv6:[::1]", 0, 1);
321 }
322
323 /* These only work when the families agree. */
324 test_connect("127.0.0.1", "127.0.0.1", 0, 1);
325 test_connect("127.0.0.1", "ipv4:127.0.0.1", 0, 1);
326 if (do_ipv6) {
327 test_connect("::1", "::1", 0, 1);
328 test_connect("::1", "127.0.0.1", 0, 0);
329 test_connect("127.0.0.1", "::1", 0, 0);
330 test_connect("::1", "ipv6:[::1]", 0, 1);
331 test_connect("::1", "ipv4:127.0.0.1", 0, 0);
332 test_connect("127.0.0.1", "ipv6:[::1]", 0, 0);
333 }
334
335 if (!external_dns_works("loopback46.unittest.grpc.io")) {
336 gpr_log(GPR_INFO, "Skipping tests that depend on *.unittest.grpc.io.");
337 } else {
338 test_connect("loopback46.unittest.grpc.io", "loopback4.unittest.grpc.io",
339 0, 1);
340 test_connect("loopback4.unittest.grpc.io", "loopback46.unittest.grpc.io",
341 0, 1);
342 if (do_ipv6) {
343 test_connect("loopback46.unittest.grpc.io",
344 "loopback6.unittest.grpc.io", 0, 1);
345 test_connect("loopback6.unittest.grpc.io",
346 "loopback46.unittest.grpc.io", 0, 1);
347 test_connect("loopback4.unittest.grpc.io", "loopback6.unittest.grpc.io",
348 0, 0);
349 test_connect("loopback6.unittest.grpc.io", "loopback4.unittest.grpc.io",
350 0, 0);
351 }
352 }
353 }
354
355 grpc_shutdown();
356
357 return 0;
358 }
359
360 #else /* GRPC_POSIX_SOCKET_EV */
361
main(int argc,char ** argv)362 int main(int argc, char** argv) { return 1; }
363
364 #endif /* GRPC_POSIX_SOCKET_EV */
365