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 <VtsHalHidlTargetTestBase.h>
25 #include <VtsHalHidlTargetTestEnvBase.h>
26
27 #include <unordered_set>
28
29 using ::android::hardware::boot::V1_0::IBootControl;
30 using ::android::hardware::boot::V1_0::CommandResult;
31 using ::android::hardware::boot::V1_0::BoolResult;
32 using ::android::hardware::boot::V1_0::Slot;
33 using ::android::hardware::hidl_string;
34 using ::android::hardware::Return;
35 using ::android::sp;
36 using std::string;
37 using std::unordered_set;
38 using std::vector;
39
40 // Test environment for Boot HIDL HAL.
41 class BootHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
42 public:
43 // get the test environment singleton
Instance()44 static BootHidlEnvironment* Instance() {
45 static BootHidlEnvironment* instance = new BootHidlEnvironment;
46 return instance;
47 }
48
registerTestServices()49 virtual void registerTestServices() override { registerTestService<IBootControl>(); }
50 };
51
52 // The main test class for the Boot HIDL HAL.
53 class BootHidlTest : public ::testing::VtsHalHidlTargetTestBase {
54 public:
SetUp()55 virtual void SetUp() override {
56 boot = ::testing::VtsHalHidlTargetTestBase::getService<IBootControl>(
57 BootHidlEnvironment::Instance()->getServiceName<IBootControl>());
58 ASSERT_NE(boot, nullptr);
59 }
60
TearDown()61 virtual void TearDown() override {}
62
63 sp<IBootControl> boot;
64 };
65
generate_callback(CommandResult * dest)66 auto generate_callback(CommandResult *dest) {
67 return [=](CommandResult cr) { *dest = cr; };
68 }
69
70 // Sanity check Boot::getNumberSlots().
TEST_F(BootHidlTest,GetNumberSlots)71 TEST_F(BootHidlTest, GetNumberSlots) {
72 uint32_t slots = boot->getNumberSlots();
73 EXPECT_LE((uint32_t)2, slots);
74 }
75
76 // Sanity check Boot::getCurrentSlot().
TEST_F(BootHidlTest,GetCurrentSlot)77 TEST_F(BootHidlTest, GetCurrentSlot) {
78 Slot curSlot = boot->getCurrentSlot();
79 uint32_t slots = boot->getNumberSlots();
80 EXPECT_LT(curSlot, slots);
81 }
82
83 // Sanity check Boot::markBootSuccessful().
TEST_F(BootHidlTest,MarkBootSuccessful)84 TEST_F(BootHidlTest, MarkBootSuccessful) {
85 CommandResult cr;
86 Return<void> result = boot->markBootSuccessful(generate_callback(&cr));
87 ASSERT_TRUE(result.isOk());
88 if (cr.success) {
89 Slot curSlot = boot->getCurrentSlot();
90 BoolResult ret = boot->isSlotMarkedSuccessful(curSlot);
91 EXPECT_EQ(BoolResult::TRUE, ret);
92 }
93 }
94
95 // Sanity check Boot::setActiveBootSlot() on good and bad inputs.
TEST_F(BootHidlTest,SetActiveBootSlot)96 TEST_F(BootHidlTest, SetActiveBootSlot) {
97 for (Slot s = 0; s < 2; s++) {
98 CommandResult cr;
99 Return<void> result = boot->setActiveBootSlot(s, generate_callback(&cr));
100 EXPECT_TRUE(result.isOk());
101 }
102 {
103 // Restore original flags to avoid problems on reboot
104 CommandResult cr;
105 Return<void> result = boot->markBootSuccessful(generate_callback(&cr));
106 EXPECT_TRUE(result.isOk());
107 EXPECT_TRUE(cr.success);
108 }
109 {
110 CommandResult cr;
111 uint32_t slots = boot->getNumberSlots();
112 Return<void> result =
113 boot->setActiveBootSlot(slots, generate_callback(&cr));
114 ASSERT_TRUE(result.isOk());
115 EXPECT_EQ(false, cr.success);
116 }
117 }
118
119 // Sanity check Boot::setSlotAsUnbootable() on good and bad inputs.
TEST_F(BootHidlTest,SetSlotAsUnbootable)120 TEST_F(BootHidlTest, SetSlotAsUnbootable) {
121 {
122 CommandResult cr;
123 Slot curSlot = boot->getCurrentSlot();
124 Slot otherSlot = curSlot ? 0 : 1;
125 Return<void> result =
126 boot->setSlotAsUnbootable(otherSlot, generate_callback(&cr));
127 EXPECT_TRUE(result.isOk());
128 if (cr.success) {
129 EXPECT_EQ(BoolResult::FALSE, boot->isSlotBootable(otherSlot));
130
131 // Restore original flags to avoid problems on reboot
132 result = boot->setActiveBootSlot(otherSlot, generate_callback(&cr));
133 EXPECT_TRUE(result.isOk());
134 EXPECT_TRUE(cr.success);
135 result = boot->setActiveBootSlot(curSlot, generate_callback(&cr));
136 EXPECT_TRUE(result.isOk());
137 EXPECT_TRUE(cr.success);
138 result = boot->markBootSuccessful(generate_callback(&cr));
139 EXPECT_TRUE(result.isOk());
140 EXPECT_TRUE(cr.success);
141 }
142 }
143 {
144 CommandResult cr;
145 uint32_t slots = boot->getNumberSlots();
146 Return<void> result =
147 boot->setSlotAsUnbootable(slots, generate_callback(&cr));
148 EXPECT_TRUE(result.isOk());
149 EXPECT_EQ(false, cr.success);
150 }
151 }
152
153 // Sanity check Boot::isSlotBootable() on good and bad inputs.
TEST_F(BootHidlTest,IsSlotBootable)154 TEST_F(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));
160 }
161
162 // Sanity check Boot::isSlotMarkedSuccessful() on good and bad inputs.
TEST_F(BootHidlTest,IsSlotMarkedSuccessful)163 TEST_F(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));
169 }
170
171 // Sanity check Boot::getSuffix() on good and bad inputs.
TEST_F(BootHidlTest,GetSuffix)172 TEST_F(BootHidlTest, GetSuffix) {
173 string suffixStr;
174 unordered_set<string> suffixes;
175 auto cb = [&](hidl_string suffix) { suffixStr = suffix.c_str(); };
176 for (Slot i = 0; i < boot->getNumberSlots(); i++) {
177 CommandResult cr;
178 Return<void> result = boot->getSuffix(i, cb);
179 EXPECT_TRUE(result.isOk());
180 ASSERT_EQ('_', suffixStr[0]);
181 ASSERT_LE((unsigned)2, suffixStr.size());
182 suffixes.insert(suffixStr);
183 }
184 // All suffixes should be unique
185 ASSERT_EQ(boot->getNumberSlots(), suffixes.size());
186 {
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 }
192 }
193
main(int argc,char ** argv)194 int main(int argc, char **argv) {
195 ::testing::AddGlobalTestEnvironment(BootHidlEnvironment::Instance());
196 ::testing::InitGoogleTest(&argc, argv);
197 BootHidlEnvironment::Instance()->init(&argc, argv);
198 int status = RUN_ALL_TESTS();
199 LOG(INFO) << "Test result = " << status;
200 return status;
201 }
202