1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
|
/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "src/core/lib/http/httpcli.h"
#include <string.h>
#include <sys/socket.h>
#include <algorithm>
#include <memory>
#include <string>
#include <thread>
#include <utility>
#include <ares.h>
#include <gtest/gtest.h>
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include <grpc/grpc.h>
#include <grpc/grpc_security.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/sync.h>
#include <grpc/support/time.h>
#include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h"
#include "src/core/lib/gprpp/status_helper.h"
#include "src/core/lib/gprpp/time.h"
#include "src/core/lib/gprpp/time_util.h"
#include "src/core/lib/iomgr/pollset.h"
#include "src/core/lib/iomgr/pollset_set.h"
#include "src/core/lib/security/credentials/credentials.h"
#include "test/core/http/httpcli_test_util.h"
#include "test/core/util/fake_udp_and_tcp_server.h"
#include "test/core/util/port.h"
#include "test/core/util/subprocess.h"
#include "test/core/util/test_config.h"
namespace {
grpc_core::Timestamp NSecondsTime(int seconds) {
return grpc_core::Timestamp::FromTimespecRoundUp(
grpc_timeout_seconds_to_deadline(seconds));
}
absl::Time AbslDeadlineSeconds(int s) {
return grpc_core::ToAbslTime(grpc_timeout_seconds_to_deadline(s));
}
int g_argc;
char** g_argv;
int g_server_port;
gpr_subprocess* g_server;
class HttpRequestTest : public ::testing::Test {
public:
HttpRequestTest() {
grpc_init();
grpc_core::ExecCtx exec_ctx;
grpc_pollset* pollset =
static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
grpc_pollset_init(pollset, &mu_);
pops_ = grpc_polling_entity_create_from_pollset(pollset);
}
~HttpRequestTest() override {
{
grpc_core::ExecCtx exec_ctx;
grpc_pollset_shutdown(
grpc_polling_entity_pollset(&pops_),
GRPC_CLOSURE_CREATE(DestroyPops, &pops_, grpc_schedule_on_exec_ctx));
}
grpc_shutdown();
}
void RunAndKick(const std::function<void()>& f) {
grpc_core::MutexLockForGprMu lock(mu_);
f();
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"pollset_kick",
grpc_pollset_kick(grpc_polling_entity_pollset(&pops_), nullptr)));
}
void PollUntil(const std::function<bool()>& predicate, absl::Time deadline) {
gpr_mu_lock(mu_);
while (!predicate()) {
GPR_ASSERT(absl::Now() < deadline);
grpc_pollset_worker* worker = nullptr;
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"pollset_work", grpc_pollset_work(grpc_polling_entity_pollset(&pops_),
&worker, NSecondsTime(1))));
gpr_mu_unlock(mu_);
gpr_mu_lock(mu_);
}
gpr_mu_unlock(mu_);
}
grpc_polling_entity* pops() { return &pops_; }
protected:
static void SetUpTestSuite() {
auto test_server = grpc_core::testing::StartHttpRequestTestServer(
g_argc, g_argv, false /* use_ssl */);
g_server = test_server.server;
g_server_port = test_server.port;
}
static void TearDownTestSuite() { gpr_subprocess_destroy(g_server); }
private:
static void DestroyPops(void* p, grpc_error_handle /*error*/) {
grpc_polling_entity* pops = static_cast<grpc_polling_entity*>(p);
grpc_pollset_destroy(grpc_polling_entity_pollset(pops));
gpr_free(grpc_polling_entity_pollset(pops));
}
gpr_mu* mu_;
grpc_polling_entity pops_;
};
struct RequestState {
explicit RequestState(HttpRequestTest* test) : test(test) {}
~RequestState() {
grpc_core::ExecCtx exec_ctx;
grpc_http_response_destroy(&response);
}
HttpRequestTest* test;
bool done = false;
grpc_http_response response = {};
grpc_pollset_set* pollset_set_to_destroy_eagerly = nullptr;
};
void OnFinish(void* arg, grpc_error_handle error) {
RequestState* request_state = static_cast<RequestState*>(arg);
if (request_state->pollset_set_to_destroy_eagerly != nullptr) {
// Destroy the request's polling entity param. The goal is to try to catch a
// bug where we might still be referencing the polling entity by
// a pending TCP connect.
grpc_pollset_set_destroy(request_state->pollset_set_to_destroy_eagerly);
}
const char* expect =
"<html><head><title>Hello world!</title></head>"
"<body><p>This is a test</p></body></html>";
GPR_ASSERT(error.ok());
grpc_http_response response = request_state->response;
gpr_log(GPR_INFO, "response status=%d error=%s", response.status,
grpc_core::StatusToString(error).c_str());
GPR_ASSERT(response.status == 200);
GPR_ASSERT(response.body_length == strlen(expect));
GPR_ASSERT(0 == memcmp(expect, response.body, response.body_length));
request_state->test->RunAndKick(
[request_state]() { request_state->done = true; });
}
void OnFinishExpectFailure(void* arg, grpc_error_handle error) {
RequestState* request_state = static_cast<RequestState*>(arg);
if (request_state->pollset_set_to_destroy_eagerly != nullptr) {
// Destroy the request's polling entity param. The goal is to try to catch a
// bug where we might still be referencing the polling entity by
// a pending TCP connect.
grpc_pollset_set_destroy(request_state->pollset_set_to_destroy_eagerly);
}
grpc_http_response response = request_state->response;
gpr_log(GPR_INFO, "response status=%d error=%s", response.status,
grpc_core::StatusToString(error).c_str());
GPR_ASSERT(!error.ok());
request_state->test->RunAndKick(
[request_state]() { request_state->done = true; });
}
TEST_F(HttpRequestTest, Get) {
RequestState request_state(this);
grpc_http_request req;
grpc_core::ExecCtx exec_ctx;
std::string host = absl::StrFormat("localhost:%d", g_server_port);
gpr_log(GPR_INFO, "requesting from %s", host.c_str());
memset(&req, 0, sizeof(req));
auto uri = grpc_core::URI::Create("http", host, "/get", {} /* query params */,
"" /* fragment */);
GPR_ASSERT(uri.ok());
grpc_core::OrphanablePtr<grpc_core::HttpRequest> http_request =
grpc_core::HttpRequest::Get(
std::move(*uri), nullptr /* channel args */, pops(), &req,
NSecondsTime(15),
GRPC_CLOSURE_CREATE(OnFinish, &request_state,
grpc_schedule_on_exec_ctx),
&request_state.response,
grpc_core::RefCountedPtr<grpc_channel_credentials>(
grpc_insecure_credentials_create()));
http_request->Start();
PollUntil([&request_state]() { return request_state.done; },
AbslDeadlineSeconds(60));
}
TEST_F(HttpRequestTest, Post) {
RequestState request_state(this);
grpc_http_request req;
grpc_core::ExecCtx exec_ctx;
std::string host = absl::StrFormat("localhost:%d", g_server_port);
gpr_log(GPR_INFO, "posting to %s", host.c_str());
memset(&req, 0, sizeof(req));
req.body = const_cast<char*>("hello");
req.body_length = 5;
auto uri = grpc_core::URI::Create("http", host, "/post",
{} /* query params */, "" /* fragment */);
GPR_ASSERT(uri.ok());
grpc_core::OrphanablePtr<grpc_core::HttpRequest> http_request =
grpc_core::HttpRequest::Post(
std::move(*uri), nullptr /* channel args */, pops(), &req,
NSecondsTime(15),
GRPC_CLOSURE_CREATE(OnFinish, &request_state,
grpc_schedule_on_exec_ctx),
&request_state.response,
grpc_core::RefCountedPtr<grpc_channel_credentials>(
grpc_insecure_credentials_create()));
http_request->Start();
PollUntil([&request_state]() { return request_state.done; },
AbslDeadlineSeconds(60));
}
int g_fake_non_responsive_dns_server_port;
void InjectNonResponsiveDNSServer(ares_channel channel) {
gpr_log(GPR_DEBUG,
"Injecting broken nameserver list. Bad server address:|[::1]:%d|.",
g_fake_non_responsive_dns_server_port);
// Configure a non-responsive DNS server at the front of c-ares's nameserver
// list.
struct ares_addr_port_node dns_server_addrs[1];
dns_server_addrs[0].family = AF_INET6;
(reinterpret_cast<char*>(&dns_server_addrs[0].addr.addr6))[15] = 0x1;
dns_server_addrs[0].tcp_port = g_fake_non_responsive_dns_server_port;
dns_server_addrs[0].udp_port = g_fake_non_responsive_dns_server_port;
dns_server_addrs[0].next = nullptr;
GPR_ASSERT(ares_set_servers_ports(channel, dns_server_addrs) == ARES_SUCCESS);
}
TEST_F(HttpRequestTest, CancelGetDuringDNSResolution) {
// Inject an unresponsive DNS server into the resolver's DNS server config
grpc_core::testing::FakeUdpAndTcpServer fake_dns_server(
grpc_core::testing::FakeUdpAndTcpServer::AcceptMode::
kWaitForClientToSendFirstBytes,
grpc_core::testing::FakeUdpAndTcpServer::CloseSocketUponCloseFromPeer);
g_fake_non_responsive_dns_server_port = fake_dns_server.port();
void (*prev_test_only_inject_config)(ares_channel channel) =
grpc_ares_test_only_inject_config;
grpc_ares_test_only_inject_config = InjectNonResponsiveDNSServer;
// Run the same test on several threads in parallel to try to trigger races
// etc.
int kNumThreads = 10;
std::vector<std::thread> threads;
threads.reserve(kNumThreads);
for (int i = 0; i < kNumThreads; i++) {
threads.push_back(std::thread([this]() {
RequestState request_state(this);
grpc_http_request req;
grpc_core::ExecCtx exec_ctx;
memset(&req, 0, sizeof(grpc_http_request));
auto uri = grpc_core::URI::Create(
"http", "dont-care-since-wont-be-resolved.test.com:443", "/get",
{} /* query params */, "" /* fragment */);
GPR_ASSERT(uri.ok());
grpc_core::OrphanablePtr<grpc_core::HttpRequest> http_request =
grpc_core::HttpRequest::Get(
std::move(*uri), nullptr /* channel args */, pops(), &req,
NSecondsTime(120),
GRPC_CLOSURE_CREATE(OnFinishExpectFailure, &request_state,
grpc_schedule_on_exec_ctx),
&request_state.response,
grpc_core::RefCountedPtr<grpc_channel_credentials>(
grpc_insecure_credentials_create()));
http_request->Start();
std::thread cancel_thread([&http_request]() {
gpr_sleep_until(grpc_timeout_seconds_to_deadline(1));
grpc_core::ExecCtx exec_ctx;
http_request.reset();
});
// Poll with a deadline explicitly lower than the request timeout, so
// that we know that the request timeout isn't just kicking in.
PollUntil([&request_state]() { return request_state.done; },
AbslDeadlineSeconds(60));
cancel_thread.join();
}));
}
for (auto& t : threads) {
t.join();
}
grpc_ares_test_only_inject_config = prev_test_only_inject_config;
}
TEST_F(HttpRequestTest, CancelGetWhileReadingResponse) {
// Start up a fake HTTP server which just accepts connections
// and then hangs, i.e. does not send back any bytes to the client.
// The goal here is to get the client to connect to this fake server
// and send a request, and then sit waiting for a response. Then, a
// separate thread will cancel the HTTP request, and that should let it
// complete.
grpc_core::testing::FakeUdpAndTcpServer fake_http_server(
grpc_core::testing::FakeUdpAndTcpServer::AcceptMode::
kWaitForClientToSendFirstBytes,
grpc_core::testing::FakeUdpAndTcpServer::CloseSocketUponCloseFromPeer);
// Run the same test on several threads in parallel to try to trigger races
// etc.
int kNumThreads = 10;
std::vector<std::thread> threads;
threads.reserve(kNumThreads);
for (int i = 0; i < kNumThreads; i++) {
grpc_core::testing::FakeUdpAndTcpServer* fake_http_server_ptr =
&fake_http_server;
threads.push_back(std::thread([this, fake_http_server_ptr]() {
RequestState request_state(this);
grpc_http_request req;
grpc_core::ExecCtx exec_ctx;
memset(&req, 0, sizeof(req));
auto uri = grpc_core::URI::Create("http", fake_http_server_ptr->address(),
"/get", {} /* query params */,
"" /* fragment */);
GPR_ASSERT(uri.ok());
grpc_core::OrphanablePtr<grpc_core::HttpRequest> http_request =
grpc_core::HttpRequest::Get(
std::move(*uri), nullptr /* channel args */, pops(), &req,
NSecondsTime(120),
GRPC_CLOSURE_CREATE(OnFinishExpectFailure, &request_state,
grpc_schedule_on_exec_ctx),
&request_state.response,
grpc_core::RefCountedPtr<grpc_channel_credentials>(
grpc_insecure_credentials_create()));
http_request->Start();
exec_ctx.Flush();
std::thread cancel_thread([&http_request]() {
gpr_sleep_until(grpc_timeout_seconds_to_deadline(1));
grpc_core::ExecCtx exec_ctx;
http_request.reset();
});
// Poll with a deadline explicitly lower than the request timeout, so
// that we know that the request timeout isn't just kicking in.
PollUntil([&request_state]() { return request_state.done; },
AbslDeadlineSeconds(60));
cancel_thread.join();
}));
}
for (auto& t : threads) {
t.join();
}
}
// The main point of this test is just to exercise the machinery around
// cancellation during TCP connection establishment, to make sure there are no
// crashes/races etc. This test doesn't actually verify that cancellation during
// TCP setup is happening, though. For that, we would need to induce packet loss
// in the test.
TEST_F(HttpRequestTest, CancelGetRacesWithConnectionFailure) {
// Grab an unoccupied port but don't listen on it. The goal
// here is just to have a server address that will reject
// TCP connection setups.
// Note that because the server is rejecting TCP connections, we
// don't really need to cancel the HTTP requests in this test case
// in order for them proceeed i.e. in order for them to pass. The test
// is still beneficial though because it can exercise the same code paths
// that would get taken if the HTTP request was cancelled while the TCP
// connect attempt was actually hanging.
int fake_server_port = grpc_pick_unused_port_or_die();
std::string fake_server_address =
absl::StrCat("[::1]:", std::to_string(fake_server_port));
// Run the same test on several threads in parallel to try to trigger races
// etc.
int kNumThreads = 10;
std::vector<std::thread> threads;
threads.reserve(kNumThreads);
for (int i = 0; i < kNumThreads; i++) {
threads.push_back(std::thread([this, fake_server_address]() {
RequestState request_state(this);
grpc_http_request req;
grpc_core::ExecCtx exec_ctx;
memset(&req, 0, sizeof(req));
auto uri =
grpc_core::URI::Create("http", fake_server_address, "/get",
{} /* query params */, "" /* fragment */);
GPR_ASSERT(uri.ok());
grpc_core::OrphanablePtr<grpc_core::HttpRequest> http_request =
grpc_core::HttpRequest::Get(
std::move(*uri), nullptr /* channel args */, pops(), &req,
NSecondsTime(120),
GRPC_CLOSURE_CREATE(OnFinishExpectFailure, &request_state,
grpc_schedule_on_exec_ctx),
&request_state.response,
grpc_core::RefCountedPtr<grpc_channel_credentials>(
grpc_insecure_credentials_create()));
// Start the HTTP request. We will ~immediately begin a TCP connect
// attempt because there's no name to resolve.
http_request->Start();
exec_ctx.Flush();
// Spawn a separate thread which ~immediately cancels the HTTP request.
// Note that even though the server is rejecting TCP connections, it can
// still take some time for the client to receive that rejection. So
// cancelling the request now can trigger the code paths that would get
// taken if the TCP connection was truly hanging e.g. from packet loss.
// The goal is just to make sure there are no crashes, races, etc.
std::thread cancel_thread([&http_request]() {
grpc_core::ExecCtx exec_ctx;
http_request.reset();
});
// Poll with a deadline explicitly lower than the request timeout, so
// that we know that the request timeout isn't just kicking in.
PollUntil([&request_state]() { return request_state.done; },
AbslDeadlineSeconds(60));
cancel_thread.join();
}));
}
for (auto& t : threads) {
t.join();
}
}
// The pollent parameter passed to HttpRequest::Get or Post is owned by
// the caller and must not be referenced by the HttpRequest after the
// requests's on_done callback is invoked. This test verifies that this
// isn't happening by destroying the request's pollset set within the
// on_done callback.
TEST_F(HttpRequestTest, CallerPollentsAreNotReferencedAfterCallbackIsRan) {
// Grab an unoccupied port but don't listen on it. The goal
// here is just to have a server address that will reject
// TCP connection setups.
// Note that we could have used a different server for this test case, e.g.
// one which accepts TCP connections. All we need here is something for the
// client to connect to, since it will be cancelled roughly during the
// connection attempt anyways.
int fake_server_port = grpc_pick_unused_port_or_die();
std::string fake_server_address =
absl::StrCat("[::1]:", std::to_string(fake_server_port));
RequestState request_state(this);
grpc_http_request req;
grpc_core::ExecCtx exec_ctx;
memset(&req, 0, sizeof(req));
req.path = const_cast<char*>("/get");
request_state.pollset_set_to_destroy_eagerly = grpc_pollset_set_create();
grpc_polling_entity_add_to_pollset_set(
pops(), request_state.pollset_set_to_destroy_eagerly);
grpc_polling_entity wrapped_pollset_set_to_destroy_eagerly =
grpc_polling_entity_create_from_pollset_set(
request_state.pollset_set_to_destroy_eagerly);
auto uri = grpc_core::URI::Create("http", fake_server_address, "/get",
{} /* query params */, "" /* fragment */);
GPR_ASSERT(uri.ok());
grpc_core::OrphanablePtr<grpc_core::HttpRequest> http_request =
grpc_core::HttpRequest::Get(
std::move(*uri), nullptr /* channel args */,
&wrapped_pollset_set_to_destroy_eagerly, &req, NSecondsTime(15),
GRPC_CLOSURE_CREATE(OnFinishExpectFailure, &request_state,
grpc_schedule_on_exec_ctx),
&request_state.response,
grpc_core::RefCountedPtr<grpc_channel_credentials>(
grpc_insecure_credentials_create()));
// Start the HTTP request. We'll start the TCP connect attempt right away.
http_request->Start();
exec_ctx.Flush();
http_request.reset(); // cancel the request
// Since the request was cancelled, the on_done callback should be flushed
// out on the ExecCtx flush below. When the on_done callback is ran, it will
// eagerly destroy 'request_state.pollset_set_to_destroy_eagerly'. Thus, we
// can't poll on that pollset here.
exec_ctx.Flush();
}
void CancelRequest(grpc_core::HttpRequest* req) {
gpr_log(
GPR_INFO,
"test only HttpRequest::OnHandshakeDone intercept orphaning request: %p",
req);
req->Orphan();
}
// This exercises the code paths that happen when we cancel an HTTP request
// before the security handshake callback runs, but after that callback has
// already been scheduled with a success result. This case is interesting
// because the current security handshake API transfers ownership of output
// arguments to the caller only if the handshake is successful, rendering
// this code path as something that only occurs with just the right timing.
TEST_F(HttpRequestTest,
CancelDuringSecurityHandshakeButHandshakeStillSucceeds) {
RequestState request_state(this);
grpc_http_request req;
grpc_core::ExecCtx exec_ctx;
std::string host = absl::StrFormat("localhost:%d", g_server_port);
gpr_log(GPR_INFO, "requesting from %s", host.c_str());
memset(&req, 0, sizeof(req));
auto uri = grpc_core::URI::Create("http", host, "/get", {} /* query params */,
"" /* fragment */);
GPR_ASSERT(uri.ok());
grpc_core::OrphanablePtr<grpc_core::HttpRequest> http_request =
grpc_core::HttpRequest::Get(
std::move(*uri), nullptr /* channel args */, pops(), &req,
NSecondsTime(15),
GRPC_CLOSURE_CREATE(OnFinishExpectFailure, &request_state,
grpc_schedule_on_exec_ctx),
&request_state.response,
grpc_core::RefCountedPtr<grpc_channel_credentials>(
grpc_insecure_credentials_create()));
grpc_core::HttpRequest::TestOnlySetOnHandshakeDoneIntercept(CancelRequest);
http_request->Start();
(void)http_request.release(); // request will be orphaned by CancelRequest
exec_ctx.Flush();
PollUntil([&request_state]() { return request_state.done; },
AbslDeadlineSeconds(60));
grpc_core::HttpRequest::TestOnlySetOnHandshakeDoneIntercept(nullptr);
}
} // namespace
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
grpc::testing::TestEnvironment env(&argc, argv);
// launch the test server later, so that --gtest_list_tests works
g_argc = argc;
g_argv = argv;
// run tests
return RUN_ALL_TESTS();
}
|