OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #include <mutex> |
| 35 #include <thread> |
| 36 |
| 37 #include <grpc++/channel.h> |
| 38 #include <grpc++/client_context.h> |
| 39 #include <grpc++/create_channel.h> |
| 40 #include <grpc++/security/auth_metadata_processor.h> |
| 41 #include <grpc++/security/credentials.h> |
| 42 #include <grpc++/security/server_credentials.h> |
| 43 #include <grpc++/server.h> |
| 44 #include <grpc++/server_builder.h> |
| 45 #include <grpc++/server_context.h> |
| 46 #include <grpc/grpc.h> |
| 47 #include <grpc/support/thd.h> |
| 48 #include <grpc/support/time.h> |
| 49 #include <gtest/gtest.h> |
| 50 |
| 51 #include "src/core/security/credentials.h" |
| 52 #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h" |
| 53 #include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 54 #include "test/core/util/port.h" |
| 55 #include "test/core/util/test_config.h" |
| 56 #include "test/cpp/end2end/test_service_impl.h" |
| 57 #include "test/cpp/util/string_ref_helper.h" |
| 58 #include "test/cpp/util/test_credentials_provider.h" |
| 59 |
| 60 using grpc::testing::EchoRequest; |
| 61 using grpc::testing::EchoResponse; |
| 62 using std::chrono::system_clock; |
| 63 |
| 64 namespace grpc { |
| 65 namespace testing { |
| 66 namespace { |
| 67 |
| 68 bool CheckIsLocalhost(const grpc::string& addr) { |
| 69 const grpc::string kIpv6("ipv6:[::1]:"); |
| 70 const grpc::string kIpv4MappedIpv6("ipv6:[::ffff:127.0.0.1]:"); |
| 71 const grpc::string kIpv4("ipv4:127.0.0.1:"); |
| 72 return addr.substr(0, kIpv4.size()) == kIpv4 || |
| 73 addr.substr(0, kIpv4MappedIpv6.size()) == kIpv4MappedIpv6 || |
| 74 addr.substr(0, kIpv6.size()) == kIpv6; |
| 75 } |
| 76 |
| 77 class TestMetadataCredentialsPlugin : public MetadataCredentialsPlugin { |
| 78 public: |
| 79 static const char kMetadataKey[]; |
| 80 |
| 81 TestMetadataCredentialsPlugin(grpc::string_ref metadata_value, |
| 82 bool is_blocking, bool is_successful) |
| 83 : metadata_value_(metadata_value.data(), metadata_value.length()), |
| 84 is_blocking_(is_blocking), |
| 85 is_successful_(is_successful) {} |
| 86 |
| 87 bool IsBlocking() const GRPC_OVERRIDE { return is_blocking_; } |
| 88 |
| 89 Status GetMetadata(grpc::string_ref service_url, grpc::string_ref method_name, |
| 90 const grpc::AuthContext& channel_auth_context, |
| 91 std::multimap<grpc::string, grpc::string>* metadata) |
| 92 GRPC_OVERRIDE { |
| 93 EXPECT_GT(service_url.length(), 0UL); |
| 94 EXPECT_GT(method_name.length(), 0UL); |
| 95 EXPECT_TRUE(channel_auth_context.IsPeerAuthenticated()); |
| 96 EXPECT_TRUE(metadata != nullptr); |
| 97 if (is_successful_) { |
| 98 metadata->insert(std::make_pair(kMetadataKey, metadata_value_)); |
| 99 return Status::OK; |
| 100 } else { |
| 101 return Status(StatusCode::NOT_FOUND, "Could not find plugin metadata."); |
| 102 } |
| 103 } |
| 104 |
| 105 private: |
| 106 grpc::string metadata_value_; |
| 107 bool is_blocking_; |
| 108 bool is_successful_; |
| 109 }; |
| 110 |
| 111 const char TestMetadataCredentialsPlugin::kMetadataKey[] = "TestPluginMetadata"; |
| 112 |
| 113 class TestAuthMetadataProcessor : public AuthMetadataProcessor { |
| 114 public: |
| 115 static const char kGoodGuy[]; |
| 116 |
| 117 TestAuthMetadataProcessor(bool is_blocking) : is_blocking_(is_blocking) {} |
| 118 |
| 119 std::shared_ptr<CallCredentials> GetCompatibleClientCreds() { |
| 120 return MetadataCredentialsFromPlugin( |
| 121 std::unique_ptr<MetadataCredentialsPlugin>( |
| 122 new TestMetadataCredentialsPlugin(kGoodGuy, is_blocking_, true))); |
| 123 } |
| 124 |
| 125 std::shared_ptr<CallCredentials> GetIncompatibleClientCreds() { |
| 126 return MetadataCredentialsFromPlugin( |
| 127 std::unique_ptr<MetadataCredentialsPlugin>( |
| 128 new TestMetadataCredentialsPlugin("Mr Hyde", is_blocking_, true))); |
| 129 } |
| 130 |
| 131 // Interface implementation |
| 132 bool IsBlocking() const GRPC_OVERRIDE { return is_blocking_; } |
| 133 |
| 134 Status Process(const InputMetadata& auth_metadata, AuthContext* context, |
| 135 OutputMetadata* consumed_auth_metadata, |
| 136 OutputMetadata* response_metadata) GRPC_OVERRIDE { |
| 137 EXPECT_TRUE(consumed_auth_metadata != nullptr); |
| 138 EXPECT_TRUE(context != nullptr); |
| 139 EXPECT_TRUE(response_metadata != nullptr); |
| 140 auto auth_md = |
| 141 auth_metadata.find(TestMetadataCredentialsPlugin::kMetadataKey); |
| 142 EXPECT_NE(auth_md, auth_metadata.end()); |
| 143 string_ref auth_md_value = auth_md->second; |
| 144 if (auth_md_value == kGoodGuy) { |
| 145 context->AddProperty(kIdentityPropName, kGoodGuy); |
| 146 context->SetPeerIdentityPropertyName(kIdentityPropName); |
| 147 consumed_auth_metadata->insert(std::make_pair( |
| 148 string(auth_md->first.data(), auth_md->first.length()), |
| 149 string(auth_md->second.data(), auth_md->second.length()))); |
| 150 return Status::OK; |
| 151 } else { |
| 152 return Status(StatusCode::UNAUTHENTICATED, |
| 153 string("Invalid principal: ") + |
| 154 string(auth_md_value.data(), auth_md_value.length())); |
| 155 } |
| 156 } |
| 157 |
| 158 private: |
| 159 static const char kIdentityPropName[]; |
| 160 bool is_blocking_; |
| 161 }; |
| 162 |
| 163 const char TestAuthMetadataProcessor::kGoodGuy[] = "Dr Jekyll"; |
| 164 const char TestAuthMetadataProcessor::kIdentityPropName[] = "novel identity"; |
| 165 |
| 166 class Proxy : public ::grpc::testing::EchoTestService::Service { |
| 167 public: |
| 168 Proxy(std::shared_ptr<Channel> channel) |
| 169 : stub_(grpc::testing::EchoTestService::NewStub(channel)) {} |
| 170 |
| 171 Status Echo(ServerContext* server_context, const EchoRequest* request, |
| 172 EchoResponse* response) GRPC_OVERRIDE { |
| 173 std::unique_ptr<ClientContext> client_context = |
| 174 ClientContext::FromServerContext(*server_context); |
| 175 return stub_->Echo(client_context.get(), *request, response); |
| 176 } |
| 177 |
| 178 private: |
| 179 std::unique_ptr< ::grpc::testing::EchoTestService::Stub> stub_; |
| 180 }; |
| 181 |
| 182 class TestServiceImplDupPkg |
| 183 : public ::grpc::testing::duplicate::EchoTestService::Service { |
| 184 public: |
| 185 Status Echo(ServerContext* context, const EchoRequest* request, |
| 186 EchoResponse* response) GRPC_OVERRIDE { |
| 187 response->set_message("no package"); |
| 188 return Status::OK; |
| 189 } |
| 190 }; |
| 191 |
| 192 class TestScenario { |
| 193 public: |
| 194 TestScenario(bool proxy, const grpc::string& creds_type) |
| 195 : use_proxy(proxy), credentials_type(creds_type) {} |
| 196 void Log() const { |
| 197 gpr_log(GPR_INFO, "Scenario: proxy %d, credentials %s", use_proxy, |
| 198 credentials_type.c_str()); |
| 199 } |
| 200 bool use_proxy; |
| 201 const grpc::string credentials_type; |
| 202 }; |
| 203 |
| 204 class End2endTest : public ::testing::TestWithParam<TestScenario> { |
| 205 protected: |
| 206 End2endTest() |
| 207 : is_server_started_(false), |
| 208 kMaxMessageSize_(8192), |
| 209 special_service_("special") { |
| 210 GetParam().Log(); |
| 211 } |
| 212 |
| 213 void TearDown() GRPC_OVERRIDE { |
| 214 if (is_server_started_) { |
| 215 server_->Shutdown(); |
| 216 if (proxy_server_) proxy_server_->Shutdown(); |
| 217 } |
| 218 } |
| 219 |
| 220 void StartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) { |
| 221 int port = grpc_pick_unused_port_or_die(); |
| 222 server_address_ << "127.0.0.1:" << port; |
| 223 // Setup server |
| 224 ServerBuilder builder; |
| 225 auto server_creds = GetServerCredentials(GetParam().credentials_type); |
| 226 if (GetParam().credentials_type != kInsecureCredentialsType) { |
| 227 server_creds->SetAuthMetadataProcessor(processor); |
| 228 } |
| 229 builder.AddListeningPort(server_address_.str(), server_creds); |
| 230 builder.RegisterService(&service_); |
| 231 builder.RegisterService("foo.test.youtube.com", &special_service_); |
| 232 builder.SetMaxMessageSize( |
| 233 kMaxMessageSize_); // For testing max message size. |
| 234 builder.RegisterService(&dup_pkg_service_); |
| 235 server_ = builder.BuildAndStart(); |
| 236 is_server_started_ = true; |
| 237 } |
| 238 |
| 239 void ResetChannel() { |
| 240 if (!is_server_started_) { |
| 241 StartServer(std::shared_ptr<AuthMetadataProcessor>()); |
| 242 } |
| 243 EXPECT_TRUE(is_server_started_); |
| 244 ChannelArguments args; |
| 245 auto channel_creds = |
| 246 GetChannelCredentials(GetParam().credentials_type, &args); |
| 247 if (!user_agent_prefix_.empty()) { |
| 248 args.SetUserAgentPrefix(user_agent_prefix_); |
| 249 } |
| 250 args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test"); |
| 251 channel_ = CreateCustomChannel(server_address_.str(), channel_creds, args); |
| 252 } |
| 253 |
| 254 void ResetStub() { |
| 255 ResetChannel(); |
| 256 if (GetParam().use_proxy) { |
| 257 proxy_service_.reset(new Proxy(channel_)); |
| 258 int port = grpc_pick_unused_port_or_die(); |
| 259 std::ostringstream proxyaddr; |
| 260 proxyaddr << "localhost:" << port; |
| 261 ServerBuilder builder; |
| 262 builder.AddListeningPort(proxyaddr.str(), InsecureServerCredentials()); |
| 263 builder.RegisterService(proxy_service_.get()); |
| 264 proxy_server_ = builder.BuildAndStart(); |
| 265 |
| 266 channel_ = CreateChannel(proxyaddr.str(), InsecureChannelCredentials()); |
| 267 } |
| 268 |
| 269 stub_ = grpc::testing::EchoTestService::NewStub(channel_); |
| 270 } |
| 271 |
| 272 bool is_server_started_; |
| 273 std::shared_ptr<Channel> channel_; |
| 274 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
| 275 std::unique_ptr<Server> server_; |
| 276 std::unique_ptr<Server> proxy_server_; |
| 277 std::unique_ptr<Proxy> proxy_service_; |
| 278 std::ostringstream server_address_; |
| 279 const int kMaxMessageSize_; |
| 280 TestServiceImpl service_; |
| 281 TestServiceImpl special_service_; |
| 282 TestServiceImplDupPkg dup_pkg_service_; |
| 283 grpc::string user_agent_prefix_; |
| 284 }; |
| 285 |
| 286 static void SendRpc(grpc::testing::EchoTestService::Stub* stub, int num_rpcs, |
| 287 bool with_binary_metadata) { |
| 288 EchoRequest request; |
| 289 EchoResponse response; |
| 290 request.set_message("Hello hello hello hello"); |
| 291 |
| 292 for (int i = 0; i < num_rpcs; ++i) { |
| 293 ClientContext context; |
| 294 if (with_binary_metadata) { |
| 295 char bytes[8] = {'\0', '\1', '\2', '\3', '\4', '\5', '\6', (char)i}; |
| 296 context.AddMetadata("custom-bin", grpc::string(bytes, 8)); |
| 297 } |
| 298 context.set_compression_algorithm(GRPC_COMPRESS_GZIP); |
| 299 Status s = stub->Echo(&context, request, &response); |
| 300 EXPECT_EQ(response.message(), request.message()); |
| 301 EXPECT_TRUE(s.ok()); |
| 302 } |
| 303 } |
| 304 |
| 305 // This class is for testing scenarios where RPCs are cancelled on the server |
| 306 // by calling ServerContext::TryCancel() |
| 307 class End2endServerTryCancelTest : public End2endTest { |
| 308 protected: |
| 309 // Helper for testing client-streaming RPCs which are cancelled on the server. |
| 310 // Depending on the value of server_try_cancel parameter, this will test one |
| 311 // of the following three scenarios: |
| 312 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading |
| 313 // any messages from the client |
| 314 // |
| 315 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading |
| 316 // messages from the client |
| 317 // |
| 318 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all |
| 319 // the messages from the client |
| 320 // |
| 321 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL. |
| 322 void TestRequestStreamServerCancel( |
| 323 ServerTryCancelRequestPhase server_try_cancel, int num_msgs_to_send) { |
| 324 ResetStub(); |
| 325 EchoRequest request; |
| 326 EchoResponse response; |
| 327 ClientContext context; |
| 328 |
| 329 // Send server_try_cancel value in the client metadata |
| 330 context.AddMetadata(kServerTryCancelRequest, |
| 331 std::to_string(server_try_cancel)); |
| 332 |
| 333 auto stream = stub_->RequestStream(&context, &response); |
| 334 |
| 335 int num_msgs_sent = 0; |
| 336 while (num_msgs_sent < num_msgs_to_send) { |
| 337 request.set_message("hello"); |
| 338 if (!stream->Write(request)) { |
| 339 break; |
| 340 } |
| 341 num_msgs_sent++; |
| 342 } |
| 343 gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent); |
| 344 |
| 345 stream->WritesDone(); |
| 346 Status s = stream->Finish(); |
| 347 |
| 348 // At this point, we know for sure that RPC was cancelled by the server |
| 349 // since we passed server_try_cancel value in the metadata. Depending on the |
| 350 // value of server_try_cancel, the RPC might have been cancelled by the |
| 351 // server at different stages. The following validates our expectations of |
| 352 // number of messages sent in various cancellation scenarios: |
| 353 |
| 354 switch (server_try_cancel) { |
| 355 case CANCEL_BEFORE_PROCESSING: |
| 356 case CANCEL_DURING_PROCESSING: |
| 357 // If the RPC is cancelled by server before / during messages from the |
| 358 // client, it means that the client most likely did not get a chance to |
| 359 // send all the messages it wanted to send. i.e num_msgs_sent <= |
| 360 // num_msgs_to_send |
| 361 EXPECT_LE(num_msgs_sent, num_msgs_to_send); |
| 362 break; |
| 363 |
| 364 case CANCEL_AFTER_PROCESSING: |
| 365 // If the RPC was cancelled after all messages were read by the server, |
| 366 // the client did get a chance to send all its messages |
| 367 EXPECT_EQ(num_msgs_sent, num_msgs_to_send); |
| 368 break; |
| 369 |
| 370 default: |
| 371 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d", |
| 372 server_try_cancel); |
| 373 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL && |
| 374 server_try_cancel <= CANCEL_AFTER_PROCESSING); |
| 375 break; |
| 376 } |
| 377 |
| 378 EXPECT_FALSE(s.ok()); |
| 379 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 380 } |
| 381 |
| 382 // Helper for testing server-streaming RPCs which are cancelled on the server. |
| 383 // Depending on the value of server_try_cancel parameter, this will test one |
| 384 // of the following three scenarios: |
| 385 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before writing |
| 386 // any messages to the client |
| 387 // |
| 388 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while writing |
| 389 // messages to the client |
| 390 // |
| 391 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after writing all |
| 392 // the messages to the client |
| 393 // |
| 394 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL. |
| 395 void TestResponseStreamServerCancel( |
| 396 ServerTryCancelRequestPhase server_try_cancel) { |
| 397 ResetStub(); |
| 398 EchoRequest request; |
| 399 EchoResponse response; |
| 400 ClientContext context; |
| 401 |
| 402 // Send server_try_cancel in the client metadata |
| 403 context.AddMetadata(kServerTryCancelRequest, |
| 404 std::to_string(server_try_cancel)); |
| 405 |
| 406 request.set_message("hello"); |
| 407 auto stream = stub_->ResponseStream(&context, request); |
| 408 |
| 409 int num_msgs_read = 0; |
| 410 while (num_msgs_read < kNumResponseStreamsMsgs) { |
| 411 if (!stream->Read(&response)) { |
| 412 break; |
| 413 } |
| 414 EXPECT_EQ(response.message(), |
| 415 request.message() + std::to_string(num_msgs_read)); |
| 416 num_msgs_read++; |
| 417 } |
| 418 gpr_log(GPR_INFO, "Read %d messages", num_msgs_read); |
| 419 |
| 420 Status s = stream->Finish(); |
| 421 |
| 422 // Depending on the value of server_try_cancel, the RPC might have been |
| 423 // cancelled by the server at different stages. The following validates our |
| 424 // expectations of number of messages read in various cancellation |
| 425 // scenarios: |
| 426 switch (server_try_cancel) { |
| 427 case CANCEL_BEFORE_PROCESSING: |
| 428 // Server cancelled before sending any messages. Which means the client |
| 429 // wouldn't have read any |
| 430 EXPECT_EQ(num_msgs_read, 0); |
| 431 break; |
| 432 |
| 433 case CANCEL_DURING_PROCESSING: |
| 434 // Server cancelled while writing messages. Client must have read less |
| 435 // than or equal to the expected number of messages |
| 436 EXPECT_LE(num_msgs_read, kNumResponseStreamsMsgs); |
| 437 break; |
| 438 |
| 439 case CANCEL_AFTER_PROCESSING: |
| 440 // Even though the Server cancelled after writing all messages, the RPC |
| 441 // may be cancelled before the Client got a chance to read all the |
| 442 // messages. |
| 443 EXPECT_LE(num_msgs_read, kNumResponseStreamsMsgs); |
| 444 break; |
| 445 |
| 446 default: { |
| 447 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d", |
| 448 server_try_cancel); |
| 449 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL && |
| 450 server_try_cancel <= CANCEL_AFTER_PROCESSING); |
| 451 break; |
| 452 } |
| 453 } |
| 454 |
| 455 EXPECT_FALSE(s.ok()); |
| 456 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 457 } |
| 458 |
| 459 // Helper for testing bidirectional-streaming RPCs which are cancelled on the |
| 460 // server. Depending on the value of server_try_cancel parameter, this will |
| 461 // test one of the following three scenarios: |
| 462 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading/ |
| 463 // writing any messages from/to the client |
| 464 // |
| 465 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading/ |
| 466 // writing messages from/to the client |
| 467 // |
| 468 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading/writing |
| 469 // all the messages from/to the client |
| 470 // |
| 471 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL. |
| 472 void TestBidiStreamServerCancel(ServerTryCancelRequestPhase server_try_cancel, |
| 473 int num_messages) { |
| 474 ResetStub(); |
| 475 EchoRequest request; |
| 476 EchoResponse response; |
| 477 ClientContext context; |
| 478 |
| 479 // Send server_try_cancel in the client metadata |
| 480 context.AddMetadata(kServerTryCancelRequest, |
| 481 std::to_string(server_try_cancel)); |
| 482 |
| 483 auto stream = stub_->BidiStream(&context); |
| 484 |
| 485 int num_msgs_read = 0; |
| 486 int num_msgs_sent = 0; |
| 487 while (num_msgs_sent < num_messages) { |
| 488 request.set_message("hello " + std::to_string(num_msgs_sent)); |
| 489 if (!stream->Write(request)) { |
| 490 break; |
| 491 } |
| 492 num_msgs_sent++; |
| 493 |
| 494 if (!stream->Read(&response)) { |
| 495 break; |
| 496 } |
| 497 num_msgs_read++; |
| 498 |
| 499 EXPECT_EQ(response.message(), request.message()); |
| 500 } |
| 501 gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent); |
| 502 gpr_log(GPR_INFO, "Read %d messages", num_msgs_read); |
| 503 |
| 504 stream->WritesDone(); |
| 505 Status s = stream->Finish(); |
| 506 |
| 507 // Depending on the value of server_try_cancel, the RPC might have been |
| 508 // cancelled by the server at different stages. The following validates our |
| 509 // expectations of number of messages read in various cancellation |
| 510 // scenarios: |
| 511 switch (server_try_cancel) { |
| 512 case CANCEL_BEFORE_PROCESSING: |
| 513 EXPECT_EQ(num_msgs_read, 0); |
| 514 break; |
| 515 |
| 516 case CANCEL_DURING_PROCESSING: |
| 517 EXPECT_LE(num_msgs_sent, num_messages); |
| 518 EXPECT_LE(num_msgs_read, num_msgs_sent); |
| 519 break; |
| 520 |
| 521 case CANCEL_AFTER_PROCESSING: |
| 522 EXPECT_EQ(num_msgs_sent, num_messages); |
| 523 |
| 524 // The Server cancelled after reading the last message and after writing |
| 525 // the message to the client. However, the RPC cancellation might have |
| 526 // taken effect before the client actually read the response. |
| 527 EXPECT_LE(num_msgs_read, num_msgs_sent); |
| 528 break; |
| 529 |
| 530 default: |
| 531 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d", |
| 532 server_try_cancel); |
| 533 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL && |
| 534 server_try_cancel <= CANCEL_AFTER_PROCESSING); |
| 535 break; |
| 536 } |
| 537 |
| 538 EXPECT_FALSE(s.ok()); |
| 539 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 540 } |
| 541 }; |
| 542 |
| 543 TEST_P(End2endServerTryCancelTest, RequestEchoServerCancel) { |
| 544 ResetStub(); |
| 545 EchoRequest request; |
| 546 EchoResponse response; |
| 547 ClientContext context; |
| 548 |
| 549 context.AddMetadata(kServerTryCancelRequest, |
| 550 std::to_string(CANCEL_BEFORE_PROCESSING)); |
| 551 Status s = stub_->Echo(&context, request, &response); |
| 552 EXPECT_FALSE(s.ok()); |
| 553 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 554 } |
| 555 |
| 556 // Server to cancel before doing reading the request |
| 557 TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelBeforeReads) { |
| 558 TestRequestStreamServerCancel(CANCEL_BEFORE_PROCESSING, 1); |
| 559 } |
| 560 |
| 561 // Server to cancel while reading a request from the stream in parallel |
| 562 TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelDuringRead) { |
| 563 TestRequestStreamServerCancel(CANCEL_DURING_PROCESSING, 10); |
| 564 } |
| 565 |
| 566 // Server to cancel after reading all the requests but before returning to the |
| 567 // client |
| 568 TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelAfterReads) { |
| 569 TestRequestStreamServerCancel(CANCEL_AFTER_PROCESSING, 4); |
| 570 } |
| 571 |
| 572 // Server to cancel before sending any response messages |
| 573 TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelBefore) { |
| 574 TestResponseStreamServerCancel(CANCEL_BEFORE_PROCESSING); |
| 575 } |
| 576 |
| 577 // Server to cancel while writing a response to the stream in parallel |
| 578 TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelDuring) { |
| 579 TestResponseStreamServerCancel(CANCEL_DURING_PROCESSING); |
| 580 } |
| 581 |
| 582 // Server to cancel after writing all the respones to the stream but before |
| 583 // returning to the client |
| 584 TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelAfter) { |
| 585 TestResponseStreamServerCancel(CANCEL_AFTER_PROCESSING); |
| 586 } |
| 587 |
| 588 // Server to cancel before reading/writing any requests/responses on the stream |
| 589 TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelBefore) { |
| 590 TestBidiStreamServerCancel(CANCEL_BEFORE_PROCESSING, 2); |
| 591 } |
| 592 |
| 593 // Server to cancel while reading/writing requests/responses on the stream in |
| 594 // parallel |
| 595 TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelDuring) { |
| 596 TestBidiStreamServerCancel(CANCEL_DURING_PROCESSING, 10); |
| 597 } |
| 598 |
| 599 // Server to cancel after reading/writing all requests/responses on the stream |
| 600 // but before returning to the client |
| 601 TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelAfter) { |
| 602 TestBidiStreamServerCancel(CANCEL_AFTER_PROCESSING, 5); |
| 603 } |
| 604 |
| 605 TEST_P(End2endTest, SimpleRpcWithCustomeUserAgentPrefix) { |
| 606 user_agent_prefix_ = "custom_prefix"; |
| 607 ResetStub(); |
| 608 EchoRequest request; |
| 609 EchoResponse response; |
| 610 request.set_message("Hello hello hello hello"); |
| 611 request.mutable_param()->set_echo_metadata(true); |
| 612 |
| 613 ClientContext context; |
| 614 Status s = stub_->Echo(&context, request, &response); |
| 615 EXPECT_EQ(response.message(), request.message()); |
| 616 EXPECT_TRUE(s.ok()); |
| 617 const auto& trailing_metadata = context.GetServerTrailingMetadata(); |
| 618 auto iter = trailing_metadata.find("user-agent"); |
| 619 EXPECT_TRUE(iter != trailing_metadata.end()); |
| 620 grpc::string expected_prefix = user_agent_prefix_ + " grpc-c++/"; |
| 621 EXPECT_TRUE(iter->second.starts_with(expected_prefix)); |
| 622 } |
| 623 |
| 624 TEST_P(End2endTest, MultipleRpcsWithVariedBinaryMetadataValue) { |
| 625 ResetStub(); |
| 626 std::vector<std::thread*> threads; |
| 627 for (int i = 0; i < 10; ++i) { |
| 628 threads.push_back(new std::thread(SendRpc, stub_.get(), 10, true)); |
| 629 } |
| 630 for (int i = 0; i < 10; ++i) { |
| 631 threads[i]->join(); |
| 632 delete threads[i]; |
| 633 } |
| 634 } |
| 635 |
| 636 TEST_P(End2endTest, MultipleRpcs) { |
| 637 ResetStub(); |
| 638 std::vector<std::thread*> threads; |
| 639 for (int i = 0; i < 10; ++i) { |
| 640 threads.push_back(new std::thread(SendRpc, stub_.get(), 10, false)); |
| 641 } |
| 642 for (int i = 0; i < 10; ++i) { |
| 643 threads[i]->join(); |
| 644 delete threads[i]; |
| 645 } |
| 646 } |
| 647 |
| 648 TEST_P(End2endTest, RequestStreamOneRequest) { |
| 649 ResetStub(); |
| 650 EchoRequest request; |
| 651 EchoResponse response; |
| 652 ClientContext context; |
| 653 |
| 654 auto stream = stub_->RequestStream(&context, &response); |
| 655 request.set_message("hello"); |
| 656 EXPECT_TRUE(stream->Write(request)); |
| 657 stream->WritesDone(); |
| 658 Status s = stream->Finish(); |
| 659 EXPECT_EQ(response.message(), request.message()); |
| 660 EXPECT_TRUE(s.ok()); |
| 661 } |
| 662 |
| 663 TEST_P(End2endTest, RequestStreamTwoRequests) { |
| 664 ResetStub(); |
| 665 EchoRequest request; |
| 666 EchoResponse response; |
| 667 ClientContext context; |
| 668 |
| 669 auto stream = stub_->RequestStream(&context, &response); |
| 670 request.set_message("hello"); |
| 671 EXPECT_TRUE(stream->Write(request)); |
| 672 EXPECT_TRUE(stream->Write(request)); |
| 673 stream->WritesDone(); |
| 674 Status s = stream->Finish(); |
| 675 EXPECT_EQ(response.message(), "hellohello"); |
| 676 EXPECT_TRUE(s.ok()); |
| 677 } |
| 678 |
| 679 TEST_P(End2endTest, ResponseStream) { |
| 680 ResetStub(); |
| 681 EchoRequest request; |
| 682 EchoResponse response; |
| 683 ClientContext context; |
| 684 request.set_message("hello"); |
| 685 |
| 686 auto stream = stub_->ResponseStream(&context, request); |
| 687 EXPECT_TRUE(stream->Read(&response)); |
| 688 EXPECT_EQ(response.message(), request.message() + "0"); |
| 689 EXPECT_TRUE(stream->Read(&response)); |
| 690 EXPECT_EQ(response.message(), request.message() + "1"); |
| 691 EXPECT_TRUE(stream->Read(&response)); |
| 692 EXPECT_EQ(response.message(), request.message() + "2"); |
| 693 EXPECT_FALSE(stream->Read(&response)); |
| 694 |
| 695 Status s = stream->Finish(); |
| 696 EXPECT_TRUE(s.ok()); |
| 697 } |
| 698 |
| 699 TEST_P(End2endTest, BidiStream) { |
| 700 ResetStub(); |
| 701 EchoRequest request; |
| 702 EchoResponse response; |
| 703 ClientContext context; |
| 704 grpc::string msg("hello"); |
| 705 |
| 706 auto stream = stub_->BidiStream(&context); |
| 707 |
| 708 request.set_message(msg + "0"); |
| 709 EXPECT_TRUE(stream->Write(request)); |
| 710 EXPECT_TRUE(stream->Read(&response)); |
| 711 EXPECT_EQ(response.message(), request.message()); |
| 712 |
| 713 request.set_message(msg + "1"); |
| 714 EXPECT_TRUE(stream->Write(request)); |
| 715 EXPECT_TRUE(stream->Read(&response)); |
| 716 EXPECT_EQ(response.message(), request.message()); |
| 717 |
| 718 request.set_message(msg + "2"); |
| 719 EXPECT_TRUE(stream->Write(request)); |
| 720 EXPECT_TRUE(stream->Read(&response)); |
| 721 EXPECT_EQ(response.message(), request.message()); |
| 722 |
| 723 stream->WritesDone(); |
| 724 EXPECT_FALSE(stream->Read(&response)); |
| 725 EXPECT_FALSE(stream->Read(&response)); |
| 726 |
| 727 Status s = stream->Finish(); |
| 728 EXPECT_TRUE(s.ok()); |
| 729 } |
| 730 |
| 731 // Talk to the two services with the same name but different package names. |
| 732 // The two stubs are created on the same channel. |
| 733 TEST_P(End2endTest, DiffPackageServices) { |
| 734 ResetStub(); |
| 735 EchoRequest request; |
| 736 EchoResponse response; |
| 737 request.set_message("Hello"); |
| 738 |
| 739 ClientContext context; |
| 740 Status s = stub_->Echo(&context, request, &response); |
| 741 EXPECT_EQ(response.message(), request.message()); |
| 742 EXPECT_TRUE(s.ok()); |
| 743 |
| 744 std::unique_ptr<grpc::testing::duplicate::EchoTestService::Stub> dup_pkg_stub( |
| 745 grpc::testing::duplicate::EchoTestService::NewStub(channel_)); |
| 746 ClientContext context2; |
| 747 s = dup_pkg_stub->Echo(&context2, request, &response); |
| 748 EXPECT_EQ("no package", response.message()); |
| 749 EXPECT_TRUE(s.ok()); |
| 750 } |
| 751 |
| 752 void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) { |
| 753 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 754 gpr_time_from_micros(delay_us, GPR_TIMESPAN))); |
| 755 while (!service->signal_client()) { |
| 756 } |
| 757 context->TryCancel(); |
| 758 } |
| 759 |
| 760 TEST_P(End2endTest, CancelRpcBeforeStart) { |
| 761 ResetStub(); |
| 762 EchoRequest request; |
| 763 EchoResponse response; |
| 764 ClientContext context; |
| 765 request.set_message("hello"); |
| 766 context.TryCancel(); |
| 767 Status s = stub_->Echo(&context, request, &response); |
| 768 EXPECT_EQ("", response.message()); |
| 769 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 770 } |
| 771 |
| 772 // Client cancels request stream after sending two messages |
| 773 TEST_P(End2endTest, ClientCancelsRequestStream) { |
| 774 ResetStub(); |
| 775 EchoRequest request; |
| 776 EchoResponse response; |
| 777 ClientContext context; |
| 778 request.set_message("hello"); |
| 779 |
| 780 auto stream = stub_->RequestStream(&context, &response); |
| 781 EXPECT_TRUE(stream->Write(request)); |
| 782 EXPECT_TRUE(stream->Write(request)); |
| 783 |
| 784 context.TryCancel(); |
| 785 |
| 786 Status s = stream->Finish(); |
| 787 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 788 |
| 789 EXPECT_EQ(response.message(), ""); |
| 790 } |
| 791 |
| 792 // Client cancels server stream after sending some messages |
| 793 TEST_P(End2endTest, ClientCancelsResponseStream) { |
| 794 ResetStub(); |
| 795 EchoRequest request; |
| 796 EchoResponse response; |
| 797 ClientContext context; |
| 798 request.set_message("hello"); |
| 799 |
| 800 auto stream = stub_->ResponseStream(&context, request); |
| 801 |
| 802 EXPECT_TRUE(stream->Read(&response)); |
| 803 EXPECT_EQ(response.message(), request.message() + "0"); |
| 804 EXPECT_TRUE(stream->Read(&response)); |
| 805 EXPECT_EQ(response.message(), request.message() + "1"); |
| 806 |
| 807 context.TryCancel(); |
| 808 |
| 809 // The cancellation races with responses, so there might be zero or |
| 810 // one responses pending, read till failure |
| 811 |
| 812 if (stream->Read(&response)) { |
| 813 EXPECT_EQ(response.message(), request.message() + "2"); |
| 814 // Since we have cancelled, we expect the next attempt to read to fail |
| 815 EXPECT_FALSE(stream->Read(&response)); |
| 816 } |
| 817 |
| 818 Status s = stream->Finish(); |
| 819 // The final status could be either of CANCELLED or OK depending on |
| 820 // who won the race. |
| 821 EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code()); |
| 822 } |
| 823 |
| 824 // Client cancels bidi stream after sending some messages |
| 825 TEST_P(End2endTest, ClientCancelsBidi) { |
| 826 ResetStub(); |
| 827 EchoRequest request; |
| 828 EchoResponse response; |
| 829 ClientContext context; |
| 830 grpc::string msg("hello"); |
| 831 |
| 832 auto stream = stub_->BidiStream(&context); |
| 833 |
| 834 request.set_message(msg + "0"); |
| 835 EXPECT_TRUE(stream->Write(request)); |
| 836 EXPECT_TRUE(stream->Read(&response)); |
| 837 EXPECT_EQ(response.message(), request.message()); |
| 838 |
| 839 request.set_message(msg + "1"); |
| 840 EXPECT_TRUE(stream->Write(request)); |
| 841 |
| 842 context.TryCancel(); |
| 843 |
| 844 // The cancellation races with responses, so there might be zero or |
| 845 // one responses pending, read till failure |
| 846 |
| 847 if (stream->Read(&response)) { |
| 848 EXPECT_EQ(response.message(), request.message()); |
| 849 // Since we have cancelled, we expect the next attempt to read to fail |
| 850 EXPECT_FALSE(stream->Read(&response)); |
| 851 } |
| 852 |
| 853 Status s = stream->Finish(); |
| 854 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 855 } |
| 856 |
| 857 TEST_P(End2endTest, RpcMaxMessageSize) { |
| 858 ResetStub(); |
| 859 EchoRequest request; |
| 860 EchoResponse response; |
| 861 request.set_message(string(kMaxMessageSize_ * 2, 'a')); |
| 862 |
| 863 ClientContext context; |
| 864 Status s = stub_->Echo(&context, request, &response); |
| 865 EXPECT_FALSE(s.ok()); |
| 866 } |
| 867 |
| 868 // Client sends 20 requests and the server returns CANCELLED status after |
| 869 // reading 10 requests. |
| 870 TEST_P(End2endTest, RequestStreamServerEarlyCancelTest) { |
| 871 ResetStub(); |
| 872 EchoRequest request; |
| 873 EchoResponse response; |
| 874 ClientContext context; |
| 875 |
| 876 context.AddMetadata(kServerCancelAfterReads, "10"); |
| 877 auto stream = stub_->RequestStream(&context, &response); |
| 878 request.set_message("hello"); |
| 879 int send_messages = 20; |
| 880 while (send_messages > 10) { |
| 881 EXPECT_TRUE(stream->Write(request)); |
| 882 send_messages--; |
| 883 } |
| 884 while (send_messages > 0) { |
| 885 stream->Write(request); |
| 886 send_messages--; |
| 887 } |
| 888 stream->WritesDone(); |
| 889 Status s = stream->Finish(); |
| 890 EXPECT_EQ(s.error_code(), StatusCode::CANCELLED); |
| 891 } |
| 892 |
| 893 void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream, |
| 894 gpr_event* ev) { |
| 895 EchoResponse resp; |
| 896 gpr_event_set(ev, (void*)1); |
| 897 while (stream->Read(&resp)) { |
| 898 gpr_log(GPR_INFO, "Read message"); |
| 899 } |
| 900 } |
| 901 |
| 902 // Run a Read and a WritesDone simultaneously. |
| 903 TEST_P(End2endTest, SimultaneousReadWritesDone) { |
| 904 ResetStub(); |
| 905 ClientContext context; |
| 906 gpr_event ev; |
| 907 gpr_event_init(&ev); |
| 908 auto stream = stub_->BidiStream(&context); |
| 909 std::thread reader_thread(ReaderThreadFunc, stream.get(), &ev); |
| 910 gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME)); |
| 911 stream->WritesDone(); |
| 912 reader_thread.join(); |
| 913 Status s = stream->Finish(); |
| 914 EXPECT_TRUE(s.ok()); |
| 915 } |
| 916 |
| 917 TEST_P(End2endTest, ChannelState) { |
| 918 ResetStub(); |
| 919 // Start IDLE |
| 920 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(false)); |
| 921 |
| 922 // Did not ask to connect, no state change. |
| 923 CompletionQueue cq; |
| 924 std::chrono::system_clock::time_point deadline = |
| 925 std::chrono::system_clock::now() + std::chrono::milliseconds(10); |
| 926 channel_->NotifyOnStateChange(GRPC_CHANNEL_IDLE, deadline, &cq, NULL); |
| 927 void* tag; |
| 928 bool ok = true; |
| 929 cq.Next(&tag, &ok); |
| 930 EXPECT_FALSE(ok); |
| 931 |
| 932 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true)); |
| 933 EXPECT_TRUE(channel_->WaitForStateChange(GRPC_CHANNEL_IDLE, |
| 934 gpr_inf_future(GPR_CLOCK_REALTIME))); |
| 935 auto state = channel_->GetState(false); |
| 936 EXPECT_TRUE(state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_READY); |
| 937 } |
| 938 |
| 939 // Takes 10s. |
| 940 TEST_P(End2endTest, ChannelStateTimeout) { |
| 941 if (GetParam().credentials_type != kInsecureCredentialsType) { |
| 942 return; |
| 943 } |
| 944 int port = grpc_pick_unused_port_or_die(); |
| 945 std::ostringstream server_address; |
| 946 server_address << "127.0.0.1:" << port; |
| 947 // Channel to non-existing server |
| 948 auto channel = |
| 949 CreateChannel(server_address.str(), InsecureChannelCredentials()); |
| 950 // Start IDLE |
| 951 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel->GetState(true)); |
| 952 |
| 953 auto state = GRPC_CHANNEL_IDLE; |
| 954 for (int i = 0; i < 10; i++) { |
| 955 channel->WaitForStateChange( |
| 956 state, std::chrono::system_clock::now() + std::chrono::seconds(1)); |
| 957 state = channel->GetState(false); |
| 958 } |
| 959 } |
| 960 |
| 961 // Talking to a non-existing service. |
| 962 TEST_P(End2endTest, NonExistingService) { |
| 963 ResetChannel(); |
| 964 std::unique_ptr<grpc::testing::UnimplementedService::Stub> stub; |
| 965 stub = grpc::testing::UnimplementedService::NewStub(channel_); |
| 966 |
| 967 EchoRequest request; |
| 968 EchoResponse response; |
| 969 request.set_message("Hello"); |
| 970 |
| 971 ClientContext context; |
| 972 Status s = stub->Unimplemented(&context, request, &response); |
| 973 EXPECT_EQ(StatusCode::UNIMPLEMENTED, s.error_code()); |
| 974 EXPECT_EQ("", s.error_message()); |
| 975 } |
| 976 |
| 977 ////////////////////////////////////////////////////////////////////////// |
| 978 // Test with and without a proxy. |
| 979 class ProxyEnd2endTest : public End2endTest { |
| 980 protected: |
| 981 }; |
| 982 |
| 983 TEST_P(ProxyEnd2endTest, SimpleRpc) { |
| 984 ResetStub(); |
| 985 SendRpc(stub_.get(), 1, false); |
| 986 } |
| 987 |
| 988 TEST_P(ProxyEnd2endTest, MultipleRpcs) { |
| 989 ResetStub(); |
| 990 std::vector<std::thread*> threads; |
| 991 for (int i = 0; i < 10; ++i) { |
| 992 threads.push_back(new std::thread(SendRpc, stub_.get(), 10, false)); |
| 993 } |
| 994 for (int i = 0; i < 10; ++i) { |
| 995 threads[i]->join(); |
| 996 delete threads[i]; |
| 997 } |
| 998 } |
| 999 |
| 1000 // Set a 10us deadline and make sure proper error is returned. |
| 1001 TEST_P(ProxyEnd2endTest, RpcDeadlineExpires) { |
| 1002 ResetStub(); |
| 1003 EchoRequest request; |
| 1004 EchoResponse response; |
| 1005 request.set_message("Hello"); |
| 1006 request.mutable_param()->set_skip_cancelled_check(true); |
| 1007 |
| 1008 ClientContext context; |
| 1009 std::chrono::system_clock::time_point deadline = |
| 1010 std::chrono::system_clock::now() + std::chrono::microseconds(10); |
| 1011 context.set_deadline(deadline); |
| 1012 Status s = stub_->Echo(&context, request, &response); |
| 1013 EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code()); |
| 1014 } |
| 1015 |
| 1016 // Set a long but finite deadline. |
| 1017 TEST_P(ProxyEnd2endTest, RpcLongDeadline) { |
| 1018 ResetStub(); |
| 1019 EchoRequest request; |
| 1020 EchoResponse response; |
| 1021 request.set_message("Hello"); |
| 1022 |
| 1023 ClientContext context; |
| 1024 std::chrono::system_clock::time_point deadline = |
| 1025 std::chrono::system_clock::now() + std::chrono::hours(1); |
| 1026 context.set_deadline(deadline); |
| 1027 Status s = stub_->Echo(&context, request, &response); |
| 1028 EXPECT_EQ(response.message(), request.message()); |
| 1029 EXPECT_TRUE(s.ok()); |
| 1030 } |
| 1031 |
| 1032 // Ask server to echo back the deadline it sees. |
| 1033 TEST_P(ProxyEnd2endTest, EchoDeadline) { |
| 1034 ResetStub(); |
| 1035 EchoRequest request; |
| 1036 EchoResponse response; |
| 1037 request.set_message("Hello"); |
| 1038 request.mutable_param()->set_echo_deadline(true); |
| 1039 |
| 1040 ClientContext context; |
| 1041 std::chrono::system_clock::time_point deadline = |
| 1042 std::chrono::system_clock::now() + std::chrono::seconds(100); |
| 1043 context.set_deadline(deadline); |
| 1044 Status s = stub_->Echo(&context, request, &response); |
| 1045 EXPECT_EQ(response.message(), request.message()); |
| 1046 EXPECT_TRUE(s.ok()); |
| 1047 gpr_timespec sent_deadline; |
| 1048 Timepoint2Timespec(deadline, &sent_deadline); |
| 1049 // Allow 1 second error. |
| 1050 EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1); |
| 1051 EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1); |
| 1052 } |
| 1053 |
| 1054 // Ask server to echo back the deadline it sees. The rpc has no deadline. |
| 1055 TEST_P(ProxyEnd2endTest, EchoDeadlineForNoDeadlineRpc) { |
| 1056 ResetStub(); |
| 1057 EchoRequest request; |
| 1058 EchoResponse response; |
| 1059 request.set_message("Hello"); |
| 1060 request.mutable_param()->set_echo_deadline(true); |
| 1061 |
| 1062 ClientContext context; |
| 1063 Status s = stub_->Echo(&context, request, &response); |
| 1064 EXPECT_EQ(response.message(), request.message()); |
| 1065 EXPECT_TRUE(s.ok()); |
| 1066 EXPECT_EQ(response.param().request_deadline(), |
| 1067 gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec); |
| 1068 } |
| 1069 |
| 1070 TEST_P(ProxyEnd2endTest, UnimplementedRpc) { |
| 1071 ResetStub(); |
| 1072 EchoRequest request; |
| 1073 EchoResponse response; |
| 1074 request.set_message("Hello"); |
| 1075 |
| 1076 ClientContext context; |
| 1077 Status s = stub_->Unimplemented(&context, request, &response); |
| 1078 EXPECT_FALSE(s.ok()); |
| 1079 EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED); |
| 1080 EXPECT_EQ(s.error_message(), ""); |
| 1081 EXPECT_EQ(response.message(), ""); |
| 1082 } |
| 1083 |
| 1084 // Client cancels rpc after 10ms |
| 1085 TEST_P(ProxyEnd2endTest, ClientCancelsRpc) { |
| 1086 ResetStub(); |
| 1087 EchoRequest request; |
| 1088 EchoResponse response; |
| 1089 request.set_message("Hello"); |
| 1090 const int kCancelDelayUs = 10 * 1000; |
| 1091 request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs); |
| 1092 |
| 1093 ClientContext context; |
| 1094 std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_); |
| 1095 Status s = stub_->Echo(&context, request, &response); |
| 1096 cancel_thread.join(); |
| 1097 EXPECT_EQ(StatusCode::CANCELLED, s.error_code()); |
| 1098 EXPECT_EQ(s.error_message(), "Cancelled"); |
| 1099 } |
| 1100 |
| 1101 // Server cancels rpc after 1ms |
| 1102 TEST_P(ProxyEnd2endTest, ServerCancelsRpc) { |
| 1103 ResetStub(); |
| 1104 EchoRequest request; |
| 1105 EchoResponse response; |
| 1106 request.set_message("Hello"); |
| 1107 request.mutable_param()->set_server_cancel_after_us(1000); |
| 1108 |
| 1109 ClientContext context; |
| 1110 Status s = stub_->Echo(&context, request, &response); |
| 1111 EXPECT_EQ(StatusCode::CANCELLED, s.error_code()); |
| 1112 EXPECT_TRUE(s.error_message().empty()); |
| 1113 } |
| 1114 |
| 1115 // Make the response larger than the flow control window. |
| 1116 TEST_P(ProxyEnd2endTest, HugeResponse) { |
| 1117 ResetStub(); |
| 1118 EchoRequest request; |
| 1119 EchoResponse response; |
| 1120 request.set_message("huge response"); |
| 1121 const size_t kResponseSize = 1024 * (1024 + 10); |
| 1122 request.mutable_param()->set_response_message_length(kResponseSize); |
| 1123 |
| 1124 ClientContext context; |
| 1125 Status s = stub_->Echo(&context, request, &response); |
| 1126 EXPECT_EQ(kResponseSize, response.message().size()); |
| 1127 EXPECT_TRUE(s.ok()); |
| 1128 } |
| 1129 |
| 1130 TEST_P(ProxyEnd2endTest, Peer) { |
| 1131 ResetStub(); |
| 1132 EchoRequest request; |
| 1133 EchoResponse response; |
| 1134 request.set_message("hello"); |
| 1135 request.mutable_param()->set_echo_peer(true); |
| 1136 |
| 1137 ClientContext context; |
| 1138 Status s = stub_->Echo(&context, request, &response); |
| 1139 EXPECT_EQ(response.message(), request.message()); |
| 1140 EXPECT_TRUE(s.ok()); |
| 1141 EXPECT_TRUE(CheckIsLocalhost(response.param().peer())); |
| 1142 EXPECT_TRUE(CheckIsLocalhost(context.peer())); |
| 1143 } |
| 1144 |
| 1145 ////////////////////////////////////////////////////////////////////////// |
| 1146 class SecureEnd2endTest : public End2endTest { |
| 1147 protected: |
| 1148 SecureEnd2endTest() { |
| 1149 GPR_ASSERT(!GetParam().use_proxy); |
| 1150 GPR_ASSERT(GetParam().credentials_type != kInsecureCredentialsType); |
| 1151 } |
| 1152 }; |
| 1153 |
| 1154 TEST_P(SecureEnd2endTest, SimpleRpcWithHost) { |
| 1155 ResetStub(); |
| 1156 |
| 1157 EchoRequest request; |
| 1158 EchoResponse response; |
| 1159 request.set_message("Hello"); |
| 1160 |
| 1161 ClientContext context; |
| 1162 context.set_authority("foo.test.youtube.com"); |
| 1163 Status s = stub_->Echo(&context, request, &response); |
| 1164 EXPECT_EQ(response.message(), request.message()); |
| 1165 EXPECT_TRUE(response.has_param()); |
| 1166 EXPECT_EQ("special", response.param().host()); |
| 1167 EXPECT_TRUE(s.ok()); |
| 1168 } |
| 1169 |
| 1170 bool MetadataContains( |
| 1171 const std::multimap<grpc::string_ref, grpc::string_ref>& metadata, |
| 1172 const grpc::string& key, const grpc::string& value) { |
| 1173 int count = 0; |
| 1174 |
| 1175 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator iter = |
| 1176 metadata.begin(); |
| 1177 iter != metadata.end(); ++iter) { |
| 1178 if (ToString(iter->first) == key && ToString(iter->second) == value) { |
| 1179 count++; |
| 1180 } |
| 1181 } |
| 1182 return count == 1; |
| 1183 } |
| 1184 |
| 1185 TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorSuccess) { |
| 1186 auto* processor = new TestAuthMetadataProcessor(true); |
| 1187 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor)); |
| 1188 ResetStub(); |
| 1189 EchoRequest request; |
| 1190 EchoResponse response; |
| 1191 ClientContext context; |
| 1192 context.set_credentials(processor->GetCompatibleClientCreds()); |
| 1193 request.set_message("Hello"); |
| 1194 request.mutable_param()->set_echo_metadata(true); |
| 1195 request.mutable_param()->set_expected_client_identity( |
| 1196 TestAuthMetadataProcessor::kGoodGuy); |
| 1197 |
| 1198 Status s = stub_->Echo(&context, request, &response); |
| 1199 EXPECT_EQ(request.message(), response.message()); |
| 1200 EXPECT_TRUE(s.ok()); |
| 1201 |
| 1202 // Metadata should have been consumed by the processor. |
| 1203 EXPECT_FALSE(MetadataContains( |
| 1204 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY, |
| 1205 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy)); |
| 1206 } |
| 1207 |
| 1208 TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorFailure) { |
| 1209 auto* processor = new TestAuthMetadataProcessor(true); |
| 1210 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor)); |
| 1211 ResetStub(); |
| 1212 EchoRequest request; |
| 1213 EchoResponse response; |
| 1214 ClientContext context; |
| 1215 context.set_credentials(processor->GetIncompatibleClientCreds()); |
| 1216 request.set_message("Hello"); |
| 1217 |
| 1218 Status s = stub_->Echo(&context, request, &response); |
| 1219 EXPECT_FALSE(s.ok()); |
| 1220 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED); |
| 1221 } |
| 1222 TEST_P(SecureEnd2endTest, SetPerCallCredentials) { |
| 1223 ResetStub(); |
| 1224 EchoRequest request; |
| 1225 EchoResponse response; |
| 1226 ClientContext context; |
| 1227 std::shared_ptr<CallCredentials> creds = |
| 1228 GoogleIAMCredentials("fake_token", "fake_selector"); |
| 1229 context.set_credentials(creds); |
| 1230 request.set_message("Hello"); |
| 1231 request.mutable_param()->set_echo_metadata(true); |
| 1232 |
| 1233 Status s = stub_->Echo(&context, request, &response); |
| 1234 EXPECT_EQ(request.message(), response.message()); |
| 1235 EXPECT_TRUE(s.ok()); |
| 1236 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1237 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 1238 "fake_token")); |
| 1239 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1240 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 1241 "fake_selector")); |
| 1242 } |
| 1243 |
| 1244 TEST_P(SecureEnd2endTest, OverridePerCallCredentials) { |
| 1245 ResetStub(); |
| 1246 EchoRequest request; |
| 1247 EchoResponse response; |
| 1248 ClientContext context; |
| 1249 std::shared_ptr<CallCredentials> creds1 = |
| 1250 GoogleIAMCredentials("fake_token1", "fake_selector1"); |
| 1251 context.set_credentials(creds1); |
| 1252 std::shared_ptr<CallCredentials> creds2 = |
| 1253 GoogleIAMCredentials("fake_token2", "fake_selector2"); |
| 1254 context.set_credentials(creds2); |
| 1255 request.set_message("Hello"); |
| 1256 request.mutable_param()->set_echo_metadata(true); |
| 1257 |
| 1258 Status s = stub_->Echo(&context, request, &response); |
| 1259 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1260 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 1261 "fake_token2")); |
| 1262 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1263 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 1264 "fake_selector2")); |
| 1265 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1266 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 1267 "fake_token1")); |
| 1268 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1269 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 1270 "fake_selector1")); |
| 1271 EXPECT_EQ(request.message(), response.message()); |
| 1272 EXPECT_TRUE(s.ok()); |
| 1273 } |
| 1274 |
| 1275 TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginFailure) { |
| 1276 ResetStub(); |
| 1277 EchoRequest request; |
| 1278 EchoResponse response; |
| 1279 ClientContext context; |
| 1280 context.set_credentials( |
| 1281 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>( |
| 1282 new TestMetadataCredentialsPlugin( |
| 1283 "Does not matter, will fail anyway (see 3rd param)", false, |
| 1284 false)))); |
| 1285 request.set_message("Hello"); |
| 1286 |
| 1287 Status s = stub_->Echo(&context, request, &response); |
| 1288 EXPECT_FALSE(s.ok()); |
| 1289 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED); |
| 1290 } |
| 1291 |
| 1292 TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorSuccess) { |
| 1293 auto* processor = new TestAuthMetadataProcessor(false); |
| 1294 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor)); |
| 1295 ResetStub(); |
| 1296 EchoRequest request; |
| 1297 EchoResponse response; |
| 1298 ClientContext context; |
| 1299 context.set_credentials(processor->GetCompatibleClientCreds()); |
| 1300 request.set_message("Hello"); |
| 1301 request.mutable_param()->set_echo_metadata(true); |
| 1302 request.mutable_param()->set_expected_client_identity( |
| 1303 TestAuthMetadataProcessor::kGoodGuy); |
| 1304 |
| 1305 Status s = stub_->Echo(&context, request, &response); |
| 1306 EXPECT_EQ(request.message(), response.message()); |
| 1307 EXPECT_TRUE(s.ok()); |
| 1308 |
| 1309 // Metadata should have been consumed by the processor. |
| 1310 EXPECT_FALSE(MetadataContains( |
| 1311 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY, |
| 1312 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy)); |
| 1313 } |
| 1314 |
| 1315 TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorFailure) { |
| 1316 auto* processor = new TestAuthMetadataProcessor(false); |
| 1317 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor)); |
| 1318 ResetStub(); |
| 1319 EchoRequest request; |
| 1320 EchoResponse response; |
| 1321 ClientContext context; |
| 1322 context.set_credentials(processor->GetIncompatibleClientCreds()); |
| 1323 request.set_message("Hello"); |
| 1324 |
| 1325 Status s = stub_->Echo(&context, request, &response); |
| 1326 EXPECT_FALSE(s.ok()); |
| 1327 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED); |
| 1328 } |
| 1329 |
| 1330 TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginFailure) { |
| 1331 ResetStub(); |
| 1332 EchoRequest request; |
| 1333 EchoResponse response; |
| 1334 ClientContext context; |
| 1335 context.set_credentials( |
| 1336 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>( |
| 1337 new TestMetadataCredentialsPlugin( |
| 1338 "Does not matter, will fail anyway (see 3rd param)", true, |
| 1339 false)))); |
| 1340 request.set_message("Hello"); |
| 1341 |
| 1342 Status s = stub_->Echo(&context, request, &response); |
| 1343 EXPECT_FALSE(s.ok()); |
| 1344 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED); |
| 1345 } |
| 1346 |
| 1347 TEST_P(SecureEnd2endTest, ClientAuthContext) { |
| 1348 ResetStub(); |
| 1349 EchoRequest request; |
| 1350 EchoResponse response; |
| 1351 request.set_message("Hello"); |
| 1352 request.mutable_param()->set_check_auth_context(true); |
| 1353 |
| 1354 ClientContext context; |
| 1355 Status s = stub_->Echo(&context, request, &response); |
| 1356 EXPECT_EQ(response.message(), request.message()); |
| 1357 EXPECT_TRUE(s.ok()); |
| 1358 |
| 1359 std::shared_ptr<const AuthContext> auth_ctx = context.auth_context(); |
| 1360 std::vector<grpc::string_ref> ssl = |
| 1361 auth_ctx->FindPropertyValues("transport_security_type"); |
| 1362 EXPECT_EQ(1u, ssl.size()); |
| 1363 EXPECT_EQ("ssl", ToString(ssl[0])); |
| 1364 EXPECT_EQ("x509_subject_alternative_name", |
| 1365 auth_ctx->GetPeerIdentityPropertyName()); |
| 1366 EXPECT_EQ(3u, auth_ctx->GetPeerIdentity().size()); |
| 1367 EXPECT_EQ("*.test.google.fr", ToString(auth_ctx->GetPeerIdentity()[0])); |
| 1368 EXPECT_EQ("waterzooi.test.google.be", |
| 1369 ToString(auth_ctx->GetPeerIdentity()[1])); |
| 1370 EXPECT_EQ("*.test.youtube.com", ToString(auth_ctx->GetPeerIdentity()[2])); |
| 1371 } |
| 1372 |
| 1373 std::vector<TestScenario> CreateTestScenarios(bool use_proxy, |
| 1374 bool test_insecure, |
| 1375 bool test_secure) { |
| 1376 std::vector<TestScenario> scenarios; |
| 1377 std::vector<grpc::string> credentials_types; |
| 1378 if (test_secure) { |
| 1379 credentials_types = GetSecureCredentialsTypeList(); |
| 1380 } |
| 1381 if (test_insecure) { |
| 1382 credentials_types.push_back(kInsecureCredentialsType); |
| 1383 } |
| 1384 for (auto it = credentials_types.begin(); it != credentials_types.end(); |
| 1385 ++it) { |
| 1386 scenarios.push_back(TestScenario(false, *it)); |
| 1387 if (use_proxy) { |
| 1388 scenarios.push_back(TestScenario(true, *it)); |
| 1389 } |
| 1390 } |
| 1391 return scenarios; |
| 1392 } |
| 1393 |
| 1394 INSTANTIATE_TEST_CASE_P(End2end, End2endTest, |
| 1395 ::testing::ValuesIn(CreateTestScenarios(false, true, |
| 1396 true))); |
| 1397 |
| 1398 INSTANTIATE_TEST_CASE_P(End2endServerTryCancel, End2endServerTryCancelTest, |
| 1399 ::testing::ValuesIn(CreateTestScenarios(false, true, |
| 1400 false))); |
| 1401 |
| 1402 INSTANTIATE_TEST_CASE_P(ProxyEnd2end, ProxyEnd2endTest, |
| 1403 ::testing::ValuesIn(CreateTestScenarios(true, true, |
| 1404 true))); |
| 1405 |
| 1406 INSTANTIATE_TEST_CASE_P(SecureEnd2end, SecureEnd2endTest, |
| 1407 ::testing::ValuesIn(CreateTestScenarios(false, false, |
| 1408 true))); |
| 1409 |
| 1410 } // namespace |
| 1411 } // namespace testing |
| 1412 } // namespace grpc |
| 1413 |
| 1414 int main(int argc, char** argv) { |
| 1415 grpc_test_init(argc, argv); |
| 1416 ::testing::InitGoogleTest(&argc, argv); |
| 1417 return RUN_ALL_TESTS(); |
| 1418 } |
OLD | NEW |