blob: f48a95d85328c9240e7c44982461cc31c8fcdef1 [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>
Connor O'Brien100b4912016-11-30 11:10:59 -080025
26using ::android::hardware::boot::V1_0::IBootControl;
27using ::android::hardware::boot::V1_0::CommandResult;
28using ::android::hardware::boot::V1_0::BoolResult;
29using ::android::hardware::boot::V1_0::Slot;
30using ::android::hardware::hidl_string;
31using ::android::hardware::Return;
32using ::android::sp;
Connor O'Brienbb884222017-04-05 14:41:34 -070033using std::string;
34using std::vector;
Connor O'Brien100b4912016-11-30 11:10:59 -080035
36// The main test class for the Boot HIDL HAL.
Yuexi Maed2bb4e2017-03-10 00:44:45 -080037class BootHidlTest : public ::testing::VtsHalHidlTargetTestBase {
Connor O'Brien100b4912016-11-30 11:10:59 -080038 public:
39 virtual void SetUp() override {
Yuexi Maed2bb4e2017-03-10 00:44:45 -080040 boot = ::testing::VtsHalHidlTargetTestBase::getService<IBootControl>();
Connor O'Brien100b4912016-11-30 11:10:59 -080041 ASSERT_NE(boot, nullptr);
Connor O'Brien100b4912016-11-30 11:10:59 -080042 }
43
44 virtual void TearDown() override {}
45
46 sp<IBootControl> boot;
47};
48
49auto generate_callback(CommandResult *dest) {
50 return [=](CommandResult cr) { *dest = cr; };
51}
52
53// Sanity check Boot::getNumberSlots().
54TEST_F(BootHidlTest, GetNumberSlots) {
55 uint32_t slots = boot->getNumberSlots();
56 EXPECT_LE((uint32_t)2, slots);
57}
58
59// Sanity check Boot::getCurrentSlot().
60TEST_F(BootHidlTest, GetCurrentSlot) {
61 Slot curSlot = boot->getCurrentSlot();
62 uint32_t slots = boot->getNumberSlots();
63 EXPECT_LT(curSlot, slots);
64}
65
66// Sanity check Boot::markBootSuccessful().
67TEST_F(BootHidlTest, MarkBootSuccessful) {
68 CommandResult cr;
69 Return<void> result = boot->markBootSuccessful(generate_callback(&cr));
Steven Morelandb6438422017-01-03 17:06:57 -080070 ASSERT_TRUE(result.isOk());
Connor O'Brien100b4912016-11-30 11:10:59 -080071 if (cr.success) {
72 Slot curSlot = boot->getCurrentSlot();
73 BoolResult ret = boot->isSlotMarkedSuccessful(curSlot);
74 EXPECT_EQ(BoolResult::TRUE, ret);
75 }
76}
77
78// Sanity check Boot::setActiveBootSlot() on good and bad inputs.
79TEST_F(BootHidlTest, SetActiveBootSlot) {
80 for (Slot s = 0; s < 2; s++) {
81 CommandResult cr;
82 Return<void> result = boot->setActiveBootSlot(s, generate_callback(&cr));
Steven Morelandb6438422017-01-03 17:06:57 -080083 EXPECT_TRUE(result.isOk());
Connor O'Brien100b4912016-11-30 11:10:59 -080084 }
85 {
Connor O'Brien86a7ec32017-01-12 11:30:43 -080086 // Restore original flags to avoid problems on reboot
87 CommandResult cr;
Connor O'Brienbb884222017-04-05 14:41:34 -070088 Return<void> result = boot->markBootSuccessful(generate_callback(&cr));
Connor O'Brien86a7ec32017-01-12 11:30:43 -080089 EXPECT_TRUE(result.isOk());
90 EXPECT_TRUE(cr.success);
91 }
92 {
Connor O'Brien100b4912016-11-30 11:10:59 -080093 CommandResult cr;
94 uint32_t slots = boot->getNumberSlots();
95 Return<void> result =
96 boot->setActiveBootSlot(slots, generate_callback(&cr));
Steven Morelandb6438422017-01-03 17:06:57 -080097 ASSERT_TRUE(result.isOk());
Connor O'Brien100b4912016-11-30 11:10:59 -080098 EXPECT_EQ(false, cr.success);
99 }
100}
101
102// Sanity check Boot::setSlotAsUnbootable() on good and bad inputs.
103TEST_F(BootHidlTest, SetSlotAsUnbootable) {
104 {
105 CommandResult cr;
106 Slot curSlot = boot->getCurrentSlot();
107 Slot otherSlot = curSlot ? 0 : 1;
108 Return<void> result =
109 boot->setSlotAsUnbootable(otherSlot, generate_callback(&cr));
Steven Morelandb6438422017-01-03 17:06:57 -0800110 EXPECT_TRUE(result.isOk());
Connor O'Brien100b4912016-11-30 11:10:59 -0800111 if (cr.success) {
112 EXPECT_EQ(BoolResult::FALSE, boot->isSlotBootable(otherSlot));
Connor O'Brien86a7ec32017-01-12 11:30:43 -0800113
114 // Restore original flags to avoid problems on reboot
115 result = boot->setActiveBootSlot(otherSlot, generate_callback(&cr));
116 EXPECT_TRUE(result.isOk());
117 EXPECT_TRUE(cr.success);
118 result = boot->setActiveBootSlot(curSlot, generate_callback(&cr));
119 EXPECT_TRUE(result.isOk());
120 EXPECT_TRUE(cr.success);
121 result = boot->markBootSuccessful(generate_callback(&cr));
122 EXPECT_TRUE(result.isOk());
Connor O'Brien100b4912016-11-30 11:10:59 -0800123 EXPECT_TRUE(cr.success);
124 }
125 }
126 {
127 CommandResult cr;
128 uint32_t slots = boot->getNumberSlots();
129 Return<void> result =
130 boot->setSlotAsUnbootable(slots, generate_callback(&cr));
Steven Morelandb6438422017-01-03 17:06:57 -0800131 EXPECT_TRUE(result.isOk());
Connor O'Brien100b4912016-11-30 11:10:59 -0800132 EXPECT_EQ(false, cr.success);
133 }
134}
135
136// Sanity check Boot::isSlotBootable() on good and bad inputs.
137TEST_F(BootHidlTest, IsSlotBootable) {
138 for (Slot s = 0; s < 2; s++) {
139 EXPECT_NE(BoolResult::INVALID_SLOT, boot->isSlotBootable(s));
140 }
141 uint32_t slots = boot->getNumberSlots();
142 EXPECT_EQ(BoolResult::INVALID_SLOT, boot->isSlotBootable(slots));
143}
144
145// Sanity check Boot::isSlotMarkedSuccessful() on good and bad inputs.
146TEST_F(BootHidlTest, IsSlotMarkedSuccessful) {
147 for (Slot s = 0; s < 2; s++) {
148 EXPECT_NE(BoolResult::INVALID_SLOT, boot->isSlotMarkedSuccessful(s));
149 }
150 uint32_t slots = boot->getNumberSlots();
151 EXPECT_EQ(BoolResult::INVALID_SLOT, boot->isSlotMarkedSuccessful(slots));
152}
153
154// Sanity check Boot::getSuffix() on good and bad inputs.
155TEST_F(BootHidlTest, GetSuffix) {
Connor O'Brienbb884222017-04-05 14:41:34 -0700156 string suffixStr;
157 vector<string> correctSuffixes = {"_a", "_b"};
158 auto cb = [&](hidl_string suffix) { suffixStr = suffix.c_str(); };
159 for (Slot i = 0; i < 2; i++) {
160 CommandResult cr;
161 Return<void> result = boot->getSuffix(i, cb);
162 EXPECT_TRUE(result.isOk());
163 ASSERT_EQ(0, suffixStr.compare(correctSuffixes[i]));
164 }
165 {
166 string emptySuffix = "";
167 Return<void> result = boot->getSuffix(boot->getNumberSlots(), cb);
168 EXPECT_TRUE(result.isOk());
169 ASSERT_EQ(0, suffixStr.compare(emptySuffix));
170 }
Connor O'Brien100b4912016-11-30 11:10:59 -0800171}
172
173int main(int argc, char **argv) {
174 ::testing::InitGoogleTest(&argc, argv);
175 int status = RUN_ALL_TESTS();
176 LOG(INFO) << "Test result = " << status;
177 return status;
178}