Debian Codegraph @

test/core/transport/binder/wire_writer_test.cc

raw
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 #include "src/core/ext/transport/binder/wire_format/wire_writer.h"
16
17 #include <string>
18 #include <utility>
19
20 #include <gtest/gtest.h>
21
22 #include "absl/memory/memory.h"
23
24 #include <grpcpp/impl/grpc_library.h>
25
26 #include "test/core/transport/binder/mock_objects.h"
27 #include "test/core/util/test_config.h"
28
29 namespace grpc_binder {
30
31 using ::testing::Return;
32
33 MATCHER_P(StrEqInt8Ptr, target, "") {
34 return std::string(reinterpret_cast<const char*>(arg), target.size()) ==
35 target;
36 }
37
38 TEST(WireWriterTest, RpcCall) {
39 grpc::internal::GrpcLibrary init_lib;
40 init_lib.init();
41 // Required because wire writer uses combiner internally.
42 grpc_core::ExecCtx exec_ctx;
43 auto mock_binder = std::make_unique<MockBinder>();
44 MockBinder& mock_binder_ref = *mock_binder;
45 MockWritableParcel mock_writable_parcel;
46 ON_CALL(mock_binder_ref, GetWritableParcel)
47 .WillByDefault(Return(&mock_writable_parcel));
48 WireWriterImpl wire_writer(std::move(mock_binder));
49
50 auto ExpectWriteByteArray = [&](const std::string& target) {
51 // length
52 EXPECT_CALL(mock_writable_parcel, WriteInt32(target.size()));
53 if (!target.empty()) {
54 // content
55 EXPECT_CALL(mock_writable_parcel,
56 WriteByteArray(StrEqInt8Ptr(target), target.size()));
57 }
58 };
59
60 ::testing::InSequence sequence;
61 int sequence_number = 0;
62
63 {
64 // flag
65 EXPECT_CALL(mock_writable_parcel, WriteInt32(0));
66 // sequence number
67 EXPECT_CALL(mock_writable_parcel, WriteInt32(sequence_number));
68
69 EXPECT_CALL(mock_binder_ref, Transact(BinderTransportTxCode(kFirstCallId)));
70
71 auto tx = std::make_unique<Transaction>(kFirstCallId, /*is_client=*/true);
72 EXPECT_TRUE(wire_writer.RpcCall(std::move(tx)).ok());
73 sequence_number++;
74 grpc_core::ExecCtx::Get()->Flush();
75 }
76 {
77 // flag
78 EXPECT_CALL(mock_writable_parcel, WriteInt32(kFlagPrefix));
79 // sequence number. This is another stream so the sequence number starts
80 // with 0.
81 EXPECT_CALL(mock_writable_parcel, WriteInt32(0));
82
83 EXPECT_CALL(mock_writable_parcel,
84 WriteString(absl::string_view("/example/method/ref")));
85
86 const std::vector<std::pair<std::string, std::string>> kMetadata = {
87 {"", ""},
88 {"", "value"},
89 {"key", ""},
90 {"key", "value"},
91 {"another-key", "another-value"},
92 };
93
94 // Number of metadata
95 EXPECT_CALL(mock_writable_parcel, WriteInt32(kMetadata.size()));
96
97 for (const auto& md : kMetadata) {
98 ExpectWriteByteArray(md.first);
99 ExpectWriteByteArray(md.second);
100 }
101
102 EXPECT_CALL(mock_binder_ref,
103 Transact(BinderTransportTxCode(kFirstCallId + 1)));
104
105 auto tx =
106 std::make_unique<Transaction>(kFirstCallId + 1, /*is_client=*/true);
107 tx->SetPrefix(kMetadata);
108 tx->SetMethodRef("/example/method/ref");
109 EXPECT_TRUE(wire_writer.RpcCall(std::move(tx)).ok());
110 grpc_core::ExecCtx::Get()->Flush();
111 }
112 {
113 // flag
114 EXPECT_CALL(mock_writable_parcel, WriteInt32(kFlagMessageData));
115 // sequence number
116 EXPECT_CALL(mock_writable_parcel, WriteInt32(sequence_number));
117
118 ExpectWriteByteArray("data");
119 EXPECT_CALL(mock_binder_ref, Transact(BinderTransportTxCode(kFirstCallId)));
120
121 auto tx = std::make_unique<Transaction>(kFirstCallId, /*is_client=*/true);
122 tx->SetData("data");
123 EXPECT_TRUE(wire_writer.RpcCall(std::move(tx)).ok());
124 sequence_number++;
125 grpc_core::ExecCtx::Get()->Flush();
126 }
127 {
128 // flag
129 EXPECT_CALL(mock_writable_parcel, WriteInt32(kFlagSuffix));
130 // sequence number
131 EXPECT_CALL(mock_writable_parcel, WriteInt32(sequence_number));
132
133 EXPECT_CALL(mock_binder_ref, Transact(BinderTransportTxCode(kFirstCallId)));
134
135 auto tx = std::make_unique<Transaction>(kFirstCallId, /*is_client=*/true);
136 tx->SetSuffix({});
137 EXPECT_TRUE(wire_writer.RpcCall(std::move(tx)).ok());
138 sequence_number++;
139 grpc_core::ExecCtx::Get()->Flush();
140 }
141 {
142 // flag
143 EXPECT_CALL(mock_writable_parcel,
144 WriteInt32(kFlagPrefix | kFlagMessageData | kFlagSuffix));
145 // sequence number
146 EXPECT_CALL(mock_writable_parcel, WriteInt32(sequence_number));
147
148 EXPECT_CALL(mock_writable_parcel,
149 WriteString(absl::string_view("/example/method/ref")));
150
151 const std::vector<std::pair<std::string, std::string>> kMetadata = {
152 {"", ""},
153 {"", "value"},
154 {"key", ""},
155 {"key", "value"},
156 {"another-key", "another-value"},
157 };
158
159 // Number of metadata
160 EXPECT_CALL(mock_writable_parcel, WriteInt32(kMetadata.size()));
161
162 for (const auto& md : kMetadata) {
163 ExpectWriteByteArray(md.first);
164 ExpectWriteByteArray(md.second);
165 }
166
167 // Empty message data
168 ExpectWriteByteArray("");
169
170 EXPECT_CALL(mock_binder_ref, Transact(BinderTransportTxCode(kFirstCallId)));
171
172 auto tx = std::make_unique<Transaction>(kFirstCallId, /*is_client=*/true);
173 // TODO(waynetu): Implement a helper function that automatically creates
174 // EXPECT_CALL based on the tx object.
175 tx->SetPrefix(kMetadata);
176 tx->SetMethodRef("/example/method/ref");
177 tx->SetData("");
178 tx->SetSuffix({});
179 EXPECT_TRUE(wire_writer.RpcCall(std::move(tx)).ok());
180 sequence_number++;
181 grpc_core::ExecCtx::Get()->Flush();
182 }
183
184 // Really large message
185 {
186 EXPECT_CALL(mock_writable_parcel,
187 WriteInt32(kFlagMessageData | kFlagMessageDataIsPartial));
188 EXPECT_CALL(mock_writable_parcel, WriteInt32(0));
189 ExpectWriteByteArray(std::string(WireWriterImpl::kBlockSize, 'a'));
190 EXPECT_CALL(mock_writable_parcel, GetDataSize)
191 .WillOnce(Return(WireWriterImpl::kBlockSize));
192 EXPECT_CALL(mock_binder_ref,
193 Transact(BinderTransportTxCode(kFirstCallId + 2)));
194
195 EXPECT_CALL(mock_writable_parcel,
196 WriteInt32(kFlagMessageData | kFlagMessageDataIsPartial));
197 EXPECT_CALL(mock_writable_parcel, WriteInt32(1));
198 ExpectWriteByteArray(std::string(WireWriterImpl::kBlockSize, 'a'));
199 EXPECT_CALL(mock_writable_parcel, GetDataSize)
200 .WillOnce(Return(WireWriterImpl::kBlockSize));
201 EXPECT_CALL(mock_binder_ref,
202 Transact(BinderTransportTxCode(kFirstCallId + 2)));
203
204 EXPECT_CALL(mock_writable_parcel, WriteInt32(kFlagMessageData));
205 EXPECT_CALL(mock_writable_parcel, WriteInt32(2));
206 ExpectWriteByteArray("a");
207 EXPECT_CALL(mock_writable_parcel, GetDataSize).WillOnce(Return(1));
208 EXPECT_CALL(mock_binder_ref,
209 Transact(BinderTransportTxCode(kFirstCallId + 2)));
210
211 // Use a new stream.
212 auto tx =
213 std::make_unique<Transaction>(kFirstCallId + 2, /*is_client=*/true);
214 tx->SetData(std::string(2 * WireWriterImpl::kBlockSize + 1, 'a'));
215 EXPECT_TRUE(wire_writer.RpcCall(std::move(tx)).ok());
216 grpc_core::ExecCtx::Get()->Flush();
217 }
218 // Really large message with metadata
219 {
220 EXPECT_CALL(
221 mock_writable_parcel,
222 WriteInt32(kFlagPrefix | kFlagMessageData | kFlagMessageDataIsPartial));
223 EXPECT_CALL(mock_writable_parcel, WriteInt32(0));
224 EXPECT_CALL(mock_writable_parcel, WriteString(absl::string_view("123")));
225 EXPECT_CALL(mock_writable_parcel, WriteInt32(0));
226 ExpectWriteByteArray(std::string(WireWriterImpl::kBlockSize, 'a'));
227 EXPECT_CALL(mock_writable_parcel, GetDataSize)
228 .WillOnce(Return(WireWriterImpl::kBlockSize));
229 EXPECT_CALL(mock_binder_ref,
230 Transact(BinderTransportTxCode(kFirstCallId + 3)));
231
232 EXPECT_CALL(mock_writable_parcel,
233 WriteInt32(kFlagMessageData | kFlagMessageDataIsPartial));
234 EXPECT_CALL(mock_writable_parcel, WriteInt32(1));
235 ExpectWriteByteArray(std::string(WireWriterImpl::kBlockSize, 'a'));
236 EXPECT_CALL(mock_writable_parcel, GetDataSize)
237 .WillOnce(Return(WireWriterImpl::kBlockSize));
238 EXPECT_CALL(mock_binder_ref,
239 Transact(BinderTransportTxCode(kFirstCallId + 3)));
240
241 EXPECT_CALL(mock_writable_parcel,
242 WriteInt32(kFlagMessageData | kFlagSuffix));
243 EXPECT_CALL(mock_writable_parcel, WriteInt32(2));
244 ExpectWriteByteArray("a");
245 EXPECT_CALL(mock_writable_parcel, GetDataSize).WillOnce(Return(1));
246 EXPECT_CALL(mock_binder_ref,
247 Transact(BinderTransportTxCode(kFirstCallId + 3)));
248
249 // Use a new stream.
250 auto tx =
251 std::make_unique<Transaction>(kFirstCallId + 3, /*is_client=*/true);
252 tx->SetPrefix({});
253 tx->SetMethodRef("123");
254 tx->SetData(std::string(2 * WireWriterImpl::kBlockSize + 1, 'a'));
255 tx->SetSuffix({});
256 EXPECT_TRUE(wire_writer.RpcCall(std::move(tx)).ok());
257 grpc_core::ExecCtx::Get()->Flush();
258 }
259 grpc_core::ExecCtx::Get()->Flush();
260 init_lib.shutdown();
261 }
262
263 } // namespace grpc_binder
264
265 int main(int argc, char** argv) {
266 ::testing::InitGoogleTest(&argc, argv);
267 grpc::testing::TestEnvironment env(&argc, argv);
268 return RUN_ALL_TESTS();
269 }