Loading tools/aapt2/link/ManifestFixer_test.cpp +18 −0 Original line number Original line Diff line number Diff line Loading @@ -402,4 +402,22 @@ TEST_F(ManifestFixerTest, UsesFeatureMustHaveNameOrGlEsVersion) { EXPECT_EQ(nullptr, Verify(input)); 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 } // namespace aapt tools/aapt2/xml/XmlActionExecutor.cpp +23 −26 Original line number Original line Diff line number Diff line Loading @@ -19,8 +19,7 @@ namespace aapt { namespace aapt { namespace xml { namespace xml { static bool wrapper_one(XmlNodeAction::ActionFunc& f, Element* el, static bool wrapper_one(XmlNodeAction::ActionFunc& f, Element* el, SourcePathDiagnostics*) { SourcePathDiagnostics*) { return f(el); return f(el); } } Loading @@ -47,8 +46,8 @@ static void PrintElementToDiagMessage(const Element* el, DiagMessage* msg) { *msg << el->name << ">"; *msg << el->name << ">"; } } bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag, SourcePathDiagnostics* diag, Element* el) const { Element* el) const { bool error = false; bool error = false; for (const ActionFuncWithDiag& action : actions_) { for (const ActionFuncWithDiag& action : actions_) { error |= !action(el, diag); error |= !action(el, diag); Loading @@ -56,13 +55,11 @@ bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, for (Element* child_el : el->GetChildElements()) { for (Element* child_el : el->GetChildElements()) { if (child_el->namespace_uri.empty()) { if (child_el->namespace_uri.empty()) { std::map<std::string, XmlNodeAction>::const_iterator iter = std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(child_el->name); map_.find(child_el->name); if (iter != map_.end()) { if (iter != map_.end()) { error |= !iter->second.Execute(policy, diag, child_el); error |= !iter->second.Execute(policy, diag, child_el); continue; continue; } } } if (policy == XmlActionExecutorPolicy::kWhitelist) { if (policy == XmlActionExecutorPolicy::kWhitelist) { DiagMessage error_msg(child_el->line_number); DiagMessage error_msg(child_el->line_number); Loading @@ -73,11 +70,12 @@ bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, error = true; error = true; } } } } } return !error; return !error; } } bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, IDiagnostics* diag, XmlResource* doc) const { XmlResource* doc) const { SourcePathDiagnostics source_diag(doc->file.source, diag); SourcePathDiagnostics source_diag(doc->file.source, diag); Element* el = FindRootElement(doc); Element* el = FindRootElement(doc); Loading @@ -90,12 +88,10 @@ bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, } } if (el->namespace_uri.empty()) { if (el->namespace_uri.empty()) { std::map<std::string, XmlNodeAction>::const_iterator iter = std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(el->name); map_.find(el->name); if (iter != map_.end()) { if (iter != map_.end()) { return iter->second.Execute(policy, &source_diag, el); return iter->second.Execute(policy, &source_diag, el); } } } if (policy == XmlActionExecutorPolicy::kWhitelist) { if (policy == XmlActionExecutorPolicy::kWhitelist) { DiagMessage error_msg(el->line_number); DiagMessage error_msg(el->line_number); Loading @@ -105,6 +101,7 @@ bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, source_diag.Error(error_msg); source_diag.Error(error_msg); return false; return false; } } } return true; return true; } } Loading tools/aapt2/xml/XmlActionExecutor.h +8 −17 Original line number Original line Diff line number Diff line Loading @@ -31,17 +31,12 @@ namespace aapt { namespace xml { namespace xml { enum class XmlActionExecutorPolicy { enum class XmlActionExecutorPolicy { /** // Actions are run if elements are matched, errors occur only when actions return false. * Actions on run if elements are matched, errors occur only when actions * return false. */ kNone, kNone, /** // The actions defined must match and run. If an element is found that does * The actions defined must match and run. If an element is found that does // not match an action, an error occurs. * not match // Note: namespaced elements are always ignored. * an action, an error occurs. */ kWhitelist, kWhitelist, }; }; Loading @@ -52,14 +47,12 @@ enum class XmlActionExecutorPolicy { */ */ class XmlNodeAction { class XmlNodeAction { public: public: using ActionFuncWithDiag = using ActionFuncWithDiag = std::function<bool(Element*, SourcePathDiagnostics*)>; std::function<bool(Element*, SourcePathDiagnostics*)>; using ActionFunc = std::function<bool(Element*)>; using ActionFunc = std::function<bool(Element*)>; /** /** * Find or create a child XmlNodeAction that will be performed for the child * Find or create a child XmlNodeAction that will be performed for the child * element * element with the name `name`. * with the name `name`. */ */ XmlNodeAction& operator[](const std::string& name) { return map_[name]; } XmlNodeAction& operator[](const std::string& name) { return map_[name]; } Loading @@ -72,8 +65,7 @@ class XmlNodeAction { private: private: friend class XmlActionExecutor; friend class XmlActionExecutor; bool Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag, bool Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag, Element* el) const; Element* el) const; std::map<std::string, XmlNodeAction> map_; std::map<std::string, XmlNodeAction> map_; std::vector<ActionFuncWithDiag> actions_; std::vector<ActionFuncWithDiag> actions_; Loading @@ -98,8 +90,7 @@ class XmlActionExecutor { * Execute the defined actions for this XmlResource. * Execute the defined actions for this XmlResource. * Returns true if all actions return true, otherwise returns false. * Returns true if all actions return true, otherwise returns false. */ */ bool Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, bool Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, XmlResource* doc) const; XmlResource* doc) const; private: private: std::map<std::string, XmlNodeAction> map_; std::map<std::string, XmlNodeAction> map_; Loading Loading
tools/aapt2/link/ManifestFixer_test.cpp +18 −0 Original line number Original line Diff line number Diff line Loading @@ -402,4 +402,22 @@ TEST_F(ManifestFixerTest, UsesFeatureMustHaveNameOrGlEsVersion) { EXPECT_EQ(nullptr, Verify(input)); 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 } // namespace aapt
tools/aapt2/xml/XmlActionExecutor.cpp +23 −26 Original line number Original line Diff line number Diff line Loading @@ -19,8 +19,7 @@ namespace aapt { namespace aapt { namespace xml { namespace xml { static bool wrapper_one(XmlNodeAction::ActionFunc& f, Element* el, static bool wrapper_one(XmlNodeAction::ActionFunc& f, Element* el, SourcePathDiagnostics*) { SourcePathDiagnostics*) { return f(el); return f(el); } } Loading @@ -47,8 +46,8 @@ static void PrintElementToDiagMessage(const Element* el, DiagMessage* msg) { *msg << el->name << ">"; *msg << el->name << ">"; } } bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag, SourcePathDiagnostics* diag, Element* el) const { Element* el) const { bool error = false; bool error = false; for (const ActionFuncWithDiag& action : actions_) { for (const ActionFuncWithDiag& action : actions_) { error |= !action(el, diag); error |= !action(el, diag); Loading @@ -56,13 +55,11 @@ bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, for (Element* child_el : el->GetChildElements()) { for (Element* child_el : el->GetChildElements()) { if (child_el->namespace_uri.empty()) { if (child_el->namespace_uri.empty()) { std::map<std::string, XmlNodeAction>::const_iterator iter = std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(child_el->name); map_.find(child_el->name); if (iter != map_.end()) { if (iter != map_.end()) { error |= !iter->second.Execute(policy, diag, child_el); error |= !iter->second.Execute(policy, diag, child_el); continue; continue; } } } if (policy == XmlActionExecutorPolicy::kWhitelist) { if (policy == XmlActionExecutorPolicy::kWhitelist) { DiagMessage error_msg(child_el->line_number); DiagMessage error_msg(child_el->line_number); Loading @@ -73,11 +70,12 @@ bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, error = true; error = true; } } } } } return !error; return !error; } } bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, IDiagnostics* diag, XmlResource* doc) const { XmlResource* doc) const { SourcePathDiagnostics source_diag(doc->file.source, diag); SourcePathDiagnostics source_diag(doc->file.source, diag); Element* el = FindRootElement(doc); Element* el = FindRootElement(doc); Loading @@ -90,12 +88,10 @@ bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, } } if (el->namespace_uri.empty()) { if (el->namespace_uri.empty()) { std::map<std::string, XmlNodeAction>::const_iterator iter = std::map<std::string, XmlNodeAction>::const_iterator iter = map_.find(el->name); map_.find(el->name); if (iter != map_.end()) { if (iter != map_.end()) { return iter->second.Execute(policy, &source_diag, el); return iter->second.Execute(policy, &source_diag, el); } } } if (policy == XmlActionExecutorPolicy::kWhitelist) { if (policy == XmlActionExecutorPolicy::kWhitelist) { DiagMessage error_msg(el->line_number); DiagMessage error_msg(el->line_number); Loading @@ -105,6 +101,7 @@ bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, source_diag.Error(error_msg); source_diag.Error(error_msg); return false; return false; } } } return true; return true; } } Loading
tools/aapt2/xml/XmlActionExecutor.h +8 −17 Original line number Original line Diff line number Diff line Loading @@ -31,17 +31,12 @@ namespace aapt { namespace xml { namespace xml { enum class XmlActionExecutorPolicy { enum class XmlActionExecutorPolicy { /** // Actions are run if elements are matched, errors occur only when actions return false. * Actions on run if elements are matched, errors occur only when actions * return false. */ kNone, kNone, /** // The actions defined must match and run. If an element is found that does * The actions defined must match and run. If an element is found that does // not match an action, an error occurs. * not match // Note: namespaced elements are always ignored. * an action, an error occurs. */ kWhitelist, kWhitelist, }; }; Loading @@ -52,14 +47,12 @@ enum class XmlActionExecutorPolicy { */ */ class XmlNodeAction { class XmlNodeAction { public: public: using ActionFuncWithDiag = using ActionFuncWithDiag = std::function<bool(Element*, SourcePathDiagnostics*)>; std::function<bool(Element*, SourcePathDiagnostics*)>; using ActionFunc = std::function<bool(Element*)>; using ActionFunc = std::function<bool(Element*)>; /** /** * Find or create a child XmlNodeAction that will be performed for the child * Find or create a child XmlNodeAction that will be performed for the child * element * element with the name `name`. * with the name `name`. */ */ XmlNodeAction& operator[](const std::string& name) { return map_[name]; } XmlNodeAction& operator[](const std::string& name) { return map_[name]; } Loading @@ -72,8 +65,7 @@ class XmlNodeAction { private: private: friend class XmlActionExecutor; friend class XmlActionExecutor; bool Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag, bool Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag, Element* el) const; Element* el) const; std::map<std::string, XmlNodeAction> map_; std::map<std::string, XmlNodeAction> map_; std::vector<ActionFuncWithDiag> actions_; std::vector<ActionFuncWithDiag> actions_; Loading @@ -98,8 +90,7 @@ class XmlActionExecutor { * Execute the defined actions for this XmlResource. * Execute the defined actions for this XmlResource. * Returns true if all actions return true, otherwise returns false. * Returns true if all actions return true, otherwise returns false. */ */ bool Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, bool Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, XmlResource* doc) const; XmlResource* doc) const; private: private: std::map<std::string, XmlNodeAction> map_; std::map<std::string, XmlNodeAction> map_; Loading