Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 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 <cstdio> // fclose |
| 18 | #include <fstream> |
| 19 | #include <memory> |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 20 | #include <string> |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 21 | |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 22 | #include "R.h" |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 23 | #include "TestConstants.h" |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 24 | #include "TestHelpers.h" |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 25 | #include "androidfw/ResourceTypes.h" |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 26 | #include "gmock/gmock.h" |
| 27 | #include "gtest/gtest.h" |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 28 | #include "idmap2/LogInfo.h" |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 29 | #include "idmap2/ResourceMapping.h" |
| 30 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 31 | using android::Res_value; |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 32 | using android::idmap2::utils::ExtractOverlayManifestInfo; |
| 33 | |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 34 | using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags; |
| 35 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 36 | namespace android::idmap2 { |
| 37 | |
| 38 | #define ASSERT_RESULT(r) \ |
| 39 | do { \ |
| 40 | auto result = r; \ |
| 41 | ASSERT_TRUE(result) << result.GetErrorMessage(); \ |
| 42 | } while (0) |
| 43 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 44 | Result<ResourceMapping> TestGetResourceMapping(const std::string& local_target_apk_path, |
| 45 | const std::string& local_overlay_apk_path, |
| 46 | const std::string& overlay_name, |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 47 | const PolicyBitmask& fulfilled_policies, |
| 48 | bool enforce_overlayable) { |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 49 | auto overlay_info = |
| 50 | ExtractOverlayManifestInfo(GetTestDataPath() + local_overlay_apk_path, overlay_name); |
| 51 | if (!overlay_info) { |
| 52 | return overlay_info.GetError(); |
| 53 | } |
| 54 | |
| 55 | const std::string target_apk_path(GetTestDataPath() + local_target_apk_path); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 56 | std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path); |
| 57 | if (!target_apk) { |
| 58 | return Error(R"(Failed to load target apk "%s")", target_apk_path.data()); |
| 59 | } |
| 60 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 61 | const std::string overlay_apk_path(GetTestDataPath() + local_overlay_apk_path); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 62 | std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path); |
| 63 | if (!overlay_apk) { |
| 64 | return Error(R"(Failed to load overlay apk "%s")", overlay_apk_path.data()); |
| 65 | } |
| 66 | |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 67 | LogInfo log_info; |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 68 | return ResourceMapping::FromApkAssets(*target_apk, *overlay_apk, *overlay_info, |
| 69 | fulfilled_policies, enforce_overlayable, log_info); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 72 | Result<Unit> MappingExists(const ResourceMapping& mapping, ResourceId target_resource, |
| 73 | ResourceId overlay_resource, bool rewrite) { |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 74 | auto target_map = mapping.GetTargetToOverlayMap(); |
| 75 | auto entry_map = target_map.find(target_resource); |
| 76 | if (entry_map == target_map.end()) { |
| 77 | return Error("Failed to find mapping for target resource"); |
| 78 | } |
| 79 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 80 | auto actual_overlay_resource = std::get_if<ResourceId>(&entry_map->second); |
| 81 | if (actual_overlay_resource == nullptr) { |
| 82 | return Error("Target resource is not mapped to an overlay resource id"); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 85 | if (*actual_overlay_resource != overlay_resource) { |
| 86 | return Error(R"(Expected id: "0x%02x" Actual id: "0x%02x")", overlay_resource, |
| 87 | *actual_overlay_resource); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | auto overlay_map = mapping.GetOverlayToTargetMap(); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 91 | auto overlay_iter = overlay_map.find(overlay_resource); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 92 | if ((overlay_iter != overlay_map.end()) != rewrite) { |
| 93 | return Error(R"(Expected rewriting: "%s")", rewrite ? "true" : "false"); |
| 94 | } |
| 95 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 96 | if (rewrite && overlay_iter->second != target_resource) { |
| 97 | return Error(R"(Expected rewrite id: "0x%02x" Actual id: "0x%02x")", target_resource, |
| 98 | overlay_iter->second); |
| 99 | } |
| 100 | |
| 101 | return Result<Unit>({}); |
| 102 | } |
| 103 | |
| 104 | Result<Unit> MappingExists(const ResourceMapping& mapping, const ResourceId& target_resource, |
| 105 | const uint8_t type, const uint32_t value) { |
| 106 | auto target_map = mapping.GetTargetToOverlayMap(); |
| 107 | auto entry_map = target_map.find(target_resource); |
| 108 | if (entry_map == target_map.end()) { |
| 109 | return Error("Failed to find mapping for target resource"); |
| 110 | } |
| 111 | |
| 112 | auto actual_overlay_value = std::get_if<TargetValue>(&entry_map->second); |
| 113 | if (actual_overlay_value == nullptr) { |
| 114 | return Error("Target resource is not mapped to an inline value"); |
| 115 | } |
| 116 | |
| 117 | if (actual_overlay_value->data_type != type) { |
| 118 | return Error(R"(Expected type: "0x%02x" Actual type: "0x%02x")", type, |
| 119 | actual_overlay_value->data_type); |
| 120 | } |
| 121 | |
| 122 | if (actual_overlay_value->data_value != value) { |
| 123 | return Error(R"(Expected value: "0x%08x" Actual value: "0x%08x")", type, |
| 124 | actual_overlay_value->data_value); |
| 125 | } |
| 126 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 127 | return Result<Unit>({}); |
| 128 | } |
| 129 | |
| 130 | TEST(ResourceMappingTests, ResourcesFromApkAssetsLegacy) { |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 131 | auto resources = TestGetResourceMapping("/target/target.apk", "/overlay/overlay-legacy.apk", "", |
| 132 | PolicyFlags::PUBLIC, /* enforce_overlayable */ false); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 133 | |
| 134 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 135 | auto& res = *resources; |
| 136 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 4U); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 137 | ASSERT_RESULT( |
| 138 | MappingExists(res, R::target::integer::int1, R::overlay::integer::int1, false /* rewrite */)); |
| 139 | ASSERT_RESULT( |
| 140 | MappingExists(res, R::target::string::str1, R::overlay::string::str1, false /* rewrite */)); |
| 141 | ASSERT_RESULT( |
| 142 | MappingExists(res, R::target::string::str3, R::overlay::string::str3, false /* rewrite */)); |
| 143 | ASSERT_RESULT( |
| 144 | MappingExists(res, R::target::string::str4, R::overlay::string::str4, false /* rewrite */)); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | TEST(ResourceMappingTests, ResourcesFromApkAssetsNonMatchingNames) { |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 148 | auto resources = TestGetResourceMapping("/target/target.apk", "/overlay/overlay.apk", "SwapNames", |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 149 | PolicyFlags::PUBLIC, |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 150 | /* enforce_overlayable */ false); |
| 151 | |
| 152 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 153 | auto& res = *resources; |
| 154 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 155 | ASSERT_RESULT( |
| 156 | MappingExists(res, R::target::string::str1, R::overlay::string::str4, true /* rewrite */)); |
| 157 | ASSERT_RESULT( |
| 158 | MappingExists(res, R::target::string::str3, R::overlay::string::str1, true /* rewrite */)); |
| 159 | ASSERT_RESULT( |
| 160 | MappingExists(res, R::target::string::str4, R::overlay::string::str3, true /* rewrite */)); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 163 | TEST(ResourceMappingTests, DoNotRewriteNonOverlayResourceId) { |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 164 | auto resources = TestGetResourceMapping("/target/target.apk", "/overlay/overlay.apk", |
| 165 | "DifferentPackages", PolicyFlags::PUBLIC, |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 166 | /* enforce_overlayable */ false); |
| 167 | |
| 168 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 169 | auto& res = *resources; |
| 170 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 2U); |
| 171 | ASSERT_EQ(res.GetOverlayToTargetMap().size(), 1U); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 172 | ASSERT_RESULT(MappingExists(res, R::target::string::str1, 0x0104000a, |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 173 | false /* rewrite */)); // -> android:string/ok |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 174 | ASSERT_RESULT( |
| 175 | MappingExists(res, R::target::string::str3, R::overlay::string::str3, true /* rewrite */)); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | TEST(ResourceMappingTests, InlineResources) { |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 179 | auto resources = TestGetResourceMapping("/target/target.apk", "/overlay/overlay.apk", "Inline", |
| 180 | PolicyFlags::PUBLIC, /* enforce_overlayable */ false); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 181 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 182 | constexpr size_t overlay_string_pool_size = 10U; |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 183 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 184 | auto& res = *resources; |
| 185 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 2U); |
| 186 | ASSERT_EQ(res.GetOverlayToTargetMap().size(), 0U); |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 187 | ASSERT_RESULT(MappingExists(res, R::target::string::str1, Res_value::TYPE_STRING, |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 188 | overlay_string_pool_size + 0U)); // -> "Hello World" |
| 189 | ASSERT_RESULT(MappingExists(res, R::target::integer::int1, Res_value::TYPE_INT_DEC, 73U)); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | TEST(ResourceMappingTests, CreateIdmapFromApkAssetsPolicySystemPublic) { |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 193 | auto resources = TestGetResourceMapping("/target/target.apk", "/overlay/overlay.apk", |
| 194 | TestConstants::OVERLAY_NAME_ALL_POLICIES, |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 195 | PolicyFlags::SYSTEM_PARTITION | PolicyFlags::PUBLIC, |
| 196 | /* enforce_overlayable */ true); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 197 | |
| 198 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 199 | auto& res = *resources; |
| 200 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 201 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_public, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 202 | R::overlay::string::policy_public, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 203 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 204 | R::overlay::string::policy_system, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 205 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 206 | R::overlay::string::policy_system_vendor, true /* rewrite */)); |
| 207 | } |
| 208 | |
| 209 | // Resources that are not declared as overlayable and resources that a protected by policies the |
| 210 | // overlay does not fulfill must not map to overlay resources. |
| 211 | TEST(ResourceMappingTests, CreateIdmapFromApkAssetsPolicySystemPublicInvalid) { |
| 212 | auto resources = TestGetResourceMapping("/target/target.apk", "/overlay/overlay.apk", |
| 213 | TestConstants::OVERLAY_NAME_ALL_POLICIES, |
| 214 | PolicyFlags::SYSTEM_PARTITION | PolicyFlags::PUBLIC, |
| 215 | /* enforce_overlayable */ true); |
| 216 | |
| 217 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 218 | auto& res = *resources; |
| 219 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U); |
| 220 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_public, |
| 221 | R::overlay::string::policy_public, true /* rewrite */)); |
| 222 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system, |
| 223 | R::overlay::string::policy_system, true /* rewrite */)); |
| 224 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor, |
| 225 | R::overlay::string::policy_system_vendor, true /* rewrite */)); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // Resources that are not declared as overlayable and resources that a protected by policies the |
| 229 | // overlay does not fulfilled can map to overlay resources when overlayable enforcement is turned |
| 230 | // off. |
| 231 | TEST(ResourceMappingTests, ResourcesFromApkAssetsPolicySystemPublicInvalidIgnoreOverlayable) { |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 232 | auto resources = TestGetResourceMapping("/target/target.apk", "/overlay/overlay.apk", |
| 233 | TestConstants::OVERLAY_NAME_ALL_POLICIES, |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 234 | PolicyFlags::SYSTEM_PARTITION | PolicyFlags::PUBLIC, |
| 235 | /* enforce_overlayable */ false); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 236 | |
| 237 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 238 | auto& res = *resources; |
Zoran Jovanovic | 0f942f9 | 2020-06-09 18:51:57 +0200 | [diff] [blame] | 239 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 11U); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 240 | ASSERT_RESULT(MappingExists(res, R::target::string::not_overlayable, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 241 | R::overlay::string::not_overlayable, true /* rewrite */)); |
| 242 | ASSERT_RESULT( |
| 243 | MappingExists(res, R::target::string::other, R::overlay::string::other, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 244 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_actor, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 245 | R::overlay::string::policy_actor, true /* rewrite */)); |
| 246 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_odm, R::overlay::string::policy_odm, |
| 247 | true /* rewrite */)); |
| 248 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_oem, R::overlay::string::policy_oem, |
| 249 | true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 250 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_product, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 251 | R::overlay::string::policy_product, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 252 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_public, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 253 | R::overlay::string::policy_public, true /* rewrite */)); |
Zoran Jovanovic | 0f942f9 | 2020-06-09 18:51:57 +0200 | [diff] [blame] | 254 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_config_signature, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 255 | R::overlay::string::policy_config_signature, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 256 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_signature, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 257 | R::overlay::string::policy_signature, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 258 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 259 | R::overlay::string::policy_system, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 260 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 261 | R::overlay::string::policy_system_vendor, true /* rewrite */)); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 264 | // Overlays that do not target an <overlayable> tag can overlay any resource if overlayable |
| 265 | // enforcement is disabled. |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 266 | TEST(ResourceMappingTests, ResourcesFromApkAssetsNoDefinedOverlayableAndNoTargetName) { |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 267 | auto resources = TestGetResourceMapping("/target/target.apk", "/overlay/overlay-legacy.apk", "", |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 268 | PolicyFlags::PUBLIC, |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 269 | /* enforce_overlayable */ false); |
| 270 | |
| 271 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 272 | auto& res = *resources; |
| 273 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 4U); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 274 | ASSERT_RESULT( |
| 275 | MappingExists(res, R::target::integer::int1, R::overlay::integer::int1, false /* rewrite */)); |
| 276 | ASSERT_RESULT( |
| 277 | MappingExists(res, R::target::string::str1, R::overlay::string::str1, false /* rewrite */)); |
| 278 | ASSERT_RESULT( |
| 279 | MappingExists(res, R::target::string::str3, R::overlay::string::str3, false /* rewrite */)); |
| 280 | ASSERT_RESULT( |
| 281 | MappingExists(res, R::target::string::str4, R::overlay::string::str4, false /* rewrite */)); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Ryan Mitchell | e88ef74 | 2020-06-03 12:55:02 -0700 | [diff] [blame] | 284 | // Overlays that are neither pre-installed nor signed with the same signature as the target cannot |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 285 | // overlay packages that have not defined overlayable resources. |
Ryan Mitchell | e88ef74 | 2020-06-03 12:55:02 -0700 | [diff] [blame] | 286 | TEST(ResourceMappingTests, ResourcesFromApkAssetsDefaultPoliciesPublicFail) { |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 287 | auto resources = |
| 288 | TestGetResourceMapping("/target/target-no-overlayable.apk", "/overlay/overlay.apk", |
| 289 | "NoTargetName", PolicyFlags::PUBLIC, |
| 290 | /* enforce_overlayable */ true); |
Ryan Mitchell | af93f5d | 2020-05-26 14:29:03 -0700 | [diff] [blame] | 291 | |
Ryan Mitchell | e88ef74 | 2020-06-03 12:55:02 -0700 | [diff] [blame] | 292 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 293 | ASSERT_EQ(resources->GetTargetToOverlayMap().size(), 0U); |
| 294 | } |
| 295 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 296 | // Overlays that are pre-installed or are signed with the same signature as the target or are |
| 297 | // signed with the same signature as the reference package can overlay packages that have not |
| 298 | // defined overlayable resources. |
Ryan Mitchell | e88ef74 | 2020-06-03 12:55:02 -0700 | [diff] [blame] | 299 | TEST(ResourceMappingTests, ResourcesFromApkAssetsDefaultPolicies) { |
| 300 | auto CheckEntries = [&](const PolicyBitmask& fulfilled_policies) -> void { |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 301 | auto resources = |
| 302 | TestGetResourceMapping("/target/target-no-overlayable.apk", "/overlay/overlay.apk", |
| 303 | TestConstants::OVERLAY_NAME_ALL_POLICIES, fulfilled_policies, |
| 304 | /* enforce_overlayable */ true); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 305 | |
Ryan Mitchell | e88ef74 | 2020-06-03 12:55:02 -0700 | [diff] [blame] | 306 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 307 | auto& res = *resources; |
Zoran Jovanovic | 0f942f9 | 2020-06-09 18:51:57 +0200 | [diff] [blame] | 308 | ASSERT_EQ(resources->GetTargetToOverlayMap().size(), 11U); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 309 | ASSERT_RESULT(MappingExists(res, R::target::string::not_overlayable, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 310 | R::overlay::string::not_overlayable, true /* rewrite */)); |
| 311 | ASSERT_RESULT(MappingExists(res, R::target::string::other, R::overlay::string::other, |
| 312 | true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 313 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_actor, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 314 | R::overlay::string::policy_actor, true /* rewrite */)); |
| 315 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_odm, R::overlay::string::policy_odm, |
| 316 | true /* rewrite */)); |
| 317 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_oem, R::overlay::string::policy_oem, |
| 318 | true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 319 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_product, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 320 | R::overlay::string::policy_product, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 321 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_public, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 322 | R::overlay::string::policy_public, true /* rewrite */)); |
Zoran Jovanovic | 0f942f9 | 2020-06-09 18:51:57 +0200 | [diff] [blame] | 323 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_config_signature, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 324 | R::overlay::string::policy_config_signature, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 325 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_signature, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 326 | R::overlay::string::policy_signature, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 327 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 328 | R::overlay::string::policy_system, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 329 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 330 | R::overlay::string::policy_system_vendor, true /* rewrite */)); |
Ryan Mitchell | e88ef74 | 2020-06-03 12:55:02 -0700 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | CheckEntries(PolicyFlags::SIGNATURE); |
Zoran Jovanovic | 0f942f9 | 2020-06-09 18:51:57 +0200 | [diff] [blame] | 334 | CheckEntries(PolicyFlags::CONFIG_SIGNATURE); |
Ryan Mitchell | e88ef74 | 2020-06-03 12:55:02 -0700 | [diff] [blame] | 335 | CheckEntries(PolicyFlags::PRODUCT_PARTITION); |
| 336 | CheckEntries(PolicyFlags::SYSTEM_PARTITION); |
| 337 | CheckEntries(PolicyFlags::VENDOR_PARTITION); |
| 338 | CheckEntries(PolicyFlags::ODM_PARTITION); |
| 339 | CheckEntries(PolicyFlags::OEM_PARTITION); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | } // namespace android::idmap2 |