Home | History | Annotate | Download | only in vintf
      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 
     78 // Returns ro.product.first_api_level if it is defined and not 0. Returns
     79 // ro.build.version.sdk otherwise.
     80 uint64_t GetShippingApiLevel() {
     81   uint64_t api_level =
     82       GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
     83   if (api_level != 0) {
     84     return api_level;
     85   }
     86   return GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
     87 }
     88 
     89 // For a given interface returns package root if known. Returns empty string
     90 // otherwise.
     91 const string PackageRoot(const FQName &fq_iface_name) {
     92   for (const auto &package_root : kPackageRoot) {
     93     if (fq_iface_name.inPackage(package_root.first)) {
     94       return package_root.second;
     95     }
     96   }
     97   return "";
     98 }
     99 
    100 // Returns true iff HAL interface is Google-defined.
    101 bool IsGoogleDefinedIface(const FQName &fq_iface_name) {
    102   // Package roots are only known for Google-defined packages.
    103   return !PackageRoot(fq_iface_name).empty();
    104 }
    105 
    106 // Returns the set of released hashes for a given HAL interface.
    107 set<string> ReleasedHashes(const FQName &fq_iface_name) {
    108   set<string> released_hashes{};
    109   string err = "";
    110 
    111   string file_path = kDataDir + PackageRoot(fq_iface_name) + kHashFileName;
    112   auto hashes = Hash::lookupHash(file_path, fq_iface_name.string(), &err);
    113   released_hashes.insert(hashes.begin(), hashes.end());
    114   return released_hashes;
    115 }
    116 
    117 // Returns the partition that a HAL is associated with.
    118 Partition PartitionOfProcess(int32_t pid) {
    119   auto partition = android::procpartition::getPartition(pid);
    120 
    121   // TODO(b/70033981): remove once ODM and Vendor manifests are distinguished
    122   if (partition == Partition::ODM) {
    123     partition = Partition::VENDOR;
    124   }
    125 
    126   return partition;
    127 }
    128 
    129 Partition PartitionOfType(SchemaType type) {
    130   switch (type) {
    131     case SchemaType::DEVICE:
    132       return Partition::VENDOR;
    133     case SchemaType::FRAMEWORK:
    134       return Partition::SYSTEM;
    135   }
    136   return Partition::UNKNOWN;
    137 }
    138 
    139 }  // namespace testing
    140 }  // namespace vintf
    141 }  // namespace android
    142 
    143 namespace std {
    144 void PrintTo(const android::vintf::testing::HalManifestPtr &v, ostream *os) {
    145   if (v == nullptr) {
    146     *os << "nullptr";
    147     return;
    148   }
    149   *os << to_string(v->type()) << " manifest";
    150 }
    151 void PrintTo(nullptr_t, ostream *os) { *os << "nullptr"; }
    152 }  // namespace std
    153