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