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