diff options
author | 2017-05-08 18:36:33 -0700 | |
---|---|---|
committer | 2017-05-08 18:36:33 -0700 | |
commit | 63699b128e009c65affe50995bd8f86eca1a8694 (patch) | |
tree | 43c4643a1d40161e1ef402cccaa195e3e03b5204 | |
parent | f93dc8b6504200d0b6d502d924a70a743f9b1411 (diff) |
AAPT2: Ignore namespaced elements in AndroidManifest.xml
Some third party stores/tools expect manifest elements
under their namespace, and AAPT2 shouldn't fail if these
are present.
Bug: 37943705
Test: make aapt2_tests
Change-Id: I87b7500c7da5e8e79fc2a78b30e8e4334124af3d
-rw-r--r-- | tools/aapt2/link/ManifestFixer_test.cpp | 18 | ||||
-rw-r--r-- | tools/aapt2/xml/XmlActionExecutor.cpp | 49 | ||||
-rw-r--r-- | tools/aapt2/xml/XmlActionExecutor.h | 25 |
3 files changed, 49 insertions, 43 deletions
diff --git a/tools/aapt2/link/ManifestFixer_test.cpp b/tools/aapt2/link/ManifestFixer_test.cpp index ce84993feebe..064d3650065d 100644 --- a/tools/aapt2/link/ManifestFixer_test.cpp +++ b/tools/aapt2/link/ManifestFixer_test.cpp @@ -402,4 +402,22 @@ TEST_F(ManifestFixerTest, UsesFeatureMustHaveNameOrGlEsVersion) { EXPECT_EQ(nullptr, Verify(input)); } +TEST_F(ManifestFixerTest, IgnoreNamespacedElements) { + std::string input = R"EOF( + <manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="android"> + <special:tag whoo="true" xmlns:special="http://google.com" /> + </manifest>)EOF"; + EXPECT_NE(nullptr, Verify(input)); +} + +TEST_F(ManifestFixerTest, DoNotIgnoreNonNamespacedElements) { + std::string input = R"EOF( + <manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="android"> + <tag whoo="true" /> + </manifest>)EOF"; + EXPECT_EQ(nullptr, Verify(input)); +} + } // namespace aapt diff --git a/tools/aapt2/xml/XmlActionExecutor.cpp b/tools/aapt2/xml/XmlActionExecutor.cpp index 7580b469a98e..352a93361633 100644 --- a/tools/aapt2/xml/XmlActionExecutor.cpp +++ b/tools/aapt2/xml/XmlActionExecutor.cpp @@ -19,8 +19,7 @@ namespace aapt { namespace xml { -static bool wrapper_one(XmlNodeAction::ActionFunc& f, Element* el, - SourcePathDiagnostics*) { +static bool wrapper_one(XmlNodeAction::ActionFunc& f, Element* el, SourcePathDiagnostics*) { return f(el); } @@ -47,8 +46,8 @@ static void PrintElementToDiagMessage(const Element* el, DiagMessage* msg) { *msg << el->name << ">"; } -bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, - SourcePathDiagnostics* diag, Element* el) const { +bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag, + Element* el) const { bool error = false; for (const ActionFuncWithDiag& action : actions_) { error |= !action(el, diag); @@ -56,28 +55,27 @@ bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, for (Element* child_el : el->GetChildElements()) { if (child_el->namespace_uri.empty()) { - std::map<std::string, XmlNodeAction>::const_iterator iter = - map_.find(child_el->name); + std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(child_el->name); if (iter != map_.end()) { error |= !iter->second.Execute(policy, diag, child_el); continue; } - } - if (policy == XmlActionExecutorPolicy::kWhitelist) { - DiagMessage error_msg(child_el->line_number); - error_msg << "unknown element "; - PrintElementToDiagMessage(child_el, &error_msg); - error_msg << " found"; - diag->Error(error_msg); - error = true; + if (policy == XmlActionExecutorPolicy::kWhitelist) { + DiagMessage error_msg(child_el->line_number); + error_msg << "unknown element "; + PrintElementToDiagMessage(child_el, &error_msg); + error_msg << " found"; + diag->Error(error_msg); + error = true; + } } } return !error; } -bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, - IDiagnostics* diag, XmlResource* doc) const { +bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, + XmlResource* doc) const { SourcePathDiagnostics source_diag(doc->file.source, diag); Element* el = FindRootElement(doc); @@ -90,20 +88,19 @@ bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, } if (el->namespace_uri.empty()) { - std::map<std::string, XmlNodeAction>::const_iterator iter = - map_.find(el->name); + std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(el->name); if (iter != map_.end()) { return iter->second.Execute(policy, &source_diag, el); } - } - if (policy == XmlActionExecutorPolicy::kWhitelist) { - DiagMessage error_msg(el->line_number); - error_msg << "unknown element "; - PrintElementToDiagMessage(el, &error_msg); - error_msg << " found"; - source_diag.Error(error_msg); - return false; + if (policy == XmlActionExecutorPolicy::kWhitelist) { + DiagMessage error_msg(el->line_number); + error_msg << "unknown element "; + PrintElementToDiagMessage(el, &error_msg); + error_msg << " found"; + source_diag.Error(error_msg); + return false; + } } return true; } diff --git a/tools/aapt2/xml/XmlActionExecutor.h b/tools/aapt2/xml/XmlActionExecutor.h index 68e35631988e..1d70045b3023 100644 --- a/tools/aapt2/xml/XmlActionExecutor.h +++ b/tools/aapt2/xml/XmlActionExecutor.h @@ -31,17 +31,12 @@ namespace aapt { namespace xml { enum class XmlActionExecutorPolicy { - /** - * Actions on run if elements are matched, errors occur only when actions - * return false. - */ + // Actions are run if elements are matched, errors occur only when actions return false. kNone, - /** - * The actions defined must match and run. If an element is found that does - * not match - * an action, an error occurs. - */ + // The actions defined must match and run. If an element is found that does + // not match an action, an error occurs. + // Note: namespaced elements are always ignored. kWhitelist, }; @@ -52,14 +47,12 @@ enum class XmlActionExecutorPolicy { */ class XmlNodeAction { public: - using ActionFuncWithDiag = - std::function<bool(Element*, SourcePathDiagnostics*)>; + using ActionFuncWithDiag = std::function<bool(Element*, SourcePathDiagnostics*)>; using ActionFunc = std::function<bool(Element*)>; /** * Find or create a child XmlNodeAction that will be performed for the child - * element - * with the name `name`. + * element with the name `name`. */ XmlNodeAction& operator[](const std::string& name) { return map_[name]; } @@ -72,8 +65,7 @@ class XmlNodeAction { private: friend class XmlActionExecutor; - bool Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag, - Element* el) const; + bool Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag, Element* el) const; std::map<std::string, XmlNodeAction> map_; std::vector<ActionFuncWithDiag> actions_; @@ -98,8 +90,7 @@ class XmlActionExecutor { * Execute the defined actions for this XmlResource. * Returns true if all actions return true, otherwise returns false. */ - bool Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, - XmlResource* doc) const; + bool Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, XmlResource* doc) const; private: std::map<std::string, XmlNodeAction> map_; |