blob: 568e041ebe5bdaead095f699d64c00229cca089d [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;
Yurii Zubrytskyic357f712023-04-13 02:32:45 -070062 AssetManager2::ApkAssetsPtr system_assets_;
63 AssetManager2::ApkAssetsPtr overlay_assets_;
64 AssetManager2::ApkAssetsPtr 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();
Yurii Zubrytskyic357f712023-04-13 02:32:45 -070070 const ResStringPool* string_pool =
71 assets[value.cookie].promote()->GetLoadedArsc()->GetStringPool();
Ryan Mitchell8a891d82019-07-01 09:48:23 -070072 return GetStringFromPool(string_pool, value.data);
73}
74
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070075}
76
Adam Lesinskied69ce82017-03-20 10:55:01 -070077TEST_F(IdmapTest, OverlayOverridesResourceValue) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -070078 AssetManager2 asset_manager;
Yurii Zubrytskyic357f712023-04-13 02:32:45 -070079 asset_manager.SetApkAssets({system_assets_, overlayable_assets_, overlay_assets_});
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;
Yurii Zubrytskyic357f712023-04-13 02:32:45 -070090 asset_manager.SetApkAssets({system_assets_, overlayable_assets_, overlay_assets_});
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000091
92 auto value = asset_manager.GetResource(overlayable::R::string::overlayable10);
93 ASSERT_TRUE(value.has_value());
94 ASSERT_EQ(value->cookie, 0U);
95 ASSERT_EQ(value->type, Res_value::TYPE_STRING);
96 ASSERT_EQ("yes", GetStringFromApkAssets(asset_manager, *value));
Ryan Mitchell8a891d82019-07-01 09:48:23 -070097}
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070098
Ryan Mitchell8a891d82019-07-01 09:48:23 -070099TEST_F(IdmapTest, OverlayOverridesResourceValueUsingInternalResource) {
100 AssetManager2 asset_manager;
Yurii Zubrytskyic357f712023-04-13 02:32:45 -0700101 asset_manager.SetApkAssets({system_assets_, overlayable_assets_, overlay_assets_});
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000102
103 auto value = asset_manager.GetResource(overlayable::R::string::overlayable8);
104 ASSERT_TRUE(value.has_value());
105 ASSERT_EQ(value->cookie, 2U);
106 ASSERT_EQ(value->type, Res_value::TYPE_REFERENCE);
107 ASSERT_EQ(value->data, (overlay::R::string::internal & 0x00ffffffU) | (0x02U << 24));
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700108}
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700109
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700110TEST_F(IdmapTest, OverlayOverridesResourceValueUsingInlineInteger) {
111 AssetManager2 asset_manager;
Yurii Zubrytskyic357f712023-04-13 02:32:45 -0700112 asset_manager.SetApkAssets({system_assets_, overlayable_assets_, overlay_assets_});
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000113
114 auto value = asset_manager.GetResource(overlayable::R::integer::config_integer);
115 ASSERT_TRUE(value.has_value());
116 ASSERT_EQ(value->cookie, 2U);
117 ASSERT_EQ(value->type, Res_value::TYPE_INT_DEC);
118 ASSERT_EQ(value->data, 42);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700119}
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700120
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700121TEST_F(IdmapTest, OverlayOverridesResourceValueUsingInlineString) {
122 AssetManager2 asset_manager;
Yurii Zubrytskyic357f712023-04-13 02:32:45 -0700123 asset_manager.SetApkAssets({system_assets_, overlayable_assets_, overlay_assets_});
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700124
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000125 auto value = asset_manager.GetResource(overlayable::R::string::overlayable11);
126 ASSERT_TRUE(value.has_value());
127 ASSERT_EQ(value->cookie, 2U);
128 ASSERT_EQ(value->type, Res_value::TYPE_STRING);
129 ASSERT_EQ("Hardcoded string", GetStringFromApkAssets(asset_manager, *value));
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700130}
131
132TEST_F(IdmapTest, OverlayOverridesResourceValueUsingOverlayingResource) {
133 AssetManager2 asset_manager;
Yurii Zubrytskyic357f712023-04-13 02:32:45 -0700134 asset_manager.SetApkAssets({system_assets_, overlayable_assets_, overlay_assets_});
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000135
136 auto value = asset_manager.GetResource(overlayable::R::string::overlayable9);
137 ASSERT_TRUE(value.has_value());
138 ASSERT_EQ(value->cookie, 2U);
139 ASSERT_EQ(value->type, Res_value::TYPE_REFERENCE);
140 ASSERT_EQ(value->data, overlayable::R::string::overlayable7);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700141}
142
143TEST_F(IdmapTest, OverlayOverridesXmlParser) {
144 AssetManager2 asset_manager;
Yurii Zubrytskyic357f712023-04-13 02:32:45 -0700145 asset_manager.SetApkAssets({system_assets_, overlayable_assets_, overlay_assets_});
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700146
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000147 auto value = asset_manager.GetResource(overlayable::R::layout::hello_view);
148 ASSERT_TRUE(value.has_value());
149 ASSERT_EQ(value->cookie, 2U);
150 ASSERT_EQ(value->type, Res_value::TYPE_STRING);
151 ASSERT_EQ("res/layout/hello_view.xml", GetStringFromApkAssets(asset_manager, *value));
152
153 auto asset = asset_manager.OpenNonAsset("res/layout/hello_view.xml", value->cookie,
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700154 Asset::ACCESS_RANDOM);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000155 auto dynamic_ref_table = asset_manager.GetDynamicRefTableForCookie(value->cookie);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700156 auto xml_tree = util::make_unique<ResXMLTree>(std::move(dynamic_ref_table));
157 status_t err = xml_tree->setTo(asset->getBuffer(true), asset->getLength(), false);
158 ASSERT_EQ(err, NO_ERROR);
159
160 while (xml_tree->next() != ResXMLParser::START_TAG) { }
161
162 // The resource id of @id/hello_view should be rewritten to the resource id/hello_view within the
163 // target.
164 ASSERT_EQ(xml_tree->getAttributeNameResID(0), 0x010100d0 /* android:attr/id */);
165 ASSERT_EQ(xml_tree->getAttributeDataType(0), Res_value::TYPE_REFERENCE);
166 ASSERT_EQ(xml_tree->getAttributeData(0), overlayable::R::id::hello_view);
167
168 // The resource id of @android:string/yes should not be rewritten even though it overlays
169 // string/overlayable10 in the target.
170 ASSERT_EQ(xml_tree->getAttributeNameResID(1), 0x0101014f /* android:attr/text */);
171 ASSERT_EQ(xml_tree->getAttributeDataType(1), Res_value::TYPE_REFERENCE);
172 ASSERT_EQ(xml_tree->getAttributeData(1), 0x01040013 /* android:string/yes */);
173
174 // The resource id of the attribute within the overlay should be rewritten to the resource id of
175 // the attribute in the target.
176 ASSERT_EQ(xml_tree->getAttributeNameResID(2), overlayable::R::attr::max_lines);
177 ASSERT_EQ(xml_tree->getAttributeDataType(2), Res_value::TYPE_INT_DEC);
178 ASSERT_EQ(xml_tree->getAttributeData(2), 4);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700179}
180
Adam Lesinskied69ce82017-03-20 10:55:01 -0700181TEST_F(IdmapTest, OverlaidResourceHasSameName) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700182 AssetManager2 asset_manager;
Yurii Zubrytskyic357f712023-04-13 02:32:45 -0700183 asset_manager.SetApkAssets({system_assets_, overlayable_assets_, overlay_assets_});
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700184
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000185 auto name = asset_manager.GetResourceName(overlayable::R::string::overlayable9);
186 ASSERT_TRUE(name.has_value());
187 ASSERT_EQ("com.android.overlayable", std::string(name->package));
188 ASSERT_EQ(std::u16string(u"string"), std::u16string(name->type16));
189 ASSERT_EQ("overlayable9", std::string(name->entry));
Adam Lesinskied69ce82017-03-20 10:55:01 -0700190}
191
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700192TEST_F(IdmapTest, OverlayLoaderInterop) {
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800193 auto asset = AssetsProvider::CreateAssetFromFile(GetTestDataPath() + "/loader/resources.arsc");
194 ASSERT_THAT(asset, NotNull());
195
196 auto loader_assets = ApkAssets::LoadTable(std::move(asset), EmptyAssetsProvider::Create(),
197 PROPERTY_LOADER);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700198 AssetManager2 asset_manager;
Yurii Zubrytskyic357f712023-04-13 02:32:45 -0700199 asset_manager.SetApkAssets({overlayable_assets_, loader_assets, overlay_assets_});
Adam Lesinskied69ce82017-03-20 10:55:01 -0700200
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000201 auto value = asset_manager.GetResource(overlayable::R::string::overlayable11);
202 ASSERT_TRUE(value.has_value());
203 ASSERT_EQ(1U, value->cookie);
204 ASSERT_EQ(Res_value::TYPE_STRING, value->type);
205 ASSERT_EQ("loader", GetStringFromApkAssets(asset_manager, *value));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700206}
207
Ryan Mitchella9093052020-03-26 17:15:01 -0700208TEST_F(IdmapTest, OverlayAssetsIsUpToDate) {
209 std::string idmap_contents;
210 ASSERT_TRUE(base::ReadFileToString("overlay/overlay.idmap", &idmap_contents));
211
212 TemporaryFile temp_file;
213 ASSERT_TRUE(base::WriteStringToFile(idmap_contents, temp_file.path));
214
215 auto apk_assets = ApkAssets::LoadOverlay(temp_file.path);
216 ASSERT_NE(nullptr, apk_assets);
217 ASSERT_TRUE(apk_assets->IsUpToDate());
218
219 unlink(temp_file.path);
220 ASSERT_FALSE(apk_assets->IsUpToDate());
221 sleep(2);
222
223 base::WriteStringToFile("hello", temp_file.path);
224 sleep(2);
225
226 ASSERT_FALSE(apk_assets->IsUpToDate());
227}
228
Adam Lesinski4c67a472016-11-10 16:43:59 -0800229} // namespace