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 | |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
| 25 | #include <hidl/GtestPrinter.h> |
| 26 | #include <hidl/ServiceManagement.h> |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 27 | |
Connor O'Brien | bb7ce50 | 2018-02-21 16:35:48 -0800 | [diff] [blame] | 28 | #include <unordered_set> |
| 29 | |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 30 | using ::android::hardware::boot::V1_0::IBootControl; |
| 31 | using ::android::hardware::boot::V1_0::CommandResult; |
| 32 | using ::android::hardware::boot::V1_0::BoolResult; |
| 33 | using ::android::hardware::boot::V1_0::Slot; |
| 34 | using ::android::hardware::hidl_string; |
| 35 | using ::android::hardware::Return; |
| 36 | using ::android::sp; |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 37 | using std::string; |
Connor O'Brien | bb7ce50 | 2018-02-21 16:35:48 -0800 | [diff] [blame] | 38 | using std::unordered_set; |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 39 | using std::vector; |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 40 | |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 41 | // The main test class for the Boot HIDL HAL. |
| 42 | class BootHidlTest : public ::testing::TestWithParam<std::string> { |
| 43 | public: |
| 44 | virtual void SetUp() override { |
| 45 | boot = IBootControl::getService(GetParam()); |
| 46 | ASSERT_NE(boot, nullptr); |
Zhuoyao Zhang | 2aba02a | 2017-11-20 17:36:47 -0800 | [diff] [blame] | 47 | } |
| 48 | |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 49 | virtual void TearDown() override {} |
Zhuoyao Zhang | 2aba02a | 2017-11-20 17:36:47 -0800 | [diff] [blame] | 50 | |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 51 | sp<IBootControl> boot; |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | auto generate_callback(CommandResult *dest) { |
| 55 | return [=](CommandResult cr) { *dest = cr; }; |
| 56 | } |
| 57 | |
| 58 | // Sanity check Boot::getNumberSlots(). |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 59 | TEST_P(BootHidlTest, GetNumberSlots) { |
| 60 | uint32_t slots = boot->getNumberSlots(); |
| 61 | EXPECT_LE((uint32_t)2, slots); |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Sanity check Boot::getCurrentSlot(). |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 65 | TEST_P(BootHidlTest, GetCurrentSlot) { |
| 66 | Slot curSlot = boot->getCurrentSlot(); |
| 67 | uint32_t slots = boot->getNumberSlots(); |
| 68 | EXPECT_LT(curSlot, slots); |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Sanity check Boot::markBootSuccessful(). |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 72 | TEST_P(BootHidlTest, MarkBootSuccessful) { |
| 73 | CommandResult cr; |
| 74 | Return<void> result = boot->markBootSuccessful(generate_callback(&cr)); |
| 75 | ASSERT_TRUE(result.isOk()); |
| 76 | if (cr.success) { |
| 77 | Slot curSlot = boot->getCurrentSlot(); |
| 78 | BoolResult ret = boot->isSlotMarkedSuccessful(curSlot); |
| 79 | EXPECT_EQ(BoolResult::TRUE, ret); |
| 80 | } |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // Sanity check Boot::setActiveBootSlot() on good and bad inputs. |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 84 | TEST_P(BootHidlTest, SetActiveBootSlot) { |
| 85 | for (Slot s = 0; s < 2; s++) { |
| 86 | CommandResult cr; |
| 87 | Return<void> result = boot->setActiveBootSlot(s, generate_callback(&cr)); |
| 88 | EXPECT_TRUE(result.isOk()); |
| 89 | } |
| 90 | { |
| 91 | // Restore original flags to avoid problems on reboot |
| 92 | CommandResult cr; |
| 93 | Return<void> result = boot->markBootSuccessful(generate_callback(&cr)); |
| 94 | EXPECT_TRUE(result.isOk()); |
| 95 | EXPECT_TRUE(cr.success); |
| 96 | } |
| 97 | { |
| 98 | CommandResult cr; |
| 99 | uint32_t slots = boot->getNumberSlots(); |
| 100 | Return<void> result = boot->setActiveBootSlot(slots, generate_callback(&cr)); |
| 101 | ASSERT_TRUE(result.isOk()); |
| 102 | EXPECT_EQ(false, cr.success); |
| 103 | } |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Sanity check Boot::setSlotAsUnbootable() on good and bad inputs. |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 107 | TEST_P(BootHidlTest, SetSlotAsUnbootable) { |
| 108 | { |
| 109 | CommandResult cr; |
| 110 | Slot curSlot = boot->getCurrentSlot(); |
| 111 | Slot otherSlot = curSlot ? 0 : 1; |
| 112 | Return<void> result = boot->setSlotAsUnbootable(otherSlot, generate_callback(&cr)); |
| 113 | EXPECT_TRUE(result.isOk()); |
| 114 | if (cr.success) { |
| 115 | EXPECT_EQ(BoolResult::FALSE, boot->isSlotBootable(otherSlot)); |
Connor O'Brien | 86a7ec3 | 2017-01-12 11:30:43 -0800 | [diff] [blame] | 116 | |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 117 | // Restore original flags to avoid problems on reboot |
| 118 | result = boot->setActiveBootSlot(otherSlot, generate_callback(&cr)); |
| 119 | EXPECT_TRUE(result.isOk()); |
| 120 | EXPECT_TRUE(cr.success); |
| 121 | result = boot->setActiveBootSlot(curSlot, generate_callback(&cr)); |
| 122 | EXPECT_TRUE(result.isOk()); |
| 123 | EXPECT_TRUE(cr.success); |
| 124 | result = boot->markBootSuccessful(generate_callback(&cr)); |
| 125 | EXPECT_TRUE(result.isOk()); |
| 126 | EXPECT_TRUE(cr.success); |
| 127 | } |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 128 | } |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 129 | { |
| 130 | CommandResult cr; |
| 131 | uint32_t slots = boot->getNumberSlots(); |
| 132 | Return<void> result = boot->setSlotAsUnbootable(slots, generate_callback(&cr)); |
| 133 | EXPECT_TRUE(result.isOk()); |
| 134 | EXPECT_EQ(false, cr.success); |
| 135 | } |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // Sanity check Boot::isSlotBootable() on good and bad inputs. |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 139 | TEST_P(BootHidlTest, IsSlotBootable) { |
| 140 | for (Slot s = 0; s < 2; s++) { |
| 141 | EXPECT_NE(BoolResult::INVALID_SLOT, boot->isSlotBootable(s)); |
| 142 | } |
| 143 | uint32_t slots = boot->getNumberSlots(); |
| 144 | EXPECT_EQ(BoolResult::INVALID_SLOT, boot->isSlotBootable(slots)); |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Sanity check Boot::isSlotMarkedSuccessful() on good and bad inputs. |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 148 | TEST_P(BootHidlTest, IsSlotMarkedSuccessful) { |
| 149 | for (Slot s = 0; s < 2; s++) { |
| 150 | EXPECT_NE(BoolResult::INVALID_SLOT, boot->isSlotMarkedSuccessful(s)); |
| 151 | } |
| 152 | uint32_t slots = boot->getNumberSlots(); |
| 153 | EXPECT_EQ(BoolResult::INVALID_SLOT, boot->isSlotMarkedSuccessful(slots)); |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Sanity check Boot::getSuffix() on good and bad inputs. |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 157 | TEST_P(BootHidlTest, GetSuffix) { |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 158 | string suffixStr; |
Connor O'Brien | bb7ce50 | 2018-02-21 16:35:48 -0800 | [diff] [blame] | 159 | unordered_set<string> suffixes; |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 160 | auto cb = [&](hidl_string suffix) { suffixStr = suffix.c_str(); }; |
Connor O'Brien | bb7ce50 | 2018-02-21 16:35:48 -0800 | [diff] [blame] | 161 | for (Slot i = 0; i < boot->getNumberSlots(); i++) { |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 162 | CommandResult cr; |
| 163 | Return<void> result = boot->getSuffix(i, cb); |
| 164 | EXPECT_TRUE(result.isOk()); |
Connor O'Brien | bb7ce50 | 2018-02-21 16:35:48 -0800 | [diff] [blame] | 165 | ASSERT_EQ('_', suffixStr[0]); |
| 166 | ASSERT_LE((unsigned)2, suffixStr.size()); |
| 167 | suffixes.insert(suffixStr); |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 168 | } |
Connor O'Brien | bb7ce50 | 2018-02-21 16:35:48 -0800 | [diff] [blame] | 169 | // All suffixes should be unique |
| 170 | ASSERT_EQ(boot->getNumberSlots(), suffixes.size()); |
Connor O'Brien | bb88422 | 2017-04-05 14:41:34 -0700 | [diff] [blame] | 171 | { |
| 172 | string emptySuffix = ""; |
| 173 | Return<void> result = boot->getSuffix(boot->getNumberSlots(), cb); |
| 174 | EXPECT_TRUE(result.isOk()); |
| 175 | ASSERT_EQ(0, suffixStr.compare(emptySuffix)); |
| 176 | } |
Connor O'Brien | 100b491 | 2016-11-30 11:10:59 -0800 | [diff] [blame] | 177 | } |
| 178 | |
nelsonli | e74bc35 | 2019-10-18 15:42:13 +0800 | [diff] [blame] | 179 | INSTANTIATE_TEST_SUITE_P( |
| 180 | PerInstance, BootHidlTest, |
| 181 | testing::ValuesIn(android::hardware::getAllHalInstanceNames(IBootControl::descriptor)), |
| 182 | android::hardware::PrintInstanceNameToString); |