blob: b43491548e2b3e99184aa1e88af0df3244b2ca7b [file] [log] [blame]
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001/*
2 * Copyright (C) 2014 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 Mitchell8a891d82019-07-01 09:48:23 -070017#include "android-base/file.h"
18#include "androidfw/ApkAssets.h"
19#include "androidfw/AssetManager2.h"
Adam Lesinski4c67a472016-11-10 16:43:59 -080020#include "androidfw/ResourceTypes.h"
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070021
Adam Lesinski4c67a472016-11-10 16:43:59 -080022#include "utils/String16.h"
23#include "utils/String8.h"
24
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070025#include "TestHelpers.h"
Ryan Mitchell8a891d82019-07-01 09:48:23 -070026#include "data/overlay/R.h"
27#include "data/overlayable/R.h"
28#include "data/system/R.h"
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070029
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080030using ::testing::NotNull;
31
Ryan Mitchell8a891d82019-07-01 09:48:23 -070032namespace overlay = com::android::overlay;
33namespace overlayable = com::android::overlayable;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070034
Adam Lesinski4c67a472016-11-10 16:43:59 -080035namespace android {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070036
Ryan Mitchell8a891d82019-07-01 09:48:23 -070037namespace {
38
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070039class IdmapTest : public ::testing::Test {
Adam Lesinski4c67a472016-11-10 16:43:59 -080040 protected:
41 void SetUp() override {
Ryan Mitchell8a891d82019-07-01 09:48:23 -070042 // Move to the test data directory so the idmap can locate the overlay APK.
Ryan Mitchella9093052020-03-26 17:15:01 -070043 original_path = base::GetExecutableDirectory();
Ryan Mitchell8a891d82019-07-01 09:48:23 -070044 chdir(GetTestDataPath().c_str());
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070045
Ryan Mitchell8a891d82019-07-01 09:48:23 -070046 system_assets_ = ApkAssets::Load("system/system.apk");
47 ASSERT_NE(nullptr, system_assets_);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070048
Ryan Mitchell8a891d82019-07-01 09:48:23 -070049 overlay_assets_ = ApkAssets::LoadOverlay("overlay/overlay.idmap");
50 ASSERT_NE(nullptr, overlay_assets_);
51
52 overlayable_assets_ = ApkAssets::Load("overlayable/overlayable.apk");
53 ASSERT_NE(nullptr, overlayable_assets_);
Ryan Mitchella9093052020-03-26 17:15:01 -070054 }
55
56 void TearDown() override {
Ryan Mitchell8a891d82019-07-01 09:48:23 -070057 chdir(original_path.c_str());
Adam Lesinski4c67a472016-11-10 16:43:59 -080058 }
59
Ryan Mitchell8a891d82019-07-01 09:48:23 -070060 protected:
Ryan Mitchella9093052020-03-26 17:15:01 -070061 std::string original_path;
Ryan Mitchell8a891d82019-07-01 09:48:23 -070062 std::unique_ptr<const ApkAssets> system_assets_;
63 std::unique_ptr<const ApkAssets> overlay_assets_;
64 std::unique_ptr<const ApkAssets> overlayable_assets_;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070065};
66
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000067std::string GetStringFromApkAssets(const AssetManager2& asset_manager,
68 const AssetManager2::SelectedValue& value) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -070069 auto assets = asset_manager.GetApkAssets();
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000070 const ResStringPool* string_pool = assets[value.cookie]->GetLoadedArsc()->GetStringPool();
Ryan Mitchell8a891d82019-07-01 09:48:23 -070071 return GetStringFromPool(string_pool, value.data);
72}
73
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070074}
75
Adam Lesinskied69ce82017-03-20 10:55:01 -070076TEST_F(IdmapTest, OverlayOverridesResourceValue) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -070077 AssetManager2 asset_manager;
78 asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(),
79 overlay_assets_.get()});
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000080
81 auto value = asset_manager.GetResource(overlayable::R::string::overlayable5);
82 ASSERT_TRUE(value.has_value());
83 ASSERT_EQ(value->cookie, 2U);
84 ASSERT_EQ(value->type, Res_value::TYPE_STRING);
85 ASSERT_EQ("Overlay One", GetStringFromApkAssets(asset_manager, *value));
Ryan Mitchell8a891d82019-07-01 09:48:23 -070086}
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070087
Ryan Mitchell8a891d82019-07-01 09:48:23 -070088TEST_F(IdmapTest, OverlayOverridesResourceValueUsingDifferentPackage) {
89 AssetManager2 asset_manager;
90 asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(),
91 overlay_assets_.get()});
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000092
93 auto value = asset_manager.GetResource(overlayable::R::string::overlayable10);
94 ASSERT_TRUE(value.has_value());
95 ASSERT_EQ(value->cookie, 0U);
96 ASSERT_EQ(value->type, Res_value::TYPE_STRING);
97 ASSERT_EQ("yes", GetStringFromApkAssets(asset_manager, *value));
Ryan Mitchell8a891d82019-07-01 09:48:23 -070098}
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070099
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700100TEST_F(IdmapTest, OverlayOverridesResourceValueUsingInternalResource) {
101 AssetManager2 asset_manager;
102 asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(),
103 overlay_assets_.get()});
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000104
105 auto value = asset_manager.GetResource(overlayable::R::string::overlayable8);
106 ASSERT_TRUE(value.has_value());
107 ASSERT_EQ(value->cookie, 2U);
108 ASSERT_EQ(value->type, Res_value::TYPE_REFERENCE);
109 ASSERT_EQ(value->data, (overlay::R::string::internal & 0x00ffffffU) | (0x02U << 24));
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700110}
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700111
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700112TEST_F(IdmapTest, OverlayOverridesResourceValueUsingInlineInteger) {
113 AssetManager2 asset_manager;
114 asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(),
115 overlay_assets_.get()});
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000116
117 auto value = asset_manager.GetResource(overlayable::R::integer::config_integer);
118 ASSERT_TRUE(value.has_value());
119 ASSERT_EQ(value->cookie, 2U);
120 ASSERT_EQ(value->type, Res_value::TYPE_INT_DEC);
121 ASSERT_EQ(value->data, 42);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700122}
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700123
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700124TEST_F(IdmapTest, OverlayOverridesResourceValueUsingInlineString) {
125 AssetManager2 asset_manager;
126 asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(),
127 overlay_assets_.get()});
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700128
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000129 auto value = asset_manager.GetResource(overlayable::R::string::overlayable11);
130 ASSERT_TRUE(value.has_value());
131 ASSERT_EQ(value->cookie, 2U);
132 ASSERT_EQ(value->type, Res_value::TYPE_STRING);
133 ASSERT_EQ("Hardcoded string", GetStringFromApkAssets(asset_manager, *value));
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700134}
135
136TEST_F(IdmapTest, OverlayOverridesResourceValueUsingOverlayingResource) {
137 AssetManager2 asset_manager;
138 asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(),
139 overlay_assets_.get()});
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000140
141 auto value = asset_manager.GetResource(overlayable::R::string::overlayable9);
142 ASSERT_TRUE(value.has_value());
143 ASSERT_EQ(value->cookie, 2U);
144 ASSERT_EQ(value->type, Res_value::TYPE_REFERENCE);
145 ASSERT_EQ(value->data, overlayable::R::string::overlayable7);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700146}
147
148TEST_F(IdmapTest, OverlayOverridesXmlParser) {
149 AssetManager2 asset_manager;
150 asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(),
151 overlay_assets_.get()});
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700152
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000153 auto value = asset_manager.GetResource(overlayable::R::layout::hello_view);
154 ASSERT_TRUE(value.has_value());
155 ASSERT_EQ(value->cookie, 2U);
156 ASSERT_EQ(value->type, Res_value::TYPE_STRING);
157 ASSERT_EQ("res/layout/hello_view.xml", GetStringFromApkAssets(asset_manager, *value));
158
159 auto asset = asset_manager.OpenNonAsset("res/layout/hello_view.xml", value->cookie,
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700160 Asset::ACCESS_RANDOM);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000161 auto dynamic_ref_table = asset_manager.GetDynamicRefTableForCookie(value->cookie);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700162 auto xml_tree = util::make_unique<ResXMLTree>(std::move(dynamic_ref_table));
163 status_t err = xml_tree->setTo(asset->getBuffer(true), asset->getLength(), false);
164 ASSERT_EQ(err, NO_ERROR);
165
166 while (xml_tree->next() != ResXMLParser::START_TAG) { }
167
168 // The resource id of @id/hello_view should be rewritten to the resource id/hello_view within the
169 // target.
170 ASSERT_EQ(xml_tree->getAttributeNameResID(0), 0x010100d0 /* android:attr/id */);
171 ASSERT_EQ(xml_tree->getAttributeDataType(0), Res_value::TYPE_REFERENCE);
172 ASSERT_EQ(xml_tree->getAttributeData(0), overlayable::R::id::hello_view);
173
174 // The resource id of @android:string/yes should not be rewritten even though it overlays
175 // string/overlayable10 in the target.
176 ASSERT_EQ(xml_tree->getAttributeNameResID(1), 0x0101014f /* android:attr/text */);
177 ASSERT_EQ(xml_tree->getAttributeDataType(1), Res_value::TYPE_REFERENCE);
178 ASSERT_EQ(xml_tree->getAttributeData(1), 0x01040013 /* android:string/yes */);
179
180 // The resource id of the attribute within the overlay should be rewritten to the resource id of
181 // the attribute in the target.
182 ASSERT_EQ(xml_tree->getAttributeNameResID(2), overlayable::R::attr::max_lines);
183 ASSERT_EQ(xml_tree->getAttributeDataType(2), Res_value::TYPE_INT_DEC);
184 ASSERT_EQ(xml_tree->getAttributeData(2), 4);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700185}
186
Adam Lesinskied69ce82017-03-20 10:55:01 -0700187TEST_F(IdmapTest, OverlaidResourceHasSameName) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700188 AssetManager2 asset_manager;
189 asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(),
190 overlay_assets_.get()});
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700191
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000192 auto name = asset_manager.GetResourceName(overlayable::R::string::overlayable9);
193 ASSERT_TRUE(name.has_value());
194 ASSERT_EQ("com.android.overlayable", std::string(name->package));
195 ASSERT_EQ(std::u16string(u"string"), std::u16string(name->type16));
196 ASSERT_EQ("overlayable9", std::string(name->entry));
Adam Lesinskied69ce82017-03-20 10:55:01 -0700197}
198
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700199TEST_F(IdmapTest, OverlayLoaderInterop) {
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800200 auto asset = AssetsProvider::CreateAssetFromFile(GetTestDataPath() + "/loader/resources.arsc");
201 ASSERT_THAT(asset, NotNull());
202
203 auto loader_assets = ApkAssets::LoadTable(std::move(asset), EmptyAssetsProvider::Create(),
204 PROPERTY_LOADER);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700205 AssetManager2 asset_manager;
206 asset_manager.SetApkAssets({overlayable_assets_.get(), loader_assets.get(),
207 overlay_assets_.get()});
Adam Lesinskied69ce82017-03-20 10:55:01 -0700208
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000209 auto value = asset_manager.GetResource(overlayable::R::string::overlayable11);
210 ASSERT_TRUE(value.has_value());
211 ASSERT_EQ(1U, value->cookie);
212 ASSERT_EQ(Res_value::TYPE_STRING, value->type);
213 ASSERT_EQ("loader", GetStringFromApkAssets(asset_manager, *value));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700214}
215
Ryan Mitchella9093052020-03-26 17:15:01 -0700216TEST_F(IdmapTest, OverlayAssetsIsUpToDate) {
217 std::string idmap_contents;
218 ASSERT_TRUE(base::ReadFileToString("overlay/overlay.idmap", &idmap_contents));
219
220 TemporaryFile temp_file;
221 ASSERT_TRUE(base::WriteStringToFile(idmap_contents, temp_file.path));
222
223 auto apk_assets = ApkAssets::LoadOverlay(temp_file.path);
224 ASSERT_NE(nullptr, apk_assets);
225 ASSERT_TRUE(apk_assets->IsUpToDate());
226
227 unlink(temp_file.path);
228 ASSERT_FALSE(apk_assets->IsUpToDate());
229 sleep(2);
230
231 base::WriteStringToFile("hello", temp_file.path);
232 sleep(2);
233
234 ASSERT_FALSE(apk_assets->IsUpToDate());
235}
236
Adam Lesinski4c67a472016-11-10 16:43:59 -0800237} // namespace