1 /*
2  * Copyright (C) 2019 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 <vector>
20 
21 #include <android-base/logging.h>
22 #include <android/hardware/boot/1.1/IBootControl.h>
23 #include <android/hardware/boot/1.1/types.h>
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26 #include <hidl/GtestPrinter.h>
27 #include <hidl/ServiceManagement.h>
28 
29 #include <unistd.h>
30 
31 using ::android::sp;
32 using ::android::hardware::hidl_enum_range;
33 using ::android::hardware::hidl_vec;
34 using ::android::hardware::Return;
35 using ::android::hardware::Void;
36 using ::android::hardware::boot::V1_1::IBootControl;
37 using ::android::hardware::boot::V1_1::MergeStatus;
38 using ::testing::Contains;
39 
40 class BootHidlTest : public testing::TestWithParam<std::string> {
41   public:
SetUp()42     virtual void SetUp() override {
43         boot = IBootControl::getService(GetParam());
44         ASSERT_NE(boot, nullptr);
45 
46         LOG(INFO) << "Test is remote " << boot->isRemote();
47     }
48 
49     sp<IBootControl> boot;
50 };
51 
ValidMergeStatusValues()52 static std::vector<MergeStatus> ValidMergeStatusValues() {
53     std::vector<MergeStatus> values;
54     for (const auto value : hidl_enum_range<MergeStatus>()) {
55         if (value == MergeStatus::UNKNOWN) {
56             continue;
57         }
58         values.push_back(value);
59     }
60     return values;
61 }
62 
63 /**
64  * Ensure merge status can be retrieved.
65  */
TEST_P(BootHidlTest,GetSnapshotMergeStatus)66 TEST_P(BootHidlTest, GetSnapshotMergeStatus) {
67     auto values = ValidMergeStatusValues();
68     auto status = (MergeStatus)boot->getSnapshotMergeStatus();
69     EXPECT_THAT(values, Contains(status));
70 }
71 
72 /**
73  * Ensure merge status can be set to arbitrary value.
74  */
TEST_P(BootHidlTest,SetSnapshotMergeStatus)75 TEST_P(BootHidlTest, SetSnapshotMergeStatus) {
76     for (const auto value : ValidMergeStatusValues()) {
77         EXPECT_TRUE(boot->setSnapshotMergeStatus(value).withDefault(false));
78         auto status = boot->getSnapshotMergeStatus();
79         if (value == MergeStatus::SNAPSHOTTED) {
80             EXPECT_TRUE(status == MergeStatus::SNAPSHOTTED || status == MergeStatus::NONE);
81         } else {
82             EXPECT_EQ(status, value);
83         }
84     }
85 }
86 
87 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BootHidlTest);
88 INSTANTIATE_TEST_SUITE_P(
89         PerInstance, BootHidlTest,
90         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IBootControl::descriptor)),
91         android::hardware::PrintInstanceNameToString);
92