blob: 27cbe88fde38d94a9dd7c92645e3c19bb35fc5c5 [file] [log] [blame]
Ryan Mitchell479fa392019-01-02 17:15:39 -08001/*
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 "Link.h"
18
Ryan Mitchell5855de72021-02-24 14:39:13 -080019#include <android-base/file.h>
20
21#include "AppInfo.h"
Ryan Mitchell479fa392019-01-02 17:15:39 -080022#include "LoadedApk.h"
23#include "test/Test.h"
24
25using testing::Eq;
Ryan Mitchell5855de72021-02-24 14:39:13 -080026using testing::HasSubstr;
Ryan Mitchell479fa392019-01-02 17:15:39 -080027using testing::Ne;
28
29namespace aapt {
30
31using LinkTest = CommandTestFixture;
32
33TEST_F(LinkTest, RemoveRawXmlStrings) {
34 StdErrDiagnostics diag;
35 const std::string compiled_files_dir = GetTestPath("compiled");
36 ASSERT_TRUE(CompileFile(GetTestPath("res/xml/test.xml"), R"(<Item AgentCode="007"/>)",
37 compiled_files_dir, &diag));
38
39 const std::string out_apk = GetTestPath("out.apk");
40 std::vector<std::string> link_args = {
41 "--manifest", GetDefaultManifest(),
42 "-o", out_apk,
43 };
44
45 ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));
46
47 // Load the binary xml tree
48 android::ResXMLTree tree;
49 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
Ryan Mitchell1d008d12021-03-19 14:54:17 -070050 ASSERT_THAT(apk, Ne(nullptr));
51
Winsonb7be7932019-01-23 11:10:52 -080052 std::unique_ptr<io::IData> data = OpenFileAsData(apk.get(), "res/xml/test.xml");
53 ASSERT_THAT(data, Ne(nullptr));
Winsonb7be7932019-01-23 11:10:52 -080054 AssertLoadXml(apk.get(), data.get(), &tree);
Ryan Mitchell479fa392019-01-02 17:15:39 -080055
56 // Check that the raw string index has not been assigned
57 EXPECT_THAT(tree.getAttributeValueStringID(0), Eq(-1));
58}
59
60TEST_F(LinkTest, KeepRawXmlStrings) {
61 StdErrDiagnostics diag;
62 const std::string compiled_files_dir = GetTestPath("compiled");
63 ASSERT_TRUE(CompileFile(GetTestPath("res/xml/test.xml"), R"(<Item AgentCode="007"/>)",
64 compiled_files_dir, &diag));
65
66 const std::string out_apk = GetTestPath("out.apk");
67 std::vector<std::string> link_args = {
68 "--manifest", GetDefaultManifest(),
69 "-o", out_apk,
70 "--keep-raw-values"
71 };
72
73 ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));
74
75 // Load the binary xml tree
76 android::ResXMLTree tree;
77 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
Ryan Mitchell1d008d12021-03-19 14:54:17 -070078 ASSERT_THAT(apk, Ne(nullptr));
79
Winsonb7be7932019-01-23 11:10:52 -080080 std::unique_ptr<io::IData> data = OpenFileAsData(apk.get(), "res/xml/test.xml");
81 ASSERT_THAT(data, Ne(nullptr));
Winsonb7be7932019-01-23 11:10:52 -080082 AssertLoadXml(apk.get(), data.get(), &tree);
Ryan Mitchell479fa392019-01-02 17:15:39 -080083
84 // Check that the raw string index has been set to the correct string pool entry
85 int32_t raw_index = tree.getAttributeValueStringID(0);
86 ASSERT_THAT(raw_index, Ne(-1));
87 EXPECT_THAT(util::GetString(tree.getStrings(), static_cast<size_t>(raw_index)), Eq("007"));
88}
89
Ryan Mitchell81dfca02019-06-07 10:20:27 -070090TEST_F(LinkTest, NoCompressAssets) {
91 StdErrDiagnostics diag;
92 std::string content(500, 'a');
93 WriteFile(GetTestPath("assets/testtxt"), content);
94 WriteFile(GetTestPath("assets/testtxt2"), content);
95 WriteFile(GetTestPath("assets/test.txt"), content);
96 WriteFile(GetTestPath("assets/test.hello.txt"), content);
97 WriteFile(GetTestPath("assets/test.hello.xml"), content);
98
99 const std::string out_apk = GetTestPath("out.apk");
100 std::vector<std::string> link_args = {
101 "--manifest", GetDefaultManifest(),
102 "-o", out_apk,
103 "-0", ".txt",
104 "-0", "txt2",
105 "-0", ".hello.txt",
106 "-0", "hello.xml",
107 "-A", GetTestPath("assets")
108 };
109
110 ASSERT_TRUE(Link(link_args, &diag));
111
112 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
113 ASSERT_THAT(apk, Ne(nullptr));
114 io::IFileCollection* zip = apk->GetFileCollection();
115 ASSERT_THAT(zip, Ne(nullptr));
116
117 auto file = zip->FindFile("assets/testtxt");
118 ASSERT_THAT(file, Ne(nullptr));
119 EXPECT_TRUE(file->WasCompressed());
120
121 file = zip->FindFile("assets/testtxt2");
122 ASSERT_THAT(file, Ne(nullptr));
123 EXPECT_FALSE(file->WasCompressed());
124
125 file = zip->FindFile("assets/test.txt");
126 ASSERT_THAT(file, Ne(nullptr));
127 EXPECT_FALSE(file->WasCompressed());
128
129 file = zip->FindFile("assets/test.hello.txt");
130 ASSERT_THAT(file, Ne(nullptr));
131 EXPECT_FALSE(file->WasCompressed());
132
133 file = zip->FindFile("assets/test.hello.xml");
134 ASSERT_THAT(file, Ne(nullptr));
135 EXPECT_FALSE(file->WasCompressed());
136}
137
138TEST_F(LinkTest, NoCompressResources) {
139 StdErrDiagnostics diag;
140 std::string content(500, 'a');
141 const std::string compiled_files_dir = GetTestPath("compiled");
142 ASSERT_TRUE(CompileFile(GetTestPath("res/raw/testtxt"), content, compiled_files_dir, &diag));
143 ASSERT_TRUE(CompileFile(GetTestPath("res/raw/test.txt"), content, compiled_files_dir, &diag));
144 ASSERT_TRUE(CompileFile(GetTestPath("res/raw/test1.hello.txt"), content, compiled_files_dir,
145 &diag));
146 ASSERT_TRUE(CompileFile(GetTestPath("res/raw/test2.goodbye.xml"), content, compiled_files_dir,
147 &diag));
148
149 const std::string out_apk = GetTestPath("out.apk");
150 std::vector<std::string> link_args = {
151 "--manifest", GetDefaultManifest(),
152 "-o", out_apk,
153 "-0", ".txt",
154 "-0", ".hello.txt",
155 "-0", "goodbye.xml",
156 };
157
158 ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));
159
160 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
161 ASSERT_THAT(apk, Ne(nullptr));
162 io::IFileCollection* zip = apk->GetFileCollection();
163 ASSERT_THAT(zip, Ne(nullptr));
164
165 auto file = zip->FindFile("res/raw/testtxt");
166 ASSERT_THAT(file, Ne(nullptr));
167 EXPECT_TRUE(file->WasCompressed());
168
169 file = zip->FindFile("res/raw/test.txt");
170 ASSERT_THAT(file, Ne(nullptr));
171 EXPECT_FALSE(file->WasCompressed());
172
173 file = zip->FindFile("res/raw/test1.hello.hello.txt");
174 ASSERT_THAT(file, Ne(nullptr));
175 EXPECT_FALSE(file->WasCompressed());
176
177 file = zip->FindFile("res/raw/test2.goodbye.goodbye.xml");
178 ASSERT_THAT(file, Ne(nullptr));
179 EXPECT_FALSE(file->WasCompressed());
180}
181
Donald Chai121c6e82019-06-12 12:51:57 -0700182TEST_F(LinkTest, OverlayStyles) {
183 StdErrDiagnostics diag;
184 const std::string compiled_files_dir = GetTestPath("compiled");
185 const std::string override_files_dir = GetTestPath("compiled-override");
186 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"),
187 R"(<resources>
188 <style name="MyStyle">
189 <item name="android:textColor">#123</item>
190 </style>
191 </resources>)",
192 compiled_files_dir, &diag));
193 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values-override.xml"),
194 R"(<resources>
195 <style name="MyStyle">
196 <item name="android:background">#456</item>
197 </style>
198 </resources>)",
199 override_files_dir, &diag));
200
201
202 const std::string out_apk = GetTestPath("out.apk");
203 std::vector<std::string> link_args = {
204 "--manifest", GetDefaultManifest(kDefaultPackageName),
205 "-o", out_apk,
206 };
207 const auto override_files = file::FindFiles(override_files_dir, &diag);
208 for (const auto &override_file : override_files.value()) {
209 link_args.push_back("-R");
210 link_args.push_back(file::BuildPath({override_files_dir, override_file}));
211 }
212 ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));
213
214 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700215 ASSERT_THAT(apk, Ne(nullptr));
216
Donald Chai121c6e82019-06-12 12:51:57 -0700217 const Style* actual_style = test::GetValue<Style>(
218 apk->GetResourceTable(), std::string(kDefaultPackageName) + ":style/MyStyle");
219 ASSERT_NE(actual_style, nullptr);
220 ASSERT_EQ(actual_style->entries.size(), 2);
221 EXPECT_EQ(actual_style->entries[0].key.id, 0x01010098); // android:textColor
222 EXPECT_EQ(actual_style->entries[1].key.id, 0x010100d4); // android:background
223}
224
225TEST_F(LinkTest, OverrideStylesInsteadOfOverlaying) {
226 StdErrDiagnostics diag;
227 const std::string compiled_files_dir = GetTestPath("compiled");
228 const std::string override_files_dir = GetTestPath("compiled-override");
229 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"),
230 R"(<resources>
231 <style name="MyStyle">
232 <item name="android:textColor">#123</item>
233 </style>
234 </resources>)",
235 compiled_files_dir, &diag));
236 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values-override.xml"),
237 R"(<resources>
238 <style name="MyStyle">
239 <item name="android:background">#456</item>
240 </style>
241 </resources>)",
242 override_files_dir, &diag));
243
244
245 const std::string out_apk = GetTestPath("out.apk");
246 std::vector<std::string> link_args = {
247 "--manifest", GetDefaultManifest(kDefaultPackageName),
248 "--override-styles-instead-of-overlaying",
249 "-o", out_apk,
250 };
251 const auto override_files = file::FindFiles(override_files_dir, &diag);
252 for (const auto &override_file : override_files.value()) {
253 link_args.push_back("-R");
254 link_args.push_back(file::BuildPath({override_files_dir, override_file}));
255 }
256 ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));
257
258 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700259 ASSERT_THAT(apk, Ne(nullptr));
260
Donald Chai121c6e82019-06-12 12:51:57 -0700261 const Style* actual_style = test::GetValue<Style>(
262 apk->GetResourceTable(), std::string(kDefaultPackageName) + ":style/MyStyle");
263 ASSERT_NE(actual_style, nullptr);
264 ASSERT_EQ(actual_style->entries.size(), 1);
265 EXPECT_EQ(actual_style->entries[0].key.id, 0x010100d4); // android:background
266}
267
Udam Sainib228df32019-06-18 16:50:34 -0700268TEST_F(LinkTest, AppInfoWithUsesSplit) {
269 StdErrDiagnostics diag;
270 const std::string base_files_dir = GetTestPath("base");
271 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"),
272 R"(<resources>
273 <string name="bar">bar</string>
274 </resources>)",
275 base_files_dir, &diag));
276 const std::string base_apk = GetTestPath("base.apk");
277 std::vector<std::string> link_args = {
278 "--manifest", GetDefaultManifest("com.aapt2.app"),
279 "-o", base_apk,
280 };
281 ASSERT_TRUE(Link(link_args, base_files_dir, &diag));
282
283 const std::string feature_manifest = GetTestPath("feature_manifest.xml");
284 WriteFile(feature_manifest, android::base::StringPrintf(R"(
285 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
286 package="com.aapt2.app" split="feature1">
287 </manifest>)"));
288 const std::string feature_files_dir = GetTestPath("feature");
289 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"),
290 R"(<resources>
291 <string name="foo">foo</string>
292 </resources>)",
293 feature_files_dir, &diag));
294 const std::string feature_apk = GetTestPath("feature.apk");
295 const std::string feature_package_id = "0x80";
296 link_args = {
297 "--manifest", feature_manifest,
298 "-I", base_apk,
299 "--package-id", feature_package_id,
300 "-o", feature_apk,
301 };
302 ASSERT_TRUE(Link(link_args, feature_files_dir, &diag));
303
304 const std::string feature2_manifest = GetTestPath("feature2_manifest.xml");
305 WriteFile(feature2_manifest, android::base::StringPrintf(R"(
306 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
307 package="com.aapt2.app" split="feature2">
308 <uses-split android:name="feature1"/>
309 </manifest>)"));
310 const std::string feature2_files_dir = GetTestPath("feature2");
311 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"),
312 R"(<resources>
313 <string-array name="string_array">
314 <item>@string/bar</item>
315 <item>@string/foo</item>
316 </string-array>
317 </resources>)",
318 feature2_files_dir, &diag));
319 const std::string feature2_apk = GetTestPath("feature2.apk");
320 const std::string feature2_package_id = "0x81";
321 link_args = {
322 "--manifest", feature2_manifest,
323 "-I", base_apk,
324 "-I", feature_apk,
325 "--package-id", feature2_package_id,
326 "-o", feature2_apk,
327 };
328 ASSERT_TRUE(Link(link_args, feature2_files_dir, &diag));
329}
330
Ryan Mitchell5855de72021-02-24 14:39:13 -0800331TEST_F(LinkTest, SharedLibraryAttributeRJava) {
332 StdErrDiagnostics diag;
333 const std::string lib_values =
334 R"(<resources>
335 <attr name="foo"/>
336 <public type="attr" name="foo" id="0x00010001"/>
337 <declare-styleable name="LibraryStyleable">
338 <attr name="foo" />
339 </declare-styleable>
340 </resources>)";
341
342 const std::string client_values =
343 R"(<resources>
344 <attr name="bar" />
345 <declare-styleable name="ClientStyleable">
346 <attr name="com.example.lib:foo" />
347 <attr name="bar" />
348 </declare-styleable>
349 </resources>)";
350
351 // Build a library with a public attribute
352 const std::string lib_res = GetTestPath("library-res");
353 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"), lib_values, lib_res, &diag));
354
355 const std::string lib_apk = GetTestPath("library.apk");
356 const std::string lib_java = GetTestPath("library_java");
357 // clang-format off
358 auto lib_manifest = ManifestBuilder(this)
359 .SetPackageName("com.example.lib")
360 .Build();
361
362 auto lib_link_args = LinkCommandBuilder(this)
363 .SetManifestFile(lib_manifest)
364 .AddFlag("--shared-lib")
365 .AddParameter("--java", lib_java)
366 .AddCompiledResDir(lib_res, &diag)
367 .Build(lib_apk);
368 // clang-format on
369 ASSERT_TRUE(Link(lib_link_args, &diag));
370
371 const std::string lib_r_java = lib_java + "/com/example/lib/R.java";
372 std::string lib_r_contents;
373 ASSERT_TRUE(android::base::ReadFileToString(lib_r_java, &lib_r_contents));
374 EXPECT_THAT(lib_r_contents, HasSubstr(" public static int foo=0x00010001;"));
375 EXPECT_THAT(lib_r_contents, HasSubstr(" com.example.lib.R.attr.foo"));
376
377 // Build a client that uses the library attribute in a declare-styleable
378 const std::string client_res = GetTestPath("client-res");
379 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"), client_values, client_res, &diag));
380
381 const std::string client_apk = GetTestPath("client.apk");
382 const std::string client_java = GetTestPath("client_java");
383 // clang-format off
384 auto client_manifest = ManifestBuilder(this)
385 .SetPackageName("com.example.client")
386 .Build();
387
388 auto client_link_args = LinkCommandBuilder(this)
389 .SetManifestFile(client_manifest)
390 .AddParameter("--java", client_java)
391 .AddParameter("-I", lib_apk)
392 .AddCompiledResDir(client_res, &diag)
393 .Build(client_apk);
394 // clang-format on
395 ASSERT_TRUE(Link(client_link_args, &diag));
396
397 const std::string client_r_java = client_java + "/com/example/client/R.java";
398 std::string client_r_contents;
399 ASSERT_TRUE(android::base::ReadFileToString(client_r_java, &client_r_contents));
400 EXPECT_THAT(client_r_contents, HasSubstr(" com.example.lib.R.attr.foo, 0x7f010000"));
401}
402
Donald Chai121c6e82019-06-12 12:51:57 -0700403} // namespace aapt