blob: d72e93c10db64566b1f371e08775661e093d4725 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * 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
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"
yang-g8b25f2a2015-07-21 23:54:36 -070054#include "test/core/end2end/data/ssl_test_data.h"
Nicolas Noble89219162015-04-07 18:01:18 -070055#include "test/core/util/port.h"
Craig Tiller14e60e92015-01-13 17:26:46 -080056#include "test/core/util/test_config.h"
yang-ge21908f2015-08-25 13:47:51 -070057#include "test/cpp/util/string_ref_helper.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058
Craig Tiller1b4e3302015-12-17 16:35:00 -080059using grpc::testing::EchoRequest;
60using grpc::testing::EchoResponse;
yangged5e7e02015-01-06 10:16:15 -080061using std::chrono::system_clock;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062
63namespace grpc {
yangged5e7e02015-01-06 10:16:15 -080064namespace testing {
yangged5e7e02015-01-06 10:16:15 -080065namespace {
66
yang-g0b6ad7d2015-06-25 14:39:01 -070067const char* kServerCancelAfterReads = "cancel_after_reads";
68
yangged5e7e02015-01-06 10:16:15 -080069// When echo_deadline is requested, deadline seen in the ServerContext is set in
70// the response in seconds.
71void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request,
72 EchoResponse* response) {
73 if (request->has_param() && request->param().echo_deadline()) {
Craig Tiller354398f2015-07-13 09:16:03 -070074 gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
Nicolas Noble89219162015-04-07 18:01:18 -070075 if (context->deadline() != system_clock::time_point::max()) {
76 Timepoint2Timespec(context->deadline(), &deadline);
yangged5e7e02015-01-06 10:16:15 -080077 }
78 response->mutable_param()->set_request_deadline(deadline.tv_sec);
79 }
80}
Craig Tiller7418d012015-02-11 15:25:03 -080081
Julien Boeuf0c711ad2015-08-28 14:10:58 -070082void CheckServerAuthContext(const ServerContext* context,
83 const grpc::string& expected_client_identity) {
yang-g85c04f92015-07-07 17:47:31 -070084 std::shared_ptr<const AuthContext> auth_ctx = context->auth_context();
yang-gd090fe12015-08-25 16:53:07 -070085 std::vector<grpc::string_ref> ssl =
yang-gc4eef2e2015-07-06 23:26:58 -070086 auth_ctx->FindPropertyValues("transport_security_type");
yang-g8b25f2a2015-07-21 23:54:36 -070087 EXPECT_EQ(1u, ssl.size());
yang-gd090fe12015-08-25 16:53:07 -070088 EXPECT_EQ("ssl", ToString(ssl[0]));
Julien Boeuf0c711ad2015-08-28 14:10:58 -070089 if (expected_client_identity.length() == 0) {
90 EXPECT_TRUE(auth_ctx->GetPeerIdentityPropertyName().empty());
91 EXPECT_TRUE(auth_ctx->GetPeerIdentity().empty());
92 EXPECT_FALSE(auth_ctx->IsPeerAuthenticated());
93 } else {
94 auto identity = auth_ctx->GetPeerIdentity();
95 EXPECT_TRUE(auth_ctx->IsPeerAuthenticated());
96 EXPECT_EQ(1u, identity.size());
97 EXPECT_EQ(expected_client_identity, identity[0]);
98 }
yang-gc4eef2e2015-07-06 23:26:58 -070099}
100
yang-gd7ead692015-07-30 10:57:45 -0700101bool CheckIsLocalhost(const grpc::string& addr) {
102 const grpc::string kIpv6("ipv6:[::1]:");
103 const grpc::string kIpv4MappedIpv6("ipv6:[::ffff:127.0.0.1]:");
104 const grpc::string kIpv4("ipv4:127.0.0.1:");
105 return addr.substr(0, kIpv4.size()) == kIpv4 ||
106 addr.substr(0, kIpv4MappedIpv6.size()) == kIpv4MappedIpv6 ||
107 addr.substr(0, kIpv6.size()) == kIpv6;
108}
109
Julien Boeuf1928d492015-09-15 15:20:11 -0700110class TestMetadataCredentialsPlugin : public MetadataCredentialsPlugin {
111 public:
112 static const char kMetadataKey[];
113
114 TestMetadataCredentialsPlugin(grpc::string_ref metadata_value,
115 bool is_blocking, bool is_successful)
116 : metadata_value_(metadata_value.data(), metadata_value.length()),
117 is_blocking_(is_blocking),
118 is_successful_(is_successful) {}
119
120 bool IsBlocking() const GRPC_OVERRIDE { return is_blocking_; }
121
Julien Boeuf114f3942015-11-19 21:45:52 -0800122 Status GetMetadata(grpc::string_ref service_url, grpc::string_ref method_name,
123 const grpc::AuthContext& channel_auth_context,
Julien Boeuf8b0b6f42015-09-22 13:31:16 -0700124 std::multimap<grpc::string, grpc::string>* metadata)
Julien Boeuf1928d492015-09-15 15:20:11 -0700125 GRPC_OVERRIDE {
126 EXPECT_GT(service_url.length(), 0UL);
Julien Boeuf114f3942015-11-19 21:45:52 -0800127 EXPECT_GT(method_name.length(), 0UL);
128 EXPECT_TRUE(channel_auth_context.IsPeerAuthenticated());
Julien Boeuf1928d492015-09-15 15:20:11 -0700129 EXPECT_TRUE(metadata != nullptr);
130 if (is_successful_) {
131 metadata->insert(std::make_pair(kMetadataKey, metadata_value_));
132 return Status::OK;
133 } else {
134 return Status(StatusCode::NOT_FOUND, "Could not find plugin metadata.");
135 }
136 }
137
138 private:
139 grpc::string metadata_value_;
140 bool is_blocking_;
141 bool is_successful_;
142};
143
144const char TestMetadataCredentialsPlugin::kMetadataKey[] = "TestPluginMetadata";
145
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700146class TestAuthMetadataProcessor : public AuthMetadataProcessor {
147 public:
148 static const char kGoodGuy[];
149
150 TestAuthMetadataProcessor(bool is_blocking) : is_blocking_(is_blocking) {}
151
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700152 std::shared_ptr<CallCredentials> GetCompatibleClientCreds() {
Julien Boeuf1928d492015-09-15 15:20:11 -0700153 return MetadataCredentialsFromPlugin(
154 std::unique_ptr<MetadataCredentialsPlugin>(
155 new TestMetadataCredentialsPlugin(kGoodGuy, is_blocking_, true)));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700156 }
Julien Boeuf1928d492015-09-15 15:20:11 -0700157
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700158 std::shared_ptr<CallCredentials> GetIncompatibleClientCreds() {
Julien Boeuf1928d492015-09-15 15:20:11 -0700159 return MetadataCredentialsFromPlugin(
160 std::unique_ptr<MetadataCredentialsPlugin>(
161 new TestMetadataCredentialsPlugin("Mr Hyde", is_blocking_, true)));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700162 }
163
164 // Interface implementation
165 bool IsBlocking() const GRPC_OVERRIDE { return is_blocking_; }
166
167 Status Process(const InputMetadata& auth_metadata, AuthContext* context,
168 OutputMetadata* consumed_auth_metadata,
169 OutputMetadata* response_metadata) GRPC_OVERRIDE {
170 EXPECT_TRUE(consumed_auth_metadata != nullptr);
171 EXPECT_TRUE(context != nullptr);
172 EXPECT_TRUE(response_metadata != nullptr);
Julien Boeuf1928d492015-09-15 15:20:11 -0700173 auto auth_md =
174 auth_metadata.find(TestMetadataCredentialsPlugin::kMetadataKey);
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700175 EXPECT_NE(auth_md, auth_metadata.end());
176 string_ref auth_md_value = auth_md->second;
Julien Boeuf1928d492015-09-15 15:20:11 -0700177 if (auth_md_value == kGoodGuy) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700178 context->AddProperty(kIdentityPropName, kGoodGuy);
179 context->SetPeerIdentityPropertyName(kIdentityPropName);
Julien Boeuf8b0b6f42015-09-22 13:31:16 -0700180 consumed_auth_metadata->insert(std::make_pair(
181 string(auth_md->first.data(), auth_md->first.length()),
182 string(auth_md->second.data(), auth_md->second.length())));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700183 return Status::OK;
184 } else {
185 return Status(StatusCode::UNAUTHENTICATED,
186 string("Invalid principal: ") +
187 string(auth_md_value.data(), auth_md_value.length()));
188 }
189 }
190
Julien Boeuf1928d492015-09-15 15:20:11 -0700191 private:
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700192 static const char kIdentityPropName[];
193 bool is_blocking_;
194};
195
196const char TestAuthMetadataProcessor::kGoodGuy[] = "Dr Jekyll";
197const char TestAuthMetadataProcessor::kIdentityPropName[] = "novel identity";
198
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800199class Proxy : public ::grpc::testing::EchoTestService::Service {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700200 public:
yang-g8c2be9f2015-08-19 16:28:09 -0700201 Proxy(std::shared_ptr<Channel> channel)
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800202 : stub_(grpc::testing::EchoTestService::NewStub(channel)) {}
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700203
204 Status Echo(ServerContext* server_context, const EchoRequest* request,
205 EchoResponse* response) GRPC_OVERRIDE {
206 std::unique_ptr<ClientContext> client_context =
207 ClientContext::FromServerContext(*server_context);
208 return stub_->Echo(client_context.get(), *request, response);
209 }
210
211 private:
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800212 std::unique_ptr< ::grpc::testing::EchoTestService::Stub> stub_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700213};
214
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800215class TestServiceImpl : public ::grpc::testing::EchoTestService::Service {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800216 public:
vjpaidf551612015-07-14 13:38:44 -0700217 TestServiceImpl() : signal_client_(false), host_() {}
Vijay Pai181ef452015-07-14 13:52:48 -0700218 explicit TestServiceImpl(const grpc::string& host)
219 : signal_client_(false), host_(new grpc::string(host)) {}
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700220
yangga4b6f5d2014-12-17 15:53:12 -0800221 Status Echo(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800222 EchoResponse* response) GRPC_OVERRIDE {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800223 response->set_message(request->message());
yangged5e7e02015-01-06 10:16:15 -0800224 MaybeEchoDeadline(context, request, response);
Craig Tiller822d2c72015-07-07 16:08:00 -0700225 if (host_) {
226 response->mutable_param()->set_host(*host_);
227 }
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700228 if (request->has_param() && request->param().client_cancel_after_us()) {
229 {
230 std::unique_lock<std::mutex> lock(mu_);
231 signal_client_ = true;
232 }
233 while (!context->IsCancelled()) {
David Garcia Quintasfeb67f62015-05-20 19:23:25 -0700234 gpr_sleep_until(gpr_time_add(
Craig Tiller20b5fe92015-07-06 10:43:50 -0700235 gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700236 gpr_time_from_micros(request->param().client_cancel_after_us(),
237 GPR_TIMESPAN)));
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700238 }
Yang Gaoc1a2c312015-06-16 10:59:46 -0700239 return Status::CANCELLED;
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700240 } else if (request->has_param() &&
241 request->param().server_cancel_after_us()) {
David Garcia Quintasfeb67f62015-05-20 19:23:25 -0700242 gpr_sleep_until(gpr_time_add(
Craig Tiller20b5fe92015-07-06 10:43:50 -0700243 gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700244 gpr_time_from_micros(request->param().server_cancel_after_us(),
245 GPR_TIMESPAN)));
Yang Gaoc1a2c312015-06-16 10:59:46 -0700246 return Status::CANCELLED;
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700247 } else {
248 EXPECT_FALSE(context->IsCancelled());
249 }
Yang Gao26a49122015-05-15 17:02:56 -0700250
251 if (request->has_param() && request->param().echo_metadata()) {
yang-ge21908f2015-08-25 13:47:51 -0700252 const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata =
Yang Gao26a49122015-05-15 17:02:56 -0700253 context->client_metadata();
yang-ge21908f2015-08-25 13:47:51 -0700254 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
255 iter = client_metadata.begin();
Yang Gao26a49122015-05-15 17:02:56 -0700256 iter != client_metadata.end(); ++iter) {
yang-ge21908f2015-08-25 13:47:51 -0700257 context->AddTrailingMetadata(ToString(iter->first),
258 ToString(iter->second));
Yang Gao26a49122015-05-15 17:02:56 -0700259 }
260 }
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700261 if (request->has_param() &&
262 (request->param().expected_client_identity().length() > 0 ||
263 request->param().check_auth_context())) {
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700264 CheckServerAuthContext(context,
265 request->param().expected_client_identity());
yang-gc4eef2e2015-07-06 23:26:58 -0700266 }
yang-g6f30dec2015-07-22 23:11:56 -0700267 if (request->has_param() &&
268 request->param().response_message_length() > 0) {
269 response->set_message(
270 grpc::string(request->param().response_message_length(), '\0'));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800271 }
yang-gd7ead692015-07-30 10:57:45 -0700272 if (request->has_param() && request->param().echo_peer()) {
273 response->mutable_param()->set_peer(context->peer());
274 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800275 return Status::OK;
276 }
nnoble0c475f02014-12-05 15:37:39 -0800277
278 // Unimplemented is left unimplemented to test the returned error.
279
yangga4b6f5d2014-12-17 15:53:12 -0800280 Status RequestStream(ServerContext* context,
281 ServerReader<EchoRequest>* reader,
Craig Tillercf133f42015-02-26 14:05:56 -0800282 EchoResponse* response) GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800283 EchoRequest request;
284 response->set_message("");
yang-g0b6ad7d2015-06-25 14:39:01 -0700285 int cancel_after_reads = 0;
yang-ge21908f2015-08-25 13:47:51 -0700286 const std::multimap<grpc::string_ref, grpc::string_ref>&
287 client_initial_metadata = context->client_metadata();
yang-g0b6ad7d2015-06-25 14:39:01 -0700288 if (client_initial_metadata.find(kServerCancelAfterReads) !=
289 client_initial_metadata.end()) {
yang-ge21908f2015-08-25 13:47:51 -0700290 std::istringstream iss(ToString(
291 client_initial_metadata.find(kServerCancelAfterReads)->second));
yang-g0b6ad7d2015-06-25 14:39:01 -0700292 iss >> cancel_after_reads;
293 gpr_log(GPR_INFO, "cancel_after_reads %d", cancel_after_reads);
294 }
nnoble0c475f02014-12-05 15:37:39 -0800295 while (reader->Read(&request)) {
yang-g0b6ad7d2015-06-25 14:39:01 -0700296 if (cancel_after_reads == 1) {
297 gpr_log(GPR_INFO, "return cancel status");
298 return Status::CANCELLED;
299 } else if (cancel_after_reads > 0) {
300 cancel_after_reads--;
301 }
nnoble0c475f02014-12-05 15:37:39 -0800302 response->mutable_message()->append(request.message());
303 }
304 return Status::OK;
305 }
306
307 // Return 3 messages.
308 // TODO(yangg) make it generic by adding a parameter into EchoRequest
yangga4b6f5d2014-12-17 15:53:12 -0800309 Status ResponseStream(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800310 ServerWriter<EchoResponse>* writer) GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800311 EchoResponse response;
312 response.set_message(request->message() + "0");
313 writer->Write(response);
314 response.set_message(request->message() + "1");
315 writer->Write(response);
316 response.set_message(request->message() + "2");
317 writer->Write(response);
318
319 return Status::OK;
320 }
321
Craig Tillercf133f42015-02-26 14:05:56 -0800322 Status BidiStream(ServerContext* context,
323 ServerReaderWriter<EchoResponse, EchoRequest>* stream)
324 GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800325 EchoRequest request;
326 EchoResponse response;
327 while (stream->Read(&request)) {
328 gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
329 response.set_message(request.message());
330 stream->Write(response);
331 }
332 return Status::OK;
333 }
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700334
335 bool signal_client() {
336 std::unique_lock<std::mutex> lock(mu_);
337 return signal_client_;
338 }
339
340 private:
341 bool signal_client_;
342 std::mutex mu_;
Craig Tiller822d2c72015-07-07 16:08:00 -0700343 std::unique_ptr<grpc::string> host_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800344};
345
yangg1456d152015-01-08 15:39:58 -0800346class TestServiceImplDupPkg
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800347 : public ::grpc::testing::duplicate::EchoTestService::Service {
yangg1456d152015-01-08 15:39:58 -0800348 public:
349 Status Echo(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800350 EchoResponse* response) GRPC_OVERRIDE {
yangg1456d152015-01-08 15:39:58 -0800351 response->set_message("no package");
352 return Status::OK;
353 }
354};
355
yang-g88d5d522015-09-29 12:46:54 -0700356class TestScenario {
357 public:
358 TestScenario(bool proxy, bool tls) : use_proxy(proxy), use_tls(tls) {}
359 void Log() const {
360 gpr_log(GPR_INFO, "Scenario: proxy %d, tls %d", use_proxy, use_tls);
361 }
362 bool use_proxy;
363 bool use_tls;
364};
365
366class End2endTest : public ::testing::TestWithParam<TestScenario> {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800367 protected:
Vijay Pai181ef452015-07-14 13:52:48 -0700368 End2endTest()
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700369 : is_server_started_(false),
370 kMaxMessageSize_(8192),
yang-g88d5d522015-09-29 12:46:54 -0700371 special_service_("special") {
372 GetParam().Log();
373 }
Craig Tiller7418d012015-02-11 15:25:03 -0800374
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700375 void TearDown() GRPC_OVERRIDE {
376 if (is_server_started_) {
377 server_->Shutdown();
378 if (proxy_server_) proxy_server_->Shutdown();
379 }
380 }
381
382 void StartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) {
Craig Tiller35e39712015-01-12 16:41:24 -0800383 int port = grpc_pick_unused_port_or_die();
yang-gd7ead692015-07-30 10:57:45 -0700384 server_address_ << "127.0.0.1:" << port;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800385 // Setup server
386 ServerBuilder builder;
yang-g88d5d522015-09-29 12:46:54 -0700387 auto server_creds = InsecureServerCredentials();
388 if (GetParam().use_tls) {
389 SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
390 test_server1_cert};
391 SslServerCredentialsOptions ssl_opts;
392 ssl_opts.pem_root_certs = "";
393 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
394 server_creds = SslServerCredentials(ssl_opts);
395 server_creds->SetAuthMetadataProcessor(processor);
396 }
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700397 builder.AddListeningPort(server_address_.str(), server_creds);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800398 builder.RegisterService(&service_);
yang-g8b25f2a2015-07-21 23:54:36 -0700399 builder.RegisterService("foo.test.youtube.com", &special_service_);
Yang Gaoc71a9d22015-05-04 00:22:12 -0700400 builder.SetMaxMessageSize(
401 kMaxMessageSize_); // For testing max message size.
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800402 builder.RegisterService(&dup_pkg_service_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800403 server_ = builder.BuildAndStart();
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700404 is_server_started_ = true;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700405 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800406
yang-g9b7757d2015-08-13 11:15:53 -0700407 void ResetChannel() {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700408 if (!is_server_started_) {
409 StartServer(std::shared_ptr<AuthMetadataProcessor>());
410 }
411 EXPECT_TRUE(is_server_started_);
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700412 ChannelArguments args;
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700413 auto channel_creds = InsecureChannelCredentials();
yang-g88d5d522015-09-29 12:46:54 -0700414 if (GetParam().use_tls) {
415 SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
416 args.SetSslTargetNameOverride("foo.test.google.fr");
417 channel_creds = SslCredentials(ssl_opts);
418 }
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700419 args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test");
yang-g88d5d522015-09-29 12:46:54 -0700420 channel_ = CreateCustomChannel(server_address_.str(), channel_creds, args);
yang-g9b7757d2015-08-13 11:15:53 -0700421 }
422
yang-g88d5d522015-09-29 12:46:54 -0700423 void ResetStub() {
yang-g9b7757d2015-08-13 11:15:53 -0700424 ResetChannel();
yang-g88d5d522015-09-29 12:46:54 -0700425 if (GetParam().use_proxy) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700426 proxy_service_.reset(new Proxy(channel_));
427 int port = grpc_pick_unused_port_or_die();
428 std::ostringstream proxyaddr;
429 proxyaddr << "localhost:" << port;
430 ServerBuilder builder;
431 builder.AddListeningPort(proxyaddr.str(), InsecureServerCredentials());
432 builder.RegisterService(proxy_service_.get());
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700433 proxy_server_ = builder.BuildAndStart();
434
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700435 channel_ = CreateChannel(proxyaddr.str(), InsecureChannelCredentials());
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700436 }
437
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800438 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
yangg1456d152015-01-08 15:39:58 -0800439 }
440
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700441 bool is_server_started_;
yang-g8c2be9f2015-08-19 16:28:09 -0700442 std::shared_ptr<Channel> channel_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800443 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800444 std::unique_ptr<Server> server_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700445 std::unique_ptr<Server> proxy_server_;
446 std::unique_ptr<Proxy> proxy_service_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800447 std::ostringstream server_address_;
Yang Gao3921c562015-04-30 16:07:06 -0700448 const int kMaxMessageSize_;
nnoble0c475f02014-12-05 15:37:39 -0800449 TestServiceImpl service_;
Craig Tiller822d2c72015-07-07 16:08:00 -0700450 TestServiceImpl special_service_;
yangg1456d152015-01-08 15:39:58 -0800451 TestServiceImplDupPkg dup_pkg_service_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800452};
453
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800454static void SendRpc(grpc::testing::EchoTestService::Stub* stub, int num_rpcs) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800455 EchoRequest request;
456 EchoResponse response;
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700457 request.set_message("Hello hello hello hello");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800458
459 for (int i = 0; i < num_rpcs; ++i) {
460 ClientContext context;
Craig Tillerbf6abee2015-07-19 22:31:04 -0700461 context.set_compression_algorithm(GRPC_COMPRESS_GZIP);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800462 Status s = stub->Echo(&context, request, &response);
463 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700464 EXPECT_TRUE(s.ok());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800465 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800466}
467
yang-g88d5d522015-09-29 12:46:54 -0700468TEST_P(End2endTest, RequestStreamOneRequest) {
469 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800470 EchoRequest request;
471 EchoResponse response;
472 ClientContext context;
473
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800474 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800475 request.set_message("hello");
476 EXPECT_TRUE(stream->Write(request));
477 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800478 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800479 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700480 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800481}
482
yang-g88d5d522015-09-29 12:46:54 -0700483TEST_P(End2endTest, RequestStreamTwoRequests) {
484 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800485 EchoRequest request;
486 EchoResponse response;
487 ClientContext context;
488
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800489 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800490 request.set_message("hello");
491 EXPECT_TRUE(stream->Write(request));
492 EXPECT_TRUE(stream->Write(request));
493 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800494 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800495 EXPECT_EQ(response.message(), "hellohello");
Yang Gaoc1a2c312015-06-16 10:59:46 -0700496 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800497}
498
yang-g88d5d522015-09-29 12:46:54 -0700499TEST_P(End2endTest, ResponseStream) {
500 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800501 EchoRequest request;
502 EchoResponse response;
503 ClientContext context;
504 request.set_message("hello");
505
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800506 auto stream = stub_->ResponseStream(&context, request);
nnoble0c475f02014-12-05 15:37:39 -0800507 EXPECT_TRUE(stream->Read(&response));
508 EXPECT_EQ(response.message(), request.message() + "0");
509 EXPECT_TRUE(stream->Read(&response));
510 EXPECT_EQ(response.message(), request.message() + "1");
511 EXPECT_TRUE(stream->Read(&response));
512 EXPECT_EQ(response.message(), request.message() + "2");
513 EXPECT_FALSE(stream->Read(&response));
514
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800515 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700516 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800517}
518
yang-g88d5d522015-09-29 12:46:54 -0700519TEST_P(End2endTest, BidiStream) {
520 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800521 EchoRequest request;
522 EchoResponse response;
523 ClientContext context;
524 grpc::string msg("hello");
525
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800526 auto stream = stub_->BidiStream(&context);
nnoble0c475f02014-12-05 15:37:39 -0800527
528 request.set_message(msg + "0");
529 EXPECT_TRUE(stream->Write(request));
530 EXPECT_TRUE(stream->Read(&response));
531 EXPECT_EQ(response.message(), request.message());
532
533 request.set_message(msg + "1");
534 EXPECT_TRUE(stream->Write(request));
535 EXPECT_TRUE(stream->Read(&response));
536 EXPECT_EQ(response.message(), request.message());
537
538 request.set_message(msg + "2");
539 EXPECT_TRUE(stream->Write(request));
540 EXPECT_TRUE(stream->Read(&response));
541 EXPECT_EQ(response.message(), request.message());
542
543 stream->WritesDone();
544 EXPECT_FALSE(stream->Read(&response));
Craig Tillerca9a6372015-12-15 18:16:28 -0800545 EXPECT_FALSE(stream->Read(&response));
nnoble0c475f02014-12-05 15:37:39 -0800546
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800547 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700548 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800549}
550
551// Talk to the two services with the same name but different package names.
552// The two stubs are created on the same channel.
yang-g88d5d522015-09-29 12:46:54 -0700553TEST_P(End2endTest, DiffPackageServices) {
554 ResetStub();
yangg1456d152015-01-08 15:39:58 -0800555 EchoRequest request;
556 EchoResponse response;
557 request.set_message("Hello");
558
yangg1456d152015-01-08 15:39:58 -0800559 ClientContext context;
yang-g8b25f2a2015-07-21 23:54:36 -0700560 Status s = stub_->Echo(&context, request, &response);
yangg1456d152015-01-08 15:39:58 -0800561 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700562 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800563
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800564 std::unique_ptr<grpc::testing::duplicate::EchoTestService::Stub> dup_pkg_stub(
565 grpc::testing::duplicate::EchoTestService::NewStub(channel_));
yangg1456d152015-01-08 15:39:58 -0800566 ClientContext context2;
567 s = dup_pkg_stub->Echo(&context2, request, &response);
568 EXPECT_EQ("no package", response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700569 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800570}
571
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700572void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) {
Craig Tiller20b5fe92015-07-06 10:43:50 -0700573 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700574 gpr_time_from_micros(delay_us, GPR_TIMESPAN)));
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700575 while (!service->signal_client()) {
576 }
577 context->TryCancel();
578}
579
yang-ga89bf502015-11-17 14:19:17 -0800580TEST_P(End2endTest, CancelRpcBeforeStart) {
581 ResetStub();
582 EchoRequest request;
583 EchoResponse response;
584 ClientContext context;
585 request.set_message("hello");
586 context.TryCancel();
587 Status s = stub_->Echo(&context, request, &response);
588 EXPECT_EQ("", response.message());
589 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
590}
591
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700592// Client cancels request stream after sending two messages
yang-g88d5d522015-09-29 12:46:54 -0700593TEST_P(End2endTest, ClientCancelsRequestStream) {
594 ResetStub();
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700595 EchoRequest request;
596 EchoResponse response;
597 ClientContext context;
598 request.set_message("hello");
599
600 auto stream = stub_->RequestStream(&context, &response);
601 EXPECT_TRUE(stream->Write(request));
602 EXPECT_TRUE(stream->Write(request));
Yang Gaoc71a9d22015-05-04 00:22:12 -0700603
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700604 context.TryCancel();
605
606 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700607 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700608
Yang Gaoc71a9d22015-05-04 00:22:12 -0700609 EXPECT_EQ(response.message(), "");
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700610}
611
Abhishek Kumare41d0402015-04-17 14:12:33 -0700612// Client cancels server stream after sending some messages
yang-g88d5d522015-09-29 12:46:54 -0700613TEST_P(End2endTest, ClientCancelsResponseStream) {
614 ResetStub();
Abhishek Kumare41d0402015-04-17 14:12:33 -0700615 EchoRequest request;
616 EchoResponse response;
617 ClientContext context;
618 request.set_message("hello");
619
620 auto stream = stub_->ResponseStream(&context, request);
621
622 EXPECT_TRUE(stream->Read(&response));
623 EXPECT_EQ(response.message(), request.message() + "0");
624 EXPECT_TRUE(stream->Read(&response));
625 EXPECT_EQ(response.message(), request.message() + "1");
626
627 context.TryCancel();
628
629 // The cancellation races with responses, so there might be zero or
630 // one responses pending, read till failure
631
632 if (stream->Read(&response)) {
633 EXPECT_EQ(response.message(), request.message() + "2");
634 // Since we have cancelled, we expect the next attempt to read to fail
635 EXPECT_FALSE(stream->Read(&response));
636 }
637
638 Status s = stream->Finish();
Abhishek Kumar18298a72015-04-17 15:00:25 -0700639 // The final status could be either of CANCELLED or OK depending on
640 // who won the race.
Yang Gaoc1a2c312015-06-16 10:59:46 -0700641 EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumare41d0402015-04-17 14:12:33 -0700642}
643
Abhishek Kumar82a83312015-04-17 13:30:51 -0700644// Client cancels bidi stream after sending some messages
yang-g88d5d522015-09-29 12:46:54 -0700645TEST_P(End2endTest, ClientCancelsBidi) {
646 ResetStub();
Abhishek Kumar82a83312015-04-17 13:30:51 -0700647 EchoRequest request;
648 EchoResponse response;
649 ClientContext context;
650 grpc::string msg("hello");
651
652 auto stream = stub_->BidiStream(&context);
653
654 request.set_message(msg + "0");
655 EXPECT_TRUE(stream->Write(request));
656 EXPECT_TRUE(stream->Read(&response));
657 EXPECT_EQ(response.message(), request.message());
658
659 request.set_message(msg + "1");
660 EXPECT_TRUE(stream->Write(request));
661
662 context.TryCancel();
663
664 // The cancellation races with responses, so there might be zero or
665 // one responses pending, read till failure
666
667 if (stream->Read(&response)) {
668 EXPECT_EQ(response.message(), request.message());
669 // Since we have cancelled, we expect the next attempt to read to fail
670 EXPECT_FALSE(stream->Read(&response));
671 }
672
673 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700674 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumar82a83312015-04-17 13:30:51 -0700675}
676
yang-g88d5d522015-09-29 12:46:54 -0700677TEST_P(End2endTest, RpcMaxMessageSize) {
678 ResetStub();
Yang Gao3921c562015-04-30 16:07:06 -0700679 EchoRequest request;
680 EchoResponse response;
Yang Gaoc71a9d22015-05-04 00:22:12 -0700681 request.set_message(string(kMaxMessageSize_ * 2, 'a'));
Yang Gao3921c562015-04-30 16:07:06 -0700682
683 ClientContext context;
684 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700685 EXPECT_FALSE(s.ok());
Yang Gao3921c562015-04-30 16:07:06 -0700686}
Abhishek Kumar82a83312015-04-17 13:30:51 -0700687
yang-g88d5d522015-09-29 12:46:54 -0700688// Client sends 20 requests and the server returns CANCELLED status after
689// reading 10 requests.
690TEST_P(End2endTest, RequestStreamServerEarlyCancelTest) {
691 ResetStub();
692 EchoRequest request;
693 EchoResponse response;
694 ClientContext context;
695
696 context.AddMetadata(kServerCancelAfterReads, "10");
697 auto stream = stub_->RequestStream(&context, &response);
698 request.set_message("hello");
699 int send_messages = 20;
yang-gc0461032015-10-02 16:22:43 -0700700 while (send_messages > 10) {
yang-g88d5d522015-09-29 12:46:54 -0700701 EXPECT_TRUE(stream->Write(request));
702 send_messages--;
703 }
yang-gc0461032015-10-02 16:22:43 -0700704 while (send_messages > 0) {
705 stream->Write(request);
706 send_messages--;
707 }
yang-g88d5d522015-09-29 12:46:54 -0700708 stream->WritesDone();
709 Status s = stream->Finish();
710 EXPECT_EQ(s.error_code(), StatusCode::CANCELLED);
711}
712
yang-g88d5d522015-09-29 12:46:54 -0700713void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream,
714 gpr_event* ev) {
715 EchoResponse resp;
716 gpr_event_set(ev, (void*)1);
717 while (stream->Read(&resp)) {
718 gpr_log(GPR_INFO, "Read message");
719 }
720}
yang-g88d5d522015-09-29 12:46:54 -0700721
722// Run a Read and a WritesDone simultaneously.
723TEST_P(End2endTest, SimultaneousReadWritesDone) {
724 ResetStub();
725 ClientContext context;
726 gpr_event ev;
727 gpr_event_init(&ev);
728 auto stream = stub_->BidiStream(&context);
729 std::thread reader_thread(ReaderThreadFunc, stream.get(), &ev);
730 gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME));
731 stream->WritesDone();
732 Status s = stream->Finish();
733 EXPECT_TRUE(s.ok());
734 reader_thread.join();
735}
736
737TEST_P(End2endTest, ChannelState) {
738 ResetStub();
739 // Start IDLE
740 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(false));
741
742 // Did not ask to connect, no state change.
743 CompletionQueue cq;
744 std::chrono::system_clock::time_point deadline =
745 std::chrono::system_clock::now() + std::chrono::milliseconds(10);
746 channel_->NotifyOnStateChange(GRPC_CHANNEL_IDLE, deadline, &cq, NULL);
747 void* tag;
748 bool ok = true;
749 cq.Next(&tag, &ok);
750 EXPECT_FALSE(ok);
751
752 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true));
753 EXPECT_TRUE(channel_->WaitForStateChange(GRPC_CHANNEL_IDLE,
754 gpr_inf_future(GPR_CLOCK_REALTIME)));
yang-g0d557502015-10-01 11:30:12 -0700755 auto state = channel_->GetState(false);
756 EXPECT_TRUE(state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_READY);
yang-g88d5d522015-09-29 12:46:54 -0700757}
758
759// Takes 10s.
760TEST_P(End2endTest, ChannelStateTimeout) {
761 if (GetParam().use_tls) {
762 return;
763 }
764 int port = grpc_pick_unused_port_or_die();
765 std::ostringstream server_address;
766 server_address << "127.0.0.1:" << port;
767 // Channel to non-existing server
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700768 auto channel =
769 CreateChannel(server_address.str(), InsecureChannelCredentials());
yang-g88d5d522015-09-29 12:46:54 -0700770 // Start IDLE
771 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel->GetState(true));
772
773 auto state = GRPC_CHANNEL_IDLE;
774 for (int i = 0; i < 10; i++) {
775 channel->WaitForStateChange(
776 state, std::chrono::system_clock::now() + std::chrono::seconds(1));
777 state = channel->GetState(false);
778 }
779}
780
781// Talking to a non-existing service.
782TEST_P(End2endTest, NonExistingService) {
783 ResetChannel();
Craig Tiller1b4e3302015-12-17 16:35:00 -0800784 std::unique_ptr<grpc::testing::UnimplementedService::Stub> stub;
785 stub = grpc::testing::UnimplementedService::NewStub(channel_);
yang-g88d5d522015-09-29 12:46:54 -0700786
787 EchoRequest request;
788 EchoResponse response;
789 request.set_message("Hello");
790
791 ClientContext context;
792 Status s = stub->Unimplemented(&context, request, &response);
793 EXPECT_EQ(StatusCode::UNIMPLEMENTED, s.error_code());
794 EXPECT_EQ("", s.error_message());
795}
796
797//////////////////////////////////////////////////////////////////////////
798// Test with and without a proxy.
799class ProxyEnd2endTest : public End2endTest {
800 protected:
801};
802
803TEST_P(ProxyEnd2endTest, SimpleRpc) {
804 ResetStub();
805 SendRpc(stub_.get(), 1);
806}
807
808TEST_P(ProxyEnd2endTest, MultipleRpcs) {
809 ResetStub();
810 std::vector<std::thread*> threads;
811 for (int i = 0; i < 10; ++i) {
812 threads.push_back(new std::thread(SendRpc, stub_.get(), 10));
813 }
814 for (int i = 0; i < 10; ++i) {
815 threads[i]->join();
816 delete threads[i];
817 }
818}
819
820// Set a 10us deadline and make sure proper error is returned.
821TEST_P(ProxyEnd2endTest, RpcDeadlineExpires) {
822 ResetStub();
823 EchoRequest request;
824 EchoResponse response;
825 request.set_message("Hello");
826
827 ClientContext context;
828 std::chrono::system_clock::time_point deadline =
829 std::chrono::system_clock::now() + std::chrono::microseconds(10);
830 context.set_deadline(deadline);
831 Status s = stub_->Echo(&context, request, &response);
832 EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code());
833}
834
835// Set a long but finite deadline.
836TEST_P(ProxyEnd2endTest, RpcLongDeadline) {
837 ResetStub();
838 EchoRequest request;
839 EchoResponse response;
840 request.set_message("Hello");
841
842 ClientContext context;
843 std::chrono::system_clock::time_point deadline =
844 std::chrono::system_clock::now() + std::chrono::hours(1);
845 context.set_deadline(deadline);
846 Status s = stub_->Echo(&context, request, &response);
847 EXPECT_EQ(response.message(), request.message());
848 EXPECT_TRUE(s.ok());
849}
850
851// Ask server to echo back the deadline it sees.
852TEST_P(ProxyEnd2endTest, EchoDeadline) {
853 ResetStub();
854 EchoRequest request;
855 EchoResponse response;
856 request.set_message("Hello");
857 request.mutable_param()->set_echo_deadline(true);
858
859 ClientContext context;
860 std::chrono::system_clock::time_point deadline =
861 std::chrono::system_clock::now() + std::chrono::seconds(100);
862 context.set_deadline(deadline);
863 Status s = stub_->Echo(&context, request, &response);
864 EXPECT_EQ(response.message(), request.message());
865 EXPECT_TRUE(s.ok());
866 gpr_timespec sent_deadline;
867 Timepoint2Timespec(deadline, &sent_deadline);
868 // Allow 1 second error.
869 EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1);
870 EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1);
871}
872
873// Ask server to echo back the deadline it sees. The rpc has no deadline.
874TEST_P(ProxyEnd2endTest, EchoDeadlineForNoDeadlineRpc) {
875 ResetStub();
876 EchoRequest request;
877 EchoResponse response;
878 request.set_message("Hello");
879 request.mutable_param()->set_echo_deadline(true);
880
881 ClientContext context;
882 Status s = stub_->Echo(&context, request, &response);
883 EXPECT_EQ(response.message(), request.message());
884 EXPECT_TRUE(s.ok());
885 EXPECT_EQ(response.param().request_deadline(),
886 gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec);
887}
888
889TEST_P(ProxyEnd2endTest, UnimplementedRpc) {
890 ResetStub();
891 EchoRequest request;
892 EchoResponse response;
893 request.set_message("Hello");
894
895 ClientContext context;
896 Status s = stub_->Unimplemented(&context, request, &response);
897 EXPECT_FALSE(s.ok());
898 EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED);
899 EXPECT_EQ(s.error_message(), "");
900 EXPECT_EQ(response.message(), "");
901}
902
903// Client cancels rpc after 10ms
904TEST_P(ProxyEnd2endTest, ClientCancelsRpc) {
905 ResetStub();
906 EchoRequest request;
907 EchoResponse response;
908 request.set_message("Hello");
909 const int kCancelDelayUs = 10 * 1000;
910 request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs);
911
912 ClientContext context;
913 std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_);
914 Status s = stub_->Echo(&context, request, &response);
915 cancel_thread.join();
916 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
917 EXPECT_EQ(s.error_message(), "Cancelled");
918}
919
920// Server cancels rpc after 1ms
921TEST_P(ProxyEnd2endTest, ServerCancelsRpc) {
922 ResetStub();
923 EchoRequest request;
924 EchoResponse response;
925 request.set_message("Hello");
926 request.mutable_param()->set_server_cancel_after_us(1000);
927
928 ClientContext context;
929 Status s = stub_->Echo(&context, request, &response);
930 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
931 EXPECT_TRUE(s.error_message().empty());
932}
933
934// Make the response larger than the flow control window.
935TEST_P(ProxyEnd2endTest, HugeResponse) {
936 ResetStub();
937 EchoRequest request;
938 EchoResponse response;
939 request.set_message("huge response");
940 const size_t kResponseSize = 1024 * (1024 + 10);
941 request.mutable_param()->set_response_message_length(kResponseSize);
942
943 ClientContext context;
944 Status s = stub_->Echo(&context, request, &response);
945 EXPECT_EQ(kResponseSize, response.message().size());
946 EXPECT_TRUE(s.ok());
947}
948
949TEST_P(ProxyEnd2endTest, Peer) {
950 ResetStub();
951 EchoRequest request;
952 EchoResponse response;
953 request.set_message("hello");
954 request.mutable_param()->set_echo_peer(true);
955
956 ClientContext context;
957 Status s = stub_->Echo(&context, request, &response);
958 EXPECT_EQ(response.message(), request.message());
959 EXPECT_TRUE(s.ok());
960 EXPECT_TRUE(CheckIsLocalhost(response.param().peer()));
961 EXPECT_TRUE(CheckIsLocalhost(context.peer()));
962}
963
964//////////////////////////////////////////////////////////////////////////
965class SecureEnd2endTest : public End2endTest {
966 protected:
967 SecureEnd2endTest() {
968 GPR_ASSERT(!GetParam().use_proxy);
969 GPR_ASSERT(GetParam().use_tls);
970 }
971};
972
973TEST_P(SecureEnd2endTest, SimpleRpcWithHost) {
974 ResetStub();
975
976 EchoRequest request;
977 EchoResponse response;
978 request.set_message("Hello");
979
980 ClientContext context;
981 context.set_authority("foo.test.youtube.com");
982 Status s = stub_->Echo(&context, request, &response);
983 EXPECT_EQ(response.message(), request.message());
984 EXPECT_TRUE(response.has_param());
985 EXPECT_EQ("special", response.param().host());
986 EXPECT_TRUE(s.ok());
987}
988
yang-ge21908f2015-08-25 13:47:51 -0700989bool MetadataContains(
990 const std::multimap<grpc::string_ref, grpc::string_ref>& metadata,
991 const grpc::string& key, const grpc::string& value) {
Yang Gao26a49122015-05-15 17:02:56 -0700992 int count = 0;
993
yang-ge21908f2015-08-25 13:47:51 -0700994 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator iter =
Yang Gao26a49122015-05-15 17:02:56 -0700995 metadata.begin();
996 iter != metadata.end(); ++iter) {
yang-ge21908f2015-08-25 13:47:51 -0700997 if (ToString(iter->first) == key && ToString(iter->second) == value) {
Yang Gao26a49122015-05-15 17:02:56 -0700998 count++;
999 }
1000 }
1001 return count == 1;
1002}
1003
yang-g88d5d522015-09-29 12:46:54 -07001004TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorSuccess) {
1005 auto* processor = new TestAuthMetadataProcessor(true);
1006 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1007 ResetStub();
1008 EchoRequest request;
1009 EchoResponse response;
1010 ClientContext context;
1011 context.set_credentials(processor->GetCompatibleClientCreds());
1012 request.set_message("Hello");
1013 request.mutable_param()->set_echo_metadata(true);
1014 request.mutable_param()->set_expected_client_identity(
1015 TestAuthMetadataProcessor::kGoodGuy);
1016
1017 Status s = stub_->Echo(&context, request, &response);
1018 EXPECT_EQ(request.message(), response.message());
1019 EXPECT_TRUE(s.ok());
1020
1021 // Metadata should have been consumed by the processor.
1022 EXPECT_FALSE(MetadataContains(
1023 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1024 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1025}
1026
1027TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorFailure) {
1028 auto* processor = new TestAuthMetadataProcessor(true);
1029 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1030 ResetStub();
1031 EchoRequest request;
1032 EchoResponse response;
1033 ClientContext context;
1034 context.set_credentials(processor->GetIncompatibleClientCreds());
1035 request.set_message("Hello");
1036
1037 Status s = stub_->Echo(&context, request, &response);
1038 EXPECT_FALSE(s.ok());
1039 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1040}
1041TEST_P(SecureEnd2endTest, SetPerCallCredentials) {
1042 ResetStub();
Yang Gaoa8938922015-05-14 11:51:07 -07001043 EchoRequest request;
1044 EchoResponse response;
1045 ClientContext context;
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001046 std::shared_ptr<CallCredentials> creds =
Julien Boeuf510a9202015-08-25 21:51:07 -07001047 GoogleIAMCredentials("fake_token", "fake_selector");
Yang Gaoa8938922015-05-14 11:51:07 -07001048 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -07001049 request.set_message("Hello");
1050 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -07001051
1052 Status s = stub_->Echo(&context, request, &response);
Yang Gaoa8938922015-05-14 11:51:07 -07001053 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -07001054 EXPECT_TRUE(s.ok());
Yang Gao26a49122015-05-15 17:02:56 -07001055 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1056 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1057 "fake_token"));
1058 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1059 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1060 "fake_selector"));
Yang Gaoa8938922015-05-14 11:51:07 -07001061}
1062
yang-g88d5d522015-09-29 12:46:54 -07001063TEST_P(SecureEnd2endTest, OverridePerCallCredentials) {
1064 ResetStub();
Yang Gaoa8938922015-05-14 11:51:07 -07001065 EchoRequest request;
1066 EchoResponse response;
1067 ClientContext context;
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001068 std::shared_ptr<CallCredentials> creds1 =
Julien Boeuf510a9202015-08-25 21:51:07 -07001069 GoogleIAMCredentials("fake_token1", "fake_selector1");
Yang Gaoa8938922015-05-14 11:51:07 -07001070 context.set_credentials(creds1);
Julien Boeufe5adc0e2015-10-12 14:08:10 -07001071 std::shared_ptr<CallCredentials> creds2 =
Julien Boeuf510a9202015-08-25 21:51:07 -07001072 GoogleIAMCredentials("fake_token2", "fake_selector2");
Yang Gaoa8938922015-05-14 11:51:07 -07001073 context.set_credentials(creds2);
Yang Gao26a49122015-05-15 17:02:56 -07001074 request.set_message("Hello");
1075 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -07001076
1077 Status s = stub_->Echo(&context, request, &response);
Yang Gao26a49122015-05-15 17:02:56 -07001078 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1079 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1080 "fake_token2"));
1081 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
1082 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1083 "fake_selector2"));
Yang Gaob57f72d2015-05-17 21:54:54 -07001084 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
1085 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
1086 "fake_token1"));
1087 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
1088 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
1089 "fake_selector1"));
Yang Gaoa8938922015-05-14 11:51:07 -07001090 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -07001091 EXPECT_TRUE(s.ok());
Yang Gaoa8938922015-05-14 11:51:07 -07001092}
1093
yang-g88d5d522015-09-29 12:46:54 -07001094TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginFailure) {
1095 ResetStub();
Julien Boeuf1928d492015-09-15 15:20:11 -07001096 EchoRequest request;
1097 EchoResponse response;
1098 ClientContext context;
1099 context.set_credentials(
1100 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1101 new TestMetadataCredentialsPlugin(
1102 "Does not matter, will fail anyway (see 3rd param)", false,
1103 false))));
1104 request.set_message("Hello");
1105
1106 Status s = stub_->Echo(&context, request, &response);
1107 EXPECT_FALSE(s.ok());
1108 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1109}
1110
yang-g88d5d522015-09-29 12:46:54 -07001111TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorSuccess) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001112 auto* processor = new TestAuthMetadataProcessor(false);
1113 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
yang-g88d5d522015-09-29 12:46:54 -07001114 ResetStub();
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001115 EchoRequest request;
1116 EchoResponse response;
1117 ClientContext context;
1118 context.set_credentials(processor->GetCompatibleClientCreds());
1119 request.set_message("Hello");
1120 request.mutable_param()->set_echo_metadata(true);
1121 request.mutable_param()->set_expected_client_identity(
1122 TestAuthMetadataProcessor::kGoodGuy);
1123
1124 Status s = stub_->Echo(&context, request, &response);
1125 EXPECT_EQ(request.message(), response.message());
1126 EXPECT_TRUE(s.ok());
1127
1128 // Metadata should have been consumed by the processor.
1129 EXPECT_FALSE(MetadataContains(
1130 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1131 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1132}
1133
yang-g88d5d522015-09-29 12:46:54 -07001134TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorFailure) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001135 auto* processor = new TestAuthMetadataProcessor(false);
1136 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
yang-g88d5d522015-09-29 12:46:54 -07001137 ResetStub();
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001138 EchoRequest request;
1139 EchoResponse response;
1140 ClientContext context;
1141 context.set_credentials(processor->GetIncompatibleClientCreds());
1142 request.set_message("Hello");
1143
1144 Status s = stub_->Echo(&context, request, &response);
1145 EXPECT_FALSE(s.ok());
1146 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1147}
1148
yang-g88d5d522015-09-29 12:46:54 -07001149TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginFailure) {
1150 ResetStub();
Julien Boeuf1928d492015-09-15 15:20:11 -07001151 EchoRequest request;
1152 EchoResponse response;
1153 ClientContext context;
1154 context.set_credentials(
1155 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
1156 new TestMetadataCredentialsPlugin(
1157 "Does not matter, will fail anyway (see 3rd param)", true,
1158 false))));
1159 request.set_message("Hello");
1160
1161 Status s = stub_->Echo(&context, request, &response);
1162 EXPECT_FALSE(s.ok());
1163 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1164}
1165
yang-g88d5d522015-09-29 12:46:54 -07001166TEST_P(SecureEnd2endTest, ClientAuthContext) {
1167 ResetStub();
yang-gc4eef2e2015-07-06 23:26:58 -07001168 EchoRequest request;
1169 EchoResponse response;
1170 request.set_message("Hello");
1171 request.mutable_param()->set_check_auth_context(true);
1172
1173 ClientContext context;
1174 Status s = stub_->Echo(&context, request, &response);
1175 EXPECT_EQ(response.message(), request.message());
1176 EXPECT_TRUE(s.ok());
1177
yang-g8b25f2a2015-07-21 23:54:36 -07001178 std::shared_ptr<const AuthContext> auth_ctx = context.auth_context();
yang-gd090fe12015-08-25 16:53:07 -07001179 std::vector<grpc::string_ref> ssl =
yang-g8b25f2a2015-07-21 23:54:36 -07001180 auth_ctx->FindPropertyValues("transport_security_type");
1181 EXPECT_EQ(1u, ssl.size());
yang-gd090fe12015-08-25 16:53:07 -07001182 EXPECT_EQ("ssl", ToString(ssl[0]));
yang-g8b25f2a2015-07-21 23:54:36 -07001183 EXPECT_EQ("x509_subject_alternative_name",
1184 auth_ctx->GetPeerIdentityPropertyName());
1185 EXPECT_EQ(3u, auth_ctx->GetPeerIdentity().size());
yang-gd090fe12015-08-25 16:53:07 -07001186 EXPECT_EQ("*.test.google.fr", ToString(auth_ctx->GetPeerIdentity()[0]));
1187 EXPECT_EQ("waterzooi.test.google.be",
1188 ToString(auth_ctx->GetPeerIdentity()[1]));
1189 EXPECT_EQ("*.test.youtube.com", ToString(auth_ctx->GetPeerIdentity()[2]));
yang-gc4eef2e2015-07-06 23:26:58 -07001190}
1191
yang-g88d5d522015-09-29 12:46:54 -07001192INSTANTIATE_TEST_CASE_P(End2end, End2endTest,
Craig Tiller93b94472015-11-02 14:18:30 -08001193 ::testing::Values(TestScenario(false, false),
1194 TestScenario(false, true)));
yang-g6f30dec2015-07-22 23:11:56 -07001195
yang-g88d5d522015-09-29 12:46:54 -07001196INSTANTIATE_TEST_CASE_P(ProxyEnd2end, ProxyEnd2endTest,
Craig Tiller93b94472015-11-02 14:18:30 -08001197 ::testing::Values(TestScenario(false, false),
yang-g88d5d522015-09-29 12:46:54 -07001198 TestScenario(false, true),
Craig Tiller93b94472015-11-02 14:18:30 -08001199 TestScenario(true, false),
1200 TestScenario(true, true)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001201
yang-g88d5d522015-09-29 12:46:54 -07001202INSTANTIATE_TEST_CASE_P(SecureEnd2end, SecureEnd2endTest,
1203 ::testing::Values(TestScenario(false, true)));
1204
yang-g8ab38362015-07-31 14:05:33 -07001205} // namespace
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001206} // namespace testing
1207} // namespace grpc
1208
1209int main(int argc, char** argv) {
1210 grpc_test_init(argc, argv);
1211 ::testing::InitGoogleTest(&argc, argv);
1212 return RUN_ALL_TESTS();
1213}