Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [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++/channel.h> |
| 38 | #include <grpc++/client_context.h> |
| 39 | #include <grpc++/create_channel.h> |
Julien Boeuf | 5be92a3 | 2015-08-28 16:28:18 -0700 | [diff] [blame] | 40 | #include <grpc++/security/auth_metadata_processor.h> |
| 41 | #include <grpc++/security/credentials.h> |
| 42 | #include <grpc++/security/server_credentials.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 43 | #include <grpc++/server.h> |
| 44 | #include <grpc++/server_builder.h> |
| 45 | #include <grpc++/server_context.h> |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 46 | #include <grpc/grpc.h> |
David Garcia Quintas | c79b065 | 2016-07-27 21:11:58 -0700 | [diff] [blame] | 47 | #include <grpc/support/log.h> |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 48 | #include <grpc/support/thd.h> |
| 49 | #include <grpc/support/time.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 50 | #include <gtest/gtest.h> |
| 51 | |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 52 | #include "src/core/lib/security/credentials/credentials.h" |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 53 | #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h" |
| 54 | #include "src/proto/grpc/testing/echo.grpc.pb.h" |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 55 | #include "test/core/util/port.h" |
Craig Tiller | 14e60e9 | 2015-01-13 17:26:46 -0800 | [diff] [blame] | 56 | #include "test/core/util/test_config.h" |
yang-g | 7b0edbd | 2016-02-02 16:05:21 -0800 | [diff] [blame] | 57 | #include "test/cpp/end2end/test_service_impl.h" |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 58 | #include "test/cpp/util/string_ref_helper.h" |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 59 | #include "test/cpp/util/test_credentials_provider.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 61 | using grpc::testing::EchoRequest; |
| 62 | using grpc::testing::EchoResponse; |
Dan Born | f2f7d57 | 2016-03-03 17:26:12 -0800 | [diff] [blame] | 63 | using grpc::testing::kTlsCredentialsType; |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 64 | using std::chrono::system_clock; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | |
| 66 | namespace grpc { |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 67 | namespace testing { |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 68 | namespace { |
| 69 | |
yang-g | d7ead69 | 2015-07-30 10:57:45 -0700 | [diff] [blame] | 70 | bool CheckIsLocalhost(const grpc::string& addr) { |
| 71 | const grpc::string kIpv6("ipv6:[::1]:"); |
| 72 | const grpc::string kIpv4MappedIpv6("ipv6:[::ffff:127.0.0.1]:"); |
| 73 | const grpc::string kIpv4("ipv4:127.0.0.1:"); |
| 74 | return addr.substr(0, kIpv4.size()) == kIpv4 || |
| 75 | addr.substr(0, kIpv4MappedIpv6.size()) == kIpv4MappedIpv6 || |
| 76 | addr.substr(0, kIpv6.size()) == kIpv6; |
| 77 | } |
| 78 | |
Julien Boeuf | 38c0cde | 2016-06-06 14:46:08 +0200 | [diff] [blame] | 79 | const char kTestCredsPluginErrorMsg[] = "Could not find plugin metadata."; |
| 80 | |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 81 | class TestMetadataCredentialsPlugin : public MetadataCredentialsPlugin { |
| 82 | public: |
yang-g | c580af3 | 2016-09-15 15:28:38 -0700 | [diff] [blame] | 83 | static const char kGoodMetadataKey[]; |
| 84 | static const char kBadMetadataKey[]; |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 85 | |
yang-g | c580af3 | 2016-09-15 15:28:38 -0700 | [diff] [blame] | 86 | TestMetadataCredentialsPlugin(grpc::string_ref metadata_key, |
| 87 | grpc::string_ref metadata_value, |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 88 | bool is_blocking, bool is_successful) |
yang-g | c580af3 | 2016-09-15 15:28:38 -0700 | [diff] [blame] | 89 | : metadata_key_(metadata_key.data(), metadata_key.length()), |
| 90 | metadata_value_(metadata_value.data(), metadata_value.length()), |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 91 | is_blocking_(is_blocking), |
| 92 | is_successful_(is_successful) {} |
| 93 | |
| 94 | bool IsBlocking() const GRPC_OVERRIDE { return is_blocking_; } |
| 95 | |
Julien Boeuf | 114f394 | 2015-11-19 21:45:52 -0800 | [diff] [blame] | 96 | Status GetMetadata(grpc::string_ref service_url, grpc::string_ref method_name, |
| 97 | const grpc::AuthContext& channel_auth_context, |
Julien Boeuf | 8b0b6f4 | 2015-09-22 13:31:16 -0700 | [diff] [blame] | 98 | std::multimap<grpc::string, grpc::string>* metadata) |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 99 | GRPC_OVERRIDE { |
| 100 | EXPECT_GT(service_url.length(), 0UL); |
Julien Boeuf | 114f394 | 2015-11-19 21:45:52 -0800 | [diff] [blame] | 101 | EXPECT_GT(method_name.length(), 0UL); |
| 102 | EXPECT_TRUE(channel_auth_context.IsPeerAuthenticated()); |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 103 | EXPECT_TRUE(metadata != nullptr); |
| 104 | if (is_successful_) { |
yang-g | c580af3 | 2016-09-15 15:28:38 -0700 | [diff] [blame] | 105 | metadata->insert(std::make_pair(metadata_key_, metadata_value_)); |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 106 | return Status::OK; |
| 107 | } else { |
Julien Boeuf | 38c0cde | 2016-06-06 14:46:08 +0200 | [diff] [blame] | 108 | return Status(StatusCode::NOT_FOUND, kTestCredsPluginErrorMsg); |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | private: |
yang-g | c580af3 | 2016-09-15 15:28:38 -0700 | [diff] [blame] | 113 | grpc::string metadata_key_; |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 114 | grpc::string metadata_value_; |
| 115 | bool is_blocking_; |
| 116 | bool is_successful_; |
| 117 | }; |
| 118 | |
yang-g | c580af3 | 2016-09-15 15:28:38 -0700 | [diff] [blame] | 119 | const char TestMetadataCredentialsPlugin::kBadMetadataKey[] = |
| 120 | "TestPluginMetadata"; |
| 121 | const char TestMetadataCredentialsPlugin::kGoodMetadataKey[] = |
| 122 | "test-plugin-metadata"; |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 123 | |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 124 | class TestAuthMetadataProcessor : public AuthMetadataProcessor { |
| 125 | public: |
| 126 | static const char kGoodGuy[]; |
| 127 | |
| 128 | TestAuthMetadataProcessor(bool is_blocking) : is_blocking_(is_blocking) {} |
| 129 | |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 130 | std::shared_ptr<CallCredentials> GetCompatibleClientCreds() { |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 131 | return MetadataCredentialsFromPlugin( |
| 132 | std::unique_ptr<MetadataCredentialsPlugin>( |
yang-g | c580af3 | 2016-09-15 15:28:38 -0700 | [diff] [blame] | 133 | new TestMetadataCredentialsPlugin( |
| 134 | TestMetadataCredentialsPlugin::kGoodMetadataKey, kGoodGuy, |
| 135 | is_blocking_, true))); |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 136 | } |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 137 | |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 138 | std::shared_ptr<CallCredentials> GetIncompatibleClientCreds() { |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 139 | return MetadataCredentialsFromPlugin( |
| 140 | std::unique_ptr<MetadataCredentialsPlugin>( |
yang-g | c580af3 | 2016-09-15 15:28:38 -0700 | [diff] [blame] | 141 | new TestMetadataCredentialsPlugin( |
| 142 | TestMetadataCredentialsPlugin::kGoodMetadataKey, "Mr Hyde", |
| 143 | is_blocking_, true))); |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Interface implementation |
| 147 | bool IsBlocking() const GRPC_OVERRIDE { return is_blocking_; } |
| 148 | |
| 149 | Status Process(const InputMetadata& auth_metadata, AuthContext* context, |
| 150 | OutputMetadata* consumed_auth_metadata, |
| 151 | OutputMetadata* response_metadata) GRPC_OVERRIDE { |
| 152 | EXPECT_TRUE(consumed_auth_metadata != nullptr); |
| 153 | EXPECT_TRUE(context != nullptr); |
| 154 | EXPECT_TRUE(response_metadata != nullptr); |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 155 | auto auth_md = |
yang-g | c580af3 | 2016-09-15 15:28:38 -0700 | [diff] [blame] | 156 | auth_metadata.find(TestMetadataCredentialsPlugin::kGoodMetadataKey); |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 157 | EXPECT_NE(auth_md, auth_metadata.end()); |
| 158 | string_ref auth_md_value = auth_md->second; |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 159 | if (auth_md_value == kGoodGuy) { |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 160 | context->AddProperty(kIdentityPropName, kGoodGuy); |
| 161 | context->SetPeerIdentityPropertyName(kIdentityPropName); |
Julien Boeuf | 8b0b6f4 | 2015-09-22 13:31:16 -0700 | [diff] [blame] | 162 | consumed_auth_metadata->insert(std::make_pair( |
| 163 | string(auth_md->first.data(), auth_md->first.length()), |
| 164 | string(auth_md->second.data(), auth_md->second.length()))); |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 165 | return Status::OK; |
| 166 | } else { |
| 167 | return Status(StatusCode::UNAUTHENTICATED, |
| 168 | string("Invalid principal: ") + |
| 169 | string(auth_md_value.data(), auth_md_value.length())); |
| 170 | } |
| 171 | } |
| 172 | |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 173 | private: |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 174 | static const char kIdentityPropName[]; |
| 175 | bool is_blocking_; |
| 176 | }; |
| 177 | |
| 178 | const char TestAuthMetadataProcessor::kGoodGuy[] = "Dr Jekyll"; |
| 179 | const char TestAuthMetadataProcessor::kIdentityPropName[] = "novel identity"; |
| 180 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 181 | class Proxy : public ::grpc::testing::EchoTestService::Service { |
Craig Tiller | 2c3be1d | 2015-08-05 15:59:27 -0700 | [diff] [blame] | 182 | public: |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 183 | Proxy(std::shared_ptr<Channel> channel) |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 184 | : stub_(grpc::testing::EchoTestService::NewStub(channel)) {} |
Craig Tiller | 2c3be1d | 2015-08-05 15:59:27 -0700 | [diff] [blame] | 185 | |
| 186 | Status Echo(ServerContext* server_context, const EchoRequest* request, |
| 187 | EchoResponse* response) GRPC_OVERRIDE { |
| 188 | std::unique_ptr<ClientContext> client_context = |
| 189 | ClientContext::FromServerContext(*server_context); |
| 190 | return stub_->Echo(client_context.get(), *request, response); |
| 191 | } |
| 192 | |
| 193 | private: |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 194 | std::unique_ptr< ::grpc::testing::EchoTestService::Stub> stub_; |
Craig Tiller | 2c3be1d | 2015-08-05 15:59:27 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 197 | class TestServiceImplDupPkg |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 198 | : public ::grpc::testing::duplicate::EchoTestService::Service { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 199 | public: |
| 200 | Status Echo(ServerContext* context, const EchoRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 201 | EchoResponse* response) GRPC_OVERRIDE { |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 202 | response->set_message("no package"); |
| 203 | return Status::OK; |
| 204 | } |
| 205 | }; |
| 206 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 207 | class TestScenario { |
| 208 | public: |
yang-g | 17197dd | 2016-02-19 00:04:22 -0800 | [diff] [blame] | 209 | TestScenario(bool proxy, const grpc::string& creds_type) |
| 210 | : use_proxy(proxy), credentials_type(creds_type) {} |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 211 | void Log() const { |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 212 | gpr_log(GPR_INFO, "Scenario: proxy %d, credentials %s", use_proxy, |
yang-g | 17197dd | 2016-02-19 00:04:22 -0800 | [diff] [blame] | 213 | credentials_type.c_str()); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 214 | } |
| 215 | bool use_proxy; |
Vijay Pai | 679c75f | 2016-06-15 13:08:00 -0700 | [diff] [blame] | 216 | // Although the below grpc::string is logically const, we can't declare |
| 217 | // them const because of a limitation in the way old compilers (e.g., gcc-4.4) |
| 218 | // manage vector insertion using a copy constructor |
| 219 | grpc::string credentials_type; |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | class End2endTest : public ::testing::TestWithParam<TestScenario> { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 223 | protected: |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 224 | End2endTest() |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 225 | : is_server_started_(false), |
| 226 | kMaxMessageSize_(8192), |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 227 | special_service_("special") { |
| 228 | GetParam().Log(); |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame^] | 229 | |
| 230 | sync_server_settings_.max_pollers = INT_MAX; |
| 231 | sync_server_settings_.min_pollers = 1; |
| 232 | sync_server_settings_.cq_timeout_msec = 10; |
| 233 | sync_server_settings_.num_cqs = 4; |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 234 | } |
Craig Tiller | 7418d01 | 2015-02-11 15:25:03 -0800 | [diff] [blame] | 235 | |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 236 | void TearDown() GRPC_OVERRIDE { |
| 237 | if (is_server_started_) { |
| 238 | server_->Shutdown(); |
| 239 | if (proxy_server_) proxy_server_->Shutdown(); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void StartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) { |
Craig Tiller | 35e3971 | 2015-01-12 16:41:24 -0800 | [diff] [blame] | 244 | int port = grpc_pick_unused_port_or_die(); |
yang-g | d7ead69 | 2015-07-30 10:57:45 -0700 | [diff] [blame] | 245 | server_address_ << "127.0.0.1:" << port; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 246 | // Setup server |
| 247 | ServerBuilder builder; |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 248 | auto server_creds = GetServerCredentials(GetParam().credentials_type); |
yang-g | 17197dd | 2016-02-19 00:04:22 -0800 | [diff] [blame] | 249 | if (GetParam().credentials_type != kInsecureCredentialsType) { |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 250 | server_creds->SetAuthMetadataProcessor(processor); |
| 251 | } |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 252 | builder.AddListeningPort(server_address_.str(), server_creds); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 253 | builder.RegisterService(&service_); |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 254 | builder.RegisterService("foo.test.youtube.com", &special_service_); |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 255 | builder.SetMaxMessageSize( |
| 256 | kMaxMessageSize_); // For testing max message size. |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 257 | builder.RegisterService(&dup_pkg_service_); |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame^] | 258 | |
| 259 | builder.SetSyncServerSettings(sync_server_settings_); |
| 260 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 261 | server_ = builder.BuildAndStart(); |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 262 | is_server_started_ = true; |
Craig Tiller | 2c3be1d | 2015-08-05 15:59:27 -0700 | [diff] [blame] | 263 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 264 | |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 265 | void ResetChannel() { |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 266 | if (!is_server_started_) { |
| 267 | StartServer(std::shared_ptr<AuthMetadataProcessor>()); |
| 268 | } |
| 269 | EXPECT_TRUE(is_server_started_); |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 270 | ChannelArguments args; |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 271 | auto channel_creds = |
| 272 | GetChannelCredentials(GetParam().credentials_type, &args); |
yang-g | d59ad7e | 2016-02-10 12:42:53 -0800 | [diff] [blame] | 273 | if (!user_agent_prefix_.empty()) { |
| 274 | args.SetUserAgentPrefix(user_agent_prefix_); |
| 275 | } |
Craig Tiller | 0dc5e6c | 2015-07-10 10:07:53 -0700 | [diff] [blame] | 276 | args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test"); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 277 | channel_ = CreateCustomChannel(server_address_.str(), channel_creds, args); |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 278 | } |
| 279 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 280 | void ResetStub() { |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 281 | ResetChannel(); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 282 | if (GetParam().use_proxy) { |
Craig Tiller | 2c3be1d | 2015-08-05 15:59:27 -0700 | [diff] [blame] | 283 | proxy_service_.reset(new Proxy(channel_)); |
| 284 | int port = grpc_pick_unused_port_or_die(); |
| 285 | std::ostringstream proxyaddr; |
| 286 | proxyaddr << "localhost:" << port; |
| 287 | ServerBuilder builder; |
| 288 | builder.AddListeningPort(proxyaddr.str(), InsecureServerCredentials()); |
| 289 | builder.RegisterService(proxy_service_.get()); |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame^] | 290 | builder.SetSyncServerSettings(sync_server_settings_); |
| 291 | |
Craig Tiller | 2c3be1d | 2015-08-05 15:59:27 -0700 | [diff] [blame] | 292 | proxy_server_ = builder.BuildAndStart(); |
| 293 | |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 294 | channel_ = CreateChannel(proxyaddr.str(), InsecureChannelCredentials()); |
Craig Tiller | 2c3be1d | 2015-08-05 15:59:27 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 297 | stub_ = grpc::testing::EchoTestService::NewStub(channel_); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 300 | bool is_server_started_; |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 301 | std::shared_ptr<Channel> channel_; |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 302 | std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 303 | std::unique_ptr<Server> server_; |
Craig Tiller | 2c3be1d | 2015-08-05 15:59:27 -0700 | [diff] [blame] | 304 | std::unique_ptr<Server> proxy_server_; |
| 305 | std::unique_ptr<Proxy> proxy_service_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 306 | std::ostringstream server_address_; |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 307 | const int kMaxMessageSize_; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 308 | TestServiceImpl service_; |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 309 | TestServiceImpl special_service_; |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 310 | TestServiceImplDupPkg dup_pkg_service_; |
yang-g | d59ad7e | 2016-02-10 12:42:53 -0800 | [diff] [blame] | 311 | grpc::string user_agent_prefix_; |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame^] | 312 | ServerBuilder::SyncServerSettings sync_server_settings_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 313 | }; |
| 314 | |
yang-g | f8174ea | 2016-02-01 00:09:13 -0800 | [diff] [blame] | 315 | static void SendRpc(grpc::testing::EchoTestService::Stub* stub, int num_rpcs, |
| 316 | bool with_binary_metadata) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 317 | EchoRequest request; |
| 318 | EchoResponse response; |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 319 | request.set_message("Hello hello hello hello"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 320 | |
| 321 | for (int i = 0; i < num_rpcs; ++i) { |
| 322 | ClientContext context; |
yang-g | f8174ea | 2016-02-01 00:09:13 -0800 | [diff] [blame] | 323 | if (with_binary_metadata) { |
| 324 | char bytes[8] = {'\0', '\1', '\2', '\3', '\4', '\5', '\6', (char)i}; |
| 325 | context.AddMetadata("custom-bin", grpc::string(bytes, 8)); |
| 326 | } |
Craig Tiller | bf6abee | 2015-07-19 22:31:04 -0700 | [diff] [blame] | 327 | context.set_compression_algorithm(GRPC_COMPRESS_GZIP); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 328 | Status s = stub->Echo(&context, request, &response); |
| 329 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 330 | EXPECT_TRUE(s.ok()); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 331 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 334 | // This class is for testing scenarios where RPCs are cancelled on the server |
| 335 | // by calling ServerContext::TryCancel() |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 336 | class End2endServerTryCancelTest : public End2endTest { |
| 337 | protected: |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 338 | // Helper for testing client-streaming RPCs which are cancelled on the server. |
| 339 | // Depending on the value of server_try_cancel parameter, this will test one |
| 340 | // of the following three scenarios: |
| 341 | // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading |
| 342 | // any messages from the client |
| 343 | // |
| 344 | // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading |
| 345 | // messages from the client |
| 346 | // |
| 347 | // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all |
| 348 | // the messages from the client |
| 349 | // |
| 350 | // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL. |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 351 | void TestRequestStreamServerCancel( |
| 352 | ServerTryCancelRequestPhase server_try_cancel, int num_msgs_to_send) { |
| 353 | ResetStub(); |
| 354 | EchoRequest request; |
| 355 | EchoResponse response; |
| 356 | ClientContext context; |
| 357 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 358 | // Send server_try_cancel value in the client metadata |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 359 | context.AddMetadata(kServerTryCancelRequest, |
Vijay Pai | a63271c | 2016-06-15 12:56:38 -0700 | [diff] [blame] | 360 | grpc::to_string(server_try_cancel)); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 361 | |
| 362 | auto stream = stub_->RequestStream(&context, &response); |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 363 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 364 | int num_msgs_sent = 0; |
| 365 | while (num_msgs_sent < num_msgs_to_send) { |
| 366 | request.set_message("hello"); |
| 367 | if (!stream->Write(request)) { |
| 368 | break; |
| 369 | } |
| 370 | num_msgs_sent++; |
| 371 | } |
| 372 | gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent); |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 373 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 374 | stream->WritesDone(); |
| 375 | Status s = stream->Finish(); |
| 376 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 377 | // At this point, we know for sure that RPC was cancelled by the server |
| 378 | // since we passed server_try_cancel value in the metadata. Depending on the |
| 379 | // value of server_try_cancel, the RPC might have been cancelled by the |
| 380 | // server at different stages. The following validates our expectations of |
| 381 | // number of messages sent in various cancellation scenarios: |
| 382 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 383 | switch (server_try_cancel) { |
| 384 | case CANCEL_BEFORE_PROCESSING: |
| 385 | case CANCEL_DURING_PROCESSING: |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 386 | // If the RPC is cancelled by server before / during messages from the |
| 387 | // client, it means that the client most likely did not get a chance to |
| 388 | // send all the messages it wanted to send. i.e num_msgs_sent <= |
| 389 | // num_msgs_to_send |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 390 | EXPECT_LE(num_msgs_sent, num_msgs_to_send); |
| 391 | break; |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 392 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 393 | case CANCEL_AFTER_PROCESSING: |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 394 | // If the RPC was cancelled after all messages were read by the server, |
| 395 | // the client did get a chance to send all its messages |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 396 | EXPECT_EQ(num_msgs_sent, num_msgs_to_send); |
| 397 | break; |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 398 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 399 | default: |
| 400 | gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d", |
| 401 | server_try_cancel); |
| 402 | EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL && |
| 403 | server_try_cancel <= CANCEL_AFTER_PROCESSING); |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | EXPECT_FALSE(s.ok()); |
| 408 | EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 409 | } |
| 410 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 411 | // Helper for testing server-streaming RPCs which are cancelled on the server. |
| 412 | // Depending on the value of server_try_cancel parameter, this will test one |
| 413 | // of the following three scenarios: |
| 414 | // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before writing |
| 415 | // any messages to the client |
| 416 | // |
| 417 | // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while writing |
| 418 | // messages to the client |
| 419 | // |
| 420 | // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after writing all |
| 421 | // the messages to the client |
| 422 | // |
| 423 | // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL. |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 424 | void TestResponseStreamServerCancel( |
| 425 | ServerTryCancelRequestPhase server_try_cancel) { |
| 426 | ResetStub(); |
| 427 | EchoRequest request; |
| 428 | EchoResponse response; |
| 429 | ClientContext context; |
| 430 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 431 | // Send server_try_cancel in the client metadata |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 432 | context.AddMetadata(kServerTryCancelRequest, |
Vijay Pai | a63271c | 2016-06-15 12:56:38 -0700 | [diff] [blame] | 433 | grpc::to_string(server_try_cancel)); |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 434 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 435 | request.set_message("hello"); |
| 436 | auto stream = stub_->ResponseStream(&context, request); |
| 437 | |
| 438 | int num_msgs_read = 0; |
| 439 | while (num_msgs_read < kNumResponseStreamsMsgs) { |
| 440 | if (!stream->Read(&response)) { |
| 441 | break; |
| 442 | } |
| 443 | EXPECT_EQ(response.message(), |
Vijay Pai | a63271c | 2016-06-15 12:56:38 -0700 | [diff] [blame] | 444 | request.message() + grpc::to_string(num_msgs_read)); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 445 | num_msgs_read++; |
| 446 | } |
| 447 | gpr_log(GPR_INFO, "Read %d messages", num_msgs_read); |
| 448 | |
| 449 | Status s = stream->Finish(); |
| 450 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 451 | // Depending on the value of server_try_cancel, the RPC might have been |
| 452 | // cancelled by the server at different stages. The following validates our |
| 453 | // expectations of number of messages read in various cancellation |
| 454 | // scenarios: |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 455 | switch (server_try_cancel) { |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 456 | case CANCEL_BEFORE_PROCESSING: |
| 457 | // Server cancelled before sending any messages. Which means the client |
| 458 | // wouldn't have read any |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 459 | EXPECT_EQ(num_msgs_read, 0); |
| 460 | break; |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 461 | |
| 462 | case CANCEL_DURING_PROCESSING: |
| 463 | // Server cancelled while writing messages. Client must have read less |
| 464 | // than or equal to the expected number of messages |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 465 | EXPECT_LE(num_msgs_read, kNumResponseStreamsMsgs); |
| 466 | break; |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 467 | |
| 468 | case CANCEL_AFTER_PROCESSING: |
Sree Kuchibhotla | 8d543e8 | 2016-02-29 18:22:25 -0800 | [diff] [blame] | 469 | // Even though the Server cancelled after writing all messages, the RPC |
| 470 | // may be cancelled before the Client got a chance to read all the |
| 471 | // messages. |
| 472 | EXPECT_LE(num_msgs_read, kNumResponseStreamsMsgs); |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 473 | break; |
| 474 | |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 475 | default: { |
| 476 | gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d", |
| 477 | server_try_cancel); |
| 478 | EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL && |
| 479 | server_try_cancel <= CANCEL_AFTER_PROCESSING); |
| 480 | break; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | EXPECT_FALSE(s.ok()); |
| 485 | EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 486 | } |
| 487 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 488 | // Helper for testing bidirectional-streaming RPCs which are cancelled on the |
| 489 | // server. Depending on the value of server_try_cancel parameter, this will |
| 490 | // test one of the following three scenarios: |
| 491 | // CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading/ |
| 492 | // writing any messages from/to the client |
| 493 | // |
| 494 | // CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading/ |
| 495 | // writing messages from/to the client |
| 496 | // |
| 497 | // CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading/writing |
| 498 | // all the messages from/to the client |
| 499 | // |
| 500 | // NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL. |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 501 | void TestBidiStreamServerCancel(ServerTryCancelRequestPhase server_try_cancel, |
| 502 | int num_messages) { |
| 503 | ResetStub(); |
| 504 | EchoRequest request; |
| 505 | EchoResponse response; |
| 506 | ClientContext context; |
| 507 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 508 | // Send server_try_cancel in the client metadata |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 509 | context.AddMetadata(kServerTryCancelRequest, |
Vijay Pai | a63271c | 2016-06-15 12:56:38 -0700 | [diff] [blame] | 510 | grpc::to_string(server_try_cancel)); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 511 | |
| 512 | auto stream = stub_->BidiStream(&context); |
| 513 | |
| 514 | int num_msgs_read = 0; |
| 515 | int num_msgs_sent = 0; |
| 516 | while (num_msgs_sent < num_messages) { |
Vijay Pai | a63271c | 2016-06-15 12:56:38 -0700 | [diff] [blame] | 517 | request.set_message("hello " + grpc::to_string(num_msgs_sent)); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 518 | if (!stream->Write(request)) { |
| 519 | break; |
| 520 | } |
| 521 | num_msgs_sent++; |
| 522 | |
| 523 | if (!stream->Read(&response)) { |
| 524 | break; |
| 525 | } |
| 526 | num_msgs_read++; |
| 527 | |
| 528 | EXPECT_EQ(response.message(), request.message()); |
| 529 | } |
| 530 | gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent); |
| 531 | gpr_log(GPR_INFO, "Read %d messages", num_msgs_read); |
| 532 | |
| 533 | stream->WritesDone(); |
| 534 | Status s = stream->Finish(); |
| 535 | |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 536 | // Depending on the value of server_try_cancel, the RPC might have been |
| 537 | // cancelled by the server at different stages. The following validates our |
| 538 | // expectations of number of messages read in various cancellation |
| 539 | // scenarios: |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 540 | switch (server_try_cancel) { |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 541 | case CANCEL_BEFORE_PROCESSING: |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 542 | EXPECT_EQ(num_msgs_read, 0); |
| 543 | break; |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 544 | |
| 545 | case CANCEL_DURING_PROCESSING: |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 546 | EXPECT_LE(num_msgs_sent, num_messages); |
| 547 | EXPECT_LE(num_msgs_read, num_msgs_sent); |
| 548 | break; |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 549 | |
| 550 | case CANCEL_AFTER_PROCESSING: |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 551 | EXPECT_EQ(num_msgs_sent, num_messages); |
Sree Kuchibhotla | 8d543e8 | 2016-02-29 18:22:25 -0800 | [diff] [blame] | 552 | |
| 553 | // The Server cancelled after reading the last message and after writing |
| 554 | // the message to the client. However, the RPC cancellation might have |
| 555 | // taken effect before the client actually read the response. |
| 556 | EXPECT_LE(num_msgs_read, num_msgs_sent); |
Sree Kuchibhotla | 0f242ac | 2016-01-29 18:12:19 -0800 | [diff] [blame] | 557 | break; |
| 558 | |
| 559 | default: |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 560 | gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d", |
| 561 | server_try_cancel); |
| 562 | EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL && |
| 563 | server_try_cancel <= CANCEL_AFTER_PROCESSING); |
| 564 | break; |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | EXPECT_FALSE(s.ok()); |
| 568 | EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 569 | } |
| 570 | }; |
| 571 | |
| 572 | TEST_P(End2endServerTryCancelTest, RequestEchoServerCancel) { |
| 573 | ResetStub(); |
| 574 | EchoRequest request; |
| 575 | EchoResponse response; |
| 576 | ClientContext context; |
| 577 | |
| 578 | context.AddMetadata(kServerTryCancelRequest, |
Vijay Pai | a63271c | 2016-06-15 12:56:38 -0700 | [diff] [blame] | 579 | grpc::to_string(CANCEL_BEFORE_PROCESSING)); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 580 | Status s = stub_->Echo(&context, request, &response); |
| 581 | EXPECT_FALSE(s.ok()); |
| 582 | EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 583 | } |
| 584 | |
| 585 | // Server to cancel before doing reading the request |
| 586 | TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelBeforeReads) { |
| 587 | TestRequestStreamServerCancel(CANCEL_BEFORE_PROCESSING, 1); |
| 588 | } |
| 589 | |
| 590 | // Server to cancel while reading a request from the stream in parallel |
| 591 | TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelDuringRead) { |
| 592 | TestRequestStreamServerCancel(CANCEL_DURING_PROCESSING, 10); |
| 593 | } |
| 594 | |
| 595 | // Server to cancel after reading all the requests but before returning to the |
| 596 | // client |
| 597 | TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelAfterReads) { |
| 598 | TestRequestStreamServerCancel(CANCEL_AFTER_PROCESSING, 4); |
| 599 | } |
| 600 | |
| 601 | // Server to cancel before sending any response messages |
| 602 | TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelBefore) { |
| 603 | TestResponseStreamServerCancel(CANCEL_BEFORE_PROCESSING); |
| 604 | } |
| 605 | |
| 606 | // Server to cancel while writing a response to the stream in parallel |
| 607 | TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelDuring) { |
| 608 | TestResponseStreamServerCancel(CANCEL_DURING_PROCESSING); |
| 609 | } |
| 610 | |
| 611 | // Server to cancel after writing all the respones to the stream but before |
| 612 | // returning to the client |
| 613 | TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelAfter) { |
| 614 | TestResponseStreamServerCancel(CANCEL_AFTER_PROCESSING); |
| 615 | } |
| 616 | |
| 617 | // Server to cancel before reading/writing any requests/responses on the stream |
| 618 | TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelBefore) { |
| 619 | TestBidiStreamServerCancel(CANCEL_BEFORE_PROCESSING, 2); |
| 620 | } |
| 621 | |
| 622 | // Server to cancel while reading/writing requests/responses on the stream in |
| 623 | // parallel |
| 624 | TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelDuring) { |
| 625 | TestBidiStreamServerCancel(CANCEL_DURING_PROCESSING, 10); |
| 626 | } |
| 627 | |
| 628 | // Server to cancel after reading/writing all requests/responses on the stream |
| 629 | // but before returning to the client |
| 630 | TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelAfter) { |
| 631 | TestBidiStreamServerCancel(CANCEL_AFTER_PROCESSING, 5); |
| 632 | } |
| 633 | |
yang-g | d59ad7e | 2016-02-10 12:42:53 -0800 | [diff] [blame] | 634 | TEST_P(End2endTest, SimpleRpcWithCustomeUserAgentPrefix) { |
| 635 | user_agent_prefix_ = "custom_prefix"; |
| 636 | ResetStub(); |
| 637 | EchoRequest request; |
| 638 | EchoResponse response; |
| 639 | request.set_message("Hello hello hello hello"); |
| 640 | request.mutable_param()->set_echo_metadata(true); |
| 641 | |
| 642 | ClientContext context; |
| 643 | Status s = stub_->Echo(&context, request, &response); |
| 644 | EXPECT_EQ(response.message(), request.message()); |
| 645 | EXPECT_TRUE(s.ok()); |
| 646 | const auto& trailing_metadata = context.GetServerTrailingMetadata(); |
| 647 | auto iter = trailing_metadata.find("user-agent"); |
| 648 | EXPECT_TRUE(iter != trailing_metadata.end()); |
| 649 | grpc::string expected_prefix = user_agent_prefix_ + " grpc-c++/"; |
| 650 | EXPECT_TRUE(iter->second.starts_with(expected_prefix)); |
| 651 | } |
| 652 | |
yang-g | f8174ea | 2016-02-01 00:09:13 -0800 | [diff] [blame] | 653 | TEST_P(End2endTest, MultipleRpcsWithVariedBinaryMetadataValue) { |
| 654 | ResetStub(); |
| 655 | std::vector<std::thread*> threads; |
| 656 | for (int i = 0; i < 10; ++i) { |
| 657 | threads.push_back(new std::thread(SendRpc, stub_.get(), 10, true)); |
| 658 | } |
| 659 | for (int i = 0; i < 10; ++i) { |
| 660 | threads[i]->join(); |
| 661 | delete threads[i]; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | TEST_P(End2endTest, MultipleRpcs) { |
| 666 | ResetStub(); |
| 667 | std::vector<std::thread*> threads; |
| 668 | for (int i = 0; i < 10; ++i) { |
| 669 | threads.push_back(new std::thread(SendRpc, stub_.get(), 10, false)); |
| 670 | } |
| 671 | for (int i = 0; i < 10; ++i) { |
| 672 | threads[i]->join(); |
| 673 | delete threads[i]; |
| 674 | } |
| 675 | } |
| 676 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 677 | TEST_P(End2endTest, RequestStreamOneRequest) { |
| 678 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 679 | EchoRequest request; |
| 680 | EchoResponse response; |
| 681 | ClientContext context; |
| 682 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 683 | auto stream = stub_->RequestStream(&context, &response); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 684 | request.set_message("hello"); |
| 685 | EXPECT_TRUE(stream->Write(request)); |
| 686 | stream->WritesDone(); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 687 | Status s = stream->Finish(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 688 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 689 | EXPECT_TRUE(s.ok()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 690 | } |
| 691 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 692 | TEST_P(End2endTest, RequestStreamTwoRequests) { |
| 693 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 694 | EchoRequest request; |
| 695 | EchoResponse response; |
| 696 | ClientContext context; |
| 697 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 698 | auto stream = stub_->RequestStream(&context, &response); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 699 | request.set_message("hello"); |
| 700 | EXPECT_TRUE(stream->Write(request)); |
| 701 | EXPECT_TRUE(stream->Write(request)); |
| 702 | stream->WritesDone(); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 703 | Status s = stream->Finish(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 704 | EXPECT_EQ(response.message(), "hellohello"); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 705 | EXPECT_TRUE(s.ok()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 706 | } |
| 707 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 708 | TEST_P(End2endTest, ResponseStream) { |
| 709 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 710 | EchoRequest request; |
| 711 | EchoResponse response; |
| 712 | ClientContext context; |
| 713 | request.set_message("hello"); |
| 714 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 715 | auto stream = stub_->ResponseStream(&context, request); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 716 | EXPECT_TRUE(stream->Read(&response)); |
| 717 | EXPECT_EQ(response.message(), request.message() + "0"); |
| 718 | EXPECT_TRUE(stream->Read(&response)); |
| 719 | EXPECT_EQ(response.message(), request.message() + "1"); |
| 720 | EXPECT_TRUE(stream->Read(&response)); |
| 721 | EXPECT_EQ(response.message(), request.message() + "2"); |
| 722 | EXPECT_FALSE(stream->Read(&response)); |
| 723 | |
Craig Tiller | 4d0fb5f | 2015-02-09 16:27:22 -0800 | [diff] [blame] | 724 | Status s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 725 | EXPECT_TRUE(s.ok()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 726 | } |
| 727 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 728 | TEST_P(End2endTest, BidiStream) { |
| 729 | ResetStub(); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 730 | EchoRequest request; |
| 731 | EchoResponse response; |
| 732 | ClientContext context; |
| 733 | grpc::string msg("hello"); |
| 734 | |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 735 | auto stream = stub_->BidiStream(&context); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 736 | |
| 737 | request.set_message(msg + "0"); |
| 738 | EXPECT_TRUE(stream->Write(request)); |
| 739 | EXPECT_TRUE(stream->Read(&response)); |
| 740 | EXPECT_EQ(response.message(), request.message()); |
| 741 | |
| 742 | request.set_message(msg + "1"); |
| 743 | EXPECT_TRUE(stream->Write(request)); |
| 744 | EXPECT_TRUE(stream->Read(&response)); |
| 745 | EXPECT_EQ(response.message(), request.message()); |
| 746 | |
| 747 | request.set_message(msg + "2"); |
| 748 | EXPECT_TRUE(stream->Write(request)); |
| 749 | EXPECT_TRUE(stream->Read(&response)); |
| 750 | EXPECT_EQ(response.message(), request.message()); |
| 751 | |
| 752 | stream->WritesDone(); |
| 753 | EXPECT_FALSE(stream->Read(&response)); |
Craig Tiller | ca9a637 | 2015-12-15 18:16:28 -0800 | [diff] [blame] | 754 | EXPECT_FALSE(stream->Read(&response)); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 755 | |
Craig Tiller | 4d0fb5f | 2015-02-09 16:27:22 -0800 | [diff] [blame] | 756 | Status s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 757 | EXPECT_TRUE(s.ok()); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | // Talk to the two services with the same name but different package names. |
| 761 | // The two stubs are created on the same channel. |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 762 | TEST_P(End2endTest, DiffPackageServices) { |
| 763 | ResetStub(); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 764 | EchoRequest request; |
| 765 | EchoResponse response; |
| 766 | request.set_message("Hello"); |
| 767 | |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 768 | ClientContext context; |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 769 | Status s = stub_->Echo(&context, request, &response); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 770 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 771 | EXPECT_TRUE(s.ok()); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 772 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 773 | std::unique_ptr<grpc::testing::duplicate::EchoTestService::Stub> dup_pkg_stub( |
| 774 | grpc::testing::duplicate::EchoTestService::NewStub(channel_)); |
yangg | 1456d15 | 2015-01-08 15:39:58 -0800 | [diff] [blame] | 775 | ClientContext context2; |
| 776 | s = dup_pkg_stub->Echo(&context2, request, &response); |
| 777 | EXPECT_EQ("no package", response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 778 | EXPECT_TRUE(s.ok()); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 781 | void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) { |
Craig Tiller | 20b5fe9 | 2015-07-06 10:43:50 -0700 | [diff] [blame] | 782 | gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
Craig Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 783 | gpr_time_from_micros(delay_us, GPR_TIMESPAN))); |
Yang Gao | 0c4b0dd | 2015-03-30 13:08:34 -0700 | [diff] [blame] | 784 | while (!service->signal_client()) { |
| 785 | } |
| 786 | context->TryCancel(); |
| 787 | } |
| 788 | |
yang-g | a89bf50 | 2015-11-17 14:19:17 -0800 | [diff] [blame] | 789 | TEST_P(End2endTest, CancelRpcBeforeStart) { |
| 790 | ResetStub(); |
| 791 | EchoRequest request; |
| 792 | EchoResponse response; |
| 793 | ClientContext context; |
| 794 | request.set_message("hello"); |
| 795 | context.TryCancel(); |
| 796 | Status s = stub_->Echo(&context, request, &response); |
| 797 | EXPECT_EQ("", response.message()); |
| 798 | EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 799 | } |
| 800 | |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 801 | // Client cancels request stream after sending two messages |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 802 | TEST_P(End2endTest, ClientCancelsRequestStream) { |
| 803 | ResetStub(); |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 804 | EchoRequest request; |
| 805 | EchoResponse response; |
| 806 | ClientContext context; |
| 807 | request.set_message("hello"); |
| 808 | |
| 809 | auto stream = stub_->RequestStream(&context, &response); |
| 810 | EXPECT_TRUE(stream->Write(request)); |
| 811 | EXPECT_TRUE(stream->Write(request)); |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 812 | |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 813 | context.TryCancel(); |
| 814 | |
| 815 | Status s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 816 | EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 817 | |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 818 | EXPECT_EQ(response.message(), ""); |
Abhishek Kumar | d774c5c | 2015-04-23 14:59:49 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Abhishek Kumar | e41d040 | 2015-04-17 14:12:33 -0700 | [diff] [blame] | 821 | // Client cancels server stream after sending some messages |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 822 | TEST_P(End2endTest, ClientCancelsResponseStream) { |
| 823 | ResetStub(); |
Abhishek Kumar | e41d040 | 2015-04-17 14:12:33 -0700 | [diff] [blame] | 824 | EchoRequest request; |
| 825 | EchoResponse response; |
| 826 | ClientContext context; |
| 827 | request.set_message("hello"); |
| 828 | |
| 829 | auto stream = stub_->ResponseStream(&context, request); |
| 830 | |
| 831 | EXPECT_TRUE(stream->Read(&response)); |
| 832 | EXPECT_EQ(response.message(), request.message() + "0"); |
| 833 | EXPECT_TRUE(stream->Read(&response)); |
| 834 | EXPECT_EQ(response.message(), request.message() + "1"); |
| 835 | |
| 836 | context.TryCancel(); |
| 837 | |
| 838 | // The cancellation races with responses, so there might be zero or |
| 839 | // one responses pending, read till failure |
| 840 | |
| 841 | if (stream->Read(&response)) { |
| 842 | EXPECT_EQ(response.message(), request.message() + "2"); |
| 843 | // Since we have cancelled, we expect the next attempt to read to fail |
| 844 | EXPECT_FALSE(stream->Read(&response)); |
| 845 | } |
| 846 | |
| 847 | Status s = stream->Finish(); |
Abhishek Kumar | 18298a7 | 2015-04-17 15:00:25 -0700 | [diff] [blame] | 848 | // The final status could be either of CANCELLED or OK depending on |
| 849 | // who won the race. |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 850 | EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code()); |
Abhishek Kumar | e41d040 | 2015-04-17 14:12:33 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Abhishek Kumar | 82a8331 | 2015-04-17 13:30:51 -0700 | [diff] [blame] | 853 | // Client cancels bidi stream after sending some messages |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 854 | TEST_P(End2endTest, ClientCancelsBidi) { |
| 855 | ResetStub(); |
Abhishek Kumar | 82a8331 | 2015-04-17 13:30:51 -0700 | [diff] [blame] | 856 | EchoRequest request; |
| 857 | EchoResponse response; |
| 858 | ClientContext context; |
| 859 | grpc::string msg("hello"); |
| 860 | |
| 861 | auto stream = stub_->BidiStream(&context); |
| 862 | |
| 863 | request.set_message(msg + "0"); |
| 864 | EXPECT_TRUE(stream->Write(request)); |
| 865 | EXPECT_TRUE(stream->Read(&response)); |
| 866 | EXPECT_EQ(response.message(), request.message()); |
| 867 | |
| 868 | request.set_message(msg + "1"); |
| 869 | EXPECT_TRUE(stream->Write(request)); |
| 870 | |
| 871 | context.TryCancel(); |
| 872 | |
| 873 | // The cancellation races with responses, so there might be zero or |
| 874 | // one responses pending, read till failure |
| 875 | |
| 876 | if (stream->Read(&response)) { |
| 877 | EXPECT_EQ(response.message(), request.message()); |
| 878 | // Since we have cancelled, we expect the next attempt to read to fail |
| 879 | EXPECT_FALSE(stream->Read(&response)); |
| 880 | } |
| 881 | |
| 882 | Status s = stream->Finish(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 883 | EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
Abhishek Kumar | 82a8331 | 2015-04-17 13:30:51 -0700 | [diff] [blame] | 884 | } |
| 885 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 886 | TEST_P(End2endTest, RpcMaxMessageSize) { |
| 887 | ResetStub(); |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 888 | EchoRequest request; |
| 889 | EchoResponse response; |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 890 | request.set_message(string(kMaxMessageSize_ * 2, 'a')); |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 891 | |
| 892 | ClientContext context; |
| 893 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 894 | EXPECT_FALSE(s.ok()); |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 895 | } |
Abhishek Kumar | 82a8331 | 2015-04-17 13:30:51 -0700 | [diff] [blame] | 896 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 897 | // Client sends 20 requests and the server returns CANCELLED status after |
| 898 | // reading 10 requests. |
| 899 | TEST_P(End2endTest, RequestStreamServerEarlyCancelTest) { |
| 900 | ResetStub(); |
| 901 | EchoRequest request; |
| 902 | EchoResponse response; |
| 903 | ClientContext context; |
| 904 | |
| 905 | context.AddMetadata(kServerCancelAfterReads, "10"); |
| 906 | auto stream = stub_->RequestStream(&context, &response); |
| 907 | request.set_message("hello"); |
| 908 | int send_messages = 20; |
yang-g | c046103 | 2015-10-02 16:22:43 -0700 | [diff] [blame] | 909 | while (send_messages > 10) { |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 910 | EXPECT_TRUE(stream->Write(request)); |
| 911 | send_messages--; |
| 912 | } |
yang-g | c046103 | 2015-10-02 16:22:43 -0700 | [diff] [blame] | 913 | while (send_messages > 0) { |
| 914 | stream->Write(request); |
| 915 | send_messages--; |
| 916 | } |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 917 | stream->WritesDone(); |
| 918 | Status s = stream->Finish(); |
| 919 | EXPECT_EQ(s.error_code(), StatusCode::CANCELLED); |
| 920 | } |
| 921 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 922 | void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream, |
| 923 | gpr_event* ev) { |
| 924 | EchoResponse resp; |
| 925 | gpr_event_set(ev, (void*)1); |
| 926 | while (stream->Read(&resp)) { |
| 927 | gpr_log(GPR_INFO, "Read message"); |
| 928 | } |
| 929 | } |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 930 | |
| 931 | // Run a Read and a WritesDone simultaneously. |
| 932 | TEST_P(End2endTest, SimultaneousReadWritesDone) { |
| 933 | ResetStub(); |
| 934 | ClientContext context; |
| 935 | gpr_event ev; |
| 936 | gpr_event_init(&ev); |
| 937 | auto stream = stub_->BidiStream(&context); |
| 938 | std::thread reader_thread(ReaderThreadFunc, stream.get(), &ev); |
| 939 | gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME)); |
| 940 | stream->WritesDone(); |
Vijay Pai | bdfec2c | 2016-02-25 11:51:21 -0800 | [diff] [blame] | 941 | reader_thread.join(); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 942 | Status s = stream->Finish(); |
| 943 | EXPECT_TRUE(s.ok()); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | TEST_P(End2endTest, ChannelState) { |
| 947 | ResetStub(); |
| 948 | // Start IDLE |
| 949 | EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(false)); |
| 950 | |
| 951 | // Did not ask to connect, no state change. |
| 952 | CompletionQueue cq; |
| 953 | std::chrono::system_clock::time_point deadline = |
| 954 | std::chrono::system_clock::now() + std::chrono::milliseconds(10); |
| 955 | channel_->NotifyOnStateChange(GRPC_CHANNEL_IDLE, deadline, &cq, NULL); |
| 956 | void* tag; |
| 957 | bool ok = true; |
| 958 | cq.Next(&tag, &ok); |
| 959 | EXPECT_FALSE(ok); |
| 960 | |
| 961 | EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true)); |
| 962 | EXPECT_TRUE(channel_->WaitForStateChange(GRPC_CHANNEL_IDLE, |
| 963 | gpr_inf_future(GPR_CLOCK_REALTIME))); |
yang-g | 0d55750 | 2015-10-01 11:30:12 -0700 | [diff] [blame] | 964 | auto state = channel_->GetState(false); |
| 965 | EXPECT_TRUE(state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_READY); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | // Takes 10s. |
| 969 | TEST_P(End2endTest, ChannelStateTimeout) { |
yang-g | 17197dd | 2016-02-19 00:04:22 -0800 | [diff] [blame] | 970 | if (GetParam().credentials_type != kInsecureCredentialsType) { |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 971 | return; |
| 972 | } |
| 973 | int port = grpc_pick_unused_port_or_die(); |
| 974 | std::ostringstream server_address; |
| 975 | server_address << "127.0.0.1:" << port; |
| 976 | // Channel to non-existing server |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 977 | auto channel = |
| 978 | CreateChannel(server_address.str(), InsecureChannelCredentials()); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 979 | // Start IDLE |
| 980 | EXPECT_EQ(GRPC_CHANNEL_IDLE, channel->GetState(true)); |
| 981 | |
| 982 | auto state = GRPC_CHANNEL_IDLE; |
| 983 | for (int i = 0; i < 10; i++) { |
| 984 | channel->WaitForStateChange( |
| 985 | state, std::chrono::system_clock::now() + std::chrono::seconds(1)); |
| 986 | state = channel->GetState(false); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | // Talking to a non-existing service. |
| 991 | TEST_P(End2endTest, NonExistingService) { |
| 992 | ResetChannel(); |
murgatroid99 | 7c5befd | 2016-09-01 17:09:47 -0700 | [diff] [blame] | 993 | std::unique_ptr<grpc::testing::UnimplementedEchoService::Stub> stub; |
| 994 | stub = grpc::testing::UnimplementedEchoService::NewStub(channel_); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 995 | |
| 996 | EchoRequest request; |
| 997 | EchoResponse response; |
| 998 | request.set_message("Hello"); |
| 999 | |
| 1000 | ClientContext context; |
| 1001 | Status s = stub->Unimplemented(&context, request, &response); |
| 1002 | EXPECT_EQ(StatusCode::UNIMPLEMENTED, s.error_code()); |
| 1003 | EXPECT_EQ("", s.error_message()); |
| 1004 | } |
| 1005 | |
yang-g | 4c07008 | 2016-05-05 23:27:13 -0700 | [diff] [blame] | 1006 | // Ask the server to send back a serialized proto in trailer. |
| 1007 | // This is an example of setting error details. |
| 1008 | TEST_P(End2endTest, BinaryTrailerTest) { |
| 1009 | ResetStub(); |
| 1010 | EchoRequest request; |
| 1011 | EchoResponse response; |
| 1012 | ClientContext context; |
| 1013 | |
| 1014 | request.mutable_param()->set_echo_metadata(true); |
| 1015 | DebugInfo* info = request.mutable_param()->mutable_debug_info(); |
| 1016 | info->add_stack_entries("stack_entry_1"); |
| 1017 | info->add_stack_entries("stack_entry_2"); |
| 1018 | info->add_stack_entries("stack_entry_3"); |
| 1019 | info->set_detail("detailed debug info"); |
| 1020 | grpc::string expected_string = info->SerializeAsString(); |
| 1021 | request.set_message("Hello"); |
| 1022 | |
| 1023 | Status s = stub_->Echo(&context, request, &response); |
| 1024 | EXPECT_FALSE(s.ok()); |
| 1025 | auto trailers = context.GetServerTrailingMetadata(); |
yang-g | 39e71c3 | 2016-05-10 10:21:41 -0700 | [diff] [blame] | 1026 | EXPECT_EQ(1u, trailers.count(kDebugInfoTrailerKey)); |
yang-g | 4c07008 | 2016-05-05 23:27:13 -0700 | [diff] [blame] | 1027 | auto iter = trailers.find(kDebugInfoTrailerKey); |
| 1028 | EXPECT_EQ(expected_string, iter->second); |
yang-g | 080528a | 2016-05-06 13:12:00 -0700 | [diff] [blame] | 1029 | // Parse the returned trailer into a DebugInfo proto. |
| 1030 | DebugInfo returned_info; |
| 1031 | EXPECT_TRUE(returned_info.ParseFromString(ToString(iter->second))); |
yang-g | 4c07008 | 2016-05-05 23:27:13 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1034 | ////////////////////////////////////////////////////////////////////////// |
| 1035 | // Test with and without a proxy. |
| 1036 | class ProxyEnd2endTest : public End2endTest { |
| 1037 | protected: |
| 1038 | }; |
| 1039 | |
| 1040 | TEST_P(ProxyEnd2endTest, SimpleRpc) { |
| 1041 | ResetStub(); |
yang-g | f8174ea | 2016-02-01 00:09:13 -0800 | [diff] [blame] | 1042 | SendRpc(stub_.get(), 1, false); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
yang-g | 000aa45 | 2016-06-07 13:08:39 -0700 | [diff] [blame] | 1045 | TEST_P(ProxyEnd2endTest, SimpleRpcWithEmptyMessages) { |
| 1046 | ResetStub(); |
| 1047 | EchoRequest request; |
| 1048 | EchoResponse response; |
| 1049 | |
| 1050 | ClientContext context; |
| 1051 | Status s = stub_->Echo(&context, request, &response); |
| 1052 | EXPECT_TRUE(s.ok()); |
| 1053 | } |
| 1054 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1055 | TEST_P(ProxyEnd2endTest, MultipleRpcs) { |
| 1056 | ResetStub(); |
| 1057 | std::vector<std::thread*> threads; |
| 1058 | for (int i = 0; i < 10; ++i) { |
yang-g | f8174ea | 2016-02-01 00:09:13 -0800 | [diff] [blame] | 1059 | threads.push_back(new std::thread(SendRpc, stub_.get(), 10, false)); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1060 | } |
| 1061 | for (int i = 0; i < 10; ++i) { |
| 1062 | threads[i]->join(); |
| 1063 | delete threads[i]; |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | // Set a 10us deadline and make sure proper error is returned. |
| 1068 | TEST_P(ProxyEnd2endTest, RpcDeadlineExpires) { |
| 1069 | ResetStub(); |
| 1070 | EchoRequest request; |
| 1071 | EchoResponse response; |
| 1072 | request.set_message("Hello"); |
Craig Tiller | b0f275e | 2016-01-27 10:45:50 -0800 | [diff] [blame] | 1073 | request.mutable_param()->set_skip_cancelled_check(true); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1074 | |
| 1075 | ClientContext context; |
| 1076 | std::chrono::system_clock::time_point deadline = |
| 1077 | std::chrono::system_clock::now() + std::chrono::microseconds(10); |
| 1078 | context.set_deadline(deadline); |
| 1079 | Status s = stub_->Echo(&context, request, &response); |
| 1080 | EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code()); |
| 1081 | } |
| 1082 | |
| 1083 | // Set a long but finite deadline. |
| 1084 | TEST_P(ProxyEnd2endTest, RpcLongDeadline) { |
| 1085 | ResetStub(); |
| 1086 | EchoRequest request; |
| 1087 | EchoResponse response; |
| 1088 | request.set_message("Hello"); |
| 1089 | |
| 1090 | ClientContext context; |
| 1091 | std::chrono::system_clock::time_point deadline = |
| 1092 | std::chrono::system_clock::now() + std::chrono::hours(1); |
| 1093 | context.set_deadline(deadline); |
| 1094 | Status s = stub_->Echo(&context, request, &response); |
| 1095 | EXPECT_EQ(response.message(), request.message()); |
| 1096 | EXPECT_TRUE(s.ok()); |
| 1097 | } |
| 1098 | |
| 1099 | // Ask server to echo back the deadline it sees. |
| 1100 | TEST_P(ProxyEnd2endTest, EchoDeadline) { |
| 1101 | ResetStub(); |
| 1102 | EchoRequest request; |
| 1103 | EchoResponse response; |
| 1104 | request.set_message("Hello"); |
| 1105 | request.mutable_param()->set_echo_deadline(true); |
| 1106 | |
| 1107 | ClientContext context; |
| 1108 | std::chrono::system_clock::time_point deadline = |
| 1109 | std::chrono::system_clock::now() + std::chrono::seconds(100); |
| 1110 | context.set_deadline(deadline); |
| 1111 | Status s = stub_->Echo(&context, request, &response); |
| 1112 | EXPECT_EQ(response.message(), request.message()); |
| 1113 | EXPECT_TRUE(s.ok()); |
| 1114 | gpr_timespec sent_deadline; |
| 1115 | Timepoint2Timespec(deadline, &sent_deadline); |
| 1116 | // Allow 1 second error. |
| 1117 | EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1); |
| 1118 | EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1); |
| 1119 | } |
| 1120 | |
| 1121 | // Ask server to echo back the deadline it sees. The rpc has no deadline. |
| 1122 | TEST_P(ProxyEnd2endTest, EchoDeadlineForNoDeadlineRpc) { |
| 1123 | ResetStub(); |
| 1124 | EchoRequest request; |
| 1125 | EchoResponse response; |
| 1126 | request.set_message("Hello"); |
| 1127 | request.mutable_param()->set_echo_deadline(true); |
| 1128 | |
| 1129 | ClientContext context; |
| 1130 | Status s = stub_->Echo(&context, request, &response); |
| 1131 | EXPECT_EQ(response.message(), request.message()); |
| 1132 | EXPECT_TRUE(s.ok()); |
| 1133 | EXPECT_EQ(response.param().request_deadline(), |
| 1134 | gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec); |
| 1135 | } |
| 1136 | |
| 1137 | TEST_P(ProxyEnd2endTest, UnimplementedRpc) { |
| 1138 | ResetStub(); |
| 1139 | EchoRequest request; |
| 1140 | EchoResponse response; |
| 1141 | request.set_message("Hello"); |
| 1142 | |
| 1143 | ClientContext context; |
| 1144 | Status s = stub_->Unimplemented(&context, request, &response); |
| 1145 | EXPECT_FALSE(s.ok()); |
| 1146 | EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED); |
| 1147 | EXPECT_EQ(s.error_message(), ""); |
| 1148 | EXPECT_EQ(response.message(), ""); |
| 1149 | } |
| 1150 | |
| 1151 | // Client cancels rpc after 10ms |
| 1152 | TEST_P(ProxyEnd2endTest, ClientCancelsRpc) { |
| 1153 | ResetStub(); |
| 1154 | EchoRequest request; |
| 1155 | EchoResponse response; |
| 1156 | request.set_message("Hello"); |
| 1157 | const int kCancelDelayUs = 10 * 1000; |
| 1158 | request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs); |
| 1159 | |
| 1160 | ClientContext context; |
| 1161 | std::thread cancel_thread(CancelRpc, &context, kCancelDelayUs, &service_); |
| 1162 | Status s = stub_->Echo(&context, request, &response); |
| 1163 | cancel_thread.join(); |
| 1164 | EXPECT_EQ(StatusCode::CANCELLED, s.error_code()); |
| 1165 | EXPECT_EQ(s.error_message(), "Cancelled"); |
| 1166 | } |
| 1167 | |
| 1168 | // Server cancels rpc after 1ms |
| 1169 | TEST_P(ProxyEnd2endTest, ServerCancelsRpc) { |
| 1170 | ResetStub(); |
| 1171 | EchoRequest request; |
| 1172 | EchoResponse response; |
| 1173 | request.set_message("Hello"); |
| 1174 | request.mutable_param()->set_server_cancel_after_us(1000); |
| 1175 | |
| 1176 | ClientContext context; |
| 1177 | Status s = stub_->Echo(&context, request, &response); |
| 1178 | EXPECT_EQ(StatusCode::CANCELLED, s.error_code()); |
| 1179 | EXPECT_TRUE(s.error_message().empty()); |
| 1180 | } |
| 1181 | |
| 1182 | // Make the response larger than the flow control window. |
| 1183 | TEST_P(ProxyEnd2endTest, HugeResponse) { |
| 1184 | ResetStub(); |
| 1185 | EchoRequest request; |
| 1186 | EchoResponse response; |
| 1187 | request.set_message("huge response"); |
| 1188 | const size_t kResponseSize = 1024 * (1024 + 10); |
| 1189 | request.mutable_param()->set_response_message_length(kResponseSize); |
| 1190 | |
| 1191 | ClientContext context; |
Craig Tiller | 6c8619b | 2016-07-07 10:41:48 -0700 | [diff] [blame] | 1192 | std::chrono::system_clock::time_point deadline = |
| 1193 | std::chrono::system_clock::now() + std::chrono::seconds(20); |
| 1194 | context.set_deadline(deadline); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1195 | Status s = stub_->Echo(&context, request, &response); |
| 1196 | EXPECT_EQ(kResponseSize, response.message().size()); |
| 1197 | EXPECT_TRUE(s.ok()); |
| 1198 | } |
| 1199 | |
| 1200 | TEST_P(ProxyEnd2endTest, Peer) { |
| 1201 | ResetStub(); |
| 1202 | EchoRequest request; |
| 1203 | EchoResponse response; |
| 1204 | request.set_message("hello"); |
| 1205 | request.mutable_param()->set_echo_peer(true); |
| 1206 | |
| 1207 | ClientContext context; |
| 1208 | Status s = stub_->Echo(&context, request, &response); |
| 1209 | EXPECT_EQ(response.message(), request.message()); |
| 1210 | EXPECT_TRUE(s.ok()); |
| 1211 | EXPECT_TRUE(CheckIsLocalhost(response.param().peer())); |
| 1212 | EXPECT_TRUE(CheckIsLocalhost(context.peer())); |
| 1213 | } |
| 1214 | |
| 1215 | ////////////////////////////////////////////////////////////////////////// |
| 1216 | class SecureEnd2endTest : public End2endTest { |
| 1217 | protected: |
| 1218 | SecureEnd2endTest() { |
| 1219 | GPR_ASSERT(!GetParam().use_proxy); |
yang-g | 17197dd | 2016-02-19 00:04:22 -0800 | [diff] [blame] | 1220 | GPR_ASSERT(GetParam().credentials_type != kInsecureCredentialsType); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1221 | } |
| 1222 | }; |
| 1223 | |
| 1224 | TEST_P(SecureEnd2endTest, SimpleRpcWithHost) { |
| 1225 | ResetStub(); |
| 1226 | |
| 1227 | EchoRequest request; |
| 1228 | EchoResponse response; |
| 1229 | request.set_message("Hello"); |
| 1230 | |
| 1231 | ClientContext context; |
| 1232 | context.set_authority("foo.test.youtube.com"); |
| 1233 | Status s = stub_->Echo(&context, request, &response); |
| 1234 | EXPECT_EQ(response.message(), request.message()); |
| 1235 | EXPECT_TRUE(response.has_param()); |
| 1236 | EXPECT_EQ("special", response.param().host()); |
| 1237 | EXPECT_TRUE(s.ok()); |
| 1238 | } |
| 1239 | |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 1240 | bool MetadataContains( |
| 1241 | const std::multimap<grpc::string_ref, grpc::string_ref>& metadata, |
| 1242 | const grpc::string& key, const grpc::string& value) { |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 1243 | int count = 0; |
| 1244 | |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 1245 | for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator iter = |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 1246 | metadata.begin(); |
| 1247 | iter != metadata.end(); ++iter) { |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 1248 | if (ToString(iter->first) == key && ToString(iter->second) == value) { |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 1249 | count++; |
| 1250 | } |
| 1251 | } |
| 1252 | return count == 1; |
| 1253 | } |
| 1254 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1255 | TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorSuccess) { |
| 1256 | auto* processor = new TestAuthMetadataProcessor(true); |
| 1257 | StartServer(std::shared_ptr<AuthMetadataProcessor>(processor)); |
| 1258 | ResetStub(); |
| 1259 | EchoRequest request; |
| 1260 | EchoResponse response; |
| 1261 | ClientContext context; |
| 1262 | context.set_credentials(processor->GetCompatibleClientCreds()); |
| 1263 | request.set_message("Hello"); |
| 1264 | request.mutable_param()->set_echo_metadata(true); |
| 1265 | request.mutable_param()->set_expected_client_identity( |
| 1266 | TestAuthMetadataProcessor::kGoodGuy); |
Dan Born | f2f7d57 | 2016-03-03 17:26:12 -0800 | [diff] [blame] | 1267 | request.mutable_param()->set_expected_transport_security_type( |
| 1268 | GetParam().credentials_type); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1269 | |
| 1270 | Status s = stub_->Echo(&context, request, &response); |
| 1271 | EXPECT_EQ(request.message(), response.message()); |
| 1272 | EXPECT_TRUE(s.ok()); |
| 1273 | |
| 1274 | // Metadata should have been consumed by the processor. |
| 1275 | EXPECT_FALSE(MetadataContains( |
| 1276 | context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY, |
| 1277 | grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy)); |
| 1278 | } |
| 1279 | |
| 1280 | TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorFailure) { |
| 1281 | auto* processor = new TestAuthMetadataProcessor(true); |
| 1282 | StartServer(std::shared_ptr<AuthMetadataProcessor>(processor)); |
| 1283 | ResetStub(); |
| 1284 | EchoRequest request; |
| 1285 | EchoResponse response; |
| 1286 | ClientContext context; |
| 1287 | context.set_credentials(processor->GetIncompatibleClientCreds()); |
| 1288 | request.set_message("Hello"); |
| 1289 | |
| 1290 | Status s = stub_->Echo(&context, request, &response); |
| 1291 | EXPECT_FALSE(s.ok()); |
| 1292 | EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED); |
| 1293 | } |
| 1294 | TEST_P(SecureEnd2endTest, SetPerCallCredentials) { |
| 1295 | ResetStub(); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 1296 | EchoRequest request; |
| 1297 | EchoResponse response; |
| 1298 | ClientContext context; |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 1299 | std::shared_ptr<CallCredentials> creds = |
Julien Boeuf | 510a920 | 2015-08-25 21:51:07 -0700 | [diff] [blame] | 1300 | GoogleIAMCredentials("fake_token", "fake_selector"); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 1301 | context.set_credentials(creds); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 1302 | request.set_message("Hello"); |
| 1303 | request.mutable_param()->set_echo_metadata(true); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 1304 | |
| 1305 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 1306 | EXPECT_EQ(request.message(), response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 1307 | EXPECT_TRUE(s.ok()); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 1308 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1309 | GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 1310 | "fake_token")); |
| 1311 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1312 | GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 1313 | "fake_selector")); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1316 | TEST_P(SecureEnd2endTest, OverridePerCallCredentials) { |
| 1317 | ResetStub(); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 1318 | EchoRequest request; |
| 1319 | EchoResponse response; |
| 1320 | ClientContext context; |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 1321 | std::shared_ptr<CallCredentials> creds1 = |
Julien Boeuf | 510a920 | 2015-08-25 21:51:07 -0700 | [diff] [blame] | 1322 | GoogleIAMCredentials("fake_token1", "fake_selector1"); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 1323 | context.set_credentials(creds1); |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 1324 | std::shared_ptr<CallCredentials> creds2 = |
Julien Boeuf | 510a920 | 2015-08-25 21:51:07 -0700 | [diff] [blame] | 1325 | GoogleIAMCredentials("fake_token2", "fake_selector2"); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 1326 | context.set_credentials(creds2); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 1327 | request.set_message("Hello"); |
| 1328 | request.mutable_param()->set_echo_metadata(true); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 1329 | |
| 1330 | Status s = stub_->Echo(&context, request, &response); |
Yang Gao | 26a4912 | 2015-05-15 17:02:56 -0700 | [diff] [blame] | 1331 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1332 | GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 1333 | "fake_token2")); |
| 1334 | EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1335 | GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 1336 | "fake_selector2")); |
Yang Gao | b57f72d | 2015-05-17 21:54:54 -0700 | [diff] [blame] | 1337 | EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1338 | GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 1339 | "fake_token1")); |
| 1340 | EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1341 | GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 1342 | "fake_selector1")); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 1343 | EXPECT_EQ(request.message(), response.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 1344 | EXPECT_TRUE(s.ok()); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
yang-g | c580af3 | 2016-09-15 15:28:38 -0700 | [diff] [blame] | 1347 | TEST_P(SecureEnd2endTest, AuthMetadataPluginKeyFailure) { |
| 1348 | ResetStub(); |
| 1349 | EchoRequest request; |
| 1350 | EchoResponse response; |
| 1351 | ClientContext context; |
| 1352 | context.set_credentials( |
| 1353 | MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>( |
| 1354 | new TestMetadataCredentialsPlugin( |
| 1355 | TestMetadataCredentialsPlugin::kBadMetadataKey, |
| 1356 | "Does not matter, will fail the key is invalid.", false, true)))); |
| 1357 | request.set_message("Hello"); |
| 1358 | |
| 1359 | Status s = stub_->Echo(&context, request, &response); |
| 1360 | EXPECT_FALSE(s.ok()); |
| 1361 | EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED); |
| 1362 | } |
| 1363 | |
yang-g | 4b4571a | 2016-09-15 23:01:09 -0700 | [diff] [blame] | 1364 | TEST_P(SecureEnd2endTest, AuthMetadataPluginValueFailure) { |
| 1365 | ResetStub(); |
| 1366 | EchoRequest request; |
| 1367 | EchoResponse response; |
| 1368 | ClientContext context; |
| 1369 | context.set_credentials( |
| 1370 | MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>( |
| 1371 | new TestMetadataCredentialsPlugin( |
| 1372 | TestMetadataCredentialsPlugin::kGoodMetadataKey, |
yang-g | d5fba28 | 2016-09-16 10:29:16 -0700 | [diff] [blame] | 1373 | "With illegal \n value.", false, true)))); |
yang-g | 4b4571a | 2016-09-15 23:01:09 -0700 | [diff] [blame] | 1374 | request.set_message("Hello"); |
| 1375 | |
| 1376 | Status s = stub_->Echo(&context, request, &response); |
| 1377 | EXPECT_FALSE(s.ok()); |
| 1378 | EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED); |
| 1379 | } |
| 1380 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1381 | TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginFailure) { |
| 1382 | ResetStub(); |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 1383 | EchoRequest request; |
| 1384 | EchoResponse response; |
| 1385 | ClientContext context; |
| 1386 | context.set_credentials( |
| 1387 | MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>( |
| 1388 | new TestMetadataCredentialsPlugin( |
yang-g | c580af3 | 2016-09-15 15:28:38 -0700 | [diff] [blame] | 1389 | TestMetadataCredentialsPlugin::kGoodMetadataKey, |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 1390 | "Does not matter, will fail anyway (see 3rd param)", false, |
| 1391 | false)))); |
| 1392 | request.set_message("Hello"); |
| 1393 | |
| 1394 | Status s = stub_->Echo(&context, request, &response); |
| 1395 | EXPECT_FALSE(s.ok()); |
| 1396 | EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED); |
Julien Boeuf | 38c0cde | 2016-06-06 14:46:08 +0200 | [diff] [blame] | 1397 | EXPECT_EQ(s.error_message(), kTestCredsPluginErrorMsg); |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1400 | TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorSuccess) { |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 1401 | auto* processor = new TestAuthMetadataProcessor(false); |
| 1402 | StartServer(std::shared_ptr<AuthMetadataProcessor>(processor)); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1403 | ResetStub(); |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 1404 | EchoRequest request; |
| 1405 | EchoResponse response; |
| 1406 | ClientContext context; |
| 1407 | context.set_credentials(processor->GetCompatibleClientCreds()); |
| 1408 | request.set_message("Hello"); |
| 1409 | request.mutable_param()->set_echo_metadata(true); |
| 1410 | request.mutable_param()->set_expected_client_identity( |
| 1411 | TestAuthMetadataProcessor::kGoodGuy); |
Dan Born | f2f7d57 | 2016-03-03 17:26:12 -0800 | [diff] [blame] | 1412 | request.mutable_param()->set_expected_transport_security_type( |
| 1413 | GetParam().credentials_type); |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 1414 | |
| 1415 | Status s = stub_->Echo(&context, request, &response); |
| 1416 | EXPECT_EQ(request.message(), response.message()); |
| 1417 | EXPECT_TRUE(s.ok()); |
| 1418 | |
| 1419 | // Metadata should have been consumed by the processor. |
| 1420 | EXPECT_FALSE(MetadataContains( |
| 1421 | context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY, |
| 1422 | grpc::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy)); |
| 1423 | } |
| 1424 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1425 | TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorFailure) { |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 1426 | auto* processor = new TestAuthMetadataProcessor(false); |
| 1427 | StartServer(std::shared_ptr<AuthMetadataProcessor>(processor)); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1428 | ResetStub(); |
Julien Boeuf | 0c711ad | 2015-08-28 14:10:58 -0700 | [diff] [blame] | 1429 | EchoRequest request; |
| 1430 | EchoResponse response; |
| 1431 | ClientContext context; |
| 1432 | context.set_credentials(processor->GetIncompatibleClientCreds()); |
| 1433 | request.set_message("Hello"); |
| 1434 | |
| 1435 | Status s = stub_->Echo(&context, request, &response); |
| 1436 | EXPECT_FALSE(s.ok()); |
| 1437 | EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED); |
| 1438 | } |
| 1439 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1440 | TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginFailure) { |
| 1441 | ResetStub(); |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 1442 | EchoRequest request; |
| 1443 | EchoResponse response; |
| 1444 | ClientContext context; |
| 1445 | context.set_credentials( |
| 1446 | MetadataCredentialsFromPlugin(std::unique_ptr<MetadataCredentialsPlugin>( |
| 1447 | new TestMetadataCredentialsPlugin( |
yang-g | c580af3 | 2016-09-15 15:28:38 -0700 | [diff] [blame] | 1448 | TestMetadataCredentialsPlugin::kGoodMetadataKey, |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 1449 | "Does not matter, will fail anyway (see 3rd param)", true, |
| 1450 | false)))); |
| 1451 | request.set_message("Hello"); |
| 1452 | |
| 1453 | Status s = stub_->Echo(&context, request, &response); |
| 1454 | EXPECT_FALSE(s.ok()); |
| 1455 | EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED); |
Julien Boeuf | 38c0cde | 2016-06-06 14:46:08 +0200 | [diff] [blame] | 1456 | EXPECT_EQ(s.error_message(), kTestCredsPluginErrorMsg); |
Julien Boeuf | 1928d49 | 2015-09-15 15:20:11 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1459 | TEST_P(SecureEnd2endTest, ClientAuthContext) { |
| 1460 | ResetStub(); |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 1461 | EchoRequest request; |
| 1462 | EchoResponse response; |
| 1463 | request.set_message("Hello"); |
Dan Born | f2f7d57 | 2016-03-03 17:26:12 -0800 | [diff] [blame] | 1464 | request.mutable_param()->set_check_auth_context(GetParam().credentials_type == |
| 1465 | kTlsCredentialsType); |
| 1466 | request.mutable_param()->set_expected_transport_security_type( |
| 1467 | GetParam().credentials_type); |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 1468 | ClientContext context; |
| 1469 | Status s = stub_->Echo(&context, request, &response); |
| 1470 | EXPECT_EQ(response.message(), request.message()); |
| 1471 | EXPECT_TRUE(s.ok()); |
| 1472 | |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 1473 | std::shared_ptr<const AuthContext> auth_ctx = context.auth_context(); |
Dan Born | f2f7d57 | 2016-03-03 17:26:12 -0800 | [diff] [blame] | 1474 | std::vector<grpc::string_ref> tst = |
yang-g | 8b25f2a | 2015-07-21 23:54:36 -0700 | [diff] [blame] | 1475 | auth_ctx->FindPropertyValues("transport_security_type"); |
Robbie Shade | 820c1f3 | 2016-06-23 14:31:28 -0400 | [diff] [blame] | 1476 | ASSERT_EQ(1u, tst.size()); |
Dan Born | f2f7d57 | 2016-03-03 17:26:12 -0800 | [diff] [blame] | 1477 | EXPECT_EQ(GetParam().credentials_type, ToString(tst[0])); |
| 1478 | if (GetParam().credentials_type == kTlsCredentialsType) { |
| 1479 | EXPECT_EQ("x509_subject_alternative_name", |
| 1480 | auth_ctx->GetPeerIdentityPropertyName()); |
Paul Querna | 47d841d | 2016-03-10 11:19:17 -0800 | [diff] [blame] | 1481 | EXPECT_EQ(4u, auth_ctx->GetPeerIdentity().size()); |
Dan Born | f2f7d57 | 2016-03-03 17:26:12 -0800 | [diff] [blame] | 1482 | EXPECT_EQ("*.test.google.fr", ToString(auth_ctx->GetPeerIdentity()[0])); |
| 1483 | EXPECT_EQ("waterzooi.test.google.be", |
| 1484 | ToString(auth_ctx->GetPeerIdentity()[1])); |
| 1485 | EXPECT_EQ("*.test.youtube.com", ToString(auth_ctx->GetPeerIdentity()[2])); |
Paul Querna | 47d841d | 2016-03-10 11:19:17 -0800 | [diff] [blame] | 1486 | EXPECT_EQ("192.168.1.3", ToString(auth_ctx->GetPeerIdentity()[3])); |
Dan Born | f2f7d57 | 2016-03-03 17:26:12 -0800 | [diff] [blame] | 1487 | } |
yang-g | c4eef2e | 2015-07-06 23:26:58 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
yang-g | 4c8aed3 | 2016-02-19 00:19:39 -0800 | [diff] [blame] | 1490 | std::vector<TestScenario> CreateTestScenarios(bool use_proxy, |
| 1491 | bool test_insecure, |
| 1492 | bool test_secure) { |
| 1493 | std::vector<TestScenario> scenarios; |
| 1494 | std::vector<grpc::string> credentials_types; |
| 1495 | if (test_secure) { |
| 1496 | credentials_types = GetSecureCredentialsTypeList(); |
| 1497 | } |
| 1498 | if (test_insecure) { |
| 1499 | credentials_types.push_back(kInsecureCredentialsType); |
| 1500 | } |
| 1501 | for (auto it = credentials_types.begin(); it != credentials_types.end(); |
| 1502 | ++it) { |
Vijay Pai | 679c75f | 2016-06-15 13:08:00 -0700 | [diff] [blame] | 1503 | scenarios.emplace_back(false, *it); |
yang-g | 4c8aed3 | 2016-02-19 00:19:39 -0800 | [diff] [blame] | 1504 | if (use_proxy) { |
Vijay Pai | 679c75f | 2016-06-15 13:08:00 -0700 | [diff] [blame] | 1505 | scenarios.emplace_back(true, *it); |
yang-g | 4c8aed3 | 2016-02-19 00:19:39 -0800 | [diff] [blame] | 1506 | } |
| 1507 | } |
| 1508 | return scenarios; |
| 1509 | } |
yang-g | 6f30dec | 2015-07-22 23:11:56 -0700 | [diff] [blame] | 1510 | |
yang-g | 4c8aed3 | 2016-02-19 00:19:39 -0800 | [diff] [blame] | 1511 | INSTANTIATE_TEST_CASE_P(End2end, End2endTest, |
| 1512 | ::testing::ValuesIn(CreateTestScenarios(false, true, |
| 1513 | true))); |
Sree Kuchibhotla | 944f4cf | 2016-01-27 14:37:26 -0800 | [diff] [blame] | 1514 | |
yang-g | 4c8aed3 | 2016-02-19 00:19:39 -0800 | [diff] [blame] | 1515 | INSTANTIATE_TEST_CASE_P(End2endServerTryCancel, End2endServerTryCancelTest, |
| 1516 | ::testing::ValuesIn(CreateTestScenarios(false, true, |
| 1517 | false))); |
| 1518 | |
| 1519 | INSTANTIATE_TEST_CASE_P(ProxyEnd2end, ProxyEnd2endTest, |
| 1520 | ::testing::ValuesIn(CreateTestScenarios(true, true, |
| 1521 | true))); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1522 | |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1523 | INSTANTIATE_TEST_CASE_P(SecureEnd2end, SecureEnd2endTest, |
yang-g | 4c8aed3 | 2016-02-19 00:19:39 -0800 | [diff] [blame] | 1524 | ::testing::ValuesIn(CreateTestScenarios(false, false, |
| 1525 | true))); |
yang-g | 88d5d52 | 2015-09-29 12:46:54 -0700 | [diff] [blame] | 1526 | |
yang-g | 8ab3836 | 2015-07-31 14:05:33 -0700 | [diff] [blame] | 1527 | } // namespace |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1528 | } // namespace testing |
| 1529 | } // namespace grpc |
| 1530 | |
| 1531 | int main(int argc, char** argv) { |
| 1532 | grpc_test_init(argc, argv); |
| 1533 | ::testing::InitGoogleTest(&argc, argv); |
| 1534 | return RUN_ALL_TESTS(); |
David Garcia Quintas | 2bf574f | 2016-01-14 15:27:08 -0800 | [diff] [blame] | 1535 | } |