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