blob: fb49d7a55a32fd6ba2e35c9ad705cd193ad9e134 [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 Moreland28318602021-06-25 01:16:29 +000018#include <binder/Parcel.h>
Steven Morelandcefba612020-11-05 22:57:06 +000019#include <gtest/gtest.h>
20
21using android::IPCThreadState;
22using android::OK;
23using android::Parcel;
Steven Moreland28318602021-06-25 01:16:29 +000024using android::status_t;
Steven Morelandcefba612020-11-05 22:57:06 +000025using android::String16;
26using android::String8;
Steven Morelandcefba612020-11-05 22:57:06 +000027
28// Tests a second operation results in a parcel at the same location as it
29// started.
Steven Moreland28318602021-06-25 01:16:29 +000030void parcelOpSameLength(const std::function<void(Parcel*)>& a,
31 const std::function<void(Parcel*)>& b) {
Steven Morelandcefba612020-11-05 22:57:06 +000032 Parcel p;
33 a(&p);
34 size_t end = p.dataPosition();
35 p.setDataPosition(0);
36 b(&p);
37 EXPECT_EQ(end, p.dataPosition());
38}
39
40TEST(Parcel, InverseInterfaceToken) {
41 const String16 token = String16("asdf");
Steven Moreland28318602021-06-25 01:16:29 +000042 parcelOpSameLength([&](Parcel* p) { p->writeInterfaceToken(token); },
43 [&](Parcel* p) {
44 EXPECT_TRUE(p->enforceInterface(token, IPCThreadState::self()));
45 });
Steven Morelandcefba612020-11-05 22:57:06 +000046}
47
48TEST(Parcel, Utf8FromUtf16Read) {
49 const char* token = "asdf";
Steven Moreland28318602021-06-25 01:16:29 +000050 parcelOpSameLength([&](Parcel* p) { p->writeString16(String16(token)); },
51 [&](Parcel* p) {
52 std::string s;
53 EXPECT_EQ(OK, p->readUtf8FromUtf16(&s));
54 EXPECT_EQ(token, s);
55 });
Steven Morelandcefba612020-11-05 22:57:06 +000056}
57
58TEST(Parcel, Utf8AsUtf16Write) {
59 std::string token = "asdf";
Steven Moreland28318602021-06-25 01:16:29 +000060 parcelOpSameLength([&](Parcel* p) { p->writeUtf8AsUtf16(token); },
61 [&](Parcel* p) {
62 String16 s;
63 EXPECT_EQ(OK, p->readString16(&s));
64 EXPECT_EQ(s, String16(token.c_str()));
65 });
Steven Morelandcefba612020-11-05 22:57:06 +000066}
67
68template <typename T>
69using readFunc = status_t (Parcel::*)(T* out) const;
70template <typename T>
71using writeFunc = status_t (Parcel::*)(const T& in);
72template <typename T>
73using copyWriteFunc = status_t (Parcel::*)(T in);
74
75template <typename T, typename WRITE_FUNC>
76void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, WRITE_FUNC w) {
77 for (const T& value : ts) {
Steven Moreland28318602021-06-25 01:16:29 +000078 parcelOpSameLength([&](Parcel* p) { (*p.*w)(value); },
79 [&](Parcel* p) {
80 T outValue;
81 EXPECT_EQ(OK, (*p.*r)(&outValue));
82 EXPECT_EQ(value, outValue);
83 });
Steven Morelandcefba612020-11-05 22:57:06 +000084 }
85}
86
87template <typename T>
88void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, writeFunc<T> w) {
89 readWriteInverse<T, writeFunc<T>>(std::move(ts), r, w);
90}
91template <typename T>
92void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, copyWriteFunc<T> w) {
93 readWriteInverse<T, copyWriteFunc<T>>(std::move(ts), r, w);
94}
95
Steven Moreland28318602021-06-25 01:16:29 +000096#define TEST_READ_WRITE_INVERSE(type, name, ...) \
97 TEST(Parcel, Inverse##name) { \
Steven Morelandcefba612020-11-05 22:57:06 +000098 readWriteInverse<type>(__VA_ARGS__, &Parcel::read##name, &Parcel::write##name); \
99 }
100
101TEST_READ_WRITE_INVERSE(int32_t, Int32, {-2, -1, 0, 1, 2});
102TEST_READ_WRITE_INVERSE(uint32_t, Uint32, {0, 1, 2});
103TEST_READ_WRITE_INVERSE(int64_t, Int64, {-2, -1, 0, 1, 2});
104TEST_READ_WRITE_INVERSE(uint64_t, Uint64, {0, 1, 2});
105TEST_READ_WRITE_INVERSE(float, Float, {-1.0f, 0.0f, 3.14f});
106TEST_READ_WRITE_INVERSE(double, Double, {-1.0, 0.0, 3.14});
107TEST_READ_WRITE_INVERSE(bool, Bool, {true, false});
108TEST_READ_WRITE_INVERSE(char16_t, Char, {u'a', u'\0'});
109TEST_READ_WRITE_INVERSE(int8_t, Byte, {-1, 0, 1});
110TEST_READ_WRITE_INVERSE(String8, String8, {String8(), String8("a"), String8("asdf")});
111TEST_READ_WRITE_INVERSE(String16, String16, {String16(), String16("a"), String16("asdf")});