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