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
17 #include "androidfw/ResourceTypes.h"
18 #include "android-base/file.h"
19
20 #include <codecvt>
21 #include <locale>
22 #include <string>
23
24 #include "utils/String16.h"
25 #include "utils/String8.h"
26
27 #include "TestHelpers.h"
28 #include "data/basic/R.h"
29 #include "data/lib_one/R.h"
30
31 namespace basic = com::android::basic;
32 namespace lib = com::android::lib_one;
33
34 namespace android {
35
TEST(ResTableTest,ShouldLoadSuccessfully)36 TEST(ResTableTest, ShouldLoadSuccessfully) {
37 std::string contents;
38 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
39 "resources.arsc", &contents));
40
41 ResTable table;
42 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
43 }
44
TEST(ResTableTest,SimpleTypeIsRetrievedCorrectly)45 TEST(ResTableTest, SimpleTypeIsRetrievedCorrectly) {
46 std::string contents;
47 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
48 "resources.arsc", &contents));
49
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"));
54 }
55
TEST(ResTableTest,ResourceNameIsResolved)56 TEST(ResTableTest, ResourceNameIsResolved) {
57 std::string contents;
58 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
59 "resources.arsc", &contents));
60
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 =
67 table.identifierForName(testName.c_str(), testName.size(), 0, 0,
68 defPackage.c_str(), defPackage.size());
69 ASSERT_NE(uint32_t(0x00000000), resID);
70 ASSERT_EQ(basic::R::string::test1, resID);
71 }
72
TEST(ResTableTest,NoParentThemeIsAppliedCorrectly)73 TEST(ResTableTest, NoParentThemeIsAppliedCorrectly) {
74 std::string contents;
75 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
76 "resources.arsc", &contents));
77
78 ResTable table;
79 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
80
81 ResTable::Theme theme(table);
82 ASSERT_EQ(NO_ERROR, theme.applyStyle(basic::R::style::Theme1));
83
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);
95 }
96
TEST(ResTableTest,ParentThemeIsAppliedCorrectly)97 TEST(ResTableTest, ParentThemeIsAppliedCorrectly) {
98 std::string contents;
99 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
100 "resources.arsc", &contents));
101
102 ResTable table;
103 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
104
105 ResTable::Theme theme(table);
106 ASSERT_EQ(NO_ERROR, theme.applyStyle(basic::R::style::Theme2));
107
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);
119 }
120
TEST(ResTableTest,LibraryThemeIsAppliedCorrectly)121 TEST(ResTableTest, LibraryThemeIsAppliedCorrectly) {
122 std::string contents;
123 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/lib_one/lib_one.apk",
124 "resources.arsc", &contents));
125
126 ResTable table;
127 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
128
129 ResTable::Theme theme(table);
130 ASSERT_EQ(NO_ERROR, theme.applyStyle(lib::R::style::Theme));
131
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);
143 }
144
TEST(ResTableTest,ReferenceToBagIsNotResolved)145 TEST(ResTableTest, ReferenceToBagIsNotResolved) {
146 std::string contents;
147 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
148 "resources.arsc", &contents));
149
150 ResTable table;
151 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
152
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);
164 }
165
TEST(ResTableTest,ResourcesStillAccessibleAfterParameterChange)166 TEST(ResTableTest, ResourcesStillAccessibleAfterParameterChange) {
167 std::string contents;
168 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
169 "resources.arsc", &contents));
170
171 ResTable table;
172 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
173
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);
179
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);
184
185 ResTable_config param;
186 memset(¶m, 0, sizeof(param));
187 param.density = 320;
188 table.setParameters(¶m);
189
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);
197 }
198
TEST(ResTableTest,ResourceIsOverridenWithBetterConfig)199 TEST(ResTableTest, ResourceIsOverridenWithBetterConfig) {
200 std::string contents;
201 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
202 "resources.arsc", &contents));
203
204 ResTable table;
205 ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size()));
206
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);
213
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);
226 }
227
TEST(ResTableTest,emptyTableHasSensibleDefaults)228 TEST(ResTableTest, emptyTableHasSensibleDefaults) {
229 const int32_t assetCookie = 1;
230
231 ResTable table;
232 ASSERT_EQ(NO_ERROR, table.addEmpty(assetCookie));
233
234 // Adding an empty table gives us one table!
235 ASSERT_EQ(uint32_t(1), table.getTableCount());
236
237 // Adding an empty table doesn't mean we get packages.
238 ASSERT_EQ(uint32_t(0), table.getBasePackageCount());
239
240 Res_value val;
241 ASSERT_LT(table.getResource(basic::R::integer::number1, &val, MAY_NOT_BE_BAG),
242 0);
243 }
244
testU16StringToInt(const char16_t * str,uint32_t expectedValue,bool expectSuccess,bool expectHex)245 void testU16StringToInt(const char16_t* str, uint32_t expectedValue,
246 bool expectSuccess, bool expectHex) {
247 size_t len = std::char_traits<char16_t>::length(str);
248
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));
252
253 Res_value out = {};
254 ASSERT_EQ(expectSuccess, U16StringToInt(str, len, &out)) << "Failed with "
255 << s;
256
257 if (!expectSuccess) {
258 ASSERT_EQ(out.TYPE_NULL, out.dataType) << "Failed with " << s;
259 return;
260 }
261
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 }
267
268 ASSERT_EQ(expectedValue, out.data) << "Failed with " << s;
269 }
270
TEST(ResTableTest,U16StringToInt)271 TEST(ResTableTest, U16StringToInt) {
272 testU16StringToInt(u"", 0U, false, false);
273 testU16StringToInt(u" ", 0U, false, false);
274 testU16StringToInt(u"\t\n", 0U, false, false);
275
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);
281
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);
289
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);
294
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);
304
305 // Hex cases.
306 testU16StringToInt(u"0x0", 0x0, true, true);
307 testU16StringToInt(u"0x42", 0x42, true, true);
308 testU16StringToInt(u" 0x42", 0x42, true, true);
309
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);
315
316 // Overflow cases:
317 testU16StringToInt(u"2147483648", 0U, false, false);
318 testU16StringToInt(u"-2147483649", 0U, false, false);
319 testU16StringToInt(u"0x1ffffffff", 0U, false, true);
320 }
321
TEST(ResTableTest,ShareButDontModifyResTable)322 TEST(ResTableTest, ShareButDontModifyResTable) {
323 std::string contents;
324 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
325 "resources.arsc", &contents));
326
327 ResTable sharedTable;
328 ASSERT_EQ(NO_ERROR, sharedTable.add(contents.data(), contents.size()));
329
330 ResTable_config param;
331 memset(¶m, 0, sizeof(param));
332 param.language[0] = 'v';
333 param.language[1] = 's';
334 sharedTable.setParameters(¶m);
335
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);
343
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));
347
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);
355
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);
368 }
369
TEST(ResTableTest,GetConfigurationsReturnsUniqueList)370 TEST(ResTableTest, GetConfigurationsReturnsUniqueList) {
371 std::string contents;
372 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk",
373 "resources.arsc", &contents));
374
375 std::string system_contents;
376 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/system/system.apk",
377 "resources.arsc", &system_contents));
378
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()));
383
384 ResTable_config configSv;
385 memset(&configSv, 0, sizeof(configSv));
386 configSv.language[0] = 's';
387 configSv.language[1] = 'v';
388
389 Vector<ResTable_config> configs;
390 table.getConfigurations(&configs);
391
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")));
398 }
399
TEST(ResTableTest,TruncatedEncodeLength)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
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], ']');
422
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) ']');
427
428 // Load an edited apk with the null terminator removed from the end of the
429 // string
430 std::string invalid_contents;
431 ASSERT_TRUE(ReadFileFromZipToString(
432 GetTestDataPath() + "/length_decode/length_decode_invalid.apk", "resources.arsc",
433 &invalid_contents));
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
448 ASSERT_FALSE(invalid_pool->string8At(invalid_val.data).has_value());
449 ASSERT_FALSE(invalid_pool->stringAt(invalid_val.data).has_value());
450 }
451
452 class ResTableParameterizedTest :
453 public testing::TestWithParam<std::string> {
454 };
455
TEST_P(ResTableParameterizedTest,ShouldLoadSparseEntriesSuccessfully)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.c_str(), 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
491 } // namespace android
492