Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | #include "androidfw/LoadedArsc.h" |
| 18 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 19 | #include "android-base/file.h" |
| 20 | #include "androidfw/ResourceUtils.h" |
| 21 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 22 | #include "TestHelpers.h" |
| 23 | #include "data/basic/R.h" |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 24 | #include "data/libclient/R.h" |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 25 | #include "data/sparse/R.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 26 | #include "data/styles/R.h" |
| 27 | |
| 28 | namespace app = com::android::app; |
| 29 | namespace basic = com::android::basic; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 30 | namespace libclient = com::android::libclient; |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 31 | namespace sparse = com::android::sparse; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 33 | using ::android::base::ReadFileToString; |
| 34 | using ::testing::Eq; |
| 35 | using ::testing::Ge; |
| 36 | using ::testing::IsNull; |
| 37 | using ::testing::NotNull; |
| 38 | using ::testing::SizeIs; |
| 39 | using ::testing::StrEq; |
| 40 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | |
| 43 | TEST(LoadedArscTest, LoadSinglePackageArsc) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 44 | std::string contents; |
| 45 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/styles/styles.apk", "resources.arsc", |
| 46 | &contents)); |
| 47 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 48 | std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(StringPiece(contents)); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 49 | ASSERT_THAT(loaded_arsc, NotNull()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 50 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 51 | const LoadedPackage* package = |
| 52 | loaded_arsc->GetPackageById(get_package_id(app::R::string::string_one)); |
| 53 | ASSERT_THAT(package, NotNull()); |
| 54 | EXPECT_THAT(package->GetPackageName(), StrEq("com.android.app")); |
| 55 | EXPECT_THAT(package->GetPackageId(), Eq(0x7f)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 56 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 57 | const uint8_t type_index = get_type_id(app::R::string::string_one) - 1; |
| 58 | const uint16_t entry_index = get_entry_id(app::R::string::string_one); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 59 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 60 | const TypeSpec* type_spec = package->GetTypeSpecByTypeIndex(type_index); |
| 61 | ASSERT_THAT(type_spec, NotNull()); |
| 62 | ASSERT_THAT(type_spec->type_count, Ge(1u)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 64 | const ResTable_type* type = type_spec->types[0]; |
| 65 | ASSERT_THAT(type, NotNull()); |
| 66 | ASSERT_THAT(LoadedPackage::GetEntry(type, entry_index), NotNull()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 69 | TEST(LoadedArscTest, LoadSparseEntryApp) { |
| 70 | std::string contents; |
| 71 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/sparse/sparse.apk", "resources.arsc", |
| 72 | &contents)); |
| 73 | |
| 74 | std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(StringPiece(contents)); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 75 | ASSERT_THAT(loaded_arsc, NotNull()); |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 76 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 77 | const LoadedPackage* package = |
| 78 | loaded_arsc->GetPackageById(get_package_id(sparse::R::integer::foo_9)); |
| 79 | ASSERT_THAT(package, NotNull()); |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 80 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 81 | const uint8_t type_index = get_type_id(sparse::R::integer::foo_9) - 1; |
| 82 | const uint16_t entry_index = get_entry_id(sparse::R::integer::foo_9); |
| 83 | |
| 84 | const TypeSpec* type_spec = package->GetTypeSpecByTypeIndex(type_index); |
| 85 | ASSERT_THAT(type_spec, NotNull()); |
| 86 | ASSERT_THAT(type_spec->type_count, Ge(1u)); |
| 87 | |
| 88 | const ResTable_type* type = type_spec->types[0]; |
| 89 | ASSERT_THAT(type, NotNull()); |
| 90 | ASSERT_THAT(LoadedPackage::GetEntry(type, entry_index), NotNull()); |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 93 | TEST(LoadedArscTest, LoadSharedLibrary) { |
| 94 | std::string contents; |
| 95 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/lib_one/lib_one.apk", "resources.arsc", |
| 96 | &contents)); |
| 97 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 98 | std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(StringPiece(contents)); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 99 | ASSERT_THAT(loaded_arsc, NotNull()); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 100 | |
| 101 | const auto& packages = loaded_arsc->GetPackages(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 102 | ASSERT_THAT(packages, SizeIs(1u)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 103 | EXPECT_TRUE(packages[0]->IsDynamic()); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 104 | EXPECT_THAT(packages[0]->GetPackageName(), StrEq("com.android.lib_one")); |
| 105 | EXPECT_THAT(packages[0]->GetPackageId(), Eq(0)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 106 | |
| 107 | const auto& dynamic_pkg_map = packages[0]->GetDynamicPackageMap(); |
| 108 | |
| 109 | // The library has no dependencies. |
| 110 | ASSERT_TRUE(dynamic_pkg_map.empty()); |
| 111 | } |
| 112 | |
| 113 | TEST(LoadedArscTest, LoadAppLinkedAgainstSharedLibrary) { |
| 114 | std::string contents; |
| 115 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/libclient/libclient.apk", |
| 116 | "resources.arsc", &contents)); |
| 117 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 118 | std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(StringPiece(contents)); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 119 | ASSERT_THAT(loaded_arsc, NotNull()); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 120 | |
| 121 | const auto& packages = loaded_arsc->GetPackages(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 122 | ASSERT_THAT(packages, SizeIs(1u)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 123 | EXPECT_FALSE(packages[0]->IsDynamic()); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 124 | EXPECT_THAT(packages[0]->GetPackageName(), StrEq("com.android.libclient")); |
| 125 | EXPECT_THAT(packages[0]->GetPackageId(), Eq(0x7f)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 126 | |
| 127 | const auto& dynamic_pkg_map = packages[0]->GetDynamicPackageMap(); |
| 128 | |
| 129 | // The library has two dependencies. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 130 | ASSERT_THAT(dynamic_pkg_map, SizeIs(2u)); |
| 131 | EXPECT_THAT(dynamic_pkg_map[0].package_name, StrEq("com.android.lib_one")); |
| 132 | EXPECT_THAT(dynamic_pkg_map[0].package_id, Eq(0x02)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 133 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 134 | EXPECT_THAT(dynamic_pkg_map[1].package_name, StrEq("com.android.lib_two")); |
| 135 | EXPECT_THAT(dynamic_pkg_map[1].package_id, Eq(0x03)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | TEST(LoadedArscTest, LoadAppAsSharedLibrary) { |
| 139 | std::string contents; |
| 140 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/appaslib/appaslib.apk", |
| 141 | "resources.arsc", &contents)); |
| 142 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 143 | std::unique_ptr<const LoadedArsc> loaded_arsc = |
| 144 | LoadedArsc::Load(StringPiece(contents), nullptr /*loaded_idmap*/, false /*system*/, |
| 145 | true /*load_as_shared_library*/); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 146 | ASSERT_THAT(loaded_arsc, NotNull()); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 147 | |
| 148 | const auto& packages = loaded_arsc->GetPackages(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 149 | ASSERT_THAT(packages, SizeIs(1u)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 150 | EXPECT_TRUE(packages[0]->IsDynamic()); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 151 | EXPECT_THAT(packages[0]->GetPackageId(), Eq(0x7f)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 154 | TEST(LoadedArscTest, LoadFeatureSplit) { |
| 155 | std::string contents; |
| 156 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/feature/feature.apk", "resources.arsc", |
| 157 | &contents)); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 158 | std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(StringPiece(contents)); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 159 | ASSERT_THAT(loaded_arsc, NotNull()); |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 160 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 161 | const LoadedPackage* package = |
| 162 | loaded_arsc->GetPackageById(get_package_id(basic::R::string::test3)); |
| 163 | ASSERT_THAT(package, NotNull()); |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 164 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 165 | uint8_t type_index = get_type_id(basic::R::string::test3) - 1; |
| 166 | uint8_t entry_index = get_entry_id(basic::R::string::test3); |
| 167 | |
| 168 | const TypeSpec* type_spec = package->GetTypeSpecByTypeIndex(type_index); |
| 169 | ASSERT_THAT(type_spec, NotNull()); |
| 170 | ASSERT_THAT(type_spec->type_count, Ge(1u)); |
| 171 | ASSERT_THAT(type_spec->types[0], NotNull()); |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 172 | |
| 173 | size_t len; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 174 | const char16_t* type_name16 = |
| 175 | package->GetTypeStringPool()->stringAt(type_spec->type_spec->id - 1, &len); |
| 176 | ASSERT_THAT(type_name16, NotNull()); |
| 177 | EXPECT_THAT(util::Utf16ToUtf8(StringPiece16(type_name16, len)), StrEq("string")); |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 178 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 179 | ASSERT_THAT(LoadedPackage::GetEntry(type_spec->types[0], entry_index), NotNull()); |
| 180 | } |
| 181 | |
| 182 | // AAPT(2) generates resource tables with chunks in a certain order. The rule is that |
| 183 | // a RES_TABLE_TYPE_TYPE with id `i` must always be preceded by a RES_TABLE_TYPE_SPEC_TYPE with |
| 184 | // id `i`. The RES_TABLE_TYPE_SPEC_TYPE does not need to be directly preceding, however. |
| 185 | // |
| 186 | // AAPT(2) generates something like: |
| 187 | // RES_TABLE_TYPE_SPEC_TYPE id=1 |
| 188 | // RES_TABLE_TYPE_TYPE id=1 |
| 189 | // RES_TABLE_TYPE_SPEC_TYPE id=2 |
| 190 | // RES_TABLE_TYPE_TYPE id=2 |
| 191 | // |
| 192 | // But the following is valid too: |
| 193 | // RES_TABLE_TYPE_SPEC_TYPE id=1 |
| 194 | // RES_TABLE_TYPE_SPEC_TYPE id=2 |
| 195 | // RES_TABLE_TYPE_TYPE id=1 |
| 196 | // RES_TABLE_TYPE_TYPE id=2 |
| 197 | // |
| 198 | TEST(LoadedArscTest, LoadOutOfOrderTypeSpecs) { |
| 199 | std::string contents; |
| 200 | ASSERT_TRUE( |
| 201 | ReadFileFromZipToString(GetTestDataPath() + "/out_of_order_types/out_of_order_types.apk", |
| 202 | "resources.arsc", &contents)); |
| 203 | |
| 204 | std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(StringPiece(contents)); |
| 205 | ASSERT_THAT(loaded_arsc, NotNull()); |
| 206 | |
| 207 | ASSERT_THAT(loaded_arsc->GetPackages(), SizeIs(1u)); |
| 208 | const auto& package = loaded_arsc->GetPackages()[0]; |
| 209 | ASSERT_THAT(package, NotNull()); |
| 210 | |
| 211 | const TypeSpec* type_spec = package->GetTypeSpecByTypeIndex(0); |
| 212 | ASSERT_THAT(type_spec, NotNull()); |
| 213 | ASSERT_THAT(type_spec->type_count, Ge(1u)); |
| 214 | ASSERT_THAT(type_spec->types[0], NotNull()); |
| 215 | |
| 216 | type_spec = package->GetTypeSpecByTypeIndex(1); |
| 217 | ASSERT_THAT(type_spec, NotNull()); |
| 218 | ASSERT_THAT(type_spec->type_count, Ge(1u)); |
| 219 | ASSERT_THAT(type_spec->types[0], NotNull()); |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 222 | class MockLoadedIdmap : public LoadedIdmap { |
| 223 | public: |
| 224 | MockLoadedIdmap() : LoadedIdmap() { |
| 225 | local_header_.magic = kIdmapMagic; |
| 226 | local_header_.version = kIdmapCurrentVersion; |
| 227 | local_header_.target_package_id = 0x08; |
| 228 | local_header_.type_count = 1; |
| 229 | header_ = &local_header_; |
| 230 | |
| 231 | entry_header = util::unique_cptr<IdmapEntry_header>( |
| 232 | (IdmapEntry_header*)::malloc(sizeof(IdmapEntry_header) + sizeof(uint32_t))); |
| 233 | entry_header->target_type_id = 0x03; |
| 234 | entry_header->overlay_type_id = 0x02; |
| 235 | entry_header->entry_id_offset = 1; |
| 236 | entry_header->entry_count = 1; |
| 237 | entry_header->entries[0] = 0x00000000u; |
| 238 | type_map_[entry_header->overlay_type_id] = entry_header.get(); |
| 239 | } |
| 240 | |
| 241 | private: |
| 242 | Idmap_header local_header_; |
| 243 | util::unique_cptr<IdmapEntry_header> entry_header; |
| 244 | }; |
| 245 | |
| 246 | TEST(LoadedArscTest, LoadOverlay) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 247 | std::string contents; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 248 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/overlay/overlay.apk", "resources.arsc", |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 249 | &contents)); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 250 | |
| 251 | MockLoadedIdmap loaded_idmap; |
| 252 | |
| 253 | std::unique_ptr<const LoadedArsc> loaded_arsc = |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 254 | LoadedArsc::Load(StringPiece(contents), &loaded_idmap); |
| 255 | ASSERT_THAT(loaded_arsc, NotNull()); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 256 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 257 | const LoadedPackage* package = loaded_arsc->GetPackageById(0x08u); |
| 258 | ASSERT_THAT(package, NotNull()); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 259 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 260 | const TypeSpec* type_spec = package->GetTypeSpecByTypeIndex(0x03u - 1); |
| 261 | ASSERT_THAT(type_spec, NotNull()); |
| 262 | ASSERT_THAT(type_spec->type_count, Ge(1u)); |
| 263 | ASSERT_THAT(type_spec->types[0], NotNull()); |
| 264 | |
| 265 | // The entry being overlaid doesn't exist at the original entry index. |
| 266 | ASSERT_THAT(LoadedPackage::GetEntry(type_spec->types[0], 0x0001u), IsNull()); |
| 267 | |
| 268 | // Since this is an overlay, the actual entry ID must be mapped. |
| 269 | ASSERT_THAT(type_spec->idmap_entries, NotNull()); |
| 270 | uint16_t target_entry_id = 0u; |
| 271 | ASSERT_TRUE(LoadedIdmap::Lookup(type_spec->idmap_entries, 0x0001u, &target_entry_id)); |
| 272 | ASSERT_THAT(target_entry_id, Eq(0x0u)); |
| 273 | ASSERT_THAT(LoadedPackage::GetEntry(type_spec->types[0], 0x0000), NotNull()); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 276 | // structs with size fields (like Res_value, ResTable_entry) should be |
| 277 | // backwards and forwards compatible (aka checking the size field against |
| 278 | // sizeof(Res_value) might not be backwards compatible. |
| 279 | TEST(LoadedArscTest, LoadingShouldBeForwardsAndBackwardsCompatible) { ASSERT_TRUE(false); } |
| 280 | |
MÃ¥rten Kongstad | 3f1f4fc | 2018-03-02 09:34:18 +0100 | [diff] [blame] | 281 | TEST(LoadedArscTest, ResourceIdentifierIterator) { |
| 282 | std::string contents; |
| 283 | ASSERT_TRUE( |
| 284 | ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", "resources.arsc", &contents)); |
| 285 | |
| 286 | std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(StringPiece(contents)); |
| 287 | ASSERT_NE(nullptr, loaded_arsc); |
| 288 | |
| 289 | const std::vector<std::unique_ptr<const LoadedPackage>>& packages = loaded_arsc->GetPackages(); |
| 290 | ASSERT_EQ(1u, packages.size()); |
| 291 | EXPECT_EQ(std::string("com.android.basic"), packages[0]->GetPackageName()); |
| 292 | |
| 293 | const auto& loaded_package = packages[0]; |
| 294 | auto iter = loaded_package->begin(); |
| 295 | auto end = loaded_package->end(); |
| 296 | |
| 297 | ASSERT_NE(end, iter); |
| 298 | ASSERT_EQ(0x7f010000u, *iter++); |
| 299 | ASSERT_EQ(0x7f010001u, *iter++); |
| 300 | ASSERT_EQ(0x7f020000u, *iter++); |
| 301 | ASSERT_EQ(0x7f020001u, *iter++); |
| 302 | ASSERT_EQ(0x7f030000u, *iter++); |
| 303 | ASSERT_EQ(0x7f030001u, *iter++); |
| 304 | ASSERT_EQ(0x7f030002u, *iter++); // note: string without default, excluded by aapt2 dump |
| 305 | ASSERT_EQ(0x7f040000u, *iter++); |
| 306 | ASSERT_EQ(0x7f040001u, *iter++); |
| 307 | ASSERT_EQ(0x7f040002u, *iter++); |
| 308 | ASSERT_EQ(0x7f040003u, *iter++); |
| 309 | ASSERT_EQ(0x7f040004u, *iter++); |
| 310 | ASSERT_EQ(0x7f040005u, *iter++); |
| 311 | ASSERT_EQ(0x7f040006u, *iter++); |
| 312 | ASSERT_EQ(0x7f040007u, *iter++); |
| 313 | ASSERT_EQ(0x7f040008u, *iter++); |
| 314 | ASSERT_EQ(0x7f040009u, *iter++); |
| 315 | ASSERT_EQ(0x7f04000au, *iter++); |
| 316 | ASSERT_EQ(0x7f04000bu, *iter++); |
| 317 | ASSERT_EQ(0x7f04000cu, *iter++); |
| 318 | ASSERT_EQ(0x7f04000du, *iter++); |
| 319 | ASSERT_EQ(0x7f050000u, *iter++); |
| 320 | ASSERT_EQ(0x7f050001u, *iter++); |
| 321 | ASSERT_EQ(0x7f060000u, *iter++); |
| 322 | ASSERT_EQ(0x7f070000u, *iter++); |
| 323 | ASSERT_EQ(0x7f070001u, *iter++); |
| 324 | ASSERT_EQ(0x7f070002u, *iter++); |
| 325 | ASSERT_EQ(0x7f070003u, *iter++); |
| 326 | ASSERT_EQ(end, iter); |
| 327 | } |
| 328 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 329 | } // namespace android |