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 // 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 true iff the device has the specified feature.
96 bool DeviceSupportsFeature(const char* feature);
97 
98 // Returns the set of released hashes for a given HAL interface.
99 set<string> ReleasedHashes(const FQName& fq_iface_name);
100 
101 // Returns the partition that a HAL is associated with.
102 Partition PartitionOfProcess(int32_t pid);
103 
104 // Returns SYSTEM for FRAMEWORK, VENDOR for DEVICE.
105 Partition PartitionOfType(SchemaType type);
106 
107 }  // namespace testing
108 }  // namespace vintf
109 
110 // Allows GTest to print pointers with a human readable string.
111 template <typename T>
PrintTo(const sp<T> & v,std::ostream * os)112 void PrintTo(const sp<T>& v, std::ostream* os) {
113   *os << android::hardware::details::toHexString<uintptr_t>(
114       reinterpret_cast<uintptr_t>(&*v), true /* prefix */);
115 }
116 template <typename T>
PrintTo(const T * v,std::ostream * os)117 void PrintTo(const T* v, std::ostream* os) {
118   *os << android::hardware::details::toHexString<uintptr_t>(
119       reinterpret_cast<uintptr_t>(v), true /* prefix */);
120 }
121 
122 }  // namespace android
123 
124 // Allows GTest to print pointers with a human readable string.
125 namespace std {
126 void PrintTo(const android::vintf::testing::HalManifestPtr& v, ostream* os);
127 template <typename T>
PrintTo(const T * v,ostream * os)128 void PrintTo(const T* v, ostream* os) {
129   *os << android::hardware::details::toHexString<uintptr_t>(
130       reinterpret_cast<uintptr_t>(v), true /* prefix */);
131 }
132 }  // namespace std
133 
134 #endif  // VTS_TREBLE_VINTF_TEST_UTILS_H_
135