1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <android/binder_ibinder_platform.h>
18 #include <android/binder_libbinder.h>
19 #include <binder/IServiceManager.h>
20 #include <binder/Parcel.h>
21 #include <binder/ParcelFileDescriptor.h>
22 #include <binder/ProcessState.h>
23 #include <binder/Status.h>
24 #include <gtest/gtest.h>
25 #include <utils/Errors.h>
26 #include <utils/String16.h>
27 #include "android-base/file.h"
28 #include "serialization.hpp"
29 
30 #include <cmath>
31 #include <cstdint>
32 #include <iostream>
33 #include <optional>
34 
35 using namespace std;
36 using namespace android;
37 using android::base::unique_fd;
38 using android::os::ParcelFileDescriptor;
39 
40 // defined in Rust
41 extern "C" AIBinder *rust_service();
42 
43 
44 const int8_t TESTDATA_I8[4] = {-128, 0, 117, 127};
45 const uint8_t TESTDATA_U8[4] = {0, 42, 117, 255};
46 const char16_t TESTDATA_CHARS[4] = {0, 42, 117, numeric_limits<char16_t>::max()};
47 const int32_t TESTDATA_I32[4] = {numeric_limits<int32_t>::min(), 0, 117, numeric_limits<int32_t>::max()};
48 const int64_t TESTDATA_I64[4] = {numeric_limits<int64_t>::min(), 0, 117, numeric_limits<int64_t>::max()};
49 const uint64_t TESTDATA_U64[4] = {0, 42, 117, numeric_limits<uint64_t>::max()};
50 const float TESTDATA_FLOAT[4] = {
51         numeric_limits<float>::quiet_NaN(),
52         -numeric_limits<float>::infinity(),
53         117.0,
54         numeric_limits<float>::infinity(),
55 };
56 const double TESTDATA_DOUBLE[4] = {
57         numeric_limits<double>::quiet_NaN(),
58         -numeric_limits<double>::infinity(),
59         117.0,
60         numeric_limits<double>::infinity(),
61 };
62 const bool TESTDATA_BOOL[4] = {true, false, false, true};
63 const char* const TESTDATA_STRS[4] = {"", nullptr, "test", ""};
64 
65 static ::testing::Environment* gEnvironment;
66 
67 class SerializationEnvironment : public ::testing::Environment {
68 public:
SetUp()69     void SetUp() override {
70         m_server = AIBinder_toPlatformBinder(rust_service());
71     }
72 
getServer(void)73     sp<IBinder> getServer(void) { return m_server; }
74 
75 private:
76     sp<IBinder> m_server;
77 };
78 
79 
80 class SerializationTest : public ::testing::Test {
81 protected:
SetUp()82     void SetUp() override {
83         ASSERT_NE(gEnvironment, nullptr);
84         m_server = static_cast<SerializationEnvironment *>(gEnvironment)->getServer();
85     }
86 
87     sp<IBinder> m_server;
88 };
89 
90 
TEST_F(SerializationTest,SerializeBool)91 TEST_F(SerializationTest, SerializeBool) {
92     android::Parcel data;
93     data.writeInterfaceToken(String16("read_parcel_test"));
94 
95     vector<bool> bools(begin(TESTDATA_BOOL), end(TESTDATA_BOOL));
96     ASSERT_EQ(data.writeBool(true), OK);
97     ASSERT_EQ(data.writeBool(false), OK);
98     ASSERT_EQ(data.writeBoolVector(bools), OK);
99     ASSERT_EQ(data.writeBoolVector(nullopt), OK);
100 
101     android::Parcel reply;
102     ASSERT_EQ(m_server->transact(TEST_BOOL, data, &reply), OK);
103 
104     vector<bool> read_bools;
105     optional<vector<bool>> maybe_bools;
106     ASSERT_EQ(reply.readBool(), true);
107     ASSERT_EQ(reply.readBool(), false);
108     ASSERT_EQ(reply.readBoolVector(&read_bools), OK);
109     ASSERT_EQ(read_bools, bools);
110     ASSERT_EQ(reply.readBoolVector(&maybe_bools), OK);
111     ASSERT_EQ(maybe_bools, nullopt);
112 
113     int32_t end;
114     ASSERT_EQ(reply.readInt32(&end), NOT_ENOUGH_DATA);
115 }
116 
TEST_F(SerializationTest,SerializeByte)117 TEST_F(SerializationTest, SerializeByte) {
118     android::Parcel data;
119     data.writeInterfaceToken(String16("read_parcel_test"));
120 
121     vector<int8_t> i8s(begin(TESTDATA_I8), end(TESTDATA_I8));
122     vector<uint8_t> u8s(begin(TESTDATA_U8), end(TESTDATA_U8));
123     data.writeByte(0);
124     data.writeByte(1);
125     data.writeByte(numeric_limits<int8_t>::max());
126     data.writeByteVector(i8s);
127     data.writeByteVector(u8s);
128     data.writeByteVector(optional<vector<int8_t>>({}));
129 
130     android::Parcel reply;
131     ASSERT_EQ(m_server->transact(TEST_BYTE, data, &reply), OK);
132 
133     vector<int8_t> read_i8s;
134     vector<uint8_t> read_u8s;
135     optional<vector<int8_t>> maybe_i8s;
136     ASSERT_EQ(reply.readByte(), 0);
137     ASSERT_EQ(reply.readByte(), 1);
138     ASSERT_EQ(reply.readByte(), numeric_limits<int8_t>::max());
139     ASSERT_EQ(reply.readByteVector(&read_i8s), OK);
140     ASSERT_EQ(read_i8s, i8s);
141     ASSERT_EQ(reply.readByteVector(&read_u8s), OK);
142     ASSERT_EQ(read_u8s, u8s);
143     ASSERT_EQ(reply.readByteVector(&maybe_i8s), OK);
144     ASSERT_EQ(maybe_i8s, nullopt);
145 
146     int32_t end;
147     ASSERT_EQ(reply.readInt32(&end), NOT_ENOUGH_DATA);
148 }
149 
TEST_F(SerializationTest,SerializeU16)150 TEST_F(SerializationTest, SerializeU16) {
151     android::Parcel data;
152     data.writeInterfaceToken(String16("read_parcel_test"));
153 
154     vector<char16_t> chars(begin(TESTDATA_CHARS), end(TESTDATA_CHARS));
155     data.writeChar(0);
156     data.writeChar(1);
157     data.writeChar(numeric_limits<char16_t>::max());
158     data.writeCharVector(chars);
159     data.writeCharVector(nullopt);
160 
161     android::Parcel reply;
162     ASSERT_EQ(m_server->transact(TEST_U16, data, &reply), OK);
163 
164     vector<char16_t> read_chars;
165     optional<vector<char16_t>> maybe_chars;
166     ASSERT_EQ(reply.readChar(), 0);
167     ASSERT_EQ(reply.readChar(), 1);
168     ASSERT_EQ(reply.readChar(), numeric_limits<char16_t>::max());
169     ASSERT_EQ(reply.readCharVector(&read_chars), OK);
170     ASSERT_EQ(read_chars, chars);
171     ASSERT_EQ(reply.readCharVector(&maybe_chars), OK);
172     ASSERT_EQ(maybe_chars, nullopt);
173 
174     int32_t end;
175     ASSERT_EQ(reply.readInt32(&end), NOT_ENOUGH_DATA);
176 }
177 
TEST_F(SerializationTest,SerializeI32)178 TEST_F(SerializationTest, SerializeI32) {
179     android::Parcel data;
180     data.writeInterfaceToken(String16("read_parcel_test"));
181 
182     vector<int32_t> i32s(begin(TESTDATA_I32), end(TESTDATA_I32));
183     data.writeInt32(0);
184     data.writeInt32(1);
185     data.writeInt32(numeric_limits<int32_t>::max());
186     data.writeInt32Vector(i32s);
187     data.writeInt32Vector(nullopt);
188 
189     android::Parcel reply;
190     ASSERT_EQ(m_server->transact(TEST_I32, data, &reply), OK);
191 
192     vector<int32_t> read_i32s;
193     optional<vector<int32_t>> maybe_i32s;
194     ASSERT_EQ(reply.readInt32(), 0);
195     ASSERT_EQ(reply.readInt32(), 1);
196     ASSERT_EQ(reply.readInt32(), numeric_limits<int32_t>::max());
197     ASSERT_EQ(reply.readInt32Vector(&read_i32s), OK);
198     ASSERT_EQ(read_i32s, i32s);
199     ASSERT_EQ(reply.readInt32Vector(&maybe_i32s), OK);
200     ASSERT_EQ(maybe_i32s, nullopt);
201 
202     int32_t end;
203     ASSERT_EQ(reply.readInt32(&end), NOT_ENOUGH_DATA);
204 }
205 
TEST_F(SerializationTest,SerializeI64)206 TEST_F(SerializationTest, SerializeI64) {
207     android::Parcel data;
208     data.writeInterfaceToken(String16("read_parcel_test"));
209 
210     vector<int64_t> i64s(begin(TESTDATA_I64), end(TESTDATA_I64));
211     data.writeInt64(0);
212     data.writeInt64(1);
213     data.writeInt64(numeric_limits<int64_t>::max());
214     data.writeInt64Vector(i64s);
215     data.writeInt64Vector(nullopt);
216 
217     android::Parcel reply;
218     ASSERT_EQ(m_server->transact(TEST_I64, data, &reply), OK);
219 
220     vector<int64_t> read_i64s;
221     optional<vector<int64_t>> maybe_i64s;
222     ASSERT_EQ(reply.readInt64(), 0);
223     ASSERT_EQ(reply.readInt64(), 1);
224     ASSERT_EQ(reply.readInt64(), numeric_limits<int64_t>::max());
225     ASSERT_EQ(reply.readInt64Vector(&read_i64s), OK);
226     ASSERT_EQ(read_i64s, i64s);
227     ASSERT_EQ(reply.readInt64Vector(&maybe_i64s), OK);
228     ASSERT_EQ(maybe_i64s, nullopt);
229 
230     int32_t end;
231     ASSERT_EQ(reply.readInt32(&end), NOT_ENOUGH_DATA);
232 }
233 
TEST_F(SerializationTest,SerializeU64)234 TEST_F(SerializationTest, SerializeU64) {
235     android::Parcel data;
236     data.writeInterfaceToken(String16("read_parcel_test"));
237 
238     vector<uint64_t> u64s(begin(TESTDATA_U64), end(TESTDATA_U64));
239     data.writeUint64(0);
240     data.writeUint64(1);
241     data.writeUint64(numeric_limits<uint64_t>::max());
242     data.writeUint64Vector(u64s);
243     data.writeUint64Vector(nullopt);
244 
245     android::Parcel reply;
246     ASSERT_EQ(m_server->transact(TEST_U64, data, &reply), OK);
247 
248     vector<uint64_t> read_u64s;
249     optional<vector<uint64_t>> maybe_u64s;
250     ASSERT_EQ(reply.readUint64(), 0);
251     ASSERT_EQ(reply.readUint64(), 1);
252     ASSERT_EQ(reply.readUint64(), numeric_limits<uint64_t>::max());
253     ASSERT_EQ(reply.readUint64Vector(&read_u64s), OK);
254     ASSERT_EQ(read_u64s, u64s);
255     ASSERT_EQ(reply.readUint64Vector(&maybe_u64s), OK);
256     ASSERT_EQ(maybe_u64s, nullopt);
257 
258     int32_t end;
259     ASSERT_EQ(reply.readInt32(&end), NOT_ENOUGH_DATA);
260 }
261 
TEST_F(SerializationTest,SerializeF32)262 TEST_F(SerializationTest, SerializeF32) {
263     android::Parcel data;
264     data.writeInterfaceToken(String16("read_parcel_test"));
265 
266     vector<float> floats(begin(TESTDATA_FLOAT), end(TESTDATA_FLOAT));
267     data.writeFloat(0);
268     data.writeFloatVector(floats);
269     data.writeFloatVector(nullopt);
270 
271     android::Parcel reply;
272     ASSERT_EQ(m_server->transact(TEST_F32, data, &reply), OK);
273 
274     vector<float> read_floats;
275     optional<vector<float>> maybe_floats;
276     ASSERT_EQ(reply.readFloat(), 0);
277     ASSERT_EQ(reply.readFloatVector(&read_floats), OK);
278     ASSERT_TRUE(isnan(read_floats[0]));
279     ASSERT_EQ(read_floats[1], floats[1]);
280     ASSERT_EQ(read_floats[2], floats[2]);
281     ASSERT_EQ(read_floats[3], floats[3]);
282     ASSERT_EQ(reply.readFloatVector(&maybe_floats), OK);
283     ASSERT_EQ(maybe_floats, nullopt);
284 
285     int32_t end;
286     ASSERT_EQ(reply.readInt32(&end), NOT_ENOUGH_DATA);
287 }
288 
TEST_F(SerializationTest,SerializeF64)289 TEST_F(SerializationTest, SerializeF64) {
290     android::Parcel data;
291     data.writeInterfaceToken(String16("read_parcel_test"));
292 
293     vector<double> doubles(begin(TESTDATA_DOUBLE), end(TESTDATA_DOUBLE));
294     data.writeDouble(0);
295     data.writeDoubleVector(doubles);
296     data.writeDoubleVector(nullopt);
297 
298     android::Parcel reply;
299     ASSERT_EQ(m_server->transact(TEST_F64, data, &reply), OK);
300 
301     vector<double> read_doubles;
302     optional<vector<double>> maybe_doubles;
303     ASSERT_EQ(reply.readDouble(), 0);
304     ASSERT_EQ(reply.readDoubleVector(&read_doubles), OK);
305     ASSERT_TRUE(isnan(read_doubles[0]));
306     ASSERT_EQ(read_doubles[1], doubles[1]);
307     ASSERT_EQ(read_doubles[2], doubles[2]);
308     ASSERT_EQ(read_doubles[3], doubles[3]);
309     ASSERT_EQ(reply.readDoubleVector(&maybe_doubles), OK);
310     ASSERT_EQ(maybe_doubles, nullopt);
311 
312     int32_t end;
313     ASSERT_EQ(reply.readInt32(&end), NOT_ENOUGH_DATA);
314 }
315 
TEST_F(SerializationTest,SerializeString)316 TEST_F(SerializationTest, SerializeString) {
317     android::Parcel data;
318     data.writeInterfaceToken(String16("read_parcel_test"));
319 
320     vector<optional<String16>> strings;
321     for (auto I = begin(TESTDATA_STRS), E = end(TESTDATA_STRS); I != E; ++I) {
322         if (*I == nullptr) {
323             strings.push_back(optional<String16>());
324         } else {
325             strings.emplace_back(*I);
326         }
327     }
328     data.writeUtf8AsUtf16(string("testing"));
329     data.writeString16(nullopt);
330     data.writeString16Vector(strings);
331     data.writeString16Vector(nullopt);
332 
333     android::Parcel reply;
334     ASSERT_EQ(m_server->transact(TEST_STRING, data, &reply), OK);
335 
336     optional<String16> maybe_string;
337     optional<vector<optional<String16>>> read_strings;
338     ASSERT_EQ(reply.readString16(), String16("testing"));
339     ASSERT_EQ(reply.readString16(&maybe_string), OK);
340     ASSERT_EQ(maybe_string, nullopt);
341     ASSERT_EQ(reply.readString16Vector(&read_strings), OK);
342     ASSERT_EQ(read_strings, strings);
343     ASSERT_EQ(reply.readString16Vector(&read_strings), OK);
344     ASSERT_EQ(read_strings, nullopt);
345 
346     int32_t end;
347     ASSERT_EQ(reply.readInt32(&end), NOT_ENOUGH_DATA);
348 }
349 
TEST_F(SerializationTest,SerializeFileDescriptor)350 TEST_F(SerializationTest, SerializeFileDescriptor) {
351     unique_fd out_file, in_file;
352     ASSERT_TRUE(base::Pipe(&out_file, &in_file));
353 
354     vector<ParcelFileDescriptor> file_descriptors;
355     file_descriptors.push_back(ParcelFileDescriptor(std::move(out_file)));
356     file_descriptors.push_back(ParcelFileDescriptor(std::move(in_file)));
357 
358     android::Parcel data;
359     data.writeInterfaceToken(String16("read_parcel_test"));
360 
361     data.writeParcelable(file_descriptors[0]);
362     data.writeParcelable(file_descriptors[1]);
363     data.writeParcelableVector(file_descriptors);
364 
365     android::Parcel reply;
366     ASSERT_EQ(m_server->transact(TEST_FILE_DESCRIPTOR, data, &reply), OK);
367 
368     ParcelFileDescriptor returned_fd1, returned_fd2;
369     vector<ParcelFileDescriptor> returned_file_descriptors;
370     ASSERT_EQ(reply.readParcelable(&returned_fd1), OK);
371     ASSERT_EQ(reply.readParcelable(&returned_fd2), OK);
372     ASSERT_EQ(reply.readParcelableVector(&returned_file_descriptors), OK);
373 
374     int32_t end;
375     ASSERT_EQ(reply.readInt32(&end), NOT_ENOUGH_DATA);
376 
377     base::WriteStringToFd("Testing", returned_fd2.get());
378     base::WriteStringToFd("File", returned_file_descriptors[1].get());
379     base::WriteStringToFd("Descriptors", file_descriptors[1].get());
380 
381     string expected = "TestingFileDescriptors";
382     vector<char> buf(expected.length());
383     base::ReadFully(file_descriptors[0].release(), buf.data(), buf.size());
384     ASSERT_EQ(expected, string(buf.data()));
385 }
386 
TEST_F(SerializationTest,SerializeIBinder)387 TEST_F(SerializationTest, SerializeIBinder) {
388     android::Parcel data;
389     data.writeInterfaceToken(String16("read_parcel_test"));
390 
391     data.writeStrongBinder(m_server);
392     data.writeStrongBinder(nullptr);
393     data.writeStrongBinderVector({m_server, nullptr});
394     data.writeStrongBinderVector(nullopt);
395 
396     android::Parcel reply;
397     ASSERT_EQ(m_server->transact(TEST_IBINDER, data, &reply), OK);
398 
399     optional<vector<sp<IBinder>>> binders;
400     ASSERT_TRUE(reply.readStrongBinder());
401     ASSERT_FALSE(reply.readStrongBinder());
402     ASSERT_EQ(reply.readStrongBinderVector(&binders), OK);
403     ASSERT_EQ(binders->size(), 2);
404     ASSERT_TRUE((*binders)[0]);
405     ASSERT_FALSE((*binders)[1]);
406     ASSERT_EQ(reply.readStrongBinderVector(&binders), OK);
407     ASSERT_FALSE(binders);
408 
409     int32_t end;
410     ASSERT_EQ(reply.readInt32(&end), NOT_ENOUGH_DATA);
411 }
412 
TEST_F(SerializationTest,SerializeStatus)413 TEST_F(SerializationTest, SerializeStatus) {
414     android::Parcel data;
415     data.writeInterfaceToken(String16("read_parcel_test"));
416 
417     binder::Status::ok().writeToParcel(&data);
418     binder::Status::fromExceptionCode(binder::Status::EX_NULL_POINTER, "a status message")
419             .writeToParcel(&data);
420     binder::Status::fromServiceSpecificError(42, "a service-specific error").writeToParcel(&data);
421 
422     android::Parcel reply;
423     ASSERT_EQ(m_server->transact(TEST_STATUS, data, &reply), OK);
424 
425     binder::Status status;
426 
427     ASSERT_EQ(status.readFromParcel(reply), OK);
428     ASSERT_TRUE(status.isOk());
429 
430     ASSERT_EQ(status.readFromParcel(reply), OK);
431     ASSERT_EQ(status.exceptionCode(), binder::Status::EX_NULL_POINTER);
432     ASSERT_EQ(status.exceptionMessage(), "a status message");
433 
434     ASSERT_EQ(status.readFromParcel(reply), OK);
435     ASSERT_EQ(status.serviceSpecificErrorCode(), 42);
436     ASSERT_EQ(status.exceptionMessage(), "a service-specific error");
437 
438     int32_t end;
439     ASSERT_EQ(reply.readInt32(&end), NOT_ENOUGH_DATA);
440 }
441 
442 // Test that failures from Rust properly propagate to C++
TEST_F(SerializationTest,SerializeRustFail)443 TEST_F(SerializationTest, SerializeRustFail) {
444     android::Parcel data;
445     data.writeInterfaceToken(String16("read_parcel_test"));
446     ASSERT_EQ(m_server->transact(TEST_FAIL, data, nullptr), FAILED_TRANSACTION);
447 }
448 
main(int argc,char ** argv)449 int main(int argc, char **argv) {
450     ::testing::InitGoogleTest(&argc, argv);
451     gEnvironment = AddGlobalTestEnvironment(new SerializationEnvironment());
452     ProcessState::self()->startThreadPool();
453     return RUN_ALL_TESTS();
454 }
455