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/Vintf.h>
18 
19 #include <android-base/logging.h>
20 #include <android-base/strings.h>
21 #include <gtest/gtest.h>
22 #include <vintf/VintfObject.h>
23 
24 namespace android {
25 
26 static std::vector<std::string> gUnimplementedInterfaces;
27 
28 // b/290539746. getAidlHalInstanceNames is usually used for parameters
29 // to create parameterized tests, so if it returns an empty list, then
30 // oftentimes the test will report no test results. This is confusing
31 // and it appears like a test error.
32 //
33 // Due to translation units defining other tests that will be instantiated
34 // in another order, we can't instantiate a test suite based on the set
35 // of unimplemented interfaces, so we can only have one test which shows
36 // the result.
TEST(AidlTestHelper,CheckNoUnimplementedInterfaces)37 TEST(AidlTestHelper, CheckNoUnimplementedInterfaces) {
38     if (gUnimplementedInterfaces.empty()) {
39         // all interfaces are implemented, so no error
40         return;
41     }
42 
43     GTEST_SKIP()
44         << "These interfaces are unimplemented on this device, so other tests may be skipped: "
45         << android::base::Join(gUnimplementedInterfaces, ", ");
46 }
47 
getAidlHalInstanceNames(const std::string & descriptor)48 std::vector<std::string> getAidlHalInstanceNames(const std::string& descriptor) {
49     size_t lastDot = descriptor.rfind('.');
50     CHECK(lastDot != std::string::npos) << "Invalid descriptor: " << descriptor;
51     const std::string package = descriptor.substr(0, lastDot);
52     const std::string iface = descriptor.substr(lastDot + 1);
53 
54     std::vector<std::string> ret;
55 
56     auto deviceManifest = vintf::VintfObject::GetDeviceHalManifest();
57     for (const std::string& instance : deviceManifest->getAidlInstances(package, iface)) {
58         ret.push_back(descriptor + "/" + instance);
59     }
60 
61     auto frameworkManifest = vintf::VintfObject::GetFrameworkHalManifest();
62     for (const std::string& instance : frameworkManifest->getAidlInstances(package, iface)) {
63         ret.push_back(descriptor + "/" + instance);
64     }
65 
66     if (ret.size() == 0) {
67         std::cerr << "WARNING: There are no instances of AIDL service '" << descriptor
68                   << "' declared on this device." << std::endl;
69 
70         gUnimplementedInterfaces.push_back(descriptor);
71     }
72 
73     return ret;
74 }
75 
getAidlHalInstanceNames(const String16 & descriptor)76 std::vector<std::string> getAidlHalInstanceNames(const String16& descriptor) {
77     return getAidlHalInstanceNames(String8(descriptor).c_str());
78 }
79 
80 }  // namespace android
81