blob: 0aab94d3299f62e75e0d3fa0ee5688d548974060 [file] [log] [blame]
yd6b83292018-04-11 09:54:56 -07001/*
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"
Ryan Mitchell0ce89732018-10-03 09:20:57 -070020#include "android-base/stringprintf.h"
21#include "android-base/utf8.h"
22
yd6b83292018-04-11 09:54:56 -070023#include "io/StringStream.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070024#include "io/ZipArchive.h"
yd6b83292018-04-11 09:54:56 -070025#include "java/AnnotationProcessor.h"
26#include "test/Test.h"
lukeedgar19b8ebf2020-06-23 10:43:42 +010027#include "format/proto/ProtoDeserialize.h"
yd6b83292018-04-11 09:54:56 -070028
29namespace aapt {
30
Mihai Nitad1a65212019-03-26 13:47:45 -070031using CompilerTest = CommandTestFixture;
32
Ryan Mitchell0ce89732018-10-03 09:20:57 -070033std::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
Ryan Mitchell833a1a62018-07-10 13:51:36 -070045int TestCompile(const std::string& path, const std::string& outDir, bool legacy,
Ryan Mitchell0ce89732018-10-03 09:20:57 -070046 StdErrDiagnostics& diag) {
yd6b83292018-04-11 09:54:56 -070047 std::vector<android::StringPiece> args;
48 args.push_back(path);
49 args.push_back("-o");
50 args.push_back(outDir);
yd6b83292018-04-11 09:54:56 -070051 if (legacy) {
52 args.push_back("--legacy");
53 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -070054 return CompileCommand(&diag).Execute(args, &std::cerr);
yd6b83292018-04-11 09:54:56 -070055}
56
Mihai Nitad1a65212019-03-26 13:47:45 -070057TEST_F(CompilerTest, MultiplePeriods) {
yd6b83292018-04-11 09:54:56 -070058 StdErrDiagnostics diag;
59 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Ryan Mitchell0ce89732018-10-03 09:20:57 -070060 const std::string kResDir = BuildPath({android::base::Dirname(android::base::GetExecutablePath()),
61 "integration-tests", "CompileTest", "res"});
yd6b83292018-04-11 09:54:56 -070062
63 // Resource files without periods in the file name should not throw errors
Ryan Mitchell0ce89732018-10-03 09:20:57 -070064 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());
yd6b83292018-04-11 09:54:56 -070067 ASSERT_EQ(TestCompile(path0, kResDir, /** legacy */ false, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070068 ASSERT_EQ(::android::base::utf8::unlink(path0_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070069 ASSERT_EQ(TestCompile(path0, kResDir, /** legacy */ true, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070070 ASSERT_EQ(::android::base::utf8::unlink(path0_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070071
Ryan Mitchell0ce89732018-10-03 09:20:57 -070072 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());
yd6b83292018-04-11 09:54:56 -070075 ASSERT_EQ(TestCompile(path1, kResDir, /** legacy */ false, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070076 ASSERT_EQ(::android::base::utf8::unlink(path1_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070077 ASSERT_EQ(TestCompile(path1, kResDir, /** legacy */ true, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070078 ASSERT_EQ(::android::base::utf8::unlink(path1_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070079
Ryan Mitchell0ce89732018-10-03 09:20:57 -070080 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());
yd6b83292018-04-11 09:54:56 -070083 ASSERT_EQ(TestCompile(path2, kResDir, /** legacy */ false, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070084 ASSERT_EQ(::android::base::utf8::unlink(path2_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070085 ASSERT_EQ(TestCompile(path2, kResDir, /** legacy */ true, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070086 ASSERT_EQ(::android::base::utf8::unlink(path2_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070087
88 // Resource files with periods in the file name should fail on non-legacy compilations
Ryan Mitchell0ce89732018-10-03 09:20:57 -070089 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());
yd6b83292018-04-11 09:54:56 -070092 ASSERT_NE(TestCompile(path3, kResDir, /** legacy */ false, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070093 ASSERT_NE(::android::base::utf8::unlink(path3_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070094 ASSERT_EQ(TestCompile(path3, kResDir, /** legacy */ true, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070095 ASSERT_EQ(::android::base::utf8::unlink(path3_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070096
Ryan Mitchell0ce89732018-10-03 09:20:57 -070097 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());
yd6b83292018-04-11 09:54:56 -0700100 ASSERT_NE(TestCompile(path4, kResDir, /** legacy */ false, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700101 ASSERT_NE(::android::base::utf8::unlink(path4_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -0700102 ASSERT_EQ(TestCompile(path4, kResDir, /** legacy */ true, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700103 ASSERT_EQ(::android::base::utf8::unlink(path4_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -0700104
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700105 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());
yd6b83292018-04-11 09:54:56 -0700108 ASSERT_NE(TestCompile(path5, kResDir, /** legacy */ false, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700109 ASSERT_NE(::android::base::utf8::unlink(path5_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -0700110 ASSERT_EQ(TestCompile(path5, kResDir, /** legacy */ true, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700111 ASSERT_EQ(::android::base::utf8::unlink(path5_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -0700112}
113
Mihai Nitad1a65212019-03-26 13:47:45 -0700114TEST_F(CompilerTest, DirInput) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700115 StdErrDiagnostics diag;
116 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700117 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());
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700123
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);
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700129 args.push_back("-v");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700130 ASSERT_EQ(CompileCommand(&diag).Execute(args, &std::cerr), 0);
131
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700132 {
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);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700142}
143
Mihai Nitad1a65212019-03-26 13:47:45 -0700144TEST_F(CompilerTest, ZipInput) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700145 StdErrDiagnostics diag;
146 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700147 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());
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700155
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
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700163 {
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);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700173}
174
Mihai Nitad1a65212019-03-26 13:47:45 -0700175/*
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.
188constexpr 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
195static 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
Ryan Mitchell81dfca02019-06-07 10:20:27 -0700204 ctf->WriteFile(source_file, sTranslatableXmlContent);
Mihai Nitad1a65212019-03-26 13:47:45 -0700205 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
221 ResourceTable* table = apk->GetResourceTable();
222 ASSERT_NE(table, nullptr);
223 table->string_pool.Sort();
224
225 const std::vector<std::unique_ptr<StringPool::Entry>>& pool_strings =
226 table->string_pool.strings();
227
228 // The actual / expected vectors have the same size
229 const size_t pool_size = pool_strings.size();
230 ASSERT_EQ(pool_size, expected.size());
231
232 for (size_t i = 0; i < pool_size; i++) {
233 std::string actual = pool_strings[i]->value;
234 ASSERT_EQ(actual, expected[i]);
235 }
236}
237
238TEST_F(CompilerTest, DoNotTranslateTest) {
239 // The first string (000) is translatable, the second is not
240 // ar-XB uses "\u200F\u202E...\u202C\u200F"
241 std::vector<std::string> expected_translatable = {
242 "000", "111", // default locale
243 "[000 one]", // en-XA
244 "\xE2\x80\x8F\xE2\x80\xAE" "000" "\xE2\x80\xAC\xE2\x80\x8F", // ar-XB
245 };
246 AssertTranslations(this, "foo", expected_translatable);
247 AssertTranslations(this, "foo_donottranslate", expected_translatable);
248
249 // No translatable strings because these are non-translatable files
250 std::vector<std::string> expected_not_translatable = {
251 "000", "111", // default locale
252 };
253 AssertTranslations(this, "donottranslate", expected_not_translatable);
254 AssertTranslations(this, "donottranslate_foo", expected_not_translatable);
255}
256
lukeedgar19b8ebf2020-06-23 10:43:42 +0100257TEST_F(CompilerTest, RelativePathTest) {
258 StdErrDiagnostics diag;
259 const std::string res_path = BuildPath(
260 {android::base::Dirname(android::base::GetExecutablePath()),
261 "integration-tests", "CompileTest", "res"});
262
263 const std::string path_values_colors = GetTestPath("values/colors.xml");
264 WriteFile(path_values_colors, "<resources>"
265 "<color name=\"color_one\">#008577</color>"
266 "</resources>");
267
268 const std::string path_layout_layout_one = GetTestPath("layout/layout_one.xml");
269 WriteFile(path_layout_layout_one, "<LinearLayout "
270 "xmlns:android=\"http://schemas.android.com/apk/res/android\">"
271 "<TextBox android:id=\"@+id/text_one\" android:background=\"@color/color_one\"/>"
272 "</LinearLayout>");
273
274 const std::string compiled_files_dir = BuildPath(
275 {android::base::Dirname(android::base::GetExecutablePath()),
276 "integration-tests", "CompileTest", "compiled"});
277 CHECK(file::mkdirs(compiled_files_dir.data()));
278
279 const std::string path_values_colors_out =
280 BuildPath({compiled_files_dir,"values_colors.arsc.flat"});
281 const std::string path_layout_layout_one_out =
282 BuildPath({compiled_files_dir, "layout_layout_one.flat"});
283 ::android::base::utf8::unlink(path_values_colors_out.c_str());
284 ::android::base::utf8::unlink(path_layout_layout_one_out.c_str());
285 const std::string apk_path = BuildPath(
286 {android::base::Dirname(android::base::GetExecutablePath()),
287 "integration-tests", "CompileTest", "out.apk"});
288
289 const std::string source_set_res = BuildPath({"main", "res"});
290 const std::string relative_path_values_colors =
291 BuildPath({source_set_res, "values", "colors.xml"});
292 const std::string relative_path_layout_layout_one =
293 BuildPath({source_set_res, "layout", "layout_one.xml"});
294
295 CompileCommand(&diag).Execute({
296 path_values_colors,
297 "-o",
298 compiled_files_dir,
299 "--source-path",
300 relative_path_values_colors},
301 &std::cerr);
302
303 CompileCommand(&diag).Execute({
304 path_layout_layout_one,
305 "-o",
306 compiled_files_dir,
307 "--source-path",
308 relative_path_layout_layout_one},
309 &std::cerr);
310
311 std::ifstream ifs_values(path_values_colors_out);
312 std::string content_values((std::istreambuf_iterator<char>(ifs_values)),
313 (std::istreambuf_iterator<char>()));
314 ASSERT_NE(content_values.find(relative_path_values_colors), -1);
315 ASSERT_EQ(content_values.find(path_values_colors), -1);
316
317 Link({"-o", apk_path, "--manifest", GetDefaultManifest(), "--proto-format"},
318 compiled_files_dir, &diag);
319
320 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(apk_path, &diag);
321 ResourceTable* resource_table = apk.get()->GetResourceTable();
322 const std::vector<std::unique_ptr<StringPool::Entry>>& pool_strings =
323 resource_table->string_pool.strings();
324
325 ASSERT_EQ(pool_strings.size(), 2);
326 ASSERT_EQ(pool_strings[0]->value, "res/layout/layout_one.xml");
327 ASSERT_EQ(pool_strings[1]->value, "res/layout-v1/layout_one.xml");
328
329 // Check resources.pb contains relative sources.
330 io::IFile* proto_file =
331 apk.get()->GetFileCollection()->FindFile("resources.pb");
332 std::unique_ptr<io::InputStream> proto_stream = proto_file->OpenInputStream();
333 io::ProtoInputStreamReader proto_reader(proto_stream.get());
334 pb::ResourceTable pb_table;
335 proto_reader.ReadMessage(&pb_table);
336
337 const std::string pool_strings_proto = pb_table.source_pool().data();
338
339 ASSERT_NE(pool_strings_proto.find(relative_path_values_colors), -1);
340 ASSERT_NE(pool_strings_proto.find(relative_path_layout_layout_one), -1);
341}
342
Mihai Nitad1a65212019-03-26 13:47:45 -0700343} // namespace aapt