1 /*
2  * Copyright (C) 2018 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 #include "DeviceManifestTest.h"
18 
19 #include <android-base/properties.h>
20 #include <android-base/result.h>
21 #include <libvts_vintf_test_common/common.h>
22 #include <vintf/VintfObject.h>
23 
24 #include "SingleManifestTest.h"
25 
26 using testing::Combine;
27 using testing::Values;
28 using testing::ValuesIn;
29 
30 namespace android {
31 namespace vintf {
32 namespace testing {
33 
SetUp()34 void DeviceManifestTest::SetUp() {
35   VtsTrebleVintfTestBase::SetUp();
36 
37   vendor_manifest_ = VintfObject::GetDeviceHalManifest();
38   ASSERT_NE(vendor_manifest_, nullptr)
39       << "Failed to get vendor HAL manifest." << endl;
40 }
41 
42 // Tests that Shipping FCM Version in the device manifest is at least the
43 // minimum Shipping FCM Version as required by Vendor API level.
44 // @VsrTest = VSR-3.2-014
TEST_F(DeviceManifestTest,ShippingFcmVersion)45 TEST_F(DeviceManifestTest, ShippingFcmVersion) {
46   uint64_t vendor_api_level = GetVendorApiLevel();
47   Level shipping_fcm_version = VintfObject::GetDeviceHalManifest()->level();
48   auto res = TestTargetFcmVersion(shipping_fcm_version, vendor_api_level);
49   ASSERT_RESULT_OK(res);
50 }
51 
52 // Check for unused HALs being declared. Every HAL that is declared in the vintf
53 // Manifest must have an entry in the Compatibility Matrix
TEST_F(DeviceManifestTest,UnusedHals)54 TEST_F(DeviceManifestTest, UnusedHals) {
55   auto vintfObject = VintfObject::GetInstance();
56   auto res = vintfObject->checkUnusedHals({});
57 
58   if (!res.ok()) {
59     uint64_t vendor_api_level = GetVendorApiLevel();
60     if (vendor_api_level < 202504) {
61       GTEST_LOG_(ERROR) << res.error();
62       GTEST_SKIP()
63           << "Not enforcing this so that existing devices can continue "
64              "to pass without changes";
65     } else {
66       ADD_FAILURE() << res.error();
67     }
68   }
69 }
70 
71 // Tests that deprecated HALs are not in the manifest, unless a higher,
72 // non-deprecated minor version is in the manifest.
73 // @VsrTest = VSR-3.2-014
TEST_F(DeviceManifestTest,NoDeprecatedHalsOnManifest)74 TEST_F(DeviceManifestTest, NoDeprecatedHalsOnManifest) {
75   string error;
76   EXPECT_EQ(android::vintf::NO_DEPRECATED_HALS,
77             VintfObject::GetInstance()->checkDeprecation(
78                 HidlInterfaceMetadata::all(), &error))
79       << error;
80 }
81 
82 // Tests that devices launching R support [email protected].  Go devices are exempt
83 // from this requirement, so we use this test to enforce instead of the
84 // compatibility matrix.
85 // @VsrTest = VSR-3.2-014
TEST_F(DeviceManifestTest,GraphicsMapperHalVersionCompatibility)86 TEST_F(DeviceManifestTest, GraphicsMapperHalVersionCompatibility) {
87   Level shipping_fcm_version = VintfObject::GetDeviceHalManifest()->level();
88   bool is_go_device =
89       android::base::GetBoolProperty("ro.config.low_ram", false);
90   const auto sdkLevel =
91       android::base::GetUintProperty<uint64_t>("ro.build.version.sdk", 10000);
92   // API 36+ requires mapper4.0 or newer regardless of the initial shipping
93   // version
94   if (sdkLevel < 36) {
95     if (shipping_fcm_version == Level::UNSPECIFIED ||
96         shipping_fcm_version < Level::R ||
97         (is_go_device && shipping_fcm_version < Level::V)) {
98       GTEST_SKIP()
99           << "Graphics mapper 4 is only required on launching R devices";
100     }
101   }
102 
103   if (shipping_fcm_version >= Level::V) {
104     bool exists = false;
105     ASSERT_TRUE(vendor_manifest_->forEachInstance(
106         [&](const ManifestHal::InstanceType& instance) -> bool {
107           if (instance.package() == "mapper" &&
108               instance.format() == HalFormat::NATIVE &&
109               instance.version().majorVer == 5 &&
110               instance.version().minorVer == 0)
111             exists = true;
112           return true;
113         }));
114     ASSERT_TRUE(exists)
115         << "Graphics mapper 5 is required on launching V+ devices";
116   } else {
117     bool exists = false;
118     bool ret = vendor_manifest_->forEachInstance(
119         [&](const ManifestHal::InstanceType& instance) -> bool {
120           if (instance.package() == "mapper" &&
121               instance.format() == HalFormat::NATIVE &&
122               instance.version().majorVer == 5 &&
123               instance.version().minorVer == 0)
124             exists = true;
125           return true;
126         });
127     // If native implementation doesn't exist, then the HIDL implementation must
128     // exist on this device.
129     if (!ret || !exists) {
130       ASSERT_TRUE(vendor_manifest_->hasHidlInstance(
131           "android.hardware.graphics.mapper", {4, 0}, "IMapper", "default"));
132       ASSERT_FALSE(vendor_manifest_->hasHidlInstance(
133           "android.hardware.graphics.mapper", {2, 0}, "IMapper", "default"));
134       ASSERT_FALSE(vendor_manifest_->hasHidlInstance(
135           "android.hardware.graphics.mapper", {2, 1}, "IMapper", "default"));
136     }
137   }
138 }
139 
140 // Devices with Shipping FCM version 3~6 must have either the HIDL or the
141 // AIDL health HAL. Because compatibility matrices cannot express OR condition
142 // between <hal>'s, add a test here.
143 //
144 // There's no need to enforce minimum HAL versions because
145 // NoDeprecatedHalsOnManifest already checks it.
146 // @VsrTest = VSR-3.2-014
TEST_F(DeviceManifestTest,HealthHal)147 TEST_F(DeviceManifestTest, HealthHal) {
148   bool has_hidl = vendor_manifest_->hasHidlInstance(
149       "android.hardware.health", {2, 0}, "IHealth", "default");
150   bool has_aidl = vendor_manifest_->hasAidlInstance("android.hardware.health",
151                                                     1, "IHealth", "default");
152   ASSERT_TRUE(has_hidl || has_aidl)
153       << "Device must have either health HIDL HAL or AIDL HAL";
154 }
155 
156 // Devices with Shipping FCM version 5+ must have the
157 // AIDL power HAL.
158 //
159 // The specific versions are handled by the framework compatibility matrix.
160 // @VsrTest = VSR-3.2-014
TEST_F(DeviceManifestTest,PowerHal)161 TEST_F(DeviceManifestTest, PowerHal) {
162   Level fcm_version = VintfObject::GetDeviceHalManifest()->level();
163   if (fcm_version == Level::UNSPECIFIED || fcm_version < Level::R) {
164     GTEST_SKIP() << "Power HAL is only required on launching R+ devices";
165   }
166   ASSERT_TRUE(vendor_manifest_->hasAidlInstance("android.hardware.power",
167                                                 "IPower", "default"))
168       << "Device must have the android.hardware.power.IPower/default HAL";
169 }
170 
171 // Devices must have either the HIDL or the AIDL gatekeeper HAL.
172 // Because compatibility matrices cannot express OR condition
173 // between <hal>'s, add a test here.
174 //
175 // There's no need to enforce minimum HAL versions because
176 // NoDeprecatedHalsOnManifest already checks it.
177 // @VsrTest = VSR-3.2-014
TEST_F(DeviceManifestTest,GatekeeperHal)178 TEST_F(DeviceManifestTest, GatekeeperHal) {
179   bool has_hidl = vendor_manifest_->hasHidlInstance(
180       "android.hardware.gatekeeper", {1, 0}, "IGatekeeper", "default");
181   bool has_aidl = vendor_manifest_->hasAidlInstance(
182       "android.hardware.gatekeeper", "IGatekeeper", "default");
183   ASSERT_TRUE(has_hidl || has_aidl)
184       << "Device must have either gatekeeper HIDL HAL or AIDL HAL";
185 }
186 
187 // Devices with Shipping FCM version 7 must have either the HIDL or the
188 // AIDL composer HAL. Because compatibility matrices cannot express OR condition
189 // between <hal>'s, add a test here.
190 //
191 // There's no need to enforce minimum HAL versions because
192 // NoDeprecatedHalsOnManifest already checks it.
193 // @VsrTest = VSR-3.2-014
TEST_F(DeviceManifestTest,ComposerHal)194 TEST_F(DeviceManifestTest, ComposerHal) {
195   bool has_hidl = vendor_manifest_->hasHidlInstance(
196       "android.hardware.graphics.composer", {2, 1}, "IComposer", "default");
197   bool has_aidl = vendor_manifest_->hasAidlInstance(
198       "android.hardware.graphics.composer3", 1, "IComposer", "default");
199   ASSERT_TRUE(has_hidl || has_aidl)
200       << "Device must have either composer HIDL HAL or AIDL HAL";
201 }
202 
203 // Devices with Shipping FCM version 7 must have either the HIDL or the
204 // AIDL gralloc HAL. Because compatibility matrices cannot express OR condition
205 // between <hal>'s, add a test here.
206 //
207 // There's no need to enforce minimum HAL versions because
208 // NoDeprecatedHalsOnManifest already checks it.
209 // @VsrTest = VSR-3.2-014
TEST_F(DeviceManifestTest,GrallocHal)210 TEST_F(DeviceManifestTest, GrallocHal) {
211   bool has_hidl = false;
212   for (size_t hidl_major = 2; hidl_major <= 4; hidl_major++)
213     has_hidl = has_hidl || vendor_manifest_->hasHidlInstance(
214                                "android.hardware.graphics.allocator",
215                                {hidl_major, 0}, "IAllocator", "default");
216 
217   bool has_aidl = vendor_manifest_->hasAidlInstance(
218       "android.hardware.graphics.allocator", "IAllocator", "default");
219 
220   ASSERT_TRUE(has_hidl || has_aidl)
221       << "Device must have either graphics allocator HIDL HAL or AIDL HAL";
222 }
223 
224 // Devices after Android T must have either the HIDL or the
225 // AIDL thermal HAL. Because compatibility matrices cannot express OR condition
226 // between <hal>'s, add a test here.
227 // @VsrTest = VSR-3.2-014
TEST_F(DeviceManifestTest,ThermalHal)228 TEST_F(DeviceManifestTest, ThermalHal) {
229   Level shipping_fcm_version = VintfObject::GetDeviceHalManifest()->level();
230   if (shipping_fcm_version == Level::UNSPECIFIED ||
231       shipping_fcm_version < Level::T) {
232     GTEST_SKIP()
233         << "Thermal HAL is only required on devices launching in T or later";
234   }
235   bool has_hidl = vendor_manifest_->hasHidlInstance(
236       "android.hardware.thermal", {2, 0}, "IThermal", "default");
237   bool has_aidl = vendor_manifest_->hasAidlInstance("android.hardware.thermal",
238                                                     "IThermal", "default");
239   ASSERT_TRUE(has_hidl || has_aidl)
240       << "Device must have either thermal HIDL HAL or AIDL HAL";
241 }
242 
243 // Tests that devices launching T support [email protected] or AIDL.
244 // Go devices are exempt
245 // from this requirement, so we use this test to enforce instead of the
246 // compatibility matrix.
247 // @VsrTest = VSR-3.2-014
TEST_F(DeviceManifestTest,GrallocHalVersionCompatibility)248 TEST_F(DeviceManifestTest, GrallocHalVersionCompatibility) {
249   Level shipping_fcm_version = VintfObject::GetDeviceHalManifest()->level();
250   bool is_go_device =
251       android::base::GetBoolProperty("ro.config.low_ram", false);
252   if (shipping_fcm_version == Level::UNSPECIFIED ||
253       shipping_fcm_version < Level::T ||
254       (is_go_device && shipping_fcm_version < Level::V)) {
255     GTEST_SKIP() << "Gralloc 4.0/AIDL is only required on launching T devices";
256   }
257 
258   bool has_aidl = vendor_manifest_->hasAidlInstance(
259       "android.hardware.graphics.allocator", 1, "IAllocator", "default");
260   bool has_hidl_4_0 = vendor_manifest_->hasHidlInstance(
261       "android.hardware.graphics.allocator", {4, 0}, "IAllocator", "default");
262   ASSERT_TRUE(has_aidl || has_hidl_4_0);
263 
264   ASSERT_FALSE(vendor_manifest_->hasHidlInstance(
265       "android.hardware.graphics.allocator", {2, 0}, "IAllocator", "default"));
266   ASSERT_FALSE(vendor_manifest_->hasHidlInstance(
267       "android.hardware.graphics.allocator", {3, 0}, "IAllocator", "default"));
268 }
269 
270 // Devices must have either the HIDL or the AIDL audio HAL, both "core" and
271 // "effect" parts must be of the same type. Checked by a test because
272 // compatibility matrices cannot express these conditions.
273 // @VsrTest = VSR-3.2-014
TEST_F(DeviceManifestTest,AudioHal)274 TEST_F(DeviceManifestTest, AudioHal) {
275   Level shipping_fcm_version = VintfObject::GetDeviceHalManifest()->level();
276   if (shipping_fcm_version == Level::UNSPECIFIED ||
277       shipping_fcm_version < Level::U) {
278     GTEST_SKIP() << "AIDL Audio HAL can only appear on launching U devices";
279   }
280   bool has_hidl_core = false;
281   for (const auto v :
282        {Version(5, 0), Version(6, 0), Version(7, 0), Version(7, 1)}) {
283     has_hidl_core |= vendor_manifest_->hasHidlInstance(
284         "android.hardware.audio", v, "IDevicesFactory", "default");
285   }
286   bool has_hidl_effect = false;
287   for (const auto v : {Version(5, 0), Version(6, 0), Version(7, 0)}) {
288     has_hidl_effect |= vendor_manifest_->hasHidlInstance(
289         "android.hardware.audio.effect", v, "IEffectsFactory", "default");
290   }
291   bool has_aidl_core = vendor_manifest_->hasAidlInstance(
292       "android.hardware.audio.core", "IConfig", "default");
293   bool has_aidl_effect = vendor_manifest_->hasAidlInstance(
294       "android.hardware.audio.effect", "IFactory", "default");
295   EXPECT_EQ(has_hidl_core, has_hidl_effect)
296       << "Device must have both Audio Core and Effect HALs of the same type";
297   EXPECT_EQ(has_aidl_core, has_aidl_effect)
298       << "Device must have both Audio Core and Effect HALs of the same type";
299   EXPECT_TRUE(has_hidl_core || has_aidl_core)
300       << "Device must have either Audio HIDL HAL or AIDL HAL";
301 }
302 
303 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SingleHidlTest);
304 INSTANTIATE_TEST_CASE_P(
305     DeviceManifest, SingleHidlTest,
306     Combine(ValuesIn(VtsTrebleVintfTestBase::GetHidlInstances(
307                 VintfObject::GetDeviceHalManifest())),
308             Values(VintfObject::GetDeviceHalManifest())),
309     &GetTestCaseSuffix<SingleHidlTest>);
310 
311 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SingleHwbinderHalTest);
312 INSTANTIATE_TEST_CASE_P(
313     DeviceManifest, SingleHwbinderHalTest,
314     Combine(ValuesIn(SingleHwbinderHalTest::ListRegisteredHwbinderHals()),
315             Values(VintfObject::GetDeviceHalManifest())),
316     &SingleHwbinderHalTest::GetTestCaseSuffix);
317 
318 INSTANTIATE_TEST_CASE_P(
319     DeviceManifest, SingleAidlTest,
320     Combine(ValuesIn(VtsTrebleVintfTestBase::GetAidlInstances(
321                 VintfObject::GetDeviceHalManifest())),
322             Values(VintfObject::GetDeviceHalManifest())),
323     &GetTestCaseSuffix<SingleAidlTest>);
324 
325 INSTANTIATE_TEST_CASE_P(
326     DeviceManifest, SingleNativeTest,
327     Combine(ValuesIn(VtsTrebleVintfTestBase::GetNativeInstances(
328                 VintfObject::GetDeviceHalManifest())),
329             Values(VintfObject::GetDeviceHalManifest())),
330     &GetTestCaseSuffix<SingleNativeTest>);
331 
332 }  // namespace testing
333 }  // namespace vintf
334 }  // namespace android
335