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 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 17 | #include <android-base/file.h> |
| 18 | #include <androidfw/ResourceTypes.h> |
| 19 | #include <gtest/gtest.h> |
| 20 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 21 | #include <cstdio> // fclose |
| 22 | #include <fstream> |
| 23 | #include <memory> |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 24 | #include <string> |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 25 | |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 26 | #include "R.h" |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 27 | #include "TestConstants.h" |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 28 | #include "TestHelpers.h" |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 29 | #include "idmap2/LogInfo.h" |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 30 | #include "idmap2/ResourceMapping.h" |
| 31 | |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 32 | using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags; |
| 33 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 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 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 42 | Result<ResourceMapping> TestGetResourceMapping(const std::string& local_target_path, |
| 43 | const std::string& local_overlay_path, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 44 | const std::string& overlay_name, |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 45 | const PolicyBitmask& fulfilled_policies, |
| 46 | bool enforce_overlayable) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 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); |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 64 | if (!overlay_info) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 65 | return Error(overlay_info.GetError(), R"(Failed to find overlay name "%s")", |
| 66 | overlay_name.c_str()); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 69 | LogInfo log_info; |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 70 | return ResourceMapping::FromContainers(**target, **overlay, *overlay_info, fulfilled_policies, |
| 71 | enforce_overlayable, log_info); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 74 | Result<Unit> MappingExists(const ResourceMapping& mapping, ResourceId target_resource, |
| 75 | ResourceId overlay_resource, bool rewrite) { |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 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 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 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"); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 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); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | auto overlay_map = mapping.GetOverlayToTargetMap(); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 93 | auto overlay_iter = overlay_map.find(overlay_resource); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 94 | if ((overlay_iter != overlay_map.end()) != rewrite) { |
| 95 | return Error(R"(Expected rewriting: "%s")", rewrite ? "true" : "false"); |
| 96 | } |
| 97 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 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 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 129 | return Result<Unit>({}); |
| 130 | } |
| 131 | |
| 132 | TEST(ResourceMappingTests, ResourcesFromApkAssetsLegacy) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 133 | auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay-legacy.apk", "", |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 134 | PolicyFlags::PUBLIC, /* enforce_overlayable */ false); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 135 | |
| 136 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 137 | auto& res = *resources; |
| 138 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 4U); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 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 */)); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | TEST(ResourceMappingTests, ResourcesFromApkAssetsNonMatchingNames) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 150 | auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk", "SwapNames", |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 151 | PolicyFlags::PUBLIC, |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 152 | /* enforce_overlayable */ false); |
| 153 | |
| 154 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 155 | auto& res = *resources; |
| 156 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 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 */)); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 165 | TEST(ResourceMappingTests, DoNotRewriteNonOverlayResourceId) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 166 | auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk", |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 167 | "DifferentPackages", PolicyFlags::PUBLIC, |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 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); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 174 | ASSERT_RESULT(MappingExists(res, R::target::string::str1, 0x0104000a, |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 175 | false /* rewrite */)); // -> android:string/ok |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 176 | ASSERT_RESULT( |
| 177 | MappingExists(res, R::target::string::str3, R::overlay::string::str3, true /* rewrite */)); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | TEST(ResourceMappingTests, InlineResources) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 181 | auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk", "Inline", |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 182 | PolicyFlags::PUBLIC, /* enforce_overlayable */ false); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 183 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 184 | constexpr size_t overlay_string_pool_size = 10U; |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 185 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 186 | auto& res = *resources; |
| 187 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 2U); |
| 188 | ASSERT_EQ(res.GetOverlayToTargetMap().size(), 0U); |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 189 | ASSERT_RESULT(MappingExists(res, R::target::string::str1, Res_value::TYPE_STRING, |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 190 | overlay_string_pool_size + 0U)); // -> "Hello World" |
| 191 | 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] | 192 | } |
| 193 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 194 | TEST(ResourceMappingTests, FabricatedOverlay) { |
| 195 | auto frro = FabricatedOverlay::Builder("com.example.overlay", "SandTheme", "test.target") |
| 196 | .SetOverlayable("TestResources") |
Jeremy Meyer | 77c8a93 | 2022-08-18 22:06:55 +0000 | [diff] [blame] | 197 | .SetResourceValue("integer/int1", Res_value::TYPE_INT_DEC, 2U, "") |
| 198 | .SetResourceValue("string/str1", Res_value::TYPE_REFERENCE, 0x7f010000, "") |
| 199 | .SetResourceValue("string/str2", Res_value::TYPE_STRING, "foobar", "") |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 200 | .Build(); |
| 201 | |
| 202 | ASSERT_TRUE(frro); |
| 203 | TemporaryFile tf; |
| 204 | std::ofstream out(tf.path); |
| 205 | ASSERT_TRUE((*frro).ToBinaryStream(out)); |
| 206 | out.close(); |
| 207 | |
| 208 | auto resources = TestGetResourceMapping("target/target.apk", tf.path, "SandTheme", |
| 209 | PolicyFlags::PUBLIC, /* enforce_overlayable */ false); |
| 210 | |
| 211 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 212 | auto& res = *resources; |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 213 | auto string_pool_data = res.GetStringPoolData(); |
| 214 | auto string_pool = ResStringPool(string_pool_data.data(), string_pool_data.size(), false); |
| 215 | |
| 216 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 217 | ASSERT_EQ(res.GetOverlayToTargetMap().size(), 0U); |
| 218 | ASSERT_RESULT(MappingExists(res, R::target::string::str1, Res_value::TYPE_REFERENCE, 0x7f010000)); |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 219 | ASSERT_RESULT(MappingExists(res, R::target::string::str2, Res_value::TYPE_STRING, |
| 220 | (uint32_t) (string_pool.indexOfString(u"foobar", 6)).value_or(-1))); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 221 | ASSERT_RESULT(MappingExists(res, R::target::integer::int1, Res_value::TYPE_INT_DEC, 2U)); |
| 222 | } |
| 223 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 224 | TEST(ResourceMappingTests, CreateIdmapFromApkAssetsPolicySystemPublic) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 225 | auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk", |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 226 | TestConstants::OVERLAY_NAME_ALL_POLICIES, |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 227 | PolicyFlags::SYSTEM_PARTITION | PolicyFlags::PUBLIC, |
| 228 | /* enforce_overlayable */ true); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 229 | |
| 230 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 231 | auto& res = *resources; |
| 232 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 233 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_public, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 234 | R::overlay::string::policy_public, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 235 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 236 | R::overlay::string::policy_system, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 237 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 238 | R::overlay::string::policy_system_vendor, true /* rewrite */)); |
| 239 | } |
| 240 | |
| 241 | // Resources that are not declared as overlayable and resources that a protected by policies the |
| 242 | // overlay does not fulfill must not map to overlay resources. |
| 243 | TEST(ResourceMappingTests, CreateIdmapFromApkAssetsPolicySystemPublicInvalid) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 244 | auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk", |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 245 | TestConstants::OVERLAY_NAME_ALL_POLICIES, |
| 246 | PolicyFlags::SYSTEM_PARTITION | PolicyFlags::PUBLIC, |
| 247 | /* enforce_overlayable */ true); |
| 248 | |
| 249 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 250 | auto& res = *resources; |
| 251 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U); |
| 252 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_public, |
| 253 | R::overlay::string::policy_public, true /* rewrite */)); |
| 254 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system, |
| 255 | R::overlay::string::policy_system, true /* rewrite */)); |
| 256 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor, |
| 257 | R::overlay::string::policy_system_vendor, true /* rewrite */)); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // Resources that are not declared as overlayable and resources that a protected by policies the |
| 261 | // overlay does not fulfilled can map to overlay resources when overlayable enforcement is turned |
| 262 | // off. |
| 263 | TEST(ResourceMappingTests, ResourcesFromApkAssetsPolicySystemPublicInvalidIgnoreOverlayable) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 264 | auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk", |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 265 | TestConstants::OVERLAY_NAME_ALL_POLICIES, |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 266 | PolicyFlags::SYSTEM_PARTITION | PolicyFlags::PUBLIC, |
| 267 | /* enforce_overlayable */ false); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 268 | |
| 269 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 270 | auto& res = *resources; |
Zoran Jovanovic | 0f942f9 | 2020-06-09 18:51:57 +0200 | [diff] [blame] | 271 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 11U); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 272 | ASSERT_RESULT(MappingExists(res, R::target::string::not_overlayable, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 273 | R::overlay::string::not_overlayable, true /* rewrite */)); |
| 274 | ASSERT_RESULT( |
| 275 | MappingExists(res, R::target::string::other, R::overlay::string::other, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 276 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_actor, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 277 | R::overlay::string::policy_actor, true /* rewrite */)); |
| 278 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_odm, R::overlay::string::policy_odm, |
| 279 | true /* rewrite */)); |
| 280 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_oem, R::overlay::string::policy_oem, |
| 281 | true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 282 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_product, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 283 | R::overlay::string::policy_product, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 284 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_public, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 285 | R::overlay::string::policy_public, true /* rewrite */)); |
Zoran Jovanovic | 0f942f9 | 2020-06-09 18:51:57 +0200 | [diff] [blame] | 286 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_config_signature, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 287 | R::overlay::string::policy_config_signature, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 288 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_signature, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 289 | R::overlay::string::policy_signature, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 290 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 291 | R::overlay::string::policy_system, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 292 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 293 | R::overlay::string::policy_system_vendor, true /* rewrite */)); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 296 | // Overlays that do not target an <overlayable> tag can overlay any resource if overlayable |
| 297 | // enforcement is disabled. |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 298 | TEST(ResourceMappingTests, ResourcesFromApkAssetsNoDefinedOverlayableAndNoTargetName) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 299 | auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay-legacy.apk", "", |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 300 | PolicyFlags::PUBLIC, |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 301 | /* enforce_overlayable */ false); |
| 302 | |
| 303 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 304 | auto& res = *resources; |
| 305 | ASSERT_EQ(res.GetTargetToOverlayMap().size(), 4U); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 306 | ASSERT_RESULT( |
| 307 | MappingExists(res, R::target::integer::int1, R::overlay::integer::int1, false /* rewrite */)); |
| 308 | ASSERT_RESULT( |
| 309 | MappingExists(res, R::target::string::str1, R::overlay::string::str1, false /* rewrite */)); |
| 310 | ASSERT_RESULT( |
| 311 | MappingExists(res, R::target::string::str3, R::overlay::string::str3, false /* rewrite */)); |
| 312 | ASSERT_RESULT( |
| 313 | MappingExists(res, R::target::string::str4, R::overlay::string::str4, false /* rewrite */)); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Ryan Mitchell | e88ef74 | 2020-06-03 12:55:02 -0700 | [diff] [blame] | 316 | // 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] | 317 | // overlay packages that have not defined overlayable resources. |
Ryan Mitchell | e88ef74 | 2020-06-03 12:55:02 -0700 | [diff] [blame] | 318 | TEST(ResourceMappingTests, ResourcesFromApkAssetsDefaultPoliciesPublicFail) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 319 | auto resources = TestGetResourceMapping("target/target-no-overlayable.apk", "overlay/overlay.apk", |
| 320 | "NoTargetName", PolicyFlags::PUBLIC, |
| 321 | /* enforce_overlayable */ true); |
Ryan Mitchell | af93f5d | 2020-05-26 14:29:03 -0700 | [diff] [blame] | 322 | |
Ryan Mitchell | e88ef74 | 2020-06-03 12:55:02 -0700 | [diff] [blame] | 323 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 324 | ASSERT_EQ(resources->GetTargetToOverlayMap().size(), 0U); |
| 325 | } |
| 326 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 327 | // Overlays that are pre-installed or are signed with the same signature as the target or are |
| 328 | // signed with the same signature as the reference package can overlay packages that have not |
| 329 | // defined overlayable resources. |
Ryan Mitchell | e88ef74 | 2020-06-03 12:55:02 -0700 | [diff] [blame] | 330 | TEST(ResourceMappingTests, ResourcesFromApkAssetsDefaultPolicies) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 331 | auto CheckEntries = [&](const PolicyBitmask& fulfilled_policies) { |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 332 | auto resources = |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 333 | TestGetResourceMapping("target/target-no-overlayable.apk", "overlay/overlay.apk", |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 334 | TestConstants::OVERLAY_NAME_ALL_POLICIES, fulfilled_policies, |
| 335 | /* enforce_overlayable */ true); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 336 | |
Ryan Mitchell | e88ef74 | 2020-06-03 12:55:02 -0700 | [diff] [blame] | 337 | ASSERT_TRUE(resources) << resources.GetErrorMessage(); |
| 338 | auto& res = *resources; |
Zoran Jovanovic | 0f942f9 | 2020-06-09 18:51:57 +0200 | [diff] [blame] | 339 | ASSERT_EQ(resources->GetTargetToOverlayMap().size(), 11U); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 340 | ASSERT_RESULT(MappingExists(res, R::target::string::not_overlayable, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 341 | R::overlay::string::not_overlayable, true /* rewrite */)); |
| 342 | ASSERT_RESULT(MappingExists(res, R::target::string::other, R::overlay::string::other, |
| 343 | true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 344 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_actor, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 345 | R::overlay::string::policy_actor, true /* rewrite */)); |
| 346 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_odm, R::overlay::string::policy_odm, |
| 347 | true /* rewrite */)); |
| 348 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_oem, R::overlay::string::policy_oem, |
| 349 | true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 350 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_product, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 351 | R::overlay::string::policy_product, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 352 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_public, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 353 | R::overlay::string::policy_public, true /* rewrite */)); |
Zoran Jovanovic | 0f942f9 | 2020-06-09 18:51:57 +0200 | [diff] [blame] | 354 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_config_signature, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 355 | R::overlay::string::policy_config_signature, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 356 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_signature, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 357 | R::overlay::string::policy_signature, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 358 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 359 | R::overlay::string::policy_system, true /* rewrite */)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 360 | ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 361 | R::overlay::string::policy_system_vendor, true /* rewrite */)); |
Ryan Mitchell | e88ef74 | 2020-06-03 12:55:02 -0700 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | CheckEntries(PolicyFlags::SIGNATURE); |
Zoran Jovanovic | 0f942f9 | 2020-06-09 18:51:57 +0200 | [diff] [blame] | 365 | CheckEntries(PolicyFlags::CONFIG_SIGNATURE); |
Ryan Mitchell | e88ef74 | 2020-06-03 12:55:02 -0700 | [diff] [blame] | 366 | CheckEntries(PolicyFlags::PRODUCT_PARTITION); |
| 367 | CheckEntries(PolicyFlags::SYSTEM_PARTITION); |
| 368 | CheckEntries(PolicyFlags::VENDOR_PARTITION); |
| 369 | CheckEntries(PolicyFlags::ODM_PARTITION); |
| 370 | CheckEntries(PolicyFlags::OEM_PARTITION); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | } // namespace android::idmap2 |