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   std::vector<HidlInstance> ret;
98   manifest->forEachInstance([manifest, &ret](const auto &manifest_instance) {
99     if (manifest_instance.format() == HalFormat::HIDL) {
100       ret.emplace_back(manifest_instance);
101     }
102     return true;  // continue to next instance
103   });
104   return ret;
105 }
106 
GetAidlInstances(const HalManifestPtr & manifest)107 std::vector<AidlInstance> VtsTrebleVintfTestBase::GetAidlInstances(
108     const HalManifestPtr &manifest) {
109   std::vector<AidlInstance> ret;
110   manifest->forEachInstance([manifest, &ret](const auto &manifest_instance) {
111     if (manifest_instance.format() == HalFormat::AIDL) {
112       ret.emplace_back(manifest_instance);
113     }
114     return true;  // continue to next instance
115   });
116   return ret;
117 }
118 
GetNativeInstances(const HalManifestPtr & manifest)119 std::vector<NativeInstance> VtsTrebleVintfTestBase::GetNativeInstances(
120     const HalManifestPtr &manifest) {
121   std::vector<NativeInstance> ret;
122   manifest->forEachInstance([manifest, &ret](const auto &manifest_instance) {
123     if (manifest_instance.format() == HalFormat::NATIVE) {
124       ret.emplace_back(manifest_instance);
125     }
126     return true;  // continue to next instance
127   });
128   return ret;
129 }
130 
GetHidlService(const FQName & fq_name,const string & instance_name,Transport transport,bool log)131 sp<IBase> VtsTrebleVintfTestBase::GetHidlService(const FQName &fq_name,
132                                                  const string &instance_name,
133                                                  Transport transport,
134                                                  bool log) {
135   return GetHidlService(fq_name.string(), instance_name, transport, log);
136 }
137 
GetHidlService(const string & fq_name,const string & instance_name,Transport transport,bool log)138 sp<IBase> VtsTrebleVintfTestBase::GetHidlService(const string &fq_name,
139                                                  const string &instance_name,
140                                                  Transport transport,
141                                                  bool log) {
142   using android::hardware::details::getRawServiceInternal;
143 
144   if (log) {
145     cout << "Getting: " << fq_name << "/" << instance_name << endl;
146   }
147 
148   // getService blocks until a service is available. In 100% of other cases
149   // where getService is used, it should be called directly. However, this test
150   // enforces that various services are actually available when they are
151   // declared, it must make a couple of precautions in case the service isn't
152   // actually available so that the proper failure can be reported.
153 
154   auto task = std::packaged_task<sp<IBase>()>([fq_name, instance_name]() {
155     return getRawServiceInternal(fq_name, instance_name, true /* retry */,
156                                  false /* getStub */);
157   });
158   int timeout_multiplier = base::GetIntProperty("ro.hw_timeout_multiplier", 1);
159   auto max_time = timeout_multiplier * std::chrono::seconds(1);
160 
161   std::future<sp<IBase>> future = task.get_future();
162   std::thread(std::move(task)).detach();
163   auto status = future.wait_for(max_time);
164 
165   if (status != std::future_status::ready) return nullptr;
166 
167   sp<IBase> base = future.get();
168   if (base == nullptr) return nullptr;
169 
170   bool wantRemote = transport == Transport::HWBINDER;
171   if (base->isRemote() != wantRemote) return nullptr;
172 
173   return base;
174 }
175 
GetAidlService(const string & name)176 sp<IBinder> VtsTrebleVintfTestBase::GetAidlService(const string &name) {
177   auto task = std::packaged_task<sp<IBinder>()>([name]() {
178     return defaultServiceManager()->waitForService(String16(name.c_str()));
179   });
180 
181   int timeout_multiplier = base::GetIntProperty("ro.hw_timeout_multiplier", 1);
182   auto max_time = timeout_multiplier * std::chrono::seconds(1);
183   auto future = task.get_future();
184   std::thread(std::move(task)).detach();
185   auto status = future.wait_for(max_time);
186 
187   return status == std::future_status::ready ? future.get() : nullptr;
188 }
189 
GetInstanceNames(const sp<IServiceManager> & manager,const FQName & fq_name)190 vector<string> VtsTrebleVintfTestBase::GetInstanceNames(
191     const sp<IServiceManager> &manager, const FQName &fq_name) {
192   vector<string> ret;
193   auto status =
194       manager->listByInterface(fq_name.string(), [&](const auto &out) {
195         for (const auto &e : out) ret.push_back(e);
196       });
197   EXPECT_TRUE(status.isOk()) << status.description();
198   return ret;
199 }
200 
GetInterfaceChain(const sp<IBase> & service)201 vector<string> VtsTrebleVintfTestBase::GetInterfaceChain(
202     const sp<IBase> &service) {
203   vector<string> iface_chain{};
204   service->interfaceChain([&iface_chain](const hidl_vec<hidl_string> &chain) {
205     for (const auto &iface_name : chain) {
206       iface_chain.push_back(iface_name);
207     }
208   });
209   return iface_chain;
210 }
211 
GetPartition(sp<IBase> hal_service)212 Partition VtsTrebleVintfTestBase::GetPartition(sp<IBase> hal_service) {
213   Partition partition = Partition::UNKNOWN;
214   auto ret = hal_service->getDebugInfo(
215       [&](const auto &info) { partition = PartitionOfProcess(info.pid); });
216   EXPECT_TRUE(ret.isOk());
217   return partition;
218 }
219 
GetDeclaredHidlHalsOfTransport(HalManifestPtr manifest,Transport transport)220 set<string> VtsTrebleVintfTestBase::GetDeclaredHidlHalsOfTransport(
221     HalManifestPtr manifest, Transport transport) {
222   EXPECT_THAT(transport,
223               AnyOf(Eq(Transport::HWBINDER), Eq(Transport::PASSTHROUGH)))
224       << "Unrecognized transport of HIDL: " << transport;
225   std::set<std::string> ret;
226   for (const auto &hidl_instance : GetHidlInstances(manifest)) {
227     if (hidl_instance.transport() != transport) {
228       continue;  // ignore
229     }
230 
231     // 1.n in manifest => 1.0, 1.1, ... 1.n are all served (if they exist)
232     FQName fq = hidl_instance.fq_name();
233     while (true) {
234       ret.insert(fq.string() + "/" + hidl_instance.instance_name());
235       if (fq.getPackageMinorVersion() <= 0) break;
236       fq = fq.downRev();
237     }
238   }
239   return ret;
240 }
241 
ListRegisteredHwbinderHals()242 std::vector<std::string> VtsTrebleVintfTestBase::ListRegisteredHwbinderHals() {
243   std::vector<std::string> return_value;
244   EXPECT_NE(default_manager(), nullptr);
245   if (default_manager() == nullptr) return {};
246   Return<void> ret = default_manager()->list([&](const auto &list) {
247     return_value.reserve(list.size());
248     for (const auto &s : list) return_value.push_back(s);
249   });
250   EXPECT_TRUE(ret.isOk());
251   return return_value;
252 }
253 
254 }  // namespace testing
255 }  // namespace vintf
256 }  // namespace android
257