1 /*
2 * Copyright (C) 2017 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 "VtsTrebleVintfTestBase.h"
18
19 #include <chrono>
20 #include <condition_variable>
21 #include <functional>
22 #include <future>
23 #include <iostream>
24 #include <map>
25 #include <mutex>
26 #include <set>
27 #include <sstream>
28 #include <string>
29 #include <thread>
30 #include <vector>
31
32 #include <android-base/logging.h>
33 #include <android-base/strings.h>
34 #include <android/hidl/manager/1.0/IServiceManager.h>
35 #include <gtest/gtest.h>
36 #include <hidl-hash/Hash.h>
37 #include <hidl-util/FQName.h>
38 #include <hidl-util/FqInstance.h>
39 #include <hidl/HidlTransportUtils.h>
40 #include <hidl/ServiceManagement.h>
41 #include <procpartition/procpartition.h>
42 #include <vintf/HalManifest.h>
43 #include <vintf/VintfObject.h>
44 #include <vintf/parse_string.h>
45
46 #include "SingleManifestTest.h"
47 #include "utils.h"
48
49 namespace android {
50 namespace vintf {
51 namespace testing {
52
53 using android::FqInstance;
54 using android::FQName;
55 using android::Hash;
56 using android::sp;
57 using android::hardware::hidl_array;
58 using android::hardware::hidl_string;
59 using android::hardware::hidl_vec;
60 using android::hardware::Return;
61 using android::hidl::base::V1_0::IBase;
62 using android::hidl::manager::V1_0::IServiceManager;
63 using android::procpartition::Partition;
64 using android::vintf::HalManifest;
65 using android::vintf::Level;
66 using android::vintf::ManifestHal;
67 using android::vintf::Transport;
68 using android::vintf::Version;
69 using android::vintf::VintfObject;
70 using android::vintf::operator<<;
71 using android::vintf::to_string;
72 using android::vintf::toFQNameString;
73
74 using std::cout;
75 using std::endl;
76 using std::map;
77 using std::set;
78 using std::string;
79 using std::vector;
80
SetUp()81 void VtsTrebleVintfTestBase::SetUp() {
82 default_manager_ = ::android::hardware::defaultServiceManager();
83 ASSERT_NE(default_manager_, nullptr)
84 << "Failed to get default service manager." << endl;
85 }
86
ForEachHidlHalInstance(const HalManifestPtr & manifest,HidlVerifyFn fn)87 void VtsTrebleVintfTestBase::ForEachHidlHalInstance(
88 const HalManifestPtr &manifest, HidlVerifyFn fn) {
89 manifest->forEachInstance([manifest, fn](const auto &manifest_instance) {
90 if (manifest_instance.format() != HalFormat::HIDL) {
91 return true; // continue to next instance
92 }
93 const FQName fq_name{manifest_instance.package(),
94 to_string(manifest_instance.version()),
95 manifest_instance.interface()};
96 const Transport transport = manifest_instance.transport();
97 const std::string instance_name = manifest_instance.instance();
98
99 auto future_result =
100 std::async([&]() { fn(fq_name, instance_name, transport); });
101 auto timeout = std::chrono::seconds(1);
102 std::future_status status = future_result.wait_for(timeout);
103 if (status != std::future_status::ready) {
104 cout << "Timed out on: " << fq_name.string() << " " << instance_name
105 << endl;
106 }
107 return true; // continue to next instance
108 });
109 }
110
ForEachAidlHalInstance(const HalManifestPtr & manifest,AidlVerifyFn fn)111 void VtsTrebleVintfTestBase::ForEachAidlHalInstance(
112 const HalManifestPtr &manifest, AidlVerifyFn fn) {
113 manifest->forEachInstance([manifest, fn](const auto &manifest_instance) {
114 if (manifest_instance.format() != HalFormat::AIDL) {
115 return true; // continue to next instance
116 }
117 const std::string &package = manifest_instance.package();
118 uint64_t version = manifest_instance.version().minorVer;
119 const std::string &interface = manifest_instance.interface();
120 const std::string &instance = manifest_instance.instance();
121 const std::optional<std::string> &updatable_via_apex =
122 manifest_instance.updatableViaApex();
123
124 auto future_result = std::async([&]() {
125 fn(package, version, interface, instance, updatable_via_apex);
126 });
127 auto timeout = std::chrono::seconds(1);
128 std::future_status status = future_result.wait_for(timeout);
129 if (status != std::future_status::ready) {
130 cout << "Timed out on: " << package << "." << interface << "/" << instance
131 << endl;
132 }
133 return true; // continue to next instance
134 });
135 }
136
GetHalService(const FQName & fq_name,const string & instance_name,Transport transport,bool log)137 sp<IBase> VtsTrebleVintfTestBase::GetHalService(const FQName &fq_name,
138 const string &instance_name,
139 Transport transport, bool log) {
140 return GetHalService(fq_name.string(), instance_name, transport, log);
141 }
142
GetHalService(const string & fq_name,const string & instance_name,Transport transport,bool log)143 sp<IBase> VtsTrebleVintfTestBase::GetHalService(const string &fq_name,
144 const string &instance_name,
145 Transport transport, bool log) {
146 using android::hardware::details::getRawServiceInternal;
147
148 if (log) {
149 cout << "Getting: " << fq_name << "/" << instance_name << endl;
150 }
151
152 // getService blocks until a service is available. In 100% of other cases
153 // where getService is used, it should be called directly. However, this test
154 // enforces that various services are actually available when they are
155 // declared, it must make a couple of precautions in case the service isn't
156 // actually available so that the proper failure can be reported.
157
158 auto task = std::packaged_task<sp<IBase>()>([fq_name, instance_name]() {
159 return getRawServiceInternal(fq_name, instance_name, true /* retry */,
160 false /* getStub */);
161 });
162 auto max_time = std::chrono::seconds(1);
163
164 std::future<sp<IBase>> future = task.get_future();
165 std::thread(std::move(task)).detach();
166 auto status = future.wait_for(max_time);
167
168 if (status != std::future_status::ready) return nullptr;
169
170 sp<IBase> base = future.get();
171 if (base == nullptr) return nullptr;
172
173 bool wantRemote = transport == Transport::HWBINDER;
174 if (base->isRemote() != wantRemote) return nullptr;
175
176 return base;
177 }
178
GetInstanceNames(const sp<IServiceManager> & manager,const FQName & fq_name)179 vector<string> VtsTrebleVintfTestBase::GetInstanceNames(
180 const sp<IServiceManager> &manager, const FQName &fq_name) {
181 vector<string> ret;
182 auto status =
183 manager->listByInterface(fq_name.string(), [&](const auto &out) {
184 for (const auto &e : out) ret.push_back(e);
185 });
186 EXPECT_TRUE(status.isOk()) << status.description();
187 return ret;
188 }
189
GetInterfaceChain(const sp<IBase> & service)190 vector<string> VtsTrebleVintfTestBase::GetInterfaceChain(
191 const sp<IBase> &service) {
192 vector<string> iface_chain{};
193 service->interfaceChain([&iface_chain](const hidl_vec<hidl_string> &chain) {
194 for (const auto &iface_name : chain) {
195 iface_chain.push_back(iface_name);
196 }
197 });
198 return iface_chain;
199 }
200
GetPartition(sp<IBase> hal_service)201 Partition VtsTrebleVintfTestBase::GetPartition(sp<IBase> hal_service) {
202 Partition partition = Partition::UNKNOWN;
203 auto ret = hal_service->getDebugInfo(
204 [&](const auto &info) { partition = PartitionOfProcess(info.pid); });
205 EXPECT_TRUE(ret.isOk());
206 return partition;
207 }
208
GetPassthroughHals(HalManifestPtr manifest)209 set<string> VtsTrebleVintfTestBase::GetPassthroughHals(
210 HalManifestPtr manifest) {
211 std::set<std::string> manifest_passthrough_hals_;
212
213 auto add_manifest_hals = [&manifest_passthrough_hals_](
214 const FQName &fq_name,
215 const string &instance_name,
216 Transport transport) {
217 if (transport == Transport::HWBINDER) {
218 // ignore
219 } else if (transport == Transport::PASSTHROUGH) {
220 // 1.n in manifest => 1.0, 1.1, ... 1.n are all served (if they exist)
221 FQName fq = fq_name;
222 while (true) {
223 manifest_passthrough_hals_.insert(fq.string() + "/" + instance_name);
224 if (fq.getPackageMinorVersion() <= 0) break;
225 fq = fq.downRev();
226 }
227 } else {
228 ADD_FAILURE() << "Unrecognized transport: " << transport;
229 }
230 };
231 ForEachHidlHalInstance(manifest, add_manifest_hals);
232 return manifest_passthrough_hals_;
233 }
234
GetHwbinderHals(HalManifestPtr manifest)235 set<string> VtsTrebleVintfTestBase::GetHwbinderHals(HalManifestPtr manifest) {
236 std::set<std::string> manifest_hwbinder_hals_;
237
238 auto add_manifest_hals = [&manifest_hwbinder_hals_](
239 const FQName &fq_name,
240 const string &instance_name,
241 Transport transport) {
242 if (transport == Transport::HWBINDER) {
243 // 1.n in manifest => 1.0, 1.1, ... 1.n are all served (if they exist)
244 FQName fq = fq_name;
245 while (true) {
246 manifest_hwbinder_hals_.insert(fq.string() + "/" + instance_name);
247 if (fq.getPackageMinorVersion() <= 0) break;
248 fq = fq.downRev();
249 }
250 } else if (transport == Transport::PASSTHROUGH) {
251 // ignore
252 } else {
253 ADD_FAILURE() << "Unrecognized transport: " << transport;
254 }
255 };
256 ForEachHidlHalInstance(manifest, add_manifest_hals);
257 return manifest_hwbinder_hals_;
258 }
259
260 } // namespace testing
261 } // namespace vintf
262 } // namespace android
263