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 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 17 | #include "androidfw/ResourceTypes.h" |
Brandon Liu | 742b11e | 2022-11-03 23:23:28 +0000 | [diff] [blame] | 18 | #include "android-base/file.h" |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 19 | |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 20 | #include <codecvt> |
| 21 | #include <locale> |
| 22 | #include <string> |
| 23 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 24 | #include "utils/String16.h" |
| 25 | #include "utils/String8.h" |
| 26 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 27 | #include "TestHelpers.h" |
Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 28 | #include "data/basic/R.h" |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 29 | #include "data/lib_one/R.h" |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 30 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 31 | namespace basic = com::android::basic; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 32 | namespace lib = com::android::lib_one; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 33 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 34 | namespace android { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 35 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 36 | TEST(ResTableTest, ShouldLoadSuccessfully) { |
| 37 | std::string contents; |
| 38 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 39 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 41 | ResTable table; |
| 42 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 45 | TEST(ResTableTest, SimpleTypeIsRetrievedCorrectly) { |
| 46 | std::string contents; |
| 47 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 48 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 49 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 50 | ResTable table; |
| 51 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
| 52 | |
| 53 | EXPECT_TRUE(IsStringEqual(table, basic::R::string::test1, "test1")); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 56 | TEST(ResTableTest, ResourceNameIsResolved) { |
| 57 | std::string contents; |
| 58 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 59 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 60 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 61 | ResTable table; |
| 62 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
| 63 | |
| 64 | String16 defPackage("com.android.basic"); |
| 65 | String16 testName("@string/test1"); |
| 66 | uint32_t resID = |
Tomasz Wasilczyk | ade0631 | 2023-08-10 23:54:44 +0000 | [diff] [blame^] | 67 | table.identifierForName(testName.c_str(), testName.size(), 0, 0, |
| 68 | defPackage.c_str(), defPackage.size()); |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 69 | ASSERT_NE(uint32_t(0x00000000), resID); |
| 70 | ASSERT_EQ(basic::R::string::test1, resID); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 73 | TEST(ResTableTest, NoParentThemeIsAppliedCorrectly) { |
| 74 | std::string contents; |
| 75 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 76 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 77 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 78 | ResTable table; |
| 79 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 80 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 81 | ResTable::Theme theme(table); |
| 82 | ASSERT_EQ(NO_ERROR, theme.applyStyle(basic::R::style::Theme1)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 84 | Res_value val; |
| 85 | uint32_t specFlags = 0; |
| 86 | ssize_t index = theme.getAttribute(basic::R::attr::attr1, &val, &specFlags); |
| 87 | ASSERT_GE(index, 0); |
| 88 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 89 | ASSERT_EQ(uint32_t(100), val.data); |
| 90 | |
| 91 | index = theme.getAttribute(basic::R::attr::attr2, &val, &specFlags); |
| 92 | ASSERT_GE(index, 0); |
| 93 | ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType); |
| 94 | ASSERT_EQ(basic::R::integer::number1, val.data); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 97 | TEST(ResTableTest, ParentThemeIsAppliedCorrectly) { |
| 98 | std::string contents; |
| 99 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 100 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 101 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 102 | ResTable table; |
| 103 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 104 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 105 | ResTable::Theme theme(table); |
| 106 | ASSERT_EQ(NO_ERROR, theme.applyStyle(basic::R::style::Theme2)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 107 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 108 | Res_value val; |
| 109 | uint32_t specFlags = 0; |
| 110 | ssize_t index = theme.getAttribute(basic::R::attr::attr1, &val, &specFlags); |
| 111 | ASSERT_GE(index, 0); |
| 112 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 113 | ASSERT_EQ(uint32_t(300), val.data); |
| 114 | |
| 115 | index = theme.getAttribute(basic::R::attr::attr2, &val, &specFlags); |
| 116 | ASSERT_GE(index, 0); |
| 117 | ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType); |
| 118 | ASSERT_EQ(basic::R::integer::number1, val.data); |
Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 121 | TEST(ResTableTest, LibraryThemeIsAppliedCorrectly) { |
| 122 | std::string contents; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 123 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/lib_one/lib_one.apk", |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 124 | "resources.arsc", &contents)); |
Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 125 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 126 | ResTable table; |
| 127 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | ccf25c7b | 2014-08-08 15:32:40 -0700 | [diff] [blame] | 128 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 129 | ResTable::Theme theme(table); |
| 130 | ASSERT_EQ(NO_ERROR, theme.applyStyle(lib::R::style::Theme)); |
Adam Lesinski | 8ac51d1 | 2016-05-10 10:01:12 -0700 | [diff] [blame] | 131 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 132 | Res_value val; |
| 133 | uint32_t specFlags = 0; |
| 134 | ssize_t index = theme.getAttribute(lib::R::attr::attr1, &val, &specFlags); |
| 135 | ASSERT_GE(index, 0); |
| 136 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 137 | ASSERT_EQ(uint32_t(700), val.data); |
| 138 | |
| 139 | index = theme.getAttribute(lib::R::attr::attr2, &val, &specFlags); |
| 140 | ASSERT_GE(index, 0); |
| 141 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 142 | ASSERT_EQ(uint32_t(700), val.data); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 145 | TEST(ResTableTest, ReferenceToBagIsNotResolved) { |
| 146 | std::string contents; |
| 147 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 148 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 149 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 150 | ResTable table; |
| 151 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 152 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 153 | Res_value val; |
| 154 | ssize_t block = |
| 155 | table.getResource(basic::R::integer::number2, &val, MAY_NOT_BE_BAG); |
| 156 | ASSERT_GE(block, 0); |
| 157 | ASSERT_EQ(Res_value::TYPE_REFERENCE, val.dataType); |
| 158 | ASSERT_EQ(basic::R::array::integerArray1, val.data); |
| 159 | |
| 160 | ssize_t newBlock = table.resolveReference(&val, block); |
| 161 | EXPECT_EQ(block, newBlock); |
| 162 | EXPECT_EQ(Res_value::TYPE_REFERENCE, val.dataType); |
| 163 | EXPECT_EQ(basic::R::array::integerArray1, val.data); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 166 | TEST(ResTableTest, ResourcesStillAccessibleAfterParameterChange) { |
| 167 | std::string contents; |
| 168 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 169 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 170 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 171 | ResTable table; |
| 172 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 173 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 174 | Res_value val; |
| 175 | ssize_t block = |
| 176 | table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| 177 | ASSERT_GE(block, 0); |
| 178 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 179 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 180 | const ResTable::bag_entry* entry; |
| 181 | ssize_t count = table.lockBag(basic::R::array::integerArray1, &entry); |
| 182 | ASSERT_GE(count, 0); |
| 183 | table.unlockBag(entry); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 184 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 185 | ResTable_config param; |
| 186 | memset(¶m, 0, sizeof(param)); |
| 187 | param.density = 320; |
| 188 | table.setParameters(¶m); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 189 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 190 | block = table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| 191 | ASSERT_GE(block, 0); |
| 192 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 193 | |
| 194 | count = table.lockBag(basic::R::array::integerArray1, &entry); |
| 195 | ASSERT_GE(count, 0); |
| 196 | table.unlockBag(entry); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 199 | TEST(ResTableTest, ResourceIsOverridenWithBetterConfig) { |
| 200 | std::string contents; |
| 201 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 202 | "resources.arsc", &contents)); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 203 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 204 | ResTable table; |
| 205 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 206 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 207 | Res_value val; |
| 208 | ssize_t block = |
| 209 | table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| 210 | ASSERT_GE(block, 0); |
| 211 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 212 | ASSERT_EQ(uint32_t(200), val.data); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 213 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 214 | ResTable_config param; |
| 215 | memset(¶m, 0, sizeof(param)); |
| 216 | param.language[0] = 's'; |
| 217 | param.language[1] = 'v'; |
| 218 | param.country[0] = 'S'; |
| 219 | param.country[1] = 'E'; |
| 220 | table.setParameters(¶m); |
| 221 | |
| 222 | block = table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| 223 | ASSERT_GE(block, 0); |
| 224 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 225 | ASSERT_EQ(uint32_t(400), val.data); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 228 | TEST(ResTableTest, emptyTableHasSensibleDefaults) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 229 | const int32_t assetCookie = 1; |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 230 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 231 | ResTable table; |
| 232 | ASSERT_EQ(NO_ERROR, table.addEmpty(assetCookie)); |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 233 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 234 | // Adding an empty table gives us one table! |
| 235 | ASSERT_EQ(uint32_t(1), table.getTableCount()); |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 236 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 237 | // Adding an empty table doesn't mean we get packages. |
| 238 | ASSERT_EQ(uint32_t(0), table.getBasePackageCount()); |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 239 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 240 | Res_value val; |
| 241 | ASSERT_LT(table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG), |
| 242 | 0); |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 245 | void testU16StringToInt(const char16_t* str, uint32_t expectedValue, |
| 246 | bool expectSuccess, bool expectHex) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 247 | size_t len = std::char_traits<char16_t>::length(str); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 248 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 249 | // Gtest can't print UTF-16 strings, so we have to convert to UTF-8 :( |
| 250 | std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert; |
| 251 | std::string s = convert.to_bytes(std::u16string(str, len)); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 252 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 253 | Res_value out = {}; |
| 254 | ASSERT_EQ(expectSuccess, U16StringToInt(str, len, &out)) << "Failed with " |
| 255 | << s; |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 256 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 257 | if (!expectSuccess) { |
| 258 | ASSERT_EQ(out.TYPE_NULL, out.dataType) << "Failed with " << s; |
| 259 | return; |
| 260 | } |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 261 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 262 | if (expectHex) { |
| 263 | ASSERT_EQ(out.TYPE_INT_HEX, out.dataType) << "Failed with " << s; |
| 264 | } else { |
| 265 | ASSERT_EQ(out.TYPE_INT_DEC, out.dataType) << "Failed with " << s; |
| 266 | } |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 267 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 268 | ASSERT_EQ(expectedValue, out.data) << "Failed with " << s; |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | TEST(ResTableTest, U16StringToInt) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 272 | testU16StringToInt(u"", 0U, false, false); |
| 273 | testU16StringToInt(u" ", 0U, false, false); |
| 274 | testU16StringToInt(u"\t\n", 0U, false, false); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 275 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 276 | testU16StringToInt(u"abcd", 0U, false, false); |
| 277 | testU16StringToInt(u"10abcd", 0U, false, false); |
| 278 | testU16StringToInt(u"42 42", 0U, false, false); |
| 279 | testU16StringToInt(u"- 42", 0U, false, false); |
| 280 | testU16StringToInt(u"-", 0U, false, false); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 281 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 282 | testU16StringToInt(u"0x", 0U, false, true); |
| 283 | testU16StringToInt(u"0xnope", 0U, false, true); |
| 284 | testU16StringToInt(u"0X42", 0U, false, true); |
| 285 | testU16StringToInt(u"0x42 0x42", 0U, false, true); |
| 286 | testU16StringToInt(u"-0x0", 0U, false, true); |
| 287 | testU16StringToInt(u"-0x42", 0U, false, true); |
| 288 | testU16StringToInt(u"- 0x42", 0U, false, true); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 289 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 290 | // Note that u" 42" would pass. This preserves the old behavior, but it may |
| 291 | // not be desired. |
| 292 | testU16StringToInt(u"42 ", 0U, false, false); |
| 293 | testU16StringToInt(u"0x42 ", 0U, false, true); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 294 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 295 | // Decimal cases. |
| 296 | testU16StringToInt(u"0", 0U, true, false); |
| 297 | testU16StringToInt(u"-0", 0U, true, false); |
| 298 | testU16StringToInt(u"42", 42U, true, false); |
| 299 | testU16StringToInt(u" 42", 42U, true, false); |
| 300 | testU16StringToInt(u"-42", static_cast<uint32_t>(-42), true, false); |
| 301 | testU16StringToInt(u" -42", static_cast<uint32_t>(-42), true, false); |
| 302 | testU16StringToInt(u"042", 42U, true, false); |
| 303 | testU16StringToInt(u"-042", static_cast<uint32_t>(-42), true, false); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 304 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 305 | // Hex cases. |
| 306 | testU16StringToInt(u"0x0", 0x0, true, true); |
| 307 | testU16StringToInt(u"0x42", 0x42, true, true); |
| 308 | testU16StringToInt(u" 0x42", 0x42, true, true); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 309 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 310 | // Just before overflow cases: |
| 311 | testU16StringToInt(u"2147483647", INT_MAX, true, false); |
| 312 | testU16StringToInt(u"-2147483648", static_cast<uint32_t>(INT_MIN), true, |
| 313 | false); |
| 314 | testU16StringToInt(u"0xffffffff", UINT_MAX, true, true); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 315 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 316 | // Overflow cases: |
| 317 | testU16StringToInt(u"2147483648", 0U, false, false); |
| 318 | testU16StringToInt(u"-2147483649", 0U, false, false); |
| 319 | testU16StringToInt(u"0x1ffffffff", 0U, false, true); |
Dan Albert | 1b4f316 | 2015-04-07 18:43:15 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 322 | TEST(ResTableTest, ShareButDontModifyResTable) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 323 | std::string contents; |
| 324 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 325 | "resources.arsc", &contents)); |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 326 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 327 | ResTable sharedTable; |
| 328 | ASSERT_EQ(NO_ERROR, sharedTable.add(contents.data(), contents.size())); |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 329 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 330 | ResTable_config param; |
| 331 | memset(¶m, 0, sizeof(param)); |
| 332 | param.language[0] = 'v'; |
| 333 | param.language[1] = 's'; |
| 334 | sharedTable.setParameters(¶m); |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 335 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 336 | // Check that we get the default value for @integer:number1 |
| 337 | Res_value val; |
| 338 | ssize_t block = |
| 339 | sharedTable.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| 340 | ASSERT_GE(block, 0); |
| 341 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 342 | ASSERT_EQ(uint32_t(600), val.data); |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 343 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 344 | // Create a new table that shares the entries of the shared table. |
| 345 | ResTable table; |
| 346 | ASSERT_EQ(NO_ERROR, table.add(&sharedTable, false)); |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 347 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 348 | // Set a new configuration on the new table. |
| 349 | memset(¶m, 0, sizeof(param)); |
| 350 | param.language[0] = 's'; |
| 351 | param.language[1] = 'v'; |
| 352 | param.country[0] = 'S'; |
| 353 | param.country[1] = 'E'; |
| 354 | table.setParameters(¶m); |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 355 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 356 | // Check that we get a new value in the new table. |
| 357 | block = table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| 358 | ASSERT_GE(block, 0); |
| 359 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 360 | ASSERT_EQ(uint32_t(400), val.data); |
| 361 | |
| 362 | // Check that we still get the old value in the shared table. |
| 363 | block = |
| 364 | sharedTable.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG); |
| 365 | ASSERT_GE(block, 0); |
| 366 | ASSERT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 367 | ASSERT_EQ(uint32_t(600), val.data); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 368 | } |
Adam Lesinski | ff5808d | 2016-02-23 17:49:53 -0800 | [diff] [blame] | 369 | |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 370 | TEST(ResTableTest, GetConfigurationsReturnsUniqueList) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 371 | std::string contents; |
| 372 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", |
| 373 | "resources.arsc", &contents)); |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 374 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 375 | std::string system_contents; |
| 376 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/system/system.apk", |
| 377 | "resources.arsc", &system_contents)); |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 378 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 379 | ResTable table; |
| 380 | ASSERT_EQ(NO_ERROR, |
| 381 | table.add(system_contents.data(), system_contents.size())); |
| 382 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 383 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 384 | ResTable_config configSv; |
| 385 | memset(&configSv, 0, sizeof(configSv)); |
| 386 | configSv.language[0] = 's'; |
| 387 | configSv.language[1] = 'v'; |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 388 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 389 | Vector<ResTable_config> configs; |
| 390 | table.getConfigurations(&configs); |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 391 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 392 | EXPECT_EQ(1, std::count(configs.begin(), configs.end(), configSv)); |
| 393 | |
| 394 | Vector<String8> locales; |
| 395 | table.getLocales(&locales); |
| 396 | |
| 397 | EXPECT_EQ(1, std::count(locales.begin(), locales.end(), String8("sv"))); |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Ryan Mitchell | ea9e8b4 | 2018-03-29 15:49:10 -0700 | [diff] [blame] | 400 | TEST(ResTableTest, TruncatedEncodeLength) { |
| 401 | std::string contents; |
| 402 | ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/length_decode/length_decode_valid.apk", |
| 403 | "resources.arsc", &contents)); |
| 404 | |
| 405 | ResTable table; |
| 406 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
| 407 | |
| 408 | Res_value val; |
| 409 | ssize_t block = table.getResource(0x7f010001, &val, MAY_NOT_BE_BAG); |
| 410 | ASSERT_GE(block, 0); |
| 411 | ASSERT_EQ(Res_value::TYPE_STRING, val.dataType); |
| 412 | |
| 413 | const ResStringPool* pool = table.getTableStringBlock(block); |
| 414 | ASSERT_TRUE(pool != NULL); |
| 415 | ASSERT_LT(val.data, pool->size()); |
| 416 | |
| 417 | // Make sure a string with a truncated length is read to its correct length |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 418 | auto target_str8 = pool->string8At(val.data); |
| 419 | ASSERT_TRUE(target_str8.has_value()); |
| 420 | ASSERT_EQ(size_t(40076), String8(target_str8->data(), target_str8->size()).size()); |
| 421 | ASSERT_EQ(target_str8->data()[40075], ']'); |
Ryan Mitchell | ea9e8b4 | 2018-03-29 15:49:10 -0700 | [diff] [blame] | 422 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 423 | auto target_str16 = pool->stringAt(val.data); |
| 424 | ASSERT_TRUE(target_str16.has_value()); |
| 425 | ASSERT_EQ(size_t(40076), String16(target_str16->data(), target_str16->size()).size()); |
| 426 | ASSERT_EQ(target_str8->data()[40075], (char16_t) ']'); |
Ryan Mitchell | ea9e8b4 | 2018-03-29 15:49:10 -0700 | [diff] [blame] | 427 | |
| 428 | // Load an edited apk with the null terminator removed from the end of the |
| 429 | // string |
| 430 | std::string invalid_contents; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 431 | ASSERT_TRUE(ReadFileFromZipToString( |
| 432 | GetTestDataPath() + "/length_decode/length_decode_invalid.apk", "resources.arsc", |
| 433 | &invalid_contents)); |
Ryan Mitchell | ea9e8b4 | 2018-03-29 15:49:10 -0700 | [diff] [blame] | 434 | ResTable invalid_table; |
| 435 | ASSERT_EQ(NO_ERROR, invalid_table.add(invalid_contents.data(), invalid_contents.size())); |
| 436 | |
| 437 | Res_value invalid_val; |
| 438 | ssize_t invalid_block = invalid_table.getResource(0x7f010001, &invalid_val, MAY_NOT_BE_BAG); |
| 439 | ASSERT_GE(invalid_block, 0); |
| 440 | ASSERT_EQ(Res_value::TYPE_STRING, invalid_val.dataType); |
| 441 | |
| 442 | const ResStringPool* invalid_pool = invalid_table.getTableStringBlock(invalid_block); |
| 443 | ASSERT_TRUE(invalid_pool != NULL); |
| 444 | ASSERT_LT(invalid_val.data, invalid_pool->size()); |
| 445 | |
| 446 | // Make sure a string with a truncated length that is not null terminated errors |
| 447 | // and does not return the string |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 448 | ASSERT_FALSE(invalid_pool->string8At(invalid_val.data).has_value()); |
| 449 | ASSERT_FALSE(invalid_pool->stringAt(invalid_val.data).has_value()); |
Ryan Mitchell | ea9e8b4 | 2018-03-29 15:49:10 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Brandon Liu | 742b11e | 2022-11-03 23:23:28 +0000 | [diff] [blame] | 452 | class ResTableParameterizedTest : |
| 453 | public testing::TestWithParam<std::string> { |
| 454 | }; |
| 455 | |
| 456 | TEST_P(ResTableParameterizedTest, ShouldLoadSparseEntriesSuccessfully) { |
| 457 | std::string contents; |
| 458 | ASSERT_TRUE(ReadFileFromZipToString(GetParam(), "resources.arsc", &contents)); |
| 459 | |
| 460 | ResTable table; |
| 461 | ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); |
| 462 | |
| 463 | ResTable_config config; |
| 464 | memset(&config, 0, sizeof(config)); |
| 465 | config.orientation = ResTable_config::ORIENTATION_LAND; |
| 466 | table.setParameters(&config); |
| 467 | |
| 468 | String16 name(u"com.android.sparse:integer/foo_9"); |
| 469 | uint32_t flags; |
| 470 | uint32_t resid = |
| 471 | table.identifierForName(name.string(), name.size(), nullptr, 0, nullptr, 0, &flags); |
| 472 | ASSERT_NE(0u, resid); |
| 473 | |
| 474 | Res_value val; |
| 475 | ResTable_config selected_config; |
| 476 | ASSERT_GE( |
| 477 | table.getResource(resid, &val, false /*mayBeBag*/, 0u /*density*/, &flags, &selected_config), |
| 478 | 0); |
| 479 | EXPECT_EQ(Res_value::TYPE_INT_DEC, val.dataType); |
| 480 | EXPECT_EQ(900u, val.data); |
| 481 | } |
| 482 | |
| 483 | INSTANTIATE_TEST_SUITE_P( |
| 484 | FrameWorkResourcesResTableTests, |
| 485 | ResTableParameterizedTest, |
| 486 | ::testing::Values( |
| 487 | base::GetExecutableDirectory() + "/tests/data/sparse/sparse.apk", |
| 488 | base::GetExecutableDirectory() + "/FrameworkResourcesSparseTestApp.apk" |
| 489 | )); |
| 490 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 491 | } // namespace android |