| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <mutex> |
| 20 |
#include <thread> |
| 21 |
|
| 22 |
#include "absl/memory/memory.h" |
| 23 |
#include "absl/strings/ascii.h" |
| 24 |
#include "absl/strings/match.h" |
| 25 |
#include "absl/strings/str_format.h" |
| 26 |
|
| 27 |
#include <grpc/grpc.h> |
| 28 |
#include <grpc/support/alloc.h> |
| 29 |
#include <grpc/support/log.h> |
| 30 |
#include <grpc/support/time.h> |
| 31 |
#include <grpcpp/channel.h> |
| 32 |
#include <grpcpp/client_context.h> |
| 33 |
#include <grpcpp/create_channel.h> |
| 34 |
#include <grpcpp/resource_quota.h> |
| 35 |
#include <grpcpp/security/auth_metadata_processor.h> |
| 36 |
#include <grpcpp/security/credentials.h> |
| 37 |
#include <grpcpp/security/server_credentials.h> |
| 38 |
#include <grpcpp/server.h> |
| 39 |
#include <grpcpp/server_builder.h> |
| 40 |
#include <grpcpp/server_context.h> |
| 41 |
#include <grpcpp/support/string_ref.h> |
| 42 |
#include <grpcpp/test/channel_test_peer.h> |
| 43 |
|
| 44 |
#include "src/core/ext/filters/client_channel/backup_poller.h" |
| 45 |
#include "src/core/lib/gprpp/env.h" |
| 46 |
#include "src/core/lib/iomgr/iomgr.h" |
| 47 |
#include "src/core/lib/security/credentials/credentials.h" |
| 48 |
#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h" |
| 49 |
#include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 50 |
#include "test/core/util/port.h" |
| 51 |
#include "test/core/util/test_config.h" |
| 52 |
#include "test/cpp/end2end/interceptors_util.h" |
| 53 |
#include "test/cpp/end2end/test_service_impl.h" |
| 54 |
#include "test/cpp/util/string_ref_helper.h" |
| 55 |
#include "test/cpp/util/test_credentials_provider.h" |
| 56 |
|
| 57 |
#ifdef GRPC_POSIX_SOCKET_EV |
| 58 |
#include "src/core/lib/iomgr/ev_posix.h" |
| 59 |
#endif // GRPC_POSIX_SOCKET_EV |
| 60 |
|
| 61 |
#include <gtest/gtest.h> |
| 62 |
|
| 63 |
using std::chrono::system_clock; |
| 64 |
|
| 65 |
namespace grpc { |
| 66 |
namespace testing { |
| 67 |
namespace { |
| 68 |
|
| 69 |
bool CheckIsLocalhost(const std::string& addr) { |
| 70 |
const std::string kIpv6("ipv6:%5B::1%5D:"); |
| 71 |
const std::string kIpv4MappedIpv6("ipv6:%5B::ffff:127.0.0.1%5D:"); |
| 72 |
const std::string kIpv4("ipv4:127.0.0.1:"); |
| 73 |
return addr.substr(0, kIpv4.size()) == kIpv4 || |
| 74 |
addr.substr(0, kIpv4MappedIpv6.size()) == kIpv4MappedIpv6 || |
| 75 |
addr.substr(0, kIpv6.size()) == kIpv6; |
| 76 |
} |
| 77 |
|
| 78 |
const int kClientChannelBackupPollIntervalMs = 200; |
| 79 |
|
| 80 |
const char kTestCredsPluginErrorMsg[] = "Could not find plugin metadata."; |
| 81 |
|
| 82 |
const char kFakeToken[] = "fake_token"; |
| 83 |
const char kFakeSelector[] = "fake_selector"; |
| 84 |
const char kExpectedFakeCredsDebugString[] = |
| 85 |
"SecureCallCredentials{GoogleIAMCredentials{Token:present," |
| 86 |
"AuthoritySelector:fake_selector}}"; |
| 87 |
|
| 88 |
const char kWrongToken[] = "wrong_token"; |
| 89 |
const char kWrongSelector[] = "wrong_selector"; |
| 90 |
const char kExpectedWrongCredsDebugString[] = |
| 91 |
"SecureCallCredentials{GoogleIAMCredentials{Token:present," |
| 92 |
"AuthoritySelector:wrong_selector}}"; |
| 93 |
|
| 94 |
const char kFakeToken1[] = "fake_token1"; |
| 95 |
const char kFakeSelector1[] = "fake_selector1"; |
| 96 |
const char kExpectedFakeCreds1DebugString[] = |
| 97 |
"SecureCallCredentials{GoogleIAMCredentials{Token:present," |
| 98 |
"AuthoritySelector:fake_selector1}}"; |
| 99 |
|
| 100 |
const char kFakeToken2[] = "fake_token2"; |
| 101 |
const char kFakeSelector2[] = "fake_selector2"; |
| 102 |
const char kExpectedFakeCreds2DebugString[] = |
| 103 |
"SecureCallCredentials{GoogleIAMCredentials{Token:present," |
| 104 |
"AuthoritySelector:fake_selector2}}"; |
| 105 |
|
| 106 |
const char kExpectedAuthMetadataPluginKeyFailureCredsDebugString[] = |
| 107 |
"SecureCallCredentials{TestMetadataCredentials{key:TestPluginMetadata," |
| 108 |
"value:Does not matter, will fail the key is invalid.}}"; |
| 109 |
const char kExpectedAuthMetadataPluginValueFailureCredsDebugString[] = |
| 110 |
"SecureCallCredentials{TestMetadataCredentials{key:test-plugin-metadata," |
| 111 |
"value:With illegal \n value.}}"; |
| 112 |
const char kExpectedAuthMetadataPluginWithDeadlineCredsDebugString[] = |
| 113 |
"SecureCallCredentials{TestMetadataCredentials{key:meta_key,value:Does not " |
| 114 |
"matter}}"; |
| 115 |
const char kExpectedNonBlockingAuthMetadataPluginFailureCredsDebugString[] = |
| 116 |
"SecureCallCredentials{TestMetadataCredentials{key:test-plugin-metadata," |
| 117 |
"value:Does not matter, will fail anyway (see 3rd param)}}"; |
| 118 |
const char |
| 119 |
kExpectedNonBlockingAuthMetadataPluginAndProcessorSuccessCredsDebugString |
| 120 |
[] = "SecureCallCredentials{TestMetadataCredentials{key:test-plugin-" |
| 121 |
"metadata,value:Dr Jekyll}}"; |
| 122 |
const char |
| 123 |
kExpectedNonBlockingAuthMetadataPluginAndProcessorFailureCredsDebugString |
| 124 |
[] = "SecureCallCredentials{TestMetadataCredentials{key:test-plugin-" |
| 125 |
"metadata,value:Mr Hyde}}"; |
| 126 |
const char kExpectedBlockingAuthMetadataPluginFailureCredsDebugString[] = |
| 127 |
"SecureCallCredentials{TestMetadataCredentials{key:test-plugin-metadata," |
| 128 |
"value:Does not matter, will fail anyway (see 3rd param)}}"; |
| 129 |
const char kExpectedCompositeCallCredsDebugString[] = |
| 130 |
"SecureCallCredentials{CompositeCallCredentials{TestMetadataCredentials{" |
| 131 |
"key:call-creds-key1,value:call-creds-val1},TestMetadataCredentials{key:" |
| 132 |
"call-creds-key2,value:call-creds-val2}}}"; |
| 133 |
|
| 134 |
class TestMetadataCredentialsPlugin : public MetadataCredentialsPlugin { |
| 135 |
public: |
| 136 |
static const char kGoodMetadataKey[]; |
| 137 |
static const char kBadMetadataKey[]; |
| 138 |
|
| 139 |
TestMetadataCredentialsPlugin(const grpc::string_ref& metadata_key, |
| 140 |
const grpc::string_ref& metadata_value, |
| 141 |
bool is_blocking, bool is_successful, |
| 142 |
int delay_ms) |
| 143 |
: metadata_key_(metadata_key.data(), metadata_key.length()), |
| 144 |
metadata_value_(metadata_value.data(), metadata_value.length()), |
| 145 |
is_blocking_(is_blocking), |
| 146 |
is_successful_(is_successful), |
| 147 |
delay_ms_(delay_ms) {} |
| 148 |
|
| 149 |
bool IsBlocking() const override { return is_blocking_; } |
| 150 |
|
| 151 |
Status GetMetadata( |
| 152 |
grpc::string_ref service_url, grpc::string_ref method_name, |
| 153 |
const grpc::AuthContext& channel_auth_context, |
| 154 |
std::multimap<std::string, std::string>* metadata) override { |
| 155 |
if (delay_ms_ != 0) { |
| 156 |
gpr_sleep_until( |
| 157 |
gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 158 |
gpr_time_from_millis(delay_ms_, GPR_TIMESPAN))); |
| 159 |
} |
| 160 |
EXPECT_GT(service_url.length(), 0UL); |
| 161 |
EXPECT_GT(method_name.length(), 0UL); |
| 162 |
EXPECT_TRUE(channel_auth_context.IsPeerAuthenticated()); |
| 163 |
EXPECT_TRUE(metadata != nullptr); |
| 164 |
if (is_successful_) { |
| 165 |
metadata->insert(std::make_pair(metadata_key_, metadata_value_)); |
| 166 |
return Status::OK; |
| 167 |
} else { |
| 168 |
return Status(StatusCode::NOT_FOUND, kTestCredsPluginErrorMsg); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
std::string DebugString() override { |
| 173 |
return absl::StrFormat("TestMetadataCredentials{key:%s,value:%s}", |
| 174 |
metadata_key_.c_str(), metadata_value_.c_str()); |
| 175 |
} |
| 176 |
|
| 177 |
private: |
| 178 |
std::string metadata_key_; |
| 179 |
std::string metadata_value_; |
| 180 |
bool is_blocking_; |
| 181 |
bool is_successful_; |
| 182 |
int delay_ms_; |
| 183 |
}; |
| 184 |
|
| 185 |
const char TestMetadataCredentialsPlugin::kBadMetadataKey[] = |
| 186 |
"TestPluginMetadata"; |
| 187 |
const char TestMetadataCredentialsPlugin::kGoodMetadataKey[] = |
| 188 |
"test-plugin-metadata"; |
| 189 |
|
| 190 |
class TestAuthMetadataProcessor : public AuthMetadataProcessor { |
| 191 |
public: |
| 192 |
static const char kGoodGuy[]; |
| 193 |
|
| 194 |
explicit TestAuthMetadataProcessor(bool is_blocking) |
| 195 |
: is_blocking_(is_blocking) {} |
| 196 |
|
| 197 |
std::shared_ptr<CallCredentials> GetCompatibleClientCreds() { |
| 198 |
return grpc::MetadataCredentialsFromPlugin( |
| 199 |
std::unique_ptr<MetadataCredentialsPlugin>( |
| 200 |
new TestMetadataCredentialsPlugin( |
| 201 |
TestMetadataCredentialsPlugin::kGoodMetadataKey, kGoodGuy, |
| 202 |
is_blocking_, true, 0))); |
| 203 |
} |
| 204 |
|
| 205 |
std::shared_ptr<CallCredentials> GetIncompatibleClientCreds() { |
| 206 |
return grpc::MetadataCredentialsFromPlugin( |
| 207 |
std::unique_ptr<MetadataCredentialsPlugin>( |
| 208 |
new TestMetadataCredentialsPlugin( |
| 209 |
TestMetadataCredentialsPlugin::kGoodMetadataKey, "Mr Hyde", |
| 210 |
is_blocking_, true, 0))); |
| 211 |
} |
| 212 |
|
| 213 |
// Interface implementation |
| 214 |
bool IsBlocking() const override { return is_blocking_; } |
| 215 |
|
| 216 |
Status Process(const InputMetadata& auth_metadata, AuthContext* context, |
| 217 |
OutputMetadata* consumed_auth_metadata, |
| 218 |
OutputMetadata* response_metadata) override { |
| 219 |
EXPECT_TRUE(consumed_auth_metadata != nullptr); |
| 220 |
EXPECT_TRUE(context != nullptr); |
| 221 |
EXPECT_TRUE(response_metadata != nullptr); |
| 222 |
auto auth_md = |
| 223 |
auth_metadata.find(TestMetadataCredentialsPlugin::kGoodMetadataKey); |
| 224 |
EXPECT_NE(auth_md, auth_metadata.end()); |
| 225 |
string_ref auth_md_value = auth_md->second; |
| 226 |
if (auth_md_value == kGoodGuy) { |
| 227 |
context->AddProperty(kIdentityPropName, kGoodGuy); |
| 228 |
context->SetPeerIdentityPropertyName(kIdentityPropName); |
| 229 |
consumed_auth_metadata->insert(std::make_pair( |
| 230 |
string(auth_md->first.data(), auth_md->first.length()), |
| 231 |
string(auth_md->second.data(), auth_md->second.length()))); |
| 232 |
return Status::OK; |
| 233 |
} else { |
| 234 |
return Status(StatusCode::UNAUTHENTICATED, |
| 235 |
string("Invalid principal: ") + |
| 236 |
string(auth_md_value.data(), auth_md_value.length())); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
private: |
| 241 |
static const char kIdentityPropName[]; |
| 242 |
bool is_blocking_; |
| 243 |
}; |
| 244 |
|
| 245 |
const char TestAuthMetadataProcessor::kGoodGuy[] = "Dr Jekyll"; |
| 246 |
const char TestAuthMetadataProcessor::kIdentityPropName[] = "novel identity"; |
| 247 |
|
| 248 |
class Proxy : public grpc::testing::EchoTestService::Service { |
| 249 |
public: |
| 250 |
explicit Proxy(const std::shared_ptr<Channel>& channel) |
| 251 |
: stub_(grpc::testing::EchoTestService::NewStub(channel)) {} |
| 252 |
|
| 253 |
Status Echo(ServerContext* server_context, const EchoRequest* request, |
| 254 |
EchoResponse* response) override { |
| 255 |
std::unique_ptr<ClientContext> client_context = |
| 256 |
ClientContext::FromServerContext(*server_context); |
| 257 |
return stub_->Echo(client_context.get(), *request, response); |
| 258 |
} |
| 259 |
|
| 260 |
private: |
| 261 |
std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
| 262 |
}; |
| 263 |
|
| 264 |
class TestServiceImplDupPkg |
| 265 |
: public grpc::testing::duplicate::EchoTestService::Service { |
| 266 |
public: |
| 267 |
Status Echo(ServerContext* /*context*/, const EchoRequest* /*request*/, |
| 268 |
EchoResponse* response) override { |
| 269 |
response->set_message("no package"); |
| 270 |
return Status::OK; |
| 271 |
} |
| 272 |
}; |
| 273 |
|
| 274 |
class TestScenario { |
| 275 |
public: |
| 276 |
TestScenario(bool use_interceptors, bool use_proxy, bool inproc, |
| 277 |
const std::string& credentials_type, bool callback_server) |
| 278 |
: use_interceptors_(use_interceptors), |
| 279 |
use_proxy_(use_proxy), |
| 280 |
inproc_(inproc), |
| 281 |
credentials_type_(credentials_type), |
| 282 |
callback_server_(callback_server) {} |
| 283 |
|
| 284 |
bool use_interceptors() const { return use_interceptors_; } |
| 285 |
bool use_proxy() const { return use_proxy_; } |
| 286 |
bool inproc() const { return inproc_; } |
| 287 |
const std::string& credentials_type() const { return credentials_type_; } |
| 288 |
bool callback_server() const { return callback_server_; } |
| 289 |
|
| 290 |
std::string AsString() const; |
| 291 |
|
| 292 |
static std::string Name(const ::testing::TestParamInfo<TestScenario>& info) { |
| 293 |
return info.param.AsString(); |
| 294 |
} |
| 295 |
|
| 296 |
private: |
| 297 |
bool use_interceptors_; |
| 298 |
bool use_proxy_; |
| 299 |
bool inproc_; |
| 300 |
const std::string credentials_type_; |
| 301 |
bool callback_server_; |
| 302 |
}; |
| 303 |
|
| 304 |
std::string TestScenario::AsString() const { |
| 305 |
std::string retval = use_interceptors_ ? "Interceptor" : ""; |
| 306 |
if (use_proxy_) retval += "Proxy"; |
| 307 |
if (inproc_) retval += "Inproc"; |
| 308 |
if (callback_server_) retval += "CallbackServer"; |
| 309 |
if (credentials_type_ == kInsecureCredentialsType) { |
| 310 |
retval += "Insecure"; |
| 311 |
} else { |
| 312 |
std::string creds_type = absl::AsciiStrToLower(credentials_type_); |
| 313 |
if (!creds_type.empty()) creds_type[0] = absl::ascii_toupper(creds_type[0]); |
| 314 |
retval += creds_type; |
| 315 |
} |
| 316 |
return retval; |
| 317 |
} |
| 318 |
|
| 319 |
class End2endTest : public ::testing::TestWithParam<TestScenario> { |
| 320 |
protected: |
| 321 |
static void SetUpTestCase() { grpc_init(); } |
| 322 |
static void TearDownTestCase() { grpc_shutdown(); } |
| 323 |
End2endTest() |
| 324 |
: is_server_started_(false), |
| 325 |
kMaxMessageSize_(8192), |
| 326 |
special_service_("special"), |
| 327 |
first_picked_port_(0) {} |
| 328 |
|
| 329 |
void TearDown() override { |
| 330 |
if (is_server_started_) { |
| 331 |
server_->Shutdown(); |
| 332 |
if (proxy_server_) proxy_server_->Shutdown(); |
| 333 |
} |
| 334 |
if (first_picked_port_ > 0) { |
| 335 |
grpc_recycle_unused_port(first_picked_port_); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
void StartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) { |
| 340 |
int port = grpc_pick_unused_port_or_die(); |
| 341 |
first_picked_port_ = port; |
| 342 |
server_address_ << "localhost:" << port; |
| 343 |
// Setup server |
| 344 |
BuildAndStartServer(processor); |
| 345 |
} |
| 346 |
|
| 347 |
void RestartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) { |
| 348 |
if (is_server_started_) { |
| 349 |
server_->Shutdown(); |
| 350 |
BuildAndStartServer(processor); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
void BuildAndStartServer( |
| 355 |
const std::shared_ptr<AuthMetadataProcessor>& processor) { |
| 356 |
ServerBuilder builder; |
| 357 |
ConfigureServerBuilder(&builder); |
| 358 |
auto server_creds = GetCredentialsProvider()->GetServerCredentials( |
| 359 |
GetParam().credentials_type()); |
| 360 |
if (GetParam().credentials_type() != kInsecureCredentialsType) { |
| 361 |
server_creds->SetAuthMetadataProcessor(processor); |
| 362 |
} |
| 363 |
if (GetParam().use_interceptors()) { |
| 364 |
std::vector< |
| 365 |
std::unique_ptr<experimental::ServerInterceptorFactoryInterface>> |
| 366 |
creators; |
| 367 |
// Add 20 phony server interceptors |
| 368 |
creators.reserve(20); |
| 369 |
for (auto i = 0; i < 20; i++) { |
| 370 |
creators.push_back(std::make_unique<PhonyInterceptorFactory>()); |
| 371 |
} |
| 372 |
builder.experimental().SetInterceptorCreators(std::move(creators)); |
| 373 |
} |
| 374 |
builder.AddListeningPort(server_address_.str(), server_creds); |
| 375 |
if (!GetParam().callback_server()) { |
| 376 |
builder.RegisterService(&service_); |
| 377 |
} else { |
| 378 |
builder.RegisterService(&callback_service_); |
| 379 |
} |
| 380 |
builder.RegisterService("foo.test.youtube.com", &special_service_); |
| 381 |
builder.RegisterService(&dup_pkg_service_); |
| 382 |
|
| 383 |
builder.SetSyncServerOption(ServerBuilder::SyncServerOption::NUM_CQS, 4); |
| 384 |
builder.SetSyncServerOption( |
| 385 |
ServerBuilder::SyncServerOption::CQ_TIMEOUT_MSEC, 10); |
| 386 |
|
| 387 |
server_ = builder.BuildAndStart(); |
| 388 |
is_server_started_ = true; |
| 389 |
} |
| 390 |
|
| 391 |
virtual void ConfigureServerBuilder(ServerBuilder* builder) { |
| 392 |
builder->SetMaxMessageSize( |
| 393 |
kMaxMessageSize_); // For testing max message size. |
| 394 |
} |
| 395 |
|
| 396 |
void ResetChannel( |
| 397 |
std::vector< |
| 398 |
std::unique_ptr<experimental::ClientInterceptorFactoryInterface>> |
| 399 |
interceptor_creators = {}) { |
| 400 |
if (!is_server_started_) { |
| 401 |
StartServer(std::shared_ptr<AuthMetadataProcessor>()); |
| 402 |
} |
| 403 |
EXPECT_TRUE(is_server_started_); |
| 404 |
ChannelArguments args; |
| 405 |
auto channel_creds = GetCredentialsProvider()->GetChannelCredentials( |
| 406 |
GetParam().credentials_type(), &args); |
| 407 |
if (!user_agent_prefix_.empty()) { |
| 408 |
args.SetUserAgentPrefix(user_agent_prefix_); |
| 409 |
} |
| 410 |
args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test"); |
| 411 |
|
| 412 |
if (!GetParam().inproc()) { |
| 413 |
if (!GetParam().use_interceptors()) { |
| 414 |
channel_ = grpc::CreateCustomChannel(server_address_.str(), |
| 415 |
channel_creds, args); |
| 416 |
} else { |
| 417 |
channel_ = CreateCustomChannelWithInterceptors( |
| 418 |
server_address_.str(), channel_creds, args, |
| 419 |
interceptor_creators.empty() ? CreatePhonyClientInterceptors() |
| 420 |
: std::move(interceptor_creators)); |
| 421 |
} |
| 422 |
} else { |
| 423 |
if (!GetParam().use_interceptors()) { |
| 424 |
channel_ = server_->InProcessChannel(args); |
| 425 |
} else { |
| 426 |
channel_ = server_->experimental().InProcessChannelWithInterceptors( |
| 427 |
args, interceptor_creators.empty() |
| 428 |
? CreatePhonyClientInterceptors() |
| 429 |
: std::move(interceptor_creators)); |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
void ResetStub( |
| 435 |
std::vector< |
| 436 |
std::unique_ptr<experimental::ClientInterceptorFactoryInterface>> |
| 437 |
interceptor_creators = {}) { |
| 438 |
ResetChannel(std::move(interceptor_creators)); |
| 439 |
if (GetParam().use_proxy()) { |
| 440 |
proxy_service_ = std::make_unique<Proxy>(channel_); |
| 441 |
int port = grpc_pick_unused_port_or_die(); |
| 442 |
std::ostringstream proxyaddr; |
| 443 |
proxyaddr << "localhost:" << port; |
| 444 |
ServerBuilder builder; |
| 445 |
builder.AddListeningPort(proxyaddr.str(), InsecureServerCredentials()); |
| 446 |
builder.RegisterService(proxy_service_.get()); |
| 447 |
|
| 448 |
builder.SetSyncServerOption(ServerBuilder::SyncServerOption::NUM_CQS, 4); |
| 449 |
builder.SetSyncServerOption( |
| 450 |
ServerBuilder::SyncServerOption::CQ_TIMEOUT_MSEC, 10); |
| 451 |
|
| 452 |
proxy_server_ = builder.BuildAndStart(); |
| 453 |
|
| 454 |
channel_ = |
| 455 |
grpc::CreateChannel(proxyaddr.str(), InsecureChannelCredentials()); |
| 456 |
} |
| 457 |
|
| 458 |
stub_ = grpc::testing::EchoTestService::NewStub(channel_); |
| 459 |
PhonyInterceptor::Reset(); |
| 460 |
} |
| 461 |
|
| 462 |
bool is_server_started_; |
| 463 |
std::shared_ptr<Channel> channel_; |
| 464 |
std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
| 465 |
std::unique_ptr<Server> server_; |
| 466 |
std::unique_ptr<Server> proxy_server_; |
| 467 |
std::unique_ptr<Proxy> proxy_service_; |
| 468 |
std::ostringstream server_address_; |
| 469 |
const int kMaxMessageSize_; |
| 470 |
TestServiceImpl service_; |
| 471 |
CallbackTestServiceImpl callback_service_; |
| 472 |
TestServiceImpl special_service_; |
| 473 |
TestServiceImplDupPkg dup_pkg_service_; |
| 474 |
std::string user_agent_prefix_; |
| 475 |
int first_picked_port_; |
| 476 |
}; |
| 477 |
|
| 478 |
void SendRpc(grpc::testing::EchoTestService::Stub* stub, int num_rpcs, |
| 479 |
bool with_binary_metadata) { |
| 480 |
EchoRequest request; |
| 481 |
EchoResponse response; |
| 482 |
request.set_message("Hello hello hello hello"); |
| 483 |
|
| 484 |
for (int i = 0; i < num_rpcs; ++i) { |
| 485 |
ClientContext context; |
| 486 |
if (with_binary_metadata) { |
| 487 |
char bytes[8] = {'\0', '\1', '\2', '\3', |
| 488 |
'\4', '\5', '\6', static_cast<char>(i)}; |
| 489 |
context.AddMetadata("custom-bin", std::string(bytes, 8)); |
| 490 |
} |
| 491 |
context.set_compression_algorithm(GRPC_COMPRESS_GZIP); |
| 492 |
Status s = stub->Echo(&context, request, &response); |
| 493 |
EXPECT_EQ(response.message(), request.message()); |
| 494 |
EXPECT_TRUE(s.ok()); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
// This class is for testing scenarios where RPCs are cancelled on the server |
| 499 |
// by calling ServerContext::TryCancel() |
| 500 |
class End2endServerTryCancelTest : public End2endTest { |
| 501 |
protected: |
| 502 |
// Helper for testing client-streaming RPCs which are cancelled on the server. |
| 503 |
// Depending on the value of server_try_cancel parameter, this will test one |
| 504 |
// of the following three scenarios: |
| 505 |
// CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading |
| 506 |
// any messages from the client |
| 507 |
// |
| 508 |
// CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading |
| 509 |
// messages from the client |
| 510 |
// |
| 511 |
// CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading all |
| 512 |
// the messages from the client |
| 513 |
// |
| 514 |
// NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL. |
| 515 |
void TestRequestStreamServerCancel( |
| 516 |
ServerTryCancelRequestPhase server_try_cancel, int num_msgs_to_send) { |
| 517 |
RestartServer(std::shared_ptr<AuthMetadataProcessor>()); |
| 518 |
ResetStub(); |
| 519 |
EchoRequest request; |
| 520 |
EchoResponse response; |
| 521 |
ClientContext context; |
| 522 |
|
| 523 |
// Send server_try_cancel value in the client metadata |
| 524 |
context.AddMetadata(kServerTryCancelRequest, |
| 525 |
std::to_string(server_try_cancel)); |
| 526 |
|
| 527 |
auto stream = stub_->RequestStream(&context, &response); |
| 528 |
|
| 529 |
int num_msgs_sent = 0; |
| 530 |
while (num_msgs_sent < num_msgs_to_send) { |
| 531 |
request.set_message("hello"); |
| 532 |
if (!stream->Write(request)) { |
| 533 |
break; |
| 534 |
} |
| 535 |
num_msgs_sent++; |
| 536 |
} |
| 537 |
gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent); |
| 538 |
|
| 539 |
stream->WritesDone(); |
| 540 |
Status s = stream->Finish(); |
| 541 |
|
| 542 |
// At this point, we know for sure that RPC was cancelled by the server |
| 543 |
// since we passed server_try_cancel value in the metadata. Depending on the |
| 544 |
// value of server_try_cancel, the RPC might have been cancelled by the |
| 545 |
// server at different stages. The following validates our expectations of |
| 546 |
// number of messages sent in various cancellation scenarios: |
| 547 |
|
| 548 |
switch (server_try_cancel) { |
| 549 |
case CANCEL_BEFORE_PROCESSING: |
| 550 |
case CANCEL_DURING_PROCESSING: |
| 551 |
// If the RPC is cancelled by server before / during messages from the |
| 552 |
// client, it means that the client most likely did not get a chance to |
| 553 |
// send all the messages it wanted to send. i.e num_msgs_sent <= |
| 554 |
// num_msgs_to_send |
| 555 |
EXPECT_LE(num_msgs_sent, num_msgs_to_send); |
| 556 |
break; |
| 557 |
|
| 558 |
case CANCEL_AFTER_PROCESSING: |
| 559 |
// If the RPC was cancelled after all messages were read by the server, |
| 560 |
// the client did get a chance to send all its messages |
| 561 |
EXPECT_EQ(num_msgs_sent, num_msgs_to_send); |
| 562 |
break; |
| 563 |
|
| 564 |
default: |
| 565 |
gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d", |
| 566 |
server_try_cancel); |
| 567 |
EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL && |
| 568 |
server_try_cancel <= CANCEL_AFTER_PROCESSING); |
| 569 |
break; |
| 570 |
} |
| 571 |
|
| 572 |
EXPECT_FALSE(s.ok()); |
| 573 |
EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 574 |
// Make sure that the server interceptors were notified |
| 575 |
if (GetParam().use_interceptors()) { |
| 576 |
EXPECT_EQ(20, PhonyInterceptor::GetNumTimesCancel()); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
// Helper for testing server-streaming RPCs which are cancelled on the server. |
| 581 |
// Depending on the value of server_try_cancel parameter, this will test one |
| 582 |
// of the following three scenarios: |
| 583 |
// CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before writing |
| 584 |
// any messages to the client |
| 585 |
// |
| 586 |
// CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while writing |
| 587 |
// messages to the client |
| 588 |
// |
| 589 |
// CANCEL_AFTER PROCESSING: Rpc is cancelled by server after writing all |
| 590 |
// the messages to the client |
| 591 |
// |
| 592 |
// NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL. |
| 593 |
void TestResponseStreamServerCancel( |
| 594 |
ServerTryCancelRequestPhase server_try_cancel) { |
| 595 |
RestartServer(std::shared_ptr<AuthMetadataProcessor>()); |
| 596 |
ResetStub(); |
| 597 |
EchoRequest request; |
| 598 |
EchoResponse response; |
| 599 |
ClientContext context; |
| 600 |
|
| 601 |
// Send server_try_cancel in the client metadata |
| 602 |
context.AddMetadata(kServerTryCancelRequest, |
| 603 |
std::to_string(server_try_cancel)); |
| 604 |
|
| 605 |
request.set_message("hello"); |
| 606 |
auto stream = stub_->ResponseStream(&context, request); |
| 607 |
|
| 608 |
int num_msgs_read = 0; |
| 609 |
while (num_msgs_read < kServerDefaultResponseStreamsToSend) { |
| 610 |
if (!stream->Read(&response)) { |
| 611 |
break; |
| 612 |
} |
| 613 |
EXPECT_EQ(response.message(), |
| 614 |
request.message() + std::to_string(num_msgs_read)); |
| 615 |
num_msgs_read++; |
| 616 |
} |
| 617 |
gpr_log(GPR_INFO, "Read %d messages", num_msgs_read); |
| 618 |
|
| 619 |
Status s = stream->Finish(); |
| 620 |
|
| 621 |
// Depending on the value of server_try_cancel, the RPC might have been |
| 622 |
// cancelled by the server at different stages. The following validates our |
| 623 |
// expectations of number of messages read in various cancellation |
| 624 |
// scenarios: |
| 625 |
switch (server_try_cancel) { |
| 626 |
case CANCEL_BEFORE_PROCESSING: |
| 627 |
// Server cancelled before sending any messages. Which means the client |
| 628 |
// wouldn't have read any |
| 629 |
EXPECT_EQ(num_msgs_read, 0); |
| 630 |
break; |
| 631 |
|
| 632 |
case CANCEL_DURING_PROCESSING: |
| 633 |
// Server cancelled while writing messages. Client must have read less |
| 634 |
// than or equal to the expected number of messages |
| 635 |
EXPECT_LE(num_msgs_read, kServerDefaultResponseStreamsToSend); |
| 636 |
break; |
| 637 |
|
| 638 |
case CANCEL_AFTER_PROCESSING: |
| 639 |
// Even though the Server cancelled after writing all messages, the RPC |
| 640 |
// may be cancelled before the Client got a chance to read all the |
| 641 |
// messages. |
| 642 |
EXPECT_LE(num_msgs_read, kServerDefaultResponseStreamsToSend); |
| 643 |
break; |
| 644 |
|
| 645 |
default: { |
| 646 |
gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d", |
| 647 |
server_try_cancel); |
| 648 |
EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL && |
| 649 |
server_try_cancel <= CANCEL_AFTER_PROCESSING); |
| 650 |
break; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
EXPECT_FALSE(s.ok()); |
| 655 |
// Make sure that the server interceptors were notified |
| 656 |
if (GetParam().use_interceptors()) { |
| 657 |
EXPECT_EQ(20, PhonyInterceptor::GetNumTimesCancel()); |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
// Helper for testing bidirectional-streaming RPCs which are cancelled on the |
| 662 |
// server. Depending on the value of server_try_cancel parameter, this will |
| 663 |
// test one of the following three scenarios: |
| 664 |
// CANCEL_BEFORE_PROCESSING: Rpc is cancelled by the server before reading/ |
| 665 |
// writing any messages from/to the client |
| 666 |
// |
| 667 |
// CANCEL_DURING_PROCESSING: Rpc is cancelled by the server while reading/ |
| 668 |
// writing messages from/to the client |
| 669 |
// |
| 670 |
// CANCEL_AFTER PROCESSING: Rpc is cancelled by server after reading/writing |
| 671 |
// all the messages from/to the client |
| 672 |
// |
| 673 |
// NOTE: Do not call this function with server_try_cancel == DO_NOT_CANCEL. |
| 674 |
void TestBidiStreamServerCancel(ServerTryCancelRequestPhase server_try_cancel, |
| 675 |
int num_messages) { |
| 676 |
RestartServer(std::shared_ptr<AuthMetadataProcessor>()); |
| 677 |
ResetStub(); |
| 678 |
EchoRequest request; |
| 679 |
EchoResponse response; |
| 680 |
ClientContext context; |
| 681 |
|
| 682 |
// Send server_try_cancel in the client metadata |
| 683 |
context.AddMetadata(kServerTryCancelRequest, |
| 684 |
std::to_string(server_try_cancel)); |
| 685 |
|
| 686 |
auto stream = stub_->BidiStream(&context); |
| 687 |
|
| 688 |
int num_msgs_read = 0; |
| 689 |
int num_msgs_sent = 0; |
| 690 |
while (num_msgs_sent < num_messages) { |
| 691 |
request.set_message("hello " + std::to_string(num_msgs_sent)); |
| 692 |
if (!stream->Write(request)) { |
| 693 |
break; |
| 694 |
} |
| 695 |
num_msgs_sent++; |
| 696 |
|
| 697 |
if (!stream->Read(&response)) { |
| 698 |
break; |
| 699 |
} |
| 700 |
num_msgs_read++; |
| 701 |
|
| 702 |
EXPECT_EQ(response.message(), request.message()); |
| 703 |
} |
| 704 |
gpr_log(GPR_INFO, "Sent %d messages", num_msgs_sent); |
| 705 |
gpr_log(GPR_INFO, "Read %d messages", num_msgs_read); |
| 706 |
|
| 707 |
stream->WritesDone(); |
| 708 |
Status s = stream->Finish(); |
| 709 |
|
| 710 |
// Depending on the value of server_try_cancel, the RPC might have been |
| 711 |
// cancelled by the server at different stages. The following validates our |
| 712 |
// expectations of number of messages read in various cancellation |
| 713 |
// scenarios: |
| 714 |
switch (server_try_cancel) { |
| 715 |
case CANCEL_BEFORE_PROCESSING: |
| 716 |
EXPECT_EQ(num_msgs_read, 0); |
| 717 |
break; |
| 718 |
|
| 719 |
case CANCEL_DURING_PROCESSING: |
| 720 |
EXPECT_LE(num_msgs_sent, num_messages); |
| 721 |
EXPECT_LE(num_msgs_read, num_msgs_sent); |
| 722 |
break; |
| 723 |
|
| 724 |
case CANCEL_AFTER_PROCESSING: |
| 725 |
EXPECT_EQ(num_msgs_sent, num_messages); |
| 726 |
|
| 727 |
// The Server cancelled after reading the last message and after writing |
| 728 |
// the message to the client. However, the RPC cancellation might have |
| 729 |
// taken effect before the client actually read the response. |
| 730 |
EXPECT_LE(num_msgs_read, num_msgs_sent); |
| 731 |
break; |
| 732 |
|
| 733 |
default: |
| 734 |
gpr_log(GPR_ERROR, "Invalid server_try_cancel value: %d", |
| 735 |
server_try_cancel); |
| 736 |
EXPECT_TRUE(server_try_cancel > DO_NOT_CANCEL && |
| 737 |
server_try_cancel <= CANCEL_AFTER_PROCESSING); |
| 738 |
break; |
| 739 |
} |
| 740 |
|
| 741 |
EXPECT_FALSE(s.ok()); |
| 742 |
EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 743 |
// Make sure that the server interceptors were notified |
| 744 |
if (GetParam().use_interceptors()) { |
| 745 |
EXPECT_EQ(20, PhonyInterceptor::GetNumTimesCancel()); |
| 746 |
} |
| 747 |
} |
| 748 |
}; |
| 749 |
|
| 750 |
TEST_P(End2endServerTryCancelTest, RequestEchoServerCancel) { |
| 751 |
ResetStub(); |
| 752 |
EchoRequest request; |
| 753 |
EchoResponse response; |
| 754 |
ClientContext context; |
| 755 |
|
| 756 |
context.AddMetadata(kServerTryCancelRequest, |
| 757 |
std::to_string(CANCEL_BEFORE_PROCESSING)); |
| 758 |
Status s = stub_->Echo(&context, request, &response); |
| 759 |
EXPECT_FALSE(s.ok()); |
| 760 |
EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 761 |
} |
| 762 |
|
| 763 |
// Server to cancel before doing reading the request |
| 764 |
TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelBeforeReads) { |
| 765 |
TestRequestStreamServerCancel(CANCEL_BEFORE_PROCESSING, 1); |
| 766 |
} |
| 767 |
|
| 768 |
// Server to cancel while reading a request from the stream in parallel |
| 769 |
TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelDuringRead) { |
| 770 |
TestRequestStreamServerCancel(CANCEL_DURING_PROCESSING, 10); |
| 771 |
} |
| 772 |
|
| 773 |
// Server to cancel after reading all the requests but before returning to the |
| 774 |
// client |
| 775 |
TEST_P(End2endServerTryCancelTest, RequestStreamServerCancelAfterReads) { |
| 776 |
TestRequestStreamServerCancel(CANCEL_AFTER_PROCESSING, 4); |
| 777 |
} |
| 778 |
|
| 779 |
// Server to cancel before sending any response messages |
| 780 |
TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelBefore) { |
| 781 |
TestResponseStreamServerCancel(CANCEL_BEFORE_PROCESSING); |
| 782 |
} |
| 783 |
|
| 784 |
// Server to cancel while writing a response to the stream in parallel |
| 785 |
TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelDuring) { |
| 786 |
TestResponseStreamServerCancel(CANCEL_DURING_PROCESSING); |
| 787 |
} |
| 788 |
|
| 789 |
// Server to cancel after writing all the respones to the stream but before |
| 790 |
// returning to the client |
| 791 |
TEST_P(End2endServerTryCancelTest, ResponseStreamServerCancelAfter) { |
| 792 |
TestResponseStreamServerCancel(CANCEL_AFTER_PROCESSING); |
| 793 |
} |
| 794 |
|
| 795 |
// Server to cancel before reading/writing any requests/responses on the stream |
| 796 |
TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelBefore) { |
| 797 |
TestBidiStreamServerCancel(CANCEL_BEFORE_PROCESSING, 2); |
| 798 |
} |
| 799 |
|
| 800 |
// Server to cancel while reading/writing requests/responses on the stream in |
| 801 |
// parallel |
| 802 |
TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelDuring) { |
| 803 |
TestBidiStreamServerCancel(CANCEL_DURING_PROCESSING, 10); |
| 804 |
} |
| 805 |
|
| 806 |
// Server to cancel after reading/writing all requests/responses on the stream |
| 807 |
// but before returning to the client |
| 808 |
TEST_P(End2endServerTryCancelTest, BidiStreamServerCancelAfter) { |
| 809 |
TestBidiStreamServerCancel(CANCEL_AFTER_PROCESSING, 5); |
| 810 |
} |
| 811 |
|
| 812 |
TEST_P(End2endTest, SimpleRpcWithCustomUserAgentPrefix) { |
| 813 |
// User-Agent is an HTTP header for HTTP transports only |
| 814 |
if (GetParam().inproc()) { |
| 815 |
return; |
| 816 |
} |
| 817 |
user_agent_prefix_ = "custom_prefix"; |
| 818 |
ResetStub(); |
| 819 |
EchoRequest request; |
| 820 |
EchoResponse response; |
| 821 |
request.set_message("Hello hello hello hello"); |
| 822 |
request.mutable_param()->set_echo_metadata(true); |
| 823 |
|
| 824 |
ClientContext context; |
| 825 |
Status s = stub_->Echo(&context, request, &response); |
| 826 |
EXPECT_EQ(response.message(), request.message()); |
| 827 |
EXPECT_TRUE(s.ok()); |
| 828 |
const auto& trailing_metadata = context.GetServerTrailingMetadata(); |
| 829 |
auto iter = trailing_metadata.find("user-agent"); |
| 830 |
EXPECT_TRUE(iter != trailing_metadata.end()); |
| 831 |
std::string expected_prefix = user_agent_prefix_ + " grpc-c++/"; |
| 832 |
EXPECT_TRUE(iter->second.starts_with(expected_prefix)) << iter->second; |
| 833 |
} |
| 834 |
|
| 835 |
TEST_P(End2endTest, MultipleRpcsWithVariedBinaryMetadataValue) { |
| 836 |
ResetStub(); |
| 837 |
std::vector<std::thread> threads; |
| 838 |
threads.reserve(10); |
| 839 |
for (int i = 0; i < 10; ++i) { |
| 840 |
threads.emplace_back(SendRpc, stub_.get(), 10, true); |
| 841 |
} |
| 842 |
for (int i = 0; i < 10; ++i) { |
| 843 |
threads[i].join(); |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
TEST_P(End2endTest, MultipleRpcs) { |
| 848 |
ResetStub(); |
| 849 |
std::vector<std::thread> threads; |
| 850 |
threads.reserve(10); |
| 851 |
for (int i = 0; i < 10; ++i) { |
| 852 |
threads.emplace_back(SendRpc, stub_.get(), 10, false); |
| 853 |
} |
| 854 |
for (int i = 0; i < 10; ++i) { |
| 855 |
threads[i].join(); |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
TEST_P(End2endTest, ManyStubs) { |
| 860 |
ResetStub(); |
| 861 |
ChannelTestPeer peer(channel_.get()); |
| 862 |
int registered_calls_pre = peer.registered_calls(); |
| 863 |
int registration_attempts_pre = peer.registration_attempts(); |
| 864 |
for (int i = 0; i < 1000; ++i) { |
| 865 |
grpc::testing::EchoTestService::NewStub(channel_); |
| 866 |
} |
| 867 |
EXPECT_EQ(peer.registered_calls(), registered_calls_pre); |
| 868 |
EXPECT_GT(peer.registration_attempts(), registration_attempts_pre); |
| 869 |
} |
| 870 |
|
| 871 |
TEST_P(End2endTest, EmptyBinaryMetadata) { |
| 872 |
ResetStub(); |
| 873 |
EchoRequest request; |
| 874 |
EchoResponse response; |
| 875 |
request.set_message("Hello hello hello hello"); |
| 876 |
ClientContext context; |
| 877 |
context.AddMetadata("custom-bin", ""); |
| 878 |
Status s = stub_->Echo(&context, request, &response); |
| 879 |
EXPECT_EQ(response.message(), request.message()); |
| 880 |
EXPECT_TRUE(s.ok()); |
| 881 |
} |
| 882 |
|
| 883 |
TEST_P(End2endTest, AuthoritySeenOnServerSide) { |
| 884 |
ResetStub(); |
| 885 |
EchoRequest request; |
| 886 |
request.mutable_param()->set_echo_host_from_authority_header(true); |
| 887 |
EchoResponse response; |
| 888 |
request.set_message("Live long and prosper."); |
| 889 |
ClientContext context; |
| 890 |
Status s = stub_->Echo(&context, request, &response); |
| 891 |
EXPECT_EQ(response.message(), request.message()); |
| 892 |
if (GetParam().credentials_type() == kTlsCredentialsType) { |
| 893 |
// SSL creds overrides the authority. |
| 894 |
EXPECT_EQ("foo.test.google.fr", response.param().host()); |
| 895 |
} else if (GetParam().inproc()) { |
| 896 |
EXPECT_EQ("inproc", response.param().host()); |
| 897 |
} else { |
| 898 |
EXPECT_EQ(server_address_.str(), response.param().host()); |
| 899 |
} |
| 900 |
EXPECT_TRUE(s.ok()); |
| 901 |
} |
| 902 |
|
| 903 |
TEST_P(End2endTest, ReconnectChannel) { |
| 904 |
if (GetParam().inproc()) { |
| 905 |
return; |
| 906 |
} |
| 907 |
int poller_slowdown_factor = 1; |
| 908 |
// It needs 2 pollset_works to reconnect the channel with polling engine |
| 909 |
// "poll" |
| 910 |
#ifdef GRPC_POSIX_SOCKET_EV |
| 911 |
grpc_core::UniquePtr<char> poller = GPR_GLOBAL_CONFIG_GET(grpc_poll_strategy); |
| 912 |
if (0 == strcmp(poller.get(), "poll")) { |
| 913 |
poller_slowdown_factor = 2; |
| 914 |
} |
| 915 |
#endif // GRPC_POSIX_SOCKET_EV |
| 916 |
ResetStub(); |
| 917 |
SendRpc(stub_.get(), 1, false); |
| 918 |
RestartServer(std::shared_ptr<AuthMetadataProcessor>()); |
| 919 |
// It needs more than GRPC_CLIENT_CHANNEL_BACKUP_POLL_INTERVAL_MS time to |
| 920 |
// reconnect the channel. Make it a factor of 5x |
| 921 |
gpr_sleep_until( |
| 922 |
gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 923 |
gpr_time_from_millis(kClientChannelBackupPollIntervalMs * 5 * |
| 924 |
poller_slowdown_factor * |
| 925 |
grpc_test_slowdown_factor(), |
| 926 |
GPR_TIMESPAN))); |
| 927 |
SendRpc(stub_.get(), 1, false); |
| 928 |
} |
| 929 |
|
| 930 |
TEST_P(End2endTest, RequestStreamOneRequest) { |
| 931 |
ResetStub(); |
| 932 |
EchoRequest request; |
| 933 |
EchoResponse response; |
| 934 |
ClientContext context; |
| 935 |
|
| 936 |
auto stream = stub_->RequestStream(&context, &response); |
| 937 |
request.set_message("hello"); |
| 938 |
EXPECT_TRUE(stream->Write(request)); |
| 939 |
stream->WritesDone(); |
| 940 |
Status s = stream->Finish(); |
| 941 |
EXPECT_EQ(response.message(), request.message()); |
| 942 |
EXPECT_TRUE(s.ok()); |
| 943 |
EXPECT_TRUE(context.debug_error_string().empty()); |
| 944 |
} |
| 945 |
|
| 946 |
TEST_P(End2endTest, RequestStreamOneRequestWithCoalescingApi) { |
| 947 |
ResetStub(); |
| 948 |
EchoRequest request; |
| 949 |
EchoResponse response; |
| 950 |
ClientContext context; |
| 951 |
|
| 952 |
context.set_initial_metadata_corked(true); |
| 953 |
auto stream = stub_->RequestStream(&context, &response); |
| 954 |
request.set_message("hello"); |
| 955 |
stream->WriteLast(request, WriteOptions()); |
| 956 |
Status s = stream->Finish(); |
| 957 |
EXPECT_EQ(response.message(), request.message()); |
| 958 |
EXPECT_TRUE(s.ok()); |
| 959 |
} |
| 960 |
|
| 961 |
TEST_P(End2endTest, RequestStreamTwoRequests) { |
| 962 |
ResetStub(); |
| 963 |
EchoRequest request; |
| 964 |
EchoResponse response; |
| 965 |
ClientContext context; |
| 966 |
|
| 967 |
auto stream = stub_->RequestStream(&context, &response); |
| 968 |
request.set_message("hello"); |
| 969 |
EXPECT_TRUE(stream->Write(request)); |
| 970 |
EXPECT_TRUE(stream->Write(request)); |
| 971 |
stream->WritesDone(); |
| 972 |
Status s = stream->Finish(); |
| 973 |
EXPECT_EQ(response.message(), "hellohello"); |
| 974 |
EXPECT_TRUE(s.ok()); |
| 975 |
} |
| 976 |
|
| 977 |
TEST_P(End2endTest, RequestStreamTwoRequestsWithWriteThrough) { |
| 978 |
ResetStub(); |
| 979 |
EchoRequest request; |
| 980 |
EchoResponse response; |
| 981 |
ClientContext context; |
| 982 |
|
| 983 |
auto stream = stub_->RequestStream(&context, &response); |
| 984 |
request.set_message("hello"); |
| 985 |
EXPECT_TRUE(stream->Write(request, WriteOptions().set_write_through())); |
| 986 |
EXPECT_TRUE(stream->Write(request, WriteOptions().set_write_through())); |
| 987 |
stream->WritesDone(); |
| 988 |
Status s = stream->Finish(); |
| 989 |
EXPECT_EQ(response.message(), "hellohello"); |
| 990 |
EXPECT_TRUE(s.ok()); |
| 991 |
} |
| 992 |
|
| 993 |
TEST_P(End2endTest, RequestStreamTwoRequestsWithCoalescingApi) { |
| 994 |
ResetStub(); |
| 995 |
EchoRequest request; |
| 996 |
EchoResponse response; |
| 997 |
ClientContext context; |
| 998 |
|
| 999 |
context.set_initial_metadata_corked(true); |
| 1000 |
auto stream = stub_->RequestStream(&context, &response); |
| 1001 |
request.set_message("hello"); |
| 1002 |
EXPECT_TRUE(stream->Write(request)); |
| 1003 |
stream->WriteLast(request, WriteOptions()); |
| 1004 |
Status s = stream->Finish(); |
| 1005 |
EXPECT_EQ(response.message(), "hellohello"); |
| 1006 |
EXPECT_TRUE(s.ok()); |
| 1007 |
} |
| 1008 |
|
| 1009 |
TEST_P(End2endTest, ResponseStream) { |
| 1010 |
ResetStub(); |
| 1011 |
EchoRequest request; |
| 1012 |
EchoResponse response; |
| 1013 |
ClientContext context; |
| 1014 |
request.set_message("hello"); |
| 1015 |
|
| 1016 |
auto stream = stub_->ResponseStream(&context, request); |
| 1017 |
for (int i = 0; i < kServerDefaultResponseStreamsToSend; ++i) { |
| 1018 |
EXPECT_TRUE(stream->Read(&response)); |
| 1019 |
EXPECT_EQ(response.message(), request.message() + std::to_string(i)); |
| 1020 |
} |
| 1021 |
EXPECT_FALSE(stream->Read(&response)); |
| 1022 |
|
| 1023 |
Status s = stream->Finish(); |
| 1024 |
EXPECT_TRUE(s.ok()); |
| 1025 |
} |
| 1026 |
|
| 1027 |
TEST_P(End2endTest, ResponseStreamWithCoalescingApi) { |
| 1028 |
ResetStub(); |
| 1029 |
EchoRequest request; |
| 1030 |
EchoResponse response; |
| 1031 |
ClientContext context; |
| 1032 |
request.set_message("hello"); |
| 1033 |
context.AddMetadata(kServerUseCoalescingApi, "1"); |
| 1034 |
|
| 1035 |
auto stream = stub_->ResponseStream(&context, request); |
| 1036 |
for (int i = 0; i < kServerDefaultResponseStreamsToSend; ++i) { |
| 1037 |
EXPECT_TRUE(stream->Read(&response)); |
| 1038 |
EXPECT_EQ(response.message(), request.message() + std::to_string(i)); |
| 1039 |
} |
| 1040 |
EXPECT_FALSE(stream->Read(&response)); |
| 1041 |
|
| 1042 |
Status s = stream->Finish(); |
| 1043 |
EXPECT_TRUE(s.ok()); |
| 1044 |
} |
| 1045 |
|
| 1046 |
// This was added to prevent regression from issue: |
| 1047 |
// https://github.com/grpc/grpc/issues/11546 |
| 1048 |
TEST_P(End2endTest, ResponseStreamWithEverythingCoalesced) { |
| 1049 |
ResetStub(); |
| 1050 |
EchoRequest request; |
| 1051 |
EchoResponse response; |
| 1052 |
ClientContext context; |
| 1053 |
request.set_message("hello"); |
| 1054 |
context.AddMetadata(kServerUseCoalescingApi, "1"); |
| 1055 |
// We will only send one message, forcing everything (init metadata, message, |
| 1056 |
// trailing) to be coalesced together. |
| 1057 |
context.AddMetadata(kServerResponseStreamsToSend, "1"); |
| 1058 |
|
| 1059 |
auto stream = stub_->ResponseStream(&context, request); |
| 1060 |
EXPECT_TRUE(stream->Read(&response)); |
| 1061 |
EXPECT_EQ(response.message(), request.message() + "0"); |
| 1062 |
|
| 1063 |
EXPECT_FALSE(stream->Read(&response)); |
| 1064 |
|
| 1065 |
Status s = stream->Finish(); |
| 1066 |
EXPECT_TRUE(s.ok()); |
| 1067 |
} |
| 1068 |
|
| 1069 |
TEST_P(End2endTest, BidiStream) { |
| 1070 |
ResetStub(); |
| 1071 |
EchoRequest request; |
| 1072 |
EchoResponse response; |
| 1073 |
ClientContext context; |
| 1074 |
std::string msg("hello"); |
| 1075 |
|
| 1076 |
auto stream = stub_->BidiStream(&context); |
| 1077 |
|
| 1078 |
for (int i = 0; i < kServerDefaultResponseStreamsToSend; ++i) { |
| 1079 |
request.set_message(msg + std::to_string(i)); |
| 1080 |
EXPECT_TRUE(stream->Write(request)); |
| 1081 |
EXPECT_TRUE(stream->Read(&response)); |
| 1082 |
EXPECT_EQ(response.message(), request.message()); |
| 1083 |
} |
| 1084 |
|
| 1085 |
stream->WritesDone(); |
| 1086 |
EXPECT_FALSE(stream->Read(&response)); |
| 1087 |
EXPECT_FALSE(stream->Read(&response)); |
| 1088 |
|
| 1089 |
Status s = stream->Finish(); |
| 1090 |
EXPECT_TRUE(s.ok()); |
| 1091 |
} |
| 1092 |
|
| 1093 |
TEST_P(End2endTest, BidiStreamWithCoalescingApi) { |
| 1094 |
ResetStub(); |
| 1095 |
EchoRequest request; |
| 1096 |
EchoResponse response; |
| 1097 |
ClientContext context; |
| 1098 |
context.AddMetadata(kServerFinishAfterNReads, "3"); |
| 1099 |
context.set_initial_metadata_corked(true); |
| 1100 |
std::string msg("hello"); |
| 1101 |
|
| 1102 |
auto stream = stub_->BidiStream(&context); |
| 1103 |
|
| 1104 |
request.set_message(msg + "0"); |
| 1105 |
EXPECT_TRUE(stream->Write(request)); |
| 1106 |
EXPECT_TRUE(stream->Read(&response)); |
| 1107 |
EXPECT_EQ(response.message(), request.message()); |
| 1108 |
|
| 1109 |
request.set_message(msg + "1"); |
| 1110 |
EXPECT_TRUE(stream->Write(request)); |
| 1111 |
EXPECT_TRUE(stream->Read(&response)); |
| 1112 |
EXPECT_EQ(response.message(), request.message()); |
| 1113 |
|
| 1114 |
request.set_message(msg + "2"); |
| 1115 |
stream->WriteLast(request, WriteOptions()); |
| 1116 |
EXPECT_TRUE(stream->Read(&response)); |
| 1117 |
EXPECT_EQ(response.message(), request.message()); |
| 1118 |
|
| 1119 |
EXPECT_FALSE(stream->Read(&response)); |
| 1120 |
EXPECT_FALSE(stream->Read(&response)); |
| 1121 |
|
| 1122 |
Status s = stream->Finish(); |
| 1123 |
EXPECT_TRUE(s.ok()); |
| 1124 |
} |
| 1125 |
|
| 1126 |
// This was added to prevent regression from issue: |
| 1127 |
// https://github.com/grpc/grpc/issues/11546 |
| 1128 |
TEST_P(End2endTest, BidiStreamWithEverythingCoalesced) { |
| 1129 |
ResetStub(); |
| 1130 |
EchoRequest request; |
| 1131 |
EchoResponse response; |
| 1132 |
ClientContext context; |
| 1133 |
context.AddMetadata(kServerFinishAfterNReads, "1"); |
| 1134 |
context.set_initial_metadata_corked(true); |
| 1135 |
std::string msg("hello"); |
| 1136 |
|
| 1137 |
auto stream = stub_->BidiStream(&context); |
| 1138 |
|
| 1139 |
request.set_message(msg + "0"); |
| 1140 |
stream->WriteLast(request, WriteOptions()); |
| 1141 |
EXPECT_TRUE(stream->Read(&response)); |
| 1142 |
EXPECT_EQ(response.message(), request.message()); |
| 1143 |
|
| 1144 |
EXPECT_FALSE(stream->Read(&response)); |
| 1145 |
EXPECT_FALSE(stream->Read(&response)); |
| 1146 |
|
| 1147 |
Status s = stream->Finish(); |
| 1148 |
EXPECT_TRUE(s.ok()); |
| 1149 |
} |
| 1150 |
|
| 1151 |
// Talk to the two services with the same name but different package names. |
| 1152 |
// The two stubs are created on the same channel. |
| 1153 |
TEST_P(End2endTest, DiffPackageServices) { |
| 1154 |
ResetStub(); |
| 1155 |
EchoRequest request; |
| 1156 |
EchoResponse response; |
| 1157 |
request.set_message("Hello"); |
| 1158 |
|
| 1159 |
ClientContext context; |
| 1160 |
Status s = stub_->Echo(&context, request, &response); |
| 1161 |
EXPECT_EQ(response.message(), request.message()); |
| 1162 |
EXPECT_TRUE(s.ok()); |
| 1163 |
|
| 1164 |
std::unique_ptr<grpc::testing::duplicate::EchoTestService::Stub> dup_pkg_stub( |
| 1165 |
grpc::testing::duplicate::EchoTestService::NewStub(channel_)); |
| 1166 |
ClientContext context2; |
| 1167 |
s = dup_pkg_stub->Echo(&context2, request, &response); |
| 1168 |
EXPECT_EQ("no package", response.message()); |
| 1169 |
EXPECT_TRUE(s.ok()); |
| 1170 |
} |
| 1171 |
|
| 1172 |
template <class ServiceType> |
| 1173 |
void CancelRpc(ClientContext* context, int delay_us, ServiceType* service) { |
| 1174 |
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 1175 |
gpr_time_from_micros(delay_us, GPR_TIMESPAN))); |
| 1176 |
while (!service->signal_client()) { |
| 1177 |
} |
| 1178 |
context->TryCancel(); |
| 1179 |
} |
| 1180 |
|
| 1181 |
TEST_P(End2endTest, CancelRpcBeforeStart) { |
| 1182 |
ResetStub(); |
| 1183 |
EchoRequest request; |
| 1184 |
EchoResponse response; |
| 1185 |
ClientContext context; |
| 1186 |
request.set_message("hello"); |
| 1187 |
context.TryCancel(); |
| 1188 |
Status s = stub_->Echo(&context, request, &response); |
| 1189 |
EXPECT_EQ("", response.message()); |
| 1190 |
EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 1191 |
if (GetParam().use_interceptors()) { |
| 1192 |
EXPECT_EQ(20, PhonyInterceptor::GetNumTimesCancel()); |
| 1193 |
} |
| 1194 |
} |
| 1195 |
|
| 1196 |
TEST_P(End2endTest, CancelRpcAfterStart) { |
| 1197 |
ResetStub(); |
| 1198 |
EchoRequest request; |
| 1199 |
EchoResponse response; |
| 1200 |
ClientContext context; |
| 1201 |
request.set_message("hello"); |
| 1202 |
request.mutable_param()->set_server_notify_client_when_started(true); |
| 1203 |
request.mutable_param()->set_skip_cancelled_check(true); |
| 1204 |
Status s; |
| 1205 |
std::thread echo_thread([this, &s, &context, &request, &response] { |
| 1206 |
s = stub_->Echo(&context, request, &response); |
| 1207 |
EXPECT_EQ(StatusCode::CANCELLED, s.error_code()); |
| 1208 |
}); |
| 1209 |
if (!GetParam().callback_server()) { |
| 1210 |
service_.ClientWaitUntilRpcStarted(); |
| 1211 |
} else { |
| 1212 |
callback_service_.ClientWaitUntilRpcStarted(); |
| 1213 |
} |
| 1214 |
|
| 1215 |
context.TryCancel(); |
| 1216 |
|
| 1217 |
if (!GetParam().callback_server()) { |
| 1218 |
service_.SignalServerToContinue(); |
| 1219 |
} else { |
| 1220 |
callback_service_.SignalServerToContinue(); |
| 1221 |
} |
| 1222 |
|
| 1223 |
echo_thread.join(); |
| 1224 |
EXPECT_EQ("", response.message()); |
| 1225 |
EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 1226 |
if (GetParam().use_interceptors()) { |
| 1227 |
EXPECT_EQ(20, PhonyInterceptor::GetNumTimesCancel()); |
| 1228 |
} |
| 1229 |
} |
| 1230 |
|
| 1231 |
// Client cancels request stream after sending two messages |
| 1232 |
TEST_P(End2endTest, ClientCancelsRequestStream) { |
| 1233 |
ResetStub(); |
| 1234 |
EchoRequest request; |
| 1235 |
EchoResponse response; |
| 1236 |
ClientContext context; |
| 1237 |
request.set_message("hello"); |
| 1238 |
|
| 1239 |
auto stream = stub_->RequestStream(&context, &response); |
| 1240 |
EXPECT_TRUE(stream->Write(request)); |
| 1241 |
EXPECT_TRUE(stream->Write(request)); |
| 1242 |
|
| 1243 |
context.TryCancel(); |
| 1244 |
|
| 1245 |
Status s = stream->Finish(); |
| 1246 |
EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 1247 |
|
| 1248 |
EXPECT_EQ(response.message(), ""); |
| 1249 |
if (GetParam().use_interceptors()) { |
| 1250 |
EXPECT_EQ(20, PhonyInterceptor::GetNumTimesCancel()); |
| 1251 |
} |
| 1252 |
} |
| 1253 |
|
| 1254 |
// Client cancels server stream after sending some messages |
| 1255 |
TEST_P(End2endTest, ClientCancelsResponseStream) { |
| 1256 |
ResetStub(); |
| 1257 |
EchoRequest request; |
| 1258 |
EchoResponse response; |
| 1259 |
ClientContext context; |
| 1260 |
request.set_message("hello"); |
| 1261 |
|
| 1262 |
auto stream = stub_->ResponseStream(&context, request); |
| 1263 |
|
| 1264 |
EXPECT_TRUE(stream->Read(&response)); |
| 1265 |
EXPECT_EQ(response.message(), request.message() + "0"); |
| 1266 |
EXPECT_TRUE(stream->Read(&response)); |
| 1267 |
EXPECT_EQ(response.message(), request.message() + "1"); |
| 1268 |
|
| 1269 |
context.TryCancel(); |
| 1270 |
|
| 1271 |
// The cancellation races with responses, so there might be zero or |
| 1272 |
// one responses pending, read till failure |
| 1273 |
|
| 1274 |
if (stream->Read(&response)) { |
| 1275 |
EXPECT_EQ(response.message(), request.message() + "2"); |
| 1276 |
// Since we have cancelled, we expect the next attempt to read to fail |
| 1277 |
EXPECT_FALSE(stream->Read(&response)); |
| 1278 |
} |
| 1279 |
|
| 1280 |
Status s = stream->Finish(); |
| 1281 |
// The final status could be either of CANCELLED or OK depending on |
| 1282 |
// who won the race. |
| 1283 |
EXPECT_GE(grpc::StatusCode::CANCELLED, s.error_code()); |
| 1284 |
if (GetParam().use_interceptors()) { |
| 1285 |
EXPECT_EQ(20, PhonyInterceptor::GetNumTimesCancel()); |
| 1286 |
} |
| 1287 |
} |
| 1288 |
|
| 1289 |
// Client cancels bidi stream after sending some messages |
| 1290 |
TEST_P(End2endTest, ClientCancelsBidi) { |
| 1291 |
ResetStub(); |
| 1292 |
EchoRequest request; |
| 1293 |
EchoResponse response; |
| 1294 |
ClientContext context; |
| 1295 |
std::string msg("hello"); |
| 1296 |
|
| 1297 |
// Send server_try_cancel value in the client metadata |
| 1298 |
context.AddMetadata(kClientTryCancelRequest, std::to_string(1)); |
| 1299 |
|
| 1300 |
auto stream = stub_->BidiStream(&context); |
| 1301 |
|
| 1302 |
request.set_message(msg + "0"); |
| 1303 |
EXPECT_TRUE(stream->Write(request)); |
| 1304 |
EXPECT_TRUE(stream->Read(&response)); |
| 1305 |
EXPECT_EQ(response.message(), request.message()); |
| 1306 |
|
| 1307 |
request.set_message(msg + "1"); |
| 1308 |
EXPECT_TRUE(stream->Write(request)); |
| 1309 |
|
| 1310 |
context.TryCancel(); |
| 1311 |
|
| 1312 |
// The cancellation races with responses, so there might be zero or |
| 1313 |
// one responses pending, read till failure |
| 1314 |
|
| 1315 |
if (stream->Read(&response)) { |
| 1316 |
EXPECT_EQ(response.message(), request.message()); |
| 1317 |
// Since we have cancelled, we expect the next attempt to read to fail |
| 1318 |
EXPECT_FALSE(stream->Read(&response)); |
| 1319 |
} |
| 1320 |
|
| 1321 |
Status s = stream->Finish(); |
| 1322 |
EXPECT_EQ(grpc::StatusCode::CANCELLED, s.error_code()); |
| 1323 |
if (GetParam().use_interceptors()) { |
| 1324 |
EXPECT_EQ(20, PhonyInterceptor::GetNumTimesCancel()); |
| 1325 |
} |
| 1326 |
} |
| 1327 |
|
| 1328 |
TEST_P(End2endTest, RpcMaxMessageSize) { |
| 1329 |
ResetStub(); |
| 1330 |
EchoRequest request; |
| 1331 |
EchoResponse response; |
| 1332 |
request.set_message(string(kMaxMessageSize_ * 2, 'a')); |
| 1333 |
request.mutable_param()->set_server_die(true); |
| 1334 |
|
| 1335 |
ClientContext context; |
| 1336 |
Status s = stub_->Echo(&context, request, &response); |
| 1337 |
EXPECT_FALSE(s.ok()); |
| 1338 |
} |
| 1339 |
|
| 1340 |
void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream, |
| 1341 |
gpr_event* ev) { |
| 1342 |
EchoResponse resp; |
| 1343 |
gpr_event_set(ev, reinterpret_cast<void*>(1)); |
| 1344 |
while (stream->Read(&resp)) { |
| 1345 |
gpr_log(GPR_INFO, "Read message"); |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
// Run a Read and a WritesDone simultaneously. |
| 1350 |
TEST_P(End2endTest, SimultaneousReadWritesDone) { |
| 1351 |
ResetStub(); |
| 1352 |
ClientContext context; |
| 1353 |
gpr_event ev; |
| 1354 |
gpr_event_init(&ev); |
| 1355 |
auto stream = stub_->BidiStream(&context); |
| 1356 |
std::thread reader_thread(ReaderThreadFunc, stream.get(), &ev); |
| 1357 |
gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME)); |
| 1358 |
stream->WritesDone(); |
| 1359 |
reader_thread.join(); |
| 1360 |
Status s = stream->Finish(); |
| 1361 |
EXPECT_TRUE(s.ok()); |
| 1362 |
} |
| 1363 |
|
| 1364 |
TEST_P(End2endTest, ChannelState) { |
| 1365 |
if (GetParam().inproc()) { |
| 1366 |
return; |
| 1367 |
} |
| 1368 |
|
| 1369 |
ResetStub(); |
| 1370 |
// Start IDLE |
| 1371 |
EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(false)); |
| 1372 |
|
| 1373 |
// Did not ask to connect, no state change. |
| 1374 |
CompletionQueue cq; |
| 1375 |
std::chrono::system_clock::time_point deadline = |
| 1376 |
std::chrono::system_clock::now() + std::chrono::milliseconds(10); |
| 1377 |
channel_->NotifyOnStateChange(GRPC_CHANNEL_IDLE, deadline, &cq, nullptr); |
| 1378 |
void* tag; |
| 1379 |
bool ok = true; |
| 1380 |
cq.Next(&tag, &ok); |
| 1381 |
EXPECT_FALSE(ok); |
| 1382 |
|
| 1383 |
EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true)); |
| 1384 |
EXPECT_TRUE(channel_->WaitForStateChange(GRPC_CHANNEL_IDLE, |
| 1385 |
gpr_inf_future(GPR_CLOCK_REALTIME))); |
| 1386 |
auto state = channel_->GetState(false); |
| 1387 |
EXPECT_TRUE(state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_READY); |
| 1388 |
} |
| 1389 |
|
| 1390 |
// Takes 10s. |
| 1391 |
TEST_P(End2endTest, ChannelStateTimeout) { |
| 1392 |
if ((GetParam().credentials_type() != kInsecureCredentialsType) || |
| 1393 |
GetParam().inproc()) { |
| 1394 |
return; |
| 1395 |
} |
| 1396 |
int port = grpc_pick_unused_port_or_die(); |
| 1397 |
std::ostringstream server_address; |
| 1398 |
server_address << "localhost:" << port; |
| 1399 |
// Channel to non-existing server |
| 1400 |
auto channel = |
| 1401 |
grpc::CreateChannel(server_address.str(), InsecureChannelCredentials()); |
| 1402 |
// Start IDLE |
| 1403 |
EXPECT_EQ(GRPC_CHANNEL_IDLE, channel->GetState(true)); |
| 1404 |
|
| 1405 |
auto state = GRPC_CHANNEL_IDLE; |
| 1406 |
for (int i = 0; i < 10; i++) { |
| 1407 |
channel->WaitForStateChange( |
| 1408 |
state, std::chrono::system_clock::now() + std::chrono::seconds(1)); |
| 1409 |
state = channel->GetState(false); |
| 1410 |
} |
| 1411 |
} |
| 1412 |
|
| 1413 |
TEST_P(End2endTest, ChannelStateOnLameChannel) { |
| 1414 |
if ((GetParam().credentials_type() != kInsecureCredentialsType) || |
| 1415 |
GetParam().inproc()) { |
| 1416 |
return; |
| 1417 |
} |
| 1418 |
// Channel using invalid target URI. This creates a lame channel. |
| 1419 |
auto channel = grpc::CreateChannel("dns:///", InsecureChannelCredentials()); |
| 1420 |
// Channel should immediately report TRANSIENT_FAILURE. |
| 1421 |
EXPECT_EQ(GRPC_CHANNEL_TRANSIENT_FAILURE, channel->GetState(true)); |
| 1422 |
// And state will never change. |
| 1423 |
auto state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
| 1424 |
for (int i = 0; i < 10; ++i) { |
| 1425 |
channel->WaitForStateChange( |
| 1426 |
state, std::chrono::system_clock::now() + std::chrono::seconds(1)); |
| 1427 |
state = channel->GetState(false); |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
// Talking to a non-existing service. |
| 1432 |
TEST_P(End2endTest, NonExistingService) { |
| 1433 |
ResetChannel(); |
| 1434 |
std::unique_ptr<grpc::testing::UnimplementedEchoService::Stub> stub; |
| 1435 |
stub = grpc::testing::UnimplementedEchoService::NewStub(channel_); |
| 1436 |
|
| 1437 |
EchoRequest request; |
| 1438 |
EchoResponse response; |
| 1439 |
request.set_message("Hello"); |
| 1440 |
|
| 1441 |
ClientContext context; |
| 1442 |
Status s = stub->Unimplemented(&context, request, &response); |
| 1443 |
EXPECT_EQ(StatusCode::UNIMPLEMENTED, s.error_code()); |
| 1444 |
EXPECT_EQ("", s.error_message()); |
| 1445 |
} |
| 1446 |
|
| 1447 |
// Ask the server to send back a serialized proto in trailer. |
| 1448 |
// This is an example of setting error details. |
| 1449 |
TEST_P(End2endTest, BinaryTrailerTest) { |
| 1450 |
ResetStub(); |
| 1451 |
EchoRequest request; |
| 1452 |
EchoResponse response; |
| 1453 |
ClientContext context; |
| 1454 |
|
| 1455 |
request.mutable_param()->set_echo_metadata(true); |
| 1456 |
DebugInfo* info = request.mutable_param()->mutable_debug_info(); |
| 1457 |
info->add_stack_entries("stack_entry_1"); |
| 1458 |
info->add_stack_entries("stack_entry_2"); |
| 1459 |
info->add_stack_entries("stack_entry_3"); |
| 1460 |
info->set_detail("detailed debug info"); |
| 1461 |
std::string expected_string = info->SerializeAsString(); |
| 1462 |
request.set_message("Hello"); |
| 1463 |
|
| 1464 |
Status s = stub_->Echo(&context, request, &response); |
| 1465 |
EXPECT_FALSE(s.ok()); |
| 1466 |
auto trailers = context.GetServerTrailingMetadata(); |
| 1467 |
EXPECT_EQ(1u, trailers.count(kDebugInfoTrailerKey)); |
| 1468 |
auto iter = trailers.find(kDebugInfoTrailerKey); |
| 1469 |
EXPECT_EQ(expected_string, iter->second); |
| 1470 |
// Parse the returned trailer into a DebugInfo proto. |
| 1471 |
DebugInfo returned_info; |
| 1472 |
EXPECT_TRUE(returned_info.ParseFromString(ToString(iter->second))); |
| 1473 |
} |
| 1474 |
|
| 1475 |
TEST_P(End2endTest, ExpectErrorTest) { |
| 1476 |
ResetStub(); |
| 1477 |
|
| 1478 |
std::vector<ErrorStatus> expected_status; |
| 1479 |
expected_status.emplace_back(); |
| 1480 |
expected_status.back().set_code(13); // INTERNAL |
| 1481 |
// No Error message or details |
| 1482 |
|
| 1483 |
expected_status.emplace_back(); |
| 1484 |
expected_status.back().set_code(13); // INTERNAL |
| 1485 |
expected_status.back().set_error_message("text error message"); |
| 1486 |
expected_status.back().set_binary_error_details("text error details"); |
| 1487 |
|
| 1488 |
expected_status.emplace_back(); |
| 1489 |
expected_status.back().set_code(13); // INTERNAL |
| 1490 |
expected_status.back().set_error_message("text error message"); |
| 1491 |
expected_status.back().set_binary_error_details( |
| 1492 |
"\x0\x1\x2\x3\x4\x5\x6\x8\x9\xA\xB"); |
| 1493 |
|
| 1494 |
for (auto iter = expected_status.begin(); iter != expected_status.end(); |
| 1495 |
++iter) { |
| 1496 |
EchoRequest request; |
| 1497 |
EchoResponse response; |
| 1498 |
ClientContext context; |
| 1499 |
request.set_message("Hello"); |
| 1500 |
auto* error = request.mutable_param()->mutable_expected_error(); |
| 1501 |
error->set_code(iter->code()); |
| 1502 |
error->set_error_message(iter->error_message()); |
| 1503 |
error->set_binary_error_details(iter->binary_error_details()); |
| 1504 |
|
| 1505 |
Status s = stub_->Echo(&context, request, &response); |
| 1506 |
EXPECT_FALSE(s.ok()); |
| 1507 |
EXPECT_EQ(iter->code(), s.error_code()); |
| 1508 |
EXPECT_EQ(iter->error_message(), s.error_message()); |
| 1509 |
EXPECT_EQ(iter->binary_error_details(), s.error_details()); |
| 1510 |
EXPECT_TRUE(absl::StrContains(context.debug_error_string(), "created")); |
| 1511 |
#ifndef NDEBUG |
| 1512 |
// grpc_core::StatusIntProperty::kFileLine is for debug only |
| 1513 |
EXPECT_TRUE(absl::StrContains(context.debug_error_string(), "file")); |
| 1514 |
EXPECT_TRUE(absl::StrContains(context.debug_error_string(), "line")); |
| 1515 |
#endif |
| 1516 |
EXPECT_TRUE(absl::StrContains(context.debug_error_string(), "status")); |
| 1517 |
EXPECT_TRUE(absl::StrContains(context.debug_error_string(), "13")); |
| 1518 |
} |
| 1519 |
} |
| 1520 |
|
| 1521 |
////////////////////////////////////////////////////////////////////////// |
| 1522 |
// Test with and without a proxy. |
| 1523 |
class ProxyEnd2endTest : public End2endTest { |
| 1524 |
protected: |
| 1525 |
}; |
| 1526 |
|
| 1527 |
TEST_P(ProxyEnd2endTest, SimpleRpc) { |
| 1528 |
ResetStub(); |
| 1529 |
SendRpc(stub_.get(), 1, false); |
| 1530 |
} |
| 1531 |
|
| 1532 |
TEST_P(ProxyEnd2endTest, SimpleRpcWithEmptyMessages) { |
| 1533 |
ResetStub(); |
| 1534 |
EchoRequest request; |
| 1535 |
EchoResponse response; |
| 1536 |
|
| 1537 |
ClientContext context; |
| 1538 |
Status s = stub_->Echo(&context, request, &response); |
| 1539 |
EXPECT_TRUE(s.ok()); |
| 1540 |
} |
| 1541 |
|
| 1542 |
TEST_P(ProxyEnd2endTest, MultipleRpcs) { |
| 1543 |
ResetStub(); |
| 1544 |
std::vector<std::thread> threads; |
| 1545 |
threads.reserve(10); |
| 1546 |
for (int i = 0; i < 10; ++i) { |
| 1547 |
threads.emplace_back(SendRpc, stub_.get(), 10, false); |
| 1548 |
} |
| 1549 |
for (int i = 0; i < 10; ++i) { |
| 1550 |
threads[i].join(); |
| 1551 |
} |
| 1552 |
} |
| 1553 |
|
| 1554 |
// Set a 10us deadline and make sure proper error is returned. |
| 1555 |
TEST_P(ProxyEnd2endTest, RpcDeadlineExpires) { |
| 1556 |
ResetStub(); |
| 1557 |
EchoRequest request; |
| 1558 |
EchoResponse response; |
| 1559 |
request.set_message("Hello"); |
| 1560 |
request.mutable_param()->set_skip_cancelled_check(true); |
| 1561 |
// Let server sleep for 4 secs first to guarantee expiry. |
| 1562 |
// 4 secs might seem a bit extreme but the timer manager would have been just |
| 1563 |
// initialized (when ResetStub() was called) and there are some warmup costs |
| 1564 |
// i.e the timer thread many not have even started. There might also be other |
| 1565 |
// delays in the timer manager thread (in acquiring locks, timer data |
| 1566 |
// structure manipulations, starting backup timer threads) that add to the |
| 1567 |
// delays. 4 secs might be still not enough in some cases but this |
| 1568 |
// significantly reduces the test flakes |
| 1569 |
request.mutable_param()->set_server_sleep_us(4 * 1000 * 1000); |
| 1570 |
|
| 1571 |
ClientContext context; |
| 1572 |
std::chrono::system_clock::time_point deadline = |
| 1573 |
std::chrono::system_clock::now() + std::chrono::milliseconds(1); |
| 1574 |
context.set_deadline(deadline); |
| 1575 |
Status s = stub_->Echo(&context, request, &response); |
| 1576 |
EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.error_code()); |
| 1577 |
} |
| 1578 |
|
| 1579 |
// Set a long but finite deadline. |
| 1580 |
TEST_P(ProxyEnd2endTest, RpcLongDeadline) { |
| 1581 |
ResetStub(); |
| 1582 |
EchoRequest request; |
| 1583 |
EchoResponse response; |
| 1584 |
request.set_message("Hello"); |
| 1585 |
|
| 1586 |
ClientContext context; |
| 1587 |
std::chrono::system_clock::time_point deadline = |
| 1588 |
std::chrono::system_clock::now() + std::chrono::hours(1); |
| 1589 |
context.set_deadline(deadline); |
| 1590 |
Status s = stub_->Echo(&context, request, &response); |
| 1591 |
EXPECT_EQ(response.message(), request.message()); |
| 1592 |
EXPECT_TRUE(s.ok()); |
| 1593 |
} |
| 1594 |
|
| 1595 |
// Ask server to echo back the deadline it sees. |
| 1596 |
TEST_P(ProxyEnd2endTest, EchoDeadline) { |
| 1597 |
ResetStub(); |
| 1598 |
EchoRequest request; |
| 1599 |
EchoResponse response; |
| 1600 |
request.set_message("Hello"); |
| 1601 |
request.mutable_param()->set_echo_deadline(true); |
| 1602 |
|
| 1603 |
ClientContext context; |
| 1604 |
std::chrono::system_clock::time_point deadline = |
| 1605 |
std::chrono::system_clock::now() + std::chrono::seconds(100); |
| 1606 |
context.set_deadline(deadline); |
| 1607 |
Status s = stub_->Echo(&context, request, &response); |
| 1608 |
EXPECT_EQ(response.message(), request.message()); |
| 1609 |
EXPECT_TRUE(s.ok()); |
| 1610 |
gpr_timespec sent_deadline; |
| 1611 |
Timepoint2Timespec(deadline, &sent_deadline); |
| 1612 |
// We want to allow some reasonable error given: |
| 1613 |
// - request_deadline() only has 1sec resolution so the best we can do is +-1 |
| 1614 |
// - if sent_deadline.tv_nsec is very close to the next second's boundary we |
| 1615 |
// can end up being off by 2 in one direction. |
| 1616 |
EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 2); |
| 1617 |
EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1); |
| 1618 |
} |
| 1619 |
|
| 1620 |
// Ask server to echo back the deadline it sees. The rpc has no deadline. |
| 1621 |
TEST_P(ProxyEnd2endTest, EchoDeadlineForNoDeadlineRpc) { |
| 1622 |
ResetStub(); |
| 1623 |
EchoRequest request; |
| 1624 |
EchoResponse response; |
| 1625 |
request.set_message("Hello"); |
| 1626 |
request.mutable_param()->set_echo_deadline(true); |
| 1627 |
|
| 1628 |
ClientContext context; |
| 1629 |
Status s = stub_->Echo(&context, request, &response); |
| 1630 |
EXPECT_EQ(response.message(), request.message()); |
| 1631 |
EXPECT_TRUE(s.ok()); |
| 1632 |
EXPECT_EQ(response.param().request_deadline(), |
| 1633 |
gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec); |
| 1634 |
} |
| 1635 |
|
| 1636 |
TEST_P(ProxyEnd2endTest, UnimplementedRpc) { |
| 1637 |
ResetStub(); |
| 1638 |
EchoRequest request; |
| 1639 |
EchoResponse response; |
| 1640 |
request.set_message("Hello"); |
| 1641 |
|
| 1642 |
ClientContext context; |
| 1643 |
Status s = stub_->Unimplemented(&context, request, &response); |
| 1644 |
EXPECT_FALSE(s.ok()); |
| 1645 |
EXPECT_EQ(s.error_code(), grpc::StatusCode::UNIMPLEMENTED); |
| 1646 |
EXPECT_EQ(s.error_message(), ""); |
| 1647 |
EXPECT_EQ(response.message(), ""); |
| 1648 |
} |
| 1649 |
|
| 1650 |
// Client cancels rpc after 10ms |
| 1651 |
TEST_P(ProxyEnd2endTest, ClientCancelsRpc) { |
| 1652 |
ResetStub(); |
| 1653 |
EchoRequest request; |
| 1654 |
EchoResponse response; |
| 1655 |
request.set_message("Hello"); |
| 1656 |
const int kCancelDelayUs = 10 * 1000; |
| 1657 |
request.mutable_param()->set_client_cancel_after_us(kCancelDelayUs); |
| 1658 |
|
| 1659 |
ClientContext context; |
| 1660 |
std::thread cancel_thread; |
| 1661 |
if (!GetParam().callback_server()) { |
| 1662 |
cancel_thread = std::thread( |
| 1663 |
[&context, this](int delay) { CancelRpc(&context, delay, &service_); }, |
| 1664 |
kCancelDelayUs); |
| 1665 |
// Note: the unusual pattern above (and below) is caused by a conflict |
| 1666 |
// between two sets of compiler expectations. clang allows const to be |
| 1667 |
// captured without mention, so there is no need to capture kCancelDelayUs |
| 1668 |
// (and indeed clang-tidy complains if you do so). OTOH, a Windows compiler |
| 1669 |
// in our tests requires an explicit capture even for const. We square this |
| 1670 |
// circle by passing the const value in as an argument to the lambda. |
| 1671 |
} else { |
| 1672 |
cancel_thread = std::thread( |
| 1673 |
[&context, this](int delay) { |
| 1674 |
CancelRpc(&context, delay, &callback_service_); |
| 1675 |
}, |
| 1676 |
kCancelDelayUs); |
| 1677 |
} |
| 1678 |
Status s = stub_->Echo(&context, request, &response); |
| 1679 |
cancel_thread.join(); |
| 1680 |
EXPECT_EQ(StatusCode::CANCELLED, s.error_code()); |
| 1681 |
EXPECT_EQ(s.error_message(), "CANCELLED"); |
| 1682 |
} |
| 1683 |
|
| 1684 |
// Server cancels rpc after 1ms |
| 1685 |
TEST_P(ProxyEnd2endTest, ServerCancelsRpc) { |
| 1686 |
ResetStub(); |
| 1687 |
EchoRequest request; |
| 1688 |
EchoResponse response; |
| 1689 |
request.set_message("Hello"); |
| 1690 |
request.mutable_param()->set_server_cancel_after_us(1000); |
| 1691 |
|
| 1692 |
ClientContext context; |
| 1693 |
Status s = stub_->Echo(&context, request, &response); |
| 1694 |
EXPECT_EQ(StatusCode::CANCELLED, s.error_code()); |
| 1695 |
EXPECT_TRUE(s.error_message().empty()); |
| 1696 |
} |
| 1697 |
|
| 1698 |
// Make the response larger than the flow control window. |
| 1699 |
TEST_P(ProxyEnd2endTest, HugeResponse) { |
| 1700 |
ResetStub(); |
| 1701 |
EchoRequest request; |
| 1702 |
EchoResponse response; |
| 1703 |
request.set_message("huge response"); |
| 1704 |
const size_t kResponseSize = 1024 * (1024 + 10); |
| 1705 |
request.mutable_param()->set_response_message_length(kResponseSize); |
| 1706 |
|
| 1707 |
ClientContext context; |
| 1708 |
std::chrono::system_clock::time_point deadline = |
| 1709 |
std::chrono::system_clock::now() + std::chrono::seconds(20); |
| 1710 |
context.set_deadline(deadline); |
| 1711 |
Status s = stub_->Echo(&context, request, &response); |
| 1712 |
EXPECT_EQ(kResponseSize, response.message().size()); |
| 1713 |
EXPECT_TRUE(s.ok()); |
| 1714 |
} |
| 1715 |
|
| 1716 |
TEST_P(ProxyEnd2endTest, Peer) { |
| 1717 |
// Peer is not meaningful for inproc |
| 1718 |
if (GetParam().inproc()) { |
| 1719 |
return; |
| 1720 |
} |
| 1721 |
ResetStub(); |
| 1722 |
EchoRequest request; |
| 1723 |
EchoResponse response; |
| 1724 |
request.set_message("hello"); |
| 1725 |
request.mutable_param()->set_echo_peer(true); |
| 1726 |
|
| 1727 |
ClientContext context; |
| 1728 |
Status s = stub_->Echo(&context, request, &response); |
| 1729 |
EXPECT_EQ(response.message(), request.message()); |
| 1730 |
EXPECT_TRUE(s.ok()); |
| 1731 |
EXPECT_TRUE(CheckIsLocalhost(response.param().peer())); |
| 1732 |
EXPECT_TRUE(CheckIsLocalhost(context.peer())); |
| 1733 |
} |
| 1734 |
|
| 1735 |
////////////////////////////////////////////////////////////////////////// |
| 1736 |
class SecureEnd2endTest : public End2endTest { |
| 1737 |
protected: |
| 1738 |
SecureEnd2endTest() { |
| 1739 |
GPR_ASSERT(!GetParam().use_proxy()); |
| 1740 |
GPR_ASSERT(GetParam().credentials_type() != kInsecureCredentialsType); |
| 1741 |
} |
| 1742 |
}; |
| 1743 |
|
| 1744 |
TEST_P(SecureEnd2endTest, SimpleRpcWithHost) { |
| 1745 |
ResetStub(); |
| 1746 |
|
| 1747 |
EchoRequest request; |
| 1748 |
EchoResponse response; |
| 1749 |
request.set_message("Hello"); |
| 1750 |
|
| 1751 |
ClientContext context; |
| 1752 |
context.set_authority("foo.test.youtube.com"); |
| 1753 |
Status s = stub_->Echo(&context, request, &response); |
| 1754 |
EXPECT_EQ(response.message(), request.message()); |
| 1755 |
EXPECT_TRUE(response.has_param()); |
| 1756 |
EXPECT_EQ("special", response.param().host()); |
| 1757 |
EXPECT_TRUE(s.ok()); |
| 1758 |
} |
| 1759 |
|
| 1760 |
bool MetadataContains( |
| 1761 |
const std::multimap<grpc::string_ref, grpc::string_ref>& metadata, |
| 1762 |
const std::string& key, const std::string& value) { |
| 1763 |
int count = 0; |
| 1764 |
|
| 1765 |
for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator iter = |
| 1766 |
metadata.begin(); |
| 1767 |
iter != metadata.end(); ++iter) { |
| 1768 |
if (ToString(iter->first) == key && ToString(iter->second) == value) { |
| 1769 |
count++; |
| 1770 |
} |
| 1771 |
} |
| 1772 |
return count == 1; |
| 1773 |
} |
| 1774 |
|
| 1775 |
TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorSuccess) { |
| 1776 |
auto* processor = new TestAuthMetadataProcessor(true); |
| 1777 |
StartServer(std::shared_ptr<AuthMetadataProcessor>(processor)); |
| 1778 |
ResetStub(); |
| 1779 |
EchoRequest request; |
| 1780 |
EchoResponse response; |
| 1781 |
ClientContext context; |
| 1782 |
context.set_credentials(processor->GetCompatibleClientCreds()); |
| 1783 |
request.set_message("Hello"); |
| 1784 |
request.mutable_param()->set_echo_metadata(true); |
| 1785 |
request.mutable_param()->set_expected_client_identity( |
| 1786 |
TestAuthMetadataProcessor::kGoodGuy); |
| 1787 |
request.mutable_param()->set_expected_transport_security_type( |
| 1788 |
GetParam().credentials_type()); |
| 1789 |
|
| 1790 |
Status s = stub_->Echo(&context, request, &response); |
| 1791 |
EXPECT_EQ(request.message(), response.message()); |
| 1792 |
EXPECT_TRUE(s.ok()); |
| 1793 |
|
| 1794 |
// Metadata should have been consumed by the processor. |
| 1795 |
EXPECT_FALSE(MetadataContains( |
| 1796 |
context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY, |
| 1797 |
std::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy)); |
| 1798 |
} |
| 1799 |
|
| 1800 |
TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginAndProcessorFailure) { |
| 1801 |
auto* processor = new TestAuthMetadataProcessor(true); |
| 1802 |
StartServer(std::shared_ptr<AuthMetadataProcessor>(processor)); |
| 1803 |
ResetStub(); |
| 1804 |
EchoRequest request; |
| 1805 |
EchoResponse response; |
| 1806 |
ClientContext context; |
| 1807 |
context.set_credentials(processor->GetIncompatibleClientCreds()); |
| 1808 |
request.set_message("Hello"); |
| 1809 |
|
| 1810 |
Status s = stub_->Echo(&context, request, &response); |
| 1811 |
EXPECT_FALSE(s.ok()); |
| 1812 |
EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED); |
| 1813 |
} |
| 1814 |
|
| 1815 |
TEST_P(SecureEnd2endTest, SetPerCallCredentials) { |
| 1816 |
ResetStub(); |
| 1817 |
EchoRequest request; |
| 1818 |
EchoResponse response; |
| 1819 |
ClientContext context; |
| 1820 |
std::shared_ptr<CallCredentials> creds = |
| 1821 |
GoogleIAMCredentials(kFakeToken, kFakeSelector); |
| 1822 |
context.set_credentials(creds); |
| 1823 |
request.set_message("Hello"); |
| 1824 |
request.mutable_param()->set_echo_metadata(true); |
| 1825 |
|
| 1826 |
Status s = stub_->Echo(&context, request, &response); |
| 1827 |
EXPECT_EQ(request.message(), response.message()); |
| 1828 |
EXPECT_TRUE(s.ok()); |
| 1829 |
EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1830 |
GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 1831 |
kFakeToken)); |
| 1832 |
EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1833 |
GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 1834 |
kFakeSelector)); |
| 1835 |
EXPECT_EQ(context.credentials()->DebugString(), |
| 1836 |
kExpectedFakeCredsDebugString); |
| 1837 |
} |
| 1838 |
|
| 1839 |
class CredentialsInterceptor : public experimental::Interceptor { |
| 1840 |
public: |
| 1841 |
explicit CredentialsInterceptor(experimental::ClientRpcInfo* info) |
| 1842 |
: info_(info) {} |
| 1843 |
|
| 1844 |
void Intercept(experimental::InterceptorBatchMethods* methods) override { |
| 1845 |
if (methods->QueryInterceptionHookPoint( |
| 1846 |
experimental::InterceptionHookPoints::PRE_SEND_INITIAL_METADATA)) { |
| 1847 |
std::shared_ptr<CallCredentials> creds = |
| 1848 |
GoogleIAMCredentials(kFakeToken, kFakeSelector); |
| 1849 |
info_->client_context()->set_credentials(creds); |
| 1850 |
} |
| 1851 |
methods->Proceed(); |
| 1852 |
} |
| 1853 |
|
| 1854 |
private: |
| 1855 |
experimental::ClientRpcInfo* info_ = nullptr; |
| 1856 |
}; |
| 1857 |
|
| 1858 |
class CredentialsInterceptorFactory |
| 1859 |
: public experimental::ClientInterceptorFactoryInterface { |
| 1860 |
CredentialsInterceptor* CreateClientInterceptor( |
| 1861 |
experimental::ClientRpcInfo* info) override { |
| 1862 |
return new CredentialsInterceptor(info); |
| 1863 |
} |
| 1864 |
}; |
| 1865 |
|
| 1866 |
TEST_P(SecureEnd2endTest, CallCredentialsInterception) { |
| 1867 |
if (!GetParam().use_interceptors()) { |
| 1868 |
return; |
| 1869 |
} |
| 1870 |
std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>> |
| 1871 |
interceptor_creators; |
| 1872 |
interceptor_creators.push_back( |
| 1873 |
std::make_unique<CredentialsInterceptorFactory>()); |
| 1874 |
ResetStub(std::move(interceptor_creators)); |
| 1875 |
EchoRequest request; |
| 1876 |
EchoResponse response; |
| 1877 |
ClientContext context; |
| 1878 |
|
| 1879 |
request.set_message("Hello"); |
| 1880 |
request.mutable_param()->set_echo_metadata(true); |
| 1881 |
|
| 1882 |
Status s = stub_->Echo(&context, request, &response); |
| 1883 |
EXPECT_EQ(request.message(), response.message()); |
| 1884 |
EXPECT_TRUE(s.ok()); |
| 1885 |
EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1886 |
GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 1887 |
kFakeToken)); |
| 1888 |
EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1889 |
GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 1890 |
kFakeSelector)); |
| 1891 |
EXPECT_EQ(context.credentials()->DebugString(), |
| 1892 |
kExpectedFakeCredsDebugString); |
| 1893 |
} |
| 1894 |
|
| 1895 |
TEST_P(SecureEnd2endTest, CallCredentialsInterceptionWithSetCredentials) { |
| 1896 |
if (!GetParam().use_interceptors()) { |
| 1897 |
return; |
| 1898 |
} |
| 1899 |
std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>> |
| 1900 |
interceptor_creators; |
| 1901 |
interceptor_creators.push_back( |
| 1902 |
std::make_unique<CredentialsInterceptorFactory>()); |
| 1903 |
ResetStub(std::move(interceptor_creators)); |
| 1904 |
EchoRequest request; |
| 1905 |
EchoResponse response; |
| 1906 |
ClientContext context; |
| 1907 |
std::shared_ptr<CallCredentials> creds1 = |
| 1908 |
GoogleIAMCredentials(kWrongToken, kWrongSelector); |
| 1909 |
context.set_credentials(creds1); |
| 1910 |
EXPECT_EQ(context.credentials(), creds1); |
| 1911 |
EXPECT_EQ(context.credentials()->DebugString(), |
| 1912 |
kExpectedWrongCredsDebugString); |
| 1913 |
request.set_message("Hello"); |
| 1914 |
request.mutable_param()->set_echo_metadata(true); |
| 1915 |
|
| 1916 |
Status s = stub_->Echo(&context, request, &response); |
| 1917 |
EXPECT_EQ(request.message(), response.message()); |
| 1918 |
EXPECT_TRUE(s.ok()); |
| 1919 |
EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1920 |
GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 1921 |
kFakeToken)); |
| 1922 |
EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1923 |
GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 1924 |
kFakeSelector)); |
| 1925 |
EXPECT_EQ(context.credentials()->DebugString(), |
| 1926 |
kExpectedFakeCredsDebugString); |
| 1927 |
} |
| 1928 |
|
| 1929 |
TEST_P(SecureEnd2endTest, OverridePerCallCredentials) { |
| 1930 |
ResetStub(); |
| 1931 |
EchoRequest request; |
| 1932 |
EchoResponse response; |
| 1933 |
ClientContext context; |
| 1934 |
std::shared_ptr<CallCredentials> creds1 = |
| 1935 |
GoogleIAMCredentials(kFakeToken1, kFakeSelector1); |
| 1936 |
context.set_credentials(creds1); |
| 1937 |
EXPECT_EQ(context.credentials(), creds1); |
| 1938 |
EXPECT_EQ(context.credentials()->DebugString(), |
| 1939 |
kExpectedFakeCreds1DebugString); |
| 1940 |
std::shared_ptr<CallCredentials> creds2 = |
| 1941 |
GoogleIAMCredentials(kFakeToken2, kFakeSelector2); |
| 1942 |
context.set_credentials(creds2); |
| 1943 |
EXPECT_EQ(context.credentials(), creds2); |
| 1944 |
request.set_message("Hello"); |
| 1945 |
request.mutable_param()->set_echo_metadata(true); |
| 1946 |
|
| 1947 |
Status s = stub_->Echo(&context, request, &response); |
| 1948 |
EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1949 |
GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 1950 |
kFakeToken2)); |
| 1951 |
EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1952 |
GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 1953 |
kFakeSelector2)); |
| 1954 |
EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1955 |
GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, |
| 1956 |
kFakeToken1)); |
| 1957 |
EXPECT_FALSE(MetadataContains(context.GetServerTrailingMetadata(), |
| 1958 |
GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, |
| 1959 |
kFakeSelector1)); |
| 1960 |
EXPECT_EQ(context.credentials()->DebugString(), |
| 1961 |
kExpectedFakeCreds2DebugString); |
| 1962 |
EXPECT_EQ(request.message(), response.message()); |
| 1963 |
EXPECT_TRUE(s.ok()); |
| 1964 |
} |
| 1965 |
|
| 1966 |
TEST_P(SecureEnd2endTest, AuthMetadataPluginKeyFailure) { |
| 1967 |
ResetStub(); |
| 1968 |
EchoRequest request; |
| 1969 |
EchoResponse response; |
| 1970 |
ClientContext context; |
| 1971 |
context.set_credentials(grpc::MetadataCredentialsFromPlugin( |
| 1972 |
std::unique_ptr<MetadataCredentialsPlugin>( |
| 1973 |
new TestMetadataCredentialsPlugin( |
| 1974 |
TestMetadataCredentialsPlugin::kBadMetadataKey, |
| 1975 |
"Does not matter, will fail the key is invalid.", false, true, |
| 1976 |
0)))); |
| 1977 |
request.set_message("Hello"); |
| 1978 |
|
| 1979 |
Status s = stub_->Echo(&context, request, &response); |
| 1980 |
EXPECT_FALSE(s.ok()); |
| 1981 |
EXPECT_EQ(s.error_code(), StatusCode::UNAVAILABLE); |
| 1982 |
EXPECT_EQ(context.credentials()->DebugString(), |
| 1983 |
kExpectedAuthMetadataPluginKeyFailureCredsDebugString); |
| 1984 |
} |
| 1985 |
|
| 1986 |
TEST_P(SecureEnd2endTest, AuthMetadataPluginValueFailure) { |
| 1987 |
ResetStub(); |
| 1988 |
EchoRequest request; |
| 1989 |
EchoResponse response; |
| 1990 |
ClientContext context; |
| 1991 |
context.set_credentials(grpc::MetadataCredentialsFromPlugin( |
| 1992 |
std::unique_ptr<MetadataCredentialsPlugin>( |
| 1993 |
new TestMetadataCredentialsPlugin( |
| 1994 |
TestMetadataCredentialsPlugin::kGoodMetadataKey, |
| 1995 |
"With illegal \n value.", false, true, 0)))); |
| 1996 |
request.set_message("Hello"); |
| 1997 |
|
| 1998 |
Status s = stub_->Echo(&context, request, &response); |
| 1999 |
EXPECT_FALSE(s.ok()); |
| 2000 |
EXPECT_EQ(s.error_code(), StatusCode::UNAVAILABLE); |
| 2001 |
EXPECT_EQ(context.credentials()->DebugString(), |
| 2002 |
kExpectedAuthMetadataPluginValueFailureCredsDebugString); |
| 2003 |
} |
| 2004 |
|
| 2005 |
TEST_P(SecureEnd2endTest, AuthMetadataPluginWithDeadline) { |
| 2006 |
ResetStub(); |
| 2007 |
EchoRequest request; |
| 2008 |
request.mutable_param()->set_skip_cancelled_check(true); |
| 2009 |
EchoResponse response; |
| 2010 |
ClientContext context; |
| 2011 |
const int delay = 100; |
| 2012 |
std::chrono::system_clock::time_point deadline = |
| 2013 |
std::chrono::system_clock::now() + std::chrono::milliseconds(delay); |
| 2014 |
context.set_deadline(deadline); |
| 2015 |
context.set_credentials(grpc::MetadataCredentialsFromPlugin( |
| 2016 |
std::unique_ptr<MetadataCredentialsPlugin>( |
| 2017 |
new TestMetadataCredentialsPlugin("meta_key", "Does not matter", true, |
| 2018 |
true, delay)))); |
| 2019 |
request.set_message("Hello"); |
| 2020 |
|
| 2021 |
Status s = stub_->Echo(&context, request, &response); |
| 2022 |
if (!s.ok()) { |
| 2023 |
EXPECT_TRUE(s.error_code() == StatusCode::DEADLINE_EXCEEDED || |
| 2024 |
s.error_code() == StatusCode::UNAVAILABLE); |
| 2025 |
} |
| 2026 |
EXPECT_EQ(context.credentials()->DebugString(), |
| 2027 |
kExpectedAuthMetadataPluginWithDeadlineCredsDebugString); |
| 2028 |
} |
| 2029 |
|
| 2030 |
TEST_P(SecureEnd2endTest, AuthMetadataPluginWithCancel) { |
| 2031 |
ResetStub(); |
| 2032 |
EchoRequest request; |
| 2033 |
request.mutable_param()->set_skip_cancelled_check(true); |
| 2034 |
EchoResponse response; |
| 2035 |
ClientContext context; |
| 2036 |
const int delay = 100; |
| 2037 |
context.set_credentials(grpc::MetadataCredentialsFromPlugin( |
| 2038 |
std::unique_ptr<MetadataCredentialsPlugin>( |
| 2039 |
new TestMetadataCredentialsPlugin("meta_key", "Does not matter", true, |
| 2040 |
true, delay)))); |
| 2041 |
request.set_message("Hello"); |
| 2042 |
|
| 2043 |
std::thread cancel_thread([&] { |
| 2044 |
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 2045 |
gpr_time_from_millis(delay, GPR_TIMESPAN))); |
| 2046 |
context.TryCancel(); |
| 2047 |
}); |
| 2048 |
Status s = stub_->Echo(&context, request, &response); |
| 2049 |
if (!s.ok()) { |
| 2050 |
EXPECT_TRUE(s.error_code() == StatusCode::CANCELLED || |
| 2051 |
s.error_code() == StatusCode::UNAVAILABLE); |
| 2052 |
} |
| 2053 |
cancel_thread.join(); |
| 2054 |
EXPECT_EQ(context.credentials()->DebugString(), |
| 2055 |
kExpectedAuthMetadataPluginWithDeadlineCredsDebugString); |
| 2056 |
} |
| 2057 |
|
| 2058 |
TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginFailure) { |
| 2059 |
ResetStub(); |
| 2060 |
EchoRequest request; |
| 2061 |
EchoResponse response; |
| 2062 |
ClientContext context; |
| 2063 |
context.set_credentials(grpc::MetadataCredentialsFromPlugin( |
| 2064 |
std::unique_ptr<MetadataCredentialsPlugin>( |
| 2065 |
new TestMetadataCredentialsPlugin( |
| 2066 |
TestMetadataCredentialsPlugin::kGoodMetadataKey, |
| 2067 |
"Does not matter, will fail anyway (see 3rd param)", false, false, |
| 2068 |
0)))); |
| 2069 |
request.set_message("Hello"); |
| 2070 |
|
| 2071 |
Status s = stub_->Echo(&context, request, &response); |
| 2072 |
EXPECT_FALSE(s.ok()); |
| 2073 |
EXPECT_EQ(s.error_code(), StatusCode::UNAVAILABLE); |
| 2074 |
EXPECT_EQ(s.error_message(), |
| 2075 |
std::string("Getting metadata from plugin failed with error: ") + |
| 2076 |
kTestCredsPluginErrorMsg); |
| 2077 |
EXPECT_EQ(context.credentials()->DebugString(), |
| 2078 |
kExpectedNonBlockingAuthMetadataPluginFailureCredsDebugString); |
| 2079 |
} |
| 2080 |
|
| 2081 |
TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorSuccess) { |
| 2082 |
auto* processor = new TestAuthMetadataProcessor(false); |
| 2083 |
StartServer(std::shared_ptr<AuthMetadataProcessor>(processor)); |
| 2084 |
ResetStub(); |
| 2085 |
EchoRequest request; |
| 2086 |
EchoResponse response; |
| 2087 |
ClientContext context; |
| 2088 |
context.set_credentials(processor->GetCompatibleClientCreds()); |
| 2089 |
request.set_message("Hello"); |
| 2090 |
request.mutable_param()->set_echo_metadata(true); |
| 2091 |
request.mutable_param()->set_expected_client_identity( |
| 2092 |
TestAuthMetadataProcessor::kGoodGuy); |
| 2093 |
request.mutable_param()->set_expected_transport_security_type( |
| 2094 |
GetParam().credentials_type()); |
| 2095 |
|
| 2096 |
Status s = stub_->Echo(&context, request, &response); |
| 2097 |
EXPECT_EQ(request.message(), response.message()); |
| 2098 |
EXPECT_TRUE(s.ok()); |
| 2099 |
|
| 2100 |
// Metadata should have been consumed by the processor. |
| 2101 |
EXPECT_FALSE(MetadataContains( |
| 2102 |
context.GetServerTrailingMetadata(), GRPC_AUTHORIZATION_METADATA_KEY, |
| 2103 |
std::string("Bearer ") + TestAuthMetadataProcessor::kGoodGuy)); |
| 2104 |
EXPECT_EQ( |
| 2105 |
context.credentials()->DebugString(), |
| 2106 |
kExpectedNonBlockingAuthMetadataPluginAndProcessorSuccessCredsDebugString); |
| 2107 |
} |
| 2108 |
|
| 2109 |
TEST_P(SecureEnd2endTest, NonBlockingAuthMetadataPluginAndProcessorFailure) { |
| 2110 |
auto* processor = new TestAuthMetadataProcessor(false); |
| 2111 |
StartServer(std::shared_ptr<AuthMetadataProcessor>(processor)); |
| 2112 |
ResetStub(); |
| 2113 |
EchoRequest request; |
| 2114 |
EchoResponse response; |
| 2115 |
ClientContext context; |
| 2116 |
context.set_credentials(processor->GetIncompatibleClientCreds()); |
| 2117 |
request.set_message("Hello"); |
| 2118 |
|
| 2119 |
Status s = stub_->Echo(&context, request, &response); |
| 2120 |
EXPECT_FALSE(s.ok()); |
| 2121 |
EXPECT_EQ(s.error_code(), StatusCode::UNAUTHENTICATED); |
| 2122 |
EXPECT_EQ( |
| 2123 |
context.credentials()->DebugString(), |
| 2124 |
kExpectedNonBlockingAuthMetadataPluginAndProcessorFailureCredsDebugString); |
| 2125 |
} |
| 2126 |
|
| 2127 |
TEST_P(SecureEnd2endTest, BlockingAuthMetadataPluginFailure) { |
| 2128 |
ResetStub(); |
| 2129 |
EchoRequest request; |
| 2130 |
EchoResponse response; |
| 2131 |
ClientContext context; |
| 2132 |
context.set_credentials(grpc::MetadataCredentialsFromPlugin( |
| 2133 |
std::unique_ptr<MetadataCredentialsPlugin>( |
| 2134 |
new TestMetadataCredentialsPlugin( |
| 2135 |
TestMetadataCredentialsPlugin::kGoodMetadataKey, |
| 2136 |
"Does not matter, will fail anyway (see 3rd param)", true, false, |
| 2137 |
0)))); |
| 2138 |
request.set_message("Hello"); |
| 2139 |
|
| 2140 |
Status s = stub_->Echo(&context, request, &response); |
| 2141 |
EXPECT_FALSE(s.ok()); |
| 2142 |
EXPECT_EQ(s.error_code(), StatusCode::UNAVAILABLE); |
| 2143 |
EXPECT_EQ(s.error_message(), |
| 2144 |
std::string("Getting metadata from plugin failed with error: ") + |
| 2145 |
kTestCredsPluginErrorMsg); |
| 2146 |
EXPECT_EQ(context.credentials()->DebugString(), |
| 2147 |
kExpectedBlockingAuthMetadataPluginFailureCredsDebugString); |
| 2148 |
} |
| 2149 |
|
| 2150 |
TEST_P(SecureEnd2endTest, CompositeCallCreds) { |
| 2151 |
ResetStub(); |
| 2152 |
EchoRequest request; |
| 2153 |
EchoResponse response; |
| 2154 |
ClientContext context; |
| 2155 |
const char kMetadataKey1[] = "call-creds-key1"; |
| 2156 |
const char kMetadataKey2[] = "call-creds-key2"; |
| 2157 |
const char kMetadataVal1[] = "call-creds-val1"; |
| 2158 |
const char kMetadataVal2[] = "call-creds-val2"; |
| 2159 |
|
| 2160 |
context.set_credentials(grpc::CompositeCallCredentials( |
| 2161 |
grpc::MetadataCredentialsFromPlugin( |
| 2162 |
std::unique_ptr<MetadataCredentialsPlugin>( |
| 2163 |
new TestMetadataCredentialsPlugin(kMetadataKey1, kMetadataVal1, |
| 2164 |
true, true, 0))), |
| 2165 |
grpc::MetadataCredentialsFromPlugin( |
| 2166 |
std::unique_ptr<MetadataCredentialsPlugin>( |
| 2167 |
new TestMetadataCredentialsPlugin(kMetadataKey2, kMetadataVal2, |
| 2168 |
true, true, 0))))); |
| 2169 |
request.set_message("Hello"); |
| 2170 |
request.mutable_param()->set_echo_metadata(true); |
| 2171 |
|
| 2172 |
Status s = stub_->Echo(&context, request, &response); |
| 2173 |
EXPECT_TRUE(s.ok()); |
| 2174 |
EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 2175 |
kMetadataKey1, kMetadataVal1)); |
| 2176 |
EXPECT_TRUE(MetadataContains(context.GetServerTrailingMetadata(), |
| 2177 |
kMetadataKey2, kMetadataVal2)); |
| 2178 |
EXPECT_EQ(context.credentials()->DebugString(), |
| 2179 |
kExpectedCompositeCallCredsDebugString); |
| 2180 |
} |
| 2181 |
|
| 2182 |
TEST_P(SecureEnd2endTest, ClientAuthContext) { |
| 2183 |
ResetStub(); |
| 2184 |
EchoRequest request; |
| 2185 |
EchoResponse response; |
| 2186 |
request.set_message("Hello"); |
| 2187 |
request.mutable_param()->set_check_auth_context( |
| 2188 |
GetParam().credentials_type() == kTlsCredentialsType); |
| 2189 |
request.mutable_param()->set_expected_transport_security_type( |
| 2190 |
GetParam().credentials_type()); |
| 2191 |
ClientContext context; |
| 2192 |
Status s = stub_->Echo(&context, request, &response); |
| 2193 |
EXPECT_EQ(response.message(), request.message()); |
| 2194 |
EXPECT_TRUE(s.ok()); |
| 2195 |
|
| 2196 |
std::shared_ptr<const AuthContext> auth_ctx = context.auth_context(); |
| 2197 |
std::vector<grpc::string_ref> tst = |
| 2198 |
auth_ctx->FindPropertyValues("transport_security_type"); |
| 2199 |
ASSERT_EQ(1u, tst.size()); |
| 2200 |
EXPECT_EQ(GetParam().credentials_type(), ToString(tst[0])); |
| 2201 |
if (GetParam().credentials_type() == kTlsCredentialsType) { |
| 2202 |
EXPECT_EQ("x509_subject_alternative_name", |
| 2203 |
auth_ctx->GetPeerIdentityPropertyName()); |
| 2204 |
EXPECT_EQ(4u, auth_ctx->GetPeerIdentity().size()); |
| 2205 |
EXPECT_EQ("*.test.google.fr", ToString(auth_ctx->GetPeerIdentity()[0])); |
| 2206 |
EXPECT_EQ("waterzooi.test.google.be", |
| 2207 |
ToString(auth_ctx->GetPeerIdentity()[1])); |
| 2208 |
EXPECT_EQ("*.test.youtube.com", ToString(auth_ctx->GetPeerIdentity()[2])); |
| 2209 |
EXPECT_EQ("192.168.1.3", ToString(auth_ctx->GetPeerIdentity()[3])); |
| 2210 |
} |
| 2211 |
} |
| 2212 |
|
| 2213 |
class ResourceQuotaEnd2endTest : public End2endTest { |
| 2214 |
public: |
| 2215 |
ResourceQuotaEnd2endTest() |
| 2216 |
: server_resource_quota_("server_resource_quota") {} |
| 2217 |
|
| 2218 |
void ConfigureServerBuilder(ServerBuilder* builder) override { |
| 2219 |
builder->SetResourceQuota(server_resource_quota_); |
| 2220 |
} |
| 2221 |
|
| 2222 |
private: |
| 2223 |
ResourceQuota server_resource_quota_; |
| 2224 |
}; |
| 2225 |
|
| 2226 |
TEST_P(ResourceQuotaEnd2endTest, SimpleRequest) { |
| 2227 |
ResetStub(); |
| 2228 |
|
| 2229 |
EchoRequest request; |
| 2230 |
EchoResponse response; |
| 2231 |
request.set_message("Hello"); |
| 2232 |
|
| 2233 |
ClientContext context; |
| 2234 |
Status s = stub_->Echo(&context, request, &response); |
| 2235 |
EXPECT_EQ(response.message(), request.message()); |
| 2236 |
EXPECT_TRUE(s.ok()); |
| 2237 |
} |
| 2238 |
|
| 2239 |
// TODO(vjpai): refactor arguments into a struct if it makes sense |
| 2240 |
std::vector<TestScenario> CreateTestScenarios(bool use_proxy, |
| 2241 |
bool test_insecure, |
| 2242 |
bool test_secure, |
| 2243 |
bool test_inproc, |
| 2244 |
bool test_callback_server) { |
| 2245 |
std::vector<TestScenario> scenarios; |
| 2246 |
std::vector<std::string> credentials_types; |
| 2247 |
|
| 2248 |
GPR_GLOBAL_CONFIG_SET(grpc_client_channel_backup_poll_interval_ms, |
| 2249 |
kClientChannelBackupPollIntervalMs); |
| 2250 |
#if TARGET_OS_IPHONE |
| 2251 |
// Workaround Apple CFStream bug |
| 2252 |
grpc_core::SetEnv("grpc_cfstream", "0"); |
| 2253 |
#endif |
| 2254 |
|
| 2255 |
if (test_secure) { |
| 2256 |
credentials_types = |
| 2257 |
GetCredentialsProvider()->GetSecureCredentialsTypeList(); |
| 2258 |
} |
| 2259 |
auto insec_ok = [] { |
| 2260 |
// Only allow insecure credentials type when it is registered with the |
| 2261 |
// provider. User may create providers that do not have insecure. |
| 2262 |
return GetCredentialsProvider()->GetChannelCredentials( |
| 2263 |
kInsecureCredentialsType, nullptr) != nullptr; |
| 2264 |
}; |
| 2265 |
if (test_insecure && insec_ok()) { |
| 2266 |
credentials_types.push_back(kInsecureCredentialsType); |
| 2267 |
} |
| 2268 |
|
| 2269 |
// Test callback with inproc or if the event-engine allows it |
| 2270 |
GPR_ASSERT(!credentials_types.empty()); |
| 2271 |
for (const auto& cred : credentials_types) { |
| 2272 |
scenarios.emplace_back(false, false, false, cred, false); |
| 2273 |
scenarios.emplace_back(true, false, false, cred, false); |
| 2274 |
if (test_callback_server) { |
| 2275 |
// Note that these scenarios will be dynamically disabled if the event |
| 2276 |
// engine doesn't run in the background |
| 2277 |
scenarios.emplace_back(false, false, false, cred, true); |
| 2278 |
scenarios.emplace_back(true, false, false, cred, true); |
| 2279 |
} |
| 2280 |
if (use_proxy) { |
| 2281 |
scenarios.emplace_back(false, true, false, cred, false); |
| 2282 |
scenarios.emplace_back(true, true, false, cred, false); |
| 2283 |
} |
| 2284 |
} |
| 2285 |
if (test_inproc && insec_ok()) { |
| 2286 |
scenarios.emplace_back(false, false, true, kInsecureCredentialsType, false); |
| 2287 |
scenarios.emplace_back(true, false, true, kInsecureCredentialsType, false); |
| 2288 |
if (test_callback_server) { |
| 2289 |
scenarios.emplace_back(false, false, true, kInsecureCredentialsType, |
| 2290 |
true); |
| 2291 |
scenarios.emplace_back(true, false, true, kInsecureCredentialsType, true); |
| 2292 |
} |
| 2293 |
} |
| 2294 |
return scenarios; |
| 2295 |
} |
| 2296 |
|
| 2297 |
INSTANTIATE_TEST_SUITE_P( |
| 2298 |
End2end, End2endTest, |
| 2299 |
::testing::ValuesIn(CreateTestScenarios(false, true, true, true, true)), |
| 2300 |
&TestScenario::Name); |
| 2301 |
|
| 2302 |
INSTANTIATE_TEST_SUITE_P( |
| 2303 |
End2endServerTryCancel, End2endServerTryCancelTest, |
| 2304 |
::testing::ValuesIn(CreateTestScenarios(false, true, true, true, true)), |
| 2305 |
&TestScenario::Name); |
| 2306 |
|
| 2307 |
INSTANTIATE_TEST_SUITE_P( |
| 2308 |
ProxyEnd2end, ProxyEnd2endTest, |
| 2309 |
::testing::ValuesIn(CreateTestScenarios(true, true, true, true, true)), |
| 2310 |
&TestScenario::Name); |
| 2311 |
|
| 2312 |
INSTANTIATE_TEST_SUITE_P( |
| 2313 |
SecureEnd2end, SecureEnd2endTest, |
| 2314 |
::testing::ValuesIn(CreateTestScenarios(false, false, true, false, true)), |
| 2315 |
&TestScenario::Name); |
| 2316 |
|
| 2317 |
INSTANTIATE_TEST_SUITE_P( |
| 2318 |
ResourceQuotaEnd2end, ResourceQuotaEnd2endTest, |
| 2319 |
::testing::ValuesIn(CreateTestScenarios(false, true, true, true, true)), |
| 2320 |
&TestScenario::Name); |
| 2321 |
|
| 2322 |
} // namespace |
| 2323 |
} // namespace testing |
| 2324 |
} // namespace grpc |
| 2325 |
|
| 2326 |
int main(int argc, char** argv) { |
| 2327 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 2328 |
::testing::InitGoogleTest(&argc, argv); |
| 2329 |
int ret = RUN_ALL_TESTS(); |
| 2330 |
return ret; |
| 2331 |
} |