| 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 WireReaderImpl. |
| 16 |
// |
| 17 |
// WireReaderImpl is responsible for turning incoming transactions into |
| 18 |
// top-level metadata. The following tests verify that the interactions between |
| 19 |
// WireReaderImpl and both the output (readable) parcel and the transport stream |
| 20 |
// receiver are correct in all possible situations. |
| 21 |
|
| 22 |
#include <memory> |
| 23 |
#include <string> |
| 24 |
#include <thread> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
#include <gtest/gtest.h> |
| 28 |
|
| 29 |
#include "absl/memory/memory.h" |
| 30 |
|
| 31 |
#include <grpcpp/security/binder_security_policy.h> |
| 32 |
|
| 33 |
#include "src/core/ext/transport/binder/wire_format/wire_reader_impl.h" |
| 34 |
#include "test/core/transport/binder/mock_objects.h" |
| 35 |
#include "test/core/util/test_config.h" |
| 36 |
|
| 37 |
namespace grpc_binder { |
| 38 |
|
| 39 |
using ::testing::DoAll; |
| 40 |
using ::testing::Return; |
| 41 |
using ::testing::SetArgPointee; |
| 42 |
using ::testing::StrictMock; |
| 43 |
|
| 44 |
namespace { |
| 45 |
|
| 46 |
class WireReaderTest : public ::testing::Test { |
| 47 |
protected: |
| 48 |
void SetUp() override { SetUp(true); } |
| 49 |
void SetUp(bool is_client) { |
| 50 |
transport_stream_receiver_ = |
| 51 |
std::make_shared<StrictMock<MockTransportStreamReceiver>>(); |
| 52 |
wire_reader_ = std::make_shared<WireReaderImpl>( |
| 53 |
transport_stream_receiver_, is_client, |
| 54 |
std::make_shared< |
| 55 |
grpc::experimental::binder::UntrustedSecurityPolicy>()); |
| 56 |
} |
| 57 |
|
| 58 |
void ExpectReadInt32(int result) { |
| 59 |
EXPECT_CALL(mock_readable_parcel_, ReadInt32) |
| 60 |
.WillOnce(DoAll(SetArgPointee<0>(result), Return(absl::OkStatus()))); |
| 61 |
} |
| 62 |
|
| 63 |
void ExpectReadByteArray(const std::string& buffer) { |
| 64 |
ExpectReadInt32(buffer.length()); |
| 65 |
if (!buffer.empty()) { |
| 66 |
EXPECT_CALL(mock_readable_parcel_, ReadByteArray) |
| 67 |
.WillOnce([buffer](std::string* data) { |
| 68 |
*data = buffer; |
| 69 |
return absl::OkStatus(); |
| 70 |
}); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
void ExpectReadString(const std::string& str) { |
| 75 |
EXPECT_CALL(mock_readable_parcel_, ReadString) |
| 76 |
.WillOnce([str](std::string* out) { |
| 77 |
*out = str; |
| 78 |
return absl::OkStatus(); |
| 79 |
}); |
| 80 |
} |
| 81 |
|
| 82 |
void UnblockSetupTransport() { |
| 83 |
// SETUP_TRANSPORT should finish before we can proceed with any other |
| 84 |
// requests and streaming calls. The MockBinder will construct a |
| 85 |
// MockTransactionReceiver, which will then sends SETUP_TRANSPORT request |
| 86 |
// back to us. |
| 87 |
wire_reader_->SetupTransport(std::make_unique<MockBinder>()); |
| 88 |
} |
| 89 |
|
| 90 |
template <typename T> |
| 91 |
absl::Status CallProcessTransaction(T tx_code) { |
| 92 |
return wire_reader_->ProcessTransaction( |
| 93 |
static_cast<transaction_code_t>(tx_code), &mock_readable_parcel_, |
| 94 |
/*uid=*/0); |
| 95 |
} |
| 96 |
|
| 97 |
std::shared_ptr<StrictMock<MockTransportStreamReceiver>> |
| 98 |
transport_stream_receiver_; |
| 99 |
std::shared_ptr<WireReaderImpl> wire_reader_; |
| 100 |
MockReadableParcel mock_readable_parcel_; |
| 101 |
}; |
| 102 |
|
| 103 |
MATCHER_P(StatusOrStrEq, target, "") { |
| 104 |
if (!arg.ok()) return false; |
| 105 |
return arg.value() == target; |
| 106 |
} |
| 107 |
|
| 108 |
MATCHER_P(StatusOrContainerEq, target, "") { |
| 109 |
if (!arg.ok()) return false; |
| 110 |
return arg.value() == target; |
| 111 |
} |
| 112 |
|
| 113 |
} // namespace |
| 114 |
|
| 115 |
TEST_F(WireReaderTest, SetupTransport) { |
| 116 |
auto mock_binder = std::make_unique<MockBinder>(); |
| 117 |
MockBinder& mock_binder_ref = *mock_binder; |
| 118 |
|
| 119 |
::testing::InSequence sequence; |
| 120 |
EXPECT_CALL(mock_binder_ref, Initialize); |
| 121 |
EXPECT_CALL(mock_binder_ref, PrepareTransaction); |
| 122 |
const MockReadableParcel mock_readable_parcel; |
| 123 |
EXPECT_CALL(mock_binder_ref, GetWritableParcel); |
| 124 |
|
| 125 |
// Write version. |
| 126 |
EXPECT_CALL(mock_binder_ref.GetWriter(), WriteInt32(1)); |
| 127 |
|
| 128 |
wire_reader_->SetupTransport(std::move(mock_binder)); |
| 129 |
} |
| 130 |
|
| 131 |
TEST_F(WireReaderTest, ProcessTransactionControlMessageSetupTransport) { |
| 132 |
::testing::InSequence sequence; |
| 133 |
UnblockSetupTransport(); |
| 134 |
} |
| 135 |
|
| 136 |
TEST_F(WireReaderTest, ProcessTransactionControlMessagePingResponse) { |
| 137 |
::testing::InSequence sequence; |
| 138 |
UnblockSetupTransport(); |
| 139 |
EXPECT_CALL(mock_readable_parcel_, ReadInt32); |
| 140 |
EXPECT_TRUE( |
| 141 |
CallProcessTransaction(BinderTransportTxCode::PING_RESPONSE).ok()); |
| 142 |
} |
| 143 |
|
| 144 |
TEST_F(WireReaderTest, ProcessTransactionServerRpcDataEmptyFlagIgnored) { |
| 145 |
::testing::InSequence sequence; |
| 146 |
UnblockSetupTransport(); |
| 147 |
|
| 148 |
// first transaction: empty flag |
| 149 |
ExpectReadInt32(0); |
| 150 |
// Won't further read sequence number. |
| 151 |
EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok()); |
| 152 |
} |
| 153 |
|
| 154 |
TEST_F(WireReaderTest, |
| 155 |
ProcessTransactionServerRpcDataFlagPrefixWithoutMetadata) { |
| 156 |
::testing::InSequence sequence; |
| 157 |
UnblockSetupTransport(); |
| 158 |
|
| 159 |
// flag |
| 160 |
ExpectReadInt32(kFlagPrefix); |
| 161 |
// sequence number |
| 162 |
ExpectReadInt32(0); |
| 163 |
|
| 164 |
// count |
| 165 |
ExpectReadInt32(0); |
| 166 |
EXPECT_CALL( |
| 167 |
*transport_stream_receiver_, |
| 168 |
NotifyRecvInitialMetadata(kFirstCallId, StatusOrContainerEq(Metadata{}))); |
| 169 |
|
| 170 |
EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok()); |
| 171 |
} |
| 172 |
|
| 173 |
TEST_F(WireReaderTest, ProcessTransactionServerRpcDataFlagPrefixWithMetadata) { |
| 174 |
::testing::InSequence sequence; |
| 175 |
UnblockSetupTransport(); |
| 176 |
|
| 177 |
// flag |
| 178 |
ExpectReadInt32(kFlagPrefix); |
| 179 |
// sequence number |
| 180 |
ExpectReadInt32(0); |
| 181 |
|
| 182 |
const std::vector<std::pair<std::string, std::string>> kMetadata = { |
| 183 |
{"", ""}, |
| 184 |
{"", "value"}, |
| 185 |
{"key", ""}, |
| 186 |
{"key", "value"}, |
| 187 |
{"another-key", "another-value"}, |
| 188 |
}; |
| 189 |
|
| 190 |
// count |
| 191 |
ExpectReadInt32(kMetadata.size()); |
| 192 |
for (const auto& md : kMetadata) { |
| 193 |
// metadata key |
| 194 |
ExpectReadByteArray(md.first); |
| 195 |
// metadata val |
| 196 |
// TODO(waynetu): metadata value can also be "parcelable". |
| 197 |
ExpectReadByteArray(md.second); |
| 198 |
} |
| 199 |
EXPECT_CALL( |
| 200 |
*transport_stream_receiver_, |
| 201 |
NotifyRecvInitialMetadata(kFirstCallId, StatusOrContainerEq(kMetadata))); |
| 202 |
|
| 203 |
EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok()); |
| 204 |
} |
| 205 |
|
| 206 |
TEST_F(WireReaderTest, ProcessTransactionServerRpcDataFlagMessageDataNonEmpty) { |
| 207 |
::testing::InSequence sequence; |
| 208 |
UnblockSetupTransport(); |
| 209 |
|
| 210 |
// flag |
| 211 |
ExpectReadInt32(kFlagMessageData); |
| 212 |
// sequence number |
| 213 |
ExpectReadInt32(0); |
| 214 |
|
| 215 |
// message data |
| 216 |
// TODO(waynetu): message data can also be "parcelable". |
| 217 |
const std::string kMessageData = "message data"; |
| 218 |
ExpectReadByteArray(kMessageData); |
| 219 |
EXPECT_CALL(*transport_stream_receiver_, |
| 220 |
NotifyRecvMessage(kFirstCallId, StatusOrStrEq(kMessageData))); |
| 221 |
|
| 222 |
EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok()); |
| 223 |
} |
| 224 |
|
| 225 |
TEST_F(WireReaderTest, ProcessTransactionServerRpcDataFlagMessageDataEmpty) { |
| 226 |
::testing::InSequence sequence; |
| 227 |
UnblockSetupTransport(); |
| 228 |
|
| 229 |
// flag |
| 230 |
ExpectReadInt32(kFlagMessageData); |
| 231 |
// sequence number |
| 232 |
ExpectReadInt32(0); |
| 233 |
|
| 234 |
// message data |
| 235 |
// TODO(waynetu): message data can also be "parcelable". |
| 236 |
const std::string kMessageData = ""; |
| 237 |
ExpectReadByteArray(kMessageData); |
| 238 |
EXPECT_CALL(*transport_stream_receiver_, |
| 239 |
NotifyRecvMessage(kFirstCallId, StatusOrStrEq(kMessageData))); |
| 240 |
|
| 241 |
EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok()); |
| 242 |
} |
| 243 |
|
| 244 |
TEST_F(WireReaderTest, ProcessTransactionServerRpcDataFlagSuffixWithStatus) { |
| 245 |
::testing::InSequence sequence; |
| 246 |
UnblockSetupTransport(); |
| 247 |
|
| 248 |
constexpr int kStatus = 0x1234; |
| 249 |
// flag |
| 250 |
ExpectReadInt32(kFlagSuffix | kFlagStatusDescription | (kStatus << 16)); |
| 251 |
// sequence number |
| 252 |
ExpectReadInt32(0); |
| 253 |
// status description |
| 254 |
EXPECT_CALL(mock_readable_parcel_, ReadString); |
| 255 |
// metadata count |
| 256 |
ExpectReadInt32(0); |
| 257 |
EXPECT_CALL(*transport_stream_receiver_, |
| 258 |
NotifyRecvTrailingMetadata( |
| 259 |
kFirstCallId, StatusOrContainerEq(Metadata{}), kStatus)); |
| 260 |
|
| 261 |
EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok()); |
| 262 |
} |
| 263 |
|
| 264 |
TEST_F(WireReaderTest, ProcessTransactionServerRpcDataFlagSuffixWithoutStatus) { |
| 265 |
::testing::InSequence sequence; |
| 266 |
UnblockSetupTransport(); |
| 267 |
|
| 268 |
// flag |
| 269 |
ExpectReadInt32(kFlagSuffix); |
| 270 |
// sequence number |
| 271 |
ExpectReadInt32(0); |
| 272 |
// No status description |
| 273 |
// metadata count |
| 274 |
ExpectReadInt32(0); |
| 275 |
EXPECT_CALL(*transport_stream_receiver_, |
| 276 |
NotifyRecvTrailingMetadata(kFirstCallId, |
| 277 |
StatusOrContainerEq(Metadata{}), 0)); |
| 278 |
|
| 279 |
EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok()); |
| 280 |
} |
| 281 |
|
| 282 |
TEST_F(WireReaderTest, InBoundFlowControl) { |
| 283 |
::testing::InSequence sequence; |
| 284 |
UnblockSetupTransport(); |
| 285 |
|
| 286 |
// data size |
| 287 |
EXPECT_CALL(mock_readable_parcel_, GetDataSize).WillOnce(Return(1000)); |
| 288 |
// flag |
| 289 |
ExpectReadInt32(kFlagMessageData | kFlagMessageDataIsPartial); |
| 290 |
// sequence number |
| 291 |
ExpectReadInt32(0); |
| 292 |
// message size |
| 293 |
ExpectReadInt32(1000); |
| 294 |
EXPECT_CALL(mock_readable_parcel_, ReadByteArray) |
| 295 |
.WillOnce(DoAll(SetArgPointee<0>(std::string(1000, 'a')), |
| 296 |
Return(absl::OkStatus()))); |
| 297 |
|
| 298 |
// Data is not completed. No callback will be triggered. |
| 299 |
EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok()); |
| 300 |
|
| 301 |
EXPECT_CALL(mock_readable_parcel_, GetDataSize).WillOnce(Return(1000)); |
| 302 |
// flag |
| 303 |
ExpectReadInt32(kFlagMessageData); |
| 304 |
// sequence number |
| 305 |
ExpectReadInt32(1); |
| 306 |
// message size |
| 307 |
ExpectReadInt32(1000); |
| 308 |
EXPECT_CALL(mock_readable_parcel_, ReadByteArray) |
| 309 |
.WillOnce(DoAll(SetArgPointee<0>(std::string(1000, 'b')), |
| 310 |
Return(absl::OkStatus()))); |
| 311 |
|
| 312 |
EXPECT_CALL(*transport_stream_receiver_, |
| 313 |
NotifyRecvMessage(kFirstCallId, |
| 314 |
StatusOrContainerEq(std::string(1000, 'a') + |
| 315 |
std::string(1000, 'b')))); |
| 316 |
EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok()); |
| 317 |
} |
| 318 |
|
| 319 |
TEST_F(WireReaderTest, ServerInitialMetadata) { |
| 320 |
SetUp(/*is_client=*/false); |
| 321 |
|
| 322 |
::testing::InSequence sequence; |
| 323 |
UnblockSetupTransport(); |
| 324 |
|
| 325 |
// flag |
| 326 |
ExpectReadInt32(kFlagPrefix); |
| 327 |
// sequence number |
| 328 |
ExpectReadInt32(0); |
| 329 |
|
| 330 |
const std::vector<std::pair<std::string, std::string>> kMetadata = { |
| 331 |
{"", ""}, |
| 332 |
{"", "value"}, |
| 333 |
{"key", ""}, |
| 334 |
{"key", "value"}, |
| 335 |
{"another-key", "another-value"}, |
| 336 |
}; |
| 337 |
|
| 338 |
// method ref |
| 339 |
ExpectReadString("test.service/rpc.method"); |
| 340 |
|
| 341 |
// metadata |
| 342 |
{ |
| 343 |
// count |
| 344 |
ExpectReadInt32(kMetadata.size()); |
| 345 |
for (const auto& md : kMetadata) { |
| 346 |
// metadata key |
| 347 |
ExpectReadByteArray(md.first); |
| 348 |
// metadata val |
| 349 |
// TODO(waynetu): metadata value can also be "parcelable". |
| 350 |
ExpectReadByteArray(md.second); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
// Since path and authority is not encoded as metadata in wire format, |
| 355 |
// wire_reader implementation should insert them as metadata before passing |
| 356 |
// to transport layer. |
| 357 |
auto metadata_expectation = kMetadata; |
| 358 |
metadata_expectation.push_back({":path", "/test.service/rpc.method"}); |
| 359 |
metadata_expectation.push_back({":authority", "binder.authority"}); |
| 360 |
|
| 361 |
EXPECT_CALL(*transport_stream_receiver_, |
| 362 |
NotifyRecvInitialMetadata( |
| 363 |
kFirstCallId, StatusOrContainerEq(metadata_expectation))); |
| 364 |
|
| 365 |
EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok()); |
| 366 |
} |
| 367 |
|
| 368 |
} // namespace grpc_binder |
| 369 |
|
| 370 |
int main(int argc, char** argv) { |
| 371 |
::testing::InitGoogleTest(&argc, argv); |
| 372 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 373 |
return RUN_ALL_TESTS(); |
| 374 |
} |