blob: bbc6f9f64414251d1511b5db6ef3bdb00f8ad43e [file] [log] [blame]
Adam Lesinskif762df22017-06-26 16:39:03 -07001/*
2 * Copyright (C) 2017 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 "java/ProguardRules.h"
Adam Koskidc21dea2017-07-21 10:55:27 -070018#include "link/Linkers.h"
Adam Lesinskif762df22017-06-26 16:39:03 -070019
Adam Lesinskia693c4a2017-11-09 11:29:39 -080020#include "io/StringStream.h"
Adam Lesinskif762df22017-06-26 16:39:03 -070021#include "test/Test.h"
22
Adam Lesinskia693c4a2017-11-09 11:29:39 -080023using ::aapt::io::StringOutputStream;
Adam Lesinskif762df22017-06-26 16:39:03 -070024using ::testing::HasSubstr;
25using ::testing::Not;
26
27namespace aapt {
28
Adam Lesinskia693c4a2017-11-09 11:29:39 -080029std::string GetKeepSetString(const proguard::KeepSet& set) {
30 std::string out;
31 StringOutputStream sout(&out);
32 proguard::WriteKeepSet(set, &sout);
33 sout.Flush();
34 return out;
35}
36
Jake Whartonab660a72018-06-08 17:56:55 -040037TEST(ProguardRulesTest, ManifestRuleDefaultConstructorOnly) {
38 std::unique_ptr<xml::XmlResource> manifest = test::BuildXmlDom(R"(
39 <manifest xmlns:android="http://schemas.android.com/apk/res/android">
40 <application android:backupAgent="com.foo.BarBackupAgent">
41 <activity android:name="com.foo.BarActivity"/>
42 <service android:name="com.foo.BarService"/>
43 <receiver android:name="com.foo.BarReceiver"/>
44 <provider android:name="com.foo.BarProvider"/>
45 </application>
46 <instrumentation android:name="com.foo.BarInstrumentation"/>
47 </manifest>)");
48
49 proguard::KeepSet set;
50 ASSERT_TRUE(proguard::CollectProguardRulesForManifest(manifest.get(), &set, false));
51
52 std::string actual = GetKeepSetString(set);
53
54 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarBackupAgent { <init>(); }"));
55 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarActivity { <init>(); }"));
56 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarService { <init>(); }"));
57 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarReceiver { <init>(); }"));
58 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarProvider { <init>(); }"));
59 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarInstrumentation { <init>(); }"));
60}
61
Adam Lesinskif762df22017-06-26 16:39:03 -070062TEST(ProguardRulesTest, FragmentNameRuleIsEmitted) {
63 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
64 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
65 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
66 android:name="com.foo.Bar"/>)");
67 layout->file.name = test::ParseNameOrDie("layout/foo");
68
69 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -070070 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -070071
Adam Lesinskia693c4a2017-11-09 11:29:39 -080072 std::string actual = GetKeepSetString(set);
Adam Lesinskif762df22017-06-26 16:39:03 -070073
Adam Lesinskif762df22017-06-26 16:39:03 -070074 EXPECT_THAT(actual, HasSubstr("com.foo.Bar"));
75}
76
77TEST(ProguardRulesTest, FragmentClassRuleIsEmitted) {
78 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
79 std::unique_ptr<xml::XmlResource> layout =
80 test::BuildXmlDom(R"(<fragment class="com.foo.Bar"/>)");
81 layout->file.name = test::ParseNameOrDie("layout/foo");
82
83 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -070084 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -070085
Adam Lesinskia693c4a2017-11-09 11:29:39 -080086 std::string actual = GetKeepSetString(set);
Adam Lesinskif762df22017-06-26 16:39:03 -070087
Adam Lesinskif762df22017-06-26 16:39:03 -070088 EXPECT_THAT(actual, HasSubstr("com.foo.Bar"));
89}
90
91TEST(ProguardRulesTest, FragmentNameAndClassRulesAreEmitted) {
92 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
93 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
94 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
95 android:name="com.foo.Baz"
96 class="com.foo.Bar"/>)");
97 layout->file.name = test::ParseNameOrDie("layout/foo");
98
99 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700100 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -0700101
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800102 std::string actual = GetKeepSetString(set);
Adam Lesinskif762df22017-06-26 16:39:03 -0700103
Adam Lesinskif762df22017-06-26 16:39:03 -0700104 EXPECT_THAT(actual, HasSubstr("com.foo.Bar"));
105 EXPECT_THAT(actual, HasSubstr("com.foo.Baz"));
106}
107
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700108TEST(ProguardRulesTest, NavigationFragmentNameAndClassRulesAreEmitted) {
109 std::unique_ptr<IAaptContext> context = test::ContextBuilder()
110 .SetCompilationPackage("com.base").Build();
111 std::unique_ptr<xml::XmlResource> navigation = test::BuildXmlDom(R"(
112 <navigation
113 xmlns:android="http://schemas.android.com/apk/res/android"
114 xmlns:app="http://schemas.android.com/apk/res-auto">
115 <custom android:id="@id/foo"
116 android:name="com.package.Foo"/>
117 <fragment android:id="@id/bar"
118 android:name="com.package.Bar">
119 <nested android:id="@id/nested"
120 android:name=".Nested"/>
121 </fragment>
122 </navigation>
123 )");
124
125 navigation->file.name = test::ParseNameOrDie("navigation/graph.xml");
126
127 proguard::KeepSet set;
128 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), navigation.get(), &set));
129
130 std::string actual = GetKeepSetString(set);
131 EXPECT_THAT(actual, HasSubstr("com.package.Foo"));
132 EXPECT_THAT(actual, HasSubstr("com.package.Bar"));
133 EXPECT_THAT(actual, HasSubstr("com.base.Nested"));
134}
135
Adam Koskidc21dea2017-07-21 10:55:27 -0700136TEST(ProguardRulesTest, CustomViewRulesAreEmitted) {
137 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
138 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
139 <View xmlns:android="http://schemas.android.com/apk/res/android">
140 <com.foo.Bar />
141 </View>)");
142 layout->file.name = test::ParseNameOrDie("layout/foo");
143
144 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700145 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Koskidc21dea2017-07-21 10:55:27 -0700146
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800147 std::string actual = GetKeepSetString(set);
Adam Koskidc21dea2017-07-21 10:55:27 -0700148
Adam Koskidc21dea2017-07-21 10:55:27 -0700149 EXPECT_THAT(actual, HasSubstr("com.foo.Bar"));
150}
151
152TEST(ProguardRulesTest, IncludedLayoutRulesAreConditional) {
153 std::unique_ptr<xml::XmlResource> bar_layout = test::BuildXmlDom(R"(
154 <View xmlns:android="http://schemas.android.com/apk/res/android">
155 <com.foo.Bar />
156 </View>)");
157 bar_layout->file.name = test::ParseNameOrDie("com.foo:layout/bar");
158
159 ResourceTable table;
160 StdErrDiagnostics errDiagnostics;
161 table.AddResource(bar_layout->file.name, ConfigDescription::DefaultConfig(), "",
162 util::make_unique<FileReference>(), &errDiagnostics);
163
164 std::unique_ptr<IAaptContext> context =
165 test::ContextBuilder()
166 .SetCompilationPackage("com.foo")
167 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(&table))
168 .Build();
169
170 std::unique_ptr<xml::XmlResource> foo_layout = test::BuildXmlDom(R"(
171 <View xmlns:android="http://schemas.android.com/apk/res/android">
172 <include layout="@layout/bar" />
173 </View>)");
174 foo_layout->file.name = test::ParseNameOrDie("com.foo:layout/foo");
175
176 XmlReferenceLinker xml_linker;
177 ASSERT_TRUE(xml_linker.Consume(context.get(), bar_layout.get()));
178 ASSERT_TRUE(xml_linker.Consume(context.get(), foo_layout.get()));
179
180 proguard::KeepSet set = proguard::KeepSet(true);
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700181 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), bar_layout.get(), &set));
182 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), foo_layout.get(), &set));
Adam Koskidc21dea2017-07-21 10:55:27 -0700183
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800184 std::string actual = GetKeepSetString(set);
Adam Koskidc21dea2017-07-21 10:55:27 -0700185
Adam Koski09ef94e2017-11-10 11:15:55 -0800186 EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800187 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700188 EXPECT_THAT(actual, HasSubstr("int foo"));
189 EXPECT_THAT(actual, HasSubstr("int bar"));
190 EXPECT_THAT(actual, HasSubstr("com.foo.Bar"));
191}
192
193TEST(ProguardRulesTest, AliasedLayoutRulesAreConditional) {
194 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
195 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
196 <View xmlns:android="http://schemas.android.com/apk/res/android">
197 <com.foo.Bar />
198 </View>)");
199 layout->file.name = test::ParseNameOrDie("layout/foo");
200
201 proguard::KeepSet set = proguard::KeepSet(true);
202 set.AddReference({test::ParseNameOrDie("layout/bar"), {}}, layout->file.name);
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700203 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Koskidc21dea2017-07-21 10:55:27 -0700204
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800205 std::string actual = GetKeepSetString(set);
Adam Koskidc21dea2017-07-21 10:55:27 -0700206
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800207 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Koski09ef94e2017-11-10 11:15:55 -0800208 EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700209 EXPECT_THAT(actual, HasSubstr("int foo"));
210 EXPECT_THAT(actual, HasSubstr("int bar"));
211 EXPECT_THAT(actual, HasSubstr("com.foo.Bar"));
212}
213
214TEST(ProguardRulesTest, NonLayoutReferencesAreUnconditional) {
215 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
216 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
217 <View xmlns:android="http://schemas.android.com/apk/res/android">
218 <com.foo.Bar />
219 </View>)");
220 layout->file.name = test::ParseNameOrDie("layout/foo");
221
222 proguard::KeepSet set = proguard::KeepSet(true);
223 set.AddReference({test::ParseNameOrDie("style/MyStyle"), {}}, layout->file.name);
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700224 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Koskidc21dea2017-07-21 10:55:27 -0700225
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800226 std::string actual = GetKeepSetString(set);
Adam Koskidc21dea2017-07-21 10:55:27 -0700227
Adam Koski09ef94e2017-11-10 11:15:55 -0800228 EXPECT_THAT(actual, Not(HasSubstr("-if")));
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800229 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700230}
231
Adam Lesinskif762df22017-06-26 16:39:03 -0700232TEST(ProguardRulesTest, ViewOnClickRuleIsEmitted) {
233 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
234 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
235 <View xmlns:android="http://schemas.android.com/apk/res/android"
236 android:onClick="bar_method" />)");
237 layout->file.name = test::ParseNameOrDie("layout/foo");
238
239 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700240 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -0700241
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800242 std::string actual = GetKeepSetString(set);
Adam Lesinskif762df22017-06-26 16:39:03 -0700243
Adam Lesinskif762df22017-06-26 16:39:03 -0700244 EXPECT_THAT(actual, HasSubstr("bar_method"));
245}
246
247TEST(ProguardRulesTest, MenuRulesAreEmitted) {
248 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
249 std::unique_ptr<xml::XmlResource> menu = test::BuildXmlDom(R"(
250 <menu xmlns:android="http://schemas.android.com/apk/res/android">
251 <item android:onClick="on_click"
252 android:actionViewClass="com.foo.Bar"
253 android:actionProviderClass="com.foo.Baz"
254 android:name="com.foo.Bat" />
255 </menu>)");
256 menu->file.name = test::ParseNameOrDie("menu/foo");
257
258 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700259 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), menu.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -0700260
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800261 std::string actual = GetKeepSetString(set);
Adam Lesinskif762df22017-06-26 16:39:03 -0700262
Adam Lesinskif762df22017-06-26 16:39:03 -0700263 EXPECT_THAT(actual, HasSubstr("on_click"));
264 EXPECT_THAT(actual, HasSubstr("com.foo.Bar"));
265 EXPECT_THAT(actual, HasSubstr("com.foo.Baz"));
266 EXPECT_THAT(actual, Not(HasSubstr("com.foo.Bat")));
267}
268
269} // namespace aapt