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 | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame^] | 65 | std::string GetStringFromApkAssets(const AssetManager2& asset_manager, |
| 66 | const AssetManager2::SelectedValue& value) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 67 | auto assets = asset_manager.GetApkAssets(); |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame^] | 68 | const ResStringPool* string_pool = assets[value.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 | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame^] | 78 | |
| 79 | auto value = asset_manager.GetResource(overlayable::R::string::overlayable5); |
| 80 | ASSERT_TRUE(value.has_value()); |
| 81 | ASSERT_EQ(value->cookie, 2U); |
| 82 | ASSERT_EQ(value->type, Res_value::TYPE_STRING); |
| 83 | ASSERT_EQ("Overlay One", GetStringFromApkAssets(asset_manager, *value)); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 84 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 85 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 86 | TEST_F(IdmapTest, OverlayOverridesResourceValueUsingDifferentPackage) { |
| 87 | AssetManager2 asset_manager; |
| 88 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 89 | overlay_assets_.get()}); |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame^] | 90 | |
| 91 | auto value = asset_manager.GetResource(overlayable::R::string::overlayable10); |
| 92 | ASSERT_TRUE(value.has_value()); |
| 93 | ASSERT_EQ(value->cookie, 0U); |
| 94 | ASSERT_EQ(value->type, Res_value::TYPE_STRING); |
| 95 | ASSERT_EQ("yes", GetStringFromApkAssets(asset_manager, *value)); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 96 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 97 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 98 | TEST_F(IdmapTest, OverlayOverridesResourceValueUsingInternalResource) { |
| 99 | AssetManager2 asset_manager; |
| 100 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 101 | overlay_assets_.get()}); |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame^] | 102 | |
| 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 Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 108 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 109 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 110 | TEST_F(IdmapTest, OverlayOverridesResourceValueUsingInlineInteger) { |
| 111 | AssetManager2 asset_manager; |
| 112 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 113 | overlay_assets_.get()}); |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame^] | 114 | |
| 115 | auto value = asset_manager.GetResource(overlayable::R::integer::config_integer); |
| 116 | ASSERT_TRUE(value.has_value()); |
| 117 | ASSERT_EQ(value->cookie, 2U); |
| 118 | ASSERT_EQ(value->type, Res_value::TYPE_INT_DEC); |
| 119 | ASSERT_EQ(value->data, 42); |
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, OverlayOverridesResourceValueUsingInlineString) { |
| 123 | AssetManager2 asset_manager; |
| 124 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 125 | overlay_assets_.get()}); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 126 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame^] | 127 | auto value = asset_manager.GetResource(overlayable::R::string::overlayable11); |
| 128 | ASSERT_TRUE(value.has_value()); |
| 129 | ASSERT_EQ(value->cookie, 2U); |
| 130 | ASSERT_EQ(value->type, Res_value::TYPE_STRING); |
| 131 | ASSERT_EQ("Hardcoded string", GetStringFromApkAssets(asset_manager, *value)); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | TEST_F(IdmapTest, OverlayOverridesResourceValueUsingOverlayingResource) { |
| 135 | AssetManager2 asset_manager; |
| 136 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 137 | overlay_assets_.get()}); |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame^] | 138 | |
| 139 | auto value = asset_manager.GetResource(overlayable::R::string::overlayable9); |
| 140 | ASSERT_TRUE(value.has_value()); |
| 141 | ASSERT_EQ(value->cookie, 2U); |
| 142 | ASSERT_EQ(value->type, Res_value::TYPE_REFERENCE); |
| 143 | ASSERT_EQ(value->data, overlayable::R::string::overlayable7); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | TEST_F(IdmapTest, OverlayOverridesXmlParser) { |
| 147 | AssetManager2 asset_manager; |
| 148 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 149 | overlay_assets_.get()}); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 150 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame^] | 151 | auto value = asset_manager.GetResource(overlayable::R::layout::hello_view); |
| 152 | ASSERT_TRUE(value.has_value()); |
| 153 | ASSERT_EQ(value->cookie, 2U); |
| 154 | ASSERT_EQ(value->type, Res_value::TYPE_STRING); |
| 155 | ASSERT_EQ("res/layout/hello_view.xml", GetStringFromApkAssets(asset_manager, *value)); |
| 156 | |
| 157 | auto asset = asset_manager.OpenNonAsset("res/layout/hello_view.xml", value->cookie, |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 158 | Asset::ACCESS_RANDOM); |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame^] | 159 | auto dynamic_ref_table = asset_manager.GetDynamicRefTableForCookie(value->cookie); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 160 | auto xml_tree = util::make_unique<ResXMLTree>(std::move(dynamic_ref_table)); |
| 161 | status_t err = xml_tree->setTo(asset->getBuffer(true), asset->getLength(), false); |
| 162 | ASSERT_EQ(err, NO_ERROR); |
| 163 | |
| 164 | while (xml_tree->next() != ResXMLParser::START_TAG) { } |
| 165 | |
| 166 | // The resource id of @id/hello_view should be rewritten to the resource id/hello_view within the |
| 167 | // target. |
| 168 | ASSERT_EQ(xml_tree->getAttributeNameResID(0), 0x010100d0 /* android:attr/id */); |
| 169 | ASSERT_EQ(xml_tree->getAttributeDataType(0), Res_value::TYPE_REFERENCE); |
| 170 | ASSERT_EQ(xml_tree->getAttributeData(0), overlayable::R::id::hello_view); |
| 171 | |
| 172 | // The resource id of @android:string/yes should not be rewritten even though it overlays |
| 173 | // string/overlayable10 in the target. |
| 174 | ASSERT_EQ(xml_tree->getAttributeNameResID(1), 0x0101014f /* android:attr/text */); |
| 175 | ASSERT_EQ(xml_tree->getAttributeDataType(1), Res_value::TYPE_REFERENCE); |
| 176 | ASSERT_EQ(xml_tree->getAttributeData(1), 0x01040013 /* android:string/yes */); |
| 177 | |
| 178 | // The resource id of the attribute within the overlay should be rewritten to the resource id of |
| 179 | // the attribute in the target. |
| 180 | ASSERT_EQ(xml_tree->getAttributeNameResID(2), overlayable::R::attr::max_lines); |
| 181 | ASSERT_EQ(xml_tree->getAttributeDataType(2), Res_value::TYPE_INT_DEC); |
| 182 | ASSERT_EQ(xml_tree->getAttributeData(2), 4); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 185 | TEST_F(IdmapTest, OverlaidResourceHasSameName) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 186 | AssetManager2 asset_manager; |
| 187 | asset_manager.SetApkAssets({system_assets_.get(), overlayable_assets_.get(), |
| 188 | overlay_assets_.get()}); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 189 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame^] | 190 | auto name = asset_manager.GetResourceName(overlayable::R::string::overlayable9); |
| 191 | ASSERT_TRUE(name.has_value()); |
| 192 | ASSERT_EQ("com.android.overlayable", std::string(name->package)); |
| 193 | ASSERT_EQ(std::u16string(u"string"), std::u16string(name->type16)); |
| 194 | ASSERT_EQ("overlayable9", std::string(name->entry)); |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 197 | TEST_F(IdmapTest, OverlayLoaderInterop) { |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 198 | auto loader_assets = ApkAssets::LoadTable("loader/resources.arsc", PROPERTY_LOADER); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 199 | AssetManager2 asset_manager; |
| 200 | asset_manager.SetApkAssets({overlayable_assets_.get(), loader_assets.get(), |
| 201 | overlay_assets_.get()}); |
Adam Lesinski | ed69ce8 | 2017-03-20 10:55:01 -0700 | [diff] [blame] | 202 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame^] | 203 | auto value = asset_manager.GetResource(overlayable::R::string::overlayable11); |
| 204 | ASSERT_TRUE(value.has_value()); |
| 205 | ASSERT_EQ(1U, value->cookie); |
| 206 | ASSERT_EQ(Res_value::TYPE_STRING, value->type); |
| 207 | ASSERT_EQ("loader", GetStringFromApkAssets(asset_manager, *value)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 210 | TEST_F(IdmapTest, OverlayAssetsIsUpToDate) { |
| 211 | std::string idmap_contents; |
| 212 | ASSERT_TRUE(base::ReadFileToString("overlay/overlay.idmap", &idmap_contents)); |
| 213 | |
| 214 | TemporaryFile temp_file; |
| 215 | ASSERT_TRUE(base::WriteStringToFile(idmap_contents, temp_file.path)); |
| 216 | |
| 217 | auto apk_assets = ApkAssets::LoadOverlay(temp_file.path); |
| 218 | ASSERT_NE(nullptr, apk_assets); |
| 219 | ASSERT_TRUE(apk_assets->IsUpToDate()); |
| 220 | |
| 221 | unlink(temp_file.path); |
| 222 | ASSERT_FALSE(apk_assets->IsUpToDate()); |
| 223 | sleep(2); |
| 224 | |
| 225 | base::WriteStringToFile("hello", temp_file.path); |
| 226 | sleep(2); |
| 227 | |
| 228 | ASSERT_FALSE(apk_assets->IsUpToDate()); |
| 229 | } |
| 230 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 231 | } // namespace |