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   }
98 
99   string test_case_name() const;
100 };
101 ostream& operator<<(ostream& os, const AidlInstance& val);
102 
103 struct NativeInstance : private ManifestInstance {
104  public:
105   NativeInstance(const ManifestInstance& other);
106   NativeInstance(const NativeInstance&) = default;
107   NativeInstance(NativeInstance&&) = default;
108 
packageNativeInstance109   string package() const { return ManifestInstance::package(); }
minor_versionNativeInstance110   uint64_t minor_version() const {
111     return ManifestInstance::version().minorVer;
112   }
major_versionNativeInstance113   uint64_t major_version() const {
114     return ManifestInstance::version().majorVer;
115   }
interfaceNativeInstance116   string interface() const { return ManifestInstance::interface(); }
instanceNativeInstance117   string instance() const { return ManifestInstance::instance(); }
118 
119   string test_case_name() const;
120 };
121 ostream& operator<<(ostream& os, const NativeInstance& val);
122 
123 // Sanitize a string so it can be used as a test case name.
124 std::string SanitizeTestCaseName(std::string original);
125 
126 // Print test case name for SingleHidlTest and SingleAidlTest
127 template <typename Test>
GetTestCaseSuffix(const::testing::TestParamInfo<typename Test::ParamType> & info)128 std::string GetTestCaseSuffix(
129     const ::testing::TestParamInfo<typename Test::ParamType>& info) {
130   const auto& instance = std::get<0>(info.param);
131   return instance.test_case_name() + "_" + std::to_string(info.index);
132 }
133 
134 using HashCharArray = hidl_array<unsigned char, 32>;
135 using HalManifestPtr = std::shared_ptr<const HalManifest>;
136 using MatrixPtr = std::shared_ptr<const CompatibilityMatrix>;
137 using RuntimeInfoPtr = std::shared_ptr<const RuntimeInfo>;
138 
139 // Path to directory on target containing test data.
140 extern const string kDataDir;
141 // Name of file containing HAL hashes.
142 extern const string kHashFileName;
143 // Map from package name to package root.
144 extern const map<string, string> kPackageRoot;
145 // HALs that are allowed to be passthrough under Treble rules.
146 extern const set<string> kPassthroughHals;
147 
148 // Read ro.vendor.api_level, that shows the minimum of the following two
149 // values:
150 // * First non-empty value for the board api level from the following
151 // properties:
152 // -- ro.board.api_level
153 // -- ro.board.first_api_level
154 // -- ro.vendor.build.version.sdk
155 // * First non-empty value for the device api level from the following
156 // properties:
157 // -- ro.product.first_api_level
158 // -- ro.build.version.sdk
159 uint64_t GetBoardApiLevel();
160 
161 // For a given interface returns package root if known. Returns empty string
162 // otherwise.
163 const string PackageRoot(const FQName& fq_iface_name);
164 
165 // Returns true iff HAL interface is Android platform.
166 bool IsAndroidPlatformInterface(const FQName& fq_iface_name);
167 
168 // Returns the set of released hashes for a given HAL interface.
169 set<string> ReleasedHashes(const FQName& fq_iface_name);
170 
171 // Returns the partition that a HAL is associated with.
172 Partition PartitionOfProcess(int32_t pid);
173 
174 // Returns SYSTEM for FRAMEWORK, VENDOR for DEVICE.
175 Partition PartitionOfType(SchemaType type);
176 
177 }  // namespace testing
178 }  // namespace vintf
179 
180 // Allows GTest to print pointers with a human readable string.
181 template <typename T>
PrintTo(const sp<T> & v,std::ostream * os)182 void PrintTo(const sp<T>& v, std::ostream* os) {
183   *os << android::hardware::details::toHexString<uintptr_t>(
184       reinterpret_cast<uintptr_t>(&*v), true /* prefix */);
185 }
186 template <typename T>
PrintTo(const T * v,std::ostream * os)187 void PrintTo(const T* v, std::ostream* os) {
188   *os << android::hardware::details::toHexString<uintptr_t>(
189       reinterpret_cast<uintptr_t>(v), true /* prefix */);
190 }
191 
192 }  // namespace android
193 
194 // Allows GTest to print pointers with a human readable string.
195 namespace std {
196 void PrintTo(const android::vintf::testing::HalManifestPtr& v, ostream* os);
197 template <typename T>
PrintTo(const T * v,ostream * os)198 void PrintTo(const T* v, ostream* os) {
199   *os << android::hardware::details::toHexString<uintptr_t>(
200       reinterpret_cast<uintptr_t>(v), true /* prefix */);
201 }
202 }  // namespace std
203 
204 #endif  // VTS_TREBLE_VINTF_TEST_UTILS_H_
205