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 #include <aidl/metadata.h>
18 #include <android-base/logging.h>
19 #include <android-base/properties.h>
20 #include <android-base/strings.h>
21 #include <android/content/pm/IPackageManagerNative.h>
22 #include <binder/IServiceManager.h>
23 #include <gtest/gtest.h>
24 #include <hidl-util/FQName.h>
25 #include <hidl/metadata.h>
26 #include <vintf/VintfObject.h>
27
28 #include <algorithm>
29 #include <cstddef>
30 #include <map>
31 #include <mutex>
32 #include <set>
33 #include <string>
34 #include <vector>
35
36 #ifdef AIDL_USE_UNFROZEN
37 constexpr bool kAidlUseUnfrozen = true;
38 #else
39 constexpr bool kAidlUseUnfrozen = false;
40 #endif
41
42 using namespace android;
43
44 // clang-format off
45 static const std::set<std::string> kKnownMissingHidl = {
46 "android.frameworks.automotive.display@1.0", // converted to AIDL, see b/170401743
47 "android.frameworks.cameraservice.device@2.1",
48 "android.frameworks.cameraservice.service@2.2", // converted to AIDL, see b/205764761
49 "android.frameworks.displayservice@1.0", // deprecated, see b/141930622
50 "android.frameworks.schedulerservice@1.0", // deprecated, see b/37226359
51 "android.frameworks.sensorservice@1.0", // deprecated, see b/205764765
52 "android.frameworks.vr.composer@1.0",
53 "android.frameworks.vr.composer@2.0",
54 "android.frameworks.stats@1.0", // converted to AIDL, see b/177667419
55 "android.hardware.atrace@1.0", // deprecated, see b/204935495
56 "android.hardware.audio@2.0",
57 "android.hardware.audio@4.0",
58 "android.hardware.audio@5.0",
59 "android.hardware.audio@6.0",
60 "android.hardware.audio@7.1", // converted to AIDL, see b/264712385
61 "android.hardware.audio.effect@2.0",
62 "android.hardware.audio.effect@4.0",
63 "android.hardware.audio.effect@5.0",
64 "android.hardware.audio.effect@6.0",
65 "android.hardware.audio.effect@7.0", // converted to AIDL, see b/264712385
66 "android.hardware.authsecret@1.0", // converted to AIDL, see b/182976659
67 "android.hardware.automotive.audiocontrol@1.0",
68 "android.hardware.automotive.audiocontrol@2.0",
69 "android.hardware.automotive.can@1.0", // converted to AIDL, see b/170405615
70 "android.hardware.automotive.evs@1.1",
71 "android.hardware.automotive.sv@1.0",
72 "android.hardware.automotive.vehicle@2.0",
73 "android.hardware.biometrics.fingerprint@2.3", // converted to AIDL, see b/152416783
74 "android.hardware.biometrics.face@1.0", // converted to AIDL, see b/168730443
75 "android.hardware.bluetooth.a2dp@1.0",
76 "android.hardware.bluetooth.audio@2.1", // converted to AIDL, see b/203490261
77 "android.hardware.bluetooth@1.1", // converted to AIDL, see b/205758693
78 "android.hardware.boot@1.2", // converted to AIDL, see b/227536004
79 "android.hardware.broadcastradio@1.1",
80 "android.hardware.broadcastradio@2.0",
81 "android.hardware.camera.provider@2.7", // Camera converted to AIDL, b/196432585
82 "android.hardware.cas@1.2", // converted to AIDL, see b/227673974
83 "android.hardware.cas.native@1.0",
84 "android.hardware.configstore@1.1", // deprecated, see b/149050985, b/149050733
85 "android.hardware.confirmationui@1.0", // converted to AIDL, see b/205760172
86 "android.hardware.contexthub@1.2",
87 "android.hardware.drm@1.4", // converted to AIDL, b/200055138
88 "android.hardware.fastboot@1.1",
89 "android.hardware.dumpstate@1.1", // deprecated, see b/205760700
90 "android.hardware.gatekeeper@1.0", // converted to AIDL, b/205760843
91 "android.hardware.gnss@1.1", // GNSS converted to AIDL, b/206670536
92 "android.hardware.gnss@2.1", // GNSS converted to AIDL, b/206670536
93 "android.hardware.gnss.measurement_corrections@1.1", // is sub-interface of gnss
94 "android.hardware.gnss.visibility_control@1.0",
95 "android.hardware.graphics.allocator@2.0",
96 "android.hardware.graphics.allocator@3.0",
97 "android.hardware.graphics.allocator@4.0", // converted to AIDL, see b/205761012
98 "android.hardware.graphics.bufferqueue@1.0",
99 "android.hardware.graphics.bufferqueue@2.0",
100 "android.hardware.graphics.composer@2.4", // converted to AIDL, see b/193240715
101 "android.hardware.graphics.mapper@2.1",
102 "android.hardware.graphics.mapper@3.0",
103 "android.hardware.graphics.mapper@4.0", // converted to Stable C, see b/205761028
104 "android.hardware.health.storage@1.0", // converted to AIDL, see b/177470478
105 "android.hardware.health@2.1", // converted to AIDL, see b/177269435
106 "android.hardware.input.classifier@1.0", // converted to AIDL, see b/205761620
107 "android.hardware.ir@1.0", // converted to AIDL, see b/205000342
108 "android.hardware.keymaster@3.0",
109 "android.hardware.keymaster@4.1", // Replaced by AIDL KeyMint, see b/111446262
110 "android.hardware.light@2.0",
111 "android.hardware.media.bufferpool@1.0",
112 "android.hardware.media.bufferpool@2.0",
113 "android.hardware.media.omx@1.0", // deprecated b/205761766
114 "android.hardware.memtrack@1.0",
115 "android.hardware.neuralnetworks@1.3", // converted to AIDL, see b/161428342
116 "android.hardware.nfc@1.2",
117 "android.hardware.oemlock@1.0", // converted to AIDL, see b/176107318 b/282160400
118 "android.hardware.power@1.3",
119 "android.hardware.power.stats@1.0",
120 "android.hardware.radio@1.6", // converted to AIDL
121 "android.hardware.radio.config@1.3", // converted to AIDL
122 "android.hardware.radio.deprecated@1.0",
123 "android.hardware.renderscript@1.0",
124 "android.hardware.soundtrigger@2.3",
125 "android.hardware.secure_element@1.2",
126 "android.hardware.sensors@1.0",
127 "android.hardware.sensors@2.1",
128 "android.hardware.tetheroffload.config@1.0",
129 "android.hardware.tetheroffload.control@1.1", // see b/170699770
130 "android.hardware.thermal@1.1",
131 "android.hardware.thermal@2.0", // Converted to AIDL (see b/205762943)
132 "android.hardware.tv.cec@1.1",
133 "android.hardware.tv.input@1.0",
134 "android.hardware.tv.tuner@1.1",
135 "android.hardware.usb@1.3", // migrated to AIDL see b/200993386
136 "android.hardware.usb.gadget@1.2",
137 "android.hardware.vibrator@1.3",
138 "android.hardware.vr@1.0",
139 "android.hardware.weaver@1.0",
140 "android.hardware.wifi@1.6", // Converted to AIDL (see b/205044134)
141 "android.hardware.wifi.hostapd@1.3", // Converted to AIDL (see b/194806512)
142 "android.hardware.wifi.supplicant@1.4", // Converted to AIDL (see b/196235436)
143 "android.hidl.base@1.0",
144 "android.hidl.memory.token@1.0",
145 "android.hidl.memory@1.0", // Deprecated (see b/205764958)
146 "android.system.net.netd@1.1", // Converted to AIDL (see b/205764585)
147 "android.system.suspend@1.0", // Converted to AIDL (see b/170260236)
148 "android.system.wifi.keystore@1.0", // Converted to AIDL (see b/205764502)
149 };
150 // clang-format on
151
152 struct VersionedAidlPackage {
153 std::string name;
154 size_t version;
155 int bugNum;
156 std::string instance;
operator <VersionedAidlPackage157 bool operator<(const VersionedAidlPackage& rhs) const {
158 return (name < rhs.name || (name == rhs.name && version < rhs.version));
159 }
160 };
161
162 static const std::set<std::string> kPhoneOnlyAidl = {
163 "android.hardware.camera.provider",
164 };
165
166 static const std::set<std::string> kAutomotiveOnlyAidl = {
167 /**
168 * These types are only used in Android Automotive, so don't expect them
169 * on phones.
170 */
171 "android.automotive.watchdog",
172 "android.frameworks.automotive.display",
173 "android.frameworks.automotive.power",
174 "android.frameworks.automotive.powerpolicy",
175 "android.frameworks.automotive.powerpolicy.internal",
176 "android.frameworks.automotive.telemetry",
177 "android.hardware.automotive.audiocontrol",
178 "android.hardware.automotive.can",
179 "android.hardware.broadcastradio",
180 "android.hardware.automotive.occupant_awareness",
181 "android.hardware.automotive.remoteaccess",
182 "android.hardware.automotive.vehicle",
183 "android.hardware.automotive.ivn",
184 "android.hardware.macsec",
185 };
186
187 static const std::set<std::string> kTvOnlyAidl = {
188 /**
189 * These types are only used in Android TV, so don't expect them on other
190 * devices.
191 * TODO(b/266868403) This test should run on TV devices to enforce the same
192 * requirements
193 */
194 "android.hardware.tv.hdmi.cec", "android.hardware.tv.hdmi.earc",
195 "android.hardware.tv.hdmi.connection", "android.hardware.tv.tuner",
196 "android.hardware.tv.input", "android.hardware.tv.mediaquality",
197 };
198
199 static const std::set<std::string> kRadioOnlyAidl = {
200 // Not all devices have radio capabilities
201 "android.hardware.radio.config", "android.hardware.radio.data",
202 "android.hardware.radio.messaging", "android.hardware.radio.modem",
203 "android.hardware.radio.network", "android.hardware.radio.sap",
204 "android.hardware.radio.sim", "android.hardware.radio.voice",
205 "android.hardware.radio.ims", "android.hardware.radio.ims.media",
206 "android.hardware.radio.satellite",
207
208 };
209
210 /*
211 * Always missing AIDL packages that are not served on Cuttlefish.
212 * These are typically types-only packages.
213 */
214 static const std::set<std::string> kAlwaysMissingAidl = {
215 // types-only packages, which never expect a default implementation
216 "android.frameworks.cameraservice.common",
217 "android.frameworks.cameraservice.device",
218 "android.hardware.audio.common",
219 "android.hardware.audio.core.sounddose",
220 "android.hardware.biometrics.common",
221 "android.hardware.camera.common",
222 "android.hardware.camera.device",
223 "android.hardware.camera.metadata",
224 "android.hardware.common",
225 "android.hardware.common.fmq",
226 "android.hardware.drm.common",
227 "android.hardware.graphics.common",
228 "android.hardware.input.common",
229 "android.media.audio.common.types",
230 "android.media.audio.eraser.types",
231 "android.hardware.radio",
232 "android.hardware.uwb.fira_android",
233 "android.hardware.wifi.common",
234 "android.hardware.keymaster",
235 "android.hardware.automotive.vehicle.property",
236 // not on Cuttlefish since it's needed only on systems using HIDL audio HAL
237 "android.hardware.audio.sounddose",
238
239 // android.hardware.media.bufferpool2 is a HAL-less interface.
240 // It could be used for buffer recycling and caching by using the interface.
241 "android.hardware.media.bufferpool2",
242
243 /**
244 * No implementation on cuttlefish for fastboot AIDL hal because it doesn't
245 * run during normal boot, only in recovery/fastboot mode.
246 */
247 "android.hardware.fastboot",
248 /**
249 * No implementation for usb gadget HAL because cuttlefish doesn't
250 * support usb gadget configfs, and currently there is no
251 * plan to add this support.
252 * Context: (b/130076572, g/android-idl-discuss/c/0SaiY0p-vJw/)
253 */
254 "android.hardware.usb.gadget",
255 // Currently this HAL only implements a feature for protected VMs, and the
256 // reference implementation of this HAL only works with pKVM hypervisor.
257 // TODO(b/360102915): remove this after implementing no-op version of HAL
258 // for cuttlefish.
259 "android.hardware.virtualization.capabilities.capabilities_service",
260 };
261
262 /*
263 * These packages should have implementations but currently do not.
264 * These must be accompanied by a bug and expected to be here temporarily.
265 */
266 static const std::vector<VersionedAidlPackage> kKnownMissingAidl = {
267 // Cuttlefish Identity Credential HAL implementation is currently
268 // stuck at version 3 while RKP support is being added. Will be
269 // updated soon.
270 {"android.hardware.identity.", 4, 266869317},
271 {"android.hardware.identity.", 5, 266869317},
272
273 {"android.se.omapi.", 1, 266870904},
274 {"android.hardware.soundtrigger3.", 3, 266941225},
275 {"android.media.soundtrigger.", 3, 266941225},
276 {"android.hardware.weaver.", 2, 262418065},
277
278 {"android.automotive.computepipe.registry.", 2, 273549907},
279 {"android.automotive.computepipe.runner.", 2, 273549907},
280 {"android.hardware.automotive.evs.", 2, 274162534},
281 {"android.hardware.security.see.authmgr.", 1, 379940224},
282 {"android.hardware.security.see.storage.", 1, 379940224},
283 {"android.hardware.security.see.hwcrypto.", 1, 379940224},
284 {"android.hardware.security.see.hdcp.", 1, 379940224},
285 };
286
287 // android.hardware.foo.IFoo -> android.hardware.foo.
getAidlPackage(const std::string & aidlType)288 std::string getAidlPackage(const std::string& aidlType) {
289 size_t lastDot = aidlType.rfind('.');
290 CHECK(lastDot != std::string::npos);
291 return aidlType.substr(0, lastDot + 1);
292 }
293
isAospAidlInterface(const std::string & name)294 static bool isAospAidlInterface(const std::string& name) {
295 return base::StartsWith(name, "android.") &&
296 !base::StartsWith(name, "android.hardware.tests.") &&
297 !base::StartsWith(name, "android.aidl.tests");
298 }
299
300 enum class DeviceType {
301 UNKNOWN,
302 AUTOMOTIVE,
303 TV,
304 WATCH,
305 PHONE,
306 };
307
getDeviceType()308 static DeviceType getDeviceType() {
309 static DeviceType type = DeviceType::UNKNOWN;
310 if (type != DeviceType::UNKNOWN) {
311 return type;
312 }
313
314 sp<IBinder> binder =
315 defaultServiceManager()->waitForService(String16("package_native"));
316 sp<content::pm::IPackageManagerNative> packageManager =
317 interface_cast<content::pm::IPackageManagerNative>(binder);
318 CHECK(packageManager != nullptr);
319
320 bool hasFeature = false;
321 // PackageManager.FEATURE_AUTOMOTIVE
322 CHECK(packageManager
323 ->hasSystemFeature(String16("android.hardware.type.automotive"), 0,
324 &hasFeature)
325 .isOk());
326 if (hasFeature) {
327 return DeviceType::AUTOMOTIVE;
328 }
329
330 // PackageManager.FEATURE_LEANBACK
331 CHECK(packageManager
332 ->hasSystemFeature(String16("android.software.leanback"), 0,
333 &hasFeature)
334 .isOk());
335 if (hasFeature) {
336 return DeviceType::TV;
337 }
338
339 // PackageManager.FEATURE_WATCH
340 CHECK(packageManager
341 ->hasSystemFeature(String16("android.hardware.type.watch"), 0,
342 &hasFeature)
343 .isOk());
344 if (hasFeature) {
345 return DeviceType::WATCH;
346 }
347
348 return DeviceType::PHONE;
349 }
350
isMissingAidl(const std::string & packageName)351 static bool isMissingAidl(const std::string& packageName) {
352 static std::once_flag unionFlag;
353 static std::set<std::string> missingAidl = kAlwaysMissingAidl;
354
355 std::call_once(unionFlag, [&]() {
356 const DeviceType type = getDeviceType();
357 switch (type) {
358 case DeviceType::AUTOMOTIVE:
359 missingAidl.insert(kPhoneOnlyAidl.begin(), kPhoneOnlyAidl.end());
360 missingAidl.insert(kTvOnlyAidl.begin(), kTvOnlyAidl.end());
361 break;
362 case DeviceType::TV:
363 missingAidl.insert(kAutomotiveOnlyAidl.begin(),
364 kAutomotiveOnlyAidl.end());
365 missingAidl.insert(kRadioOnlyAidl.begin(), kRadioOnlyAidl.end());
366 break;
367 case DeviceType::WATCH:
368 missingAidl.insert(kAutomotiveOnlyAidl.begin(),
369 kAutomotiveOnlyAidl.end());
370 missingAidl.insert(kPhoneOnlyAidl.begin(), kPhoneOnlyAidl.end());
371 missingAidl.insert(kTvOnlyAidl.begin(), kTvOnlyAidl.end());
372 break;
373 case DeviceType::PHONE:
374 missingAidl.insert(kAutomotiveOnlyAidl.begin(),
375 kAutomotiveOnlyAidl.end());
376 missingAidl.insert(kTvOnlyAidl.begin(), kTvOnlyAidl.end());
377 break;
378 case DeviceType::UNKNOWN:
379 CHECK(false) << "getDeviceType return UNKNOWN type.";
380 break;
381 }
382 });
383
384 return missingAidl.find(packageName) != missingAidl.end();
385 }
386
allAidlManifestInterfaces()387 static std::vector<VersionedAidlPackage> allAidlManifestInterfaces() {
388 std::vector<VersionedAidlPackage> ret;
389 auto setInserter = [&](const vintf::ManifestInstance& i) -> bool {
390 if (i.format() != vintf::HalFormat::AIDL) {
391 return true; // continue
392 }
393 ret.push_back({i.package() + "." + i.interface(), i.version().minorVer, 0,
394 i.instance()});
395 return true; // continue
396 };
397 vintf::VintfObject::GetDeviceHalManifest()->forEachInstance(setInserter);
398 vintf::VintfObject::GetFrameworkHalManifest()->forEachInstance(setInserter);
399 return ret;
400 }
401
TEST(Hal,AllAidlInterfacesAreInAosp)402 TEST(Hal, AllAidlInterfacesAreInAosp) {
403 if (!kAidlUseUnfrozen) {
404 GTEST_SKIP() << "Not valid in 'next' configuration";
405 }
406 if (getDeviceType() != DeviceType::PHONE) {
407 GTEST_SKIP() << "Test only supports phones right now";
408 }
409 for (const auto& package : allAidlManifestInterfaces()) {
410 EXPECT_TRUE(isAospAidlInterface(package.name))
411 << "This device should only have AOSP interfaces, not: "
412 << package.name;
413 }
414 }
415
TEST(Hal,NoExtensionsOnAospInterfaces)416 TEST(Hal, NoExtensionsOnAospInterfaces) {
417 if (!kAidlUseUnfrozen) {
418 GTEST_SKIP() << "Not valid in 'next' configuration";
419 }
420 if (getDeviceType() != DeviceType::PHONE) {
421 GTEST_SKIP() << "Test only supports phones right now";
422 }
423 for (const auto& package : allAidlManifestInterfaces()) {
424 if (isAospAidlInterface(package.name)) {
425 std::string instance = package.name + "/" + package.instance;
426 sp<IBinder> binder =
427 defaultServiceManager()->waitForService(String16(instance.c_str()));
428 EXPECT_NE(binder, nullptr)
429 << "Failed to find " << instance << " even though it is declared. "
430 << "Check for crashes or misconficuration of the service";
431 if (binder) {
432 sp<IBinder> extension = nullptr;
433 auto status = binder->getExtension(&extension);
434 EXPECT_EQ(status, OK) << "Failed to getExtension on " << instance
435 << " status: " << statusToString(status);
436 EXPECT_EQ(extension, nullptr)
437 << "Found an extension interface on " << instance
438 << ". This is not allowed on Cuttlefish";
439 }
440 }
441 }
442 }
443
444 struct AidlPackageCheck {
445 bool hasRegistration;
446 bool knownMissing;
447 };
448
TEST(Hal,AidlInterfacesImplemented)449 TEST(Hal, AidlInterfacesImplemented) {
450 if (!kAidlUseUnfrozen) {
451 GTEST_SKIP() << "Not valid in 'next' configuration";
452 }
453 if (getDeviceType() != DeviceType::PHONE) {
454 GTEST_SKIP() << "Test only supports phones right now";
455 }
456 std::vector<VersionedAidlPackage> manifest = allAidlManifestInterfaces();
457 std::vector<VersionedAidlPackage> thoughtMissing = kKnownMissingAidl;
458
459 for (const auto& treePackage : AidlInterfaceMetadata::all()) {
460 ASSERT_FALSE(treePackage.types.empty()) << treePackage.name;
461 if (std::none_of(treePackage.types.begin(), treePackage.types.end(),
462 isAospAidlInterface) ||
463 isMissingAidl(treePackage.name)) {
464 continue;
465 }
466 if (treePackage.stability != "vintf") {
467 continue;
468 }
469
470 // expect versions from 1 to latest version. If the package has development
471 // the latest version is the latest known version + 1. Each of these need
472 // to be checked for registration and knownMissing.
473 std::map<size_t, AidlPackageCheck> expectedVersions;
474 for (const auto version : treePackage.versions) {
475 expectedVersions[version] = {false, false};
476 }
477 if (treePackage.has_development) {
478 size_t version =
479 treePackage.versions.empty() ? 1 : *treePackage.versions.rbegin() + 1;
480 expectedVersions[version] = {false, false};
481 }
482
483 // Check all types and versions defined by the package for registration.
484 // The package version is considered registered if any of those types are
485 // present in the manifest with the same version.
486 // The package version is considered known missing if it is found in
487 // thoughtMissing.
488 bool latestRegistered = false;
489 for (const std::string& type : treePackage.types) {
490 for (auto& [version, check] : expectedVersions) {
491 auto it = std::remove_if(
492 manifest.begin(), manifest.end(),
493 [&type, &ver = version](const VersionedAidlPackage& package) {
494 return package.name == type && package.version == ver;
495 });
496 if (it != manifest.end()) {
497 manifest.erase(it, manifest.end());
498 if (version == expectedVersions.rbegin()->first) {
499 latestRegistered = true;
500 }
501 check.hasRegistration = true;
502 }
503 it = std::remove_if(
504 thoughtMissing.begin(), thoughtMissing.end(),
505 [&type, &ver = version](const VersionedAidlPackage& package) {
506 return package.name == getAidlPackage(type) &&
507 package.version == ver;
508 });
509 if (it != thoughtMissing.end()) {
510 thoughtMissing.erase(it, thoughtMissing.end());
511 check.knownMissing = true;
512 }
513 }
514 }
515
516 if (!latestRegistered && !expectedVersions.rbegin()->second.knownMissing) {
517 ADD_FAILURE() << "The latest version ("
518 << expectedVersions.rbegin()->first
519 << ") of the module is not implemented: "
520 << treePackage.name
521 << " which declares the following types:\n "
522 << base::Join(treePackage.types, "\n ");
523 }
524
525 for (const auto& [version, check] : expectedVersions) {
526 if (check.knownMissing) {
527 if (check.hasRegistration) {
528 ADD_FAILURE() << "Package in missing list, but available: "
529 << treePackage.name << " V" << version
530 << " which declares the following types:\n "
531 << base::Join(treePackage.types, "\n ");
532 }
533
534 continue;
535 }
536 }
537 }
538
539 for (const auto& package : thoughtMissing) {
540 ADD_FAILURE() << "Interface in missing list and cannot find it anywhere: "
541 << package.name << " V" << package.version;
542 }
543
544 for (const auto& package : manifest) {
545 ADD_FAILURE() << "Can't find manifest entry in tree: " << package.name
546 << " version: " << package.version;
547 }
548 }
549