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