blob: ca9a444bcb05a4b796f60e087ed45617be9846d1 [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
Winson62ac8b52019-12-04 08:36:48 -080026#include "R.h"
Ryan Mitchell30dc2e02020-12-02 11:43:18 -080027#include "TestConstants.h"
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070028#include "TestHelpers.h"
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020029#include "idmap2/LogInfo.h"
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070030#include "idmap2/ResourceMapping.h"
31
Winson62ac8b52019-12-04 08:36:48 -080032using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
33
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070034namespace 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 Mitchell2ed8bfa2021-01-08 13:34:28 -080042Result<ResourceMapping> TestGetResourceMapping(const std::string& local_target_path,
43 const std::string& local_overlay_path,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -080044 const std::string& overlay_name,
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070045 const PolicyBitmask& fulfilled_policies,
46 bool enforce_overlayable) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080047 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 Mitchell30dc2e02020-12-02 11:43:18 -080064 if (!overlay_info) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080065 return Error(overlay_info.GetError(), R"(Failed to find overlay name "%s")",
66 overlay_name.c_str());
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070067 }
68
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020069 LogInfo log_info;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080070 return ResourceMapping::FromContainers(**target, **overlay, *overlay_info, fulfilled_policies,
71 enforce_overlayable, log_info);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070072}
73
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070074Result<Unit> MappingExists(const ResourceMapping& mapping, ResourceId target_resource,
75 ResourceId overlay_resource, bool rewrite) {
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070076 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 Mitchellbf1f45b2020-09-29 17:22:52 -070082 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 Mitchell9e4f52b2019-09-19 12:15:52 -070085 }
86
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070087 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 Mitchell9e4f52b2019-09-19 12:15:52 -070090 }
91
92 auto overlay_map = mapping.GetOverlayToTargetMap();
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070093 auto overlay_iter = overlay_map.find(overlay_resource);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070094 if ((overlay_iter != overlay_map.end()) != rewrite) {
95 return Error(R"(Expected rewriting: "%s")", rewrite ? "true" : "false");
96 }
97
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070098 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
106Result<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 Mitchell9e4f52b2019-09-19 12:15:52 -0700129 return Result<Unit>({});
130}
131
132TEST(ResourceMappingTests, ResourcesFromApkAssetsLegacy) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800133 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay-legacy.apk", "",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800134 PolicyFlags::PUBLIC, /* enforce_overlayable */ false);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700135
136 ASSERT_TRUE(resources) << resources.GetErrorMessage();
137 auto& res = *resources;
138 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 4U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700139 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 Mitchell9e4f52b2019-09-19 12:15:52 -0700147}
148
149TEST(ResourceMappingTests, ResourcesFromApkAssetsNonMatchingNames) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800150 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk", "SwapNames",
Winson62ac8b52019-12-04 08:36:48 -0800151 PolicyFlags::PUBLIC,
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700152 /* enforce_overlayable */ false);
153
154 ASSERT_TRUE(resources) << resources.GetErrorMessage();
155 auto& res = *resources;
156 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700157 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 Mitchell9e4f52b2019-09-19 12:15:52 -0700163}
164
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700165TEST(ResourceMappingTests, DoNotRewriteNonOverlayResourceId) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800166 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800167 "DifferentPackages", PolicyFlags::PUBLIC,
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700168 /* 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 Mitchellbf1f45b2020-09-29 17:22:52 -0700174 ASSERT_RESULT(MappingExists(res, R::target::string::str1, 0x0104000a,
Winson62ac8b52019-12-04 08:36:48 -0800175 false /* rewrite */)); // -> android:string/ok
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800176 ASSERT_RESULT(
177 MappingExists(res, R::target::string::str3, R::overlay::string::str3, true /* rewrite */));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700178}
179
180TEST(ResourceMappingTests, InlineResources) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800181 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk", "Inline",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800182 PolicyFlags::PUBLIC, /* enforce_overlayable */ false);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700183
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800184 constexpr size_t overlay_string_pool_size = 10U;
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700185 ASSERT_TRUE(resources) << resources.GetErrorMessage();
186 auto& res = *resources;
187 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 2U);
188 ASSERT_EQ(res.GetOverlayToTargetMap().size(), 0U);
Winson62ac8b52019-12-04 08:36:48 -0800189 ASSERT_RESULT(MappingExists(res, R::target::string::str1, Res_value::TYPE_STRING,
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700190 overlay_string_pool_size + 0U)); // -> "Hello World"
191 ASSERT_RESULT(MappingExists(res, R::target::integer::int1, Res_value::TYPE_INT_DEC, 73U));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700192}
193
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800194TEST(ResourceMappingTests, FabricatedOverlay) {
195 auto frro = FabricatedOverlay::Builder("com.example.overlay", "SandTheme", "test.target")
196 .SetOverlayable("TestResources")
Jeremy Meyer77c8a932022-08-18 22:06:55 +0000197 .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 Mitchell2ed8bfa2021-01-08 13:34:28 -0800200 .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 Meyer65871022022-07-12 22:45:08 +0000213 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 Mitchell2ed8bfa2021-01-08 13:34:28 -0800217 ASSERT_EQ(res.GetOverlayToTargetMap().size(), 0U);
218 ASSERT_RESULT(MappingExists(res, R::target::string::str1, Res_value::TYPE_REFERENCE, 0x7f010000));
Jeremy Meyer65871022022-07-12 22:45:08 +0000219 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 Mitchell2ed8bfa2021-01-08 13:34:28 -0800221 ASSERT_RESULT(MappingExists(res, R::target::integer::int1, Res_value::TYPE_INT_DEC, 2U));
222}
223
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700224TEST(ResourceMappingTests, CreateIdmapFromApkAssetsPolicySystemPublic) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800225 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800226 TestConstants::OVERLAY_NAME_ALL_POLICIES,
Winson62ac8b52019-12-04 08:36:48 -0800227 PolicyFlags::SYSTEM_PARTITION | PolicyFlags::PUBLIC,
228 /* enforce_overlayable */ true);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700229
230 ASSERT_TRUE(resources) << resources.GetErrorMessage();
231 auto& res = *resources;
232 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700233 ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800234 R::overlay::string::policy_public, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700235 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800236 R::overlay::string::policy_system, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700237 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800238 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.
243TEST(ResourceMappingTests, CreateIdmapFromApkAssetsPolicySystemPublicInvalid) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800244 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800245 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 Mitchell9e4f52b2019-09-19 12:15:52 -0700258}
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.
263TEST(ResourceMappingTests, ResourcesFromApkAssetsPolicySystemPublicInvalidIgnoreOverlayable) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800264 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800265 TestConstants::OVERLAY_NAME_ALL_POLICIES,
Winson62ac8b52019-12-04 08:36:48 -0800266 PolicyFlags::SYSTEM_PARTITION | PolicyFlags::PUBLIC,
267 /* enforce_overlayable */ false);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700268
269 ASSERT_TRUE(resources) << resources.GetErrorMessage();
270 auto& res = *resources;
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200271 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 11U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700272 ASSERT_RESULT(MappingExists(res, R::target::string::not_overlayable,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800273 R::overlay::string::not_overlayable, true /* rewrite */));
274 ASSERT_RESULT(
275 MappingExists(res, R::target::string::other, R::overlay::string::other, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700276 ASSERT_RESULT(MappingExists(res, R::target::string::policy_actor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800277 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 Mitchellbf1f45b2020-09-29 17:22:52 -0700282 ASSERT_RESULT(MappingExists(res, R::target::string::policy_product,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800283 R::overlay::string::policy_product, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700284 ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800285 R::overlay::string::policy_public, true /* rewrite */));
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200286 ASSERT_RESULT(MappingExists(res, R::target::string::policy_config_signature,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800287 R::overlay::string::policy_config_signature, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700288 ASSERT_RESULT(MappingExists(res, R::target::string::policy_signature,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800289 R::overlay::string::policy_signature, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700290 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800291 R::overlay::string::policy_system, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700292 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800293 R::overlay::string::policy_system_vendor, true /* rewrite */));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700294}
295
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800296// Overlays that do not target an <overlayable> tag can overlay any resource if overlayable
297// enforcement is disabled.
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700298TEST(ResourceMappingTests, ResourcesFromApkAssetsNoDefinedOverlayableAndNoTargetName) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800299 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay-legacy.apk", "",
Winson62ac8b52019-12-04 08:36:48 -0800300 PolicyFlags::PUBLIC,
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700301 /* enforce_overlayable */ false);
302
303 ASSERT_TRUE(resources) << resources.GetErrorMessage();
304 auto& res = *resources;
305 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 4U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700306 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 Mitchell9e4f52b2019-09-19 12:15:52 -0700314}
315
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700316// Overlays that are neither pre-installed nor signed with the same signature as the target cannot
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700317// overlay packages that have not defined overlayable resources.
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700318TEST(ResourceMappingTests, ResourcesFromApkAssetsDefaultPoliciesPublicFail) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800319 auto resources = TestGetResourceMapping("target/target-no-overlayable.apk", "overlay/overlay.apk",
320 "NoTargetName", PolicyFlags::PUBLIC,
321 /* enforce_overlayable */ true);
Ryan Mitchellaf93f5d2020-05-26 14:29:03 -0700322
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700323 ASSERT_TRUE(resources) << resources.GetErrorMessage();
324 ASSERT_EQ(resources->GetTargetToOverlayMap().size(), 0U);
325}
326
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700327// 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 Mitchelle88ef742020-06-03 12:55:02 -0700330TEST(ResourceMappingTests, ResourcesFromApkAssetsDefaultPolicies) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800331 auto CheckEntries = [&](const PolicyBitmask& fulfilled_policies) {
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800332 auto resources =
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800333 TestGetResourceMapping("target/target-no-overlayable.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800334 TestConstants::OVERLAY_NAME_ALL_POLICIES, fulfilled_policies,
335 /* enforce_overlayable */ true);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700336
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700337 ASSERT_TRUE(resources) << resources.GetErrorMessage();
338 auto& res = *resources;
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200339 ASSERT_EQ(resources->GetTargetToOverlayMap().size(), 11U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700340 ASSERT_RESULT(MappingExists(res, R::target::string::not_overlayable,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800341 R::overlay::string::not_overlayable, true /* rewrite */));
342 ASSERT_RESULT(MappingExists(res, R::target::string::other, R::overlay::string::other,
343 true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700344 ASSERT_RESULT(MappingExists(res, R::target::string::policy_actor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800345 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 Mitchellbf1f45b2020-09-29 17:22:52 -0700350 ASSERT_RESULT(MappingExists(res, R::target::string::policy_product,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800351 R::overlay::string::policy_product, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700352 ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800353 R::overlay::string::policy_public, true /* rewrite */));
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200354 ASSERT_RESULT(MappingExists(res, R::target::string::policy_config_signature,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800355 R::overlay::string::policy_config_signature, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700356 ASSERT_RESULT(MappingExists(res, R::target::string::policy_signature,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800357 R::overlay::string::policy_signature, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700358 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800359 R::overlay::string::policy_system, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700360 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800361 R::overlay::string::policy_system_vendor, true /* rewrite */));
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700362 };
363
364 CheckEntries(PolicyFlags::SIGNATURE);
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200365 CheckEntries(PolicyFlags::CONFIG_SIGNATURE);
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700366 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 Mitchell9e4f52b2019-09-19 12:15:52 -0700371}
372
373} // namespace android::idmap2