blob: 380e462a3abac16ebb33199e866a138918b9e4d6 [file] [log] [blame]
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -07001/*
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 Mitchell2ed8bfa2021-01-08 13:34:28 -080017#include <android-base/file.h>
18#include <androidfw/ResourceTypes.h>
19#include <gtest/gtest.h>
20
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070021#include <cstdio> // fclose
22#include <fstream>
23#include <memory>
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070024#include <string>
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070025
Jeremy Meyera761f332022-10-21 17:42:14 +000026#include <fcntl.h>
Winson62ac8b52019-12-04 08:36:48 -080027#include "R.h"
Ryan Mitchell30dc2e02020-12-02 11:43:18 -080028#include "TestConstants.h"
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070029#include "TestHelpers.h"
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020030#include "idmap2/LogInfo.h"
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070031#include "idmap2/ResourceMapping.h"
32
Winson62ac8b52019-12-04 08:36:48 -080033using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
34
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070035namespace android::idmap2 {
36
37#define ASSERT_RESULT(r) \
38 do { \
39 auto result = r; \
40 ASSERT_TRUE(result) << result.GetErrorMessage(); \
41 } while (0)
42
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080043Result<ResourceMapping> TestGetResourceMapping(const std::string& local_target_path,
44 const std::string& local_overlay_path,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -080045 const std::string& overlay_name,
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070046 const PolicyBitmask& fulfilled_policies,
47 bool enforce_overlayable) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080048 const std::string target_path = (local_target_path[0] == '/')
49 ? local_target_path
50 : (GetTestDataPath() + "/" + local_target_path);
51 auto target = TargetResourceContainer::FromPath(target_path);
52 if (!target) {
53 return Error(target.GetError(), R"(Failed to load target "%s")", target_path.c_str());
54 }
55
56 const std::string overlay_path = (local_overlay_path[0] == '/')
57 ? local_overlay_path
58 : (GetTestDataPath() + "/" + local_overlay_path);
59 auto overlay = OverlayResourceContainer::FromPath(overlay_path);
60 if (!overlay) {
61 return Error(overlay.GetError(), R"(Failed to load overlay "%s")", overlay_path.c_str());
62 }
63
64 auto overlay_info = (*overlay)->FindOverlayInfo(overlay_name);
Ryan Mitchell30dc2e02020-12-02 11:43:18 -080065 if (!overlay_info) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080066 return Error(overlay_info.GetError(), R"(Failed to find overlay name "%s")",
67 overlay_name.c_str());
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070068 }
69
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020070 LogInfo log_info;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080071 return ResourceMapping::FromContainers(**target, **overlay, *overlay_info, fulfilled_policies,
72 enforce_overlayable, log_info);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070073}
74
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070075Result<Unit> MappingExists(const ResourceMapping& mapping, ResourceId target_resource,
76 ResourceId overlay_resource, bool rewrite) {
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070077 auto target_map = mapping.GetTargetToOverlayMap();
78 auto entry_map = target_map.find(target_resource);
79 if (entry_map == target_map.end()) {
Jeremy Meyera761f332022-10-21 17:42:14 +000080 std::string keys;
81 for (const auto &pair : target_map) {
82 keys.append(fmt::format("0x{:x}", pair.first)).append(" ");
83 }
84 return Error(R"(Failed to find mapping for target resource "0x%02x": "%s")",
85 target_resource, keys.c_str());
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070086 }
87
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070088 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 Mitchell9e4f52b2019-09-19 12:15:52 -070091 }
92
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070093 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 Mitchell9e4f52b2019-09-19 12:15:52 -070096 }
97
98 auto overlay_map = mapping.GetOverlayToTargetMap();
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070099 auto overlay_iter = overlay_map.find(overlay_resource);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700100 if ((overlay_iter != overlay_map.end()) != rewrite) {
101 return Error(R"(Expected rewriting: "%s")", rewrite ? "true" : "false");
102 }
103
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700104 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
112Result<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()) {
Jeremy Meyera761f332022-10-21 17:42:14 +0000117 std::string keys;
118 for (const auto &pair : target_map) {
119 keys.append(fmt::format("{:x}", pair.first)).append(" ");
120 }
121 return Error(R"(Failed to find mapping for target resource "0x%02x": "%s")",
122 target_resource, keys.c_str());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700123 }
124
Jeremy Meyerbe2b7792022-08-23 17:42:50 +0000125 auto config_map = std::get_if<ConfigMap>(&entry_map->second);
126 if (config_map == nullptr || config_map->empty()) {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700127 return Error("Target resource is not mapped to an inline value");
128 }
Jeremy Meyerbe2b7792022-08-23 17:42:50 +0000129 auto actual_overlay_value = config_map->begin()->second;
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700130
Jeremy Meyerbe2b7792022-08-23 17:42:50 +0000131 if (actual_overlay_value.data_type != type) {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700132 return Error(R"(Expected type: "0x%02x" Actual type: "0x%02x")", type,
Jeremy Meyerbe2b7792022-08-23 17:42:50 +0000133 actual_overlay_value.data_type);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700134 }
135
Jeremy Meyerbe2b7792022-08-23 17:42:50 +0000136 if (actual_overlay_value.data_value != value) {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700137 return Error(R"(Expected value: "0x%08x" Actual value: "0x%08x")", type,
Jeremy Meyerbe2b7792022-08-23 17:42:50 +0000138 actual_overlay_value.data_value);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700139 }
140
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700141 return Result<Unit>({});
142}
143
144TEST(ResourceMappingTests, ResourcesFromApkAssetsLegacy) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800145 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay-legacy.apk", "",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800146 PolicyFlags::PUBLIC, /* enforce_overlayable */ false);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700147
148 ASSERT_TRUE(resources) << resources.GetErrorMessage();
149 auto& res = *resources;
150 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 4U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700151 ASSERT_RESULT(
152 MappingExists(res, R::target::integer::int1, R::overlay::integer::int1, false /* rewrite */));
153 ASSERT_RESULT(
154 MappingExists(res, R::target::string::str1, R::overlay::string::str1, false /* rewrite */));
155 ASSERT_RESULT(
156 MappingExists(res, R::target::string::str3, R::overlay::string::str3, false /* rewrite */));
157 ASSERT_RESULT(
158 MappingExists(res, R::target::string::str4, R::overlay::string::str4, false /* rewrite */));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700159}
160
161TEST(ResourceMappingTests, ResourcesFromApkAssetsNonMatchingNames) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800162 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk", "SwapNames",
Winson62ac8b52019-12-04 08:36:48 -0800163 PolicyFlags::PUBLIC,
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700164 /* enforce_overlayable */ false);
165
166 ASSERT_TRUE(resources) << resources.GetErrorMessage();
167 auto& res = *resources;
168 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700169 ASSERT_RESULT(
170 MappingExists(res, R::target::string::str1, R::overlay::string::str4, true /* rewrite */));
171 ASSERT_RESULT(
172 MappingExists(res, R::target::string::str3, R::overlay::string::str1, true /* rewrite */));
173 ASSERT_RESULT(
174 MappingExists(res, R::target::string::str4, R::overlay::string::str3, true /* rewrite */));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700175}
176
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700177TEST(ResourceMappingTests, DoNotRewriteNonOverlayResourceId) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800178 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800179 "DifferentPackages", PolicyFlags::PUBLIC,
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700180 /* enforce_overlayable */ false);
181
182 ASSERT_TRUE(resources) << resources.GetErrorMessage();
183 auto& res = *resources;
184 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 2U);
185 ASSERT_EQ(res.GetOverlayToTargetMap().size(), 1U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700186 ASSERT_RESULT(MappingExists(res, R::target::string::str1, 0x0104000a,
Winson62ac8b52019-12-04 08:36:48 -0800187 false /* rewrite */)); // -> android:string/ok
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800188 ASSERT_RESULT(
189 MappingExists(res, R::target::string::str3, R::overlay::string::str3, true /* rewrite */));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700190}
191
192TEST(ResourceMappingTests, InlineResources) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800193 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk", "Inline",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800194 PolicyFlags::PUBLIC, /* enforce_overlayable */ false);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700195
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800196 constexpr size_t overlay_string_pool_size = 10U;
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700197 ASSERT_TRUE(resources) << resources.GetErrorMessage();
198 auto& res = *resources;
199 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 2U);
200 ASSERT_EQ(res.GetOverlayToTargetMap().size(), 0U);
Winson62ac8b52019-12-04 08:36:48 -0800201 ASSERT_RESULT(MappingExists(res, R::target::string::str1, Res_value::TYPE_STRING,
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700202 overlay_string_pool_size + 0U)); // -> "Hello World"
203 ASSERT_RESULT(MappingExists(res, R::target::integer::int1, Res_value::TYPE_INT_DEC, 73U));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700204}
205
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800206TEST(ResourceMappingTests, FabricatedOverlay) {
Jeremy Meyera761f332022-10-21 17:42:14 +0000207 auto path = GetTestDataPath() + "/overlay/res/drawable/android.png";
208 auto fd = android::base::unique_fd(::open(path.c_str(), O_RDONLY | O_CLOEXEC));
209 ASSERT_TRUE(fd > 0) << "errno " << errno << " for path " << path;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800210 auto frro = FabricatedOverlay::Builder("com.example.overlay", "SandTheme", "test.target")
211 .SetOverlayable("TestResources")
Jeremy Meyer77c8a932022-08-18 22:06:55 +0000212 .SetResourceValue("integer/int1", Res_value::TYPE_INT_DEC, 2U, "")
213 .SetResourceValue("string/str1", Res_value::TYPE_REFERENCE, 0x7f010000, "")
214 .SetResourceValue("string/str2", Res_value::TYPE_STRING, "foobar", "")
Jeremy Meyera761f332022-10-21 17:42:14 +0000215 .SetResourceValue("drawable/dr1", fd, "")
216 .setFrroPath("/foo/bar/biz.frro")
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800217 .Build();
218
219 ASSERT_TRUE(frro);
220 TemporaryFile tf;
221 std::ofstream out(tf.path);
222 ASSERT_TRUE((*frro).ToBinaryStream(out));
223 out.close();
224
225 auto resources = TestGetResourceMapping("target/target.apk", tf.path, "SandTheme",
226 PolicyFlags::PUBLIC, /* enforce_overlayable */ false);
227
228 ASSERT_TRUE(resources) << resources.GetErrorMessage();
229 auto& res = *resources;
Jeremy Meyer65871022022-07-12 22:45:08 +0000230 auto string_pool_data = res.GetStringPoolData();
231 auto string_pool = ResStringPool(string_pool_data.data(), string_pool_data.size(), false);
232
Jeremy Meyera761f332022-10-21 17:42:14 +0000233 std::u16string expected_uri = u"frro://foo/bar/biz.frro?offset=16&size=8341";
234 uint32_t uri_index
235 = string_pool.indexOfString(expected_uri.data(), expected_uri.length()).value_or(-1);
236
237 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 4U);
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800238 ASSERT_EQ(res.GetOverlayToTargetMap().size(), 0U);
239 ASSERT_RESULT(MappingExists(res, R::target::string::str1, Res_value::TYPE_REFERENCE, 0x7f010000));
Jeremy Meyer65871022022-07-12 22:45:08 +0000240 ASSERT_RESULT(MappingExists(res, R::target::string::str2, Res_value::TYPE_STRING,
241 (uint32_t) (string_pool.indexOfString(u"foobar", 6)).value_or(-1)));
Jeremy Meyera761f332022-10-21 17:42:14 +0000242 ASSERT_RESULT(MappingExists(res, R::target::drawable::dr1, Res_value::TYPE_STRING, uri_index));
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800243 ASSERT_RESULT(MappingExists(res, R::target::integer::int1, Res_value::TYPE_INT_DEC, 2U));
244}
245
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700246TEST(ResourceMappingTests, CreateIdmapFromApkAssetsPolicySystemPublic) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800247 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800248 TestConstants::OVERLAY_NAME_ALL_POLICIES,
Winson62ac8b52019-12-04 08:36:48 -0800249 PolicyFlags::SYSTEM_PARTITION | PolicyFlags::PUBLIC,
250 /* enforce_overlayable */ true);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700251
252 ASSERT_TRUE(resources) << resources.GetErrorMessage();
253 auto& res = *resources;
254 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700255 ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800256 R::overlay::string::policy_public, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700257 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800258 R::overlay::string::policy_system, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700259 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800260 R::overlay::string::policy_system_vendor, true /* rewrite */));
261}
262
263// Resources that are not declared as overlayable and resources that a protected by policies the
264// overlay does not fulfill must not map to overlay resources.
265TEST(ResourceMappingTests, CreateIdmapFromApkAssetsPolicySystemPublicInvalid) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800266 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800267 TestConstants::OVERLAY_NAME_ALL_POLICIES,
268 PolicyFlags::SYSTEM_PARTITION | PolicyFlags::PUBLIC,
269 /* enforce_overlayable */ true);
270
271 ASSERT_TRUE(resources) << resources.GetErrorMessage();
272 auto& res = *resources;
273 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U);
274 ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
275 R::overlay::string::policy_public, true /* rewrite */));
276 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
277 R::overlay::string::policy_system, true /* rewrite */));
278 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
279 R::overlay::string::policy_system_vendor, true /* rewrite */));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700280}
281
282// Resources that are not declared as overlayable and resources that a protected by policies the
283// overlay does not fulfilled can map to overlay resources when overlayable enforcement is turned
284// off.
285TEST(ResourceMappingTests, ResourcesFromApkAssetsPolicySystemPublicInvalidIgnoreOverlayable) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800286 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800287 TestConstants::OVERLAY_NAME_ALL_POLICIES,
Winson62ac8b52019-12-04 08:36:48 -0800288 PolicyFlags::SYSTEM_PARTITION | PolicyFlags::PUBLIC,
289 /* enforce_overlayable */ false);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700290
291 ASSERT_TRUE(resources) << resources.GetErrorMessage();
292 auto& res = *resources;
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200293 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 11U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700294 ASSERT_RESULT(MappingExists(res, R::target::string::not_overlayable,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800295 R::overlay::string::not_overlayable, true /* rewrite */));
296 ASSERT_RESULT(
297 MappingExists(res, R::target::string::other, R::overlay::string::other, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700298 ASSERT_RESULT(MappingExists(res, R::target::string::policy_actor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800299 R::overlay::string::policy_actor, true /* rewrite */));
300 ASSERT_RESULT(MappingExists(res, R::target::string::policy_odm, R::overlay::string::policy_odm,
301 true /* rewrite */));
302 ASSERT_RESULT(MappingExists(res, R::target::string::policy_oem, R::overlay::string::policy_oem,
303 true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700304 ASSERT_RESULT(MappingExists(res, R::target::string::policy_product,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800305 R::overlay::string::policy_product, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700306 ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800307 R::overlay::string::policy_public, true /* rewrite */));
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200308 ASSERT_RESULT(MappingExists(res, R::target::string::policy_config_signature,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800309 R::overlay::string::policy_config_signature, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700310 ASSERT_RESULT(MappingExists(res, R::target::string::policy_signature,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800311 R::overlay::string::policy_signature, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700312 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800313 R::overlay::string::policy_system, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700314 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800315 R::overlay::string::policy_system_vendor, true /* rewrite */));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700316}
317
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800318// Overlays that do not target an <overlayable> tag can overlay any resource if overlayable
319// enforcement is disabled.
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700320TEST(ResourceMappingTests, ResourcesFromApkAssetsNoDefinedOverlayableAndNoTargetName) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800321 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay-legacy.apk", "",
Winson62ac8b52019-12-04 08:36:48 -0800322 PolicyFlags::PUBLIC,
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700323 /* enforce_overlayable */ false);
324
325 ASSERT_TRUE(resources) << resources.GetErrorMessage();
326 auto& res = *resources;
327 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 4U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700328 ASSERT_RESULT(
329 MappingExists(res, R::target::integer::int1, R::overlay::integer::int1, false /* rewrite */));
330 ASSERT_RESULT(
331 MappingExists(res, R::target::string::str1, R::overlay::string::str1, false /* rewrite */));
332 ASSERT_RESULT(
333 MappingExists(res, R::target::string::str3, R::overlay::string::str3, false /* rewrite */));
334 ASSERT_RESULT(
335 MappingExists(res, R::target::string::str4, R::overlay::string::str4, false /* rewrite */));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700336}
337
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700338// Overlays that are neither pre-installed nor signed with the same signature as the target cannot
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700339// overlay packages that have not defined overlayable resources.
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700340TEST(ResourceMappingTests, ResourcesFromApkAssetsDefaultPoliciesPublicFail) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800341 auto resources = TestGetResourceMapping("target/target-no-overlayable.apk", "overlay/overlay.apk",
342 "NoTargetName", PolicyFlags::PUBLIC,
343 /* enforce_overlayable */ true);
Ryan Mitchellaf93f5d2020-05-26 14:29:03 -0700344
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700345 ASSERT_TRUE(resources) << resources.GetErrorMessage();
346 ASSERT_EQ(resources->GetTargetToOverlayMap().size(), 0U);
347}
348
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700349// Overlays that are pre-installed or are signed with the same signature as the target or are
350// signed with the same signature as the reference package can overlay packages that have not
351// defined overlayable resources.
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700352TEST(ResourceMappingTests, ResourcesFromApkAssetsDefaultPolicies) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800353 auto CheckEntries = [&](const PolicyBitmask& fulfilled_policies) {
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800354 auto resources =
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800355 TestGetResourceMapping("target/target-no-overlayable.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800356 TestConstants::OVERLAY_NAME_ALL_POLICIES, fulfilled_policies,
357 /* enforce_overlayable */ true);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700358
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700359 ASSERT_TRUE(resources) << resources.GetErrorMessage();
360 auto& res = *resources;
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200361 ASSERT_EQ(resources->GetTargetToOverlayMap().size(), 11U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700362 ASSERT_RESULT(MappingExists(res, R::target::string::not_overlayable,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800363 R::overlay::string::not_overlayable, true /* rewrite */));
364 ASSERT_RESULT(MappingExists(res, R::target::string::other, R::overlay::string::other,
365 true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700366 ASSERT_RESULT(MappingExists(res, R::target::string::policy_actor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800367 R::overlay::string::policy_actor, true /* rewrite */));
368 ASSERT_RESULT(MappingExists(res, R::target::string::policy_odm, R::overlay::string::policy_odm,
369 true /* rewrite */));
370 ASSERT_RESULT(MappingExists(res, R::target::string::policy_oem, R::overlay::string::policy_oem,
371 true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700372 ASSERT_RESULT(MappingExists(res, R::target::string::policy_product,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800373 R::overlay::string::policy_product, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700374 ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800375 R::overlay::string::policy_public, true /* rewrite */));
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200376 ASSERT_RESULT(MappingExists(res, R::target::string::policy_config_signature,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800377 R::overlay::string::policy_config_signature, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700378 ASSERT_RESULT(MappingExists(res, R::target::string::policy_signature,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800379 R::overlay::string::policy_signature, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700380 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800381 R::overlay::string::policy_system, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700382 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800383 R::overlay::string::policy_system_vendor, true /* rewrite */));
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700384 };
385
386 CheckEntries(PolicyFlags::SIGNATURE);
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200387 CheckEntries(PolicyFlags::CONFIG_SIGNATURE);
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700388 CheckEntries(PolicyFlags::PRODUCT_PARTITION);
389 CheckEntries(PolicyFlags::SYSTEM_PARTITION);
390 CheckEntries(PolicyFlags::VENDOR_PARTITION);
391 CheckEntries(PolicyFlags::ODM_PARTITION);
392 CheckEntries(PolicyFlags::OEM_PARTITION);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700393}
394
395} // namespace android::idmap2