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