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 <algorithm>
20 #include <memory>
21 #include <string>
22
23 #include "absl/status/statusor.h"
24
25 #include <grpc/impl/propagation_bits.h>
26 #include <grpc/slice.h>
27 #include <grpc/status.h>
28 #include <grpc/support/time.h>
29
30 #include "src/core/lib/iomgr/port.h"
31 #include "src/core/lib/iomgr/resolved_address.h"
32
33 // This test won't work except with posix sockets enabled
34 #ifdef GRPC_POSIX_SOCKET_EV
35
36 #include <string.h>
37
38 #include <vector>
39
40 #include "absl/strings/str_format.h"
41 #include "absl/strings/str_join.h"
42 #include "absl/strings/str_split.h"
43 #include "absl/strings/string_view.h"
44
45 #include <grpc/grpc.h>
46 #include <grpc/grpc_security.h>
47 #include <grpc/support/alloc.h>
48 #include <grpc/support/log.h>
49
50 #include "src/core/lib/address_utils/sockaddr_utils.h"
51 #include "src/core/lib/gprpp/host_port.h"
52 #include "src/core/lib/iomgr/error.h"
53 #include "src/core/lib/iomgr/resolve_address.h"
54 #include "src/core/lib/iomgr/socket_utils_posix.h"
55 #include "src/core/lib/transport/error_utils.h"
56 #include "test/core/end2end/cq_verifier.h"
57 #include "test/core/util/port.h"
58 #include "test/core/util/test_config.h"
59
60 // This test exercises IPv4, IPv6, and dualstack sockets in various ways.
61
drain_cq(grpc_completion_queue * cq)62 static void drain_cq(grpc_completion_queue* cq) {
63 grpc_event ev;
64 do {
65 ev = grpc_completion_queue_next(
66 cq, grpc_timeout_milliseconds_to_deadline(5000), nullptr);
67 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
68 }
69
log_resolved_addrs(const char * label,const char * hostname)70 static void log_resolved_addrs(const char* label, const char* hostname) {
71 absl::StatusOr<std::vector<grpc_resolved_address>> addresses_or =
72 grpc_core::GetDNSResolver()->LookupHostnameBlocking(hostname, "80");
73 if (!addresses_or.ok()) {
74 GRPC_LOG_IF_ERROR(hostname,
75 absl_status_to_grpc_error(addresses_or.status()));
76 return;
77 }
78 for (const auto& addr : *addresses_or) {
79 gpr_log(GPR_INFO, "%s: %s", label, grpc_sockaddr_to_uri(&addr)->c_str());
80 }
81 }
82
test_connect(const char * server_host,const char * client_host,int port,int expect_ok)83 void test_connect(const char* server_host, const char* client_host, int port,
84 int expect_ok) {
85 grpc_channel* client;
86 grpc_server* server;
87 grpc_completion_queue* cq;
88 grpc_call* c;
89 grpc_call* s;
90 gpr_timespec deadline;
91 int got_port;
92 grpc_op ops[6];
93 grpc_op* op;
94 grpc_metadata_array initial_metadata_recv;
95 grpc_metadata_array trailing_metadata_recv;
96 grpc_metadata_array request_metadata_recv;
97 grpc_status_code status;
98 grpc_call_error error;
99 grpc_slice details;
100 int was_cancelled = 2;
101 grpc_call_details call_details;
102 char* peer;
103 int picked_port = 0;
104
105 if (port == 0) {
106 port = grpc_pick_unused_port_or_die();
107 picked_port = 1;
108 }
109
110 std::string server_hostport = grpc_core::JoinHostPort(server_host, port);
111
112 grpc_metadata_array_init(&initial_metadata_recv);
113 grpc_metadata_array_init(&trailing_metadata_recv);
114 grpc_metadata_array_init(&request_metadata_recv);
115 grpc_call_details_init(&call_details);
116
117 // Create server.
118 cq = grpc_completion_queue_create_for_next(nullptr);
119 server = grpc_server_create(nullptr, nullptr);
120 grpc_server_register_completion_queue(server, cq, nullptr);
121 grpc_server_credentials* server_creds =
122 grpc_insecure_server_credentials_create();
123 GPR_ASSERT((got_port = grpc_server_add_http2_port(
124 server, server_hostport.c_str(), server_creds)) > 0);
125 grpc_server_credentials_release(server_creds);
126 if (port == 0) {
127 port = got_port;
128 } else {
129 GPR_ASSERT(port == got_port);
130 }
131 grpc_server_start(server);
132 grpc_core::CqVerifier cqv(cq);
133
134 // Create client.
135 std::string client_hostport;
136 if (client_host[0] == 'i') {
137 // for ipv4:/ipv6: addresses, concatenate the port to each of the parts
138 std::vector<absl::string_view> uri_parts =
139 absl::StrSplit(client_host, ',', absl::SkipEmpty());
140 std::vector<std::string> hosts_with_port;
141 hosts_with_port.reserve(uri_parts.size());
142 for (const absl::string_view& uri_part : uri_parts) {
143 hosts_with_port.push_back(absl::StrFormat("%s:%d", uri_part, port));
144 }
145 client_hostport = absl::StrJoin(hosts_with_port, ",");
146 } else {
147 client_hostport = grpc_core::JoinHostPort(client_host, port);
148 }
149 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
150 client = grpc_channel_create(client_hostport.c_str(), creds, nullptr);
151 grpc_channel_credentials_release(creds);
152
153 gpr_log(GPR_INFO, "Testing with server=%s client=%s (expecting %s)",
154 server_hostport.c_str(), client_hostport.c_str(),
155 expect_ok ? "success" : "failure");
156 log_resolved_addrs("server resolved addr", server_host);
157 log_resolved_addrs("client resolved addr", client_host);
158
159 if (expect_ok) {
160 // Normal deadline, shouldn't be reached.
161 deadline = grpc_timeout_milliseconds_to_deadline(60000);
162 } else {
163 // Give up faster when failure is expected.
164 // BUG: Setting this to 1000 reveals a memory leak (b/18608927).
165 deadline = grpc_timeout_milliseconds_to_deadline(8000);
166 }
167
168 // Send a trivial request.
169 grpc_slice host = grpc_slice_from_static_string("foo.test.google.fr");
170 c = grpc_channel_create_call(client, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
171 grpc_slice_from_static_string("/foo"), &host,
172 deadline, nullptr);
173 GPR_ASSERT(c);
174
175 memset(ops, 0, sizeof(ops));
176 op = ops;
177 op->op = GRPC_OP_SEND_INITIAL_METADATA;
178 op->data.send_initial_metadata.count = 0;
179 op->flags = expect_ok ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0;
180 op->reserved = nullptr;
181 op++;
182 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
183 op->flags = 0;
184 op->reserved = nullptr;
185 op++;
186 op->op = GRPC_OP_RECV_INITIAL_METADATA;
187 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
188 op->flags = 0;
189 op->reserved = nullptr;
190 op++;
191 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
192 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
193 op->data.recv_status_on_client.status = &status;
194 op->data.recv_status_on_client.status_details = &details;
195 op->flags = 0;
196 op->reserved = nullptr;
197 op++;
198 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops),
199 grpc_core::CqVerifier::tag(1), nullptr);
200 GPR_ASSERT(GRPC_CALL_OK == error);
201
202 if (expect_ok) {
203 // Check for a successful request.
204 error = grpc_server_request_call(server, &s, &call_details,
205 &request_metadata_recv, cq, cq,
206 grpc_core::CqVerifier::tag(101));
207 GPR_ASSERT(GRPC_CALL_OK == error);
208 cqv.Expect(grpc_core::CqVerifier::tag(101), true);
209 cqv.Verify();
210
211 memset(ops, 0, sizeof(ops));
212 op = ops;
213 op->op = GRPC_OP_SEND_INITIAL_METADATA;
214 op->data.send_initial_metadata.count = 0;
215 op->flags = 0;
216 op++;
217 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
218 op->data.send_status_from_server.trailing_metadata_count = 0;
219 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
220 grpc_slice status_details = grpc_slice_from_static_string("xyz");
221 op->data.send_status_from_server.status_details = &status_details;
222 op->flags = 0;
223 op++;
224 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
225 op->data.recv_close_on_server.cancelled = &was_cancelled;
226 op->flags = 0;
227 op++;
228 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops),
229 grpc_core::CqVerifier::tag(102), nullptr);
230 GPR_ASSERT(GRPC_CALL_OK == error);
231
232 cqv.Expect(grpc_core::CqVerifier::tag(102), true);
233 cqv.Expect(grpc_core::CqVerifier::tag(1), true);
234 cqv.Verify();
235
236 peer = grpc_call_get_peer(c);
237 gpr_log(GPR_DEBUG, "got peer: '%s'", peer);
238 gpr_free(peer);
239
240 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
241 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
242 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
243 GPR_ASSERT(0 ==
244 grpc_slice_str_cmp(call_details.host, "foo.test.google.fr"));
245 GPR_ASSERT(was_cancelled == 0);
246
247 grpc_call_unref(s);
248 } else {
249 // Check for a failed connection.
250 cqv.Expect(grpc_core::CqVerifier::tag(1), true);
251 cqv.Verify();
252
253 gpr_log(GPR_INFO, "status: %d (expected: %d)", status,
254 GRPC_STATUS_UNAVAILABLE);
255 GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
256 }
257
258 grpc_call_unref(c);
259
260 // Destroy client.
261 grpc_channel_destroy(client);
262
263 // Destroy server.
264 grpc_server_shutdown_and_notify(server, cq, grpc_core::CqVerifier::tag(1000));
265 grpc_event ev;
266 do {
267 ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5),
268 nullptr);
269 } while (ev.type != GRPC_OP_COMPLETE ||
270 ev.tag != grpc_core::CqVerifier::tag(1000));
271
272 grpc_server_destroy(server);
273 grpc_completion_queue_shutdown(cq);
274 drain_cq(cq);
275 grpc_completion_queue_destroy(cq);
276
277 grpc_metadata_array_destroy(&initial_metadata_recv);
278 grpc_metadata_array_destroy(&trailing_metadata_recv);
279 grpc_metadata_array_destroy(&request_metadata_recv);
280
281 grpc_call_details_destroy(&call_details);
282 grpc_slice_unref(details);
283 if (picked_port) {
284 grpc_recycle_unused_port(port);
285 }
286 }
287
external_dns_works(const char * host)288 int external_dns_works(const char* host) {
289 auto addresses_or =
290 grpc_core::GetDNSResolver()->LookupHostnameBlocking(host, "80");
291 if (!addresses_or.ok()) {
292 return 0;
293 }
294 int result = 1;
295 for (const auto& addr : *addresses_or) {
296 // Kokoro on Macservice uses Google DNS64 servers by default
297 // (https://en.wikipedia.org/wiki/Google_Public_DNS) and that breaks
298 // "dualstack_socket_test" due to loopback4.unittest.grpc.io resolving to
299 // [64:ff9b::7f00:1]. (Working as expected for DNS64, but it prevents the
300 // dualstack_socket_test from functioning correctly). See b/201064791.
301 if (grpc_sockaddr_to_uri(&addr).value() ==
302 "ipv6:%5B64:ff9b::7f00:1%5D:80") {
303 gpr_log(
304 GPR_INFO,
305 "Detected DNS64 server response. Tests that depend on "
306 "*.unittest.grpc.io. will be skipped as they won't work with DNS64.");
307 result = 0;
308 break;
309 }
310 }
311 return result;
312 }
313
main(int argc,char ** argv)314 int main(int argc, char** argv) {
315 int do_ipv6 = 1;
316
317 grpc::testing::TestEnvironment env(&argc, argv);
318 grpc_init();
319
320 if (!grpc_ipv6_loopback_available()) {
321 gpr_log(GPR_INFO, "Can't bind to ::1. Skipping IPv6 tests.");
322 do_ipv6 = 0;
323 }
324
325 // For coverage, test with and without dualstack sockets.
326 for (grpc_forbid_dualstack_sockets_for_testing = 0;
327 grpc_forbid_dualstack_sockets_for_testing <= 1;
328 grpc_forbid_dualstack_sockets_for_testing++) {
329 // :: and 0.0.0.0 are handled identically.
330 test_connect("::", "127.0.0.1", 0, 1);
331 test_connect("::", "::ffff:127.0.0.1", 0, 1);
332 test_connect("::", "ipv4:127.0.0.1", 0, 1);
333 test_connect("::", "ipv6:[::ffff:127.0.0.1]", 0, 1);
334 test_connect("::", "localhost", 0, 1);
335 test_connect("0.0.0.0", "127.0.0.1", 0, 1);
336 test_connect("0.0.0.0", "::ffff:127.0.0.1", 0, 1);
337 test_connect("0.0.0.0", "ipv4:127.0.0.1", 0, 1);
338 test_connect("0.0.0.0", "ipv4:127.0.0.1,127.0.0.2,127.0.0.3", 0, 1);
339 test_connect("0.0.0.0", "ipv6:[::ffff:127.0.0.1],[::ffff:127.0.0.2]", 0, 1);
340 test_connect("0.0.0.0", "localhost", 0, 1);
341 if (do_ipv6) {
342 test_connect("::", "::1", 0, 1);
343 test_connect("0.0.0.0", "::1", 0, 1);
344 test_connect("::", "ipv6:[::1]", 0, 1);
345 test_connect("0.0.0.0", "ipv6:[::1]", 0, 1);
346 }
347
348 // These only work when the families agree.
349 test_connect("127.0.0.1", "127.0.0.1", 0, 1);
350 test_connect("127.0.0.1", "ipv4:127.0.0.1", 0, 1);
351 if (do_ipv6) {
352 test_connect("::1", "::1", 0, 1);
353 test_connect("::1", "127.0.0.1", 0, 0);
354 test_connect("127.0.0.1", "::1", 0, 0);
355 test_connect("::1", "ipv6:[::1]", 0, 1);
356 test_connect("::1", "ipv4:127.0.0.1", 0, 0);
357 test_connect("127.0.0.1", "ipv6:[::1]", 0, 0);
358 }
359
360 if (!external_dns_works("loopback4.unittest.grpc.io") ||
361 !external_dns_works("loopback46.unittest.grpc.io")) {
362 gpr_log(GPR_INFO, "Skipping tests that depend on *.unittest.grpc.io.");
363 } else {
364 test_connect("loopback46.unittest.grpc.io", "loopback4.unittest.grpc.io",
365 0, 1);
366 test_connect("loopback4.unittest.grpc.io", "loopback46.unittest.grpc.io",
367 0, 1);
368 if (do_ipv6) {
369 test_connect("loopback46.unittest.grpc.io",
370 "loopback6.unittest.grpc.io", 0, 1);
371 test_connect("loopback6.unittest.grpc.io",
372 "loopback46.unittest.grpc.io", 0, 1);
373 test_connect("loopback4.unittest.grpc.io", "loopback6.unittest.grpc.io",
374 0, 0);
375 test_connect("loopback6.unittest.grpc.io", "loopback4.unittest.grpc.io",
376 0, 0);
377 }
378 }
379 }
380
381 grpc_shutdown();
382
383 return 0;
384 }
385
386 #else // GRPC_POSIX_SOCKET_EV
387
main(int argc,char ** argv)388 int main(int argc, char** argv) { return 1; }
389
390 #endif // GRPC_POSIX_SOCKET_EV
391