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