blob: 359c783de510503e4d2d85dd10225232712a4e10 [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;
Steven Morelandcefba612020-11-05 22:57:06 +000033
Steven Moreland8f39fa02020-11-18 00:35:25 +000034TEST(Parcel, NonNullTerminatedString8) {
35 String8 kTestString = String8("test-is-good");
36
37 // write non-null terminated string
38 Parcel p;
39 p.writeString8(kTestString);
40 p.setDataPosition(0);
41 // BAD! assumption of wire format for test
42 // write over length of string
43 p.writeInt32(kTestString.size() - 2);
44
45 p.setDataPosition(0);
46 String8 output;
47 EXPECT_NE(OK, p.readString8(&output));
48 EXPECT_EQ(output.size(), 0);
49}
50
51TEST(Parcel, NonNullTerminatedString16) {
52 String16 kTestString = String16("test-is-good");
53
54 // write non-null terminated string
55 Parcel p;
56 p.writeString16(kTestString);
57 p.setDataPosition(0);
58 // BAD! assumption of wire format for test
59 // write over length of string
60 p.writeInt32(kTestString.size() - 2);
61
62 p.setDataPosition(0);
63 String16 output;
64 EXPECT_NE(OK, p.readString16(&output));
65 EXPECT_EQ(output.size(), 0);
66}
67
Jooyung Hand23f9502021-12-23 14:39:57 +090068TEST(Parcel, EnforceNoDataAvail) {
69 const int32_t kTestInt = 42;
70 const String8 kTestString = String8("test-is-good");
71 Parcel p;
72 p.writeInt32(kTestInt);
73 p.writeString8(kTestString);
74 p.setDataPosition(0);
75 EXPECT_EQ(kTestInt, p.readInt32());
76 EXPECT_EQ(p.enforceNoDataAvail().exceptionCode(), Status::Exception::EX_BAD_PARCELABLE);
77 EXPECT_EQ(kTestString, p.readString8());
78 EXPECT_EQ(p.enforceNoDataAvail().exceptionCode(), Status::Exception::EX_NONE);
79}
80
Steven Moreland4b2f18d2022-03-24 00:32:25 +000081TEST(Parcel, DebugReadAllBinders) {
82 sp<IBinder> binder1 = sp<BBinder>::make();
83 sp<IBinder> binder2 = sp<BBinder>::make();
84
85 Parcel p;
86 p.writeInt32(4);
87 p.writeStrongBinder(binder1);
88 p.writeStrongBinder(nullptr);
89 p.writeInt32(4);
90 p.writeStrongBinder(binder2);
91 p.writeInt32(4);
92
93 auto ret = p.debugReadAllStrongBinders();
94
95 ASSERT_EQ(ret.size(), 2);
96 EXPECT_EQ(ret[0], binder1);
97 EXPECT_EQ(ret[1], binder2);
98}
99
100TEST(Parcel, DebugReadAllFds) {
101 Parcel p;
102 p.writeInt32(4);
103 p.writeFileDescriptor(STDOUT_FILENO, false /*takeOwnership*/);
104 p.writeInt32(4);
105 p.writeFileDescriptor(STDIN_FILENO, false /*takeOwnership*/);
106 p.writeInt32(4);
107
108 auto ret = p.debugReadAllFileDescriptors();
109
110 ASSERT_EQ(ret.size(), 2);
111 EXPECT_EQ(ret[0], STDOUT_FILENO);
112 EXPECT_EQ(ret[1], STDIN_FILENO);
113}
114
Steven Morelandcefba612020-11-05 22:57:06 +0000115// Tests a second operation results in a parcel at the same location as it
116// started.
117void parcelOpSameLength(const std::function<void(Parcel*)>& a, const std::function<void(Parcel*)>& b) {
118 Parcel p;
119 a(&p);
120 size_t end = p.dataPosition();
121 p.setDataPosition(0);
122 b(&p);
123 EXPECT_EQ(end, p.dataPosition());
124}
125
126TEST(Parcel, InverseInterfaceToken) {
127 const String16 token = String16("asdf");
128 parcelOpSameLength([&] (Parcel* p) {
129 p->writeInterfaceToken(token);
130 }, [&] (Parcel* p) {
131 EXPECT_TRUE(p->enforceInterface(token, IPCThreadState::self()));
132 });
133}
134
135TEST(Parcel, Utf8FromUtf16Read) {
136 const char* token = "asdf";
137 parcelOpSameLength([&] (Parcel* p) {
138 p->writeString16(String16(token));
139 }, [&] (Parcel* p) {
140 std::string s;
141 EXPECT_EQ(OK, p->readUtf8FromUtf16(&s));
142 EXPECT_EQ(token, s);
143 });
144}
145
146TEST(Parcel, Utf8AsUtf16Write) {
147 std::string token = "asdf";
148 parcelOpSameLength([&] (Parcel* p) {
149 p->writeUtf8AsUtf16(token);
150 }, [&] (Parcel* p) {
151 String16 s;
152 EXPECT_EQ(OK, p->readString16(&s));
153 EXPECT_EQ(s, String16(token.c_str()));
154 });
155}
156
157template <typename T>
158using readFunc = status_t (Parcel::*)(T* out) const;
159template <typename T>
160using writeFunc = status_t (Parcel::*)(const T& in);
161template <typename T>
162using copyWriteFunc = status_t (Parcel::*)(T in);
163
164template <typename T, typename WRITE_FUNC>
165void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, WRITE_FUNC w) {
166 for (const T& value : ts) {
167 parcelOpSameLength([&] (Parcel* p) {
168 (*p.*w)(value);
169 }, [&] (Parcel* p) {
170 T outValue;
171 EXPECT_EQ(OK, (*p.*r)(&outValue));
172 EXPECT_EQ(value, outValue);
173 });
174 }
175}
176
177template <typename T>
178void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, writeFunc<T> w) {
179 readWriteInverse<T, writeFunc<T>>(std::move(ts), r, w);
180}
181template <typename T>
182void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, copyWriteFunc<T> w) {
183 readWriteInverse<T, copyWriteFunc<T>>(std::move(ts), r, w);
184}
185
186#define TEST_READ_WRITE_INVERSE(type, name, ...) \
187 TEST(Parcel, Inverse##name) { \
188 readWriteInverse<type>(__VA_ARGS__, &Parcel::read##name, &Parcel::write##name); \
189 }
190
191TEST_READ_WRITE_INVERSE(int32_t, Int32, {-2, -1, 0, 1, 2});
192TEST_READ_WRITE_INVERSE(uint32_t, Uint32, {0, 1, 2});
193TEST_READ_WRITE_INVERSE(int64_t, Int64, {-2, -1, 0, 1, 2});
194TEST_READ_WRITE_INVERSE(uint64_t, Uint64, {0, 1, 2});
195TEST_READ_WRITE_INVERSE(float, Float, {-1.0f, 0.0f, 3.14f});
196TEST_READ_WRITE_INVERSE(double, Double, {-1.0, 0.0, 3.14});
197TEST_READ_WRITE_INVERSE(bool, Bool, {true, false});
198TEST_READ_WRITE_INVERSE(char16_t, Char, {u'a', u'\0'});
199TEST_READ_WRITE_INVERSE(int8_t, Byte, {-1, 0, 1});
200TEST_READ_WRITE_INVERSE(String8, String8, {String8(), String8("a"), String8("asdf")});
201TEST_READ_WRITE_INVERSE(String16, String16, {String16(), String16("a"), String16("asdf")});
Steven Morelandf80809b2021-10-07 18:09:20 -0700202
203TEST(Parcel, GetOpenAshmemSize) {
204 constexpr size_t kSize = 1024;
205 constexpr size_t kCount = 3;
206
207 Parcel p;
208
209 for (size_t i = 0; i < kCount; i++) {
210 int fd = ashmem_create_region("test-getOpenAshmemSize", kSize);
211 ASSERT_GE(fd, 0);
212 ASSERT_EQ(OK, p.writeFileDescriptor(fd, true /* take ownership */));
213
214 ASSERT_EQ((kSize * (i + 1)), p.getOpenAshmemSize());
215 }
216}