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 "AppInfo.h"
18 #include "Link.h"
19
20 #include "LoadedApk.h"
21 #include "test/Test.h"
22
23 using testing::Eq;
24 using testing::Ne;
25
26 namespace aapt {
27
28 using LinkTest = CommandTestFixture;
29
TEST_F(LinkTest,RemoveRawXmlStrings)30 TEST_F(LinkTest, RemoveRawXmlStrings) {
31 StdErrDiagnostics diag;
32 const std::string compiled_files_dir = GetTestPath("compiled");
33 ASSERT_TRUE(CompileFile(GetTestPath("res/xml/test.xml"), R"(<Item AgentCode="007"/>)",
34 compiled_files_dir, &diag));
35
36 const std::string out_apk = GetTestPath("out.apk");
37 std::vector<std::string> link_args = {
38 "--manifest", GetDefaultManifest(),
39 "-o", out_apk,
40 };
41
42 ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));
43
44 // Load the binary xml tree
45 android::ResXMLTree tree;
46 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
47 std::unique_ptr<io::IData> data = OpenFileAsData(apk.get(), "res/xml/test.xml");
48 ASSERT_THAT(data, Ne(nullptr));
49 AssertLoadXml(apk.get(), data.get(), &tree);
50
51 // Check that the raw string index has not been assigned
52 EXPECT_THAT(tree.getAttributeValueStringID(0), Eq(-1));
53 }
54
TEST_F(LinkTest,KeepRawXmlStrings)55 TEST_F(LinkTest, KeepRawXmlStrings) {
56 StdErrDiagnostics diag;
57 const std::string compiled_files_dir = GetTestPath("compiled");
58 ASSERT_TRUE(CompileFile(GetTestPath("res/xml/test.xml"), R"(<Item AgentCode="007"/>)",
59 compiled_files_dir, &diag));
60
61 const std::string out_apk = GetTestPath("out.apk");
62 std::vector<std::string> link_args = {
63 "--manifest", GetDefaultManifest(),
64 "-o", out_apk,
65 "--keep-raw-values"
66 };
67
68 ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));
69
70 // Load the binary xml tree
71 android::ResXMLTree tree;
72 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
73 std::unique_ptr<io::IData> data = OpenFileAsData(apk.get(), "res/xml/test.xml");
74 ASSERT_THAT(data, Ne(nullptr));
75 AssertLoadXml(apk.get(), data.get(), &tree);
76
77 // Check that the raw string index has been set to the correct string pool entry
78 int32_t raw_index = tree.getAttributeValueStringID(0);
79 ASSERT_THAT(raw_index, Ne(-1));
80 EXPECT_THAT(util::GetString(tree.getStrings(), static_cast<size_t>(raw_index)), Eq("007"));
81 }
82
TEST_F(LinkTest,NoCompressAssets)83 TEST_F(LinkTest, NoCompressAssets) {
84 StdErrDiagnostics diag;
85 std::string content(500, 'a');
86 WriteFile(GetTestPath("assets/testtxt"), content);
87 WriteFile(GetTestPath("assets/testtxt2"), content);
88 WriteFile(GetTestPath("assets/test.txt"), content);
89 WriteFile(GetTestPath("assets/test.hello.txt"), content);
90 WriteFile(GetTestPath("assets/test.hello.xml"), content);
91
92 const std::string out_apk = GetTestPath("out.apk");
93 std::vector<std::string> link_args = {
94 "--manifest", GetDefaultManifest(),
95 "-o", out_apk,
96 "-0", ".txt",
97 "-0", "txt2",
98 "-0", ".hello.txt",
99 "-0", "hello.xml",
100 "-A", GetTestPath("assets")
101 };
102
103 ASSERT_TRUE(Link(link_args, &diag));
104
105 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
106 ASSERT_THAT(apk, Ne(nullptr));
107 io::IFileCollection* zip = apk->GetFileCollection();
108 ASSERT_THAT(zip, Ne(nullptr));
109
110 auto file = zip->FindFile("assets/testtxt");
111 ASSERT_THAT(file, Ne(nullptr));
112 EXPECT_TRUE(file->WasCompressed());
113
114 file = zip->FindFile("assets/testtxt2");
115 ASSERT_THAT(file, Ne(nullptr));
116 EXPECT_FALSE(file->WasCompressed());
117
118 file = zip->FindFile("assets/test.txt");
119 ASSERT_THAT(file, Ne(nullptr));
120 EXPECT_FALSE(file->WasCompressed());
121
122 file = zip->FindFile("assets/test.hello.txt");
123 ASSERT_THAT(file, Ne(nullptr));
124 EXPECT_FALSE(file->WasCompressed());
125
126 file = zip->FindFile("assets/test.hello.xml");
127 ASSERT_THAT(file, Ne(nullptr));
128 EXPECT_FALSE(file->WasCompressed());
129 }
130
TEST_F(LinkTest,NoCompressResources)131 TEST_F(LinkTest, NoCompressResources) {
132 StdErrDiagnostics diag;
133 std::string content(500, 'a');
134 const std::string compiled_files_dir = GetTestPath("compiled");
135 ASSERT_TRUE(CompileFile(GetTestPath("res/raw/testtxt"), content, compiled_files_dir, &diag));
136 ASSERT_TRUE(CompileFile(GetTestPath("res/raw/test.txt"), content, compiled_files_dir, &diag));
137 ASSERT_TRUE(CompileFile(GetTestPath("res/raw/test1.hello.txt"), content, compiled_files_dir,
138 &diag));
139 ASSERT_TRUE(CompileFile(GetTestPath("res/raw/test2.goodbye.xml"), content, compiled_files_dir,
140 &diag));
141
142 const std::string out_apk = GetTestPath("out.apk");
143 std::vector<std::string> link_args = {
144 "--manifest", GetDefaultManifest(),
145 "-o", out_apk,
146 "-0", ".txt",
147 "-0", ".hello.txt",
148 "-0", "goodbye.xml",
149 };
150
151 ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));
152
153 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
154 ASSERT_THAT(apk, Ne(nullptr));
155 io::IFileCollection* zip = apk->GetFileCollection();
156 ASSERT_THAT(zip, Ne(nullptr));
157
158 auto file = zip->FindFile("res/raw/testtxt");
159 ASSERT_THAT(file, Ne(nullptr));
160 EXPECT_TRUE(file->WasCompressed());
161
162 file = zip->FindFile("res/raw/test.txt");
163 ASSERT_THAT(file, Ne(nullptr));
164 EXPECT_FALSE(file->WasCompressed());
165
166 file = zip->FindFile("res/raw/test1.hello.hello.txt");
167 ASSERT_THAT(file, Ne(nullptr));
168 EXPECT_FALSE(file->WasCompressed());
169
170 file = zip->FindFile("res/raw/test2.goodbye.goodbye.xml");
171 ASSERT_THAT(file, Ne(nullptr));
172 EXPECT_FALSE(file->WasCompressed());
173 }
174
TEST_F(LinkTest,OverlayStyles)175 TEST_F(LinkTest, OverlayStyles) {
176 StdErrDiagnostics diag;
177 const std::string compiled_files_dir = GetTestPath("compiled");
178 const std::string override_files_dir = GetTestPath("compiled-override");
179 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"),
180 R"(<resources>
181 <style name="MyStyle">
182 <item name="android:textColor">#123</item>
183 </style>
184 </resources>)",
185 compiled_files_dir, &diag));
186 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values-override.xml"),
187 R"(<resources>
188 <style name="MyStyle">
189 <item name="android:background">#456</item>
190 </style>
191 </resources>)",
192 override_files_dir, &diag));
193
194
195 const std::string out_apk = GetTestPath("out.apk");
196 std::vector<std::string> link_args = {
197 "--manifest", GetDefaultManifest(kDefaultPackageName),
198 "-o", out_apk,
199 };
200 const auto override_files = file::FindFiles(override_files_dir, &diag);
201 for (const auto &override_file : override_files.value()) {
202 link_args.push_back("-R");
203 link_args.push_back(file::BuildPath({override_files_dir, override_file}));
204 }
205 ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));
206
207 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
208 const Style* actual_style = test::GetValue<Style>(
209 apk->GetResourceTable(), std::string(kDefaultPackageName) + ":style/MyStyle");
210 ASSERT_NE(actual_style, nullptr);
211 ASSERT_EQ(actual_style->entries.size(), 2);
212 EXPECT_EQ(actual_style->entries[0].key.id, 0x01010098); // android:textColor
213 EXPECT_EQ(actual_style->entries[1].key.id, 0x010100d4); // android:background
214 }
215
TEST_F(LinkTest,OverrideStylesInsteadOfOverlaying)216 TEST_F(LinkTest, OverrideStylesInsteadOfOverlaying) {
217 StdErrDiagnostics diag;
218 const std::string compiled_files_dir = GetTestPath("compiled");
219 const std::string override_files_dir = GetTestPath("compiled-override");
220 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"),
221 R"(<resources>
222 <style name="MyStyle">
223 <item name="android:textColor">#123</item>
224 </style>
225 </resources>)",
226 compiled_files_dir, &diag));
227 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values-override.xml"),
228 R"(<resources>
229 <style name="MyStyle">
230 <item name="android:background">#456</item>
231 </style>
232 </resources>)",
233 override_files_dir, &diag));
234
235
236 const std::string out_apk = GetTestPath("out.apk");
237 std::vector<std::string> link_args = {
238 "--manifest", GetDefaultManifest(kDefaultPackageName),
239 "--override-styles-instead-of-overlaying",
240 "-o", out_apk,
241 };
242 const auto override_files = file::FindFiles(override_files_dir, &diag);
243 for (const auto &override_file : override_files.value()) {
244 link_args.push_back("-R");
245 link_args.push_back(file::BuildPath({override_files_dir, override_file}));
246 }
247 ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));
248
249 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
250 const Style* actual_style = test::GetValue<Style>(
251 apk->GetResourceTable(), std::string(kDefaultPackageName) + ":style/MyStyle");
252 ASSERT_NE(actual_style, nullptr);
253 ASSERT_EQ(actual_style->entries.size(), 1);
254 EXPECT_EQ(actual_style->entries[0].key.id, 0x010100d4); // android:background
255 }
256
TEST_F(LinkTest,AppInfoWithUsesSplit)257 TEST_F(LinkTest, AppInfoWithUsesSplit) {
258 StdErrDiagnostics diag;
259 const std::string base_files_dir = GetTestPath("base");
260 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"),
261 R"(<resources>
262 <string name="bar">bar</string>
263 </resources>)",
264 base_files_dir, &diag));
265 const std::string base_apk = GetTestPath("base.apk");
266 std::vector<std::string> link_args = {
267 "--manifest", GetDefaultManifest("com.aapt2.app"),
268 "-o", base_apk,
269 };
270 ASSERT_TRUE(Link(link_args, base_files_dir, &diag));
271
272 const std::string feature_manifest = GetTestPath("feature_manifest.xml");
273 WriteFile(feature_manifest, android::base::StringPrintf(R"(
274 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
275 package="com.aapt2.app" split="feature1">
276 </manifest>)"));
277 const std::string feature_files_dir = GetTestPath("feature");
278 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"),
279 R"(<resources>
280 <string name="foo">foo</string>
281 </resources>)",
282 feature_files_dir, &diag));
283 const std::string feature_apk = GetTestPath("feature.apk");
284 const std::string feature_package_id = "0x80";
285 link_args = {
286 "--manifest", feature_manifest,
287 "-I", base_apk,
288 "--package-id", feature_package_id,
289 "-o", feature_apk,
290 };
291 ASSERT_TRUE(Link(link_args, feature_files_dir, &diag));
292
293 const std::string feature2_manifest = GetTestPath("feature2_manifest.xml");
294 WriteFile(feature2_manifest, android::base::StringPrintf(R"(
295 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
296 package="com.aapt2.app" split="feature2">
297 <uses-split android:name="feature1"/>
298 </manifest>)"));
299 const std::string feature2_files_dir = GetTestPath("feature2");
300 ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"),
301 R"(<resources>
302 <string-array name="string_array">
303 <item>@string/bar</item>
304 <item>@string/foo</item>
305 </string-array>
306 </resources>)",
307 feature2_files_dir, &diag));
308 const std::string feature2_apk = GetTestPath("feature2.apk");
309 const std::string feature2_package_id = "0x81";
310 link_args = {
311 "--manifest", feature2_manifest,
312 "-I", base_apk,
313 "-I", feature_apk,
314 "--package-id", feature2_package_id,
315 "-o", feature2_apk,
316 };
317 ASSERT_TRUE(Link(link_args, feature2_files_dir, &diag));
318 }
319
320 } // namespace aapt
321