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