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