ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [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 | #include <aidl/Gtest.h> |
| 17 | #include <aidl/Vintf.h> |
| 18 | |
| 19 | #include <aidl/android/hardware/weaver/IWeaver.h> |
| 20 | #include <android/binder_manager.h> |
| 21 | #include <android/binder_process.h> |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 22 | #include <android/hardware/weaver/1.0/IWeaver.h> |
| 23 | #include <hidl/GtestPrinter.h> |
| 24 | #include <hidl/ServiceManagement.h> |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 25 | |
| 26 | #include <limits> |
| 27 | |
| 28 | using ::aidl::android::hardware::weaver::IWeaver; |
| 29 | using ::aidl::android::hardware::weaver::WeaverConfig; |
| 30 | using ::aidl::android::hardware::weaver::WeaverReadResponse; |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 31 | using ::aidl::android::hardware::weaver::WeaverReadStatus; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 32 | |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 33 | using HidlIWeaver = ::android::hardware::weaver::V1_0::IWeaver; |
| 34 | using HidlWeaverConfig = ::android::hardware::weaver::V1_0::WeaverConfig; |
| 35 | using HidlWeaverReadStatus = ::android::hardware::weaver::V1_0::WeaverReadStatus; |
| 36 | using HidlWeaverReadResponse = ::android::hardware::weaver::V1_0::WeaverReadResponse; |
| 37 | using HidlWeaverStatus = ::android::hardware::weaver::V1_0::WeaverStatus; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 38 | |
| 39 | const std::vector<uint8_t> KEY{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; |
| 40 | const std::vector<uint8_t> WRONG_KEY{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 41 | const std::vector<uint8_t> VALUE{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; |
| 42 | const std::vector<uint8_t> OTHER_VALUE{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 255, 255}; |
| 43 | |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 44 | class WeaverAdapter { |
| 45 | public: |
| 46 | virtual ~WeaverAdapter() {} |
| 47 | virtual bool isReady() = 0; |
| 48 | virtual ::ndk::ScopedAStatus getConfig(WeaverConfig* _aidl_return) = 0; |
| 49 | virtual ::ndk::ScopedAStatus read(int32_t in_slotId, const std::vector<uint8_t>& in_key, |
| 50 | WeaverReadResponse* _aidl_return) = 0; |
| 51 | virtual ::ndk::ScopedAStatus write(int32_t in_slotId, const std::vector<uint8_t>& in_key, |
| 52 | const std::vector<uint8_t>& in_value) = 0; |
| 53 | }; |
| 54 | |
| 55 | class WeaverAidlAdapter : public WeaverAdapter { |
| 56 | public: |
| 57 | WeaverAidlAdapter(const std::string& param) |
| 58 | : aidl_weaver_(IWeaver::fromBinder( |
| 59 | ::ndk::SpAIBinder(AServiceManager_waitForService(param.c_str())))) {} |
| 60 | ~WeaverAidlAdapter() {} |
| 61 | |
| 62 | bool isReady() { return aidl_weaver_ != nullptr; } |
| 63 | |
| 64 | ::ndk::ScopedAStatus getConfig(WeaverConfig* _aidl_return) { |
| 65 | return aidl_weaver_->getConfig(_aidl_return); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 66 | } |
| 67 | |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 68 | ::ndk::ScopedAStatus read(int32_t in_slotId, const std::vector<uint8_t>& in_key, |
| 69 | WeaverReadResponse* _aidl_return) { |
| 70 | return aidl_weaver_->read(in_slotId, in_key, _aidl_return); |
| 71 | } |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 72 | |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 73 | ::ndk::ScopedAStatus write(int32_t in_slotId, const std::vector<uint8_t>& in_key, |
| 74 | const std::vector<uint8_t>& in_value) { |
| 75 | return aidl_weaver_->write(in_slotId, in_key, in_value); |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | std::shared_ptr<IWeaver> aidl_weaver_; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 80 | }; |
| 81 | |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 82 | class WeaverHidlAdapter : public WeaverAdapter { |
| 83 | public: |
| 84 | WeaverHidlAdapter(const std::string& param) : hidl_weaver_(HidlIWeaver::getService(param)) {} |
| 85 | ~WeaverHidlAdapter() {} |
| 86 | |
| 87 | bool isReady() { return hidl_weaver_ != nullptr; } |
| 88 | |
| 89 | ::ndk::ScopedAStatus getConfig(WeaverConfig* _aidl_return) { |
| 90 | bool callbackCalled = false; |
| 91 | HidlWeaverStatus status; |
| 92 | HidlWeaverConfig config; |
| 93 | auto ret = hidl_weaver_->getConfig([&](HidlWeaverStatus s, HidlWeaverConfig c) { |
| 94 | callbackCalled = true; |
| 95 | status = s; |
| 96 | config = c; |
| 97 | }); |
| 98 | if (!ret.isOk() || !callbackCalled || status != HidlWeaverStatus::OK) { |
| 99 | return ::ndk::ScopedAStatus::fromStatus(STATUS_FAILED_TRANSACTION); |
| 100 | } |
| 101 | _aidl_return->slots = config.slots; |
| 102 | _aidl_return->keySize = config.keySize; |
| 103 | _aidl_return->valueSize = config.valueSize; |
| 104 | return ::ndk::ScopedAStatus::ok(); |
| 105 | } |
| 106 | |
| 107 | ::ndk::ScopedAStatus read(int32_t in_slotId, const std::vector<uint8_t>& in_key, |
| 108 | WeaverReadResponse* _aidl_return) { |
| 109 | bool callbackCalled = false; |
| 110 | HidlWeaverReadStatus status; |
| 111 | std::vector<uint8_t> value; |
| 112 | uint32_t timeout; |
| 113 | auto ret = hidl_weaver_->read(in_slotId, in_key, |
| 114 | [&](HidlWeaverReadStatus s, HidlWeaverReadResponse r) { |
| 115 | callbackCalled = true; |
| 116 | status = s; |
| 117 | value = r.value; |
| 118 | timeout = r.timeout; |
| 119 | }); |
| 120 | if (!ret.isOk() || !callbackCalled) { |
| 121 | return ::ndk::ScopedAStatus::fromStatus(STATUS_FAILED_TRANSACTION); |
| 122 | } |
| 123 | switch (status) { |
| 124 | case HidlWeaverReadStatus::OK: |
| 125 | _aidl_return->status = WeaverReadStatus::OK; |
| 126 | break; |
| 127 | case HidlWeaverReadStatus::FAILED: |
| 128 | _aidl_return->status = WeaverReadStatus::FAILED; |
| 129 | break; |
| 130 | case HidlWeaverReadStatus::INCORRECT_KEY: |
| 131 | _aidl_return->status = WeaverReadStatus::INCORRECT_KEY; |
| 132 | break; |
| 133 | case HidlWeaverReadStatus::THROTTLE: |
| 134 | _aidl_return->status = WeaverReadStatus::THROTTLE; |
| 135 | break; |
| 136 | default: |
| 137 | ADD_FAILURE() << "Unknown HIDL read status: " << static_cast<uint32_t>(status); |
| 138 | _aidl_return->status = WeaverReadStatus::FAILED; |
| 139 | break; |
| 140 | } |
| 141 | _aidl_return->value = value; |
| 142 | _aidl_return->timeout = timeout; |
| 143 | return ::ndk::ScopedAStatus::ok(); |
| 144 | } |
| 145 | |
| 146 | ::ndk::ScopedAStatus write(int32_t in_slotId, const std::vector<uint8_t>& in_key, |
| 147 | const std::vector<uint8_t>& in_value) { |
| 148 | auto status = hidl_weaver_->write(in_slotId, in_key, in_value); |
| 149 | switch (status) { |
| 150 | case HidlWeaverStatus::OK: |
| 151 | return ::ndk::ScopedAStatus::ok(); |
| 152 | case HidlWeaverStatus::FAILED: |
| 153 | return ::ndk::ScopedAStatus::fromStatus(STATUS_FAILED_TRANSACTION); |
| 154 | default: |
| 155 | ADD_FAILURE() << "Unknown HIDL write status: " << status.description(); |
| 156 | return ::ndk::ScopedAStatus::fromStatus(STATUS_FAILED_TRANSACTION); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | android::sp<HidlIWeaver> hidl_weaver_; |
| 162 | }; |
| 163 | |
| 164 | class WeaverTest : public ::testing::TestWithParam<std::tuple<std::string, std::string>> { |
| 165 | protected: |
| 166 | void SetUp() override; |
| 167 | void TearDown() override {} |
| 168 | |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 169 | std::unique_ptr<WeaverAdapter> weaver_; |
| 170 | WeaverConfig config_; |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | void WeaverTest::SetUp() { |
| 174 | std::string api, instance_name; |
| 175 | std::tie(api, instance_name) = GetParam(); |
| 176 | if (api == "hidl") { |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 177 | weaver_.reset(new WeaverHidlAdapter(instance_name)); |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 178 | } else if (api == "aidl") { |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 179 | weaver_.reset(new WeaverAidlAdapter(instance_name)); |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 180 | } else { |
| 181 | FAIL() << "Bad test parameterization"; |
| 182 | } |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 183 | ASSERT_TRUE(weaver_->isReady()); |
| 184 | |
| 185 | auto ret = weaver_->getConfig(&config_); |
| 186 | ASSERT_TRUE(ret.isOk()); |
| 187 | ASSERT_GT(config_.slots, 0); |
| 188 | GTEST_LOG_(INFO) << "WeaverConfig: slots=" << config_.slots << ", keySize=" << config_.keySize |
| 189 | << ", valueSize=" << config_.valueSize; |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 190 | } |
| 191 | |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 192 | /* |
| 193 | * Checks config values are suitably large |
| 194 | */ |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 195 | TEST_P(WeaverTest, GetConfig) { |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 196 | EXPECT_GE(config_.slots, 16u); |
| 197 | EXPECT_GE(config_.keySize, 16u); |
| 198 | EXPECT_GE(config_.valueSize, 16u); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Gets the config twice and checks they are the same |
| 203 | */ |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 204 | TEST_P(WeaverTest, GettingConfigMultipleTimesGivesSameResult) { |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 205 | WeaverConfig config2; |
| 206 | |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 207 | auto ret = weaver_->getConfig(&config2); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 208 | ASSERT_TRUE(ret.isOk()); |
| 209 | |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 210 | EXPECT_EQ(config_, config2); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Gets the number of slots from the config and writes a key and value to the last one |
| 215 | */ |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 216 | TEST_P(WeaverTest, WriteToLastSlot) { |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 217 | const uint32_t lastSlot = config_.slots - 1; |
| 218 | const auto writeRet = weaver_->write(lastSlot, KEY, VALUE); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 219 | ASSERT_TRUE(writeRet.isOk()); |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Writes a key and value to a slot |
| 224 | * Reads the slot with the same key and receives the value that was previously written |
| 225 | */ |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 226 | TEST_P(WeaverTest, WriteFollowedByReadGivesTheSameValue) { |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 227 | constexpr uint32_t slotId = 0; |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 228 | const auto ret = weaver_->write(slotId, KEY, VALUE); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 229 | ASSERT_TRUE(ret.isOk()); |
| 230 | |
| 231 | WeaverReadResponse response; |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 232 | const auto readRet = weaver_->read(slotId, KEY, &response); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 233 | ASSERT_TRUE(readRet.isOk()); |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 234 | EXPECT_EQ(response.value, VALUE); |
| 235 | EXPECT_EQ(response.timeout, 0u); |
| 236 | EXPECT_EQ(response.status, WeaverReadStatus::OK); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Writes a key and value to a slot |
| 241 | * Overwrites the slot with a new key and value |
| 242 | * Reads the slot with the new key and receives the new value |
| 243 | */ |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 244 | TEST_P(WeaverTest, OverwritingSlotUpdatesTheValue) { |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 245 | constexpr uint32_t slotId = 0; |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 246 | const auto initialWriteRet = weaver_->write(slotId, WRONG_KEY, VALUE); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 247 | ASSERT_TRUE(initialWriteRet.isOk()); |
| 248 | |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 249 | const auto overwriteRet = weaver_->write(slotId, KEY, OTHER_VALUE); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 250 | ASSERT_TRUE(overwriteRet.isOk()); |
| 251 | |
| 252 | WeaverReadResponse response; |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 253 | const auto readRet = weaver_->read(slotId, KEY, &response); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 254 | ASSERT_TRUE(readRet.isOk()); |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 255 | EXPECT_EQ(response.value, OTHER_VALUE); |
| 256 | EXPECT_EQ(response.timeout, 0u); |
| 257 | EXPECT_EQ(response.status, WeaverReadStatus::OK); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Writes a key and value to a slot |
| 262 | * Reads the slot with a different key so does not receive the value |
| 263 | */ |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 264 | TEST_P(WeaverTest, WriteFollowedByReadWithWrongKeyDoesNotGiveTheValue) { |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 265 | constexpr uint32_t slotId = 0; |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 266 | const auto writeRet = weaver_->write(slotId, KEY, VALUE); |
| 267 | ASSERT_TRUE(writeRet.isOk()); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 268 | |
| 269 | WeaverReadResponse response; |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 270 | const auto readRet = weaver_->read(slotId, WRONG_KEY, &response); |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 271 | ASSERT_TRUE(readRet.isOk()); |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 272 | EXPECT_TRUE(response.value.empty()); |
| 273 | EXPECT_EQ(response.status, WeaverReadStatus::INCORRECT_KEY); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Writing to an invalid slot fails |
| 278 | */ |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 279 | TEST_P(WeaverTest, WritingToInvalidSlotFails) { |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 280 | if (config_.slots == std::numeric_limits<uint32_t>::max()) { |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 281 | // If there are no invalid slots then pass |
| 282 | return; |
| 283 | } |
| 284 | |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 285 | const auto writeRet = weaver_->write(config_.slots, KEY, VALUE); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 286 | ASSERT_FALSE(writeRet.isOk()); |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Reading from an invalid slot fails rather than incorrect key |
| 291 | */ |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 292 | TEST_P(WeaverTest, ReadingFromInvalidSlotFails) { |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 293 | if (config_.slots == std::numeric_limits<uint32_t>::max()) { |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 294 | // If there are no invalid slots then pass |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | WeaverReadResponse response; |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 299 | const auto readRet = weaver_->read(config_.slots, KEY, &response); |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 300 | ASSERT_TRUE(readRet.isOk()); |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 301 | EXPECT_TRUE(response.value.empty()); |
| 302 | EXPECT_EQ(response.timeout, 0u); |
| 303 | EXPECT_EQ(response.status, WeaverReadStatus::FAILED); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Writing a key that is too large fails |
| 308 | */ |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 309 | TEST_P(WeaverTest, WriteWithTooLargeKeyFails) { |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 310 | std::vector<uint8_t> bigKey(config_.keySize + 1); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 311 | |
| 312 | constexpr uint32_t slotId = 0; |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 313 | const auto writeRet = weaver_->write(slotId, bigKey, VALUE); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 314 | ASSERT_FALSE(writeRet.isOk()); |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Writing a value that is too large fails |
| 319 | */ |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 320 | TEST_P(WeaverTest, WriteWithTooLargeValueFails) { |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 321 | std::vector<uint8_t> bigValue(config_.valueSize + 1); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 322 | |
| 323 | constexpr uint32_t slotId = 0; |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 324 | const auto writeRet = weaver_->write(slotId, KEY, bigValue); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 325 | ASSERT_FALSE(writeRet.isOk()); |
| 326 | } |
| 327 | |
| 328 | /* |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 329 | * Reading with a key that is too large fails |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 330 | */ |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 331 | TEST_P(WeaverTest, ReadWithTooLargeKeyFails) { |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 332 | std::vector<uint8_t> bigKey(config_.keySize + 1); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 333 | |
| 334 | constexpr uint32_t slotId = 0; |
| 335 | WeaverReadResponse response; |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 336 | const auto readRet = weaver_->read(slotId, bigKey, &response); |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 337 | ASSERT_TRUE(readRet.isOk()); |
Eric Biggers | 961a138 | 2023-07-18 02:34:00 +0000 | [diff] [blame^] | 338 | EXPECT_TRUE(response.value.empty()); |
| 339 | EXPECT_EQ(response.timeout, 0u); |
| 340 | EXPECT_EQ(response.status, WeaverReadStatus::FAILED); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 341 | } |
| 342 | |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 343 | // Instantiate the test for each HIDL Weaver service. |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 344 | INSTANTIATE_TEST_SUITE_P( |
Eric Biggers | b59654f | 2023-07-18 02:33:59 +0000 | [diff] [blame] | 345 | PerHidlInstance, WeaverTest, |
| 346 | testing::Combine(testing::Values("hidl"), |
| 347 | testing::ValuesIn(android::hardware::getAllHalInstanceNames( |
| 348 | HidlIWeaver::descriptor))), |
| 349 | [](const testing::TestParamInfo<std::tuple<std::string, std::string>>& info) { |
| 350 | return android::hardware::PrintInstanceNameToString( |
| 351 | testing::TestParamInfo<std::string>{std::get<1>(info.param), info.index}); |
| 352 | }); |
| 353 | |
| 354 | // Instantiate the test for each AIDL Weaver service. |
| 355 | INSTANTIATE_TEST_SUITE_P( |
| 356 | PerAidlInstance, WeaverTest, |
| 357 | testing::Combine(testing::Values("aidl"), |
| 358 | testing::ValuesIn(android::getAidlHalInstanceNames(IWeaver::descriptor))), |
| 359 | [](const testing::TestParamInfo<std::tuple<std::string, std::string>>& info) { |
| 360 | // This name_generator makes the instance name be included in the test case names, e.g. |
| 361 | // "PerAidlInstance/WeaverTest#GetConfig/0_android_hardware_weaver_IWeaver_default" |
| 362 | // instead of "PerAidlInstance/WeaverTest#GetConfig/0". |
| 363 | return android::PrintInstanceNameToString( |
| 364 | testing::TestParamInfo<std::string>{std::get<1>(info.param), info.index}); |
| 365 | }); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 366 | |
| 367 | int main(int argc, char** argv) { |
| 368 | ::testing::InitGoogleTest(&argc, argv); |
| 369 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 370 | ABinderProcess_startThreadPool(); |
| 371 | return RUN_ALL_TESTS(); |
| 372 | } |