1 /*
2  * Copyright (C) 2016 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/LoadedArsc.h"
18 
19 #include "android-base/file.h"
20 #include "androidfw/ResourceUtils.h"
21 
22 #include "TestHelpers.h"
23 #include "data/basic/R.h"
24 #include "data/libclient/R.h"
25 #include "data/overlayable/R.h"
26 #include "data/sparse/R.h"
27 #include "data/styles/R.h"
28 #include "data/system/R.h"
29 
30 namespace app = com::android::app;
31 namespace basic = com::android::basic;
32 namespace libclient = com::android::libclient;
33 namespace overlayable = com::android::overlayable;
34 namespace sparse = com::android::sparse;
35 
36 using ::android::base::ReadFileToString;
37 using ::testing::Eq;
38 using ::testing::Ge;
39 using ::testing::IsNull;
40 using ::testing::NotNull;
41 using ::testing::SizeIs;
42 using ::testing::StrEq;
43 
44 using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
45 
46 namespace android {
47 
TEST(LoadedArscTest,LoadSinglePackageArsc)48 TEST(LoadedArscTest, LoadSinglePackageArsc) {
49   std::string contents;
50   ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/styles/styles.apk", "resources.arsc",
51                                       &contents));
52 
53   auto loaded_arsc = LoadedArsc::Load(reinterpret_cast<const void*>(contents.data()),
54                                                                     contents.length());
55   ASSERT_THAT(loaded_arsc, NotNull());
56 
57   const LoadedPackage* package =
58       loaded_arsc->GetPackageById(get_package_id(app::R::string::string_one));
59   ASSERT_THAT(package, NotNull());
60   EXPECT_THAT(package->GetPackageName(), StrEq("com.android.app"));
61   EXPECT_THAT(package->GetPackageId(), Eq(0x7f));
62 
63   const uint8_t type_index = get_type_id(app::R::string::string_one) - 1;
64   const uint16_t entry_index = get_entry_id(app::R::string::string_one);
65 
66   const TypeSpec* type_spec = package->GetTypeSpecByTypeIndex(type_index);
67   ASSERT_THAT(type_spec, NotNull());
68   ASSERT_THAT(type_spec->type_entries.size(), Ge(1u));
69 
70   auto type = type_spec->type_entries[0];
71   ASSERT_TRUE(LoadedPackage::GetEntry(type.type, entry_index).has_value());
72 }
73 
TEST(LoadedArscTest,LoadSharedLibrary)74 TEST(LoadedArscTest, LoadSharedLibrary) {
75   std::string contents;
76   ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/lib_one/lib_one.apk", "resources.arsc",
77                                       &contents));
78 
79   std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(contents.data(),
80                                                                    contents.length());
81   ASSERT_THAT(loaded_arsc, NotNull());
82 
83   const auto& packages = loaded_arsc->GetPackages();
84   ASSERT_THAT(packages, SizeIs(1u));
85   EXPECT_TRUE(packages[0]->IsDynamic());
86   EXPECT_THAT(packages[0]->GetPackageName(), StrEq("com.android.lib_one"));
87   EXPECT_THAT(packages[0]->GetPackageId(), Eq(0));
88 
89   const auto& dynamic_pkg_map = packages[0]->GetDynamicPackageMap();
90 
91   // The library has no dependencies.
92   ASSERT_TRUE(dynamic_pkg_map.empty());
93 }
94 
TEST(LoadedArscTest,LoadAppLinkedAgainstSharedLibrary)95 TEST(LoadedArscTest, LoadAppLinkedAgainstSharedLibrary) {
96   std::string contents;
97   ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/libclient/libclient.apk",
98                                       "resources.arsc", &contents));
99 
100   std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(contents.data(),
101                                                                    contents.length());
102   ASSERT_THAT(loaded_arsc, NotNull());
103 
104   const auto& packages = loaded_arsc->GetPackages();
105   ASSERT_THAT(packages, SizeIs(1u));
106   EXPECT_FALSE(packages[0]->IsDynamic());
107   EXPECT_THAT(packages[0]->GetPackageName(), StrEq("com.android.libclient"));
108   EXPECT_THAT(packages[0]->GetPackageId(), Eq(0x7f));
109 
110   const auto& dynamic_pkg_map = packages[0]->GetDynamicPackageMap();
111 
112   // The library has two dependencies.
113   ASSERT_THAT(dynamic_pkg_map, SizeIs(2u));
114   EXPECT_THAT(dynamic_pkg_map[0].package_name, StrEq("com.android.lib_one"));
115   EXPECT_THAT(dynamic_pkg_map[0].package_id, Eq(0x02));
116 
117   EXPECT_THAT(dynamic_pkg_map[1].package_name, StrEq("com.android.lib_two"));
118   EXPECT_THAT(dynamic_pkg_map[1].package_id, Eq(0x03));
119 }
120 
TEST(LoadedArscTest,LoadAppAsSharedLibrary)121 TEST(LoadedArscTest, LoadAppAsSharedLibrary) {
122   std::string contents;
123   ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/appaslib/appaslib.apk",
124                                       "resources.arsc", &contents));
125 
126   std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(contents.data(),
127                                                                    contents.length(),
128                                                                    nullptr /* loaded_idmap */,
129                                                                    PROPERTY_DYNAMIC);
130   ASSERT_THAT(loaded_arsc, NotNull());
131 
132   const auto& packages = loaded_arsc->GetPackages();
133   ASSERT_THAT(packages, SizeIs(1u));
134   EXPECT_TRUE(packages[0]->IsDynamic());
135   EXPECT_THAT(packages[0]->GetPackageId(), Eq(0x7f));
136 }
137 
TEST(LoadedArscTest,LoadFeatureSplit)138 TEST(LoadedArscTest, LoadFeatureSplit) {
139   std::string contents;
140   ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/feature/feature.apk", "resources.arsc",
141                                       &contents));
142   std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(contents.data(),
143                                                                    contents.length());
144   ASSERT_THAT(loaded_arsc, NotNull());
145 
146   const LoadedPackage* package =
147       loaded_arsc->GetPackageById(get_package_id(basic::R::string::test3));
148   ASSERT_THAT(package, NotNull());
149 
150   uint8_t type_index = get_type_id(basic::R::string::test3) - 1;
151   uint8_t entry_index = get_entry_id(basic::R::string::test3);
152 
153   const TypeSpec* type_spec = package->GetTypeSpecByTypeIndex(type_index);
154   ASSERT_THAT(type_spec, NotNull());
155   ASSERT_THAT(type_spec->type_entries.size(), Ge(1u));
156 
157   auto type_name16 = package->GetTypeStringPool()->stringAt(type_spec->type_spec->id - 1);
158   ASSERT_TRUE(type_name16.has_value());
159   EXPECT_THAT(util::Utf16ToUtf8(*type_name16), StrEq("string"));
160 
161   ASSERT_TRUE(LoadedPackage::GetEntry(type_spec->type_entries[0].type, entry_index).has_value());
162 }
163 
164 // AAPT(2) generates resource tables with chunks in a certain order. The rule is that
165 // a RES_TABLE_TYPE_TYPE with id `i` must always be preceded by a RES_TABLE_TYPE_SPEC_TYPE with
166 // id `i`. The RES_TABLE_TYPE_SPEC_TYPE does not need to be directly preceding, however.
167 //
168 // AAPT(2) generates something like:
169 //   RES_TABLE_TYPE_SPEC_TYPE id=1
170 //   RES_TABLE_TYPE_TYPE id=1
171 //   RES_TABLE_TYPE_SPEC_TYPE id=2
172 //   RES_TABLE_TYPE_TYPE id=2
173 //
174 // But the following is valid too:
175 //   RES_TABLE_TYPE_SPEC_TYPE id=1
176 //   RES_TABLE_TYPE_SPEC_TYPE id=2
177 //   RES_TABLE_TYPE_TYPE id=1
178 //   RES_TABLE_TYPE_TYPE id=2
179 //
TEST(LoadedArscTest,LoadOutOfOrderTypeSpecs)180 TEST(LoadedArscTest, LoadOutOfOrderTypeSpecs) {
181   std::string contents;
182   ASSERT_TRUE(
183       ReadFileFromZipToString(GetTestDataPath() + "/out_of_order_types/out_of_order_types.apk",
184                               "resources.arsc", &contents));
185 
186   std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(contents.data(),
187                                                                    contents.length());
188   ASSERT_THAT(loaded_arsc, NotNull());
189 
190   ASSERT_THAT(loaded_arsc->GetPackages(), SizeIs(1u));
191   const auto& package = loaded_arsc->GetPackages()[0];
192   ASSERT_THAT(package, NotNull());
193 
194   const TypeSpec* type_spec = package->GetTypeSpecByTypeIndex(0);
195   ASSERT_THAT(type_spec, NotNull());
196   ASSERT_THAT(type_spec->type_entries.size(), Ge(1u));
197 
198   type_spec = package->GetTypeSpecByTypeIndex(1);
199   ASSERT_THAT(type_spec, NotNull());
200   ASSERT_THAT(type_spec->type_entries.size(), Ge(1u));
201 }
202 
TEST(LoadedArscTest,LoadOverlayable)203 TEST(LoadedArscTest, LoadOverlayable) {
204   std::string contents;
205   ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/overlayable/overlayable.apk",
206                                       "resources.arsc", &contents));
207 
208   std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(contents.data(),
209                                                                    contents.length());
210 
211   ASSERT_THAT(loaded_arsc, NotNull());
212   const LoadedPackage* package = loaded_arsc->GetPackageById(
213       get_package_id(overlayable::R::string::not_overlayable));
214 
215   const OverlayableInfo* info = package->GetOverlayableInfo(
216       overlayable::R::string::not_overlayable);
217   ASSERT_THAT(info, IsNull());
218 
219   info = package->GetOverlayableInfo(overlayable::R::string::overlayable1);
220   ASSERT_THAT(info, NotNull());
221   EXPECT_THAT(info->name, Eq("OverlayableResources1"));
222   EXPECT_THAT(info->actor, Eq("overlay://theme"));
223   EXPECT_THAT(info->policy_flags, Eq(PolicyFlags::PUBLIC));
224 
225   info = package->GetOverlayableInfo(overlayable::R::string::overlayable2);
226   ASSERT_THAT(info, NotNull());
227   EXPECT_THAT(info->name, Eq("OverlayableResources1"));
228   EXPECT_THAT(info->actor, Eq("overlay://theme"));
229   EXPECT_THAT(info->policy_flags,
230               Eq(PolicyFlags::SYSTEM_PARTITION
231                  | PolicyFlags::PRODUCT_PARTITION));
232 
233   info = package->GetOverlayableInfo(overlayable::R::string::overlayable3);
234   ASSERT_THAT(info, NotNull());
235   EXPECT_THAT(info->name, Eq("OverlayableResources2"));
236   EXPECT_THAT(info->actor, Eq("overlay://com.android.overlayable"));
237   EXPECT_THAT(info->policy_flags,
238               Eq(PolicyFlags::VENDOR_PARTITION
239                  | PolicyFlags::PRODUCT_PARTITION));
240 
241   info = package->GetOverlayableInfo(overlayable::R::string::overlayable4);
242   EXPECT_THAT(info->name, Eq("OverlayableResources1"));
243   EXPECT_THAT(info->actor, Eq("overlay://theme"));
244   ASSERT_THAT(info, NotNull());
245   EXPECT_THAT(info->policy_flags, Eq(PolicyFlags::PUBLIC));
246 }
247 
TEST(LoadedArscTest,ResourceIdentifierIterator)248 TEST(LoadedArscTest, ResourceIdentifierIterator) {
249   std::string contents;
250   ASSERT_TRUE(
251       ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", "resources.arsc", &contents));
252 
253   std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(contents.data(),
254                                                                    contents.length());
255   ASSERT_NE(nullptr, loaded_arsc);
256 
257   const std::vector<std::unique_ptr<const LoadedPackage>>& packages = loaded_arsc->GetPackages();
258   ASSERT_EQ(1u, packages.size());
259   ASSERT_EQ(std::string("com.android.basic"), packages[0]->GetPackageName());
260 
261   const auto& loaded_package = packages[0];
262   auto iter = loaded_package->begin();
263   auto end = loaded_package->end();
264 
265   ASSERT_NE(end, iter);
266   ASSERT_EQ(0x7f010000u, *iter++);
267   ASSERT_EQ(0x7f010001u, *iter++);
268   ASSERT_EQ(0x7f020000u, *iter++);
269   ASSERT_EQ(0x7f020001u, *iter++);
270   ASSERT_EQ(0x7f030000u, *iter++);
271   ASSERT_EQ(0x7f030001u, *iter++);
272   ASSERT_EQ(0x7f030002u, *iter++);  // note: string without default, excluded by aapt2 dump
273   ASSERT_EQ(0x7f040000u, *iter++);
274   ASSERT_EQ(0x7f040001u, *iter++);
275   ASSERT_EQ(0x7f040002u, *iter++);
276   ASSERT_EQ(0x7f040003u, *iter++);
277   ASSERT_EQ(0x7f040004u, *iter++);
278   ASSERT_EQ(0x7f040005u, *iter++);
279   ASSERT_EQ(0x7f040006u, *iter++);
280   ASSERT_EQ(0x7f040007u, *iter++);
281   ASSERT_EQ(0x7f040008u, *iter++);
282   ASSERT_EQ(0x7f040009u, *iter++);
283   ASSERT_EQ(0x7f04000au, *iter++);
284   ASSERT_EQ(0x7f04000bu, *iter++);
285   ASSERT_EQ(0x7f04000cu, *iter++);
286   ASSERT_EQ(0x7f04000du, *iter++);
287   ASSERT_EQ(0x7f050000u, *iter++);
288   ASSERT_EQ(0x7f050001u, *iter++);
289   ASSERT_EQ(0x7f060000u, *iter++);
290   ASSERT_EQ(0x7f070000u, *iter++);
291   ASSERT_EQ(0x7f070001u, *iter++);
292   ASSERT_EQ(0x7f070002u, *iter++);
293   ASSERT_EQ(0x7f070003u, *iter++);
294   ASSERT_EQ(end, iter);
295 }
296 
TEST(LoadedArscTest,GetOverlayableMap)297 TEST(LoadedArscTest, GetOverlayableMap) {
298   std::string contents;
299   ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/overlayable/overlayable.apk",
300                                       "resources.arsc", &contents));
301 
302   std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(contents.data(),
303                                                                    contents.length());
304   ASSERT_NE(nullptr, loaded_arsc);
305 
306   const std::vector<std::unique_ptr<const LoadedPackage>>& packages = loaded_arsc->GetPackages();
307   ASSERT_EQ(1u, packages.size());
308   ASSERT_EQ(std::string("com.android.overlayable"), packages[0]->GetPackageName());
309 
310   const auto map = packages[0]->GetOverlayableMap();
311   ASSERT_EQ(3, map.size());
312   ASSERT_EQ(map.at("OverlayableResources1"), "overlay://theme");
313   ASSERT_EQ(map.at("OverlayableResources2"), "overlay://com.android.overlayable");
314   ASSERT_EQ(map.at("OverlayableResources3"), "");
315 }
316 
TEST(LoadedArscTest,LoadCustomLoader)317 TEST(LoadedArscTest, LoadCustomLoader) {
318   auto asset = AssetsProvider::CreateAssetFromFile(GetTestDataPath() + "/loader/resources.arsc");
319   ASSERT_THAT(asset, NotNull());
320 
321   const StringPiece data(
322       reinterpret_cast<const char*>(asset->getBuffer(true /*wordAligned*/)),
323       asset->getLength());
324 
325   std::unique_ptr<const LoadedArsc> loaded_arsc =
326       LoadedArsc::Load(data.data(), data.length(), nullptr, PROPERTY_LOADER);
327   ASSERT_THAT(loaded_arsc, NotNull());
328 
329   const LoadedPackage* package =
330       loaded_arsc->GetPackageById(get_package_id(overlayable::R::string::overlayable11));
331   ASSERT_THAT(package, NotNull());
332   EXPECT_THAT(package->GetPackageName(), StrEq("com.android.loader"));
333   EXPECT_THAT(package->GetPackageId(), Eq(0x7f));
334 
335   const uint8_t type_index = get_type_id(overlayable::R::string::overlayable11) - 1;
336   const uint16_t entry_index = get_entry_id(overlayable::R::string::overlayable11);
337 
338   const TypeSpec* type_spec = package->GetTypeSpecByTypeIndex(type_index);
339   ASSERT_THAT(type_spec, NotNull());
340   ASSERT_THAT(type_spec->type_entries.size(), Ge(1u));
341 
342   auto type = type_spec->type_entries[0];
343   ASSERT_TRUE(LoadedPackage::GetEntry(type.type, entry_index).has_value());
344 }
345 
346 // structs with size fields (like Res_value, ResTable_entry) should be
347 // backwards and forwards compatible (aka checking the size field against
348 // sizeof(Res_value) might not be backwards compatible.
349 // TEST(LoadedArscTest, LoadingShouldBeForwardsAndBackwardsCompatible) { ASSERT_TRUE(false); }
350 
351 class LoadedArscParameterizedTest :
352     public testing::TestWithParam<std::string> {
353 };
354 
TEST_P(LoadedArscParameterizedTest,LoadSparseEntryApp)355 TEST_P(LoadedArscParameterizedTest, LoadSparseEntryApp) {
356   std::string contents;
357   ASSERT_TRUE(ReadFileFromZipToString(GetParam(), "resources.arsc", &contents));
358 
359   std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(contents.data(),
360                                                                    contents.length());
361   ASSERT_THAT(loaded_arsc, NotNull());
362 
363   const LoadedPackage* package =
364       loaded_arsc->GetPackageById(get_package_id(sparse::R::integer::foo_9));
365   ASSERT_THAT(package, NotNull());
366 
367   const uint8_t type_index = get_type_id(sparse::R::integer::foo_9) - 1;
368   const uint16_t entry_index = get_entry_id(sparse::R::integer::foo_9);
369 
370   const TypeSpec* type_spec = package->GetTypeSpecByTypeIndex(type_index);
371   ASSERT_THAT(type_spec, NotNull());
372   ASSERT_THAT(type_spec->type_entries.size(), Ge(1u));
373 
374   auto type = type_spec->type_entries[0];
375   ASSERT_TRUE(LoadedPackage::GetEntry(type.type, entry_index).has_value());
376 }
377 
TEST_P(LoadedArscParameterizedTest,FindSparseEntryApp)378 TEST_P(LoadedArscParameterizedTest, FindSparseEntryApp) {
379   std::string contents;
380   ASSERT_TRUE(ReadFileFromZipToString(GetParam(), "resources.arsc", &contents));
381 
382   std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(contents.data(),
383                                                                    contents.length());
384   ASSERT_THAT(loaded_arsc, NotNull());
385 
386   const LoadedPackage* package =
387       loaded_arsc->GetPackageById(get_package_id(sparse::R::string::only_land));
388   ASSERT_THAT(package, NotNull());
389 
390   const uint8_t type_index = get_type_id(sparse::R::string::only_land) - 1;
391 
392   const TypeSpec* type_spec = package->GetTypeSpecByTypeIndex(type_index);
393   ASSERT_THAT(type_spec, NotNull());
394   ASSERT_THAT(type_spec->type_entries.size(), Ge(1u));
395 
396   // Type Entry with default orientation is not sparse encoded because the ratio of
397   // populated entries to total entries is above threshold.
398   // Only find out default locale because Soong build system will introduce pseudo
399   // locales for the apk generated at runtime.
400   auto type_entry_default = std::find_if(
401     type_spec->type_entries.begin(), type_spec->type_entries.end(),
402     [] (const TypeSpec::TypeEntry& x) { return x.config.orientation == 0 &&
403                                                x.config.locale == 0; });
404   ASSERT_NE(type_entry_default, type_spec->type_entries.end());
405   ASSERT_EQ(type_entry_default->type->flags & ResTable_type::FLAG_SPARSE, 0);
406 
407   // Type Entry with land orientation is sparse encoded as expected.
408   // Only find out default locale because Soong build system will introduce pseudo
409   // locales for the apk generated at runtime.
410   auto type_entry_land = std::find_if(
411     type_spec->type_entries.begin(), type_spec->type_entries.end(),
412     [](const TypeSpec::TypeEntry& x) { return x.config.orientation ==
413                                               ResTable_config::ORIENTATION_LAND &&
414                                               x.config.locale == 0; });
415   ASSERT_NE(type_entry_land, type_spec->type_entries.end());
416   ASSERT_NE(type_entry_land->type->flags & ResTable_type::FLAG_SPARSE, 0);
417 
418   // Test fetching a resource with only sparsely encoded configs by name.
419   auto id = package->FindEntryByName(u"string", u"only_land");
420   ASSERT_EQ(id.value(), fix_package_id(sparse::R::string::only_land, 0));
421 }
422 
423 INSTANTIATE_TEST_SUITE_P(
424         FrameWorkResourcesLoadedArscTests,
425         LoadedArscParameterizedTest,
426         ::testing::Values(
427           base::GetExecutableDirectory() + "/tests/data/sparse/sparse.apk",
428           base::GetExecutableDirectory() + "/FrameworkResourcesSparseTestApp.apk"
429         ));
430 
431 }  // namespace android
432