David Anderson | 0473214 | 2019-10-07 13:34:24 -0700 | [diff] [blame] | 1 | /* |
Sivananthinikumari | b201888 | 2022-06-13 18:10:25 +0530 | [diff] [blame^] | 2 | |
David Anderson | 0473214 | 2019-10-07 13:34:24 -0700 | [diff] [blame] | 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> |
Sivananthinikumari | b201888 | 2022-06-13 18:10:25 +0530 | [diff] [blame^] | 23 | #include <android-base/properties.h> |
David Anderson | 0473214 | 2019-10-07 13:34:24 -0700 | [diff] [blame] | 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 | |
Sivananthinikumari | b201888 | 2022-06-13 18:10:25 +0530 | [diff] [blame^] | 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 | |
| 49 | bool IsVirtualAbEnabled() { |
| 50 | return android::base::GetBoolProperty("ro.virtual_ab.enabled", false); |
| 51 | } |
| 52 | |
David Anderson | 0473214 | 2019-10-07 13:34:24 -0700 | [diff] [blame] | 53 | class BootHidlTest : public testing::TestWithParam<std::string> { |
| 54 | public: |
| 55 | virtual void SetUp() override { |
Sivananthinikumari | b201888 | 2022-06-13 18:10:25 +0530 | [diff] [blame^] | 56 | SKIP_IF_NON_VIRTUAL_AB(); |
David Anderson | 0473214 | 2019-10-07 13:34:24 -0700 | [diff] [blame] | 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 | |
| 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 | */ |
| 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 | */ |
| 89 | TEST_P(BootHidlTest, SetSnapshotMergeStatus) { |
| 90 | for (const auto value : ValidMergeStatusValues()) { |
| 91 | EXPECT_TRUE(boot->setSnapshotMergeStatus(value).withDefault(false)); |
| 92 | auto status = boot->getSnapshotMergeStatus(); |
David Anderson | f0fbfdd | 2020-04-28 17:44:32 -0700 | [diff] [blame] | 93 | if (value == MergeStatus::SNAPSHOTTED) { |
| 94 | EXPECT_TRUE(status == MergeStatus::SNAPSHOTTED || status == MergeStatus::NONE); |
| 95 | } else { |
| 96 | EXPECT_EQ(status, value); |
| 97 | } |
David Anderson | 0473214 | 2019-10-07 13:34:24 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame] | 101 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BootHidlTest); |
David Anderson | 0473214 | 2019-10-07 13:34:24 -0700 | [diff] [blame] | 102 | INSTANTIATE_TEST_SUITE_P( |
Haibo Huang | 83b4c1e | 2019-11-08 11:59:40 -0800 | [diff] [blame] | 103 | PerInstance, BootHidlTest, |
David Anderson | 0473214 | 2019-10-07 13:34:24 -0700 | [diff] [blame] | 104 | testing::ValuesIn(android::hardware::getAllHalInstanceNames(IBootControl::descriptor)), |
| 105 | android::hardware::PrintInstanceNameToString); |