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