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 
53 // Returns ro.product.first_api_level if it is defined and not 0. Returns
54 // ro.build.version.sdk otherwise.
GetShippingApiLevel()55 uint64_t GetShippingApiLevel() {
56   uint64_t api_level =
57       GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
58   if (api_level != 0) {
59     return api_level;
60   }
61   return GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
62 }
63 
64 // For a given interface returns package root if known. Returns empty string
65 // otherwise.
PackageRoot(const FQName & fq_iface_name)66 const string PackageRoot(const FQName &fq_iface_name) {
67   for (const auto &package_root : kPackageRoot) {
68     if (fq_iface_name.inPackage(package_root.first)) {
69       return package_root.second;
70     }
71   }
72   return "";
73 }
74 
75 // Returns true iff HAL interface is Android platform.
IsAndroidPlatformInterface(const FQName & fq_iface_name)76 bool IsAndroidPlatformInterface(const FQName &fq_iface_name) {
77   // Package roots are only known for Android platform packages.
78   return !PackageRoot(fq_iface_name).empty();
79 }
80 
81 // Returns true iff the device has the specified feature.
DeviceSupportsFeature(const char * feature)82 bool DeviceSupportsFeature(const char *feature) {
83   bool device_supports_feature = false;
84   FILE *p = popen("pm list features", "re");
85   if (p) {
86     char *line = NULL;
87     size_t len = 0;
88     while (getline(&line, &len, p) > 0) {
89       if (strstr(line, feature)) {
90         device_supports_feature = true;
91         break;
92       }
93     }
94     pclose(p);
95   }
96   return device_supports_feature;
97 }
98 
99 // Returns the set of released hashes for a given HAL interface.
ReleasedHashes(const FQName & fq_iface_name)100 set<string> ReleasedHashes(const FQName &fq_iface_name) {
101   set<string> released_hashes{};
102   string err = "";
103 
104   string file_path = kDataDir + PackageRoot(fq_iface_name) + kHashFileName;
105   auto hashes = Hash::lookupHash(file_path, fq_iface_name.string(), &err);
106   released_hashes.insert(hashes.begin(), hashes.end());
107   return released_hashes;
108 }
109 
110 // Returns the partition that a HAL is associated with.
PartitionOfProcess(int32_t pid)111 Partition PartitionOfProcess(int32_t pid) {
112   auto partition = android::procpartition::getPartition(pid);
113 
114   // TODO(b/70033981): remove once ODM and Vendor manifests are distinguished
115   if (partition == Partition::ODM) {
116     partition = Partition::VENDOR;
117   }
118 
119   return partition;
120 }
121 
PartitionOfType(SchemaType type)122 Partition PartitionOfType(SchemaType type) {
123   switch (type) {
124     case SchemaType::DEVICE:
125       return Partition::VENDOR;
126     case SchemaType::FRAMEWORK:
127       return Partition::SYSTEM;
128   }
129   return Partition::UNKNOWN;
130 }
131 
132 }  // namespace testing
133 }  // namespace vintf
134 }  // namespace android
135 
136 namespace std {
PrintTo(const android::vintf::testing::HalManifestPtr & v,ostream * os)137 void PrintTo(const android::vintf::testing::HalManifestPtr &v, ostream *os) {
138   if (v == nullptr) {
139     *os << "nullptr";
140     return;
141   }
142   *os << to_string(v->type()) << " manifest";
143 }
144 }  // namespace std
145