blob: 34fc43f9261ea731dd15df52225e0d0af479261c [file] [log] [blame]
Steven Morelandcefba612020-11-05 22:57:06 +00001/*
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
Steven Morelandcefba612020-11-05 22:57:06 +000017#include <binder/IPCThreadState.h>
Steven Morelandf80809b2021-10-07 18:09:20 -070018#include <binder/Parcel.h>
Jooyung Hand23f9502021-12-23 14:39:57 +090019#include <binder/Status.h>
Steven Morelandf80809b2021-10-07 18:09:20 -070020#include <cutils/ashmem.h>
Steven Morelandcefba612020-11-05 22:57:06 +000021#include <gtest/gtest.h>
22
Steven Moreland4b2f18d2022-03-24 00:32:25 +000023using android::BBinder;
24using android::IBinder;
Steven Morelandcefba612020-11-05 22:57:06 +000025using android::IPCThreadState;
26using android::OK;
27using android::Parcel;
Steven Moreland4b2f18d2022-03-24 00:32:25 +000028using android::sp;
Jooyung Hand23f9502021-12-23 14:39:57 +090029using android::status_t;
Steven Morelandcefba612020-11-05 22:57:06 +000030using android::String16;
31using android::String8;
Jooyung Hand23f9502021-12-23 14:39:57 +090032using android::binder::Status;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070033using android::binder::unique_fd;
Steven Morelandcefba612020-11-05 22:57:06 +000034
Steven Moreland8f39fa02020-11-18 00:35:25 +000035TEST(Parcel, NonNullTerminatedString8) {
36 String8 kTestString = String8("test-is-good");
37
38 // write non-null terminated string
39 Parcel p;
40 p.writeString8(kTestString);
41 p.setDataPosition(0);
42 // BAD! assumption of wire format for test
43 // write over length of string
44 p.writeInt32(kTestString.size() - 2);
45
46 p.setDataPosition(0);
47 String8 output;
48 EXPECT_NE(OK, p.readString8(&output));
49 EXPECT_EQ(output.size(), 0);
50}
51
52TEST(Parcel, NonNullTerminatedString16) {
53 String16 kTestString = String16("test-is-good");
54
55 // write non-null terminated string
56 Parcel p;
57 p.writeString16(kTestString);
58 p.setDataPosition(0);
59 // BAD! assumption of wire format for test
60 // write over length of string
61 p.writeInt32(kTestString.size() - 2);
62
63 p.setDataPosition(0);
64 String16 output;
65 EXPECT_NE(OK, p.readString16(&output));
66 EXPECT_EQ(output.size(), 0);
67}
68
Jooyung Hand23f9502021-12-23 14:39:57 +090069TEST(Parcel, EnforceNoDataAvail) {
70 const int32_t kTestInt = 42;
71 const String8 kTestString = String8("test-is-good");
72 Parcel p;
73 p.writeInt32(kTestInt);
74 p.writeString8(kTestString);
75 p.setDataPosition(0);
76 EXPECT_EQ(kTestInt, p.readInt32());
77 EXPECT_EQ(p.enforceNoDataAvail().exceptionCode(), Status::Exception::EX_BAD_PARCELABLE);
78 EXPECT_EQ(kTestString, p.readString8());
79 EXPECT_EQ(p.enforceNoDataAvail().exceptionCode(), Status::Exception::EX_NONE);
80}
81
Steven Moreland4b2f18d2022-03-24 00:32:25 +000082TEST(Parcel, DebugReadAllBinders) {
83 sp<IBinder> binder1 = sp<BBinder>::make();
84 sp<IBinder> binder2 = sp<BBinder>::make();
85
86 Parcel p;
87 p.writeInt32(4);
88 p.writeStrongBinder(binder1);
89 p.writeStrongBinder(nullptr);
90 p.writeInt32(4);
91 p.writeStrongBinder(binder2);
92 p.writeInt32(4);
93
94 auto ret = p.debugReadAllStrongBinders();
95
96 ASSERT_EQ(ret.size(), 2);
97 EXPECT_EQ(ret[0], binder1);
98 EXPECT_EQ(ret[1], binder2);
99}
100
101TEST(Parcel, DebugReadAllFds) {
102 Parcel p;
103 p.writeInt32(4);
104 p.writeFileDescriptor(STDOUT_FILENO, false /*takeOwnership*/);
105 p.writeInt32(4);
106 p.writeFileDescriptor(STDIN_FILENO, false /*takeOwnership*/);
107 p.writeInt32(4);
108
109 auto ret = p.debugReadAllFileDescriptors();
110
111 ASSERT_EQ(ret.size(), 2);
112 EXPECT_EQ(ret[0], STDOUT_FILENO);
113 EXPECT_EQ(ret[1], STDIN_FILENO);
114}
115
Steven Morelanda2f835d2023-06-15 16:50:05 +0000116TEST(Parcel, AppendFromEmpty) {
117 Parcel p1;
118 Parcel p2;
119 p2.writeInt32(2);
120
121 ASSERT_EQ(OK, p1.appendFrom(&p2, 0, p2.dataSize()));
122
123 p1.setDataPosition(0);
124 ASSERT_EQ(2, p1.readInt32());
125
126 p2.setDataPosition(0);
127 ASSERT_EQ(2, p2.readInt32());
128}
129
130TEST(Parcel, AppendPlainData) {
131 Parcel p1;
132 p1.writeInt32(1);
133 Parcel p2;
134 p2.writeInt32(2);
135
136 ASSERT_EQ(OK, p1.appendFrom(&p2, 0, p2.dataSize()));
137
138 p1.setDataPosition(0);
139 ASSERT_EQ(1, p1.readInt32());
140 ASSERT_EQ(2, p1.readInt32());
141
142 p2.setDataPosition(0);
143 ASSERT_EQ(2, p2.readInt32());
144}
145
146TEST(Parcel, AppendPlainDataPartial) {
147 Parcel p1;
148 p1.writeInt32(1);
149 Parcel p2;
150 p2.writeInt32(2);
151 p2.writeInt32(3);
152 p2.writeInt32(4);
153
154 // only copy 8 bytes (two int32's worth)
155 ASSERT_EQ(OK, p1.appendFrom(&p2, 0, 8));
156
157 p1.setDataPosition(0);
158 ASSERT_EQ(1, p1.readInt32());
159 ASSERT_EQ(2, p1.readInt32());
160 ASSERT_EQ(3, p1.readInt32());
161 ASSERT_EQ(0, p1.readInt32()); // not 4, end of Parcel
162
163 p2.setDataPosition(0);
164 ASSERT_EQ(2, p2.readInt32());
165}
166
167TEST(Parcel, AppendWithBinder) {
168 sp<IBinder> b1 = sp<BBinder>::make();
169 sp<IBinder> b2 = sp<BBinder>::make();
170
171 Parcel p1;
172 p1.writeInt32(1);
173 p1.writeStrongBinder(b1);
174 Parcel p2;
175 p2.writeInt32(2);
176 p2.writeStrongBinder(b2);
177
178 ASSERT_EQ(OK, p1.appendFrom(&p2, 0, p2.dataSize()));
179
180 p1.setDataPosition(0);
181 ASSERT_EQ(1, p1.readInt32());
182 ASSERT_EQ(b1, p1.readStrongBinder());
183 ASSERT_EQ(2, p1.readInt32());
184 ASSERT_EQ(b2, p1.readStrongBinder());
185 ASSERT_EQ(2, p1.objectsCount());
186
187 p2.setDataPosition(0);
188 ASSERT_EQ(2, p2.readInt32());
189 ASSERT_EQ(b2, p2.readStrongBinder());
190}
191
192TEST(Parcel, AppendWithBinderPartial) {
193 sp<IBinder> b1 = sp<BBinder>::make();
194 sp<IBinder> b2 = sp<BBinder>::make();
195
196 Parcel p1;
197 p1.writeInt32(1);
198 p1.writeStrongBinder(b1);
199 Parcel p2;
200 p2.writeInt32(2);
201 p2.writeStrongBinder(b2);
202
203 ASSERT_EQ(OK, p1.appendFrom(&p2, 0, 8)); // BAD: 4 bytes into strong binder
204
205 p1.setDataPosition(0);
206 ASSERT_EQ(1, p1.readInt32());
207 ASSERT_EQ(b1, p1.readStrongBinder());
208 ASSERT_EQ(2, p1.readInt32());
209 ASSERT_EQ(1935813253, p1.readInt32()); // whatever garbage that is there (ABI)
210 ASSERT_EQ(1, p1.objectsCount());
211
212 p2.setDataPosition(0);
213 ASSERT_EQ(2, p2.readInt32());
214 ASSERT_EQ(b2, p2.readStrongBinder());
215}
216
217TEST(Parcel, AppendWithFd) {
218 unique_fd fd1 = unique_fd(dup(0));
219 unique_fd fd2 = unique_fd(dup(0));
220
221 Parcel p1;
222 p1.writeInt32(1);
223 p1.writeDupFileDescriptor(0); // with ownership
224 p1.writeFileDescriptor(fd1.get()); // without ownership
225 Parcel p2;
226 p2.writeInt32(2);
227 p2.writeDupFileDescriptor(0); // with ownership
228 p2.writeFileDescriptor(fd2.get()); // without ownership
229
230 ASSERT_EQ(OK, p1.appendFrom(&p2, 0, p2.dataSize()));
231
232 p1.setDataPosition(0);
233 ASSERT_EQ(1, p1.readInt32());
234 ASSERT_NE(-1, p1.readFileDescriptor());
235 ASSERT_NE(-1, p1.readFileDescriptor());
236 ASSERT_EQ(2, p1.readInt32());
237 ASSERT_NE(-1, p1.readFileDescriptor());
238 ASSERT_NE(-1, p1.readFileDescriptor());
239 ASSERT_EQ(4, p1.objectsCount());
240
241 p2.setDataPosition(0);
242 ASSERT_EQ(2, p2.readInt32());
243 ASSERT_NE(-1, p1.readFileDescriptor());
244 ASSERT_NE(-1, p1.readFileDescriptor());
245}
246
247TEST(Parcel, AppendWithFdPartial) {
248 unique_fd fd1 = unique_fd(dup(0));
249 unique_fd fd2 = unique_fd(dup(0));
250
251 Parcel p1;
252 p1.writeInt32(1);
253 p1.writeDupFileDescriptor(0); // with ownership
254 p1.writeFileDescriptor(fd1.get()); // without ownership
255 Parcel p2;
256 p2.writeInt32(2);
257 p2.writeDupFileDescriptor(0); // with ownership
258 p2.writeFileDescriptor(fd2.get()); // without ownership
259
260 ASSERT_EQ(OK, p1.appendFrom(&p2, 0, 8)); // BAD: 4 bytes into binder
261
262 p1.setDataPosition(0);
263 ASSERT_EQ(1, p1.readInt32());
264 ASSERT_NE(-1, p1.readFileDescriptor());
265 ASSERT_NE(-1, p1.readFileDescriptor());
266 ASSERT_EQ(2, p1.readInt32());
267 ASSERT_EQ(1717840517, p1.readInt32()); // whatever garbage that is there (ABI)
268 ASSERT_EQ(2, p1.objectsCount());
269
270 p2.setDataPosition(0);
271 ASSERT_EQ(2, p2.readInt32());
272 ASSERT_NE(-1, p1.readFileDescriptor());
273 ASSERT_NE(-1, p1.readFileDescriptor());
274}
275
Steven Morelandcefba612020-11-05 22:57:06 +0000276// Tests a second operation results in a parcel at the same location as it
277// started.
278void parcelOpSameLength(const std::function<void(Parcel*)>& a, const std::function<void(Parcel*)>& b) {
279 Parcel p;
280 a(&p);
281 size_t end = p.dataPosition();
282 p.setDataPosition(0);
283 b(&p);
284 EXPECT_EQ(end, p.dataPosition());
285}
286
287TEST(Parcel, InverseInterfaceToken) {
288 const String16 token = String16("asdf");
289 parcelOpSameLength([&] (Parcel* p) {
290 p->writeInterfaceToken(token);
291 }, [&] (Parcel* p) {
292 EXPECT_TRUE(p->enforceInterface(token, IPCThreadState::self()));
293 });
294}
295
296TEST(Parcel, Utf8FromUtf16Read) {
297 const char* token = "asdf";
298 parcelOpSameLength([&] (Parcel* p) {
299 p->writeString16(String16(token));
300 }, [&] (Parcel* p) {
301 std::string s;
302 EXPECT_EQ(OK, p->readUtf8FromUtf16(&s));
303 EXPECT_EQ(token, s);
304 });
305}
306
307TEST(Parcel, Utf8AsUtf16Write) {
308 std::string token = "asdf";
309 parcelOpSameLength([&] (Parcel* p) {
310 p->writeUtf8AsUtf16(token);
311 }, [&] (Parcel* p) {
312 String16 s;
313 EXPECT_EQ(OK, p->readString16(&s));
314 EXPECT_EQ(s, String16(token.c_str()));
315 });
316}
317
318template <typename T>
319using readFunc = status_t (Parcel::*)(T* out) const;
320template <typename T>
321using writeFunc = status_t (Parcel::*)(const T& in);
322template <typename T>
323using copyWriteFunc = status_t (Parcel::*)(T in);
324
325template <typename T, typename WRITE_FUNC>
326void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, WRITE_FUNC w) {
327 for (const T& value : ts) {
328 parcelOpSameLength([&] (Parcel* p) {
329 (*p.*w)(value);
330 }, [&] (Parcel* p) {
331 T outValue;
332 EXPECT_EQ(OK, (*p.*r)(&outValue));
333 EXPECT_EQ(value, outValue);
334 });
335 }
336}
337
338template <typename T>
339void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, writeFunc<T> w) {
340 readWriteInverse<T, writeFunc<T>>(std::move(ts), r, w);
341}
342template <typename T>
343void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, copyWriteFunc<T> w) {
344 readWriteInverse<T, copyWriteFunc<T>>(std::move(ts), r, w);
345}
346
347#define TEST_READ_WRITE_INVERSE(type, name, ...) \
348 TEST(Parcel, Inverse##name) { \
349 readWriteInverse<type>(__VA_ARGS__, &Parcel::read##name, &Parcel::write##name); \
350 }
351
352TEST_READ_WRITE_INVERSE(int32_t, Int32, {-2, -1, 0, 1, 2});
353TEST_READ_WRITE_INVERSE(uint32_t, Uint32, {0, 1, 2});
354TEST_READ_WRITE_INVERSE(int64_t, Int64, {-2, -1, 0, 1, 2});
355TEST_READ_WRITE_INVERSE(uint64_t, Uint64, {0, 1, 2});
356TEST_READ_WRITE_INVERSE(float, Float, {-1.0f, 0.0f, 3.14f});
357TEST_READ_WRITE_INVERSE(double, Double, {-1.0, 0.0, 3.14});
358TEST_READ_WRITE_INVERSE(bool, Bool, {true, false});
359TEST_READ_WRITE_INVERSE(char16_t, Char, {u'a', u'\0'});
360TEST_READ_WRITE_INVERSE(int8_t, Byte, {-1, 0, 1});
361TEST_READ_WRITE_INVERSE(String8, String8, {String8(), String8("a"), String8("asdf")});
362TEST_READ_WRITE_INVERSE(String16, String16, {String16(), String16("a"), String16("asdf")});
Steven Morelandf80809b2021-10-07 18:09:20 -0700363
364TEST(Parcel, GetOpenAshmemSize) {
365 constexpr size_t kSize = 1024;
366 constexpr size_t kCount = 3;
367
368 Parcel p;
369
370 for (size_t i = 0; i < kCount; i++) {
371 int fd = ashmem_create_region("test-getOpenAshmemSize", kSize);
372 ASSERT_GE(fd, 0);
373 ASSERT_EQ(OK, p.writeFileDescriptor(fd, true /* take ownership */));
374
375 ASSERT_EQ((kSize * (i + 1)), p.getOpenAshmemSize());
376 }
377}