1 /*
2  * Copyright (C) 2018 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 #ifndef VTS_TREBLE_VINTF_TEST_UTILS_H_
18 #define VTS_TREBLE_VINTF_TEST_UTILS_H_
19 #include <android/hidl/manager/1.0/IServiceManager.h>
20 #include <gtest/gtest.h>
21 #include <hidl-hash/Hash.h>
22 #include <hidl-util/FQName.h>
23 #include <hidl/HidlSupport.h>
24 #include <procpartition/procpartition.h>
25 #include <vintf/VintfObject.h>
26 #include <vintf/parse_string.h>
27 
28 #include <map>
29 #include <optional>
30 #include <set>
31 #include <string>
32 #include <vector>
33 
34 namespace android {
35 namespace vintf {
36 namespace testing {
37 
38 using android::FQName;
39 using android::Hash;
40 using android::sp;
41 using android::hardware::hidl_array;
42 using android::hardware::hidl_string;
43 using android::hardware::hidl_vec;
44 using android::hardware::Return;
45 using android::hidl::base::V1_0::IBase;
46 using android::hidl::manager::V1_0::IServiceManager;
47 using android::procpartition::Partition;
48 using android::vintf::HalManifest;
49 using android::vintf::Level;
50 using android::vintf::ManifestHal;
51 using android::vintf::ManifestInstance;
52 using android::vintf::RuntimeInfo;
53 using android::vintf::SchemaType;
54 using android::vintf::to_string;
55 using android::vintf::Transport;
56 using android::vintf::Version;
57 using android::vintf::VintfObject;
58 
59 using std::cout;
60 using std::endl;
61 using std::map;
62 using std::multimap;
63 using std::optional;
64 using std::ostream;
65 using std::set;
66 using std::string;
67 using std::vector;
68 
69 // Wrapper of ManifestInstance that hides details irrelevant to HIDL.
70 struct HidlInstance : private ManifestInstance {
71  public:
72   HidlInstance(const ManifestInstance& other);
73   HidlInstance(const HidlInstance&) = default;
74   HidlInstance(HidlInstance&&) = default;
fq_nameHidlInstance75   FQName fq_name() const {
76     return FQName{package(), to_string(version()), interface()};
77   }
instance_nameHidlInstance78   string instance_name() const { return instance(); };
transportHidlInstance79   Transport transport() const { return ManifestInstance::transport(); }
80 
81   string test_case_name() const;
82 };
83 ostream& operator<<(ostream& os, const HidlInstance& val);
84 
85 // Wrapper of ManifestInstance that hides details irrelevant to AIDL.
86 struct AidlInstance : private ManifestInstance {
87  public:
88   AidlInstance(const ManifestInstance& other);
89   AidlInstance(const AidlInstance&) = default;
90   AidlInstance(AidlInstance&&) = default;
packageAidlInstance91   string package() const { return ManifestInstance::package(); }
versionAidlInstance92   uint64_t version() const { return ManifestInstance::version().minorVer; }
interfaceAidlInstance93   string interface() const { return ManifestInstance::interface(); }
instanceAidlInstance94   string instance() const { return ManifestInstance::instance(); }
updatable_via_apexAidlInstance95   std::optional<string> updatable_via_apex() const {
96     return ManifestInstance::updatableViaApex();
97   }
updatable_via_systemAidlInstance98   bool updatable_via_system() const {
99     return ManifestInstance::updatableViaSystem();
100   }
101 
102   string test_case_name() const;
103 };
104 ostream& operator<<(ostream& os, const AidlInstance& val);
105 
106 struct NativeInstance : private ManifestInstance {
107  public:
108   NativeInstance(const ManifestInstance& other);
109   NativeInstance(const NativeInstance&) = default;
110   NativeInstance(NativeInstance&&) = default;
111 
packageNativeInstance112   string package() const { return ManifestInstance::package(); }
minor_versionNativeInstance113   uint64_t minor_version() const {
114     return ManifestInstance::version().minorVer;
115   }
major_versionNativeInstance116   uint64_t major_version() const {
117     return ManifestInstance::version().majorVer;
118   }
interfaceNativeInstance119   string interface() const { return ManifestInstance::interface(); }
instanceNativeInstance120   string instance() const { return ManifestInstance::instance(); }
121 
122   string test_case_name() const;
123 };
124 ostream& operator<<(ostream& os, const NativeInstance& val);
125 
126 // Sanitize a string so it can be used as a test case name.
127 std::string SanitizeTestCaseName(std::string original);
128 
129 // Print test case name for SingleHidlTest and SingleAidlTest
130 template <typename Test>
GetTestCaseSuffix(const::testing::TestParamInfo<typename Test::ParamType> & info)131 std::string GetTestCaseSuffix(
132     const ::testing::TestParamInfo<typename Test::ParamType>& info) {
133   const auto& instance = std::get<0>(info.param);
134   return instance.test_case_name() + "_" + std::to_string(info.index);
135 }
136 
137 using HashCharArray = hidl_array<unsigned char, 32>;
138 using HalManifestPtr = std::shared_ptr<const HalManifest>;
139 using MatrixPtr = std::shared_ptr<const CompatibilityMatrix>;
140 using RuntimeInfoPtr = std::shared_ptr<const RuntimeInfo>;
141 
142 // Path to directory on target containing test data.
143 extern const string kDataDir;
144 // Name of file containing HAL hashes.
145 extern const string kHashFileName;
146 // Map from package name to package root.
147 extern const map<string, string> kPackageRoot;
148 // HALs that are allowed to be passthrough under Treble rules.
149 extern const set<string> kPassthroughHals;
150 
151 // Read ro.vendor.api_level
152 // See `property_initialize_ro_vendor_api_level()` for details on how
153 // this is calculated. In system/core/init/property_service.cpp.
154 uint64_t GetVendorApiLevel();
155 
156 // For a given interface returns package root if known. Returns empty string
157 // otherwise.
158 const string PackageRoot(const FQName& fq_iface_name);
159 
160 // Returns true iff HAL interface is Android platform.
161 bool IsAndroidPlatformInterface(const FQName& fq_iface_name);
162 
163 // Returns the set of released hashes for a given HAL interface.
164 set<string> ReleasedHashes(const FQName& fq_iface_name);
165 
166 // Returns the partition that a HAL is associated with.
167 Partition PartitionOfProcess(int32_t pid);
168 
169 // Returns SYSTEM for FRAMEWORK, VENDOR for DEVICE.
170 Partition PartitionOfType(SchemaType type);
171 
172 }  // namespace testing
173 }  // namespace vintf
174 
175 // Allows GTest to print pointers with a human readable string.
176 template <typename T>
PrintTo(const sp<T> & v,std::ostream * os)177 void PrintTo(const sp<T>& v, std::ostream* os) {
178   *os << android::hardware::details::toHexString<uintptr_t>(
179       reinterpret_cast<uintptr_t>(&*v), true /* prefix */);
180 }
181 template <typename T>
PrintTo(const T * v,std::ostream * os)182 void PrintTo(const T* v, std::ostream* os) {
183   *os << android::hardware::details::toHexString<uintptr_t>(
184       reinterpret_cast<uintptr_t>(v), true /* prefix */);
185 }
186 
187 }  // namespace android
188 
189 // Allows GTest to print pointers with a human readable string.
190 namespace std {
191 void PrintTo(const android::vintf::testing::HalManifestPtr& v, ostream* os);
192 template <typename T>
PrintTo(const T * v,ostream * os)193 void PrintTo(const T* v, ostream* os) {
194   *os << android::hardware::details::toHexString<uintptr_t>(
195       reinterpret_cast<uintptr_t>(v), true /* prefix */);
196 }
197 }  // namespace std
198 
199 #endif  // VTS_TREBLE_VINTF_TEST_UTILS_H_
200