| 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 <string.h> |
| 20 |
|
| 21 |
#include <algorithm> |
| 22 |
#include <functional> |
| 23 |
#include <memory> |
| 24 |
#include <string> |
| 25 |
#include <thread> |
| 26 |
#include <utility> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
#include <gtest/gtest.h> |
| 30 |
|
| 31 |
#include "absl/status/statusor.h" |
| 32 |
#include "absl/strings/str_format.h" |
| 33 |
#include "absl/time/clock.h" |
| 34 |
#include "absl/time/time.h" |
| 35 |
|
| 36 |
#include <grpc/grpc.h> |
| 37 |
#include <grpc/support/alloc.h> |
| 38 |
#include <grpc/support/log.h> |
| 39 |
#include <grpc/support/sync.h> |
| 40 |
#include <grpc/support/time.h> |
| 41 |
|
| 42 |
#include "src/core/lib/channel/channel_args.h" |
| 43 |
#include "src/core/lib/gprpp/orphanable.h" |
| 44 |
#include "src/core/lib/gprpp/status_helper.h" |
| 45 |
#include "src/core/lib/gprpp/sync.h" |
| 46 |
#include "src/core/lib/gprpp/time.h" |
| 47 |
#include "src/core/lib/gprpp/time_util.h" |
| 48 |
#include "src/core/lib/http/httpcli.h" |
| 49 |
#include "src/core/lib/http/httpcli_ssl_credentials.h" |
| 50 |
#include "src/core/lib/http/parser.h" |
| 51 |
#include "src/core/lib/iomgr/closure.h" |
| 52 |
#include "src/core/lib/iomgr/error.h" |
| 53 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 54 |
#include "src/core/lib/iomgr/iomgr_fwd.h" |
| 55 |
#include "src/core/lib/iomgr/polling_entity.h" |
| 56 |
#include "src/core/lib/iomgr/pollset.h" |
| 57 |
#include "src/core/lib/security/credentials/credentials.h" |
| 58 |
#include "src/core/lib/uri/uri_parser.h" |
| 59 |
#include "test/core/http/httpcli_test_util.h" |
| 60 |
#include "test/core/util/fake_udp_and_tcp_server.h" |
| 61 |
#include "test/core/util/subprocess.h" |
| 62 |
#include "test/core/util/test_config.h" |
| 63 |
|
| 64 |
namespace { |
| 65 |
|
| 66 |
grpc_core::Timestamp NSecondsTime(int seconds) { |
| 67 |
return grpc_core::Timestamp::FromTimespecRoundUp( |
| 68 |
grpc_timeout_seconds_to_deadline(seconds)); |
| 69 |
} |
| 70 |
|
| 71 |
absl::Time AbslDeadlineSeconds(int s) { |
| 72 |
return grpc_core::ToAbslTime(grpc_timeout_seconds_to_deadline(s)); |
| 73 |
} |
| 74 |
|
| 75 |
int g_argc; |
| 76 |
char** g_argv; |
| 77 |
int g_server_port; |
| 78 |
gpr_subprocess* g_server; |
| 79 |
|
| 80 |
class HttpsCliTest : public ::testing::Test { |
| 81 |
public: |
| 82 |
HttpsCliTest() { |
| 83 |
grpc_init(); |
| 84 |
grpc_core::ExecCtx exec_ctx; |
| 85 |
grpc_pollset* pollset = |
| 86 |
static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size())); |
| 87 |
grpc_pollset_init(pollset, &mu_); |
| 88 |
pops_ = grpc_polling_entity_create_from_pollset(pollset); |
| 89 |
} |
| 90 |
~HttpsCliTest() override { |
| 91 |
{ |
| 92 |
grpc_core::ExecCtx exec_ctx; |
| 93 |
grpc_pollset_shutdown( |
| 94 |
grpc_polling_entity_pollset(&pops_), |
| 95 |
GRPC_CLOSURE_CREATE(DestroyPops, &pops_, grpc_schedule_on_exec_ctx)); |
| 96 |
} |
| 97 |
grpc_shutdown(); |
| 98 |
} |
| 99 |
|
| 100 |
void RunAndKick(const std::function<void()>& f) { |
| 101 |
grpc_core::MutexLockForGprMu lock(mu_); |
| 102 |
f(); |
| 103 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 104 |
"pollset_kick", |
| 105 |
grpc_pollset_kick(grpc_polling_entity_pollset(&pops_), nullptr))); |
| 106 |
} |
| 107 |
|
| 108 |
void PollUntil(const std::function<bool()>& predicate, absl::Time deadline) { |
| 109 |
gpr_mu_lock(mu_); |
| 110 |
while (!predicate()) { |
| 111 |
GPR_ASSERT(absl::Now() < deadline); |
| 112 |
grpc_pollset_worker* worker = nullptr; |
| 113 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 114 |
"pollset_work", grpc_pollset_work(grpc_polling_entity_pollset(&pops_), |
| 115 |
&worker, NSecondsTime(1)))); |
| 116 |
gpr_mu_unlock(mu_); |
| 117 |
gpr_mu_lock(mu_); |
| 118 |
} |
| 119 |
gpr_mu_unlock(mu_); |
| 120 |
} |
| 121 |
|
| 122 |
grpc_polling_entity* pops() { return &pops_; } |
| 123 |
|
| 124 |
protected: |
| 125 |
static void SetUpTestSuite() { |
| 126 |
auto test_server = grpc_core::testing::StartHttpRequestTestServer( |
| 127 |
g_argc, g_argv, true /* use_ssl */); |
| 128 |
g_server = test_server.server; |
| 129 |
g_server_port = test_server.port; |
| 130 |
} |
| 131 |
|
| 132 |
static void TearDownTestSuite() { gpr_subprocess_destroy(g_server); } |
| 133 |
|
| 134 |
private: |
| 135 |
static void DestroyPops(void* p, grpc_error_handle /*error*/) { |
| 136 |
grpc_polling_entity* pops = static_cast<grpc_polling_entity*>(p); |
| 137 |
grpc_pollset_destroy(grpc_polling_entity_pollset(pops)); |
| 138 |
gpr_free(grpc_polling_entity_pollset(pops)); |
| 139 |
} |
| 140 |
|
| 141 |
gpr_mu* mu_; |
| 142 |
grpc_polling_entity pops_; |
| 143 |
}; |
| 144 |
|
| 145 |
struct RequestState { |
| 146 |
explicit RequestState(HttpsCliTest* test) : test(test) {} |
| 147 |
|
| 148 |
~RequestState() { |
| 149 |
grpc_core::ExecCtx exec_ctx; |
| 150 |
grpc_http_response_destroy(&response); |
| 151 |
} |
| 152 |
|
| 153 |
HttpsCliTest* test; |
| 154 |
bool done = false; |
| 155 |
grpc_http_response response = {}; |
| 156 |
}; |
| 157 |
|
| 158 |
void OnFinish(void* arg, grpc_error_handle error) { |
| 159 |
RequestState* request_state = static_cast<RequestState*>(arg); |
| 160 |
const char* expect = |
| 161 |
"<html><head><title>Hello world!</title></head>" |
| 162 |
"<body><p>This is a test</p></body></html>"; |
| 163 |
GPR_ASSERT(error.ok()); |
| 164 |
grpc_http_response response = request_state->response; |
| 165 |
gpr_log(GPR_INFO, "response status=%d error=%s", response.status, |
| 166 |
grpc_core::StatusToString(error).c_str()); |
| 167 |
GPR_ASSERT(response.status == 200); |
| 168 |
GPR_ASSERT(response.body_length == strlen(expect)); |
| 169 |
GPR_ASSERT(0 == memcmp(expect, response.body, response.body_length)); |
| 170 |
request_state->test->RunAndKick( |
| 171 |
[request_state]() { request_state->done = true; }); |
| 172 |
} |
| 173 |
|
| 174 |
void OnFinishExpectFailure(void* arg, grpc_error_handle error) { |
| 175 |
RequestState* request_state = static_cast<RequestState*>(arg); |
| 176 |
grpc_http_response response = request_state->response; |
| 177 |
gpr_log(GPR_INFO, "response status=%d error=%s", response.status, |
| 178 |
grpc_core::StatusToString(error).c_str()); |
| 179 |
GPR_ASSERT(!error.ok()); |
| 180 |
request_state->test->RunAndKick( |
| 181 |
[request_state]() { request_state->done = true; }); |
| 182 |
} |
| 183 |
|
| 184 |
TEST_F(HttpsCliTest, Get) { |
| 185 |
RequestState request_state(this); |
| 186 |
grpc_http_request req; |
| 187 |
grpc_core::ExecCtx exec_ctx; |
| 188 |
std::string host = absl::StrFormat("localhost:%d", g_server_port); |
| 189 |
gpr_log(GPR_INFO, "requesting from %s", host.c_str()); |
| 190 |
memset(&req, 0, sizeof(req)); |
| 191 |
grpc_arg ssl_override_arg = grpc_channel_arg_string_create( |
| 192 |
const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG), |
| 193 |
const_cast<char*>("foo.test.google.fr")); |
| 194 |
grpc_channel_args args = {1, &ssl_override_arg}; |
| 195 |
auto uri = grpc_core::URI::Create("https", host, "/get", |
| 196 |
{} /* query params */, "" /* fragment */); |
| 197 |
GPR_ASSERT(uri.ok()); |
| 198 |
grpc_core::OrphanablePtr<grpc_core::HttpRequest> http_request = |
| 199 |
grpc_core::HttpRequest::Get( |
| 200 |
std::move(*uri), &args, pops(), &req, NSecondsTime(15), |
| 201 |
GRPC_CLOSURE_CREATE(OnFinish, &request_state, |
| 202 |
grpc_schedule_on_exec_ctx), |
| 203 |
&request_state.response, |
| 204 |
grpc_core::CreateHttpRequestSSLCredentials()); |
| 205 |
http_request->Start(); |
| 206 |
PollUntil([&request_state]() { return request_state.done; }, |
| 207 |
AbslDeadlineSeconds(60)); |
| 208 |
} |
| 209 |
|
| 210 |
TEST_F(HttpsCliTest, Post) { |
| 211 |
RequestState request_state(this); |
| 212 |
grpc_http_request req; |
| 213 |
grpc_core::ExecCtx exec_ctx; |
| 214 |
std::string host = absl::StrFormat("localhost:%d", g_server_port); |
| 215 |
gpr_log(GPR_INFO, "posting to %s", host.c_str()); |
| 216 |
memset(&req, 0, sizeof(req)); |
| 217 |
req.body = const_cast<char*>("hello"); |
| 218 |
req.body_length = 5; |
| 219 |
grpc_arg ssl_override_arg = grpc_channel_arg_string_create( |
| 220 |
const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG), |
| 221 |
const_cast<char*>("foo.test.google.fr")); |
| 222 |
grpc_channel_args args = {1, &ssl_override_arg}; |
| 223 |
auto uri = grpc_core::URI::Create("https", host, "/post", |
| 224 |
{} /* query params */, "" /* fragment */); |
| 225 |
GPR_ASSERT(uri.ok()); |
| 226 |
grpc_core::OrphanablePtr<grpc_core::HttpRequest> http_request = |
| 227 |
grpc_core::HttpRequest::Post( |
| 228 |
std::move(*uri), &args /* channel args */, pops(), &req, |
| 229 |
NSecondsTime(15), |
| 230 |
GRPC_CLOSURE_CREATE(OnFinish, &request_state, |
| 231 |
grpc_schedule_on_exec_ctx), |
| 232 |
&request_state.response, |
| 233 |
grpc_core::CreateHttpRequestSSLCredentials()); |
| 234 |
http_request->Start(); |
| 235 |
PollUntil([&request_state]() { return request_state.done; }, |
| 236 |
AbslDeadlineSeconds(60)); |
| 237 |
} |
| 238 |
|
| 239 |
// The goal of this test is to make sure that we can cancel HTTP requests |
| 240 |
// while they're waiting for a response from the server to finish their |
| 241 |
// SSL handshakes. Note that the main focus of this test is to just exercise |
| 242 |
// the relevant code paths and make sure there aren't any crashes etc., rather |
| 243 |
// than to make sure that cancellation happens in a timely manner. |
| 244 |
TEST_F(HttpsCliTest, CancelGetDuringSSLHandshake) { |
| 245 |
// Start up a fake TCP server which accepts connections and then hangs, |
| 246 |
// i.e. it won't send any bytes back to the client. |
| 247 |
grpc_core::testing::FakeUdpAndTcpServer fake_http_server( |
| 248 |
grpc_core::testing::FakeUdpAndTcpServer::AcceptMode:: |
| 249 |
kWaitForClientToSendFirstBytes, |
| 250 |
grpc_core::testing::FakeUdpAndTcpServer::CloseSocketUponCloseFromPeer); |
| 251 |
// Use multiple threads to try to trigger races etc. |
| 252 |
int kNumThreads = 10; |
| 253 |
std::vector<std::thread> threads; |
| 254 |
threads.reserve(kNumThreads); |
| 255 |
for (int i = 0; i < kNumThreads; i++) { |
| 256 |
grpc_core::testing::FakeUdpAndTcpServer* fake_http_server_ptr = |
| 257 |
&fake_http_server; |
| 258 |
threads.push_back(std::thread([this, fake_http_server_ptr]() { |
| 259 |
RequestState request_state(this); |
| 260 |
grpc_http_request req; |
| 261 |
grpc_core::ExecCtx exec_ctx; |
| 262 |
memset(&req, 0, sizeof(req)); |
| 263 |
grpc_arg ssl_override_arg = grpc_channel_arg_string_create( |
| 264 |
const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG), |
| 265 |
const_cast<char*>("foo.test.google.fr")); |
| 266 |
grpc_channel_args args = {1, &ssl_override_arg}; |
| 267 |
auto uri = grpc_core::URI::Create( |
| 268 |
"https", fake_http_server_ptr->address(), "/get", |
| 269 |
{} /* query params */, "" /* fragment */); |
| 270 |
grpc_core::OrphanablePtr<grpc_core::HttpRequest> http_request = |
| 271 |
grpc_core::HttpRequest::Get( |
| 272 |
std::move(*uri), &args, pops(), &req, NSecondsTime(120), |
| 273 |
GRPC_CLOSURE_CREATE(OnFinishExpectFailure, &request_state, |
| 274 |
grpc_schedule_on_exec_ctx), |
| 275 |
&request_state.response, |
| 276 |
grpc_core::CreateHttpRequestSSLCredentials()); |
| 277 |
// Start a request. It will establish a TCP connection to the |
| 278 |
// server and then begin an SSL handshake. The server won't send |
| 279 |
// anything back though, so it will be stuck in its SSL handshake, |
| 280 |
// waiting for the firt response from the server. |
| 281 |
http_request->Start(); |
| 282 |
exec_ctx.Flush(); |
| 283 |
std::thread cancel_thread([&http_request]() { |
| 284 |
// Give one second to let the client get into the middle of its |
| 285 |
// SSL handshake, and then cancel the request. |
| 286 |
gpr_sleep_until(grpc_timeout_seconds_to_deadline(1)); |
| 287 |
grpc_core::ExecCtx exec_ctx; |
| 288 |
http_request.reset(); |
| 289 |
}); |
| 290 |
// Poll with a deadline explicitly lower than the request timeout, so |
| 291 |
// that we know that the request timeout isn't just kicking in. |
| 292 |
PollUntil([&request_state]() { return request_state.done; }, |
| 293 |
AbslDeadlineSeconds(60)); |
| 294 |
cancel_thread.join(); |
| 295 |
})); |
| 296 |
} |
| 297 |
for (auto& t : threads) { |
| 298 |
t.join(); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
} // namespace |
| 303 |
|
| 304 |
int main(int argc, char** argv) { |
| 305 |
::testing::InitGoogleTest(&argc, argv); |
| 306 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 307 |
// launch the test server later, so that --gtest_list_tests works |
| 308 |
g_argc = argc; |
| 309 |
g_argv = argv; |
| 310 |
// run tests |
| 311 |
return RUN_ALL_TESTS(); |
| 312 |
} |