blob: 945981b981a0708e2bd52abc52010f446ba25507 [file] [log] [blame]
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001/*
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 Lesinski4c67a472016-11-10 16:43:59 -080017#include "androidfw/ResourceTypes.h"
Brandon Liu742b11e2022-11-03 23:23:28 +000018#include "android-base/file.h"
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070019
Dan Albert1b4f3162015-04-07 18:43:15 -070020#include <codecvt>
21#include <locale>
22#include <string>
23
Adam Lesinski4c67a472016-11-10 16:43:59 -080024#include "utils/String16.h"
25#include "utils/String8.h"
26
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070027#include "TestHelpers.h"
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -070028#include "data/basic/R.h"
Adam Lesinskida431a22016-12-29 16:08:16 -050029#include "data/lib_one/R.h"
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070030
Adam Lesinski4c67a472016-11-10 16:43:59 -080031namespace basic = com::android::basic;
Adam Lesinskida431a22016-12-29 16:08:16 -050032namespace lib = com::android::lib_one;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070033
Adam Lesinski4c67a472016-11-10 16:43:59 -080034namespace android {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070035
Adam Lesinski4c67a472016-11-10 16:43:59 -080036TEST(ResTableTest, ShouldLoadSuccessfully) {
37 std::string contents;
38 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
39 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070040
Adam Lesinski4c67a472016-11-10 16:43:59 -080041 ResTable table;
42 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070043}
44
Adam Lesinski4c67a472016-11-10 16:43:59 -080045TEST(ResTableTest, SimpleTypeIsRetrievedCorrectly) {
46 std::string contents;
47 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
48 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070049
Adam Lesinski4c67a472016-11-10 16:43:59 -080050 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 Lesinskif90f2f8d2014-06-06 14:27:00 -070054}
55
Adam Lesinski4c67a472016-11-10 16:43:59 -080056TEST(ResTableTest, ResourceNameIsResolved) {
57 std::string contents;
58 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
59 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070060
Adam Lesinski4c67a472016-11-10 16:43:59 -080061 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 Wasilczykade06312023-08-10 23:54:44 +000067 table.identifierForName(testName.c_str(), testName.size(), 0, 0,
68 defPackage.c_str(), defPackage.size());
Adam Lesinski4c67a472016-11-10 16:43:59 -080069 ASSERT_NE(uint32_t(0x00000000), resID);
70 ASSERT_EQ(basic::R::string::test1, resID);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070071}
72
Adam Lesinski4c67a472016-11-10 16:43:59 -080073TEST(ResTableTest, NoParentThemeIsAppliedCorrectly) {
74 std::string contents;
75 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
76 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070077
Adam Lesinski4c67a472016-11-10 16:43:59 -080078 ResTable table;
79 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070080
Adam Lesinski4c67a472016-11-10 16:43:59 -080081 ResTable::Theme theme(table);
82 ASSERT_EQ(NO_ERROR, theme.applyStyle(basic::R::style::Theme1));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070083
Adam Lesinski4c67a472016-11-10 16:43:59 -080084 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 Lesinskif90f2f8d2014-06-06 14:27:00 -070095}
96
Adam Lesinski4c67a472016-11-10 16:43:59 -080097TEST(ResTableTest, ParentThemeIsAppliedCorrectly) {
98 std::string contents;
99 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
100 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700101
Adam Lesinski4c67a472016-11-10 16:43:59 -0800102 ResTable table;
103 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700104
Adam Lesinski4c67a472016-11-10 16:43:59 -0800105 ResTable::Theme theme(table);
106 ASSERT_EQ(NO_ERROR, theme.applyStyle(basic::R::style::Theme2));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700107
Adam Lesinski4c67a472016-11-10 16:43:59 -0800108 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 Lesinskiccf25c7b2014-08-08 15:32:40 -0700119}
120
Adam Lesinski4c67a472016-11-10 16:43:59 -0800121TEST(ResTableTest, LibraryThemeIsAppliedCorrectly) {
122 std::string contents;
Adam Lesinskida431a22016-12-29 16:08:16 -0500123 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/lib_one/lib_one.apk",
Adam Lesinski4c67a472016-11-10 16:43:59 -0800124 "resources.arsc", &contents));
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -0700125
Adam Lesinski4c67a472016-11-10 16:43:59 -0800126 ResTable table;
127 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -0700128
Adam Lesinski4c67a472016-11-10 16:43:59 -0800129 ResTable::Theme theme(table);
130 ASSERT_EQ(NO_ERROR, theme.applyStyle(lib::R::style::Theme));
Adam Lesinski8ac51d12016-05-10 10:01:12 -0700131
Adam Lesinski4c67a472016-11-10 16:43:59 -0800132 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700143}
144
Adam Lesinski4c67a472016-11-10 16:43:59 -0800145TEST(ResTableTest, ReferenceToBagIsNotResolved) {
146 std::string contents;
147 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
148 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700149
Adam Lesinski4c67a472016-11-10 16:43:59 -0800150 ResTable table;
151 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700152
Adam Lesinski4c67a472016-11-10 16:43:59 -0800153 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700164}
165
Adam Lesinski4c67a472016-11-10 16:43:59 -0800166TEST(ResTableTest, ResourcesStillAccessibleAfterParameterChange) {
167 std::string contents;
168 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
169 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700170
Adam Lesinski4c67a472016-11-10 16:43:59 -0800171 ResTable table;
172 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700173
Adam Lesinski4c67a472016-11-10 16:43:59 -0800174 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700179
Adam Lesinski4c67a472016-11-10 16:43:59 -0800180 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700184
Adam Lesinski4c67a472016-11-10 16:43:59 -0800185 ResTable_config param;
186 memset(&param, 0, sizeof(param));
187 param.density = 320;
188 table.setParameters(&param);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700189
Adam Lesinski4c67a472016-11-10 16:43:59 -0800190 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700197}
198
Adam Lesinski4c67a472016-11-10 16:43:59 -0800199TEST(ResTableTest, ResourceIsOverridenWithBetterConfig) {
200 std::string contents;
201 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
202 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700203
Adam Lesinski4c67a472016-11-10 16:43:59 -0800204 ResTable table;
205 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700206
Adam Lesinski4c67a472016-11-10 16:43:59 -0800207 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700213
Adam Lesinski4c67a472016-11-10 16:43:59 -0800214 ResTable_config param;
215 memset(&param, 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(&param);
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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700226}
227
Adam Lesinski2cb761e2014-08-15 13:59:02 -0700228TEST(ResTableTest, emptyTableHasSensibleDefaults) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800229 const int32_t assetCookie = 1;
Adam Lesinski2cb761e2014-08-15 13:59:02 -0700230
Adam Lesinski4c67a472016-11-10 16:43:59 -0800231 ResTable table;
232 ASSERT_EQ(NO_ERROR, table.addEmpty(assetCookie));
Adam Lesinski2cb761e2014-08-15 13:59:02 -0700233
Adam Lesinski4c67a472016-11-10 16:43:59 -0800234 // Adding an empty table gives us one table!
235 ASSERT_EQ(uint32_t(1), table.getTableCount());
Adam Lesinski2cb761e2014-08-15 13:59:02 -0700236
Adam Lesinski4c67a472016-11-10 16:43:59 -0800237 // Adding an empty table doesn't mean we get packages.
238 ASSERT_EQ(uint32_t(0), table.getBasePackageCount());
Adam Lesinski2cb761e2014-08-15 13:59:02 -0700239
Adam Lesinski4c67a472016-11-10 16:43:59 -0800240 Res_value val;
241 ASSERT_LT(table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG),
242 0);
Adam Lesinski2cb761e2014-08-15 13:59:02 -0700243}
244
Dan Albert1b4f3162015-04-07 18:43:15 -0700245void testU16StringToInt(const char16_t* str, uint32_t expectedValue,
246 bool expectSuccess, bool expectHex) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800247 size_t len = std::char_traits<char16_t>::length(str);
Dan Albert1b4f3162015-04-07 18:43:15 -0700248
Adam Lesinski4c67a472016-11-10 16:43:59 -0800249 // 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 Albert1b4f3162015-04-07 18:43:15 -0700252
Adam Lesinski4c67a472016-11-10 16:43:59 -0800253 Res_value out = {};
254 ASSERT_EQ(expectSuccess, U16StringToInt(str, len, &out)) << "Failed with "
255 << s;
Dan Albert1b4f3162015-04-07 18:43:15 -0700256
Adam Lesinski4c67a472016-11-10 16:43:59 -0800257 if (!expectSuccess) {
258 ASSERT_EQ(out.TYPE_NULL, out.dataType) << "Failed with " << s;
259 return;
260 }
Dan Albert1b4f3162015-04-07 18:43:15 -0700261
Adam Lesinski4c67a472016-11-10 16:43:59 -0800262 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 Albert1b4f3162015-04-07 18:43:15 -0700267
Adam Lesinski4c67a472016-11-10 16:43:59 -0800268 ASSERT_EQ(expectedValue, out.data) << "Failed with " << s;
Dan Albert1b4f3162015-04-07 18:43:15 -0700269}
270
271TEST(ResTableTest, U16StringToInt) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800272 testU16StringToInt(u"", 0U, false, false);
273 testU16StringToInt(u" ", 0U, false, false);
274 testU16StringToInt(u"\t\n", 0U, false, false);
Dan Albert1b4f3162015-04-07 18:43:15 -0700275
Adam Lesinski4c67a472016-11-10 16:43:59 -0800276 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 Albert1b4f3162015-04-07 18:43:15 -0700281
Adam Lesinski4c67a472016-11-10 16:43:59 -0800282 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 Albert1b4f3162015-04-07 18:43:15 -0700289
Adam Lesinski4c67a472016-11-10 16:43:59 -0800290 // 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 Albert1b4f3162015-04-07 18:43:15 -0700294
Adam Lesinski4c67a472016-11-10 16:43:59 -0800295 // 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 Albert1b4f3162015-04-07 18:43:15 -0700304
Adam Lesinski4c67a472016-11-10 16:43:59 -0800305 // Hex cases.
306 testU16StringToInt(u"0x0", 0x0, true, true);
307 testU16StringToInt(u"0x42", 0x42, true, true);
308 testU16StringToInt(u" 0x42", 0x42, true, true);
Dan Albert1b4f3162015-04-07 18:43:15 -0700309
Adam Lesinski4c67a472016-11-10 16:43:59 -0800310 // 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 Albert1b4f3162015-04-07 18:43:15 -0700315
Adam Lesinski4c67a472016-11-10 16:43:59 -0800316 // Overflow cases:
317 testU16StringToInt(u"2147483648", 0U, false, false);
318 testU16StringToInt(u"-2147483649", 0U, false, false);
319 testU16StringToInt(u"0x1ffffffff", 0U, false, true);
Dan Albert1b4f3162015-04-07 18:43:15 -0700320}
321
Adam Lesinskiff5808d2016-02-23 17:49:53 -0800322TEST(ResTableTest, ShareButDontModifyResTable) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800323 std::string contents;
324 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
325 "resources.arsc", &contents));
Adam Lesinskiff5808d2016-02-23 17:49:53 -0800326
Adam Lesinski4c67a472016-11-10 16:43:59 -0800327 ResTable sharedTable;
328 ASSERT_EQ(NO_ERROR, sharedTable.add(contents.data(), contents.size()));
Adam Lesinskiff5808d2016-02-23 17:49:53 -0800329
Adam Lesinski4c67a472016-11-10 16:43:59 -0800330 ResTable_config param;
331 memset(&param, 0, sizeof(param));
332 param.language[0] = 'v';
333 param.language[1] = 's';
334 sharedTable.setParameters(&param);
Adam Lesinskiff5808d2016-02-23 17:49:53 -0800335
Adam Lesinski4c67a472016-11-10 16:43:59 -0800336 // 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 Lesinskiff5808d2016-02-23 17:49:53 -0800343
Adam Lesinski4c67a472016-11-10 16:43:59 -0800344 // 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 Lesinskiff5808d2016-02-23 17:49:53 -0800347
Adam Lesinski4c67a472016-11-10 16:43:59 -0800348 // Set a new configuration on the new table.
349 memset(&param, 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(&param);
Adam Lesinskiff5808d2016-02-23 17:49:53 -0800355
Adam Lesinski4c67a472016-11-10 16:43:59 -0800356 // 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700368}
Adam Lesinskiff5808d2016-02-23 17:49:53 -0800369
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700370TEST(ResTableTest, GetConfigurationsReturnsUniqueList) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800371 std::string contents;
372 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
373 "resources.arsc", &contents));
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700374
Adam Lesinski4c67a472016-11-10 16:43:59 -0800375 std::string system_contents;
376 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/system/system.apk",
377 "resources.arsc", &system_contents));
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700378
Adam Lesinski4c67a472016-11-10 16:43:59 -0800379 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 Lesinskib7e1ce02016-04-11 20:03:01 -0700383
Adam Lesinski4c67a472016-11-10 16:43:59 -0800384 ResTable_config configSv;
385 memset(&configSv, 0, sizeof(configSv));
386 configSv.language[0] = 's';
387 configSv.language[1] = 'v';
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700388
Adam Lesinski4c67a472016-11-10 16:43:59 -0800389 Vector<ResTable_config> configs;
390 table.getConfigurations(&configs);
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700391
Adam Lesinski4c67a472016-11-10 16:43:59 -0800392 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 Lesinskib7e1ce02016-04-11 20:03:01 -0700398}
399
Ryan Mitchellea9e8b42018-03-29 15:49:10 -0700400TEST(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 Mitchelldb21f09a2020-11-16 23:08:18 +0000418 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 Mitchellea9e8b42018-03-29 15:49:10 -0700422
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000423 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 Mitchellea9e8b42018-03-29 15:49:10 -0700427
428 // Load an edited apk with the null terminator removed from the end of the
429 // string
430 std::string invalid_contents;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000431 ASSERT_TRUE(ReadFileFromZipToString(
432 GetTestDataPath() + "/length_decode/length_decode_invalid.apk", "resources.arsc",
433 &invalid_contents));
Ryan Mitchellea9e8b42018-03-29 15:49:10 -0700434 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 Mitchelldb21f09a2020-11-16 23:08:18 +0000448 ASSERT_FALSE(invalid_pool->string8At(invalid_val.data).has_value());
449 ASSERT_FALSE(invalid_pool->stringAt(invalid_val.data).has_value());
Ryan Mitchellea9e8b42018-03-29 15:49:10 -0700450}
451
Brandon Liu742b11e2022-11-03 23:23:28 +0000452class ResTableParameterizedTest :
453 public testing::TestWithParam<std::string> {
454};
455
456TEST_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
483INSTANTIATE_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 Lesinski4c67a472016-11-10 16:43:59 -0800491} // namespace android