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