1 /*
2  * Copyright (C) 2015 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 "ResourceTable.h"
18 
19 #include <algorithm>
20 #include <ostream>
21 #include <string>
22 
23 #include "ResourceValues.h"
24 #include "androidfw/IDiagnostics.h"
25 #include "test/Test.h"
26 #include "util/Util.h"
27 
28 using ::android::ConfigDescription;
29 using ::android::StringPiece;
30 using ::testing::Eq;
31 using ::testing::NotNull;
32 using ::testing::StrEq;
33 
34 using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
35 
36 namespace aapt {
37 
TEST(ResourceTableTest,FailToAddResourceWithBadName)38 TEST(ResourceTableTest, FailToAddResourceWithBadName) {
39   ResourceTable table;
40 
41   EXPECT_FALSE(
42       table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:id/hey,there")).Build(),
43                         test::GetDiagnostics()));
44 
45   EXPECT_FALSE(
46       table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:id/hey:there")).Build(),
47                         test::GetDiagnostics()));
48 }
49 
TEST(ResourceTableTest,AddResourceWithWeirdNameWhenAddingMangledResources)50 TEST(ResourceTableTest, AddResourceWithWeirdNameWhenAddingMangledResources) {
51   ResourceTable table;
52 
53   EXPECT_TRUE(
54       table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:id/heythere       "))
55                             .SetAllowMangled(true)
56                             .Build(),
57                         test::GetDiagnostics()));
58 }
59 
TEST(ResourceTableTest,AddOneResource)60 TEST(ResourceTableTest, AddOneResource) {
61   ResourceTable table;
62 
63   EXPECT_TRUE(table.AddResource(
64       NewResourceBuilder(test::ParseNameOrDie("android:attr/id"))
65           .SetValue(test::ValueBuilder<Id>().SetSource("test/path/file.xml", 23u).Build())
66           .Build(),
67       test::GetDiagnostics()));
68 
69   EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/id"), NotNull());
70 }
71 
TEST(ResourceTableTest,AddMultipleResources)72 TEST(ResourceTableTest, AddMultipleResources) {
73   ResourceTable table;
74 
75   ConfigDescription language_config;
76   memcpy(language_config.language, "pl", sizeof(language_config.language));
77 
78   EXPECT_TRUE(table.AddResource(
79       NewResourceBuilder(test::ParseNameOrDie("android:attr/layout_width"))
80           .SetValue(test::ValueBuilder<Id>().SetSource("test/path/file.xml", 10u).Build())
81           .Build(),
82       test::GetDiagnostics()));
83 
84   EXPECT_TRUE(table.AddResource(
85       NewResourceBuilder(test::ParseNameOrDie("android:attr/id"))
86           .SetValue(test::ValueBuilder<Id>().SetSource("test/path/file.xml", 12u).Build())
87           .Build(),
88       test::GetDiagnostics()));
89 
90   EXPECT_TRUE(table.AddResource(
91       NewResourceBuilder(test::ParseNameOrDie("android:string/ok"))
92           .SetValue(test::ValueBuilder<Id>().SetSource("test/path/file.xml", 14u).Build())
93           .Build(),
94       test::GetDiagnostics()));
95 
96   EXPECT_TRUE(
97       table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:string/ok"))
98                             .SetValue(test::ValueBuilder<BinaryPrimitive>(android::Res_value{})
99                                           .SetSource("test/path/file.xml", 20u)
100                                           .Build(),
101                                       language_config)
102                             .Build(),
103                         test::GetDiagnostics()));
104 
105   EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/layout_width"), NotNull());
106   EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/id"), NotNull());
107   EXPECT_THAT(test::GetValue<Id>(&table, "android:string/ok"), NotNull());
108   EXPECT_THAT(test::GetValueForConfig<BinaryPrimitive>(&table, "android:string/ok", language_config), NotNull());
109 }
110 
TEST(ResourceTableTest,OverrideWeakResourceValue)111 TEST(ResourceTableTest, OverrideWeakResourceValue) {
112   ResourceTable table;
113   ASSERT_TRUE(table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:attr/foo"))
114                                     .SetValue(test::AttributeBuilder().SetWeak(true).Build())
115                                     .Build(),
116                                 test::GetDiagnostics()));
117 
118   Attribute* attr = test::GetValue<Attribute>(&table, "android:attr/foo");
119   ASSERT_THAT(attr, NotNull());
120   EXPECT_TRUE(attr->IsWeak());
121 
122   ASSERT_TRUE(table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:attr/foo"))
123                                     .SetValue(util::make_unique<Attribute>())
124                                     .Build(),
125                                 test::GetDiagnostics()));
126 
127   attr = test::GetValue<Attribute>(&table, "android:attr/foo");
128   ASSERT_THAT(attr, NotNull());
129   EXPECT_FALSE(attr->IsWeak());
130 }
131 
TEST(ResourceTableTest,AllowCompatibleDuplicateAttributes)132 TEST(ResourceTableTest, AllowCompatibleDuplicateAttributes) {
133   ResourceTable table;
134 
135   const ResourceName name = test::ParseNameOrDie("android:attr/foo");
136   Attribute attr_one(android::ResTable_map::TYPE_STRING);
137   attr_one.SetWeak(true);
138   Attribute attr_two(android::ResTable_map::TYPE_STRING | android::ResTable_map::TYPE_REFERENCE);
139   attr_two.SetWeak(true);
140 
141   ASSERT_TRUE(table.AddResource(
142       NewResourceBuilder(name).SetValue(util::make_unique<Attribute>(attr_one)).Build(),
143       test::GetDiagnostics()));
144   ASSERT_TRUE(table.AddResource(
145       NewResourceBuilder(name).SetValue(util::make_unique<Attribute>(attr_two)).Build(),
146       test::GetDiagnostics()));
147 }
148 
TEST(ResourceTableTest,ProductVaryingValues)149 TEST(ResourceTableTest, ProductVaryingValues) {
150   ResourceTable table;
151   ASSERT_TRUE(table.AddResource(
152       NewResourceBuilder(test::ParseNameOrDie("android:string/foo"))
153           .SetValue(util::make_unique<Id>(), test::ParseConfigOrDie("land"), "tablet")
154           .Build(),
155       test::GetDiagnostics()));
156 
157   ASSERT_TRUE(table.AddResource(
158       NewResourceBuilder(test::ParseNameOrDie("android:string/foo"))
159           .SetValue(util::make_unique<Id>(), test::ParseConfigOrDie("land"), "phone")
160           .Build(),
161       test::GetDiagnostics()));
162 
163   EXPECT_THAT(test::GetValueForConfigAndProduct<Id>(&table, "android:string/foo",test::ParseConfigOrDie("land"), "tablet"), NotNull());
164   EXPECT_THAT(test::GetValueForConfigAndProduct<Id>(&table, "android:string/foo",test::ParseConfigOrDie("land"), "phone"), NotNull());
165 
166   std::optional<ResourceTable::SearchResult> sr =
167       table.FindResource(test::ParseNameOrDie("android:string/foo"));
168   ASSERT_TRUE(sr);
169   std::vector<ResourceConfigValue*> values =
170       sr.value().entry->FindAllValues(test::ParseConfigOrDie("land"));
171   ASSERT_EQ(2u, values.size());
172   EXPECT_EQ(std::string("phone"), values[0]->product);
173   EXPECT_EQ(std::string("tablet"), values[1]->product);
174 }
175 
LevelToString(Visibility::Level level)176 static StringPiece LevelToString(Visibility::Level level) {
177   switch (level) {
178     case Visibility::Level::kPrivate:
179       return "private";
180     case Visibility::Level::kPublic:
181       return "private";
182     default:
183       return "undefined";
184   }
185 }
186 
VisibilityOfResource(const ResourceTable & table,const ResourceNameRef & name,Visibility::Level level,StringPiece comment)187 static ::testing::AssertionResult VisibilityOfResource(const ResourceTable& table,
188                                                        const ResourceNameRef& name,
189                                                        Visibility::Level level,
190                                                        StringPiece comment) {
191   std::optional<ResourceTable::SearchResult> result = table.FindResource(name);
192   if (!result) {
193     return ::testing::AssertionFailure() << "no resource '" << name << "' found in table";
194   }
195 
196   const Visibility& visibility = result.value().entry->visibility;
197   if (visibility.level != level) {
198     return ::testing::AssertionFailure() << "expected visibility " << LevelToString(level)
199                                          << " but got " << LevelToString(visibility.level);
200   }
201 
202   if (visibility.comment != comment) {
203     return ::testing::AssertionFailure() << "expected visibility comment '" << comment
204                                          << "' but got '" << visibility.comment << "'";
205   }
206   return ::testing::AssertionSuccess();
207 }
208 
TEST(ResourceTableTest,SetVisibility)209 TEST(ResourceTableTest, SetVisibility) {
210   using Level = Visibility::Level;
211 
212   ResourceTable table;
213   const ResourceName name = test::ParseNameOrDie("android:string/foo");
214 
215   Visibility visibility;
216   visibility.level = Visibility::Level::kPrivate;
217   visibility.comment = "private";
218   ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(),
219                                 test::GetDiagnostics()));
220   ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPrivate, "private"));
221 
222   visibility.level = Visibility::Level::kUndefined;
223   visibility.comment = "undefined";
224   ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(),
225                                 test::GetDiagnostics()));
226   ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPrivate, "private"));
227 
228   visibility.level = Visibility::Level::kPublic;
229   visibility.comment = "public";
230   ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(),
231                                 test::GetDiagnostics()));
232   ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPublic, "public"));
233 
234   visibility.level = Visibility::Level::kPrivate;
235   visibility.comment = "private";
236   ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(),
237                                 test::GetDiagnostics()));
238   ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPublic, "public"));
239 }
240 
TEST(ResourceTableTest,SetAllowNew)241 TEST(ResourceTableTest, SetAllowNew) {
242   ResourceTable table;
243   const ResourceName name = test::ParseNameOrDie("android:string/foo");
244 
245   AllowNew allow_new;
246   std::optional<ResourceTable::SearchResult> result;
247 
248   allow_new.comment = "first";
249   ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetAllowNew(allow_new).Build(),
250                                 test::GetDiagnostics()));
251   result = table.FindResource(name);
252   ASSERT_TRUE(result);
253   ASSERT_TRUE(result.value().entry->allow_new);
254   ASSERT_THAT(result.value().entry->allow_new.value().comment, StrEq("first"));
255 
256   allow_new.comment = "second";
257   ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetAllowNew(allow_new).Build(),
258                                 test::GetDiagnostics()));
259   result = table.FindResource(name);
260   ASSERT_TRUE(result);
261   ASSERT_TRUE(result.value().entry->allow_new);
262   ASSERT_THAT(result.value().entry->allow_new.value().comment, StrEq("second"));
263 }
264 
TEST(ResourceTableTest,SetOverlayable)265 TEST(ResourceTableTest, SetOverlayable) {
266   ResourceTable table;
267   auto overlayable = std::make_shared<Overlayable>(
268       "Name", "overlay://theme", android::Source("res/values/overlayable.xml", 40));
269   OverlayableItem overlayable_item(overlayable);
270   overlayable_item.policies |= PolicyFlags::PRODUCT_PARTITION;
271   overlayable_item.policies |= PolicyFlags::VENDOR_PARTITION;
272   overlayable_item.comment = "comment";
273   overlayable_item.source = android::Source("res/values/overlayable.xml", 42);
274 
275   const ResourceName name = test::ParseNameOrDie("android:string/foo");
276   ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item).Build(),
277                                 test::GetDiagnostics()));
278   std::optional<ResourceTable::SearchResult> search_result = table.FindResource(name);
279 
280   ASSERT_TRUE(search_result);
281   ASSERT_TRUE(search_result.value().entry->overlayable_item);
282 
283   OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value();
284   EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
285   EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
286   EXPECT_THAT(result_overlayable_item.overlayable->source.path, Eq("res/values/overlayable.xml"));
287   EXPECT_THAT(result_overlayable_item.overlayable->source.line, 40);
288   EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION
289                                                    | PolicyFlags::VENDOR_PARTITION));
290   ASSERT_THAT(result_overlayable_item.comment, StrEq("comment"));
291   EXPECT_THAT(result_overlayable_item.source.path, Eq("res/values/overlayable.xml"));
292   EXPECT_THAT(result_overlayable_item.source.line, 42);
293 }
294 
TEST(ResourceTableTest,SetMultipleOverlayableResources)295 TEST(ResourceTableTest, SetMultipleOverlayableResources) {
296   ResourceTable table;
297 
298   const ResourceName foo = test::ParseNameOrDie("android:string/foo");
299   auto group = std::make_shared<Overlayable>("Name", "overlay://theme");
300   OverlayableItem overlayable(group);
301   overlayable.policies = PolicyFlags::PRODUCT_PARTITION;
302   ASSERT_TRUE(table.AddResource(NewResourceBuilder(foo).SetOverlayable(overlayable).Build(),
303                                 test::GetDiagnostics()));
304 
305   const ResourceName bar = test::ParseNameOrDie("android:string/bar");
306   OverlayableItem overlayable2(group);
307   overlayable2.policies = PolicyFlags::PRODUCT_PARTITION;
308   ASSERT_TRUE(table.AddResource(NewResourceBuilder(bar).SetOverlayable(overlayable2).Build(),
309                                 test::GetDiagnostics()));
310 
311   const ResourceName baz = test::ParseNameOrDie("android:string/baz");
312   OverlayableItem overlayable3(group);
313   overlayable3.policies = PolicyFlags::VENDOR_PARTITION;
314   ASSERT_TRUE(table.AddResource(NewResourceBuilder(baz).SetOverlayable(overlayable3).Build(),
315                                 test::GetDiagnostics()));
316 }
317 
TEST(ResourceTableTest,SetOverlayableDifferentResourcesDifferentName)318 TEST(ResourceTableTest, SetOverlayableDifferentResourcesDifferentName) {
319   ResourceTable table;
320 
321   const ResourceName foo = test::ParseNameOrDie("android:string/foo");
322   OverlayableItem overlayable_item(std::make_shared<Overlayable>("Name", "overlay://theme"));
323   overlayable_item.policies = PolicyFlags::PRODUCT_PARTITION;
324   ASSERT_TRUE(table.AddResource(NewResourceBuilder(foo).SetOverlayable(overlayable_item).Build(),
325                                 test::GetDiagnostics()));
326 
327   const ResourceName bar = test::ParseNameOrDie("android:string/bar");
328   OverlayableItem overlayable_item2(std::make_shared<Overlayable>("Name2",  "overlay://theme"));
329   overlayable_item2.policies = PolicyFlags::PRODUCT_PARTITION;
330   ASSERT_TRUE(table.AddResource(NewResourceBuilder(bar).SetOverlayable(overlayable_item2).Build(),
331                                 test::GetDiagnostics()));
332 }
333 
TEST(ResourceTableTest,SetOverlayableSameResourcesFail)334 TEST(ResourceTableTest, SetOverlayableSameResourcesFail) {
335   ResourceTable table;
336   const ResourceName name = test::ParseNameOrDie("android:string/foo");
337 
338   auto overlayable = std::make_shared<Overlayable>("Name", "overlay://theme");
339   OverlayableItem overlayable_item(overlayable);
340   ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item).Build(),
341                                 test::GetDiagnostics()));
342 
343   OverlayableItem overlayable_item2(overlayable);
344   ASSERT_FALSE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item2).Build(),
345                                  test::GetDiagnostics()));
346 }
347 
TEST(ResourceTableTest,SetOverlayableSameResourcesDifferentNameFail)348 TEST(ResourceTableTest,  SetOverlayableSameResourcesDifferentNameFail) {
349   ResourceTable table;
350   const ResourceName name = test::ParseNameOrDie("android:string/foo");
351 
352   auto overlayable = std::make_shared<Overlayable>("Name", "overlay://theme");
353   OverlayableItem overlayable_item(overlayable);
354   ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item).Build(),
355                                 test::GetDiagnostics()));
356 
357   auto overlayable2 = std::make_shared<Overlayable>("Other", "overlay://theme");
358   OverlayableItem overlayable_item2(overlayable2);
359   ASSERT_FALSE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item2).Build(),
360                                  test::GetDiagnostics()));
361 }
362 
TEST(ResourceTableTest,ConflictingIds)363 TEST(ResourceTableTest, ConflictingIds) {
364   ResourceTable table;
365   const ResourceName name = test::ParseNameOrDie("android:string/foo");
366   ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetId(0x01010000).Build(),
367                                 test::GetDiagnostics()));
368   ASSERT_FALSE(table.AddResource(NewResourceBuilder(name).SetId(0x01010001).Build(),
369                                  test::GetDiagnostics()));
370 }
371 
TEST(ResourceTableTest,ConflictingIdsCreateEntry)372 TEST(ResourceTableTest, ConflictingIdsCreateEntry) {
373   ResourceTable table;
374   const ResourceName name = test::ParseNameOrDie("android:string/foo");
375   ASSERT_TRUE(table.AddResource(
376       NewResourceBuilder(name).SetId(0x01010000, OnIdConflict::CREATE_ENTRY).Build(),
377       test::GetDiagnostics()));
378   ASSERT_TRUE(table.AddResource(
379       NewResourceBuilder(name).SetId(0x01010001, OnIdConflict::CREATE_ENTRY).Build(),
380       test::GetDiagnostics()));
381 
382   // Non-ambiguous query
383   ASSERT_TRUE(table.AddResource(
384       NewResourceBuilder(name).SetId(0x01010001).SetValue(std::make_unique<Id>()).Build(),
385       test::GetDiagnostics()));
386 }
387 
388 }  // namespace aapt
389