blob: 841d47b264677685f98ab395a2f27c968973780e [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
17#include <binder/Parcel.h>
18#include <binder/IPCThreadState.h>
19#include <gtest/gtest.h>
20
21using android::IPCThreadState;
22using android::OK;
23using android::Parcel;
24using android::String16;
25using android::String8;
26using android::status_t;
27
Steven Moreland8f39fa02020-11-18 00:35:25 +000028TEST(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
45TEST(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
Steven Morelandcefba612020-11-05 22:57:06 +000062// Tests a second operation results in a parcel at the same location as it
63// started.
64void 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
73TEST(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
82TEST(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
93TEST(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
104template <typename T>
105using readFunc = status_t (Parcel::*)(T* out) const;
106template <typename T>
107using writeFunc = status_t (Parcel::*)(const T& in);
108template <typename T>
109using copyWriteFunc = status_t (Parcel::*)(T in);
110
111template <typename T, typename WRITE_FUNC>
112void 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
124template <typename T>
125void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, writeFunc<T> w) {
126 readWriteInverse<T, writeFunc<T>>(std::move(ts), r, w);
127}
128template <typename T>
129void 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
138TEST_READ_WRITE_INVERSE(int32_t, Int32, {-2, -1, 0, 1, 2});
139TEST_READ_WRITE_INVERSE(uint32_t, Uint32, {0, 1, 2});
140TEST_READ_WRITE_INVERSE(int64_t, Int64, {-2, -1, 0, 1, 2});
141TEST_READ_WRITE_INVERSE(uint64_t, Uint64, {0, 1, 2});
142TEST_READ_WRITE_INVERSE(float, Float, {-1.0f, 0.0f, 3.14f});
143TEST_READ_WRITE_INVERSE(double, Double, {-1.0, 0.0, 3.14});
144TEST_READ_WRITE_INVERSE(bool, Bool, {true, false});
145TEST_READ_WRITE_INVERSE(char16_t, Char, {u'a', u'\0'});
146TEST_READ_WRITE_INVERSE(int8_t, Byte, {-1, 0, 1});
147TEST_READ_WRITE_INVERSE(String8, String8, {String8(), String8("a"), String8("asdf")});
148TEST_READ_WRITE_INVERSE(String16, String16, {String16(), String16("a"), String16("asdf")});