blob: d1d7f73b6ec2cb7ffbe188cb411ab865232d3e71 [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
Yuexi Maed2bb4e2017-03-10 00:44:45 -080024#include <VtsHalHidlTargetTestBase.h>
Zhuoyao Zhang2aba02a2017-11-20 17:36:47 -080025#include <VtsHalHidlTargetTestEnvBase.h>
Connor O'Brien100b4912016-11-30 11:10:59 -080026
27using ::android::hardware::boot::V1_0::IBootControl;
28using ::android::hardware::boot::V1_0::CommandResult;
29using ::android::hardware::boot::V1_0::BoolResult;
30using ::android::hardware::boot::V1_0::Slot;
31using ::android::hardware::hidl_string;
32using ::android::hardware::Return;
33using ::android::sp;
Connor O'Brienbb884222017-04-05 14:41:34 -070034using std::string;
35using std::vector;
Connor O'Brien100b4912016-11-30 11:10:59 -080036
Zhuoyao Zhang2aba02a2017-11-20 17:36:47 -080037// Test environment for Boot HIDL HAL.
38class BootHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
39 public:
40 // get the test environment singleton
41 static BootHidlEnvironment* Instance() {
42 static BootHidlEnvironment* instance = new BootHidlEnvironment;
43 return instance;
44 }
45
46 virtual void registerTestServices() override { registerTestService<IBootControl>(); }
47};
48
Connor O'Brien100b4912016-11-30 11:10:59 -080049// The main test class for the Boot HIDL HAL.
Yuexi Maed2bb4e2017-03-10 00:44:45 -080050class BootHidlTest : public ::testing::VtsHalHidlTargetTestBase {
Connor O'Brien100b4912016-11-30 11:10:59 -080051 public:
52 virtual void SetUp() override {
Zhuoyao Zhang2aba02a2017-11-20 17:36:47 -080053 boot = ::testing::VtsHalHidlTargetTestBase::getService<IBootControl>(
54 BootHidlEnvironment::Instance()->getServiceName<IBootControl>());
55 ASSERT_NE(boot, nullptr);
Connor O'Brien100b4912016-11-30 11:10:59 -080056 }
57
58 virtual void TearDown() override {}
59
60 sp<IBootControl> boot;
61};
62
63auto generate_callback(CommandResult *dest) {
64 return [=](CommandResult cr) { *dest = cr; };
65}
66
67// Sanity check Boot::getNumberSlots().
68TEST_F(BootHidlTest, GetNumberSlots) {
69 uint32_t slots = boot->getNumberSlots();
70 EXPECT_LE((uint32_t)2, slots);
71}
72
73// Sanity check Boot::getCurrentSlot().
74TEST_F(BootHidlTest, GetCurrentSlot) {
75 Slot curSlot = boot->getCurrentSlot();
76 uint32_t slots = boot->getNumberSlots();
77 EXPECT_LT(curSlot, slots);
78}
79
80// Sanity check Boot::markBootSuccessful().
81TEST_F(BootHidlTest, MarkBootSuccessful) {
82 CommandResult cr;
83 Return<void> result = boot->markBootSuccessful(generate_callback(&cr));
Steven Morelandb6438422017-01-03 17:06:57 -080084 ASSERT_TRUE(result.isOk());
Connor O'Brien100b4912016-11-30 11:10:59 -080085 if (cr.success) {
86 Slot curSlot = boot->getCurrentSlot();
87 BoolResult ret = boot->isSlotMarkedSuccessful(curSlot);
88 EXPECT_EQ(BoolResult::TRUE, ret);
89 }
90}
91
92// Sanity check Boot::setActiveBootSlot() on good and bad inputs.
93TEST_F(BootHidlTest, SetActiveBootSlot) {
94 for (Slot s = 0; s < 2; s++) {
95 CommandResult cr;
96 Return<void> result = boot->setActiveBootSlot(s, generate_callback(&cr));
Steven Morelandb6438422017-01-03 17:06:57 -080097 EXPECT_TRUE(result.isOk());
Connor O'Brien100b4912016-11-30 11:10:59 -080098 }
99 {
Connor O'Brien86a7ec32017-01-12 11:30:43 -0800100 // Restore original flags to avoid problems on reboot
101 CommandResult cr;
Connor O'Brienbb884222017-04-05 14:41:34 -0700102 Return<void> result = boot->markBootSuccessful(generate_callback(&cr));
Connor O'Brien86a7ec32017-01-12 11:30:43 -0800103 EXPECT_TRUE(result.isOk());
104 EXPECT_TRUE(cr.success);
105 }
106 {
Connor O'Brien100b4912016-11-30 11:10:59 -0800107 CommandResult cr;
108 uint32_t slots = boot->getNumberSlots();
109 Return<void> result =
110 boot->setActiveBootSlot(slots, generate_callback(&cr));
Steven Morelandb6438422017-01-03 17:06:57 -0800111 ASSERT_TRUE(result.isOk());
Connor O'Brien100b4912016-11-30 11:10:59 -0800112 EXPECT_EQ(false, cr.success);
113 }
114}
115
116// Sanity check Boot::setSlotAsUnbootable() on good and bad inputs.
117TEST_F(BootHidlTest, SetSlotAsUnbootable) {
118 {
119 CommandResult cr;
120 Slot curSlot = boot->getCurrentSlot();
121 Slot otherSlot = curSlot ? 0 : 1;
122 Return<void> result =
123 boot->setSlotAsUnbootable(otherSlot, generate_callback(&cr));
Steven Morelandb6438422017-01-03 17:06:57 -0800124 EXPECT_TRUE(result.isOk());
Connor O'Brien100b4912016-11-30 11:10:59 -0800125 if (cr.success) {
126 EXPECT_EQ(BoolResult::FALSE, boot->isSlotBootable(otherSlot));
Connor O'Brien86a7ec32017-01-12 11:30:43 -0800127
128 // Restore original flags to avoid problems on reboot
129 result = boot->setActiveBootSlot(otherSlot, generate_callback(&cr));
130 EXPECT_TRUE(result.isOk());
131 EXPECT_TRUE(cr.success);
132 result = boot->setActiveBootSlot(curSlot, generate_callback(&cr));
133 EXPECT_TRUE(result.isOk());
134 EXPECT_TRUE(cr.success);
135 result = boot->markBootSuccessful(generate_callback(&cr));
136 EXPECT_TRUE(result.isOk());
Connor O'Brien100b4912016-11-30 11:10:59 -0800137 EXPECT_TRUE(cr.success);
138 }
139 }
140 {
141 CommandResult cr;
142 uint32_t slots = boot->getNumberSlots();
143 Return<void> result =
144 boot->setSlotAsUnbootable(slots, generate_callback(&cr));
Steven Morelandb6438422017-01-03 17:06:57 -0800145 EXPECT_TRUE(result.isOk());
Connor O'Brien100b4912016-11-30 11:10:59 -0800146 EXPECT_EQ(false, cr.success);
147 }
148}
149
150// Sanity check Boot::isSlotBootable() on good and bad inputs.
151TEST_F(BootHidlTest, IsSlotBootable) {
152 for (Slot s = 0; s < 2; s++) {
153 EXPECT_NE(BoolResult::INVALID_SLOT, boot->isSlotBootable(s));
154 }
155 uint32_t slots = boot->getNumberSlots();
156 EXPECT_EQ(BoolResult::INVALID_SLOT, boot->isSlotBootable(slots));
157}
158
159// Sanity check Boot::isSlotMarkedSuccessful() on good and bad inputs.
160TEST_F(BootHidlTest, IsSlotMarkedSuccessful) {
161 for (Slot s = 0; s < 2; s++) {
162 EXPECT_NE(BoolResult::INVALID_SLOT, boot->isSlotMarkedSuccessful(s));
163 }
164 uint32_t slots = boot->getNumberSlots();
165 EXPECT_EQ(BoolResult::INVALID_SLOT, boot->isSlotMarkedSuccessful(slots));
166}
167
168// Sanity check Boot::getSuffix() on good and bad inputs.
169TEST_F(BootHidlTest, GetSuffix) {
Connor O'Brienbb884222017-04-05 14:41:34 -0700170 string suffixStr;
171 vector<string> correctSuffixes = {"_a", "_b"};
172 auto cb = [&](hidl_string suffix) { suffixStr = suffix.c_str(); };
173 for (Slot i = 0; i < 2; i++) {
174 CommandResult cr;
175 Return<void> result = boot->getSuffix(i, cb);
176 EXPECT_TRUE(result.isOk());
177 ASSERT_EQ(0, suffixStr.compare(correctSuffixes[i]));
178 }
179 {
180 string emptySuffix = "";
181 Return<void> result = boot->getSuffix(boot->getNumberSlots(), cb);
182 EXPECT_TRUE(result.isOk());
183 ASSERT_EQ(0, suffixStr.compare(emptySuffix));
184 }
Connor O'Brien100b4912016-11-30 11:10:59 -0800185}
186
187int main(int argc, char **argv) {
Zhuoyao Zhang2aba02a2017-11-20 17:36:47 -0800188 ::testing::AddGlobalTestEnvironment(BootHidlEnvironment::Instance());
189 ::testing::InitGoogleTest(&argc, argv);
190 BootHidlEnvironment::Instance()->init(&argc, argv);
191 int status = RUN_ALL_TESTS();
192 LOG(INFO) << "Test result = " << status;
193 return status;
Connor O'Brien100b4912016-11-30 11:10:59 -0800194}