Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 1 | /* |
| 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 Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 17 | #include "android-base/file.h" |
| 18 | #include "androidfw/ApkAssets.h" |
| 19 | #include "androidfw/AssetManager2.h" |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 20 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 21 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 22 | #include "utils/String16.h" |
| 23 | #include "utils/String8.h" |
| 24 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 25 | #include "TestHelpers.h" |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 26 | #include "data/overlay/R.h" |
| 27 | #include "data/overlayable/R.h" |
| 28 | #include "data/system/R.h" |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 29 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 30 | namespace overlay = com::android::overlay; |
| 31 | namespace overlayable = com::android::overlayable; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 33 | namespace android { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 34 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 35 | namespace { |
| 36 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 37 | class IdmapTest : public ::testing::Test { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 38 | protected: |
| 39 | void SetUp() override { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 40 | // Move to the test data directory so the idmap can locate the overlay APK. |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 41 | original_path = base::GetExecutableDirectory(); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 42 | chdir(GetTestDataPath().c_str()); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 43 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 44 | system_assets_ = ApkAssets::Load("system/system.apk"); |
| 45 | ASSERT_NE(nullptr, system_assets_); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 46 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 47 | overlay_assets_ = ApkAssets::LoadOverlay("overlay/overlay.idmap"); |
| 48 | ASSERT_NE(nullptr, overlay_assets_); |
| 49 | |
| 50 | overlayable_assets_ = ApkAssets::Load("overlayable/overlayable.apk"); |
| 51 | ASSERT_NE(nullptr, overlayable_assets_); |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void TearDown() override { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 55 | chdir(original_path.c_str()); |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 58 | protected: |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 59 | std::string original_path; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 60 | std::unique_ptr<const ApkAssets> system_assets_; |
| 61 | std::unique_ptr<const ApkAssets> overlay_assets_; |
| 62 | std::unique_ptr<const ApkAssets> overlayable_assets_; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 65 | std::string GetStringFromApkAssets(const AssetManager2& asset_manager, const Res_value& value, |
| 66 | ApkAssetsCookie cookie) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 67 | auto assets = asset_manager.GetApkAssets(); |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 68 | const ResStringPool* string_pool = assets[cookie]->GetLoadedArsc()->GetStringPool(); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 69 | return GetStringFromPool(string_pool, value.data); |
| 70 | } |
| 71 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 74 | TEST_F(IdmapTest, OverlayOverridesResourceValue) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 75 | AssetManager2 asset_manager; |
| 76 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 77 | overlay_assets_.get()}); |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 78 | Res_value val; |
| 79 | ResTable_config config; |
| 80 | uint32_t flags; |
| 81 | ApkAssetsCookie cookie = asset_manager.GetResource(overlayable::R::string::overlayable5, |
| 82 | false /* may_be_bag */, |
| 83 | 0 /* density_override */, &val, &config, |
| 84 | &flags); |
| 85 | ASSERT_EQ(cookie, 2U); |
| 86 | ASSERT_EQ(val.dataType, Res_value::TYPE_STRING); |
| 87 | ASSERT_EQ(GetStringFromApkAssets(asset_manager, val, cookie), "Overlay One"); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 88 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 89 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 90 | TEST_F(IdmapTest, OverlayOverridesResourceValueUsingDifferentPackage) { |
| 91 | AssetManager2 asset_manager; |
| 92 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 93 | overlay_assets_.get()}); |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 94 | Res_value val; |
| 95 | ResTable_config config; |
| 96 | uint32_t flags; |
| 97 | ApkAssetsCookie cookie = asset_manager.GetResource(overlayable::R::string::overlayable10, |
| 98 | false /* may_be_bag */, |
| 99 | 0 /* density_override */, &val, &config, |
| 100 | &flags); |
| 101 | ASSERT_EQ(cookie, 0U); |
| 102 | ASSERT_EQ(val.dataType, Res_value::TYPE_STRING); |
| 103 | ASSERT_EQ(GetStringFromApkAssets(asset_manager, val, cookie), "yes"); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 104 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 105 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 106 | TEST_F(IdmapTest, OverlayOverridesResourceValueUsingInternalResource) { |
| 107 | AssetManager2 asset_manager; |
| 108 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 109 | overlay_assets_.get()}); |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 110 | Res_value val; |
| 111 | ResTable_config config; |
| 112 | uint32_t flags; |
| 113 | ApkAssetsCookie cookie = asset_manager.GetResource(overlayable::R::string::overlayable8, |
| 114 | false /* may_be_bag */, |
| 115 | 0 /* density_override */, &val, &config, |
| 116 | &flags); |
| 117 | ASSERT_EQ(cookie, 2U); |
| 118 | ASSERT_EQ(val.dataType, Res_value::TYPE_REFERENCE); |
| 119 | ASSERT_EQ(val.data, (overlay::R::string::internal & 0x00ffffff) | (0x02 << 24)); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 120 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 121 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 122 | TEST_F(IdmapTest, OverlayOverridesResourceValueUsingInlineInteger) { |
| 123 | AssetManager2 asset_manager; |
| 124 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 125 | overlay_assets_.get()}); |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 126 | Res_value val; |
| 127 | ResTable_config config; |
| 128 | uint32_t flags; |
| 129 | ApkAssetsCookie cookie = asset_manager.GetResource(overlayable::R::integer::config_integer, |
| 130 | false /* may_be_bag */, |
| 131 | 0 /* density_override */, &val, &config, |
| 132 | &flags); |
| 133 | ASSERT_EQ(cookie, 2U); |
| 134 | ASSERT_EQ(val.dataType, Res_value::TYPE_INT_DEC); |
| 135 | ASSERT_EQ(val.data, 42); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 136 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 137 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 138 | TEST_F(IdmapTest, OverlayOverridesResourceValueUsingInlineString) { |
| 139 | AssetManager2 asset_manager; |
| 140 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 141 | overlay_assets_.get()}); |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 142 | Res_value val; |
| 143 | ResTable_config config; |
| 144 | uint32_t flags; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 145 | |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 146 | ApkAssetsCookie cookie = asset_manager.GetResource(overlayable::R::string::overlayable11, |
| 147 | false /* may_be_bag */, |
| 148 | 0 /* density_override */, &val, &config, |
| 149 | &flags); |
| 150 | ASSERT_EQ(cookie, 2U); |
| 151 | ASSERT_EQ(val.dataType, Res_value::TYPE_STRING); |
| 152 | ASSERT_EQ(GetStringFromApkAssets(asset_manager, val, cookie), "Hardcoded string"); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | TEST_F(IdmapTest, OverlayOverridesResourceValueUsingOverlayingResource) { |
| 156 | AssetManager2 asset_manager; |
| 157 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 158 | overlay_assets_.get()}); |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 159 | Res_value val; |
| 160 | ResTable_config config; |
| 161 | uint32_t flags; |
| 162 | ApkAssetsCookie cookie = asset_manager.GetResource(overlayable::R::string::overlayable9, |
| 163 | false /* may_be_bag */, |
| 164 | 0 /* density_override */, &val, &config, |
| 165 | &flags); |
| 166 | ASSERT_EQ(cookie, 2U); |
| 167 | ASSERT_EQ(val.dataType, Res_value::TYPE_REFERENCE); |
| 168 | ASSERT_EQ(val.data, overlayable::R::string::overlayable7); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | TEST_F(IdmapTest, OverlayOverridesXmlParser) { |
| 172 | AssetManager2 asset_manager; |
| 173 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 174 | overlay_assets_.get()}); |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 175 | Res_value val; |
| 176 | ResTable_config config; |
| 177 | uint32_t flags; |
| 178 | ApkAssetsCookie cookie = asset_manager.GetResource(overlayable::R::layout::hello_view, |
| 179 | false /* may_be_bag */, |
| 180 | 0 /* density_override */, &val, &config, |
| 181 | &flags); |
| 182 | ASSERT_EQ(cookie, 2U); |
| 183 | ASSERT_EQ(val.dataType, Res_value::TYPE_STRING); |
| 184 | ASSERT_EQ(GetStringFromApkAssets(asset_manager, val, cookie), "res/layout/hello_view.xml"); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 185 | |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 186 | auto asset = asset_manager.OpenNonAsset("res/layout/hello_view.xml", cookie, |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 187 | Asset::ACCESS_RANDOM); |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 188 | auto dynamic_ref_table = asset_manager.GetDynamicRefTableForCookie(cookie); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 189 | auto xml_tree = util::make_unique<ResXMLTree>(std::move(dynamic_ref_table)); |
| 190 | status_t err = xml_tree->setTo(asset->getBuffer(true), asset->getLength(), false); |
| 191 | ASSERT_EQ(err, NO_ERROR); |
| 192 | |
| 193 | while (xml_tree->next() != ResXMLParser::START_TAG) { } |
| 194 | |
| 195 | // The resource id of @id/hello_view should be rewritten to the resource id/hello_view within the |
| 196 | // target. |
| 197 | ASSERT_EQ(xml_tree->getAttributeNameResID(0), 0x010100d0 /* android:attr/id */); |
| 198 | ASSERT_EQ(xml_tree->getAttributeDataType(0), Res_value::TYPE_REFERENCE); |
| 199 | ASSERT_EQ(xml_tree->getAttributeData(0), overlayable::R::id::hello_view); |
| 200 | |
| 201 | // The resource id of @android:string/yes should not be rewritten even though it overlays |
| 202 | // string/overlayable10 in the target. |
| 203 | ASSERT_EQ(xml_tree->getAttributeNameResID(1), 0x0101014f /* android:attr/text */); |
| 204 | ASSERT_EQ(xml_tree->getAttributeDataType(1), Res_value::TYPE_REFERENCE); |
| 205 | ASSERT_EQ(xml_tree->getAttributeData(1), 0x01040013 /* android:string/yes */); |
| 206 | |
| 207 | // The resource id of the attribute within the overlay should be rewritten to the resource id of |
| 208 | // the attribute in the target. |
| 209 | ASSERT_EQ(xml_tree->getAttributeNameResID(2), overlayable::R::attr::max_lines); |
| 210 | ASSERT_EQ(xml_tree->getAttributeDataType(2), Res_value::TYPE_INT_DEC); |
| 211 | ASSERT_EQ(xml_tree->getAttributeData(2), 4); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 214 | TEST_F(IdmapTest, OverlaidResourceHasSameName) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 215 | AssetManager2 asset_manager; |
| 216 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 217 | overlay_assets_.get()}); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 218 | |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 219 | AssetManager2::ResourceName name; |
| 220 | ASSERT_TRUE(asset_manager.GetResourceName(overlayable::R::string::overlayable9, &name)); |
| 221 | ASSERT_EQ(std::string(name.package), "com.android.overlayable"); |
| 222 | ASSERT_EQ(String16(name.type16), u"string"); |
| 223 | ASSERT_EQ(std::string(name.entry), "overlayable9"); |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 226 | TEST_F(IdmapTest, OverlayLoaderInterop) { |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 227 | std::string contents; |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 228 | auto loader_assets = ApkAssets::LoadTable("loader/resources.arsc", PROPERTY_LOADER); |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 229 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 230 | AssetManager2 asset_manager; |
| 231 | asset_manager.SetApkAssets({overlayable_assets_.get(), loader_assets.get(), |
| 232 | overlay_assets_.get()}); |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 233 | |
Ryan Mitchell | 55ef616 | 2020-11-13 23:55:20 +0000 | [diff] [blame] | 234 | Res_value val; |
| 235 | ResTable_config config; |
| 236 | uint32_t flags; |
| 237 | ApkAssetsCookie cookie = asset_manager.GetResource(overlayable::R::string::overlayable11, |
| 238 | false /* may_be_bag */, |
| 239 | 0 /* density_override */, &val, &config, |
| 240 | &flags); |
| 241 | std::cout << asset_manager.GetLastResourceResolution(); |
| 242 | ASSERT_EQ(cookie, 1U); |
| 243 | ASSERT_EQ(val.dataType, Res_value::TYPE_STRING); |
| 244 | ASSERT_EQ(GetStringFromApkAssets(asset_manager, val, cookie), "loader"); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 247 | TEST_F(IdmapTest, OverlayAssetsIsUpToDate) { |
| 248 | std::string idmap_contents; |
| 249 | ASSERT_TRUE(base::ReadFileToString("overlay/overlay.idmap", &idmap_contents)); |
| 250 | |
| 251 | TemporaryFile temp_file; |
| 252 | ASSERT_TRUE(base::WriteStringToFile(idmap_contents, temp_file.path)); |
| 253 | |
| 254 | auto apk_assets = ApkAssets::LoadOverlay(temp_file.path); |
| 255 | ASSERT_NE(nullptr, apk_assets); |
| 256 | ASSERT_TRUE(apk_assets->IsUpToDate()); |
| 257 | |
| 258 | unlink(temp_file.path); |
| 259 | ASSERT_FALSE(apk_assets->IsUpToDate()); |
| 260 | sleep(2); |
| 261 | |
| 262 | base::WriteStringToFile("hello", temp_file.path); |
| 263 | sleep(2); |
| 264 | |
| 265 | ASSERT_FALSE(apk_assets->IsUpToDate()); |
| 266 | } |
| 267 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 268 | } // namespace |