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 | f80809b | 2021-10-07 18:09:20 -0700 | [diff] [blame^] | 18 | #include <binder/Parcel.h> |
| 19 | #include <cutils/ashmem.h> |
Steven Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | using android::IPCThreadState; |
| 23 | using android::OK; |
| 24 | using android::Parcel; |
| 25 | using android::String16; |
| 26 | using android::String8; |
| 27 | using android::status_t; |
| 28 | |
Steven Moreland | 8f39fa0 | 2020-11-18 00:35:25 +0000 | [diff] [blame] | 29 | TEST(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 | |
| 46 | TEST(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 Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 63 | // Tests a second operation results in a parcel at the same location as it |
| 64 | // started. |
| 65 | void 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 | |
| 74 | TEST(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 | |
| 83 | TEST(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 | |
| 94 | TEST(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 | |
| 105 | template <typename T> |
| 106 | using readFunc = status_t (Parcel::*)(T* out) const; |
| 107 | template <typename T> |
| 108 | using writeFunc = status_t (Parcel::*)(const T& in); |
| 109 | template <typename T> |
| 110 | using copyWriteFunc = status_t (Parcel::*)(T in); |
| 111 | |
| 112 | template <typename T, typename WRITE_FUNC> |
| 113 | void 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 | |
| 125 | template <typename T> |
| 126 | void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, writeFunc<T> w) { |
| 127 | readWriteInverse<T, writeFunc<T>>(std::move(ts), r, w); |
| 128 | } |
| 129 | template <typename T> |
| 130 | void 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 | |
| 139 | TEST_READ_WRITE_INVERSE(int32_t, Int32, {-2, -1, 0, 1, 2}); |
| 140 | TEST_READ_WRITE_INVERSE(uint32_t, Uint32, {0, 1, 2}); |
| 141 | TEST_READ_WRITE_INVERSE(int64_t, Int64, {-2, -1, 0, 1, 2}); |
| 142 | TEST_READ_WRITE_INVERSE(uint64_t, Uint64, {0, 1, 2}); |
| 143 | TEST_READ_WRITE_INVERSE(float, Float, {-1.0f, 0.0f, 3.14f}); |
| 144 | TEST_READ_WRITE_INVERSE(double, Double, {-1.0, 0.0, 3.14}); |
| 145 | TEST_READ_WRITE_INVERSE(bool, Bool, {true, false}); |
| 146 | TEST_READ_WRITE_INVERSE(char16_t, Char, {u'a', u'\0'}); |
| 147 | TEST_READ_WRITE_INVERSE(int8_t, Byte, {-1, 0, 1}); |
| 148 | TEST_READ_WRITE_INVERSE(String8, String8, {String8(), String8("a"), String8("asdf")}); |
| 149 | TEST_READ_WRITE_INVERSE(String16, String16, {String16(), String16("a"), String16("asdf")}); |
Steven Moreland | f80809b | 2021-10-07 18:09:20 -0700 | [diff] [blame^] | 150 | |
| 151 | TEST(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 | } |