|
|
|
@ -57,92 +57,6 @@ typedef enum { |
|
|
|
|
CANCEL_AFTER_PROCESSING |
|
|
|
|
} ServerTryCancelRequestPhase; |
|
|
|
|
|
|
|
|
|
namespace { |
|
|
|
|
|
|
|
|
|
// When echo_deadline is requested, deadline seen in the ServerContext is set in
|
|
|
|
|
// the response in seconds.
|
|
|
|
|
void MaybeEchoDeadline(experimental::ServerContextBase* context, |
|
|
|
|
const EchoRequest* request, EchoResponse* response) { |
|
|
|
|
if (request->has_param() && request->param().echo_deadline()) { |
|
|
|
|
gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_REALTIME); |
|
|
|
|
if (context->deadline() != system_clock::time_point::max()) { |
|
|
|
|
Timepoint2Timespec(context->deadline(), &deadline); |
|
|
|
|
} |
|
|
|
|
response->mutable_param()->set_request_deadline(deadline.tv_sec); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CheckServerAuthContext( |
|
|
|
|
const experimental::ServerContextBase* context, |
|
|
|
|
const grpc::string& expected_transport_security_type, |
|
|
|
|
const grpc::string& expected_client_identity) { |
|
|
|
|
std::shared_ptr<const AuthContext> auth_ctx = context->auth_context(); |
|
|
|
|
std::vector<grpc::string_ref> tst = |
|
|
|
|
auth_ctx->FindPropertyValues("transport_security_type"); |
|
|
|
|
EXPECT_EQ(1u, tst.size()); |
|
|
|
|
EXPECT_EQ(expected_transport_security_type, ToString(tst[0])); |
|
|
|
|
if (expected_client_identity.empty()) { |
|
|
|
|
EXPECT_TRUE(auth_ctx->GetPeerIdentityPropertyName().empty()); |
|
|
|
|
EXPECT_TRUE(auth_ctx->GetPeerIdentity().empty()); |
|
|
|
|
EXPECT_FALSE(auth_ctx->IsPeerAuthenticated()); |
|
|
|
|
} else { |
|
|
|
|
auto identity = auth_ctx->GetPeerIdentity(); |
|
|
|
|
EXPECT_TRUE(auth_ctx->IsPeerAuthenticated()); |
|
|
|
|
EXPECT_EQ(1u, identity.size()); |
|
|
|
|
EXPECT_EQ(expected_client_identity, identity[0]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Returns the number of pairs in metadata that exactly match the given
|
|
|
|
|
// key-value pair. Returns -1 if the pair wasn't found.
|
|
|
|
|
int MetadataMatchCount( |
|
|
|
|
const std::multimap<grpc::string_ref, grpc::string_ref>& metadata, |
|
|
|
|
const grpc::string& key, const grpc::string& value) { |
|
|
|
|
int count = 0; |
|
|
|
|
for (const auto& metadatum : metadata) { |
|
|
|
|
if (ToString(metadatum.first) == key && |
|
|
|
|
ToString(metadatum.second) == value) { |
|
|
|
|
count++; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return count; |
|
|
|
|
} |
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
namespace { |
|
|
|
|
int GetIntValueFromMetadataHelper( |
|
|
|
|
const char* key, |
|
|
|
|
const std::multimap<grpc::string_ref, grpc::string_ref>& metadata, |
|
|
|
|
int default_value) { |
|
|
|
|
if (metadata.find(key) != metadata.end()) { |
|
|
|
|
std::istringstream iss(ToString(metadata.find(key)->second)); |
|
|
|
|
iss >> default_value; |
|
|
|
|
gpr_log(GPR_INFO, "%s : %d", key, default_value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return default_value; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int GetIntValueFromMetadata( |
|
|
|
|
const char* key, |
|
|
|
|
const std::multimap<grpc::string_ref, grpc::string_ref>& metadata, |
|
|
|
|
int default_value) { |
|
|
|
|
return GetIntValueFromMetadataHelper(key, metadata, default_value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ServerTryCancel(ServerContext* context) { |
|
|
|
|
EXPECT_FALSE(context->IsCancelled()); |
|
|
|
|
context->TryCancel(); |
|
|
|
|
gpr_log(GPR_INFO, "Server called TryCancel() to cancel the request"); |
|
|
|
|
// Now wait until it's really canceled
|
|
|
|
|
while (!context->IsCancelled()) { |
|
|
|
|
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
|
|
|
|
gpr_time_from_micros(1000, GPR_TIMESPAN))); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
class TestServiceSignaller { |
|
|
|
|
public: |
|
|
|
|
void ClientWaitUntilRpcStarted() { |
|
|
|
@ -179,6 +93,87 @@ class TestMultipleServiceImpl : public RpcService { |
|
|
|
|
explicit TestMultipleServiceImpl(const grpc::string& host) |
|
|
|
|
: signal_client_(false), host_(new grpc::string(host)) {} |
|
|
|
|
|
|
|
|
|
// When echo_deadline is requested, deadline seen in the ServerContext is set
|
|
|
|
|
// in the response in seconds.
|
|
|
|
|
void static MaybeEchoDeadline(experimental::ServerContextBase* context, |
|
|
|
|
const EchoRequest* request, |
|
|
|
|
EchoResponse* response) { |
|
|
|
|
if (request->has_param() && request->param().echo_deadline()) { |
|
|
|
|
gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_REALTIME); |
|
|
|
|
if (context->deadline() != system_clock::time_point::max()) { |
|
|
|
|
Timepoint2Timespec(context->deadline(), &deadline); |
|
|
|
|
} |
|
|
|
|
response->mutable_param()->set_request_deadline(deadline.tv_sec); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void static CheckServerAuthContext( |
|
|
|
|
const experimental::ServerContextBase* context, |
|
|
|
|
const grpc::string& expected_transport_security_type, |
|
|
|
|
const grpc::string& expected_client_identity) { |
|
|
|
|
std::shared_ptr<const AuthContext> auth_ctx = context->auth_context(); |
|
|
|
|
std::vector<grpc::string_ref> tst = |
|
|
|
|
auth_ctx->FindPropertyValues("transport_security_type"); |
|
|
|
|
EXPECT_EQ(1u, tst.size()); |
|
|
|
|
EXPECT_EQ(expected_transport_security_type, ToString(tst[0])); |
|
|
|
|
if (expected_client_identity.empty()) { |
|
|
|
|
EXPECT_TRUE(auth_ctx->GetPeerIdentityPropertyName().empty()); |
|
|
|
|
EXPECT_TRUE(auth_ctx->GetPeerIdentity().empty()); |
|
|
|
|
EXPECT_FALSE(auth_ctx->IsPeerAuthenticated()); |
|
|
|
|
} else { |
|
|
|
|
auto identity = auth_ctx->GetPeerIdentity(); |
|
|
|
|
EXPECT_TRUE(auth_ctx->IsPeerAuthenticated()); |
|
|
|
|
EXPECT_EQ(1u, identity.size()); |
|
|
|
|
EXPECT_EQ(expected_client_identity, identity[0]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Returns the number of pairs in metadata that exactly match the given
|
|
|
|
|
// key-value pair. Returns -1 if the pair wasn't found.
|
|
|
|
|
int static MetadataMatchCount( |
|
|
|
|
const std::multimap<grpc::string_ref, grpc::string_ref>& metadata, |
|
|
|
|
const grpc::string& key, const grpc::string& value) { |
|
|
|
|
int count = 0; |
|
|
|
|
for (const auto& metadatum : metadata) { |
|
|
|
|
if (ToString(metadatum.first) == key && |
|
|
|
|
ToString(metadatum.second) == value) { |
|
|
|
|
count++; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return count; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int static GetIntValueFromMetadataHelper( |
|
|
|
|
const char* key, |
|
|
|
|
const std::multimap<grpc::string_ref, grpc::string_ref>& metadata, |
|
|
|
|
int default_value) { |
|
|
|
|
if (metadata.find(key) != metadata.end()) { |
|
|
|
|
std::istringstream iss(ToString(metadata.find(key)->second)); |
|
|
|
|
iss >> default_value; |
|
|
|
|
gpr_log(GPR_INFO, "%s : %d", key, default_value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return default_value; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int static GetIntValueFromMetadata( |
|
|
|
|
const char* key, |
|
|
|
|
const std::multimap<grpc::string_ref, grpc::string_ref>& metadata, |
|
|
|
|
int default_value) { |
|
|
|
|
return GetIntValueFromMetadataHelper(key, metadata, default_value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void static ServerTryCancel(ServerContext* context) { |
|
|
|
|
EXPECT_FALSE(context->IsCancelled()); |
|
|
|
|
context->TryCancel(); |
|
|
|
|
gpr_log(GPR_INFO, "Server called TryCancel() to cancel the request"); |
|
|
|
|
// Now wait until it's really canceled
|
|
|
|
|
while (!context->IsCancelled()) { |
|
|
|
|
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
|
|
|
|
gpr_time_from_micros(1000, GPR_TIMESPAN))); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Status Echo(ServerContext* context, const EchoRequest* request, |
|
|
|
|
EchoResponse* response) { |
|
|
|
|
if (request->has_param() && |
|
|
|
@ -311,6 +306,7 @@ class TestMultipleServiceImpl : public RpcService { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Unimplemented is left unimplemented to test the returned error.
|
|
|
|
|
|
|
|
|
|
Status RequestStream(ServerContext* context, |
|
|
|
|
ServerReader<EchoRequest>* reader, |
|
|
|
|
EchoResponse* response) { |
|
|
|
|