Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #define LOG_TAG "boot_hidl_hal_test" |
| 18 | #include <android-base/logging.h> |
| 19 | |
| 20 | #include <cutils/properties.h> |
| 21 | |
| 22 | #include <android/hardware/boot/1.0/IBootControl.h> |
| 23 | |
Yuexi Ma | ed2bb4e | 2017-03-10 00:44:45 -0800 | [diff] [blame] | 24 | #include <VtsHalHidlTargetTestBase.h> |
Zhuoyao Zhang | 2aba02a | 2017-11-20 17:36:47 -0800 | [diff] [blame] | 25 | #include <VtsHalHidlTargetTestEnvBase.h> |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 26 | |
Connor O'Brien | bb7ce50 | 2018-02-21 16:35:48 -0800 | [diff] [blame] | 27 | #include <unordered_set> |
| 28 | |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 29 | using ::android::hardware::boot::V1_0::IBootControl; |
| 30 | using ::android::hardware::boot::V1_0::CommandResult; |
| 31 | using ::android::hardware::boot::V1_0::BoolResult; |
| 32 | using ::android::hardware::boot::V1_0::Slot; |
| 33 | using ::android::hardware::hidl_string; |
| 34 | using ::android::hardware::Return; |
| 35 | using ::android::sp; |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 36 | using std::string; |
Connor O'Brien | bb7ce50 | 2018-02-21 16:35:48 -0800 | [diff] [blame] | 37 | using std::unordered_set; |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 38 | using std::vector; |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 39 | |
Zhuoyao Zhang | 2aba02a | 2017-11-20 17:36:47 -0800 | [diff] [blame] | 40 | // Test environment for Boot HIDL HAL. |
| 41 | class BootHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase { |
| 42 | public: |
| 43 | // get the test environment singleton |
| 44 | static BootHidlEnvironment* Instance() { |
| 45 | static BootHidlEnvironment* instance = new BootHidlEnvironment; |
| 46 | return instance; |
| 47 | } |
| 48 | |
| 49 | virtual void registerTestServices() override { registerTestService<IBootControl>(); } |
| 50 | }; |
| 51 | |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 52 | // The main test class for the Boot HIDL HAL. |
Yuexi Ma | ed2bb4e | 2017-03-10 00:44:45 -0800 | [diff] [blame] | 53 | class BootHidlTest : public ::testing::VtsHalHidlTargetTestBase { |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 54 | public: |
| 55 | virtual void SetUp() override { |
Zhuoyao Zhang | 2aba02a | 2017-11-20 17:36:47 -0800 | [diff] [blame] | 56 | boot = ::testing::VtsHalHidlTargetTestBase::getService<IBootControl>( |
| 57 | BootHidlEnvironment::Instance()->getServiceName<IBootControl>()); |
| 58 | ASSERT_NE(boot, nullptr); |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | virtual void TearDown() override {} |
| 62 | |
| 63 | sp<IBootControl> boot; |
| 64 | }; |
| 65 | |
| 66 | auto generate_callback(CommandResult *dest) { |
| 67 | return [=](CommandResult cr) { *dest = cr; }; |
| 68 | } |
| 69 | |
| 70 | // Sanity check Boot::getNumberSlots(). |
| 71 | TEST_F(BootHidlTest, GetNumberSlots) { |
| 72 | uint32_t slots = boot->getNumberSlots(); |
| 73 | EXPECT_LE((uint32_t)2, slots); |
| 74 | } |
| 75 | |
| 76 | // Sanity check Boot::getCurrentSlot(). |
| 77 | TEST_F(BootHidlTest, GetCurrentSlot) { |
| 78 | Slot curSlot = boot->getCurrentSlot(); |
| 79 | uint32_t slots = boot->getNumberSlots(); |
| 80 | EXPECT_LT(curSlot, slots); |
| 81 | } |
| 82 | |
| 83 | // Sanity check Boot::markBootSuccessful(). |
| 84 | TEST_F(BootHidlTest, MarkBootSuccessful) { |
| 85 | CommandResult cr; |
| 86 | Return<void> result = boot->markBootSuccessful(generate_callback(&cr)); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 87 | ASSERT_TRUE(result.isOk()); |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 88 | if (cr.success) { |
| 89 | Slot curSlot = boot->getCurrentSlot(); |
| 90 | BoolResult ret = boot->isSlotMarkedSuccessful(curSlot); |
| 91 | EXPECT_EQ(BoolResult::TRUE, ret); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Sanity check Boot::setActiveBootSlot() on good and bad inputs. |
| 96 | TEST_F(BootHidlTest, SetActiveBootSlot) { |
| 97 | for (Slot s = 0; s < 2; s++) { |
| 98 | CommandResult cr; |
| 99 | Return<void> result = boot->setActiveBootSlot(s, generate_callback(&cr)); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 100 | EXPECT_TRUE(result.isOk()); |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 101 | } |
| 102 | { |
Connor O'Brien | 86a7ec3 | 2017-01-12 11:30:43 -0800 | [diff] [blame] | 103 | // Restore original flags to avoid problems on reboot |
| 104 | CommandResult cr; |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 105 | Return<void> result = boot->markBootSuccessful(generate_callback(&cr)); |
Connor O'Brien | 86a7ec3 | 2017-01-12 11:30:43 -0800 | [diff] [blame] | 106 | EXPECT_TRUE(result.isOk()); |
| 107 | EXPECT_TRUE(cr.success); |
| 108 | } |
| 109 | { |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 110 | CommandResult cr; |
| 111 | uint32_t slots = boot->getNumberSlots(); |
| 112 | Return<void> result = |
| 113 | boot->setActiveBootSlot(slots, generate_callback(&cr)); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 114 | ASSERT_TRUE(result.isOk()); |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 115 | EXPECT_EQ(false, cr.success); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Sanity check Boot::setSlotAsUnbootable() on good and bad inputs. |
| 120 | TEST_F(BootHidlTest, SetSlotAsUnbootable) { |
| 121 | { |
| 122 | CommandResult cr; |
| 123 | Slot curSlot = boot->getCurrentSlot(); |
| 124 | Slot otherSlot = curSlot ? 0 : 1; |
| 125 | Return<void> result = |
| 126 | boot->setSlotAsUnbootable(otherSlot, generate_callback(&cr)); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 127 | EXPECT_TRUE(result.isOk()); |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 128 | if (cr.success) { |
| 129 | EXPECT_EQ(BoolResult::FALSE, boot->isSlotBootable(otherSlot)); |
Connor O'Brien | 86a7ec3 | 2017-01-12 11:30:43 -0800 | [diff] [blame] | 130 | |
| 131 | // Restore original flags to avoid problems on reboot |
| 132 | result = boot->setActiveBootSlot(otherSlot, generate_callback(&cr)); |
| 133 | EXPECT_TRUE(result.isOk()); |
| 134 | EXPECT_TRUE(cr.success); |
| 135 | result = boot->setActiveBootSlot(curSlot, generate_callback(&cr)); |
| 136 | EXPECT_TRUE(result.isOk()); |
| 137 | EXPECT_TRUE(cr.success); |
| 138 | result = boot->markBootSuccessful(generate_callback(&cr)); |
| 139 | EXPECT_TRUE(result.isOk()); |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 140 | EXPECT_TRUE(cr.success); |
| 141 | } |
| 142 | } |
| 143 | { |
| 144 | CommandResult cr; |
| 145 | uint32_t slots = boot->getNumberSlots(); |
| 146 | Return<void> result = |
| 147 | boot->setSlotAsUnbootable(slots, generate_callback(&cr)); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 148 | EXPECT_TRUE(result.isOk()); |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 149 | EXPECT_EQ(false, cr.success); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Sanity check Boot::isSlotBootable() on good and bad inputs. |
| 154 | TEST_F(BootHidlTest, IsSlotBootable) { |
| 155 | for (Slot s = 0; s < 2; s++) { |
| 156 | EXPECT_NE(BoolResult::INVALID_SLOT, boot->isSlotBootable(s)); |
| 157 | } |
| 158 | uint32_t slots = boot->getNumberSlots(); |
| 159 | EXPECT_EQ(BoolResult::INVALID_SLOT, boot->isSlotBootable(slots)); |
| 160 | } |
| 161 | |
| 162 | // Sanity check Boot::isSlotMarkedSuccessful() on good and bad inputs. |
| 163 | TEST_F(BootHidlTest, IsSlotMarkedSuccessful) { |
| 164 | for (Slot s = 0; s < 2; s++) { |
| 165 | EXPECT_NE(BoolResult::INVALID_SLOT, boot->isSlotMarkedSuccessful(s)); |
| 166 | } |
| 167 | uint32_t slots = boot->getNumberSlots(); |
| 168 | EXPECT_EQ(BoolResult::INVALID_SLOT, boot->isSlotMarkedSuccessful(slots)); |
| 169 | } |
| 170 | |
| 171 | // Sanity check Boot::getSuffix() on good and bad inputs. |
| 172 | TEST_F(BootHidlTest, GetSuffix) { |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 173 | string suffixStr; |
Connor O'Brien | bb7ce50 | 2018-02-21 16:35:48 -0800 | [diff] [blame] | 174 | unordered_set<string> suffixes; |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 175 | auto cb = [&](hidl_string suffix) { suffixStr = suffix.c_str(); }; |
Connor O'Brien | bb7ce50 | 2018-02-21 16:35:48 -0800 | [diff] [blame] | 176 | for (Slot i = 0; i < boot->getNumberSlots(); i++) { |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 177 | CommandResult cr; |
| 178 | Return<void> result = boot->getSuffix(i, cb); |
| 179 | EXPECT_TRUE(result.isOk()); |
Connor O'Brien | bb7ce50 | 2018-02-21 16:35:48 -0800 | [diff] [blame] | 180 | ASSERT_EQ('_', suffixStr[0]); |
| 181 | ASSERT_LE((unsigned)2, suffixStr.size()); |
| 182 | suffixes.insert(suffixStr); |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 183 | } |
Connor O'Brien | bb7ce50 | 2018-02-21 16:35:48 -0800 | [diff] [blame] | 184 | // All suffixes should be unique |
| 185 | ASSERT_EQ(boot->getNumberSlots(), suffixes.size()); |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 186 | { |
| 187 | string emptySuffix = ""; |
| 188 | Return<void> result = boot->getSuffix(boot->getNumberSlots(), cb); |
| 189 | EXPECT_TRUE(result.isOk()); |
| 190 | ASSERT_EQ(0, suffixStr.compare(emptySuffix)); |
| 191 | } |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | int main(int argc, char **argv) { |
Zhuoyao Zhang | 2aba02a | 2017-11-20 17:36:47 -0800 | [diff] [blame] | 195 | ::testing::AddGlobalTestEnvironment(BootHidlEnvironment::Instance()); |
| 196 | ::testing::InitGoogleTest(&argc, argv); |
| 197 | BootHidlEnvironment::Instance()->init(&argc, argv); |
| 198 | int status = RUN_ALL_TESTS(); |
| 199 | LOG(INFO) << "Test result = " << status; |
| 200 | return status; |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 201 | } |