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 "Compile.h"
18 
19 #include "android-base/file.h"
20 #include "android-base/stringprintf.h"
21 #include "android-base/utf8.h"
22 
23 #include "io/StringStream.h"
24 #include "io/ZipArchive.h"
25 #include "java/AnnotationProcessor.h"
26 #include "test/Test.h"
27 #include "format/proto/ProtoDeserialize.h"
28 
29 namespace aapt {
30 
31 using CompilerTest = CommandTestFixture;
32 
BuildPath(std::vector<std::string> args)33 std::string BuildPath(std::vector<std::string> args) {
34   std::string out;
35   if (args.empty()) {
36     return out;
37   }
38   out = args[0];
39   for (int i = 1; i < args.size(); i++) {
40     file::AppendPath(&out, args[i]);
41   }
42   return out;
43 }
44 
TestCompile(const std::string & path,const std::string & outDir,bool legacy,StdErrDiagnostics & diag)45 int TestCompile(const std::string& path, const std::string& outDir, bool legacy,
46                 StdErrDiagnostics& diag) {
47   std::vector<android::StringPiece> args;
48   args.push_back(path);
49   args.push_back("-o");
50   args.push_back(outDir);
51   if (legacy) {
52     args.push_back("--legacy");
53   }
54   return CompileCommand(&diag).Execute(args, &std::cerr);
55 }
56 
TEST_F(CompilerTest,MultiplePeriods)57 TEST_F(CompilerTest, MultiplePeriods) {
58   StdErrDiagnostics diag;
59   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
60   const std::string kResDir = BuildPath({android::base::Dirname(android::base::GetExecutablePath()),
61                                          "integration-tests", "CompileTest", "res"});
62 
63   // Resource files without periods in the file name should not throw errors
64   const std::string path0 = BuildPath({kResDir, "values", "values.xml"});
65   const std::string path0_out = BuildPath({kResDir, "values_values.arsc.flat"});
66   ::android::base::utf8::unlink(path0_out.c_str());
67   ASSERT_EQ(TestCompile(path0, kResDir, /** legacy */ false, diag), 0);
68   ASSERT_EQ(::android::base::utf8::unlink(path0_out.c_str()), 0);
69   ASSERT_EQ(TestCompile(path0, kResDir, /** legacy */ true, diag), 0);
70   ASSERT_EQ(::android::base::utf8::unlink(path0_out.c_str()), 0);
71 
72   const std::string path1 = BuildPath({kResDir, "drawable", "image.png"});
73   const std::string path1_out = BuildPath({kResDir, "drawable_image.png.flat"});
74   ::android::base::utf8::unlink(path1_out.c_str());
75   ASSERT_EQ(TestCompile(path1, kResDir, /** legacy */ false, diag), 0);
76   ASSERT_EQ(::android::base::utf8::unlink(path1_out.c_str()), 0);
77   ASSERT_EQ(TestCompile(path1, kResDir, /** legacy */ true, diag), 0);
78   ASSERT_EQ(::android::base::utf8::unlink(path1_out.c_str()), 0);
79 
80   const std::string path2 = BuildPath({kResDir, "drawable", "image.9.png"});
81   const std::string path2_out = BuildPath({kResDir, "drawable_image.9.png.flat"});
82   ::android::base::utf8::unlink(path2_out.c_str());
83   ASSERT_EQ(TestCompile(path2, kResDir, /** legacy */ false, diag), 0);
84   ASSERT_EQ(::android::base::utf8::unlink(path2_out.c_str()), 0);
85   ASSERT_EQ(TestCompile(path2, kResDir, /** legacy */ true, diag), 0);
86   ASSERT_EQ(::android::base::utf8::unlink(path2_out.c_str()), 0);
87 
88   // Resource files with periods in the file name should fail on non-legacy compilations
89   const std::string path3 = BuildPath({kResDir, "values", "values.all.xml"});
90   const std::string path3_out = BuildPath({kResDir, "values_values.all.arsc.flat"});
91   ::android::base::utf8::unlink(path3_out.c_str());
92   ASSERT_NE(TestCompile(path3, kResDir, /** legacy */ false, diag), 0);
93   ASSERT_NE(::android::base::utf8::unlink(path3_out.c_str()), 0);
94   ASSERT_EQ(TestCompile(path3, kResDir, /** legacy */ true, diag), 0);
95   ASSERT_EQ(::android::base::utf8::unlink(path3_out.c_str()), 0);
96 
97   const std::string path4 = BuildPath({kResDir, "drawable", "image.small.png"});
98   const std::string path4_out = BuildPath({kResDir, "drawable_image.small.png.flat"});
99   ::android::base::utf8::unlink(path4_out.c_str());
100   ASSERT_NE(TestCompile(path4, kResDir, /** legacy */ false, diag), 0);
101   ASSERT_NE(::android::base::utf8::unlink(path4_out.c_str()), 0);
102   ASSERT_EQ(TestCompile(path4, kResDir, /** legacy */ true, diag), 0);
103   ASSERT_EQ(::android::base::utf8::unlink(path4_out.c_str()), 0);
104 
105   const std::string path5 = BuildPath({kResDir, "drawable", "image.small.9.png"});
106   const std::string path5_out = BuildPath({kResDir, "drawable_image.small.9.png.flat"});
107   ::android::base::utf8::unlink(path5_out.c_str());
108   ASSERT_NE(TestCompile(path5, kResDir, /** legacy */ false, diag), 0);
109   ASSERT_NE(::android::base::utf8::unlink(path5_out.c_str()), 0);
110   ASSERT_EQ(TestCompile(path5, kResDir, /** legacy */ true, diag), 0);
111   ASSERT_EQ(::android::base::utf8::unlink(path5_out.c_str()), 0);
112 }
113 
TEST_F(CompilerTest,DirInput)114 TEST_F(CompilerTest, DirInput) {
115   StdErrDiagnostics diag;
116   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
117   const std::string kResDir = BuildPath({android::base::Dirname(android::base::GetExecutablePath()),
118                                          "integration-tests", "CompileTest", "DirInput", "res"});
119   const std::string kOutputFlata =
120       BuildPath({android::base::Dirname(android::base::GetExecutablePath()), "integration-tests",
121                  "CompileTest", "DirInput", "compiled.flata"});
122   ::android::base::utf8::unlink(kOutputFlata.c_str());
123 
124   std::vector<android::StringPiece> args;
125   args.push_back("--dir");
126   args.push_back(kResDir);
127   args.push_back("-o");
128   args.push_back(kOutputFlata);
129   args.push_back("-v");
130   ASSERT_EQ(CompileCommand(&diag).Execute(args, &std::cerr), 0);
131 
132   {
133     // Check for the presence of the compiled files
134     std::string err;
135     std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(kOutputFlata, &err);
136     ASSERT_NE(zip, nullptr) << err;
137     ASSERT_NE(zip->FindFile("drawable_image.png.flat"), nullptr);
138     ASSERT_NE(zip->FindFile("layout_layout.xml.flat"), nullptr);
139     ASSERT_NE(zip->FindFile("values_values.arsc.flat"), nullptr);
140   }
141   ASSERT_EQ(::android::base::utf8::unlink(kOutputFlata.c_str()), 0);
142 }
143 
TEST_F(CompilerTest,ZipInput)144 TEST_F(CompilerTest, ZipInput) {
145   StdErrDiagnostics diag;
146   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
147   const std::string kResZip =
148       BuildPath({android::base::Dirname(android::base::GetExecutablePath()), "integration-tests",
149                  "CompileTest", "ZipInput", "res.zip"});
150   const std::string kOutputFlata =
151       BuildPath({android::base::Dirname(android::base::GetExecutablePath()), "integration-tests",
152                  "CompileTest", "ZipInput", "compiled.flata"});
153 
154   ::android::base::utf8::unlink(kOutputFlata.c_str());
155 
156   std::vector<android::StringPiece> args;
157   args.push_back("--zip");
158   args.push_back(kResZip);
159   args.push_back("-o");
160   args.push_back(kOutputFlata);
161   ASSERT_EQ(CompileCommand(&diag).Execute(args, &std::cerr), 0);
162 
163   {
164     // Check for the presence of the compiled files
165     std::string err;
166     std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(kOutputFlata, &err);
167     ASSERT_NE(zip, nullptr) << err;
168     ASSERT_NE(zip->FindFile("drawable_image.png.flat"), nullptr);
169     ASSERT_NE(zip->FindFile("layout_layout.xml.flat"), nullptr);
170     ASSERT_NE(zip->FindFile("values_values.arsc.flat"), nullptr);
171   }
172   ASSERT_EQ(::android::base::utf8::unlink(kOutputFlata.c_str()), 0);
173 }
174 
175 /*
176  * This tests the "protection" from pseudo-translation of
177  * non-translatable files (starting with 'donotranslate')
178  * and strings (with the translatable="false" attribute)
179  *
180  * We check 4 string files, 2 translatable, and 2 not (based on file name)
181  * Each file contains 2 strings, one translatable, one not (attribute based)
182  * Each of these files are compiled and linked into one .apk, then we load the
183  * strings from the apk and check if there are pseudo-translated strings.
184  */
185 
186 // Using 000 and 111 because they are not changed by pseudo-translation,
187 // making our life easier.
188 constexpr static const char sTranslatableXmlContent[] =
189     "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
190     "<resources>"
191     "  <string name=\"normal\">000</string>"
192     "  <string name=\"non_translatable\" translatable=\"false\">111</string>"
193     "</resources>";
194 
AssertTranslations(CommandTestFixture * ctf,std::string file_name,std::vector<std::string> expected)195 static void AssertTranslations(CommandTestFixture *ctf, std::string file_name,
196     std::vector<std::string> expected) {
197 
198   StdErrDiagnostics diag;
199 
200   const std::string source_file = ctf->GetTestPath("/res/values/" + file_name + ".xml");
201   const std::string compiled_files_dir = ctf->GetTestPath("/compiled_" + file_name);
202   const std::string out_apk = ctf->GetTestPath("/" + file_name + ".apk");
203 
204   ctf->WriteFile(source_file, sTranslatableXmlContent);
205   CHECK(file::mkdirs(compiled_files_dir.data()));
206 
207   ASSERT_EQ(CompileCommand(&diag).Execute({
208       source_file,
209       "-o", compiled_files_dir,
210       "-v",
211       "--pseudo-localize"
212   }, &std::cerr), 0);
213 
214   ASSERT_TRUE(ctf->Link({
215       "--manifest", ctf->GetDefaultManifest(),
216       "-o", out_apk
217   }, compiled_files_dir, &diag));
218 
219   std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
220   ASSERT_NE(apk, nullptr);
221 
222   ResourceTable* table = apk->GetResourceTable();
223   ASSERT_NE(table, nullptr);
224   table->string_pool.Sort();
225 
226   const std::vector<std::unique_ptr<StringPool::Entry>>& pool_strings =
227       table->string_pool.strings();
228 
229   // The actual / expected vectors have the same size
230   const size_t pool_size = pool_strings.size();
231   ASSERT_EQ(pool_size, expected.size());
232 
233   for (size_t i = 0; i < pool_size; i++) {
234     std::string actual = pool_strings[i]->value;
235     ASSERT_EQ(actual, expected[i]);
236   }
237 }
238 
TEST_F(CompilerTest,DoNotTranslateTest)239 TEST_F(CompilerTest, DoNotTranslateTest) {
240   // The first string (000) is translatable, the second is not
241   // ar-XB uses "\u200F\u202E...\u202C\u200F"
242   std::vector<std::string> expected_translatable = {
243       "000", "111", // default locale
244       "[000 one]", // en-XA
245       "\xE2\x80\x8F\xE2\x80\xAE" "000" "\xE2\x80\xAC\xE2\x80\x8F", // ar-XB
246   };
247   AssertTranslations(this, "foo", expected_translatable);
248   AssertTranslations(this, "foo_donottranslate", expected_translatable);
249 
250   // No translatable strings because these are non-translatable files
251   std::vector<std::string> expected_not_translatable = {
252       "000", "111", // default locale
253   };
254   AssertTranslations(this, "donottranslate", expected_not_translatable);
255   AssertTranslations(this, "donottranslate_foo", expected_not_translatable);
256 }
257 
TEST_F(CompilerTest,RelativePathTest)258 TEST_F(CompilerTest, RelativePathTest) {
259   StdErrDiagnostics diag;
260   const std::string res_path = BuildPath(
261       {android::base::Dirname(android::base::GetExecutablePath()),
262        "integration-tests", "CompileTest", "res"});
263 
264   const std::string path_values_colors = GetTestPath("values/colors.xml");
265   WriteFile(path_values_colors, "<resources>"
266                    "<color name=\"color_one\">#008577</color>"
267                    "</resources>");
268 
269   const std::string path_layout_layout_one = GetTestPath("layout/layout_one.xml");
270   WriteFile(path_layout_layout_one, "<LinearLayout "
271                    "xmlns:android=\"http://schemas.android.com/apk/res/android\">"
272                    "<TextBox android:id=\"@+id/text_one\" android:background=\"@color/color_one\"/>"
273                    "</LinearLayout>");
274 
275   const std::string compiled_files_dir  = BuildPath(
276       {android::base::Dirname(android::base::GetExecutablePath()),
277        "integration-tests", "CompileTest", "compiled"});
278   CHECK(file::mkdirs(compiled_files_dir.data()));
279 
280   const std::string path_values_colors_out =
281       BuildPath({compiled_files_dir,"values_colors.arsc.flat"});
282   const std::string path_layout_layout_one_out =
283       BuildPath({compiled_files_dir, "layout_layout_one.flat"});
284   ::android::base::utf8::unlink(path_values_colors_out.c_str());
285   ::android::base::utf8::unlink(path_layout_layout_one_out.c_str());
286   const std::string apk_path = BuildPath(
287       {android::base::Dirname(android::base::GetExecutablePath()),
288        "integration-tests", "CompileTest", "out.apk"});
289 
290   const std::string source_set_res = BuildPath({"main", "res"});
291   const std::string relative_path_values_colors =
292       BuildPath({source_set_res, "values", "colors.xml"});
293   const std::string relative_path_layout_layout_one =
294       BuildPath({source_set_res, "layout", "layout_one.xml"});
295 
296   CompileCommand(&diag).Execute({
297     path_values_colors,
298     "-o",
299     compiled_files_dir,
300     "--source-path",
301     relative_path_values_colors},
302         &std::cerr);
303 
304   CompileCommand(&diag).Execute({
305     path_layout_layout_one,
306     "-o",
307     compiled_files_dir,
308     "--source-path",
309     relative_path_layout_layout_one},
310         &std::cerr);
311 
312   std::ifstream ifs_values(path_values_colors_out);
313   std::string content_values((std::istreambuf_iterator<char>(ifs_values)),
314                              (std::istreambuf_iterator<char>()));
315   ASSERT_NE(content_values.find(relative_path_values_colors), -1);
316   ASSERT_EQ(content_values.find(path_values_colors), -1);
317 
318   ASSERT_TRUE(Link({"-o", apk_path,
319                     "--manifest", GetDefaultManifest(),
320                     "--proto-format"},
321                     compiled_files_dir, &diag));
322 
323   std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(apk_path, &diag);
324   ResourceTable* resource_table = apk.get()->GetResourceTable();
325   const std::vector<std::unique_ptr<StringPool::Entry>>& pool_strings =
326       resource_table->string_pool.strings();
327 
328   ASSERT_EQ(pool_strings.size(), 2);
329   ASSERT_EQ(pool_strings[0]->value, "res/layout/layout_one.xml");
330   ASSERT_EQ(pool_strings[1]->value, "res/layout-v1/layout_one.xml");
331 
332   // Check resources.pb contains relative sources.
333   io::IFile* proto_file =
334       apk.get()->GetFileCollection()->FindFile("resources.pb");
335   std::unique_ptr<io::InputStream> proto_stream = proto_file->OpenInputStream();
336   io::ProtoInputStreamReader proto_reader(proto_stream.get());
337   pb::ResourceTable pb_table;
338   proto_reader.ReadMessage(&pb_table);
339 
340   const std::string pool_strings_proto = pb_table.source_pool().data();
341 
342   ASSERT_NE(pool_strings_proto.find(relative_path_values_colors), -1);
343   ASSERT_NE(pool_strings_proto.find(relative_path_layout_layout_one), -1);
344 }
345 
346 }  // namespace aapt
347