/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "java/ProguardRules.h" #include "link/Linkers.h" #include "io/StringStream.h" #include "test/Test.h" using ::aapt::io::StringOutputStream; using ::android::ConfigDescription; using ::testing::HasSubstr; using ::testing::Not; namespace aapt { std::string GetKeepSetString(const proguard::KeepSet& set, bool minimal_rules) { std::string out; StringOutputStream sout(&out); proguard::WriteKeepSet(set, &sout, minimal_rules); sout.Flush(); return out; } TEST(ProguardRulesTest, ManifestRuleDefaultConstructorOnly) { std::unique_ptr manifest = test::BuildXmlDom(R"( )"); proguard::KeepSet set; ASSERT_TRUE(proguard::CollectProguardRulesForManifest(manifest.get(), &set, false)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarAppComponentFactory { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarBackupAgent { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarApplication { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarActivity { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarService { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarReceiver { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarProvider { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarInstrumentation { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarZygotePreload { (); }")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarAppComponentFactory { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarBackupAgent { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarApplication { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarActivity { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarService { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarReceiver { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarProvider { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarInstrumentation { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarZygotePreload { (); }")); } TEST(ProguardRulesTest, FragmentNameRuleIsEmitted) { std::unique_ptr context = test::ContextBuilder().Build(); std::unique_ptr layout = test::BuildXmlDom(R"( )"); layout->file.name = test::ParseNameOrDie("layout/foo"); proguard::KeepSet set; ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (...); }")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (); }")); } TEST(ProguardRulesTest, FragmentClassRuleIsEmitted) { std::unique_ptr context = test::ContextBuilder().Build(); std::unique_ptr layout = test::BuildXmlDom(R"()"); layout->file.name = test::ParseNameOrDie("layout/foo"); proguard::KeepSet set; ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (...); }")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (); }")); } TEST(ProguardRulesTest, FragmentNameAndClassRulesAreEmitted) { std::unique_ptr context = test::ContextBuilder().Build(); std::unique_ptr layout = test::BuildXmlDom(R"( )"); layout->file.name = test::ParseNameOrDie("layout/foo"); proguard::KeepSet set; ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (...); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { (...); }")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { (); }")); } TEST(ProguardRulesTest, FragmentContainerViewNameRuleIsEmitted) { std::unique_ptr context = test::ContextBuilder().Build(); std::unique_ptr layout = test::BuildXmlDom(R"( )"); layout->file.name = test::ParseNameOrDie("layout/foo"); proguard::KeepSet set; ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (...); }")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (); }")); } TEST(ProguardRulesTest, FragmentContainerViewClassRuleIsEmitted) { std::unique_ptr context = test::ContextBuilder().Build(); std::unique_ptr layout = test::BuildXmlDom(R"()"); layout->file.name = test::ParseNameOrDie("layout/foo"); proguard::KeepSet set; ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (...); }")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (); }")); } TEST(ProguardRulesTest, FragmentContainerViewNameAndClassRulesAreEmitted) { std::unique_ptr context = test::ContextBuilder().Build(); std::unique_ptr layout = test::BuildXmlDom(R"( )"); layout->file.name = test::ParseNameOrDie("layout/foo"); proguard::KeepSet set; ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (...); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { (...); }")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { (); }")); } TEST(ProguardRulesTest, NavigationFragmentNameAndClassRulesAreEmitted) { std::unique_ptr context = test::ContextBuilder() .SetCompilationPackage("com.base").Build(); std::unique_ptr navigation = test::BuildXmlDom(R"( )"); navigation->file.name = test::ParseNameOrDie("navigation/graph.xml"); proguard::KeepSet set; ASSERT_TRUE(proguard::CollectProguardRules(context.get(), navigation.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr("-keep class com.package.Foo { (...); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.package.Bar { (...); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.base.Nested { (...); }")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr("-keep class com.package.Foo { (...); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.package.Bar { (...); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.base.Nested { (...); }")); } TEST(ProguardRulesTest, CustomViewRulesAreEmitted) { std::unique_ptr context = test::ContextBuilder().Build(); std::unique_ptr layout = test::BuildXmlDom(R"( )"); layout->file.name = test::ParseNameOrDie("layout/foo"); proguard::KeepSet set; ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (...); }")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr( "-keep class com.foo.Bar { (android.content.Context, android.util.AttributeSet); }")); } TEST(ProguardRulesTest, IncludedLayoutRulesAreConditional) { std::unique_ptr bar_layout = test::BuildXmlDom(R"( )"); bar_layout->file.name = test::ParseNameOrDie("com.foo:layout/bar"); ResourceTable table; StdErrDiagnostics errDiagnostics; table.AddResource(bar_layout->file.name, ConfigDescription::DefaultConfig(), "", util::make_unique(), &errDiagnostics); std::unique_ptr context = test::ContextBuilder() .SetCompilationPackage("com.foo") .AddSymbolSource(util::make_unique(&table)) .Build(); std::unique_ptr foo_layout = test::BuildXmlDom(R"( )"); foo_layout->file.name = test::ParseNameOrDie("com.foo:layout/foo"); XmlReferenceLinker xml_linker; ASSERT_TRUE(xml_linker.Consume(context.get(), bar_layout.get())); ASSERT_TRUE(xml_linker.Consume(context.get(), foo_layout.get())); proguard::KeepSet set = proguard::KeepSet(true); ASSERT_TRUE(proguard::CollectProguardRules(context.get(), bar_layout.get(), &set)); ASSERT_TRUE(proguard::CollectProguardRules(context.get(), foo_layout.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr("-if class **.R$layout")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (...); }")); EXPECT_THAT(actual, HasSubstr("int foo")); EXPECT_THAT(actual, HasSubstr("int bar")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr("-if class **.R$layout")); EXPECT_THAT(actual, HasSubstr( "-keep class com.foo.Bar { (android.content.Context, android.util.AttributeSet); }")); EXPECT_THAT(actual, HasSubstr("int foo")); EXPECT_THAT(actual, HasSubstr("int bar")); } TEST(ProguardRulesTest, AliasedLayoutRulesAreConditional) { std::unique_ptr context = test::ContextBuilder().Build(); std::unique_ptr layout = test::BuildXmlDom(R"( )"); layout->file.name = test::ParseNameOrDie("layout/foo"); proguard::KeepSet set = proguard::KeepSet(true); set.AddReference({test::ParseNameOrDie("layout/bar"), {}}, layout->file.name); ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr( "-keep class com.foo.Bar { (...); }")); EXPECT_THAT(actual, HasSubstr("-if class **.R$layout")); EXPECT_THAT(actual, HasSubstr("int foo")); EXPECT_THAT(actual, HasSubstr("int bar")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr( "-keep class com.foo.Bar { (android.content.Context, android.util.AttributeSet); }")); EXPECT_THAT(actual, HasSubstr("-if class **.R$layout")); EXPECT_THAT(actual, HasSubstr("int foo")); EXPECT_THAT(actual, HasSubstr("int bar")); } TEST(ProguardRulesTest, NonLayoutReferencesAreUnconditional) { std::unique_ptr context = test::ContextBuilder().Build(); std::unique_ptr layout = test::BuildXmlDom(R"( )"); layout->file.name = test::ParseNameOrDie("layout/foo"); proguard::KeepSet set = proguard::KeepSet(true); set.AddReference({test::ParseNameOrDie("style/MyStyle"), {}}, layout->file.name); ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, Not(HasSubstr("-if"))); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (...); }")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, Not(HasSubstr("-if"))); EXPECT_THAT(actual, HasSubstr( "-keep class com.foo.Bar { (android.content.Context, android.util.AttributeSet); }")); } TEST(ProguardRulesTest, ViewOnClickRuleIsEmitted) { std::unique_ptr context = test::ContextBuilder().Build(); std::unique_ptr layout = test::BuildXmlDom(R"( )"); layout->file.name = test::ParseNameOrDie("layout/foo"); proguard::KeepSet set; ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr( "-keepclassmembers class * { *** bar_method(android.view.View); }")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr( "-keepclassmembers class * { *** bar_method(android.view.View); }")); } TEST(ProguardRulesTest, MenuRulesAreEmitted) { std::unique_ptr context = test::ContextBuilder().Build(); std::unique_ptr menu = test::BuildXmlDom(R"( )"); menu->file.name = test::ParseNameOrDie("menu/foo"); proguard::KeepSet set; ASSERT_TRUE(proguard::CollectProguardRules(context.get(), menu.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr( "-keepclassmembers class * { *** on_click(android.view.MenuItem); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (...); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { (...); }")); EXPECT_THAT(actual, Not(HasSubstr("com.foo.Bat"))); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr( "-keepclassmembers class * { *** on_click(android.view.MenuItem); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (android.content.Context); }")); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { (android.content.Context); }")); EXPECT_THAT(actual, Not(HasSubstr("com.foo.Bat"))); } TEST(ProguardRulesTest, TransitionPathMotionRulesAreEmitted) { std::unique_ptr context = test::ContextBuilder().Build(); std::unique_ptr transition = test::BuildXmlDom(R"( )"); transition->file.name = test::ParseNameOrDie("transition/foo"); proguard::KeepSet set; ASSERT_TRUE(proguard::CollectProguardRules(context.get(), transition.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (...); }")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr( "-keep class com.foo.Bar { (android.content.Context, android.util.AttributeSet); }")); } TEST(ProguardRulesTest, TransitionRulesAreEmitted) { std::unique_ptr context = test::ContextBuilder().Build(); std::unique_ptr transitionSet = test::BuildXmlDom(R"( )"); transitionSet->file.name = test::ParseNameOrDie("transition/foo"); proguard::KeepSet set; ASSERT_TRUE(proguard::CollectProguardRules(context.get(), transitionSet.get(), &set)); std::string actual = GetKeepSetString(set, /** minimal_rules */ false); EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { (...); }")); actual = GetKeepSetString(set, /** minimal_rules */ true); EXPECT_THAT(actual, HasSubstr( "-keep class com.foo.Bar { (android.content.Context, android.util.AttributeSet); }")); } TEST(ProguardRulesTest, UsageLocationComparator) { proguard::UsageLocation location1 = {{"pkg", ResourceType::kAttr, "x"}}; proguard::UsageLocation location2 = {{"pkg", ResourceType::kAttr, "y"}}; EXPECT_EQ(location1 < location2, true); EXPECT_EQ(location2 < location1, false); } } // namespace aapt