blob: 578f26e9c2d3112cb9776383376f7505d0dcc944 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
Yang Gao69fe0752015-06-01 14:32:38 -070019#include <mutex>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080020#include <thread>
yangged5e7e02015-01-06 10:16:15 -080021
yang-g9e2f90c2015-08-21 15:35:03 -070022#include <grpc++/channel.h>
23#include <grpc++/client_context.h>
24#include <grpc++/create_channel.h>
Craig Tiller20afa3d2016-10-17 14:52:14 -070025#include <grpc++/resource_quota.h>
Julien Boeuf5be92a32015-08-28 16:28:18 -070026#include <grpc++/security/auth_metadata_processor.h>
27#include <grpc++/security/credentials.h>
28#include <grpc++/security/server_credentials.h>
yang-g9e2f90c2015-08-21 15:35:03 -070029#include <grpc++/server.h>
30#include <grpc++/server_builder.h>
31#include <grpc++/server_context.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080032#include <grpc/grpc.h>
Yuchen Zeng7fa814f2017-10-15 19:00:16 -070033#include <grpc/support/alloc.h>
David Garcia Quintasc79b0652016-07-27 21:11:58 -070034#include <grpc/support/log.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080035#include <grpc/support/thd.h>
36#include <grpc/support/time.h>
yang-g9e2f90c2015-08-21 15:35:03 -070037
Mark D. Rothdbdf4952018-01-18 11:21:12 -080038#include "src/core/lib/gpr/env.h"
Julien Boeuf8ca294e2016-05-02 14:56:30 -070039#include "src/core/lib/security/credentials/credentials.h"
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080040#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
41#include "src/proto/grpc/testing/echo.grpc.pb.h"
Nicolas Noble89219162015-04-07 18:01:18 -070042#include "test/core/util/port.h"
Craig Tiller14e60e92015-01-13 17:26:46 -080043#include "test/core/util/test_config.h"
yang-g7b0edbd2016-02-02 16:05:21 -080044#include "test/cpp/end2end/test_service_impl.h"
yang-ge21908f2015-08-25 13:47:51 -070045#include "test/cpp/util/string_ref_helper.h"
yang-g7d2a3e12016-02-18 15:41:56 -080046#include "test/cpp/util/test_credentials_provider.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047
Nicolas "Pixel" Noble3726e3d2017-05-10 18:33:12 +020048#include <gtest/gtest.h>
49
Craig Tiller1b4e3302015-12-17 16:35:00 -080050using grpc::testing::EchoRequest;
51using grpc::testing::EchoResponse;
Dan Bornf2f7d572016-03-03 17:26:12 -080052using grpc::testing::kTlsCredentialsType;
yangged5e7e02015-01-06 10:16:15 -080053using std::chrono::system_clock;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054
55namespace grpc {
yangged5e7e02015-01-06 10:16:15 -080056namespace testing {
yangged5e7e02015-01-06 10:16:15 -080057namespace {
58
yang-gd7ead692015-07-30 10:57:45 -070059bool CheckIsLocalhost(const grpc::string& addr) {
60 const grpc::string kIpv6("ipv6:[::1]:");
61 const grpc::string kIpv4MappedIpv6("ipv6:[::ffff:127.0.0.1]:");
62 const grpc::string kIpv4("ipv4:127.0.0.1:");
63 return addr.substr(0, kIpv4.size()) == kIpv4 ||
64 addr.substr(0, kIpv4MappedIpv6.size()) == kIpv4MappedIpv6 ||
65 addr.substr(0, kIpv6.size()) == kIpv6;
66}
67
Julien Boeuf38c0cde2016-06-06 14:46:08 +020068const char kTestCredsPluginErrorMsg[] = "Could not find plugin metadata.";
69
Julien Boeuf1928d492015-09-15 15:20:11 -070070class TestMetadataCredentialsPlugin : public MetadataCredentialsPlugin {
71 public:
yang-gc580af32016-09-15 15:28:38 -070072 static const char kGoodMetadataKey[];
73 static const char kBadMetadataKey[];
Julien Boeuf1928d492015-09-15 15:20:11 -070074
yang-gc580af32016-09-15 15:28:38 -070075 TestMetadataCredentialsPlugin(grpc::string_ref metadata_key,
76 grpc::string_ref metadata_value,
Julien Boeuf1928d492015-09-15 15:20:11 -070077 bool is_blocking, bool is_successful)
yang-gc580af32016-09-15 15:28:38 -070078 : metadata_key_(metadata_key.data(), metadata_key.length()),
79 metadata_value_(metadata_value.data(), metadata_value.length()),
Julien Boeuf1928d492015-09-15 15:20:11 -070080 is_blocking_(is_blocking),
81 is_successful_(is_successful) {}
82
Vijay Paic0b2acb2016-11-01 16:31:56 -070083 bool IsBlocking() const override { return is_blocking_; }
Julien Boeuf1928d492015-09-15 15:20:11 -070084
Vijay Pai713c7b82016-11-01 16:33:18 -070085 Status GetMetadata(
86 grpc::string_ref service_url, grpc::string_ref method_name,
87 const grpc::AuthContext& channel_auth_context,
88 std::multimap<grpc::string, grpc::string>* metadata) override {
Julien Boeuf1928d492015-09-15 15:20:11 -070089 EXPECT_GT(service_url.length(), 0UL);
Julien Boeuf114f3942015-11-19 21:45:52 -080090 EXPECT_GT(method_name.length(), 0UL);
91 EXPECT_TRUE(channel_auth_context.IsPeerAuthenticated());
Julien Boeuf1928d492015-09-15 15:20:11 -070092 EXPECT_TRUE(metadata != nullptr);
93 if (is_successful_) {
yang-gc580af32016-09-15 15:28:38 -070094 metadata->insert(std::make_pair(metadata_key_, metadata_value_));
Julien Boeuf1928d492015-09-15 15:20:11 -070095 return Status::OK;
96 } else {
Julien Boeuf38c0cde2016-06-06 14:46:08 +020097 return Status(StatusCode::NOT_FOUND, kTestCredsPluginErrorMsg);
Julien Boeuf1928d492015-09-15 15:20:11 -070098 }
99 }
100
101 private:
yang-gc580af32016-09-15 15:28:38 -0700102 grpc::string metadata_key_;
Julien Boeuf1928d492015-09-15 15:20:11 -0700103 grpc::string metadata_value_;
104 bool is_blocking_;
105 bool is_successful_;
106};
107
yang-gc580af32016-09-15 15:28:38 -0700108const char TestMetadataCredentialsPlugin::kBadMetadataKey[] =
109 "TestPluginMetadata";
110const char TestMetadataCredentialsPlugin::kGoodMetadataKey[] =
111 "test-plugin-metadata";
Julien Boeuf1928d492015-09-15 15:20:11 -0700112
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700113class TestAuthMetadataProcessor : public AuthMetadataProcessor {
114 public:
115 static const char kGoodGuy[];
116
117 TestAuthMetadataProcessor(bool is_blocking) : is_blocking_(is_blocking) {}
118
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700119 std::shared_ptr<CallCredentials> GetCompatibleClientCreds() {
Julien Boeuf1928d492015-09-15 15:20:11 -0700120 return MetadataCredentialsFromPlugin(
121 std::unique_ptr<MetadataCredentialsPlugin>(
yang-gc580af32016-09-15 15:28:38 -0700122 new TestMetadataCredentialsPlugin(
123 TestMetadataCredentialsPlugin::kGoodMetadataKey, kGoodGuy,
124 is_blocking_, true)));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700125 }
Julien Boeuf1928d492015-09-15 15:20:11 -0700126
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700127 std::shared_ptr<CallCredentials> GetIncompatibleClientCreds() {
Julien Boeuf1928d492015-09-15 15:20:11 -0700128 return MetadataCredentialsFromPlugin(
129 std::unique_ptr<MetadataCredentialsPlugin>(
yang-gc580af32016-09-15 15:28:38 -0700130 new TestMetadataCredentialsPlugin(
131 TestMetadataCredentialsPlugin::kGoodMetadataKey, "Mr Hyde",
132 is_blocking_, true)));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700133 }
134
135 // Interface implementation
Vijay Paic0b2acb2016-11-01 16:31:56 -0700136 bool IsBlocking() const override { return is_blocking_; }
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700137
138 Status Process(const InputMetadata& auth_metadata, AuthContext* context,
139 OutputMetadata* consumed_auth_metadata,
Vijay Paic0b2acb2016-11-01 16:31:56 -0700140 OutputMetadata* response_metadata) override {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700141 EXPECT_TRUE(consumed_auth_metadata != nullptr);
142 EXPECT_TRUE(context != nullptr);
143 EXPECT_TRUE(response_metadata != nullptr);
Julien Boeuf1928d492015-09-15 15:20:11 -0700144 auto auth_md =
yang-gc580af32016-09-15 15:28:38 -0700145 auth_metadata.find(TestMetadataCredentialsPlugin::kGoodMetadataKey);
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700146 EXPECT_NE(auth_md, auth_metadata.end());
147 string_ref auth_md_value = auth_md->second;
Julien Boeuf1928d492015-09-15 15:20:11 -0700148 if (auth_md_value == kGoodGuy) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700149 context->AddProperty(kIdentityPropName, kGoodGuy);
150 context->SetPeerIdentityPropertyName(kIdentityPropName);
Julien Boeuf8b0b6f42015-09-22 13:31:16 -0700151 consumed_auth_metadata->insert(std::make_pair(
152 string(auth_md->first.data(), auth_md->first.length()),
153 string(auth_md->second.data(), auth_md->second.length())));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700154 return Status::OK;
155 } else {
156 return Status(StatusCode::UNAUTHENTICATED,
157 string("Invalid principal: ") +
158 string(auth_md_value.data(), auth_md_value.length()));
159 }
160 }
161
Julien Boeuf1928d492015-09-15 15:20:11 -0700162 private:
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700163 static const char kIdentityPropName[];
164 bool is_blocking_;
165};
166
167const char TestAuthMetadataProcessor::kGoodGuy[] = "Dr Jekyll";
168const char TestAuthMetadataProcessor::kIdentityPropName[] = "novel identity";
169
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800170class Proxy : public ::grpc::testing::EchoTestService::Service {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700171 public:
yang-g8c2be9f2015-08-19 16:28:09 -0700172 Proxy(std::shared_ptr<Channel> channel)
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800173 : stub_(grpc::testing::EchoTestService::NewStub(channel)) {}
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700174
175 Status Echo(ServerContext* server_context, const EchoRequest* request,
Vijay Paic0b2acb2016-11-01 16:31:56 -0700176 EchoResponse* response) override {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700177 std::unique_ptr<ClientContext> client_context =
178 ClientContext::FromServerContext(*server_context);
179 return stub_->Echo(client_context.get(), *request, response);
180 }
181
182 private:
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800183 std::unique_ptr< ::grpc::testing::EchoTestService::Stub> stub_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700184};
185
yangg1456d152015-01-08 15:39:58 -0800186class TestServiceImplDupPkg
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800187 : public ::grpc::testing::duplicate::EchoTestService::Service {
yangg1456d152015-01-08 15:39:58 -0800188 public:
189 Status Echo(ServerContext* context, const EchoRequest* request,
Vijay Paic0b2acb2016-11-01 16:31:56 -0700190 EchoResponse* response) override {
yangg1456d152015-01-08 15:39:58 -0800191 response->set_message("no package");
192 return Status::OK;
193 }
194};
195
yang-g88d5d522015-09-29 12:46:54 -0700196class TestScenario {
197 public:
Vijay Pai3d7d5f42017-05-04 10:02:24 -0700198 TestScenario(bool proxy, bool inproc_stub, const grpc::string& creds_type)
199 : use_proxy(proxy), inproc(inproc_stub), credentials_type(creds_type) {}
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800200 void Log() const;
yang-g88d5d522015-09-29 12:46:54 -0700201 bool use_proxy;
Vijay Pai3d7d5f42017-05-04 10:02:24 -0700202 bool inproc;
Vijay Paif063f792017-10-05 15:41:40 -0700203 const grpc::string credentials_type;
yang-g88d5d522015-09-29 12:46:54 -0700204};
205
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800206static std::ostream& operator<<(std::ostream& out,
207 const TestScenario& scenario) {
208 return out << "TestScenario{use_proxy="
Vijay Pai3d7d5f42017-05-04 10:02:24 -0700209 << (scenario.use_proxy ? "true" : "false")
210 << ", inproc=" << (scenario.inproc ? "true" : "false")
211 << ", credentials='" << scenario.credentials_type << "'}";
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800212}
213
214void TestScenario::Log() const {
215 std::ostringstream out;
216 out << *this;
217 gpr_log(GPR_DEBUG, "%s", out.str().c_str());
218}
219
yang-g88d5d522015-09-29 12:46:54 -0700220class End2endTest : public ::testing::TestWithParam<TestScenario> {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800221 protected:
Vijay Pai181ef452015-07-14 13:52:48 -0700222 End2endTest()
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700223 : is_server_started_(false),
224 kMaxMessageSize_(8192),
Alexander Polcyn7a57b0c2017-10-13 17:01:32 -0700225 special_service_("special"),
226 first_picked_port_(0) {
yang-g88d5d522015-09-29 12:46:54 -0700227 GetParam().Log();
228 }
Craig Tiller7418d012015-02-11 15:25:03 -0800229
Vijay Paic0b2acb2016-11-01 16:31:56 -0700230 void TearDown() override {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700231 if (is_server_started_) {
232 server_->Shutdown();
233 if (proxy_server_) proxy_server_->Shutdown();
234 }
Alexander Polcyn7a57b0c2017-10-13 17:01:32 -0700235 if (first_picked_port_ > 0) {
236 grpc_recycle_unused_port(first_picked_port_);
237 }
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700238 }
239
240 void StartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) {
Craig Tiller35e39712015-01-12 16:41:24 -0800241 int port = grpc_pick_unused_port_or_die();
Alexander Polcyn7a57b0c2017-10-13 17:01:32 -0700242 first_picked_port_ = port;
yang-gd7ead692015-07-30 10:57:45 -0700243 server_address_ << "127.0.0.1:" << port;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800244 // Setup server
Yuchen Zengee3e3312017-08-04 13:54:04 -0700245 BuildAndStartServer(processor);
246 }
247
248 void RestartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) {
249 if (is_server_started_) {
250 server_->Shutdown();
251 BuildAndStartServer(processor);
252 }
253 }
254
255 void BuildAndStartServer(
256 const std::shared_ptr<AuthMetadataProcessor>& processor) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800257 ServerBuilder builder;
Craig Tillerdb1a5cc2016-09-28 14:22:12 -0700258 ConfigureServerBuilder(&builder);
yang-gcc591022017-01-11 11:10:43 -0800259 auto server_creds = GetCredentialsProvider()->GetServerCredentials(
260 GetParam().credentials_type);
yang-g17197dd2016-02-19 00:04:22 -0800261 if (GetParam().credentials_type != kInsecureCredentialsType) {
yang-g88d5d522015-09-29 12:46:54 -0700262 server_creds->SetAuthMetadataProcessor(processor);
263 }
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700264 builder.AddListeningPort(server_address_.str(), server_creds);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800265 builder.RegisterService(&service_);
yang-g8b25f2a2015-07-21 23:54:36 -0700266 builder.RegisterService("foo.test.youtube.com", &special_service_);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800267 builder.RegisterService(&dup_pkg_service_);
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700268
Sree Kuchibhotlac37a8a52016-10-13 15:40:15 -0700269 builder.SetSyncServerOption(ServerBuilder::SyncServerOption::NUM_CQS, 4);
270 builder.SetSyncServerOption(
271 ServerBuilder::SyncServerOption::CQ_TIMEOUT_MSEC, 10);
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700272
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800273 server_ = builder.BuildAndStart();
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700274 is_server_started_ = true;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700275 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800276
Craig Tillerdb1a5cc2016-09-28 14:22:12 -0700277 virtual void ConfigureServerBuilder(ServerBuilder* builder) {
278 builder->SetMaxMessageSize(
279 kMaxMessageSize_); // For testing max message size.
280 }
281
yang-g9b7757d2015-08-13 11:15:53 -0700282 void ResetChannel() {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700283 if (!is_server_started_) {
284 StartServer(std::shared_ptr<AuthMetadataProcessor>());
285 }
286 EXPECT_TRUE(is_server_started_);
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700287 ChannelArguments args;
yang-gcc591022017-01-11 11:10:43 -0800288 auto channel_creds = GetCredentialsProvider()->GetChannelCredentials(
289 GetParam().credentials_type, &args);
yang-gd59ad7e2016-02-10 12:42:53 -0800290 if (!user_agent_prefix_.empty()) {
291 args.SetUserAgentPrefix(user_agent_prefix_);
292 }
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700293 args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test");
Vijay Pai3d7d5f42017-05-04 10:02:24 -0700294
295 if (!GetParam().inproc) {
296 channel_ =
297 CreateCustomChannel(server_address_.str(), channel_creds, args);
298 } else {
299 channel_ = server_->InProcessChannel(args);
300 }
yang-g9b7757d2015-08-13 11:15:53 -0700301 }
302
yang-g88d5d522015-09-29 12:46:54 -0700303 void ResetStub() {
yang-g9b7757d2015-08-13 11:15:53 -0700304 ResetChannel();
yang-g88d5d522015-09-29 12:46:54 -0700305 if (GetParam().use_proxy) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700306 proxy_service_.reset(new Proxy(channel_));
307 int port = grpc_pick_unused_port_or_die();
308 std::ostringstream proxyaddr;
309 proxyaddr << "localhost:" << port;
310 ServerBuilder builder;
311 builder.AddListeningPort(proxyaddr.str(), InsecureServerCredentials());
312 builder.RegisterService(proxy_service_.get());
Sree Kuchibhotlac37a8a52016-10-13 15:40:15 -0700313
314 builder.SetSyncServerOption(ServerBuilder::SyncServerOption::NUM_CQS, 4);
315 builder.SetSyncServerOption(
316 ServerBuilder::SyncServerOption::CQ_TIMEOUT_MSEC, 10);
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700317
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700318 proxy_server_ = builder.BuildAndStart();
319
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700320 channel_ = CreateChannel(proxyaddr.str(), InsecureChannelCredentials());
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700321 }
322
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800323 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
yangg1456d152015-01-08 15:39:58 -0800324 }
325
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700326 bool is_server_started_;
yang-g8c2be9f2015-08-19 16:28:09 -0700327 std::shared_ptr<Channel> channel_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800328 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800329 std::unique_ptr<Server> server_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700330 std::unique_ptr<Server> proxy_server_;
331 std::unique_ptr<Proxy> proxy_service_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800332 std::ostringstream server_address_;
Yang Gao3921c562015-04-30 16:07:06 -0700333 const int kMaxMessageSize_;
nnoble0c475f02014-12-05 15:37:39 -0800334 TestServiceImpl service_;
Craig Tiller822d2c72015-07-07 16:08:00 -0700335 TestServiceImpl special_service_;
yangg1456d152015-01-08 15:39:58 -0800336 TestServiceImplDupPkg dup_pkg_service_;
yang-gd59ad7e2016-02-10 12:42:53 -0800337 grpc::string user_agent_prefix_;
Alexander Polcyn7a57b0c2017-10-13 17:01:32 -0700338 int first_picked_port_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800339};
340
yang-gf8174ea2016-02-01 00:09:13 -0800341static void SendRpc(grpc::testing::EchoTestService::Stub* stub, int num_rpcs,
342 bool with_binary_metadata) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800343 EchoRequest request;
344 EchoResponse response;
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700345 request.set_message("Hello hello hello hello");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800346
347 for (int i = 0; i < num_rpcs; ++i) {
348 ClientContext context;
yang-gf8174ea2016-02-01 00:09:13 -0800349 if (with_binary_metadata) {
350 char bytes[8] = {'\0', '\1', '\2', '\3', '\4', '\5', '\6', (char)i};
351 context.AddMetadata("custom-bin", grpc::string(bytes, 8));
352 }
Muxi Yan8fa4d152017-09-26 09:53:55 -0700353 context.set_compression_algorithm(GRPC_COMPRESS_MESSAGE_GZIP);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800354 Status s = stub->Echo(&context, request, &response);
355 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700356 EXPECT_TRUE(s.ok());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800357 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800358}
359
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800360// This class is for testing scenarios where RPCs are cancelled on the server
361// by calling ServerContext::TryCancel()
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800362class End2endServerTryCancelTest : public End2endTest {
363 protected:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800364 // Helper for testing client-streaming RPCs which are cancelled on the server.
365 // Depending on the value of server_try_cancel parameter, this will test one
366 // of the following three scenarios:
367 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading
368 // any messages from the client
369 //
370 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading
371 // messages from the client
372 //
373 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all
374 // the messages from the client
375 //
376 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800377 void TestRequestStreamServerCancel(
378 ServerTryCancelRequestPhase server_try_cancel, int num_msgs_to_send) {
379 ResetStub();
380 EchoRequest request;
381 EchoResponse response;
382 ClientContext context;
383
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800384 // Send server_try_cancel value in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800385 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700386 grpc::to_string(server_try_cancel));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800387
388 auto stream = stub_->RequestStream(&context, &response);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800389
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800390 int num_msgs_sent = 0;
391 while (num_msgs_sent < num_msgs_to_send) {
392 request.set_message("hello");
393 if (!stream->Write(request)) {
394 break;
395 }
396 num_msgs_sent++;
397 }
398 gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800399
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800400 stream->WritesDone();
401 Status s = stream->Finish();
402
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800403 // At this point, we know for sure that RPC was cancelled by the server
404 // since we passed server_try_cancel value in the metadata. Depending on the
405 // value of server_try_cancel, the RPC might have been cancelled by the
406 // server at different stages. The following validates our expectations of
407 // number of messages sent in various cancellation scenarios:
408
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800409 switch (server_try_cancel) {
410 case CANCEL_BEFORE_PROCESSING:
411 case CANCEL_DURING_PROCESSING:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800412 // If the RPC is cancelled by server before / during messages from the
413 // client, it means that the client most likely did not get a chance to
414 // send all the messages it wanted to send. i.e num_msgs_sent <=
415 // num_msgs_to_send
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800416 EXPECT_LE(num_msgs_sent, num_msgs_to_send);
417 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800418
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800419 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800420 // If the RPC was cancelled after all messages were read by the server,
421 // the client did get a chance to send all its messages
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800422 EXPECT_EQ(num_msgs_sent, num_msgs_to_send);
423 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800424
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800425 default:
426 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
427 server_try_cancel);
428 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
429 server_try_cancel <= CANCEL_AFTER_PROCESSING);
430 break;
431 }
432
433 EXPECT_FALSE(s.ok());
434 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
435 }
436
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800437 // Helper for testing server-streaming RPCs which are cancelled on the server.
438 // Depending on the value of server_try_cancel parameter, this will test one
439 // of the following three scenarios:
440 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before writing
441 // any messages to the client
442 //
443 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while writing
444 // messages to the client
445 //
446 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after writing all
447 // the messages to the client
448 //
449 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800450 void TestResponseStreamServerCancel(
451 ServerTryCancelRequestPhase server_try_cancel) {
452 ResetStub();
453 EchoRequest request;
454 EchoResponse response;
455 ClientContext context;
456
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800457 // Send server_try_cancel in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800458 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700459 grpc::to_string(server_try_cancel));
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800460
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800461 request.set_message("hello");
462 auto stream = stub_->ResponseStream(&context, request);
463
464 int num_msgs_read = 0;
ncteisen9bee3082017-06-20 12:30:24 -0700465 while (num_msgs_read < kServerDefaultResponseStreamsToSend) {
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800466 if (!stream->Read(&response)) {
467 break;
468 }
469 EXPECT_EQ(response.message(),
Vijay Paia63271c2016-06-15 12:56:38 -0700470 request.message() + grpc::to_string(num_msgs_read));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800471 num_msgs_read++;
472 }
473 gpr_log(GPR_INFO, "Read %d messages", num_msgs_read);
474
475 Status s = stream->Finish();
476
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800477 // Depending on the value of server_try_cancel, the RPC might have been
478 // cancelled by the server at different stages. The following validates our
479 // expectations of number of messages read in various cancellation
480 // scenarios:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800481 switch (server_try_cancel) {
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800482 case CANCEL_BEFORE_PROCESSING:
483 // Server cancelled before sending any messages. Which means the client
484 // wouldn't have read any
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800485 EXPECT_EQ(num_msgs_read, 0);
486 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800487
488 case CANCEL_DURING_PROCESSING:
489 // Server cancelled while writing messages. Client must have read less
490 // than or equal to the expected number of messages
ncteisen9bee3082017-06-20 12:30:24 -0700491 EXPECT_LE(num_msgs_read, kServerDefaultResponseStreamsToSend);
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800492 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800493
494 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla8d543e82016-02-29 18:22:25 -0800495 // Even though the Server cancelled after writing all messages, the RPC
496 // may be cancelled before the Client got a chance to read all the
497 // messages.
ncteisen9bee3082017-06-20 12:30:24 -0700498 EXPECT_LE(num_msgs_read, kServerDefaultResponseStreamsToSend);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800499 break;
500
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800501 default: {
502 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
503 server_try_cancel);
504 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
505 server_try_cancel <= CANCEL_AFTER_PROCESSING);
506 break;
507 }
508 }
509
510 EXPECT_FALSE(s.ok());
511 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
512 }
513
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800514 // Helper for testing bidirectional-streaming RPCs which are cancelled on the
515 // server. Depending on the value of server_try_cancel parameter, this will
516 // test one of the following three scenarios:
517 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading/
518 // writing any messages from/to the client
519 //
520 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading/
521 // writing messages from/to the client
522 //
523 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading/writing
524 // all the messages from/to the client
525 //
526 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800527 void TestBidiStreamServerCancel(ServerTryCancelRequestPhase server_try_cancel,
528 int num_messages) {
529 ResetStub();
530 EchoRequest request;
531 EchoResponse response;
532 ClientContext context;
533
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800534 // Send server_try_cancel in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800535 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700536 grpc::to_string(server_try_cancel));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800537
538 auto stream = stub_->BidiStream(&context);
539
540 int num_msgs_read = 0;
541 int num_msgs_sent = 0;
542 while (num_msgs_sent < num_messages) {
Vijay Paia63271c2016-06-15 12:56:38 -0700543 request.set_message("hello " + grpc::to_string(num_msgs_sent));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800544 if (!stream->Write(request)) {
545 break;
546 }
547 num_msgs_sent++;
548
549 if (!stream->Read(&response)) {
550 break;
551 }
552 num_msgs_read++;
553
554 EXPECT_EQ(response.message(), request.message());
555 }
556 gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent);
557 gpr_log(GPR_INFO, "Read %d messages", num_msgs_read);
558
559 stream->WritesDone();
560 Status s = stream->Finish();
561
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800562 // Depending on the value of server_try_cancel, the RPC might have been
563 // cancelled by the server at different stages. The following validates our
564 // expectations of number of messages read in various cancellation
565 // scenarios:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800566 switch (server_try_cancel) {
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800567 case CANCEL_BEFORE_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800568 EXPECT_EQ(num_msgs_read, 0);
569 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800570
571 case CANCEL_DURING_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800572 EXPECT_LE(num_msgs_sent, num_messages);
573 EXPECT_LE(num_msgs_read, num_msgs_sent);
574 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800575
576 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800577 EXPECT_EQ(num_msgs_sent, num_messages);
Sree Kuchibhotla8d543e82016-02-29 18:22:25 -0800578
579 // The Server cancelled after reading the last message and after writing
580 // the message to the client. However, the RPC cancellation might have
581 // taken effect before the client actually read the response.
582 EXPECT_LE(num_msgs_read, num_msgs_sent);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800583 break;
584
585 default:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800586 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
587 server_try_cancel);
588 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
589 server_try_cancel <= CANCEL_AFTER_PROCESSING);
590 break;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800591 }
592
593 EXPECT_FALSE(s.ok());
594 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
595 }
596};
597
598TEST_P(End2endServerTryCancelTest, RequestEchoServerCancel) {
599 ResetStub();
600 EchoRequest request;
601 EchoResponse response;
602 ClientContext context;
603
604 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700605 grpc::to_string(CANCEL_BEFORE_PROCESSING));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800606 Status s = stub_->Echo(&context, request, &response);
607 EXPECT_FALSE(s.ok());
608 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
609}
610
611// Server to cancel before doing reading the request
612TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelBeforeReads) {
613 TestRequestStreamServerCancel(CANCEL_BEFORE_PROCESSING, 1);
614}
615
616// Server to cancel while reading a request from the stream in parallel
617TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelDuringRead) {
618 TestRequestStreamServerCancel(CANCEL_DURING_PROCESSING, 10);
619}
620
621// Server to cancel after reading all the requests but before returning to the
622// client
623TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelAfterReads) {
624 TestRequestStreamServerCancel(CANCEL_AFTER_PROCESSING, 4);
625}
626
627// Server to cancel before sending any response messages
628TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelBefore) {
629 TestResponseStreamServerCancel(CANCEL_BEFORE_PROCESSING);
630}
631
632// Server to cancel while writing a response to the stream in parallel
633TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelDuring) {
634 TestResponseStreamServerCancel(CANCEL_DURING_PROCESSING);
635}
636
637// Server to cancel after writing all the respones to the stream but before
638// returning to the client
639TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelAfter) {
640 TestResponseStreamServerCancel(CANCEL_AFTER_PROCESSING);
641}
642
643// Server to cancel before reading/writing any requests/responses on the stream
644TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelBefore) {
645 TestBidiStreamServerCancel(CANCEL_BEFORE_PROCESSING, 2);
646}
647
648// Server to cancel while reading/writing requests/responses on the stream in
649// parallel
650TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelDuring) {
651 TestBidiStreamServerCancel(CANCEL_DURING_PROCESSING, 10);
652}
653
654// Server to cancel after reading/writing all requests/responses on the stream
655// but before returning to the client
656TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelAfter) {
657 TestBidiStreamServerCancel(CANCEL_AFTER_PROCESSING, 5);
658}
659
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800660TEST_P(End2endTest, SimpleRpcWithCustomUserAgentPrefix) {
Vijay Pai3d7d5f42017-05-04 10:02:24 -0700661 // User-Agent is an HTTP header for HTTP transports only
662 if (GetParam().inproc) {
663 return;
664 }
yang-gd59ad7e2016-02-10 12:42:53 -0800665 user_agent_prefix_ = "custom_prefix";
666 ResetStub();
667 EchoRequest request;
668 EchoResponse response;
669 request.set_message("Hello hello hello hello");
670 request.mutable_param()->set_echo_metadata(true);
671
672 ClientContext context;
673 Status s = stub_->Echo(&context, request, &response);
674 EXPECT_EQ(response.message(), request.message());
675 EXPECT_TRUE(s.ok());
676 const auto& trailing_metadata = context.GetServerTrailingMetadata();
677 auto iter = trailing_metadata.find("user-agent");
678 EXPECT_TRUE(iter != trailing_metadata.end());
679 grpc::string expected_prefix = user_agent_prefix_ + " grpc-c++/";
Mark D. Rothe3a21002016-10-24 13:29:05 -0700680 EXPECT_TRUE(iter->second.starts_with(expected_prefix)) << iter->second;
yang-gd59ad7e2016-02-10 12:42:53 -0800681}
682
yang-gf8174ea2016-02-01 00:09:13 -0800683TEST_P(End2endTest, MultipleRpcsWithVariedBinaryMetadataValue) {
684 ResetStub();
Vijay Paib0a6be22016-11-03 12:45:02 -0700685 std::vector<std::thread> threads;
yang-gf8174ea2016-02-01 00:09:13 -0800686 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -0700687 threads.emplace_back(SendRpc, stub_.get(), 10, true);
yang-gf8174ea2016-02-01 00:09:13 -0800688 }
689 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -0700690 threads[i].join();
yang-gf8174ea2016-02-01 00:09:13 -0800691 }
692}
693
694TEST_P(End2endTest, MultipleRpcs) {
695 ResetStub();
Vijay Paib0a6be22016-11-03 12:45:02 -0700696 std::vector<std::thread> threads;
yang-gf8174ea2016-02-01 00:09:13 -0800697 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -0700698 threads.emplace_back(SendRpc, stub_.get(), 10, false);
yang-gf8174ea2016-02-01 00:09:13 -0800699 }
700 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -0700701 threads[i].join();
yang-gf8174ea2016-02-01 00:09:13 -0800702 }
703}
704
Yuchen Zengee3e3312017-08-04 13:54:04 -0700705TEST_P(End2endTest, ReconnectChannel) {
706 if (GetParam().inproc) {
707 return;
708 }
Yuchen Zeng7fa814f2017-10-15 19:00:16 -0700709 int poller_slowdown_factor = 1;
710 // It needs 2 pollset_works to reconnect the channel with polling engine
711 // "poll"
712 char* s = gpr_getenv("GRPC_POLL_STRATEGY");
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800713 if (s != nullptr && 0 == strcmp(s, "poll")) {
Yuchen Zeng7fa814f2017-10-15 19:00:16 -0700714 poller_slowdown_factor = 2;
715 }
716 gpr_free(s);
Yuchen Zengee3e3312017-08-04 13:54:04 -0700717 ResetStub();
718 SendRpc(stub_.get(), 1, false);
719 RestartServer(std::shared_ptr<AuthMetadataProcessor>());
Yuchen Zengcbb92962017-10-18 11:47:10 -0700720 // It needs more than GRPC_CLIENT_CHANNEL_BACKUP_POLL_INTERVAL_MS time to
721 // reconnect the channel.
Yuchen Zeng0bad30a2017-10-05 21:47:39 -0700722 gpr_sleep_until(gpr_time_add(
723 gpr_now(GPR_CLOCK_REALTIME),
Yuchen Zeng7fa814f2017-10-15 19:00:16 -0700724 gpr_time_from_millis(
Yuchen Zengcbb92962017-10-18 11:47:10 -0700725 300 * poller_slowdown_factor * grpc_test_slowdown_factor(),
Yuchen Zeng7fa814f2017-10-15 19:00:16 -0700726 GPR_TIMESPAN)));
Yuchen Zengee3e3312017-08-04 13:54:04 -0700727 SendRpc(stub_.get(), 1, false);
728}
729
yang-g88d5d522015-09-29 12:46:54 -0700730TEST_P(End2endTest, RequestStreamOneRequest) {
731 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800732 EchoRequest request;
733 EchoResponse response;
734 ClientContext context;
735
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800736 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800737 request.set_message("hello");
738 EXPECT_TRUE(stream->Write(request));
739 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800740 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800741 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700742 EXPECT_TRUE(s.ok());
ncteisen6193c632017-12-08 10:49:31 -0800743 EXPECT_TRUE(context.debug_error_string().empty());
nnoble0c475f02014-12-05 15:37:39 -0800744}
745
Yuxuan Li34894e42017-02-08 17:57:24 -0800746TEST_P(End2endTest, RequestStreamOneRequestWithCoalescingApi) {
747 ResetStub();
748 EchoRequest request;
749 EchoResponse response;
750 ClientContext context;
751
Yuxuan Libdc76ab2017-03-03 13:59:30 -0800752 context.set_initial_metadata_corked(true);
Yuxuan Li34894e42017-02-08 17:57:24 -0800753 auto stream = stub_->RequestStream(&context, &response);
754 request.set_message("hello");
755 stream->WriteLast(request, WriteOptions());
756 Status s = stream->Finish();
757 EXPECT_EQ(response.message(), request.message());
758 EXPECT_TRUE(s.ok());
759}
760
yang-g88d5d522015-09-29 12:46:54 -0700761TEST_P(End2endTest, RequestStreamTwoRequests) {
762 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800763 EchoRequest request;
764 EchoResponse response;
765 ClientContext context;
766
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800767 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800768 request.set_message("hello");
769 EXPECT_TRUE(stream->Write(request));
770 EXPECT_TRUE(stream->Write(request));
771 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800772 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800773 EXPECT_EQ(response.message(), "hellohello");
Yang Gaoc1a2c312015-06-16 10:59:46 -0700774 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800775}
776
Craig Tiller9bebf8b2017-09-01 10:20:13 -0700777TEST_P(End2endTest, RequestStreamTwoRequestsWithWriteThrough) {
778 ResetStub();
779 EchoRequest request;
780 EchoResponse response;
781 ClientContext context;
782
783 auto stream = stub_->RequestStream(&context, &response);
784 request.set_message("hello");
785 EXPECT_TRUE(stream->Write(request, WriteOptions().set_write_through()));
786 EXPECT_TRUE(stream->Write(request, WriteOptions().set_write_through()));
787 stream->WritesDone();
788 Status s = stream->Finish();
789 EXPECT_EQ(response.message(), "hellohello");
790 EXPECT_TRUE(s.ok());
791}
792
Yuxuan Li34894e42017-02-08 17:57:24 -0800793TEST_P(End2endTest, RequestStreamTwoRequestsWithCoalescingApi) {
794 ResetStub();
795 EchoRequest request;
796 EchoResponse response;
797 ClientContext context;
798
Yuxuan Libdc76ab2017-03-03 13:59:30 -0800799 context.set_initial_metadata_corked(true);
Yuxuan Li34894e42017-02-08 17:57:24 -0800800 auto stream = stub_->RequestStream(&context, &response);
801 request.set_message("hello");
802 EXPECT_TRUE(stream->Write(request));
803 stream->WriteLast(request, WriteOptions());
804 Status s = stream->Finish();
805 EXPECT_EQ(response.message(), "hellohello");
806 EXPECT_TRUE(s.ok());
807}
808
yang-g88d5d522015-09-29 12:46:54 -0700809TEST_P(End2endTest, ResponseStream) {
810 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800811 EchoRequest request;
812 EchoResponse response;
813 ClientContext context;
814 request.set_message("hello");
815
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800816 auto stream = stub_->ResponseStream(&context, request);
ncteisen9bee3082017-06-20 12:30:24 -0700817 for (int i = 0; i < kServerDefaultResponseStreamsToSend; ++i) {
818 EXPECT_TRUE(stream->Read(&response));
819 EXPECT_EQ(response.message(), request.message() + grpc::to_string(i));
820 }
nnoble0c475f02014-12-05 15:37:39 -0800821 EXPECT_FALSE(stream->Read(&response));
822
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800823 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700824 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800825}
826
Yuxuan Li34894e42017-02-08 17:57:24 -0800827TEST_P(End2endTest, ResponseStreamWithCoalescingApi) {
828 ResetStub();
829 EchoRequest request;
830 EchoResponse response;
831 ClientContext context;
832 request.set_message("hello");
833 context.AddMetadata(kServerUseCoalescingApi, "1");
834
835 auto stream = stub_->ResponseStream(&context, request);
ncteisen9bee3082017-06-20 12:30:24 -0700836 for (int i = 0; i < kServerDefaultResponseStreamsToSend; ++i) {
837 EXPECT_TRUE(stream->Read(&response));
838 EXPECT_EQ(response.message(), request.message() + grpc::to_string(i));
839 }
Yuxuan Li34894e42017-02-08 17:57:24 -0800840 EXPECT_FALSE(stream->Read(&response));
841
842 Status s = stream->Finish();
843 EXPECT_TRUE(s.ok());
844}
845
ncteisenbe7b82b2017-06-20 12:31:04 -0700846// This was added to prevent regression from issue:
847// https://github.com/grpc/grpc/issues/11546
848TEST_P(End2endTest, ResponseStreamWithEverythingCoalesced) {
849 ResetStub();
850 EchoRequest request;
851 EchoResponse response;
852 ClientContext context;
853 request.set_message("hello");
854 context.AddMetadata(kServerUseCoalescingApi, "1");
855 // We will only send one message, forcing everything (init metadata, message,
856 // trailing) to be coalesced together.
857 context.AddMetadata(kServerResponseStreamsToSend, "1");
858
859 auto stream = stub_->ResponseStream(&context, request);
860 EXPECT_TRUE(stream->Read(&response));
861 EXPECT_EQ(response.message(), request.message() + "0");
862
863 EXPECT_FALSE(stream->Read(&response));
864
865 Status s = stream->Finish();
866 EXPECT_TRUE(s.ok());
867}
868
yang-g88d5d522015-09-29 12:46:54 -0700869TEST_P(End2endTest, BidiStream) {
870 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800871 EchoRequest request;
872 EchoResponse response;
873 ClientContext context;
874 grpc::string msg("hello");
875
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800876 auto stream = stub_->BidiStream(&context);
nnoble0c475f02014-12-05 15:37:39 -0800877
ncteisen9bee3082017-06-20 12:30:24 -0700878 for (int i = 0; i < kServerDefaultResponseStreamsToSend; ++i) {
879 request.set_message(msg + grpc::to_string(i));
880 EXPECT_TRUE(stream->Write(request));
881 EXPECT_TRUE(stream->Read(&response));
882 EXPECT_EQ(response.message(), request.message());
883 }
nnoble0c475f02014-12-05 15:37:39 -0800884
885 stream->WritesDone();
886 EXPECT_FALSE(stream->Read(&response));
Craig Tillerca9a6372015-12-15 18:16:28 -0800887 EXPECT_FALSE(stream->Read(&response));
nnoble0c475f02014-12-05 15:37:39 -0800888
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800889 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700890 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800891}
892
Yuxuan Li34894e42017-02-08 17:57:24 -0800893TEST_P(End2endTest, BidiStreamWithCoalescingApi) {
894 ResetStub();
895 EchoRequest request;
896 EchoResponse response;
897 ClientContext context;
898 context.AddMetadata(kServerFinishAfterNReads, "3");
Yuxuan Libdc76ab2017-03-03 13:59:30 -0800899 context.set_initial_metadata_corked(true);
Yuxuan Li34894e42017-02-08 17:57:24 -0800900 grpc::string msg("hello");
901
902 auto stream = stub_->BidiStream(&context);
903
904 request.set_message(msg + "0");
905 EXPECT_TRUE(stream->Write(request));
906 EXPECT_TRUE(stream->Read(&response));
907 EXPECT_EQ(response.message(), request.message());
908
909 request.set_message(msg + "1");
910 EXPECT_TRUE(stream->Write(request));
911 EXPECT_TRUE(stream->Read(&response));
912 EXPECT_EQ(response.message(), request.message());
913
914 request.set_message(msg + "2");
915 stream->WriteLast(request, WriteOptions());
916 EXPECT_TRUE(stream->Read(&response));
917 EXPECT_EQ(response.message(), request.message());
918
919 EXPECT_FALSE(stream->Read(&response));
920 EXPECT_FALSE(stream->Read(&response));
921
922 Status s = stream->Finish();
923 EXPECT_TRUE(s.ok());
924}
925
ncteisen43f2b552017-06-26 17:00:10 -0700926// This was added to prevent regression from issue:
927// https://github.com/grpc/grpc/issues/11546
928TEST_P(End2endTest, BidiStreamWithEverythingCoalesced) {
929 ResetStub();
930 EchoRequest request;
931 EchoResponse response;
932 ClientContext context;
933 context.AddMetadata(kServerFinishAfterNReads, "1");
934 context.set_initial_metadata_corked(true);
935 grpc::string msg("hello");
936
937 auto stream = stub_->BidiStream(&context);
938
939 request.set_message(msg + "0");
940 stream->WriteLast(request, WriteOptions());
941 EXPECT_TRUE(stream->Read(&response));
942 EXPECT_EQ(response.message(), request.message());
943
944 EXPECT_FALSE(stream->Read(&response));
945 EXPECT_FALSE(stream->Read(&response));
946
947 Status s = stream->Finish();
948 EXPECT_TRUE(s.ok());
949}
950
yangg1456d152015-01-08 15:39:58 -0800951// Talk to the two services with the same name but different package names.
952// The two stubs are created on the same channel.
yang-g88d5d522015-09-29 12:46:54 -0700953TEST_P(End2endTest, DiffPackageServices) {
954 ResetStub();
yangg1456d152015-01-08 15:39:58 -0800955 EchoRequest request;
956 EchoResponse response;
957 request.set_message("Hello");
958
yangg1456d152015-01-08 15:39:58 -0800959 ClientContext context;
yang-g8b25f2a2015-07-21 23:54:36 -0700960 Status s = stub_->Echo(&context, request, &response);
yangg1456d152015-01-08 15:39:58 -0800961 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700962 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800963
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800964 std::unique_ptr<grpc::testing::duplicate::EchoTestService::Stub> dup_pkg_stub(
965 grpc::testing::duplicate::EchoTestService::NewStub(channel_));
yangg1456d152015-01-08 15:39:58 -0800966 ClientContext context2;
967 s = dup_pkg_stub->Echo(&context2, request, &response);
968 EXPECT_EQ("no package", response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700969 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800970}
971
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700972void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) {
Craig Tiller20b5fe92015-07-06 10:43:50 -0700973 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700974 gpr_time_from_micros(delay_us, GPR_TIMESPAN)));
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700975 while (!service->signal_client()) {
976 }
977 context->TryCancel();
978}
979
yang-ga89bf502015-11-17 14:19:17 -0800980TEST_P(End2endTest, CancelRpcBeforeStart) {
981 ResetStub();
982 EchoRequest request;
983 EchoResponse response;
984 ClientContext context;
985 request.set_message("hello");
986 context.TryCancel();
987 Status s = stub_->Echo(&context, request, &response);
988 EXPECT_EQ("", response.message());
989 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
990}
991
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700992// Client cancels request stream after sending two messages
yang-g88d5d522015-09-29 12:46:54 -0700993TEST_P(End2endTest, ClientCancelsRequestStream) {
994 ResetStub();
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700995 EchoRequest request;
996 EchoResponse response;
997 ClientContext context;
998 request.set_message("hello");
999
1000 auto stream = stub_->RequestStream(&context, &response);
1001 EXPECT_TRUE(stream->Write(request));
1002 EXPECT_TRUE(stream->Write(request));
Yang Gaoc71a9d22015-05-04 00:22:12 -07001003
Abhishek Kumard774c5c2015-04-23 14:59:49 -07001004 context.TryCancel();
1005
1006 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -07001007 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumard774c5c2015-04-23 14:59:49 -07001008
Yang Gaoc71a9d22015-05-04 00:22:12 -07001009 EXPECT_EQ(response.message(), "");
Abhishek Kumard774c5c2015-04-23 14:59:49 -07001010}
1011
Abhishek Kumare41d0402015-04-17 14:12:33 -07001012// Client cancels server stream after sending some messages
yang-g88d5d522015-09-29 12:46:54 -07001013TEST_P(End2endTest, ClientCancelsResponseStream) {
1014 ResetStub();
Abhishek Kumare41d0402015-04-17 14:12:33 -07001015 EchoRequest request;
1016 EchoResponse response;
1017 ClientContext context;
1018 request.set_message("hello");
1019
1020 auto stream = stub_->ResponseStream(&context, request);
1021
1022 EXPECT_TRUE(stream->Read(&response));
1023 EXPECT_EQ(response.message(), request.message() + "0");
1024 EXPECT_TRUE(stream->Read(&response));
1025 EXPECT_EQ(response.message(), request.message() + "1");
1026
1027 context.TryCancel();
1028
1029 // The cancellation races with responses, so there might be zero or
1030 // one responses pending, read till failure
1031
1032 if (stream->Read(&response)) {
1033 EXPECT_EQ(response.message(), request.message() + "2");
1034 // Since we have cancelled, we expect the next attempt to read to fail
1035 EXPECT_FALSE(stream->Read(&response));
1036 }
1037
1038 Status s = stream->Finish();
Abhishek Kumar18298a72015-04-17 15:00:25 -07001039 // The final status could be either of CANCELLED or OK depending on
1040 // who won the race.
Yang Gaoc1a2c312015-06-16 10:59:46 -07001041 EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumare41d0402015-04-17 14:12:33 -07001042}
1043
Abhishek Kumar82a83312015-04-17 13:30:51 -07001044// Client cancels bidi stream after sending some messages
yang-g88d5d522015-09-29 12:46:54 -07001045TEST_P(End2endTest, ClientCancelsBidi) {
1046 ResetStub();
Abhishek Kumar82a83312015-04-17 13:30:51 -07001047 EchoRequest request;
1048 EchoResponse response;
1049 ClientContext context;
1050 grpc::string msg("hello");
1051
1052 auto stream = stub_->BidiStream(&context);
1053
1054 request.set_message(msg + "0");
1055 EXPECT_TRUE(stream->Write(request));
1056 EXPECT_TRUE(stream->Read(&response));
1057 EXPECT_EQ(response.message(), request.message());
1058
1059 request.set_message(msg + "1");
1060 EXPECT_TRUE(stream->Write(request));
1061
1062 context.TryCancel();
1063
1064 // The cancellation races with responses, so there might be zero or
1065 // one responses pending, read till failure
1066
1067 if (stream->Read(&response)) {
1068 EXPECT_EQ(response.message(), request.message());
1069 // Since we have cancelled, we expect the next attempt to read to fail
1070 EXPECT_FALSE(stream->Read(&response));
1071 }
1072
1073 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -07001074 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumar82a83312015-04-17 13:30:51 -07001075}
1076
yang-g88d5d522015-09-29 12:46:54 -07001077TEST_P(End2endTest, RpcMaxMessageSize) {
1078 ResetStub();
Yang Gao3921c562015-04-30 16:07:06 -07001079 EchoRequest request;
1080 EchoResponse response;
Yang Gaoc71a9d22015-05-04 00:22:12 -07001081 request.set_message(string(kMaxMessageSize_ * 2, 'a'));
yang-g23f777d2017-02-22 23:32:26 -08001082 request.mutable_param()->set_server_die(true);
Yang Gao3921c562015-04-30 16:07:06 -07001083
1084 ClientContext context;
1085 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -07001086 EXPECT_FALSE(s.ok());
Yang Gao3921c562015-04-30 16:07:06 -07001087}
Abhishek Kumar82a83312015-04-17 13:30:51 -07001088
yang-g88d5d522015-09-29 12:46:54 -07001089void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream,
1090 gpr_event* ev) {
1091 EchoResponse resp;
1092 gpr_event_set(ev, (void*)1);
1093 while (stream->Read(&resp)) {
1094 gpr_log(GPR_INFO, "Read message");
1095 }
1096}
yang-g88d5d522015-09-29 12:46:54 -07001097
1098// Run a Read and a WritesDone simultaneously.
1099TEST_P(End2endTest, SimultaneousReadWritesDone) {
1100 ResetStub();
1101 ClientContext context;
1102 gpr_event ev;
1103 gpr_event_init(&ev);
1104 auto stream = stub_->BidiStream(&context);
1105 std::thread reader_thread(ReaderThreadFunc, stream.get(), &ev);
1106 gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME));
1107 stream->WritesDone();
Vijay Paibdfec2c2016-02-25 11:51:21 -08001108 reader_thread.join();
yang-g88d5d522015-09-29 12:46:54 -07001109 Status s = stream->Finish();
1110 EXPECT_TRUE(s.ok());
yang-g88d5d522015-09-29 12:46:54 -07001111}
1112
1113TEST_P(End2endTest, ChannelState) {
Vijay Pai3d7d5f42017-05-04 10:02:24 -07001114 if (GetParam().inproc) {
1115 return;
1116 }
1117
yang-g88d5d522015-09-29 12:46:54 -07001118 ResetStub();
1119 // Start IDLE
1120 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(false));
1121
1122 // Did not ask to connect, no state change.
1123 CompletionQueue cq;
1124 std::chrono::system_clock::time_point deadline =
1125 std::chrono::system_clock::now() + std::chrono::milliseconds(10);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -08001126 channel_->NotifyOnStateChange(GRPC_CHANNEL_IDLE, deadline, &cq, nullptr);
yang-g88d5d522015-09-29 12:46:54 -07001127 void* tag;
1128 bool ok = true;
1129 cq.Next(&tag, &ok);
1130 EXPECT_FALSE(ok);
1131
1132 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true));
1133 EXPECT_TRUE(channel_->WaitForStateChange(GRPC_CHANNEL_IDLE,
1134 gpr_inf_future(GPR_CLOCK_REALTIME)));
yang-g0d557502015-10-01 11:30:12 -07001135 auto state = channel_->GetState(false);
1136 EXPECT_TRUE(state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_READY);
yang-g88d5d522015-09-29 12:46:54 -07001137}
1138
1139// Takes 10s.
1140TEST_P(End2endTest, ChannelStateTimeout) {
Vijay Pai3d7d5f42017-05-04 10:02:24 -07001141 if ((GetParam().credentials_type != kInsecureCredentialsType) ||
1142 GetParam().inproc) {
yang-g88d5d522015-09-29 12:46:54 -07001143 return;
1144 }
1145 int port = grpc_pick_unused_port_or_die();
1146 std::ostringstream server_address;
1147 server_address << "127.0.0.1:" << port;
1148 // Channel to non-existing server
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001149 auto channel =
1150 CreateChannel(server_address.str(), InsecureChannelCredentials());
yang-g88d5d522015-09-29 12:46:54 -07001151 // Start IDLE
1152 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel->GetState(true));
1153
1154 auto state = GRPC_CHANNEL_IDLE;
1155 for (int i = 0; i < 10; i++) {
1156 channel->WaitForStateChange(
1157 state, std::chrono::system_clock::now() + std::chrono::seconds(1));
1158 state = channel->GetState(false);
1159 }
1160}
1161
1162// Talking to a non-existing service.
1163TEST_P(End2endTest, NonExistingService) {
1164 ResetChannel();
murgatroid997c5befd2016-09-01 17:09:47 -07001165 std::unique_ptr<grpc::testing::UnimplementedEchoService::Stub> stub;
1166 stub = grpc::testing::UnimplementedEchoService::NewStub(channel_);
yang-g88d5d522015-09-29 12:46:54 -07001167
1168 EchoRequest request;
1169 EchoResponse response;
1170 request.set_message("Hello");
1171
1172 ClientContext context;
1173 Status s = stub->Unimplemented(&context, request, &response);
1174 EXPECT_EQ(StatusCode::UNIMPLEMENTED, s.error_code());
1175 EXPECT_EQ("", s.error_message());
1176}
1177
yang-g4c070082016-05-05 23:27:13 -07001178// Ask the server to send back a serialized proto in trailer.
1179// This is an example of setting error details.
1180TEST_P(End2endTest, BinaryTrailerTest) {
1181 ResetStub();
1182 EchoRequest request;
1183 EchoResponse response;
1184 ClientContext context;
1185
1186 request.mutable_param()->set_echo_metadata(true);
1187 DebugInfo* info = request.mutable_param()->mutable_debug_info();
1188 info->add_stack_entries("stack_entry_1");
1189 info->add_stack_entries("stack_entry_2");
1190 info->add_stack_entries("stack_entry_3");
1191 info->set_detail("detailed debug info");
1192 grpc::string expected_string = info->SerializeAsString();
1193 request.set_message("Hello");
1194
1195 Status s = stub_->Echo(&context, request, &response);
1196 EXPECT_FALSE(s.ok());
1197 auto trailers = context.GetServerTrailingMetadata();
yang-g39e71c32016-05-10 10:21:41 -07001198 EXPECT_EQ(1u, trailers.count(kDebugInfoTrailerKey));
yang-g4c070082016-05-05 23:27:13 -07001199 auto iter = trailers.find(kDebugInfoTrailerKey);
1200 EXPECT_EQ(expected_string, iter->second);
yang-g080528a2016-05-06 13:12:00 -07001201 // Parse the returned trailer into a DebugInfo proto.
1202 DebugInfo returned_info;
1203 EXPECT_TRUE(returned_info.ParseFromString(ToString(iter->second)));
yang-g4c070082016-05-05 23:27:13 -07001204}
1205
yang-gf4c8add2017-04-07 14:17:41 -07001206TEST_P(End2endTest, ExpectErrorTest) {
1207 ResetStub();
1208
1209 std::vector<ErrorStatus> expected_status;
1210 expected_status.emplace_back();
1211 expected_status.back().set_code(13); // INTERNAL
1212 expected_status.back().set_error_message("text error message");
1213 expected_status.back().set_binary_error_details("text error details");
1214 expected_status.emplace_back();
1215 expected_status.back().set_code(13); // INTERNAL
1216 expected_status.back().set_error_message("text error message");
1217 expected_status.back().set_binary_error_details(
1218 "\x0\x1\x2\x3\x4\x5\x6\x8\x9\xA\xB");
1219
1220 for (auto iter = expected_status.begin(); iter != expected_status.end();
1221 ++iter) {
1222 EchoRequest request;
1223 EchoResponse response;
1224 ClientContext context;
1225 request.set_message("Hello");
1226 auto* error = request.mutable_param()->mutable_expected_error();
1227 error->set_code(iter->code());
1228 error->set_error_message(iter->error_message());
1229 error->set_binary_error_details(iter->binary_error_details());
1230
1231 Status s = stub_->Echo(&context, request, &response);
1232 EXPECT_FALSE(s.ok());
1233 EXPECT_EQ(iter->code(), s.error_code());
1234 EXPECT_EQ(iter->error_message(), s.error_message());
1235 EXPECT_EQ(iter->binary_error_details(), s.error_details());
ncteisen18bc2782017-12-04 13:25:14 -08001236 EXPECT_TRUE(context.debug_error_string().find("created") !=
1237 std::string::npos);
1238 EXPECT_TRUE(context.debug_error_string().find("file") != std::string::npos);
1239 EXPECT_TRUE(context.debug_error_string().find("line") != std::string::npos);
1240 EXPECT_TRUE(context.debug_error_string().find("status") !=
1241 std::string::npos);
1242 EXPECT_TRUE(context.debug_error_string().find("13") != std::string::npos);
yang-gf4c8add2017-04-07 14:17:41 -07001243 }
1244}
1245
yang-g88d5d522015-09-29 12:46:54 -07001246//////////////////////////////////////////////////////////////////////////
1247// Test with and without a proxy.
1248class ProxyEnd2endTest : public End2endTest {
1249 protected:
1250};
1251
1252TEST_P(ProxyEnd2endTest, SimpleRpc) {
1253 ResetStub();
yang-gf8174ea2016-02-01 00:09:13 -08001254 SendRpc(stub_.get(), 1, false);
yang-g88d5d522015-09-29 12:46:54 -07001255}
1256
yang-g000aa452016-06-07 13:08:39 -07001257TEST_P(ProxyEnd2endTest, SimpleRpcWithEmptyMessages) {
1258 ResetStub();
1259 EchoRequest request;
1260 EchoResponse response;
1261
1262 ClientContext context;
1263 Status s = stub_->Echo(&context, request, &response);
1264 EXPECT_TRUE(s.ok());
1265}
1266
yang-g88d5d522015-09-29 12:46:54 -07001267TEST_P(ProxyEnd2endTest, MultipleRpcs) {
1268 ResetStub();
Vijay Paib0a6be22016-11-03 12:45:02 -07001269 std::vector<std::thread> threads;
yang-g88d5d522015-09-29 12:46:54 -07001270 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -07001271 threads.emplace_back(SendRpc, stub_.get(), 10, false);
yang-g88d5d522015-09-29 12:46:54 -07001272 }
1273 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -07001274 threads[i].join();
yang-g88d5d522015-09-29 12:46:54 -07001275 }
1276}
1277
1278// Set a 10us deadline and make sure proper error is returned.
1279TEST_P(ProxyEnd2endTest, RpcDeadlineExpires) {
1280 ResetStub();
1281 EchoRequest request;
1282 EchoResponse response;
1283 request.set_message("Hello");
Craig Tillerb0f275e2016-01-27 10:45:50 -08001284 request.mutable_param()->set_skip_cancelled_check(true);
Sree Kuchibhotlacc6601e2017-12-07 11:23:40 -08001285 // Let server sleep for 40 ms first to guarantee expiry.
1286 // 40 ms might seem a bit extreme but the timer manager would have been just
1287 // initialized (when ResetStub() was called) and there are some warmup costs
1288 // i.e the timer thread many not have even started. There might also be other
1289 // delays in the timer manager thread (in acquiring locks, timer data
1290 // structure manipulations, starting backup timer threads) that add to the
1291 // delays. 40ms is still not enough in some cases but this significantly
1292 // reduces the test flakes
1293 request.mutable_param()->set_server_sleep_us(40 * 1000);
yang-g88d5d522015-09-29 12:46:54 -07001294
1295 ClientContext context;
1296 std::chrono::system_clock::time_point deadline =
Sree Kuchibhotlacc6601e2017-12-07 11:23:40 -08001297 std::chrono::system_clock::now() + std::chrono::milliseconds(1);
yang-g88d5d522015-09-29 12:46:54 -07001298 context.set_deadline(deadline);
1299 Status s = stub_->Echo(&context, request, &response);
1300 EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code());
1301}
1302
1303// Set a long but finite deadline.
1304TEST_P(ProxyEnd2endTest, RpcLongDeadline) {
1305 ResetStub();
1306 EchoRequest request;
1307 EchoResponse response;
1308 request.set_message("Hello");
1309
1310 ClientContext context;
1311 std::chrono::system_clock::time_point deadline =
1312 std::chrono::system_clock::now() + std::chrono::hours(1);
1313 context.set_deadline(deadline);
1314 Status s = stub_->Echo(&context, request, &response);
1315 EXPECT_EQ(response.message(), request.message());
1316 EXPECT_TRUE(s.ok());
1317}
1318
1319// Ask server to echo back the deadline it sees.
1320TEST_P(ProxyEnd2endTest, EchoDeadline) {
1321 ResetStub();
1322 EchoRequest request;
1323 EchoResponse response;
1324 request.set_message("Hello");
1325 request.mutable_param()->set_echo_deadline(true);
1326
1327 ClientContext context;
1328 std::chrono::system_clock::time_point deadline =
1329 std::chrono::system_clock::now() + std::chrono::seconds(100);
1330 context.set_deadline(deadline);
1331 Status s = stub_->Echo(&context, request, &response);
1332 EXPECT_EQ(response.message(), request.message());
1333 EXPECT_TRUE(s.ok());
1334 gpr_timespec sent_deadline;
1335 Timepoint2Timespec(deadline, &sent_deadline);
Jan Tattermuscha3088a62018-01-30 23:38:26 +01001336 // We want to allow some reasonable error given:
1337 // - request_deadline() only has 1sec resolution so the best we can do is +-1
1338 // - if sent_deadline.tv_nsec is very close to the next second's boundary we
1339 // can end up being off by 2 in one direction.
1340 EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 2);
yang-g88d5d522015-09-29 12:46:54 -07001341 EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1);
1342}
1343
1344// Ask server to echo back the deadline it sees. The rpc has no deadline.
1345TEST_P(ProxyEnd2endTest, EchoDeadlineForNoDeadlineRpc) {
1346 ResetStub();
1347 EchoRequest request;
1348 EchoResponse response;
1349 request.set_message("Hello");
1350 request.mutable_param()->set_echo_deadline(true);
1351
1352 ClientContext context;
1353 Status s = stub_->Echo(&context, request, &response);
1354 EXPECT_EQ(response.message(), request.message());
1355 EXPECT_TRUE(s.ok());
1356 EXPECT_EQ(response.param().request_deadline(),
1357 gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec);
1358}
1359
1360TEST_P(ProxyEnd2endTest, UnimplementedRpc) {
1361 ResetStub();
1362 EchoRequest request;
1363 EchoResponse response;
1364 request.set_message("Hello");
1365
1366 ClientContext context;
1367 Status s = stub_->Unimplemented(&context, request, &response);
1368 EXPECT_FALSE(s.ok());
1369 EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED);
1370 EXPECT_EQ(s.error_message(), "");
1371 EXPECT_EQ(response.message(), "");
1372}
1373
1374// Client cancels rpc after 10ms
1375TEST_P(ProxyEnd2endTest, ClientCancelsRpc) {
1376 ResetStub();
1377 EchoRequest request;
1378 EchoResponse response;
1379 request.set_message("Hello");
1380 const int kCancelDelayUs = 10 * 1000;
1381 request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs);
1382
1383 ClientContext context;
1384 std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_);
1385 Status s = stub_->Echo(&context, request, &response);
1386 cancel_thread.join();
1387 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
1388 EXPECT_EQ(s.error_message(), "Cancelled");
1389}
1390
1391// Server cancels rpc after 1ms
1392TEST_P(ProxyEnd2endTest, ServerCancelsRpc) {
1393 ResetStub();
1394 EchoRequest request;
1395 EchoResponse response;
1396 request.set_message("Hello");
1397 request.mutable_param()->set_server_cancel_after_us(1000);
1398
1399 ClientContext context;
1400 Status s = stub_->Echo(&context, request, &response);
1401 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
1402 EXPECT_TRUE(s.error_message().empty());
1403}
1404
1405// Make the response larger than the flow control window.
1406TEST_P(ProxyEnd2endTest, HugeResponse) {
1407 ResetStub();
1408 EchoRequest request;
1409 EchoResponse response;
1410 request.set_message("huge response");
1411 const size_t kResponseSize = 1024 * (1024 + 10);
1412 request.mutable_param()->set_response_message_length(kResponseSize);
1413
1414 ClientContext context;
Craig Tiller6c8619b2016-07-07 10:41:48 -07001415 std::chrono::system_clock::time_point deadline =
1416 std::chrono::system_clock::now() + std::chrono::seconds(20);
1417 context.set_deadline(deadline);
yang-g88d5d522015-09-29 12:46:54 -07001418 Status s = stub_->Echo(&context, request, &response);
1419 EXPECT_EQ(kResponseSize, response.message().size());
1420 EXPECT_TRUE(s.ok());
1421}
1422
1423TEST_P(ProxyEnd2endTest, Peer) {
Vijay Paia6294052017-10-10 20:47:54 -07001424 // Peer is not meaningful for inproc
1425 if (GetParam().inproc) {
1426 return;
1427 }
yang-g88d5d522015-09-29 12:46:54 -07001428 ResetStub();
1429 EchoRequest request;
1430 EchoResponse response;
1431 request.set_message("hello");
1432 request.mutable_param()->set_echo_peer(true);
1433
1434 ClientContext context;
1435 Status s = stub_->Echo(&context, request, &response);
1436 EXPECT_EQ(response.message(), request.message());
1437 EXPECT_TRUE(s.ok());
1438 EXPECT_TRUE(CheckIsLocalhost(response.param().peer()));
1439 EXPECT_TRUE(CheckIsLocalhost(context.peer()));
1440}
1441
1442//////////////////////////////////////////////////////////////////////////
1443class SecureEnd2endTest : public End2endTest {
1444 protected:
1445 SecureEnd2endTest() {
1446 GPR_ASSERT(!GetParam().use_proxy);
yang-g17197dd2016-02-19 00:04:22 -08001447 GPR_ASSERT(GetParam().credentials_type != kInsecureCredentialsType);
yang-g88d5d522015-09-29 12:46:54 -07001448 }
1449};
1450
1451TEST_P(SecureEnd2endTest, SimpleRpcWithHost) {
1452 ResetStub();
1453
1454 EchoRequest request;
1455 EchoResponse response;
1456 request.set_message("Hello");
1457
1458 ClientContext context;
1459 context.set_authority("foo.test.youtube.com");
1460 Status s = stub_->Echo(&context, request, &response);
1461 EXPECT_EQ(response.message(), request.message());
1462 EXPECT_TRUE(response.has_param());
1463 EXPECT_EQ("special", response.param().host());
1464 EXPECT_TRUE(s.ok());
1465}
1466
yang-ge21908f2015-08-25 13:47:51 -07001467bool MetadataContains(
1468 const std::multimap<grpc::string_ref, grpc::string_ref>& metadata,
1469 const grpc::string& key, const grpc::string& value) {
Yang Gao26a49122015-05-15 17:02:56 -07001470 int count = 0;
1471
yang-ge21908f2015-08-25 13:47:51 -07001472 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator iter =
Yang Gao26a49122015-05-15 17:02:56 -07001473 metadata.begin();
1474 iter != metadata.end(); ++iter) {
yang-ge21908f2015-08-25 13:47:51 -07001475 if (ToString(iter->first) == key && ToString(iter->second) == value) {
Yang Gao26a49122015-05-15 17:02:56 -07001476 count++;
1477 }
1478 }
1479 return count == 1;
1480}
1481
yang-g88d5d522015-09-29 12:46:54 -07001482TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorSuccess) {
1483 auto* processor = new TestAuthMetadataProcessor(true);
1484 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1485 ResetStub();
1486 EchoRequest request;
1487 EchoResponse response;
1488 ClientContext context;
1489 context.set_credentials(processor->GetCompatibleClientCreds());
1490 request.set_message("Hello");
1491 request.mutable_param()->set_echo_metadata(true);
1492 request.mutable_param()->set_expected_client_identity(
1493 TestAuthMetadataProcessor::kGoodGuy);
Dan Bornf2f7d572016-03-03 17:26:12 -08001494 request.mutable_param()->set_expected_transport_security_type(
1495 GetParam().credentials_type);
yang-g88d5d522015-09-29 12:46:54 -07001496
1497 Status s = stub_->Echo(&context, request, &response);
1498 EXPECT_EQ(request.message(), response.message());
1499 EXPECT_TRUE(s.ok());
1500
1501 // Metadata should have been consumed by the processor.
1502 EXPECT_FALSE(MetadataContains(
1503 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1504 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1505}
1506
1507TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorFailure) {
1508 auto* processor = new TestAuthMetadataProcessor(true);
1509 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1510 ResetStub();
1511 EchoRequest request;
1512 EchoResponse response;
1513 ClientContext context;
1514 context.set_credentials(processor->GetIncompatibleClientCreds());
1515 request.set_message("Hello");
1516
1517 Status s = stub_->Echo(&context, request, &response);
1518 EXPECT_FALSE(s.ok());
1519 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1520}
Craig Tiller7c70b6c2017-01-23 07:48:42 -08001521
yang-g88d5d522015-09-29 12:46:54 -07001522TEST_P(SecureEnd2endTest, SetPerCallCredentials) {
1523 ResetStub();
Yang Gaoa8938922015-05-14 11:51:07 -07001524 EchoRequest request;
1525 EchoResponse response;
1526 ClientContext context;
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001527 std::shared_ptr<CallCredentials> creds =
Julien Boeuf510a9202015-08-25 21:51:07 -07001528 GoogleIAMCredentials("fake_token", "fake_selector");
Yang Gaoa8938922015-05-14 11:51:07 -07001529 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -07001530 request.set_message("Hello");
1531 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -07001532
1533 Status s = stub_->Echo(&context, request, &response);
Yang Gaoa8938922015-05-14 11:51:07 -07001534 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -07001535 EXPECT_TRUE(s.ok());
Yang Gao26a49122015-05-15 17:02:56 -07001536 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1537 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1538 "fake_token"));
1539 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1540 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1541 "fake_selector"));
Yang Gaoa8938922015-05-14 11:51:07 -07001542}
1543
yang-g88d5d522015-09-29 12:46:54 -07001544TEST_P(SecureEnd2endTest, OverridePerCallCredentials) {
1545 ResetStub();
Yang Gaoa8938922015-05-14 11:51:07 -07001546 EchoRequest request;
1547 EchoResponse response;
1548 ClientContext context;
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001549 std::shared_ptr<CallCredentials> creds1 =
Julien Boeuf510a9202015-08-25 21:51:07 -07001550 GoogleIAMCredentials("fake_token1", "fake_selector1");
Yang Gaoa8938922015-05-14 11:51:07 -07001551 context.set_credentials(creds1);
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001552 std::shared_ptr<CallCredentials> creds2 =
Julien Boeuf510a9202015-08-25 21:51:07 -07001553 GoogleIAMCredentials("fake_token2", "fake_selector2");
Yang Gaoa8938922015-05-14 11:51:07 -07001554 context.set_credentials(creds2);
Yang Gao26a49122015-05-15 17:02:56 -07001555 request.set_message("Hello");
1556 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -07001557
1558 Status s = stub_->Echo(&context, request, &response);
Yang Gao26a49122015-05-15 17:02:56 -07001559 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1560 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1561 "fake_token2"));
1562 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1563 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1564 "fake_selector2"));
Yang Gaob57f72d2015-05-17 21:54:54 -07001565 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
1566 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1567 "fake_token1"));
1568 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
1569 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1570 "fake_selector1"));
Yang Gaoa8938922015-05-14 11:51:07 -07001571 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -07001572 EXPECT_TRUE(s.ok());
Yang Gaoa8938922015-05-14 11:51:07 -07001573}
1574
yang-gc580af32016-09-15 15:28:38 -07001575TEST_P(SecureEnd2endTest, AuthMetadataPluginKeyFailure) {
1576 ResetStub();
1577 EchoRequest request;
1578 EchoResponse response;
1579 ClientContext context;
1580 context.set_credentials(
1581 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1582 new TestMetadataCredentialsPlugin(
1583 TestMetadataCredentialsPlugin::kBadMetadataKey,
1584 "Does not matter, will fail the key is invalid.", false, true))));
1585 request.set_message("Hello");
1586
1587 Status s = stub_->Echo(&context, request, &response);
1588 EXPECT_FALSE(s.ok());
Jan Tattermuschef0e64c2017-11-13 07:50:37 +01001589 EXPECT_EQ(s.error_code(), StatusCode::UNAVAILABLE);
yang-gc580af32016-09-15 15:28:38 -07001590}
1591
yang-g4b4571a2016-09-15 23:01:09 -07001592TEST_P(SecureEnd2endTest, AuthMetadataPluginValueFailure) {
1593 ResetStub();
1594 EchoRequest request;
1595 EchoResponse response;
1596 ClientContext context;
1597 context.set_credentials(
1598 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1599 new TestMetadataCredentialsPlugin(
1600 TestMetadataCredentialsPlugin::kGoodMetadataKey,
yang-gd5fba282016-09-16 10:29:16 -07001601 "With illegal \n value.", false, true))));
yang-g4b4571a2016-09-15 23:01:09 -07001602 request.set_message("Hello");
1603
1604 Status s = stub_->Echo(&context, request, &response);
1605 EXPECT_FALSE(s.ok());
Jan Tattermuschef0e64c2017-11-13 07:50:37 +01001606 EXPECT_EQ(s.error_code(), StatusCode::UNAVAILABLE);
yang-g4b4571a2016-09-15 23:01:09 -07001607}
1608
yang-g88d5d522015-09-29 12:46:54 -07001609TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginFailure) {
1610 ResetStub();
Julien Boeuf1928d492015-09-15 15:20:11 -07001611 EchoRequest request;
1612 EchoResponse response;
1613 ClientContext context;
1614 context.set_credentials(
1615 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1616 new TestMetadataCredentialsPlugin(
yang-gc580af32016-09-15 15:28:38 -07001617 TestMetadataCredentialsPlugin::kGoodMetadataKey,
Julien Boeuf1928d492015-09-15 15:20:11 -07001618 "Does not matter, will fail anyway (see 3rd param)", false,
1619 false))));
1620 request.set_message("Hello");
1621
1622 Status s = stub_->Echo(&context, request, &response);
1623 EXPECT_FALSE(s.ok());
Jan Tattermuschef0e64c2017-11-13 07:50:37 +01001624 EXPECT_EQ(s.error_code(), StatusCode::UNAVAILABLE);
Mark D. Rothe0778b22017-07-21 15:42:00 -07001625 EXPECT_EQ(s.error_message(),
1626 grpc::string("Getting metadata from plugin failed with error: ") +
1627 kTestCredsPluginErrorMsg);
Julien Boeuf1928d492015-09-15 15:20:11 -07001628}
1629
yang-g88d5d522015-09-29 12:46:54 -07001630TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorSuccess) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001631 auto* processor = new TestAuthMetadataProcessor(false);
1632 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
yang-g88d5d522015-09-29 12:46:54 -07001633 ResetStub();
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001634 EchoRequest request;
1635 EchoResponse response;
1636 ClientContext context;
1637 context.set_credentials(processor->GetCompatibleClientCreds());
1638 request.set_message("Hello");
1639 request.mutable_param()->set_echo_metadata(true);
1640 request.mutable_param()->set_expected_client_identity(
1641 TestAuthMetadataProcessor::kGoodGuy);
Dan Bornf2f7d572016-03-03 17:26:12 -08001642 request.mutable_param()->set_expected_transport_security_type(
1643 GetParam().credentials_type);
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001644
1645 Status s = stub_->Echo(&context, request, &response);
1646 EXPECT_EQ(request.message(), response.message());
1647 EXPECT_TRUE(s.ok());
1648
1649 // Metadata should have been consumed by the processor.
1650 EXPECT_FALSE(MetadataContains(
1651 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1652 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1653}
1654
yang-g88d5d522015-09-29 12:46:54 -07001655TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorFailure) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001656 auto* processor = new TestAuthMetadataProcessor(false);
1657 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
yang-g88d5d522015-09-29 12:46:54 -07001658 ResetStub();
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001659 EchoRequest request;
1660 EchoResponse response;
1661 ClientContext context;
1662 context.set_credentials(processor->GetIncompatibleClientCreds());
1663 request.set_message("Hello");
1664
1665 Status s = stub_->Echo(&context, request, &response);
1666 EXPECT_FALSE(s.ok());
1667 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1668}
1669
yang-g88d5d522015-09-29 12:46:54 -07001670TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginFailure) {
1671 ResetStub();
Julien Boeuf1928d492015-09-15 15:20:11 -07001672 EchoRequest request;
1673 EchoResponse response;
1674 ClientContext context;
1675 context.set_credentials(
1676 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1677 new TestMetadataCredentialsPlugin(
yang-gc580af32016-09-15 15:28:38 -07001678 TestMetadataCredentialsPlugin::kGoodMetadataKey,
Julien Boeuf1928d492015-09-15 15:20:11 -07001679 "Does not matter, will fail anyway (see 3rd param)", true,
1680 false))));
1681 request.set_message("Hello");
1682
1683 Status s = stub_->Echo(&context, request, &response);
1684 EXPECT_FALSE(s.ok());
Jan Tattermuschef0e64c2017-11-13 07:50:37 +01001685 EXPECT_EQ(s.error_code(), StatusCode::UNAVAILABLE);
Mark D. Rothe0778b22017-07-21 15:42:00 -07001686 EXPECT_EQ(s.error_message(),
1687 grpc::string("Getting metadata from plugin failed with error: ") +
1688 kTestCredsPluginErrorMsg);
Julien Boeuf1928d492015-09-15 15:20:11 -07001689}
1690
yang-g3c003282017-09-20 13:58:13 -07001691TEST_P(SecureEnd2endTest, CompositeCallCreds) {
1692 ResetStub();
1693 EchoRequest request;
1694 EchoResponse response;
1695 ClientContext context;
1696 const char kMetadataKey1[] = "call-creds-key1";
1697 const char kMetadataKey2[] = "call-creds-key2";
1698 const char kMetadataVal1[] = "call-creds-val1";
1699 const char kMetadataVal2[] = "call-creds-val2";
1700
1701 context.set_credentials(CompositeCallCredentials(
1702 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1703 new TestMetadataCredentialsPlugin(kMetadataKey1, kMetadataVal1, true,
1704 true))),
1705 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1706 new TestMetadataCredentialsPlugin(kMetadataKey2, kMetadataVal2, true,
1707 true)))));
1708 request.set_message("Hello");
1709 request.mutable_param()->set_echo_metadata(true);
1710
1711 Status s = stub_->Echo(&context, request, &response);
1712 EXPECT_TRUE(s.ok());
1713 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1714 kMetadataKey1, kMetadataVal1));
1715 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1716 kMetadataKey2, kMetadataVal2));
1717}
1718
yang-g88d5d522015-09-29 12:46:54 -07001719TEST_P(SecureEnd2endTest, ClientAuthContext) {
1720 ResetStub();
yang-gc4eef2e2015-07-06 23:26:58 -07001721 EchoRequest request;
1722 EchoResponse response;
1723 request.set_message("Hello");
Dan Bornf2f7d572016-03-03 17:26:12 -08001724 request.mutable_param()->set_check_auth_context(GetParam().credentials_type ==
1725 kTlsCredentialsType);
1726 request.mutable_param()->set_expected_transport_security_type(
1727 GetParam().credentials_type);
yang-gc4eef2e2015-07-06 23:26:58 -07001728 ClientContext context;
1729 Status s = stub_->Echo(&context, request, &response);
1730 EXPECT_EQ(response.message(), request.message());
1731 EXPECT_TRUE(s.ok());
1732
yang-g8b25f2a2015-07-21 23:54:36 -07001733 std::shared_ptr<const AuthContext> auth_ctx = context.auth_context();
Dan Bornf2f7d572016-03-03 17:26:12 -08001734 std::vector<grpc::string_ref> tst =
yang-g8b25f2a2015-07-21 23:54:36 -07001735 auth_ctx->FindPropertyValues("transport_security_type");
Robbie Shade820c1f32016-06-23 14:31:28 -04001736 ASSERT_EQ(1u, tst.size());
Dan Bornf2f7d572016-03-03 17:26:12 -08001737 EXPECT_EQ(GetParam().credentials_type, ToString(tst[0]));
1738 if (GetParam().credentials_type == kTlsCredentialsType) {
1739 EXPECT_EQ("x509_subject_alternative_name",
1740 auth_ctx->GetPeerIdentityPropertyName());
Paul Querna47d841d2016-03-10 11:19:17 -08001741 EXPECT_EQ(4u, auth_ctx->GetPeerIdentity().size());
Dan Bornf2f7d572016-03-03 17:26:12 -08001742 EXPECT_EQ("*.test.google.fr", ToString(auth_ctx->GetPeerIdentity()[0]));
1743 EXPECT_EQ("waterzooi.test.google.be",
1744 ToString(auth_ctx->GetPeerIdentity()[1]));
1745 EXPECT_EQ("*.test.youtube.com", ToString(auth_ctx->GetPeerIdentity()[2]));
Paul Querna47d841d2016-03-10 11:19:17 -08001746 EXPECT_EQ("192.168.1.3", ToString(auth_ctx->GetPeerIdentity()[3]));
Dan Bornf2f7d572016-03-03 17:26:12 -08001747 }
yang-gc4eef2e2015-07-06 23:26:58 -07001748}
1749
Craig Tiller20afa3d2016-10-17 14:52:14 -07001750class ResourceQuotaEnd2endTest : public End2endTest {
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001751 public:
Craig Tiller20afa3d2016-10-17 14:52:14 -07001752 ResourceQuotaEnd2endTest()
1753 : server_resource_quota_("server_resource_quota") {}
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001754
Vijay Paic0b2acb2016-11-01 16:31:56 -07001755 virtual void ConfigureServerBuilder(ServerBuilder* builder) override {
Craig Tiller20afa3d2016-10-17 14:52:14 -07001756 builder->SetResourceQuota(server_resource_quota_);
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001757 }
1758
1759 private:
Craig Tiller20afa3d2016-10-17 14:52:14 -07001760 ResourceQuota server_resource_quota_;
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001761};
1762
Craig Tiller20afa3d2016-10-17 14:52:14 -07001763TEST_P(ResourceQuotaEnd2endTest, SimpleRequest) {
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001764 ResetStub();
1765
1766 EchoRequest request;
1767 EchoResponse response;
1768 request.set_message("Hello");
1769
1770 ClientContext context;
1771 Status s = stub_->Echo(&context, request, &response);
1772 EXPECT_EQ(response.message(), request.message());
1773 EXPECT_TRUE(s.ok());
1774}
1775
yang-g4c8aed32016-02-19 00:19:39 -08001776std::vector<TestScenario> CreateTestScenarios(bool use_proxy,
1777 bool test_insecure,
Vijay Pai3d7d5f42017-05-04 10:02:24 -07001778 bool test_secure,
1779 bool test_inproc) {
yang-g4c8aed32016-02-19 00:19:39 -08001780 std::vector<TestScenario> scenarios;
1781 std::vector<grpc::string> credentials_types;
1782 if (test_secure) {
yang-gcc591022017-01-11 11:10:43 -08001783 credentials_types =
1784 GetCredentialsProvider()->GetSecureCredentialsTypeList();
yang-g4c8aed32016-02-19 00:19:39 -08001785 }
Vijay Pai3d7d5f42017-05-04 10:02:24 -07001786 auto insec_ok = [] {
1787 // Only allow insecure credentials type when it is registered with the
yang-gcc591022017-01-11 11:10:43 -08001788 // provider. User may create providers that do not have insecure.
Vijay Pai3d7d5f42017-05-04 10:02:24 -07001789 return GetCredentialsProvider()->GetChannelCredentials(
1790 kInsecureCredentialsType, nullptr) != nullptr;
1791 };
1792 if (test_insecure && insec_ok()) {
1793 credentials_types.push_back(kInsecureCredentialsType);
yang-g4c8aed32016-02-19 00:19:39 -08001794 }
yang-gcc591022017-01-11 11:10:43 -08001795 GPR_ASSERT(!credentials_types.empty());
Vijay Paia6294052017-10-10 20:47:54 -07001796 for (const auto& cred : credentials_types) {
1797 scenarios.emplace_back(false, false, cred);
yang-g4c8aed32016-02-19 00:19:39 -08001798 if (use_proxy) {
Vijay Paia6294052017-10-10 20:47:54 -07001799 scenarios.emplace_back(true, false, cred);
yang-g4c8aed32016-02-19 00:19:39 -08001800 }
1801 }
Vijay Pai3d7d5f42017-05-04 10:02:24 -07001802 if (test_inproc && insec_ok()) {
1803 scenarios.emplace_back(false, true, kInsecureCredentialsType);
1804 }
yang-g4c8aed32016-02-19 00:19:39 -08001805 return scenarios;
1806}
yang-g6f30dec2015-07-22 23:11:56 -07001807
yang-g4c8aed32016-02-19 00:19:39 -08001808INSTANTIATE_TEST_CASE_P(End2end, End2endTest,
1809 ::testing::ValuesIn(CreateTestScenarios(false, true,
Vijay Pai3d7d5f42017-05-04 10:02:24 -07001810 true, true)));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001811
yang-g4c8aed32016-02-19 00:19:39 -08001812INSTANTIATE_TEST_CASE_P(End2endServerTryCancel, End2endServerTryCancelTest,
1813 ::testing::ValuesIn(CreateTestScenarios(false, true,
Vijay Pai3d7d5f42017-05-04 10:02:24 -07001814 true, true)));
yang-g4c8aed32016-02-19 00:19:39 -08001815
1816INSTANTIATE_TEST_CASE_P(ProxyEnd2end, ProxyEnd2endTest,
1817 ::testing::ValuesIn(CreateTestScenarios(true, true,
Vijay Paia6294052017-10-10 20:47:54 -07001818 true, true)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001819
yang-g88d5d522015-09-29 12:46:54 -07001820INSTANTIATE_TEST_CASE_P(SecureEnd2end, SecureEnd2endTest,
yang-g4c8aed32016-02-19 00:19:39 -08001821 ::testing::ValuesIn(CreateTestScenarios(false, false,
Vijay Pai3d7d5f42017-05-04 10:02:24 -07001822 true, false)));
yang-g88d5d522015-09-29 12:46:54 -07001823
Craig Tiller20afa3d2016-10-17 14:52:14 -07001824INSTANTIATE_TEST_CASE_P(ResourceQuotaEnd2end, ResourceQuotaEnd2endTest,
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001825 ::testing::ValuesIn(CreateTestScenarios(false, true,
Vijay Pai3d7d5f42017-05-04 10:02:24 -07001826 true, true)));
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001827
yang-g8ab38362015-07-31 14:05:33 -07001828} // namespace
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001829} // namespace testing
1830} // namespace grpc
1831
1832int main(int argc, char** argv) {
Sree Kuchibhotlade5f6bf2018-01-25 17:23:42 -08001833 gpr_setenv("GRPC_CLIENT_CHANNEL_BACKUP_POLL_INTERVAL_MS", "200");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001834 grpc_test_init(argc, argv);
1835 ::testing::InitGoogleTest(&argc, argv);
1836 return RUN_ALL_TESTS();
David Garcia Quintas2bf574f2016-01-14 15:27:08 -08001837}