blob: df9aae7436fc454214583f992da2d6465e512dc6 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
murgatroid99ace28d32016-01-14 10:12:10 -08003 * Copyright 2015-2016, 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
Yang Gao26a49122015-05-15 17:02:56 -070051#include "src/core/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;
yangged5e7e02015-01-06 10:16:15 -080062using std::chrono::system_clock;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063
64namespace grpc {
yangged5e7e02015-01-06 10:16:15 -080065namespace testing {
yangged5e7e02015-01-06 10:16:15 -080066namespace {
67
yang-gd7ead692015-07-30 10:57:45 -070068bool CheckIsLocalhost(const grpc::string& addr) {
69 const grpc::string kIpv6("ipv6:[::1]:");
70 const grpc::string kIpv4MappedIpv6("ipv6:[::ffff:127.0.0.1]:");
71 const grpc::string kIpv4("ipv4:127.0.0.1:");
72 return addr.substr(0, kIpv4.size()) == kIpv4 ||
73 addr.substr(0, kIpv4MappedIpv6.size()) == kIpv4MappedIpv6 ||
74 addr.substr(0, kIpv6.size()) == kIpv6;
75}
76
Julien Boeuf1928d492015-09-15 15:20:11 -070077class TestMetadataCredentialsPlugin : public MetadataCredentialsPlugin {
78 public:
79 static const char kMetadataKey[];
80
81 TestMetadataCredentialsPlugin(grpc::string_ref metadata_value,
82 bool is_blocking, bool is_successful)
83 : metadata_value_(metadata_value.data(), metadata_value.length()),
84 is_blocking_(is_blocking),
85 is_successful_(is_successful) {}
86
87 bool IsBlocking() const GRPC_OVERRIDE { return is_blocking_; }
88
Julien Boeuf114f3942015-11-19 21:45:52 -080089 Status GetMetadata(grpc::string_ref service_url, grpc::string_ref method_name,
90 const grpc::AuthContext& channel_auth_context,
Julien Boeuf8b0b6f42015-09-22 13:31:16 -070091 std::multimap<grpc::string, grpc::string>* metadata)
Julien Boeuf1928d492015-09-15 15:20:11 -070092 GRPC_OVERRIDE {
93 EXPECT_GT(service_url.length(), 0UL);
Julien Boeuf114f3942015-11-19 21:45:52 -080094 EXPECT_GT(method_name.length(), 0UL);
95 EXPECT_TRUE(channel_auth_context.IsPeerAuthenticated());
Julien Boeuf1928d492015-09-15 15:20:11 -070096 EXPECT_TRUE(metadata != nullptr);
97 if (is_successful_) {
98 metadata->insert(std::make_pair(kMetadataKey, metadata_value_));
99 return Status::OK;
100 } else {
101 return Status(StatusCode::NOT_FOUND, "Could not find plugin metadata.");
102 }
103 }
104
105 private:
106 grpc::string metadata_value_;
107 bool is_blocking_;
108 bool is_successful_;
109};
110
111const char TestMetadataCredentialsPlugin::kMetadataKey[] = "TestPluginMetadata";
112
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700113class TestAuthMetadataProcessor : public AuthMetadataProcessor {
114 public:
115 static const char kGoodGuy[];
116
117 TestAuthMetadataProcessor(bool is_blocking) : is_blocking_(is_blocking) {}
118
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700119 std::shared_ptr<CallCredentials> GetCompatibleClientCreds() {
Julien Boeuf1928d492015-09-15 15:20:11 -0700120 return MetadataCredentialsFromPlugin(
121 std::unique_ptr<MetadataCredentialsPlugin>(
122 new TestMetadataCredentialsPlugin(kGoodGuy, is_blocking_, true)));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700123 }
Julien Boeuf1928d492015-09-15 15:20:11 -0700124
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700125 std::shared_ptr<CallCredentials> GetIncompatibleClientCreds() {
Julien Boeuf1928d492015-09-15 15:20:11 -0700126 return MetadataCredentialsFromPlugin(
127 std::unique_ptr<MetadataCredentialsPlugin>(
128 new TestMetadataCredentialsPlugin("Mr Hyde", is_blocking_, true)));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700129 }
130
131 // Interface implementation
132 bool IsBlocking() const GRPC_OVERRIDE { return is_blocking_; }
133
134 Status Process(const InputMetadata& auth_metadata, AuthContext* context,
135 OutputMetadata* consumed_auth_metadata,
136 OutputMetadata* response_metadata) GRPC_OVERRIDE {
137 EXPECT_TRUE(consumed_auth_metadata != nullptr);
138 EXPECT_TRUE(context != nullptr);
139 EXPECT_TRUE(response_metadata != nullptr);
Julien Boeuf1928d492015-09-15 15:20:11 -0700140 auto auth_md =
141 auth_metadata.find(TestMetadataCredentialsPlugin::kMetadataKey);
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700142 EXPECT_NE(auth_md, auth_metadata.end());
143 string_ref auth_md_value = auth_md->second;
Julien Boeuf1928d492015-09-15 15:20:11 -0700144 if (auth_md_value == kGoodGuy) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700145 context->AddProperty(kIdentityPropName, kGoodGuy);
146 context->SetPeerIdentityPropertyName(kIdentityPropName);
Julien Boeuf8b0b6f42015-09-22 13:31:16 -0700147 consumed_auth_metadata->insert(std::make_pair(
148 string(auth_md->first.data(), auth_md->first.length()),
149 string(auth_md->second.data(), auth_md->second.length())));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700150 return Status::OK;
151 } else {
152 return Status(StatusCode::UNAUTHENTICATED,
153 string("Invalid principal: ") +
154 string(auth_md_value.data(), auth_md_value.length()));
155 }
156 }
157
Julien Boeuf1928d492015-09-15 15:20:11 -0700158 private:
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700159 static const char kIdentityPropName[];
160 bool is_blocking_;
161};
162
163const char TestAuthMetadataProcessor::kGoodGuy[] = "Dr Jekyll";
164const char TestAuthMetadataProcessor::kIdentityPropName[] = "novel identity";
165
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800166class Proxy : public ::grpc::testing::EchoTestService::Service {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700167 public:
yang-g8c2be9f2015-08-19 16:28:09 -0700168 Proxy(std::shared_ptr<Channel> channel)
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800169 : stub_(grpc::testing::EchoTestService::NewStub(channel)) {}
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700170
171 Status Echo(ServerContext* server_context, const EchoRequest* request,
172 EchoResponse* response) GRPC_OVERRIDE {
173 std::unique_ptr<ClientContext> client_context =
174 ClientContext::FromServerContext(*server_context);
175 return stub_->Echo(client_context.get(), *request, response);
176 }
177
178 private:
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800179 std::unique_ptr< ::grpc::testing::EchoTestService::Stub> stub_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700180};
181
yangg1456d152015-01-08 15:39:58 -0800182class TestServiceImplDupPkg
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800183 : public ::grpc::testing::duplicate::EchoTestService::Service {
yangg1456d152015-01-08 15:39:58 -0800184 public:
185 Status Echo(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800186 EchoResponse* response) GRPC_OVERRIDE {
yangg1456d152015-01-08 15:39:58 -0800187 response->set_message("no package");
188 return Status::OK;
189 }
190};
191
yang-g88d5d522015-09-29 12:46:54 -0700192class TestScenario {
193 public:
yang-g17197dd2016-02-19 00:04:22 -0800194 TestScenario(bool proxy, const grpc::string& creds_type)
195 : use_proxy(proxy), credentials_type(creds_type) {}
yang-g88d5d522015-09-29 12:46:54 -0700196 void Log() const {
yang-g7d2a3e12016-02-18 15:41:56 -0800197 gpr_log(GPR_INFO, "Scenario: proxy %d, credentials %s", use_proxy,
yang-g17197dd2016-02-19 00:04:22 -0800198 credentials_type.c_str());
yang-g88d5d522015-09-29 12:46:54 -0700199 }
200 bool use_proxy;
yang-g17197dd2016-02-19 00:04:22 -0800201 const grpc::string credentials_type;
yang-g88d5d522015-09-29 12:46:54 -0700202};
203
204class End2endTest : public ::testing::TestWithParam<TestScenario> {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800205 protected:
Vijay Pai181ef452015-07-14 13:52:48 -0700206 End2endTest()
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700207 : is_server_started_(false),
208 kMaxMessageSize_(8192),
yang-g88d5d522015-09-29 12:46:54 -0700209 special_service_("special") {
210 GetParam().Log();
211 }
Craig Tiller7418d012015-02-11 15:25:03 -0800212
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700213 void TearDown() GRPC_OVERRIDE {
214 if (is_server_started_) {
215 server_->Shutdown();
216 if (proxy_server_) proxy_server_->Shutdown();
217 }
218 }
219
220 void StartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) {
Craig Tiller35e39712015-01-12 16:41:24 -0800221 int port = grpc_pick_unused_port_or_die();
yang-gd7ead692015-07-30 10:57:45 -0700222 server_address_ << "127.0.0.1:" << port;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800223 // Setup server
224 ServerBuilder builder;
yang-g7d2a3e12016-02-18 15:41:56 -0800225 auto server_creds = GetServerCredentials(GetParam().credentials_type);
yang-g17197dd2016-02-19 00:04:22 -0800226 if (GetParam().credentials_type != kInsecureCredentialsType) {
yang-g88d5d522015-09-29 12:46:54 -0700227 server_creds->SetAuthMetadataProcessor(processor);
228 }
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700229 builder.AddListeningPort(server_address_.str(), server_creds);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800230 builder.RegisterService(&service_);
yang-g8b25f2a2015-07-21 23:54:36 -0700231 builder.RegisterService("foo.test.youtube.com", &special_service_);
Yang Gaoc71a9d22015-05-04 00:22:12 -0700232 builder.SetMaxMessageSize(
233 kMaxMessageSize_); // For testing max message size.
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800234 builder.RegisterService(&dup_pkg_service_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800235 server_ = builder.BuildAndStart();
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700236 is_server_started_ = true;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700237 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800238
yang-g9b7757d2015-08-13 11:15:53 -0700239 void ResetChannel() {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700240 if (!is_server_started_) {
241 StartServer(std::shared_ptr<AuthMetadataProcessor>());
242 }
243 EXPECT_TRUE(is_server_started_);
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700244 ChannelArguments args;
yang-g7d2a3e12016-02-18 15:41:56 -0800245 auto channel_creds =
246 GetChannelCredentials(GetParam().credentials_type, &args);
yang-gd59ad7e2016-02-10 12:42:53 -0800247 if (!user_agent_prefix_.empty()) {
248 args.SetUserAgentPrefix(user_agent_prefix_);
249 }
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700250 args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test");
yang-g88d5d522015-09-29 12:46:54 -0700251 channel_ = CreateCustomChannel(server_address_.str(), channel_creds, args);
yang-g9b7757d2015-08-13 11:15:53 -0700252 }
253
yang-g88d5d522015-09-29 12:46:54 -0700254 void ResetStub() {
yang-g9b7757d2015-08-13 11:15:53 -0700255 ResetChannel();
yang-g88d5d522015-09-29 12:46:54 -0700256 if (GetParam().use_proxy) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700257 proxy_service_.reset(new Proxy(channel_));
258 int port = grpc_pick_unused_port_or_die();
259 std::ostringstream proxyaddr;
260 proxyaddr << "localhost:" << port;
261 ServerBuilder builder;
262 builder.AddListeningPort(proxyaddr.str(), InsecureServerCredentials());
263 builder.RegisterService(proxy_service_.get());
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700264 proxy_server_ = builder.BuildAndStart();
265
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700266 channel_ = CreateChannel(proxyaddr.str(), InsecureChannelCredentials());
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700267 }
268
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800269 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
yangg1456d152015-01-08 15:39:58 -0800270 }
271
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700272 bool is_server_started_;
yang-g8c2be9f2015-08-19 16:28:09 -0700273 std::shared_ptr<Channel> channel_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800274 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800275 std::unique_ptr<Server> server_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700276 std::unique_ptr<Server> proxy_server_;
277 std::unique_ptr<Proxy> proxy_service_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800278 std::ostringstream server_address_;
Yang Gao3921c562015-04-30 16:07:06 -0700279 const int kMaxMessageSize_;
nnoble0c475f02014-12-05 15:37:39 -0800280 TestServiceImpl service_;
Craig Tiller822d2c72015-07-07 16:08:00 -0700281 TestServiceImpl special_service_;
yangg1456d152015-01-08 15:39:58 -0800282 TestServiceImplDupPkg dup_pkg_service_;
yang-gd59ad7e2016-02-10 12:42:53 -0800283 grpc::string user_agent_prefix_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800284};
285
yang-gf8174ea2016-02-01 00:09:13 -0800286static void SendRpc(grpc::testing::EchoTestService::Stub* stub, int num_rpcs,
287 bool with_binary_metadata) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800288 EchoRequest request;
289 EchoResponse response;
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700290 request.set_message("Hello hello hello hello");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800291
292 for (int i = 0; i < num_rpcs; ++i) {
293 ClientContext context;
yang-gf8174ea2016-02-01 00:09:13 -0800294 if (with_binary_metadata) {
295 char bytes[8] = {'\0', '\1', '\2', '\3', '\4', '\5', '\6', (char)i};
296 context.AddMetadata("custom-bin", grpc::string(bytes, 8));
297 }
Craig Tillerbf6abee2015-07-19 22:31:04 -0700298 context.set_compression_algorithm(GRPC_COMPRESS_GZIP);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800299 Status s = stub->Echo(&context, request, &response);
300 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700301 EXPECT_TRUE(s.ok());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800302 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800303}
304
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800305// This class is for testing scenarios where RPCs are cancelled on the server
306// by calling ServerContext::TryCancel()
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800307class End2endServerTryCancelTest : public End2endTest {
308 protected:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800309 // Helper for testing client-streaming RPCs which are cancelled on the server.
310 // Depending on the value of server_try_cancel parameter, this will test one
311 // of the following three scenarios:
312 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading
313 // any messages from the client
314 //
315 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading
316 // messages from the client
317 //
318 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all
319 // the messages from the client
320 //
321 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800322 void TestRequestStreamServerCancel(
323 ServerTryCancelRequestPhase server_try_cancel, int num_msgs_to_send) {
324 ResetStub();
325 EchoRequest request;
326 EchoResponse response;
327 ClientContext context;
328
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800329 // Send server_try_cancel value in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800330 context.AddMetadata(kServerTryCancelRequest,
331 std::to_string(server_try_cancel));
332
333 auto stream = stub_->RequestStream(&context, &response);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800334
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800335 int num_msgs_sent = 0;
336 while (num_msgs_sent < num_msgs_to_send) {
337 request.set_message("hello");
338 if (!stream->Write(request)) {
339 break;
340 }
341 num_msgs_sent++;
342 }
343 gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800344
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800345 stream->WritesDone();
346 Status s = stream->Finish();
347
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800348 // At this point, we know for sure that RPC was cancelled by the server
349 // since we passed server_try_cancel value in the metadata. Depending on the
350 // value of server_try_cancel, the RPC might have been cancelled by the
351 // server at different stages. The following validates our expectations of
352 // number of messages sent in various cancellation scenarios:
353
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800354 switch (server_try_cancel) {
355 case CANCEL_BEFORE_PROCESSING:
356 case CANCEL_DURING_PROCESSING:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800357 // If the RPC is cancelled by server before / during messages from the
358 // client, it means that the client most likely did not get a chance to
359 // send all the messages it wanted to send. i.e num_msgs_sent <=
360 // num_msgs_to_send
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800361 EXPECT_LE(num_msgs_sent, num_msgs_to_send);
362 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800363
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800364 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800365 // If the RPC was cancelled after all messages were read by the server,
366 // the client did get a chance to send all its messages
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800367 EXPECT_EQ(num_msgs_sent, num_msgs_to_send);
368 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800369
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800370 default:
371 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
372 server_try_cancel);
373 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
374 server_try_cancel <= CANCEL_AFTER_PROCESSING);
375 break;
376 }
377
378 EXPECT_FALSE(s.ok());
379 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
380 }
381
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800382 // Helper for testing server-streaming RPCs which are cancelled on the server.
383 // Depending on the value of server_try_cancel parameter, this will test one
384 // of the following three scenarios:
385 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before writing
386 // any messages to the client
387 //
388 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while writing
389 // messages to the client
390 //
391 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after writing all
392 // the messages to the client
393 //
394 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800395 void TestResponseStreamServerCancel(
396 ServerTryCancelRequestPhase server_try_cancel) {
397 ResetStub();
398 EchoRequest request;
399 EchoResponse response;
400 ClientContext context;
401
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800402 // Send server_try_cancel in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800403 context.AddMetadata(kServerTryCancelRequest,
404 std::to_string(server_try_cancel));
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800405
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800406 request.set_message("hello");
407 auto stream = stub_->ResponseStream(&context, request);
408
409 int num_msgs_read = 0;
410 while (num_msgs_read < kNumResponseStreamsMsgs) {
411 if (!stream->Read(&response)) {
412 break;
413 }
414 EXPECT_EQ(response.message(),
415 request.message() + std::to_string(num_msgs_read));
416 num_msgs_read++;
417 }
418 gpr_log(GPR_INFO, "Read %d messages", num_msgs_read);
419
420 Status s = stream->Finish();
421
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800422 // Depending on the value of server_try_cancel, the RPC might have been
423 // cancelled by the server at different stages. The following validates our
424 // expectations of number of messages read in various cancellation
425 // scenarios:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800426 switch (server_try_cancel) {
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800427 case CANCEL_BEFORE_PROCESSING:
428 // Server cancelled before sending any messages. Which means the client
429 // wouldn't have read any
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800430 EXPECT_EQ(num_msgs_read, 0);
431 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800432
433 case CANCEL_DURING_PROCESSING:
434 // Server cancelled while writing messages. Client must have read less
435 // than or equal to the expected number of messages
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800436 EXPECT_LE(num_msgs_read, kNumResponseStreamsMsgs);
437 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800438
439 case CANCEL_AFTER_PROCESSING:
440 // Server cancelled after writing all messages. Client must have read
441 // all messages
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800442 EXPECT_EQ(num_msgs_read, kNumResponseStreamsMsgs);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800443 break;
444
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800445 default: {
446 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
447 server_try_cancel);
448 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
449 server_try_cancel <= CANCEL_AFTER_PROCESSING);
450 break;
451 }
452 }
453
454 EXPECT_FALSE(s.ok());
455 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
456 }
457
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800458 // Helper for testing bidirectional-streaming RPCs which are cancelled on the
459 // server. Depending on the value of server_try_cancel parameter, this will
460 // test one of the following three scenarios:
461 // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading/
462 // writing any messages from/to the client
463 //
464 // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading/
465 // writing messages from/to the client
466 //
467 // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading/writing
468 // all the messages from/to the client
469 //
470 // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL.
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800471 void TestBidiStreamServerCancel(ServerTryCancelRequestPhase server_try_cancel,
472 int num_messages) {
473 ResetStub();
474 EchoRequest request;
475 EchoResponse response;
476 ClientContext context;
477
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800478 // Send server_try_cancel in the client metadata
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800479 context.AddMetadata(kServerTryCancelRequest,
480 std::to_string(server_try_cancel));
481
482 auto stream = stub_->BidiStream(&context);
483
484 int num_msgs_read = 0;
485 int num_msgs_sent = 0;
486 while (num_msgs_sent < num_messages) {
487 request.set_message("hello " + std::to_string(num_msgs_sent));
488 if (!stream->Write(request)) {
489 break;
490 }
491 num_msgs_sent++;
492
493 if (!stream->Read(&response)) {
494 break;
495 }
496 num_msgs_read++;
497
498 EXPECT_EQ(response.message(), request.message());
499 }
500 gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent);
501 gpr_log(GPR_INFO, "Read %d messages", num_msgs_read);
502
503 stream->WritesDone();
504 Status s = stream->Finish();
505
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800506 // Depending on the value of server_try_cancel, the RPC might have been
507 // cancelled by the server at different stages. The following validates our
508 // expectations of number of messages read in various cancellation
509 // scenarios:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800510 switch (server_try_cancel) {
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800511 case CANCEL_BEFORE_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800512 EXPECT_EQ(num_msgs_read, 0);
513 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800514
515 case CANCEL_DURING_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800516 EXPECT_LE(num_msgs_sent, num_messages);
517 EXPECT_LE(num_msgs_read, num_msgs_sent);
518 break;
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800519
520 case CANCEL_AFTER_PROCESSING:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800521 EXPECT_EQ(num_msgs_sent, num_messages);
522 EXPECT_EQ(num_msgs_read, num_msgs_sent);
Sree Kuchibhotla0f242ac2016-01-29 18:12:19 -0800523 break;
524
525 default:
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800526 gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d",
527 server_try_cancel);
528 EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL &&
529 server_try_cancel <= CANCEL_AFTER_PROCESSING);
530 break;
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -0800531 }
532
533 EXPECT_FALSE(s.ok());
534 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
535 }
536};
537
538TEST_P(End2endServerTryCancelTest, RequestEchoServerCancel) {
539 ResetStub();
540 EchoRequest request;
541 EchoResponse response;
542 ClientContext context;
543
544 context.AddMetadata(kServerTryCancelRequest,
545 std::to_string(CANCEL_BEFORE_PROCESSING));
546 Status s = stub_->Echo(&context, request, &response);
547 EXPECT_FALSE(s.ok());
548 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
549}
550
551// Server to cancel before doing reading the request
552TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelBeforeReads) {
553 TestRequestStreamServerCancel(CANCEL_BEFORE_PROCESSING, 1);
554}
555
556// Server to cancel while reading a request from the stream in parallel
557TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelDuringRead) {
558 TestRequestStreamServerCancel(CANCEL_DURING_PROCESSING, 10);
559}
560
561// Server to cancel after reading all the requests but before returning to the
562// client
563TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelAfterReads) {
564 TestRequestStreamServerCancel(CANCEL_AFTER_PROCESSING, 4);
565}
566
567// Server to cancel before sending any response messages
568TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelBefore) {
569 TestResponseStreamServerCancel(CANCEL_BEFORE_PROCESSING);
570}
571
572// Server to cancel while writing a response to the stream in parallel
573TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelDuring) {
574 TestResponseStreamServerCancel(CANCEL_DURING_PROCESSING);
575}
576
577// Server to cancel after writing all the respones to the stream but before
578// returning to the client
579TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelAfter) {
580 TestResponseStreamServerCancel(CANCEL_AFTER_PROCESSING);
581}
582
583// Server to cancel before reading/writing any requests/responses on the stream
584TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelBefore) {
585 TestBidiStreamServerCancel(CANCEL_BEFORE_PROCESSING, 2);
586}
587
588// Server to cancel while reading/writing requests/responses on the stream in
589// parallel
590TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelDuring) {
591 TestBidiStreamServerCancel(CANCEL_DURING_PROCESSING, 10);
592}
593
594// Server to cancel after reading/writing all requests/responses on the stream
595// but before returning to the client
596TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelAfter) {
597 TestBidiStreamServerCancel(CANCEL_AFTER_PROCESSING, 5);
598}
599
yang-gd59ad7e2016-02-10 12:42:53 -0800600TEST_P(End2endTest, SimpleRpcWithCustomeUserAgentPrefix) {
601 user_agent_prefix_ = "custom_prefix";
602 ResetStub();
603 EchoRequest request;
604 EchoResponse response;
605 request.set_message("Hello hello hello hello");
606 request.mutable_param()->set_echo_metadata(true);
607
608 ClientContext context;
609 Status s = stub_->Echo(&context, request, &response);
610 EXPECT_EQ(response.message(), request.message());
611 EXPECT_TRUE(s.ok());
612 const auto& trailing_metadata = context.GetServerTrailingMetadata();
613 auto iter = trailing_metadata.find("user-agent");
614 EXPECT_TRUE(iter != trailing_metadata.end());
615 grpc::string expected_prefix = user_agent_prefix_ + " grpc-c++/";
616 EXPECT_TRUE(iter->second.starts_with(expected_prefix));
617}
618
yang-gf8174ea2016-02-01 00:09:13 -0800619TEST_P(End2endTest, MultipleRpcsWithVariedBinaryMetadataValue) {
620 ResetStub();
621 std::vector<std::thread*> threads;
622 for (int i = 0; i < 10; ++i) {
623 threads.push_back(new std::thread(SendRpc, stub_.get(), 10, true));
624 }
625 for (int i = 0; i < 10; ++i) {
626 threads[i]->join();
627 delete threads[i];
628 }
629}
630
631TEST_P(End2endTest, MultipleRpcs) {
632 ResetStub();
633 std::vector<std::thread*> threads;
634 for (int i = 0; i < 10; ++i) {
635 threads.push_back(new std::thread(SendRpc, stub_.get(), 10, false));
636 }
637 for (int i = 0; i < 10; ++i) {
638 threads[i]->join();
639 delete threads[i];
640 }
641}
642
yang-g88d5d522015-09-29 12:46:54 -0700643TEST_P(End2endTest, RequestStreamOneRequest) {
644 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800645 EchoRequest request;
646 EchoResponse response;
647 ClientContext context;
648
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800649 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800650 request.set_message("hello");
651 EXPECT_TRUE(stream->Write(request));
652 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800653 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800654 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700655 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800656}
657
yang-g88d5d522015-09-29 12:46:54 -0700658TEST_P(End2endTest, RequestStreamTwoRequests) {
659 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800660 EchoRequest request;
661 EchoResponse response;
662 ClientContext context;
663
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800664 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800665 request.set_message("hello");
666 EXPECT_TRUE(stream->Write(request));
667 EXPECT_TRUE(stream->Write(request));
668 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800669 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800670 EXPECT_EQ(response.message(), "hellohello");
Yang Gaoc1a2c312015-06-16 10:59:46 -0700671 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800672}
673
yang-g88d5d522015-09-29 12:46:54 -0700674TEST_P(End2endTest, ResponseStream) {
675 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800676 EchoRequest request;
677 EchoResponse response;
678 ClientContext context;
679 request.set_message("hello");
680
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800681 auto stream = stub_->ResponseStream(&context, request);
nnoble0c475f02014-12-05 15:37:39 -0800682 EXPECT_TRUE(stream->Read(&response));
683 EXPECT_EQ(response.message(), request.message() + "0");
684 EXPECT_TRUE(stream->Read(&response));
685 EXPECT_EQ(response.message(), request.message() + "1");
686 EXPECT_TRUE(stream->Read(&response));
687 EXPECT_EQ(response.message(), request.message() + "2");
688 EXPECT_FALSE(stream->Read(&response));
689
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800690 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700691 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800692}
693
yang-g88d5d522015-09-29 12:46:54 -0700694TEST_P(End2endTest, BidiStream) {
695 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800696 EchoRequest request;
697 EchoResponse response;
698 ClientContext context;
699 grpc::string msg("hello");
700
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800701 auto stream = stub_->BidiStream(&context);
nnoble0c475f02014-12-05 15:37:39 -0800702
703 request.set_message(msg + "0");
704 EXPECT_TRUE(stream->Write(request));
705 EXPECT_TRUE(stream->Read(&response));
706 EXPECT_EQ(response.message(), request.message());
707
708 request.set_message(msg + "1");
709 EXPECT_TRUE(stream->Write(request));
710 EXPECT_TRUE(stream->Read(&response));
711 EXPECT_EQ(response.message(), request.message());
712
713 request.set_message(msg + "2");
714 EXPECT_TRUE(stream->Write(request));
715 EXPECT_TRUE(stream->Read(&response));
716 EXPECT_EQ(response.message(), request.message());
717
718 stream->WritesDone();
719 EXPECT_FALSE(stream->Read(&response));
Craig Tillerca9a6372015-12-15 18:16:28 -0800720 EXPECT_FALSE(stream->Read(&response));
nnoble0c475f02014-12-05 15:37:39 -0800721
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800722 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700723 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800724}
725
726// Talk to the two services with the same name but different package names.
727// The two stubs are created on the same channel.
yang-g88d5d522015-09-29 12:46:54 -0700728TEST_P(End2endTest, DiffPackageServices) {
729 ResetStub();
yangg1456d152015-01-08 15:39:58 -0800730 EchoRequest request;
731 EchoResponse response;
732 request.set_message("Hello");
733
yangg1456d152015-01-08 15:39:58 -0800734 ClientContext context;
yang-g8b25f2a2015-07-21 23:54:36 -0700735 Status s = stub_->Echo(&context, request, &response);
yangg1456d152015-01-08 15:39:58 -0800736 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700737 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800738
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800739 std::unique_ptr<grpc::testing::duplicate::EchoTestService::Stub> dup_pkg_stub(
740 grpc::testing::duplicate::EchoTestService::NewStub(channel_));
yangg1456d152015-01-08 15:39:58 -0800741 ClientContext context2;
742 s = dup_pkg_stub->Echo(&context2, request, &response);
743 EXPECT_EQ("no package", response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700744 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800745}
746
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700747void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) {
Craig Tiller20b5fe92015-07-06 10:43:50 -0700748 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700749 gpr_time_from_micros(delay_us, GPR_TIMESPAN)));
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700750 while (!service->signal_client()) {
751 }
752 context->TryCancel();
753}
754
yang-ga89bf502015-11-17 14:19:17 -0800755TEST_P(End2endTest, CancelRpcBeforeStart) {
756 ResetStub();
757 EchoRequest request;
758 EchoResponse response;
759 ClientContext context;
760 request.set_message("hello");
761 context.TryCancel();
762 Status s = stub_->Echo(&context, request, &response);
763 EXPECT_EQ("", response.message());
764 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
765}
766
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700767// Client cancels request stream after sending two messages
yang-g88d5d522015-09-29 12:46:54 -0700768TEST_P(End2endTest, ClientCancelsRequestStream) {
769 ResetStub();
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700770 EchoRequest request;
771 EchoResponse response;
772 ClientContext context;
773 request.set_message("hello");
774
775 auto stream = stub_->RequestStream(&context, &response);
776 EXPECT_TRUE(stream->Write(request));
777 EXPECT_TRUE(stream->Write(request));
Yang Gaoc71a9d22015-05-04 00:22:12 -0700778
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700779 context.TryCancel();
780
781 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700782 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700783
Yang Gaoc71a9d22015-05-04 00:22:12 -0700784 EXPECT_EQ(response.message(), "");
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700785}
786
Abhishek Kumare41d0402015-04-17 14:12:33 -0700787// Client cancels server stream after sending some messages
yang-g88d5d522015-09-29 12:46:54 -0700788TEST_P(End2endTest, ClientCancelsResponseStream) {
789 ResetStub();
Abhishek Kumare41d0402015-04-17 14:12:33 -0700790 EchoRequest request;
791 EchoResponse response;
792 ClientContext context;
793 request.set_message("hello");
794
795 auto stream = stub_->ResponseStream(&context, request);
796
797 EXPECT_TRUE(stream->Read(&response));
798 EXPECT_EQ(response.message(), request.message() + "0");
799 EXPECT_TRUE(stream->Read(&response));
800 EXPECT_EQ(response.message(), request.message() + "1");
801
802 context.TryCancel();
803
804 // The cancellation races with responses, so there might be zero or
805 // one responses pending, read till failure
806
807 if (stream->Read(&response)) {
808 EXPECT_EQ(response.message(), request.message() + "2");
809 // Since we have cancelled, we expect the next attempt to read to fail
810 EXPECT_FALSE(stream->Read(&response));
811 }
812
813 Status s = stream->Finish();
Abhishek Kumar18298a72015-04-17 15:00:25 -0700814 // The final status could be either of CANCELLED or OK depending on
815 // who won the race.
Yang Gaoc1a2c312015-06-16 10:59:46 -0700816 EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumare41d0402015-04-17 14:12:33 -0700817}
818
Abhishek Kumar82a83312015-04-17 13:30:51 -0700819// Client cancels bidi stream after sending some messages
yang-g88d5d522015-09-29 12:46:54 -0700820TEST_P(End2endTest, ClientCancelsBidi) {
821 ResetStub();
Abhishek Kumar82a83312015-04-17 13:30:51 -0700822 EchoRequest request;
823 EchoResponse response;
824 ClientContext context;
825 grpc::string msg("hello");
826
827 auto stream = stub_->BidiStream(&context);
828
829 request.set_message(msg + "0");
830 EXPECT_TRUE(stream->Write(request));
831 EXPECT_TRUE(stream->Read(&response));
832 EXPECT_EQ(response.message(), request.message());
833
834 request.set_message(msg + "1");
835 EXPECT_TRUE(stream->Write(request));
836
837 context.TryCancel();
838
839 // The cancellation races with responses, so there might be zero or
840 // one responses pending, read till failure
841
842 if (stream->Read(&response)) {
843 EXPECT_EQ(response.message(), request.message());
844 // Since we have cancelled, we expect the next attempt to read to fail
845 EXPECT_FALSE(stream->Read(&response));
846 }
847
848 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700849 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumar82a83312015-04-17 13:30:51 -0700850}
851
yang-g88d5d522015-09-29 12:46:54 -0700852TEST_P(End2endTest, RpcMaxMessageSize) {
853 ResetStub();
Yang Gao3921c562015-04-30 16:07:06 -0700854 EchoRequest request;
855 EchoResponse response;
Yang Gaoc71a9d22015-05-04 00:22:12 -0700856 request.set_message(string(kMaxMessageSize_ * 2, 'a'));
Yang Gao3921c562015-04-30 16:07:06 -0700857
858 ClientContext context;
859 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700860 EXPECT_FALSE(s.ok());
Yang Gao3921c562015-04-30 16:07:06 -0700861}
Abhishek Kumar82a83312015-04-17 13:30:51 -0700862
yang-g88d5d522015-09-29 12:46:54 -0700863// Client sends 20 requests and the server returns CANCELLED status after
864// reading 10 requests.
865TEST_P(End2endTest, RequestStreamServerEarlyCancelTest) {
866 ResetStub();
867 EchoRequest request;
868 EchoResponse response;
869 ClientContext context;
870
871 context.AddMetadata(kServerCancelAfterReads, "10");
872 auto stream = stub_->RequestStream(&context, &response);
873 request.set_message("hello");
874 int send_messages = 20;
yang-gc0461032015-10-02 16:22:43 -0700875 while (send_messages > 10) {
yang-g88d5d522015-09-29 12:46:54 -0700876 EXPECT_TRUE(stream->Write(request));
877 send_messages--;
878 }
yang-gc0461032015-10-02 16:22:43 -0700879 while (send_messages > 0) {
880 stream->Write(request);
881 send_messages--;
882 }
yang-g88d5d522015-09-29 12:46:54 -0700883 stream->WritesDone();
884 Status s = stream->Finish();
885 EXPECT_EQ(s.error_code(), StatusCode::CANCELLED);
886}
887
yang-g88d5d522015-09-29 12:46:54 -0700888void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream,
889 gpr_event* ev) {
890 EchoResponse resp;
891 gpr_event_set(ev, (void*)1);
892 while (stream->Read(&resp)) {
893 gpr_log(GPR_INFO, "Read message");
894 }
895}
yang-g88d5d522015-09-29 12:46:54 -0700896
897// Run a Read and a WritesDone simultaneously.
898TEST_P(End2endTest, SimultaneousReadWritesDone) {
899 ResetStub();
900 ClientContext context;
901 gpr_event ev;
902 gpr_event_init(&ev);
903 auto stream = stub_->BidiStream(&context);
904 std::thread reader_thread(ReaderThreadFunc, stream.get(), &ev);
905 gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME));
906 stream->WritesDone();
907 Status s = stream->Finish();
908 EXPECT_TRUE(s.ok());
909 reader_thread.join();
910}
911
912TEST_P(End2endTest, ChannelState) {
913 ResetStub();
914 // Start IDLE
915 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(false));
916
917 // Did not ask to connect, no state change.
918 CompletionQueue cq;
919 std::chrono::system_clock::time_point deadline =
920 std::chrono::system_clock::now() + std::chrono::milliseconds(10);
921 channel_->NotifyOnStateChange(GRPC_CHANNEL_IDLE, deadline, &cq, NULL);
922 void* tag;
923 bool ok = true;
924 cq.Next(&tag, &ok);
925 EXPECT_FALSE(ok);
926
927 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true));
928 EXPECT_TRUE(channel_->WaitForStateChange(GRPC_CHANNEL_IDLE,
929 gpr_inf_future(GPR_CLOCK_REALTIME)));
yang-g0d557502015-10-01 11:30:12 -0700930 auto state = channel_->GetState(false);
931 EXPECT_TRUE(state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_READY);
yang-g88d5d522015-09-29 12:46:54 -0700932}
933
934// Takes 10s.
935TEST_P(End2endTest, ChannelStateTimeout) {
yang-g17197dd2016-02-19 00:04:22 -0800936 if (GetParam().credentials_type != kInsecureCredentialsType) {
yang-g88d5d522015-09-29 12:46:54 -0700937 return;
938 }
939 int port = grpc_pick_unused_port_or_die();
940 std::ostringstream server_address;
941 server_address << "127.0.0.1:" << port;
942 // Channel to non-existing server
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700943 auto channel =
944 CreateChannel(server_address.str(), InsecureChannelCredentials());
yang-g88d5d522015-09-29 12:46:54 -0700945 // Start IDLE
946 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel->GetState(true));
947
948 auto state = GRPC_CHANNEL_IDLE;
949 for (int i = 0; i < 10; i++) {
950 channel->WaitForStateChange(
951 state, std::chrono::system_clock::now() + std::chrono::seconds(1));
952 state = channel->GetState(false);
953 }
954}
955
956// Talking to a non-existing service.
957TEST_P(End2endTest, NonExistingService) {
958 ResetChannel();
Craig Tiller1b4e3302015-12-17 16:35:00 -0800959 std::unique_ptr<grpc::testing::UnimplementedService::Stub> stub;
960 stub = grpc::testing::UnimplementedService::NewStub(channel_);
yang-g88d5d522015-09-29 12:46:54 -0700961
962 EchoRequest request;
963 EchoResponse response;
964 request.set_message("Hello");
965
966 ClientContext context;
967 Status s = stub->Unimplemented(&context, request, &response);
968 EXPECT_EQ(StatusCode::UNIMPLEMENTED, s.error_code());
969 EXPECT_EQ("", s.error_message());
970}
971
972//////////////////////////////////////////////////////////////////////////
973// Test with and without a proxy.
974class ProxyEnd2endTest : public End2endTest {
975 protected:
976};
977
978TEST_P(ProxyEnd2endTest, SimpleRpc) {
979 ResetStub();
yang-gf8174ea2016-02-01 00:09:13 -0800980 SendRpc(stub_.get(), 1, false);
yang-g88d5d522015-09-29 12:46:54 -0700981}
982
983TEST_P(ProxyEnd2endTest, MultipleRpcs) {
984 ResetStub();
985 std::vector<std::thread*> threads;
986 for (int i = 0; i < 10; ++i) {
yang-gf8174ea2016-02-01 00:09:13 -0800987 threads.push_back(new std::thread(SendRpc, stub_.get(), 10, false));
yang-g88d5d522015-09-29 12:46:54 -0700988 }
989 for (int i = 0; i < 10; ++i) {
990 threads[i]->join();
991 delete threads[i];
992 }
993}
994
995// Set a 10us deadline and make sure proper error is returned.
996TEST_P(ProxyEnd2endTest, RpcDeadlineExpires) {
997 ResetStub();
998 EchoRequest request;
999 EchoResponse response;
1000 request.set_message("Hello");
Craig Tillerb0f275e2016-01-27 10:45:50 -08001001 request.mutable_param()->set_skip_cancelled_check(true);
yang-g88d5d522015-09-29 12:46:54 -07001002
1003 ClientContext context;
1004 std::chrono::system_clock::time_point deadline =
1005 std::chrono::system_clock::now() + std::chrono::microseconds(10);
1006 context.set_deadline(deadline);
1007 Status s = stub_->Echo(&context, request, &response);
1008 EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code());
1009}
1010
1011// Set a long but finite deadline.
1012TEST_P(ProxyEnd2endTest, RpcLongDeadline) {
1013 ResetStub();
1014 EchoRequest request;
1015 EchoResponse response;
1016 request.set_message("Hello");
1017
1018 ClientContext context;
1019 std::chrono::system_clock::time_point deadline =
1020 std::chrono::system_clock::now() + std::chrono::hours(1);
1021 context.set_deadline(deadline);
1022 Status s = stub_->Echo(&context, request, &response);
1023 EXPECT_EQ(response.message(), request.message());
1024 EXPECT_TRUE(s.ok());
1025}
1026
1027// Ask server to echo back the deadline it sees.
1028TEST_P(ProxyEnd2endTest, EchoDeadline) {
1029 ResetStub();
1030 EchoRequest request;
1031 EchoResponse response;
1032 request.set_message("Hello");
1033 request.mutable_param()->set_echo_deadline(true);
1034
1035 ClientContext context;
1036 std::chrono::system_clock::time_point deadline =
1037 std::chrono::system_clock::now() + std::chrono::seconds(100);
1038 context.set_deadline(deadline);
1039 Status s = stub_->Echo(&context, request, &response);
1040 EXPECT_EQ(response.message(), request.message());
1041 EXPECT_TRUE(s.ok());
1042 gpr_timespec sent_deadline;
1043 Timepoint2Timespec(deadline, &sent_deadline);
1044 // Allow 1 second error.
1045 EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1);
1046 EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1);
1047}
1048
1049// Ask server to echo back the deadline it sees. The rpc has no deadline.
1050TEST_P(ProxyEnd2endTest, EchoDeadlineForNoDeadlineRpc) {
1051 ResetStub();
1052 EchoRequest request;
1053 EchoResponse response;
1054 request.set_message("Hello");
1055 request.mutable_param()->set_echo_deadline(true);
1056
1057 ClientContext context;
1058 Status s = stub_->Echo(&context, request, &response);
1059 EXPECT_EQ(response.message(), request.message());
1060 EXPECT_TRUE(s.ok());
1061 EXPECT_EQ(response.param().request_deadline(),
1062 gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec);
1063}
1064
1065TEST_P(ProxyEnd2endTest, UnimplementedRpc) {
1066 ResetStub();
1067 EchoRequest request;
1068 EchoResponse response;
1069 request.set_message("Hello");
1070
1071 ClientContext context;
1072 Status s = stub_->Unimplemented(&context, request, &response);
1073 EXPECT_FALSE(s.ok());
1074 EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED);
1075 EXPECT_EQ(s.error_message(), "");
1076 EXPECT_EQ(response.message(), "");
1077}
1078
1079// Client cancels rpc after 10ms
1080TEST_P(ProxyEnd2endTest, ClientCancelsRpc) {
1081 ResetStub();
1082 EchoRequest request;
1083 EchoResponse response;
1084 request.set_message("Hello");
1085 const int kCancelDelayUs = 10 * 1000;
1086 request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs);
1087
1088 ClientContext context;
1089 std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_);
1090 Status s = stub_->Echo(&context, request, &response);
1091 cancel_thread.join();
1092 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
1093 EXPECT_EQ(s.error_message(), "Cancelled");
1094}
1095
1096// Server cancels rpc after 1ms
1097TEST_P(ProxyEnd2endTest, ServerCancelsRpc) {
1098 ResetStub();
1099 EchoRequest request;
1100 EchoResponse response;
1101 request.set_message("Hello");
1102 request.mutable_param()->set_server_cancel_after_us(1000);
1103
1104 ClientContext context;
1105 Status s = stub_->Echo(&context, request, &response);
1106 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
1107 EXPECT_TRUE(s.error_message().empty());
1108}
1109
1110// Make the response larger than the flow control window.
1111TEST_P(ProxyEnd2endTest, HugeResponse) {
1112 ResetStub();
1113 EchoRequest request;
1114 EchoResponse response;
1115 request.set_message("huge response");
1116 const size_t kResponseSize = 1024 * (1024 + 10);
1117 request.mutable_param()->set_response_message_length(kResponseSize);
1118
1119 ClientContext context;
1120 Status s = stub_->Echo(&context, request, &response);
1121 EXPECT_EQ(kResponseSize, response.message().size());
1122 EXPECT_TRUE(s.ok());
1123}
1124
1125TEST_P(ProxyEnd2endTest, Peer) {
1126 ResetStub();
1127 EchoRequest request;
1128 EchoResponse response;
1129 request.set_message("hello");
1130 request.mutable_param()->set_echo_peer(true);
1131
1132 ClientContext context;
1133 Status s = stub_->Echo(&context, request, &response);
1134 EXPECT_EQ(response.message(), request.message());
1135 EXPECT_TRUE(s.ok());
1136 EXPECT_TRUE(CheckIsLocalhost(response.param().peer()));
1137 EXPECT_TRUE(CheckIsLocalhost(context.peer()));
1138}
1139
1140//////////////////////////////////////////////////////////////////////////
1141class SecureEnd2endTest : public End2endTest {
1142 protected:
1143 SecureEnd2endTest() {
1144 GPR_ASSERT(!GetParam().use_proxy);
yang-g17197dd2016-02-19 00:04:22 -08001145 GPR_ASSERT(GetParam().credentials_type != kInsecureCredentialsType);
yang-g88d5d522015-09-29 12:46:54 -07001146 }
1147};
1148
1149TEST_P(SecureEnd2endTest, SimpleRpcWithHost) {
1150 ResetStub();
1151
1152 EchoRequest request;
1153 EchoResponse response;
1154 request.set_message("Hello");
1155
1156 ClientContext context;
1157 context.set_authority("foo.test.youtube.com");
1158 Status s = stub_->Echo(&context, request, &response);
1159 EXPECT_EQ(response.message(), request.message());
1160 EXPECT_TRUE(response.has_param());
1161 EXPECT_EQ("special", response.param().host());
1162 EXPECT_TRUE(s.ok());
1163}
1164
yang-ge21908f2015-08-25 13:47:51 -07001165bool MetadataContains(
1166 const std::multimap<grpc::string_ref, grpc::string_ref>& metadata,
1167 const grpc::string& key, const grpc::string& value) {
Yang Gao26a49122015-05-15 17:02:56 -07001168 int count = 0;
1169
yang-ge21908f2015-08-25 13:47:51 -07001170 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator iter =
Yang Gao26a49122015-05-15 17:02:56 -07001171 metadata.begin();
1172 iter != metadata.end(); ++iter) {
yang-ge21908f2015-08-25 13:47:51 -07001173 if (ToString(iter->first) == key && ToString(iter->second) == value) {
Yang Gao26a49122015-05-15 17:02:56 -07001174 count++;
1175 }
1176 }
1177 return count == 1;
1178}
1179
yang-g88d5d522015-09-29 12:46:54 -07001180TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorSuccess) {
1181 auto* processor = new TestAuthMetadataProcessor(true);
1182 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1183 ResetStub();
1184 EchoRequest request;
1185 EchoResponse response;
1186 ClientContext context;
1187 context.set_credentials(processor->GetCompatibleClientCreds());
1188 request.set_message("Hello");
1189 request.mutable_param()->set_echo_metadata(true);
1190 request.mutable_param()->set_expected_client_identity(
1191 TestAuthMetadataProcessor::kGoodGuy);
1192
1193 Status s = stub_->Echo(&context, request, &response);
1194 EXPECT_EQ(request.message(), response.message());
1195 EXPECT_TRUE(s.ok());
1196
1197 // Metadata should have been consumed by the processor.
1198 EXPECT_FALSE(MetadataContains(
1199 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1200 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1201}
1202
1203TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorFailure) {
1204 auto* processor = new TestAuthMetadataProcessor(true);
1205 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1206 ResetStub();
1207 EchoRequest request;
1208 EchoResponse response;
1209 ClientContext context;
1210 context.set_credentials(processor->GetIncompatibleClientCreds());
1211 request.set_message("Hello");
1212
1213 Status s = stub_->Echo(&context, request, &response);
1214 EXPECT_FALSE(s.ok());
1215 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1216}
1217TEST_P(SecureEnd2endTest, SetPerCallCredentials) {
1218 ResetStub();
Yang Gaoa8938922015-05-14 11:51:07 -07001219 EchoRequest request;
1220 EchoResponse response;
1221 ClientContext context;
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001222 std::shared_ptr<CallCredentials> creds =
Julien Boeuf510a9202015-08-25 21:51:07 -07001223 GoogleIAMCredentials("fake_token", "fake_selector");
Yang Gaoa8938922015-05-14 11:51:07 -07001224 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -07001225 request.set_message("Hello");
1226 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -07001227
1228 Status s = stub_->Echo(&context, request, &response);
Yang Gaoa8938922015-05-14 11:51:07 -07001229 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -07001230 EXPECT_TRUE(s.ok());
Yang Gao26a49122015-05-15 17:02:56 -07001231 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1232 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1233 "fake_token"));
1234 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1235 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1236 "fake_selector"));
Yang Gaoa8938922015-05-14 11:51:07 -07001237}
1238
yang-g88d5d522015-09-29 12:46:54 -07001239TEST_P(SecureEnd2endTest, OverridePerCallCredentials) {
1240 ResetStub();
Yang Gaoa8938922015-05-14 11:51:07 -07001241 EchoRequest request;
1242 EchoResponse response;
1243 ClientContext context;
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001244 std::shared_ptr<CallCredentials> creds1 =
Julien Boeuf510a9202015-08-25 21:51:07 -07001245 GoogleIAMCredentials("fake_token1", "fake_selector1");
Yang Gaoa8938922015-05-14 11:51:07 -07001246 context.set_credentials(creds1);
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001247 std::shared_ptr<CallCredentials> creds2 =
Julien Boeuf510a9202015-08-25 21:51:07 -07001248 GoogleIAMCredentials("fake_token2", "fake_selector2");
Yang Gaoa8938922015-05-14 11:51:07 -07001249 context.set_credentials(creds2);
Yang Gao26a49122015-05-15 17:02:56 -07001250 request.set_message("Hello");
1251 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -07001252
1253 Status s = stub_->Echo(&context, request, &response);
Yang Gao26a49122015-05-15 17:02:56 -07001254 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1255 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1256 "fake_token2"));
1257 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1258 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1259 "fake_selector2"));
Yang Gaob57f72d2015-05-17 21:54:54 -07001260 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
1261 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1262 "fake_token1"));
1263 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
1264 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1265 "fake_selector1"));
Yang Gaoa8938922015-05-14 11:51:07 -07001266 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -07001267 EXPECT_TRUE(s.ok());
Yang Gaoa8938922015-05-14 11:51:07 -07001268}
1269
yang-g88d5d522015-09-29 12:46:54 -07001270TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginFailure) {
1271 ResetStub();
Julien Boeuf1928d492015-09-15 15:20:11 -07001272 EchoRequest request;
1273 EchoResponse response;
1274 ClientContext context;
1275 context.set_credentials(
1276 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1277 new TestMetadataCredentialsPlugin(
1278 "Does not matter, will fail anyway (see 3rd param)", false,
1279 false))));
1280 request.set_message("Hello");
1281
1282 Status s = stub_->Echo(&context, request, &response);
1283 EXPECT_FALSE(s.ok());
1284 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1285}
1286
yang-g88d5d522015-09-29 12:46:54 -07001287TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorSuccess) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001288 auto* processor = new TestAuthMetadataProcessor(false);
1289 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
yang-g88d5d522015-09-29 12:46:54 -07001290 ResetStub();
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001291 EchoRequest request;
1292 EchoResponse response;
1293 ClientContext context;
1294 context.set_credentials(processor->GetCompatibleClientCreds());
1295 request.set_message("Hello");
1296 request.mutable_param()->set_echo_metadata(true);
1297 request.mutable_param()->set_expected_client_identity(
1298 TestAuthMetadataProcessor::kGoodGuy);
1299
1300 Status s = stub_->Echo(&context, request, &response);
1301 EXPECT_EQ(request.message(), response.message());
1302 EXPECT_TRUE(s.ok());
1303
1304 // Metadata should have been consumed by the processor.
1305 EXPECT_FALSE(MetadataContains(
1306 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1307 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1308}
1309
yang-g88d5d522015-09-29 12:46:54 -07001310TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorFailure) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001311 auto* processor = new TestAuthMetadataProcessor(false);
1312 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
yang-g88d5d522015-09-29 12:46:54 -07001313 ResetStub();
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001314 EchoRequest request;
1315 EchoResponse response;
1316 ClientContext context;
1317 context.set_credentials(processor->GetIncompatibleClientCreds());
1318 request.set_message("Hello");
1319
1320 Status s = stub_->Echo(&context, request, &response);
1321 EXPECT_FALSE(s.ok());
1322 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1323}
1324
yang-g88d5d522015-09-29 12:46:54 -07001325TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginFailure) {
1326 ResetStub();
Julien Boeuf1928d492015-09-15 15:20:11 -07001327 EchoRequest request;
1328 EchoResponse response;
1329 ClientContext context;
1330 context.set_credentials(
1331 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1332 new TestMetadataCredentialsPlugin(
1333 "Does not matter, will fail anyway (see 3rd param)", true,
1334 false))));
1335 request.set_message("Hello");
1336
1337 Status s = stub_->Echo(&context, request, &response);
1338 EXPECT_FALSE(s.ok());
1339 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1340}
1341
yang-g88d5d522015-09-29 12:46:54 -07001342TEST_P(SecureEnd2endTest, ClientAuthContext) {
1343 ResetStub();
yang-gc4eef2e2015-07-06 23:26:58 -07001344 EchoRequest request;
1345 EchoResponse response;
1346 request.set_message("Hello");
1347 request.mutable_param()->set_check_auth_context(true);
1348
1349 ClientContext context;
1350 Status s = stub_->Echo(&context, request, &response);
1351 EXPECT_EQ(response.message(), request.message());
1352 EXPECT_TRUE(s.ok());
1353
yang-g8b25f2a2015-07-21 23:54:36 -07001354 std::shared_ptr<const AuthContext> auth_ctx = context.auth_context();
yang-gd090fe12015-08-25 16:53:07 -07001355 std::vector<grpc::string_ref> ssl =
yang-g8b25f2a2015-07-21 23:54:36 -07001356 auth_ctx->FindPropertyValues("transport_security_type");
1357 EXPECT_EQ(1u, ssl.size());
yang-gd090fe12015-08-25 16:53:07 -07001358 EXPECT_EQ("ssl", ToString(ssl[0]));
yang-g8b25f2a2015-07-21 23:54:36 -07001359 EXPECT_EQ("x509_subject_alternative_name",
1360 auth_ctx->GetPeerIdentityPropertyName());
1361 EXPECT_EQ(3u, auth_ctx->GetPeerIdentity().size());
yang-gd090fe12015-08-25 16:53:07 -07001362 EXPECT_EQ("*.test.google.fr", ToString(auth_ctx->GetPeerIdentity()[0]));
1363 EXPECT_EQ("waterzooi.test.google.be",
1364 ToString(auth_ctx->GetPeerIdentity()[1]));
1365 EXPECT_EQ("*.test.youtube.com", ToString(auth_ctx->GetPeerIdentity()[2]));
yang-gc4eef2e2015-07-06 23:26:58 -07001366}
1367
yang-g17197dd2016-02-19 00:04:22 -08001368INSTANTIATE_TEST_CASE_P(
1369 End2end, End2endTest,
1370 ::testing::Values(TestScenario(false, kInsecureCredentialsType),
1371 TestScenario(false, kTlsCredentialsType)));
yang-g6f30dec2015-07-22 23:11:56 -07001372
yang-g17197dd2016-02-19 00:04:22 -08001373INSTANTIATE_TEST_CASE_P(
1374 End2endServerTryCancel, End2endServerTryCancelTest,
1375 ::testing::Values(TestScenario(false, kInsecureCredentialsType)));
Sree Kuchibhotla944f4cf2016-01-27 14:37:26 -08001376
yang-g17197dd2016-02-19 00:04:22 -08001377INSTANTIATE_TEST_CASE_P(
1378 ProxyEnd2end, ProxyEnd2endTest,
1379 ::testing::Values(TestScenario(false, kInsecureCredentialsType),
1380 TestScenario(false, kTlsCredentialsType),
1381 TestScenario(true, kInsecureCredentialsType),
1382 TestScenario(true, kTlsCredentialsType)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001383
yang-g88d5d522015-09-29 12:46:54 -07001384INSTANTIATE_TEST_CASE_P(SecureEnd2end, SecureEnd2endTest,
yang-g17197dd2016-02-19 00:04:22 -08001385 ::testing::Values(TestScenario(false,
1386 kTlsCredentialsType)));
yang-g88d5d522015-09-29 12:46:54 -07001387
yang-g8ab38362015-07-31 14:05:33 -07001388} // namespace
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001389} // namespace testing
1390} // namespace grpc
1391
1392int main(int argc, char** argv) {
1393 grpc_test_init(argc, argv);
1394 ::testing::InitGoogleTest(&argc, argv);
1395 return RUN_ALL_TESTS();
David Garcia Quintas2bf574f2016-01-14 15:27:08 -08001396}