blob: ea04ab97db239c963484fe04a5d6b5af93fa2a71 [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>
Julien Boeuf5be92a32015-08-28 16:28:18 -070040#include <grpc++/security/auth_metadata_processor.h>
41#include <grpc++/security/credentials.h>
42#include <grpc++/security/server_credentials.h>
yang-g9e2f90c2015-08-21 15:35:03 -070043#include <grpc++/server.h>
44#include <grpc++/server_builder.h>
45#include <grpc++/server_context.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080046#include <grpc/grpc.h>
47#include <grpc/support/thd.h>
48#include <grpc/support/time.h>
yang-g9e2f90c2015-08-21 15:35:03 -070049#include <gtest/gtest.h>
50
Julien Boeuf8ca294e2016-05-02 14:56:30 -070051#include "src/core/lib/security/credentials/credentials.h"
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080052#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
53#include "src/proto/grpc/testing/echo.grpc.pb.h"
Nicolas Noble89219162015-04-07 18:01:18 -070054#include "test/core/util/port.h"
Craig Tiller14e60e92015-01-13 17:26:46 -080055#include "test/core/util/test_config.h"
yang-g7b0edbd2016-02-02 16:05:21 -080056#include "test/cpp/end2end/test_service_impl.h"
yang-ge21908f2015-08-25 13:47:51 -070057#include "test/cpp/util/string_ref_helper.h"
yang-g7d2a3e12016-02-18 15:41:56 -080058#include "test/cpp/util/test_credentials_provider.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059
Craig Tiller1b4e3302015-12-17 16:35:00 -080060using grpc::testing::EchoRequest;
61using grpc::testing::EchoResponse;
Dan Bornf2f7d572016-03-03 17:26:12 -080062using grpc::testing::kTlsCredentialsType;
yangged5e7e02015-01-06 10:16:15 -080063using std::chrono::system_clock;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064
65namespace grpc {
yangged5e7e02015-01-06 10:16:15 -080066namespace testing {
yangged5e7e02015-01-06 10:16:15 -080067namespace {
68
yang-gd7ead692015-07-30 10:57:45 -070069bool CheckIsLocalhost(const grpc::string& addr) {
70 const grpc::string kIpv6("ipv6:[::1]:");
71 const grpc::string kIpv4MappedIpv6("ipv6:[::ffff:127.0.0.1]:");
72 const grpc::string kIpv4("ipv4:127.0.0.1:");
73 return addr.substr(0, kIpv4.size()) == kIpv4 ||
74 addr.substr(0, kIpv4MappedIpv6.size()) == kIpv4MappedIpv6 ||
75 addr.substr(0, kIpv6.size()) == kIpv6;
76}
77
Julien Boeuf1928d492015-09-15 15:20:11 -070078class TestMetadataCredentialsPlugin : public MetadataCredentialsPlugin {
79 public:
80 static const char kMetadataKey[];
81
82 TestMetadataCredentialsPlugin(grpc::string_ref metadata_value,
83 bool is_blocking, bool is_successful)
84 : metadata_value_(metadata_value.data(), metadata_value.length()),
85 is_blocking_(is_blocking),
86 is_successful_(is_successful) {}
87
88 bool IsBlocking() const GRPC_OVERRIDE { return is_blocking_; }
89
Julien Boeuf114f3942015-11-19 21:45:52 -080090 Status GetMetadata(grpc::string_ref service_url, grpc::string_ref method_name,
91 const grpc::AuthContext& channel_auth_context,
Julien Boeuf8b0b6f42015-09-22 13:31:16 -070092 std::multimap<grpc::string, grpc::string>* metadata)
Julien Boeuf1928d492015-09-15 15:20:11 -070093 GRPC_OVERRIDE {
94 EXPECT_GT(service_url.length(), 0UL);
Julien Boeuf114f3942015-11-19 21:45:52 -080095 EXPECT_GT(method_name.length(), 0UL);
96 EXPECT_TRUE(channel_auth_context.IsPeerAuthenticated());
Julien Boeuf1928d492015-09-15 15:20:11 -070097 EXPECT_TRUE(metadata != nullptr);
98 if (is_successful_) {
99 metadata->insert(std::make_pair(kMetadataKey, metadata_value_));
100 return Status::OK;
101 } else {
102 return Status(StatusCode::NOT_FOUND, "Could not find plugin metadata.");
103 }
104 }
105
106 private:
107 grpc::string metadata_value_;
108 bool is_blocking_;
109 bool is_successful_;
110};
111
112const char TestMetadataCredentialsPlugin::kMetadataKey[] = "TestPluginMetadata";
113
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700114class TestAuthMetadataProcessor : public AuthMetadataProcessor {
115 public:
116 static const char kGoodGuy[];
117
118 TestAuthMetadataProcessor(bool is_blocking) : is_blocking_(is_blocking) {}
119
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700120 std::shared_ptr<CallCredentials> GetCompatibleClientCreds() {
Julien Boeuf1928d492015-09-15 15:20:11 -0700121 return MetadataCredentialsFromPlugin(
122 std::unique_ptr<MetadataCredentialsPlugin>(
123 new TestMetadataCredentialsPlugin(kGoodGuy, is_blocking_, true)));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700124 }
Julien Boeuf1928d492015-09-15 15:20:11 -0700125
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700126 std::shared_ptr<CallCredentials> GetIncompatibleClientCreds() {
Julien Boeuf1928d492015-09-15 15:20:11 -0700127 return MetadataCredentialsFromPlugin(
128 std::unique_ptr<MetadataCredentialsPlugin>(
129 new TestMetadataCredentialsPlugin("Mr Hyde", is_blocking_, true)));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700130 }
131
132 // Interface implementation
133 bool IsBlocking() const GRPC_OVERRIDE { return is_blocking_; }
134
135 Status Process(const InputMetadata& auth_metadata, AuthContext* context,
136 OutputMetadata* consumed_auth_metadata,
137 OutputMetadata* response_metadata) GRPC_OVERRIDE {
138 EXPECT_TRUE(consumed_auth_metadata != nullptr);
139 EXPECT_TRUE(context != nullptr);
140 EXPECT_TRUE(response_metadata != nullptr);
Julien Boeuf1928d492015-09-15 15:20:11 -0700141 auto auth_md =
142 auth_metadata.find(TestMetadataCredentialsPlugin::kMetadataKey);
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700143 EXPECT_NE(auth_md, auth_metadata.end());
144 string_ref auth_md_value = auth_md->second;
Julien Boeuf1928d492015-09-15 15:20:11 -0700145 if (auth_md_value == kGoodGuy) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700146 context->AddProperty(kIdentityPropName, kGoodGuy);
147 context->SetPeerIdentityPropertyName(kIdentityPropName);
Julien Boeuf8b0b6f42015-09-22 13:31:16 -0700148 consumed_auth_metadata->insert(std::make_pair(
149 string(auth_md->first.data(), auth_md->first.length()),
150 string(auth_md->second.data(), auth_md->second.length())));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700151 return Status::OK;
152 } else {
153 return Status(StatusCode::UNAUTHENTICATED,
154 string("Invalid principal: ") +
155 string(auth_md_value.data(), auth_md_value.length()));
156 }
157 }
158
Julien Boeuf1928d492015-09-15 15:20:11 -0700159 private:
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700160 static const char kIdentityPropName[];
161 bool is_blocking_;
162};
163
164const char TestAuthMetadataProcessor::kGoodGuy[] = "Dr Jekyll";
165const char TestAuthMetadataProcessor::kIdentityPropName[] = "novel identity";
166
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800167class Proxy : public ::grpc::testing::EchoTestService::Service {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700168 public:
yang-g8c2be9f2015-08-19 16:28:09 -0700169 Proxy(std::shared_ptr<Channel> channel)
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800170 : stub_(grpc::testing::EchoTestService::NewStub(channel)) {}
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700171
172 Status Echo(ServerContext* server_context, const EchoRequest* request,
173 EchoResponse* response) GRPC_OVERRIDE {
174 std::unique_ptr<ClientContext> client_context =
175 ClientContext::FromServerContext(*server_context);
176 return stub_->Echo(client_context.get(), *request, response);
177 }
178
179 private:
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800180 std::unique_ptr< ::grpc::testing::EchoTestService::Stub> stub_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700181};
182
yangg1456d152015-01-08 15:39:58 -0800183class TestServiceImplDupPkg
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800184 : public ::grpc::testing::duplicate::EchoTestService::Service {
yangg1456d152015-01-08 15:39:58 -0800185 public:
186 Status Echo(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800187 EchoResponse* response) GRPC_OVERRIDE {
yangg1456d152015-01-08 15:39:58 -0800188 response->set_message("no package");
189 return Status::OK;
190 }
191};
192
yang-g88d5d522015-09-29 12:46:54 -0700193class TestScenario {
194 public:
yang-g17197dd2016-02-19 00:04:22 -0800195 TestScenario(bool proxy, const grpc::string& creds_type)
196 : use_proxy(proxy), credentials_type(creds_type) {}
yang-g88d5d522015-09-29 12:46:54 -0700197 void Log() const {
yang-g7d2a3e12016-02-18 15:41:56 -0800198 gpr_log(GPR_INFO, "Scenario: proxy %d, credentials %s", use_proxy,
yang-g17197dd2016-02-19 00:04:22 -0800199 credentials_type.c_str());
yang-g88d5d522015-09-29 12:46:54 -0700200 }
201 bool use_proxy;
Vijay Pai679c75f2016-06-15 13:08:00 -0700202 // Although the below grpc::string is logically const, we can't declare
203 // them const because of a limitation in the way old compilers (e.g., gcc-4.4)
204 // manage vector insertion using a copy constructor
205 grpc::string credentials_type;
yang-g88d5d522015-09-29 12:46:54 -0700206};
207
208class End2endTest : public ::testing::TestWithParam<TestScenario> {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209 protected:
Vijay Pai181ef452015-07-14 13:52:48 -0700210 End2endTest()
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700211 : is_server_started_(false),
212 kMaxMessageSize_(8192),
yang-g88d5d522015-09-29 12:46:54 -0700213 special_service_("special") {
214 GetParam().Log();
215 }
Craig Tiller7418d012015-02-11 15:25:03 -0800216
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700217 void TearDown() GRPC_OVERRIDE {
218 if (is_server_started_) {
219 server_->Shutdown();
220 if (proxy_server_) proxy_server_->Shutdown();
221 }
222 }
223
224 void StartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) {
Craig Tiller35e39712015-01-12 16:41:24 -0800225 int port = grpc_pick_unused_port_or_die();
yang-gd7ead692015-07-30 10:57:45 -0700226 server_address_ << "127.0.0.1:" << port;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800227 // Setup server
228 ServerBuilder builder;
yang-g7d2a3e12016-02-18 15:41:56 -0800229 auto server_creds = GetServerCredentials(GetParam().credentials_type);
yang-g17197dd2016-02-19 00:04:22 -0800230 if (GetParam().credentials_type != kInsecureCredentialsType) {
yang-g88d5d522015-09-29 12:46:54 -0700231 server_creds->SetAuthMetadataProcessor(processor);
232 }
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700233 builder.AddListeningPort(server_address_.str(), server_creds);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800234 builder.RegisterService(&service_);
yang-g8b25f2a2015-07-21 23:54:36 -0700235 builder.RegisterService("foo.test.youtube.com", &special_service_);
Yang Gaoc71a9d22015-05-04 00:22:12 -0700236 builder.SetMaxMessageSize(
237 kMaxMessageSize_); // For testing max message size.
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800238 builder.RegisterService(&dup_pkg_service_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800239 server_ = builder.BuildAndStart();
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700240 is_server_started_ = true;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700241 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800242
yang-g9b7757d2015-08-13 11:15:53 -0700243 void ResetChannel() {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700244 if (!is_server_started_) {
245 StartServer(std::shared_ptr<AuthMetadataProcessor>());
246 }
247 EXPECT_TRUE(is_server_started_);
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700248 ChannelArguments args;
yang-g7d2a3e12016-02-18 15:41:56 -0800249 auto channel_creds =
250 GetChannelCredentials(GetParam().credentials_type, &args);
yang-gd59ad7e2016-02-10 12:42:53 -0800251 if (!user_agent_prefix_.empty()) {
252 args.SetUserAgentPrefix(user_agent_prefix_);
253 }
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700254 args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test");
yang-g88d5d522015-09-29 12:46:54 -0700255 channel_ = CreateCustomChannel(server_address_.str(), channel_creds, args);
yang-g9b7757d2015-08-13 11:15:53 -0700256 }
257
yang-g88d5d522015-09-29 12:46:54 -0700258 void ResetStub() {
yang-g9b7757d2015-08-13 11:15:53 -0700259 ResetChannel();
yang-g88d5d522015-09-29 12:46:54 -0700260 if (GetParam().use_proxy) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700261 proxy_service_.reset(new Proxy(channel_));
262 int port = grpc_pick_unused_port_or_die();
263 std::ostringstream proxyaddr;
264 proxyaddr << "localhost:" << port;
265 ServerBuilder builder;
266 builder.AddListeningPort(proxyaddr.str(), InsecureServerCredentials());
267 builder.RegisterService(proxy_service_.get());
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700268 proxy_server_ = builder.BuildAndStart();
269
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700270 channel_ = CreateChannel(proxyaddr.str(), InsecureChannelCredentials());
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700271 }
272
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800273 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
yangg1456d152015-01-08 15:39:58 -0800274 }
275
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700276 bool is_server_started_;
yang-g8c2be9f2015-08-19 16:28:09 -0700277 std::shared_ptr<Channel> channel_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800278 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800279 std::unique_ptr<Server> server_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700280 std::unique_ptr<Server> proxy_server_;
281 std::unique_ptr<Proxy> proxy_service_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800282 std::ostringstream server_address_;
Yang Gao3921c562015-04-30 16:07:06 -0700283 const int kMaxMessageSize_;
nnoble0c475f02014-12-05 15:37:39 -0800284 TestServiceImpl service_;
Craig Tiller822d2c72015-07-07 16:08:00 -0700285 TestServiceImpl special_service_;
yangg1456d152015-01-08 15:39:58 -0800286 TestServiceImplDupPkg dup_pkg_service_;
yang-gd59ad7e2016-02-10 12:42:53 -0800287 grpc::string user_agent_prefix_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800288};
289
yang-gf8174ea2016-02-01 00:09:13 -0800290static void SendRpc(grpc::testing::EchoTestService::Stub* stub, int num_rpcs,
291 bool with_binary_metadata) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800292 EchoRequest request;
293 EchoResponse response;
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700294 request.set_message("Hello hello hello hello");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800295
296 for (int i = 0; i < num_rpcs; ++i) {
297 ClientContext context;
yang-gf8174ea2016-02-01 00:09:13 -0800298 if (with_binary_metadata) {
299 char bytes[8] = {'\0', '\1', '\2', '\3', '\4', '\5', '\6', (char)i};
300 context.AddMetadata("custom-bin", grpc::string(bytes, 8));
301 }
Craig Tillerbf6abee2015-07-19 22:31:04 -0700302 context.set_compression_algorithm(GRPC_COMPRESS_GZIP);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800303 Status s = stub->Echo(&context, request, &response);
304 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700305 EXPECT_TRUE(s.ok());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800306 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800307}
308
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800309// This class is for testing scenarios where RPCs are cancelled on the server
310// by calling ServerContext::TryCancel()
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800311class End2endServerTryCancelTest : public End2endTest {
312 protected:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800313 // Helper for testing client-streaming RPCs which are cancelled on the server.
314 // Depending on the value of server_try_cancel parameter, this will test one
315 // of the following three scenarios:
316 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading
317 // any messages from the client
318 //
319 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading
320 // messages from the client
321 //
322 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all
323 // the messages from the client
324 //
325 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800326 void TestRequestStreamServerCancel(
327 ServerTryCancelRequestPhase server_try_cancel, int num_msgs_to_send) {
328 ResetStub();
329 EchoRequest request;
330 EchoResponse response;
331 ClientContext context;
332
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800333 // Send server_try_cancel value in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800334 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700335 grpc::to_string(server_try_cancel));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800336
337 auto stream = stub_->RequestStream(&context, &response);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800338
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800339 int num_msgs_sent = 0;
340 while (num_msgs_sent < num_msgs_to_send) {
341 request.set_message("hello");
342 if (!stream->Write(request)) {
343 break;
344 }
345 num_msgs_sent++;
346 }
347 gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800348
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800349 stream->WritesDone();
350 Status s = stream->Finish();
351
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800352 // At this point, we know for sure that RPC was cancelled by the server
353 // since we passed server_try_cancel value in the metadata. Depending on the
354 // value of server_try_cancel, the RPC might have been cancelled by the
355 // server at different stages. The following validates our expectations of
356 // number of messages sent in various cancellation scenarios:
357
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800358 switch (server_try_cancel) {
359 case CANCEL_BEFORE_PROCESSING:
360 case CANCEL_DURING_PROCESSING:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800361 // If the RPC is cancelled by server before / during messages from the
362 // client, it means that the client most likely did not get a chance to
363 // send all the messages it wanted to send. i.e num_msgs_sent <=
364 // num_msgs_to_send
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800365 EXPECT_LE(num_msgs_sent, num_msgs_to_send);
366 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800367
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800368 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800369 // If the RPC was cancelled after all messages were read by the server,
370 // the client did get a chance to send all its messages
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800371 EXPECT_EQ(num_msgs_sent, num_msgs_to_send);
372 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800373
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800374 default:
375 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
376 server_try_cancel);
377 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
378 server_try_cancel <= CANCEL_AFTER_PROCESSING);
379 break;
380 }
381
382 EXPECT_FALSE(s.ok());
383 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
384 }
385
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800386 // Helper for testing server-streaming RPCs which are cancelled on the server.
387 // Depending on the value of server_try_cancel parameter, this will test one
388 // of the following three scenarios:
389 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before writing
390 // any messages to the client
391 //
392 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while writing
393 // messages to the client
394 //
395 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after writing all
396 // the messages to the client
397 //
398 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800399 void TestResponseStreamServerCancel(
400 ServerTryCancelRequestPhase server_try_cancel) {
401 ResetStub();
402 EchoRequest request;
403 EchoResponse response;
404 ClientContext context;
405
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800406 // Send server_try_cancel in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800407 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700408 grpc::to_string(server_try_cancel));
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800409
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800410 request.set_message("hello");
411 auto stream = stub_->ResponseStream(&context, request);
412
413 int num_msgs_read = 0;
414 while (num_msgs_read < kNumResponseStreamsMsgs) {
415 if (!stream->Read(&response)) {
416 break;
417 }
418 EXPECT_EQ(response.message(),
Vijay Paia63271c2016-06-15 12:56:38 -0700419 request.message() + grpc::to_string(num_msgs_read));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800420 num_msgs_read++;
421 }
422 gpr_log(GPR_INFO, "Read %d messages", num_msgs_read);
423
424 Status s = stream->Finish();
425
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800426 // Depending on the value of server_try_cancel, the RPC might have been
427 // cancelled by the server at different stages. The following validates our
428 // expectations of number of messages read in various cancellation
429 // scenarios:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800430 switch (server_try_cancel) {
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800431 case CANCEL_BEFORE_PROCESSING:
432 // Server cancelled before sending any messages. Which means the client
433 // wouldn't have read any
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800434 EXPECT_EQ(num_msgs_read, 0);
435 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800436
437 case CANCEL_DURING_PROCESSING:
438 // Server cancelled while writing messages. Client must have read less
439 // than or equal to the expected number of messages
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800440 EXPECT_LE(num_msgs_read, kNumResponseStreamsMsgs);
441 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800442
443 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla8d543e82016-02-29 18:22:25 -0800444 // Even though the Server cancelled after writing all messages, the RPC
445 // may be cancelled before the Client got a chance to read all the
446 // messages.
447 EXPECT_LE(num_msgs_read, kNumResponseStreamsMsgs);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800448 break;
449
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800450 default: {
451 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
452 server_try_cancel);
453 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
454 server_try_cancel <= CANCEL_AFTER_PROCESSING);
455 break;
456 }
457 }
458
459 EXPECT_FALSE(s.ok());
460 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
461 }
462
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800463 // Helper for testing bidirectional-streaming RPCs which are cancelled on the
464 // server. Depending on the value of server_try_cancel parameter, this will
465 // test one of the following three scenarios:
466 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading/
467 // writing any messages from/to the client
468 //
469 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading/
470 // writing messages from/to the client
471 //
472 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading/writing
473 // all the messages from/to the client
474 //
475 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800476 void TestBidiStreamServerCancel(ServerTryCancelRequestPhase server_try_cancel,
477 int num_messages) {
478 ResetStub();
479 EchoRequest request;
480 EchoResponse response;
481 ClientContext context;
482
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800483 // Send server_try_cancel in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800484 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700485 grpc::to_string(server_try_cancel));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800486
487 auto stream = stub_->BidiStream(&context);
488
489 int num_msgs_read = 0;
490 int num_msgs_sent = 0;
491 while (num_msgs_sent < num_messages) {
Vijay Paia63271c2016-06-15 12:56:38 -0700492 request.set_message("hello " + grpc::to_string(num_msgs_sent));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800493 if (!stream->Write(request)) {
494 break;
495 }
496 num_msgs_sent++;
497
498 if (!stream->Read(&response)) {
499 break;
500 }
501 num_msgs_read++;
502
503 EXPECT_EQ(response.message(), request.message());
504 }
505 gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent);
506 gpr_log(GPR_INFO, "Read %d messages", num_msgs_read);
507
508 stream->WritesDone();
509 Status s = stream->Finish();
510
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800511 // Depending on the value of server_try_cancel, the RPC might have been
512 // cancelled by the server at different stages. The following validates our
513 // expectations of number of messages read in various cancellation
514 // scenarios:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800515 switch (server_try_cancel) {
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800516 case CANCEL_BEFORE_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800517 EXPECT_EQ(num_msgs_read, 0);
518 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800519
520 case CANCEL_DURING_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800521 EXPECT_LE(num_msgs_sent, num_messages);
522 EXPECT_LE(num_msgs_read, num_msgs_sent);
523 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800524
525 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800526 EXPECT_EQ(num_msgs_sent, num_messages);
Sree Kuchibhotla8d543e82016-02-29 18:22:25 -0800527
528 // The Server cancelled after reading the last message and after writing
529 // the message to the client. However, the RPC cancellation might have
530 // taken effect before the client actually read the response.
531 EXPECT_LE(num_msgs_read, num_msgs_sent);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800532 break;
533
534 default:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800535 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
536 server_try_cancel);
537 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
538 server_try_cancel <= CANCEL_AFTER_PROCESSING);
539 break;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800540 }
541
542 EXPECT_FALSE(s.ok());
543 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
544 }
545};
546
547TEST_P(End2endServerTryCancelTest, RequestEchoServerCancel) {
548 ResetStub();
549 EchoRequest request;
550 EchoResponse response;
551 ClientContext context;
552
553 context.AddMetadata(kServerTryCancelRequest,
Vijay Paia63271c2016-06-15 12:56:38 -0700554 grpc::to_string(CANCEL_BEFORE_PROCESSING));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800555 Status s = stub_->Echo(&context, request, &response);
556 EXPECT_FALSE(s.ok());
557 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
558}
559
560// Server to cancel before doing reading the request
561TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelBeforeReads) {
562 TestRequestStreamServerCancel(CANCEL_BEFORE_PROCESSING, 1);
563}
564
565// Server to cancel while reading a request from the stream in parallel
566TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelDuringRead) {
567 TestRequestStreamServerCancel(CANCEL_DURING_PROCESSING, 10);
568}
569
570// Server to cancel after reading all the requests but before returning to the
571// client
572TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelAfterReads) {
573 TestRequestStreamServerCancel(CANCEL_AFTER_PROCESSING, 4);
574}
575
576// Server to cancel before sending any response messages
577TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelBefore) {
578 TestResponseStreamServerCancel(CANCEL_BEFORE_PROCESSING);
579}
580
581// Server to cancel while writing a response to the stream in parallel
582TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelDuring) {
583 TestResponseStreamServerCancel(CANCEL_DURING_PROCESSING);
584}
585
586// Server to cancel after writing all the respones to the stream but before
587// returning to the client
588TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelAfter) {
589 TestResponseStreamServerCancel(CANCEL_AFTER_PROCESSING);
590}
591
592// Server to cancel before reading/writing any requests/responses on the stream
593TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelBefore) {
594 TestBidiStreamServerCancel(CANCEL_BEFORE_PROCESSING, 2);
595}
596
597// Server to cancel while reading/writing requests/responses on the stream in
598// parallel
599TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelDuring) {
600 TestBidiStreamServerCancel(CANCEL_DURING_PROCESSING, 10);
601}
602
603// Server to cancel after reading/writing all requests/responses on the stream
604// but before returning to the client
605TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelAfter) {
606 TestBidiStreamServerCancel(CANCEL_AFTER_PROCESSING, 5);
607}
608
yang-gd59ad7e2016-02-10 12:42:53 -0800609TEST_P(End2endTest, SimpleRpcWithCustomeUserAgentPrefix) {
610 user_agent_prefix_ = "custom_prefix";
611 ResetStub();
612 EchoRequest request;
613 EchoResponse response;
614 request.set_message("Hello hello hello hello");
615 request.mutable_param()->set_echo_metadata(true);
616
617 ClientContext context;
618 Status s = stub_->Echo(&context, request, &response);
619 EXPECT_EQ(response.message(), request.message());
620 EXPECT_TRUE(s.ok());
621 const auto& trailing_metadata = context.GetServerTrailingMetadata();
622 auto iter = trailing_metadata.find("user-agent");
623 EXPECT_TRUE(iter != trailing_metadata.end());
624 grpc::string expected_prefix = user_agent_prefix_ + " grpc-c++/";
625 EXPECT_TRUE(iter->second.starts_with(expected_prefix));
626}
627
yang-gf8174ea2016-02-01 00:09:13 -0800628TEST_P(End2endTest, MultipleRpcsWithVariedBinaryMetadataValue) {
629 ResetStub();
630 std::vector<std::thread*> threads;
631 for (int i = 0; i < 10; ++i) {
632 threads.push_back(new std::thread(SendRpc, stub_.get(), 10, true));
633 }
634 for (int i = 0; i < 10; ++i) {
635 threads[i]->join();
636 delete threads[i];
637 }
638}
639
640TEST_P(End2endTest, MultipleRpcs) {
641 ResetStub();
642 std::vector<std::thread*> threads;
643 for (int i = 0; i < 10; ++i) {
644 threads.push_back(new std::thread(SendRpc, stub_.get(), 10, false));
645 }
646 for (int i = 0; i < 10; ++i) {
647 threads[i]->join();
648 delete threads[i];
649 }
650}
651
yang-g88d5d522015-09-29 12:46:54 -0700652TEST_P(End2endTest, RequestStreamOneRequest) {
653 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800654 EchoRequest request;
655 EchoResponse response;
656 ClientContext context;
657
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800658 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800659 request.set_message("hello");
660 EXPECT_TRUE(stream->Write(request));
661 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800662 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800663 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700664 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800665}
666
yang-g88d5d522015-09-29 12:46:54 -0700667TEST_P(End2endTest, RequestStreamTwoRequests) {
668 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800669 EchoRequest request;
670 EchoResponse response;
671 ClientContext context;
672
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800673 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800674 request.set_message("hello");
675 EXPECT_TRUE(stream->Write(request));
676 EXPECT_TRUE(stream->Write(request));
677 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800678 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800679 EXPECT_EQ(response.message(), "hellohello");
Yang Gaoc1a2c312015-06-16 10:59:46 -0700680 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800681}
682
yang-g88d5d522015-09-29 12:46:54 -0700683TEST_P(End2endTest, ResponseStream) {
684 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800685 EchoRequest request;
686 EchoResponse response;
687 ClientContext context;
688 request.set_message("hello");
689
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800690 auto stream = stub_->ResponseStream(&context, request);
nnoble0c475f02014-12-05 15:37:39 -0800691 EXPECT_TRUE(stream->Read(&response));
692 EXPECT_EQ(response.message(), request.message() + "0");
693 EXPECT_TRUE(stream->Read(&response));
694 EXPECT_EQ(response.message(), request.message() + "1");
695 EXPECT_TRUE(stream->Read(&response));
696 EXPECT_EQ(response.message(), request.message() + "2");
697 EXPECT_FALSE(stream->Read(&response));
698
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800699 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700700 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800701}
702
yang-g88d5d522015-09-29 12:46:54 -0700703TEST_P(End2endTest, BidiStream) {
704 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800705 EchoRequest request;
706 EchoResponse response;
707 ClientContext context;
708 grpc::string msg("hello");
709
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800710 auto stream = stub_->BidiStream(&context);
nnoble0c475f02014-12-05 15:37:39 -0800711
712 request.set_message(msg + "0");
713 EXPECT_TRUE(stream->Write(request));
714 EXPECT_TRUE(stream->Read(&response));
715 EXPECT_EQ(response.message(), request.message());
716
717 request.set_message(msg + "1");
718 EXPECT_TRUE(stream->Write(request));
719 EXPECT_TRUE(stream->Read(&response));
720 EXPECT_EQ(response.message(), request.message());
721
722 request.set_message(msg + "2");
723 EXPECT_TRUE(stream->Write(request));
724 EXPECT_TRUE(stream->Read(&response));
725 EXPECT_EQ(response.message(), request.message());
726
727 stream->WritesDone();
728 EXPECT_FALSE(stream->Read(&response));
Craig Tillerca9a6372015-12-15 18:16:28 -0800729 EXPECT_FALSE(stream->Read(&response));
nnoble0c475f02014-12-05 15:37:39 -0800730
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800731 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700732 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800733}
734
735// Talk to the two services with the same name but different package names.
736// The two stubs are created on the same channel.
yang-g88d5d522015-09-29 12:46:54 -0700737TEST_P(End2endTest, DiffPackageServices) {
738 ResetStub();
yangg1456d152015-01-08 15:39:58 -0800739 EchoRequest request;
740 EchoResponse response;
741 request.set_message("Hello");
742
yangg1456d152015-01-08 15:39:58 -0800743 ClientContext context;
yang-g8b25f2a2015-07-21 23:54:36 -0700744 Status s = stub_->Echo(&context, request, &response);
yangg1456d152015-01-08 15:39:58 -0800745 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700746 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800747
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800748 std::unique_ptr<grpc::testing::duplicate::EchoTestService::Stub> dup_pkg_stub(
749 grpc::testing::duplicate::EchoTestService::NewStub(channel_));
yangg1456d152015-01-08 15:39:58 -0800750 ClientContext context2;
751 s = dup_pkg_stub->Echo(&context2, request, &response);
752 EXPECT_EQ("no package", response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700753 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800754}
755
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700756void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) {
Craig Tiller20b5fe92015-07-06 10:43:50 -0700757 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700758 gpr_time_from_micros(delay_us, GPR_TIMESPAN)));
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700759 while (!service->signal_client()) {
760 }
761 context->TryCancel();
762}
763
yang-ga89bf502015-11-17 14:19:17 -0800764TEST_P(End2endTest, CancelRpcBeforeStart) {
765 ResetStub();
766 EchoRequest request;
767 EchoResponse response;
768 ClientContext context;
769 request.set_message("hello");
770 context.TryCancel();
771 Status s = stub_->Echo(&context, request, &response);
772 EXPECT_EQ("", response.message());
773 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
774}
775
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700776// Client cancels request stream after sending two messages
yang-g88d5d522015-09-29 12:46:54 -0700777TEST_P(End2endTest, ClientCancelsRequestStream) {
778 ResetStub();
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700779 EchoRequest request;
780 EchoResponse response;
781 ClientContext context;
782 request.set_message("hello");
783
784 auto stream = stub_->RequestStream(&context, &response);
785 EXPECT_TRUE(stream->Write(request));
786 EXPECT_TRUE(stream->Write(request));
Yang Gaoc71a9d22015-05-04 00:22:12 -0700787
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700788 context.TryCancel();
789
790 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700791 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700792
Yang Gaoc71a9d22015-05-04 00:22:12 -0700793 EXPECT_EQ(response.message(), "");
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700794}
795
Abhishek Kumare41d0402015-04-17 14:12:33 -0700796// Client cancels server stream after sending some messages
yang-g88d5d522015-09-29 12:46:54 -0700797TEST_P(End2endTest, ClientCancelsResponseStream) {
798 ResetStub();
Abhishek Kumare41d0402015-04-17 14:12:33 -0700799 EchoRequest request;
800 EchoResponse response;
801 ClientContext context;
802 request.set_message("hello");
803
804 auto stream = stub_->ResponseStream(&context, request);
805
806 EXPECT_TRUE(stream->Read(&response));
807 EXPECT_EQ(response.message(), request.message() + "0");
808 EXPECT_TRUE(stream->Read(&response));
809 EXPECT_EQ(response.message(), request.message() + "1");
810
811 context.TryCancel();
812
813 // The cancellation races with responses, so there might be zero or
814 // one responses pending, read till failure
815
816 if (stream->Read(&response)) {
817 EXPECT_EQ(response.message(), request.message() + "2");
818 // Since we have cancelled, we expect the next attempt to read to fail
819 EXPECT_FALSE(stream->Read(&response));
820 }
821
822 Status s = stream->Finish();
Abhishek Kumar18298a72015-04-17 15:00:25 -0700823 // The final status could be either of CANCELLED or OK depending on
824 // who won the race.
Yang Gaoc1a2c312015-06-16 10:59:46 -0700825 EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumare41d0402015-04-17 14:12:33 -0700826}
827
Abhishek Kumar82a83312015-04-17 13:30:51 -0700828// Client cancels bidi stream after sending some messages
yang-g88d5d522015-09-29 12:46:54 -0700829TEST_P(End2endTest, ClientCancelsBidi) {
830 ResetStub();
Abhishek Kumar82a83312015-04-17 13:30:51 -0700831 EchoRequest request;
832 EchoResponse response;
833 ClientContext context;
834 grpc::string msg("hello");
835
836 auto stream = stub_->BidiStream(&context);
837
838 request.set_message(msg + "0");
839 EXPECT_TRUE(stream->Write(request));
840 EXPECT_TRUE(stream->Read(&response));
841 EXPECT_EQ(response.message(), request.message());
842
843 request.set_message(msg + "1");
844 EXPECT_TRUE(stream->Write(request));
845
846 context.TryCancel();
847
848 // The cancellation races with responses, so there might be zero or
849 // one responses pending, read till failure
850
851 if (stream->Read(&response)) {
852 EXPECT_EQ(response.message(), request.message());
853 // Since we have cancelled, we expect the next attempt to read to fail
854 EXPECT_FALSE(stream->Read(&response));
855 }
856
857 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700858 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumar82a83312015-04-17 13:30:51 -0700859}
860
yang-g88d5d522015-09-29 12:46:54 -0700861TEST_P(End2endTest, RpcMaxMessageSize) {
862 ResetStub();
Yang Gao3921c562015-04-30 16:07:06 -0700863 EchoRequest request;
864 EchoResponse response;
Yang Gaoc71a9d22015-05-04 00:22:12 -0700865 request.set_message(string(kMaxMessageSize_ * 2, 'a'));
Yang Gao3921c562015-04-30 16:07:06 -0700866
867 ClientContext context;
868 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700869 EXPECT_FALSE(s.ok());
Yang Gao3921c562015-04-30 16:07:06 -0700870}
Abhishek Kumar82a83312015-04-17 13:30:51 -0700871
yang-g88d5d522015-09-29 12:46:54 -0700872// Client sends 20 requests and the server returns CANCELLED status after
873// reading 10 requests.
874TEST_P(End2endTest, RequestStreamServerEarlyCancelTest) {
875 ResetStub();
876 EchoRequest request;
877 EchoResponse response;
878 ClientContext context;
879
880 context.AddMetadata(kServerCancelAfterReads, "10");
881 auto stream = stub_->RequestStream(&context, &response);
882 request.set_message("hello");
883 int send_messages = 20;
yang-gc0461032015-10-02 16:22:43 -0700884 while (send_messages > 10) {
yang-g88d5d522015-09-29 12:46:54 -0700885 EXPECT_TRUE(stream->Write(request));
886 send_messages--;
887 }
yang-gc0461032015-10-02 16:22:43 -0700888 while (send_messages > 0) {
889 stream->Write(request);
890 send_messages--;
891 }
yang-g88d5d522015-09-29 12:46:54 -0700892 stream->WritesDone();
893 Status s = stream->Finish();
894 EXPECT_EQ(s.error_code(), StatusCode::CANCELLED);
895}
896
yang-g88d5d522015-09-29 12:46:54 -0700897void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream,
898 gpr_event* ev) {
899 EchoResponse resp;
900 gpr_event_set(ev, (void*)1);
901 while (stream->Read(&resp)) {
902 gpr_log(GPR_INFO, "Read message");
903 }
904}
yang-g88d5d522015-09-29 12:46:54 -0700905
906// Run a Read and a WritesDone simultaneously.
907TEST_P(End2endTest, SimultaneousReadWritesDone) {
908 ResetStub();
909 ClientContext context;
910 gpr_event ev;
911 gpr_event_init(&ev);
912 auto stream = stub_->BidiStream(&context);
913 std::thread reader_thread(ReaderThreadFunc, stream.get(), &ev);
914 gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME));
915 stream->WritesDone();
Vijay Paibdfec2c2016-02-25 11:51:21 -0800916 reader_thread.join();
yang-g88d5d522015-09-29 12:46:54 -0700917 Status s = stream->Finish();
918 EXPECT_TRUE(s.ok());
yang-g88d5d522015-09-29 12:46:54 -0700919}
920
921TEST_P(End2endTest, ChannelState) {
922 ResetStub();
923 // Start IDLE
924 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(false));
925
926 // Did not ask to connect, no state change.
927 CompletionQueue cq;
928 std::chrono::system_clock::time_point deadline =
929 std::chrono::system_clock::now() + std::chrono::milliseconds(10);
930 channel_->NotifyOnStateChange(GRPC_CHANNEL_IDLE, deadline, &cq, NULL);
931 void* tag;
932 bool ok = true;
933 cq.Next(&tag, &ok);
934 EXPECT_FALSE(ok);
935
936 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true));
937 EXPECT_TRUE(channel_->WaitForStateChange(GRPC_CHANNEL_IDLE,
938 gpr_inf_future(GPR_CLOCK_REALTIME)));
yang-g0d557502015-10-01 11:30:12 -0700939 auto state = channel_->GetState(false);
940 EXPECT_TRUE(state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_READY);
yang-g88d5d522015-09-29 12:46:54 -0700941}
942
943// Takes 10s.
944TEST_P(End2endTest, ChannelStateTimeout) {
yang-g17197dd2016-02-19 00:04:22 -0800945 if (GetParam().credentials_type != kInsecureCredentialsType) {
yang-g88d5d522015-09-29 12:46:54 -0700946 return;
947 }
948 int port = grpc_pick_unused_port_or_die();
949 std::ostringstream server_address;
950 server_address << "127.0.0.1:" << port;
951 // Channel to non-existing server
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700952 auto channel =
953 CreateChannel(server_address.str(), InsecureChannelCredentials());
yang-g88d5d522015-09-29 12:46:54 -0700954 // Start IDLE
955 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel->GetState(true));
956
957 auto state = GRPC_CHANNEL_IDLE;
958 for (int i = 0; i < 10; i++) {
959 channel->WaitForStateChange(
960 state, std::chrono::system_clock::now() + std::chrono::seconds(1));
961 state = channel->GetState(false);
962 }
963}
964
965// Talking to a non-existing service.
966TEST_P(End2endTest, NonExistingService) {
967 ResetChannel();
Craig Tiller1b4e3302015-12-17 16:35:00 -0800968 std::unique_ptr<grpc::testing::UnimplementedService::Stub> stub;
969 stub = grpc::testing::UnimplementedService::NewStub(channel_);
yang-g88d5d522015-09-29 12:46:54 -0700970
971 EchoRequest request;
972 EchoResponse response;
973 request.set_message("Hello");
974
975 ClientContext context;
976 Status s = stub->Unimplemented(&context, request, &response);
977 EXPECT_EQ(StatusCode::UNIMPLEMENTED, s.error_code());
978 EXPECT_EQ("", s.error_message());
979}
980
yang-g4c070082016-05-05 23:27:13 -0700981// Ask the server to send back a serialized proto in trailer.
982// This is an example of setting error details.
983TEST_P(End2endTest, BinaryTrailerTest) {
984 ResetStub();
985 EchoRequest request;
986 EchoResponse response;
987 ClientContext context;
988
989 request.mutable_param()->set_echo_metadata(true);
990 DebugInfo* info = request.mutable_param()->mutable_debug_info();
991 info->add_stack_entries("stack_entry_1");
992 info->add_stack_entries("stack_entry_2");
993 info->add_stack_entries("stack_entry_3");
994 info->set_detail("detailed debug info");
995 grpc::string expected_string = info->SerializeAsString();
996 request.set_message("Hello");
997
998 Status s = stub_->Echo(&context, request, &response);
999 EXPECT_FALSE(s.ok());
1000 auto trailers = context.GetServerTrailingMetadata();
yang-g39e71c32016-05-10 10:21:41 -07001001 EXPECT_EQ(1u, trailers.count(kDebugInfoTrailerKey));
yang-g4c070082016-05-05 23:27:13 -07001002 auto iter = trailers.find(kDebugInfoTrailerKey);
1003 EXPECT_EQ(expected_string, iter->second);
yang-g080528a2016-05-06 13:12:00 -07001004 // Parse the returned trailer into a DebugInfo proto.
1005 DebugInfo returned_info;
1006 EXPECT_TRUE(returned_info.ParseFromString(ToString(iter->second)));
yang-g4c070082016-05-05 23:27:13 -07001007}
1008
yang-g88d5d522015-09-29 12:46:54 -07001009//////////////////////////////////////////////////////////////////////////
1010// Test with and without a proxy.
1011class ProxyEnd2endTest : public End2endTest {
1012 protected:
1013};
1014
1015TEST_P(ProxyEnd2endTest, SimpleRpc) {
1016 ResetStub();
yang-gf8174ea2016-02-01 00:09:13 -08001017 SendRpc(stub_.get(), 1, false);
yang-g88d5d522015-09-29 12:46:54 -07001018}
1019
yang-g000aa452016-06-07 13:08:39 -07001020TEST_P(ProxyEnd2endTest, SimpleRpcWithEmptyMessages) {
1021 ResetStub();
1022 EchoRequest request;
1023 EchoResponse response;
1024
1025 ClientContext context;
1026 Status s = stub_->Echo(&context, request, &response);
1027 EXPECT_TRUE(s.ok());
1028}
1029
yang-g88d5d522015-09-29 12:46:54 -07001030TEST_P(ProxyEnd2endTest, MultipleRpcs) {
1031 ResetStub();
1032 std::vector<std::thread*> threads;
1033 for (int i = 0; i < 10; ++i) {
yang-gf8174ea2016-02-01 00:09:13 -08001034 threads.push_back(new std::thread(SendRpc, stub_.get(), 10, false));
yang-g88d5d522015-09-29 12:46:54 -07001035 }
1036 for (int i = 0; i < 10; ++i) {
1037 threads[i]->join();
1038 delete threads[i];
1039 }
1040}
1041
1042// Set a 10us deadline and make sure proper error is returned.
1043TEST_P(ProxyEnd2endTest, RpcDeadlineExpires) {
1044 ResetStub();
1045 EchoRequest request;
1046 EchoResponse response;
1047 request.set_message("Hello");
Craig Tillerb0f275e2016-01-27 10:45:50 -08001048 request.mutable_param()->set_skip_cancelled_check(true);
yang-g88d5d522015-09-29 12:46:54 -07001049
1050 ClientContext context;
1051 std::chrono::system_clock::time_point deadline =
1052 std::chrono::system_clock::now() + std::chrono::microseconds(10);
1053 context.set_deadline(deadline);
1054 Status s = stub_->Echo(&context, request, &response);
1055 EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code());
1056}
1057
1058// Set a long but finite deadline.
1059TEST_P(ProxyEnd2endTest, RpcLongDeadline) {
1060 ResetStub();
1061 EchoRequest request;
1062 EchoResponse response;
1063 request.set_message("Hello");
1064
1065 ClientContext context;
1066 std::chrono::system_clock::time_point deadline =
1067 std::chrono::system_clock::now() + std::chrono::hours(1);
1068 context.set_deadline(deadline);
1069 Status s = stub_->Echo(&context, request, &response);
1070 EXPECT_EQ(response.message(), request.message());
1071 EXPECT_TRUE(s.ok());
1072}
1073
1074// Ask server to echo back the deadline it sees.
1075TEST_P(ProxyEnd2endTest, EchoDeadline) {
1076 ResetStub();
1077 EchoRequest request;
1078 EchoResponse response;
1079 request.set_message("Hello");
1080 request.mutable_param()->set_echo_deadline(true);
1081
1082 ClientContext context;
1083 std::chrono::system_clock::time_point deadline =
1084 std::chrono::system_clock::now() + std::chrono::seconds(100);
1085 context.set_deadline(deadline);
1086 Status s = stub_->Echo(&context, request, &response);
1087 EXPECT_EQ(response.message(), request.message());
1088 EXPECT_TRUE(s.ok());
1089 gpr_timespec sent_deadline;
1090 Timepoint2Timespec(deadline, &sent_deadline);
1091 // Allow 1 second error.
1092 EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1);
1093 EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1);
1094}
1095
1096// Ask server to echo back the deadline it sees. The rpc has no deadline.
1097TEST_P(ProxyEnd2endTest, EchoDeadlineForNoDeadlineRpc) {
1098 ResetStub();
1099 EchoRequest request;
1100 EchoResponse response;
1101 request.set_message("Hello");
1102 request.mutable_param()->set_echo_deadline(true);
1103
1104 ClientContext context;
1105 Status s = stub_->Echo(&context, request, &response);
1106 EXPECT_EQ(response.message(), request.message());
1107 EXPECT_TRUE(s.ok());
1108 EXPECT_EQ(response.param().request_deadline(),
1109 gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec);
1110}
1111
1112TEST_P(ProxyEnd2endTest, UnimplementedRpc) {
1113 ResetStub();
1114 EchoRequest request;
1115 EchoResponse response;
1116 request.set_message("Hello");
1117
1118 ClientContext context;
1119 Status s = stub_->Unimplemented(&context, request, &response);
1120 EXPECT_FALSE(s.ok());
1121 EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED);
1122 EXPECT_EQ(s.error_message(), "");
1123 EXPECT_EQ(response.message(), "");
1124}
1125
1126// Client cancels rpc after 10ms
1127TEST_P(ProxyEnd2endTest, ClientCancelsRpc) {
1128 ResetStub();
1129 EchoRequest request;
1130 EchoResponse response;
1131 request.set_message("Hello");
1132 const int kCancelDelayUs = 10 * 1000;
1133 request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs);
1134
1135 ClientContext context;
1136 std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_);
1137 Status s = stub_->Echo(&context, request, &response);
1138 cancel_thread.join();
1139 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
1140 EXPECT_EQ(s.error_message(), "Cancelled");
1141}
1142
1143// Server cancels rpc after 1ms
1144TEST_P(ProxyEnd2endTest, ServerCancelsRpc) {
1145 ResetStub();
1146 EchoRequest request;
1147 EchoResponse response;
1148 request.set_message("Hello");
1149 request.mutable_param()->set_server_cancel_after_us(1000);
1150
1151 ClientContext context;
1152 Status s = stub_->Echo(&context, request, &response);
1153 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
1154 EXPECT_TRUE(s.error_message().empty());
1155}
1156
1157// Make the response larger than the flow control window.
1158TEST_P(ProxyEnd2endTest, HugeResponse) {
1159 ResetStub();
1160 EchoRequest request;
1161 EchoResponse response;
1162 request.set_message("huge response");
1163 const size_t kResponseSize = 1024 * (1024 + 10);
1164 request.mutable_param()->set_response_message_length(kResponseSize);
1165
1166 ClientContext context;
1167 Status s = stub_->Echo(&context, request, &response);
1168 EXPECT_EQ(kResponseSize, response.message().size());
1169 EXPECT_TRUE(s.ok());
1170}
1171
1172TEST_P(ProxyEnd2endTest, Peer) {
1173 ResetStub();
1174 EchoRequest request;
1175 EchoResponse response;
1176 request.set_message("hello");
1177 request.mutable_param()->set_echo_peer(true);
1178
1179 ClientContext context;
1180 Status s = stub_->Echo(&context, request, &response);
1181 EXPECT_EQ(response.message(), request.message());
1182 EXPECT_TRUE(s.ok());
1183 EXPECT_TRUE(CheckIsLocalhost(response.param().peer()));
1184 EXPECT_TRUE(CheckIsLocalhost(context.peer()));
1185}
1186
1187//////////////////////////////////////////////////////////////////////////
1188class SecureEnd2endTest : public End2endTest {
1189 protected:
1190 SecureEnd2endTest() {
1191 GPR_ASSERT(!GetParam().use_proxy);
yang-g17197dd2016-02-19 00:04:22 -08001192 GPR_ASSERT(GetParam().credentials_type != kInsecureCredentialsType);
yang-g88d5d522015-09-29 12:46:54 -07001193 }
1194};
1195
1196TEST_P(SecureEnd2endTest, SimpleRpcWithHost) {
1197 ResetStub();
1198
1199 EchoRequest request;
1200 EchoResponse response;
1201 request.set_message("Hello");
1202
1203 ClientContext context;
1204 context.set_authority("foo.test.youtube.com");
1205 Status s = stub_->Echo(&context, request, &response);
1206 EXPECT_EQ(response.message(), request.message());
1207 EXPECT_TRUE(response.has_param());
1208 EXPECT_EQ("special", response.param().host());
1209 EXPECT_TRUE(s.ok());
1210}
1211
yang-ge21908f2015-08-25 13:47:51 -07001212bool MetadataContains(
1213 const std::multimap<grpc::string_ref, grpc::string_ref>& metadata,
1214 const grpc::string& key, const grpc::string& value) {
Yang Gao26a49122015-05-15 17:02:56 -07001215 int count = 0;
1216
yang-ge21908f2015-08-25 13:47:51 -07001217 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator iter =
Yang Gao26a49122015-05-15 17:02:56 -07001218 metadata.begin();
1219 iter != metadata.end(); ++iter) {
yang-ge21908f2015-08-25 13:47:51 -07001220 if (ToString(iter->first) == key && ToString(iter->second) == value) {
Yang Gao26a49122015-05-15 17:02:56 -07001221 count++;
1222 }
1223 }
1224 return count == 1;
1225}
1226
yang-g88d5d522015-09-29 12:46:54 -07001227TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorSuccess) {
1228 auto* processor = new TestAuthMetadataProcessor(true);
1229 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1230 ResetStub();
1231 EchoRequest request;
1232 EchoResponse response;
1233 ClientContext context;
1234 context.set_credentials(processor->GetCompatibleClientCreds());
1235 request.set_message("Hello");
1236 request.mutable_param()->set_echo_metadata(true);
1237 request.mutable_param()->set_expected_client_identity(
1238 TestAuthMetadataProcessor::kGoodGuy);
Dan Bornf2f7d572016-03-03 17:26:12 -08001239 request.mutable_param()->set_expected_transport_security_type(
1240 GetParam().credentials_type);
yang-g88d5d522015-09-29 12:46:54 -07001241
1242 Status s = stub_->Echo(&context, request, &response);
1243 EXPECT_EQ(request.message(), response.message());
1244 EXPECT_TRUE(s.ok());
1245
1246 // Metadata should have been consumed by the processor.
1247 EXPECT_FALSE(MetadataContains(
1248 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1249 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1250}
1251
1252TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorFailure) {
1253 auto* processor = new TestAuthMetadataProcessor(true);
1254 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1255 ResetStub();
1256 EchoRequest request;
1257 EchoResponse response;
1258 ClientContext context;
1259 context.set_credentials(processor->GetIncompatibleClientCreds());
1260 request.set_message("Hello");
1261
1262 Status s = stub_->Echo(&context, request, &response);
1263 EXPECT_FALSE(s.ok());
1264 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1265}
1266TEST_P(SecureEnd2endTest, SetPerCallCredentials) {
1267 ResetStub();
Yang Gaoa8938922015-05-14 11:51:07 -07001268 EchoRequest request;
1269 EchoResponse response;
1270 ClientContext context;
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001271 std::shared_ptr<CallCredentials> creds =
Julien Boeuf510a9202015-08-25 21:51:07 -07001272 GoogleIAMCredentials("fake_token", "fake_selector");
Yang Gaoa8938922015-05-14 11:51:07 -07001273 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -07001274 request.set_message("Hello");
1275 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -07001276
1277 Status s = stub_->Echo(&context, request, &response);
Yang Gaoa8938922015-05-14 11:51:07 -07001278 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -07001279 EXPECT_TRUE(s.ok());
Yang Gao26a49122015-05-15 17:02:56 -07001280 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1281 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1282 "fake_token"));
1283 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1284 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1285 "fake_selector"));
Yang Gaoa8938922015-05-14 11:51:07 -07001286}
1287
yang-g88d5d522015-09-29 12:46:54 -07001288TEST_P(SecureEnd2endTest, OverridePerCallCredentials) {
1289 ResetStub();
Yang Gaoa8938922015-05-14 11:51:07 -07001290 EchoRequest request;
1291 EchoResponse response;
1292 ClientContext context;
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001293 std::shared_ptr<CallCredentials> creds1 =
Julien Boeuf510a9202015-08-25 21:51:07 -07001294 GoogleIAMCredentials("fake_token1", "fake_selector1");
Yang Gaoa8938922015-05-14 11:51:07 -07001295 context.set_credentials(creds1);
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001296 std::shared_ptr<CallCredentials> creds2 =
Julien Boeuf510a9202015-08-25 21:51:07 -07001297 GoogleIAMCredentials("fake_token2", "fake_selector2");
Yang Gaoa8938922015-05-14 11:51:07 -07001298 context.set_credentials(creds2);
Yang Gao26a49122015-05-15 17:02:56 -07001299 request.set_message("Hello");
1300 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -07001301
1302 Status s = stub_->Echo(&context, request, &response);
Yang Gao26a49122015-05-15 17:02:56 -07001303 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1304 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1305 "fake_token2"));
1306 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1307 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1308 "fake_selector2"));
Yang Gaob57f72d2015-05-17 21:54:54 -07001309 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
1310 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1311 "fake_token1"));
1312 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
1313 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1314 "fake_selector1"));
Yang Gaoa8938922015-05-14 11:51:07 -07001315 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -07001316 EXPECT_TRUE(s.ok());
Yang Gaoa8938922015-05-14 11:51:07 -07001317}
1318
yang-g88d5d522015-09-29 12:46:54 -07001319TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginFailure) {
1320 ResetStub();
Julien Boeuf1928d492015-09-15 15:20:11 -07001321 EchoRequest request;
1322 EchoResponse response;
1323 ClientContext context;
1324 context.set_credentials(
1325 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1326 new TestMetadataCredentialsPlugin(
1327 "Does not matter, will fail anyway (see 3rd param)", false,
1328 false))));
1329 request.set_message("Hello");
1330
1331 Status s = stub_->Echo(&context, request, &response);
1332 EXPECT_FALSE(s.ok());
1333 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1334}
1335
yang-g88d5d522015-09-29 12:46:54 -07001336TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorSuccess) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001337 auto* processor = new TestAuthMetadataProcessor(false);
1338 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
yang-g88d5d522015-09-29 12:46:54 -07001339 ResetStub();
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001340 EchoRequest request;
1341 EchoResponse response;
1342 ClientContext context;
1343 context.set_credentials(processor->GetCompatibleClientCreds());
1344 request.set_message("Hello");
1345 request.mutable_param()->set_echo_metadata(true);
1346 request.mutable_param()->set_expected_client_identity(
1347 TestAuthMetadataProcessor::kGoodGuy);
Dan Bornf2f7d572016-03-03 17:26:12 -08001348 request.mutable_param()->set_expected_transport_security_type(
1349 GetParam().credentials_type);
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001350
1351 Status s = stub_->Echo(&context, request, &response);
1352 EXPECT_EQ(request.message(), response.message());
1353 EXPECT_TRUE(s.ok());
1354
1355 // Metadata should have been consumed by the processor.
1356 EXPECT_FALSE(MetadataContains(
1357 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1358 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1359}
1360
yang-g88d5d522015-09-29 12:46:54 -07001361TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorFailure) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001362 auto* processor = new TestAuthMetadataProcessor(false);
1363 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
yang-g88d5d522015-09-29 12:46:54 -07001364 ResetStub();
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001365 EchoRequest request;
1366 EchoResponse response;
1367 ClientContext context;
1368 context.set_credentials(processor->GetIncompatibleClientCreds());
1369 request.set_message("Hello");
1370
1371 Status s = stub_->Echo(&context, request, &response);
1372 EXPECT_FALSE(s.ok());
1373 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1374}
1375
yang-g88d5d522015-09-29 12:46:54 -07001376TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginFailure) {
1377 ResetStub();
Julien Boeuf1928d492015-09-15 15:20:11 -07001378 EchoRequest request;
1379 EchoResponse response;
1380 ClientContext context;
1381 context.set_credentials(
1382 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1383 new TestMetadataCredentialsPlugin(
1384 "Does not matter, will fail anyway (see 3rd param)", true,
1385 false))));
1386 request.set_message("Hello");
1387
1388 Status s = stub_->Echo(&context, request, &response);
1389 EXPECT_FALSE(s.ok());
1390 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1391}
1392
yang-g88d5d522015-09-29 12:46:54 -07001393TEST_P(SecureEnd2endTest, ClientAuthContext) {
1394 ResetStub();
yang-gc4eef2e2015-07-06 23:26:58 -07001395 EchoRequest request;
1396 EchoResponse response;
1397 request.set_message("Hello");
Dan Bornf2f7d572016-03-03 17:26:12 -08001398 request.mutable_param()->set_check_auth_context(GetParam().credentials_type ==
1399 kTlsCredentialsType);
1400 request.mutable_param()->set_expected_transport_security_type(
1401 GetParam().credentials_type);
yang-gc4eef2e2015-07-06 23:26:58 -07001402 ClientContext context;
1403 Status s = stub_->Echo(&context, request, &response);
1404 EXPECT_EQ(response.message(), request.message());
1405 EXPECT_TRUE(s.ok());
1406
yang-g8b25f2a2015-07-21 23:54:36 -07001407 std::shared_ptr<const AuthContext> auth_ctx = context.auth_context();
Dan Bornf2f7d572016-03-03 17:26:12 -08001408 std::vector<grpc::string_ref> tst =
yang-g8b25f2a2015-07-21 23:54:36 -07001409 auth_ctx->FindPropertyValues("transport_security_type");
Robbie Shade820c1f32016-06-23 14:31:28 -04001410 ASSERT_EQ(1u, tst.size());
Dan Bornf2f7d572016-03-03 17:26:12 -08001411 EXPECT_EQ(GetParam().credentials_type, ToString(tst[0]));
1412 if (GetParam().credentials_type == kTlsCredentialsType) {
1413 EXPECT_EQ("x509_subject_alternative_name",
1414 auth_ctx->GetPeerIdentityPropertyName());
Paul Querna47d841d2016-03-10 11:19:17 -08001415 EXPECT_EQ(4u, auth_ctx->GetPeerIdentity().size());
Dan Bornf2f7d572016-03-03 17:26:12 -08001416 EXPECT_EQ("*.test.google.fr", ToString(auth_ctx->GetPeerIdentity()[0]));
1417 EXPECT_EQ("waterzooi.test.google.be",
1418 ToString(auth_ctx->GetPeerIdentity()[1]));
1419 EXPECT_EQ("*.test.youtube.com", ToString(auth_ctx->GetPeerIdentity()[2]));
Paul Querna47d841d2016-03-10 11:19:17 -08001420 EXPECT_EQ("192.168.1.3", ToString(auth_ctx->GetPeerIdentity()[3]));
Dan Bornf2f7d572016-03-03 17:26:12 -08001421 }
yang-gc4eef2e2015-07-06 23:26:58 -07001422}
1423
yang-g4c8aed32016-02-19 00:19:39 -08001424std::vector<TestScenario> CreateTestScenarios(bool use_proxy,
1425 bool test_insecure,
1426 bool test_secure) {
1427 std::vector<TestScenario> scenarios;
1428 std::vector<grpc::string> credentials_types;
1429 if (test_secure) {
1430 credentials_types = GetSecureCredentialsTypeList();
1431 }
1432 if (test_insecure) {
1433 credentials_types.push_back(kInsecureCredentialsType);
1434 }
1435 for (auto it = credentials_types.begin(); it != credentials_types.end();
1436 ++it) {
Vijay Pai679c75f2016-06-15 13:08:00 -07001437 scenarios.emplace_back(false, *it);
yang-g4c8aed32016-02-19 00:19:39 -08001438 if (use_proxy) {
Vijay Pai679c75f2016-06-15 13:08:00 -07001439 scenarios.emplace_back(true, *it);
yang-g4c8aed32016-02-19 00:19:39 -08001440 }
1441 }
1442 return scenarios;
1443}
yang-g6f30dec2015-07-22 23:11:56 -07001444
yang-g4c8aed32016-02-19 00:19:39 -08001445INSTANTIATE_TEST_CASE_P(End2end, End2endTest,
1446 ::testing::ValuesIn(CreateTestScenarios(false, true,
1447 true)));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001448
yang-g4c8aed32016-02-19 00:19:39 -08001449INSTANTIATE_TEST_CASE_P(End2endServerTryCancel, End2endServerTryCancelTest,
1450 ::testing::ValuesIn(CreateTestScenarios(false, true,
1451 false)));
1452
1453INSTANTIATE_TEST_CASE_P(ProxyEnd2end, ProxyEnd2endTest,
1454 ::testing::ValuesIn(CreateTestScenarios(true, true,
1455 true)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001456
yang-g88d5d522015-09-29 12:46:54 -07001457INSTANTIATE_TEST_CASE_P(SecureEnd2end, SecureEnd2endTest,
yang-g4c8aed32016-02-19 00:19:39 -08001458 ::testing::ValuesIn(CreateTestScenarios(false, false,
1459 true)));
yang-g88d5d522015-09-29 12:46:54 -07001460
yang-g8ab38362015-07-31 14:05:33 -07001461} // namespace
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001462} // namespace testing
1463} // namespace grpc
1464
1465int main(int argc, char** argv) {
1466 grpc_test_init(argc, argv);
1467 ::testing::InitGoogleTest(&argc, argv);
1468 return RUN_ALL_TESTS();
David Garcia Quintas2bf574f2016-01-14 15:27:08 -08001469}