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> |
| 22 | |
| 23 | #include <limits> |
| 24 | |
| 25 | using ::aidl::android::hardware::weaver::IWeaver; |
| 26 | using ::aidl::android::hardware::weaver::WeaverConfig; |
| 27 | using ::aidl::android::hardware::weaver::WeaverReadResponse; |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 28 | using ::aidl::android::hardware::weaver::WeaverReadStatus; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 29 | |
| 30 | using ::ndk::SpAIBinder; |
| 31 | |
| 32 | const std::vector<uint8_t> KEY{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; |
| 33 | const std::vector<uint8_t> WRONG_KEY{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 34 | const std::vector<uint8_t> VALUE{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; |
| 35 | const std::vector<uint8_t> OTHER_VALUE{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 255, 255}; |
| 36 | |
| 37 | struct WeaverAidlTest : public ::testing::TestWithParam<std::string> { |
| 38 | virtual void SetUp() override { |
| 39 | weaver = IWeaver::fromBinder( |
| 40 | SpAIBinder(AServiceManager_waitForService(GetParam().c_str()))); |
| 41 | ASSERT_NE(weaver, nullptr); |
| 42 | } |
| 43 | |
| 44 | virtual void TearDown() override {} |
| 45 | |
| 46 | std::shared_ptr<IWeaver> weaver; |
| 47 | }; |
| 48 | |
| 49 | /* |
| 50 | * Checks config values are suitably large |
| 51 | */ |
| 52 | TEST_P(WeaverAidlTest, GetConfig) { |
| 53 | WeaverConfig config; |
| 54 | |
| 55 | auto ret = weaver->getConfig(&config); |
| 56 | |
| 57 | ASSERT_TRUE(ret.isOk()); |
| 58 | |
| 59 | EXPECT_GE(config.slots, 16u); |
| 60 | EXPECT_GE(config.keySize, 16u); |
| 61 | EXPECT_GE(config.valueSize, 16u); |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Gets the config twice and checks they are the same |
| 66 | */ |
| 67 | TEST_P(WeaverAidlTest, GettingConfigMultipleTimesGivesSameResult) { |
| 68 | WeaverConfig config1; |
| 69 | WeaverConfig config2; |
| 70 | |
| 71 | auto ret = weaver->getConfig(&config1); |
| 72 | ASSERT_TRUE(ret.isOk()); |
| 73 | |
| 74 | ret = weaver->getConfig(&config2); |
| 75 | ASSERT_TRUE(ret.isOk()); |
| 76 | |
| 77 | EXPECT_EQ(config1, config2); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Gets the number of slots from the config and writes a key and value to the last one |
| 82 | */ |
| 83 | TEST_P(WeaverAidlTest, WriteToLastSlot) { |
| 84 | WeaverConfig config; |
| 85 | const auto configRet = weaver->getConfig(&config); |
| 86 | |
| 87 | ASSERT_TRUE(configRet.isOk()); |
| 88 | |
| 89 | const uint32_t lastSlot = config.slots - 1; |
| 90 | const auto writeRet = weaver->write(lastSlot, KEY, VALUE); |
| 91 | ASSERT_TRUE(writeRet.isOk()); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Writes a key and value to a slot |
| 96 | * Reads the slot with the same key and receives the value that was previously written |
| 97 | */ |
| 98 | TEST_P(WeaverAidlTest, WriteFollowedByReadGivesTheSameValue) { |
| 99 | constexpr uint32_t slotId = 0; |
| 100 | const auto ret = weaver->write(slotId, KEY, VALUE); |
| 101 | ASSERT_TRUE(ret.isOk()); |
| 102 | |
| 103 | WeaverReadResponse response; |
| 104 | std::vector<uint8_t> readValue; |
| 105 | uint32_t timeout; |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 106 | WeaverReadStatus status; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 107 | const auto readRet = weaver->read(slotId, KEY, &response); |
| 108 | |
| 109 | readValue = response.value; |
| 110 | timeout = response.timeout; |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 111 | status = response.status; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 112 | |
| 113 | ASSERT_TRUE(readRet.isOk()); |
| 114 | EXPECT_EQ(readValue, VALUE); |
| 115 | EXPECT_EQ(timeout, 0u); |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 116 | EXPECT_EQ(status, WeaverReadStatus::OK); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Writes a key and value to a slot |
| 121 | * Overwrites the slot with a new key and value |
| 122 | * Reads the slot with the new key and receives the new value |
| 123 | */ |
| 124 | TEST_P(WeaverAidlTest, OverwritingSlotUpdatesTheValue) { |
| 125 | constexpr uint32_t slotId = 0; |
| 126 | const auto initialWriteRet = weaver->write(slotId, WRONG_KEY, VALUE); |
| 127 | ASSERT_TRUE(initialWriteRet.isOk()); |
| 128 | |
| 129 | const auto overwriteRet = weaver->write(slotId, KEY, OTHER_VALUE); |
| 130 | ASSERT_TRUE(overwriteRet.isOk()); |
| 131 | |
| 132 | WeaverReadResponse response; |
| 133 | std::vector<uint8_t> readValue; |
| 134 | uint32_t timeout; |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 135 | WeaverReadStatus status; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 136 | const auto readRet = weaver->read(slotId, KEY, &response); |
| 137 | |
| 138 | readValue = response.value; |
| 139 | timeout = response.timeout; |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 140 | status = response.status; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 141 | |
| 142 | ASSERT_TRUE(readRet.isOk()); |
| 143 | EXPECT_EQ(readValue, OTHER_VALUE); |
| 144 | EXPECT_EQ(timeout, 0u); |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 145 | EXPECT_EQ(status, WeaverReadStatus::OK); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Writes a key and value to a slot |
| 150 | * Reads the slot with a different key so does not receive the value |
| 151 | */ |
| 152 | TEST_P(WeaverAidlTest, WriteFollowedByReadWithWrongKeyDoesNotGiveTheValue) { |
| 153 | constexpr uint32_t slotId = 0; |
| 154 | const auto ret = weaver->write(slotId, KEY, VALUE); |
| 155 | ASSERT_TRUE(ret.isOk()); |
| 156 | |
| 157 | WeaverReadResponse response; |
| 158 | std::vector<uint8_t> readValue; |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 159 | WeaverReadStatus status; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 160 | const auto readRet = |
| 161 | weaver->read(slotId, WRONG_KEY, &response); |
| 162 | |
| 163 | readValue = response.value; |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 164 | status = response.status; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 165 | |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 166 | ASSERT_TRUE(readRet.isOk()); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 167 | EXPECT_TRUE(readValue.empty()); |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 168 | EXPECT_EQ(status, WeaverReadStatus::INCORRECT_KEY); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Writing to an invalid slot fails |
| 173 | */ |
| 174 | TEST_P(WeaverAidlTest, WritingToInvalidSlotFails) { |
| 175 | WeaverConfig config; |
| 176 | const auto configRet = weaver->getConfig(&config); |
| 177 | ASSERT_TRUE(configRet.isOk()); |
| 178 | |
| 179 | if (config.slots == std::numeric_limits<uint32_t>::max()) { |
| 180 | // If there are no invalid slots then pass |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | const auto writeRet = weaver->write(config.slots, KEY, VALUE); |
| 185 | ASSERT_FALSE(writeRet.isOk()); |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Reading from an invalid slot fails rather than incorrect key |
| 190 | */ |
| 191 | TEST_P(WeaverAidlTest, ReadingFromInvalidSlotFails) { |
| 192 | WeaverConfig config; |
| 193 | const auto configRet = weaver->getConfig(&config); |
| 194 | ASSERT_TRUE(configRet.isOk()); |
| 195 | |
| 196 | if (config.slots == std::numeric_limits<uint32_t>::max()) { |
| 197 | // If there are no invalid slots then pass |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | WeaverReadResponse response; |
| 202 | std::vector<uint8_t> readValue; |
| 203 | uint32_t timeout; |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 204 | WeaverReadStatus status; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 205 | const auto readRet = |
| 206 | weaver->read(config.slots, KEY, &response); |
| 207 | |
| 208 | readValue = response.value; |
| 209 | timeout = response.timeout; |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 210 | status = response.status; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 211 | |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 212 | ASSERT_TRUE(readRet.isOk()); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 213 | EXPECT_TRUE(readValue.empty()); |
| 214 | EXPECT_EQ(timeout, 0u); |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 215 | EXPECT_EQ(status, WeaverReadStatus::FAILED); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Writing a key that is too large fails |
| 220 | */ |
| 221 | TEST_P(WeaverAidlTest, WriteWithTooLargeKeyFails) { |
| 222 | WeaverConfig config; |
| 223 | const auto configRet = weaver->getConfig(&config); |
| 224 | ASSERT_TRUE(configRet.isOk()); |
| 225 | |
| 226 | std::vector<uint8_t> bigKey(config.keySize + 1); |
| 227 | |
| 228 | constexpr uint32_t slotId = 0; |
| 229 | const auto writeRet = weaver->write(slotId, bigKey, VALUE); |
| 230 | ASSERT_FALSE(writeRet.isOk()); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Writing a value that is too large fails |
| 235 | */ |
| 236 | TEST_P(WeaverAidlTest, WriteWithTooLargeValueFails) { |
| 237 | WeaverConfig config; |
| 238 | const auto configRet = weaver->getConfig(&config); |
| 239 | ASSERT_TRUE(configRet.isOk()); |
| 240 | |
| 241 | std::vector<uint8_t> bigValue(config.valueSize + 1); |
| 242 | |
| 243 | constexpr uint32_t slotId = 0; |
| 244 | const auto writeRet = weaver->write(slotId, KEY, bigValue); |
| 245 | ASSERT_FALSE(writeRet.isOk()); |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Reading with a key that is loo large fails |
| 250 | */ |
| 251 | TEST_P(WeaverAidlTest, ReadWithTooLargeKeyFails) { |
| 252 | WeaverConfig config; |
| 253 | const auto configRet = weaver->getConfig(&config); |
| 254 | ASSERT_TRUE(configRet.isOk()); |
| 255 | |
| 256 | std::vector<uint8_t> bigKey(config.keySize + 1); |
| 257 | |
| 258 | constexpr uint32_t slotId = 0; |
| 259 | WeaverReadResponse response; |
| 260 | std::vector<uint8_t> readValue; |
| 261 | uint32_t timeout; |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 262 | WeaverReadStatus status; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 263 | const auto readRet = |
| 264 | weaver->read(slotId, bigKey, &response); |
| 265 | |
| 266 | readValue = response.value; |
| 267 | timeout = response.timeout; |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 268 | status = response.status; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 269 | |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 270 | ASSERT_TRUE(readRet.isOk()); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 271 | EXPECT_TRUE(readValue.empty()); |
| 272 | EXPECT_EQ(timeout, 0u); |
ChengYou Ho | 20c47b4 | 2022-11-30 17:51:17 +0000 | [diff] [blame] | 273 | EXPECT_EQ(status, WeaverReadStatus::FAILED); |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WeaverAidlTest); |
| 277 | INSTANTIATE_TEST_SUITE_P( |
| 278 | PerInstance, WeaverAidlTest, |
| 279 | testing::ValuesIn(android::getAidlHalInstanceNames(IWeaver::descriptor)), |
| 280 | android::PrintInstanceNameToString); |
| 281 | |
| 282 | int main(int argc, char** argv) { |
| 283 | ::testing::InitGoogleTest(&argc, argv); |
| 284 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 285 | ABinderProcess_startThreadPool(); |
| 286 | return RUN_ALL_TESTS(); |
| 287 | } |