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