1 /*
2 
3  * Copyright (C) 2019 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #define LOG_TAG "boot_hidl_hal_test"
19 
20 #include <vector>
21 
22 #include <android-base/logging.h>
23 #include <android-base/properties.h>
24 #include <android/hardware/boot/1.1/IBootControl.h>
25 #include <android/hardware/boot/1.1/types.h>
26 #include <gmock/gmock.h>
27 #include <gtest/gtest.h>
28 #include <hidl/GtestPrinter.h>
29 #include <hidl/ServiceManagement.h>
30 
31 #include <unistd.h>
32 
33 using ::android::sp;
34 using ::android::hardware::hidl_enum_range;
35 using ::android::hardware::hidl_vec;
36 using ::android::hardware::Return;
37 using ::android::hardware::Void;
38 using ::android::hardware::boot::V1_1::IBootControl;
39 using ::android::hardware::boot::V1_1::MergeStatus;
40 using ::testing::Contains;
41 
42 bool IsVirtualAbEnabled();
43 
44 #define SKIP_IF_NON_VIRTUAL_AB()                                                        \
45     do {                                                                                \
46         if (!IsVirtualAbEnabled()) GTEST_SKIP() << "Test for Virtual A/B devices only"; \
47     } while (0)
48 
IsVirtualAbEnabled()49 bool IsVirtualAbEnabled() {
50     return android::base::GetBoolProperty("ro.virtual_ab.enabled", false);
51 }
52 
53 class BootHidlTest : public testing::TestWithParam<std::string> {
54   public:
SetUp()55     virtual void SetUp() override {
56         SKIP_IF_NON_VIRTUAL_AB();
57         boot = IBootControl::getService(GetParam());
58         ASSERT_NE(boot, nullptr);
59 
60         LOG(INFO) << "Test is remote " << boot->isRemote();
61     }
62 
63     sp<IBootControl> boot;
64 };
65 
ValidMergeStatusValues()66 static std::vector<MergeStatus> ValidMergeStatusValues() {
67     std::vector<MergeStatus> values;
68     for (const auto value : hidl_enum_range<MergeStatus>()) {
69         if (value == MergeStatus::UNKNOWN) {
70             continue;
71         }
72         values.push_back(value);
73     }
74     return values;
75 }
76 
77 /**
78  * Ensure merge status can be retrieved.
79  */
TEST_P(BootHidlTest,GetSnapshotMergeStatus)80 TEST_P(BootHidlTest, GetSnapshotMergeStatus) {
81     auto values = ValidMergeStatusValues();
82     auto status = (MergeStatus)boot->getSnapshotMergeStatus();
83     EXPECT_THAT(values, Contains(status));
84 }
85 
86 /**
87  * Ensure merge status can be set to arbitrary value.
88  */
TEST_P(BootHidlTest,SetSnapshotMergeStatus)89 TEST_P(BootHidlTest, SetSnapshotMergeStatus) {
90     for (const auto value : ValidMergeStatusValues()) {
91         EXPECT_TRUE(boot->setSnapshotMergeStatus(value).withDefault(false));
92         auto status = boot->getSnapshotMergeStatus();
93         if (value == MergeStatus::SNAPSHOTTED) {
94             EXPECT_TRUE(status == MergeStatus::SNAPSHOTTED || status == MergeStatus::NONE);
95         } else {
96             EXPECT_EQ(status, value);
97         }
98     }
99 }
100 
101 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BootHidlTest);
102 INSTANTIATE_TEST_SUITE_P(
103         PerInstance, BootHidlTest,
104         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IBootControl::descriptor)),
105         android::hardware::PrintInstanceNameToString);
106