blob: 17642281c312094076c23f4b1ebd6297fae358da [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
28// Tests a second operation results in a parcel at the same location as it
29// started.
30void parcelOpSameLength(const std::function<void(Parcel*)>& a, const std::function<void(Parcel*)>& b) {
31 Parcel p;
32 a(&p);
33 size_t end = p.dataPosition();
34 p.setDataPosition(0);
35 b(&p);
36 EXPECT_EQ(end, p.dataPosition());
37}
38
39TEST(Parcel, InverseInterfaceToken) {
40 const String16 token = String16("asdf");
41 parcelOpSameLength([&] (Parcel* p) {
42 p->writeInterfaceToken(token);
43 }, [&] (Parcel* p) {
44 EXPECT_TRUE(p->enforceInterface(token, IPCThreadState::self()));
45 });
46}
47
48TEST(Parcel, Utf8FromUtf16Read) {
49 const char* token = "asdf";
50 parcelOpSameLength([&] (Parcel* p) {
51 p->writeString16(String16(token));
52 }, [&] (Parcel* p) {
53 std::string s;
54 EXPECT_EQ(OK, p->readUtf8FromUtf16(&s));
55 EXPECT_EQ(token, s);
56 });
57}
58
59TEST(Parcel, Utf8AsUtf16Write) {
60 std::string token = "asdf";
61 parcelOpSameLength([&] (Parcel* p) {
62 p->writeUtf8AsUtf16(token);
63 }, [&] (Parcel* p) {
64 String16 s;
65 EXPECT_EQ(OK, p->readString16(&s));
66 EXPECT_EQ(s, String16(token.c_str()));
67 });
68}
69
70template <typename T>
71using readFunc = status_t (Parcel::*)(T* out) const;
72template <typename T>
73using writeFunc = status_t (Parcel::*)(const T& in);
74template <typename T>
75using copyWriteFunc = status_t (Parcel::*)(T in);
76
77template <typename T, typename WRITE_FUNC>
78void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, WRITE_FUNC w) {
79 for (const T& value : ts) {
80 parcelOpSameLength([&] (Parcel* p) {
81 (*p.*w)(value);
82 }, [&] (Parcel* p) {
83 T outValue;
84 EXPECT_EQ(OK, (*p.*r)(&outValue));
85 EXPECT_EQ(value, outValue);
86 });
87 }
88}
89
90template <typename T>
91void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, writeFunc<T> w) {
92 readWriteInverse<T, writeFunc<T>>(std::move(ts), r, w);
93}
94template <typename T>
95void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, copyWriteFunc<T> w) {
96 readWriteInverse<T, copyWriteFunc<T>>(std::move(ts), r, w);
97}
98
99#define TEST_READ_WRITE_INVERSE(type, name, ...) \
100 TEST(Parcel, Inverse##name) { \
101 readWriteInverse<type>(__VA_ARGS__, &Parcel::read##name, &Parcel::write##name); \
102 }
103
104TEST_READ_WRITE_INVERSE(int32_t, Int32, {-2, -1, 0, 1, 2});
105TEST_READ_WRITE_INVERSE(uint32_t, Uint32, {0, 1, 2});
106TEST_READ_WRITE_INVERSE(int64_t, Int64, {-2, -1, 0, 1, 2});
107TEST_READ_WRITE_INVERSE(uint64_t, Uint64, {0, 1, 2});
108TEST_READ_WRITE_INVERSE(float, Float, {-1.0f, 0.0f, 3.14f});
109TEST_READ_WRITE_INVERSE(double, Double, {-1.0, 0.0, 3.14});
110TEST_READ_WRITE_INVERSE(bool, Bool, {true, false});
111TEST_READ_WRITE_INVERSE(char16_t, Char, {u'a', u'\0'});
112TEST_READ_WRITE_INVERSE(int8_t, Byte, {-1, 0, 1});
113TEST_READ_WRITE_INVERSE(String8, String8, {String8(), String8("a"), String8("asdf")});
114TEST_READ_WRITE_INVERSE(String16, String16, {String16(), String16("a"), String16("asdf")});