| Steven Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 1 | /* | 
|  | 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 Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 17 | #include <binder/IPCThreadState.h> | 
| Steven Moreland | 2831860 | 2021-06-25 01:16:29 +0000 | [diff] [blame] | 18 | #include <binder/Parcel.h> | 
| Steven Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 19 | #include <gtest/gtest.h> | 
|  | 20 |  | 
|  | 21 | using android::IPCThreadState; | 
|  | 22 | using android::OK; | 
|  | 23 | using android::Parcel; | 
| Steven Moreland | 2831860 | 2021-06-25 01:16:29 +0000 | [diff] [blame] | 24 | using android::status_t; | 
| Steven Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 25 | using android::String16; | 
|  | 26 | using android::String8; | 
| Steven Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 27 |  | 
|  | 28 | // Tests a second operation results in a parcel at the same location as it | 
|  | 29 | // started. | 
| Steven Moreland | 2831860 | 2021-06-25 01:16:29 +0000 | [diff] [blame] | 30 | void parcelOpSameLength(const std::function<void(Parcel*)>& a, | 
|  | 31 | const std::function<void(Parcel*)>& b) { | 
| Steven Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 32 | 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 |  | 
|  | 40 | TEST(Parcel, InverseInterfaceToken) { | 
|  | 41 | const String16 token = String16("asdf"); | 
| Steven Moreland | 2831860 | 2021-06-25 01:16:29 +0000 | [diff] [blame] | 42 | parcelOpSameLength([&](Parcel* p) { p->writeInterfaceToken(token); }, | 
|  | 43 | [&](Parcel* p) { | 
|  | 44 | EXPECT_TRUE(p->enforceInterface(token, IPCThreadState::self())); | 
|  | 45 | }); | 
| Steven Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 46 | } | 
|  | 47 |  | 
|  | 48 | TEST(Parcel, Utf8FromUtf16Read) { | 
|  | 49 | const char* token = "asdf"; | 
| Steven Moreland | 2831860 | 2021-06-25 01:16:29 +0000 | [diff] [blame] | 50 | 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 Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 56 | } | 
|  | 57 |  | 
|  | 58 | TEST(Parcel, Utf8AsUtf16Write) { | 
|  | 59 | std::string token = "asdf"; | 
| Steven Moreland | 2831860 | 2021-06-25 01:16:29 +0000 | [diff] [blame] | 60 | 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 Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
|  | 68 | template <typename T> | 
|  | 69 | using readFunc = status_t (Parcel::*)(T* out) const; | 
|  | 70 | template <typename T> | 
|  | 71 | using writeFunc = status_t (Parcel::*)(const T& in); | 
|  | 72 | template <typename T> | 
|  | 73 | using copyWriteFunc = status_t (Parcel::*)(T in); | 
|  | 74 |  | 
|  | 75 | template <typename T, typename WRITE_FUNC> | 
|  | 76 | void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, WRITE_FUNC w) { | 
|  | 77 | for (const T& value : ts) { | 
| Steven Moreland | 2831860 | 2021-06-25 01:16:29 +0000 | [diff] [blame] | 78 | 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 Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 84 | } | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | template <typename T> | 
|  | 88 | void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, writeFunc<T> w) { | 
|  | 89 | readWriteInverse<T, writeFunc<T>>(std::move(ts), r, w); | 
|  | 90 | } | 
|  | 91 | template <typename T> | 
|  | 92 | void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, copyWriteFunc<T> w) { | 
|  | 93 | readWriteInverse<T, copyWriteFunc<T>>(std::move(ts), r, w); | 
|  | 94 | } | 
|  | 95 |  | 
| Steven Moreland | 2831860 | 2021-06-25 01:16:29 +0000 | [diff] [blame] | 96 | #define TEST_READ_WRITE_INVERSE(type, name, ...)                                        \ | 
|  | 97 | TEST(Parcel, Inverse##name) {                                                       \ | 
| Steven Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 98 | readWriteInverse<type>(__VA_ARGS__, &Parcel::read##name, &Parcel::write##name); \ | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | TEST_READ_WRITE_INVERSE(int32_t, Int32, {-2, -1, 0, 1, 2}); | 
|  | 102 | TEST_READ_WRITE_INVERSE(uint32_t, Uint32, {0, 1, 2}); | 
|  | 103 | TEST_READ_WRITE_INVERSE(int64_t, Int64, {-2, -1, 0, 1, 2}); | 
|  | 104 | TEST_READ_WRITE_INVERSE(uint64_t, Uint64, {0, 1, 2}); | 
|  | 105 | TEST_READ_WRITE_INVERSE(float, Float, {-1.0f, 0.0f, 3.14f}); | 
|  | 106 | TEST_READ_WRITE_INVERSE(double, Double, {-1.0, 0.0, 3.14}); | 
|  | 107 | TEST_READ_WRITE_INVERSE(bool, Bool, {true, false}); | 
|  | 108 | TEST_READ_WRITE_INVERSE(char16_t, Char, {u'a', u'\0'}); | 
|  | 109 | TEST_READ_WRITE_INVERSE(int8_t, Byte, {-1, 0, 1}); | 
|  | 110 | TEST_READ_WRITE_INVERSE(String8, String8, {String8(), String8("a"), String8("asdf")}); | 
|  | 111 | TEST_READ_WRITE_INVERSE(String16, String16, {String16(), String16("a"), String16("asdf")}); |