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/AssetManager2.h"
18 #include "androidfw/AssetManager.h"
19 
20 #include "android-base/logging.h"
21 
22 #include "TestHelpers.h"
23 #include "androidfw/ResourceUtils.h"
24 #include "data/appaslib/R.h"
25 #include "data/basic/R.h"
26 #include "data/lib_one/R.h"
27 #include "data/lib_two/R.h"
28 #include "data/libclient/R.h"
29 #include "data/styles/R.h"
30 #include "data/system/R.h"
31 
32 namespace app = com::android::app;
33 namespace appaslib = com::android::appaslib::app;
34 namespace basic = com::android::basic;
35 namespace lib_one = com::android::lib_one;
36 namespace lib_two = com::android::lib_two;
37 namespace libclient = com::android::libclient;
38 
39 using ::testing::Eq;
40 using ::testing::NotNull;
41 using ::testing::StrEq;
42 
43 namespace android {
44 
45 class AssetManager2Test : public ::testing::Test {
46  public:
SetUp()47   void SetUp() override {
48     basic_assets_ = ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
49     ASSERT_NE(nullptr, basic_assets_);
50 
51     basic_de_fr_assets_ = ApkAssets::Load(GetTestDataPath() + "/basic/basic_de_fr.apk");
52     ASSERT_NE(nullptr, basic_de_fr_assets_);
53 
54     style_assets_ = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk");
55     ASSERT_NE(nullptr, style_assets_);
56 
57     lib_one_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_one/lib_one.apk");
58     ASSERT_NE(nullptr, lib_one_assets_);
59 
60     lib_two_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_two/lib_two.apk");
61     ASSERT_NE(nullptr, lib_two_assets_);
62 
63     libclient_assets_ = ApkAssets::Load(GetTestDataPath() + "/libclient/libclient.apk");
64     ASSERT_NE(nullptr, libclient_assets_);
65 
66     appaslib_assets_ = ApkAssets::LoadAsSharedLibrary(GetTestDataPath() + "/appaslib/appaslib.apk");
67     ASSERT_NE(nullptr, appaslib_assets_);
68 
69     system_assets_ = ApkAssets::Load(GetTestDataPath() + "/system/system.apk", true /*system*/);
70     ASSERT_NE(nullptr, system_assets_);
71 
72     app_assets_ = ApkAssets::Load(GetTestDataPath() + "/app/app.apk");
73     ASSERT_THAT(app_assets_, NotNull());
74 
75     overlayable_assets_ = ApkAssets::Load(GetTestDataPath() + "/overlayable/overlayable.apk");
76     ASSERT_THAT(overlayable_assets_, NotNull());
77   }
78 
79  protected:
80   std::unique_ptr<const ApkAssets> basic_assets_;
81   std::unique_ptr<const ApkAssets> basic_de_fr_assets_;
82   std::unique_ptr<const ApkAssets> style_assets_;
83   std::unique_ptr<const ApkAssets> lib_one_assets_;
84   std::unique_ptr<const ApkAssets> lib_two_assets_;
85   std::unique_ptr<const ApkAssets> libclient_assets_;
86   std::unique_ptr<const ApkAssets> appaslib_assets_;
87   std::unique_ptr<const ApkAssets> system_assets_;
88   std::unique_ptr<const ApkAssets> app_assets_;
89   std::unique_ptr<const ApkAssets> overlayable_assets_;
90 };
91 
TEST_F(AssetManager2Test,FindsResourceFromSingleApkAssets)92 TEST_F(AssetManager2Test, FindsResourceFromSingleApkAssets) {
93   ResTable_config desired_config;
94   memset(&desired_config, 0, sizeof(desired_config));
95   desired_config.language[0] = 'd';
96   desired_config.language[1] = 'e';
97 
98   AssetManager2 assetmanager;
99   assetmanager.SetConfiguration(desired_config);
100   assetmanager.SetApkAssets({basic_assets_.get()});
101 
102   Res_value value;
103   ResTable_config selected_config;
104   uint32_t flags;
105 
106   ApkAssetsCookie cookie =
107       assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
108                                0 /*density_override*/, &value, &selected_config, &flags);
109   ASSERT_NE(kInvalidCookie, cookie);
110 
111   // Came from our ApkAssets.
112   EXPECT_EQ(0, cookie);
113 
114   // It is the default config.
115   EXPECT_EQ(0, selected_config.language[0]);
116   EXPECT_EQ(0, selected_config.language[1]);
117 
118   // It is a string.
119   EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
120 }
121 
TEST_F(AssetManager2Test,FindsResourceFromMultipleApkAssets)122 TEST_F(AssetManager2Test, FindsResourceFromMultipleApkAssets) {
123   ResTable_config desired_config;
124   memset(&desired_config, 0, sizeof(desired_config));
125   desired_config.language[0] = 'd';
126   desired_config.language[1] = 'e';
127 
128   AssetManager2 assetmanager;
129   assetmanager.SetConfiguration(desired_config);
130   assetmanager.SetApkAssets({basic_assets_.get(), basic_de_fr_assets_.get()});
131 
132   Res_value value;
133   ResTable_config selected_config;
134   uint32_t flags;
135 
136   ApkAssetsCookie cookie =
137       assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
138                                0 /*density_override*/, &value, &selected_config, &flags);
139   ASSERT_NE(kInvalidCookie, cookie);
140 
141   // Came from our de_fr ApkAssets.
142   EXPECT_EQ(1, cookie);
143 
144   // The configuration is German.
145   EXPECT_EQ('d', selected_config.language[0]);
146   EXPECT_EQ('e', selected_config.language[1]);
147 
148   // It is a string.
149   EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
150 }
151 
TEST_F(AssetManager2Test,FindsResourceFromSharedLibrary)152 TEST_F(AssetManager2Test, FindsResourceFromSharedLibrary) {
153   AssetManager2 assetmanager;
154 
155   // libclient is built with lib_one and then lib_two in order.
156   // Reverse the order to test that proper package ID re-assignment is happening.
157   assetmanager.SetApkAssets(
158       {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
159 
160   Res_value value;
161   ResTable_config selected_config;
162   uint32_t flags;
163 
164   ApkAssetsCookie cookie =
165       assetmanager.GetResource(libclient::R::string::foo_one, false /*may_be_bag*/,
166                                0 /*density_override*/, &value, &selected_config, &flags);
167   ASSERT_NE(kInvalidCookie, cookie);
168 
169   // Reference comes from libclient.
170   EXPECT_EQ(2, cookie);
171   EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
172 
173   // Lookup the reference.
174   cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/,
175                                     &value, &selected_config, &flags);
176   ASSERT_NE(kInvalidCookie, cookie);
177   EXPECT_EQ(1, cookie);
178   EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
179   EXPECT_EQ(std::string("Foo from lib_one"),
180             GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data));
181 
182   cookie = assetmanager.GetResource(libclient::R::string::foo_two, false /*may_be_bag*/,
183                                     0 /*density_override*/, &value, &selected_config, &flags);
184   ASSERT_NE(kInvalidCookie, cookie);
185 
186   // Reference comes from libclient.
187   EXPECT_EQ(2, cookie);
188   EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
189 
190   // Lookup the reference.
191   cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/,
192                                     &value, &selected_config, &flags);
193   ASSERT_NE(kInvalidCookie, cookie);
194   EXPECT_EQ(0, cookie);
195   EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
196   EXPECT_EQ(std::string("Foo from lib_two"),
197             GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data));
198 }
199 
TEST_F(AssetManager2Test,FindsResourceFromAppLoadedAsSharedLibrary)200 TEST_F(AssetManager2Test, FindsResourceFromAppLoadedAsSharedLibrary) {
201   AssetManager2 assetmanager;
202   assetmanager.SetApkAssets({appaslib_assets_.get()});
203 
204   // The appaslib package will have been assigned the package ID 0x02.
205 
206   Res_value value;
207   ResTable_config selected_config;
208   uint32_t flags;
209   ApkAssetsCookie cookie = assetmanager.GetResource(
210       fix_package_id(appaslib::R::integer::number1, 0x02), false /*may_be_bag*/,
211       0u /*density_override*/, &value, &selected_config, &flags);
212   ASSERT_NE(kInvalidCookie, cookie);
213   EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
214   EXPECT_EQ(fix_package_id(appaslib::R::array::integerArray1, 0x02), value.data);
215 }
216 
TEST_F(AssetManager2Test,GetSharedLibraryResourceName)217 TEST_F(AssetManager2Test, GetSharedLibraryResourceName) {
218   AssetManager2 assetmanager;
219   assetmanager.SetApkAssets({lib_one_assets_.get()});
220 
221   AssetManager2::ResourceName name;
222   ASSERT_TRUE(assetmanager.GetResourceName(lib_one::R::string::foo, &name));
223   std::string formatted_name = ToFormattedResourceString(&name);
224   ASSERT_EQ(formatted_name, "com.android.lib_one:string/foo");
225 }
226 
TEST_F(AssetManager2Test,FindsBagResourceFromSingleApkAssets)227 TEST_F(AssetManager2Test, FindsBagResourceFromSingleApkAssets) {
228   AssetManager2 assetmanager;
229   assetmanager.SetApkAssets({basic_assets_.get()});
230 
231   const ResolvedBag* bag = assetmanager.GetBag(basic::R::array::integerArray1);
232   ASSERT_NE(nullptr, bag);
233   ASSERT_EQ(3u, bag->entry_count);
234 
235   EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[0].value.dataType);
236   EXPECT_EQ(1u, bag->entries[0].value.data);
237   EXPECT_EQ(0, bag->entries[0].cookie);
238 
239   EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[1].value.dataType);
240   EXPECT_EQ(2u, bag->entries[1].value.data);
241   EXPECT_EQ(0, bag->entries[1].cookie);
242 
243   EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[2].value.dataType);
244   EXPECT_EQ(3u, bag->entries[2].value.data);
245   EXPECT_EQ(0, bag->entries[2].cookie);
246 }
247 
TEST_F(AssetManager2Test,FindsBagResourceFromMultipleApkAssets)248 TEST_F(AssetManager2Test, FindsBagResourceFromMultipleApkAssets) {}
249 
TEST_F(AssetManager2Test,FindsBagResourceFromSharedLibrary)250 TEST_F(AssetManager2Test, FindsBagResourceFromSharedLibrary) {
251   AssetManager2 assetmanager;
252 
253   // libclient is built with lib_one and then lib_two in order.
254   // Reverse the order to test that proper package ID re-assignment is happening.
255   assetmanager.SetApkAssets(
256       {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
257 
258   const ResolvedBag* bag = assetmanager.GetBag(fix_package_id(lib_one::R::style::Theme, 0x03));
259   ASSERT_NE(nullptr, bag);
260   ASSERT_GE(bag->entry_count, 2u);
261 
262   // First two attributes come from lib_one.
263   EXPECT_EQ(1, bag->entries[0].cookie);
264   EXPECT_EQ(0x03, get_package_id(bag->entries[0].key));
265   EXPECT_EQ(1, bag->entries[1].cookie);
266   EXPECT_EQ(0x03, get_package_id(bag->entries[1].key));
267 }
268 
TEST_F(AssetManager2Test,FindsStyleResourceWithParentFromSharedLibrary)269 TEST_F(AssetManager2Test, FindsStyleResourceWithParentFromSharedLibrary) {
270   AssetManager2 assetmanager;
271 
272   // libclient is built with lib_one and then lib_two in order.
273   // Reverse the order to test that proper package ID re-assignment is happening.
274   assetmanager.SetApkAssets(
275       {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
276 
277   const ResolvedBag* bag = assetmanager.GetBag(libclient::R::style::Theme);
278   ASSERT_NE(nullptr, bag);
279   ASSERT_GE(bag->entry_count, 2u);
280 
281   // First two attributes come from lib_one.
282   EXPECT_EQ(1, bag->entries[0].cookie);
283   EXPECT_EQ(0x03, get_package_id(bag->entries[0].key));
284   EXPECT_EQ(1, bag->entries[1].cookie);
285   EXPECT_EQ(0x03, get_package_id(bag->entries[1].key));
286 }
287 
TEST_F(AssetManager2Test,MergesStylesWithParentFromSingleApkAssets)288 TEST_F(AssetManager2Test, MergesStylesWithParentFromSingleApkAssets) {
289   AssetManager2 assetmanager;
290   assetmanager.SetApkAssets({style_assets_.get()});
291 
292   const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleOne);
293   ASSERT_NE(nullptr, bag_one);
294   ASSERT_EQ(2u, bag_one->entry_count);
295 
296   EXPECT_EQ(app::R::attr::attr_one, bag_one->entries[0].key);
297   EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[0].value.dataType);
298   EXPECT_EQ(1u, bag_one->entries[0].value.data);
299   EXPECT_EQ(0, bag_one->entries[0].cookie);
300 
301   EXPECT_EQ(app::R::attr::attr_two, bag_one->entries[1].key);
302   EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[1].value.dataType);
303   EXPECT_EQ(2u, bag_one->entries[1].value.data);
304   EXPECT_EQ(0, bag_one->entries[1].cookie);
305 
306   const ResolvedBag* bag_two = assetmanager.GetBag(app::R::style::StyleTwo);
307   ASSERT_NE(nullptr, bag_two);
308   ASSERT_EQ(6u, bag_two->entry_count);
309 
310   // attr_one is inherited from StyleOne.
311   EXPECT_EQ(app::R::attr::attr_one, bag_two->entries[0].key);
312   EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[0].value.dataType);
313   EXPECT_EQ(1u, bag_two->entries[0].value.data);
314   EXPECT_EQ(0, bag_two->entries[0].cookie);
315   EXPECT_EQ(app::R::style::StyleOne, bag_two->entries[0].style);
316 
317   // attr_two should be overridden from StyleOne by StyleTwo.
318   EXPECT_EQ(app::R::attr::attr_two, bag_two->entries[1].key);
319   EXPECT_EQ(Res_value::TYPE_STRING, bag_two->entries[1].value.dataType);
320   EXPECT_EQ(0, bag_two->entries[1].cookie);
321   EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[1].style);
322   EXPECT_EQ(std::string("string"), GetStringFromPool(assetmanager.GetStringPoolForCookie(0),
323                                                      bag_two->entries[1].value.data));
324 
325   // The rest are new attributes.
326 
327   EXPECT_EQ(app::R::attr::attr_three, bag_two->entries[2].key);
328   EXPECT_EQ(Res_value::TYPE_ATTRIBUTE, bag_two->entries[2].value.dataType);
329   EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[2].value.data);
330   EXPECT_EQ(0, bag_two->entries[2].cookie);
331   EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[2].style);
332 
333   EXPECT_EQ(app::R::attr::attr_five, bag_two->entries[3].key);
334   EXPECT_EQ(Res_value::TYPE_REFERENCE, bag_two->entries[3].value.dataType);
335   EXPECT_EQ(app::R::string::string_one, bag_two->entries[3].value.data);
336   EXPECT_EQ(0, bag_two->entries[3].cookie);
337   EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[3].style);
338 
339   EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[4].key);
340   EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[4].value.dataType);
341   EXPECT_EQ(3u, bag_two->entries[4].value.data);
342   EXPECT_EQ(0, bag_two->entries[4].cookie);
343   EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[4].style);
344 
345   EXPECT_EQ(app::R::attr::attr_empty, bag_two->entries[5].key);
346   EXPECT_EQ(Res_value::TYPE_NULL, bag_two->entries[5].value.dataType);
347   EXPECT_EQ(Res_value::DATA_NULL_EMPTY, bag_two->entries[5].value.data);
348   EXPECT_EQ(0, bag_two->entries[5].cookie);
349   EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[5].style);
350 }
351 
TEST_F(AssetManager2Test,MergeStylesCircularDependency)352 TEST_F(AssetManager2Test, MergeStylesCircularDependency) {
353   AssetManager2 assetmanager;
354   assetmanager.SetApkAssets({style_assets_.get()});
355 
356   // GetBag should stop traversing the parents of styles when a circular
357   // dependency is detected
358   const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleFour);
359   ASSERT_NE(nullptr, bag_one);
360   ASSERT_EQ(3u, bag_one->entry_count);
361 }
362 
TEST_F(AssetManager2Test,ResolveReferenceToResource)363 TEST_F(AssetManager2Test, ResolveReferenceToResource) {
364   AssetManager2 assetmanager;
365   assetmanager.SetApkAssets({basic_assets_.get()});
366 
367   Res_value value;
368   ResTable_config selected_config;
369   uint32_t flags;
370   ApkAssetsCookie cookie =
371       assetmanager.GetResource(basic::R::integer::ref1, false /*may_be_bag*/,
372                                0u /*density_override*/, &value, &selected_config, &flags);
373   ASSERT_NE(kInvalidCookie, cookie);
374 
375   EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
376   EXPECT_EQ(basic::R::integer::ref2, value.data);
377 
378   uint32_t last_ref = 0u;
379   cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
380   ASSERT_NE(kInvalidCookie, cookie);
381   EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
382   EXPECT_EQ(12000u, value.data);
383   EXPECT_EQ(basic::R::integer::ref2, last_ref);
384 }
385 
TEST_F(AssetManager2Test,ResolveReferenceToBag)386 TEST_F(AssetManager2Test, ResolveReferenceToBag) {
387   AssetManager2 assetmanager;
388   assetmanager.SetApkAssets({basic_assets_.get()});
389 
390   Res_value value;
391   ResTable_config selected_config;
392   uint32_t flags;
393   ApkAssetsCookie cookie =
394       assetmanager.GetResource(basic::R::integer::number2, true /*may_be_bag*/,
395                                0u /*density_override*/, &value, &selected_config, &flags);
396   ASSERT_NE(kInvalidCookie, cookie);
397 
398   EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
399   EXPECT_EQ(basic::R::array::integerArray1, value.data);
400 
401   uint32_t last_ref = 0u;
402   cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
403   ASSERT_NE(kInvalidCookie, cookie);
404   EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
405   EXPECT_EQ(basic::R::array::integerArray1, value.data);
406   EXPECT_EQ(basic::R::array::integerArray1, last_ref);
407 }
408 
TEST_F(AssetManager2Test,ResolveDeepIdReference)409 TEST_F(AssetManager2Test, ResolveDeepIdReference) {
410   AssetManager2 assetmanager;
411   assetmanager.SetApkAssets({basic_assets_.get()});
412 
413   // Set up the resource ids
414   const uint32_t high_ref = assetmanager
415       .GetResourceId("@id/high_ref", "values", "com.android.basic");
416   ASSERT_NE(high_ref, 0u);
417   const uint32_t middle_ref = assetmanager
418       .GetResourceId("@id/middle_ref", "values", "com.android.basic");
419   ASSERT_NE(middle_ref, 0u);
420   const uint32_t low_ref = assetmanager
421       .GetResourceId("@id/low_ref", "values", "com.android.basic");
422   ASSERT_NE(low_ref, 0u);
423 
424   // Retrieve the most shallow resource
425   Res_value value;
426   ResTable_config config;
427   uint32_t flags;
428   ApkAssetsCookie cookie = assetmanager.GetResource(high_ref, false /*may_be_bag*/,
429                                                     0 /*density_override*/,
430                                                     &value, &config, &flags);
431   ASSERT_NE(kInvalidCookie, cookie);
432   EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
433   EXPECT_EQ(middle_ref, value.data);
434 
435   // Check that resolving the reference resolves to the deepest id
436   uint32_t last_ref = high_ref;
437   assetmanager.ResolveReference(cookie, &value, &config, &flags, &last_ref);
438   EXPECT_EQ(last_ref, low_ref);
439 }
440 
TEST_F(AssetManager2Test,KeepLastReferenceIdUnmodifiedIfNoReferenceIsResolved)441 TEST_F(AssetManager2Test, KeepLastReferenceIdUnmodifiedIfNoReferenceIsResolved) {
442   AssetManager2 assetmanager;
443   assetmanager.SetApkAssets({basic_assets_.get()});
444 
445   ResTable_config selected_config;
446   memset(&selected_config, 0, sizeof(selected_config));
447 
448   uint32_t flags = 0u;
449 
450   // Create some kind of Res_value that is NOT a reference.
451   Res_value value;
452   value.dataType = Res_value::TYPE_STRING;
453   value.data = 0;
454 
455   uint32_t last_ref = basic::R::string::test1;
456   EXPECT_EQ(1, assetmanager.ResolveReference(1, &value, &selected_config, &flags, &last_ref));
457   EXPECT_EQ(basic::R::string::test1, last_ref);
458 }
459 
IsConfigurationPresent(const std::set<ResTable_config> & configurations,const ResTable_config & configuration)460 static bool IsConfigurationPresent(const std::set<ResTable_config>& configurations,
461                                    const ResTable_config& configuration) {
462   return configurations.count(configuration) > 0;
463 }
464 
TEST_F(AssetManager2Test,GetResourceConfigurations)465 TEST_F(AssetManager2Test, GetResourceConfigurations) {
466   AssetManager2 assetmanager;
467   assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
468 
469   std::set<ResTable_config> configurations = assetmanager.GetResourceConfigurations();
470 
471   // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
472   // And one extra for the default configuration.
473   EXPECT_EQ(4u, configurations.size());
474 
475   ResTable_config expected_config;
476   memset(&expected_config, 0, sizeof(expected_config));
477   expected_config.language[0] = 's';
478   expected_config.language[1] = 'v';
479   EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
480 
481   expected_config.language[0] = 'd';
482   expected_config.language[1] = 'e';
483   EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
484 
485   expected_config.language[0] = 'f';
486   expected_config.language[1] = 'r';
487   EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
488 
489   // Take out the system assets.
490   configurations = assetmanager.GetResourceConfigurations(true /* exclude_system */);
491 
492   // We expect de and fr from basic_de_fr assets.
493   EXPECT_EQ(2u, configurations.size());
494 
495   expected_config.language[0] = 's';
496   expected_config.language[1] = 'v';
497   EXPECT_FALSE(IsConfigurationPresent(configurations, expected_config));
498 
499   expected_config.language[0] = 'd';
500   expected_config.language[1] = 'e';
501   EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
502 
503   expected_config.language[0] = 'f';
504   expected_config.language[1] = 'r';
505   EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
506 }
507 
TEST_F(AssetManager2Test,GetResourceLocales)508 TEST_F(AssetManager2Test, GetResourceLocales) {
509   AssetManager2 assetmanager;
510   assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
511 
512   std::set<std::string> locales = assetmanager.GetResourceLocales();
513 
514   // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
515   EXPECT_EQ(3u, locales.size());
516   EXPECT_GT(locales.count("sv"), 0u);
517   EXPECT_GT(locales.count("de"), 0u);
518   EXPECT_GT(locales.count("fr"), 0u);
519 
520   locales = assetmanager.GetResourceLocales(true /*exclude_system*/);
521   // We expect the de and fr locales from basic_de_fr assets.
522   EXPECT_EQ(2u, locales.size());
523   EXPECT_GT(locales.count("de"), 0u);
524   EXPECT_GT(locales.count("fr"), 0u);
525 }
526 
TEST_F(AssetManager2Test,GetResourceId)527 TEST_F(AssetManager2Test, GetResourceId) {
528   AssetManager2 assetmanager;
529   assetmanager.SetApkAssets({basic_assets_.get()});
530 
531   EXPECT_EQ(basic::R::layout::main,
532             assetmanager.GetResourceId("com.android.basic:layout/main", "", ""));
533   EXPECT_EQ(basic::R::layout::main,
534             assetmanager.GetResourceId("layout/main", "", "com.android.basic"));
535   EXPECT_EQ(basic::R::layout::main,
536             assetmanager.GetResourceId("main", "layout", "com.android.basic"));
537 }
538 
TEST_F(AssetManager2Test,OpensFileFromSingleApkAssets)539 TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {
540   AssetManager2 assetmanager;
541   assetmanager.SetApkAssets({system_assets_.get()});
542 
543   std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
544   ASSERT_THAT(asset, NotNull());
545 
546   const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
547   ASSERT_THAT(data, NotNull());
548   EXPECT_THAT(std::string(data, asset->getLength()), StrEq("file\n"));
549 }
550 
TEST_F(AssetManager2Test,OpensFileFromMultipleApkAssets)551 TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {
552   AssetManager2 assetmanager;
553   assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
554 
555   std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
556   ASSERT_THAT(asset, NotNull());
557 
558   const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
559   ASSERT_THAT(data, NotNull());
560   EXPECT_THAT(std::string(data, asset->getLength()), StrEq("app override file\n"));
561 }
562 
TEST_F(AssetManager2Test,OpenDir)563 TEST_F(AssetManager2Test, OpenDir) {
564   AssetManager2 assetmanager;
565   assetmanager.SetApkAssets({system_assets_.get()});
566 
567   std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
568   ASSERT_THAT(asset_dir, NotNull());
569   ASSERT_THAT(asset_dir->getFileCount(), Eq(2u));
570 
571   EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("file.txt")));
572   EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
573 
574   EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("subdir")));
575   EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeDirectory));
576 
577   asset_dir = assetmanager.OpenDir("subdir");
578   ASSERT_THAT(asset_dir, NotNull());
579   ASSERT_THAT(asset_dir->getFileCount(), Eq(1u));
580 
581   EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("subdir_file.txt")));
582   EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
583 }
584 
TEST_F(AssetManager2Test,OpenDirFromManyApks)585 TEST_F(AssetManager2Test, OpenDirFromManyApks) {
586   AssetManager2 assetmanager;
587   assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
588 
589   std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
590   ASSERT_THAT(asset_dir, NotNull());
591   ASSERT_THAT(asset_dir->getFileCount(), Eq(3u));
592 
593   EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("app_file.txt")));
594   EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
595 
596   EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("file.txt")));
597   EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeRegular));
598 
599   EXPECT_THAT(asset_dir->getFileName(2), Eq(String8("subdir")));
600   EXPECT_THAT(asset_dir->getFileType(2), Eq(FileType::kFileTypeDirectory));
601 }
602 
TEST_F(AssetManager2Test,GetLastPathWithoutEnablingReturnsEmpty)603 TEST_F(AssetManager2Test, GetLastPathWithoutEnablingReturnsEmpty) {
604   ResTable_config desired_config;
605 
606   AssetManager2 assetmanager;
607   assetmanager.SetConfiguration(desired_config);
608   assetmanager.SetApkAssets({basic_assets_.get()});
609   assetmanager.SetResourceResolutionLoggingEnabled(false);
610 
611   Res_value value;
612   ResTable_config selected_config;
613   uint32_t flags;
614 
615   ApkAssetsCookie cookie =
616       assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
617                                0 /*density_override*/, &value, &selected_config, &flags);
618   ASSERT_NE(kInvalidCookie, cookie);
619 
620   auto result = assetmanager.GetLastResourceResolution();
621   EXPECT_EQ("", result);
622 }
623 
TEST_F(AssetManager2Test,GetLastPathWithoutResolutionReturnsEmpty)624 TEST_F(AssetManager2Test, GetLastPathWithoutResolutionReturnsEmpty) {
625   ResTable_config desired_config;
626 
627   AssetManager2 assetmanager;
628   assetmanager.SetConfiguration(desired_config);
629   assetmanager.SetApkAssets({basic_assets_.get()});
630 
631   auto result = assetmanager.GetLastResourceResolution();
632   EXPECT_EQ("", result);
633 }
634 
TEST_F(AssetManager2Test,GetLastPathWithSingleApkAssets)635 TEST_F(AssetManager2Test, GetLastPathWithSingleApkAssets) {
636   ResTable_config desired_config;
637   memset(&desired_config, 0, sizeof(desired_config));
638   desired_config.language[0] = 'd';
639   desired_config.language[1] = 'e';
640 
641   AssetManager2 assetmanager;
642   assetmanager.SetResourceResolutionLoggingEnabled(true);
643   assetmanager.SetConfiguration(desired_config);
644   assetmanager.SetApkAssets({basic_assets_.get()});
645 
646   Res_value value;
647   ResTable_config selected_config;
648   uint32_t flags;
649 
650   ApkAssetsCookie cookie =
651       assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
652                                0 /*density_override*/, &value, &selected_config, &flags);
653   ASSERT_NE(kInvalidCookie, cookie);
654 
655   auto result = assetmanager.GetLastResourceResolution();
656   EXPECT_EQ("Resolution for 0x7f030000 com.android.basic:string/test1\n\tFor config -de\n\tFound initial: com.android.basic", result);
657 }
658 
TEST_F(AssetManager2Test,GetLastPathWithMultipleApkAssets)659 TEST_F(AssetManager2Test, GetLastPathWithMultipleApkAssets) {
660   ResTable_config desired_config;
661   memset(&desired_config, 0, sizeof(desired_config));
662   desired_config.language[0] = 'd';
663   desired_config.language[1] = 'e';
664 
665   AssetManager2 assetmanager;
666   assetmanager.SetResourceResolutionLoggingEnabled(true);
667   assetmanager.SetConfiguration(desired_config);
668   assetmanager.SetApkAssets({basic_assets_.get(), basic_de_fr_assets_.get()});
669 
670   Res_value value = Res_value();
671   ResTable_config selected_config;
672   uint32_t flags;
673 
674   ApkAssetsCookie cookie =
675       assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
676                                0 /*density_override*/, &value, &selected_config, &flags);
677   ASSERT_NE(kInvalidCookie, cookie);
678 
679   auto result = assetmanager.GetLastResourceResolution();
680   EXPECT_EQ("Resolution for 0x7f030000 com.android.basic:string/test1\n\tFor config -de\n\tFound initial: com.android.basic\n\tFound better: com.android.basic -de", result);
681 }
682 
TEST_F(AssetManager2Test,GetLastPathAfterDisablingReturnsEmpty)683 TEST_F(AssetManager2Test, GetLastPathAfterDisablingReturnsEmpty) {
684   ResTable_config desired_config;
685   memset(&desired_config, 0, sizeof(desired_config));
686 
687   AssetManager2 assetmanager;
688   assetmanager.SetResourceResolutionLoggingEnabled(true);
689   assetmanager.SetConfiguration(desired_config);
690   assetmanager.SetApkAssets({basic_assets_.get()});
691 
692   Res_value value = Res_value();
693   ResTable_config selected_config;
694   uint32_t flags;
695 
696   ApkAssetsCookie cookie =
697       assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
698                                0 /*density_override*/, &value, &selected_config, &flags);
699   ASSERT_NE(kInvalidCookie, cookie);
700 
701   auto resultEnabled = assetmanager.GetLastResourceResolution();
702   ASSERT_NE("", resultEnabled);
703 
704   assetmanager.SetResourceResolutionLoggingEnabled(false);
705 
706   auto resultDisabled = assetmanager.GetLastResourceResolution();
707   EXPECT_EQ("", resultDisabled);
708 }
709 
TEST_F(AssetManager2Test,GetOverlayableMap)710 TEST_F(AssetManager2Test, GetOverlayableMap) {
711   ResTable_config desired_config;
712   memset(&desired_config, 0, sizeof(desired_config));
713 
714   AssetManager2 assetmanager;
715   assetmanager.SetResourceResolutionLoggingEnabled(true);
716   assetmanager.SetConfiguration(desired_config);
717   assetmanager.SetApkAssets({overlayable_assets_.get()});
718 
719   const auto map = assetmanager.GetOverlayableMapForPackage(0x7f);
720   ASSERT_NE(nullptr, map);
721   ASSERT_EQ(2, map->size());
722   ASSERT_EQ(map->at("OverlayableResources1"), "overlay://theme");
723   ASSERT_EQ(map->at("OverlayableResources2"), "overlay://com.android.overlayable");
724 }
725 
726 }  // namespace android
727