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;
operator <VersionedAidlPackage156   bool operator<(const VersionedAidlPackage& rhs) const {
157     return (name < rhs.name || (name == rhs.name && version < rhs.version));
158   }
159 };
160 
161 static const std::set<std::string> kPhoneOnlyAidl = {
162     "android.hardware.camera.provider",
163 };
164 
165 static const std::set<std::string> kAutomotiveOnlyAidl = {
166     /**
167      * These types are only used in Android Automotive, so don't expect them
168      * on phones.
169      */
170     "android.automotive.watchdog",
171     "android.frameworks.automotive.display",
172     "android.frameworks.automotive.powerpolicy",
173     "android.frameworks.automotive.powerpolicy.internal",
174     "android.frameworks.automotive.telemetry",
175     "android.hardware.automotive.audiocontrol",
176     "android.hardware.automotive.can",
177     "android.hardware.broadcastradio",
178     "android.hardware.automotive.occupant_awareness",
179     "android.hardware.automotive.remoteaccess",
180     "android.hardware.automotive.vehicle",
181     "android.hardware.automotive.ivn",
182     "android.hardware.macsec",
183 };
184 
185 static const std::set<std::string> kTvOnlyAidl = {
186     /**
187      * These types are only used in Android TV, so don't expect them on other
188      * devices.
189      * TODO(b/266868403) This test should run on TV devices to enforce the same
190      * requirements
191      */
192     "android.hardware.tv.hdmi.cec",        "android.hardware.tv.hdmi.earc",
193     "android.hardware.tv.hdmi.connection", "android.hardware.tv.tuner",
194     "android.hardware.tv.input",
195 };
196 
197 static const std::set<std::string> kRadioOnlyAidl = {
198     // Not all devices have radio capabilities
199     "android.hardware.radio.config",    "android.hardware.radio.data",
200     "android.hardware.radio.messaging", "android.hardware.radio.modem",
201     "android.hardware.radio.network",   "android.hardware.radio.sap",
202     "android.hardware.radio.sim",       "android.hardware.radio.voice",
203     "android.hardware.radio.ims",       "android.hardware.radio.ims.media",
204     "android.hardware.radio.satellite",
205 
206 };
207 
208 /*
209  * Always missing AIDL packages that are not served on Cuttlefish.
210  * These are typically types-only packages.
211  */
212 static const std::set<std::string> kAlwaysMissingAidl = {
213     // types-only packages, which never expect a default implementation
214     "android.frameworks.cameraservice.common",
215     "android.frameworks.cameraservice.device",
216     "android.hardware.audio.common",
217     "android.hardware.audio.core.sounddose",
218     "android.hardware.biometrics.common",
219     "android.hardware.camera.common",
220     "android.hardware.camera.device",
221     "android.hardware.camera.metadata",
222     "android.hardware.common",
223     "android.hardware.common.fmq",
224     "android.hardware.graphics.common",
225     "android.hardware.input.common",
226     "android.media.audio.common.types",
227     "android.hardware.radio",
228     "android.hardware.uwb.fira_android",
229     "android.hardware.wifi.common",
230     "android.hardware.keymaster",
231     "android.hardware.automotive.vehicle.property",
232     // not on Cuttlefish since it's needed only on systems using HIDL audio HAL
233     "android.hardware.audio.sounddose",
234 
235     // android.hardware.media.bufferpool2 is a HAL-less interface.
236     // It could be used for buffer recycling and caching by using the interface.
237     "android.hardware.media.bufferpool2",
238 
239     /**
240      * No implementation on cuttlefish for fastboot AIDL hal because it doesn't
241      * run during normal boot, only in recovery/fastboot mode.
242      */
243     "android.hardware.fastboot",
244     /**
245      * No implementation for usb gadget HAL because cuttlefish doesn't
246      * support usb gadget configfs, and currently there is no
247      * plan to add this support.
248      * Context: (b/130076572, g/android-idl-discuss/c/0SaiY0p-vJw/)
249      */
250     "android.hardware.usb.gadget",
251 };
252 
253 /*
254  * These packages should have implementations but currently do not.
255  * These must be accompanied by a bug and expected to be here temporarily.
256  */
257 static const std::vector<VersionedAidlPackage> kKnownMissingAidl = {
258     // Cuttlefish Identity Credential HAL implementation is currently
259     // stuck at version 3 while RKP support is being added. Will be
260     // updated soon.
261     {"android.hardware.identity.", 4, 266869317},
262     {"android.hardware.identity.", 5, 266869317},
263 
264     {"android.se.omapi.", 1, 266870904},
265     {"android.hardware.soundtrigger3.", 2, 266941225},
266     {"android.media.soundtrigger.", 2, 266941225},
267     {"android.hardware.weaver.", 2, 262418065},
268 
269     {"android.automotive.computepipe.registry.", 2, 273549907},
270     {"android.automotive.computepipe.runner.", 2, 273549907},
271     {"android.hardware.automotive.evs.", 2, 274162534},
272 };
273 
274 // android.hardware.foo.IFoo -> android.hardware.foo.
getAidlPackage(const std::string & aidlType)275 std::string getAidlPackage(const std::string& aidlType) {
276   size_t lastDot = aidlType.rfind('.');
277   CHECK(lastDot != std::string::npos);
278   return aidlType.substr(0, lastDot + 1);
279 }
280 
isAospAidlInterface(const std::string & name)281 static bool isAospAidlInterface(const std::string& name) {
282   return base::StartsWith(name, "android.") &&
283          !base::StartsWith(name, "android.hardware.tests.") &&
284          !base::StartsWith(name, "android.aidl.tests");
285 }
286 
287 enum class DeviceType {
288   UNKNOWN,
289   AUTOMOTIVE,
290   TV,
291   WATCH,
292   PHONE,
293 };
294 
getDeviceType()295 static DeviceType getDeviceType() {
296   static DeviceType type = DeviceType::UNKNOWN;
297   if (type != DeviceType::UNKNOWN) {
298     return type;
299   }
300 
301   sp<IBinder> binder =
302       defaultServiceManager()->waitForService(String16("package_native"));
303   sp<content::pm::IPackageManagerNative> packageManager =
304       interface_cast<content::pm::IPackageManagerNative>(binder);
305   CHECK(packageManager != nullptr);
306 
307   bool hasFeature = false;
308   // PackageManager.FEATURE_AUTOMOTIVE
309   CHECK(packageManager
310             ->hasSystemFeature(String16("android.hardware.type.automotive"), 0,
311                                &hasFeature)
312             .isOk());
313   if (hasFeature) {
314     return DeviceType::AUTOMOTIVE;
315   }
316 
317   // PackageManager.FEATURE_LEANBACK
318   CHECK(packageManager
319             ->hasSystemFeature(String16("android.software.leanback"), 0,
320                                &hasFeature)
321             .isOk());
322   if (hasFeature) {
323     return DeviceType::TV;
324   }
325 
326   // PackageManager.FEATURE_WATCH
327   CHECK(packageManager
328             ->hasSystemFeature(String16("android.hardware.type.watch"), 0,
329                                &hasFeature)
330             .isOk());
331   if (hasFeature) {
332     return DeviceType::WATCH;
333   }
334 
335   return DeviceType::PHONE;
336 }
337 
isMissingAidl(const std::string & packageName)338 static bool isMissingAidl(const std::string& packageName) {
339   static std::once_flag unionFlag;
340   static std::set<std::string> missingAidl = kAlwaysMissingAidl;
341 
342   std::call_once(unionFlag, [&]() {
343     const DeviceType type = getDeviceType();
344     switch (type) {
345       case DeviceType::AUTOMOTIVE:
346         missingAidl.insert(kPhoneOnlyAidl.begin(), kPhoneOnlyAidl.end());
347         missingAidl.insert(kTvOnlyAidl.begin(), kTvOnlyAidl.end());
348         break;
349       case DeviceType::TV:
350         missingAidl.insert(kAutomotiveOnlyAidl.begin(),
351                            kAutomotiveOnlyAidl.end());
352         missingAidl.insert(kRadioOnlyAidl.begin(), kRadioOnlyAidl.end());
353         break;
354       case DeviceType::WATCH:
355         missingAidl.insert(kAutomotiveOnlyAidl.begin(),
356                            kAutomotiveOnlyAidl.end());
357         missingAidl.insert(kPhoneOnlyAidl.begin(), kPhoneOnlyAidl.end());
358         missingAidl.insert(kTvOnlyAidl.begin(), kTvOnlyAidl.end());
359         break;
360       case DeviceType::PHONE:
361         missingAidl.insert(kAutomotiveOnlyAidl.begin(),
362                            kAutomotiveOnlyAidl.end());
363         missingAidl.insert(kTvOnlyAidl.begin(), kTvOnlyAidl.end());
364         break;
365       case DeviceType::UNKNOWN:
366         CHECK(false) << "getDeviceType return UNKNOWN type.";
367         break;
368     }
369   });
370 
371   return missingAidl.find(packageName) != missingAidl.end();
372 }
373 
allAidlManifestInterfaces()374 static std::vector<VersionedAidlPackage> allAidlManifestInterfaces() {
375   std::vector<VersionedAidlPackage> ret;
376   auto setInserter = [&](const vintf::ManifestInstance& i) -> bool {
377     if (i.format() != vintf::HalFormat::AIDL) {
378       return true;  // continue
379     }
380     ret.push_back({i.package() + "." + i.interface(), i.version().minorVer, 0});
381     return true;  // continue
382   };
383   vintf::VintfObject::GetDeviceHalManifest()->forEachInstance(setInserter);
384   vintf::VintfObject::GetFrameworkHalManifest()->forEachInstance(setInserter);
385   return ret;
386 }
387 
TEST(Hal,AllAidlInterfacesAreInAosp)388 TEST(Hal, AllAidlInterfacesAreInAosp) {
389   if (!kAidlUseUnfrozen) {
390     GTEST_SKIP() << "Not valid in 'next' configuration";
391   }
392   if (getDeviceType() != DeviceType::PHONE) {
393     GTEST_SKIP() << "Test only supports phones right now";
394   }
395   for (const auto& package : allAidlManifestInterfaces()) {
396     EXPECT_TRUE(isAospAidlInterface(package.name))
397         << "This device should only have AOSP interfaces, not: "
398         << package.name;
399   }
400 }
401 
402 struct AidlPackageCheck {
403   bool hasRegistration;
404   bool knownMissing;
405 };
406 
TEST(Hal,AidlInterfacesImplemented)407 TEST(Hal, AidlInterfacesImplemented) {
408   if (!kAidlUseUnfrozen) {
409     GTEST_SKIP() << "Not valid in 'next' configuration";
410   }
411   if (getDeviceType() != DeviceType::PHONE) {
412     GTEST_SKIP() << "Test only supports phones right now";
413   }
414   std::vector<VersionedAidlPackage> manifest = allAidlManifestInterfaces();
415   std::vector<VersionedAidlPackage> thoughtMissing = kKnownMissingAidl;
416 
417   for (const auto& treePackage : AidlInterfaceMetadata::all()) {
418     ASSERT_FALSE(treePackage.types.empty()) << treePackage.name;
419     if (std::none_of(treePackage.types.begin(), treePackage.types.end(),
420                      isAospAidlInterface) ||
421         isMissingAidl(treePackage.name)) {
422       continue;
423     }
424     if (treePackage.stability != "vintf") {
425       continue;
426     }
427 
428     // expect versions from 1 to latest version. If the package has development
429     // the latest version is the latest known version + 1. Each of these need
430     // to be checked for registration and knownMissing.
431     std::map<size_t, AidlPackageCheck> expectedVersions;
432     for (const auto version : treePackage.versions) {
433       expectedVersions[version] = {false, false};
434     }
435     if (treePackage.has_development) {
436       size_t version =
437           treePackage.versions.empty() ? 1 : *treePackage.versions.rbegin() + 1;
438       expectedVersions[version] = {false, false};
439     }
440 
441     // Check all types and versions defined by the package for registration.
442     // The package version is considered registered if any of those types are
443     // present in the manifest with the same version.
444     // The package version is considered known missing if it is found in
445     // thoughtMissing.
446     bool latestRegistered = false;
447     for (const std::string& type : treePackage.types) {
448       for (auto& [version, check] : expectedVersions) {
449         auto it = std::remove_if(
450             manifest.begin(), manifest.end(),
451             [&type, &ver = version](const VersionedAidlPackage& package) {
452               return package.name == type && package.version == ver;
453             });
454         if (it != manifest.end()) {
455           manifest.erase(it, manifest.end());
456           if (version == expectedVersions.rbegin()->first) {
457             latestRegistered = true;
458           }
459           check.hasRegistration = true;
460         }
461         it = std::remove_if(
462             thoughtMissing.begin(), thoughtMissing.end(),
463             [&type, &ver = version](const VersionedAidlPackage& package) {
464               return package.name == getAidlPackage(type) &&
465                      package.version == ver;
466             });
467         if (it != thoughtMissing.end()) {
468           thoughtMissing.erase(it, thoughtMissing.end());
469           check.knownMissing = true;
470         }
471       }
472     }
473 
474     if (!latestRegistered && !expectedVersions.rbegin()->second.knownMissing) {
475       ADD_FAILURE() << "The latest version ("
476                     << expectedVersions.rbegin()->first
477                     << ") of the module is not implemented: "
478                     << treePackage.name
479                     << " which declares the following types:\n    "
480                     << base::Join(treePackage.types, "\n    ");
481     }
482 
483     for (const auto& [version, check] : expectedVersions) {
484       if (check.knownMissing) {
485         if (check.hasRegistration) {
486           ADD_FAILURE() << "Package in missing list, but available: "
487                         << treePackage.name << " V" << version
488                         << " which declares the following types:\n    "
489                         << base::Join(treePackage.types, "\n    ");
490         }
491 
492         continue;
493       }
494     }
495   }
496 
497   for (const auto& package : thoughtMissing) {
498     ADD_FAILURE() << "Interface in missing list and cannot find it anywhere: "
499                   << package.name << " V" << package.version;
500   }
501 
502   for (const auto& package : manifest) {
503     ADD_FAILURE() << "Can't find manifest entry in tree: " << package.name
504                   << " version: " << package.version;
505   }
506 }
507