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