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