1 /*
2 * Copyright (C) 2022 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 #include <android-base/properties.h>
17 #include <android-base/strings.h>
18 #include <android/api-level.h>
19 #include <android/hidl/manager/1.0/IServiceManager.h>
20 #include <gmock/gmock.h>
21 #include <hidl/ServiceManagement.h>
22 #include <vintf/VintfObject.h>
23
24 #define __ANDROID_VENDOR_API_24Q2__ 202404
25
26 namespace android {
27 namespace vintf {
28 namespace testing {
29
30 static constexpr int kMaxNumberOfHidlHalsU = 100;
31 static constexpr int kMaxNumberOfHidlHalsV = 0;
32
33 // Tests that the device is not registering any HIDL interfaces.
34 // HIDL is being deprecated. Only applicable to devices launching with Android
35 // 14 and later.
36 class VintfNoHidlTest : public ::testing::Test {};
37
allHidlManifestInterfaces()38 static std::set<std::string> allHidlManifestInterfaces() {
39 std::set<std::string> ret;
40 auto setInserter = [&](const vintf::ManifestInstance& i) -> bool {
41 if (i.format() != vintf::HalFormat::HIDL) {
42 return true;
43 }
44 ret.insert(i.getFqInstance().getFqNameString());
45 return true;
46 };
47 vintf::VintfObject::GetDeviceHalManifest()->forEachInstance(setInserter);
48 vintf::VintfObject::GetFrameworkHalManifest()->forEachInstance(setInserter);
49 return ret;
50 }
51
52 // @VsrTest = VSR-3.2-001.001|VSR-3.2-001.002
TEST_F(VintfNoHidlTest,NoHidl)53 TEST_F(VintfNoHidlTest, NoHidl) {
54 int apiLevel = android::base::GetIntProperty("ro.vendor.api_level", 0);
55 if (apiLevel < __ANDROID_API_U__) {
56 GTEST_SKIP() << "Not applicable to this device";
57 return;
58 }
59 int maxNumberOfHidlHals = 0;
60 std::set<std::string> halInterfaces;
61 if (apiLevel == __ANDROID_API_U__) {
62 maxNumberOfHidlHals = kMaxNumberOfHidlHalsU;
63 sp<hidl::manager::V1_0::IServiceManager> sm =
64 ::android::hardware::defaultServiceManager();
65 ASSERT_NE(sm, nullptr);
66 hardware::Return<void> ret =
67 sm->list([&halInterfaces](const auto& interfaces) {
68 for (const auto& interface : interfaces) {
69 std::vector<std::string> splitInterface =
70 android::base::Split(interface, "@");
71 ASSERT_GE(splitInterface.size(), 1);
72 // We only care about packages, since HIDL HALs typically need to
73 // include all of the older minor versions as well as the version
74 // they are implementing and we don't want to count those
75 halInterfaces.insert(splitInterface[0]);
76 }
77 });
78 } else if (apiLevel == __ANDROID_VENDOR_API_24Q2__) {
79 maxNumberOfHidlHals = kMaxNumberOfHidlHalsV;
80 halInterfaces = allHidlManifestInterfaces();
81 } else {
82 // TODO(232439834) We can remove this once kMaxNumberOfHidlHalsV is 0.
83 GTEST_FAIL() << "Unexpected Android vendor API level (" << apiLevel
84 << "). Must be either " << __ANDROID_API_U__ << " or "
85 << __ANDROID_VENDOR_API_24Q2__;
86 }
87 if (halInterfaces.size() > maxNumberOfHidlHals) {
88 ADD_FAILURE() << "There are " << halInterfaces.size()
89 << " HIDL interfaces served on the device. "
90 << "These must be converted to AIDL as part of HIDL's "
91 "deprecation processes.";
92 for (const auto& interface : halInterfaces) {
93 ADD_FAILURE() << interface << " registered as a HIDL interface "
94 << "but must be in AIDL";
95 }
96 }
97 }
98
99 } // namespace testing
100 } // namespace vintf
101 } // namespace android
102