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