blob: 5a1d808af06f28f0cf362610ae8ea3f139eb635c [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
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070032using android::Res_value;
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070033
Winson62ac8b52019-12-04 08:36:48 -080034using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
35
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070036namespace android::idmap2 {
37
38#define ASSERT_RESULT(r) \
39 do { \
40 auto result = r; \
41 ASSERT_TRUE(result) << result.GetErrorMessage(); \
42 } while (0)
43
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080044Result<ResourceMapping> TestGetResourceMapping(const std::string& local_target_path,
45 const std::string& local_overlay_path,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -080046 const std::string& overlay_name,
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070047 const PolicyBitmask& fulfilled_policies,
48 bool enforce_overlayable) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080049 const std::string target_path = (local_target_path[0] == '/')
50 ? local_target_path
51 : (GetTestDataPath() + "/" + local_target_path);
52 auto target = TargetResourceContainer::FromPath(target_path);
53 if (!target) {
54 return Error(target.GetError(), R"(Failed to load target "%s")", target_path.c_str());
55 }
56
57 const std::string overlay_path = (local_overlay_path[0] == '/')
58 ? local_overlay_path
59 : (GetTestDataPath() + "/" + local_overlay_path);
60 auto overlay = OverlayResourceContainer::FromPath(overlay_path);
61 if (!overlay) {
62 return Error(overlay.GetError(), R"(Failed to load overlay "%s")", overlay_path.c_str());
63 }
64
65 auto overlay_info = (*overlay)->FindOverlayInfo(overlay_name);
Ryan Mitchell30dc2e02020-12-02 11:43:18 -080066 if (!overlay_info) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080067 return Error(overlay_info.GetError(), R"(Failed to find overlay name "%s")",
68 overlay_name.c_str());
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070069 }
70
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020071 LogInfo log_info;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080072 return ResourceMapping::FromContainers(**target, **overlay, *overlay_info, fulfilled_policies,
73 enforce_overlayable, log_info);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070074}
75
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070076Result<Unit> MappingExists(const ResourceMapping& mapping, ResourceId target_resource,
77 ResourceId overlay_resource, bool rewrite) {
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070078 auto target_map = mapping.GetTargetToOverlayMap();
79 auto entry_map = target_map.find(target_resource);
80 if (entry_map == target_map.end()) {
81 return Error("Failed to find mapping for target resource");
82 }
83
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070084 auto actual_overlay_resource = std::get_if<ResourceId>(&entry_map->second);
85 if (actual_overlay_resource == nullptr) {
86 return Error("Target resource is not mapped to an overlay resource id");
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070087 }
88
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070089 if (*actual_overlay_resource != overlay_resource) {
90 return Error(R"(Expected id: "0x%02x" Actual id: "0x%02x")", overlay_resource,
91 *actual_overlay_resource);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070092 }
93
94 auto overlay_map = mapping.GetOverlayToTargetMap();
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070095 auto overlay_iter = overlay_map.find(overlay_resource);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070096 if ((overlay_iter != overlay_map.end()) != rewrite) {
97 return Error(R"(Expected rewriting: "%s")", rewrite ? "true" : "false");
98 }
99
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700100 if (rewrite && overlay_iter->second != target_resource) {
101 return Error(R"(Expected rewrite id: "0x%02x" Actual id: "0x%02x")", target_resource,
102 overlay_iter->second);
103 }
104
105 return Result<Unit>({});
106}
107
108Result<Unit> MappingExists(const ResourceMapping& mapping, const ResourceId& target_resource,
109 const uint8_t type, const uint32_t value) {
110 auto target_map = mapping.GetTargetToOverlayMap();
111 auto entry_map = target_map.find(target_resource);
112 if (entry_map == target_map.end()) {
113 return Error("Failed to find mapping for target resource");
114 }
115
116 auto actual_overlay_value = std::get_if<TargetValue>(&entry_map->second);
117 if (actual_overlay_value == nullptr) {
118 return Error("Target resource is not mapped to an inline value");
119 }
120
121 if (actual_overlay_value->data_type != type) {
122 return Error(R"(Expected type: "0x%02x" Actual type: "0x%02x")", type,
123 actual_overlay_value->data_type);
124 }
125
126 if (actual_overlay_value->data_value != value) {
127 return Error(R"(Expected value: "0x%08x" Actual value: "0x%08x")", type,
128 actual_overlay_value->data_value);
129 }
130
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700131 return Result<Unit>({});
132}
133
134TEST(ResourceMappingTests, ResourcesFromApkAssetsLegacy) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800135 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay-legacy.apk", "",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800136 PolicyFlags::PUBLIC, /* enforce_overlayable */ false);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700137
138 ASSERT_TRUE(resources) << resources.GetErrorMessage();
139 auto& res = *resources;
140 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 4U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700141 ASSERT_RESULT(
142 MappingExists(res, R::target::integer::int1, R::overlay::integer::int1, false /* rewrite */));
143 ASSERT_RESULT(
144 MappingExists(res, R::target::string::str1, R::overlay::string::str1, false /* rewrite */));
145 ASSERT_RESULT(
146 MappingExists(res, R::target::string::str3, R::overlay::string::str3, false /* rewrite */));
147 ASSERT_RESULT(
148 MappingExists(res, R::target::string::str4, R::overlay::string::str4, false /* rewrite */));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700149}
150
151TEST(ResourceMappingTests, ResourcesFromApkAssetsNonMatchingNames) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800152 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk", "SwapNames",
Winson62ac8b52019-12-04 08:36:48 -0800153 PolicyFlags::PUBLIC,
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700154 /* enforce_overlayable */ false);
155
156 ASSERT_TRUE(resources) << resources.GetErrorMessage();
157 auto& res = *resources;
158 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700159 ASSERT_RESULT(
160 MappingExists(res, R::target::string::str1, R::overlay::string::str4, true /* rewrite */));
161 ASSERT_RESULT(
162 MappingExists(res, R::target::string::str3, R::overlay::string::str1, true /* rewrite */));
163 ASSERT_RESULT(
164 MappingExists(res, R::target::string::str4, R::overlay::string::str3, true /* rewrite */));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700165}
166
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700167TEST(ResourceMappingTests, DoNotRewriteNonOverlayResourceId) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800168 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800169 "DifferentPackages", PolicyFlags::PUBLIC,
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700170 /* enforce_overlayable */ false);
171
172 ASSERT_TRUE(resources) << resources.GetErrorMessage();
173 auto& res = *resources;
174 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 2U);
175 ASSERT_EQ(res.GetOverlayToTargetMap().size(), 1U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700176 ASSERT_RESULT(MappingExists(res, R::target::string::str1, 0x0104000a,
Winson62ac8b52019-12-04 08:36:48 -0800177 false /* rewrite */)); // -> android:string/ok
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800178 ASSERT_RESULT(
179 MappingExists(res, R::target::string::str3, R::overlay::string::str3, true /* rewrite */));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700180}
181
182TEST(ResourceMappingTests, InlineResources) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800183 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk", "Inline",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800184 PolicyFlags::PUBLIC, /* enforce_overlayable */ false);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700185
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800186 constexpr size_t overlay_string_pool_size = 10U;
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700187 ASSERT_TRUE(resources) << resources.GetErrorMessage();
188 auto& res = *resources;
189 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 2U);
190 ASSERT_EQ(res.GetOverlayToTargetMap().size(), 0U);
Winson62ac8b52019-12-04 08:36:48 -0800191 ASSERT_RESULT(MappingExists(res, R::target::string::str1, Res_value::TYPE_STRING,
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700192 overlay_string_pool_size + 0U)); // -> "Hello World"
193 ASSERT_RESULT(MappingExists(res, R::target::integer::int1, Res_value::TYPE_INT_DEC, 73U));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700194}
195
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800196TEST(ResourceMappingTests, FabricatedOverlay) {
197 auto frro = FabricatedOverlay::Builder("com.example.overlay", "SandTheme", "test.target")
198 .SetOverlayable("TestResources")
199 .SetResourceValue("integer/int1", Res_value::TYPE_INT_DEC, 2U)
200 .SetResourceValue("string/str1", Res_value::TYPE_REFERENCE, 0x7f010000)
201 .Build();
202
203 ASSERT_TRUE(frro);
204 TemporaryFile tf;
205 std::ofstream out(tf.path);
206 ASSERT_TRUE((*frro).ToBinaryStream(out));
207 out.close();
208
209 auto resources = TestGetResourceMapping("target/target.apk", tf.path, "SandTheme",
210 PolicyFlags::PUBLIC, /* enforce_overlayable */ false);
211
212 ASSERT_TRUE(resources) << resources.GetErrorMessage();
213 auto& res = *resources;
214 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 2U);
215 ASSERT_EQ(res.GetOverlayToTargetMap().size(), 0U);
216 ASSERT_RESULT(MappingExists(res, R::target::string::str1, Res_value::TYPE_REFERENCE, 0x7f010000));
217 ASSERT_RESULT(MappingExists(res, R::target::integer::int1, Res_value::TYPE_INT_DEC, 2U));
218}
219
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700220TEST(ResourceMappingTests, CreateIdmapFromApkAssetsPolicySystemPublic) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800221 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800222 TestConstants::OVERLAY_NAME_ALL_POLICIES,
Winson62ac8b52019-12-04 08:36:48 -0800223 PolicyFlags::SYSTEM_PARTITION | PolicyFlags::PUBLIC,
224 /* enforce_overlayable */ true);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700225
226 ASSERT_TRUE(resources) << resources.GetErrorMessage();
227 auto& res = *resources;
228 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700229 ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800230 R::overlay::string::policy_public, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700231 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800232 R::overlay::string::policy_system, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700233 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800234 R::overlay::string::policy_system_vendor, true /* rewrite */));
235}
236
237// Resources that are not declared as overlayable and resources that a protected by policies the
238// overlay does not fulfill must not map to overlay resources.
239TEST(ResourceMappingTests, CreateIdmapFromApkAssetsPolicySystemPublicInvalid) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800240 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800241 TestConstants::OVERLAY_NAME_ALL_POLICIES,
242 PolicyFlags::SYSTEM_PARTITION | PolicyFlags::PUBLIC,
243 /* enforce_overlayable */ true);
244
245 ASSERT_TRUE(resources) << resources.GetErrorMessage();
246 auto& res = *resources;
247 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 3U);
248 ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
249 R::overlay::string::policy_public, true /* rewrite */));
250 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
251 R::overlay::string::policy_system, true /* rewrite */));
252 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
253 R::overlay::string::policy_system_vendor, true /* rewrite */));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700254}
255
256// Resources that are not declared as overlayable and resources that a protected by policies the
257// overlay does not fulfilled can map to overlay resources when overlayable enforcement is turned
258// off.
259TEST(ResourceMappingTests, ResourcesFromApkAssetsPolicySystemPublicInvalidIgnoreOverlayable) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800260 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800261 TestConstants::OVERLAY_NAME_ALL_POLICIES,
Winson62ac8b52019-12-04 08:36:48 -0800262 PolicyFlags::SYSTEM_PARTITION | PolicyFlags::PUBLIC,
263 /* enforce_overlayable */ false);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700264
265 ASSERT_TRUE(resources) << resources.GetErrorMessage();
266 auto& res = *resources;
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200267 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 11U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700268 ASSERT_RESULT(MappingExists(res, R::target::string::not_overlayable,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800269 R::overlay::string::not_overlayable, true /* rewrite */));
270 ASSERT_RESULT(
271 MappingExists(res, R::target::string::other, R::overlay::string::other, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700272 ASSERT_RESULT(MappingExists(res, R::target::string::policy_actor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800273 R::overlay::string::policy_actor, true /* rewrite */));
274 ASSERT_RESULT(MappingExists(res, R::target::string::policy_odm, R::overlay::string::policy_odm,
275 true /* rewrite */));
276 ASSERT_RESULT(MappingExists(res, R::target::string::policy_oem, R::overlay::string::policy_oem,
277 true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700278 ASSERT_RESULT(MappingExists(res, R::target::string::policy_product,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800279 R::overlay::string::policy_product, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700280 ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800281 R::overlay::string::policy_public, true /* rewrite */));
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200282 ASSERT_RESULT(MappingExists(res, R::target::string::policy_config_signature,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800283 R::overlay::string::policy_config_signature, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700284 ASSERT_RESULT(MappingExists(res, R::target::string::policy_signature,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800285 R::overlay::string::policy_signature, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700286 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800287 R::overlay::string::policy_system, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700288 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800289 R::overlay::string::policy_system_vendor, true /* rewrite */));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700290}
291
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800292// Overlays that do not target an <overlayable> tag can overlay any resource if overlayable
293// enforcement is disabled.
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700294TEST(ResourceMappingTests, ResourcesFromApkAssetsNoDefinedOverlayableAndNoTargetName) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800295 auto resources = TestGetResourceMapping("target/target.apk", "overlay/overlay-legacy.apk", "",
Winson62ac8b52019-12-04 08:36:48 -0800296 PolicyFlags::PUBLIC,
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700297 /* enforce_overlayable */ false);
298
299 ASSERT_TRUE(resources) << resources.GetErrorMessage();
300 auto& res = *resources;
301 ASSERT_EQ(res.GetTargetToOverlayMap().size(), 4U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700302 ASSERT_RESULT(
303 MappingExists(res, R::target::integer::int1, R::overlay::integer::int1, false /* rewrite */));
304 ASSERT_RESULT(
305 MappingExists(res, R::target::string::str1, R::overlay::string::str1, false /* rewrite */));
306 ASSERT_RESULT(
307 MappingExists(res, R::target::string::str3, R::overlay::string::str3, false /* rewrite */));
308 ASSERT_RESULT(
309 MappingExists(res, R::target::string::str4, R::overlay::string::str4, false /* rewrite */));
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700310}
311
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700312// Overlays that are neither pre-installed nor signed with the same signature as the target cannot
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700313// overlay packages that have not defined overlayable resources.
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700314TEST(ResourceMappingTests, ResourcesFromApkAssetsDefaultPoliciesPublicFail) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800315 auto resources = TestGetResourceMapping("target/target-no-overlayable.apk", "overlay/overlay.apk",
316 "NoTargetName", PolicyFlags::PUBLIC,
317 /* enforce_overlayable */ true);
Ryan Mitchellaf93f5d2020-05-26 14:29:03 -0700318
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700319 ASSERT_TRUE(resources) << resources.GetErrorMessage();
320 ASSERT_EQ(resources->GetTargetToOverlayMap().size(), 0U);
321}
322
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700323// Overlays that are pre-installed or are signed with the same signature as the target or are
324// signed with the same signature as the reference package can overlay packages that have not
325// defined overlayable resources.
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700326TEST(ResourceMappingTests, ResourcesFromApkAssetsDefaultPolicies) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800327 auto CheckEntries = [&](const PolicyBitmask& fulfilled_policies) {
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800328 auto resources =
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800329 TestGetResourceMapping("target/target-no-overlayable.apk", "overlay/overlay.apk",
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800330 TestConstants::OVERLAY_NAME_ALL_POLICIES, fulfilled_policies,
331 /* enforce_overlayable */ true);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700332
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700333 ASSERT_TRUE(resources) << resources.GetErrorMessage();
334 auto& res = *resources;
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200335 ASSERT_EQ(resources->GetTargetToOverlayMap().size(), 11U);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700336 ASSERT_RESULT(MappingExists(res, R::target::string::not_overlayable,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800337 R::overlay::string::not_overlayable, true /* rewrite */));
338 ASSERT_RESULT(MappingExists(res, R::target::string::other, R::overlay::string::other,
339 true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700340 ASSERT_RESULT(MappingExists(res, R::target::string::policy_actor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800341 R::overlay::string::policy_actor, true /* rewrite */));
342 ASSERT_RESULT(MappingExists(res, R::target::string::policy_odm, R::overlay::string::policy_odm,
343 true /* rewrite */));
344 ASSERT_RESULT(MappingExists(res, R::target::string::policy_oem, R::overlay::string::policy_oem,
345 true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700346 ASSERT_RESULT(MappingExists(res, R::target::string::policy_product,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800347 R::overlay::string::policy_product, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700348 ASSERT_RESULT(MappingExists(res, R::target::string::policy_public,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800349 R::overlay::string::policy_public, true /* rewrite */));
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200350 ASSERT_RESULT(MappingExists(res, R::target::string::policy_config_signature,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800351 R::overlay::string::policy_config_signature, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700352 ASSERT_RESULT(MappingExists(res, R::target::string::policy_signature,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800353 R::overlay::string::policy_signature, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700354 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800355 R::overlay::string::policy_system, true /* rewrite */));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700356 ASSERT_RESULT(MappingExists(res, R::target::string::policy_system_vendor,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800357 R::overlay::string::policy_system_vendor, true /* rewrite */));
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700358 };
359
360 CheckEntries(PolicyFlags::SIGNATURE);
Zoran Jovanovic0f942f92020-06-09 18:51:57 +0200361 CheckEntries(PolicyFlags::CONFIG_SIGNATURE);
Ryan Mitchelle88ef742020-06-03 12:55:02 -0700362 CheckEntries(PolicyFlags::PRODUCT_PARTITION);
363 CheckEntries(PolicyFlags::SYSTEM_PARTITION);
364 CheckEntries(PolicyFlags::VENDOR_PARTITION);
365 CheckEntries(PolicyFlags::ODM_PARTITION);
366 CheckEntries(PolicyFlags::OEM_PARTITION);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700367}
368
369} // namespace android::idmap2