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