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 #include "utils.h"
18
19 #include <map>
20 #include <set>
21 #include <string>
22 #include <vector>
23
24 #include <android-base/properties.h>
25
26 using android::base::GetUintProperty;
27
28 namespace android {
29 namespace vintf {
30 namespace testing {
31
32 // Path to directory on target containing test data.
33 const string kDataDir = "/data/local/tmp/";
34
35 // Name of file containing HAL hashes.
36 const string kHashFileName = "current.txt";
37
38 // Map from package name to package root.
39 const map<string, string> kPackageRoot = {
40 {"android.frameworks", "frameworks/hardware/interfaces/"},
41 {"android.hardware", "hardware/interfaces/"},
42 {"android.hidl", "system/libhidl/transport/"},
43 {"android.system", "system/hardware/interfaces/"},
44 };
45
46 // HALs that are allowed to be passthrough under Treble rules.
47 const set<string> kPassthroughHals = {
48 "android.hardware.graphics.mapper", "android.hardware.renderscript",
49 "android.hidl.memory",
50 };
51
52 // kFcm2ApiLevelMap is associated with API level. There can be multiple
53 // Framework Compatibility Matrix Version (FCM Version) per API level, or
54 // multiple API levels per FCM version.
55 // kFcm2ApiLevelMap is defined apart from android::vintf::Level. Level is an
56 // integer designed to be irrelevant with API level; the O / O_MR1 values are
57 // historic values for convenience, and should be removed (b/70628538). Hence
58 // these values are not used here.
59 // For example:
60 // ...
61 // // Assume devices launch with Android X must implement FCM version >= 9
62 // X = 9,
63 // // Assume devices launch with Android Y and Android Z must implement
64 // // FCM version >= 11
65 // Y = 11,
66 // Z = 11
67 const map<size_t /* Shipping API Level */, Level /* FCM Version */>
68 kFcm2ApiLevelMap{{// N. The test runs on devices that launch with N and
69 // become a Treble device when upgrading to O.
70 {25, static_cast<Level>(1)},
71 // O
72 {26, static_cast<Level>(1)},
73 // O MR-1
74 {27, static_cast<Level>(2)},
75 // P
76 {28, static_cast<Level>(3)},
77 // Q
78 {29, static_cast<Level>(4)}}};
79
80 // Returns ro.product.first_api_level if it is defined and not 0. Returns
81 // ro.build.version.sdk otherwise.
GetShippingApiLevel()82 uint64_t GetShippingApiLevel() {
83 uint64_t api_level =
84 GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
85 if (api_level != 0) {
86 return api_level;
87 }
88 return GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
89 }
90
91 // For a given interface returns package root if known. Returns empty string
92 // otherwise.
PackageRoot(const FQName & fq_iface_name)93 const string PackageRoot(const FQName &fq_iface_name) {
94 for (const auto &package_root : kPackageRoot) {
95 if (fq_iface_name.inPackage(package_root.first)) {
96 return package_root.second;
97 }
98 }
99 return "";
100 }
101
102 // Returns true iff HAL interface is Android platform.
IsAndroidPlatformInterface(const FQName & fq_iface_name)103 bool IsAndroidPlatformInterface(const FQName &fq_iface_name) {
104 // Package roots are only known for Android platform packages.
105 return !PackageRoot(fq_iface_name).empty();
106 }
107
108 // Returns the set of released hashes for a given HAL interface.
ReleasedHashes(const FQName & fq_iface_name)109 set<string> ReleasedHashes(const FQName &fq_iface_name) {
110 set<string> released_hashes{};
111 string err = "";
112
113 string file_path = kDataDir + PackageRoot(fq_iface_name) + kHashFileName;
114 auto hashes = Hash::lookupHash(file_path, fq_iface_name.string(), &err);
115 released_hashes.insert(hashes.begin(), hashes.end());
116 return released_hashes;
117 }
118
119 // Returns the partition that a HAL is associated with.
PartitionOfProcess(int32_t pid)120 Partition PartitionOfProcess(int32_t pid) {
121 auto partition = android::procpartition::getPartition(pid);
122
123 // TODO(b/70033981): remove once ODM and Vendor manifests are distinguished
124 if (partition == Partition::ODM) {
125 partition = Partition::VENDOR;
126 }
127
128 return partition;
129 }
130
PartitionOfType(SchemaType type)131 Partition PartitionOfType(SchemaType type) {
132 switch (type) {
133 case SchemaType::DEVICE:
134 return Partition::VENDOR;
135 case SchemaType::FRAMEWORK:
136 return Partition::SYSTEM;
137 }
138 return Partition::UNKNOWN;
139 }
140
141 } // namespace testing
142 } // namespace vintf
143 } // namespace android
144
145 namespace std {
PrintTo(const android::vintf::testing::HalManifestPtr & v,ostream * os)146 void PrintTo(const android::vintf::testing::HalManifestPtr &v, ostream *os) {
147 if (v == nullptr) {
148 *os << "nullptr";
149 return;
150 }
151 *os << to_string(v->type()) << " manifest";
152 }
153 } // namespace std
154