blob: a548f4a144ec5c8b208f9d4392f9894739cdac84 [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
Jake Wharton420785e2018-06-11 15:40:48 -040074 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -070075}
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
Jake Wharton420785e2018-06-11 15:40:48 -040088 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -070089}
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
Jake Wharton420785e2018-06-11 15:40:48 -0400104 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
105 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(...); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700106}
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);
Jake Wharton420785e2018-06-11 15:40:48 -0400131 EXPECT_THAT(actual, HasSubstr("-keep class com.package.Foo { <init>(...); }"));
132 EXPECT_THAT(actual, HasSubstr("-keep class com.package.Bar { <init>(...); }"));
133 EXPECT_THAT(actual, HasSubstr("-keep class com.base.Nested { <init>(...); }"));
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700134}
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
Jake Wharton420785e2018-06-11 15:40:48 -0400149 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700150}
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"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700190}
191
192TEST(ProguardRulesTest, AliasedLayoutRulesAreConditional) {
193 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
194 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
195 <View xmlns:android="http://schemas.android.com/apk/res/android">
196 <com.foo.Bar />
197 </View>)");
198 layout->file.name = test::ParseNameOrDie("layout/foo");
199
200 proguard::KeepSet set = proguard::KeepSet(true);
201 set.AddReference({test::ParseNameOrDie("layout/bar"), {}}, layout->file.name);
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700202 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Koskidc21dea2017-07-21 10:55:27 -0700203
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800204 std::string actual = GetKeepSetString(set);
Adam Koskidc21dea2017-07-21 10:55:27 -0700205
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800206 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Koski09ef94e2017-11-10 11:15:55 -0800207 EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700208 EXPECT_THAT(actual, HasSubstr("int foo"));
209 EXPECT_THAT(actual, HasSubstr("int bar"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700210}
211
212TEST(ProguardRulesTest, NonLayoutReferencesAreUnconditional) {
213 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
214 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
215 <View xmlns:android="http://schemas.android.com/apk/res/android">
216 <com.foo.Bar />
217 </View>)");
218 layout->file.name = test::ParseNameOrDie("layout/foo");
219
220 proguard::KeepSet set = proguard::KeepSet(true);
221 set.AddReference({test::ParseNameOrDie("style/MyStyle"), {}}, layout->file.name);
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700222 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Koskidc21dea2017-07-21 10:55:27 -0700223
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800224 std::string actual = GetKeepSetString(set);
Adam Koskidc21dea2017-07-21 10:55:27 -0700225
Adam Koski09ef94e2017-11-10 11:15:55 -0800226 EXPECT_THAT(actual, Not(HasSubstr("-if")));
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800227 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700228}
229
Adam Lesinskif762df22017-06-26 16:39:03 -0700230TEST(ProguardRulesTest, ViewOnClickRuleIsEmitted) {
231 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
232 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
233 <View xmlns:android="http://schemas.android.com/apk/res/android"
234 android:onClick="bar_method" />)");
235 layout->file.name = test::ParseNameOrDie("layout/foo");
236
237 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700238 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -0700239
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800240 std::string actual = GetKeepSetString(set);
Adam Lesinskif762df22017-06-26 16:39:03 -0700241
Jake Wharton420785e2018-06-11 15:40:48 -0400242 EXPECT_THAT(actual, HasSubstr("-keepclassmembers class * { *** bar_method(...); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700243}
244
245TEST(ProguardRulesTest, MenuRulesAreEmitted) {
246 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
247 std::unique_ptr<xml::XmlResource> menu = test::BuildXmlDom(R"(
248 <menu xmlns:android="http://schemas.android.com/apk/res/android">
249 <item android:onClick="on_click"
250 android:actionViewClass="com.foo.Bar"
251 android:actionProviderClass="com.foo.Baz"
252 android:name="com.foo.Bat" />
253 </menu>)");
254 menu->file.name = test::ParseNameOrDie("menu/foo");
255
256 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700257 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), menu.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -0700258
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800259 std::string actual = GetKeepSetString(set);
Adam Lesinskif762df22017-06-26 16:39:03 -0700260
Jake Wharton420785e2018-06-11 15:40:48 -0400261 EXPECT_THAT(actual, HasSubstr("-keepclassmembers class * { *** on_click(...); }"));
262 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
263 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(...); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700264 EXPECT_THAT(actual, Not(HasSubstr("com.foo.Bat")));
265}
266
Jake Wharton420785e2018-06-11 15:40:48 -0400267TEST(ProguardRulesTest, TransitionPathMotionRulesAreEmitted) {
268 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
269 std::unique_ptr<xml::XmlResource> transition = test::BuildXmlDom(R"(
270 <changeBounds>
271 <pathMotion class="com.foo.Bar"/>
272 </changeBounds>)");
273 transition->file.name = test::ParseNameOrDie("transition/foo");
274
275 proguard::KeepSet set;
276 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), transition.get(), &set));
277
278 std::string actual = GetKeepSetString(set);
279
280 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
281}
282
283TEST(ProguardRulesTest, TransitionRulesAreEmitted) {
284 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
285 std::unique_ptr<xml::XmlResource> transitionSet = test::BuildXmlDom(R"(
286 <transitionSet>
287 <transition class="com.foo.Bar"/>
288 </transitionSet>)");
289 transitionSet->file.name = test::ParseNameOrDie("transition/foo");
290
291 proguard::KeepSet set;
292 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), transitionSet.get(), &set));
293
294 std::string actual = GetKeepSetString(set);
295
296 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
297}
298
Adam Lesinskif762df22017-06-26 16:39:03 -0700299} // namespace aapt