blob: 9bb892c694bbd809b97eb80e51627b91f2dbea60 [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) {}
yang-g88d5d522015-09-29 12:46:54 -0700212 void Log() const {
yang-g7d2a3e12016-02-18 15:41:56 -0800213 gpr_log(GPR_INFO, "Scenario: proxy %d, credentials %s", use_proxy,
yang-g17197dd2016-02-19 00:04:22 -0800214 credentials_type.c_str());
yang-g88d5d522015-09-29 12:46:54 -0700215 }
216 bool use_proxy;
Vijay Pai679c75f2016-06-15 13:08:00 -0700217 // Although the below grpc::string is logically const, we can't declare
218 // them const because of a limitation in the way old compilers (e.g., gcc-4.4)
219 // manage vector insertion using a copy constructor
220 grpc::string credentials_type;
yang-g88d5d522015-09-29 12:46:54 -0700221};
222
223class End2endTest : public ::testing::TestWithParam<TestScenario> {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800224 protected:
Vijay Pai181ef452015-07-14 13:52:48 -0700225 End2endTest()
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700226 : is_server_started_(false),
227 kMaxMessageSize_(8192),
yang-g88d5d522015-09-29 12:46:54 -0700228 special_service_("special") {
229 GetParam().Log();
230 }
Craig Tiller7418d012015-02-11 15:25:03 -0800231
Vijay Paic0b2acb2016-11-01 16:31:56 -0700232 void TearDown() override {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700233 if (is_server_started_) {
234 server_->Shutdown();
235 if (proxy_server_) proxy_server_->Shutdown();
236 }
237 }
238
239 void StartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) {
Craig Tiller35e39712015-01-12 16:41:24 -0800240 int port = grpc_pick_unused_port_or_die();
yang-gd7ead692015-07-30 10:57:45 -0700241 server_address_ << "127.0.0.1:" << port;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800242 // Setup server
243 ServerBuilder builder;
Craig Tillerdb1a5cc2016-09-28 14:22:12 -0700244 ConfigureServerBuilder(&builder);
yang-g7d2a3e12016-02-18 15:41:56 -0800245 auto server_creds = GetServerCredentials(GetParam().credentials_type);
yang-g17197dd2016-02-19 00:04:22 -0800246 if (GetParam().credentials_type != kInsecureCredentialsType) {
yang-g88d5d522015-09-29 12:46:54 -0700247 server_creds->SetAuthMetadataProcessor(processor);
248 }
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700249 builder.AddListeningPort(server_address_.str(), server_creds);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800250 builder.RegisterService(&service_);
yang-g8b25f2a2015-07-21 23:54:36 -0700251 builder.RegisterService("foo.test.youtube.com", &special_service_);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800252 builder.RegisterService(&dup_pkg_service_);
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700253
Sree Kuchibhotlac37a8a52016-10-13 15:40:15 -0700254 builder.SetSyncServerOption(ServerBuilder::SyncServerOption::NUM_CQS, 4);
255 builder.SetSyncServerOption(
256 ServerBuilder::SyncServerOption::CQ_TIMEOUT_MSEC, 10);
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700257
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800258 server_ = builder.BuildAndStart();
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700259 is_server_started_ = true;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700260 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800261
Craig Tillerdb1a5cc2016-09-28 14:22:12 -0700262 virtual void ConfigureServerBuilder(ServerBuilder* builder) {
263 builder->SetMaxMessageSize(
264 kMaxMessageSize_); // For testing max message size.
265 }
266
yang-g9b7757d2015-08-13 11:15:53 -0700267 void ResetChannel() {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700268 if (!is_server_started_) {
269 StartServer(std::shared_ptr<AuthMetadataProcessor>());
270 }
271 EXPECT_TRUE(is_server_started_);
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700272 ChannelArguments args;
yang-g7d2a3e12016-02-18 15:41:56 -0800273 auto channel_creds =
274 GetChannelCredentials(GetParam().credentials_type, &args);
yang-gd59ad7e2016-02-10 12:42:53 -0800275 if (!user_agent_prefix_.empty()) {
276 args.SetUserAgentPrefix(user_agent_prefix_);
277 }
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700278 args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test");
yang-g88d5d522015-09-29 12:46:54 -0700279 channel_ = CreateCustomChannel(server_address_.str(), channel_creds, args);
yang-g9b7757d2015-08-13 11:15:53 -0700280 }
281
yang-g88d5d522015-09-29 12:46:54 -0700282 void ResetStub() {
yang-g9b7757d2015-08-13 11:15:53 -0700283 ResetChannel();
yang-g88d5d522015-09-29 12:46:54 -0700284 if (GetParam().use_proxy) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700285 proxy_service_.reset(new Proxy(channel_));
286 int port = grpc_pick_unused_port_or_die();
287 std::ostringstream proxyaddr;
288 proxyaddr << "localhost:" << port;
289 ServerBuilder builder;
290 builder.AddListeningPort(proxyaddr.str(), InsecureServerCredentials());
291 builder.RegisterService(proxy_service_.get());
Sree Kuchibhotlac37a8a52016-10-13 15:40:15 -0700292
293 builder.SetSyncServerOption(ServerBuilder::SyncServerOption::NUM_CQS, 4);
294 builder.SetSyncServerOption(
295 ServerBuilder::SyncServerOption::CQ_TIMEOUT_MSEC, 10);
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700296
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700297 proxy_server_ = builder.BuildAndStart();
298
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700299 channel_ = CreateChannel(proxyaddr.str(), InsecureChannelCredentials());
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700300 }
301
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800302 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
yangg1456d152015-01-08 15:39:58 -0800303 }
304
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700305 bool is_server_started_;
yang-g8c2be9f2015-08-19 16:28:09 -0700306 std::shared_ptr<Channel> channel_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800307 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800308 std::unique_ptr<Server> server_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700309 std::unique_ptr<Server> proxy_server_;
310 std::unique_ptr<Proxy> proxy_service_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800311 std::ostringstream server_address_;
Yang Gao3921c562015-04-30 16:07:06 -0700312 const int kMaxMessageSize_;
nnoble0c475f02014-12-05 15:37:39 -0800313 TestServiceImpl service_;
Craig Tiller822d2c72015-07-07 16:08:00 -0700314 TestServiceImpl special_service_;
yangg1456d152015-01-08 15:39:58 -0800315 TestServiceImplDupPkg dup_pkg_service_;
yang-gd59ad7e2016-02-10 12:42:53 -0800316 grpc::string user_agent_prefix_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800317};
318
yang-gf8174ea2016-02-01 00:09:13 -0800319static void SendRpc(grpc::testing::EchoTestService::Stub* stub, int num_rpcs,
320 bool with_binary_metadata) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800321 EchoRequest request;
322 EchoResponse response;
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700323 request.set_message("Hello hello hello hello");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800324
325 for (int i = 0; i < num_rpcs; ++i) {
326 ClientContext context;
yang-gf8174ea2016-02-01 00:09:13 -0800327 if (with_binary_metadata) {
328 char bytes[8] = {'\0', '\1', '\2', '\3', '\4', '\5', '\6', (char)i};
329 context.AddMetadata("custom-bin", grpc::string(bytes, 8));
330 }
Craig Tillerbf6abee2015-07-19 22:31:04 -0700331 context.set_compression_algorithm(GRPC_COMPRESS_GZIP);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800332 Status s = stub->Echo(&context, request, &response);
333 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700334 EXPECT_TRUE(s.ok());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800335 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800336}
337
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800338// This class is for testing scenarios where RPCs are cancelled on the server
339// by calling ServerContext::TryCancel()
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800340class End2endServerTryCancelTest : public End2endTest {
341 protected:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800342 // Helper for testing client-streaming RPCs which are cancelled on the server.
343 // Depending on the value of server_try_cancel parameter, this will test one
344 // of the following three scenarios:
345 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading
346 // any messages from the client
347 //
348 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading
349 // messages from the client
350 //
351 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all
352 // the messages from the client
353 //
354 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800355 void TestRequestStreamServerCancel(
356 ServerTryCancelRequestPhase server_try_cancel, int num_msgs_to_send) {
357 ResetStub();
358 EchoRequest request;
359 EchoResponse response;
360 ClientContext context;
361
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800362 // Send server_try_cancel value in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800363 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700364 grpc::to_string(server_try_cancel));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800365
366 auto stream = stub_->RequestStream(&context, &response);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800367
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800368 int num_msgs_sent = 0;
369 while (num_msgs_sent < num_msgs_to_send) {
370 request.set_message("hello");
371 if (!stream->Write(request)) {
372 break;
373 }
374 num_msgs_sent++;
375 }
376 gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800377
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800378 stream->WritesDone();
379 Status s = stream->Finish();
380
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800381 // At this point, we know for sure that RPC was cancelled by the server
382 // since we passed server_try_cancel value in the metadata. Depending on the
383 // value of server_try_cancel, the RPC might have been cancelled by the
384 // server at different stages. The following validates our expectations of
385 // number of messages sent in various cancellation scenarios:
386
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800387 switch (server_try_cancel) {
388 case CANCEL_BEFORE_PROCESSING:
389 case CANCEL_DURING_PROCESSING:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800390 // If the RPC is cancelled by server before / during messages from the
391 // client, it means that the client most likely did not get a chance to
392 // send all the messages it wanted to send. i.e num_msgs_sent <=
393 // num_msgs_to_send
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800394 EXPECT_LE(num_msgs_sent, num_msgs_to_send);
395 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800396
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800397 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800398 // If the RPC was cancelled after all messages were read by the server,
399 // the client did get a chance to send all its messages
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800400 EXPECT_EQ(num_msgs_sent, num_msgs_to_send);
401 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800402
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800403 default:
404 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
405 server_try_cancel);
406 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
407 server_try_cancel <= CANCEL_AFTER_PROCESSING);
408 break;
409 }
410
411 EXPECT_FALSE(s.ok());
412 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
413 }
414
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800415 // Helper for testing server-streaming RPCs which are cancelled on the server.
416 // Depending on the value of server_try_cancel parameter, this will test one
417 // of the following three scenarios:
418 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before writing
419 // any messages to the client
420 //
421 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while writing
422 // messages to the client
423 //
424 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after writing all
425 // the messages to the client
426 //
427 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800428 void TestResponseStreamServerCancel(
429 ServerTryCancelRequestPhase server_try_cancel) {
430 ResetStub();
431 EchoRequest request;
432 EchoResponse response;
433 ClientContext context;
434
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800435 // Send server_try_cancel in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800436 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700437 grpc::to_string(server_try_cancel));
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800438
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800439 request.set_message("hello");
440 auto stream = stub_->ResponseStream(&context, request);
441
442 int num_msgs_read = 0;
443 while (num_msgs_read < kNumResponseStreamsMsgs) {
444 if (!stream->Read(&response)) {
445 break;
446 }
447 EXPECT_EQ(response.message(),
Vijay Paia63271c2016-06-15 12:56:38 -0700448 request.message() + grpc::to_string(num_msgs_read));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800449 num_msgs_read++;
450 }
451 gpr_log(GPR_INFO, "Read %d messages", num_msgs_read);
452
453 Status s = stream->Finish();
454
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800455 // Depending on the value of server_try_cancel, the RPC might have been
456 // cancelled by the server at different stages. The following validates our
457 // expectations of number of messages read in various cancellation
458 // scenarios:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800459 switch (server_try_cancel) {
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800460 case CANCEL_BEFORE_PROCESSING:
461 // Server cancelled before sending any messages. Which means the client
462 // wouldn't have read any
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800463 EXPECT_EQ(num_msgs_read, 0);
464 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800465
466 case CANCEL_DURING_PROCESSING:
467 // Server cancelled while writing messages. Client must have read less
468 // than or equal to the expected number of messages
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800469 EXPECT_LE(num_msgs_read, kNumResponseStreamsMsgs);
470 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800471
472 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla8d543e82016-02-29 18:22:25 -0800473 // Even though the Server cancelled after writing all messages, the RPC
474 // may be cancelled before the Client got a chance to read all the
475 // messages.
476 EXPECT_LE(num_msgs_read, kNumResponseStreamsMsgs);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800477 break;
478
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800479 default: {
480 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
481 server_try_cancel);
482 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
483 server_try_cancel <= CANCEL_AFTER_PROCESSING);
484 break;
485 }
486 }
487
488 EXPECT_FALSE(s.ok());
489 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
490 }
491
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800492 // Helper for testing bidirectional-streaming RPCs which are cancelled on the
493 // server. Depending on the value of server_try_cancel parameter, this will
494 // test one of the following three scenarios:
495 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading/
496 // writing any messages from/to the client
497 //
498 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading/
499 // writing messages from/to the client
500 //
501 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading/writing
502 // all the messages from/to the client
503 //
504 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800505 void TestBidiStreamServerCancel(ServerTryCancelRequestPhase server_try_cancel,
506 int num_messages) {
507 ResetStub();
508 EchoRequest request;
509 EchoResponse response;
510 ClientContext context;
511
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800512 // Send server_try_cancel in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800513 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700514 grpc::to_string(server_try_cancel));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800515
516 auto stream = stub_->BidiStream(&context);
517
518 int num_msgs_read = 0;
519 int num_msgs_sent = 0;
520 while (num_msgs_sent < num_messages) {
Vijay Paia63271c2016-06-15 12:56:38 -0700521 request.set_message("hello " + grpc::to_string(num_msgs_sent));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800522 if (!stream->Write(request)) {
523 break;
524 }
525 num_msgs_sent++;
526
527 if (!stream->Read(&response)) {
528 break;
529 }
530 num_msgs_read++;
531
532 EXPECT_EQ(response.message(), request.message());
533 }
534 gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent);
535 gpr_log(GPR_INFO, "Read %d messages", num_msgs_read);
536
537 stream->WritesDone();
538 Status s = stream->Finish();
539
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800540 // Depending on the value of server_try_cancel, the RPC might have been
541 // cancelled by the server at different stages. The following validates our
542 // expectations of number of messages read in various cancellation
543 // scenarios:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800544 switch (server_try_cancel) {
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800545 case CANCEL_BEFORE_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800546 EXPECT_EQ(num_msgs_read, 0);
547 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800548
549 case CANCEL_DURING_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800550 EXPECT_LE(num_msgs_sent, num_messages);
551 EXPECT_LE(num_msgs_read, num_msgs_sent);
552 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800553
554 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800555 EXPECT_EQ(num_msgs_sent, num_messages);
Sree Kuchibhotla8d543e82016-02-29 18:22:25 -0800556
557 // The Server cancelled after reading the last message and after writing
558 // the message to the client. However, the RPC cancellation might have
559 // taken effect before the client actually read the response.
560 EXPECT_LE(num_msgs_read, num_msgs_sent);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800561 break;
562
563 default:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800564 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
565 server_try_cancel);
566 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
567 server_try_cancel <= CANCEL_AFTER_PROCESSING);
568 break;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800569 }
570
571 EXPECT_FALSE(s.ok());
572 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
573 }
574};
575
576TEST_P(End2endServerTryCancelTest, RequestEchoServerCancel) {
577 ResetStub();
578 EchoRequest request;
579 EchoResponse response;
580 ClientContext context;
581
582 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700583 grpc::to_string(CANCEL_BEFORE_PROCESSING));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800584 Status s = stub_->Echo(&context, request, &response);
585 EXPECT_FALSE(s.ok());
586 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
587}
588
589// Server to cancel before doing reading the request
590TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelBeforeReads) {
591 TestRequestStreamServerCancel(CANCEL_BEFORE_PROCESSING, 1);
592}
593
594// Server to cancel while reading a request from the stream in parallel
595TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelDuringRead) {
596 TestRequestStreamServerCancel(CANCEL_DURING_PROCESSING, 10);
597}
598
599// Server to cancel after reading all the requests but before returning to the
600// client
601TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelAfterReads) {
602 TestRequestStreamServerCancel(CANCEL_AFTER_PROCESSING, 4);
603}
604
605// Server to cancel before sending any response messages
606TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelBefore) {
607 TestResponseStreamServerCancel(CANCEL_BEFORE_PROCESSING);
608}
609
610// Server to cancel while writing a response to the stream in parallel
611TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelDuring) {
612 TestResponseStreamServerCancel(CANCEL_DURING_PROCESSING);
613}
614
615// Server to cancel after writing all the respones to the stream but before
616// returning to the client
617TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelAfter) {
618 TestResponseStreamServerCancel(CANCEL_AFTER_PROCESSING);
619}
620
621// Server to cancel before reading/writing any requests/responses on the stream
622TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelBefore) {
623 TestBidiStreamServerCancel(CANCEL_BEFORE_PROCESSING, 2);
624}
625
626// Server to cancel while reading/writing requests/responses on the stream in
627// parallel
628TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelDuring) {
629 TestBidiStreamServerCancel(CANCEL_DURING_PROCESSING, 10);
630}
631
632// Server to cancel after reading/writing all requests/responses on the stream
633// but before returning to the client
634TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelAfter) {
635 TestBidiStreamServerCancel(CANCEL_AFTER_PROCESSING, 5);
636}
637
yang-gd59ad7e2016-02-10 12:42:53 -0800638TEST_P(End2endTest, SimpleRpcWithCustomeUserAgentPrefix) {
639 user_agent_prefix_ = "custom_prefix";
640 ResetStub();
641 EchoRequest request;
642 EchoResponse response;
643 request.set_message("Hello hello hello hello");
644 request.mutable_param()->set_echo_metadata(true);
645
646 ClientContext context;
647 Status s = stub_->Echo(&context, request, &response);
648 EXPECT_EQ(response.message(), request.message());
649 EXPECT_TRUE(s.ok());
650 const auto& trailing_metadata = context.GetServerTrailingMetadata();
651 auto iter = trailing_metadata.find("user-agent");
652 EXPECT_TRUE(iter != trailing_metadata.end());
653 grpc::string expected_prefix = user_agent_prefix_ + " grpc-c++/";
Mark D. Rothe3a21002016-10-24 13:29:05 -0700654 EXPECT_TRUE(iter->second.starts_with(expected_prefix)) << iter->second;
yang-gd59ad7e2016-02-10 12:42:53 -0800655}
656
yang-gf8174ea2016-02-01 00:09:13 -0800657TEST_P(End2endTest, MultipleRpcsWithVariedBinaryMetadataValue) {
658 ResetStub();
Vijay Paib0a6be22016-11-03 12:45:02 -0700659 std::vector<std::thread> threads;
yang-gf8174ea2016-02-01 00:09:13 -0800660 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -0700661 threads.emplace_back(SendRpc, stub_.get(), 10, true);
yang-gf8174ea2016-02-01 00:09:13 -0800662 }
663 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -0700664 threads[i].join();
yang-gf8174ea2016-02-01 00:09:13 -0800665 }
666}
667
668TEST_P(End2endTest, MultipleRpcs) {
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, false);
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
yang-g88d5d522015-09-29 12:46:54 -0700679TEST_P(End2endTest, RequestStreamOneRequest) {
680 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800681 EchoRequest request;
682 EchoResponse response;
683 ClientContext context;
684
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800685 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800686 request.set_message("hello");
687 EXPECT_TRUE(stream->Write(request));
688 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800689 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800690 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700691 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800692}
693
yang-g88d5d522015-09-29 12:46:54 -0700694TEST_P(End2endTest, RequestStreamTwoRequests) {
695 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800696 EchoRequest request;
697 EchoResponse response;
698 ClientContext context;
699
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800700 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800701 request.set_message("hello");
702 EXPECT_TRUE(stream->Write(request));
703 EXPECT_TRUE(stream->Write(request));
704 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800705 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800706 EXPECT_EQ(response.message(), "hellohello");
Yang Gaoc1a2c312015-06-16 10:59:46 -0700707 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800708}
709
yang-g88d5d522015-09-29 12:46:54 -0700710TEST_P(End2endTest, ResponseStream) {
711 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800712 EchoRequest request;
713 EchoResponse response;
714 ClientContext context;
715 request.set_message("hello");
716
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800717 auto stream = stub_->ResponseStream(&context, request);
nnoble0c475f02014-12-05 15:37:39 -0800718 EXPECT_TRUE(stream->Read(&response));
719 EXPECT_EQ(response.message(), request.message() + "0");
720 EXPECT_TRUE(stream->Read(&response));
721 EXPECT_EQ(response.message(), request.message() + "1");
722 EXPECT_TRUE(stream->Read(&response));
723 EXPECT_EQ(response.message(), request.message() + "2");
724 EXPECT_FALSE(stream->Read(&response));
725
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800726 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700727 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800728}
729
yang-g88d5d522015-09-29 12:46:54 -0700730TEST_P(End2endTest, BidiStream) {
731 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800732 EchoRequest request;
733 EchoResponse response;
734 ClientContext context;
735 grpc::string msg("hello");
736
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800737 auto stream = stub_->BidiStream(&context);
nnoble0c475f02014-12-05 15:37:39 -0800738
739 request.set_message(msg + "0");
740 EXPECT_TRUE(stream->Write(request));
741 EXPECT_TRUE(stream->Read(&response));
742 EXPECT_EQ(response.message(), request.message());
743
744 request.set_message(msg + "1");
745 EXPECT_TRUE(stream->Write(request));
746 EXPECT_TRUE(stream->Read(&response));
747 EXPECT_EQ(response.message(), request.message());
748
749 request.set_message(msg + "2");
750 EXPECT_TRUE(stream->Write(request));
751 EXPECT_TRUE(stream->Read(&response));
752 EXPECT_EQ(response.message(), request.message());
753
754 stream->WritesDone();
755 EXPECT_FALSE(stream->Read(&response));
Craig Tillerca9a6372015-12-15 18:16:28 -0800756 EXPECT_FALSE(stream->Read(&response));
nnoble0c475f02014-12-05 15:37:39 -0800757
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800758 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700759 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800760}
761
762// Talk to the two services with the same name but different package names.
763// The two stubs are created on the same channel.
yang-g88d5d522015-09-29 12:46:54 -0700764TEST_P(End2endTest, DiffPackageServices) {
765 ResetStub();
yangg1456d152015-01-08 15:39:58 -0800766 EchoRequest request;
767 EchoResponse response;
768 request.set_message("Hello");
769
yangg1456d152015-01-08 15:39:58 -0800770 ClientContext context;
yang-g8b25f2a2015-07-21 23:54:36 -0700771 Status s = stub_->Echo(&context, request, &response);
yangg1456d152015-01-08 15:39:58 -0800772 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700773 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800774
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800775 std::unique_ptr<grpc::testing::duplicate::EchoTestService::Stub> dup_pkg_stub(
776 grpc::testing::duplicate::EchoTestService::NewStub(channel_));
yangg1456d152015-01-08 15:39:58 -0800777 ClientContext context2;
778 s = dup_pkg_stub->Echo(&context2, request, &response);
779 EXPECT_EQ("no package", response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700780 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800781}
782
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700783void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) {
Craig Tiller20b5fe92015-07-06 10:43:50 -0700784 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700785 gpr_time_from_micros(delay_us, GPR_TIMESPAN)));
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700786 while (!service->signal_client()) {
787 }
788 context->TryCancel();
789}
790
yang-ga89bf502015-11-17 14:19:17 -0800791TEST_P(End2endTest, CancelRpcBeforeStart) {
792 ResetStub();
793 EchoRequest request;
794 EchoResponse response;
795 ClientContext context;
796 request.set_message("hello");
797 context.TryCancel();
798 Status s = stub_->Echo(&context, request, &response);
799 EXPECT_EQ("", response.message());
800 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
801}
802
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700803// Client cancels request stream after sending two messages
yang-g88d5d522015-09-29 12:46:54 -0700804TEST_P(End2endTest, ClientCancelsRequestStream) {
805 ResetStub();
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700806 EchoRequest request;
807 EchoResponse response;
808 ClientContext context;
809 request.set_message("hello");
810
811 auto stream = stub_->RequestStream(&context, &response);
812 EXPECT_TRUE(stream->Write(request));
813 EXPECT_TRUE(stream->Write(request));
Yang Gaoc71a9d22015-05-04 00:22:12 -0700814
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700815 context.TryCancel();
816
817 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700818 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700819
Yang Gaoc71a9d22015-05-04 00:22:12 -0700820 EXPECT_EQ(response.message(), "");
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700821}
822
Abhishek Kumare41d0402015-04-17 14:12:33 -0700823// Client cancels server stream after sending some messages
yang-g88d5d522015-09-29 12:46:54 -0700824TEST_P(End2endTest, ClientCancelsResponseStream) {
825 ResetStub();
Abhishek Kumare41d0402015-04-17 14:12:33 -0700826 EchoRequest request;
827 EchoResponse response;
828 ClientContext context;
829 request.set_message("hello");
830
831 auto stream = stub_->ResponseStream(&context, request);
832
833 EXPECT_TRUE(stream->Read(&response));
834 EXPECT_EQ(response.message(), request.message() + "0");
835 EXPECT_TRUE(stream->Read(&response));
836 EXPECT_EQ(response.message(), request.message() + "1");
837
838 context.TryCancel();
839
840 // The cancellation races with responses, so there might be zero or
841 // one responses pending, read till failure
842
843 if (stream->Read(&response)) {
844 EXPECT_EQ(response.message(), request.message() + "2");
845 // Since we have cancelled, we expect the next attempt to read to fail
846 EXPECT_FALSE(stream->Read(&response));
847 }
848
849 Status s = stream->Finish();
Abhishek Kumar18298a72015-04-17 15:00:25 -0700850 // The final status could be either of CANCELLED or OK depending on
851 // who won the race.
Yang Gaoc1a2c312015-06-16 10:59:46 -0700852 EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumare41d0402015-04-17 14:12:33 -0700853}
854
Abhishek Kumar82a83312015-04-17 13:30:51 -0700855// Client cancels bidi stream after sending some messages
yang-g88d5d522015-09-29 12:46:54 -0700856TEST_P(End2endTest, ClientCancelsBidi) {
857 ResetStub();
Abhishek Kumar82a83312015-04-17 13:30:51 -0700858 EchoRequest request;
859 EchoResponse response;
860 ClientContext context;
861 grpc::string msg("hello");
862
863 auto stream = stub_->BidiStream(&context);
864
865 request.set_message(msg + "0");
866 EXPECT_TRUE(stream->Write(request));
867 EXPECT_TRUE(stream->Read(&response));
868 EXPECT_EQ(response.message(), request.message());
869
870 request.set_message(msg + "1");
871 EXPECT_TRUE(stream->Write(request));
872
873 context.TryCancel();
874
875 // The cancellation races with responses, so there might be zero or
876 // one responses pending, read till failure
877
878 if (stream->Read(&response)) {
879 EXPECT_EQ(response.message(), request.message());
880 // Since we have cancelled, we expect the next attempt to read to fail
881 EXPECT_FALSE(stream->Read(&response));
882 }
883
884 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700885 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumar82a83312015-04-17 13:30:51 -0700886}
887
yang-g88d5d522015-09-29 12:46:54 -0700888TEST_P(End2endTest, RpcMaxMessageSize) {
889 ResetStub();
Yang Gao3921c562015-04-30 16:07:06 -0700890 EchoRequest request;
891 EchoResponse response;
Yang Gaoc71a9d22015-05-04 00:22:12 -0700892 request.set_message(string(kMaxMessageSize_ * 2, 'a'));
Yang Gao3921c562015-04-30 16:07:06 -0700893
894 ClientContext context;
895 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700896 EXPECT_FALSE(s.ok());
Yang Gao3921c562015-04-30 16:07:06 -0700897}
Abhishek Kumar82a83312015-04-17 13:30:51 -0700898
yang-g88d5d522015-09-29 12:46:54 -0700899// Client sends 20 requests and the server returns CANCELLED status after
900// reading 10 requests.
901TEST_P(End2endTest, RequestStreamServerEarlyCancelTest) {
902 ResetStub();
903 EchoRequest request;
904 EchoResponse response;
905 ClientContext context;
906
907 context.AddMetadata(kServerCancelAfterReads, "10");
908 auto stream = stub_->RequestStream(&context, &response);
909 request.set_message("hello");
910 int send_messages = 20;
yang-gc0461032015-10-02 16:22:43 -0700911 while (send_messages > 10) {
yang-g88d5d522015-09-29 12:46:54 -0700912 EXPECT_TRUE(stream->Write(request));
913 send_messages--;
914 }
yang-gc0461032015-10-02 16:22:43 -0700915 while (send_messages > 0) {
916 stream->Write(request);
917 send_messages--;
918 }
yang-g88d5d522015-09-29 12:46:54 -0700919 stream->WritesDone();
920 Status s = stream->Finish();
921 EXPECT_EQ(s.error_code(), StatusCode::CANCELLED);
922}
923
yang-g88d5d522015-09-29 12:46:54 -0700924void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream,
925 gpr_event* ev) {
926 EchoResponse resp;
927 gpr_event_set(ev, (void*)1);
928 while (stream->Read(&resp)) {
929 gpr_log(GPR_INFO, "Read message");
930 }
931}
yang-g88d5d522015-09-29 12:46:54 -0700932
933// Run a Read and a WritesDone simultaneously.
934TEST_P(End2endTest, SimultaneousReadWritesDone) {
935 ResetStub();
936 ClientContext context;
937 gpr_event ev;
938 gpr_event_init(&ev);
939 auto stream = stub_->BidiStream(&context);
940 std::thread reader_thread(ReaderThreadFunc, stream.get(), &ev);
941 gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME));
942 stream->WritesDone();
Vijay Paibdfec2c2016-02-25 11:51:21 -0800943 reader_thread.join();
yang-g88d5d522015-09-29 12:46:54 -0700944 Status s = stream->Finish();
945 EXPECT_TRUE(s.ok());
yang-g88d5d522015-09-29 12:46:54 -0700946}
947
948TEST_P(End2endTest, ChannelState) {
949 ResetStub();
950 // Start IDLE
951 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(false));
952
953 // Did not ask to connect, no state change.
954 CompletionQueue cq;
955 std::chrono::system_clock::time_point deadline =
956 std::chrono::system_clock::now() + std::chrono::milliseconds(10);
957 channel_->NotifyOnStateChange(GRPC_CHANNEL_IDLE, deadline, &cq, NULL);
958 void* tag;
959 bool ok = true;
960 cq.Next(&tag, &ok);
961 EXPECT_FALSE(ok);
962
963 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true));
964 EXPECT_TRUE(channel_->WaitForStateChange(GRPC_CHANNEL_IDLE,
965 gpr_inf_future(GPR_CLOCK_REALTIME)));
yang-g0d557502015-10-01 11:30:12 -0700966 auto state = channel_->GetState(false);
967 EXPECT_TRUE(state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_READY);
yang-g88d5d522015-09-29 12:46:54 -0700968}
969
970// Takes 10s.
971TEST_P(End2endTest, ChannelStateTimeout) {
yang-g17197dd2016-02-19 00:04:22 -0800972 if (GetParam().credentials_type != kInsecureCredentialsType) {
yang-g88d5d522015-09-29 12:46:54 -0700973 return;
974 }
975 int port = grpc_pick_unused_port_or_die();
976 std::ostringstream server_address;
977 server_address << "127.0.0.1:" << port;
978 // Channel to non-existing server
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700979 auto channel =
980 CreateChannel(server_address.str(), InsecureChannelCredentials());
yang-g88d5d522015-09-29 12:46:54 -0700981 // Start IDLE
982 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel->GetState(true));
983
984 auto state = GRPC_CHANNEL_IDLE;
985 for (int i = 0; i < 10; i++) {
986 channel->WaitForStateChange(
987 state, std::chrono::system_clock::now() + std::chrono::seconds(1));
988 state = channel->GetState(false);
989 }
990}
991
992// Talking to a non-existing service.
993TEST_P(End2endTest, NonExistingService) {
994 ResetChannel();
murgatroid997c5befd2016-09-01 17:09:47 -0700995 std::unique_ptr<grpc::testing::UnimplementedEchoService::Stub> stub;
996 stub = grpc::testing::UnimplementedEchoService::NewStub(channel_);
yang-g88d5d522015-09-29 12:46:54 -0700997
998 EchoRequest request;
999 EchoResponse response;
1000 request.set_message("Hello");
1001
1002 ClientContext context;
1003 Status s = stub->Unimplemented(&context, request, &response);
1004 EXPECT_EQ(StatusCode::UNIMPLEMENTED, s.error_code());
1005 EXPECT_EQ("", s.error_message());
1006}
1007
yang-g4c070082016-05-05 23:27:13 -07001008// Ask the server to send back a serialized proto in trailer.
1009// This is an example of setting error details.
1010TEST_P(End2endTest, BinaryTrailerTest) {
1011 ResetStub();
1012 EchoRequest request;
1013 EchoResponse response;
1014 ClientContext context;
1015
1016 request.mutable_param()->set_echo_metadata(true);
1017 DebugInfo* info = request.mutable_param()->mutable_debug_info();
1018 info->add_stack_entries("stack_entry_1");
1019 info->add_stack_entries("stack_entry_2");
1020 info->add_stack_entries("stack_entry_3");
1021 info->set_detail("detailed debug info");
1022 grpc::string expected_string = info->SerializeAsString();
1023 request.set_message("Hello");
1024
1025 Status s = stub_->Echo(&context, request, &response);
1026 EXPECT_FALSE(s.ok());
1027 auto trailers = context.GetServerTrailingMetadata();
yang-g39e71c32016-05-10 10:21:41 -07001028 EXPECT_EQ(1u, trailers.count(kDebugInfoTrailerKey));
yang-g4c070082016-05-05 23:27:13 -07001029 auto iter = trailers.find(kDebugInfoTrailerKey);
1030 EXPECT_EQ(expected_string, iter->second);
yang-g080528a2016-05-06 13:12:00 -07001031 // Parse the returned trailer into a DebugInfo proto.
1032 DebugInfo returned_info;
1033 EXPECT_TRUE(returned_info.ParseFromString(ToString(iter->second)));
yang-g4c070082016-05-05 23:27:13 -07001034}
1035
yang-g88d5d522015-09-29 12:46:54 -07001036//////////////////////////////////////////////////////////////////////////
1037// Test with and without a proxy.
1038class ProxyEnd2endTest : public End2endTest {
1039 protected:
1040};
1041
1042TEST_P(ProxyEnd2endTest, SimpleRpc) {
1043 ResetStub();
yang-gf8174ea2016-02-01 00:09:13 -08001044 SendRpc(stub_.get(), 1, false);
yang-g88d5d522015-09-29 12:46:54 -07001045}
1046
yang-g000aa452016-06-07 13:08:39 -07001047TEST_P(ProxyEnd2endTest, SimpleRpcWithEmptyMessages) {
1048 ResetStub();
1049 EchoRequest request;
1050 EchoResponse response;
1051
1052 ClientContext context;
1053 Status s = stub_->Echo(&context, request, &response);
1054 EXPECT_TRUE(s.ok());
1055}
1056
yang-g88d5d522015-09-29 12:46:54 -07001057TEST_P(ProxyEnd2endTest, MultipleRpcs) {
1058 ResetStub();
Vijay Paib0a6be22016-11-03 12:45:02 -07001059 std::vector<std::thread> threads;
yang-g88d5d522015-09-29 12:46:54 -07001060 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -07001061 threads.emplace_back(SendRpc, stub_.get(), 10, false);
yang-g88d5d522015-09-29 12:46:54 -07001062 }
1063 for (int i = 0; i < 10; ++i) {
Vijay Paib0a6be22016-11-03 12:45:02 -07001064 threads[i].join();
yang-g88d5d522015-09-29 12:46:54 -07001065 }
1066}
1067
1068// Set a 10us deadline and make sure proper error is returned.
1069TEST_P(ProxyEnd2endTest, RpcDeadlineExpires) {
1070 ResetStub();
1071 EchoRequest request;
1072 EchoResponse response;
1073 request.set_message("Hello");
Craig Tillerb0f275e2016-01-27 10:45:50 -08001074 request.mutable_param()->set_skip_cancelled_check(true);
yang-g88d5d522015-09-29 12:46:54 -07001075
1076 ClientContext context;
1077 std::chrono::system_clock::time_point deadline =
1078 std::chrono::system_clock::now() + std::chrono::microseconds(10);
1079 context.set_deadline(deadline);
1080 Status s = stub_->Echo(&context, request, &response);
1081 EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code());
1082}
1083
1084// Set a long but finite deadline.
1085TEST_P(ProxyEnd2endTest, RpcLongDeadline) {
1086 ResetStub();
1087 EchoRequest request;
1088 EchoResponse response;
1089 request.set_message("Hello");
1090
1091 ClientContext context;
1092 std::chrono::system_clock::time_point deadline =
1093 std::chrono::system_clock::now() + std::chrono::hours(1);
1094 context.set_deadline(deadline);
1095 Status s = stub_->Echo(&context, request, &response);
1096 EXPECT_EQ(response.message(), request.message());
1097 EXPECT_TRUE(s.ok());
1098}
1099
1100// Ask server to echo back the deadline it sees.
1101TEST_P(ProxyEnd2endTest, EchoDeadline) {
1102 ResetStub();
1103 EchoRequest request;
1104 EchoResponse response;
1105 request.set_message("Hello");
1106 request.mutable_param()->set_echo_deadline(true);
1107
1108 ClientContext context;
1109 std::chrono::system_clock::time_point deadline =
1110 std::chrono::system_clock::now() + std::chrono::seconds(100);
1111 context.set_deadline(deadline);
1112 Status s = stub_->Echo(&context, request, &response);
1113 EXPECT_EQ(response.message(), request.message());
1114 EXPECT_TRUE(s.ok());
1115 gpr_timespec sent_deadline;
1116 Timepoint2Timespec(deadline, &sent_deadline);
1117 // Allow 1 second error.
1118 EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1);
1119 EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1);
1120}
1121
1122// Ask server to echo back the deadline it sees. The rpc has no deadline.
1123TEST_P(ProxyEnd2endTest, EchoDeadlineForNoDeadlineRpc) {
1124 ResetStub();
1125 EchoRequest request;
1126 EchoResponse response;
1127 request.set_message("Hello");
1128 request.mutable_param()->set_echo_deadline(true);
1129
1130 ClientContext context;
1131 Status s = stub_->Echo(&context, request, &response);
1132 EXPECT_EQ(response.message(), request.message());
1133 EXPECT_TRUE(s.ok());
1134 EXPECT_EQ(response.param().request_deadline(),
1135 gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec);
1136}
1137
1138TEST_P(ProxyEnd2endTest, UnimplementedRpc) {
1139 ResetStub();
1140 EchoRequest request;
1141 EchoResponse response;
1142 request.set_message("Hello");
1143
1144 ClientContext context;
1145 Status s = stub_->Unimplemented(&context, request, &response);
1146 EXPECT_FALSE(s.ok());
1147 EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED);
1148 EXPECT_EQ(s.error_message(), "");
1149 EXPECT_EQ(response.message(), "");
1150}
1151
1152// Client cancels rpc after 10ms
1153TEST_P(ProxyEnd2endTest, ClientCancelsRpc) {
1154 ResetStub();
1155 EchoRequest request;
1156 EchoResponse response;
1157 request.set_message("Hello");
1158 const int kCancelDelayUs = 10 * 1000;
1159 request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs);
1160
1161 ClientContext context;
1162 std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_);
1163 Status s = stub_->Echo(&context, request, &response);
1164 cancel_thread.join();
1165 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
1166 EXPECT_EQ(s.error_message(), "Cancelled");
1167}
1168
1169// Server cancels rpc after 1ms
1170TEST_P(ProxyEnd2endTest, ServerCancelsRpc) {
1171 ResetStub();
1172 EchoRequest request;
1173 EchoResponse response;
1174 request.set_message("Hello");
1175 request.mutable_param()->set_server_cancel_after_us(1000);
1176
1177 ClientContext context;
1178 Status s = stub_->Echo(&context, request, &response);
1179 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
1180 EXPECT_TRUE(s.error_message().empty());
1181}
1182
1183// Make the response larger than the flow control window.
1184TEST_P(ProxyEnd2endTest, HugeResponse) {
1185 ResetStub();
1186 EchoRequest request;
1187 EchoResponse response;
1188 request.set_message("huge response");
1189 const size_t kResponseSize = 1024 * (1024 + 10);
1190 request.mutable_param()->set_response_message_length(kResponseSize);
1191
1192 ClientContext context;
Craig Tiller6c8619b2016-07-07 10:41:48 -07001193 std::chrono::system_clock::time_point deadline =
1194 std::chrono::system_clock::now() + std::chrono::seconds(20);
1195 context.set_deadline(deadline);
yang-g88d5d522015-09-29 12:46:54 -07001196 Status s = stub_->Echo(&context, request, &response);
1197 EXPECT_EQ(kResponseSize, response.message().size());
1198 EXPECT_TRUE(s.ok());
1199}
1200
1201TEST_P(ProxyEnd2endTest, Peer) {
1202 ResetStub();
1203 EchoRequest request;
1204 EchoResponse response;
1205 request.set_message("hello");
1206 request.mutable_param()->set_echo_peer(true);
1207
1208 ClientContext context;
1209 Status s = stub_->Echo(&context, request, &response);
1210 EXPECT_EQ(response.message(), request.message());
1211 EXPECT_TRUE(s.ok());
1212 EXPECT_TRUE(CheckIsLocalhost(response.param().peer()));
1213 EXPECT_TRUE(CheckIsLocalhost(context.peer()));
1214}
1215
1216//////////////////////////////////////////////////////////////////////////
1217class SecureEnd2endTest : public End2endTest {
1218 protected:
1219 SecureEnd2endTest() {
1220 GPR_ASSERT(!GetParam().use_proxy);
yang-g17197dd2016-02-19 00:04:22 -08001221 GPR_ASSERT(GetParam().credentials_type != kInsecureCredentialsType);
yang-g88d5d522015-09-29 12:46:54 -07001222 }
1223};
1224
1225TEST_P(SecureEnd2endTest, SimpleRpcWithHost) {
1226 ResetStub();
1227
1228 EchoRequest request;
1229 EchoResponse response;
1230 request.set_message("Hello");
1231
1232 ClientContext context;
1233 context.set_authority("foo.test.youtube.com");
1234 Status s = stub_->Echo(&context, request, &response);
1235 EXPECT_EQ(response.message(), request.message());
1236 EXPECT_TRUE(response.has_param());
1237 EXPECT_EQ("special", response.param().host());
1238 EXPECT_TRUE(s.ok());
1239}
1240
yang-ge21908f2015-08-25 13:47:51 -07001241bool MetadataContains(
1242 const std::multimap<grpc::string_ref, grpc::string_ref>& metadata,
1243 const grpc::string& key, const grpc::string& value) {
Yang Gao26a49122015-05-15 17:02:56 -07001244 int count = 0;
1245
yang-ge21908f2015-08-25 13:47:51 -07001246 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator iter =
Yang Gao26a49122015-05-15 17:02:56 -07001247 metadata.begin();
1248 iter != metadata.end(); ++iter) {
yang-ge21908f2015-08-25 13:47:51 -07001249 if (ToString(iter->first) == key && ToString(iter->second) == value) {
Yang Gao26a49122015-05-15 17:02:56 -07001250 count++;
1251 }
1252 }
1253 return count == 1;
1254}
1255
yang-g88d5d522015-09-29 12:46:54 -07001256TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorSuccess) {
1257 auto* processor = new TestAuthMetadataProcessor(true);
1258 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1259 ResetStub();
1260 EchoRequest request;
1261 EchoResponse response;
1262 ClientContext context;
1263 context.set_credentials(processor->GetCompatibleClientCreds());
1264 request.set_message("Hello");
1265 request.mutable_param()->set_echo_metadata(true);
1266 request.mutable_param()->set_expected_client_identity(
1267 TestAuthMetadataProcessor::kGoodGuy);
Dan Bornf2f7d572016-03-03 17:26:12 -08001268 request.mutable_param()->set_expected_transport_security_type(
1269 GetParam().credentials_type);
yang-g88d5d522015-09-29 12:46:54 -07001270
1271 Status s = stub_->Echo(&context, request, &response);
1272 EXPECT_EQ(request.message(), response.message());
1273 EXPECT_TRUE(s.ok());
1274
1275 // Metadata should have been consumed by the processor.
1276 EXPECT_FALSE(MetadataContains(
1277 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1278 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1279}
1280
1281TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorFailure) {
1282 auto* processor = new TestAuthMetadataProcessor(true);
1283 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1284 ResetStub();
1285 EchoRequest request;
1286 EchoResponse response;
1287 ClientContext context;
1288 context.set_credentials(processor->GetIncompatibleClientCreds());
1289 request.set_message("Hello");
1290
1291 Status s = stub_->Echo(&context, request, &response);
1292 EXPECT_FALSE(s.ok());
1293 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1294}
1295TEST_P(SecureEnd2endTest, SetPerCallCredentials) {
1296 ResetStub();
Yang Gaoa8938922015-05-14 11:51:07 -07001297 EchoRequest request;
1298 EchoResponse response;
1299 ClientContext context;
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001300 std::shared_ptr<CallCredentials> creds =
Julien Boeuf510a9202015-08-25 21:51:07 -07001301 GoogleIAMCredentials("fake_token", "fake_selector");
Yang Gaoa8938922015-05-14 11:51:07 -07001302 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -07001303 request.set_message("Hello");
1304 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -07001305
1306 Status s = stub_->Echo(&context, request, &response);
Yang Gaoa8938922015-05-14 11:51:07 -07001307 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -07001308 EXPECT_TRUE(s.ok());
Yang Gao26a49122015-05-15 17:02:56 -07001309 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1310 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1311 "fake_token"));
1312 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1313 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1314 "fake_selector"));
Yang Gaoa8938922015-05-14 11:51:07 -07001315}
1316
yang-g88d5d522015-09-29 12:46:54 -07001317TEST_P(SecureEnd2endTest, OverridePerCallCredentials) {
1318 ResetStub();
Yang Gaoa8938922015-05-14 11:51:07 -07001319 EchoRequest request;
1320 EchoResponse response;
1321 ClientContext context;
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001322 std::shared_ptr<CallCredentials> creds1 =
Julien Boeuf510a9202015-08-25 21:51:07 -07001323 GoogleIAMCredentials("fake_token1", "fake_selector1");
Yang Gaoa8938922015-05-14 11:51:07 -07001324 context.set_credentials(creds1);
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001325 std::shared_ptr<CallCredentials> creds2 =
Julien Boeuf510a9202015-08-25 21:51:07 -07001326 GoogleIAMCredentials("fake_token2", "fake_selector2");
Yang Gaoa8938922015-05-14 11:51:07 -07001327 context.set_credentials(creds2);
Yang Gao26a49122015-05-15 17:02:56 -07001328 request.set_message("Hello");
1329 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -07001330
1331 Status s = stub_->Echo(&context, request, &response);
Yang Gao26a49122015-05-15 17:02:56 -07001332 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1333 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1334 "fake_token2"));
1335 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1336 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1337 "fake_selector2"));
Yang Gaob57f72d2015-05-17 21:54:54 -07001338 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
1339 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1340 "fake_token1"));
1341 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
1342 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1343 "fake_selector1"));
Yang Gaoa8938922015-05-14 11:51:07 -07001344 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -07001345 EXPECT_TRUE(s.ok());
Yang Gaoa8938922015-05-14 11:51:07 -07001346}
1347
yang-gc580af32016-09-15 15:28:38 -07001348TEST_P(SecureEnd2endTest, AuthMetadataPluginKeyFailure) {
1349 ResetStub();
1350 EchoRequest request;
1351 EchoResponse response;
1352 ClientContext context;
1353 context.set_credentials(
1354 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1355 new TestMetadataCredentialsPlugin(
1356 TestMetadataCredentialsPlugin::kBadMetadataKey,
1357 "Does not matter, will fail the key is invalid.", false, true))));
1358 request.set_message("Hello");
1359
1360 Status s = stub_->Echo(&context, request, &response);
1361 EXPECT_FALSE(s.ok());
1362 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1363}
1364
yang-g4b4571a2016-09-15 23:01:09 -07001365TEST_P(SecureEnd2endTest, AuthMetadataPluginValueFailure) {
1366 ResetStub();
1367 EchoRequest request;
1368 EchoResponse response;
1369 ClientContext context;
1370 context.set_credentials(
1371 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1372 new TestMetadataCredentialsPlugin(
1373 TestMetadataCredentialsPlugin::kGoodMetadataKey,
yang-gd5fba282016-09-16 10:29:16 -07001374 "With illegal \n value.", false, true))));
yang-g4b4571a2016-09-15 23:01:09 -07001375 request.set_message("Hello");
1376
1377 Status s = stub_->Echo(&context, request, &response);
1378 EXPECT_FALSE(s.ok());
1379 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1380}
1381
yang-g88d5d522015-09-29 12:46:54 -07001382TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginFailure) {
1383 ResetStub();
Julien Boeuf1928d492015-09-15 15:20:11 -07001384 EchoRequest request;
1385 EchoResponse response;
1386 ClientContext context;
1387 context.set_credentials(
1388 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1389 new TestMetadataCredentialsPlugin(
yang-gc580af32016-09-15 15:28:38 -07001390 TestMetadataCredentialsPlugin::kGoodMetadataKey,
Julien Boeuf1928d492015-09-15 15:20:11 -07001391 "Does not matter, will fail anyway (see 3rd param)", false,
1392 false))));
1393 request.set_message("Hello");
1394
1395 Status s = stub_->Echo(&context, request, &response);
1396 EXPECT_FALSE(s.ok());
1397 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
Julien Boeuf38c0cde2016-06-06 14:46:08 +02001398 EXPECT_EQ(s.error_message(), kTestCredsPluginErrorMsg);
Julien Boeuf1928d492015-09-15 15:20:11 -07001399}
1400
yang-g88d5d522015-09-29 12:46:54 -07001401TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorSuccess) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001402 auto* processor = new TestAuthMetadataProcessor(false);
1403 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
yang-g88d5d522015-09-29 12:46:54 -07001404 ResetStub();
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001405 EchoRequest request;
1406 EchoResponse response;
1407 ClientContext context;
1408 context.set_credentials(processor->GetCompatibleClientCreds());
1409 request.set_message("Hello");
1410 request.mutable_param()->set_echo_metadata(true);
1411 request.mutable_param()->set_expected_client_identity(
1412 TestAuthMetadataProcessor::kGoodGuy);
Dan Bornf2f7d572016-03-03 17:26:12 -08001413 request.mutable_param()->set_expected_transport_security_type(
1414 GetParam().credentials_type);
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001415
1416 Status s = stub_->Echo(&context, request, &response);
1417 EXPECT_EQ(request.message(), response.message());
1418 EXPECT_TRUE(s.ok());
1419
1420 // Metadata should have been consumed by the processor.
1421 EXPECT_FALSE(MetadataContains(
1422 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1423 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1424}
1425
yang-g88d5d522015-09-29 12:46:54 -07001426TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorFailure) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001427 auto* processor = new TestAuthMetadataProcessor(false);
1428 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
yang-g88d5d522015-09-29 12:46:54 -07001429 ResetStub();
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001430 EchoRequest request;
1431 EchoResponse response;
1432 ClientContext context;
1433 context.set_credentials(processor->GetIncompatibleClientCreds());
1434 request.set_message("Hello");
1435
1436 Status s = stub_->Echo(&context, request, &response);
1437 EXPECT_FALSE(s.ok());
1438 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1439}
1440
yang-g88d5d522015-09-29 12:46:54 -07001441TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginFailure) {
1442 ResetStub();
Julien Boeuf1928d492015-09-15 15:20:11 -07001443 EchoRequest request;
1444 EchoResponse response;
1445 ClientContext context;
1446 context.set_credentials(
1447 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1448 new TestMetadataCredentialsPlugin(
yang-gc580af32016-09-15 15:28:38 -07001449 TestMetadataCredentialsPlugin::kGoodMetadataKey,
Julien Boeuf1928d492015-09-15 15:20:11 -07001450 "Does not matter, will fail anyway (see 3rd param)", true,
1451 false))));
1452 request.set_message("Hello");
1453
1454 Status s = stub_->Echo(&context, request, &response);
1455 EXPECT_FALSE(s.ok());
1456 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
Julien Boeuf38c0cde2016-06-06 14:46:08 +02001457 EXPECT_EQ(s.error_message(), kTestCredsPluginErrorMsg);
Julien Boeuf1928d492015-09-15 15:20:11 -07001458}
1459
yang-g88d5d522015-09-29 12:46:54 -07001460TEST_P(SecureEnd2endTest, ClientAuthContext) {
1461 ResetStub();
yang-gc4eef2e2015-07-06 23:26:58 -07001462 EchoRequest request;
1463 EchoResponse response;
1464 request.set_message("Hello");
Dan Bornf2f7d572016-03-03 17:26:12 -08001465 request.mutable_param()->set_check_auth_context(GetParam().credentials_type ==
1466 kTlsCredentialsType);
1467 request.mutable_param()->set_expected_transport_security_type(
1468 GetParam().credentials_type);
yang-gc4eef2e2015-07-06 23:26:58 -07001469 ClientContext context;
1470 Status s = stub_->Echo(&context, request, &response);
1471 EXPECT_EQ(response.message(), request.message());
1472 EXPECT_TRUE(s.ok());
1473
yang-g8b25f2a2015-07-21 23:54:36 -07001474 std::shared_ptr<const AuthContext> auth_ctx = context.auth_context();
Dan Bornf2f7d572016-03-03 17:26:12 -08001475 std::vector<grpc::string_ref> tst =
yang-g8b25f2a2015-07-21 23:54:36 -07001476 auth_ctx->FindPropertyValues("transport_security_type");
Robbie Shade820c1f32016-06-23 14:31:28 -04001477 ASSERT_EQ(1u, tst.size());
Dan Bornf2f7d572016-03-03 17:26:12 -08001478 EXPECT_EQ(GetParam().credentials_type, ToString(tst[0]));
1479 if (GetParam().credentials_type == kTlsCredentialsType) {
1480 EXPECT_EQ("x509_subject_alternative_name",
1481 auth_ctx->GetPeerIdentityPropertyName());
Paul Querna47d841d2016-03-10 11:19:17 -08001482 EXPECT_EQ(4u, auth_ctx->GetPeerIdentity().size());
Dan Bornf2f7d572016-03-03 17:26:12 -08001483 EXPECT_EQ("*.test.google.fr", ToString(auth_ctx->GetPeerIdentity()[0]));
1484 EXPECT_EQ("waterzooi.test.google.be",
1485 ToString(auth_ctx->GetPeerIdentity()[1]));
1486 EXPECT_EQ("*.test.youtube.com", ToString(auth_ctx->GetPeerIdentity()[2]));
Paul Querna47d841d2016-03-10 11:19:17 -08001487 EXPECT_EQ("192.168.1.3", ToString(auth_ctx->GetPeerIdentity()[3]));
Dan Bornf2f7d572016-03-03 17:26:12 -08001488 }
yang-gc4eef2e2015-07-06 23:26:58 -07001489}
1490
Craig Tiller20afa3d2016-10-17 14:52:14 -07001491class ResourceQuotaEnd2endTest : public End2endTest {
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001492 public:
Craig Tiller20afa3d2016-10-17 14:52:14 -07001493 ResourceQuotaEnd2endTest()
1494 : server_resource_quota_("server_resource_quota") {}
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001495
Vijay Paic0b2acb2016-11-01 16:31:56 -07001496 virtual void ConfigureServerBuilder(ServerBuilder* builder) override {
Craig Tiller20afa3d2016-10-17 14:52:14 -07001497 builder->SetResourceQuota(server_resource_quota_);
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001498 }
1499
1500 private:
Craig Tiller20afa3d2016-10-17 14:52:14 -07001501 ResourceQuota server_resource_quota_;
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001502};
1503
Craig Tiller20afa3d2016-10-17 14:52:14 -07001504TEST_P(ResourceQuotaEnd2endTest, SimpleRequest) {
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001505 ResetStub();
1506
1507 EchoRequest request;
1508 EchoResponse response;
1509 request.set_message("Hello");
1510
1511 ClientContext context;
1512 Status s = stub_->Echo(&context, request, &response);
1513 EXPECT_EQ(response.message(), request.message());
1514 EXPECT_TRUE(s.ok());
1515}
1516
yang-g4c8aed32016-02-19 00:19:39 -08001517std::vector<TestScenario> CreateTestScenarios(bool use_proxy,
1518 bool test_insecure,
1519 bool test_secure) {
1520 std::vector<TestScenario> scenarios;
1521 std::vector<grpc::string> credentials_types;
1522 if (test_secure) {
1523 credentials_types = GetSecureCredentialsTypeList();
1524 }
1525 if (test_insecure) {
1526 credentials_types.push_back(kInsecureCredentialsType);
1527 }
1528 for (auto it = credentials_types.begin(); it != credentials_types.end();
1529 ++it) {
Vijay Pai679c75f2016-06-15 13:08:00 -07001530 scenarios.emplace_back(false, *it);
yang-g4c8aed32016-02-19 00:19:39 -08001531 if (use_proxy) {
Vijay Pai679c75f2016-06-15 13:08:00 -07001532 scenarios.emplace_back(true, *it);
yang-g4c8aed32016-02-19 00:19:39 -08001533 }
1534 }
1535 return scenarios;
1536}
yang-g6f30dec2015-07-22 23:11:56 -07001537
yang-g4c8aed32016-02-19 00:19:39 -08001538INSTANTIATE_TEST_CASE_P(End2end, End2endTest,
1539 ::testing::ValuesIn(CreateTestScenarios(false, true,
1540 true)));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001541
yang-g4c8aed32016-02-19 00:19:39 -08001542INSTANTIATE_TEST_CASE_P(End2endServerTryCancel, End2endServerTryCancelTest,
1543 ::testing::ValuesIn(CreateTestScenarios(false, true,
1544 false)));
1545
1546INSTANTIATE_TEST_CASE_P(ProxyEnd2end, ProxyEnd2endTest,
1547 ::testing::ValuesIn(CreateTestScenarios(true, true,
1548 true)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001549
yang-g88d5d522015-09-29 12:46:54 -07001550INSTANTIATE_TEST_CASE_P(SecureEnd2end, SecureEnd2endTest,
yang-g4c8aed32016-02-19 00:19:39 -08001551 ::testing::ValuesIn(CreateTestScenarios(false, false,
1552 true)));
yang-g88d5d522015-09-29 12:46:54 -07001553
Craig Tiller20afa3d2016-10-17 14:52:14 -07001554INSTANTIATE_TEST_CASE_P(ResourceQuotaEnd2end, ResourceQuotaEnd2endTest,
Craig Tillerdb1a5cc2016-09-28 14:22:12 -07001555 ::testing::ValuesIn(CreateTestScenarios(false, true,
1556 true)));
1557
yang-g8ab38362015-07-31 14:05:33 -07001558} // namespace
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001559} // namespace testing
1560} // namespace grpc
1561
1562int main(int argc, char** argv) {
1563 grpc_test_init(argc, argv);
1564 ::testing::InitGoogleTest(&argc, argv);
1565 return RUN_ALL_TESTS();
David Garcia Quintas2bf574f2016-01-14 15:27:08 -08001566}