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