blob: 0c9313f88f0d7e8ef09ade3ee458de00058d85e4 [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
Craig Tiller9533d042016-03-25 17:11:06 -070051#include "src/core/lib/security/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;
yang-g17197dd2016-02-19 00:04:22 -0800202 const grpc::string credentials_type;
yang-g88d5d522015-09-29 12:46:54 -0700203};
204
205class End2endTest : public ::testing::TestWithParam<TestScenario> {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206 protected:
Vijay Pai181ef452015-07-14 13:52:48 -0700207 End2endTest()
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700208 : is_server_started_(false),
209 kMaxMessageSize_(8192),
yang-g88d5d522015-09-29 12:46:54 -0700210 special_service_("special") {
211 GetParam().Log();
212 }
Craig Tiller7418d012015-02-11 15:25:03 -0800213
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700214 void TearDown() GRPC_OVERRIDE {
215 if (is_server_started_) {
216 server_->Shutdown();
217 if (proxy_server_) proxy_server_->Shutdown();
218 }
219 }
220
221 void StartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) {
Craig Tiller35e39712015-01-12 16:41:24 -0800222 int port = grpc_pick_unused_port_or_die();
yang-gd7ead692015-07-30 10:57:45 -0700223 server_address_ << "127.0.0.1:" << port;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800224 // Setup server
225 ServerBuilder builder;
yang-g7d2a3e12016-02-18 15:41:56 -0800226 auto server_creds = GetServerCredentials(GetParam().credentials_type);
yang-g17197dd2016-02-19 00:04:22 -0800227 if (GetParam().credentials_type != kInsecureCredentialsType) {
yang-g88d5d522015-09-29 12:46:54 -0700228 server_creds->SetAuthMetadataProcessor(processor);
229 }
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700230 builder.AddListeningPort(server_address_.str(), server_creds);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800231 builder.RegisterService(&service_);
yang-g8b25f2a2015-07-21 23:54:36 -0700232 builder.RegisterService("foo.test.youtube.com", &special_service_);
Yang Gaoc71a9d22015-05-04 00:22:12 -0700233 builder.SetMaxMessageSize(
234 kMaxMessageSize_); // For testing max message size.
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800235 builder.RegisterService(&dup_pkg_service_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800236 server_ = builder.BuildAndStart();
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700237 is_server_started_ = true;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700238 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800239
yang-g9b7757d2015-08-13 11:15:53 -0700240 void ResetChannel() {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700241 if (!is_server_started_) {
242 StartServer(std::shared_ptr<AuthMetadataProcessor>());
243 }
244 EXPECT_TRUE(is_server_started_);
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700245 ChannelArguments args;
yang-g7d2a3e12016-02-18 15:41:56 -0800246 auto channel_creds =
247 GetChannelCredentials(GetParam().credentials_type, &args);
yang-gd59ad7e2016-02-10 12:42:53 -0800248 if (!user_agent_prefix_.empty()) {
249 args.SetUserAgentPrefix(user_agent_prefix_);
250 }
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700251 args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test");
yang-g88d5d522015-09-29 12:46:54 -0700252 channel_ = CreateCustomChannel(server_address_.str(), channel_creds, args);
yang-g9b7757d2015-08-13 11:15:53 -0700253 }
254
yang-g88d5d522015-09-29 12:46:54 -0700255 void ResetStub() {
yang-g9b7757d2015-08-13 11:15:53 -0700256 ResetChannel();
yang-g88d5d522015-09-29 12:46:54 -0700257 if (GetParam().use_proxy) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700258 proxy_service_.reset(new Proxy(channel_));
259 int port = grpc_pick_unused_port_or_die();
260 std::ostringstream proxyaddr;
261 proxyaddr << "localhost:" << port;
262 ServerBuilder builder;
263 builder.AddListeningPort(proxyaddr.str(), InsecureServerCredentials());
264 builder.RegisterService(proxy_service_.get());
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700265 proxy_server_ = builder.BuildAndStart();
266
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700267 channel_ = CreateChannel(proxyaddr.str(), InsecureChannelCredentials());
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700268 }
269
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800270 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
yangg1456d152015-01-08 15:39:58 -0800271 }
272
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700273 bool is_server_started_;
yang-g8c2be9f2015-08-19 16:28:09 -0700274 std::shared_ptr<Channel> channel_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800275 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800276 std::unique_ptr<Server> server_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700277 std::unique_ptr<Server> proxy_server_;
278 std::unique_ptr<Proxy> proxy_service_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800279 std::ostringstream server_address_;
Yang Gao3921c562015-04-30 16:07:06 -0700280 const int kMaxMessageSize_;
nnoble0c475f02014-12-05 15:37:39 -0800281 TestServiceImpl service_;
Craig Tiller822d2c72015-07-07 16:08:00 -0700282 TestServiceImpl special_service_;
yangg1456d152015-01-08 15:39:58 -0800283 TestServiceImplDupPkg dup_pkg_service_;
yang-gd59ad7e2016-02-10 12:42:53 -0800284 grpc::string user_agent_prefix_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800285};
286
yang-gf8174ea2016-02-01 00:09:13 -0800287static void SendRpc(grpc::testing::EchoTestService::Stub* stub, int num_rpcs,
288 bool with_binary_metadata) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800289 EchoRequest request;
290 EchoResponse response;
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700291 request.set_message("Hello hello hello hello");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800292
293 for (int i = 0; i < num_rpcs; ++i) {
294 ClientContext context;
yang-gf8174ea2016-02-01 00:09:13 -0800295 if (with_binary_metadata) {
296 char bytes[8] = {'\0', '\1', '\2', '\3', '\4', '\5', '\6', (char)i};
297 context.AddMetadata("custom-bin", grpc::string(bytes, 8));
298 }
Craig Tillerbf6abee2015-07-19 22:31:04 -0700299 context.set_compression_algorithm(GRPC_COMPRESS_GZIP);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800300 Status s = stub->Echo(&context, request, &response);
301 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700302 EXPECT_TRUE(s.ok());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800303 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800304}
305
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800306// This class is for testing scenarios where RPCs are cancelled on the server
307// by calling ServerContext::TryCancel()
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800308class End2endServerTryCancelTest : public End2endTest {
309 protected:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800310 // Helper for testing client-streaming RPCs which are cancelled on the server.
311 // Depending on the value of server_try_cancel parameter, this will test one
312 // of the following three scenarios:
313 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading
314 // any messages from the client
315 //
316 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading
317 // messages from the client
318 //
319 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all
320 // the messages from the client
321 //
322 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800323 void TestRequestStreamServerCancel(
324 ServerTryCancelRequestPhase server_try_cancel, int num_msgs_to_send) {
325 ResetStub();
326 EchoRequest request;
327 EchoResponse response;
328 ClientContext context;
329
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800330 // Send server_try_cancel value in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800331 context.AddMetadata(kServerTryCancelRequest,
332 std::to_string(server_try_cancel));
333
334 auto stream = stub_->RequestStream(&context, &response);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800335
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800336 int num_msgs_sent = 0;
337 while (num_msgs_sent < num_msgs_to_send) {
338 request.set_message("hello");
339 if (!stream->Write(request)) {
340 break;
341 }
342 num_msgs_sent++;
343 }
344 gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800345
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800346 stream->WritesDone();
347 Status s = stream->Finish();
348
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800349 // At this point, we know for sure that RPC was cancelled by the server
350 // since we passed server_try_cancel value in the metadata. Depending on the
351 // value of server_try_cancel, the RPC might have been cancelled by the
352 // server at different stages. The following validates our expectations of
353 // number of messages sent in various cancellation scenarios:
354
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800355 switch (server_try_cancel) {
356 case CANCEL_BEFORE_PROCESSING:
357 case CANCEL_DURING_PROCESSING:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800358 // If the RPC is cancelled by server before / during messages from the
359 // client, it means that the client most likely did not get a chance to
360 // send all the messages it wanted to send. i.e num_msgs_sent <=
361 // num_msgs_to_send
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800362 EXPECT_LE(num_msgs_sent, num_msgs_to_send);
363 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800364
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800365 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800366 // If the RPC was cancelled after all messages were read by the server,
367 // the client did get a chance to send all its messages
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800368 EXPECT_EQ(num_msgs_sent, num_msgs_to_send);
369 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800370
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800371 default:
372 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
373 server_try_cancel);
374 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
375 server_try_cancel <= CANCEL_AFTER_PROCESSING);
376 break;
377 }
378
379 EXPECT_FALSE(s.ok());
380 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
381 }
382
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800383 // Helper for testing server-streaming RPCs which are cancelled on the server.
384 // Depending on the value of server_try_cancel parameter, this will test one
385 // of the following three scenarios:
386 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before writing
387 // any messages to the client
388 //
389 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while writing
390 // messages to the client
391 //
392 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after writing all
393 // the messages to the client
394 //
395 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800396 void TestResponseStreamServerCancel(
397 ServerTryCancelRequestPhase server_try_cancel) {
398 ResetStub();
399 EchoRequest request;
400 EchoResponse response;
401 ClientContext context;
402
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800403 // Send server_try_cancel in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800404 context.AddMetadata(kServerTryCancelRequest,
405 std::to_string(server_try_cancel));
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800406
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800407 request.set_message("hello");
408 auto stream = stub_->ResponseStream(&context, request);
409
410 int num_msgs_read = 0;
411 while (num_msgs_read < kNumResponseStreamsMsgs) {
412 if (!stream->Read(&response)) {
413 break;
414 }
415 EXPECT_EQ(response.message(),
416 request.message() + std::to_string(num_msgs_read));
417 num_msgs_read++;
418 }
419 gpr_log(GPR_INFO, "Read %d messages", num_msgs_read);
420
421 Status s = stream->Finish();
422
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800423 // Depending on the value of server_try_cancel, the RPC might have been
424 // cancelled by the server at different stages. The following validates our
425 // expectations of number of messages read in various cancellation
426 // scenarios:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800427 switch (server_try_cancel) {
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800428 case CANCEL_BEFORE_PROCESSING:
429 // Server cancelled before sending any messages. Which means the client
430 // wouldn't have read any
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800431 EXPECT_EQ(num_msgs_read, 0);
432 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800433
434 case CANCEL_DURING_PROCESSING:
435 // Server cancelled while writing messages. Client must have read less
436 // than or equal to the expected number of messages
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800437 EXPECT_LE(num_msgs_read, kNumResponseStreamsMsgs);
438 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800439
440 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla8d543e82016-02-29 18:22:25 -0800441 // Even though the Server cancelled after writing all messages, the RPC
442 // may be cancelled before the Client got a chance to read all the
443 // messages.
444 EXPECT_LE(num_msgs_read, kNumResponseStreamsMsgs);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800445 break;
446
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800447 default: {
448 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
449 server_try_cancel);
450 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
451 server_try_cancel <= CANCEL_AFTER_PROCESSING);
452 break;
453 }
454 }
455
456 EXPECT_FALSE(s.ok());
457 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
458 }
459
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800460 // Helper for testing bidirectional-streaming RPCs which are cancelled on the
461 // server. Depending on the value of server_try_cancel parameter, this will
462 // test one of the following three scenarios:
463 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading/
464 // writing any messages from/to the client
465 //
466 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading/
467 // writing messages from/to the client
468 //
469 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading/writing
470 // all the messages from/to the client
471 //
472 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800473 void TestBidiStreamServerCancel(ServerTryCancelRequestPhase server_try_cancel,
474 int num_messages) {
475 ResetStub();
476 EchoRequest request;
477 EchoResponse response;
478 ClientContext context;
479
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800480 // Send server_try_cancel in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800481 context.AddMetadata(kServerTryCancelRequest,
482 std::to_string(server_try_cancel));
483
484 auto stream = stub_->BidiStream(&context);
485
486 int num_msgs_read = 0;
487 int num_msgs_sent = 0;
488 while (num_msgs_sent < num_messages) {
489 request.set_message("hello " + std::to_string(num_msgs_sent));
490 if (!stream->Write(request)) {
491 break;
492 }
493 num_msgs_sent++;
494
495 if (!stream->Read(&response)) {
496 break;
497 }
498 num_msgs_read++;
499
500 EXPECT_EQ(response.message(), request.message());
501 }
502 gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent);
503 gpr_log(GPR_INFO, "Read %d messages", num_msgs_read);
504
505 stream->WritesDone();
506 Status s = stream->Finish();
507
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800508 // Depending on the value of server_try_cancel, the RPC might have been
509 // cancelled by the server at different stages. The following validates our
510 // expectations of number of messages read in various cancellation
511 // scenarios:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800512 switch (server_try_cancel) {
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800513 case CANCEL_BEFORE_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800514 EXPECT_EQ(num_msgs_read, 0);
515 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800516
517 case CANCEL_DURING_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800518 EXPECT_LE(num_msgs_sent, num_messages);
519 EXPECT_LE(num_msgs_read, num_msgs_sent);
520 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800521
522 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800523 EXPECT_EQ(num_msgs_sent, num_messages);
Sree Kuchibhotla8d543e82016-02-29 18:22:25 -0800524
525 // The Server cancelled after reading the last message and after writing
526 // the message to the client. However, the RPC cancellation might have
527 // taken effect before the client actually read the response.
528 EXPECT_LE(num_msgs_read, num_msgs_sent);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800529 break;
530
531 default:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800532 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
533 server_try_cancel);
534 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
535 server_try_cancel <= CANCEL_AFTER_PROCESSING);
536 break;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800537 }
538
539 EXPECT_FALSE(s.ok());
540 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
541 }
542};
543
544TEST_P(End2endServerTryCancelTest, RequestEchoServerCancel) {
545 ResetStub();
546 EchoRequest request;
547 EchoResponse response;
548 ClientContext context;
549
550 context.AddMetadata(kServerTryCancelRequest,
551 std::to_string(CANCEL_BEFORE_PROCESSING));
552 Status s = stub_->Echo(&context, request, &response);
553 EXPECT_FALSE(s.ok());
554 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
555}
556
557// Server to cancel before doing reading the request
558TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelBeforeReads) {
559 TestRequestStreamServerCancel(CANCEL_BEFORE_PROCESSING, 1);
560}
561
562// Server to cancel while reading a request from the stream in parallel
563TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelDuringRead) {
564 TestRequestStreamServerCancel(CANCEL_DURING_PROCESSING, 10);
565}
566
567// Server to cancel after reading all the requests but before returning to the
568// client
569TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelAfterReads) {
570 TestRequestStreamServerCancel(CANCEL_AFTER_PROCESSING, 4);
571}
572
573// Server to cancel before sending any response messages
574TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelBefore) {
575 TestResponseStreamServerCancel(CANCEL_BEFORE_PROCESSING);
576}
577
578// Server to cancel while writing a response to the stream in parallel
579TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelDuring) {
580 TestResponseStreamServerCancel(CANCEL_DURING_PROCESSING);
581}
582
583// Server to cancel after writing all the respones to the stream but before
584// returning to the client
585TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelAfter) {
586 TestResponseStreamServerCancel(CANCEL_AFTER_PROCESSING);
587}
588
589// Server to cancel before reading/writing any requests/responses on the stream
590TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelBefore) {
591 TestBidiStreamServerCancel(CANCEL_BEFORE_PROCESSING, 2);
592}
593
594// Server to cancel while reading/writing requests/responses on the stream in
595// parallel
596TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelDuring) {
597 TestBidiStreamServerCancel(CANCEL_DURING_PROCESSING, 10);
598}
599
600// Server to cancel after reading/writing all requests/responses on the stream
601// but before returning to the client
602TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelAfter) {
603 TestBidiStreamServerCancel(CANCEL_AFTER_PROCESSING, 5);
604}
605
yang-gd59ad7e2016-02-10 12:42:53 -0800606TEST_P(End2endTest, SimpleRpcWithCustomeUserAgentPrefix) {
607 user_agent_prefix_ = "custom_prefix";
608 ResetStub();
609 EchoRequest request;
610 EchoResponse response;
611 request.set_message("Hello hello hello hello");
612 request.mutable_param()->set_echo_metadata(true);
613
614 ClientContext context;
615 Status s = stub_->Echo(&context, request, &response);
616 EXPECT_EQ(response.message(), request.message());
617 EXPECT_TRUE(s.ok());
618 const auto& trailing_metadata = context.GetServerTrailingMetadata();
619 auto iter = trailing_metadata.find("user-agent");
620 EXPECT_TRUE(iter != trailing_metadata.end());
621 grpc::string expected_prefix = user_agent_prefix_ + " grpc-c++/";
622 EXPECT_TRUE(iter->second.starts_with(expected_prefix));
623}
624
yang-gf8174ea2016-02-01 00:09:13 -0800625TEST_P(End2endTest, MultipleRpcsWithVariedBinaryMetadataValue) {
626 ResetStub();
627 std::vector<std::thread*> threads;
628 for (int i = 0; i < 10; ++i) {
629 threads.push_back(new std::thread(SendRpc, stub_.get(), 10, true));
630 }
631 for (int i = 0; i < 10; ++i) {
632 threads[i]->join();
633 delete threads[i];
634 }
635}
636
637TEST_P(End2endTest, MultipleRpcs) {
638 ResetStub();
639 std::vector<std::thread*> threads;
640 for (int i = 0; i < 10; ++i) {
641 threads.push_back(new std::thread(SendRpc, stub_.get(), 10, false));
642 }
643 for (int i = 0; i < 10; ++i) {
644 threads[i]->join();
645 delete threads[i];
646 }
647}
648
yang-g88d5d522015-09-29 12:46:54 -0700649TEST_P(End2endTest, RequestStreamOneRequest) {
650 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800651 EchoRequest request;
652 EchoResponse response;
653 ClientContext context;
654
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800655 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800656 request.set_message("hello");
657 EXPECT_TRUE(stream->Write(request));
658 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800659 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800660 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700661 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800662}
663
yang-g88d5d522015-09-29 12:46:54 -0700664TEST_P(End2endTest, RequestStreamTwoRequests) {
665 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800666 EchoRequest request;
667 EchoResponse response;
668 ClientContext context;
669
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800670 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800671 request.set_message("hello");
672 EXPECT_TRUE(stream->Write(request));
673 EXPECT_TRUE(stream->Write(request));
674 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800675 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800676 EXPECT_EQ(response.message(), "hellohello");
Yang Gaoc1a2c312015-06-16 10:59:46 -0700677 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800678}
679
yang-g88d5d522015-09-29 12:46:54 -0700680TEST_P(End2endTest, ResponseStream) {
681 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800682 EchoRequest request;
683 EchoResponse response;
684 ClientContext context;
685 request.set_message("hello");
686
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800687 auto stream = stub_->ResponseStream(&context, request);
nnoble0c475f02014-12-05 15:37:39 -0800688 EXPECT_TRUE(stream->Read(&response));
689 EXPECT_EQ(response.message(), request.message() + "0");
690 EXPECT_TRUE(stream->Read(&response));
691 EXPECT_EQ(response.message(), request.message() + "1");
692 EXPECT_TRUE(stream->Read(&response));
693 EXPECT_EQ(response.message(), request.message() + "2");
694 EXPECT_FALSE(stream->Read(&response));
695
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800696 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700697 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800698}
699
yang-g88d5d522015-09-29 12:46:54 -0700700TEST_P(End2endTest, BidiStream) {
701 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800702 EchoRequest request;
703 EchoResponse response;
704 ClientContext context;
705 grpc::string msg("hello");
706
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800707 auto stream = stub_->BidiStream(&context);
nnoble0c475f02014-12-05 15:37:39 -0800708
709 request.set_message(msg + "0");
710 EXPECT_TRUE(stream->Write(request));
711 EXPECT_TRUE(stream->Read(&response));
712 EXPECT_EQ(response.message(), request.message());
713
714 request.set_message(msg + "1");
715 EXPECT_TRUE(stream->Write(request));
716 EXPECT_TRUE(stream->Read(&response));
717 EXPECT_EQ(response.message(), request.message());
718
719 request.set_message(msg + "2");
720 EXPECT_TRUE(stream->Write(request));
721 EXPECT_TRUE(stream->Read(&response));
722 EXPECT_EQ(response.message(), request.message());
723
724 stream->WritesDone();
725 EXPECT_FALSE(stream->Read(&response));
Craig Tillerca9a6372015-12-15 18:16:28 -0800726 EXPECT_FALSE(stream->Read(&response));
nnoble0c475f02014-12-05 15:37:39 -0800727
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800728 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700729 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800730}
731
732// Talk to the two services with the same name but different package names.
733// The two stubs are created on the same channel.
yang-g88d5d522015-09-29 12:46:54 -0700734TEST_P(End2endTest, DiffPackageServices) {
735 ResetStub();
yangg1456d152015-01-08 15:39:58 -0800736 EchoRequest request;
737 EchoResponse response;
738 request.set_message("Hello");
739
yangg1456d152015-01-08 15:39:58 -0800740 ClientContext context;
yang-g8b25f2a2015-07-21 23:54:36 -0700741 Status s = stub_->Echo(&context, request, &response);
yangg1456d152015-01-08 15:39:58 -0800742 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700743 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800744
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800745 std::unique_ptr<grpc::testing::duplicate::EchoTestService::Stub> dup_pkg_stub(
746 grpc::testing::duplicate::EchoTestService::NewStub(channel_));
yangg1456d152015-01-08 15:39:58 -0800747 ClientContext context2;
748 s = dup_pkg_stub->Echo(&context2, request, &response);
749 EXPECT_EQ("no package", response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700750 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800751}
752
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700753void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) {
Craig Tiller20b5fe92015-07-06 10:43:50 -0700754 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700755 gpr_time_from_micros(delay_us, GPR_TIMESPAN)));
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700756 while (!service->signal_client()) {
757 }
758 context->TryCancel();
759}
760
yang-ga89bf502015-11-17 14:19:17 -0800761TEST_P(End2endTest, CancelRpcBeforeStart) {
762 ResetStub();
763 EchoRequest request;
764 EchoResponse response;
765 ClientContext context;
766 request.set_message("hello");
767 context.TryCancel();
768 Status s = stub_->Echo(&context, request, &response);
769 EXPECT_EQ("", response.message());
770 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
771}
772
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700773// Client cancels request stream after sending two messages
yang-g88d5d522015-09-29 12:46:54 -0700774TEST_P(End2endTest, ClientCancelsRequestStream) {
775 ResetStub();
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700776 EchoRequest request;
777 EchoResponse response;
778 ClientContext context;
779 request.set_message("hello");
780
781 auto stream = stub_->RequestStream(&context, &response);
782 EXPECT_TRUE(stream->Write(request));
783 EXPECT_TRUE(stream->Write(request));
Yang Gaoc71a9d22015-05-04 00:22:12 -0700784
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700785 context.TryCancel();
786
787 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700788 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700789
Yang Gaoc71a9d22015-05-04 00:22:12 -0700790 EXPECT_EQ(response.message(), "");
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700791}
792
Abhishek Kumare41d0402015-04-17 14:12:33 -0700793// Client cancels server stream after sending some messages
yang-g88d5d522015-09-29 12:46:54 -0700794TEST_P(End2endTest, ClientCancelsResponseStream) {
795 ResetStub();
Abhishek Kumare41d0402015-04-17 14:12:33 -0700796 EchoRequest request;
797 EchoResponse response;
798 ClientContext context;
799 request.set_message("hello");
800
801 auto stream = stub_->ResponseStream(&context, request);
802
803 EXPECT_TRUE(stream->Read(&response));
804 EXPECT_EQ(response.message(), request.message() + "0");
805 EXPECT_TRUE(stream->Read(&response));
806 EXPECT_EQ(response.message(), request.message() + "1");
807
808 context.TryCancel();
809
810 // The cancellation races with responses, so there might be zero or
811 // one responses pending, read till failure
812
813 if (stream->Read(&response)) {
814 EXPECT_EQ(response.message(), request.message() + "2");
815 // Since we have cancelled, we expect the next attempt to read to fail
816 EXPECT_FALSE(stream->Read(&response));
817 }
818
819 Status s = stream->Finish();
Abhishek Kumar18298a72015-04-17 15:00:25 -0700820 // The final status could be either of CANCELLED or OK depending on
821 // who won the race.
Yang Gaoc1a2c312015-06-16 10:59:46 -0700822 EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumare41d0402015-04-17 14:12:33 -0700823}
824
Abhishek Kumar82a83312015-04-17 13:30:51 -0700825// Client cancels bidi stream after sending some messages
yang-g88d5d522015-09-29 12:46:54 -0700826TEST_P(End2endTest, ClientCancelsBidi) {
827 ResetStub();
Abhishek Kumar82a83312015-04-17 13:30:51 -0700828 EchoRequest request;
829 EchoResponse response;
830 ClientContext context;
831 grpc::string msg("hello");
832
833 auto stream = stub_->BidiStream(&context);
834
835 request.set_message(msg + "0");
836 EXPECT_TRUE(stream->Write(request));
837 EXPECT_TRUE(stream->Read(&response));
838 EXPECT_EQ(response.message(), request.message());
839
840 request.set_message(msg + "1");
841 EXPECT_TRUE(stream->Write(request));
842
843 context.TryCancel();
844
845 // The cancellation races with responses, so there might be zero or
846 // one responses pending, read till failure
847
848 if (stream->Read(&response)) {
849 EXPECT_EQ(response.message(), request.message());
850 // Since we have cancelled, we expect the next attempt to read to fail
851 EXPECT_FALSE(stream->Read(&response));
852 }
853
854 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700855 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumar82a83312015-04-17 13:30:51 -0700856}
857
yang-g88d5d522015-09-29 12:46:54 -0700858TEST_P(End2endTest, RpcMaxMessageSize) {
859 ResetStub();
Yang Gao3921c562015-04-30 16:07:06 -0700860 EchoRequest request;
861 EchoResponse response;
Yang Gaoc71a9d22015-05-04 00:22:12 -0700862 request.set_message(string(kMaxMessageSize_ * 2, 'a'));
Yang Gao3921c562015-04-30 16:07:06 -0700863
864 ClientContext context;
865 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700866 EXPECT_FALSE(s.ok());
Yang Gao3921c562015-04-30 16:07:06 -0700867}
Abhishek Kumar82a83312015-04-17 13:30:51 -0700868
yang-g88d5d522015-09-29 12:46:54 -0700869// Client sends 20 requests and the server returns CANCELLED status after
870// reading 10 requests.
871TEST_P(End2endTest, RequestStreamServerEarlyCancelTest) {
872 ResetStub();
873 EchoRequest request;
874 EchoResponse response;
875 ClientContext context;
876
877 context.AddMetadata(kServerCancelAfterReads, "10");
878 auto stream = stub_->RequestStream(&context, &response);
879 request.set_message("hello");
880 int send_messages = 20;
yang-gc0461032015-10-02 16:22:43 -0700881 while (send_messages > 10) {
yang-g88d5d522015-09-29 12:46:54 -0700882 EXPECT_TRUE(stream->Write(request));
883 send_messages--;
884 }
yang-gc0461032015-10-02 16:22:43 -0700885 while (send_messages > 0) {
886 stream->Write(request);
887 send_messages--;
888 }
yang-g88d5d522015-09-29 12:46:54 -0700889 stream->WritesDone();
890 Status s = stream->Finish();
891 EXPECT_EQ(s.error_code(), StatusCode::CANCELLED);
892}
893
yang-g88d5d522015-09-29 12:46:54 -0700894void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream,
895 gpr_event* ev) {
896 EchoResponse resp;
897 gpr_event_set(ev, (void*)1);
898 while (stream->Read(&resp)) {
899 gpr_log(GPR_INFO, "Read message");
900 }
901}
yang-g88d5d522015-09-29 12:46:54 -0700902
903// Run a Read and a WritesDone simultaneously.
904TEST_P(End2endTest, SimultaneousReadWritesDone) {
905 ResetStub();
906 ClientContext context;
907 gpr_event ev;
908 gpr_event_init(&ev);
909 auto stream = stub_->BidiStream(&context);
910 std::thread reader_thread(ReaderThreadFunc, stream.get(), &ev);
911 gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME));
912 stream->WritesDone();
Vijay Paibdfec2c2016-02-25 11:51:21 -0800913 reader_thread.join();
yang-g88d5d522015-09-29 12:46:54 -0700914 Status s = stream->Finish();
915 EXPECT_TRUE(s.ok());
yang-g88d5d522015-09-29 12:46:54 -0700916}
917
918TEST_P(End2endTest, ChannelState) {
919 ResetStub();
920 // Start IDLE
921 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(false));
922
923 // Did not ask to connect, no state change.
924 CompletionQueue cq;
925 std::chrono::system_clock::time_point deadline =
926 std::chrono::system_clock::now() + std::chrono::milliseconds(10);
927 channel_->NotifyOnStateChange(GRPC_CHANNEL_IDLE, deadline, &cq, NULL);
928 void* tag;
929 bool ok = true;
930 cq.Next(&tag, &ok);
931 EXPECT_FALSE(ok);
932
933 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true));
934 EXPECT_TRUE(channel_->WaitForStateChange(GRPC_CHANNEL_IDLE,
935 gpr_inf_future(GPR_CLOCK_REALTIME)));
yang-g0d557502015-10-01 11:30:12 -0700936 auto state = channel_->GetState(false);
937 EXPECT_TRUE(state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_READY);
yang-g88d5d522015-09-29 12:46:54 -0700938}
939
940// Takes 10s.
941TEST_P(End2endTest, ChannelStateTimeout) {
yang-g17197dd2016-02-19 00:04:22 -0800942 if (GetParam().credentials_type != kInsecureCredentialsType) {
yang-g88d5d522015-09-29 12:46:54 -0700943 return;
944 }
945 int port = grpc_pick_unused_port_or_die();
946 std::ostringstream server_address;
947 server_address << "127.0.0.1:" << port;
948 // Channel to non-existing server
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700949 auto channel =
950 CreateChannel(server_address.str(), InsecureChannelCredentials());
yang-g88d5d522015-09-29 12:46:54 -0700951 // Start IDLE
952 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel->GetState(true));
953
954 auto state = GRPC_CHANNEL_IDLE;
955 for (int i = 0; i < 10; i++) {
956 channel->WaitForStateChange(
957 state, std::chrono::system_clock::now() + std::chrono::seconds(1));
958 state = channel->GetState(false);
959 }
960}
961
962// Talking to a non-existing service.
963TEST_P(End2endTest, NonExistingService) {
964 ResetChannel();
Craig Tiller1b4e3302015-12-17 16:35:00 -0800965 std::unique_ptr<grpc::testing::UnimplementedService::Stub> stub;
966 stub = grpc::testing::UnimplementedService::NewStub(channel_);
yang-g88d5d522015-09-29 12:46:54 -0700967
968 EchoRequest request;
969 EchoResponse response;
970 request.set_message("Hello");
971
972 ClientContext context;
973 Status s = stub->Unimplemented(&context, request, &response);
974 EXPECT_EQ(StatusCode::UNIMPLEMENTED, s.error_code());
975 EXPECT_EQ("", s.error_message());
976}
977
978//////////////////////////////////////////////////////////////////////////
979// Test with and without a proxy.
980class ProxyEnd2endTest : public End2endTest {
981 protected:
982};
983
984TEST_P(ProxyEnd2endTest, SimpleRpc) {
985 ResetStub();
yang-gf8174ea2016-02-01 00:09:13 -0800986 SendRpc(stub_.get(), 1, false);
yang-g88d5d522015-09-29 12:46:54 -0700987}
988
989TEST_P(ProxyEnd2endTest, MultipleRpcs) {
990 ResetStub();
991 std::vector<std::thread*> threads;
992 for (int i = 0; i < 10; ++i) {
yang-gf8174ea2016-02-01 00:09:13 -0800993 threads.push_back(new std::thread(SendRpc, stub_.get(), 10, false));
yang-g88d5d522015-09-29 12:46:54 -0700994 }
995 for (int i = 0; i < 10; ++i) {
996 threads[i]->join();
997 delete threads[i];
998 }
999}
1000
1001// Set a 10us deadline and make sure proper error is returned.
1002TEST_P(ProxyEnd2endTest, RpcDeadlineExpires) {
1003 ResetStub();
1004 EchoRequest request;
1005 EchoResponse response;
1006 request.set_message("Hello");
Craig Tillerb0f275e2016-01-27 10:45:50 -08001007 request.mutable_param()->set_skip_cancelled_check(true);
yang-g88d5d522015-09-29 12:46:54 -07001008
1009 ClientContext context;
1010 std::chrono::system_clock::time_point deadline =
1011 std::chrono::system_clock::now() + std::chrono::microseconds(10);
1012 context.set_deadline(deadline);
1013 Status s = stub_->Echo(&context, request, &response);
1014 EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code());
1015}
1016
1017// Set a long but finite deadline.
1018TEST_P(ProxyEnd2endTest, RpcLongDeadline) {
1019 ResetStub();
1020 EchoRequest request;
1021 EchoResponse response;
1022 request.set_message("Hello");
1023
1024 ClientContext context;
1025 std::chrono::system_clock::time_point deadline =
1026 std::chrono::system_clock::now() + std::chrono::hours(1);
1027 context.set_deadline(deadline);
1028 Status s = stub_->Echo(&context, request, &response);
1029 EXPECT_EQ(response.message(), request.message());
1030 EXPECT_TRUE(s.ok());
1031}
1032
1033// Ask server to echo back the deadline it sees.
1034TEST_P(ProxyEnd2endTest, EchoDeadline) {
1035 ResetStub();
1036 EchoRequest request;
1037 EchoResponse response;
1038 request.set_message("Hello");
1039 request.mutable_param()->set_echo_deadline(true);
1040
1041 ClientContext context;
1042 std::chrono::system_clock::time_point deadline =
1043 std::chrono::system_clock::now() + std::chrono::seconds(100);
1044 context.set_deadline(deadline);
1045 Status s = stub_->Echo(&context, request, &response);
1046 EXPECT_EQ(response.message(), request.message());
1047 EXPECT_TRUE(s.ok());
1048 gpr_timespec sent_deadline;
1049 Timepoint2Timespec(deadline, &sent_deadline);
1050 // Allow 1 second error.
1051 EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1);
1052 EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1);
1053}
1054
1055// Ask server to echo back the deadline it sees. The rpc has no deadline.
1056TEST_P(ProxyEnd2endTest, EchoDeadlineForNoDeadlineRpc) {
1057 ResetStub();
1058 EchoRequest request;
1059 EchoResponse response;
1060 request.set_message("Hello");
1061 request.mutable_param()->set_echo_deadline(true);
1062
1063 ClientContext context;
1064 Status s = stub_->Echo(&context, request, &response);
1065 EXPECT_EQ(response.message(), request.message());
1066 EXPECT_TRUE(s.ok());
1067 EXPECT_EQ(response.param().request_deadline(),
1068 gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec);
1069}
1070
1071TEST_P(ProxyEnd2endTest, UnimplementedRpc) {
1072 ResetStub();
1073 EchoRequest request;
1074 EchoResponse response;
1075 request.set_message("Hello");
1076
1077 ClientContext context;
1078 Status s = stub_->Unimplemented(&context, request, &response);
1079 EXPECT_FALSE(s.ok());
1080 EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED);
1081 EXPECT_EQ(s.error_message(), "");
1082 EXPECT_EQ(response.message(), "");
1083}
1084
1085// Client cancels rpc after 10ms
1086TEST_P(ProxyEnd2endTest, ClientCancelsRpc) {
1087 ResetStub();
1088 EchoRequest request;
1089 EchoResponse response;
1090 request.set_message("Hello");
1091 const int kCancelDelayUs = 10 * 1000;
1092 request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs);
1093
1094 ClientContext context;
1095 std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_);
1096 Status s = stub_->Echo(&context, request, &response);
1097 cancel_thread.join();
1098 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
1099 EXPECT_EQ(s.error_message(), "Cancelled");
1100}
1101
1102// Server cancels rpc after 1ms
1103TEST_P(ProxyEnd2endTest, ServerCancelsRpc) {
1104 ResetStub();
1105 EchoRequest request;
1106 EchoResponse response;
1107 request.set_message("Hello");
1108 request.mutable_param()->set_server_cancel_after_us(1000);
1109
1110 ClientContext context;
1111 Status s = stub_->Echo(&context, request, &response);
1112 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
1113 EXPECT_TRUE(s.error_message().empty());
1114}
1115
1116// Make the response larger than the flow control window.
1117TEST_P(ProxyEnd2endTest, HugeResponse) {
1118 ResetStub();
1119 EchoRequest request;
1120 EchoResponse response;
1121 request.set_message("huge response");
1122 const size_t kResponseSize = 1024 * (1024 + 10);
1123 request.mutable_param()->set_response_message_length(kResponseSize);
1124
1125 ClientContext context;
1126 Status s = stub_->Echo(&context, request, &response);
1127 EXPECT_EQ(kResponseSize, response.message().size());
1128 EXPECT_TRUE(s.ok());
1129}
1130
1131TEST_P(ProxyEnd2endTest, Peer) {
1132 ResetStub();
1133 EchoRequest request;
1134 EchoResponse response;
1135 request.set_message("hello");
1136 request.mutable_param()->set_echo_peer(true);
1137
1138 ClientContext context;
1139 Status s = stub_->Echo(&context, request, &response);
1140 EXPECT_EQ(response.message(), request.message());
1141 EXPECT_TRUE(s.ok());
1142 EXPECT_TRUE(CheckIsLocalhost(response.param().peer()));
1143 EXPECT_TRUE(CheckIsLocalhost(context.peer()));
1144}
1145
1146//////////////////////////////////////////////////////////////////////////
1147class SecureEnd2endTest : public End2endTest {
1148 protected:
1149 SecureEnd2endTest() {
1150 GPR_ASSERT(!GetParam().use_proxy);
yang-g17197dd2016-02-19 00:04:22 -08001151 GPR_ASSERT(GetParam().credentials_type != kInsecureCredentialsType);
yang-g88d5d522015-09-29 12:46:54 -07001152 }
1153};
1154
1155TEST_P(SecureEnd2endTest, SimpleRpcWithHost) {
1156 ResetStub();
1157
1158 EchoRequest request;
1159 EchoResponse response;
1160 request.set_message("Hello");
1161
1162 ClientContext context;
1163 context.set_authority("foo.test.youtube.com");
1164 Status s = stub_->Echo(&context, request, &response);
1165 EXPECT_EQ(response.message(), request.message());
1166 EXPECT_TRUE(response.has_param());
1167 EXPECT_EQ("special", response.param().host());
1168 EXPECT_TRUE(s.ok());
1169}
1170
yang-ge21908f2015-08-25 13:47:51 -07001171bool MetadataContains(
1172 const std::multimap<grpc::string_ref, grpc::string_ref>& metadata,
1173 const grpc::string& key, const grpc::string& value) {
Yang Gao26a49122015-05-15 17:02:56 -07001174 int count = 0;
1175
yang-ge21908f2015-08-25 13:47:51 -07001176 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator iter =
Yang Gao26a49122015-05-15 17:02:56 -07001177 metadata.begin();
1178 iter != metadata.end(); ++iter) {
yang-ge21908f2015-08-25 13:47:51 -07001179 if (ToString(iter->first) == key && ToString(iter->second) == value) {
Yang Gao26a49122015-05-15 17:02:56 -07001180 count++;
1181 }
1182 }
1183 return count == 1;
1184}
1185
yang-g88d5d522015-09-29 12:46:54 -07001186TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorSuccess) {
1187 auto* processor = new TestAuthMetadataProcessor(true);
1188 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1189 ResetStub();
1190 EchoRequest request;
1191 EchoResponse response;
1192 ClientContext context;
1193 context.set_credentials(processor->GetCompatibleClientCreds());
1194 request.set_message("Hello");
1195 request.mutable_param()->set_echo_metadata(true);
1196 request.mutable_param()->set_expected_client_identity(
1197 TestAuthMetadataProcessor::kGoodGuy);
Dan Bornf2f7d572016-03-03 17:26:12 -08001198 request.mutable_param()->set_expected_transport_security_type(
1199 GetParam().credentials_type);
yang-g88d5d522015-09-29 12:46:54 -07001200
1201 Status s = stub_->Echo(&context, request, &response);
1202 EXPECT_EQ(request.message(), response.message());
1203 EXPECT_TRUE(s.ok());
1204
1205 // Metadata should have been consumed by the processor.
1206 EXPECT_FALSE(MetadataContains(
1207 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1208 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1209}
1210
1211TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorFailure) {
1212 auto* processor = new TestAuthMetadataProcessor(true);
1213 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1214 ResetStub();
1215 EchoRequest request;
1216 EchoResponse response;
1217 ClientContext context;
1218 context.set_credentials(processor->GetIncompatibleClientCreds());
1219 request.set_message("Hello");
1220
1221 Status s = stub_->Echo(&context, request, &response);
1222 EXPECT_FALSE(s.ok());
1223 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1224}
1225TEST_P(SecureEnd2endTest, SetPerCallCredentials) {
1226 ResetStub();
Yang Gaoa8938922015-05-14 11:51:07 -07001227 EchoRequest request;
1228 EchoResponse response;
1229 ClientContext context;
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001230 std::shared_ptr<CallCredentials> creds =
Julien Boeuf510a9202015-08-25 21:51:07 -07001231 GoogleIAMCredentials("fake_token", "fake_selector");
Yang Gaoa8938922015-05-14 11:51:07 -07001232 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -07001233 request.set_message("Hello");
1234 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -07001235
1236 Status s = stub_->Echo(&context, request, &response);
Yang Gaoa8938922015-05-14 11:51:07 -07001237 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -07001238 EXPECT_TRUE(s.ok());
Yang Gao26a49122015-05-15 17:02:56 -07001239 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1240 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1241 "fake_token"));
1242 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1243 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1244 "fake_selector"));
Yang Gaoa8938922015-05-14 11:51:07 -07001245}
1246
yang-g88d5d522015-09-29 12:46:54 -07001247TEST_P(SecureEnd2endTest, OverridePerCallCredentials) {
1248 ResetStub();
Yang Gaoa8938922015-05-14 11:51:07 -07001249 EchoRequest request;
1250 EchoResponse response;
1251 ClientContext context;
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001252 std::shared_ptr<CallCredentials> creds1 =
Julien Boeuf510a9202015-08-25 21:51:07 -07001253 GoogleIAMCredentials("fake_token1", "fake_selector1");
Yang Gaoa8938922015-05-14 11:51:07 -07001254 context.set_credentials(creds1);
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001255 std::shared_ptr<CallCredentials> creds2 =
Julien Boeuf510a9202015-08-25 21:51:07 -07001256 GoogleIAMCredentials("fake_token2", "fake_selector2");
Yang Gaoa8938922015-05-14 11:51:07 -07001257 context.set_credentials(creds2);
Yang Gao26a49122015-05-15 17:02:56 -07001258 request.set_message("Hello");
1259 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -07001260
1261 Status s = stub_->Echo(&context, request, &response);
Yang Gao26a49122015-05-15 17:02:56 -07001262 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1263 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1264 "fake_token2"));
1265 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1266 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1267 "fake_selector2"));
Yang Gaob57f72d2015-05-17 21:54:54 -07001268 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
1269 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1270 "fake_token1"));
1271 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
1272 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1273 "fake_selector1"));
Yang Gaoa8938922015-05-14 11:51:07 -07001274 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -07001275 EXPECT_TRUE(s.ok());
Yang Gaoa8938922015-05-14 11:51:07 -07001276}
1277
yang-g88d5d522015-09-29 12:46:54 -07001278TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginFailure) {
1279 ResetStub();
Julien Boeuf1928d492015-09-15 15:20:11 -07001280 EchoRequest request;
1281 EchoResponse response;
1282 ClientContext context;
1283 context.set_credentials(
1284 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1285 new TestMetadataCredentialsPlugin(
1286 "Does not matter, will fail anyway (see 3rd param)", false,
1287 false))));
1288 request.set_message("Hello");
1289
1290 Status s = stub_->Echo(&context, request, &response);
1291 EXPECT_FALSE(s.ok());
1292 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1293}
1294
yang-g88d5d522015-09-29 12:46:54 -07001295TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorSuccess) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001296 auto* processor = new TestAuthMetadataProcessor(false);
1297 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
yang-g88d5d522015-09-29 12:46:54 -07001298 ResetStub();
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001299 EchoRequest request;
1300 EchoResponse response;
1301 ClientContext context;
1302 context.set_credentials(processor->GetCompatibleClientCreds());
1303 request.set_message("Hello");
1304 request.mutable_param()->set_echo_metadata(true);
1305 request.mutable_param()->set_expected_client_identity(
1306 TestAuthMetadataProcessor::kGoodGuy);
Dan Bornf2f7d572016-03-03 17:26:12 -08001307 request.mutable_param()->set_expected_transport_security_type(
1308 GetParam().credentials_type);
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001309
1310 Status s = stub_->Echo(&context, request, &response);
1311 EXPECT_EQ(request.message(), response.message());
1312 EXPECT_TRUE(s.ok());
1313
1314 // Metadata should have been consumed by the processor.
1315 EXPECT_FALSE(MetadataContains(
1316 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1317 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1318}
1319
yang-g88d5d522015-09-29 12:46:54 -07001320TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorFailure) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001321 auto* processor = new TestAuthMetadataProcessor(false);
1322 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
yang-g88d5d522015-09-29 12:46:54 -07001323 ResetStub();
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001324 EchoRequest request;
1325 EchoResponse response;
1326 ClientContext context;
1327 context.set_credentials(processor->GetIncompatibleClientCreds());
1328 request.set_message("Hello");
1329
1330 Status s = stub_->Echo(&context, request, &response);
1331 EXPECT_FALSE(s.ok());
1332 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1333}
1334
yang-g88d5d522015-09-29 12:46:54 -07001335TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginFailure) {
1336 ResetStub();
Julien Boeuf1928d492015-09-15 15:20:11 -07001337 EchoRequest request;
1338 EchoResponse response;
1339 ClientContext context;
1340 context.set_credentials(
1341 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1342 new TestMetadataCredentialsPlugin(
1343 "Does not matter, will fail anyway (see 3rd param)", true,
1344 false))));
1345 request.set_message("Hello");
1346
1347 Status s = stub_->Echo(&context, request, &response);
1348 EXPECT_FALSE(s.ok());
1349 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1350}
1351
yang-g88d5d522015-09-29 12:46:54 -07001352TEST_P(SecureEnd2endTest, ClientAuthContext) {
1353 ResetStub();
yang-gc4eef2e2015-07-06 23:26:58 -07001354 EchoRequest request;
1355 EchoResponse response;
1356 request.set_message("Hello");
Dan Bornf2f7d572016-03-03 17:26:12 -08001357 request.mutable_param()->set_check_auth_context(GetParam().credentials_type ==
1358 kTlsCredentialsType);
1359 request.mutable_param()->set_expected_transport_security_type(
1360 GetParam().credentials_type);
yang-gc4eef2e2015-07-06 23:26:58 -07001361 ClientContext context;
1362 Status s = stub_->Echo(&context, request, &response);
1363 EXPECT_EQ(response.message(), request.message());
1364 EXPECT_TRUE(s.ok());
1365
yang-g8b25f2a2015-07-21 23:54:36 -07001366 std::shared_ptr<const AuthContext> auth_ctx = context.auth_context();
Dan Bornf2f7d572016-03-03 17:26:12 -08001367 std::vector<grpc::string_ref> tst =
yang-g8b25f2a2015-07-21 23:54:36 -07001368 auth_ctx->FindPropertyValues("transport_security_type");
Dan Bornf2f7d572016-03-03 17:26:12 -08001369 EXPECT_EQ(1u, tst.size());
1370 EXPECT_EQ(GetParam().credentials_type, ToString(tst[0]));
1371 if (GetParam().credentials_type == kTlsCredentialsType) {
1372 EXPECT_EQ("x509_subject_alternative_name",
1373 auth_ctx->GetPeerIdentityPropertyName());
Paul Querna47d841d2016-03-10 11:19:17 -08001374 EXPECT_EQ(4u, auth_ctx->GetPeerIdentity().size());
Dan Bornf2f7d572016-03-03 17:26:12 -08001375 EXPECT_EQ("*.test.google.fr", ToString(auth_ctx->GetPeerIdentity()[0]));
1376 EXPECT_EQ("waterzooi.test.google.be",
1377 ToString(auth_ctx->GetPeerIdentity()[1]));
1378 EXPECT_EQ("*.test.youtube.com", ToString(auth_ctx->GetPeerIdentity()[2]));
Paul Querna47d841d2016-03-10 11:19:17 -08001379 EXPECT_EQ("192.168.1.3", ToString(auth_ctx->GetPeerIdentity()[3]));
Dan Bornf2f7d572016-03-03 17:26:12 -08001380 }
yang-gc4eef2e2015-07-06 23:26:58 -07001381}
1382
yang-g4c8aed32016-02-19 00:19:39 -08001383std::vector<TestScenario> CreateTestScenarios(bool use_proxy,
1384 bool test_insecure,
1385 bool test_secure) {
1386 std::vector<TestScenario> scenarios;
1387 std::vector<grpc::string> credentials_types;
1388 if (test_secure) {
1389 credentials_types = GetSecureCredentialsTypeList();
1390 }
1391 if (test_insecure) {
1392 credentials_types.push_back(kInsecureCredentialsType);
1393 }
1394 for (auto it = credentials_types.begin(); it != credentials_types.end();
1395 ++it) {
1396 scenarios.push_back(TestScenario(false, *it));
1397 if (use_proxy) {
1398 scenarios.push_back(TestScenario(true, *it));
1399 }
1400 }
1401 return scenarios;
1402}
yang-g6f30dec2015-07-22 23:11:56 -07001403
yang-g4c8aed32016-02-19 00:19:39 -08001404INSTANTIATE_TEST_CASE_P(End2end, End2endTest,
1405 ::testing::ValuesIn(CreateTestScenarios(false, true,
1406 true)));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001407
yang-g4c8aed32016-02-19 00:19:39 -08001408INSTANTIATE_TEST_CASE_P(End2endServerTryCancel, End2endServerTryCancelTest,
1409 ::testing::ValuesIn(CreateTestScenarios(false, true,
1410 false)));
1411
1412INSTANTIATE_TEST_CASE_P(ProxyEnd2end, ProxyEnd2endTest,
1413 ::testing::ValuesIn(CreateTestScenarios(true, true,
1414 true)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001415
yang-g88d5d522015-09-29 12:46:54 -07001416INSTANTIATE_TEST_CASE_P(SecureEnd2end, SecureEnd2endTest,
yang-g4c8aed32016-02-19 00:19:39 -08001417 ::testing::ValuesIn(CreateTestScenarios(false, false,
1418 true)));
yang-g88d5d522015-09-29 12:46:54 -07001419
yang-g8ab38362015-07-31 14:05:33 -07001420} // namespace
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001421} // namespace testing
1422} // namespace grpc
1423
1424int main(int argc, char** argv) {
1425 grpc_test_init(argc, argv);
1426 ::testing::InitGoogleTest(&argc, argv);
1427 return RUN_ALL_TESTS();
David Garcia Quintas2bf574f2016-01-14 15:27:08 -08001428}