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