Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / test / core / transport / binder / wire_reader_test.cc
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 #include <memory>
22 #include <string>
23 #include <thread>
24 #include <utility>
25
26 #include <gtest/gtest.h>
27
28 #include "absl/memory/memory.h"
29
30 #include "src/core/ext/transport/binder/wire_format/wire_reader_impl.h"
31 #include "test/core/transport/binder/mock_objects.h"
32 #include "test/core/util/test_config.h"
33
34 namespace grpc_binder {
35
36 using ::testing::DoAll;
37 using ::testing::Return;
38 using ::testing::SetArgPointee;
39 using ::testing::StrictMock;
40
41 namespace {
42
43 class WireReaderTest : public ::testing::Test {
44  public:
45   WireReaderTest()
46       : transport_stream_receiver_(
47             std::make_shared<StrictMock<MockTransportStreamReceiver>>()),
48         wire_reader_(transport_stream_receiver_, /*is_client=*/true) {}
49
50  protected:
51   void ExpectReadInt32(int result) {
52     EXPECT_CALL(mock_readable_parcel_, ReadInt32)
53         .WillOnce(DoAll(SetArgPointee<0>(result), Return(absl::OkStatus())));
54   }
55
56   void ExpectReadByteArray(const std::string& buffer) {
57     ExpectReadInt32(buffer.length());
58     if (!buffer.empty()) {
59       EXPECT_CALL(mock_readable_parcel_, ReadByteArray)
60           .WillOnce([buffer](std::string* data) {
61             *data = buffer;
62             return absl::OkStatus();
63           });
64     }
65   }
66
67   void UnblockSetupTransport() {
68     // SETUP_TRANSPORT should finish before we can proceed with any other
69     // requests and streaming calls. The MockBinder will construct a
70     // MockTransactionReceiver, which will then sends SETUP_TRANSPORT request
71     // back to us.
72     wire_reader_.SetupTransport(absl::make_unique<MockBinder>());
73   }
74
75   template <typename T>
76   absl::Status CallProcessTransaction(T tx_code) {
77     return wire_reader_.ProcessTransaction(
78         static_cast<transaction_code_t>(tx_code), &mock_readable_parcel_);
79   }
80
81   std::shared_ptr<StrictMock<MockTransportStreamReceiver>>
82       transport_stream_receiver_;
83   WireReaderImpl wire_reader_;
84   MockReadableParcel mock_readable_parcel_;
85 };
86
87 MATCHER_P(StatusOrStrEq, target, "") {
88   if (!arg.ok()) return false;
89   return arg.value() == target;
90 }
91
92 MATCHER_P(StatusOrContainerEq, target, "") {
93   if (!arg.ok()) return false;
94   return arg.value() == target;
95 }
96
97 }  // namespace
98
99 TEST_F(WireReaderTest, SetupTransport) {
100   auto mock_binder = absl::make_unique<MockBinder>();
101   MockBinder& mock_binder_ref = *mock_binder;
102
103   ::testing::InSequence sequence;
104   EXPECT_CALL(mock_binder_ref, Initialize);
105   EXPECT_CALL(mock_binder_ref, PrepareTransaction);
106   const MockReadableParcel mock_readable_parcel;
107   EXPECT_CALL(mock_binder_ref, GetWritableParcel);
108
109   // Write version.
110   EXPECT_CALL(mock_binder_ref.GetWriter(), WriteInt32(77));
111
112   // The transaction receiver immediately informs the wire writer that the
113   // transport has been successfully set up.
114   EXPECT_CALL(mock_binder_ref, ConstructTxReceiver);
115
116   EXPECT_CALL(mock_binder_ref.GetReader(), ReadInt32);
117   EXPECT_CALL(mock_binder_ref.GetReader(), ReadBinder);
118
119   // Write transaction receiver.
120   EXPECT_CALL(mock_binder_ref.GetWriter(), WriteBinder);
121   // Perform transaction.
122   EXPECT_CALL(mock_binder_ref, Transact);
123
124   wire_reader_.SetupTransport(std::move(mock_binder));
125 }
126
127 TEST_F(WireReaderTest, ProcessTransactionControlMessageSetupTransport) {
128   ::testing::InSequence sequence;
129   UnblockSetupTransport();
130 }
131
132 TEST_F(WireReaderTest, ProcessTransactionControlMessagePingResponse) {
133   ::testing::InSequence sequence;
134   UnblockSetupTransport();
135   EXPECT_CALL(mock_readable_parcel_, ReadInt32);
136   EXPECT_TRUE(
137       CallProcessTransaction(BinderTransportTxCode::PING_RESPONSE).ok());
138 }
139
140 TEST_F(WireReaderTest, ProcessTransactionServerRpcDataEmptyFlagIgnored) {
141   ::testing::InSequence sequence;
142   UnblockSetupTransport();
143
144   // first transaction: empty flag
145   ExpectReadInt32(0);
146   // Won't further read sequence number.
147   EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok());
148 }
149
150 TEST_F(WireReaderTest,
151        ProcessTransactionServerRpcDataFlagPrefixWithoutMetadata) {
152   ::testing::InSequence sequence;
153   UnblockSetupTransport();
154
155   // flag
156   ExpectReadInt32(kFlagPrefix);
157   // sequence number
158   ExpectReadInt32(0);
159
160   // count
161   ExpectReadInt32(0);
162   EXPECT_CALL(
163       *transport_stream_receiver_,
164       NotifyRecvInitialMetadata(kFirstCallId, StatusOrContainerEq(Metadata{})));
165
166   EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok());
167 }
168
169 TEST_F(WireReaderTest, ProcessTransactionServerRpcDataFlagPrefixWithMetadata) {
170   ::testing::InSequence sequence;
171   UnblockSetupTransport();
172
173   // flag
174   ExpectReadInt32(kFlagPrefix);
175   // sequence number
176   ExpectReadInt32(0);
177
178   const std::vector<std::pair<std::string, std::string>> kMetadata = {
179       {"", ""},
180       {"", "value"},
181       {"key", ""},
182       {"key", "value"},
183       {"another-key", "another-value"},
184   };
185
186   // count
187   ExpectReadInt32(kMetadata.size());
188   for (const auto& md : kMetadata) {
189     // metadata key
190     ExpectReadByteArray(md.first);
191     // metadata val
192     // TODO(waynetu): metadata value can also be "parcelable".
193     ExpectReadByteArray(md.second);
194   }
195   EXPECT_CALL(
196       *transport_stream_receiver_,
197       NotifyRecvInitialMetadata(kFirstCallId, StatusOrContainerEq(kMetadata)));
198
199   EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok());
200 }
201
202 TEST_F(WireReaderTest, ProcessTransactionServerRpcDataFlagMessageDataNonEmpty) {
203   ::testing::InSequence sequence;
204   UnblockSetupTransport();
205
206   // flag
207   ExpectReadInt32(kFlagMessageData);
208   // sequence number
209   ExpectReadInt32(0);
210
211   // message data
212   // TODO(waynetu): message data can also be "parcelable".
213   const std::string kMessageData = "message data";
214   ExpectReadByteArray(kMessageData);
215   EXPECT_CALL(*transport_stream_receiver_,
216               NotifyRecvMessage(kFirstCallId, StatusOrStrEq(kMessageData)));
217
218   EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok());
219 }
220
221 TEST_F(WireReaderTest, ProcessTransactionServerRpcDataFlagMessageDataEmpty) {
222   ::testing::InSequence sequence;
223   UnblockSetupTransport();
224
225   // flag
226   ExpectReadInt32(kFlagMessageData);
227   // sequence number
228   ExpectReadInt32(0);
229
230   // message data
231   // TODO(waynetu): message data can also be "parcelable".
232   const std::string kMessageData = "";
233   ExpectReadByteArray(kMessageData);
234   EXPECT_CALL(*transport_stream_receiver_,
235               NotifyRecvMessage(kFirstCallId, StatusOrStrEq(kMessageData)));
236
237   EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok());
238 }
239
240 TEST_F(WireReaderTest, ProcessTransactionServerRpcDataFlagSuffixWithStatus) {
241   ::testing::InSequence sequence;
242   UnblockSetupTransport();
243
244   constexpr int kStatus = 0x1234;
245   // flag
246   ExpectReadInt32(kFlagSuffix | kFlagStatusDescription | (kStatus << 16));
247   // sequence number
248   ExpectReadInt32(0);
249   // status description
250   EXPECT_CALL(mock_readable_parcel_, ReadString);
251   // metadata count
252   ExpectReadInt32(0);
253   EXPECT_CALL(*transport_stream_receiver_,
254               NotifyRecvTrailingMetadata(
255                   kFirstCallId, StatusOrContainerEq(Metadata{}), kStatus));
256
257   EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok());
258 }
259
260 TEST_F(WireReaderTest, ProcessTransactionServerRpcDataFlagSuffixWithoutStatus) {
261   ::testing::InSequence sequence;
262   UnblockSetupTransport();
263
264   // flag
265   ExpectReadInt32(kFlagSuffix);
266   // sequence number
267   ExpectReadInt32(0);
268   // No status description
269   // metadata count
270   ExpectReadInt32(0);
271   EXPECT_CALL(*transport_stream_receiver_,
272               NotifyRecvTrailingMetadata(kFirstCallId,
273                                          StatusOrContainerEq(Metadata{}), 0));
274
275   EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok());
276 }
277
278 TEST_F(WireReaderTest, InBoundFlowControl) {
279   ::testing::InSequence sequence;
280   UnblockSetupTransport();
281
282   // data size
283   EXPECT_CALL(mock_readable_parcel_, GetDataSize).WillOnce(Return(1000));
284   // flag
285   ExpectReadInt32(kFlagMessageData | kFlagMessageDataIsPartial);
286   // sequence number
287   ExpectReadInt32(0);
288   // message size
289   ExpectReadInt32(1000);
290   EXPECT_CALL(mock_readable_parcel_, ReadByteArray)
291       .WillOnce(DoAll(SetArgPointee<0>(std::string(1000, 'a')),
292                       Return(absl::OkStatus())));
293
294   // Data is not completed. No callback will be triggered.
295   EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok());
296
297   EXPECT_CALL(mock_readable_parcel_, GetDataSize).WillOnce(Return(1000));
298   // flag
299   ExpectReadInt32(kFlagMessageData);
300   // sequence number
301   ExpectReadInt32(1);
302   // message size
303   ExpectReadInt32(1000);
304   EXPECT_CALL(mock_readable_parcel_, ReadByteArray)
305       .WillOnce(DoAll(SetArgPointee<0>(std::string(1000, 'b')),
306                       Return(absl::OkStatus())));
307
308   EXPECT_CALL(*transport_stream_receiver_,
309               NotifyRecvMessage(kFirstCallId,
310                                 StatusOrContainerEq(std::string(1000, 'a') +
311                                                     std::string(1000, 'b'))));
312   EXPECT_TRUE(CallProcessTransaction(kFirstCallId).ok());
313 }
314
315 }  // namespace grpc_binder
316
317 int main(int argc, char** argv) {
318   ::testing::InitGoogleTest(&argc, argv);
319   grpc::testing::TestEnvironment env(argc, argv);
320   return RUN_ALL_TESTS();
321 }