blob: 9aeb00c47e63f0b2b124825ac7e1a9dd05a0962c [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"
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070018
Dan Albert1b4f3162015-04-07 18:43:15 -070019#include <codecvt>
20#include <locale>
21#include <string>
22
Adam Lesinski4c67a472016-11-10 16:43:59 -080023#include "utils/String16.h"
24#include "utils/String8.h"
25
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070026#include "TestHelpers.h"
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -070027#include "data/basic/R.h"
Adam Lesinskida431a22016-12-29 16:08:16 -050028#include "data/lib_one/R.h"
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070029
Adam Lesinski4c67a472016-11-10 16:43:59 -080030namespace basic = com::android::basic;
Adam Lesinskida431a22016-12-29 16:08:16 -050031namespace lib = com::android::lib_one;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070032
Adam Lesinski4c67a472016-11-10 16:43:59 -080033namespace android {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070034
Adam Lesinski4c67a472016-11-10 16:43:59 -080035TEST(ResTableTest, ShouldLoadSuccessfully) {
36 std::string contents;
37 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
38 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070039
Adam Lesinski4c67a472016-11-10 16:43:59 -080040 ResTable table;
41 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070042}
43
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080044TEST(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 Lesinski4c67a472016-11-10 16:43:59 -080072TEST(ResTableTest, SimpleTypeIsRetrievedCorrectly) {
73 std::string contents;
74 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
75 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070076
Adam Lesinski4c67a472016-11-10 16:43:59 -080077 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 Lesinskif90f2f8d2014-06-06 14:27:00 -070081}
82
Adam Lesinski4c67a472016-11-10 16:43:59 -080083TEST(ResTableTest, ResourceNameIsResolved) {
84 std::string contents;
85 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
86 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070087
Adam Lesinski4c67a472016-11-10 16:43:59 -080088 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 Lesinskif90f2f8d2014-06-06 14:27:00 -070098}
99
Adam Lesinski4c67a472016-11-10 16:43:59 -0800100TEST(ResTableTest, NoParentThemeIsAppliedCorrectly) {
101 std::string contents;
102 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
103 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700104
Adam Lesinski4c67a472016-11-10 16:43:59 -0800105 ResTable table;
106 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700107
Adam Lesinski4c67a472016-11-10 16:43:59 -0800108 ResTable::Theme theme(table);
109 ASSERT_EQ(NO_ERROR, theme.applyStyle(basic::R::style::Theme1));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700110
Adam Lesinski4c67a472016-11-10 16:43:59 -0800111 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700122}
123
Adam Lesinski4c67a472016-11-10 16:43:59 -0800124TEST(ResTableTest, ParentThemeIsAppliedCorrectly) {
125 std::string contents;
126 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
127 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700128
Adam Lesinski4c67a472016-11-10 16:43:59 -0800129 ResTable table;
130 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700131
Adam Lesinski4c67a472016-11-10 16:43:59 -0800132 ResTable::Theme theme(table);
133 ASSERT_EQ(NO_ERROR, theme.applyStyle(basic::R::style::Theme2));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700134
Adam Lesinski4c67a472016-11-10 16:43:59 -0800135 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 Lesinskiccf25c7b2014-08-08 15:32:40 -0700146}
147
Adam Lesinski4c67a472016-11-10 16:43:59 -0800148TEST(ResTableTest, LibraryThemeIsAppliedCorrectly) {
149 std::string contents;
Adam Lesinskida431a22016-12-29 16:08:16 -0500150 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/lib_one/lib_one.apk",
Adam Lesinski4c67a472016-11-10 16:43:59 -0800151 "resources.arsc", &contents));
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -0700152
Adam Lesinski4c67a472016-11-10 16:43:59 -0800153 ResTable table;
154 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
Adam Lesinskiccf25c7b2014-08-08 15:32:40 -0700155
Adam Lesinski4c67a472016-11-10 16:43:59 -0800156 ResTable::Theme theme(table);
157 ASSERT_EQ(NO_ERROR, theme.applyStyle(lib::R::style::Theme));
Adam Lesinski8ac51d12016-05-10 10:01:12 -0700158
Adam Lesinski4c67a472016-11-10 16:43:59 -0800159 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700170}
171
Adam Lesinski4c67a472016-11-10 16:43:59 -0800172TEST(ResTableTest, ReferenceToBagIsNotResolved) {
173 std::string contents;
174 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
175 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700176
Adam Lesinski4c67a472016-11-10 16:43:59 -0800177 ResTable table;
178 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700179
Adam Lesinski4c67a472016-11-10 16:43:59 -0800180 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700191}
192
Adam Lesinski4c67a472016-11-10 16:43:59 -0800193TEST(ResTableTest, ResourcesStillAccessibleAfterParameterChange) {
194 std::string contents;
195 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
196 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700197
Adam Lesinski4c67a472016-11-10 16:43:59 -0800198 ResTable table;
199 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700200
Adam Lesinski4c67a472016-11-10 16:43:59 -0800201 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700206
Adam Lesinski4c67a472016-11-10 16:43:59 -0800207 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700211
Adam Lesinski4c67a472016-11-10 16:43:59 -0800212 ResTable_config param;
213 memset(&param, 0, sizeof(param));
214 param.density = 320;
215 table.setParameters(&param);
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700216
Adam Lesinski4c67a472016-11-10 16:43:59 -0800217 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700224}
225
Adam Lesinski4c67a472016-11-10 16:43:59 -0800226TEST(ResTableTest, ResourceIsOverridenWithBetterConfig) {
227 std::string contents;
228 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
229 "resources.arsc", &contents));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700230
Adam Lesinski4c67a472016-11-10 16:43:59 -0800231 ResTable table;
232 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700233
Adam Lesinski4c67a472016-11-10 16:43:59 -0800234 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700240
Adam Lesinski4c67a472016-11-10 16:43:59 -0800241 ResTable_config param;
242 memset(&param, 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(&param);
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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700253}
254
Adam Lesinski2cb761e2014-08-15 13:59:02 -0700255TEST(ResTableTest, emptyTableHasSensibleDefaults) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800256 const int32_t assetCookie = 1;
Adam Lesinski2cb761e2014-08-15 13:59:02 -0700257
Adam Lesinski4c67a472016-11-10 16:43:59 -0800258 ResTable table;
259 ASSERT_EQ(NO_ERROR, table.addEmpty(assetCookie));
Adam Lesinski2cb761e2014-08-15 13:59:02 -0700260
Adam Lesinski4c67a472016-11-10 16:43:59 -0800261 // Adding an empty table gives us one table!
262 ASSERT_EQ(uint32_t(1), table.getTableCount());
Adam Lesinski2cb761e2014-08-15 13:59:02 -0700263
Adam Lesinski4c67a472016-11-10 16:43:59 -0800264 // Adding an empty table doesn't mean we get packages.
265 ASSERT_EQ(uint32_t(0), table.getBasePackageCount());
Adam Lesinski2cb761e2014-08-15 13:59:02 -0700266
Adam Lesinski4c67a472016-11-10 16:43:59 -0800267 Res_value val;
268 ASSERT_LT(table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG),
269 0);
Adam Lesinski2cb761e2014-08-15 13:59:02 -0700270}
271
Dan Albert1b4f3162015-04-07 18:43:15 -0700272void testU16StringToInt(const char16_t* str, uint32_t expectedValue,
273 bool expectSuccess, bool expectHex) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800274 size_t len = std::char_traits<char16_t>::length(str);
Dan Albert1b4f3162015-04-07 18:43:15 -0700275
Adam Lesinski4c67a472016-11-10 16:43:59 -0800276 // 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 Albert1b4f3162015-04-07 18:43:15 -0700279
Adam Lesinski4c67a472016-11-10 16:43:59 -0800280 Res_value out = {};
281 ASSERT_EQ(expectSuccess, U16StringToInt(str, len, &out)) << "Failed with "
282 << s;
Dan Albert1b4f3162015-04-07 18:43:15 -0700283
Adam Lesinski4c67a472016-11-10 16:43:59 -0800284 if (!expectSuccess) {
285 ASSERT_EQ(out.TYPE_NULL, out.dataType) << "Failed with " << s;
286 return;
287 }
Dan Albert1b4f3162015-04-07 18:43:15 -0700288
Adam Lesinski4c67a472016-11-10 16:43:59 -0800289 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 Albert1b4f3162015-04-07 18:43:15 -0700294
Adam Lesinski4c67a472016-11-10 16:43:59 -0800295 ASSERT_EQ(expectedValue, out.data) << "Failed with " << s;
Dan Albert1b4f3162015-04-07 18:43:15 -0700296}
297
298TEST(ResTableTest, U16StringToInt) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800299 testU16StringToInt(u"", 0U, false, false);
300 testU16StringToInt(u" ", 0U, false, false);
301 testU16StringToInt(u"\t\n", 0U, false, false);
Dan Albert1b4f3162015-04-07 18:43:15 -0700302
Adam Lesinski4c67a472016-11-10 16:43:59 -0800303 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 Albert1b4f3162015-04-07 18:43:15 -0700308
Adam Lesinski4c67a472016-11-10 16:43:59 -0800309 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 Albert1b4f3162015-04-07 18:43:15 -0700316
Adam Lesinski4c67a472016-11-10 16:43:59 -0800317 // 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 Albert1b4f3162015-04-07 18:43:15 -0700321
Adam Lesinski4c67a472016-11-10 16:43:59 -0800322 // 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 Albert1b4f3162015-04-07 18:43:15 -0700331
Adam Lesinski4c67a472016-11-10 16:43:59 -0800332 // Hex cases.
333 testU16StringToInt(u"0x0", 0x0, true, true);
334 testU16StringToInt(u"0x42", 0x42, true, true);
335 testU16StringToInt(u" 0x42", 0x42, true, true);
Dan Albert1b4f3162015-04-07 18:43:15 -0700336
Adam Lesinski4c67a472016-11-10 16:43:59 -0800337 // 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 Albert1b4f3162015-04-07 18:43:15 -0700342
Adam Lesinski4c67a472016-11-10 16:43:59 -0800343 // Overflow cases:
344 testU16StringToInt(u"2147483648", 0U, false, false);
345 testU16StringToInt(u"-2147483649", 0U, false, false);
346 testU16StringToInt(u"0x1ffffffff", 0U, false, true);
Dan Albert1b4f3162015-04-07 18:43:15 -0700347}
348
Adam Lesinskiff5808d2016-02-23 17:49:53 -0800349TEST(ResTableTest, ShareButDontModifyResTable) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800350 std::string contents;
351 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
352 "resources.arsc", &contents));
Adam Lesinskiff5808d2016-02-23 17:49:53 -0800353
Adam Lesinski4c67a472016-11-10 16:43:59 -0800354 ResTable sharedTable;
355 ASSERT_EQ(NO_ERROR, sharedTable.add(contents.data(), contents.size()));
Adam Lesinskiff5808d2016-02-23 17:49:53 -0800356
Adam Lesinski4c67a472016-11-10 16:43:59 -0800357 ResTable_config param;
358 memset(&param, 0, sizeof(param));
359 param.language[0] = 'v';
360 param.language[1] = 's';
361 sharedTable.setParameters(&param);
Adam Lesinskiff5808d2016-02-23 17:49:53 -0800362
Adam Lesinski4c67a472016-11-10 16:43:59 -0800363 // 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 Lesinskiff5808d2016-02-23 17:49:53 -0800370
Adam Lesinski4c67a472016-11-10 16:43:59 -0800371 // 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 Lesinskiff5808d2016-02-23 17:49:53 -0800374
Adam Lesinski4c67a472016-11-10 16:43:59 -0800375 // Set a new configuration on the new table.
376 memset(&param, 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(&param);
Adam Lesinskiff5808d2016-02-23 17:49:53 -0800382
Adam Lesinski4c67a472016-11-10 16:43:59 -0800383 // 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 Lesinskif90f2f8d2014-06-06 14:27:00 -0700395}
Adam Lesinskiff5808d2016-02-23 17:49:53 -0800396
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700397TEST(ResTableTest, GetConfigurationsReturnsUniqueList) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800398 std::string contents;
399 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
400 "resources.arsc", &contents));
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700401
Adam Lesinski4c67a472016-11-10 16:43:59 -0800402 std::string system_contents;
403 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/system/system.apk",
404 "resources.arsc", &system_contents));
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700405
Adam Lesinski4c67a472016-11-10 16:43:59 -0800406 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 Lesinskib7e1ce02016-04-11 20:03:01 -0700410
Adam Lesinski4c67a472016-11-10 16:43:59 -0800411 ResTable_config configSv;
412 memset(&configSv, 0, sizeof(configSv));
413 configSv.language[0] = 's';
414 configSv.language[1] = 'v';
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700415
Adam Lesinski4c67a472016-11-10 16:43:59 -0800416 Vector<ResTable_config> configs;
417 table.getConfigurations(&configs);
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700418
Adam Lesinski4c67a472016-11-10 16:43:59 -0800419 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 Lesinskib7e1ce02016-04-11 20:03:01 -0700425}
426
Ryan Mitchell2ad530d2018-03-29 15:49:10 -0700427TEST(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 Mitchell80094e32020-11-16 23:08:18 +0000445 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 Mitchell2ad530d2018-03-29 15:49:10 -0700449
Ryan Mitchell80094e32020-11-16 23:08:18 +0000450 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 Mitchell2ad530d2018-03-29 15:49:10 -0700454
455 // Load an edited apk with the null terminator removed from the end of the
456 // string
457 std::string invalid_contents;
Ryan Mitchell80094e32020-11-16 23:08:18 +0000458 ASSERT_TRUE(ReadFileFromZipToString(
459 GetTestDataPath() + "/length_decode/length_decode_invalid.apk", "resources.arsc",
460 &invalid_contents));
Ryan Mitchell2ad530d2018-03-29 15:49:10 -0700461 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 Mitchell80094e32020-11-16 23:08:18 +0000475 ASSERT_FALSE(invalid_pool->string8At(invalid_val.data).has_value());
476 ASSERT_FALSE(invalid_pool->stringAt(invalid_val.data).has_value());
Ryan Mitchell2ad530d2018-03-29 15:49:10 -0700477}
478
Adam Lesinski4c67a472016-11-10 16:43:59 -0800479} // namespace android