Andrew Scull | 2f6aae5 | 2017-06-12 16:38:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 <android/hardware/weaver/1.0/IWeaver.h> |
| 18 | |
| 19 | #include <limits> |
| 20 | |
| 21 | #include <VtsHalHidlTargetTestBase.h> |
Zhuoyao Zhang | ff7abc0 | 2018-02-08 21:16:16 -0800 | [diff] [blame] | 22 | #include <VtsHalHidlTargetTestEnvBase.h> |
Andrew Scull | 2f6aae5 | 2017-06-12 16:38:46 +0100 | [diff] [blame] | 23 | |
| 24 | using ::android::hardware::weaver::V1_0::IWeaver; |
| 25 | using ::android::hardware::weaver::V1_0::WeaverConfig; |
| 26 | using ::android::hardware::weaver::V1_0::WeaverReadStatus; |
| 27 | using ::android::hardware::weaver::V1_0::WeaverReadResponse; |
| 28 | using ::android::hardware::weaver::V1_0::WeaverStatus; |
| 29 | using ::android::hardware::Return; |
| 30 | using ::android::sp; |
| 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 | |
Zhuoyao Zhang | ff7abc0 | 2018-02-08 21:16:16 -0800 | [diff] [blame] | 37 | // Test environment for Weaver HIDL HAL. |
| 38 | class WeaverHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase { |
| 39 | public: |
| 40 | // get the test environment singleton |
| 41 | static WeaverHidlEnvironment* Instance() { |
| 42 | static WeaverHidlEnvironment* instance = new WeaverHidlEnvironment; |
| 43 | return instance; |
| 44 | } |
| 45 | |
| 46 | virtual void registerTestServices() override { registerTestService<IWeaver>(); } |
| 47 | }; |
| 48 | |
Andrew Scull | 2f6aae5 | 2017-06-12 16:38:46 +0100 | [diff] [blame] | 49 | struct WeaverHidlTest : public ::testing::VtsHalHidlTargetTestBase { |
| 50 | virtual void SetUp() override { |
Zhuoyao Zhang | ff7abc0 | 2018-02-08 21:16:16 -0800 | [diff] [blame] | 51 | weaver = ::testing::VtsHalHidlTargetTestBase::getService<IWeaver>( |
| 52 | WeaverHidlEnvironment::Instance()->getServiceName<IWeaver>()); |
Andrew Scull | 2f6aae5 | 2017-06-12 16:38:46 +0100 | [diff] [blame] | 53 | ASSERT_NE(weaver, nullptr); |
| 54 | } |
| 55 | |
| 56 | virtual void TearDown() override {} |
| 57 | |
| 58 | sp<IWeaver> weaver; |
| 59 | }; |
| 60 | |
| 61 | /* |
| 62 | * Checks config values are suitably large |
| 63 | */ |
| 64 | TEST_F(WeaverHidlTest, GetConfig) { |
| 65 | WeaverStatus status; |
| 66 | WeaverConfig config; |
| 67 | |
| 68 | bool callbackCalled = false; |
| 69 | auto ret = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) { |
| 70 | callbackCalled = true; |
| 71 | status = s; |
| 72 | config = c; |
| 73 | }); |
| 74 | ASSERT_TRUE(ret.isOk()); |
| 75 | ASSERT_TRUE(callbackCalled); |
| 76 | ASSERT_EQ(status, WeaverStatus::OK); |
| 77 | |
| 78 | EXPECT_GE(config.slots, 16u); |
| 79 | EXPECT_GE(config.keySize, 16u); |
| 80 | EXPECT_GE(config.valueSize, 16u); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Gets the config twice and checks they are the same |
| 85 | */ |
| 86 | TEST_F(WeaverHidlTest, GettingConfigMultipleTimesGivesSameResult) { |
| 87 | WeaverConfig config1; |
| 88 | WeaverConfig config2; |
| 89 | |
| 90 | WeaverStatus status; |
| 91 | bool callbackCalled = false; |
| 92 | auto ret = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) { |
| 93 | callbackCalled = true; |
| 94 | status = s; |
| 95 | config1 = c; |
| 96 | }); |
| 97 | ASSERT_TRUE(ret.isOk()); |
| 98 | ASSERT_TRUE(callbackCalled); |
| 99 | ASSERT_EQ(status, WeaverStatus::OK); |
| 100 | |
| 101 | callbackCalled = false; |
| 102 | ret = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) { |
| 103 | callbackCalled = true; |
| 104 | status = s; |
| 105 | config2 = c; |
| 106 | }); |
| 107 | ASSERT_TRUE(ret.isOk()); |
| 108 | ASSERT_TRUE(callbackCalled); |
| 109 | ASSERT_EQ(status, WeaverStatus::OK); |
| 110 | |
| 111 | EXPECT_EQ(config1, config2); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Gets the number of slots from the config and writes a key and value to the last one |
| 116 | */ |
| 117 | TEST_F(WeaverHidlTest, WriteToLastSlot) { |
| 118 | WeaverStatus status; |
| 119 | WeaverConfig config; |
| 120 | const auto configRet = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) { |
| 121 | status = s; |
| 122 | config = c; |
| 123 | }); |
| 124 | ASSERT_TRUE(configRet.isOk()); |
| 125 | ASSERT_EQ(status, WeaverStatus::OK); |
| 126 | |
| 127 | const uint32_t lastSlot = config.slots - 1; |
| 128 | const auto writeRet = weaver->write(lastSlot, KEY, VALUE); |
| 129 | ASSERT_TRUE(writeRet.isOk()); |
| 130 | ASSERT_EQ(writeRet, WeaverStatus::OK); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Writes a key and value to a slot |
| 135 | * Reads the slot with the same key and receives the value that was previously written |
| 136 | */ |
| 137 | TEST_F(WeaverHidlTest, WriteFollowedByReadGivesTheSameValue) { |
| 138 | constexpr uint32_t slotId = 0; |
| 139 | const auto ret = weaver->write(slotId, KEY, VALUE); |
| 140 | ASSERT_TRUE(ret.isOk()); |
| 141 | ASSERT_EQ(ret, WeaverStatus::OK); |
| 142 | |
| 143 | bool callbackCalled = false; |
| 144 | WeaverReadStatus status; |
| 145 | std::vector<uint8_t> readValue; |
| 146 | uint32_t timeout; |
| 147 | const auto readRet = weaver->read(slotId, KEY, [&](WeaverReadStatus s, WeaverReadResponse r) { |
| 148 | callbackCalled = true; |
| 149 | status = s; |
| 150 | readValue = r.value; |
| 151 | timeout = r.timeout; |
| 152 | }); |
| 153 | ASSERT_TRUE(readRet.isOk()); |
| 154 | ASSERT_TRUE(callbackCalled); |
| 155 | ASSERT_EQ(status, WeaverReadStatus::OK); |
| 156 | EXPECT_EQ(readValue, VALUE); |
| 157 | EXPECT_EQ(timeout, 0u); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Writes a key and value to a slot |
| 162 | * Overwrites the slot with a new key and value |
| 163 | * Reads the slot with the new key and receives the new value |
| 164 | */ |
| 165 | TEST_F(WeaverHidlTest, OverwritingSlotUpdatesTheValue) { |
| 166 | constexpr uint32_t slotId = 0; |
| 167 | const auto initialWriteRet = weaver->write(slotId, WRONG_KEY, VALUE); |
| 168 | ASSERT_TRUE(initialWriteRet.isOk()); |
| 169 | ASSERT_EQ(initialWriteRet, WeaverStatus::OK); |
| 170 | |
| 171 | const auto overwriteRet = weaver->write(slotId, KEY, OTHER_VALUE); |
| 172 | ASSERT_TRUE(overwriteRet.isOk()); |
| 173 | ASSERT_EQ(overwriteRet, WeaverStatus::OK); |
| 174 | |
| 175 | bool callbackCalled = false; |
| 176 | WeaverReadStatus status; |
| 177 | std::vector<uint8_t> readValue; |
| 178 | uint32_t timeout; |
| 179 | const auto readRet = weaver->read(slotId, KEY, [&](WeaverReadStatus s, WeaverReadResponse r) { |
| 180 | callbackCalled = true; |
| 181 | status = s; |
| 182 | readValue = r.value; |
| 183 | timeout = r.timeout; |
| 184 | }); |
| 185 | ASSERT_TRUE(readRet.isOk()); |
| 186 | ASSERT_TRUE(callbackCalled); |
| 187 | ASSERT_EQ(status, WeaverReadStatus::OK); |
| 188 | EXPECT_EQ(readValue, OTHER_VALUE); |
| 189 | EXPECT_EQ(timeout, 0u); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Writes a key and value to a slot |
| 194 | * Reads the slot with a different key so does not receive the value |
| 195 | */ |
| 196 | TEST_F(WeaverHidlTest, WriteFollowedByReadWithWrongKeyDoesNotGiveTheValue) { |
| 197 | constexpr uint32_t slotId = 0; |
| 198 | const auto ret = weaver->write(slotId, KEY, VALUE); |
| 199 | ASSERT_TRUE(ret.isOk()); |
| 200 | ASSERT_EQ(ret, WeaverStatus::OK); |
| 201 | |
| 202 | bool callbackCalled = false; |
| 203 | WeaverReadStatus status; |
| 204 | std::vector<uint8_t> readValue; |
| 205 | const auto readRet = |
| 206 | weaver->read(slotId, WRONG_KEY, [&](WeaverReadStatus s, WeaverReadResponse r) { |
| 207 | callbackCalled = true; |
| 208 | status = s; |
| 209 | readValue = r.value; |
| 210 | }); |
| 211 | ASSERT_TRUE(callbackCalled); |
| 212 | ASSERT_TRUE(readRet.isOk()); |
| 213 | ASSERT_EQ(status, WeaverReadStatus::INCORRECT_KEY); |
| 214 | EXPECT_TRUE(readValue.empty()); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Writing to an invalid slot fails |
| 219 | */ |
| 220 | TEST_F(WeaverHidlTest, WritingToInvalidSlotFails) { |
| 221 | WeaverStatus status; |
| 222 | WeaverConfig config; |
| 223 | const auto configRet = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) { |
| 224 | status = s; |
| 225 | config = c; |
| 226 | }); |
| 227 | ASSERT_TRUE(configRet.isOk()); |
| 228 | ASSERT_EQ(status, WeaverStatus::OK); |
| 229 | |
| 230 | if (config.slots == std::numeric_limits<uint32_t>::max()) { |
| 231 | // If there are no invalid slots then pass |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | const auto writeRet = weaver->write(config.slots, KEY, VALUE); |
| 236 | ASSERT_TRUE(writeRet.isOk()); |
| 237 | ASSERT_EQ(writeRet, WeaverStatus::FAILED); |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Reading from an invalid slot fails rather than incorrect key |
| 242 | */ |
| 243 | TEST_F(WeaverHidlTest, ReadingFromInvalidSlotFails) { |
| 244 | WeaverStatus status; |
| 245 | WeaverConfig config; |
| 246 | const auto configRet = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) { |
| 247 | status = s; |
| 248 | config = c; |
| 249 | }); |
| 250 | ASSERT_TRUE(configRet.isOk()); |
| 251 | ASSERT_EQ(status, WeaverStatus::OK); |
| 252 | |
| 253 | if (config.slots == std::numeric_limits<uint32_t>::max()) { |
| 254 | // If there are no invalid slots then pass |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | bool callbackCalled = false; |
| 259 | WeaverReadStatus readStatus; |
| 260 | std::vector<uint8_t> readValue; |
| 261 | uint32_t timeout; |
| 262 | const auto readRet = |
| 263 | weaver->read(config.slots, KEY, [&](WeaverReadStatus s, WeaverReadResponse r) { |
| 264 | callbackCalled = true; |
| 265 | readStatus = s; |
| 266 | readValue = r.value; |
| 267 | timeout = r.timeout; |
| 268 | }); |
| 269 | ASSERT_TRUE(callbackCalled); |
| 270 | ASSERT_TRUE(readRet.isOk()); |
| 271 | ASSERT_EQ(readStatus, WeaverReadStatus::FAILED); |
| 272 | EXPECT_TRUE(readValue.empty()); |
| 273 | EXPECT_EQ(timeout, 0u); |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Writing a key that is too large fails |
| 278 | */ |
| 279 | TEST_F(WeaverHidlTest, WriteWithTooLargeKeyFails) { |
| 280 | WeaverStatus status; |
| 281 | WeaverConfig config; |
| 282 | const auto configRet = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) { |
| 283 | status = s; |
| 284 | config = c; |
| 285 | }); |
| 286 | ASSERT_TRUE(configRet.isOk()); |
| 287 | ASSERT_EQ(status, WeaverStatus::OK); |
| 288 | |
| 289 | std::vector<uint8_t> bigKey(config.keySize + 1); |
| 290 | |
| 291 | constexpr uint32_t slotId = 0; |
| 292 | const auto writeRet = weaver->write(slotId, bigKey, VALUE); |
| 293 | ASSERT_TRUE(writeRet.isOk()); |
| 294 | ASSERT_EQ(writeRet, WeaverStatus::FAILED); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Writing a value that is too large fails |
| 299 | */ |
| 300 | TEST_F(WeaverHidlTest, WriteWithTooLargeValueFails) { |
| 301 | WeaverStatus status; |
| 302 | WeaverConfig config; |
| 303 | const auto configRet = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) { |
| 304 | status = s; |
| 305 | config = c; |
| 306 | }); |
| 307 | ASSERT_TRUE(configRet.isOk()); |
| 308 | ASSERT_EQ(status, WeaverStatus::OK); |
| 309 | |
| 310 | std::vector<uint8_t> bigValue(config.valueSize + 1); |
| 311 | |
| 312 | constexpr uint32_t slotId = 0; |
| 313 | const auto writeRet = weaver->write(slotId, KEY, bigValue); |
| 314 | ASSERT_TRUE(writeRet.isOk()); |
| 315 | ASSERT_EQ(writeRet, WeaverStatus::FAILED); |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Reading with a key that is loo large fails |
| 320 | */ |
| 321 | TEST_F(WeaverHidlTest, ReadWithTooLargeKeyFails) { |
| 322 | WeaverStatus status; |
| 323 | WeaverConfig config; |
| 324 | const auto configRet = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) { |
| 325 | status = s; |
| 326 | config = c; |
| 327 | }); |
| 328 | ASSERT_TRUE(configRet.isOk()); |
| 329 | ASSERT_EQ(status, WeaverStatus::OK); |
| 330 | |
| 331 | std::vector<uint8_t> bigKey(config.keySize + 1); |
| 332 | |
| 333 | constexpr uint32_t slotId = 0; |
| 334 | bool callbackCalled = false; |
| 335 | WeaverReadStatus readStatus; |
| 336 | std::vector<uint8_t> readValue; |
| 337 | uint32_t timeout; |
| 338 | const auto readRet = |
| 339 | weaver->read(slotId, bigKey, [&](WeaverReadStatus s, WeaverReadResponse r) { |
| 340 | callbackCalled = true; |
| 341 | readStatus = s; |
| 342 | readValue = r.value; |
| 343 | timeout = r.timeout; |
| 344 | }); |
| 345 | ASSERT_TRUE(callbackCalled); |
| 346 | ASSERT_TRUE(readRet.isOk()); |
| 347 | ASSERT_EQ(readStatus, WeaverReadStatus::FAILED); |
| 348 | EXPECT_TRUE(readValue.empty()); |
| 349 | EXPECT_EQ(timeout, 0u); |
| 350 | } |
Zhuoyao Zhang | ff7abc0 | 2018-02-08 21:16:16 -0800 | [diff] [blame] | 351 | |
| 352 | int main(int argc, char** argv) { |
| 353 | ::testing::AddGlobalTestEnvironment(WeaverHidlEnvironment::Instance()); |
| 354 | ::testing::InitGoogleTest(&argc, argv); |
| 355 | WeaverHidlEnvironment::Instance()->init(&argc, argv); |
| 356 | int status = RUN_ALL_TESTS(); |
| 357 | ALOGI("Test result = %d", status); |
| 358 | return status; |
| 359 | } |