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> |
Jooyung Han | d23f950 | 2021-12-23 14:39:57 +0900 | [diff] [blame] | 19 | #include <binder/Status.h> |
Steven Moreland | f80809b | 2021-10-07 18:09:20 -0700 | [diff] [blame] | 20 | #include <cutils/ashmem.h> |
Steven Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 21 | #include <gtest/gtest.h> |
| 22 | |
Steven Moreland | 4b2f18d | 2022-03-24 00:32:25 +0000 | [diff] [blame^] | 23 | using android::BBinder; |
| 24 | using android::IBinder; |
Steven Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 25 | using android::IPCThreadState; |
| 26 | using android::OK; |
| 27 | using android::Parcel; |
Steven Moreland | 4b2f18d | 2022-03-24 00:32:25 +0000 | [diff] [blame^] | 28 | using android::sp; |
Jooyung Han | d23f950 | 2021-12-23 14:39:57 +0900 | [diff] [blame] | 29 | using android::status_t; |
Steven Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 30 | using android::String16; |
| 31 | using android::String8; |
Jooyung Han | d23f950 | 2021-12-23 14:39:57 +0900 | [diff] [blame] | 32 | using android::binder::Status; |
Steven Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 33 | |
Steven Moreland | 8f39fa0 | 2020-11-18 00:35:25 +0000 | [diff] [blame] | 34 | TEST(Parcel, NonNullTerminatedString8) { |
| 35 | String8 kTestString = String8("test-is-good"); |
| 36 | |
| 37 | // write non-null terminated string |
| 38 | Parcel p; |
| 39 | p.writeString8(kTestString); |
| 40 | p.setDataPosition(0); |
| 41 | // BAD! assumption of wire format for test |
| 42 | // write over length of string |
| 43 | p.writeInt32(kTestString.size() - 2); |
| 44 | |
| 45 | p.setDataPosition(0); |
| 46 | String8 output; |
| 47 | EXPECT_NE(OK, p.readString8(&output)); |
| 48 | EXPECT_EQ(output.size(), 0); |
| 49 | } |
| 50 | |
| 51 | TEST(Parcel, NonNullTerminatedString16) { |
| 52 | String16 kTestString = String16("test-is-good"); |
| 53 | |
| 54 | // write non-null terminated string |
| 55 | Parcel p; |
| 56 | p.writeString16(kTestString); |
| 57 | p.setDataPosition(0); |
| 58 | // BAD! assumption of wire format for test |
| 59 | // write over length of string |
| 60 | p.writeInt32(kTestString.size() - 2); |
| 61 | |
| 62 | p.setDataPosition(0); |
| 63 | String16 output; |
| 64 | EXPECT_NE(OK, p.readString16(&output)); |
| 65 | EXPECT_EQ(output.size(), 0); |
| 66 | } |
| 67 | |
Jooyung Han | d23f950 | 2021-12-23 14:39:57 +0900 | [diff] [blame] | 68 | TEST(Parcel, EnforceNoDataAvail) { |
| 69 | const int32_t kTestInt = 42; |
| 70 | const String8 kTestString = String8("test-is-good"); |
| 71 | Parcel p; |
| 72 | p.writeInt32(kTestInt); |
| 73 | p.writeString8(kTestString); |
| 74 | p.setDataPosition(0); |
| 75 | EXPECT_EQ(kTestInt, p.readInt32()); |
| 76 | EXPECT_EQ(p.enforceNoDataAvail().exceptionCode(), Status::Exception::EX_BAD_PARCELABLE); |
| 77 | EXPECT_EQ(kTestString, p.readString8()); |
| 78 | EXPECT_EQ(p.enforceNoDataAvail().exceptionCode(), Status::Exception::EX_NONE); |
| 79 | } |
| 80 | |
Steven Moreland | 4b2f18d | 2022-03-24 00:32:25 +0000 | [diff] [blame^] | 81 | TEST(Parcel, DebugReadAllBinders) { |
| 82 | sp<IBinder> binder1 = sp<BBinder>::make(); |
| 83 | sp<IBinder> binder2 = sp<BBinder>::make(); |
| 84 | |
| 85 | Parcel p; |
| 86 | p.writeInt32(4); |
| 87 | p.writeStrongBinder(binder1); |
| 88 | p.writeStrongBinder(nullptr); |
| 89 | p.writeInt32(4); |
| 90 | p.writeStrongBinder(binder2); |
| 91 | p.writeInt32(4); |
| 92 | |
| 93 | auto ret = p.debugReadAllStrongBinders(); |
| 94 | |
| 95 | ASSERT_EQ(ret.size(), 2); |
| 96 | EXPECT_EQ(ret[0], binder1); |
| 97 | EXPECT_EQ(ret[1], binder2); |
| 98 | } |
| 99 | |
| 100 | TEST(Parcel, DebugReadAllFds) { |
| 101 | Parcel p; |
| 102 | p.writeInt32(4); |
| 103 | p.writeFileDescriptor(STDOUT_FILENO, false /*takeOwnership*/); |
| 104 | p.writeInt32(4); |
| 105 | p.writeFileDescriptor(STDIN_FILENO, false /*takeOwnership*/); |
| 106 | p.writeInt32(4); |
| 107 | |
| 108 | auto ret = p.debugReadAllFileDescriptors(); |
| 109 | |
| 110 | ASSERT_EQ(ret.size(), 2); |
| 111 | EXPECT_EQ(ret[0], STDOUT_FILENO); |
| 112 | EXPECT_EQ(ret[1], STDIN_FILENO); |
| 113 | } |
| 114 | |
Steven Moreland | cefba61 | 2020-11-05 22:57:06 +0000 | [diff] [blame] | 115 | // Tests a second operation results in a parcel at the same location as it |
| 116 | // started. |
| 117 | void parcelOpSameLength(const std::function<void(Parcel*)>& a, const std::function<void(Parcel*)>& b) { |
| 118 | Parcel p; |
| 119 | a(&p); |
| 120 | size_t end = p.dataPosition(); |
| 121 | p.setDataPosition(0); |
| 122 | b(&p); |
| 123 | EXPECT_EQ(end, p.dataPosition()); |
| 124 | } |
| 125 | |
| 126 | TEST(Parcel, InverseInterfaceToken) { |
| 127 | const String16 token = String16("asdf"); |
| 128 | parcelOpSameLength([&] (Parcel* p) { |
| 129 | p->writeInterfaceToken(token); |
| 130 | }, [&] (Parcel* p) { |
| 131 | EXPECT_TRUE(p->enforceInterface(token, IPCThreadState::self())); |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | TEST(Parcel, Utf8FromUtf16Read) { |
| 136 | const char* token = "asdf"; |
| 137 | parcelOpSameLength([&] (Parcel* p) { |
| 138 | p->writeString16(String16(token)); |
| 139 | }, [&] (Parcel* p) { |
| 140 | std::string s; |
| 141 | EXPECT_EQ(OK, p->readUtf8FromUtf16(&s)); |
| 142 | EXPECT_EQ(token, s); |
| 143 | }); |
| 144 | } |
| 145 | |
| 146 | TEST(Parcel, Utf8AsUtf16Write) { |
| 147 | std::string token = "asdf"; |
| 148 | parcelOpSameLength([&] (Parcel* p) { |
| 149 | p->writeUtf8AsUtf16(token); |
| 150 | }, [&] (Parcel* p) { |
| 151 | String16 s; |
| 152 | EXPECT_EQ(OK, p->readString16(&s)); |
| 153 | EXPECT_EQ(s, String16(token.c_str())); |
| 154 | }); |
| 155 | } |
| 156 | |
| 157 | template <typename T> |
| 158 | using readFunc = status_t (Parcel::*)(T* out) const; |
| 159 | template <typename T> |
| 160 | using writeFunc = status_t (Parcel::*)(const T& in); |
| 161 | template <typename T> |
| 162 | using copyWriteFunc = status_t (Parcel::*)(T in); |
| 163 | |
| 164 | template <typename T, typename WRITE_FUNC> |
| 165 | void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, WRITE_FUNC w) { |
| 166 | for (const T& value : ts) { |
| 167 | parcelOpSameLength([&] (Parcel* p) { |
| 168 | (*p.*w)(value); |
| 169 | }, [&] (Parcel* p) { |
| 170 | T outValue; |
| 171 | EXPECT_EQ(OK, (*p.*r)(&outValue)); |
| 172 | EXPECT_EQ(value, outValue); |
| 173 | }); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | template <typename T> |
| 178 | void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, writeFunc<T> w) { |
| 179 | readWriteInverse<T, writeFunc<T>>(std::move(ts), r, w); |
| 180 | } |
| 181 | template <typename T> |
| 182 | void readWriteInverse(std::vector<T>&& ts, readFunc<T> r, copyWriteFunc<T> w) { |
| 183 | readWriteInverse<T, copyWriteFunc<T>>(std::move(ts), r, w); |
| 184 | } |
| 185 | |
| 186 | #define TEST_READ_WRITE_INVERSE(type, name, ...) \ |
| 187 | TEST(Parcel, Inverse##name) { \ |
| 188 | readWriteInverse<type>(__VA_ARGS__, &Parcel::read##name, &Parcel::write##name); \ |
| 189 | } |
| 190 | |
| 191 | TEST_READ_WRITE_INVERSE(int32_t, Int32, {-2, -1, 0, 1, 2}); |
| 192 | TEST_READ_WRITE_INVERSE(uint32_t, Uint32, {0, 1, 2}); |
| 193 | TEST_READ_WRITE_INVERSE(int64_t, Int64, {-2, -1, 0, 1, 2}); |
| 194 | TEST_READ_WRITE_INVERSE(uint64_t, Uint64, {0, 1, 2}); |
| 195 | TEST_READ_WRITE_INVERSE(float, Float, {-1.0f, 0.0f, 3.14f}); |
| 196 | TEST_READ_WRITE_INVERSE(double, Double, {-1.0, 0.0, 3.14}); |
| 197 | TEST_READ_WRITE_INVERSE(bool, Bool, {true, false}); |
| 198 | TEST_READ_WRITE_INVERSE(char16_t, Char, {u'a', u'\0'}); |
| 199 | TEST_READ_WRITE_INVERSE(int8_t, Byte, {-1, 0, 1}); |
| 200 | TEST_READ_WRITE_INVERSE(String8, String8, {String8(), String8("a"), String8("asdf")}); |
| 201 | TEST_READ_WRITE_INVERSE(String16, String16, {String16(), String16("a"), String16("asdf")}); |
Steven Moreland | f80809b | 2021-10-07 18:09:20 -0700 | [diff] [blame] | 202 | |
| 203 | TEST(Parcel, GetOpenAshmemSize) { |
| 204 | constexpr size_t kSize = 1024; |
| 205 | constexpr size_t kCount = 3; |
| 206 | |
| 207 | Parcel p; |
| 208 | |
| 209 | for (size_t i = 0; i < kCount; i++) { |
| 210 | int fd = ashmem_create_region("test-getOpenAshmemSize", kSize); |
| 211 | ASSERT_GE(fd, 0); |
| 212 | ASSERT_EQ(OK, p.writeFileDescriptor(fd, true /* take ownership */)); |
| 213 | |
| 214 | ASSERT_EQ((kSize * (i + 1)), p.getOpenAshmemSize()); |
| 215 | } |
| 216 | } |