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