blob: d2c7edc64b9dbb1f21958dbee6ee002e7dd5c395 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 Gao69fe0752015-06-01 14:32:38 -070034#include <mutex>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035#include <thread>
yangged5e7e02015-01-06 10:16:15 -080036
yang-g9e2f90c2015-08-21 15:35:03 -070037#include <grpc++/channel.h>
38#include <grpc++/client_context.h>
39#include <grpc++/create_channel.h>
Craig Tiller20afa3d2016-10-17 14:52:14 -070040#include <grpc++/resource_quota.h>
Julien Boeuf5be92a32015-08-28 16:28:18 -070041#include <grpc++/security/auth_metadata_processor.h>
42#include <grpc++/security/credentials.h>
43#include <grpc++/security/server_credentials.h>
yang-g9e2f90c2015-08-21 15:35:03 -070044#include <grpc++/server.h>
45#include <grpc++/server_builder.h>
46#include <grpc++/server_context.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080047#include <grpc/grpc.h>
David Garcia Quintasc79b0652016-07-27 21:11:58 -070048#include <grpc/support/log.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080049#include <grpc/support/thd.h>
50#include <grpc/support/time.h>
yang-g9e2f90c2015-08-21 15:35:03 -070051#include <gtest/gtest.h>
52
Julien Boeuf8ca294e2016-05-02 14:56:30 -070053#include "src/core/lib/security/credentials/credentials.h"
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080054#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
55#include "src/proto/grpc/testing/echo.grpc.pb.h"
Nicolas Noble89219162015-04-07 18:01:18 -070056#include "test/core/util/port.h"
Craig Tiller14e60e92015-01-13 17:26:46 -080057#include "test/core/util/test_config.h"
yang-g7b0edbd2016-02-02 16:05:21 -080058#include "test/cpp/end2end/test_service_impl.h"
yang-ge21908f2015-08-25 13:47:51 -070059#include "test/cpp/util/string_ref_helper.h"
yang-g7d2a3e12016-02-18 15:41:56 -080060#include "test/cpp/util/test_credentials_provider.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061
Craig Tiller1b4e3302015-12-17 16:35:00 -080062using grpc::testing::EchoRequest;
63using grpc::testing::EchoResponse;
Dan Bornf2f7d572016-03-03 17:26:12 -080064using grpc::testing::kTlsCredentialsType;
yangged5e7e02015-01-06 10:16:15 -080065using std::chrono::system_clock;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080066
67namespace grpc {
yangged5e7e02015-01-06 10:16:15 -080068namespace testing {
yangged5e7e02015-01-06 10:16:15 -080069namespace {
70
yang-gd7ead692015-07-30 10:57:45 -070071bool CheckIsLocalhost(const grpc::string& addr) {
72 const grpc::string kIpv6("ipv6:[::1]:");
73 const grpc::string kIpv4MappedIpv6("ipv6:[::ffff:127.0.0.1]:");
74 const grpc::string kIpv4("ipv4:127.0.0.1:");
75 return addr.substr(0, kIpv4.size()) == kIpv4 ||
76 addr.substr(0, kIpv4MappedIpv6.size()) == kIpv4MappedIpv6 ||
77 addr.substr(0, kIpv6.size()) == kIpv6;
78}
79
Julien Boeuf38c0cde2016-06-06 14:46:08 +020080const char kTestCredsPluginErrorMsg[] = "Could not find plugin metadata.";
81
Julien Boeuf1928d492015-09-15 15:20:11 -070082class TestMetadataCredentialsPlugin : public MetadataCredentialsPlugin {
83 public:
yang-gc580af32016-09-15 15:28:38 -070084 static const char kGoodMetadataKey[];
85 static const char kBadMetadataKey[];
Julien Boeuf1928d492015-09-15 15:20:11 -070086
yang-gc580af32016-09-15 15:28:38 -070087 TestMetadataCredentialsPlugin(grpc::string_ref metadata_key,
88 grpc::string_ref metadata_value,
Julien Boeuf1928d492015-09-15 15:20:11 -070089 bool is_blocking, bool is_successful)
yang-gc580af32016-09-15 15:28:38 -070090 : metadata_key_(metadata_key.data(), metadata_key.length()),
91 metadata_value_(metadata_value.data(), metadata_value.length()),
Julien Boeuf1928d492015-09-15 15:20:11 -070092 is_blocking_(is_blocking),
93 is_successful_(is_successful) {}
94
Vijay Paic0b2acb2016-11-01 16:31:56 -070095 bool IsBlocking() const override { return is_blocking_; }
Julien Boeuf1928d492015-09-15 15:20:11 -070096
Vijay Pai713c7b82016-11-01 16:33:18 -070097 Status GetMetadata(
98 grpc::string_ref service_url, grpc::string_ref method_name,
99 const grpc::AuthContext& channel_auth_context,
100 std::multimap<grpc::string, grpc::string>* metadata) override {
Julien Boeuf1928d492015-09-15 15:20:11 -0700101 EXPECT_GT(service_url.length(), 0UL);
Julien Boeuf114f3942015-11-19 21:45:52 -0800102 EXPECT_GT(method_name.length(), 0UL);
103 EXPECT_TRUE(channel_auth_context.IsPeerAuthenticated());
Julien Boeuf1928d492015-09-15 15:20:11 -0700104 EXPECT_TRUE(metadata != nullptr);
105 if (is_successful_) {
yang-gc580af32016-09-15 15:28:38 -0700106 metadata->insert(std::make_pair(metadata_key_, metadata_value_));
Julien Boeuf1928d492015-09-15 15:20:11 -0700107 return Status::OK;
108 } else {
Julien Boeuf38c0cde2016-06-06 14:46:08 +0200109 return Status(StatusCode::NOT_FOUND, kTestCredsPluginErrorMsg);
Julien Boeuf1928d492015-09-15 15:20:11 -0700110 }
111 }
112
113 private:
yang-gc580af32016-09-15 15:28:38 -0700114 grpc::string metadata_key_;
Julien Boeuf1928d492015-09-15 15:20:11 -0700115 grpc::string metadata_value_;
116 bool is_blocking_;
117 bool is_successful_;
118};
119
yang-gc580af32016-09-15 15:28:38 -0700120const char TestMetadataCredentialsPlugin::kBadMetadataKey[] =
121 "TestPluginMetadata";
122const char TestMetadataCredentialsPlugin::kGoodMetadataKey[] =
123 "test-plugin-metadata";
Julien Boeuf1928d492015-09-15 15:20:11 -0700124
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700125class TestAuthMetadataProcessor : public AuthMetadataProcessor {
126 public:
127 static const char kGoodGuy[];
128
129 TestAuthMetadataProcessor(bool is_blocking) : is_blocking_(is_blocking) {}
130
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700131 std::shared_ptr<CallCredentials> GetCompatibleClientCreds() {
Julien Boeuf1928d492015-09-15 15:20:11 -0700132 return MetadataCredentialsFromPlugin(
133 std::unique_ptr<MetadataCredentialsPlugin>(
yang-gc580af32016-09-15 15:28:38 -0700134 new TestMetadataCredentialsPlugin(
135 TestMetadataCredentialsPlugin::kGoodMetadataKey, kGoodGuy,
136 is_blocking_, true)));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700137 }
Julien Boeuf1928d492015-09-15 15:20:11 -0700138
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700139 std::shared_ptr<CallCredentials> GetIncompatibleClientCreds() {
Julien Boeuf1928d492015-09-15 15:20:11 -0700140 return MetadataCredentialsFromPlugin(
141 std::unique_ptr<MetadataCredentialsPlugin>(
yang-gc580af32016-09-15 15:28:38 -0700142 new TestMetadataCredentialsPlugin(
143 TestMetadataCredentialsPlugin::kGoodMetadataKey, "Mr Hyde",
144 is_blocking_, true)));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700145 }
146
147 // Interface implementation
Vijay Paic0b2acb2016-11-01 16:31:56 -0700148 bool IsBlocking() const override { return is_blocking_; }
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700149
150 Status Process(const InputMetadata& auth_metadata, AuthContext* context,
151 OutputMetadata* consumed_auth_metadata,
Vijay Paic0b2acb2016-11-01 16:31:56 -0700152 OutputMetadata* response_metadata) override {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700153 EXPECT_TRUE(consumed_auth_metadata != nullptr);
154 EXPECT_TRUE(context != nullptr);
155 EXPECT_TRUE(response_metadata != nullptr);
Julien Boeuf1928d492015-09-15 15:20:11 -0700156 auto auth_md =
yang-gc580af32016-09-15 15:28:38 -0700157 auth_metadata.find(TestMetadataCredentialsPlugin::kGoodMetadataKey);
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700158 EXPECT_NE(auth_md, auth_metadata.end());
159 string_ref auth_md_value = auth_md->second;
Julien Boeuf1928d492015-09-15 15:20:11 -0700160 if (auth_md_value == kGoodGuy) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700161 context->AddProperty(kIdentityPropName, kGoodGuy);
162 context->SetPeerIdentityPropertyName(kIdentityPropName);
Julien Boeuf8b0b6f42015-09-22 13:31:16 -0700163 consumed_auth_metadata->insert(std::make_pair(
164 string(auth_md->first.data(), auth_md->first.length()),
165 string(auth_md->second.data(), auth_md->second.length())));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700166 return Status::OK;
167 } else {
168 return Status(StatusCode::UNAUTHENTICATED,
169 string("Invalid principal: ") +
170 string(auth_md_value.data(), auth_md_value.length()));
171 }
172 }
173
Julien Boeuf1928d492015-09-15 15:20:11 -0700174 private:
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700175 static const char kIdentityPropName[];
176 bool is_blocking_;
177};
178
179const char TestAuthMetadataProcessor::kGoodGuy[] = "Dr Jekyll";
180const char TestAuthMetadataProcessor::kIdentityPropName[] = "novel identity";
181
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800182class Proxy : public ::grpc::testing::EchoTestService::Service {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700183 public:
yang-g8c2be9f2015-08-19 16:28:09 -0700184 Proxy(std::shared_ptr<Channel> channel)
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800185 : stub_(grpc::testing::EchoTestService::NewStub(channel)) {}
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700186
187 Status Echo(ServerContext* server_context, const EchoRequest* request,
Vijay Paic0b2acb2016-11-01 16:31:56 -0700188 EchoResponse* response) override {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700189 std::unique_ptr<ClientContext> client_context =
190 ClientContext::FromServerContext(*server_context);
191 return stub_->Echo(client_context.get(), *request, response);
192 }
193
194 private:
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800195 std::unique_ptr< ::grpc::testing::EchoTestService::Stub> stub_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700196};
197
yangg1456d152015-01-08 15:39:58 -0800198class TestServiceImplDupPkg
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800199 : public ::grpc::testing::duplicate::EchoTestService::Service {
yangg1456d152015-01-08 15:39:58 -0800200 public:
201 Status Echo(ServerContext* context, const EchoRequest* request,
Vijay Paic0b2acb2016-11-01 16:31:56 -0700202 EchoResponse* response) override {
yangg1456d152015-01-08 15:39:58 -0800203 response->set_message("no package");
204 return Status::OK;
205 }
206};
207
yang-g88d5d522015-09-29 12:46:54 -0700208class TestScenario {
209 public:
yang-g17197dd2016-02-19 00:04:22 -0800210 TestScenario(bool proxy, const grpc::string& creds_type)
211 : use_proxy(proxy), credentials_type(creds_type) {}
Craig Tillerf658bf02016-12-08 14:11:47 -0800212 void Log() const;
yang-g88d5d522015-09-29 12:46:54 -0700213 bool use_proxy;
Vijay Pai679c75f2016-06-15 13:08:00 -0700214 // Although the below grpc::string is logically const, we can't declare
215 // them const because of a limitation in the way old compilers (e.g., gcc-4.4)
216 // manage vector insertion using a copy constructor
217 grpc::string credentials_type;
yang-g88d5d522015-09-29 12:46:54 -0700218};
219
Craig Tillerf658bf02016-12-08 14:11:47 -0800220static std::ostream& operator<<(std::ostream& out,
221 const TestScenario& scenario) {
222 return out << "TestScenario{use_proxy="
223 << (scenario.use_proxy ? "true" : "false") << ", credentials='"
224 << scenario.credentials_type << "'}";
225}
226
227void TestScenario::Log() const {
228 std::ostringstream out;
229 out << *this;
230 gpr_log(GPR_DEBUG, "%s", out.str().c_str());
231}
232
yang-g88d5d522015-09-29 12:46:54 -0700233class End2endTest : public ::testing::TestWithParam<TestScenario> {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800234 protected:
Vijay Pai181ef452015-07-14 13:52:48 -0700235 End2endTest()
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700236 : is_server_started_(false),
237 kMaxMessageSize_(8192),
yang-g88d5d522015-09-29 12:46:54 -0700238 special_service_("special") {
239 GetParam().Log();
240 }
Craig Tiller7418d012015-02-11 15:25:03 -0800241
Vijay Paic0b2acb2016-11-01 16:31:56 -0700242 void TearDown() override {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700243 if (is_server_started_) {
244 server_->Shutdown();
245 if (proxy_server_) proxy_server_->Shutdown();
246 }
247 }
248
249 void StartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) {
Craig Tiller35e39712015-01-12 16:41:24 -0800250 int port = grpc_pick_unused_port_or_die();
yang-gd7ead692015-07-30 10:57:45 -0700251 server_address_ << "127.0.0.1:" << port;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800252 // Setup server
253 ServerBuilder builder;
Craig Tillerdb1a5cc2016-09-28 14:22:12 -0700254 ConfigureServerBuilder(&builder);
yang-gcc591022017-01-11 11:10:43 -0800255 auto server_creds = GetCredentialsProvider()->GetServerCredentials(
256 GetParam().credentials_type);
yang-g17197dd2016-02-19 00:04:22 -0800257 if (GetParam().credentials_type != kInsecureCredentialsType) {
yang-g88d5d522015-09-29 12:46:54 -0700258 server_creds->SetAuthMetadataProcessor(processor);
259 }
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700260 builder.AddListeningPort(server_address_.str(), server_creds);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800261 builder.RegisterService(&service_);
yang-g8b25f2a2015-07-21 23:54:36 -0700262 builder.RegisterService("foo.test.youtube.com", &special_service_);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800263 builder.RegisterService(&dup_pkg_service_);
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700264
Sree Kuchibhotlac37a8a52016-10-13 15:40:15 -0700265 builder.SetSyncServerOption(ServerBuilder::SyncServerOption::NUM_CQS, 4);
266 builder.SetSyncServerOption(
267 ServerBuilder::SyncServerOption::CQ_TIMEOUT_MSEC, 10);
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700268
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800269 server_ = builder.BuildAndStart();
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700270 is_server_started_ = true;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700271 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800272
Craig Tillerdb1a5cc2016-09-28 14:22:12 -0700273 virtual void ConfigureServerBuilder(ServerBuilder* builder) {
274 builder->SetMaxMessageSize(
275 kMaxMessageSize_); // For testing max message size.
276 }
277
yang-g9b7757d2015-08-13 11:15:53 -0700278 void ResetChannel() {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700279 if (!is_server_started_) {
280 StartServer(std::shared_ptr<AuthMetadataProcessor>());
281 }
282 EXPECT_TRUE(is_server_started_);
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700283 ChannelArguments args;
yang-gcc591022017-01-11 11:10:43 -0800284 auto channel_creds = GetCredentialsProvider()->GetChannelCredentials(
285 GetParam().credentials_type, &args);
yang-gd59ad7e2016-02-10 12:42:53 -0800286 if (!user_agent_prefix_.empty()) {
287 args.SetUserAgentPrefix(user_agent_prefix_);
288 }
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700289 args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test");
yang-g88d5d522015-09-29 12:46:54 -0700290 channel_ = CreateCustomChannel(server_address_.str(), channel_creds, args);
yang-g9b7757d2015-08-13 11:15:53 -0700291 }
292
yang-g88d5d522015-09-29 12:46:54 -0700293 void ResetStub() {
yang-g9b7757d2015-08-13 11:15:53 -0700294 ResetChannel();
yang-g88d5d522015-09-29 12:46:54 -0700295 if (GetParam().use_proxy) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700296 proxy_service_.reset(new Proxy(channel_));
297 int port = grpc_pick_unused_port_or_die();
298 std::ostringstream proxyaddr;
299 proxyaddr << "localhost:" << port;
300 ServerBuilder builder;
301 builder.AddListeningPort(proxyaddr.str(), InsecureServerCredentials());
302 builder.RegisterService(proxy_service_.get());
Sree Kuchibhotlac37a8a52016-10-13 15:40:15 -0700303
304 builder.SetSyncServerOption(ServerBuilder::SyncServerOption::NUM_CQS, 4);
305 builder.SetSyncServerOption(
306 ServerBuilder::SyncServerOption::CQ_TIMEOUT_MSEC, 10);
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700307
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700308 proxy_server_ = builder.BuildAndStart();
309
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700310 channel_ = CreateChannel(proxyaddr.str(), InsecureChannelCredentials());
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700311 }
312
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800313 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
yangg1456d152015-01-08 15:39:58 -0800314 }
315
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700316 bool is_server_started_;
yang-g8c2be9f2015-08-19 16:28:09 -0700317 std::shared_ptr<Channel> channel_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800318 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800319 std::unique_ptr<Server> server_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700320 std::unique_ptr<Server> proxy_server_;
321 std::unique_ptr<Proxy> proxy_service_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800322 std::ostringstream server_address_;
Yang Gao3921c562015-04-30 16:07:06 -0700323 const int kMaxMessageSize_;
nnoble0c475f02014-12-05 15:37:39 -0800324 TestServiceImpl service_;
Craig Tiller822d2c72015-07-07 16:08:00 -0700325 TestServiceImpl special_service_;
yangg1456d152015-01-08 15:39:58 -0800326 TestServiceImplDupPkg dup_pkg_service_;
yang-gd59ad7e2016-02-10 12:42:53 -0800327 grpc::string user_agent_prefix_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800328};
329
yang-gf8174ea2016-02-01 00:09:13 -0800330static void SendRpc(grpc::testing::EchoTestService::Stub* stub, int num_rpcs,
331 bool with_binary_metadata) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800332 EchoRequest request;
333 EchoResponse response;
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700334 request.set_message("Hello hello hello hello");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800335
336 for (int i = 0; i < num_rpcs; ++i) {
337 ClientContext context;
yang-gf8174ea2016-02-01 00:09:13 -0800338 if (with_binary_metadata) {
339 char bytes[8] = {'\0', '\1', '\2', '\3', '\4', '\5', '\6', (char)i};
340 context.AddMetadata("custom-bin", grpc::string(bytes, 8));
341 }
Craig Tillerbf6abee2015-07-19 22:31:04 -0700342 context.set_compression_algorithm(GRPC_COMPRESS_GZIP);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800343 Status s = stub->Echo(&context, request, &response);
344 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700345 EXPECT_TRUE(s.ok());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800346 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800347}
348
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800349// This class is for testing scenarios where RPCs are cancelled on the server
350// by calling ServerContext::TryCancel()
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800351class End2endServerTryCancelTest : public End2endTest {
352 protected:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800353 // Helper for testing client-streaming RPCs which are cancelled on the server.
354 // Depending on the value of server_try_cancel parameter, this will test one
355 // of the following three scenarios:
356 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading
357 // any messages from the client
358 //
359 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading
360 // messages from the client
361 //
362 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all
363 // the messages from the client
364 //
365 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800366 void TestRequestStreamServerCancel(
367 ServerTryCancelRequestPhase server_try_cancel, int num_msgs_to_send) {
368 ResetStub();
369 EchoRequest request;
370 EchoResponse response;
371 ClientContext context;
372
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800373 // Send server_try_cancel value in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800374 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700375 grpc::to_string(server_try_cancel));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800376
377 auto stream = stub_->RequestStream(&context, &response);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800378
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800379 int num_msgs_sent = 0;
380 while (num_msgs_sent < num_msgs_to_send) {
381 request.set_message("hello");
382 if (!stream->Write(request)) {
383 break;
384 }
385 num_msgs_sent++;
386 }
387 gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800388
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800389 stream->WritesDone();
390 Status s = stream->Finish();
391
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800392 // At this point, we know for sure that RPC was cancelled by the server
393 // since we passed server_try_cancel value in the metadata. Depending on the
394 // value of server_try_cancel, the RPC might have been cancelled by the
395 // server at different stages. The following validates our expectations of
396 // number of messages sent in various cancellation scenarios:
397
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800398 switch (server_try_cancel) {
399 case CANCEL_BEFORE_PROCESSING:
400 case CANCEL_DURING_PROCESSING:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800401 // If the RPC is cancelled by server before / during messages from the
402 // client, it means that the client most likely did not get a chance to
403 // send all the messages it wanted to send. i.e num_msgs_sent <=
404 // num_msgs_to_send
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800405 EXPECT_LE(num_msgs_sent, num_msgs_to_send);
406 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800407
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800408 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800409 // If the RPC was cancelled after all messages were read by the server,
410 // the client did get a chance to send all its messages
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800411 EXPECT_EQ(num_msgs_sent, num_msgs_to_send);
412 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800413
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800414 default:
415 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
416 server_try_cancel);
417 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
418 server_try_cancel <= CANCEL_AFTER_PROCESSING);
419 break;
420 }
421
422 EXPECT_FALSE(s.ok());
423 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
424 }
425
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800426 // Helper for testing server-streaming RPCs which are cancelled on the server.
427 // Depending on the value of server_try_cancel parameter, this will test one
428 // of the following three scenarios:
429 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before writing
430 // any messages to the client
431 //
432 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while writing
433 // messages to the client
434 //
435 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after writing all
436 // the messages to the client
437 //
438 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800439 void TestResponseStreamServerCancel(
440 ServerTryCancelRequestPhase server_try_cancel) {
441 ResetStub();
442 EchoRequest request;
443 EchoResponse response;
444 ClientContext context;
445
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800446 // Send server_try_cancel in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800447 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700448 grpc::to_string(server_try_cancel));
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800449
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800450 request.set_message("hello");
451 auto stream = stub_->ResponseStream(&context, request);
452
453 int num_msgs_read = 0;
454 while (num_msgs_read < kNumResponseStreamsMsgs) {
455 if (!stream->Read(&response)) {
456 break;
457 }
458 EXPECT_EQ(response.message(),
Vijay Paia63271c2016-06-15 12:56:38 -0700459 request.message() + grpc::to_string(num_msgs_read));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800460 num_msgs_read++;
461 }
462 gpr_log(GPR_INFO, "Read %d messages", num_msgs_read);
463
464 Status s = stream->Finish();
465
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800466 // Depending on the value of server_try_cancel, the RPC might have been
467 // cancelled by the server at different stages. The following validates our
468 // expectations of number of messages read in various cancellation
469 // scenarios:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800470 switch (server_try_cancel) {
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800471 case CANCEL_BEFORE_PROCESSING:
472 // Server cancelled before sending any messages. Which means the client
473 // wouldn't have read any
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800474 EXPECT_EQ(num_msgs_read, 0);
475 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800476
477 case CANCEL_DURING_PROCESSING:
478 // Server cancelled while writing messages. Client must have read less
479 // than or equal to the expected number of messages
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800480 EXPECT_LE(num_msgs_read, kNumResponseStreamsMsgs);
481 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800482
483 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla8d543e82016-02-29 18:22:25 -0800484 // Even though the Server cancelled after writing all messages, the RPC
485 // may be cancelled before the Client got a chance to read all the
486 // messages.
487 EXPECT_LE(num_msgs_read, kNumResponseStreamsMsgs);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800488 break;
489
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800490 default: {
491 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
492 server_try_cancel);
493 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
494 server_try_cancel <= CANCEL_AFTER_PROCESSING);
495 break;
496 }
497 }
498
499 EXPECT_FALSE(s.ok());
500 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
501 }
502
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800503 // Helper for testing bidirectional-streaming RPCs which are cancelled on the
504 // server. Depending on the value of server_try_cancel parameter, this will
505 // test one of the following three scenarios:
506 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading/
507 // writing any messages from/to the client
508 //
509 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading/
510 // writing messages from/to the client
511 //
512 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading/writing
513 // all the messages from/to the client
514 //
515 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800516 void TestBidiStreamServerCancel(ServerTryCancelRequestPhase server_try_cancel,
517 int num_messages) {
518 ResetStub();
519 EchoRequest request;
520 EchoResponse response;
521 ClientContext context;
522
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800523 // Send server_try_cancel in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800524 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700525 grpc::to_string(server_try_cancel));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800526
527 auto stream = stub_->BidiStream(&context);
528
529 int num_msgs_read = 0;
530 int num_msgs_sent = 0;
531 while (num_msgs_sent < num_messages) {
Vijay Paia63271c2016-06-15 12:56:38 -0700532 request.set_message("hello " + grpc::to_string(num_msgs_sent));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800533 if (!stream->Write(request)) {
534 break;
535 }
536 num_msgs_sent++;
537
538 if (!stream->Read(&response)) {
539 break;
540 }
541 num_msgs_read++;
542
543 EXPECT_EQ(response.message(), request.message());
544 }
545 gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent);
546 gpr_log(GPR_INFO, "Read %d messages", num_msgs_read);
547
548 stream->WritesDone();
549 Status s = stream->Finish();
550
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800551 // Depending on the value of server_try_cancel, the RPC might have been
552 // cancelled by the server at different stages. The following validates our
553 // expectations of number of messages read in various cancellation
554 // scenarios:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800555 switch (server_try_cancel) {
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800556 case CANCEL_BEFORE_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800557 EXPECT_EQ(num_msgs_read, 0);
558 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800559
560 case CANCEL_DURING_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800561 EXPECT_LE(num_msgs_sent, num_messages);
562 EXPECT_LE(num_msgs_read, num_msgs_sent);
563 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800564
565 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800566 EXPECT_EQ(num_msgs_sent, num_messages);
Sree Kuchibhotla8d543e82016-02-29 18:22:25 -0800567
568 // The Server cancelled after reading the last message and after writing
569 // the message to the client. However, the RPC cancellation might have
570 // taken effect before the client actually read the response.
571 EXPECT_LE(num_msgs_read, num_msgs_sent);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800572 break;
573
574 default:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800575 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
576 server_try_cancel);
577 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
578 server_try_cancel <= CANCEL_AFTER_PROCESSING);
579 break;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800580 }
581
582 EXPECT_FALSE(s.ok());
583 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
584 }
585};
586
587TEST_P(End2endServerTryCancelTest, RequestEchoServerCancel) {
588 ResetStub();
589 EchoRequest request;
590 EchoResponse response;
591 ClientContext context;
592
593 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700594 grpc::to_string(CANCEL_BEFORE_PROCESSING));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800595 Status s = stub_->Echo(&context, request, &response);
596 EXPECT_FALSE(s.ok());
597 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
598}
599
600// Server to cancel before doing reading the request
601TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelBeforeReads) {
602 TestRequestStreamServerCancel(CANCEL_BEFORE_PROCESSING, 1);
603}
604
605// Server to cancel while reading a request from the stream in parallel
606TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelDuringRead) {
607 TestRequestStreamServerCancel(CANCEL_DURING_PROCESSING, 10);
608}
609
610// Server to cancel after reading all the requests but before returning to the
611// client
612TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelAfterReads) {
613 TestRequestStreamServerCancel(CANCEL_AFTER_PROCESSING, 4);
614}
615
616// Server to cancel before sending any response messages
617TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelBefore) {
618 TestResponseStreamServerCancel(CANCEL_BEFORE_PROCESSING);
619}
620
621// Server to cancel while writing a response to the stream in parallel
622TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelDuring) {
623 TestResponseStreamServerCancel(CANCEL_DURING_PROCESSING);
624}
625
626// Server to cancel after writing all the respones to the stream but before
627// returning to the client
628TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelAfter) {
629 TestResponseStreamServerCancel(CANCEL_AFTER_PROCESSING);
630}
631
632// Server to cancel before reading/writing any requests/responses on the stream
633TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelBefore) {
634 TestBidiStreamServerCancel(CANCEL_BEFORE_PROCESSING, 2);
635}
636
637// Server to cancel while reading/writing requests/responses on the stream in
638// parallel
639TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelDuring) {
640 TestBidiStreamServerCancel(CANCEL_DURING_PROCESSING, 10);
641}
642
643// Server to cancel after reading/writing all requests/responses on the stream
644// but before returning to the client
645TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelAfter) {
646 TestBidiStreamServerCancel(CANCEL_AFTER_PROCESSING, 5);
647}
648
Craig Tillerf658bf02016-12-08 14:11:47 -0800649TEST_P(End2endTest, SimpleRpcWithCustomUserAgentPrefix) {
yang-gd59ad7e2016-02-10 12:42:53 -0800650 user_agent_prefix_ = "custom_prefix";
651 ResetStub();
652 EchoRequest request;
653 EchoResponse response;
654 request.set_message("Hello hello hello hello");
655 request.mutable_param()->set_echo_metadata(true);
656
657 ClientContext context;
658 Status s = stub_->Echo(&context, request, &response);
659 EXPECT_EQ(response.message(), request.message());
660 EXPECT_TRUE(s.ok());
661 const auto& trailing_metadata = context.GetServerTrailingMetadata();
662 auto iter = trailing_metadata.find("user-agent");
663 EXPECT_TRUE(iter != trailing_metadata.end());
664 grpc::string expected_prefix = user_agent_prefix_ + " grpc-c++/";
Mark D. Rothe3a21002016-10-24 13:29:05 -0700665 EXPECT_TRUE(iter->second.starts_with(expected_prefix)) << iter->second;
yang-gd59ad7e2016-02-10 12:42:53 -0800666}
667
yang-gf8174ea2016-02-01 00:09:13 -0800668TEST_P(End2endTest, MultipleRpcsWithVariedBinaryMetadataValue) {
669 ResetStub();
Vijay Paib0a6be22016-11-03 12:45:02 -0700670 std::vector<std::thread> threads;
yang-gf8174ea2016-02-01 00:09:13 -0800671 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -0700672 threads.emplace_back(SendRpc, stub_.get(), 10, true);
yang-gf8174ea2016-02-01 00:09:13 -0800673 }
674 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -0700675 threads[i].join();
yang-gf8174ea2016-02-01 00:09:13 -0800676 }
677}
678
679TEST_P(End2endTest, MultipleRpcs) {
680 ResetStub();
Vijay Paib0a6be22016-11-03 12:45:02 -0700681 std::vector<std::thread> threads;
yang-gf8174ea2016-02-01 00:09:13 -0800682 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -0700683 threads.emplace_back(SendRpc, stub_.get(), 10, false);
yang-gf8174ea2016-02-01 00:09:13 -0800684 }
685 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -0700686 threads[i].join();
yang-gf8174ea2016-02-01 00:09:13 -0800687 }
688}
689
yang-g88d5d522015-09-29 12:46:54 -0700690TEST_P(End2endTest, RequestStreamOneRequest) {
691 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800692 EchoRequest request;
693 EchoResponse response;
694 ClientContext context;
695
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800696 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800697 request.set_message("hello");
698 EXPECT_TRUE(stream->Write(request));
699 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800700 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800701 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700702 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800703}
704
yang-g88d5d522015-09-29 12:46:54 -0700705TEST_P(End2endTest, RequestStreamTwoRequests) {
706 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800707 EchoRequest request;
708 EchoResponse response;
709 ClientContext context;
710
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800711 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800712 request.set_message("hello");
713 EXPECT_TRUE(stream->Write(request));
714 EXPECT_TRUE(stream->Write(request));
715 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800716 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800717 EXPECT_EQ(response.message(), "hellohello");
Yang Gaoc1a2c312015-06-16 10:59:46 -0700718 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800719}
720
yang-g88d5d522015-09-29 12:46:54 -0700721TEST_P(End2endTest, ResponseStream) {
722 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800723 EchoRequest request;
724 EchoResponse response;
725 ClientContext context;
726 request.set_message("hello");
727
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800728 auto stream = stub_->ResponseStream(&context, request);
nnoble0c475f02014-12-05 15:37:39 -0800729 EXPECT_TRUE(stream->Read(&response));
730 EXPECT_EQ(response.message(), request.message() + "0");
731 EXPECT_TRUE(stream->Read(&response));
732 EXPECT_EQ(response.message(), request.message() + "1");
733 EXPECT_TRUE(stream->Read(&response));
734 EXPECT_EQ(response.message(), request.message() + "2");
735 EXPECT_FALSE(stream->Read(&response));
736
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800737 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700738 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800739}
740
yang-g88d5d522015-09-29 12:46:54 -0700741TEST_P(End2endTest, BidiStream) {
742 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800743 EchoRequest request;
744 EchoResponse response;
745 ClientContext context;
746 grpc::string msg("hello");
747
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800748 auto stream = stub_->BidiStream(&context);
nnoble0c475f02014-12-05 15:37:39 -0800749
750 request.set_message(msg + "0");
751 EXPECT_TRUE(stream->Write(request));
752 EXPECT_TRUE(stream->Read(&response));
753 EXPECT_EQ(response.message(), request.message());
754
755 request.set_message(msg + "1");
756 EXPECT_TRUE(stream->Write(request));
757 EXPECT_TRUE(stream->Read(&response));
758 EXPECT_EQ(response.message(), request.message());
759
760 request.set_message(msg + "2");
761 EXPECT_TRUE(stream->Write(request));
762 EXPECT_TRUE(stream->Read(&response));
763 EXPECT_EQ(response.message(), request.message());
764
765 stream->WritesDone();
766 EXPECT_FALSE(stream->Read(&response));
Craig Tillerca9a6372015-12-15 18:16:28 -0800767 EXPECT_FALSE(stream->Read(&response));
nnoble0c475f02014-12-05 15:37:39 -0800768
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800769 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700770 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800771}
772
773// Talk to the two services with the same name but different package names.
774// The two stubs are created on the same channel.
yang-g88d5d522015-09-29 12:46:54 -0700775TEST_P(End2endTest, DiffPackageServices) {
776 ResetStub();
yangg1456d152015-01-08 15:39:58 -0800777 EchoRequest request;
778 EchoResponse response;
779 request.set_message("Hello");
780
yangg1456d152015-01-08 15:39:58 -0800781 ClientContext context;
yang-g8b25f2a2015-07-21 23:54:36 -0700782 Status s = stub_->Echo(&context, request, &response);
yangg1456d152015-01-08 15:39:58 -0800783 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700784 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800785
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800786 std::unique_ptr<grpc::testing::duplicate::EchoTestService::Stub> dup_pkg_stub(
787 grpc::testing::duplicate::EchoTestService::NewStub(channel_));
yangg1456d152015-01-08 15:39:58 -0800788 ClientContext context2;
789 s = dup_pkg_stub->Echo(&context2, request, &response);
790 EXPECT_EQ("no package", response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700791 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800792}
793
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700794void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) {
Craig Tiller20b5fe92015-07-06 10:43:50 -0700795 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700796 gpr_time_from_micros(delay_us, GPR_TIMESPAN)));
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700797 while (!service->signal_client()) {
798 }
799 context->TryCancel();
800}
801
yang-ga89bf502015-11-17 14:19:17 -0800802TEST_P(End2endTest, CancelRpcBeforeStart) {
803 ResetStub();
804 EchoRequest request;
805 EchoResponse response;
806 ClientContext context;
807 request.set_message("hello");
808 context.TryCancel();
809 Status s = stub_->Echo(&context, request, &response);
810 EXPECT_EQ("", response.message());
811 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
812}
813
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700814// Client cancels request stream after sending two messages
yang-g88d5d522015-09-29 12:46:54 -0700815TEST_P(End2endTest, ClientCancelsRequestStream) {
816 ResetStub();
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700817 EchoRequest request;
818 EchoResponse response;
819 ClientContext context;
820 request.set_message("hello");
821
822 auto stream = stub_->RequestStream(&context, &response);
823 EXPECT_TRUE(stream->Write(request));
824 EXPECT_TRUE(stream->Write(request));
Yang Gaoc71a9d22015-05-04 00:22:12 -0700825
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700826 context.TryCancel();
827
828 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700829 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700830
Yang Gaoc71a9d22015-05-04 00:22:12 -0700831 EXPECT_EQ(response.message(), "");
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700832}
833
Abhishek Kumare41d0402015-04-17 14:12:33 -0700834// Client cancels server stream after sending some messages
yang-g88d5d522015-09-29 12:46:54 -0700835TEST_P(End2endTest, ClientCancelsResponseStream) {
836 ResetStub();
Abhishek Kumare41d0402015-04-17 14:12:33 -0700837 EchoRequest request;
838 EchoResponse response;
839 ClientContext context;
840 request.set_message("hello");
841
842 auto stream = stub_->ResponseStream(&context, request);
843
844 EXPECT_TRUE(stream->Read(&response));
845 EXPECT_EQ(response.message(), request.message() + "0");
846 EXPECT_TRUE(stream->Read(&response));
847 EXPECT_EQ(response.message(), request.message() + "1");
848
849 context.TryCancel();
850
851 // The cancellation races with responses, so there might be zero or
852 // one responses pending, read till failure
853
854 if (stream->Read(&response)) {
855 EXPECT_EQ(response.message(), request.message() + "2");
856 // Since we have cancelled, we expect the next attempt to read to fail
857 EXPECT_FALSE(stream->Read(&response));
858 }
859
860 Status s = stream->Finish();
Abhishek Kumar18298a72015-04-17 15:00:25 -0700861 // The final status could be either of CANCELLED or OK depending on
862 // who won the race.
Yang Gaoc1a2c312015-06-16 10:59:46 -0700863 EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumare41d0402015-04-17 14:12:33 -0700864}
865
Abhishek Kumar82a83312015-04-17 13:30:51 -0700866// Client cancels bidi stream after sending some messages
yang-g88d5d522015-09-29 12:46:54 -0700867TEST_P(End2endTest, ClientCancelsBidi) {
868 ResetStub();
Abhishek Kumar82a83312015-04-17 13:30:51 -0700869 EchoRequest request;
870 EchoResponse response;
871 ClientContext context;
872 grpc::string msg("hello");
873
874 auto stream = stub_->BidiStream(&context);
875
876 request.set_message(msg + "0");
877 EXPECT_TRUE(stream->Write(request));
878 EXPECT_TRUE(stream->Read(&response));
879 EXPECT_EQ(response.message(), request.message());
880
881 request.set_message(msg + "1");
882 EXPECT_TRUE(stream->Write(request));
883
884 context.TryCancel();
885
886 // The cancellation races with responses, so there might be zero or
887 // one responses pending, read till failure
888
889 if (stream->Read(&response)) {
890 EXPECT_EQ(response.message(), request.message());
891 // Since we have cancelled, we expect the next attempt to read to fail
892 EXPECT_FALSE(stream->Read(&response));
893 }
894
895 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700896 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumar82a83312015-04-17 13:30:51 -0700897}
898
yang-g88d5d522015-09-29 12:46:54 -0700899TEST_P(End2endTest, RpcMaxMessageSize) {
900 ResetStub();
Yang Gao3921c562015-04-30 16:07:06 -0700901 EchoRequest request;
902 EchoResponse response;
Yang Gaoc71a9d22015-05-04 00:22:12 -0700903 request.set_message(string(kMaxMessageSize_ * 2, 'a'));
Yang Gao3921c562015-04-30 16:07:06 -0700904
905 ClientContext context;
906 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700907 EXPECT_FALSE(s.ok());
Yang Gao3921c562015-04-30 16:07:06 -0700908}
Abhishek Kumar82a83312015-04-17 13:30:51 -0700909
yang-g88d5d522015-09-29 12:46:54 -0700910// Client sends 20 requests and the server returns CANCELLED status after
911// reading 10 requests.
912TEST_P(End2endTest, RequestStreamServerEarlyCancelTest) {
913 ResetStub();
914 EchoRequest request;
915 EchoResponse response;
916 ClientContext context;
917
918 context.AddMetadata(kServerCancelAfterReads, "10");
919 auto stream = stub_->RequestStream(&context, &response);
920 request.set_message("hello");
921 int send_messages = 20;
yang-gc0461032015-10-02 16:22:43 -0700922 while (send_messages > 10) {
yang-g88d5d522015-09-29 12:46:54 -0700923 EXPECT_TRUE(stream->Write(request));
924 send_messages--;
925 }
yang-gc0461032015-10-02 16:22:43 -0700926 while (send_messages > 0) {
927 stream->Write(request);
928 send_messages--;
929 }
yang-g88d5d522015-09-29 12:46:54 -0700930 stream->WritesDone();
931 Status s = stream->Finish();
932 EXPECT_EQ(s.error_code(), StatusCode::CANCELLED);
933}
934
yang-g88d5d522015-09-29 12:46:54 -0700935void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream,
936 gpr_event* ev) {
937 EchoResponse resp;
938 gpr_event_set(ev, (void*)1);
939 while (stream->Read(&resp)) {
940 gpr_log(GPR_INFO, "Read message");
941 }
942}
yang-g88d5d522015-09-29 12:46:54 -0700943
944// Run a Read and a WritesDone simultaneously.
945TEST_P(End2endTest, SimultaneousReadWritesDone) {
946 ResetStub();
947 ClientContext context;
948 gpr_event ev;
949 gpr_event_init(&ev);
950 auto stream = stub_->BidiStream(&context);
951 std::thread reader_thread(ReaderThreadFunc, stream.get(), &ev);
952 gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME));
953 stream->WritesDone();
Vijay Paibdfec2c2016-02-25 11:51:21 -0800954 reader_thread.join();
yang-g88d5d522015-09-29 12:46:54 -0700955 Status s = stream->Finish();
956 EXPECT_TRUE(s.ok());
yang-g88d5d522015-09-29 12:46:54 -0700957}
958
959TEST_P(End2endTest, ChannelState) {
960 ResetStub();
961 // Start IDLE
962 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(false));
963
964 // Did not ask to connect, no state change.
965 CompletionQueue cq;
966 std::chrono::system_clock::time_point deadline =
967 std::chrono::system_clock::now() + std::chrono::milliseconds(10);
968 channel_->NotifyOnStateChange(GRPC_CHANNEL_IDLE, deadline, &cq, NULL);
969 void* tag;
970 bool ok = true;
971 cq.Next(&tag, &ok);
972 EXPECT_FALSE(ok);
973
974 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true));
975 EXPECT_TRUE(channel_->WaitForStateChange(GRPC_CHANNEL_IDLE,
976 gpr_inf_future(GPR_CLOCK_REALTIME)));
yang-g0d557502015-10-01 11:30:12 -0700977 auto state = channel_->GetState(false);
978 EXPECT_TRUE(state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_READY);
yang-g88d5d522015-09-29 12:46:54 -0700979}
980
981// Takes 10s.
982TEST_P(End2endTest, ChannelStateTimeout) {
yang-g17197dd2016-02-19 00:04:22 -0800983 if (GetParam().credentials_type != kInsecureCredentialsType) {
yang-g88d5d522015-09-29 12:46:54 -0700984 return;
985 }
986 int port = grpc_pick_unused_port_or_die();
987 std::ostringstream server_address;
988 server_address << "127.0.0.1:" << port;
989 // Channel to non-existing server
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700990 auto channel =
991 CreateChannel(server_address.str(), InsecureChannelCredentials());
yang-g88d5d522015-09-29 12:46:54 -0700992 // Start IDLE
993 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel->GetState(true));
994
995 auto state = GRPC_CHANNEL_IDLE;
996 for (int i = 0; i < 10; i++) {
997 channel->WaitForStateChange(
998 state, std::chrono::system_clock::now() + std::chrono::seconds(1));
999 state = channel->GetState(false);
1000 }
1001}
1002
1003// Talking to a non-existing service.
1004TEST_P(End2endTest, NonExistingService) {
1005 ResetChannel();
murgatroid997c5befd2016-09-01 17:09:47 -07001006 std::unique_ptr<grpc::testing::UnimplementedEchoService::Stub> stub;
1007 stub = grpc::testing::UnimplementedEchoService::NewStub(channel_);
yang-g88d5d522015-09-29 12:46:54 -07001008
1009 EchoRequest request;
1010 EchoResponse response;
1011 request.set_message("Hello");
1012
1013 ClientContext context;
1014 Status s = stub->Unimplemented(&context, request, &response);
1015 EXPECT_EQ(StatusCode::UNIMPLEMENTED, s.error_code());
1016 EXPECT_EQ("", s.error_message());
1017}
1018
yang-g4c070082016-05-05 23:27:13 -07001019// Ask the server to send back a serialized proto in trailer.
1020// This is an example of setting error details.
1021TEST_P(End2endTest, BinaryTrailerTest) {
1022 ResetStub();
1023 EchoRequest request;
1024 EchoResponse response;
1025 ClientContext context;
1026
1027 request.mutable_param()->set_echo_metadata(true);
1028 DebugInfo* info = request.mutable_param()->mutable_debug_info();
1029 info->add_stack_entries("stack_entry_1");
1030 info->add_stack_entries("stack_entry_2");
1031 info->add_stack_entries("stack_entry_3");
1032 info->set_detail("detailed debug info");
1033 grpc::string expected_string = info->SerializeAsString();
1034 request.set_message("Hello");
1035
1036 Status s = stub_->Echo(&context, request, &response);
1037 EXPECT_FALSE(s.ok());
1038 auto trailers = context.GetServerTrailingMetadata();
yang-g39e71c32016-05-10 10:21:41 -07001039 EXPECT_EQ(1u, trailers.count(kDebugInfoTrailerKey));
yang-g4c070082016-05-05 23:27:13 -07001040 auto iter = trailers.find(kDebugInfoTrailerKey);
1041 EXPECT_EQ(expected_string, iter->second);
yang-g080528a2016-05-06 13:12:00 -07001042 // Parse the returned trailer into a DebugInfo proto.
1043 DebugInfo returned_info;
1044 EXPECT_TRUE(returned_info.ParseFromString(ToString(iter->second)));
yang-g4c070082016-05-05 23:27:13 -07001045}
1046
yang-g88d5d522015-09-29 12:46:54 -07001047//////////////////////////////////////////////////////////////////////////
1048// Test with and without a proxy.
1049class ProxyEnd2endTest : public End2endTest {
1050 protected:
1051};
1052
1053TEST_P(ProxyEnd2endTest, SimpleRpc) {
1054 ResetStub();
yang-gf8174ea2016-02-01 00:09:13 -08001055 SendRpc(stub_.get(), 1, false);
yang-g88d5d522015-09-29 12:46:54 -07001056}
1057
yang-g000aa452016-06-07 13:08:39 -07001058TEST_P(ProxyEnd2endTest, SimpleRpcWithEmptyMessages) {
1059 ResetStub();
1060 EchoRequest request;
1061 EchoResponse response;
1062
1063 ClientContext context;
1064 Status s = stub_->Echo(&context, request, &response);
1065 EXPECT_TRUE(s.ok());
1066}
1067
yang-g88d5d522015-09-29 12:46:54 -07001068TEST_P(ProxyEnd2endTest, MultipleRpcs) {
1069 ResetStub();
Vijay Paib0a6be22016-11-03 12:45:02 -07001070 std::vector<std::thread> threads;
yang-g88d5d522015-09-29 12:46:54 -07001071 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -07001072 threads.emplace_back(SendRpc, stub_.get(), 10, false);
yang-g88d5d522015-09-29 12:46:54 -07001073 }
1074 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -07001075 threads[i].join();
yang-g88d5d522015-09-29 12:46:54 -07001076 }
1077}
1078
1079// Set a 10us deadline and make sure proper error is returned.
1080TEST_P(ProxyEnd2endTest, RpcDeadlineExpires) {
1081 ResetStub();
1082 EchoRequest request;
1083 EchoResponse response;
1084 request.set_message("Hello");
Craig Tillerb0f275e2016-01-27 10:45:50 -08001085 request.mutable_param()->set_skip_cancelled_check(true);
yang-g88d5d522015-09-29 12:46:54 -07001086
1087 ClientContext context;
1088 std::chrono::system_clock::time_point deadline =
1089 std::chrono::system_clock::now() + std::chrono::microseconds(10);
1090 context.set_deadline(deadline);
1091 Status s = stub_->Echo(&context, request, &response);
1092 EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code());
1093}
1094
1095// Set a long but finite deadline.
1096TEST_P(ProxyEnd2endTest, RpcLongDeadline) {
1097 ResetStub();
1098 EchoRequest request;
1099 EchoResponse response;
1100 request.set_message("Hello");
1101
1102 ClientContext context;
1103 std::chrono::system_clock::time_point deadline =
1104 std::chrono::system_clock::now() + std::chrono::hours(1);
1105 context.set_deadline(deadline);
1106 Status s = stub_->Echo(&context, request, &response);
1107 EXPECT_EQ(response.message(), request.message());
1108 EXPECT_TRUE(s.ok());
1109}
1110
1111// Ask server to echo back the deadline it sees.
1112TEST_P(ProxyEnd2endTest, EchoDeadline) {
1113 ResetStub();
1114 EchoRequest request;
1115 EchoResponse response;
1116 request.set_message("Hello");
1117 request.mutable_param()->set_echo_deadline(true);
1118
1119 ClientContext context;
1120 std::chrono::system_clock::time_point deadline =
1121 std::chrono::system_clock::now() + std::chrono::seconds(100);
1122 context.set_deadline(deadline);
1123 Status s = stub_->Echo(&context, request, &response);
1124 EXPECT_EQ(response.message(), request.message());
1125 EXPECT_TRUE(s.ok());
1126 gpr_timespec sent_deadline;
1127 Timepoint2Timespec(deadline, &sent_deadline);
1128 // Allow 1 second error.
1129 EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1);
1130 EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1);
1131}
1132
1133// Ask server to echo back the deadline it sees. The rpc has no deadline.
1134TEST_P(ProxyEnd2endTest, EchoDeadlineForNoDeadlineRpc) {
1135 ResetStub();
1136 EchoRequest request;
1137 EchoResponse response;
1138 request.set_message("Hello");
1139 request.mutable_param()->set_echo_deadline(true);
1140
1141 ClientContext context;
1142 Status s = stub_->Echo(&context, request, &response);
1143 EXPECT_EQ(response.message(), request.message());
1144 EXPECT_TRUE(s.ok());
1145 EXPECT_EQ(response.param().request_deadline(),
1146 gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec);
1147}
1148
1149TEST_P(ProxyEnd2endTest, UnimplementedRpc) {
1150 ResetStub();
1151 EchoRequest request;
1152 EchoResponse response;
1153 request.set_message("Hello");
1154
1155 ClientContext context;
1156 Status s = stub_->Unimplemented(&context, request, &response);
1157 EXPECT_FALSE(s.ok());
1158 EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED);
1159 EXPECT_EQ(s.error_message(), "");
1160 EXPECT_EQ(response.message(), "");
1161}
1162
1163// Client cancels rpc after 10ms
1164TEST_P(ProxyEnd2endTest, ClientCancelsRpc) {
1165 ResetStub();
1166 EchoRequest request;
1167 EchoResponse response;
1168 request.set_message("Hello");
1169 const int kCancelDelayUs = 10 * 1000;
1170 request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs);
1171
1172 ClientContext context;
1173 std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_);
1174 Status s = stub_->Echo(&context, request, &response);
1175 cancel_thread.join();
1176 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
1177 EXPECT_EQ(s.error_message(), "Cancelled");
1178}
1179
1180// Server cancels rpc after 1ms
1181TEST_P(ProxyEnd2endTest, ServerCancelsRpc) {
1182 ResetStub();
1183 EchoRequest request;
1184 EchoResponse response;
1185 request.set_message("Hello");
1186 request.mutable_param()->set_server_cancel_after_us(1000);
1187
1188 ClientContext context;
1189 Status s = stub_->Echo(&context, request, &response);
1190 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
1191 EXPECT_TRUE(s.error_message().empty());
1192}
1193
1194// Make the response larger than the flow control window.
1195TEST_P(ProxyEnd2endTest, HugeResponse) {
1196 ResetStub();
1197 EchoRequest request;
1198 EchoResponse response;
1199 request.set_message("huge response");
1200 const size_t kResponseSize = 1024 * (1024 + 10);
1201 request.mutable_param()->set_response_message_length(kResponseSize);
1202
1203 ClientContext context;
Craig Tiller6c8619b2016-07-07 10:41:48 -07001204 std::chrono::system_clock::time_point deadline =
1205 std::chrono::system_clock::now() + std::chrono::seconds(20);
1206 context.set_deadline(deadline);
yang-g88d5d522015-09-29 12:46:54 -07001207 Status s = stub_->Echo(&context, request, &response);
1208 EXPECT_EQ(kResponseSize, response.message().size());
1209 EXPECT_TRUE(s.ok());
1210}
1211
1212TEST_P(ProxyEnd2endTest, Peer) {
1213 ResetStub();
1214 EchoRequest request;
1215 EchoResponse response;
1216 request.set_message("hello");
1217 request.mutable_param()->set_echo_peer(true);
1218
1219 ClientContext context;
1220 Status s = stub_->Echo(&context, request, &response);
1221 EXPECT_EQ(response.message(), request.message());
1222 EXPECT_TRUE(s.ok());
1223 EXPECT_TRUE(CheckIsLocalhost(response.param().peer()));
1224 EXPECT_TRUE(CheckIsLocalhost(context.peer()));
1225}
1226
1227//////////////////////////////////////////////////////////////////////////
1228class SecureEnd2endTest : public End2endTest {
1229 protected:
1230 SecureEnd2endTest() {
1231 GPR_ASSERT(!GetParam().use_proxy);
yang-g17197dd2016-02-19 00:04:22 -08001232 GPR_ASSERT(GetParam().credentials_type != kInsecureCredentialsType);
yang-g88d5d522015-09-29 12:46:54 -07001233 }
1234};
1235
1236TEST_P(SecureEnd2endTest, SimpleRpcWithHost) {
1237 ResetStub();
1238
1239 EchoRequest request;
1240 EchoResponse response;
1241 request.set_message("Hello");
1242
1243 ClientContext context;
1244 context.set_authority("foo.test.youtube.com");
1245 Status s = stub_->Echo(&context, request, &response);
1246 EXPECT_EQ(response.message(), request.message());
1247 EXPECT_TRUE(response.has_param());
1248 EXPECT_EQ("special", response.param().host());
1249 EXPECT_TRUE(s.ok());
1250}
1251
yang-ge21908f2015-08-25 13:47:51 -07001252bool MetadataContains(
1253 const std::multimap<grpc::string_ref, grpc::string_ref>& metadata,
1254 const grpc::string& key, const grpc::string& value) {
Yang Gao26a49122015-05-15 17:02:56 -07001255 int count = 0;
1256
yang-ge21908f2015-08-25 13:47:51 -07001257 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator iter =
Yang Gao26a49122015-05-15 17:02:56 -07001258 metadata.begin();
1259 iter != metadata.end(); ++iter) {
yang-ge21908f2015-08-25 13:47:51 -07001260 if (ToString(iter->first) == key && ToString(iter->second) == value) {
Yang Gao26a49122015-05-15 17:02:56 -07001261 count++;
1262 }
1263 }
1264 return count == 1;
1265}
1266
yang-g88d5d522015-09-29 12:46:54 -07001267TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorSuccess) {
1268 auto* processor = new TestAuthMetadataProcessor(true);
1269 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1270 ResetStub();
1271 EchoRequest request;
1272 EchoResponse response;
1273 ClientContext context;
1274 context.set_credentials(processor->GetCompatibleClientCreds());
1275 request.set_message("Hello");
1276 request.mutable_param()->set_echo_metadata(true);
1277 request.mutable_param()->set_expected_client_identity(
1278 TestAuthMetadataProcessor::kGoodGuy);
Dan Bornf2f7d572016-03-03 17:26:12 -08001279 request.mutable_param()->set_expected_transport_security_type(
1280 GetParam().credentials_type);
yang-g88d5d522015-09-29 12:46:54 -07001281
1282 Status s = stub_->Echo(&context, request, &response);
1283 EXPECT_EQ(request.message(), response.message());
1284 EXPECT_TRUE(s.ok());
1285
1286 // Metadata should have been consumed by the processor.
1287 EXPECT_FALSE(MetadataContains(
1288 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1289 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1290}
1291
1292TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorFailure) {
1293 auto* processor = new TestAuthMetadataProcessor(true);
1294 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1295 ResetStub();
1296 EchoRequest request;
1297 EchoResponse response;
1298 ClientContext context;
1299 context.set_credentials(processor->GetIncompatibleClientCreds());
1300 request.set_message("Hello");
1301
1302 Status s = stub_->Echo(&context, request, &response);
1303 EXPECT_FALSE(s.ok());
1304 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1305}
Craig Tiller95beab22017-01-12 13:41:52 -08001306
yang-g88d5d522015-09-29 12:46:54 -07001307TEST_P(SecureEnd2endTest, SetPerCallCredentials) {
1308 ResetStub();
Yang Gaoa8938922015-05-14 11:51:07 -07001309 EchoRequest request;
1310 EchoResponse response;
1311 ClientContext context;
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001312 std::shared_ptr<CallCredentials> creds =
Julien Boeuf510a9202015-08-25 21:51:07 -07001313 GoogleIAMCredentials("fake_token", "fake_selector");
Yang Gaoa8938922015-05-14 11:51:07 -07001314 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -07001315 request.set_message("Hello");
1316 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -07001317
1318 Status s = stub_->Echo(&context, request, &response);
Yang Gaoa8938922015-05-14 11:51:07 -07001319 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -07001320 EXPECT_TRUE(s.ok());
Yang Gao26a49122015-05-15 17:02:56 -07001321 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1322 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1323 "fake_token"));
1324 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1325 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1326 "fake_selector"));
Yang Gaoa8938922015-05-14 11:51:07 -07001327}
1328
yang-g88d5d522015-09-29 12:46:54 -07001329TEST_P(SecureEnd2endTest, OverridePerCallCredentials) {
1330 ResetStub();
Yang Gaoa8938922015-05-14 11:51:07 -07001331 EchoRequest request;
1332 EchoResponse response;
1333 ClientContext context;
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001334 std::shared_ptr<CallCredentials> creds1 =
Julien Boeuf510a9202015-08-25 21:51:07 -07001335 GoogleIAMCredentials("fake_token1", "fake_selector1");
Yang Gaoa8938922015-05-14 11:51:07 -07001336 context.set_credentials(creds1);
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001337 std::shared_ptr<CallCredentials> creds2 =
Julien Boeuf510a9202015-08-25 21:51:07 -07001338 GoogleIAMCredentials("fake_token2", "fake_selector2");
Yang Gaoa8938922015-05-14 11:51:07 -07001339 context.set_credentials(creds2);
Yang Gao26a49122015-05-15 17:02:56 -07001340 request.set_message("Hello");
1341 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -07001342
1343 Status s = stub_->Echo(&context, request, &response);
Yang Gao26a49122015-05-15 17:02:56 -07001344 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1345 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1346 "fake_token2"));
1347 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1348 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1349 "fake_selector2"));
Yang Gaob57f72d2015-05-17 21:54:54 -07001350 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
1351 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1352 "fake_token1"));
1353 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
1354 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1355 "fake_selector1"));
Yang Gaoa8938922015-05-14 11:51:07 -07001356 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -07001357 EXPECT_TRUE(s.ok());
Yang Gaoa8938922015-05-14 11:51:07 -07001358}
1359
yang-gc580af32016-09-15 15:28:38 -07001360TEST_P(SecureEnd2endTest, AuthMetadataPluginKeyFailure) {
1361 ResetStub();
1362 EchoRequest request;
1363 EchoResponse response;
1364 ClientContext context;
1365 context.set_credentials(
1366 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1367 new TestMetadataCredentialsPlugin(
1368 TestMetadataCredentialsPlugin::kBadMetadataKey,
1369 "Does not matter, will fail the key is invalid.", false, true))));
1370 request.set_message("Hello");
1371
1372 Status s = stub_->Echo(&context, request, &response);
1373 EXPECT_FALSE(s.ok());
1374 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1375}
1376
yang-g4b4571a2016-09-15 23:01:09 -07001377TEST_P(SecureEnd2endTest, AuthMetadataPluginValueFailure) {
1378 ResetStub();
1379 EchoRequest request;
1380 EchoResponse response;
1381 ClientContext context;
1382 context.set_credentials(
1383 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1384 new TestMetadataCredentialsPlugin(
1385 TestMetadataCredentialsPlugin::kGoodMetadataKey,
yang-gd5fba282016-09-16 10:29:16 -07001386 "With illegal \n value.", false, true))));
yang-g4b4571a2016-09-15 23:01:09 -07001387 request.set_message("Hello");
1388
1389 Status s = stub_->Echo(&context, request, &response);
1390 EXPECT_FALSE(s.ok());
1391 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1392}
1393
yang-g88d5d522015-09-29 12:46:54 -07001394TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginFailure) {
1395 ResetStub();
Julien Boeuf1928d492015-09-15 15:20:11 -07001396 EchoRequest request;
1397 EchoResponse response;
1398 ClientContext context;
1399 context.set_credentials(
1400 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1401 new TestMetadataCredentialsPlugin(
yang-gc580af32016-09-15 15:28:38 -07001402 TestMetadataCredentialsPlugin::kGoodMetadataKey,
Julien Boeuf1928d492015-09-15 15:20:11 -07001403 "Does not matter, will fail anyway (see 3rd param)", false,
1404 false))));
1405 request.set_message("Hello");
1406
1407 Status s = stub_->Echo(&context, request, &response);
1408 EXPECT_FALSE(s.ok());
1409 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
Julien Boeuf38c0cde2016-06-06 14:46:08 +02001410 EXPECT_EQ(s.error_message(), kTestCredsPluginErrorMsg);
Julien Boeuf1928d492015-09-15 15:20:11 -07001411}
1412
yang-g88d5d522015-09-29 12:46:54 -07001413TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorSuccess) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001414 auto* processor = new TestAuthMetadataProcessor(false);
1415 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
yang-g88d5d522015-09-29 12:46:54 -07001416 ResetStub();
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001417 EchoRequest request;
1418 EchoResponse response;
1419 ClientContext context;
1420 context.set_credentials(processor->GetCompatibleClientCreds());
1421 request.set_message("Hello");
1422 request.mutable_param()->set_echo_metadata(true);
1423 request.mutable_param()->set_expected_client_identity(
1424 TestAuthMetadataProcessor::kGoodGuy);
Dan Bornf2f7d572016-03-03 17:26:12 -08001425 request.mutable_param()->set_expected_transport_security_type(
1426 GetParam().credentials_type);
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001427
1428 Status s = stub_->Echo(&context, request, &response);
1429 EXPECT_EQ(request.message(), response.message());
1430 EXPECT_TRUE(s.ok());
1431
1432 // Metadata should have been consumed by the processor.
1433 EXPECT_FALSE(MetadataContains(
1434 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1435 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1436}
1437
yang-g88d5d522015-09-29 12:46:54 -07001438TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorFailure) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001439 auto* processor = new TestAuthMetadataProcessor(false);
1440 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
yang-g88d5d522015-09-29 12:46:54 -07001441 ResetStub();
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001442 EchoRequest request;
1443 EchoResponse response;
1444 ClientContext context;
1445 context.set_credentials(processor->GetIncompatibleClientCreds());
1446 request.set_message("Hello");
1447
1448 Status s = stub_->Echo(&context, request, &response);
1449 EXPECT_FALSE(s.ok());
1450 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1451}
1452
yang-g88d5d522015-09-29 12:46:54 -07001453TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginFailure) {
1454 ResetStub();
Julien Boeuf1928d492015-09-15 15:20:11 -07001455 EchoRequest request;
1456 EchoResponse response;
1457 ClientContext context;
1458 context.set_credentials(
1459 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1460 new TestMetadataCredentialsPlugin(
yang-gc580af32016-09-15 15:28:38 -07001461 TestMetadataCredentialsPlugin::kGoodMetadataKey,
Julien Boeuf1928d492015-09-15 15:20:11 -07001462 "Does not matter, will fail anyway (see 3rd param)", true,
1463 false))));
1464 request.set_message("Hello");
1465
1466 Status s = stub_->Echo(&context, request, &response);
1467 EXPECT_FALSE(s.ok());
1468 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
Julien Boeuf38c0cde2016-06-06 14:46:08 +02001469 EXPECT_EQ(s.error_message(), kTestCredsPluginErrorMsg);
Julien Boeuf1928d492015-09-15 15:20:11 -07001470}
1471
yang-g88d5d522015-09-29 12:46:54 -07001472TEST_P(SecureEnd2endTest, ClientAuthContext) {
1473 ResetStub();
yang-gc4eef2e2015-07-06 23:26:58 -07001474 EchoRequest request;
1475 EchoResponse response;
1476 request.set_message("Hello");
Dan Bornf2f7d572016-03-03 17:26:12 -08001477 request.mutable_param()->set_check_auth_context(GetParam().credentials_type ==
1478 kTlsCredentialsType);
1479 request.mutable_param()->set_expected_transport_security_type(
1480 GetParam().credentials_type);
yang-gc4eef2e2015-07-06 23:26:58 -07001481 ClientContext context;
1482 Status s = stub_->Echo(&context, request, &response);
1483 EXPECT_EQ(response.message(), request.message());
1484 EXPECT_TRUE(s.ok());
1485
yang-g8b25f2a2015-07-21 23:54:36 -07001486 std::shared_ptr<const AuthContext> auth_ctx = context.auth_context();
Dan Bornf2f7d572016-03-03 17:26:12 -08001487 std::vector<grpc::string_ref> tst =
yang-g8b25f2a2015-07-21 23:54:36 -07001488 auth_ctx->FindPropertyValues("transport_security_type");
Robbie Shade820c1f32016-06-23 14:31:28 -04001489 ASSERT_EQ(1u, tst.size());
Dan Bornf2f7d572016-03-03 17:26:12 -08001490 EXPECT_EQ(GetParam().credentials_type, ToString(tst[0]));
1491 if (GetParam().credentials_type == kTlsCredentialsType) {
1492 EXPECT_EQ("x509_subject_alternative_name",
1493 auth_ctx->GetPeerIdentityPropertyName());
Paul Querna47d841d2016-03-10 11:19:17 -08001494 EXPECT_EQ(4u, auth_ctx->GetPeerIdentity().size());
Dan Bornf2f7d572016-03-03 17:26:12 -08001495 EXPECT_EQ("*.test.google.fr", ToString(auth_ctx->GetPeerIdentity()[0]));
1496 EXPECT_EQ("waterzooi.test.google.be",
1497 ToString(auth_ctx->GetPeerIdentity()[1]));
1498 EXPECT_EQ("*.test.youtube.com", ToString(auth_ctx->GetPeerIdentity()[2]));
Paul Querna47d841d2016-03-10 11:19:17 -08001499 EXPECT_EQ("192.168.1.3", ToString(auth_ctx->GetPeerIdentity()[3]));
Dan Bornf2f7d572016-03-03 17:26:12 -08001500 }
yang-gc4eef2e2015-07-06 23:26:58 -07001501}
1502
Craig Tiller20afa3d2016-10-17 14:52:14 -07001503class ResourceQuotaEnd2endTest : public End2endTest {
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001504 public:
Craig Tiller20afa3d2016-10-17 14:52:14 -07001505 ResourceQuotaEnd2endTest()
1506 : server_resource_quota_("server_resource_quota") {}
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001507
Vijay Paic0b2acb2016-11-01 16:31:56 -07001508 virtual void ConfigureServerBuilder(ServerBuilder* builder) override {
Craig Tiller20afa3d2016-10-17 14:52:14 -07001509 builder->SetResourceQuota(server_resource_quota_);
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001510 }
1511
1512 private:
Craig Tiller20afa3d2016-10-17 14:52:14 -07001513 ResourceQuota server_resource_quota_;
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001514};
1515
Craig Tiller20afa3d2016-10-17 14:52:14 -07001516TEST_P(ResourceQuotaEnd2endTest, SimpleRequest) {
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001517 ResetStub();
1518
1519 EchoRequest request;
1520 EchoResponse response;
1521 request.set_message("Hello");
1522
1523 ClientContext context;
1524 Status s = stub_->Echo(&context, request, &response);
1525 EXPECT_EQ(response.message(), request.message());
1526 EXPECT_TRUE(s.ok());
1527}
1528
yang-g4c8aed32016-02-19 00:19:39 -08001529std::vector<TestScenario> CreateTestScenarios(bool use_proxy,
1530 bool test_insecure,
1531 bool test_secure) {
1532 std::vector<TestScenario> scenarios;
1533 std::vector<grpc::string> credentials_types;
1534 if (test_secure) {
yang-gcc591022017-01-11 11:10:43 -08001535 credentials_types =
1536 GetCredentialsProvider()->GetSecureCredentialsTypeList();
yang-g4c8aed32016-02-19 00:19:39 -08001537 }
1538 if (test_insecure) {
yang-gcc591022017-01-11 11:10:43 -08001539 // Only add insecure credentials type when it is registered with the
1540 // provider. User may create providers that do not have insecure.
1541 if (GetCredentialsProvider()->GetChannelCredentials(
1542 kInsecureCredentialsType, nullptr) != nullptr) {
1543 credentials_types.push_back(kInsecureCredentialsType);
1544 }
yang-g4c8aed32016-02-19 00:19:39 -08001545 }
yang-gcc591022017-01-11 11:10:43 -08001546 GPR_ASSERT(!credentials_types.empty());
yang-g4c8aed32016-02-19 00:19:39 -08001547 for (auto it = credentials_types.begin(); it != credentials_types.end();
1548 ++it) {
Vijay Pai679c75f2016-06-15 13:08:00 -07001549 scenarios.emplace_back(false, *it);
yang-g4c8aed32016-02-19 00:19:39 -08001550 if (use_proxy) {
Vijay Pai679c75f2016-06-15 13:08:00 -07001551 scenarios.emplace_back(true, *it);
yang-g4c8aed32016-02-19 00:19:39 -08001552 }
1553 }
1554 return scenarios;
1555}
yang-g6f30dec2015-07-22 23:11:56 -07001556
yang-g4c8aed32016-02-19 00:19:39 -08001557INSTANTIATE_TEST_CASE_P(End2end, End2endTest,
1558 ::testing::ValuesIn(CreateTestScenarios(false, true,
1559 true)));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001560
yang-g4c8aed32016-02-19 00:19:39 -08001561INSTANTIATE_TEST_CASE_P(End2endServerTryCancel, End2endServerTryCancelTest,
1562 ::testing::ValuesIn(CreateTestScenarios(false, true,
yang-gcc591022017-01-11 11:10:43 -08001563 true)));
yang-g4c8aed32016-02-19 00:19:39 -08001564
1565INSTANTIATE_TEST_CASE_P(ProxyEnd2end, ProxyEnd2endTest,
1566 ::testing::ValuesIn(CreateTestScenarios(true, true,
1567 true)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001568
yang-g88d5d522015-09-29 12:46:54 -07001569INSTANTIATE_TEST_CASE_P(SecureEnd2end, SecureEnd2endTest,
yang-g4c8aed32016-02-19 00:19:39 -08001570 ::testing::ValuesIn(CreateTestScenarios(false, false,
1571 true)));
yang-g88d5d522015-09-29 12:46:54 -07001572
Craig Tiller20afa3d2016-10-17 14:52:14 -07001573INSTANTIATE_TEST_CASE_P(ResourceQuotaEnd2end, ResourceQuotaEnd2endTest,
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001574 ::testing::ValuesIn(CreateTestScenarios(false, true,
1575 true)));
1576
yang-g8ab38362015-07-31 14:05:33 -07001577} // namespace
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001578} // namespace testing
1579} // namespace grpc
1580
1581int main(int argc, char** argv) {
1582 grpc_test_init(argc, argv);
1583 ::testing::InitGoogleTest(&argc, argv);
1584 return RUN_ALL_TESTS();
David Garcia Quintas2bf574f2016-01-14 15:27:08 -08001585}