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 
32 // Tests that the device is not registering any HIDL interfaces.
33 // HIDL is being deprecated. Only applicable to devices launching with Android
34 // 14 and later.
35 class VintfNoHidlTest : public ::testing::Test {};
36 
allHidlManifestInterfaces()37 static std::set<std::string> allHidlManifestInterfaces() {
38   std::set<std::string> ret;
39   auto setInserter = [&](const vintf::ManifestInstance& i) -> bool {
40     if (i.format() != vintf::HalFormat::HIDL) {
41       return true;
42     }
43     ret.insert(i.getFqInstance().getFqNameString());
44     return true;
45   };
46   vintf::VintfObject::GetDeviceHalManifest()->forEachInstance(setInserter);
47   vintf::VintfObject::GetFrameworkHalManifest()->forEachInstance(setInserter);
48   return ret;
49 }
50 
51 // @VsrTest = VSR-3.2-001.001|VSR-3.2-001.002
TEST_F(VintfNoHidlTest,NoHidl)52 TEST_F(VintfNoHidlTest, NoHidl) {
53   int apiLevel = android::base::GetIntProperty("ro.vendor.api_level", 0);
54   if (apiLevel < __ANDROID_API_U__) {
55     GTEST_SKIP() << "Not applicable to this device";
56     return;
57   }
58   int maxNumberOfHidlHals;
59   std::set<std::string> halInterfaces;
60   if (apiLevel == __ANDROID_API_U__) {
61     maxNumberOfHidlHals = kMaxNumberOfHidlHalsU;
62     sp<hidl::manager::V1_0::IServiceManager> sm =
63         ::android::hardware::defaultServiceManager();
64     ASSERT_NE(sm, nullptr);
65     hardware::Return<void> ret =
66         sm->list([&halInterfaces](const auto& interfaces) {
67           for (const auto& interface : interfaces) {
68             std::vector<std::string> splitInterface =
69                 android::base::Split(interface, "@");
70             ASSERT_GE(splitInterface.size(), 1);
71             // We only care about packages, since HIDL HALs typically need to
72             // include all of the older minor versions as well as the version
73             // they are implementing and we don't want to count those
74             halInterfaces.insert(splitInterface[0]);
75           }
76         });
77   } else {
78     maxNumberOfHidlHals = 0;
79     halInterfaces = allHidlManifestInterfaces();
80   }
81   if (halInterfaces.size() > maxNumberOfHidlHals) {
82     ADD_FAILURE()
83         << "There are " << halInterfaces.size()
84         << " HIDL interfaces served on the device. "
85         << "These must be converted to AIDL as part of HIDL's "
86            "deprecation processes.\n"
87            "NOTE: vts_treble_vintf_vendor_test should pass before this test. "
88            "Make sure the device under test is targeting "
89            "the correct Framework Compatibility Matrix with "
90            "target-level=\"202404\" or greater. That will cause "
91            "the framework/system HIDL services to stop being registered. "
92            "If those are still registered because the device is targeting "
93            "and older FCM, this test will fail.";
94     for (const auto& interface : halInterfaces) {
95       ADD_FAILURE() << interface << " registered as a HIDL interface "
96                     << "but must be in AIDL";
97     }
98   }
99 }
100 
101 }  // namespace testing
102 }  // namespace vintf
103 }  // namespace android
104