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