blob: 5017e8a230f44837847ff60de3a69eb79514c0b5 [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/grpc.h>
38#include <grpc/support/thd.h>
39#include <grpc/support/time.h>
40#include <grpc++/channel.h>
41#include <grpc++/client_context.h>
42#include <grpc++/create_channel.h>
Julien Boeuf5be92a32015-08-28 16:28:18 -070043#include <grpc++/security/auth_metadata_processor.h>
44#include <grpc++/security/credentials.h>
45#include <grpc++/security/server_credentials.h>
yang-g9e2f90c2015-08-21 15:35:03 -070046#include <grpc++/server.h>
47#include <grpc++/server_builder.h>
48#include <grpc++/server_context.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"
yang-g8b25f2a2015-07-21 23:54:36 -070052#include "test/core/end2end/data/ssl_test_data.h"
Nicolas Noble89219162015-04-07 18:01:18 -070053#include "test/core/util/port.h"
Craig Tiller14e60e92015-01-13 17:26:46 -080054#include "test/core/util/test_config.h"
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020055#include "test/cpp/util/echo_duplicate.grpc.pb.h"
56#include "test/cpp/util/echo.grpc.pb.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
59using grpc::cpp::test::util::EchoRequest;
60using grpc::cpp::test::util::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 {
65
66namespace {
67
yang-g0b6ad7d2015-06-25 14:39:01 -070068const char* kServerCancelAfterReads = "cancel_after_reads";
69
yangged5e7e02015-01-06 10:16:15 -080070// When echo_deadline is requested, deadline seen in the ServerContext is set in
71// the response in seconds.
72void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request,
73 EchoResponse* response) {
74 if (request->has_param() && request->param().echo_deadline()) {
Craig Tiller354398f2015-07-13 09:16:03 -070075 gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
Nicolas Noble89219162015-04-07 18:01:18 -070076 if (context->deadline() != system_clock::time_point::max()) {
77 Timepoint2Timespec(context->deadline(), &deadline);
yangged5e7e02015-01-06 10:16:15 -080078 }
79 response->mutable_param()->set_request_deadline(deadline.tv_sec);
80 }
81}
Craig Tiller7418d012015-02-11 15:25:03 -080082
Julien Boeuf0c711ad2015-08-28 14:10:58 -070083void CheckServerAuthContext(const ServerContext* context,
84 const grpc::string& expected_client_identity) {
yang-g85c04f92015-07-07 17:47:31 -070085 std::shared_ptr<const AuthContext> auth_ctx = context->auth_context();
yang-gd090fe12015-08-25 16:53:07 -070086 std::vector<grpc::string_ref> ssl =
yang-gc4eef2e2015-07-06 23:26:58 -070087 auth_ctx->FindPropertyValues("transport_security_type");
yang-g8b25f2a2015-07-21 23:54:36 -070088 EXPECT_EQ(1u, ssl.size());
yang-gd090fe12015-08-25 16:53:07 -070089 EXPECT_EQ("ssl", ToString(ssl[0]));
Julien Boeuf0c711ad2015-08-28 14:10:58 -070090 if (expected_client_identity.length() == 0) {
91 EXPECT_TRUE(auth_ctx->GetPeerIdentityPropertyName().empty());
92 EXPECT_TRUE(auth_ctx->GetPeerIdentity().empty());
93 EXPECT_FALSE(auth_ctx->IsPeerAuthenticated());
94 } else {
95 auto identity = auth_ctx->GetPeerIdentity();
96 EXPECT_TRUE(auth_ctx->IsPeerAuthenticated());
97 EXPECT_EQ(1u, identity.size());
98 EXPECT_EQ(expected_client_identity, identity[0]);
99 }
yang-gc4eef2e2015-07-06 23:26:58 -0700100}
101
yang-gd7ead692015-07-30 10:57:45 -0700102bool CheckIsLocalhost(const grpc::string& addr) {
103 const grpc::string kIpv6("ipv6:[::1]:");
104 const grpc::string kIpv4MappedIpv6("ipv6:[::ffff:127.0.0.1]:");
105 const grpc::string kIpv4("ipv4:127.0.0.1:");
106 return addr.substr(0, kIpv4.size()) == kIpv4 ||
107 addr.substr(0, kIpv4MappedIpv6.size()) == kIpv4MappedIpv6 ||
108 addr.substr(0, kIpv6.size()) == kIpv6;
109}
110
Julien Boeuf1928d492015-09-15 15:20:11 -0700111class TestMetadataCredentialsPlugin : public MetadataCredentialsPlugin {
112 public:
113 static const char kMetadataKey[];
114
115 TestMetadataCredentialsPlugin(grpc::string_ref metadata_value,
116 bool is_blocking, bool is_successful)
117 : metadata_value_(metadata_value.data(), metadata_value.length()),
118 is_blocking_(is_blocking),
119 is_successful_(is_successful) {}
120
121 bool IsBlocking() const GRPC_OVERRIDE { return is_blocking_; }
122
123 Status GetMetadata(grpc::string_ref service_url,
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);
127 EXPECT_TRUE(metadata != nullptr);
128 if (is_successful_) {
129 metadata->insert(std::make_pair(kMetadataKey, metadata_value_));
130 return Status::OK;
131 } else {
132 return Status(StatusCode::NOT_FOUND, "Could not find plugin metadata.");
133 }
134 }
135
136 private:
137 grpc::string metadata_value_;
138 bool is_blocking_;
139 bool is_successful_;
140};
141
142const char TestMetadataCredentialsPlugin::kMetadataKey[] = "TestPluginMetadata";
143
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700144class TestAuthMetadataProcessor : public AuthMetadataProcessor {
145 public:
146 static const char kGoodGuy[];
147
148 TestAuthMetadataProcessor(bool is_blocking) : is_blocking_(is_blocking) {}
149
150 std::shared_ptr<Credentials> GetCompatibleClientCreds() {
Julien Boeuf1928d492015-09-15 15:20:11 -0700151 return MetadataCredentialsFromPlugin(
152 std::unique_ptr<MetadataCredentialsPlugin>(
153 new TestMetadataCredentialsPlugin(kGoodGuy, is_blocking_, true)));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700154 }
Julien Boeuf1928d492015-09-15 15:20:11 -0700155
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700156 std::shared_ptr<Credentials> GetIncompatibleClientCreds() {
Julien Boeuf1928d492015-09-15 15:20:11 -0700157 return MetadataCredentialsFromPlugin(
158 std::unique_ptr<MetadataCredentialsPlugin>(
159 new TestMetadataCredentialsPlugin("Mr Hyde", is_blocking_, true)));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700160 }
161
162 // Interface implementation
163 bool IsBlocking() const GRPC_OVERRIDE { return is_blocking_; }
164
165 Status Process(const InputMetadata& auth_metadata, AuthContext* context,
166 OutputMetadata* consumed_auth_metadata,
167 OutputMetadata* response_metadata) GRPC_OVERRIDE {
168 EXPECT_TRUE(consumed_auth_metadata != nullptr);
169 EXPECT_TRUE(context != nullptr);
170 EXPECT_TRUE(response_metadata != nullptr);
Julien Boeuf1928d492015-09-15 15:20:11 -0700171 auto auth_md =
172 auth_metadata.find(TestMetadataCredentialsPlugin::kMetadataKey);
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700173 EXPECT_NE(auth_md, auth_metadata.end());
174 string_ref auth_md_value = auth_md->second;
Julien Boeuf1928d492015-09-15 15:20:11 -0700175 if (auth_md_value == kGoodGuy) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700176 context->AddProperty(kIdentityPropName, kGoodGuy);
177 context->SetPeerIdentityPropertyName(kIdentityPropName);
Julien Boeuf8b0b6f42015-09-22 13:31:16 -0700178 consumed_auth_metadata->insert(std::make_pair(
179 string(auth_md->first.data(), auth_md->first.length()),
180 string(auth_md->second.data(), auth_md->second.length())));
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700181 return Status::OK;
182 } else {
183 return Status(StatusCode::UNAUTHENTICATED,
184 string("Invalid principal: ") +
185 string(auth_md_value.data(), auth_md_value.length()));
186 }
187 }
188
Julien Boeuf1928d492015-09-15 15:20:11 -0700189 private:
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700190 static const char kIdentityPropName[];
191 bool is_blocking_;
192};
193
194const char TestAuthMetadataProcessor::kGoodGuy[] = "Dr Jekyll";
195const char TestAuthMetadataProcessor::kIdentityPropName[] = "novel identity";
196
197
yangged5e7e02015-01-06 10:16:15 -0800198} // namespace
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800199
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700200class Proxy : public ::grpc::cpp::test::util::TestService::Service {
201 public:
yang-g8c2be9f2015-08-19 16:28:09 -0700202 Proxy(std::shared_ptr<Channel> channel)
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700203 : stub_(grpc::cpp::test::util::TestService::NewStub(channel)) {}
204
205 Status Echo(ServerContext* server_context, const EchoRequest* request,
206 EchoResponse* response) GRPC_OVERRIDE {
207 std::unique_ptr<ClientContext> client_context =
208 ClientContext::FromServerContext(*server_context);
209 return stub_->Echo(client_context.get(), *request, response);
210 }
211
212 private:
David G. Quintas086a9822015-08-11 18:56:10 -0700213 std::unique_ptr< ::grpc::cpp::test::util::TestService::Stub> stub_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700214};
215
yangg1456d152015-01-08 15:39:58 -0800216class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800217 public:
vjpaidf551612015-07-14 13:38:44 -0700218 TestServiceImpl() : signal_client_(false), host_() {}
Vijay Pai181ef452015-07-14 13:52:48 -0700219 explicit TestServiceImpl(const grpc::string& host)
220 : signal_client_(false), host_(new grpc::string(host)) {}
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700221
yangga4b6f5d2014-12-17 15:53:12 -0800222 Status Echo(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800223 EchoResponse* response) GRPC_OVERRIDE {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800224 response->set_message(request->message());
yangged5e7e02015-01-06 10:16:15 -0800225 MaybeEchoDeadline(context, request, response);
Craig Tiller822d2c72015-07-07 16:08:00 -0700226 if (host_) {
227 response->mutable_param()->set_host(*host_);
228 }
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700229 if (request->has_param() && request->param().client_cancel_after_us()) {
230 {
231 std::unique_lock<std::mutex> lock(mu_);
232 signal_client_ = true;
233 }
234 while (!context->IsCancelled()) {
David Garcia Quintasfeb67f62015-05-20 19:23:25 -0700235 gpr_sleep_until(gpr_time_add(
Craig Tiller20b5fe92015-07-06 10:43:50 -0700236 gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700237 gpr_time_from_micros(request->param().client_cancel_after_us(),
238 GPR_TIMESPAN)));
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700239 }
Yang Gaoc1a2c312015-06-16 10:59:46 -0700240 return Status::CANCELLED;
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700241 } else if (request->has_param() &&
242 request->param().server_cancel_after_us()) {
David Garcia Quintasfeb67f62015-05-20 19:23:25 -0700243 gpr_sleep_until(gpr_time_add(
Craig Tiller20b5fe92015-07-06 10:43:50 -0700244 gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700245 gpr_time_from_micros(request->param().server_cancel_after_us(),
246 GPR_TIMESPAN)));
Yang Gaoc1a2c312015-06-16 10:59:46 -0700247 return Status::CANCELLED;
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700248 } else {
249 EXPECT_FALSE(context->IsCancelled());
250 }
Yang Gao26a49122015-05-15 17:02:56 -0700251
252 if (request->has_param() && request->param().echo_metadata()) {
yang-ge21908f2015-08-25 13:47:51 -0700253 const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata =
Yang Gao26a49122015-05-15 17:02:56 -0700254 context->client_metadata();
yang-ge21908f2015-08-25 13:47:51 -0700255 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
256 iter = client_metadata.begin();
Yang Gao26a49122015-05-15 17:02:56 -0700257 iter != client_metadata.end(); ++iter) {
yang-ge21908f2015-08-25 13:47:51 -0700258 context->AddTrailingMetadata(ToString(iter->first),
259 ToString(iter->second));
Yang Gao26a49122015-05-15 17:02:56 -0700260 }
261 }
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700262 if (request->has_param() &&
263 (request->param().expected_client_identity().length() > 0 ||
264 request->param().check_auth_context())) {
265 CheckServerAuthContext(context, 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
347 : public ::grpc::cpp::test::util::duplicate::TestService::Service {
348 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
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700356/* Param is whether or not to use a proxy -- some tests use TEST_F as they don't
357 need this functionality */
358class End2endTest : public ::testing::TestWithParam<bool> {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800359 protected:
Vijay Pai181ef452015-07-14 13:52:48 -0700360 End2endTest()
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700361 : is_server_started_(false),
362 kMaxMessageSize_(8192),
363 special_service_("special") {}
Craig Tiller7418d012015-02-11 15:25:03 -0800364
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700365 void TearDown() GRPC_OVERRIDE {
366 if (is_server_started_) {
367 server_->Shutdown();
368 if (proxy_server_) proxy_server_->Shutdown();
369 }
370 }
371
372 void StartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) {
Craig Tiller35e39712015-01-12 16:41:24 -0800373 int port = grpc_pick_unused_port_or_die();
yang-gd7ead692015-07-30 10:57:45 -0700374 server_address_ << "127.0.0.1:" << port;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800375 // Setup server
376 ServerBuilder builder;
yang-g8b25f2a2015-07-21 23:54:36 -0700377 SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
Craig Tillerd6c98df2015-08-18 09:33:44 -0700378 test_server1_cert};
yang-g8b25f2a2015-07-21 23:54:36 -0700379 SslServerCredentialsOptions ssl_opts;
380 ssl_opts.pem_root_certs = "";
381 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700382 auto server_creds = SslServerCredentials(ssl_opts);
383 server_creds->SetAuthMetadataProcessor(processor);
384 builder.AddListeningPort(server_address_.str(), server_creds);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800385 builder.RegisterService(&service_);
yang-g8b25f2a2015-07-21 23:54:36 -0700386 builder.RegisterService("foo.test.youtube.com", &special_service_);
Yang Gaoc71a9d22015-05-04 00:22:12 -0700387 builder.SetMaxMessageSize(
388 kMaxMessageSize_); // For testing max message size.
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800389 builder.RegisterService(&dup_pkg_service_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800390 server_ = builder.BuildAndStart();
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700391 is_server_started_ = true;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700392 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800393
yang-g9b7757d2015-08-13 11:15:53 -0700394 void ResetChannel() {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700395 if (!is_server_started_) {
396 StartServer(std::shared_ptr<AuthMetadataProcessor>());
397 }
398 EXPECT_TRUE(is_server_started_);
yang-g8b25f2a2015-07-21 23:54:36 -0700399 SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700400 ChannelArguments args;
yang-g8b25f2a2015-07-21 23:54:36 -0700401 args.SetSslTargetNameOverride("foo.test.google.fr");
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700402 args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test");
yang-g730055d2015-08-27 12:29:45 -0700403 channel_ = CreateCustomChannel(server_address_.str(),
404 SslCredentials(ssl_opts), args);
yang-g9b7757d2015-08-13 11:15:53 -0700405 }
406
407 void ResetStub(bool use_proxy) {
408 ResetChannel();
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700409 if (use_proxy) {
410 proxy_service_.reset(new Proxy(channel_));
411 int port = grpc_pick_unused_port_or_die();
412 std::ostringstream proxyaddr;
413 proxyaddr << "localhost:" << port;
414 ServerBuilder builder;
415 builder.AddListeningPort(proxyaddr.str(), InsecureServerCredentials());
416 builder.RegisterService(proxy_service_.get());
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700417 proxy_server_ = builder.BuildAndStart();
418
yang-g730055d2015-08-27 12:29:45 -0700419 channel_ = CreateChannel(proxyaddr.str(), InsecureCredentials());
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700420 }
421
Nicolas "Pixel" Noble7fa51672015-09-02 02:29:09 +0200422 stub_ = grpc::cpp::test::util::TestService::NewStub(channel_);
yangg1456d152015-01-08 15:39:58 -0800423 }
424
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700425 bool is_server_started_;
yang-g8c2be9f2015-08-19 16:28:09 -0700426 std::shared_ptr<Channel> channel_;
yangg1456d152015-01-08 15:39:58 -0800427 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800428 std::unique_ptr<Server> server_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700429 std::unique_ptr<Server> proxy_server_;
430 std::unique_ptr<Proxy> proxy_service_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800431 std::ostringstream server_address_;
Yang Gao3921c562015-04-30 16:07:06 -0700432 const int kMaxMessageSize_;
nnoble0c475f02014-12-05 15:37:39 -0800433 TestServiceImpl service_;
Craig Tiller822d2c72015-07-07 16:08:00 -0700434 TestServiceImpl special_service_;
yangg1456d152015-01-08 15:39:58 -0800435 TestServiceImplDupPkg dup_pkg_service_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800436};
437
yangg1456d152015-01-08 15:39:58 -0800438static void SendRpc(grpc::cpp::test::util::TestService::Stub* stub,
439 int num_rpcs) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800440 EchoRequest request;
441 EchoResponse response;
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700442 request.set_message("Hello hello hello hello");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800443
444 for (int i = 0; i < num_rpcs; ++i) {
445 ClientContext context;
Craig Tillerbf6abee2015-07-19 22:31:04 -0700446 context.set_compression_algorithm(GRPC_COMPRESS_GZIP);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800447 Status s = stub->Echo(&context, request, &response);
448 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700449 EXPECT_TRUE(s.ok());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800450 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800451}
452
Craig Tiller822d2c72015-07-07 16:08:00 -0700453TEST_F(End2endTest, SimpleRpcWithHost) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700454 ResetStub(false);
Craig Tiller822d2c72015-07-07 16:08:00 -0700455
456 EchoRequest request;
457 EchoResponse response;
458 request.set_message("Hello");
459
460 ClientContext context;
yang-g8b25f2a2015-07-21 23:54:36 -0700461 context.set_authority("foo.test.youtube.com");
Craig Tiller822d2c72015-07-07 16:08:00 -0700462 Status s = stub_->Echo(&context, request, &response);
463 EXPECT_EQ(response.message(), request.message());
464 EXPECT_TRUE(response.has_param());
yang-g8b25f2a2015-07-21 23:54:36 -0700465 EXPECT_EQ("special", response.param().host());
Craig Tiller822d2c72015-07-07 16:08:00 -0700466 EXPECT_TRUE(s.ok());
467}
468
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700469TEST_P(End2endTest, SimpleRpc) {
470 ResetStub(GetParam());
yangg1456d152015-01-08 15:39:58 -0800471 SendRpc(stub_.get(), 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800472}
473
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700474TEST_P(End2endTest, MultipleRpcs) {
475 ResetStub(GetParam());
Craig Tiller35e39712015-01-12 16:41:24 -0800476 std::vector<std::thread*> threads;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800477 for (int i = 0; i < 10; ++i) {
yangg1456d152015-01-08 15:39:58 -0800478 threads.push_back(new std::thread(SendRpc, stub_.get(), 10));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800479 }
480 for (int i = 0; i < 10; ++i) {
481 threads[i]->join();
482 delete threads[i];
483 }
484}
485
yanggda029e32014-12-18 10:24:04 -0800486// Set a 10us deadline and make sure proper error is returned.
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700487TEST_P(End2endTest, RpcDeadlineExpires) {
488 ResetStub(GetParam());
yanggda029e32014-12-18 10:24:04 -0800489 EchoRequest request;
490 EchoResponse response;
491 request.set_message("Hello");
492
493 ClientContext context;
494 std::chrono::system_clock::time_point deadline =
495 std::chrono::system_clock::now() + std::chrono::microseconds(10);
Nicolas Noble89219162015-04-07 18:01:18 -0700496 context.set_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800497 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700498 EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code());
yanggda029e32014-12-18 10:24:04 -0800499}
500
yangged5e7e02015-01-06 10:16:15 -0800501// Set a long but finite deadline.
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700502TEST_P(End2endTest, RpcLongDeadline) {
503 ResetStub(GetParam());
yangged5e7e02015-01-06 10:16:15 -0800504 EchoRequest request;
505 EchoResponse response;
506 request.set_message("Hello");
507
508 ClientContext context;
509 std::chrono::system_clock::time_point deadline =
510 std::chrono::system_clock::now() + std::chrono::hours(1);
Nicolas Noble89219162015-04-07 18:01:18 -0700511 context.set_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800512 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800513 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700514 EXPECT_TRUE(s.ok());
yangged5e7e02015-01-06 10:16:15 -0800515}
516
517// Ask server to echo back the deadline it sees.
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700518TEST_P(End2endTest, EchoDeadline) {
519 ResetStub(GetParam());
yangged5e7e02015-01-06 10:16:15 -0800520 EchoRequest request;
521 EchoResponse response;
522 request.set_message("Hello");
523 request.mutable_param()->set_echo_deadline(true);
524
525 ClientContext context;
526 std::chrono::system_clock::time_point deadline =
527 std::chrono::system_clock::now() + std::chrono::seconds(100);
Nicolas Noble89219162015-04-07 18:01:18 -0700528 context.set_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800529 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800530 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700531 EXPECT_TRUE(s.ok());
yangged5e7e02015-01-06 10:16:15 -0800532 gpr_timespec sent_deadline;
533 Timepoint2Timespec(deadline, &sent_deadline);
534 // Allow 1 second error.
535 EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1);
536 EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1);
yangged5e7e02015-01-06 10:16:15 -0800537}
538
539// Ask server to echo back the deadline it sees. The rpc has no deadline.
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700540TEST_P(End2endTest, EchoDeadlineForNoDeadlineRpc) {
541 ResetStub(GetParam());
yangged5e7e02015-01-06 10:16:15 -0800542 EchoRequest request;
543 EchoResponse response;
544 request.set_message("Hello");
545 request.mutable_param()->set_echo_deadline(true);
546
547 ClientContext context;
yangg1456d152015-01-08 15:39:58 -0800548 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800549 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700550 EXPECT_TRUE(s.ok());
Craig Tiller354398f2015-07-13 09:16:03 -0700551 EXPECT_EQ(response.param().request_deadline(),
552 gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec);
yangged5e7e02015-01-06 10:16:15 -0800553}
554
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700555TEST_P(End2endTest, UnimplementedRpc) {
556 ResetStub(GetParam());
nnoble0c475f02014-12-05 15:37:39 -0800557 EchoRequest request;
558 EchoResponse response;
559 request.set_message("Hello");
560
561 ClientContext context;
yangg1456d152015-01-08 15:39:58 -0800562 Status s = stub_->Unimplemented(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700563 EXPECT_FALSE(s.ok());
564 EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED);
565 EXPECT_EQ(s.error_message(), "");
nnoble0c475f02014-12-05 15:37:39 -0800566 EXPECT_EQ(response.message(), "");
nnoble0c475f02014-12-05 15:37:39 -0800567}
568
569TEST_F(End2endTest, RequestStreamOneRequest) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700570 ResetStub(false);
nnoble0c475f02014-12-05 15:37:39 -0800571 EchoRequest request;
572 EchoResponse response;
573 ClientContext context;
574
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800575 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800576 request.set_message("hello");
577 EXPECT_TRUE(stream->Write(request));
578 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800579 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800580 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700581 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800582}
583
584TEST_F(End2endTest, RequestStreamTwoRequests) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700585 ResetStub(false);
nnoble0c475f02014-12-05 15:37:39 -0800586 EchoRequest request;
587 EchoResponse response;
588 ClientContext context;
589
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800590 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800591 request.set_message("hello");
592 EXPECT_TRUE(stream->Write(request));
593 EXPECT_TRUE(stream->Write(request));
594 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800595 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800596 EXPECT_EQ(response.message(), "hellohello");
Yang Gaoc1a2c312015-06-16 10:59:46 -0700597 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800598}
599
600TEST_F(End2endTest, ResponseStream) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700601 ResetStub(false);
nnoble0c475f02014-12-05 15:37:39 -0800602 EchoRequest request;
603 EchoResponse response;
604 ClientContext context;
605 request.set_message("hello");
606
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800607 auto stream = stub_->ResponseStream(&context, request);
nnoble0c475f02014-12-05 15:37:39 -0800608 EXPECT_TRUE(stream->Read(&response));
609 EXPECT_EQ(response.message(), request.message() + "0");
610 EXPECT_TRUE(stream->Read(&response));
611 EXPECT_EQ(response.message(), request.message() + "1");
612 EXPECT_TRUE(stream->Read(&response));
613 EXPECT_EQ(response.message(), request.message() + "2");
614 EXPECT_FALSE(stream->Read(&response));
615
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800616 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700617 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800618}
619
620TEST_F(End2endTest, BidiStream) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700621 ResetStub(false);
nnoble0c475f02014-12-05 15:37:39 -0800622 EchoRequest request;
623 EchoResponse response;
624 ClientContext context;
625 grpc::string msg("hello");
626
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800627 auto stream = stub_->BidiStream(&context);
nnoble0c475f02014-12-05 15:37:39 -0800628
629 request.set_message(msg + "0");
630 EXPECT_TRUE(stream->Write(request));
631 EXPECT_TRUE(stream->Read(&response));
632 EXPECT_EQ(response.message(), request.message());
633
634 request.set_message(msg + "1");
635 EXPECT_TRUE(stream->Write(request));
636 EXPECT_TRUE(stream->Read(&response));
637 EXPECT_EQ(response.message(), request.message());
638
639 request.set_message(msg + "2");
640 EXPECT_TRUE(stream->Write(request));
641 EXPECT_TRUE(stream->Read(&response));
642 EXPECT_EQ(response.message(), request.message());
643
644 stream->WritesDone();
645 EXPECT_FALSE(stream->Read(&response));
646
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800647 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700648 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800649}
650
651// Talk to the two services with the same name but different package names.
652// The two stubs are created on the same channel.
653TEST_F(End2endTest, DiffPackageServices) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700654 ResetStub(false);
yangg1456d152015-01-08 15:39:58 -0800655 EchoRequest request;
656 EchoResponse response;
657 request.set_message("Hello");
658
yangg1456d152015-01-08 15:39:58 -0800659 ClientContext context;
yang-g8b25f2a2015-07-21 23:54:36 -0700660 Status s = stub_->Echo(&context, request, &response);
yangg1456d152015-01-08 15:39:58 -0800661 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700662 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800663
664 std::unique_ptr<grpc::cpp::test::util::duplicate::TestService::Stub>
665 dup_pkg_stub(
yang-g8b25f2a2015-07-21 23:54:36 -0700666 grpc::cpp::test::util::duplicate::TestService::NewStub(channel_));
yangg1456d152015-01-08 15:39:58 -0800667 ClientContext context2;
668 s = dup_pkg_stub->Echo(&context2, request, &response);
669 EXPECT_EQ("no package", response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700670 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800671}
672
yangg4105e2b2015-01-09 14:19:44 -0800673// rpc and stream should fail on bad credentials.
674TEST_F(End2endTest, BadCredentials) {
Julien Boeuf510a9202015-08-25 21:51:07 -0700675 std::shared_ptr<Credentials> bad_creds = GoogleRefreshTokenCredentials("");
Craig Tillerb256faa2015-07-23 11:28:16 -0700676 EXPECT_EQ(static_cast<Credentials*>(nullptr), bad_creds.get());
yang-g8c2be9f2015-08-19 16:28:09 -0700677 std::shared_ptr<Channel> channel =
yang-g730055d2015-08-27 12:29:45 -0700678 CreateChannel(server_address_.str(), bad_creds);
yangg4105e2b2015-01-09 14:19:44 -0800679 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub(
680 grpc::cpp::test::util::TestService::NewStub(channel));
681 EchoRequest request;
682 EchoResponse response;
683 ClientContext context;
Yang Gao26a49122015-05-15 17:02:56 -0700684 request.set_message("Hello");
yangg4105e2b2015-01-09 14:19:44 -0800685
686 Status s = stub->Echo(&context, request, &response);
687 EXPECT_EQ("", response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700688 EXPECT_FALSE(s.ok());
yang-gc31cd862015-08-17 15:37:27 -0700689 EXPECT_EQ(StatusCode::INVALID_ARGUMENT, s.error_code());
690 EXPECT_EQ("Invalid credentials.", s.error_message());
yangg4105e2b2015-01-09 14:19:44 -0800691
692 ClientContext context2;
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800693 auto stream = stub->BidiStream(&context2);
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800694 s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700695 EXPECT_FALSE(s.ok());
yang-gc31cd862015-08-17 15:37:27 -0700696 EXPECT_EQ(StatusCode::INVALID_ARGUMENT, s.error_code());
697 EXPECT_EQ("Invalid credentials.", s.error_message());
yangg4105e2b2015-01-09 14:19:44 -0800698}
699
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700700void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) {
Craig Tiller20b5fe92015-07-06 10:43:50 -0700701 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700702 gpr_time_from_micros(delay_us, GPR_TIMESPAN)));
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700703 while (!service->signal_client()) {
704 }
705 context->TryCancel();
706}
707
708// Client cancels rpc after 10ms
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700709TEST_P(End2endTest, ClientCancelsRpc) {
710 ResetStub(GetParam());
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700711 EchoRequest request;
712 EchoResponse response;
713 request.set_message("Hello");
714 const int kCancelDelayUs = 10 * 1000;
715 request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs);
716
717 ClientContext context;
718 std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_);
719 Status s = stub_->Echo(&context, request, &response);
720 cancel_thread.join();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700721 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
722 EXPECT_EQ(s.error_message(), "Cancelled");
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700723}
724
725// Server cancels rpc after 1ms
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700726TEST_P(End2endTest, ServerCancelsRpc) {
727 ResetStub(GetParam());
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700728 EchoRequest request;
729 EchoResponse response;
730 request.set_message("Hello");
731 request.mutable_param()->set_server_cancel_after_us(1000);
732
733 ClientContext context;
734 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700735 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
736 EXPECT_TRUE(s.error_message().empty());
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700737}
738
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700739// Client cancels request stream after sending two messages
740TEST_F(End2endTest, ClientCancelsRequestStream) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700741 ResetStub(false);
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700742 EchoRequest request;
743 EchoResponse response;
744 ClientContext context;
745 request.set_message("hello");
746
747 auto stream = stub_->RequestStream(&context, &response);
748 EXPECT_TRUE(stream->Write(request));
749 EXPECT_TRUE(stream->Write(request));
Yang Gaoc71a9d22015-05-04 00:22:12 -0700750
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700751 context.TryCancel();
752
753 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700754 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700755
Yang Gaoc71a9d22015-05-04 00:22:12 -0700756 EXPECT_EQ(response.message(), "");
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700757}
758
Abhishek Kumare41d0402015-04-17 14:12:33 -0700759// Client cancels server stream after sending some messages
Abhishek Kumara1d3a722015-04-23 10:24:04 -0700760TEST_F(End2endTest, ClientCancelsResponseStream) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700761 ResetStub(false);
Abhishek Kumare41d0402015-04-17 14:12:33 -0700762 EchoRequest request;
763 EchoResponse response;
764 ClientContext context;
765 request.set_message("hello");
766
767 auto stream = stub_->ResponseStream(&context, request);
768
769 EXPECT_TRUE(stream->Read(&response));
770 EXPECT_EQ(response.message(), request.message() + "0");
771 EXPECT_TRUE(stream->Read(&response));
772 EXPECT_EQ(response.message(), request.message() + "1");
773
774 context.TryCancel();
775
776 // The cancellation races with responses, so there might be zero or
777 // one responses pending, read till failure
778
779 if (stream->Read(&response)) {
780 EXPECT_EQ(response.message(), request.message() + "2");
781 // Since we have cancelled, we expect the next attempt to read to fail
782 EXPECT_FALSE(stream->Read(&response));
783 }
784
785 Status s = stream->Finish();
Abhishek Kumar18298a72015-04-17 15:00:25 -0700786 // The final status could be either of CANCELLED or OK depending on
787 // who won the race.
Yang Gaoc1a2c312015-06-16 10:59:46 -0700788 EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumare41d0402015-04-17 14:12:33 -0700789}
790
Abhishek Kumar82a83312015-04-17 13:30:51 -0700791// Client cancels bidi stream after sending some messages
792TEST_F(End2endTest, ClientCancelsBidi) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700793 ResetStub(false);
Abhishek Kumar82a83312015-04-17 13:30:51 -0700794 EchoRequest request;
795 EchoResponse response;
796 ClientContext context;
797 grpc::string msg("hello");
798
799 auto stream = stub_->BidiStream(&context);
800
801 request.set_message(msg + "0");
802 EXPECT_TRUE(stream->Write(request));
803 EXPECT_TRUE(stream->Read(&response));
804 EXPECT_EQ(response.message(), request.message());
805
806 request.set_message(msg + "1");
807 EXPECT_TRUE(stream->Write(request));
808
809 context.TryCancel();
810
811 // The cancellation races with responses, so there might be zero or
812 // one responses pending, read till failure
813
814 if (stream->Read(&response)) {
815 EXPECT_EQ(response.message(), request.message());
816 // Since we have cancelled, we expect the next attempt to read to fail
817 EXPECT_FALSE(stream->Read(&response));
818 }
819
820 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700821 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumar82a83312015-04-17 13:30:51 -0700822}
823
Yang Gao3921c562015-04-30 16:07:06 -0700824TEST_F(End2endTest, RpcMaxMessageSize) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700825 ResetStub(false);
Yang Gao3921c562015-04-30 16:07:06 -0700826 EchoRequest request;
827 EchoResponse response;
Yang Gaoc71a9d22015-05-04 00:22:12 -0700828 request.set_message(string(kMaxMessageSize_ * 2, 'a'));
Yang Gao3921c562015-04-30 16:07:06 -0700829
830 ClientContext context;
831 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700832 EXPECT_FALSE(s.ok());
Yang Gao3921c562015-04-30 16:07:06 -0700833}
Abhishek Kumar82a83312015-04-17 13:30:51 -0700834
yang-ge21908f2015-08-25 13:47:51 -0700835bool MetadataContains(
836 const std::multimap<grpc::string_ref, grpc::string_ref>& metadata,
837 const grpc::string& key, const grpc::string& value) {
Yang Gao26a49122015-05-15 17:02:56 -0700838 int count = 0;
839
yang-ge21908f2015-08-25 13:47:51 -0700840 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator iter =
Yang Gao26a49122015-05-15 17:02:56 -0700841 metadata.begin();
842 iter != metadata.end(); ++iter) {
yang-ge21908f2015-08-25 13:47:51 -0700843 if (ToString(iter->first) == key && ToString(iter->second) == value) {
Yang Gao26a49122015-05-15 17:02:56 -0700844 count++;
845 }
846 }
847 return count == 1;
848}
849
Yang Gaoa8938922015-05-14 11:51:07 -0700850TEST_F(End2endTest, SetPerCallCredentials) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700851 ResetStub(false);
Yang Gaoa8938922015-05-14 11:51:07 -0700852 EchoRequest request;
853 EchoResponse response;
854 ClientContext context;
855 std::shared_ptr<Credentials> creds =
Julien Boeuf510a9202015-08-25 21:51:07 -0700856 GoogleIAMCredentials("fake_token", "fake_selector");
Yang Gaoa8938922015-05-14 11:51:07 -0700857 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -0700858 request.set_message("Hello");
859 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -0700860
861 Status s = stub_->Echo(&context, request, &response);
Yang Gaoa8938922015-05-14 11:51:07 -0700862 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700863 EXPECT_TRUE(s.ok());
Yang Gao26a49122015-05-15 17:02:56 -0700864 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
865 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
866 "fake_token"));
867 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
868 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
869 "fake_selector"));
Yang Gaoa8938922015-05-14 11:51:07 -0700870}
871
872TEST_F(End2endTest, InsecurePerCallCredentials) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700873 ResetStub(false);
Yang Gaoa8938922015-05-14 11:51:07 -0700874 EchoRequest request;
875 EchoResponse response;
876 ClientContext context;
877 std::shared_ptr<Credentials> creds = InsecureCredentials();
878 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -0700879 request.set_message("Hello");
880 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -0700881
882 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700883 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
884 EXPECT_EQ("Failed to set credentials to rpc.", s.error_message());
Yang Gaoa8938922015-05-14 11:51:07 -0700885}
886
887TEST_F(End2endTest, OverridePerCallCredentials) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700888 ResetStub(false);
Yang Gaoa8938922015-05-14 11:51:07 -0700889 EchoRequest request;
890 EchoResponse response;
891 ClientContext context;
Yang Gao26a49122015-05-15 17:02:56 -0700892 std::shared_ptr<Credentials> creds1 =
Julien Boeuf510a9202015-08-25 21:51:07 -0700893 GoogleIAMCredentials("fake_token1", "fake_selector1");
Yang Gaoa8938922015-05-14 11:51:07 -0700894 context.set_credentials(creds1);
895 std::shared_ptr<Credentials> creds2 =
Julien Boeuf510a9202015-08-25 21:51:07 -0700896 GoogleIAMCredentials("fake_token2", "fake_selector2");
Yang Gaoa8938922015-05-14 11:51:07 -0700897 context.set_credentials(creds2);
Yang Gao26a49122015-05-15 17:02:56 -0700898 request.set_message("Hello");
899 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -0700900
901 Status s = stub_->Echo(&context, request, &response);
Yang Gao26a49122015-05-15 17:02:56 -0700902 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
903 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
904 "fake_token2"));
905 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
906 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
907 "fake_selector2"));
Yang Gaob57f72d2015-05-17 21:54:54 -0700908 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
909 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
910 "fake_token1"));
911 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
912 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
913 "fake_selector1"));
Yang Gaoa8938922015-05-14 11:51:07 -0700914 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700915 EXPECT_TRUE(s.ok());
Yang Gaoa8938922015-05-14 11:51:07 -0700916}
917
Julien Boeuf1928d492015-09-15 15:20:11 -0700918TEST_F(End2endTest, NonBlockingAuthMetadataPluginFailure) {
919 ResetStub(false);
920 EchoRequest request;
921 EchoResponse response;
922 ClientContext context;
923 context.set_credentials(
924 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
925 new TestMetadataCredentialsPlugin(
926 "Does not matter, will fail anyway (see 3rd param)", false,
927 false))));
928 request.set_message("Hello");
929
930 Status s = stub_->Echo(&context, request, &response);
931 EXPECT_FALSE(s.ok());
932 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
933}
934
935TEST_F(End2endTest, NonBlockingAuthMetadataPluginAndProcessorSuccess) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700936 auto* processor = new TestAuthMetadataProcessor(false);
937 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
938 ResetStub(false);
939 EchoRequest request;
940 EchoResponse response;
941 ClientContext context;
942 context.set_credentials(processor->GetCompatibleClientCreds());
943 request.set_message("Hello");
944 request.mutable_param()->set_echo_metadata(true);
945 request.mutable_param()->set_expected_client_identity(
946 TestAuthMetadataProcessor::kGoodGuy);
947
948 Status s = stub_->Echo(&context, request, &response);
949 EXPECT_EQ(request.message(), response.message());
950 EXPECT_TRUE(s.ok());
951
952 // Metadata should have been consumed by the processor.
953 EXPECT_FALSE(MetadataContains(
954 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
955 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
956}
957
Julien Boeuf1928d492015-09-15 15:20:11 -0700958TEST_F(End2endTest, NonBlockingAuthMetadataPluginAndProcessorFailure) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700959 auto* processor = new TestAuthMetadataProcessor(false);
960 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
961 ResetStub(false);
962 EchoRequest request;
963 EchoResponse response;
964 ClientContext context;
965 context.set_credentials(processor->GetIncompatibleClientCreds());
966 request.set_message("Hello");
967
968 Status s = stub_->Echo(&context, request, &response);
969 EXPECT_FALSE(s.ok());
970 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
971}
972
Julien Boeuf1928d492015-09-15 15:20:11 -0700973TEST_F(End2endTest, BlockingAuthMetadataPluginFailure) {
974 ResetStub(false);
975 EchoRequest request;
976 EchoResponse response;
977 ClientContext context;
978 context.set_credentials(
979 MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>(
980 new TestMetadataCredentialsPlugin(
981 "Does not matter, will fail anyway (see 3rd param)", true,
982 false))));
983 request.set_message("Hello");
984
985 Status s = stub_->Echo(&context, request, &response);
986 EXPECT_FALSE(s.ok());
987 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
988}
989
990TEST_F(End2endTest, BlockingAuthMetadataPluginAndProcessorSuccess) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700991 auto* processor = new TestAuthMetadataProcessor(true);
992 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
993 ResetStub(false);
994 EchoRequest request;
995 EchoResponse response;
996 ClientContext context;
997 context.set_credentials(processor->GetCompatibleClientCreds());
998 request.set_message("Hello");
999 request.mutable_param()->set_echo_metadata(true);
1000 request.mutable_param()->set_expected_client_identity(
1001 TestAuthMetadataProcessor::kGoodGuy);
1002
1003 Status s = stub_->Echo(&context, request, &response);
1004 EXPECT_EQ(request.message(), response.message());
1005 EXPECT_TRUE(s.ok());
1006
1007 // Metadata should have been consumed by the processor.
1008 EXPECT_FALSE(MetadataContains(
1009 context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY,
1010 grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy));
1011}
1012
Julien Boeuf1928d492015-09-15 15:20:11 -07001013TEST_F(End2endTest, BlockingAuthMetadataPluginAndProcessorFailure) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -07001014 auto* processor = new TestAuthMetadataProcessor(true);
1015 StartServer(std::shared_ptr<AuthMetadataProcessor>(processor));
1016 ResetStub(false);
1017 EchoRequest request;
1018 EchoResponse response;
1019 ClientContext context;
1020 context.set_credentials(processor->GetIncompatibleClientCreds());
1021 request.set_message("Hello");
1022
1023 Status s = stub_->Echo(&context, request, &response);
1024 EXPECT_FALSE(s.ok());
1025 EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED);
1026}
1027
yang-g0b6ad7d2015-06-25 14:39:01 -07001028// Client sends 20 requests and the server returns CANCELLED status after
1029// reading 10 requests.
1030TEST_F(End2endTest, RequestStreamServerEarlyCancelTest) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -07001031 ResetStub(false);
yang-g0b6ad7d2015-06-25 14:39:01 -07001032 EchoRequest request;
1033 EchoResponse response;
1034 ClientContext context;
1035
1036 context.AddMetadata(kServerCancelAfterReads, "10");
1037 auto stream = stub_->RequestStream(&context, &response);
1038 request.set_message("hello");
1039 int send_messages = 20;
1040 while (send_messages > 0) {
1041 EXPECT_TRUE(stream->Write(request));
1042 send_messages--;
1043 }
1044 stream->WritesDone();
1045 Status s = stream->Finish();
1046 EXPECT_EQ(s.error_code(), StatusCode::CANCELLED);
1047}
1048
yang-gc4eef2e2015-07-06 23:26:58 -07001049TEST_F(End2endTest, ClientAuthContext) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -07001050 ResetStub(false);
yang-gc4eef2e2015-07-06 23:26:58 -07001051 EchoRequest request;
1052 EchoResponse response;
1053 request.set_message("Hello");
1054 request.mutable_param()->set_check_auth_context(true);
1055
1056 ClientContext context;
1057 Status s = stub_->Echo(&context, request, &response);
1058 EXPECT_EQ(response.message(), request.message());
1059 EXPECT_TRUE(s.ok());
1060
yang-g8b25f2a2015-07-21 23:54:36 -07001061 std::shared_ptr<const AuthContext> auth_ctx = context.auth_context();
yang-gd090fe12015-08-25 16:53:07 -07001062 std::vector<grpc::string_ref> ssl =
yang-g8b25f2a2015-07-21 23:54:36 -07001063 auth_ctx->FindPropertyValues("transport_security_type");
1064 EXPECT_EQ(1u, ssl.size());
yang-gd090fe12015-08-25 16:53:07 -07001065 EXPECT_EQ("ssl", ToString(ssl[0]));
yang-g8b25f2a2015-07-21 23:54:36 -07001066 EXPECT_EQ("x509_subject_alternative_name",
1067 auth_ctx->GetPeerIdentityPropertyName());
1068 EXPECT_EQ(3u, auth_ctx->GetPeerIdentity().size());
yang-gd090fe12015-08-25 16:53:07 -07001069 EXPECT_EQ("*.test.google.fr", ToString(auth_ctx->GetPeerIdentity()[0]));
1070 EXPECT_EQ("waterzooi.test.google.be",
1071 ToString(auth_ctx->GetPeerIdentity()[1]));
1072 EXPECT_EQ("*.test.youtube.com", ToString(auth_ctx->GetPeerIdentity()[2]));
yang-gc4eef2e2015-07-06 23:26:58 -07001073}
1074
yang-g6f30dec2015-07-22 23:11:56 -07001075// Make the response larger than the flow control window.
Craig Tiller2c3be1d2015-08-05 15:59:27 -07001076TEST_P(End2endTest, HugeResponse) {
1077 ResetStub(GetParam());
yang-g6f30dec2015-07-22 23:11:56 -07001078 EchoRequest request;
1079 EchoResponse response;
1080 request.set_message("huge response");
vjpai51d22752015-07-24 14:11:04 -07001081 const size_t kResponseSize = 1024 * (1024 + 10);
yang-g6f30dec2015-07-22 23:11:56 -07001082 request.mutable_param()->set_response_message_length(kResponseSize);
1083
1084 ClientContext context;
1085 Status s = stub_->Echo(&context, request, &response);
1086 EXPECT_EQ(kResponseSize, response.message().size());
1087 EXPECT_TRUE(s.ok());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001088}
1089
yang-g8ab38362015-07-31 14:05:33 -07001090namespace {
yang-g36f59652015-08-05 15:15:18 -07001091void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream,
Craig Tillerd6c98df2015-08-18 09:33:44 -07001092 gpr_event* ev) {
yang-g8ab38362015-07-31 14:05:33 -07001093 EchoResponse resp;
Craig Tiller7c1be052015-07-31 15:38:37 -07001094 gpr_event_set(ev, (void*)1);
yang-g8ab38362015-07-31 14:05:33 -07001095 while (stream->Read(&resp)) {
1096 gpr_log(GPR_INFO, "Read message");
1097 }
1098}
1099} // namespace
1100
1101// Run a Read and a WritesDone simultaneously.
Craig Tillerfb21ae62015-08-03 10:16:11 -07001102TEST_F(End2endTest, SimultaneousReadWritesDone) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -07001103 ResetStub(false);
yang-g8ab38362015-07-31 14:05:33 -07001104 ClientContext context;
Craig Tiller7c1be052015-07-31 15:38:37 -07001105 gpr_event ev;
1106 gpr_event_init(&ev);
yang-g8ab38362015-07-31 14:05:33 -07001107 auto stream = stub_->BidiStream(&context);
Craig Tiller7c1be052015-07-31 15:38:37 -07001108 std::thread reader_thread(ReaderThreadFunc, stream.get(), &ev);
1109 gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME));
yang-g8ab38362015-07-31 14:05:33 -07001110 stream->WritesDone();
1111 Status s = stream->Finish();
1112 EXPECT_TRUE(s.ok());
1113 reader_thread.join();
1114}
1115
Craig Tiller2c3be1d2015-08-05 15:59:27 -07001116TEST_P(End2endTest, Peer) {
1117 ResetStub(GetParam());
yang-gd7ead692015-07-30 10:57:45 -07001118 EchoRequest request;
1119 EchoResponse response;
1120 request.set_message("hello");
1121 request.mutable_param()->set_echo_peer(true);
1122
1123 ClientContext context;
1124 Status s = stub_->Echo(&context, request, &response);
1125 EXPECT_EQ(response.message(), request.message());
1126 EXPECT_TRUE(s.ok());
1127 EXPECT_TRUE(CheckIsLocalhost(response.param().peer()));
1128 EXPECT_TRUE(CheckIsLocalhost(context.peer()));
1129}
1130
yang-g36f59652015-08-05 15:15:18 -07001131TEST_F(End2endTest, ChannelState) {
yang-gcec757f2015-08-06 22:53:58 -07001132 ResetStub(false);
yang-g36f59652015-08-05 15:15:18 -07001133 // Start IDLE
1134 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(false));
1135
yang-g8708dd72015-08-05 15:57:14 -07001136 // Did not ask to connect, no state change.
yang-g36f59652015-08-05 15:15:18 -07001137 CompletionQueue cq;
1138 std::chrono::system_clock::time_point deadline =
1139 std::chrono::system_clock::now() + std::chrono::milliseconds(10);
yang-g36f59652015-08-05 15:15:18 -07001140 channel_->NotifyOnStateChange(GRPC_CHANNEL_IDLE, deadline, &cq, NULL);
1141 void* tag;
1142 bool ok = true;
1143 cq.Next(&tag, &ok);
1144 EXPECT_FALSE(ok);
1145
1146 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true));
Craig Tillerd6c98df2015-08-18 09:33:44 -07001147 EXPECT_TRUE(channel_->WaitForStateChange(GRPC_CHANNEL_IDLE,
1148 gpr_inf_future(GPR_CLOCK_REALTIME)));
yang-g36f59652015-08-05 15:15:18 -07001149 EXPECT_EQ(GRPC_CHANNEL_CONNECTING, channel_->GetState(false));
1150}
1151
yang-gd886f332015-09-18 01:24:14 -07001152// Takes 10s.
1153TEST_F(End2endTest, ChannelStateTimeout) {
1154 int port = grpc_pick_unused_port_or_die();
1155 std::ostringstream server_address;
1156 server_address << "127.0.0.1:" << port;
1157 // Channel to non-existing server
1158 auto channel = CreateChannel(server_address.str(), InsecureCredentials());
1159 // Start IDLE
1160 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel->GetState(true));
1161
1162 auto state = GRPC_CHANNEL_IDLE;
1163 for (int i = 0; i < 10; i++) {
1164 channel->WaitForStateChange(state, std::chrono::system_clock::now() +
yang-g5e8abed2015-09-18 01:31:57 -07001165 std::chrono::seconds(1));
yang-gd886f332015-09-18 01:24:14 -07001166 state = channel->GetState(false);
1167 }
1168}
1169
yang-g9b7757d2015-08-13 11:15:53 -07001170// Talking to a non-existing service.
1171TEST_F(End2endTest, NonExistingService) {
1172 ResetChannel();
1173 std::unique_ptr<grpc::cpp::test::util::UnimplementedService::Stub> stub;
Nicolas "Pixel" Noble7fa51672015-09-02 02:29:09 +02001174 stub = grpc::cpp::test::util::UnimplementedService::NewStub(channel_);
yang-g9b7757d2015-08-13 11:15:53 -07001175
1176 EchoRequest request;
1177 EchoResponse response;
1178 request.set_message("Hello");
1179
1180 ClientContext context;
1181 Status s = stub->Unimplemented(&context, request, &response);
1182 EXPECT_EQ(StatusCode::UNIMPLEMENTED, s.error_code());
1183 EXPECT_EQ("", s.error_message());
1184}
1185
Craig Tiller2c3be1d2015-08-05 15:59:27 -07001186INSTANTIATE_TEST_CASE_P(End2end, End2endTest, ::testing::Values(false, true));
1187
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001188} // namespace testing
1189} // namespace grpc
1190
1191int main(int argc, char** argv) {
1192 grpc_test_init(argc, argv);
1193 ::testing::InitGoogleTest(&argc, argv);
1194 return RUN_ALL_TESTS();
1195}