blob: 37669815c630abc6b6973352422cf6d946dc6755 [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 Gao26a49122015-05-15 17:02:56 -070037#include "src/core/security/credentials.h"
yang-g8b25f2a2015-07-21 23:54:36 -070038#include "test/core/end2end/data/ssl_test_data.h"
Nicolas Noble89219162015-04-07 18:01:18 -070039#include "test/core/util/port.h"
Craig Tiller14e60e92015-01-13 17:26:46 -080040#include "test/core/util/test_config.h"
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020041#include "test/cpp/util/echo_duplicate.grpc.pb.h"
42#include "test/cpp/util/echo.grpc.pb.h"
yangg59dfc902014-12-19 14:00:14 -080043#include <grpc++/channel_arguments.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044#include <grpc++/channel_interface.h>
45#include <grpc++/client_context.h>
46#include <grpc++/create_channel.h>
yangg4105e2b2015-01-09 14:19:44 -080047#include <grpc++/credentials.h>
Vijay Pai1f3e6c12015-07-23 16:18:21 -070048#include <grpc++/dynamic_thread_pool.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049#include <grpc++/server.h>
50#include <grpc++/server_builder.h>
yangga4b6f5d2014-12-17 15:53:12 -080051#include <grpc++/server_context.h>
Craig Tiller42bc87c2015-02-23 08:50:19 -080052#include <grpc++/server_credentials.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053#include <grpc++/status.h>
nnoble0c475f02014-12-05 15:37:39 -080054#include <grpc++/stream.h>
Nicolas Noble89219162015-04-07 18:01:18 -070055#include <grpc++/time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056#include <gtest/gtest.h>
57
58#include <grpc/grpc.h>
59#include <grpc/support/thd.h>
yangged5e7e02015-01-06 10:16:15 -080060#include <grpc/support/time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061
62using grpc::cpp::test::util::EchoRequest;
63using grpc::cpp::test::util::EchoResponse;
yangged5e7e02015-01-06 10:16:15 -080064using std::chrono::system_clock;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065
66namespace grpc {
yangged5e7e02015-01-06 10:16:15 -080067namespace testing {
68
69namespace {
70
yang-g0b6ad7d2015-06-25 14:39:01 -070071const char* kServerCancelAfterReads = "cancel_after_reads";
72
yangged5e7e02015-01-06 10:16:15 -080073// When echo_deadline is requested, deadline seen in the ServerContext is set in
74// the response in seconds.
75void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request,
76 EchoResponse* response) {
77 if (request->has_param() && request->param().echo_deadline()) {
Craig Tiller354398f2015-07-13 09:16:03 -070078 gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
Nicolas Noble89219162015-04-07 18:01:18 -070079 if (context->deadline() != system_clock::time_point::max()) {
80 Timepoint2Timespec(context->deadline(), &deadline);
yangged5e7e02015-01-06 10:16:15 -080081 }
82 response->mutable_param()->set_request_deadline(deadline.tv_sec);
83 }
84}
Craig Tiller7418d012015-02-11 15:25:03 -080085
yang-g8b25f2a2015-07-21 23:54:36 -070086void CheckServerAuthContext(const ServerContext* context) {
yang-g85c04f92015-07-07 17:47:31 -070087 std::shared_ptr<const AuthContext> auth_ctx = context->auth_context();
yang-g8b25f2a2015-07-21 23:54:36 -070088 std::vector<grpc::string> ssl =
yang-gc4eef2e2015-07-06 23:26:58 -070089 auth_ctx->FindPropertyValues("transport_security_type");
yang-g8b25f2a2015-07-21 23:54:36 -070090 EXPECT_EQ(1u, ssl.size());
91 EXPECT_EQ("ssl", ssl[0]);
yang-gc4eef2e2015-07-06 23:26:58 -070092 EXPECT_TRUE(auth_ctx->GetPeerIdentityPropertyName().empty());
93 EXPECT_TRUE(auth_ctx->GetPeerIdentity().empty());
94}
95
yang-gd7ead692015-07-30 10:57:45 -070096bool CheckIsLocalhost(const grpc::string& addr) {
97 const grpc::string kIpv6("ipv6:[::1]:");
98 const grpc::string kIpv4MappedIpv6("ipv6:[::ffff:127.0.0.1]:");
99 const grpc::string kIpv4("ipv4:127.0.0.1:");
100 return addr.substr(0, kIpv4.size()) == kIpv4 ||
101 addr.substr(0, kIpv4MappedIpv6.size()) == kIpv4MappedIpv6 ||
102 addr.substr(0, kIpv6.size()) == kIpv6;
103}
104
yangged5e7e02015-01-06 10:16:15 -0800105} // namespace
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700107class Proxy : public ::grpc::cpp::test::util::TestService::Service {
108 public:
109 Proxy(std::shared_ptr<ChannelInterface> channel)
110 : stub_(grpc::cpp::test::util::TestService::NewStub(channel)) {}
111
112 Status Echo(ServerContext* server_context, const EchoRequest* request,
113 EchoResponse* response) GRPC_OVERRIDE {
114 std::unique_ptr<ClientContext> client_context =
115 ClientContext::FromServerContext(*server_context);
116 return stub_->Echo(client_context.get(), *request, response);
117 }
118
119 private:
David G. Quintas086a9822015-08-11 18:56:10 -0700120 std::unique_ptr< ::grpc::cpp::test::util::TestService::Stub> stub_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700121};
122
yangg1456d152015-01-08 15:39:58 -0800123class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800124 public:
vjpaidf551612015-07-14 13:38:44 -0700125 TestServiceImpl() : signal_client_(false), host_() {}
Vijay Pai181ef452015-07-14 13:52:48 -0700126 explicit TestServiceImpl(const grpc::string& host)
127 : signal_client_(false), host_(new grpc::string(host)) {}
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700128
yangga4b6f5d2014-12-17 15:53:12 -0800129 Status Echo(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800130 EchoResponse* response) GRPC_OVERRIDE {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131 response->set_message(request->message());
yangged5e7e02015-01-06 10:16:15 -0800132 MaybeEchoDeadline(context, request, response);
Craig Tiller822d2c72015-07-07 16:08:00 -0700133 if (host_) {
134 response->mutable_param()->set_host(*host_);
135 }
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700136 if (request->has_param() && request->param().client_cancel_after_us()) {
137 {
138 std::unique_lock<std::mutex> lock(mu_);
139 signal_client_ = true;
140 }
141 while (!context->IsCancelled()) {
David Garcia Quintasfeb67f62015-05-20 19:23:25 -0700142 gpr_sleep_until(gpr_time_add(
Craig Tiller20b5fe92015-07-06 10:43:50 -0700143 gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700144 gpr_time_from_micros(request->param().client_cancel_after_us(),
145 GPR_TIMESPAN)));
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700146 }
Yang Gaoc1a2c312015-06-16 10:59:46 -0700147 return Status::CANCELLED;
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700148 } else if (request->has_param() &&
149 request->param().server_cancel_after_us()) {
David Garcia Quintasfeb67f62015-05-20 19:23:25 -0700150 gpr_sleep_until(gpr_time_add(
Craig Tiller20b5fe92015-07-06 10:43:50 -0700151 gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700152 gpr_time_from_micros(request->param().server_cancel_after_us(),
153 GPR_TIMESPAN)));
Yang Gaoc1a2c312015-06-16 10:59:46 -0700154 return Status::CANCELLED;
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700155 } else {
156 EXPECT_FALSE(context->IsCancelled());
157 }
Yang Gao26a49122015-05-15 17:02:56 -0700158
159 if (request->has_param() && request->param().echo_metadata()) {
160 const std::multimap<grpc::string, grpc::string>& client_metadata =
161 context->client_metadata();
162 for (std::multimap<grpc::string, grpc::string>::const_iterator iter =
163 client_metadata.begin();
164 iter != client_metadata.end(); ++iter) {
165 context->AddTrailingMetadata((*iter).first, (*iter).second);
166 }
167 }
yang-gc4eef2e2015-07-06 23:26:58 -0700168 if (request->has_param() && request->param().check_auth_context()) {
yang-g8b25f2a2015-07-21 23:54:36 -0700169 CheckServerAuthContext(context);
yang-gc4eef2e2015-07-06 23:26:58 -0700170 }
yang-g6f30dec2015-07-22 23:11:56 -0700171 if (request->has_param() &&
172 request->param().response_message_length() > 0) {
173 response->set_message(
174 grpc::string(request->param().response_message_length(), '\0'));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800175 }
yang-gd7ead692015-07-30 10:57:45 -0700176 if (request->has_param() && request->param().echo_peer()) {
177 response->mutable_param()->set_peer(context->peer());
178 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800179 return Status::OK;
180 }
nnoble0c475f02014-12-05 15:37:39 -0800181
182 // Unimplemented is left unimplemented to test the returned error.
183
yangga4b6f5d2014-12-17 15:53:12 -0800184 Status RequestStream(ServerContext* context,
185 ServerReader<EchoRequest>* reader,
Craig Tillercf133f42015-02-26 14:05:56 -0800186 EchoResponse* response) GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800187 EchoRequest request;
188 response->set_message("");
yang-g0b6ad7d2015-06-25 14:39:01 -0700189 int cancel_after_reads = 0;
190 const std::multimap<grpc::string, grpc::string> client_initial_metadata =
191 context->client_metadata();
192 if (client_initial_metadata.find(kServerCancelAfterReads) !=
193 client_initial_metadata.end()) {
194 std::istringstream iss(
195 client_initial_metadata.find(kServerCancelAfterReads)->second);
196 iss >> cancel_after_reads;
197 gpr_log(GPR_INFO, "cancel_after_reads %d", cancel_after_reads);
198 }
nnoble0c475f02014-12-05 15:37:39 -0800199 while (reader->Read(&request)) {
yang-g0b6ad7d2015-06-25 14:39:01 -0700200 if (cancel_after_reads == 1) {
201 gpr_log(GPR_INFO, "return cancel status");
202 return Status::CANCELLED;
203 } else if (cancel_after_reads > 0) {
204 cancel_after_reads--;
205 }
nnoble0c475f02014-12-05 15:37:39 -0800206 response->mutable_message()->append(request.message());
207 }
208 return Status::OK;
209 }
210
211 // Return 3 messages.
212 // TODO(yangg) make it generic by adding a parameter into EchoRequest
yangga4b6f5d2014-12-17 15:53:12 -0800213 Status ResponseStream(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800214 ServerWriter<EchoResponse>* writer) GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800215 EchoResponse response;
216 response.set_message(request->message() + "0");
217 writer->Write(response);
218 response.set_message(request->message() + "1");
219 writer->Write(response);
220 response.set_message(request->message() + "2");
221 writer->Write(response);
222
223 return Status::OK;
224 }
225
Craig Tillercf133f42015-02-26 14:05:56 -0800226 Status BidiStream(ServerContext* context,
227 ServerReaderWriter<EchoResponse, EchoRequest>* stream)
228 GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800229 EchoRequest request;
230 EchoResponse response;
231 while (stream->Read(&request)) {
232 gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
233 response.set_message(request.message());
234 stream->Write(response);
235 }
236 return Status::OK;
237 }
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700238
239 bool signal_client() {
240 std::unique_lock<std::mutex> lock(mu_);
241 return signal_client_;
242 }
243
244 private:
245 bool signal_client_;
246 std::mutex mu_;
Craig Tiller822d2c72015-07-07 16:08:00 -0700247 std::unique_ptr<grpc::string> host_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800248};
249
yangg1456d152015-01-08 15:39:58 -0800250class TestServiceImplDupPkg
251 : public ::grpc::cpp::test::util::duplicate::TestService::Service {
252 public:
253 Status Echo(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800254 EchoResponse* response) GRPC_OVERRIDE {
yangg1456d152015-01-08 15:39:58 -0800255 response->set_message("no package");
256 return Status::OK;
257 }
258};
259
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700260/* Param is whether or not to use a proxy -- some tests use TEST_F as they don't
261 need this functionality */
262class End2endTest : public ::testing::TestWithParam<bool> {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800263 protected:
Vijay Pai181ef452015-07-14 13:52:48 -0700264 End2endTest()
265 : kMaxMessageSize_(8192), special_service_("special"), thread_pool_(2) {}
Craig Tiller7418d012015-02-11 15:25:03 -0800266
Craig Tillercf133f42015-02-26 14:05:56 -0800267 void SetUp() GRPC_OVERRIDE {
Craig Tiller35e39712015-01-12 16:41:24 -0800268 int port = grpc_pick_unused_port_or_die();
yang-gd7ead692015-07-30 10:57:45 -0700269 server_address_ << "127.0.0.1:" << port;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800270 // Setup server
271 ServerBuilder builder;
yang-g8b25f2a2015-07-21 23:54:36 -0700272 SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
273 test_server1_cert};
274 SslServerCredentialsOptions ssl_opts;
275 ssl_opts.pem_root_certs = "";
276 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
Yang Gaob57f72d2015-05-17 21:54:54 -0700277 builder.AddListeningPort(server_address_.str(),
yang-g8b25f2a2015-07-21 23:54:36 -0700278 SslServerCredentials(ssl_opts));
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800279 builder.RegisterService(&service_);
yang-g8b25f2a2015-07-21 23:54:36 -0700280 builder.RegisterService("foo.test.youtube.com", &special_service_);
Yang Gaoc71a9d22015-05-04 00:22:12 -0700281 builder.SetMaxMessageSize(
282 kMaxMessageSize_); // For testing max message size.
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800283 builder.RegisterService(&dup_pkg_service_);
Craig Tiller7418d012015-02-11 15:25:03 -0800284 builder.SetThreadPool(&thread_pool_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800285 server_ = builder.BuildAndStart();
286 }
287
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700288 void TearDown() GRPC_OVERRIDE {
289 server_->Shutdown();
290 if (proxy_server_) proxy_server_->Shutdown();
291 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800292
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700293 void ResetStub(bool use_proxy) {
yang-g8b25f2a2015-07-21 23:54:36 -0700294 SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700295 ChannelArguments args;
yang-g8b25f2a2015-07-21 23:54:36 -0700296 args.SetSslTargetNameOverride("foo.test.google.fr");
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700297 args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test");
yang-g8b25f2a2015-07-21 23:54:36 -0700298 channel_ = CreateChannel(server_address_.str(), SslCredentials(ssl_opts),
299 args);
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700300 if (use_proxy) {
301 proxy_service_.reset(new Proxy(channel_));
302 int port = grpc_pick_unused_port_or_die();
303 std::ostringstream proxyaddr;
304 proxyaddr << "localhost:" << port;
305 ServerBuilder builder;
306 builder.AddListeningPort(proxyaddr.str(), InsecureServerCredentials());
307 builder.RegisterService(proxy_service_.get());
308 builder.SetThreadPool(&thread_pool_);
309 proxy_server_ = builder.BuildAndStart();
310
311 channel_ = CreateChannel(proxyaddr.str(), InsecureCredentials(),
312 ChannelArguments());
313 }
314
yang-g8b25f2a2015-07-21 23:54:36 -0700315 stub_ = std::move(grpc::cpp::test::util::TestService::NewStub(channel_));
yangg1456d152015-01-08 15:39:58 -0800316 }
317
yang-g8b25f2a2015-07-21 23:54:36 -0700318 std::shared_ptr<ChannelInterface> channel_;
yangg1456d152015-01-08 15:39:58 -0800319 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800320 std::unique_ptr<Server> server_;
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700321 std::unique_ptr<Server> proxy_server_;
322 std::unique_ptr<Proxy> proxy_service_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800323 std::ostringstream server_address_;
Yang Gao3921c562015-04-30 16:07:06 -0700324 const int kMaxMessageSize_;
nnoble0c475f02014-12-05 15:37:39 -0800325 TestServiceImpl service_;
Craig Tiller822d2c72015-07-07 16:08:00 -0700326 TestServiceImpl special_service_;
yangg1456d152015-01-08 15:39:58 -0800327 TestServiceImplDupPkg dup_pkg_service_;
Vijay Pai1f3e6c12015-07-23 16:18:21 -0700328 DynamicThreadPool thread_pool_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800329};
330
yangg1456d152015-01-08 15:39:58 -0800331static void SendRpc(grpc::cpp::test::util::TestService::Stub* stub,
332 int num_rpcs) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800333 EchoRequest request;
334 EchoResponse response;
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700335 request.set_message("Hello hello hello hello");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800336
337 for (int i = 0; i < num_rpcs; ++i) {
338 ClientContext context;
Craig Tillerbf6abee2015-07-19 22:31:04 -0700339 context.set_compression_algorithm(GRPC_COMPRESS_GZIP);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800340 Status s = stub->Echo(&context, request, &response);
341 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700342 EXPECT_TRUE(s.ok());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800343 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800344}
345
Craig Tiller822d2c72015-07-07 16:08:00 -0700346TEST_F(End2endTest, SimpleRpcWithHost) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700347 ResetStub(false);
Craig Tiller822d2c72015-07-07 16:08:00 -0700348
349 EchoRequest request;
350 EchoResponse response;
351 request.set_message("Hello");
352
353 ClientContext context;
yang-g8b25f2a2015-07-21 23:54:36 -0700354 context.set_authority("foo.test.youtube.com");
Craig Tiller822d2c72015-07-07 16:08:00 -0700355 Status s = stub_->Echo(&context, request, &response);
356 EXPECT_EQ(response.message(), request.message());
357 EXPECT_TRUE(response.has_param());
yang-g8b25f2a2015-07-21 23:54:36 -0700358 EXPECT_EQ("special", response.param().host());
Craig Tiller822d2c72015-07-07 16:08:00 -0700359 EXPECT_TRUE(s.ok());
360}
361
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700362TEST_P(End2endTest, SimpleRpc) {
363 ResetStub(GetParam());
yangg1456d152015-01-08 15:39:58 -0800364 SendRpc(stub_.get(), 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800365}
366
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700367TEST_P(End2endTest, MultipleRpcs) {
368 ResetStub(GetParam());
Craig Tiller35e39712015-01-12 16:41:24 -0800369 std::vector<std::thread*> threads;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800370 for (int i = 0; i < 10; ++i) {
yangg1456d152015-01-08 15:39:58 -0800371 threads.push_back(new std::thread(SendRpc, stub_.get(), 10));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800372 }
373 for (int i = 0; i < 10; ++i) {
374 threads[i]->join();
375 delete threads[i];
376 }
377}
378
yanggda029e32014-12-18 10:24:04 -0800379// Set a 10us deadline and make sure proper error is returned.
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700380TEST_P(End2endTest, RpcDeadlineExpires) {
381 ResetStub(GetParam());
yanggda029e32014-12-18 10:24:04 -0800382 EchoRequest request;
383 EchoResponse response;
384 request.set_message("Hello");
385
386 ClientContext context;
387 std::chrono::system_clock::time_point deadline =
388 std::chrono::system_clock::now() + std::chrono::microseconds(10);
Nicolas Noble89219162015-04-07 18:01:18 -0700389 context.set_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800390 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700391 EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code());
yanggda029e32014-12-18 10:24:04 -0800392}
393
yangged5e7e02015-01-06 10:16:15 -0800394// Set a long but finite deadline.
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700395TEST_P(End2endTest, RpcLongDeadline) {
396 ResetStub(GetParam());
yangged5e7e02015-01-06 10:16:15 -0800397 EchoRequest request;
398 EchoResponse response;
399 request.set_message("Hello");
400
401 ClientContext context;
402 std::chrono::system_clock::time_point deadline =
403 std::chrono::system_clock::now() + std::chrono::hours(1);
Nicolas Noble89219162015-04-07 18:01:18 -0700404 context.set_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800405 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800406 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700407 EXPECT_TRUE(s.ok());
yangged5e7e02015-01-06 10:16:15 -0800408}
409
410// Ask server to echo back the deadline it sees.
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700411TEST_P(End2endTest, EchoDeadline) {
412 ResetStub(GetParam());
yangged5e7e02015-01-06 10:16:15 -0800413 EchoRequest request;
414 EchoResponse response;
415 request.set_message("Hello");
416 request.mutable_param()->set_echo_deadline(true);
417
418 ClientContext context;
419 std::chrono::system_clock::time_point deadline =
420 std::chrono::system_clock::now() + std::chrono::seconds(100);
Nicolas Noble89219162015-04-07 18:01:18 -0700421 context.set_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800422 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800423 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700424 EXPECT_TRUE(s.ok());
yangged5e7e02015-01-06 10:16:15 -0800425 gpr_timespec sent_deadline;
426 Timepoint2Timespec(deadline, &sent_deadline);
427 // Allow 1 second error.
428 EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1);
429 EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1);
yangged5e7e02015-01-06 10:16:15 -0800430}
431
432// Ask server to echo back the deadline it sees. The rpc has no deadline.
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700433TEST_P(End2endTest, EchoDeadlineForNoDeadlineRpc) {
434 ResetStub(GetParam());
yangged5e7e02015-01-06 10:16:15 -0800435 EchoRequest request;
436 EchoResponse response;
437 request.set_message("Hello");
438 request.mutable_param()->set_echo_deadline(true);
439
440 ClientContext context;
yangg1456d152015-01-08 15:39:58 -0800441 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800442 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700443 EXPECT_TRUE(s.ok());
Craig Tiller354398f2015-07-13 09:16:03 -0700444 EXPECT_EQ(response.param().request_deadline(),
445 gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec);
yangged5e7e02015-01-06 10:16:15 -0800446}
447
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700448TEST_P(End2endTest, UnimplementedRpc) {
449 ResetStub(GetParam());
nnoble0c475f02014-12-05 15:37:39 -0800450 EchoRequest request;
451 EchoResponse response;
452 request.set_message("Hello");
453
454 ClientContext context;
yangg1456d152015-01-08 15:39:58 -0800455 Status s = stub_->Unimplemented(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700456 EXPECT_FALSE(s.ok());
457 EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED);
458 EXPECT_EQ(s.error_message(), "");
nnoble0c475f02014-12-05 15:37:39 -0800459 EXPECT_EQ(response.message(), "");
nnoble0c475f02014-12-05 15:37:39 -0800460}
461
462TEST_F(End2endTest, RequestStreamOneRequest) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700463 ResetStub(false);
nnoble0c475f02014-12-05 15:37:39 -0800464 EchoRequest request;
465 EchoResponse response;
466 ClientContext context;
467
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800468 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800469 request.set_message("hello");
470 EXPECT_TRUE(stream->Write(request));
471 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800472 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800473 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700474 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800475}
476
477TEST_F(End2endTest, RequestStreamTwoRequests) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700478 ResetStub(false);
nnoble0c475f02014-12-05 15:37:39 -0800479 EchoRequest request;
480 EchoResponse response;
481 ClientContext context;
482
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800483 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800484 request.set_message("hello");
485 EXPECT_TRUE(stream->Write(request));
486 EXPECT_TRUE(stream->Write(request));
487 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800488 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800489 EXPECT_EQ(response.message(), "hellohello");
Yang Gaoc1a2c312015-06-16 10:59:46 -0700490 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800491}
492
493TEST_F(End2endTest, ResponseStream) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700494 ResetStub(false);
nnoble0c475f02014-12-05 15:37:39 -0800495 EchoRequest request;
496 EchoResponse response;
497 ClientContext context;
498 request.set_message("hello");
499
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800500 auto stream = stub_->ResponseStream(&context, request);
nnoble0c475f02014-12-05 15:37:39 -0800501 EXPECT_TRUE(stream->Read(&response));
502 EXPECT_EQ(response.message(), request.message() + "0");
503 EXPECT_TRUE(stream->Read(&response));
504 EXPECT_EQ(response.message(), request.message() + "1");
505 EXPECT_TRUE(stream->Read(&response));
506 EXPECT_EQ(response.message(), request.message() + "2");
507 EXPECT_FALSE(stream->Read(&response));
508
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800509 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700510 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800511}
512
513TEST_F(End2endTest, BidiStream) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700514 ResetStub(false);
nnoble0c475f02014-12-05 15:37:39 -0800515 EchoRequest request;
516 EchoResponse response;
517 ClientContext context;
518 grpc::string msg("hello");
519
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800520 auto stream = stub_->BidiStream(&context);
nnoble0c475f02014-12-05 15:37:39 -0800521
522 request.set_message(msg + "0");
523 EXPECT_TRUE(stream->Write(request));
524 EXPECT_TRUE(stream->Read(&response));
525 EXPECT_EQ(response.message(), request.message());
526
527 request.set_message(msg + "1");
528 EXPECT_TRUE(stream->Write(request));
529 EXPECT_TRUE(stream->Read(&response));
530 EXPECT_EQ(response.message(), request.message());
531
532 request.set_message(msg + "2");
533 EXPECT_TRUE(stream->Write(request));
534 EXPECT_TRUE(stream->Read(&response));
535 EXPECT_EQ(response.message(), request.message());
536
537 stream->WritesDone();
538 EXPECT_FALSE(stream->Read(&response));
539
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800540 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700541 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800542}
543
544// Talk to the two services with the same name but different package names.
545// The two stubs are created on the same channel.
546TEST_F(End2endTest, DiffPackageServices) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700547 ResetStub(false);
yangg1456d152015-01-08 15:39:58 -0800548 EchoRequest request;
549 EchoResponse response;
550 request.set_message("Hello");
551
yangg1456d152015-01-08 15:39:58 -0800552 ClientContext context;
yang-g8b25f2a2015-07-21 23:54:36 -0700553 Status s = stub_->Echo(&context, request, &response);
yangg1456d152015-01-08 15:39:58 -0800554 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700555 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800556
557 std::unique_ptr<grpc::cpp::test::util::duplicate::TestService::Stub>
558 dup_pkg_stub(
yang-g8b25f2a2015-07-21 23:54:36 -0700559 grpc::cpp::test::util::duplicate::TestService::NewStub(channel_));
yangg1456d152015-01-08 15:39:58 -0800560 ClientContext context2;
561 s = dup_pkg_stub->Echo(&context2, request, &response);
562 EXPECT_EQ("no package", response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700563 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800564}
565
yangg4105e2b2015-01-09 14:19:44 -0800566// rpc and stream should fail on bad credentials.
567TEST_F(End2endTest, BadCredentials) {
Yang Gaoa8938922015-05-14 11:51:07 -0700568 std::shared_ptr<Credentials> bad_creds = ServiceAccountCredentials("", "", 1);
Craig Tillerb256faa2015-07-23 11:28:16 -0700569 EXPECT_EQ(static_cast<Credentials*>(nullptr), bad_creds.get());
yangg4105e2b2015-01-09 14:19:44 -0800570 std::shared_ptr<ChannelInterface> channel =
571 CreateChannel(server_address_.str(), bad_creds, ChannelArguments());
572 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub(
573 grpc::cpp::test::util::TestService::NewStub(channel));
574 EchoRequest request;
575 EchoResponse response;
576 ClientContext context;
Yang Gao26a49122015-05-15 17:02:56 -0700577 request.set_message("Hello");
yangg4105e2b2015-01-09 14:19:44 -0800578
579 Status s = stub->Echo(&context, request, &response);
580 EXPECT_EQ("", response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700581 EXPECT_FALSE(s.ok());
582 EXPECT_EQ(StatusCode::UNKNOWN, s.error_code());
583 EXPECT_EQ("Rpc sent on a lame channel.", s.error_message());
yangg4105e2b2015-01-09 14:19:44 -0800584
585 ClientContext context2;
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800586 auto stream = stub->BidiStream(&context2);
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800587 s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700588 EXPECT_FALSE(s.ok());
589 EXPECT_EQ(StatusCode::UNKNOWN, s.error_code());
590 EXPECT_EQ("Rpc sent on a lame channel.", s.error_message());
yangg4105e2b2015-01-09 14:19:44 -0800591}
592
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700593void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) {
Craig Tiller20b5fe92015-07-06 10:43:50 -0700594 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700595 gpr_time_from_micros(delay_us, GPR_TIMESPAN)));
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700596 while (!service->signal_client()) {
597 }
598 context->TryCancel();
599}
600
601// Client cancels rpc after 10ms
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700602TEST_P(End2endTest, ClientCancelsRpc) {
603 ResetStub(GetParam());
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700604 EchoRequest request;
605 EchoResponse response;
606 request.set_message("Hello");
607 const int kCancelDelayUs = 10 * 1000;
608 request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs);
609
610 ClientContext context;
611 std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_);
612 Status s = stub_->Echo(&context, request, &response);
613 cancel_thread.join();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700614 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
615 EXPECT_EQ(s.error_message(), "Cancelled");
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700616}
617
618// Server cancels rpc after 1ms
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700619TEST_P(End2endTest, ServerCancelsRpc) {
620 ResetStub(GetParam());
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700621 EchoRequest request;
622 EchoResponse response;
623 request.set_message("Hello");
624 request.mutable_param()->set_server_cancel_after_us(1000);
625
626 ClientContext context;
627 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700628 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
629 EXPECT_TRUE(s.error_message().empty());
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700630}
631
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700632// Client cancels request stream after sending two messages
633TEST_F(End2endTest, ClientCancelsRequestStream) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700634 ResetStub(false);
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700635 EchoRequest request;
636 EchoResponse response;
637 ClientContext context;
638 request.set_message("hello");
639
640 auto stream = stub_->RequestStream(&context, &response);
641 EXPECT_TRUE(stream->Write(request));
642 EXPECT_TRUE(stream->Write(request));
Yang Gaoc71a9d22015-05-04 00:22:12 -0700643
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700644 context.TryCancel();
645
646 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700647 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700648
Yang Gaoc71a9d22015-05-04 00:22:12 -0700649 EXPECT_EQ(response.message(), "");
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700650}
651
Abhishek Kumare41d0402015-04-17 14:12:33 -0700652// Client cancels server stream after sending some messages
Abhishek Kumara1d3a722015-04-23 10:24:04 -0700653TEST_F(End2endTest, ClientCancelsResponseStream) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700654 ResetStub(false);
Abhishek Kumare41d0402015-04-17 14:12:33 -0700655 EchoRequest request;
656 EchoResponse response;
657 ClientContext context;
658 request.set_message("hello");
659
660 auto stream = stub_->ResponseStream(&context, request);
661
662 EXPECT_TRUE(stream->Read(&response));
663 EXPECT_EQ(response.message(), request.message() + "0");
664 EXPECT_TRUE(stream->Read(&response));
665 EXPECT_EQ(response.message(), request.message() + "1");
666
667 context.TryCancel();
668
669 // The cancellation races with responses, so there might be zero or
670 // one responses pending, read till failure
671
672 if (stream->Read(&response)) {
673 EXPECT_EQ(response.message(), request.message() + "2");
674 // Since we have cancelled, we expect the next attempt to read to fail
675 EXPECT_FALSE(stream->Read(&response));
676 }
677
678 Status s = stream->Finish();
Abhishek Kumar18298a72015-04-17 15:00:25 -0700679 // The final status could be either of CANCELLED or OK depending on
680 // who won the race.
Yang Gaoc1a2c312015-06-16 10:59:46 -0700681 EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumare41d0402015-04-17 14:12:33 -0700682}
683
Abhishek Kumar82a83312015-04-17 13:30:51 -0700684// Client cancels bidi stream after sending some messages
685TEST_F(End2endTest, ClientCancelsBidi) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700686 ResetStub(false);
Abhishek Kumar82a83312015-04-17 13:30:51 -0700687 EchoRequest request;
688 EchoResponse response;
689 ClientContext context;
690 grpc::string msg("hello");
691
692 auto stream = stub_->BidiStream(&context);
693
694 request.set_message(msg + "0");
695 EXPECT_TRUE(stream->Write(request));
696 EXPECT_TRUE(stream->Read(&response));
697 EXPECT_EQ(response.message(), request.message());
698
699 request.set_message(msg + "1");
700 EXPECT_TRUE(stream->Write(request));
701
702 context.TryCancel();
703
704 // The cancellation races with responses, so there might be zero or
705 // one responses pending, read till failure
706
707 if (stream->Read(&response)) {
708 EXPECT_EQ(response.message(), request.message());
709 // Since we have cancelled, we expect the next attempt to read to fail
710 EXPECT_FALSE(stream->Read(&response));
711 }
712
713 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700714 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumar82a83312015-04-17 13:30:51 -0700715}
716
Yang Gao3921c562015-04-30 16:07:06 -0700717TEST_F(End2endTest, RpcMaxMessageSize) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700718 ResetStub(false);
Yang Gao3921c562015-04-30 16:07:06 -0700719 EchoRequest request;
720 EchoResponse response;
Yang Gaoc71a9d22015-05-04 00:22:12 -0700721 request.set_message(string(kMaxMessageSize_ * 2, 'a'));
Yang Gao3921c562015-04-30 16:07:06 -0700722
723 ClientContext context;
724 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700725 EXPECT_FALSE(s.ok());
Yang Gao3921c562015-04-30 16:07:06 -0700726}
Abhishek Kumar82a83312015-04-17 13:30:51 -0700727
Yang Gao26a49122015-05-15 17:02:56 -0700728bool MetadataContains(const std::multimap<grpc::string, grpc::string>& metadata,
729 const grpc::string& key, const grpc::string& value) {
730 int count = 0;
731
732 for (std::multimap<grpc::string, grpc::string>::const_iterator iter =
733 metadata.begin();
734 iter != metadata.end(); ++iter) {
735 if ((*iter).first == key && (*iter).second == value) {
736 count++;
737 }
738 }
739 return count == 1;
740}
741
Yang Gaoa8938922015-05-14 11:51:07 -0700742TEST_F(End2endTest, SetPerCallCredentials) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700743 ResetStub(false);
Yang Gaoa8938922015-05-14 11:51:07 -0700744 EchoRequest request;
745 EchoResponse response;
746 ClientContext context;
747 std::shared_ptr<Credentials> creds =
748 IAMCredentials("fake_token", "fake_selector");
749 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -0700750 request.set_message("Hello");
751 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -0700752
753 Status s = stub_->Echo(&context, request, &response);
Yang Gaoa8938922015-05-14 11:51:07 -0700754 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700755 EXPECT_TRUE(s.ok());
Yang Gao26a49122015-05-15 17:02:56 -0700756 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
757 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
758 "fake_token"));
759 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
760 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
761 "fake_selector"));
Yang Gaoa8938922015-05-14 11:51:07 -0700762}
763
764TEST_F(End2endTest, InsecurePerCallCredentials) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700765 ResetStub(false);
Yang Gaoa8938922015-05-14 11:51:07 -0700766 EchoRequest request;
767 EchoResponse response;
768 ClientContext context;
769 std::shared_ptr<Credentials> creds = InsecureCredentials();
770 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -0700771 request.set_message("Hello");
772 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -0700773
774 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700775 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
776 EXPECT_EQ("Failed to set credentials to rpc.", s.error_message());
Yang Gaoa8938922015-05-14 11:51:07 -0700777}
778
779TEST_F(End2endTest, OverridePerCallCredentials) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700780 ResetStub(false);
Yang Gaoa8938922015-05-14 11:51:07 -0700781 EchoRequest request;
782 EchoResponse response;
783 ClientContext context;
Yang Gao26a49122015-05-15 17:02:56 -0700784 std::shared_ptr<Credentials> creds1 =
785 IAMCredentials("fake_token1", "fake_selector1");
Yang Gaoa8938922015-05-14 11:51:07 -0700786 context.set_credentials(creds1);
787 std::shared_ptr<Credentials> creds2 =
Yang Gao26a49122015-05-15 17:02:56 -0700788 IAMCredentials("fake_token2", "fake_selector2");
Yang Gaoa8938922015-05-14 11:51:07 -0700789 context.set_credentials(creds2);
Yang Gao26a49122015-05-15 17:02:56 -0700790 request.set_message("Hello");
791 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -0700792
793 Status s = stub_->Echo(&context, request, &response);
Yang Gao26a49122015-05-15 17:02:56 -0700794 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
795 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
796 "fake_token2"));
797 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
798 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
799 "fake_selector2"));
Yang Gaob57f72d2015-05-17 21:54:54 -0700800 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
801 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
802 "fake_token1"));
803 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
804 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
805 "fake_selector1"));
Yang Gaoa8938922015-05-14 11:51:07 -0700806 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700807 EXPECT_TRUE(s.ok());
Yang Gaoa8938922015-05-14 11:51:07 -0700808}
809
yang-g0b6ad7d2015-06-25 14:39:01 -0700810// Client sends 20 requests and the server returns CANCELLED status after
811// reading 10 requests.
812TEST_F(End2endTest, RequestStreamServerEarlyCancelTest) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700813 ResetStub(false);
yang-g0b6ad7d2015-06-25 14:39:01 -0700814 EchoRequest request;
815 EchoResponse response;
816 ClientContext context;
817
818 context.AddMetadata(kServerCancelAfterReads, "10");
819 auto stream = stub_->RequestStream(&context, &response);
820 request.set_message("hello");
821 int send_messages = 20;
822 while (send_messages > 0) {
823 EXPECT_TRUE(stream->Write(request));
824 send_messages--;
825 }
826 stream->WritesDone();
827 Status s = stream->Finish();
828 EXPECT_EQ(s.error_code(), StatusCode::CANCELLED);
829}
830
yang-gc4eef2e2015-07-06 23:26:58 -0700831TEST_F(End2endTest, ClientAuthContext) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700832 ResetStub(false);
yang-gc4eef2e2015-07-06 23:26:58 -0700833 EchoRequest request;
834 EchoResponse response;
835 request.set_message("Hello");
836 request.mutable_param()->set_check_auth_context(true);
837
838 ClientContext context;
839 Status s = stub_->Echo(&context, request, &response);
840 EXPECT_EQ(response.message(), request.message());
841 EXPECT_TRUE(s.ok());
842
yang-g8b25f2a2015-07-21 23:54:36 -0700843 std::shared_ptr<const AuthContext> auth_ctx = context.auth_context();
844 std::vector<grpc::string> ssl =
845 auth_ctx->FindPropertyValues("transport_security_type");
846 EXPECT_EQ(1u, ssl.size());
847 EXPECT_EQ("ssl", ssl[0]);
848 EXPECT_EQ("x509_subject_alternative_name",
849 auth_ctx->GetPeerIdentityPropertyName());
850 EXPECT_EQ(3u, auth_ctx->GetPeerIdentity().size());
851 EXPECT_EQ("*.test.google.fr", auth_ctx->GetPeerIdentity()[0]);
852 EXPECT_EQ("waterzooi.test.google.be", auth_ctx->GetPeerIdentity()[1]);
853 EXPECT_EQ("*.test.youtube.com", auth_ctx->GetPeerIdentity()[2]);
yang-gc4eef2e2015-07-06 23:26:58 -0700854}
855
yang-g6f30dec2015-07-22 23:11:56 -0700856// Make the response larger than the flow control window.
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700857TEST_P(End2endTest, HugeResponse) {
858 ResetStub(GetParam());
yang-g6f30dec2015-07-22 23:11:56 -0700859 EchoRequest request;
860 EchoResponse response;
861 request.set_message("huge response");
vjpai51d22752015-07-24 14:11:04 -0700862 const size_t kResponseSize = 1024 * (1024 + 10);
yang-g6f30dec2015-07-22 23:11:56 -0700863 request.mutable_param()->set_response_message_length(kResponseSize);
864
865 ClientContext context;
866 Status s = stub_->Echo(&context, request, &response);
867 EXPECT_EQ(kResponseSize, response.message().size());
868 EXPECT_TRUE(s.ok());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800869}
870
yang-g8ab38362015-07-31 14:05:33 -0700871namespace {
yang-g36f59652015-08-05 15:15:18 -0700872void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream,
873 gpr_event *ev) {
yang-g8ab38362015-07-31 14:05:33 -0700874 EchoResponse resp;
Craig Tiller7c1be052015-07-31 15:38:37 -0700875 gpr_event_set(ev, (void*)1);
yang-g8ab38362015-07-31 14:05:33 -0700876 while (stream->Read(&resp)) {
877 gpr_log(GPR_INFO, "Read message");
878 }
879}
880} // namespace
881
882// Run a Read and a WritesDone simultaneously.
Craig Tillerfb21ae62015-08-03 10:16:11 -0700883TEST_F(End2endTest, SimultaneousReadWritesDone) {
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700884 ResetStub(false);
yang-g8ab38362015-07-31 14:05:33 -0700885 ClientContext context;
Craig Tiller7c1be052015-07-31 15:38:37 -0700886 gpr_event ev;
887 gpr_event_init(&ev);
yang-g8ab38362015-07-31 14:05:33 -0700888 auto stream = stub_->BidiStream(&context);
Craig Tiller7c1be052015-07-31 15:38:37 -0700889 std::thread reader_thread(ReaderThreadFunc, stream.get(), &ev);
890 gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME));
yang-g8ab38362015-07-31 14:05:33 -0700891 stream->WritesDone();
892 Status s = stream->Finish();
893 EXPECT_TRUE(s.ok());
894 reader_thread.join();
895}
896
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700897TEST_P(End2endTest, Peer) {
898 ResetStub(GetParam());
yang-gd7ead692015-07-30 10:57:45 -0700899 EchoRequest request;
900 EchoResponse response;
901 request.set_message("hello");
902 request.mutable_param()->set_echo_peer(true);
903
904 ClientContext context;
905 Status s = stub_->Echo(&context, request, &response);
906 EXPECT_EQ(response.message(), request.message());
907 EXPECT_TRUE(s.ok());
908 EXPECT_TRUE(CheckIsLocalhost(response.param().peer()));
909 EXPECT_TRUE(CheckIsLocalhost(context.peer()));
910}
911
yang-g36f59652015-08-05 15:15:18 -0700912TEST_F(End2endTest, ChannelState) {
yang-gcec757f2015-08-06 22:53:58 -0700913 ResetStub(false);
yang-g36f59652015-08-05 15:15:18 -0700914 // Start IDLE
915 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(false));
916
yang-g8708dd72015-08-05 15:57:14 -0700917 // Did not ask to connect, no state change.
yang-g36f59652015-08-05 15:15:18 -0700918 CompletionQueue cq;
919 std::chrono::system_clock::time_point deadline =
920 std::chrono::system_clock::now() + std::chrono::milliseconds(10);
yang-g36f59652015-08-05 15:15:18 -0700921 channel_->NotifyOnStateChange(GRPC_CHANNEL_IDLE, deadline, &cq, NULL);
922 void* tag;
923 bool ok = true;
924 cq.Next(&tag, &ok);
925 EXPECT_FALSE(ok);
926
927 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true));
928 EXPECT_TRUE(channel_->WaitForStateChange(
929 GRPC_CHANNEL_IDLE, gpr_inf_future(GPR_CLOCK_REALTIME)));
930 EXPECT_EQ(GRPC_CHANNEL_CONNECTING, channel_->GetState(false));
931}
932
Craig Tiller2c3be1d2015-08-05 15:59:27 -0700933INSTANTIATE_TEST_CASE_P(End2end, End2endTest, ::testing::Values(false, true));
934
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800935} // namespace testing
936} // namespace grpc
937
938int main(int argc, char** argv) {
939 grpc_test_init(argc, argv);
940 ::testing::InitGoogleTest(&argc, argv);
941 return RUN_ALL_TESTS();
942}