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 // Try the following properties in this order, returning the first non-zero
88 // (non-empty) value:
89 // - ro.board.api_level
90 // - ro.board.first_api_level
91 // - ro.product.first_api_level
92 // - ro.build.version.sdk
93 uint64_t GetBoardApiLevel();
94
95 // For a given interface returns package root if known. Returns empty string
96 // otherwise.
97 const string PackageRoot(const FQName& fq_iface_name);
98
99 // Returns true iff HAL interface is Android platform.
100 bool IsAndroidPlatformInterface(const FQName& fq_iface_name);
101
102 // Returns the set of released hashes for a given HAL interface.
103 set<string> ReleasedHashes(const FQName& fq_iface_name);
104
105 // Returns the partition that a HAL is associated with.
106 Partition PartitionOfProcess(int32_t pid);
107
108 // Returns SYSTEM for FRAMEWORK, VENDOR for DEVICE.
109 Partition PartitionOfType(SchemaType type);
110
111 } // namespace testing
112 } // namespace vintf
113
114 // Allows GTest to print pointers with a human readable string.
115 template <typename T>
PrintTo(const sp<T> & v,std::ostream * os)116 void PrintTo(const sp<T>& v, std::ostream* os) {
117 *os << android::hardware::details::toHexString<uintptr_t>(
118 reinterpret_cast<uintptr_t>(&*v), true /* prefix */);
119 }
120 template <typename T>
PrintTo(const T * v,std::ostream * os)121 void PrintTo(const T* v, std::ostream* os) {
122 *os << android::hardware::details::toHexString<uintptr_t>(
123 reinterpret_cast<uintptr_t>(v), true /* prefix */);
124 }
125
126 } // namespace android
127
128 // Allows GTest to print pointers with a human readable string.
129 namespace std {
130 void PrintTo(const android::vintf::testing::HalManifestPtr& v, ostream* os);
131 template <typename T>
PrintTo(const T * v,ostream * os)132 void PrintTo(const T* v, ostream* os) {
133 *os << android::hardware::details::toHexString<uintptr_t>(
134 reinterpret_cast<uintptr_t>(v), true /* prefix */);
135 }
136 } // namespace std
137
138 #endif // VTS_TREBLE_VINTF_TEST_UTILS_H_
139