Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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 Gao | 69fe075 | 2015-06-01 14:32:38 -0700 | [diff] [blame] | 34 | #include <mutex> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | #include <thread> |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 36 | |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 37 | #include "src/core/security/credentials.h" |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 38 | #include "test/core/end2end/data/ssl_test_data.h" |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 39 | #include "test/core/util/port.h" |
Craig Tiller | 14e60e9 | 2015-01-13 17:26:46 -0800 | [diff] [blame] | 40 | #include "test/core/util/test_config.h" |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 41 | #include "test/cpp/util/echo_duplicate.grpc.pb.h" |
| 42 | #include "test/cpp/util/echo.grpc.pb.h" |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 43 | #include <grpc++/channel_arguments.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | #include <grpc++/channel_interface.h> |
| 45 | #include <grpc++/client_context.h> |
| 46 | #include <grpc++/create_channel.h> |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 47 | #include <grpc++/credentials.h> |
vjpai | a2e1bb1 | 2015-07-15 16:49:49 -0700 | [diff] [blame] | 48 | #include <grpc++/fixed_size_thread_pool.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | #include <grpc++/server.h> |
| 50 | #include <grpc++/server_builder.h> |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 51 | #include <grpc++/server_context.h> |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 52 | #include <grpc++/server_credentials.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | #include <grpc++/status.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 54 | #include <grpc++/stream.h> |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 55 | #include <grpc++/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | #include <gtest/gtest.h> |
| 57 | |
| 58 | #include <grpc/grpc.h> |
| 59 | #include <grpc/support/thd.h> |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 60 | #include <grpc/support/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | |
| 62 | using grpc::cpp::test::util::EchoRequest; |
| 63 | using grpc::cpp::test::util::EchoResponse; |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 64 | using std::chrono::system_clock; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | |
| 66 | namespace grpc { |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 67 | namespace testing { |
| 68 | |
| 69 | namespace { |
| 70 | |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 71 | const char* kServerCancelAfterReads = "cancel_after_reads"; |
| 72 | |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 73 | // When echo_deadline is requested, deadline seen in the ServerContext is set in |
| 74 | // the response in seconds. |
| 75 | void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request, |
| 76 | EchoResponse* response) { |
| 77 | if (request->has_param() && request->param().echo_deadline()) { |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 78 | gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_REALTIME); |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 79 | if (context->deadline() != system_clock::time_point::max()) { |
| 80 | Timepoint2Timespec(context->deadline(), &deadline); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 81 | } |
| 82 | response->mutable_param()->set_request_deadline(deadline.tv_sec); |
| 83 | } |
| 84 | } |
Craig Tiller | 7418d01 | 2015-02-11 15:25:03 -0800 | [diff] [blame] | 85 | |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 86 | void CheckServerAuthContext(const ServerContext* context) { |
yang-g | 85c04f9 | 2015-07-07 17:47:31 -0700 | [diff] [blame] | 87 | std::shared_ptr<const AuthContext> auth_ctx = context->auth_context(); |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 88 | std::vector<grpc::string> ssl = |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 89 | auth_ctx->FindPropertyValues("transport_security_type"); |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 90 | EXPECT_EQ(1u, ssl.size()); |
| 91 | EXPECT_EQ("ssl", ssl[0]); |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 92 | EXPECT_TRUE(auth_ctx->GetPeerIdentityPropertyName().empty()); |
| 93 | EXPECT_TRUE(auth_ctx->GetPeerIdentity().empty()); |
| 94 | } |
| 95 | |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 96 | } // namespace |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 97 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 98 | class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | public: |
vjpai | df55161 | 2015-07-14 13:38:44 -0700 | [diff] [blame] | 100 | TestServiceImpl() : signal_client_(false), host_() {} |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 101 | explicit TestServiceImpl(const grpc::string& host) |
| 102 | : signal_client_(false), host_(new grpc::string(host)) {} |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 103 | |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 104 | Status Echo(ServerContext* context, const EchoRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 105 | EchoResponse* response) GRPC_OVERRIDE { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 106 | response->set_message(request->message()); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 107 | MaybeEchoDeadline(context, request, response); |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 108 | if (host_) { |
| 109 | response->mutable_param()->set_host(*host_); |
| 110 | } |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 111 | if (request->has_param() && request->param().client_cancel_after_us()) { |
| 112 | { |
| 113 | std::unique_lock<std::mutex> lock(mu_); |
| 114 | signal_client_ = true; |
| 115 | } |
| 116 | while (!context->IsCancelled()) { |
David Garcia Quintas | feb67f6 | 2015-05-20 19:23:25 -0700 | [diff] [blame] | 117 | gpr_sleep_until(gpr_time_add( |
Craig Tiller | 20b5fe9 | 2015-07-06 10:43:50 -0700 | [diff] [blame] | 118 | gpr_now(GPR_CLOCK_REALTIME), |
Craig Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 119 | gpr_time_from_micros(request->param().client_cancel_after_us(), |
| 120 | GPR_TIMESPAN))); |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 121 | } |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 122 | return Status::CANCELLED; |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 123 | } else if (request->has_param() && |
| 124 | request->param().server_cancel_after_us()) { |
David Garcia Quintas | feb67f6 | 2015-05-20 19:23:25 -0700 | [diff] [blame] | 125 | gpr_sleep_until(gpr_time_add( |
Craig Tiller | 20b5fe9 | 2015-07-06 10:43:50 -0700 | [diff] [blame] | 126 | gpr_now(GPR_CLOCK_REALTIME), |
Craig Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 127 | gpr_time_from_micros(request->param().server_cancel_after_us(), |
| 128 | GPR_TIMESPAN))); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 129 | return Status::CANCELLED; |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 130 | } else { |
| 131 | EXPECT_FALSE(context->IsCancelled()); |
| 132 | } |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 133 | |
| 134 | if (request->has_param() && request->param().echo_metadata()) { |
| 135 | const std::multimap<grpc::string, grpc::string>& client_metadata = |
| 136 | context->client_metadata(); |
| 137 | for (std::multimap<grpc::string, grpc::string>::const_iterator iter = |
| 138 | client_metadata.begin(); |
| 139 | iter != client_metadata.end(); ++iter) { |
| 140 | context->AddTrailingMetadata((*iter).first, (*iter).second); |
| 141 | } |
| 142 | } |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 143 | if (request->has_param() && request->param().check_auth_context()) { |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 144 | CheckServerAuthContext(context); |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 145 | } |
yang-g | 6f30dec | 2015-07-22 23:11:56 -0700 | [diff] [blame] | 146 | if (request->has_param() && |
| 147 | request->param().response_message_length() > 0) { |
| 148 | response->set_message( |
| 149 | grpc::string(request->param().response_message_length(), '\0')); |
| 150 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 151 | return Status::OK; |
| 152 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 153 | |
| 154 | // Unimplemented is left unimplemented to test the returned error. |
| 155 | |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 156 | Status RequestStream(ServerContext* context, |
| 157 | ServerReader<EchoRequest>* reader, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 158 | EchoResponse* response) GRPC_OVERRIDE { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 159 | EchoRequest request; |
| 160 | response->set_message(""); |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 161 | int cancel_after_reads = 0; |
| 162 | const std::multimap<grpc::string, grpc::string> client_initial_metadata = |
| 163 | context->client_metadata(); |
| 164 | if (client_initial_metadata.find(kServerCancelAfterReads) != |
| 165 | client_initial_metadata.end()) { |
| 166 | std::istringstream iss( |
| 167 | client_initial_metadata.find(kServerCancelAfterReads)->second); |
| 168 | iss >> cancel_after_reads; |
| 169 | gpr_log(GPR_INFO, "cancel_after_reads %d", cancel_after_reads); |
| 170 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 171 | while (reader->Read(&request)) { |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 172 | if (cancel_after_reads == 1) { |
| 173 | gpr_log(GPR_INFO, "return cancel status"); |
| 174 | return Status::CANCELLED; |
| 175 | } else if (cancel_after_reads > 0) { |
| 176 | cancel_after_reads--; |
| 177 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 178 | response->mutable_message()->append(request.message()); |
| 179 | } |
| 180 | return Status::OK; |
| 181 | } |
| 182 | |
| 183 | // Return 3 messages. |
| 184 | // TODO(yangg) make it generic by adding a parameter into EchoRequest |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 185 | Status ResponseStream(ServerContext* context, const EchoRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 186 | ServerWriter<EchoResponse>* writer) GRPC_OVERRIDE { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 187 | EchoResponse response; |
| 188 | response.set_message(request->message() + "0"); |
| 189 | writer->Write(response); |
| 190 | response.set_message(request->message() + "1"); |
| 191 | writer->Write(response); |
| 192 | response.set_message(request->message() + "2"); |
| 193 | writer->Write(response); |
| 194 | |
| 195 | return Status::OK; |
| 196 | } |
| 197 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 198 | Status BidiStream(ServerContext* context, |
| 199 | ServerReaderWriter<EchoResponse, EchoRequest>* stream) |
| 200 | GRPC_OVERRIDE { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 201 | EchoRequest request; |
| 202 | EchoResponse response; |
| 203 | while (stream->Read(&request)) { |
| 204 | gpr_log(GPR_INFO, "recv msg %s", request.message().c_str()); |
| 205 | response.set_message(request.message()); |
| 206 | stream->Write(response); |
| 207 | } |
| 208 | return Status::OK; |
| 209 | } |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 210 | |
| 211 | bool signal_client() { |
| 212 | std::unique_lock<std::mutex> lock(mu_); |
| 213 | return signal_client_; |
| 214 | } |
| 215 | |
| 216 | private: |
| 217 | bool signal_client_; |
| 218 | std::mutex mu_; |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 219 | std::unique_ptr<grpc::string> host_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 220 | }; |
| 221 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 222 | class TestServiceImplDupPkg |
| 223 | : public ::grpc::cpp::test::util::duplicate::TestService::Service { |
| 224 | public: |
| 225 | Status Echo(ServerContext* context, const EchoRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 226 | EchoResponse* response) GRPC_OVERRIDE { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 227 | response->set_message("no package"); |
| 228 | return Status::OK; |
| 229 | } |
| 230 | }; |
| 231 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 232 | class End2endTest : public ::testing::Test { |
| 233 | protected: |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 234 | End2endTest() |
| 235 | : kMaxMessageSize_(8192), special_service_("special"), thread_pool_(2) {} |
Craig Tiller | 7418d01 | 2015-02-11 15:25:03 -0800 | [diff] [blame] | 236 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 237 | void SetUp() GRPC_OVERRIDE { |
Craig Tiller | 35e3971 | 2015-01-12 16:41:24 -0800 | [diff] [blame] | 238 | int port = grpc_pick_unused_port_or_die(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 239 | server_address_ << "localhost:" << port; |
| 240 | // Setup server |
| 241 | ServerBuilder builder; |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 242 | SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key, |
| 243 | test_server1_cert}; |
| 244 | SslServerCredentialsOptions ssl_opts; |
| 245 | ssl_opts.pem_root_certs = ""; |
| 246 | ssl_opts.pem_key_cert_pairs.push_back(pkcp); |
Yang Gao | b57f72d | 2015-05-17 21:54:54 -0700 | [diff] [blame] | 247 | builder.AddListeningPort(server_address_.str(), |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 248 | SslServerCredentials(ssl_opts)); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 249 | builder.RegisterService(&service_); |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 250 | builder.RegisterService("foo.test.youtube.com", &special_service_); |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 251 | builder.SetMaxMessageSize( |
| 252 | kMaxMessageSize_); // For testing max message size. |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 253 | builder.RegisterService(&dup_pkg_service_); |
Craig Tiller | 7418d01 | 2015-02-11 15:25:03 -0800 | [diff] [blame] | 254 | builder.SetThreadPool(&thread_pool_); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 255 | server_ = builder.BuildAndStart(); |
| 256 | } |
| 257 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 258 | void TearDown() GRPC_OVERRIDE { server_->Shutdown(); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 259 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 260 | void ResetStub() { |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 261 | SslCredentialsOptions ssl_opts = {test_root_cert, "", ""}; |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 262 | ChannelArguments args; |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 263 | args.SetSslTargetNameOverride("foo.test.google.fr"); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 264 | args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test"); |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 265 | channel_ = CreateChannel(server_address_.str(), SslCredentials(ssl_opts), |
| 266 | args); |
| 267 | stub_ = std::move(grpc::cpp::test::util::TestService::NewStub(channel_)); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 268 | } |
| 269 | |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 270 | std::shared_ptr<ChannelInterface> channel_; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 271 | std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 272 | std::unique_ptr<Server> server_; |
| 273 | std::ostringstream server_address_; |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 274 | const int kMaxMessageSize_; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 275 | TestServiceImpl service_; |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 276 | TestServiceImpl special_service_; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 277 | TestServiceImplDupPkg dup_pkg_service_; |
Vijay Pai | 69f2410 | 2015-07-15 12:33:37 -0700 | [diff] [blame] | 278 | FixedSizeThreadPool thread_pool_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 279 | }; |
| 280 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 281 | static void SendRpc(grpc::cpp::test::util::TestService::Stub* stub, |
| 282 | int num_rpcs) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 283 | EchoRequest request; |
| 284 | EchoResponse response; |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 285 | request.set_message("Hello hello hello hello"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 286 | |
| 287 | for (int i = 0; i < num_rpcs; ++i) { |
| 288 | ClientContext context; |
Craig Tiller | bf6abee | 2015-07-19 22:31:04 -0700 | [diff] [blame] | 289 | context.set_compression_algorithm(GRPC_COMPRESS_GZIP); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 290 | Status s = stub->Echo(&context, request, &response); |
| 291 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 292 | EXPECT_TRUE(s.ok()); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 293 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 296 | TEST_F(End2endTest, SimpleRpcWithHost) { |
| 297 | ResetStub(); |
| 298 | |
| 299 | EchoRequest request; |
| 300 | EchoResponse response; |
| 301 | request.set_message("Hello"); |
| 302 | |
| 303 | ClientContext context; |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 304 | context.set_authority("foo.test.youtube.com"); |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 305 | Status s = stub_->Echo(&context, request, &response); |
| 306 | EXPECT_EQ(response.message(), request.message()); |
| 307 | EXPECT_TRUE(response.has_param()); |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 308 | EXPECT_EQ("special", response.param().host()); |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 309 | EXPECT_TRUE(s.ok()); |
| 310 | } |
| 311 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 312 | TEST_F(End2endTest, SimpleRpc) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 313 | ResetStub(); |
| 314 | SendRpc(stub_.get(), 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | TEST_F(End2endTest, MultipleRpcs) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 318 | ResetStub(); |
Craig Tiller | 35e3971 | 2015-01-12 16:41:24 -0800 | [diff] [blame] | 319 | std::vector<std::thread*> threads; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 320 | for (int i = 0; i < 10; ++i) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 321 | threads.push_back(new std::thread(SendRpc, stub_.get(), 10)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 322 | } |
| 323 | for (int i = 0; i < 10; ++i) { |
| 324 | threads[i]->join(); |
| 325 | delete threads[i]; |
| 326 | } |
| 327 | } |
| 328 | |
yangg | da029e3 | 2014-12-18 10:24:04 -0800 | [diff] [blame] | 329 | // Set a 10us deadline and make sure proper error is returned. |
| 330 | TEST_F(End2endTest, RpcDeadlineExpires) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 331 | ResetStub(); |
yangg | da029e3 | 2014-12-18 10:24:04 -0800 | [diff] [blame] | 332 | EchoRequest request; |
| 333 | EchoResponse response; |
| 334 | request.set_message("Hello"); |
| 335 | |
| 336 | ClientContext context; |
| 337 | std::chrono::system_clock::time_point deadline = |
| 338 | std::chrono::system_clock::now() + std::chrono::microseconds(10); |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 339 | context.set_deadline(deadline); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 340 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 341 | EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code()); |
yangg | da029e3 | 2014-12-18 10:24:04 -0800 | [diff] [blame] | 342 | } |
| 343 | |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 344 | // Set a long but finite deadline. |
| 345 | TEST_F(End2endTest, RpcLongDeadline) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 346 | ResetStub(); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 347 | EchoRequest request; |
| 348 | EchoResponse response; |
| 349 | request.set_message("Hello"); |
| 350 | |
| 351 | ClientContext context; |
| 352 | std::chrono::system_clock::time_point deadline = |
| 353 | std::chrono::system_clock::now() + std::chrono::hours(1); |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 354 | context.set_deadline(deadline); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 355 | Status s = stub_->Echo(&context, request, &response); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 356 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 357 | EXPECT_TRUE(s.ok()); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // Ask server to echo back the deadline it sees. |
| 361 | TEST_F(End2endTest, EchoDeadline) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 362 | ResetStub(); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 363 | EchoRequest request; |
| 364 | EchoResponse response; |
| 365 | request.set_message("Hello"); |
| 366 | request.mutable_param()->set_echo_deadline(true); |
| 367 | |
| 368 | ClientContext context; |
| 369 | std::chrono::system_clock::time_point deadline = |
| 370 | std::chrono::system_clock::now() + std::chrono::seconds(100); |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 371 | context.set_deadline(deadline); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 372 | Status s = stub_->Echo(&context, request, &response); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 373 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 374 | EXPECT_TRUE(s.ok()); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 375 | gpr_timespec sent_deadline; |
| 376 | Timepoint2Timespec(deadline, &sent_deadline); |
| 377 | // Allow 1 second error. |
| 378 | EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1); |
| 379 | EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // Ask server to echo back the deadline it sees. The rpc has no deadline. |
| 383 | TEST_F(End2endTest, EchoDeadlineForNoDeadlineRpc) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 384 | ResetStub(); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 385 | EchoRequest request; |
| 386 | EchoResponse response; |
| 387 | request.set_message("Hello"); |
| 388 | request.mutable_param()->set_echo_deadline(true); |
| 389 | |
| 390 | ClientContext context; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 391 | Status s = stub_->Echo(&context, request, &response); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 392 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 393 | EXPECT_TRUE(s.ok()); |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 394 | EXPECT_EQ(response.param().request_deadline(), |
| 395 | gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 396 | } |
| 397 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 398 | TEST_F(End2endTest, UnimplementedRpc) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 399 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 400 | EchoRequest request; |
| 401 | EchoResponse response; |
| 402 | request.set_message("Hello"); |
| 403 | |
| 404 | ClientContext context; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 405 | Status s = stub_->Unimplemented(&context, request, &response); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 406 | EXPECT_FALSE(s.ok()); |
| 407 | EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED); |
| 408 | EXPECT_EQ(s.error_message(), ""); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 409 | EXPECT_EQ(response.message(), ""); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | TEST_F(End2endTest, RequestStreamOneRequest) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 413 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 414 | EchoRequest request; |
| 415 | EchoResponse response; |
| 416 | ClientContext context; |
| 417 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 418 | auto stream = stub_->RequestStream(&context, &response); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 419 | request.set_message("hello"); |
| 420 | EXPECT_TRUE(stream->Write(request)); |
| 421 | stream->WritesDone(); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 422 | Status s = stream->Finish(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 423 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 424 | EXPECT_TRUE(s.ok()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | TEST_F(End2endTest, RequestStreamTwoRequests) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 428 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 429 | EchoRequest request; |
| 430 | EchoResponse response; |
| 431 | ClientContext context; |
| 432 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 433 | auto stream = stub_->RequestStream(&context, &response); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 434 | request.set_message("hello"); |
| 435 | EXPECT_TRUE(stream->Write(request)); |
| 436 | EXPECT_TRUE(stream->Write(request)); |
| 437 | stream->WritesDone(); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 438 | Status s = stream->Finish(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 439 | EXPECT_EQ(response.message(), "hellohello"); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 440 | EXPECT_TRUE(s.ok()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | TEST_F(End2endTest, ResponseStream) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 444 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 445 | EchoRequest request; |
| 446 | EchoResponse response; |
| 447 | ClientContext context; |
| 448 | request.set_message("hello"); |
| 449 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 450 | auto stream = stub_->ResponseStream(&context, request); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 451 | EXPECT_TRUE(stream->Read(&response)); |
| 452 | EXPECT_EQ(response.message(), request.message() + "0"); |
| 453 | EXPECT_TRUE(stream->Read(&response)); |
| 454 | EXPECT_EQ(response.message(), request.message() + "1"); |
| 455 | EXPECT_TRUE(stream->Read(&response)); |
| 456 | EXPECT_EQ(response.message(), request.message() + "2"); |
| 457 | EXPECT_FALSE(stream->Read(&response)); |
| 458 | |
Craig Tiller | 4d0fb5f | 2015-02-09 16:27:22 -0800 | [diff] [blame] | 459 | Status s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 460 | EXPECT_TRUE(s.ok()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | TEST_F(End2endTest, BidiStream) { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 464 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 465 | EchoRequest request; |
| 466 | EchoResponse response; |
| 467 | ClientContext context; |
| 468 | grpc::string msg("hello"); |
| 469 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 470 | auto stream = stub_->BidiStream(&context); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 471 | |
| 472 | request.set_message(msg + "0"); |
| 473 | EXPECT_TRUE(stream->Write(request)); |
| 474 | EXPECT_TRUE(stream->Read(&response)); |
| 475 | EXPECT_EQ(response.message(), request.message()); |
| 476 | |
| 477 | request.set_message(msg + "1"); |
| 478 | EXPECT_TRUE(stream->Write(request)); |
| 479 | EXPECT_TRUE(stream->Read(&response)); |
| 480 | EXPECT_EQ(response.message(), request.message()); |
| 481 | |
| 482 | request.set_message(msg + "2"); |
| 483 | EXPECT_TRUE(stream->Write(request)); |
| 484 | EXPECT_TRUE(stream->Read(&response)); |
| 485 | EXPECT_EQ(response.message(), request.message()); |
| 486 | |
| 487 | stream->WritesDone(); |
| 488 | EXPECT_FALSE(stream->Read(&response)); |
| 489 | |
Craig Tiller | 4d0fb5f | 2015-02-09 16:27:22 -0800 | [diff] [blame] | 490 | Status s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 491 | EXPECT_TRUE(s.ok()); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // Talk to the two services with the same name but different package names. |
| 495 | // The two stubs are created on the same channel. |
| 496 | TEST_F(End2endTest, DiffPackageServices) { |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 497 | ResetStub(); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 498 | EchoRequest request; |
| 499 | EchoResponse response; |
| 500 | request.set_message("Hello"); |
| 501 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 502 | ClientContext context; |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 503 | Status s = stub_->Echo(&context, request, &response); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 504 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 505 | EXPECT_TRUE(s.ok()); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 506 | |
| 507 | std::unique_ptr<grpc::cpp::test::util::duplicate::TestService::Stub> |
| 508 | dup_pkg_stub( |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 509 | grpc::cpp::test::util::duplicate::TestService::NewStub(channel_)); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 510 | ClientContext context2; |
| 511 | s = dup_pkg_stub->Echo(&context2, request, &response); |
| 512 | EXPECT_EQ("no package", response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 513 | EXPECT_TRUE(s.ok()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 514 | } |
| 515 | |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 516 | // rpc and stream should fail on bad credentials. |
| 517 | TEST_F(End2endTest, BadCredentials) { |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 518 | std::shared_ptr<Credentials> bad_creds = ServiceAccountCredentials("", "", 1); |
Vijay Pai | c4af224 | 2015-07-21 18:43:28 +0000 | [diff] [blame] | 519 | EXPECT_EQ(static_cast<Credentials *>(nullptr), bad_creds.get()); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 520 | std::shared_ptr<ChannelInterface> channel = |
| 521 | CreateChannel(server_address_.str(), bad_creds, ChannelArguments()); |
| 522 | std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub( |
| 523 | grpc::cpp::test::util::TestService::NewStub(channel)); |
| 524 | EchoRequest request; |
| 525 | EchoResponse response; |
| 526 | ClientContext context; |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 527 | request.set_message("Hello"); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 528 | |
| 529 | Status s = stub->Echo(&context, request, &response); |
| 530 | EXPECT_EQ("", response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 531 | EXPECT_FALSE(s.ok()); |
| 532 | EXPECT_EQ(StatusCode::UNKNOWN, s.error_code()); |
| 533 | EXPECT_EQ("Rpc sent on a lame channel.", s.error_message()); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 534 | |
| 535 | ClientContext context2; |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 536 | auto stream = stub->BidiStream(&context2); |
Craig Tiller | 4d0fb5f | 2015-02-09 16:27:22 -0800 | [diff] [blame] | 537 | s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 538 | EXPECT_FALSE(s.ok()); |
| 539 | EXPECT_EQ(StatusCode::UNKNOWN, s.error_code()); |
| 540 | EXPECT_EQ("Rpc sent on a lame channel.", s.error_message()); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 543 | void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) { |
Craig Tiller | 20b5fe9 | 2015-07-06 10:43:50 -0700 | [diff] [blame] | 544 | gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
Craig Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 545 | gpr_time_from_micros(delay_us, GPR_TIMESPAN))); |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 546 | while (!service->signal_client()) { |
| 547 | } |
| 548 | context->TryCancel(); |
| 549 | } |
| 550 | |
| 551 | // Client cancels rpc after 10ms |
| 552 | TEST_F(End2endTest, ClientCancelsRpc) { |
| 553 | ResetStub(); |
| 554 | EchoRequest request; |
| 555 | EchoResponse response; |
| 556 | request.set_message("Hello"); |
| 557 | const int kCancelDelayUs = 10 * 1000; |
| 558 | request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs); |
| 559 | |
| 560 | ClientContext context; |
| 561 | std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_); |
| 562 | Status s = stub_->Echo(&context, request, &response); |
| 563 | cancel_thread.join(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 564 | EXPECT_EQ(StatusCode::CANCELLED, s.error_code()); |
| 565 | EXPECT_EQ(s.error_message(), "Cancelled"); |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // Server cancels rpc after 1ms |
| 569 | TEST_F(End2endTest, ServerCancelsRpc) { |
| 570 | ResetStub(); |
| 571 | EchoRequest request; |
| 572 | EchoResponse response; |
| 573 | request.set_message("Hello"); |
| 574 | request.mutable_param()->set_server_cancel_after_us(1000); |
| 575 | |
| 576 | ClientContext context; |
| 577 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 578 | EXPECT_EQ(StatusCode::CANCELLED, s.error_code()); |
| 579 | EXPECT_TRUE(s.error_message().empty()); |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 582 | // Client cancels request stream after sending two messages |
| 583 | TEST_F(End2endTest, ClientCancelsRequestStream) { |
| 584 | ResetStub(); |
| 585 | EchoRequest request; |
| 586 | EchoResponse response; |
| 587 | ClientContext context; |
| 588 | request.set_message("hello"); |
| 589 | |
| 590 | auto stream = stub_->RequestStream(&context, &response); |
| 591 | EXPECT_TRUE(stream->Write(request)); |
| 592 | EXPECT_TRUE(stream->Write(request)); |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 593 | |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 594 | context.TryCancel(); |
| 595 | |
| 596 | Status s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 597 | EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 598 | |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 599 | EXPECT_EQ(response.message(), ""); |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Abhishek Kumar | e41d040 | 2015-04-17 14:12:33 -0700 | [diff] [blame] | 602 | // Client cancels server stream after sending some messages |
Abhishek Kumar | a1d3a72 | 2015-04-23 10:24:04 -0700 | [diff] [blame] | 603 | TEST_F(End2endTest, ClientCancelsResponseStream) { |
Abhishek Kumar | e41d040 | 2015-04-17 14:12:33 -0700 | [diff] [blame] | 604 | ResetStub(); |
| 605 | EchoRequest request; |
| 606 | EchoResponse response; |
| 607 | ClientContext context; |
| 608 | request.set_message("hello"); |
| 609 | |
| 610 | auto stream = stub_->ResponseStream(&context, request); |
| 611 | |
| 612 | EXPECT_TRUE(stream->Read(&response)); |
| 613 | EXPECT_EQ(response.message(), request.message() + "0"); |
| 614 | EXPECT_TRUE(stream->Read(&response)); |
| 615 | EXPECT_EQ(response.message(), request.message() + "1"); |
| 616 | |
| 617 | context.TryCancel(); |
| 618 | |
| 619 | // The cancellation races with responses, so there might be zero or |
| 620 | // one responses pending, read till failure |
| 621 | |
| 622 | if (stream->Read(&response)) { |
| 623 | EXPECT_EQ(response.message(), request.message() + "2"); |
| 624 | // Since we have cancelled, we expect the next attempt to read to fail |
| 625 | EXPECT_FALSE(stream->Read(&response)); |
| 626 | } |
| 627 | |
| 628 | Status s = stream->Finish(); |
Abhishek Kumar | 18298a7 | 2015-04-17 15:00:25 -0700 | [diff] [blame] | 629 | // The final status could be either of CANCELLED or OK depending on |
| 630 | // who won the race. |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 631 | EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code()); |
Abhishek Kumar | e41d040 | 2015-04-17 14:12:33 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Abhishek Kumar | 82a8331 | 2015-04-17 13:30:51 -0700 | [diff] [blame] | 634 | // Client cancels bidi stream after sending some messages |
| 635 | TEST_F(End2endTest, ClientCancelsBidi) { |
| 636 | ResetStub(); |
| 637 | EchoRequest request; |
| 638 | EchoResponse response; |
| 639 | ClientContext context; |
| 640 | grpc::string msg("hello"); |
| 641 | |
| 642 | auto stream = stub_->BidiStream(&context); |
| 643 | |
| 644 | request.set_message(msg + "0"); |
| 645 | EXPECT_TRUE(stream->Write(request)); |
| 646 | EXPECT_TRUE(stream->Read(&response)); |
| 647 | EXPECT_EQ(response.message(), request.message()); |
| 648 | |
| 649 | request.set_message(msg + "1"); |
| 650 | EXPECT_TRUE(stream->Write(request)); |
| 651 | |
| 652 | context.TryCancel(); |
| 653 | |
| 654 | // The cancellation races with responses, so there might be zero or |
| 655 | // one responses pending, read till failure |
| 656 | |
| 657 | if (stream->Read(&response)) { |
| 658 | EXPECT_EQ(response.message(), request.message()); |
| 659 | // Since we have cancelled, we expect the next attempt to read to fail |
| 660 | EXPECT_FALSE(stream->Read(&response)); |
| 661 | } |
| 662 | |
| 663 | Status s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 664 | EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
Abhishek Kumar | 82a8331 | 2015-04-17 13:30:51 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 667 | TEST_F(End2endTest, RpcMaxMessageSize) { |
| 668 | ResetStub(); |
| 669 | EchoRequest request; |
| 670 | EchoResponse response; |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 671 | request.set_message(string(kMaxMessageSize_ * 2, 'a')); |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 672 | |
| 673 | ClientContext context; |
| 674 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 675 | EXPECT_FALSE(s.ok()); |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 676 | } |
Abhishek Kumar | 82a8331 | 2015-04-17 13:30:51 -0700 | [diff] [blame] | 677 | |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 678 | bool MetadataContains(const std::multimap<grpc::string, grpc::string>& metadata, |
| 679 | const grpc::string& key, const grpc::string& value) { |
| 680 | int count = 0; |
| 681 | |
| 682 | for (std::multimap<grpc::string, grpc::string>::const_iterator iter = |
| 683 | metadata.begin(); |
| 684 | iter != metadata.end(); ++iter) { |
| 685 | if ((*iter).first == key && (*iter).second == value) { |
| 686 | count++; |
| 687 | } |
| 688 | } |
| 689 | return count == 1; |
| 690 | } |
| 691 | |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 692 | TEST_F(End2endTest, SetPerCallCredentials) { |
| 693 | ResetStub(); |
| 694 | EchoRequest request; |
| 695 | EchoResponse response; |
| 696 | ClientContext context; |
| 697 | std::shared_ptr<Credentials> creds = |
| 698 | IAMCredentials("fake_token", "fake_selector"); |
| 699 | context.set_credentials(creds); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 700 | request.set_message("Hello"); |
| 701 | request.mutable_param()->set_echo_metadata(true); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 702 | |
| 703 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 704 | EXPECT_EQ(request.message(), response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 705 | EXPECT_TRUE(s.ok()); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 706 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 707 | GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 708 | "fake_token")); |
| 709 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 710 | GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 711 | "fake_selector")); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | TEST_F(End2endTest, InsecurePerCallCredentials) { |
| 715 | ResetStub(); |
| 716 | EchoRequest request; |
| 717 | EchoResponse response; |
| 718 | ClientContext context; |
| 719 | std::shared_ptr<Credentials> creds = InsecureCredentials(); |
| 720 | context.set_credentials(creds); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 721 | request.set_message("Hello"); |
| 722 | request.mutable_param()->set_echo_metadata(true); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 723 | |
| 724 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 725 | EXPECT_EQ(StatusCode::CANCELLED, s.error_code()); |
| 726 | EXPECT_EQ("Failed to set credentials to rpc.", s.error_message()); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | TEST_F(End2endTest, OverridePerCallCredentials) { |
| 730 | ResetStub(); |
| 731 | EchoRequest request; |
| 732 | EchoResponse response; |
| 733 | ClientContext context; |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 734 | std::shared_ptr<Credentials> creds1 = |
| 735 | IAMCredentials("fake_token1", "fake_selector1"); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 736 | context.set_credentials(creds1); |
| 737 | std::shared_ptr<Credentials> creds2 = |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 738 | IAMCredentials("fake_token2", "fake_selector2"); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 739 | context.set_credentials(creds2); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 740 | request.set_message("Hello"); |
| 741 | request.mutable_param()->set_echo_metadata(true); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 742 | |
| 743 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 744 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 745 | GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 746 | "fake_token2")); |
| 747 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 748 | GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 749 | "fake_selector2")); |
Yang Gao | b57f72d | 2015-05-17 21:54:54 -0700 | [diff] [blame] | 750 | EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(), |
| 751 | GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 752 | "fake_token1")); |
| 753 | EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(), |
| 754 | GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 755 | "fake_selector1")); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 756 | EXPECT_EQ(request.message(), response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 757 | EXPECT_TRUE(s.ok()); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 758 | } |
| 759 | |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 760 | // Client sends 20 requests and the server returns CANCELLED status after |
| 761 | // reading 10 requests. |
| 762 | TEST_F(End2endTest, RequestStreamServerEarlyCancelTest) { |
| 763 | ResetStub(); |
| 764 | EchoRequest request; |
| 765 | EchoResponse response; |
| 766 | ClientContext context; |
| 767 | |
| 768 | context.AddMetadata(kServerCancelAfterReads, "10"); |
| 769 | auto stream = stub_->RequestStream(&context, &response); |
| 770 | request.set_message("hello"); |
| 771 | int send_messages = 20; |
| 772 | while (send_messages > 0) { |
| 773 | EXPECT_TRUE(stream->Write(request)); |
| 774 | send_messages--; |
| 775 | } |
| 776 | stream->WritesDone(); |
| 777 | Status s = stream->Finish(); |
| 778 | EXPECT_EQ(s.error_code(), StatusCode::CANCELLED); |
| 779 | } |
| 780 | |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 781 | TEST_F(End2endTest, ClientAuthContext) { |
| 782 | ResetStub(); |
| 783 | EchoRequest request; |
| 784 | EchoResponse response; |
| 785 | request.set_message("Hello"); |
| 786 | request.mutable_param()->set_check_auth_context(true); |
| 787 | |
| 788 | ClientContext context; |
| 789 | Status s = stub_->Echo(&context, request, &response); |
| 790 | EXPECT_EQ(response.message(), request.message()); |
| 791 | EXPECT_TRUE(s.ok()); |
| 792 | |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 793 | std::shared_ptr<const AuthContext> auth_ctx = context.auth_context(); |
| 794 | std::vector<grpc::string> ssl = |
| 795 | auth_ctx->FindPropertyValues("transport_security_type"); |
| 796 | EXPECT_EQ(1u, ssl.size()); |
| 797 | EXPECT_EQ("ssl", ssl[0]); |
| 798 | EXPECT_EQ("x509_subject_alternative_name", |
| 799 | auth_ctx->GetPeerIdentityPropertyName()); |
| 800 | EXPECT_EQ(3u, auth_ctx->GetPeerIdentity().size()); |
| 801 | EXPECT_EQ("*.test.google.fr", auth_ctx->GetPeerIdentity()[0]); |
| 802 | EXPECT_EQ("waterzooi.test.google.be", auth_ctx->GetPeerIdentity()[1]); |
| 803 | EXPECT_EQ("*.test.youtube.com", auth_ctx->GetPeerIdentity()[2]); |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 804 | } |
| 805 | |
yang-g | 6f30dec | 2015-07-22 23:11:56 -0700 | [diff] [blame] | 806 | // Make the response larger than the flow control window. |
| 807 | TEST_F(End2endTest, HugeResponse) { |
| 808 | ResetStub(); |
| 809 | EchoRequest request; |
| 810 | EchoResponse response; |
| 811 | request.set_message("huge response"); |
| 812 | const int kResponseSize = 1024 * (1024 + 10); |
| 813 | request.mutable_param()->set_response_message_length(kResponseSize); |
| 814 | |
| 815 | ClientContext context; |
| 816 | Status s = stub_->Echo(&context, request, &response); |
| 817 | EXPECT_EQ(kResponseSize, response.message().size()); |
| 818 | EXPECT_TRUE(s.ok()); |
| 819 | } |
| 820 | |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 821 | } // namespace testing |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 822 | } // namespace grpc |
| 823 | |
| 824 | int main(int argc, char** argv) { |
Craig Tiller | 14e60e9 | 2015-01-13 17:26:46 -0800 | [diff] [blame] | 825 | grpc_test_init(argc, argv); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 826 | ::testing::InitGoogleTest(&argc, argv); |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame] | 827 | return RUN_ALL_TESTS(); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 828 | } |