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 HalVerifyFn = std::function<void(const FQName& fq_name,
66 const string& instance_name, Transport)>;
67 using HashCharArray = hidl_array<unsigned char, 32>;
68 using HalManifestPtr = std::shared_ptr<const HalManifest>;
69 using MatrixPtr = std::shared_ptr<const CompatibilityMatrix>;
70 using RuntimeInfoPtr = std::shared_ptr<const RuntimeInfo>;
71
72 // Path to directory on target containing test data.
73 extern const string kDataDir;
74 // Name of file containing HAL hashes.
75 extern const string kHashFileName;
76 // Map from package name to package root.
77 extern const map<string, string> kPackageRoot;
78 // HALs that are allowed to be passthrough under Treble rules.
79 extern const set<string> kPassthroughHals;
80
81 extern const map<size_t /* Shipping API Level */, Level /* FCM Version */>
82 kFcm2ApiLevelMap;
83
84 // Returns ro.product.first_api_level if it is defined and not 0. Returns
85 // ro.build.version.sdk otherwise.
86 uint64_t GetShippingApiLevel();
87
88 // For a given interface returns package root if known. Returns empty string
89 // otherwise.
90 const string PackageRoot(const FQName& fq_iface_name);
91
92 // Returns true iff HAL interface is Android platform.
93 bool IsAndroidPlatformInterface(const FQName& fq_iface_name);
94
95 // Returns the set of released hashes for a given HAL interface.
96 set<string> ReleasedHashes(const FQName& fq_iface_name);
97
98 // Returns the partition that a HAL is associated with.
99 Partition PartitionOfProcess(int32_t pid);
100
101 // Returns SYSTEM for FRAMEWORK, VENDOR for DEVICE.
102 Partition PartitionOfType(SchemaType type);
103
104 } // namespace testing
105 } // namespace vintf
106
107 // Allows GTest to print pointers with a human readable string.
108 template <typename T>
PrintTo(const sp<T> & v,std::ostream * os)109 void PrintTo(const sp<T>& v, std::ostream* os) {
110 *os << android::hardware::details::toHexString<uintptr_t>(
111 reinterpret_cast<uintptr_t>(&*v), true /* prefix */);
112 }
113 template <typename T>
PrintTo(const T * v,std::ostream * os)114 void PrintTo(const T* v, std::ostream* os) {
115 *os << android::hardware::details::toHexString<uintptr_t>(
116 reinterpret_cast<uintptr_t>(v), true /* prefix */);
117 }
118
119 } // namespace android
120
121 // Allows GTest to print pointers with a human readable string.
122 namespace std {
123 void PrintTo(const android::vintf::testing::HalManifestPtr& v, ostream* os);
124 template <typename T>
PrintTo(const T * v,ostream * os)125 void PrintTo(const T* v, ostream* os) {
126 *os << android::hardware::details::toHexString<uintptr_t>(
127 reinterpret_cast<uintptr_t>(v), true /* prefix */);
128 }
129 } // namespace std
130
131 #endif // VTS_TREBLE_VINTF_TEST_UTILS_H_
132