• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <aidl/metadata.h>
17 #include <android-base/logging.h>
18 #include <android-base/strings.h>
19 #include <gtest/gtest.h>
20 #include <hidl/metadata.h>
21 #include <hidl-util/FQName.h>
22 #include <vintf/VintfObject.h>
23 
24 using namespace android;
25 
26 static const std::set<std::string> kKnownMissingHidl = {
27     "android.frameworks.bufferhub@1.0",
28     "android.frameworks.cameraservice.device@2.1",
29     "android.frameworks.schedulerservice@1.0", // deprecated, see b/37226359
30     "android.frameworks.vr.composer@1.0",
31     "android.frameworks.vr.composer@2.0",
32     "android.frameworks.automotive.display@1.0",
33     "android.frameworks.stats@1.0",  // converted to AIDL, see b/177667419
34     "android.hardware.audio@2.0",
35     "android.hardware.audio@4.0",
36     "android.hardware.audio@5.0",
37     "android.hardware.audio@7.0",
38     "android.hardware.audio.effect@2.0",
39     "android.hardware.audio.effect@4.0",
40     "android.hardware.audio.effect@5.0",
41     "android.hardware.audio.effect@7.0",
42     "android.hardware.automotive.audiocontrol@1.0",
43     "android.hardware.automotive.audiocontrol@2.0",
44     "android.hardware.automotive.can@1.0",
45     "android.hardware.automotive.evs@1.1",
46     "android.hardware.automotive.sv@1.0",
47     "android.hardware.automotive.vehicle@2.0",
48     "android.hardware.biometrics.fingerprint@2.3",
49     "android.hardware.bluetooth.a2dp@1.0",
50     "android.hardware.broadcastradio@1.1",
51     "android.hardware.broadcastradio@2.0",
52     "android.hardware.cas.native@1.0",
53     "android.hardware.confirmationui@1.0",
54     "android.hardware.configstore@1.1", // deprecated, see b/149050985, b/149050733
55     "android.hardware.fastboot@1.1",
56     "android.hardware.gnss.measurement_corrections@1.1", // is sub-interface of gnss
57     "android.hardware.gnss.visibility_control@1.0",
58     "android.hardware.graphics.allocator@2.0",
59     "android.hardware.graphics.allocator@3.0",
60     "android.hardware.graphics.bufferqueue@1.0",
61     "android.hardware.graphics.bufferqueue@2.0",
62     "android.hardware.graphics.composer@2.4",
63     "android.hardware.graphics.mapper@2.1",
64     "android.hardware.graphics.mapper@3.0",
65     "android.hardware.health.storage@1.0", // converted to AIDL, see b/177470478
66     "android.hardware.ir@1.0",
67     "android.hardware.keymaster@3.0",
68     "android.hardware.keymaster@4.1", // Replaced by KeyMint
69     "android.hardware.light@2.0",
70     "android.hardware.media.bufferpool@1.0",
71     "android.hardware.media.bufferpool@2.0",
72     "android.hardware.memtrack@1.0",
73     "android.hardware.nfc@1.2",
74     "android.hardware.oemlock@1.0",
75     "android.hardware.power@1.3",
76     "android.hardware.power.stats@1.0",
77     "android.hardware.radio.deprecated@1.0",
78     "android.hardware.renderscript@1.0",
79     "android.hardware.soundtrigger@2.3",
80     "android.hardware.secure_element@1.2",
81     "android.hardware.sensors@1.0",
82     "android.hardware.tetheroffload.config@1.0",
83     "android.hardware.tetheroffload.control@1.1", // see b/170699770
84     "android.hardware.thermal@1.1",
85     "android.hardware.tv.cec@1.1",
86     "android.hardware.tv.input@1.0",
87     "android.hardware.tv.tuner@1.1",
88     "android.hardware.usb@1.3",
89     "android.hardware.usb.gadget@1.2",
90     "android.hardware.vibrator@1.3",
91     "android.hardware.vr@1.0",
92     "android.hardware.weaver@1.0",
93     "android.hardware.wifi@1.5",
94     "android.hardware.wifi.hostapd@1.3",
95     "android.hardware.wifi.offload@1.0",
96     "android.hidl.base@1.0",
97     "android.hidl.memory.token@1.0",
98 };
99 
100 static const std::set<std::string> kKnownMissingAidl = {
101     // types-only packages, which never expect a default implementation
102     "android.hardware.biometrics.common.",
103     "android.hardware.common.",
104     "android.hardware.common.fmq.",
105     "android.hardware.graphics.common.",
106 
107     // These KeyMaster types are in an AIDL types-only HAL because they're used
108     // by the Identity Credential AIDL HAL. Remove this when fully porting
109     // KeyMaster to AIDL.
110     "android.hardware.keymaster.",
111 
112     // These types are only used in Automotive.
113     "android.automotive.computepipe.registry.",
114     "android.automotive.computepipe.runner.",
115     "android.automotive.watchdog.",
116     "android.frameworks.automotive.powerpolicy.",
117     "android.frameworks.automotive.telemetry.",
118     "android.hardware.automotive.audiocontrol.",
119     "android.hardware.automotive.occupant_awareness.",
120 };
121 
122 // AOSP packages which are never considered
isHidlPackageConsidered(const FQName & name)123 static bool isHidlPackageConsidered(const FQName& name) {
124     static std::vector<std::string> gAospExclude = {
125         // packages not implemented now that we never expect to be implemented
126         "android.hardware.tests",
127         // packages not registered with hwservicemanager, usually sub-interfaces
128         "android.hardware.camera.device",
129     };
130     for (const std::string& package : gAospExclude) {
131         if (name.inPackage(package)) {
132             return false;
133         }
134     }
135     return true;
136 }
137 
isAospHidlInterface(const FQName & name)138 static bool isAospHidlInterface(const FQName& name) {
139     static const std::vector<std::string> kAospPackages = {
140         "android.hidl",
141         "android.hardware",
142         "android.frameworks",
143         "android.system",
144     };
145     for (const std::string& package : kAospPackages) {
146         if (name.inPackage(package)) {
147             return true;
148         }
149     }
150     return false;
151 }
152 
allTreeHidlInterfaces()153 static std::set<FQName> allTreeHidlInterfaces() {
154     std::set<FQName> ret;
155     for (const auto& iface : HidlInterfaceMetadata::all()) {
156         FQName f;
157         CHECK(f.setTo(iface.name)) << iface.name;
158         ret.insert(f);
159     }
160     return ret;
161 }
162 
allHidlManifestInterfaces()163 static std::set<FQName> allHidlManifestInterfaces() {
164     std::set<FQName> ret;
165     auto setInserter = [&] (const vintf::ManifestInstance& i) -> bool {
166         if (i.format() != vintf::HalFormat::HIDL) {
167             return true;  // continue
168         }
169         ret.insert(i.getFqInstance().getFqName());
170         return true;  // continue
171     };
172     vintf::VintfObject::GetDeviceHalManifest()->forEachInstance(setInserter);
173     vintf::VintfObject::GetFrameworkHalManifest()->forEachInstance(setInserter);
174     return ret;
175 }
176 
isAospAidlInterface(const std::string & name)177 static bool isAospAidlInterface(const std::string& name) {
178     return base::StartsWith(name, "android.") &&
179         !base::StartsWith(name, "android.hardware.tests.") &&
180         !base::StartsWith(name, "android.aidl.tests");
181 }
182 
allAidlManifestInterfaces()183 static std::set<std::string> allAidlManifestInterfaces() {
184     std::set<std::string> ret;
185     auto setInserter = [&] (const vintf::ManifestInstance& i) -> bool {
186         if (i.format() != vintf::HalFormat::AIDL) {
187             return true;  // continue
188         }
189         ret.insert(i.package() + "." + i.interface());
190         return true;  // continue
191     };
192     vintf::VintfObject::GetDeviceHalManifest()->forEachInstance(setInserter);
193     vintf::VintfObject::GetFrameworkHalManifest()->forEachInstance(setInserter);
194     return ret;
195 }
196 
TEST(Hal,AllHidlInterfacesAreInAosp)197 TEST(Hal, AllHidlInterfacesAreInAosp) {
198     for (const FQName& name : allHidlManifestInterfaces()) {
199       EXPECT_TRUE(isAospHidlInterface(name))
200           << "This device should only have AOSP interfaces, not: "
201           << name.string();
202     }
203 }
204 
TEST(Hal,HidlInterfacesImplemented)205 TEST(Hal, HidlInterfacesImplemented) {
206     // instances -> major version -> minor versions
207     std::map<std::string, std::map<size_t, std::set<size_t>>> unimplemented;
208 
209     for (const FQName& f : allTreeHidlInterfaces()) {
210         if (!isAospHidlInterface(f)) continue;
211         if (!isHidlPackageConsidered(f)) continue;
212 
213         unimplemented[f.package()][f.getPackageMajorVersion()].insert(f.getPackageMinorVersion());
214     }
215 
216     // we'll be removing items from this which we know are missing
217     // in order to be left with those elements which we thought we
218     // knew were missing but are actually present
219     std::set<std::string> thoughtMissing = kKnownMissingHidl;
220 
221     for (const FQName& f : allHidlManifestInterfaces()) {
222         if (thoughtMissing.erase(f.getPackageAndVersion().string()) > 0) {
223              ADD_FAILURE() << "Instance in missing list, but available: " << f.string();
224         }
225 
226         std::set<size_t>& minors = unimplemented[f.package()][f.getPackageMajorVersion()];
227         size_t minor = f.getPackageMinorVersion();
228 
229         auto it = minors.find(minor);
230         if (it == minors.end()) continue;
231 
232         // if 1.2 is implemented, also considere 1.0, 1.1 implemented
233         minors.erase(minors.begin(), std::next(it));
234     }
235 
236     for (const auto& [package, minorsPerMajor] : unimplemented) {
237         for (const auto& [major, minors] : minorsPerMajor) {
238             if (minors.empty()) continue;
239 
240             size_t maxMinor = *minors.rbegin();
241 
242             FQName missing;
243             ASSERT_TRUE(missing.setTo(package, major, maxMinor));
244 
245             if (thoughtMissing.erase(missing.string()) > 0) continue;
246 
247             ADD_FAILURE() << "Missing implementation from " << missing.string();
248         }
249     }
250 
251     for (const std::string& missing : thoughtMissing) {
252         ADD_FAILURE() << "Instance in missing list and cannot find it anywhere: " << missing
253                   << " (multiple versions in missing list?)";
254     }
255 }
256 
TEST(Hal,AllAidlInterfacesAreInAosp)257 TEST(Hal, AllAidlInterfacesAreInAosp) {
258     for (const std::string& name : allAidlManifestInterfaces()) {
259       EXPECT_TRUE(isAospAidlInterface(name))
260           << "This device should only have AOSP interfaces, not: " << name;
261     }
262 }
263 
264 // android.hardware.foo.IFoo -> android.hardware.foo.
getAidlPackage(const std::string & aidlType)265 std::string getAidlPackage(const std::string& aidlType) {
266     size_t lastDot = aidlType.rfind('.');
267     CHECK(lastDot != std::string::npos);
268     return aidlType.substr(0, lastDot + 1);
269 }
270 
TEST(Hal,AidlInterfacesImplemented)271 TEST(Hal, AidlInterfacesImplemented) {
272     std::set<std::string> manifest = allAidlManifestInterfaces();
273     std::set<std::string> thoughtMissing = kKnownMissingAidl;
274 
275     for (const auto& iface : AidlInterfaceMetadata::all()) {
276         ASSERT_FALSE(iface.types.empty()) << iface.name;  // sanity
277         if (std::none_of(iface.types.begin(), iface.types.end(), isAospAidlInterface)) continue;
278         if (iface.stability != "vintf") continue;
279 
280         bool hasRegistration = false;
281         bool knownMissing = false;
282         for (const std::string& type : iface.types) {
283             if (manifest.erase(type) > 0) hasRegistration = true;
284             if (thoughtMissing.erase(getAidlPackage(type)) > 0)  knownMissing = true;
285         }
286 
287         if (knownMissing) {
288             if (hasRegistration) {
289                 ADD_FAILURE() << "Interface in missing list, but available: " << iface.name
290                           << " which declares the following types:\n    "
291                           << base::Join(iface.types, "\n    ");
292             }
293 
294             continue;
295         }
296 
297         EXPECT_TRUE(hasRegistration) << iface.name << " which declares the following types:\n    "
298             << base::Join(iface.types, "\n    ");
299     }
300 
301     for (const std::string& iface : thoughtMissing) {
302         ADD_FAILURE() << "Interface in manifest list and cannot find it anywhere: " << iface;
303     }
304 
305     for (const std::string& iface : manifest) {
306         ADD_FAILURE() << "Can't find manifest entry in tree: " << iface;
307     }
308 }
309