| 1 |
// Copyright 2021 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Unit-tests for grpc_binder_transport |
| 16 |
// |
| 17 |
// Verify that a calls to the perform_stream_op of grpc_binder_transport |
| 18 |
// transform into the correct sequence of binder transactions. |
| 19 |
#include "src/core/ext/transport/binder/transport/binder_transport.h" |
| 20 |
|
| 21 |
#include <memory> |
| 22 |
#include <string> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include <gtest/gtest.h> |
| 26 |
|
| 27 |
#include "absl/memory/memory.h" |
| 28 |
#include "absl/strings/match.h" |
| 29 |
#include "absl/strings/str_join.h" |
| 30 |
|
| 31 |
#include <grpc/grpc.h> |
| 32 |
#include <grpcpp/security/binder_security_policy.h> |
| 33 |
|
| 34 |
#include "src/core/ext/transport/binder/transport/binder_stream.h" |
| 35 |
#include "src/core/lib/gprpp/notification.h" |
| 36 |
#include "src/core/lib/resource_quota/resource_quota.h" |
| 37 |
#include "test/core/transport/binder/mock_objects.h" |
| 38 |
#include "test/core/util/test_config.h" |
| 39 |
|
| 40 |
static auto* g_memory_allocator = new grpc_core::MemoryAllocator( |
| 41 |
grpc_core::ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator( |
| 42 |
"test")); |
| 43 |
|
| 44 |
namespace grpc_binder { |
| 45 |
namespace { |
| 46 |
|
| 47 |
using ::testing::Expectation; |
| 48 |
using ::testing::NiceMock; |
| 49 |
using ::testing::Return; |
| 50 |
|
| 51 |
class BinderTransportTest : public ::testing::Test { |
| 52 |
public: |
| 53 |
BinderTransportTest() |
| 54 |
: arena_(grpc_core::Arena::Create(/* initial_size = */ 1, |
| 55 |
g_memory_allocator)), |
| 56 |
transport_(grpc_create_binder_transport_client( |
| 57 |
std::make_unique<NiceMock<MockBinder>>(), |
| 58 |
std::make_shared< |
| 59 |
grpc::experimental::binder::UntrustedSecurityPolicy>())) { |
| 60 |
auto* gbt = reinterpret_cast<grpc_binder_transport*>(transport_); |
| 61 |
gbt->wire_writer = std::make_unique<MockWireWriter>(); |
| 62 |
GRPC_STREAM_REF_INIT(&ref_, 1, nullptr, nullptr, "phony ref"); |
| 63 |
} |
| 64 |
|
| 65 |
~BinderTransportTest() override { |
| 66 |
grpc_core::ExecCtx exec_ctx; |
| 67 |
grpc_transport_destroy(transport_); |
| 68 |
grpc_core::ExecCtx::Get()->Flush(); |
| 69 |
for (grpc_binder_stream* gbs : stream_buffer_) { |
| 70 |
gbs->~grpc_binder_stream(); |
| 71 |
gpr_free(gbs); |
| 72 |
} |
| 73 |
arena_->Destroy(); |
| 74 |
} |
| 75 |
|
| 76 |
void PerformStreamOp(grpc_binder_stream* gbs, |
| 77 |
grpc_transport_stream_op_batch* op) { |
| 78 |
grpc_transport_perform_stream_op(transport_, |
| 79 |
reinterpret_cast<grpc_stream*>(gbs), op); |
| 80 |
} |
| 81 |
|
| 82 |
grpc_binder_transport* GetBinderTransport() { |
| 83 |
return reinterpret_cast<grpc_binder_transport*>(transport_); |
| 84 |
} |
| 85 |
|
| 86 |
grpc_binder_stream* InitNewBinderStream() { |
| 87 |
grpc_binder_stream* gbs = static_cast<grpc_binder_stream*>( |
| 88 |
gpr_malloc(grpc_transport_stream_size(transport_))); |
| 89 |
grpc_transport_init_stream(transport_, reinterpret_cast<grpc_stream*>(gbs), |
| 90 |
&ref_, nullptr, arena_); |
| 91 |
stream_buffer_.push_back(gbs); |
| 92 |
return gbs; |
| 93 |
} |
| 94 |
|
| 95 |
MockWireWriter& GetWireWriter() { |
| 96 |
return *reinterpret_cast<MockWireWriter*>( |
| 97 |
GetBinderTransport()->wire_writer.get()); |
| 98 |
} |
| 99 |
|
| 100 |
static void SetUpTestSuite() { grpc_init(); } |
| 101 |
static void TearDownTestSuite() { grpc_shutdown(); } |
| 102 |
|
| 103 |
protected: |
| 104 |
grpc_core::Arena* arena_; |
| 105 |
grpc_transport* transport_; |
| 106 |
grpc_stream_refcount ref_; |
| 107 |
std::vector<grpc_binder_stream*> stream_buffer_; |
| 108 |
}; |
| 109 |
|
| 110 |
void MockCallback(void* arg, grpc_error_handle error); |
| 111 |
|
| 112 |
class MockGrpcClosure { |
| 113 |
public: |
| 114 |
explicit MockGrpcClosure(grpc_core::Notification* notification = nullptr) |
| 115 |
: notification_(notification) { |
| 116 |
GRPC_CLOSURE_INIT(&closure_, MockCallback, this, nullptr); |
| 117 |
} |
| 118 |
|
| 119 |
grpc_closure* GetGrpcClosure() { return &closure_; } |
| 120 |
MOCK_METHOD(void, Callback, (grpc_error_handle), ()); |
| 121 |
|
| 122 |
grpc_core::Notification* notification_; |
| 123 |
|
| 124 |
private: |
| 125 |
grpc_closure closure_; |
| 126 |
}; |
| 127 |
|
| 128 |
void MockCallback(void* arg, grpc_error_handle error) { |
| 129 |
MockGrpcClosure* mock_closure = static_cast<MockGrpcClosure*>(arg); |
| 130 |
mock_closure->Callback(error); |
| 131 |
if (mock_closure->notification_) { |
| 132 |
mock_closure->notification_->Notify(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
std::string MetadataString(const Metadata& a) { |
| 137 |
return absl::StrCat( |
| 138 |
"{", |
| 139 |
absl::StrJoin( |
| 140 |
a, ", ", |
| 141 |
[](std::string* out, const std::pair<std::string, std::string>& kv) { |
| 142 |
out->append( |
| 143 |
absl::StrCat("\"", kv.first, "\": \"", kv.second, "\"")); |
| 144 |
}), |
| 145 |
"}"); |
| 146 |
} |
| 147 |
|
| 148 |
bool MetadataEquivalent(Metadata a, Metadata b) { |
| 149 |
std::sort(a.begin(), a.end()); |
| 150 |
std::sort(b.begin(), b.end()); |
| 151 |
return a == b; |
| 152 |
} |
| 153 |
|
| 154 |
// Matches with transactions having the desired flag, method_ref, |
| 155 |
// initial_metadata, and message_data. |
| 156 |
MATCHER_P4(TransactionMatches, flag, method_ref, initial_metadata, message_data, |
| 157 |
"") { |
| 158 |
if (arg->GetFlags() != flag) return false; |
| 159 |
if (flag & kFlagPrefix) { |
| 160 |
if (arg->GetMethodRef() != method_ref) { |
| 161 |
printf("METHOD REF NOT EQ: %s %s\n", |
| 162 |
std::string(arg->GetMethodRef()).c_str(), |
| 163 |
std::string(method_ref).c_str()); |
| 164 |
return false; |
| 165 |
} |
| 166 |
if (!MetadataEquivalent(arg->GetPrefixMetadata(), initial_metadata)) { |
| 167 |
printf("METADATA NOT EQUIVALENT: %s %s\n", |
| 168 |
MetadataString(arg->GetPrefixMetadata()).c_str(), |
| 169 |
MetadataString(initial_metadata).c_str()); |
| 170 |
return false; |
| 171 |
} |
| 172 |
} |
| 173 |
if (flag & kFlagMessageData) { |
| 174 |
if (arg->GetMessageData() != message_data) return false; |
| 175 |
} |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
// Matches with grpc_error having error message containing |msg|. |
| 180 |
MATCHER_P(GrpcErrorMessageContains, msg, "") { |
| 181 |
return absl::StrContains(grpc_core::StatusToString(arg), msg); |
| 182 |
} |
| 183 |
|
| 184 |
namespace { |
| 185 |
class MetadataEncoder { |
| 186 |
public: |
| 187 |
void Encode(const grpc_core::Slice& key, const grpc_core::Slice& value) { |
| 188 |
metadata_.emplace_back(std::string(key.as_string_view()), |
| 189 |
std::string(value.as_string_view())); |
| 190 |
} |
| 191 |
|
| 192 |
template <typename Which> |
| 193 |
void Encode(Which, const typename Which::ValueType& value) { |
| 194 |
metadata_.emplace_back( |
| 195 |
std::string(Which::key()), |
| 196 |
std::string(grpc_core::Slice(Which::Encode(value)).as_string_view())); |
| 197 |
} |
| 198 |
|
| 199 |
const Metadata& metadata() const { return metadata_; } |
| 200 |
|
| 201 |
private: |
| 202 |
Metadata metadata_; |
| 203 |
}; |
| 204 |
} // namespace |
| 205 |
|
| 206 |
// Verify that the lower-level metadata has the same content as the gRPC |
| 207 |
// metadata. |
| 208 |
void VerifyMetadataEqual(const Metadata& md, |
| 209 |
const grpc_metadata_batch& grpc_md) { |
| 210 |
MetadataEncoder encoder; |
| 211 |
grpc_md.Encode(&encoder); |
| 212 |
EXPECT_TRUE(MetadataEquivalent(encoder.metadata(), md)); |
| 213 |
} |
| 214 |
|
| 215 |
// RAII helper classes for constructing gRPC metadata and receiving callbacks. |
| 216 |
struct MakeSendInitialMetadata { |
| 217 |
MakeSendInitialMetadata(const Metadata& initial_metadata, |
| 218 |
const std::string& method_ref, |
| 219 |
grpc_transport_stream_op_batch* op) { |
| 220 |
for (const auto& md : initial_metadata) { |
| 221 |
const std::string& key = md.first; |
| 222 |
const std::string& value = md.second; |
| 223 |
grpc_initial_metadata.Append( |
| 224 |
key, grpc_core::Slice::FromCopiedString(value), |
| 225 |
[](absl::string_view, const grpc_core::Slice&) { abort(); }); |
| 226 |
} |
| 227 |
if (!method_ref.empty()) { |
| 228 |
grpc_initial_metadata.Set(grpc_core::HttpPathMetadata(), |
| 229 |
grpc_core::Slice::FromCopiedString(method_ref)); |
| 230 |
} |
| 231 |
op->send_initial_metadata = true; |
| 232 |
op->payload->send_initial_metadata.send_initial_metadata = |
| 233 |
&grpc_initial_metadata; |
| 234 |
} |
| 235 |
~MakeSendInitialMetadata() {} |
| 236 |
|
| 237 |
grpc_core::ScopedArenaPtr arena = |
| 238 |
grpc_core::MakeScopedArena(1024, g_memory_allocator); |
| 239 |
grpc_metadata_batch grpc_initial_metadata{arena.get()}; |
| 240 |
}; |
| 241 |
|
| 242 |
struct MakeSendMessage { |
| 243 |
MakeSendMessage(const std::string& message, |
| 244 |
grpc_transport_stream_op_batch* op) { |
| 245 |
send_stream.Append(grpc_core::Slice::FromCopiedString(message)); |
| 246 |
|
| 247 |
op->send_message = true; |
| 248 |
op->payload->send_message.send_message = &send_stream; |
| 249 |
} |
| 250 |
|
| 251 |
grpc_core::SliceBuffer send_stream; |
| 252 |
}; |
| 253 |
|
| 254 |
struct MakeSendTrailingMetadata { |
| 255 |
explicit MakeSendTrailingMetadata(const Metadata& trailing_metadata, |
| 256 |
grpc_transport_stream_op_batch* op) { |
| 257 |
EXPECT_TRUE(trailing_metadata.empty()); |
| 258 |
|
| 259 |
op->send_trailing_metadata = true; |
| 260 |
op->payload->send_trailing_metadata.send_trailing_metadata = |
| 261 |
&grpc_trailing_metadata; |
| 262 |
} |
| 263 |
|
| 264 |
grpc_core::ScopedArenaPtr arena = |
| 265 |
grpc_core::MakeScopedArena(1024, g_memory_allocator); |
| 266 |
grpc_metadata_batch grpc_trailing_metadata{arena.get()}; |
| 267 |
}; |
| 268 |
|
| 269 |
struct MakeRecvInitialMetadata { |
| 270 |
explicit MakeRecvInitialMetadata(grpc_transport_stream_op_batch* op, |
| 271 |
Expectation* call_before = nullptr) |
| 272 |
: ready(¬ification) { |
| 273 |
op->recv_initial_metadata = true; |
| 274 |
op->payload->recv_initial_metadata.recv_initial_metadata = |
| 275 |
&grpc_initial_metadata; |
| 276 |
op->payload->recv_initial_metadata.recv_initial_metadata_ready = |
| 277 |
ready.GetGrpcClosure(); |
| 278 |
if (call_before) { |
| 279 |
EXPECT_CALL(ready, Callback).After(*call_before); |
| 280 |
} else { |
| 281 |
EXPECT_CALL(ready, Callback); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
~MakeRecvInitialMetadata() {} |
| 286 |
|
| 287 |
MockGrpcClosure ready; |
| 288 |
grpc_core::ScopedArenaPtr arena = |
| 289 |
grpc_core::MakeScopedArena(1024, g_memory_allocator); |
| 290 |
grpc_metadata_batch grpc_initial_metadata{arena.get()}; |
| 291 |
grpc_core::Notification notification; |
| 292 |
}; |
| 293 |
|
| 294 |
struct MakeRecvMessage { |
| 295 |
explicit MakeRecvMessage(grpc_transport_stream_op_batch* op, |
| 296 |
Expectation* call_before = nullptr) |
| 297 |
: ready(¬ification) { |
| 298 |
op->recv_message = true; |
| 299 |
op->payload->recv_message.recv_message = &grpc_message; |
| 300 |
op->payload->recv_message.recv_message_ready = ready.GetGrpcClosure(); |
| 301 |
if (call_before) { |
| 302 |
EXPECT_CALL(ready, Callback).After(*call_before); |
| 303 |
} else { |
| 304 |
EXPECT_CALL(ready, Callback); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
MockGrpcClosure ready; |
| 309 |
grpc_core::Notification notification; |
| 310 |
absl::optional<grpc_core::SliceBuffer> grpc_message; |
| 311 |
}; |
| 312 |
|
| 313 |
struct MakeRecvTrailingMetadata { |
| 314 |
explicit MakeRecvTrailingMetadata(grpc_transport_stream_op_batch* op, |
| 315 |
Expectation* call_before = nullptr) |
| 316 |
: ready(¬ification) { |
| 317 |
op->recv_trailing_metadata = true; |
| 318 |
op->payload->recv_trailing_metadata.recv_trailing_metadata = |
| 319 |
&grpc_trailing_metadata; |
| 320 |
op->payload->recv_trailing_metadata.recv_trailing_metadata_ready = |
| 321 |
ready.GetGrpcClosure(); |
| 322 |
if (call_before) { |
| 323 |
EXPECT_CALL(ready, Callback).After(*call_before); |
| 324 |
} else { |
| 325 |
EXPECT_CALL(ready, Callback); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
~MakeRecvTrailingMetadata() {} |
| 330 |
|
| 331 |
MockGrpcClosure ready; |
| 332 |
grpc_core::ScopedArenaPtr arena = |
| 333 |
grpc_core::MakeScopedArena(1024, g_memory_allocator); |
| 334 |
grpc_metadata_batch grpc_trailing_metadata{arena.get()}; |
| 335 |
grpc_core::Notification notification; |
| 336 |
}; |
| 337 |
|
| 338 |
const Metadata kDefaultMetadata = { |
| 339 |
{"", ""}, |
| 340 |
{"", "value"}, |
| 341 |
{"key", ""}, |
| 342 |
{"key", "value"}, |
| 343 |
}; |
| 344 |
|
| 345 |
constexpr char kDefaultMethodRef[] = "/some/path"; |
| 346 |
constexpr char kDefaultMessage[] = "binder transport message"; |
| 347 |
constexpr int kDefaultStatus = 0x1234; |
| 348 |
|
| 349 |
Metadata AppendMethodRef(const Metadata& md, const std::string& method_ref) { |
| 350 |
Metadata result = md; |
| 351 |
result.emplace_back(":path", method_ref); |
| 352 |
return result; |
| 353 |
} |
| 354 |
|
| 355 |
Metadata AppendStatus(const Metadata& md, int status) { |
| 356 |
Metadata result = md; |
| 357 |
result.emplace_back("grpc-status", std::to_string(status)); |
| 358 |
return result; |
| 359 |
} |
| 360 |
|
| 361 |
} // namespace |
| 362 |
|
| 363 |
TEST_F(BinderTransportTest, CreateBinderTransport) { |
| 364 |
EXPECT_NE(transport_, nullptr); |
| 365 |
} |
| 366 |
|
| 367 |
TEST_F(BinderTransportTest, TransactionIdIncrement) { |
| 368 |
grpc_binder_stream* gbs0 = InitNewBinderStream(); |
| 369 |
EXPECT_EQ(gbs0->t, GetBinderTransport()); |
| 370 |
EXPECT_EQ(gbs0->tx_code, kFirstCallId); |
| 371 |
grpc_binder_stream* gbs1 = InitNewBinderStream(); |
| 372 |
EXPECT_EQ(gbs1->t, GetBinderTransport()); |
| 373 |
EXPECT_EQ(gbs1->tx_code, kFirstCallId + 1); |
| 374 |
grpc_binder_stream* gbs2 = InitNewBinderStream(); |
| 375 |
EXPECT_EQ(gbs2->t, GetBinderTransport()); |
| 376 |
EXPECT_EQ(gbs2->tx_code, kFirstCallId + 2); |
| 377 |
} |
| 378 |
|
| 379 |
TEST_F(BinderTransportTest, PerformSendInitialMetadata) { |
| 380 |
grpc_core::ExecCtx exec_ctx; |
| 381 |
grpc_binder_stream* gbs = InitNewBinderStream(); |
| 382 |
grpc_transport_stream_op_batch op{}; |
| 383 |
grpc_transport_stream_op_batch_payload payload(nullptr); |
| 384 |
op.payload = &payload; |
| 385 |
const Metadata kInitialMetadata = kDefaultMetadata; |
| 386 |
MakeSendInitialMetadata send_initial_metadata(kInitialMetadata, "", &op); |
| 387 |
MockGrpcClosure mock_on_complete; |
| 388 |
op.on_complete = mock_on_complete.GetGrpcClosure(); |
| 389 |
|
| 390 |
::testing::InSequence sequence; |
| 391 |
EXPECT_CALL(GetWireWriter(), RpcCall(TransactionMatches( |
| 392 |
kFlagPrefix, "", kInitialMetadata, ""))); |
| 393 |
EXPECT_CALL(mock_on_complete, Callback); |
| 394 |
|
| 395 |
PerformStreamOp(gbs, &op); |
| 396 |
grpc_core::ExecCtx::Get()->Flush(); |
| 397 |
} |
| 398 |
|
| 399 |
TEST_F(BinderTransportTest, PerformSendInitialMetadataMethodRef) { |
| 400 |
grpc_core::ExecCtx exec_ctx; |
| 401 |
grpc_binder_stream* gbs = InitNewBinderStream(); |
| 402 |
grpc_transport_stream_op_batch op{}; |
| 403 |
grpc_transport_stream_op_batch_payload payload(nullptr); |
| 404 |
op.payload = &payload; |
| 405 |
const Metadata kInitialMetadata = kDefaultMetadata; |
| 406 |
const std::string kMethodRef = kDefaultMethodRef; |
| 407 |
MakeSendInitialMetadata send_initial_metadata(kInitialMetadata, kMethodRef, |
| 408 |
&op); |
| 409 |
MockGrpcClosure mock_on_complete; |
| 410 |
op.on_complete = mock_on_complete.GetGrpcClosure(); |
| 411 |
|
| 412 |
::testing::InSequence sequence; |
| 413 |
EXPECT_CALL(GetWireWriter(), |
| 414 |
RpcCall(TransactionMatches(kFlagPrefix, kMethodRef.substr(1), |
| 415 |
kInitialMetadata, ""))); |
| 416 |
EXPECT_CALL(mock_on_complete, Callback); |
| 417 |
|
| 418 |
PerformStreamOp(gbs, &op); |
| 419 |
grpc_core::ExecCtx::Get()->Flush(); |
| 420 |
} |
| 421 |
|
| 422 |
TEST_F(BinderTransportTest, PerformSendMessage) { |
| 423 |
grpc_core::ExecCtx exec_ctx; |
| 424 |
grpc_binder_stream* gbs = InitNewBinderStream(); |
| 425 |
grpc_transport_stream_op_batch op{}; |
| 426 |
grpc_transport_stream_op_batch_payload payload(nullptr); |
| 427 |
op.payload = &payload; |
| 428 |
|
| 429 |
const std::string kMessage = kDefaultMessage; |
| 430 |
MakeSendMessage send_message(kMessage, &op); |
| 431 |
MockGrpcClosure mock_on_complete; |
| 432 |
op.on_complete = mock_on_complete.GetGrpcClosure(); |
| 433 |
|
| 434 |
::testing::InSequence sequence; |
| 435 |
EXPECT_CALL( |
| 436 |
GetWireWriter(), |
| 437 |
RpcCall(TransactionMatches(kFlagMessageData, "", Metadata{}, kMessage))); |
| 438 |
EXPECT_CALL(mock_on_complete, Callback); |
| 439 |
|
| 440 |
PerformStreamOp(gbs, &op); |
| 441 |
grpc_core::ExecCtx::Get()->Flush(); |
| 442 |
} |
| 443 |
|
| 444 |
TEST_F(BinderTransportTest, PerformSendTrailingMetadata) { |
| 445 |
grpc_core::ExecCtx exec_ctx; |
| 446 |
grpc_binder_stream* gbs = InitNewBinderStream(); |
| 447 |
grpc_transport_stream_op_batch op{}; |
| 448 |
grpc_transport_stream_op_batch_payload payload(nullptr); |
| 449 |
op.payload = &payload; |
| 450 |
// The wireformat guarantees that suffix metadata will always be empty. |
| 451 |
// TODO(waynetu): Check whether gRPC can internally add extra trailing |
| 452 |
// metadata. |
| 453 |
const Metadata kTrailingMetadata = {}; |
| 454 |
MakeSendTrailingMetadata send_trailing_metadata(kTrailingMetadata, &op); |
| 455 |
MockGrpcClosure mock_on_complete; |
| 456 |
op.on_complete = mock_on_complete.GetGrpcClosure(); |
| 457 |
|
| 458 |
::testing::InSequence sequence; |
| 459 |
EXPECT_CALL(GetWireWriter(), RpcCall(TransactionMatches( |
| 460 |
kFlagSuffix, "", kTrailingMetadata, ""))); |
| 461 |
EXPECT_CALL(mock_on_complete, Callback); |
| 462 |
|
| 463 |
PerformStreamOp(gbs, &op); |
| 464 |
grpc_core::ExecCtx::Get()->Flush(); |
| 465 |
} |
| 466 |
|
| 467 |
TEST_F(BinderTransportTest, PerformSendAll) { |
| 468 |
grpc_core::ExecCtx exec_ctx; |
| 469 |
grpc_binder_stream* gbs = InitNewBinderStream(); |
| 470 |
grpc_transport_stream_op_batch op{}; |
| 471 |
grpc_transport_stream_op_batch_payload payload(nullptr); |
| 472 |
op.payload = &payload; |
| 473 |
|
| 474 |
const Metadata kInitialMetadata = kDefaultMetadata; |
| 475 |
const std::string kMethodRef = kDefaultMethodRef; |
| 476 |
MakeSendInitialMetadata send_initial_metadata(kInitialMetadata, kMethodRef, |
| 477 |
&op); |
| 478 |
|
| 479 |
const std::string kMessage = kDefaultMessage; |
| 480 |
MakeSendMessage send_message(kMessage, &op); |
| 481 |
|
| 482 |
// The wireformat guarantees that suffix metadata will always be empty. |
| 483 |
// TODO(waynetu): Check whether gRPC can internally add extra trailing |
| 484 |
// metadata. |
| 485 |
const Metadata kTrailingMetadata = {}; |
| 486 |
MakeSendTrailingMetadata send_trailing_metadata(kTrailingMetadata, &op); |
| 487 |
|
| 488 |
MockGrpcClosure mock_on_complete; |
| 489 |
op.on_complete = mock_on_complete.GetGrpcClosure(); |
| 490 |
|
| 491 |
::testing::InSequence sequence; |
| 492 |
EXPECT_CALL(GetWireWriter(), |
| 493 |
RpcCall(TransactionMatches( |
| 494 |
kFlagPrefix | kFlagMessageData | kFlagSuffix, |
| 495 |
kMethodRef.substr(1), kInitialMetadata, kMessage))); |
| 496 |
EXPECT_CALL(mock_on_complete, Callback); |
| 497 |
|
| 498 |
PerformStreamOp(gbs, &op); |
| 499 |
grpc_core::ExecCtx::Get()->Flush(); |
| 500 |
} |
| 501 |
|
| 502 |
TEST_F(BinderTransportTest, PerformRecvInitialMetadata) { |
| 503 |
grpc_core::ExecCtx exec_ctx; |
| 504 |
grpc_binder_stream* gbs = InitNewBinderStream(); |
| 505 |
grpc_transport_stream_op_batch op{}; |
| 506 |
grpc_transport_stream_op_batch_payload payload(nullptr); |
| 507 |
op.payload = &payload; |
| 508 |
|
| 509 |
MakeRecvInitialMetadata recv_initial_metadata(&op); |
| 510 |
|
| 511 |
const Metadata kInitialMetadata = kDefaultMetadata; |
| 512 |
auto* gbt = reinterpret_cast<grpc_binder_transport*>(transport_); |
| 513 |
gbt->transport_stream_receiver->NotifyRecvInitialMetadata(gbs->tx_code, |
| 514 |
kInitialMetadata); |
| 515 |
PerformStreamOp(gbs, &op); |
| 516 |
grpc_core::ExecCtx::Get()->Flush(); |
| 517 |
recv_initial_metadata.notification.WaitForNotification(); |
| 518 |
|
| 519 |
VerifyMetadataEqual(kInitialMetadata, |
| 520 |
recv_initial_metadata.grpc_initial_metadata); |
| 521 |
} |
| 522 |
|
| 523 |
TEST_F(BinderTransportTest, PerformRecvInitialMetadataWithMethodRef) { |
| 524 |
grpc_core::ExecCtx exec_ctx; |
| 525 |
grpc_binder_stream* gbs = InitNewBinderStream(); |
| 526 |
grpc_transport_stream_op_batch op{}; |
| 527 |
grpc_transport_stream_op_batch_payload payload(nullptr); |
| 528 |
op.payload = &payload; |
| 529 |
|
| 530 |
MakeRecvInitialMetadata recv_initial_metadata(&op); |
| 531 |
|
| 532 |
auto* gbt = reinterpret_cast<grpc_binder_transport*>(transport_); |
| 533 |
const Metadata kInitialMetadataWithMethodRef = |
| 534 |
AppendMethodRef(kDefaultMetadata, kDefaultMethodRef); |
| 535 |
gbt->transport_stream_receiver->NotifyRecvInitialMetadata( |
| 536 |
gbs->tx_code, kInitialMetadataWithMethodRef); |
| 537 |
PerformStreamOp(gbs, &op); |
| 538 |
grpc_core::ExecCtx::Get()->Flush(); |
| 539 |
recv_initial_metadata.notification.WaitForNotification(); |
| 540 |
|
| 541 |
VerifyMetadataEqual(kInitialMetadataWithMethodRef, |
| 542 |
recv_initial_metadata.grpc_initial_metadata); |
| 543 |
} |
| 544 |
|
| 545 |
TEST_F(BinderTransportTest, PerformRecvMessage) { |
| 546 |
grpc_core::ExecCtx exec_ctx; |
| 547 |
grpc_binder_stream* gbs = InitNewBinderStream(); |
| 548 |
grpc_transport_stream_op_batch op{}; |
| 549 |
grpc_transport_stream_op_batch_payload payload(nullptr); |
| 550 |
op.payload = &payload; |
| 551 |
|
| 552 |
MakeRecvMessage recv_message(&op); |
| 553 |
|
| 554 |
auto* gbt = reinterpret_cast<grpc_binder_transport*>(transport_); |
| 555 |
const std::string kMessage = kDefaultMessage; |
| 556 |
gbt->transport_stream_receiver->NotifyRecvMessage(gbs->tx_code, kMessage); |
| 557 |
|
| 558 |
PerformStreamOp(gbs, &op); |
| 559 |
grpc_core::ExecCtx::Get()->Flush(); |
| 560 |
recv_message.notification.WaitForNotification(); |
| 561 |
|
| 562 |
EXPECT_EQ(kMessage, recv_message.grpc_message->JoinIntoString()); |
| 563 |
} |
| 564 |
|
| 565 |
TEST_F(BinderTransportTest, PerformRecvTrailingMetadata) { |
| 566 |
grpc_core::ExecCtx exec_ctx; |
| 567 |
grpc_binder_stream* gbs = InitNewBinderStream(); |
| 568 |
grpc_transport_stream_op_batch op{}; |
| 569 |
grpc_transport_stream_op_batch_payload payload(nullptr); |
| 570 |
op.payload = &payload; |
| 571 |
|
| 572 |
MakeRecvTrailingMetadata recv_trailing_metadata(&op); |
| 573 |
|
| 574 |
const Metadata kTrailingMetadata = kDefaultMetadata; |
| 575 |
auto* gbt = reinterpret_cast<grpc_binder_transport*>(transport_); |
| 576 |
constexpr int kStatus = kDefaultStatus; |
| 577 |
gbt->transport_stream_receiver->NotifyRecvTrailingMetadata( |
| 578 |
gbs->tx_code, kTrailingMetadata, kStatus); |
| 579 |
PerformStreamOp(gbs, &op); |
| 580 |
grpc_core::ExecCtx::Get()->Flush(); |
| 581 |
recv_trailing_metadata.notification.WaitForNotification(); |
| 582 |
|
| 583 |
VerifyMetadataEqual(AppendStatus(kTrailingMetadata, kStatus), |
| 584 |
recv_trailing_metadata.grpc_trailing_metadata); |
| 585 |
} |
| 586 |
|
| 587 |
TEST_F(BinderTransportTest, PerformRecvAll) { |
| 588 |
grpc_core::ExecCtx exec_ctx; |
| 589 |
grpc_binder_stream* gbs = InitNewBinderStream(); |
| 590 |
grpc_transport_stream_op_batch op{}; |
| 591 |
grpc_transport_stream_op_batch_payload payload(nullptr); |
| 592 |
op.payload = &payload; |
| 593 |
|
| 594 |
MakeRecvInitialMetadata recv_initial_metadata(&op); |
| 595 |
MakeRecvMessage recv_message(&op); |
| 596 |
MakeRecvTrailingMetadata recv_trailing_metadata(&op); |
| 597 |
|
| 598 |
auto* gbt = reinterpret_cast<grpc_binder_transport*>(transport_); |
| 599 |
const Metadata kInitialMetadataWithMethodRef = |
| 600 |
AppendMethodRef(kDefaultMetadata, kDefaultMethodRef); |
| 601 |
gbt->transport_stream_receiver->NotifyRecvInitialMetadata( |
| 602 |
gbs->tx_code, kInitialMetadataWithMethodRef); |
| 603 |
|
| 604 |
const std::string kMessage = kDefaultMessage; |
| 605 |
gbt->transport_stream_receiver->NotifyRecvMessage(gbs->tx_code, kMessage); |
| 606 |
|
| 607 |
Metadata trailing_metadata = kDefaultMetadata; |
| 608 |
constexpr int kStatus = kDefaultStatus; |
| 609 |
gbt->transport_stream_receiver->NotifyRecvTrailingMetadata( |
| 610 |
gbs->tx_code, trailing_metadata, kStatus); |
| 611 |
PerformStreamOp(gbs, &op); |
| 612 |
grpc_core::ExecCtx::Get()->Flush(); |
| 613 |
recv_trailing_metadata.notification.WaitForNotification(); |
| 614 |
|
| 615 |
VerifyMetadataEqual(kInitialMetadataWithMethodRef, |
| 616 |
recv_initial_metadata.grpc_initial_metadata); |
| 617 |
trailing_metadata.emplace_back("grpc-status", std::to_string(kStatus)); |
| 618 |
VerifyMetadataEqual(trailing_metadata, |
| 619 |
recv_trailing_metadata.grpc_trailing_metadata); |
| 620 |
EXPECT_EQ(kMessage, recv_message.grpc_message->JoinIntoString()); |
| 621 |
} |
| 622 |
|
| 623 |
TEST_F(BinderTransportTest, PerformAllOps) { |
| 624 |
grpc_core::ExecCtx exec_ctx; |
| 625 |
grpc_binder_stream* gbs = InitNewBinderStream(); |
| 626 |
grpc_transport_stream_op_batch op{}; |
| 627 |
grpc_transport_stream_op_batch_payload payload(nullptr); |
| 628 |
op.payload = &payload; |
| 629 |
|
| 630 |
const Metadata kSendInitialMetadata = kDefaultMetadata; |
| 631 |
const std::string kMethodRef = kDefaultMethodRef; |
| 632 |
MakeSendInitialMetadata send_initial_metadata(kSendInitialMetadata, |
| 633 |
kMethodRef, &op); |
| 634 |
|
| 635 |
const std::string kSendMessage = kDefaultMessage; |
| 636 |
MakeSendMessage send_message(kSendMessage, &op); |
| 637 |
|
| 638 |
// The wireformat guarantees that suffix metadata will always be empty. |
| 639 |
// TODO(waynetu): Check whether gRPC can internally add extra trailing |
| 640 |
// metadata. |
| 641 |
const Metadata kSendTrailingMetadata = {}; |
| 642 |
MakeSendTrailingMetadata send_trailing_metadata(kSendTrailingMetadata, &op); |
| 643 |
|
| 644 |
MockGrpcClosure mock_on_complete; |
| 645 |
op.on_complete = mock_on_complete.GetGrpcClosure(); |
| 646 |
|
| 647 |
// TODO(waynetu): Currently, we simply drop the prefix '/' from the :path |
| 648 |
// argument to obtain the method name. Update the test if this turns out to be |
| 649 |
// incorrect. |
| 650 |
EXPECT_CALL(GetWireWriter(), |
| 651 |
RpcCall(TransactionMatches( |
| 652 |
kFlagPrefix | kFlagMessageData | kFlagSuffix, |
| 653 |
kMethodRef.substr(1), kSendInitialMetadata, kSendMessage))); |
| 654 |
Expectation on_complete = EXPECT_CALL(mock_on_complete, Callback); |
| 655 |
|
| 656 |
// Recv callbacks can happen after the on_complete callback. |
| 657 |
MakeRecvInitialMetadata recv_initial_metadata( |
| 658 |
&op, /* call_before = */ &on_complete); |
| 659 |
MakeRecvMessage recv_message(&op, /* call_before = */ &on_complete); |
| 660 |
MakeRecvTrailingMetadata recv_trailing_metadata( |
| 661 |
&op, /* call_before = */ &on_complete); |
| 662 |
|
| 663 |
PerformStreamOp(gbs, &op); |
| 664 |
|
| 665 |
// Flush the execution context to force on_complete to run before recv |
| 666 |
// callbacks get scheduled. |
| 667 |
grpc_core::ExecCtx::Get()->Flush(); |
| 668 |
|
| 669 |
auto* gbt = reinterpret_cast<grpc_binder_transport*>(transport_); |
| 670 |
const Metadata kRecvInitialMetadata = |
| 671 |
AppendMethodRef(kDefaultMetadata, kDefaultMethodRef); |
| 672 |
gbt->transport_stream_receiver->NotifyRecvInitialMetadata( |
| 673 |
gbs->tx_code, kRecvInitialMetadata); |
| 674 |
const std::string kRecvMessage = kDefaultMessage; |
| 675 |
gbt->transport_stream_receiver->NotifyRecvMessage(gbs->tx_code, kRecvMessage); |
| 676 |
const Metadata kRecvTrailingMetadata = kDefaultMetadata; |
| 677 |
constexpr int kStatus = 0x1234; |
| 678 |
gbt->transport_stream_receiver->NotifyRecvTrailingMetadata( |
| 679 |
gbs->tx_code, kRecvTrailingMetadata, kStatus); |
| 680 |
|
| 681 |
grpc_core::ExecCtx::Get()->Flush(); |
| 682 |
recv_initial_metadata.notification.WaitForNotification(); |
| 683 |
recv_message.notification.WaitForNotification(); |
| 684 |
recv_trailing_metadata.notification.WaitForNotification(); |
| 685 |
|
| 686 |
VerifyMetadataEqual(kRecvInitialMetadata, |
| 687 |
recv_initial_metadata.grpc_initial_metadata); |
| 688 |
VerifyMetadataEqual(AppendStatus(kRecvTrailingMetadata, kStatus), |
| 689 |
recv_trailing_metadata.grpc_trailing_metadata); |
| 690 |
|
| 691 |
EXPECT_EQ(kRecvMessage, recv_message.grpc_message->JoinIntoString()); |
| 692 |
} |
| 693 |
|
| 694 |
TEST_F(BinderTransportTest, WireWriterRpcCallErrorPropagates) { |
| 695 |
grpc_core::ExecCtx exec_ctx; |
| 696 |
grpc_binder_stream* gbs = InitNewBinderStream(); |
| 697 |
|
| 698 |
MockGrpcClosure mock_on_complete1; |
| 699 |
MockGrpcClosure mock_on_complete2; |
| 700 |
|
| 701 |
EXPECT_CALL(GetWireWriter(), RpcCall) |
| 702 |
.WillOnce(Return(absl::OkStatus())) |
| 703 |
.WillOnce(Return(absl::InternalError("WireWriter::RpcCall failed"))); |
| 704 |
EXPECT_CALL(mock_on_complete1, Callback(absl::OkStatus())); |
| 705 |
EXPECT_CALL(mock_on_complete2, |
| 706 |
Callback(GrpcErrorMessageContains("WireWriter::RpcCall failed"))); |
| 707 |
|
| 708 |
const Metadata kInitialMetadata = {}; |
| 709 |
grpc_transport_stream_op_batch op1{}; |
| 710 |
grpc_transport_stream_op_batch_payload payload1(nullptr); |
| 711 |
op1.payload = &payload1; |
| 712 |
MakeSendInitialMetadata send_initial_metadata1(kInitialMetadata, "", &op1); |
| 713 |
op1.on_complete = mock_on_complete1.GetGrpcClosure(); |
| 714 |
|
| 715 |
grpc_transport_stream_op_batch op2{}; |
| 716 |
grpc_transport_stream_op_batch_payload payload2(nullptr); |
| 717 |
op2.payload = &payload2; |
| 718 |
MakeSendInitialMetadata send_initial_metadata2(kInitialMetadata, "", &op2); |
| 719 |
op2.on_complete = mock_on_complete2.GetGrpcClosure(); |
| 720 |
|
| 721 |
PerformStreamOp(gbs, &op1); |
| 722 |
PerformStreamOp(gbs, &op2); |
| 723 |
grpc_core::ExecCtx::Get()->Flush(); |
| 724 |
} |
| 725 |
|
| 726 |
} // namespace grpc_binder |
| 727 |
|
| 728 |
int main(int argc, char** argv) { |
| 729 |
::testing::InitGoogleTest(&argc, argv); |
| 730 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 731 |
return RUN_ALL_TESTS(); |
| 732 |
} |