1 /*
2  * Copyright (C) 2020 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 
19 #include <android-base/logging.h>
20 #include <android/hardware/boot/1.2/IBootControl.h>
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include <hidl/GtestPrinter.h>
24 #include <hidl/ServiceManagement.h>
25 
26 #include <unistd.h>
27 
28 using ::android::sp;
29 using ::android::hardware::Return;
30 using ::android::hardware::Void;
31 using ::android::hardware::boot::V1_0::CommandResult;
32 using ::android::hardware::boot::V1_0::Slot;
33 using ::android::hardware::boot::V1_2::IBootControl;
34 
35 class BootHidlTest : public testing::TestWithParam<std::string> {
36   public:
SetUp()37     virtual void SetUp() override {
38         boot = IBootControl::getService(GetParam());
39         ASSERT_NE(boot, nullptr);
40 
41         LOG(INFO) << "Test is remote " << boot->isRemote();
42     }
43 
44     sp<IBootControl> boot;
45 };
46 
generate_callback(CommandResult * dest)47 auto generate_callback(CommandResult* dest) {
48     return [=](CommandResult cr) { *dest = cr; };
49 }
50 
TEST_P(BootHidlTest,GetActiveBootSlot)51 TEST_P(BootHidlTest, GetActiveBootSlot) {
52     Slot curSlot = boot->getCurrentSlot();
53     Slot otherSlot = curSlot ? 0 : 1;
54 
55     // Set the active slot, then check if the getter returns the correct slot.
56     CommandResult cr;
57     Return<void> result = boot->setActiveBootSlot(otherSlot, generate_callback(&cr));
58     EXPECT_TRUE(result.isOk());
59     Slot activeSlot = boot->getActiveBootSlot();
60     EXPECT_EQ(otherSlot, activeSlot);
61 
62     result = boot->setActiveBootSlot(curSlot, generate_callback(&cr));
63     EXPECT_TRUE(result.isOk());
64     activeSlot = boot->getActiveBootSlot();
65     EXPECT_EQ(curSlot, activeSlot);
66 }
67 
68 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BootHidlTest);
69 INSTANTIATE_TEST_SUITE_P(
70         PerInstance, BootHidlTest,
71         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IBootControl::descriptor)),
72         android::hardware::PrintInstanceNameToString);
73