blob: e4ad1b9bda4a2747a077a93ea8b40e66abf8d0ae [file] [log] [blame]
Connor O'Brien100b4912016-11-30 11:10:59 -08001/*
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
nelsonlie74bc352019-10-18 15:42:13 +080024#include <gtest/gtest.h>
25#include <hidl/GtestPrinter.h>
26#include <hidl/ServiceManagement.h>
Connor O'Brien100b4912016-11-30 11:10:59 -080027
Connor O'Brienbb7ce502018-02-21 16:35:48 -080028#include <unordered_set>
29
Connor O'Brien100b4912016-11-30 11:10:59 -080030using ::android::hardware::boot::V1_0::IBootControl;
31using ::android::hardware::boot::V1_0::CommandResult;
32using ::android::hardware::boot::V1_0::BoolResult;
33using ::android::hardware::boot::V1_0::Slot;
34using ::android::hardware::hidl_string;
35using ::android::hardware::Return;
36using ::android::sp;
Connor O'Brienbb884222017-04-05 14:41:34 -070037using std::string;
Connor O'Brienbb7ce502018-02-21 16:35:48 -080038using std::unordered_set;
Connor O'Brienbb884222017-04-05 14:41:34 -070039using std::vector;
Connor O'Brien100b4912016-11-30 11:10:59 -080040
nelsonlie74bc352019-10-18 15:42:13 +080041// The main test class for the Boot HIDL HAL.
42class BootHidlTest : public ::testing::TestWithParam<std::string> {
43 public:
44 virtual void SetUp() override {
45 boot = IBootControl::getService(GetParam());
46 ASSERT_NE(boot, nullptr);
Zhuoyao Zhang2aba02a2017-11-20 17:36:47 -080047 }
48
nelsonlie74bc352019-10-18 15:42:13 +080049 virtual void TearDown() override {}
Zhuoyao Zhang2aba02a2017-11-20 17:36:47 -080050
nelsonlie74bc352019-10-18 15:42:13 +080051 sp<IBootControl> boot;
Connor O'Brien100b4912016-11-30 11:10:59 -080052};
53
54auto generate_callback(CommandResult *dest) {
55 return [=](CommandResult cr) { *dest = cr; };
56}
57
58// Sanity check Boot::getNumberSlots().
nelsonlie74bc352019-10-18 15:42:13 +080059TEST_P(BootHidlTest, GetNumberSlots) {
60 uint32_t slots = boot->getNumberSlots();
61 EXPECT_LE((uint32_t)2, slots);
Connor O'Brien100b4912016-11-30 11:10:59 -080062}
63
64// Sanity check Boot::getCurrentSlot().
nelsonlie74bc352019-10-18 15:42:13 +080065TEST_P(BootHidlTest, GetCurrentSlot) {
66 Slot curSlot = boot->getCurrentSlot();
67 uint32_t slots = boot->getNumberSlots();
68 EXPECT_LT(curSlot, slots);
Connor O'Brien100b4912016-11-30 11:10:59 -080069}
70
71// Sanity check Boot::markBootSuccessful().
nelsonlie74bc352019-10-18 15:42:13 +080072TEST_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'Brien100b4912016-11-30 11:10:59 -080081}
82
David Andersond028b912020-05-18 17:38:47 +000083TEST_P(BootHidlTest, SetActiveBootSlot) {
84 Slot curSlot = boot->getCurrentSlot();
85 Slot otherSlot = curSlot ? 0 : 1;
86 auto otherBootable = boot->isSlotBootable(otherSlot);
87
nelsonlie74bc352019-10-18 15:42:13 +080088 for (Slot s = 0; s < 2; s++) {
89 CommandResult cr;
90 Return<void> result = boot->setActiveBootSlot(s, generate_callback(&cr));
91 EXPECT_TRUE(result.isOk());
92 }
93 {
94 // Restore original flags to avoid problems on reboot
95 CommandResult cr;
David Andersond028b912020-05-18 17:38:47 +000096 auto result = boot->setActiveBootSlot(curSlot, generate_callback(&cr));
97 EXPECT_TRUE(result.isOk());
98 EXPECT_TRUE(cr.success);
99
100 if (otherBootable == BoolResult::FALSE) {
101 result = boot->setSlotAsUnbootable(otherSlot, generate_callback(&cr));
102 EXPECT_TRUE(result.isOk());
103 EXPECT_TRUE(cr.success);
104 }
105
106 result = boot->markBootSuccessful(generate_callback(&cr));
nelsonlie74bc352019-10-18 15:42:13 +0800107 EXPECT_TRUE(result.isOk());
108 EXPECT_TRUE(cr.success);
109 }
110 {
111 CommandResult cr;
112 uint32_t slots = boot->getNumberSlots();
113 Return<void> result = boot->setActiveBootSlot(slots, generate_callback(&cr));
114 ASSERT_TRUE(result.isOk());
115 EXPECT_EQ(false, cr.success);
116 }
Connor O'Brien100b4912016-11-30 11:10:59 -0800117}
118
David Andersond028b912020-05-18 17:38:47 +0000119TEST_P(BootHidlTest, SetSlotAsUnbootable) {
120 Slot curSlot = boot->getCurrentSlot();
121 Slot otherSlot = curSlot ? 0 : 1;
122 auto otherBootable = boot->isSlotBootable(otherSlot);
nelsonlie74bc352019-10-18 15:42:13 +0800123 {
124 CommandResult cr;
nelsonlie74bc352019-10-18 15:42:13 +0800125 Return<void> result = boot->setSlotAsUnbootable(otherSlot, generate_callback(&cr));
126 EXPECT_TRUE(result.isOk());
127 if (cr.success) {
128 EXPECT_EQ(BoolResult::FALSE, boot->isSlotBootable(otherSlot));
Connor O'Brien86a7ec32017-01-12 11:30:43 -0800129
nelsonlie74bc352019-10-18 15:42:13 +0800130 // Restore original flags to avoid problems on reboot
David Andersond028b912020-05-18 17:38:47 +0000131 if (otherBootable == BoolResult::TRUE) {
132 result = boot->setActiveBootSlot(otherSlot, generate_callback(&cr));
133 EXPECT_TRUE(result.isOk());
134 EXPECT_TRUE(cr.success);
135 }
nelsonlie74bc352019-10-18 15:42:13 +0800136 result = boot->setActiveBootSlot(curSlot, generate_callback(&cr));
137 EXPECT_TRUE(result.isOk());
138 EXPECT_TRUE(cr.success);
139 result = boot->markBootSuccessful(generate_callback(&cr));
140 EXPECT_TRUE(result.isOk());
141 EXPECT_TRUE(cr.success);
142 }
Connor O'Brien100b4912016-11-30 11:10:59 -0800143 }
nelsonlie74bc352019-10-18 15:42:13 +0800144 {
145 CommandResult cr;
146 uint32_t slots = boot->getNumberSlots();
147 Return<void> result = boot->setSlotAsUnbootable(slots, generate_callback(&cr));
148 EXPECT_TRUE(result.isOk());
149 EXPECT_EQ(false, cr.success);
150 }
Connor O'Brien100b4912016-11-30 11:10:59 -0800151}
152
153// Sanity check Boot::isSlotBootable() on good and bad inputs.
nelsonlie74bc352019-10-18 15:42:13 +0800154TEST_P(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));
Connor O'Brien100b4912016-11-30 11:10:59 -0800160}
161
162// Sanity check Boot::isSlotMarkedSuccessful() on good and bad inputs.
nelsonlie74bc352019-10-18 15:42:13 +0800163TEST_P(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));
Connor O'Brien100b4912016-11-30 11:10:59 -0800169}
170
171// Sanity check Boot::getSuffix() on good and bad inputs.
nelsonlie74bc352019-10-18 15:42:13 +0800172TEST_P(BootHidlTest, GetSuffix) {
Connor O'Brienbb884222017-04-05 14:41:34 -0700173 string suffixStr;
Connor O'Brienbb7ce502018-02-21 16:35:48 -0800174 unordered_set<string> suffixes;
Connor O'Brienbb884222017-04-05 14:41:34 -0700175 auto cb = [&](hidl_string suffix) { suffixStr = suffix.c_str(); };
Connor O'Brienbb7ce502018-02-21 16:35:48 -0800176 for (Slot i = 0; i < boot->getNumberSlots(); i++) {
Connor O'Brienbb884222017-04-05 14:41:34 -0700177 CommandResult cr;
178 Return<void> result = boot->getSuffix(i, cb);
179 EXPECT_TRUE(result.isOk());
Connor O'Brienbb7ce502018-02-21 16:35:48 -0800180 ASSERT_EQ('_', suffixStr[0]);
181 ASSERT_LE((unsigned)2, suffixStr.size());
182 suffixes.insert(suffixStr);
Connor O'Brienbb884222017-04-05 14:41:34 -0700183 }
Connor O'Brienbb7ce502018-02-21 16:35:48 -0800184 // All suffixes should be unique
185 ASSERT_EQ(boot->getNumberSlots(), suffixes.size());
Connor O'Brienbb884222017-04-05 14:41:34 -0700186 {
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'Brien100b4912016-11-30 11:10:59 -0800192}
193
Dan Shiba4d5322020-07-28 13:09:30 -0700194GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BootHidlTest);
nelsonlie74bc352019-10-18 15:42:13 +0800195INSTANTIATE_TEST_SUITE_P(
196 PerInstance, BootHidlTest,
197 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IBootControl::descriptor)),
198 android::hardware::PrintInstanceNameToString);