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 <map>
20 #include <set>
21 #include <string>
22 #include <vector>
23 
24 #include <android/hidl/manager/1.0/IServiceManager.h>
25 #include <hidl-hash/Hash.h>
26 #include <hidl-util/FQName.h>
27 #include <hidl/HidlSupport.h>
28 #include <procpartition/procpartition.h>
29 #include <vintf/VintfObject.h>
30 #include <vintf/parse_string.h>
31 
32 namespace android {
33 namespace vintf {
34 namespace testing {
35 
36 using android::FQName;
37 using android::Hash;
38 using android::sp;
39 using android::hardware::hidl_array;
40 using android::hardware::hidl_string;
41 using android::hardware::hidl_vec;
42 using android::hardware::Return;
43 using android::hidl::base::V1_0::IBase;
44 using android::hidl::manager::V1_0::IServiceManager;
45 using android::procpartition::Partition;
46 using android::vintf::HalManifest;
47 using android::vintf::Level;
48 using android::vintf::ManifestHal;
49 using android::vintf::RuntimeInfo;
50 using android::vintf::SchemaType;
51 using android::vintf::to_string;
52 using android::vintf::Transport;
53 using android::vintf::Version;
54 using android::vintf::VintfObject;
55 
56 using std::cout;
57 using std::endl;
58 using std::map;
59 using std::multimap;
60 using std::set;
61 using std::string;
62 
63 using std::vector;
64 
65 using HidlVerifyFn = std::function<void(
66     const FQName& fq_name, const string& instance_name, Transport)>;
67 using AidlVerifyFn =
68     std::function<void(const std::string& package, const std::string& name,
69                        const std::string& instance)>;
70 using HashCharArray = hidl_array<unsigned char, 32>;
71 using HalManifestPtr = std::shared_ptr<const HalManifest>;
72 using MatrixPtr = std::shared_ptr<const CompatibilityMatrix>;
73 using RuntimeInfoPtr = std::shared_ptr<const RuntimeInfo>;
74 
75 // Path to directory on target containing test data.
76 extern const string kDataDir;
77 // Name of file containing HAL hashes.
78 extern const string kHashFileName;
79 // Map from package name to package root.
80 extern const map<string, string> kPackageRoot;
81 // HALs that are allowed to be passthrough under Treble rules.
82 extern const set<string> kPassthroughHals;
83 
84 extern const map<size_t /* Shipping API Level */, Level /* FCM Version */>
85     kFcm2ApiLevelMap;
86 
87 // Returns ro.product.first_api_level if it is defined and not 0. Returns
88 // ro.build.version.sdk otherwise.
89 uint64_t GetShippingApiLevel();
90 
91 // For a given interface returns package root if known. Returns empty string
92 // otherwise.
93 const string PackageRoot(const FQName& fq_iface_name);
94 
95 // Returns true iff HAL interface is Android platform.
96 bool IsAndroidPlatformInterface(const FQName& fq_iface_name);
97 
98 // Returns true iff the device has the specified feature.
99 bool DeviceSupportsFeature(const char* feature);
100 
101 // Returns the set of released hashes for a given HAL interface.
102 set<string> ReleasedHashes(const FQName& fq_iface_name);
103 
104 // Returns the partition that a HAL is associated with.
105 Partition PartitionOfProcess(int32_t pid);
106 
107 // Returns SYSTEM for FRAMEWORK, VENDOR for DEVICE.
108 Partition PartitionOfType(SchemaType type);
109 
110 }  // namespace testing
111 }  // namespace vintf
112 
113 // Allows GTest to print pointers with a human readable string.
114 template <typename T>
PrintTo(const sp<T> & v,std::ostream * os)115 void PrintTo(const sp<T>& v, std::ostream* os) {
116   *os << android::hardware::details::toHexString<uintptr_t>(
117       reinterpret_cast<uintptr_t>(&*v), true /* prefix */);
118 }
119 template <typename T>
PrintTo(const T * v,std::ostream * os)120 void PrintTo(const T* v, std::ostream* os) {
121   *os << android::hardware::details::toHexString<uintptr_t>(
122       reinterpret_cast<uintptr_t>(v), true /* prefix */);
123 }
124 
125 }  // namespace android
126 
127 // Allows GTest to print pointers with a human readable string.
128 namespace std {
129 void PrintTo(const android::vintf::testing::HalManifestPtr& v, ostream* os);
130 template <typename T>
PrintTo(const T * v,ostream * os)131 void PrintTo(const T* v, ostream* os) {
132   *os << android::hardware::details::toHexString<uintptr_t>(
133       reinterpret_cast<uintptr_t>(v), true /* prefix */);
134 }
135 }  // namespace std
136 
137 #endif  // VTS_TREBLE_VINTF_TEST_UTILS_H_
138