blob: 12ac25c6dffd180caee5b26eecc1252351516213 [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
yangg1456d152015-01-08 15:39:58 -0800107class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800108 public:
vjpaidf551612015-07-14 13:38:44 -0700109 TestServiceImpl() : signal_client_(false), host_() {}
Vijay Pai181ef452015-07-14 13:52:48 -0700110 explicit TestServiceImpl(const grpc::string& host)
111 : signal_client_(false), host_(new grpc::string(host)) {}
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700112
yangga4b6f5d2014-12-17 15:53:12 -0800113 Status Echo(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800114 EchoResponse* response) GRPC_OVERRIDE {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800115 response->set_message(request->message());
yangged5e7e02015-01-06 10:16:15 -0800116 MaybeEchoDeadline(context, request, response);
Craig Tiller822d2c72015-07-07 16:08:00 -0700117 if (host_) {
118 response->mutable_param()->set_host(*host_);
119 }
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700120 if (request->has_param() && request->param().client_cancel_after_us()) {
121 {
122 std::unique_lock<std::mutex> lock(mu_);
123 signal_client_ = true;
124 }
125 while (!context->IsCancelled()) {
David Garcia Quintasfeb67f62015-05-20 19:23:25 -0700126 gpr_sleep_until(gpr_time_add(
Craig Tiller20b5fe92015-07-06 10:43:50 -0700127 gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700128 gpr_time_from_micros(request->param().client_cancel_after_us(),
129 GPR_TIMESPAN)));
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700130 }
Yang Gaoc1a2c312015-06-16 10:59:46 -0700131 return Status::CANCELLED;
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700132 } else if (request->has_param() &&
133 request->param().server_cancel_after_us()) {
David Garcia Quintasfeb67f62015-05-20 19:23:25 -0700134 gpr_sleep_until(gpr_time_add(
Craig Tiller20b5fe92015-07-06 10:43:50 -0700135 gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700136 gpr_time_from_micros(request->param().server_cancel_after_us(),
137 GPR_TIMESPAN)));
Yang Gaoc1a2c312015-06-16 10:59:46 -0700138 return Status::CANCELLED;
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700139 } else {
140 EXPECT_FALSE(context->IsCancelled());
141 }
Yang Gao26a49122015-05-15 17:02:56 -0700142
143 if (request->has_param() && request->param().echo_metadata()) {
144 const std::multimap<grpc::string, grpc::string>& client_metadata =
145 context->client_metadata();
146 for (std::multimap<grpc::string, grpc::string>::const_iterator iter =
147 client_metadata.begin();
148 iter != client_metadata.end(); ++iter) {
149 context->AddTrailingMetadata((*iter).first, (*iter).second);
150 }
151 }
yang-gc4eef2e2015-07-06 23:26:58 -0700152 if (request->has_param() && request->param().check_auth_context()) {
yang-g8b25f2a2015-07-21 23:54:36 -0700153 CheckServerAuthContext(context);
yang-gc4eef2e2015-07-06 23:26:58 -0700154 }
yang-g6f30dec2015-07-22 23:11:56 -0700155 if (request->has_param() &&
156 request->param().response_message_length() > 0) {
157 response->set_message(
158 grpc::string(request->param().response_message_length(), '\0'));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800159 }
yang-gd7ead692015-07-30 10:57:45 -0700160 if (request->has_param() && request->param().echo_peer()) {
161 response->mutable_param()->set_peer(context->peer());
162 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800163 return Status::OK;
164 }
nnoble0c475f02014-12-05 15:37:39 -0800165
166 // Unimplemented is left unimplemented to test the returned error.
167
yangga4b6f5d2014-12-17 15:53:12 -0800168 Status RequestStream(ServerContext* context,
169 ServerReader<EchoRequest>* reader,
Craig Tillercf133f42015-02-26 14:05:56 -0800170 EchoResponse* response) GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800171 EchoRequest request;
172 response->set_message("");
yang-g0b6ad7d2015-06-25 14:39:01 -0700173 int cancel_after_reads = 0;
174 const std::multimap<grpc::string, grpc::string> client_initial_metadata =
175 context->client_metadata();
176 if (client_initial_metadata.find(kServerCancelAfterReads) !=
177 client_initial_metadata.end()) {
178 std::istringstream iss(
179 client_initial_metadata.find(kServerCancelAfterReads)->second);
180 iss >> cancel_after_reads;
181 gpr_log(GPR_INFO, "cancel_after_reads %d", cancel_after_reads);
182 }
nnoble0c475f02014-12-05 15:37:39 -0800183 while (reader->Read(&request)) {
yang-g0b6ad7d2015-06-25 14:39:01 -0700184 if (cancel_after_reads == 1) {
185 gpr_log(GPR_INFO, "return cancel status");
186 return Status::CANCELLED;
187 } else if (cancel_after_reads > 0) {
188 cancel_after_reads--;
189 }
nnoble0c475f02014-12-05 15:37:39 -0800190 response->mutable_message()->append(request.message());
191 }
192 return Status::OK;
193 }
194
195 // Return 3 messages.
196 // TODO(yangg) make it generic by adding a parameter into EchoRequest
yangga4b6f5d2014-12-17 15:53:12 -0800197 Status ResponseStream(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800198 ServerWriter<EchoResponse>* writer) GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800199 EchoResponse response;
200 response.set_message(request->message() + "0");
201 writer->Write(response);
202 response.set_message(request->message() + "1");
203 writer->Write(response);
204 response.set_message(request->message() + "2");
205 writer->Write(response);
206
207 return Status::OK;
208 }
209
Craig Tillercf133f42015-02-26 14:05:56 -0800210 Status BidiStream(ServerContext* context,
211 ServerReaderWriter<EchoResponse, EchoRequest>* stream)
212 GRPC_OVERRIDE {
nnoble0c475f02014-12-05 15:37:39 -0800213 EchoRequest request;
214 EchoResponse response;
215 while (stream->Read(&request)) {
216 gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
217 response.set_message(request.message());
218 stream->Write(response);
219 }
220 return Status::OK;
221 }
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700222
223 bool signal_client() {
224 std::unique_lock<std::mutex> lock(mu_);
225 return signal_client_;
226 }
227
228 private:
229 bool signal_client_;
230 std::mutex mu_;
Craig Tiller822d2c72015-07-07 16:08:00 -0700231 std::unique_ptr<grpc::string> host_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800232};
233
yangg1456d152015-01-08 15:39:58 -0800234class TestServiceImplDupPkg
235 : public ::grpc::cpp::test::util::duplicate::TestService::Service {
236 public:
237 Status Echo(ServerContext* context, const EchoRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -0800238 EchoResponse* response) GRPC_OVERRIDE {
yangg1456d152015-01-08 15:39:58 -0800239 response->set_message("no package");
240 return Status::OK;
241 }
242};
243
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800244class End2endTest : public ::testing::Test {
245 protected:
Vijay Pai181ef452015-07-14 13:52:48 -0700246 End2endTest()
247 : kMaxMessageSize_(8192), special_service_("special"), thread_pool_(2) {}
Craig Tiller7418d012015-02-11 15:25:03 -0800248
Craig Tillercf133f42015-02-26 14:05:56 -0800249 void SetUp() GRPC_OVERRIDE {
Craig Tiller35e39712015-01-12 16:41:24 -0800250 int port = grpc_pick_unused_port_or_die();
yang-gd7ead692015-07-30 10:57:45 -0700251 server_address_ << "127.0.0.1:" << port;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800252 // Setup server
253 ServerBuilder builder;
yang-g8b25f2a2015-07-21 23:54:36 -0700254 SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
255 test_server1_cert};
256 SslServerCredentialsOptions ssl_opts;
257 ssl_opts.pem_root_certs = "";
258 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
Yang Gaob57f72d2015-05-17 21:54:54 -0700259 builder.AddListeningPort(server_address_.str(),
yang-g8b25f2a2015-07-21 23:54:36 -0700260 SslServerCredentials(ssl_opts));
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800261 builder.RegisterService(&service_);
yang-g8b25f2a2015-07-21 23:54:36 -0700262 builder.RegisterService("foo.test.youtube.com", &special_service_);
Yang Gaoc71a9d22015-05-04 00:22:12 -0700263 builder.SetMaxMessageSize(
264 kMaxMessageSize_); // For testing max message size.
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800265 builder.RegisterService(&dup_pkg_service_);
Craig Tiller7418d012015-02-11 15:25:03 -0800266 builder.SetThreadPool(&thread_pool_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800267 server_ = builder.BuildAndStart();
268 }
269
Craig Tillercf133f42015-02-26 14:05:56 -0800270 void TearDown() GRPC_OVERRIDE { server_->Shutdown(); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800271
yangg1456d152015-01-08 15:39:58 -0800272 void ResetStub() {
yang-g8b25f2a2015-07-21 23:54:36 -0700273 SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700274 ChannelArguments args;
yang-g8b25f2a2015-07-21 23:54:36 -0700275 args.SetSslTargetNameOverride("foo.test.google.fr");
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700276 args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test");
yang-g8b25f2a2015-07-21 23:54:36 -0700277 channel_ = CreateChannel(server_address_.str(), SslCredentials(ssl_opts),
278 args);
279 stub_ = std::move(grpc::cpp::test::util::TestService::NewStub(channel_));
yangg1456d152015-01-08 15:39:58 -0800280 }
281
yang-g8b25f2a2015-07-21 23:54:36 -0700282 std::shared_ptr<ChannelInterface> channel_;
yangg1456d152015-01-08 15:39:58 -0800283 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800284 std::unique_ptr<Server> server_;
285 std::ostringstream server_address_;
Yang Gao3921c562015-04-30 16:07:06 -0700286 const int kMaxMessageSize_;
nnoble0c475f02014-12-05 15:37:39 -0800287 TestServiceImpl service_;
Craig Tiller822d2c72015-07-07 16:08:00 -0700288 TestServiceImpl special_service_;
yangg1456d152015-01-08 15:39:58 -0800289 TestServiceImplDupPkg dup_pkg_service_;
Vijay Pai1f3e6c12015-07-23 16:18:21 -0700290 DynamicThreadPool thread_pool_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800291};
292
yangg1456d152015-01-08 15:39:58 -0800293static void SendRpc(grpc::cpp::test::util::TestService::Stub* stub,
294 int num_rpcs) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800295 EchoRequest request;
296 EchoResponse response;
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700297 request.set_message("Hello hello hello hello");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800298
299 for (int i = 0; i < num_rpcs; ++i) {
300 ClientContext context;
Craig Tillerbf6abee2015-07-19 22:31:04 -0700301 context.set_compression_algorithm(GRPC_COMPRESS_GZIP);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800302 Status s = stub->Echo(&context, request, &response);
303 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700304 EXPECT_TRUE(s.ok());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800305 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800306}
307
Craig Tiller822d2c72015-07-07 16:08:00 -0700308TEST_F(End2endTest, SimpleRpcWithHost) {
309 ResetStub();
310
311 EchoRequest request;
312 EchoResponse response;
313 request.set_message("Hello");
314
315 ClientContext context;
yang-g8b25f2a2015-07-21 23:54:36 -0700316 context.set_authority("foo.test.youtube.com");
Craig Tiller822d2c72015-07-07 16:08:00 -0700317 Status s = stub_->Echo(&context, request, &response);
318 EXPECT_EQ(response.message(), request.message());
319 EXPECT_TRUE(response.has_param());
yang-g8b25f2a2015-07-21 23:54:36 -0700320 EXPECT_EQ("special", response.param().host());
Craig Tiller822d2c72015-07-07 16:08:00 -0700321 EXPECT_TRUE(s.ok());
322}
323
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800324TEST_F(End2endTest, SimpleRpc) {
yangg1456d152015-01-08 15:39:58 -0800325 ResetStub();
326 SendRpc(stub_.get(), 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800327}
328
329TEST_F(End2endTest, MultipleRpcs) {
yangg1456d152015-01-08 15:39:58 -0800330 ResetStub();
Craig Tiller35e39712015-01-12 16:41:24 -0800331 std::vector<std::thread*> threads;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800332 for (int i = 0; i < 10; ++i) {
yangg1456d152015-01-08 15:39:58 -0800333 threads.push_back(new std::thread(SendRpc, stub_.get(), 10));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800334 }
335 for (int i = 0; i < 10; ++i) {
336 threads[i]->join();
337 delete threads[i];
338 }
339}
340
yanggda029e32014-12-18 10:24:04 -0800341// Set a 10us deadline and make sure proper error is returned.
342TEST_F(End2endTest, RpcDeadlineExpires) {
yangg1456d152015-01-08 15:39:58 -0800343 ResetStub();
yanggda029e32014-12-18 10:24:04 -0800344 EchoRequest request;
345 EchoResponse response;
346 request.set_message("Hello");
347
348 ClientContext context;
349 std::chrono::system_clock::time_point deadline =
350 std::chrono::system_clock::now() + std::chrono::microseconds(10);
Nicolas Noble89219162015-04-07 18:01:18 -0700351 context.set_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800352 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700353 EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code());
yanggda029e32014-12-18 10:24:04 -0800354}
355
yangged5e7e02015-01-06 10:16:15 -0800356// Set a long but finite deadline.
357TEST_F(End2endTest, RpcLongDeadline) {
yangg1456d152015-01-08 15:39:58 -0800358 ResetStub();
yangged5e7e02015-01-06 10:16:15 -0800359 EchoRequest request;
360 EchoResponse response;
361 request.set_message("Hello");
362
363 ClientContext context;
364 std::chrono::system_clock::time_point deadline =
365 std::chrono::system_clock::now() + std::chrono::hours(1);
Nicolas Noble89219162015-04-07 18:01:18 -0700366 context.set_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800367 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800368 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700369 EXPECT_TRUE(s.ok());
yangged5e7e02015-01-06 10:16:15 -0800370}
371
372// Ask server to echo back the deadline it sees.
373TEST_F(End2endTest, EchoDeadline) {
yangg1456d152015-01-08 15:39:58 -0800374 ResetStub();
yangged5e7e02015-01-06 10:16:15 -0800375 EchoRequest request;
376 EchoResponse response;
377 request.set_message("Hello");
378 request.mutable_param()->set_echo_deadline(true);
379
380 ClientContext context;
381 std::chrono::system_clock::time_point deadline =
382 std::chrono::system_clock::now() + std::chrono::seconds(100);
Nicolas Noble89219162015-04-07 18:01:18 -0700383 context.set_deadline(deadline);
yangg1456d152015-01-08 15:39:58 -0800384 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800385 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700386 EXPECT_TRUE(s.ok());
yangged5e7e02015-01-06 10:16:15 -0800387 gpr_timespec sent_deadline;
388 Timepoint2Timespec(deadline, &sent_deadline);
389 // Allow 1 second error.
390 EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1);
391 EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1);
yangged5e7e02015-01-06 10:16:15 -0800392}
393
394// Ask server to echo back the deadline it sees. The rpc has no deadline.
395TEST_F(End2endTest, EchoDeadlineForNoDeadlineRpc) {
yangg1456d152015-01-08 15:39:58 -0800396 ResetStub();
yangged5e7e02015-01-06 10:16:15 -0800397 EchoRequest request;
398 EchoResponse response;
399 request.set_message("Hello");
400 request.mutable_param()->set_echo_deadline(true);
401
402 ClientContext context;
yangg1456d152015-01-08 15:39:58 -0800403 Status s = stub_->Echo(&context, request, &response);
yangged5e7e02015-01-06 10:16:15 -0800404 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700405 EXPECT_TRUE(s.ok());
Craig Tiller354398f2015-07-13 09:16:03 -0700406 EXPECT_EQ(response.param().request_deadline(),
407 gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec);
yangged5e7e02015-01-06 10:16:15 -0800408}
409
nnoble0c475f02014-12-05 15:37:39 -0800410TEST_F(End2endTest, UnimplementedRpc) {
yangg1456d152015-01-08 15:39:58 -0800411 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800412 EchoRequest request;
413 EchoResponse response;
414 request.set_message("Hello");
415
416 ClientContext context;
yangg1456d152015-01-08 15:39:58 -0800417 Status s = stub_->Unimplemented(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700418 EXPECT_FALSE(s.ok());
419 EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED);
420 EXPECT_EQ(s.error_message(), "");
nnoble0c475f02014-12-05 15:37:39 -0800421 EXPECT_EQ(response.message(), "");
nnoble0c475f02014-12-05 15:37:39 -0800422}
423
424TEST_F(End2endTest, RequestStreamOneRequest) {
yangg1456d152015-01-08 15:39:58 -0800425 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800426 EchoRequest request;
427 EchoResponse response;
428 ClientContext context;
429
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800430 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800431 request.set_message("hello");
432 EXPECT_TRUE(stream->Write(request));
433 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800434 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800435 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700436 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800437}
438
439TEST_F(End2endTest, RequestStreamTwoRequests) {
yangg1456d152015-01-08 15:39:58 -0800440 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800441 EchoRequest request;
442 EchoResponse response;
443 ClientContext context;
444
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800445 auto stream = stub_->RequestStream(&context, &response);
nnoble0c475f02014-12-05 15:37:39 -0800446 request.set_message("hello");
447 EXPECT_TRUE(stream->Write(request));
448 EXPECT_TRUE(stream->Write(request));
449 stream->WritesDone();
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800450 Status s = stream->Finish();
nnoble0c475f02014-12-05 15:37:39 -0800451 EXPECT_EQ(response.message(), "hellohello");
Yang Gaoc1a2c312015-06-16 10:59:46 -0700452 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800453}
454
455TEST_F(End2endTest, ResponseStream) {
yangg1456d152015-01-08 15:39:58 -0800456 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800457 EchoRequest request;
458 EchoResponse response;
459 ClientContext context;
460 request.set_message("hello");
461
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800462 auto stream = stub_->ResponseStream(&context, request);
nnoble0c475f02014-12-05 15:37:39 -0800463 EXPECT_TRUE(stream->Read(&response));
464 EXPECT_EQ(response.message(), request.message() + "0");
465 EXPECT_TRUE(stream->Read(&response));
466 EXPECT_EQ(response.message(), request.message() + "1");
467 EXPECT_TRUE(stream->Read(&response));
468 EXPECT_EQ(response.message(), request.message() + "2");
469 EXPECT_FALSE(stream->Read(&response));
470
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800471 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700472 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800473}
474
475TEST_F(End2endTest, BidiStream) {
yangg1456d152015-01-08 15:39:58 -0800476 ResetStub();
nnoble0c475f02014-12-05 15:37:39 -0800477 EchoRequest request;
478 EchoResponse response;
479 ClientContext context;
480 grpc::string msg("hello");
481
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800482 auto stream = stub_->BidiStream(&context);
nnoble0c475f02014-12-05 15:37:39 -0800483
484 request.set_message(msg + "0");
485 EXPECT_TRUE(stream->Write(request));
486 EXPECT_TRUE(stream->Read(&response));
487 EXPECT_EQ(response.message(), request.message());
488
489 request.set_message(msg + "1");
490 EXPECT_TRUE(stream->Write(request));
491 EXPECT_TRUE(stream->Read(&response));
492 EXPECT_EQ(response.message(), request.message());
493
494 request.set_message(msg + "2");
495 EXPECT_TRUE(stream->Write(request));
496 EXPECT_TRUE(stream->Read(&response));
497 EXPECT_EQ(response.message(), request.message());
498
499 stream->WritesDone();
500 EXPECT_FALSE(stream->Read(&response));
501
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800502 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700503 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800504}
505
506// Talk to the two services with the same name but different package names.
507// The two stubs are created on the same channel.
508TEST_F(End2endTest, DiffPackageServices) {
yang-g8b25f2a2015-07-21 23:54:36 -0700509 ResetStub();
yangg1456d152015-01-08 15:39:58 -0800510 EchoRequest request;
511 EchoResponse response;
512 request.set_message("Hello");
513
yangg1456d152015-01-08 15:39:58 -0800514 ClientContext context;
yang-g8b25f2a2015-07-21 23:54:36 -0700515 Status s = stub_->Echo(&context, request, &response);
yangg1456d152015-01-08 15:39:58 -0800516 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700517 EXPECT_TRUE(s.ok());
yangg1456d152015-01-08 15:39:58 -0800518
519 std::unique_ptr<grpc::cpp::test::util::duplicate::TestService::Stub>
520 dup_pkg_stub(
yang-g8b25f2a2015-07-21 23:54:36 -0700521 grpc::cpp::test::util::duplicate::TestService::NewStub(channel_));
yangg1456d152015-01-08 15:39:58 -0800522 ClientContext context2;
523 s = dup_pkg_stub->Echo(&context2, request, &response);
524 EXPECT_EQ("no package", response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700525 EXPECT_TRUE(s.ok());
nnoble0c475f02014-12-05 15:37:39 -0800526}
527
yangg4105e2b2015-01-09 14:19:44 -0800528// rpc and stream should fail on bad credentials.
529TEST_F(End2endTest, BadCredentials) {
Yang Gaoa8938922015-05-14 11:51:07 -0700530 std::shared_ptr<Credentials> bad_creds = ServiceAccountCredentials("", "", 1);
Craig Tillerb256faa2015-07-23 11:28:16 -0700531 EXPECT_EQ(static_cast<Credentials*>(nullptr), bad_creds.get());
yangg4105e2b2015-01-09 14:19:44 -0800532 std::shared_ptr<ChannelInterface> channel =
533 CreateChannel(server_address_.str(), bad_creds, ChannelArguments());
534 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub(
535 grpc::cpp::test::util::TestService::NewStub(channel));
536 EchoRequest request;
537 EchoResponse response;
538 ClientContext context;
Yang Gao26a49122015-05-15 17:02:56 -0700539 request.set_message("Hello");
yangg4105e2b2015-01-09 14:19:44 -0800540
541 Status s = stub->Echo(&context, request, &response);
542 EXPECT_EQ("", response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700543 EXPECT_FALSE(s.ok());
544 EXPECT_EQ(StatusCode::UNKNOWN, s.error_code());
545 EXPECT_EQ("Rpc sent on a lame channel.", s.error_message());
yangg4105e2b2015-01-09 14:19:44 -0800546
547 ClientContext context2;
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800548 auto stream = stub->BidiStream(&context2);
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800549 s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700550 EXPECT_FALSE(s.ok());
551 EXPECT_EQ(StatusCode::UNKNOWN, s.error_code());
552 EXPECT_EQ("Rpc sent on a lame channel.", s.error_message());
yangg4105e2b2015-01-09 14:19:44 -0800553}
554
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700555void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) {
Craig Tiller20b5fe92015-07-06 10:43:50 -0700556 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
Craig Tiller677c50c2015-07-13 10:49:06 -0700557 gpr_time_from_micros(delay_us, GPR_TIMESPAN)));
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700558 while (!service->signal_client()) {
559 }
560 context->TryCancel();
561}
562
563// Client cancels rpc after 10ms
564TEST_F(End2endTest, ClientCancelsRpc) {
565 ResetStub();
566 EchoRequest request;
567 EchoResponse response;
568 request.set_message("Hello");
569 const int kCancelDelayUs = 10 * 1000;
570 request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs);
571
572 ClientContext context;
573 std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_);
574 Status s = stub_->Echo(&context, request, &response);
575 cancel_thread.join();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700576 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
577 EXPECT_EQ(s.error_message(), "Cancelled");
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700578}
579
580// Server cancels rpc after 1ms
581TEST_F(End2endTest, ServerCancelsRpc) {
582 ResetStub();
583 EchoRequest request;
584 EchoResponse response;
585 request.set_message("Hello");
586 request.mutable_param()->set_server_cancel_after_us(1000);
587
588 ClientContext context;
589 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700590 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
591 EXPECT_TRUE(s.error_message().empty());
Yang Gao0c4b0dd2015-03-30 13:08:34 -0700592}
593
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700594// Client cancels request stream after sending two messages
595TEST_F(End2endTest, ClientCancelsRequestStream) {
596 ResetStub();
597 EchoRequest request;
598 EchoResponse response;
599 ClientContext context;
600 request.set_message("hello");
601
602 auto stream = stub_->RequestStream(&context, &response);
603 EXPECT_TRUE(stream->Write(request));
604 EXPECT_TRUE(stream->Write(request));
Yang Gaoc71a9d22015-05-04 00:22:12 -0700605
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700606 context.TryCancel();
607
608 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700609 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700610
Yang Gaoc71a9d22015-05-04 00:22:12 -0700611 EXPECT_EQ(response.message(), "");
Abhishek Kumard774c5c2015-04-23 14:59:49 -0700612}
613
Abhishek Kumare41d0402015-04-17 14:12:33 -0700614// Client cancels server stream after sending some messages
Abhishek Kumara1d3a722015-04-23 10:24:04 -0700615TEST_F(End2endTest, ClientCancelsResponseStream) {
Abhishek Kumare41d0402015-04-17 14:12:33 -0700616 ResetStub();
617 EchoRequest request;
618 EchoResponse response;
619 ClientContext context;
620 request.set_message("hello");
621
622 auto stream = stub_->ResponseStream(&context, request);
623
624 EXPECT_TRUE(stream->Read(&response));
625 EXPECT_EQ(response.message(), request.message() + "0");
626 EXPECT_TRUE(stream->Read(&response));
627 EXPECT_EQ(response.message(), request.message() + "1");
628
629 context.TryCancel();
630
631 // The cancellation races with responses, so there might be zero or
632 // one responses pending, read till failure
633
634 if (stream->Read(&response)) {
635 EXPECT_EQ(response.message(), request.message() + "2");
636 // Since we have cancelled, we expect the next attempt to read to fail
637 EXPECT_FALSE(stream->Read(&response));
638 }
639
640 Status s = stream->Finish();
Abhishek Kumar18298a72015-04-17 15:00:25 -0700641 // The final status could be either of CANCELLED or OK depending on
642 // who won the race.
Yang Gaoc1a2c312015-06-16 10:59:46 -0700643 EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumare41d0402015-04-17 14:12:33 -0700644}
645
Abhishek Kumar82a83312015-04-17 13:30:51 -0700646// Client cancels bidi stream after sending some messages
647TEST_F(End2endTest, ClientCancelsBidi) {
648 ResetStub();
649 EchoRequest request;
650 EchoResponse response;
651 ClientContext context;
652 grpc::string msg("hello");
653
654 auto stream = stub_->BidiStream(&context);
655
656 request.set_message(msg + "0");
657 EXPECT_TRUE(stream->Write(request));
658 EXPECT_TRUE(stream->Read(&response));
659 EXPECT_EQ(response.message(), request.message());
660
661 request.set_message(msg + "1");
662 EXPECT_TRUE(stream->Write(request));
663
664 context.TryCancel();
665
666 // The cancellation races with responses, so there might be zero or
667 // one responses pending, read till failure
668
669 if (stream->Read(&response)) {
670 EXPECT_EQ(response.message(), request.message());
671 // Since we have cancelled, we expect the next attempt to read to fail
672 EXPECT_FALSE(stream->Read(&response));
673 }
674
675 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700676 EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code());
Abhishek Kumar82a83312015-04-17 13:30:51 -0700677}
678
Yang Gao3921c562015-04-30 16:07:06 -0700679TEST_F(End2endTest, RpcMaxMessageSize) {
680 ResetStub();
681 EchoRequest request;
682 EchoResponse response;
Yang Gaoc71a9d22015-05-04 00:22:12 -0700683 request.set_message(string(kMaxMessageSize_ * 2, 'a'));
Yang Gao3921c562015-04-30 16:07:06 -0700684
685 ClientContext context;
686 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700687 EXPECT_FALSE(s.ok());
Yang Gao3921c562015-04-30 16:07:06 -0700688}
Abhishek Kumar82a83312015-04-17 13:30:51 -0700689
Yang Gao26a49122015-05-15 17:02:56 -0700690bool MetadataContains(const std::multimap<grpc::string, grpc::string>& metadata,
691 const grpc::string& key, const grpc::string& value) {
692 int count = 0;
693
694 for (std::multimap<grpc::string, grpc::string>::const_iterator iter =
695 metadata.begin();
696 iter != metadata.end(); ++iter) {
697 if ((*iter).first == key && (*iter).second == value) {
698 count++;
699 }
700 }
701 return count == 1;
702}
703
Yang Gaoa8938922015-05-14 11:51:07 -0700704TEST_F(End2endTest, SetPerCallCredentials) {
705 ResetStub();
706 EchoRequest request;
707 EchoResponse response;
708 ClientContext context;
709 std::shared_ptr<Credentials> creds =
710 IAMCredentials("fake_token", "fake_selector");
711 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -0700712 request.set_message("Hello");
713 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -0700714
715 Status s = stub_->Echo(&context, request, &response);
Yang Gaoa8938922015-05-14 11:51:07 -0700716 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700717 EXPECT_TRUE(s.ok());
Yang Gao26a49122015-05-15 17:02:56 -0700718 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
719 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
720 "fake_token"));
721 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
722 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
723 "fake_selector"));
Yang Gaoa8938922015-05-14 11:51:07 -0700724}
725
726TEST_F(End2endTest, InsecurePerCallCredentials) {
727 ResetStub();
728 EchoRequest request;
729 EchoResponse response;
730 ClientContext context;
731 std::shared_ptr<Credentials> creds = InsecureCredentials();
732 context.set_credentials(creds);
Yang Gao26a49122015-05-15 17:02:56 -0700733 request.set_message("Hello");
734 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -0700735
736 Status s = stub_->Echo(&context, request, &response);
Yang Gaoc1a2c312015-06-16 10:59:46 -0700737 EXPECT_EQ(StatusCode::CANCELLED, s.error_code());
738 EXPECT_EQ("Failed to set credentials to rpc.", s.error_message());
Yang Gaoa8938922015-05-14 11:51:07 -0700739}
740
741TEST_F(End2endTest, OverridePerCallCredentials) {
742 ResetStub();
743 EchoRequest request;
744 EchoResponse response;
745 ClientContext context;
Yang Gao26a49122015-05-15 17:02:56 -0700746 std::shared_ptr<Credentials> creds1 =
747 IAMCredentials("fake_token1", "fake_selector1");
Yang Gaoa8938922015-05-14 11:51:07 -0700748 context.set_credentials(creds1);
749 std::shared_ptr<Credentials> creds2 =
Yang Gao26a49122015-05-15 17:02:56 -0700750 IAMCredentials("fake_token2", "fake_selector2");
Yang Gaoa8938922015-05-14 11:51:07 -0700751 context.set_credentials(creds2);
Yang Gao26a49122015-05-15 17:02:56 -0700752 request.set_message("Hello");
753 request.mutable_param()->set_echo_metadata(true);
Yang Gaoa8938922015-05-14 11:51:07 -0700754
755 Status s = stub_->Echo(&context, request, &response);
Yang Gao26a49122015-05-15 17:02:56 -0700756 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
757 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
758 "fake_token2"));
759 EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(),
760 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
761 "fake_selector2"));
Yang Gaob57f72d2015-05-17 21:54:54 -0700762 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
763 GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
764 "fake_token1"));
765 EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(),
766 GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
767 "fake_selector1"));
Yang Gaoa8938922015-05-14 11:51:07 -0700768 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700769 EXPECT_TRUE(s.ok());
Yang Gaoa8938922015-05-14 11:51:07 -0700770}
771
yang-g0b6ad7d2015-06-25 14:39:01 -0700772// Client sends 20 requests and the server returns CANCELLED status after
773// reading 10 requests.
774TEST_F(End2endTest, RequestStreamServerEarlyCancelTest) {
775 ResetStub();
776 EchoRequest request;
777 EchoResponse response;
778 ClientContext context;
779
780 context.AddMetadata(kServerCancelAfterReads, "10");
781 auto stream = stub_->RequestStream(&context, &response);
782 request.set_message("hello");
783 int send_messages = 20;
784 while (send_messages > 0) {
785 EXPECT_TRUE(stream->Write(request));
786 send_messages--;
787 }
788 stream->WritesDone();
789 Status s = stream->Finish();
790 EXPECT_EQ(s.error_code(), StatusCode::CANCELLED);
791}
792
yang-gc4eef2e2015-07-06 23:26:58 -0700793TEST_F(End2endTest, ClientAuthContext) {
794 ResetStub();
795 EchoRequest request;
796 EchoResponse response;
797 request.set_message("Hello");
798 request.mutable_param()->set_check_auth_context(true);
799
800 ClientContext context;
801 Status s = stub_->Echo(&context, request, &response);
802 EXPECT_EQ(response.message(), request.message());
803 EXPECT_TRUE(s.ok());
804
yang-g8b25f2a2015-07-21 23:54:36 -0700805 std::shared_ptr<const AuthContext> auth_ctx = context.auth_context();
806 std::vector<grpc::string> ssl =
807 auth_ctx->FindPropertyValues("transport_security_type");
808 EXPECT_EQ(1u, ssl.size());
809 EXPECT_EQ("ssl", ssl[0]);
810 EXPECT_EQ("x509_subject_alternative_name",
811 auth_ctx->GetPeerIdentityPropertyName());
812 EXPECT_EQ(3u, auth_ctx->GetPeerIdentity().size());
813 EXPECT_EQ("*.test.google.fr", auth_ctx->GetPeerIdentity()[0]);
814 EXPECT_EQ("waterzooi.test.google.be", auth_ctx->GetPeerIdentity()[1]);
815 EXPECT_EQ("*.test.youtube.com", auth_ctx->GetPeerIdentity()[2]);
yang-gc4eef2e2015-07-06 23:26:58 -0700816}
817
yang-g6f30dec2015-07-22 23:11:56 -0700818// Make the response larger than the flow control window.
819TEST_F(End2endTest, HugeResponse) {
820 ResetStub();
821 EchoRequest request;
822 EchoResponse response;
823 request.set_message("huge response");
vjpai51d22752015-07-24 14:11:04 -0700824 const size_t kResponseSize = 1024 * (1024 + 10);
yang-g6f30dec2015-07-22 23:11:56 -0700825 request.mutable_param()->set_response_message_length(kResponseSize);
826
827 ClientContext context;
828 Status s = stub_->Echo(&context, request, &response);
829 EXPECT_EQ(kResponseSize, response.message().size());
830 EXPECT_TRUE(s.ok());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800831}
832
yang-g8ab38362015-07-31 14:05:33 -0700833namespace {
yang-g36f59652015-08-05 15:15:18 -0700834void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream,
835 gpr_event *ev) {
yang-g8ab38362015-07-31 14:05:33 -0700836 EchoResponse resp;
Craig Tiller7c1be052015-07-31 15:38:37 -0700837 gpr_event_set(ev, (void*)1);
yang-g8ab38362015-07-31 14:05:33 -0700838 while (stream->Read(&resp)) {
839 gpr_log(GPR_INFO, "Read message");
840 }
841}
842} // namespace
843
844// Run a Read and a WritesDone simultaneously.
Craig Tillerfb21ae62015-08-03 10:16:11 -0700845TEST_F(End2endTest, SimultaneousReadWritesDone) {
yang-g8ab38362015-07-31 14:05:33 -0700846 ResetStub();
847 ClientContext context;
Craig Tiller7c1be052015-07-31 15:38:37 -0700848 gpr_event ev;
849 gpr_event_init(&ev);
yang-g8ab38362015-07-31 14:05:33 -0700850 auto stream = stub_->BidiStream(&context);
Craig Tiller7c1be052015-07-31 15:38:37 -0700851 std::thread reader_thread(ReaderThreadFunc, stream.get(), &ev);
852 gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME));
yang-g8ab38362015-07-31 14:05:33 -0700853 stream->WritesDone();
854 Status s = stream->Finish();
855 EXPECT_TRUE(s.ok());
856 reader_thread.join();
857}
858
yang-gd7ead692015-07-30 10:57:45 -0700859TEST_F(End2endTest, Peer) {
860 ResetStub();
861 EchoRequest request;
862 EchoResponse response;
863 request.set_message("hello");
864 request.mutable_param()->set_echo_peer(true);
865
866 ClientContext context;
867 Status s = stub_->Echo(&context, request, &response);
868 EXPECT_EQ(response.message(), request.message());
869 EXPECT_TRUE(s.ok());
870 EXPECT_TRUE(CheckIsLocalhost(response.param().peer()));
871 EXPECT_TRUE(CheckIsLocalhost(context.peer()));
872}
873
yang-g36f59652015-08-05 15:15:18 -0700874TEST_F(End2endTest, ChannelState) {
875 ResetStub();
876 // Start IDLE
877 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(false));
878
yang-g8708dd72015-08-05 15:57:14 -0700879 // Did not ask to connect, no state change.
yang-g36f59652015-08-05 15:15:18 -0700880 CompletionQueue cq;
881 std::chrono::system_clock::time_point deadline =
882 std::chrono::system_clock::now() + std::chrono::milliseconds(10);
yang-g36f59652015-08-05 15:15:18 -0700883 channel_->NotifyOnStateChange(GRPC_CHANNEL_IDLE, deadline, &cq, NULL);
884 void* tag;
885 bool ok = true;
886 cq.Next(&tag, &ok);
887 EXPECT_FALSE(ok);
888
889 EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true));
890 EXPECT_TRUE(channel_->WaitForStateChange(
891 GRPC_CHANNEL_IDLE, gpr_inf_future(GPR_CLOCK_REALTIME)));
892 EXPECT_EQ(GRPC_CHANNEL_CONNECTING, channel_->GetState(false));
yang-g8708dd72015-08-05 15:57:14 -0700893 EXPECT_TRUE(channel_->WaitForState(GRPC_CHANNEL_READY,
894 gpr_inf_future(GPR_CLOCK_REALTIME)));
yang-g36f59652015-08-05 15:15:18 -0700895}
896
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800897} // namespace testing
898} // namespace grpc
899
900int main(int argc, char** argv) {
901 grpc_test_init(argc, argv);
902 ::testing::InitGoogleTest(&argc, argv);
903 return RUN_ALL_TESTS();
904}