• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 "optimize/Obfuscator.h"
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 
23 #include "ResourceTable.h"
24 #include "android-base/file.h"
25 #include "test/Test.h"
26 
27 using ::aapt::test::GetValue;
28 using ::testing::AnyOf;
29 using ::testing::Eq;
30 using ::testing::HasSubstr;
31 using ::testing::IsFalse;
32 using ::testing::IsTrue;
33 using ::testing::Not;
34 using ::testing::NotNull;
35 
GetExtension(android::StringPiece path)36 android::StringPiece GetExtension(android::StringPiece path) {
37   auto iter = std::find(path.begin(), path.end(), '.');
38   return android::StringPiece(iter, path.end() - iter);
39 }
40 
FillTable(aapt::test::ResourceTableBuilder & builder,int start,int end)41 void FillTable(aapt::test::ResourceTableBuilder& builder, int start, int end) {
42   for (int i = start; i < end; i++) {
43     builder.AddFileReference("android:drawable/xmlfile" + std::to_string(i),
44                              "res/drawable/xmlfile" + std::to_string(i) + ".xml");
45   }
46 }
47 
48 namespace aapt {
49 
TEST(ObfuscatorTest,FileRefPathsChangedInResourceTable)50 TEST(ObfuscatorTest, FileRefPathsChangedInResourceTable) {
51   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
52 
53   std::unique_ptr<ResourceTable> table =
54       test::ResourceTableBuilder()
55           .AddFileReference("android:drawable/xmlfile", "res/drawables/xmlfile.xml")
56           .AddFileReference("android:drawable/xmlfile2", "res/drawables/xmlfile2.xml")
57           .AddString("android:string/string", "res/should/still/be/the/same.png")
58           .Build();
59 
60   OptimizeOptions options{.shorten_resource_paths = true};
61   std::map<std::string, std::string>& path_map = options.table_flattener_options.shortened_path_map;
62   ASSERT_TRUE(Obfuscator(options).Consume(context.get(), table.get()));
63 
64   // Expect that the path map is populated
65   ASSERT_THAT(path_map.find("res/drawables/xmlfile.xml"), Not(Eq(path_map.end())));
66   ASSERT_THAT(path_map.find("res/drawables/xmlfile2.xml"), Not(Eq(path_map.end())));
67 
68   // The file paths were changed
69   EXPECT_THAT(path_map.at("res/drawables/xmlfile.xml"), Not(Eq("res/drawables/xmlfile.xml")));
70   EXPECT_THAT(path_map.at("res/drawables/xmlfile2.xml"), Not(Eq("res/drawables/xmlfile2.xml")));
71 
72   // Different file paths should remain different
73   EXPECT_THAT(path_map["res/drawables/xmlfile.xml"],
74               Not(Eq(path_map["res/drawables/xmlfile2.xml"])));
75 
76   FileReference* ref = GetValue<FileReference>(table.get(), "android:drawable/xmlfile");
77   ASSERT_THAT(ref, NotNull());
78   // The map correctly points to the new location of the file
79   EXPECT_THAT(path_map["res/drawables/xmlfile.xml"], Eq(*ref->path));
80 
81   // Strings should not be affected, only file paths
82   EXPECT_THAT(*GetValue<String>(table.get(), "android:string/string")->value,
83               Eq("res/should/still/be/the/same.png"));
84   EXPECT_THAT(path_map.find("res/should/still/be/the/same.png"), Eq(path_map.end()));
85 }
86 
TEST(ObfuscatorTest,SkipColorFileRefPaths)87 TEST(ObfuscatorTest, SkipColorFileRefPaths) {
88   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
89 
90   std::unique_ptr<ResourceTable> table =
91       test::ResourceTableBuilder()
92           .AddFileReference("android:color/colorlist", "res/color/colorlist.xml")
93           .AddFileReference("android:color/colorlist", "res/color-mdp-v21/colorlist.xml",
94                             test::ParseConfigOrDie("mdp-v21"))
95           .Build();
96 
97   OptimizeOptions options{.shorten_resource_paths = true};
98   std::map<std::string, std::string>& path_map = options.table_flattener_options.shortened_path_map;
99   ASSERT_TRUE(Obfuscator(options).Consume(context.get(), table.get()));
100 
101   // Expect that the path map to not contain the ColorStateList
102   ASSERT_THAT(path_map.find("res/color/colorlist.xml"), Eq(path_map.end()));
103   ASSERT_THAT(path_map.find("res/color-mdp-v21/colorlist.xml"), Eq(path_map.end()));
104 }
105 
TEST(ObfuscatorTest,SkipPathShortenExemptions)106 TEST(ObfuscatorTest, SkipPathShortenExemptions) {
107   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
108 
109   std::unique_ptr<ResourceTable> table =
110       test::ResourceTableBuilder()
111           .AddFileReference("android:drawable/xmlfile", "res/drawables/xmlfile.xml")
112           .AddFileReference("android:drawable/xmlfile2", "res/drawables/xmlfile2.xml")
113           .AddString("android:string/string", "res/should/still/be/the/same.png")
114           .Build();
115 
116   OptimizeOptions options{.shorten_resource_paths = true};
117   TableFlattenerOptions& flattenerOptions = options.table_flattener_options;
118   flattenerOptions.path_shorten_exemptions.insert(
119       ResourceName({}, ResourceType::kDrawable, "xmlfile"));
120   std::map<std::string, std::string>& path_map = options.table_flattener_options.shortened_path_map;
121   ASSERT_TRUE(Obfuscator(options).Consume(context.get(), table.get()));
122 
123   // Expect that the path map to not contain the first drawable which is in exemption set
124   EXPECT_THAT(path_map.find("res/drawables/xmlfile.xml"), Eq(path_map.end()));
125 
126   // Expect that the path map to contain the second drawable which is not in exemption set
127   EXPECT_THAT(path_map.find("res/drawables/xmlfile2.xml"), Not(Eq(path_map.end())));
128 
129   FileReference* ref = GetValue<FileReference>(table.get(), "android:drawable/xmlfile");
130   EXPECT_THAT(ref, NotNull());
131   ASSERT_THAT(HasFailure(), IsFalse());
132   // The path of first drawable in exemption was not changed
133   EXPECT_THAT("res/drawables/xmlfile.xml", Eq(*ref->path));
134 
135   // The file path of second drawable not in exemption set was changed
136   EXPECT_THAT(path_map.at("res/drawables/xmlfile2.xml"), Not(Eq("res/drawables/xmlfile2.xml")));
137 
138   FileReference* ref2 = GetValue<FileReference>(table.get(), "android:drawable/xmlfile2");
139   ASSERT_THAT(ref, NotNull());
140   // The map of second drawable not in exemption correctly points to the new location of the file
141   EXPECT_THAT(path_map["res/drawables/xmlfile2.xml"], Eq(*ref2->path));
142 }
143 
TEST(ObfuscatorTest,KeepExtensions)144 TEST(ObfuscatorTest, KeepExtensions) {
145   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
146 
147   std::string original_xml_path = "res/drawable/xmlfile.xml";
148   std::string original_png_path = "res/drawable/pngfile.png";
149 
150   std::unique_ptr<ResourceTable> table =
151       test::ResourceTableBuilder()
152           .AddFileReference("android:color/xmlfile", original_xml_path)
153           .AddFileReference("android:color/pngfile", original_png_path)
154           .Build();
155 
156   OptimizeOptions options{.shorten_resource_paths = true};
157   std::map<std::string, std::string>& path_map = options.table_flattener_options.shortened_path_map;
158   ASSERT_TRUE(Obfuscator(options).Consume(context.get(), table.get()));
159 
160   // Expect that the path map is populated
161   ASSERT_THAT(path_map.find("res/drawable/xmlfile.xml"), Not(Eq(path_map.end())));
162   ASSERT_THAT(path_map.find("res/drawable/pngfile.png"), Not(Eq(path_map.end())));
163 
164   auto shortend_xml_path = path_map[original_xml_path];
165   auto shortend_png_path = path_map[original_png_path];
166 
167   EXPECT_THAT(GetExtension(path_map[original_xml_path]), Eq(android::StringPiece(".xml")));
168   EXPECT_THAT(GetExtension(path_map[original_png_path]), Eq(android::StringPiece(".png")));
169 }
170 
TEST(ObfuscatorTest,DeterministicallyHandleCollisions)171 TEST(ObfuscatorTest, DeterministicallyHandleCollisions) {
172   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
173 
174   // 4000 resources is the limit at which the hash space is expanded to 3
175   // letters to reduce collisions, we want as many collisions as possible thus
176   // N-1.
177   const auto kNumResources = 3999;
178   const auto kNumTries = 5;
179 
180   test::ResourceTableBuilder builder1;
181   FillTable(builder1, 0, kNumResources);
182   std::unique_ptr<ResourceTable> table1 = builder1.Build();
183   OptimizeOptions options{.shorten_resource_paths = true};
184   std::map<std::string, std::string>& expected_mapping =
185       options.table_flattener_options.shortened_path_map;
186   ASSERT_TRUE(Obfuscator(options).Consume(context.get(), table1.get()));
187 
188   // We are trying to ensure lack of non-determinism, it is not simple to prove
189   // a negative, thus we must try the test a few times so that the test itself
190   // is non-flaky. Basically create the pathmap 5 times from the same set of
191   // resources but a different order of addition and then ensure they are always
192   // mapped to the same short path.
193   for (int i = 0; i < kNumTries; i++) {
194     test::ResourceTableBuilder builder2;
195     // This loop adds resources to the resource table in the range of
196     // [0:kNumResources).  Adding the file references in different order makes
197     // non-determinism more likely to surface. Thus we add resources
198     // [start_index:kNumResources) first then [0:start_index). We also use a
199     // different start_index each run.
200     int start_index = (kNumResources / kNumTries) * i;
201     FillTable(builder2, start_index, kNumResources);
202     FillTable(builder2, 0, start_index);
203     std::unique_ptr<ResourceTable> table2 = builder2.Build();
204 
205     OptimizeOptions actualOptimizerOptions{.shorten_resource_paths = true};
206     TableFlattenerOptions& actual_options = actualOptimizerOptions.table_flattener_options;
207     std::map<std::string, std::string>& actual_mapping = actual_options.shortened_path_map;
208     ASSERT_TRUE(Obfuscator(actualOptimizerOptions).Consume(context.get(), table2.get()));
209 
210     for (auto& item : actual_mapping) {
211       ASSERT_THAT(expected_mapping[item.first], Eq(item.second));
212     }
213   }
214 }
215 
TEST(ObfuscatorTest,DumpIdResourceMap)216 TEST(ObfuscatorTest, DumpIdResourceMap) {
217   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
218 
219   OverlayableItem overlayable_item(std::make_shared<Overlayable>("TestName", "overlay://theme"));
220   overlayable_item.policies |= PolicyFlags::PRODUCT_PARTITION;
221   overlayable_item.policies |= PolicyFlags::SYSTEM_PARTITION;
222   overlayable_item.policies |= PolicyFlags::VENDOR_PARTITION;
223 
224   std::string original_xml_path = "res/drawable/xmlfile.xml";
225   std::string original_png_path = "res/drawable/pngfile.png";
226 
227   std::string name = "com.app.test:string/overlayable";
228   std::unique_ptr<ResourceTable> table =
229       test::ResourceTableBuilder()
230           .AddFileReference("android:color/xmlfile", original_xml_path)
231           .AddFileReference("android:color/pngfile", original_png_path)
232           .AddValue("com.app.test:color/mycolor", aapt::ResourceId(0x7f020000),
233                     aapt::util::make_unique<aapt::BinaryPrimitive>(
234                         uint8_t(android::Res_value::TYPE_INT_COLOR_ARGB8), 0xffaabbcc))
235           .AddString("com.app.test:string/mystring", ResourceId(0x7f030000), "hi")
236           .AddString("com.app.test:string/in_exemption", ResourceId(0x7f030001), "Hi")
237           .AddString(name, ResourceId(0x7f030002), "HI")
238           .SetOverlayable(name, overlayable_item)
239           .Build();
240 
241   OptimizeOptions options{.shorten_resource_paths = true};
242   TableFlattenerOptions& flattenerOptions = options.table_flattener_options;
243   flattenerOptions.collapse_key_stringpool = true;
244   flattenerOptions.name_collapse_exemptions.insert(
245       ResourceName({}, ResourceType::kString, "in_exemption"));
246   auto& id_resource_map = flattenerOptions.id_resource_map;
247   ASSERT_TRUE(Obfuscator(options).Consume(context.get(), table.get()));
248 
249   // Expect that the id resource name map is populated
250   EXPECT_THAT(id_resource_map.at(0x7f020000), Eq("mycolor"));
251   EXPECT_THAT(id_resource_map.at(0x7f030000), Eq("mystring"));
252   EXPECT_THAT(id_resource_map.find(0x7f030001), Eq(id_resource_map.end()));
253   EXPECT_THAT(id_resource_map.find(0x7f030002), Eq(id_resource_map.end()));
254 }
255 
TEST(ObfuscatorTest,IsEnabledWithDefaultOption)256 TEST(ObfuscatorTest, IsEnabledWithDefaultOption) {
257   OptimizeOptions options;
258   Obfuscator obfuscatorWithDefaultOption(options);
259   ASSERT_THAT(obfuscatorWithDefaultOption.IsEnabled(), Eq(false));
260 }
261 
TEST(ObfuscatorTest,IsEnabledWithShortenPathOption)262 TEST(ObfuscatorTest, IsEnabledWithShortenPathOption) {
263   OptimizeOptions options{.shorten_resource_paths = true};
264   Obfuscator obfuscatorWithShortenPathOption(options);
265   ASSERT_THAT(obfuscatorWithShortenPathOption.IsEnabled(), Eq(true));
266 }
267 
TEST(ObfuscatorTest,IsEnabledWithCollapseStringPoolOption)268 TEST(ObfuscatorTest, IsEnabledWithCollapseStringPoolOption) {
269   OptimizeOptions options;
270   options.table_flattener_options.collapse_key_stringpool = true;
271   Obfuscator obfuscatorWithCollapseStringPoolOption(options);
272   ASSERT_THAT(obfuscatorWithCollapseStringPoolOption.IsEnabled(), Eq(true));
273 }
274 
TEST(ObfuscatorTest,IsEnabledWithShortenPathAndCollapseStringPoolOption)275 TEST(ObfuscatorTest, IsEnabledWithShortenPathAndCollapseStringPoolOption) {
276   OptimizeOptions options{.shorten_resource_paths = true};
277   options.table_flattener_options.collapse_key_stringpool = true;
278   Obfuscator obfuscatorWithCollapseStringPoolOption(options);
279   ASSERT_THAT(obfuscatorWithCollapseStringPoolOption.IsEnabled(), Eq(true));
280 }
281 
getProtocolBufferTableUnderTest()282 static std::unique_ptr<ResourceTable> getProtocolBufferTableUnderTest() {
283   std::string original_xml_path = "res/drawable/xmlfile.xml";
284   std::string original_png_path = "res/drawable/pngfile.png";
285 
286   return test::ResourceTableBuilder()
287       .AddFileReference("com.app.test:drawable/xmlfile", original_xml_path)
288       .AddFileReference("com.app.test:drawable/pngfile", original_png_path)
289       .AddValue("com.app.test:color/mycolor", aapt::ResourceId(0x7f020000),
290                 aapt::util::make_unique<aapt::BinaryPrimitive>(
291                     uint8_t(android::Res_value::TYPE_INT_COLOR_ARGB8), 0xffaabbcc))
292       .AddString("com.app.test:string/mystring", ResourceId(0x7f030000), "hello world")
293       .Build();
294 }
295 
TEST(ObfuscatorTest,WriteObfuscationMapInProtocolBufferFormat)296 TEST(ObfuscatorTest, WriteObfuscationMapInProtocolBufferFormat) {
297   OptimizeOptions options{.shorten_resource_paths = true};
298   options.table_flattener_options.collapse_key_stringpool = true;
299   Obfuscator obfuscator(options);
300   ASSERT_TRUE(obfuscator.Consume(test::ContextBuilder().Build().get(),
301                                  getProtocolBufferTableUnderTest().get()));
302 
303   obfuscator.WriteObfuscationMap("obfuscated_map.pb");
304 
305   std::string pbOut;
306   android::base::ReadFileToString("obfuscated_map.pb", &pbOut, false /* follow_symlinks */);
307   EXPECT_THAT(pbOut, HasSubstr("drawable/xmlfile.xml"));
308   EXPECT_THAT(pbOut, HasSubstr("drawable/pngfile.png"));
309   EXPECT_THAT(pbOut, HasSubstr("mycolor"));
310   EXPECT_THAT(pbOut, HasSubstr("mystring"));
311   pb::ResourceMappings resourceMappings;
312   EXPECT_THAT(resourceMappings.ParseFromString(pbOut), IsTrue());
313   EXPECT_THAT(resourceMappings.collapsed_names().resource_names_size(), Eq(2));
314   auto& resource_names = resourceMappings.collapsed_names().resource_names();
315   EXPECT_THAT(resource_names.at(0).name(), AnyOf(Eq("mycolor"), Eq("mystring")));
316   EXPECT_THAT(resource_names.at(1).name(), AnyOf(Eq("mycolor"), Eq("mystring")));
317   auto& shortened_paths = resourceMappings.shortened_paths();
318   EXPECT_THAT(shortened_paths.resource_paths_size(), Eq(2));
319   EXPECT_THAT(shortened_paths.resource_paths(0).original_path(),
320               AnyOf(Eq("res/drawable/pngfile.png"), Eq("res/drawable/xmlfile.xml")));
321   EXPECT_THAT(shortened_paths.resource_paths(1).original_path(),
322               AnyOf(Eq("res/drawable/pngfile.png"), Eq("res/drawable/xmlfile.xml")));
323 }
324 
TEST(ObfuscatorTest,WriteObfuscatingMapWithNonEnabledOption)325 TEST(ObfuscatorTest, WriteObfuscatingMapWithNonEnabledOption) {
326   OptimizeOptions options;
327   Obfuscator obfuscator(options);
328   ASSERT_TRUE(obfuscator.Consume(test::ContextBuilder().Build().get(),
329                                  getProtocolBufferTableUnderTest().get()));
330 
331   obfuscator.WriteObfuscationMap("obfuscated_map.pb");
332 
333   std::string pbOut;
334   android::base::ReadFileToString("obfuscated_map.pb", &pbOut, false /* follow_symlinks */);
335   ASSERT_THAT(pbOut, Eq(""));
336 }
337 
338 }  // namespace aapt
339