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 <binder/Parcel.h>
18 #include <binder/IPCThreadState.h>
19 #include <gtest/gtest.h>
20
21 using android::IPCThreadState;
22 using android::OK;
23 using android::Parcel;
24 using android::String16;
25 using android::String8;
26 using android::status_t;
27
TEST(Parcel,NonNullTerminatedString8)28 TEST(Parcel, NonNullTerminatedString8) {
29 String8 kTestString = String8("test-is-good");
30
31 // write non-null terminated string
32 Parcel p;
33 p.writeString8(kTestString);
34 p.setDataPosition(0);
35 // BAD! assumption of wire format for test
36 // write over length of string
37 p.writeInt32(kTestString.size() - 2);
38
39 p.setDataPosition(0);
40 String8 output;
41 EXPECT_NE(OK, p.readString8(&output));
42 EXPECT_EQ(output.size(), 0);
43 }
44
TEST(Parcel,NonNullTerminatedString16)45 TEST(Parcel, NonNullTerminatedString16) {
46 String16 kTestString = String16("test-is-good");
47
48 // write non-null terminated string
49 Parcel p;
50 p.writeString16(kTestString);
51 p.setDataPosition(0);
52 // BAD! assumption of wire format for test
53 // write over length of string
54 p.writeInt32(kTestString.size() - 2);
55
56 p.setDataPosition(0);
57 String16 output;
58 EXPECT_NE(OK, p.readString16(&output));
59 EXPECT_EQ(output.size(), 0);
60 }
61
62 // Tests a second operation results in a parcel at the same location as it
63 // started.
parcelOpSameLength(const std::function<void (Parcel *)> & a,const std::function<void (Parcel *)> & b)64 void parcelOpSameLength(const std::function<void(Parcel*)>& a, const std::function<void(Parcel*)>& b) {
65 Parcel p;
66 a(&p);
67 size_t end = p.dataPosition();
68 p.setDataPosition(0);
69 b(&p);
70 EXPECT_EQ(end, p.dataPosition());
71 }
72
TEST(Parcel,InverseInterfaceToken)73 TEST(Parcel, InverseInterfaceToken) {
74 const String16 token = String16("asdf");
75 parcelOpSameLength([&] (Parcel* p) {
76 p->writeInterfaceToken(token);
77 }, [&] (Parcel* p) {
78 EXPECT_TRUE(p->enforceInterface(token, IPCThreadState::self()));
79 });
80 }
81
TEST(Parcel,Utf8FromUtf16Read)82 TEST(Parcel, Utf8FromUtf16Read) {
83 const char* token = "asdf";
84 parcelOpSameLength([&] (Parcel* p) {
85 p->writeString16(String16(token));
86 }, [&] (Parcel* p) {
87 std::string s;
88 EXPECT_EQ(OK, p->readUtf8FromUtf16(&s));
89 EXPECT_EQ(token, s);
90 });
91 }
92
TEST(Parcel,Utf8AsUtf16Write)93 TEST(Parcel, Utf8AsUtf16Write) {
94 std::string token = "asdf";
95 parcelOpSameLength([&] (Parcel* p) {
96 p->writeUtf8AsUtf16(token);
97 }, [&] (Parcel* p) {
98 String16 s;
99 EXPECT_EQ(OK, p->readString16(&s));
100 EXPECT_EQ(s, String16(token.c_str()));
101 });
102 }
103
104 template <typename T>
105 using readFunc = status_t (Parcel::*)(T* out) const;
106 template <typename T>
107 using writeFunc = status_t (Parcel::*)(const T& in);
108 template <typename T>
109 using copyWriteFunc = status_t (Parcel::*)(T in);
110
111 template <typename T, typename WRITE_FUNC>
readWriteInverse(std::vector<T> && ts,readFunc<T> r,WRITE_FUNC w)112 void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, WRITE_FUNC w) {
113 for (const T& value : ts) {
114 parcelOpSameLength([&] (Parcel* p) {
115 (*p.*w)(value);
116 }, [&] (Parcel* p) {
117 T outValue;
118 EXPECT_EQ(OK, (*p.*r)(&outValue));
119 EXPECT_EQ(value, outValue);
120 });
121 }
122 }
123
124 template <typename T>
readWriteInverse(std::vector<T> && ts,readFunc<T> r,writeFunc<T> w)125 void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, writeFunc<T> w) {
126 readWriteInverse<T, writeFunc<T>>(std::move(ts), r, w);
127 }
128 template <typename T>
readWriteInverse(std::vector<T> && ts,readFunc<T> r,copyWriteFunc<T> w)129 void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, copyWriteFunc<T> w) {
130 readWriteInverse<T, copyWriteFunc<T>>(std::move(ts), r, w);
131 }
132
133 #define TEST_READ_WRITE_INVERSE(type, name, ...) \
134 TEST(Parcel, Inverse##name) { \
135 readWriteInverse<type>(__VA_ARGS__, &Parcel::read##name, &Parcel::write##name); \
136 }
137
138 TEST_READ_WRITE_INVERSE(int32_t, Int32, {-2, -1, 0, 1, 2});
139 TEST_READ_WRITE_INVERSE(uint32_t, Uint32, {0, 1, 2});
140 TEST_READ_WRITE_INVERSE(int64_t, Int64, {-2, -1, 0, 1, 2});
141 TEST_READ_WRITE_INVERSE(uint64_t, Uint64, {0, 1, 2});
142 TEST_READ_WRITE_INVERSE(float, Float, {-1.0f, 0.0f, 3.14f});
143 TEST_READ_WRITE_INVERSE(double, Double, {-1.0, 0.0, 3.14});
144 TEST_READ_WRITE_INVERSE(bool, Bool, {true, false});
145 TEST_READ_WRITE_INVERSE(char16_t, Char, {u'a', u'\0'});
146 TEST_READ_WRITE_INVERSE(int8_t, Byte, {-1, 0, 1});
147 TEST_READ_WRITE_INVERSE(String8, String8, {String8(), String8("a"), String8("asdf")});
148 TEST_READ_WRITE_INVERSE(String16, String16, {String16(), String16("a"), String16("asdf")});
149