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