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