1
2 /*
3 * Copyright (C) 2018 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include "utils.h"
19
20 #include <map>
21 #include <set>
22 #include <string>
23 #include <vector>
24
25 #include <android-base/properties.h>
26
27 using android::base::GetUintProperty;
28
29 namespace android {
30 namespace vintf {
31 namespace testing {
32
33 // Path to directory on target containing test data.
34 const string kDataDir = "/data/local/tmp/";
35
36 // Name of file containing HAL hashes.
37 const string kHashFileName = "current.txt";
38
39 // Map from package name to package root.
40 const map<string, string> kPackageRoot = {
41 {"android.frameworks", "frameworks/hardware/interfaces/"},
42 {"android.hardware", "hardware/interfaces/"},
43 {"android.hidl", "system/libhidl/transport/"},
44 {"android.system", "system/hardware/interfaces/"},
45 };
46
47 // HALs that are allowed to be passthrough under Treble rules.
48 const set<string> kPassthroughHals = {
49 "android.hardware.graphics.mapper", "android.hardware.renderscript",
50 "android.hidl.memory",
51 };
52
GetBoardApiLevel()53 uint64_t GetBoardApiLevel() {
54 return GetUintProperty<uint64_t>("ro.vendor.api_level", 0);
55 }
56
57 // For a given interface returns package root if known. Returns empty string
58 // otherwise.
PackageRoot(const FQName & fq_iface_name)59 const string PackageRoot(const FQName &fq_iface_name) {
60 for (const auto &package_root : kPackageRoot) {
61 if (fq_iface_name.inPackage(package_root.first)) {
62 return package_root.second;
63 }
64 }
65 return "";
66 }
67
68 // Returns true iff HAL interface is Android platform.
IsAndroidPlatformInterface(const FQName & fq_iface_name)69 bool IsAndroidPlatformInterface(const FQName &fq_iface_name) {
70 // Package roots are only known for Android platform packages.
71 return !PackageRoot(fq_iface_name).empty();
72 }
73
74 // Returns the set of released hashes for a given HAL interface.
ReleasedHashes(const FQName & fq_iface_name)75 set<string> ReleasedHashes(const FQName &fq_iface_name) {
76 set<string> released_hashes{};
77 string err = "";
78
79 string file_path = kDataDir + PackageRoot(fq_iface_name) + kHashFileName;
80 auto hashes = Hash::lookupHash(file_path, fq_iface_name.string(), &err);
81 released_hashes.insert(hashes.begin(), hashes.end());
82 return released_hashes;
83 }
84
85 // Returns the partition that a HAL is associated with.
PartitionOfProcess(int32_t pid)86 Partition PartitionOfProcess(int32_t pid) {
87 auto partition = android::procpartition::getPartition(pid);
88
89 // TODO(b/70033981): remove once ODM and Vendor manifests are distinguished
90 if (partition == Partition::ODM) {
91 partition = Partition::VENDOR;
92 }
93
94 return partition;
95 }
96
PartitionOfType(SchemaType type)97 Partition PartitionOfType(SchemaType type) {
98 switch (type) {
99 case SchemaType::DEVICE:
100 return Partition::VENDOR;
101 case SchemaType::FRAMEWORK:
102 return Partition::SYSTEM;
103 }
104 return Partition::UNKNOWN;
105 }
106
107 } // namespace testing
108 } // namespace vintf
109 } // namespace android
110
111 namespace std {
PrintTo(const android::vintf::testing::HalManifestPtr & v,ostream * os)112 void PrintTo(const android::vintf::testing::HalManifestPtr &v, ostream *os) {
113 if (v == nullptr) {
114 *os << "nullptr";
115 return;
116 }
117 *os << to_string(v->type()) << " manifest";
118 }
119 } // namespace std
120