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