Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 939df096 authored by Ryan Mitchell's avatar Ryan Mitchell
Browse files

Add odm and oem policies

This change adds parsing, encoding, and validating of odm and oem
overlayable policies to aapt2, libandroidfw, and idmap2.

Bug: 121033532
Test: aapt2_tests, idmap2_tests
Change-Id: Ifc0d4b6c9f9c37e06b2988abade69dbb277c50c2
parent 48869353
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ interface IIdmap2 {
  const int POLICY_VENDOR_PARTITION = 0x00000004;
  const int POLICY_PRODUCT_PARTITION = 0x00000008;
  const int POLICY_SIGNATURE = 0x00000010;
  const int POLICY_ODM_PARTITION = 0x00000020;
  const int POLICY_OEM_PARTITION = 0x00000040;

  @utf8InCpp String getIdmapPath(@utf8InCpp String overlayApkPath, int userId);
  boolean removeIdmap(@utf8InCpp String overlayApkPath, int userId);
+4 −2
Original line number Diff line number Diff line
@@ -26,11 +26,13 @@

namespace android::idmap2 {

constexpr const char* kPolicyPublic = "public";
constexpr const char* kPolicyOdm = "odm";
constexpr const char* kPolicyOem = "oem";
constexpr const char* kPolicyProduct = "product";
constexpr const char* kPolicyPublic = "public";
constexpr const char* kPolicySignature = "signature";
constexpr const char* kPolicySystem = "system";
constexpr const char* kPolicyVendor = "vendor";
constexpr const char* kPolicySignature = "signature";

using PolicyFlags = ResTable_overlayable_policy_header::PolicyFlags;
using PolicyBitmask = uint32_t;
+1 −0
Original line number Diff line number Diff line
@@ -282,6 +282,7 @@ Result<Unit> CheckOverlayable(const LoadedPackage& target_package,
                              const utils::OverlayManifestInfo& overlay_info,
                              const PolicyBitmask& fulfilled_policies, const ResourceId& resid) {
  static constexpr const PolicyBitmask sDefaultPolicies =
      PolicyFlags::POLICY_ODM_PARTITION | PolicyFlags::POLICY_OEM_PARTITION |
      PolicyFlags::POLICY_SYSTEM_PARTITION | PolicyFlags::POLICY_VENDOR_PARTITION |
      PolicyFlags::POLICY_PRODUCT_PARTITION | PolicyFlags::POLICY_SIGNATURE;

+24 −17
Original line number Diff line number Diff line
@@ -17,8 +17,8 @@
#include "idmap2/Policies.h"

#include <iterator>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>

#include "androidfw/ResourceTypes.h"
@@ -27,19 +27,17 @@

namespace android::idmap2 {

namespace {

const std::map<android::StringPiece, PolicyFlags> kStringToFlag = {
Result<PolicyBitmask> PoliciesToBitmask(const std::vector<std::string>& policies) {
  static const std::unordered_map<android::StringPiece, PolicyFlags> kStringToFlag = {
      {kPolicyOdm, PolicyFlags::POLICY_ODM_PARTITION},
      {kPolicyOem, PolicyFlags::POLICY_OEM_PARTITION},
      {kPolicyPublic, PolicyFlags::POLICY_PUBLIC},
      {kPolicyProduct, PolicyFlags::POLICY_PRODUCT_PARTITION},
      {kPolicySignature, PolicyFlags::POLICY_SIGNATURE},
      {kPolicySystem, PolicyFlags::POLICY_SYSTEM_PARTITION},
      {kPolicyVendor, PolicyFlags::POLICY_VENDOR_PARTITION},
    {kPolicySignature, PolicyFlags::POLICY_SIGNATURE},
  };

}  // namespace

Result<PolicyBitmask> PoliciesToBitmask(const std::vector<std::string>& policies) {
  PolicyBitmask bitmask = 0;
  for (const std::string& policy : policies) {
    const auto iter = kStringToFlag.find(policy);
@@ -55,6 +53,15 @@ Result<PolicyBitmask> PoliciesToBitmask(const std::vector<std::string>& policies

std::vector<std::string> BitmaskToPolicies(const PolicyBitmask& bitmask) {
  std::vector<std::string> policies;

  if ((bitmask & PolicyFlags::POLICY_ODM_PARTITION) != 0) {
    policies.emplace_back(kPolicyOdm);
  }

  if ((bitmask & PolicyFlags::POLICY_OEM_PARTITION) != 0) {
    policies.emplace_back(kPolicyOem);
  }

  if ((bitmask & PolicyFlags::POLICY_PUBLIC) != 0) {
    policies.emplace_back(kPolicyPublic);
  }
@@ -63,6 +70,10 @@ std::vector<std::string> BitmaskToPolicies(const PolicyBitmask& bitmask) {
    policies.emplace_back(kPolicyProduct);
  }

  if ((bitmask & PolicyFlags::POLICY_SIGNATURE) != 0) {
    policies.emplace_back(kPolicySignature);
  }

  if ((bitmask & PolicyFlags::POLICY_SYSTEM_PARTITION) != 0) {
    policies.emplace_back(kPolicySystem);
  }
@@ -71,10 +82,6 @@ std::vector<std::string> BitmaskToPolicies(const PolicyBitmask& bitmask) {
    policies.emplace_back(kPolicyVendor);
  }

  if ((bitmask & PolicyFlags::POLICY_SIGNATURE) != 0) {
    policies.emplace_back(kPolicySignature);
  }

  return policies;
}

+20 −14
Original line number Diff line number Diff line
@@ -109,49 +109,55 @@ TEST(BinaryStreamVisitorTests, CreateIdmapFromApkAssetsInteropWithLoadedIdmap) {
  success = LoadedIdmap::Lookup(header, 0x0002, &entry);  // string/c
  ASSERT_FALSE(success);

  success = LoadedIdmap::Lookup(header, 0x0003, &entry);  // string/other
  success = LoadedIdmap::Lookup(header, 0x0003, &entry);  // string/policy_odm
  ASSERT_FALSE(success);

  success = LoadedIdmap::Lookup(header, 0x0004, &entry);  // string/not_overlayable
  success = LoadedIdmap::Lookup(header, 0x0004, &entry);  // string/policy_oem
  ASSERT_FALSE(success);

  success = LoadedIdmap::Lookup(header, 0x0005, &entry);  // string/policy_product
  success = LoadedIdmap::Lookup(header, 0x0005, &entry);  // string/other
  ASSERT_FALSE(success);

  success = LoadedIdmap::Lookup(header, 0x0006, &entry);  // string/policy_public
  success = LoadedIdmap::Lookup(header, 0x0006, &entry);  // string/not_overlayable
  ASSERT_FALSE(success);

  success = LoadedIdmap::Lookup(header, 0x0007, &entry);  // string/policy_system
  success = LoadedIdmap::Lookup(header, 0x0007, &entry);  // string/policy_product
  ASSERT_FALSE(success);

  success = LoadedIdmap::Lookup(header, 0x0008, &entry);  // string/policy_system_vendor
  success = LoadedIdmap::Lookup(header, 0x0008, &entry);  // string/policy_public
  ASSERT_FALSE(success);

  success = LoadedIdmap::Lookup(header, 0x0009, &entry);  // string/policy_signature
  success = LoadedIdmap::Lookup(header, 0x0009, &entry);  // string/policy_system
  ASSERT_FALSE(success);

  success = LoadedIdmap::Lookup(header, 0x000a, &entry);  // string/str1
  success = LoadedIdmap::Lookup(header, 0x000a, &entry);  // string/policy_system_vendor
  ASSERT_FALSE(success);

  success = LoadedIdmap::Lookup(header, 0x000b, &entry);  // string/policy_signature
  ASSERT_FALSE(success);

  success = LoadedIdmap::Lookup(header, 0x000c, &entry);  // string/str1
  ASSERT_TRUE(success);
  ASSERT_EQ(entry, 0x0000);

  success = LoadedIdmap::Lookup(header, 0x000b, &entry);  // string/str2
  success = LoadedIdmap::Lookup(header, 0x000d, &entry);  // string/str2
  ASSERT_FALSE(success);

  success = LoadedIdmap::Lookup(header, 0x000c, &entry);  // string/str3
  success = LoadedIdmap::Lookup(header, 0x000e, &entry);  // string/str3
  ASSERT_TRUE(success);
  ASSERT_EQ(entry, 0x0001);

  success = LoadedIdmap::Lookup(header, 0x000d, &entry);  // string/str4
  success = LoadedIdmap::Lookup(header, 0x000f, &entry);  // string/str4
  ASSERT_TRUE(success);
  ASSERT_EQ(entry, 0x0002);

  success = LoadedIdmap::Lookup(header, 0x000e, &entry);  // string/x
  success = LoadedIdmap::Lookup(header, 0x0010, &entry);  // string/x
  ASSERT_FALSE(success);

  success = LoadedIdmap::Lookup(header, 0x000f, &entry);  // string/y
  success = LoadedIdmap::Lookup(header, 0x0011, &entry);  // string/y
  ASSERT_FALSE(success);

  success = LoadedIdmap::Lookup(header, 0x0010, &entry);  // string/z
  success = LoadedIdmap::Lookup(header, 0x0012, &entry);  // string/z
  ASSERT_FALSE(success);
}

Loading