From cce85f6b6691e4b1ddb81171e3ca5b5aeb71e6b9 Mon Sep 17 00:00:00 2001 From: Mattijs Korpershoek Date: Tue, 8 Apr 2014 14:10:03 +0200 Subject: Test-platform enable/disable xml validation BZ: 184054 test-platform has no way to tell to the parameter framework that it wants the parameter framework to validate xmls on loading. New command added to test-platform executable. setValidateSchemasOnStart true|false If set to true, the parameter framework will try to validate the .xmls with the .xsds located next to them. If set to false, it will never attempt to validate. SetValidateSchemas is false by default to allow backward compatibility. Note(a): This assumes that the script is able to find the .xsd files (which should be in /tmp/Schemas/). Change-Id: I7a5b0bc09d31e6647d8c631380d31503666e7e7b Signed-off-by: Mattijs Korpershoek --- parameter/Android.mk | 10 ++-- parameter/FileIncluderElementBuilder.h | 57 +++++++++++++++++++++++ parameter/ParameterMgr.cpp | 47 ++++++++++++++----- parameter/ParameterMgr.h | 25 ++++++++++ parameter/ParameterMgrPlatformConnector.cpp | 18 +++++++ parameter/XmlFileIncluderElement.cpp | 16 +++++-- parameter/XmlFileIncluderElement.h | 6 ++- parameter/include/ParameterMgrPlatformConnector.h | 18 ++++++- test/test-platform/TestPlatform.cpp | 12 +++++ tools/xmlGenerator/hostDomainGenerator.sh | 1 + 10 files changed, 185 insertions(+), 25 deletions(-) mode change 100755 => 100644 parameter/Android.mk create mode 100644 parameter/FileIncluderElementBuilder.h diff --git a/parameter/Android.mk b/parameter/Android.mk old mode 100755 new mode 100644 index dd0c71d..04e201c --- a/parameter/Android.mk +++ b/parameter/Android.mk @@ -141,7 +141,6 @@ common_c_includes := \ $(LOCAL_PATH)/../remote-processor/ common_shared_libraries := libicuuc -common_static_libraries := libxml2 ############################# # Target build @@ -166,7 +165,7 @@ LOCAL_C_INCLUDES += \ bionic/ LOCAL_SHARED_LIBRARIES := $(common_shared_libraries) libdl libstlport -LOCAL_STATIC_LIBRARIES := libxmlserializer libpfw_utility $(common_static_libraries) +LOCAL_STATIC_LIBRARIES := libxmlserializer libpfw_utility libxml2 include $(BUILD_SHARED_LIBRARY) @@ -189,7 +188,7 @@ LOCAL_C_INCLUDES += \ $(common_c_includes) LOCAL_SHARED_LIBRARIES := $(common_shared_libraries)-host -LOCAL_STATIC_LIBRARIES := libxmlserializer_host libpfw_utility_host $(common_static_libraries) +LOCAL_STATIC_LIBRARIES := libxmlserializer_host libpfw_utility_host libxml2-schemas LOCAL_LDLIBS += -ldl @@ -207,7 +206,7 @@ LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH) LOCAL_STATIC_LIBRARIES := \ libxmlserializer \ libpfw_utility \ - $(common_static_libraries) + libxml2 include $(BUILD_STATIC_LIBRARY) @@ -223,7 +222,6 @@ LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH) LOCAL_STATIC_LIBRARIES := \ libxmlserializer_host \ libpfw_utility_host \ - $(common_static_libraries) + libxml2 include $(BUILD_HOST_STATIC_LIBRARY) - diff --git a/parameter/FileIncluderElementBuilder.h b/parameter/FileIncluderElementBuilder.h new file mode 100644 index 0000000..bccb227 --- /dev/null +++ b/parameter/FileIncluderElementBuilder.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2014, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#pragma once + +#include "ElementBuilder.h" +#include "XmlFileIncluderElement.h" + +/** This is the XmlFileIncluderElement builder class + * + * It is builds the XmlFileIncluderElement with + * a mandatory option specific to xml: + * whether it should be validated with schemas or not + */ +class CFileIncluderElementBuilder : public CElementBuilder +{ +public: + CFileIncluderElementBuilder(bool bValidateWithSchemas) : + CElementBuilder(), + _bValidateWithSchemas(bValidateWithSchemas) + {} + + virtual CElement *createElement(const CXmlElement &xmlElement) const + { + return new CXmlFileIncluderElement(xmlElement.getNameAttribute(), + xmlElement.getType(), _bValidateWithSchemas); + } + +private: + bool _bValidateWithSchemas; +}; diff --git a/parameter/ParameterMgr.cpp b/parameter/ParameterMgr.cpp index 639c59f..2882f67 100644 --- a/parameter/ParameterMgr.cpp +++ b/parameter/ParameterMgr.cpp @@ -1,4 +1,4 @@ - /* +/* * Copyright (c) 2011-2014, Intel Corporation * All rights reserved. * @@ -36,8 +36,10 @@ #include "NamedElementBuilderTemplate.h" #include "KindElementBuilderTemplate.h" #include "ElementBuilderTemplate.h" +#include "XmlFileIncluderElement.h" #include "SelectionCriterionType.h" #include "SubsystemElementBuilder.h" +#include "FileIncluderElementBuilder.h" #include "SelectionCriteria.h" #include "ComponentType.h" #include "ComponentInstance.h" @@ -289,7 +291,9 @@ CParameterMgr::CParameterMgr(const string& strConfigurationFilePath) : _pLogger(NULL), _uiLogDepth(0), _bFailOnMissingSubsystem(true), - _bFailOnFailedSettingsLoad(true) + _bFailOnFailedSettingsLoad(true), + _bValidateSchemasOnStart(false) + { // Tuning Mode Mutex bzero(&_blackboardMutex, sizeof(_blackboardMutex)); @@ -301,9 +305,6 @@ CParameterMgr::CParameterMgr(const string& strConfigurationFilePath) : addChild(new CSystemClass); addChild(new CConfigurableDomains); - // Feed element library - feedElementLibraries(); - _pCommandHandler = new CCommandHandler(this); // Add command parsers @@ -409,6 +410,8 @@ bool CParameterMgr::load(string& strError) { CAutoLog autoLog(this, "Loading"); + feedElementLibraries(); + // Load Framework configuration if (!loadFrameworkConfiguration(strError)) { @@ -644,12 +647,16 @@ bool CParameterMgr::loadSettingsFromConfigFile(string& strError) bool CParameterMgr::xmlParse(CXmlElementSerializingContext& elementSerializingContext, CElement* pRootElement, const string& strXmlFilePath, const string& strXmlFolder, CParameterMgr::ElementLibrary eElementLibrary, const string& strNameAttrituteName) { // Init serializing context - elementSerializingContext.set(_pElementLibrarySet->getElementLibrary(eElementLibrary), strXmlFolder, _strSchemaFolderLocation); + elementSerializingContext.set(_pElementLibrarySet->getElementLibrary( + eElementLibrary), strXmlFolder, _strSchemaFolderLocation); // Get Schema file associated to root element string strXmlSchemaFilePath = _strSchemaFolderLocation + "/" + pRootElement->getKind() + ".xsd"; - CXmlFileDocSource fileDocSource(strXmlFilePath, strXmlSchemaFilePath, pRootElement->getKind(), pRootElement->getName(), strNameAttrituteName); + CXmlFileDocSource fileDocSource(strXmlFilePath, strXmlSchemaFilePath, + pRootElement->getKind(), + pRootElement->getName(), strNameAttrituteName, + _bValidateSchemasOnStart); // Start clean pRootElement->clean(); @@ -776,10 +783,22 @@ void CParameterMgr::setFailureOnFailedSettingsLoad(bool bFail) { _bFailOnFailedSettingsLoad = bFail; } + bool CParameterMgr::getFailureOnFailedSettingsLoad() { return _bFailOnFailedSettingsLoad; } + +void CParameterMgr::setValidateSchemasOnStart(bool bValidate) +{ + _bValidateSchemasOnStart = bValidate; +} + +bool CParameterMgr::getValidateSchemasOnStart() const +{ + return _bValidateSchemasOnStart; +} + /////////////////// Remote command parsers /// Version CParameterMgr::CCommandHandler::CommandStatus CParameterMgr::versionCommandProcess(const IRemoteCommand& remoteCommand, string& strResult) @@ -1999,14 +2018,16 @@ bool CParameterMgr::importDomainsXml(const string& strXmlSource, bool bWithSetti // when importing from a file strXmlSource is the file name pSource = new CXmlFileDocSource(strXmlSource, strXmlSchemaFilePath, pConfigurableDomains->getKind(), - pConfigurableDomains->getName(), "SystemClassName"); + pConfigurableDomains->getName(), "SystemClassName", + _bValidateSchemasOnStart); } else { // when importing from an xml string, strXmlSource contains the string pSource = new CXmlStringDocSource(strXmlSource, strXmlSchemaFilePath, pConfigurableDomains->getKind(), - pConfigurableDomains->getName(), "SystemClassName"); + pConfigurableDomains->getName(), "SystemClassName", + _bValidateSchemasOnStart); } // Start clean @@ -2063,7 +2084,8 @@ bool CParameterMgr::exportDomainsXml(string& strXmlDest, bool bWithSettings, boo // Use a doc source by loading data from instantiated Configurable Domains CXmlMemoryDocSource memorySource(pConfigurableDomains, pConfigurableDomains->getKind(), - strXmlSchemaFilePath, "parameter-framework", getVersion()); + strXmlSchemaFilePath, "parameter-framework", + getVersion(), _bValidateSchemasOnStart); // Xml Sink CXmlDocSink* pSink; @@ -2181,7 +2203,7 @@ void CParameterMgr::feedElementLibraries() pParameterCreationLibrary->addElementBuilder("EnumParameter", new TNamedElementBuilderTemplate()); pParameterCreationLibrary->addElementBuilder("ValuePair", new TElementBuilderTemplate()); pParameterCreationLibrary->addElementBuilder("FixedPointParameter", new TNamedElementBuilderTemplate()); - pParameterCreationLibrary->addElementBuilder("SubsystemInclude", new TKindElementBuilderTemplate()); + pParameterCreationLibrary->addElementBuilder("SubsystemInclude", new CFileIncluderElementBuilder(_bValidateSchemasOnStart)); _pElementLibrarySet->addElementLibrary(pParameterCreationLibrary); @@ -2338,7 +2360,8 @@ bool CParameterMgr::getSystemClassXMLString(string& strResult) CXmlSerializingContext xmlSerializingContext(strError); // Use a doc source by loading data from instantiated Configurable Domains - CXmlMemoryDocSource memorySource(pSystemClass, pSystemClass->getKind()); + CXmlMemoryDocSource memorySource(pSystemClass, pSystemClass->getKind(), + _bValidateSchemasOnStart); // Use a doc sink that write the doc data in a string CXmlStringDocSink stringSink(strResult); diff --git a/parameter/ParameterMgr.h b/parameter/ParameterMgr.h index bb38d22..7d64e5c 100644 --- a/parameter/ParameterMgr.h +++ b/parameter/ParameterMgr.h @@ -167,6 +167,24 @@ public: */ bool getFailureOnFailedSettingsLoad(); + /** Should .xml files be validated on start ? + * + * @param[in] bValidate: + * If set to true, parameterMgr will report an error + * when being unable to validate .xml files + * If set to false, no .xml/xsd validation will happen + * (default behaviour) + * + * @return false if unable to set, true otherwise. + */ + void setValidateSchemasOnStart(bool bValidate); + + /** Would .xml files be validated on start? + * + * @return areSchemasValidated + */ + bool getValidateSchemasOnStart() const; + //////////// Tuning ///////////// // Tuning mode bool setTuningMode(bool bOn, string& strError); @@ -508,5 +526,12 @@ private: * will continue the parameterMgr start with no domains. */ bool _bFailOnFailedSettingsLoad; + + /** + * If set to true, parameterMgr will report an error + * when being unable to validate .xml files + * If set to false, no .xml/xsd validation will happen (default behaviour) + */ + bool _bValidateSchemasOnStart; }; diff --git a/parameter/ParameterMgrPlatformConnector.cpp b/parameter/ParameterMgrPlatformConnector.cpp index 9ffde18..bcc9012 100644 --- a/parameter/ParameterMgrPlatformConnector.cpp +++ b/parameter/ParameterMgrPlatformConnector.cpp @@ -126,6 +126,24 @@ bool CParameterMgrPlatformConnector::getFailureOnFailedSettingsLoad() return _pParameterMgr->getFailureOnFailedSettingsLoad(); } +bool CParameterMgrPlatformConnector::setValidateSchemasOnStart( + bool bValidate, std::string& strError) +{ + if (_bStarted) { + + strError = "Can not enable xml validation after the start of the parameter-framework"; + return false; + } + + _pParameterMgr->setValidateSchemasOnStart(bValidate); + return true; +} + +bool CParameterMgrPlatformConnector::getValidateSchemasOnStart() +{ + return _pParameterMgr->getValidateSchemasOnStart(); +} + // Start bool CParameterMgrPlatformConnector::start(string& strError) { diff --git a/parameter/XmlFileIncluderElement.cpp b/parameter/XmlFileIncluderElement.cpp index 8543744..2e97bdb 100644 --- a/parameter/XmlFileIncluderElement.cpp +++ b/parameter/XmlFileIncluderElement.cpp @@ -36,9 +36,12 @@ #include #define base CKindElement - -CXmlFileIncluderElement::CXmlFileIncluderElement(const string& strName, const string& strKind) : base(strName, strKind) +CXmlFileIncluderElement::CXmlFileIncluderElement(const string& strName, + const string& strKind, + bool bValidateWithSchemas) : base(strName, + strKind) { + _bValidateSchemasOnStart = bValidateWithSchemas; } // From IXmlSink @@ -58,13 +61,18 @@ bool CXmlFileIncluderElement::fromXml(const CXmlElement& xmlElement, CXmlSeriali // Instantiate parser string strIncludedElementType = getIncludedElementType(); - { // Open a log section titled with loading file path CAutoLog autolog(this, "Loading " + strPath); // Use a doc source that load data from a file - CXmlFileDocSource fileDocSource(strPath, elementSerializingContext.getXmlSchemaPathFolder() + "/" + strIncludedElementType + ".xsd", strIncludedElementType); + string strPathToXsdFile = elementSerializingContext.getXmlSchemaPathFolder() + "/" + + strIncludedElementType + ".xsd"; + + CXmlFileDocSource fileDocSource(strPath, + strPathToXsdFile, + strIncludedElementType, + _bValidateSchemasOnStart); if (!fileDocSource.isParsable(elementSerializingContext)) { diff --git a/parameter/XmlFileIncluderElement.h b/parameter/XmlFileIncluderElement.h index 7471962..1a88ce2 100644 --- a/parameter/XmlFileIncluderElement.h +++ b/parameter/XmlFileIncluderElement.h @@ -35,11 +35,13 @@ class CXmlFileIncluderElement : public CKindElement { public: - CXmlFileIncluderElement(const string& strName, const string& strKind); - + CXmlFileIncluderElement(const string& strName, + const string& strKind, + bool bValidateWithSchemas); // From IXmlSink virtual bool fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext); private: // Element type string getIncludedElementType() const; + bool _bValidateSchemasOnStart; }; diff --git a/parameter/include/ParameterMgrPlatformConnector.h b/parameter/include/ParameterMgrPlatformConnector.h index 71819f9..d489545 100644 --- a/parameter/include/ParameterMgrPlatformConnector.h +++ b/parameter/include/ParameterMgrPlatformConnector.h @@ -114,6 +114,23 @@ public: */ bool getFailureOnFailedSettingsLoad(); + /** Should .xml files be validated on start ? + * + * @param[in] bValidate: + * If set to true, parameterMgr will abort when being unable to validate .xml files + * If set to false, no .xml/xsd validation will happen (default behaviour) + * @param[out] strResult a string containing the result of the command. + * + * @return false if unable to set, true otherwise. + */ + bool setValidateSchemasOnStart(bool bValidate, std::string &strError); + + /** Would .xml files be validated on start? + * + * @return areSchemasValidated + */ + bool getValidateSchemasOnStart(); + private: CParameterMgrPlatformConnector(const CParameterMgrPlatformConnector&); CParameterMgrPlatformConnector& operator=(const CParameterMgrPlatformConnector&); @@ -129,4 +146,3 @@ private: // Private logging CParameterMgrLogger* _pParameterMgrLogger; }; - diff --git a/test/test-platform/TestPlatform.cpp b/test/test-platform/TestPlatform.cpp index 6bf6350..aaa3d02 100644 --- a/test/test-platform/TestPlatform.cpp +++ b/test/test-platform/TestPlatform.cpp @@ -27,6 +27,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + #include #include #include @@ -120,6 +121,17 @@ CTestPlatform::CTestPlatform(const string& strClass, int iPortNumber, sem_t& exi 0, "", "Get policy for failed settings load, either abort start or continue without domains."); + _pCommandHandler->addCommandParser( + "setValidateSchemasOnStart", + &CTestPlatform::setter<&CParameterMgrPlatformConnector::setValidateSchemasOnStart>, + 1, "true|false", + "Set policy for schema validation based on .xsd files (false by default)."); + _pCommandHandler->addCommandParser( + "getValidateSchemasOnStart", + &CTestPlatform::getter<&CParameterMgrPlatformConnector::getValidateSchemasOnStart>, + 0, "", + "Get policy for schema validation based on .xsd files."); + // Create server _pRemoteProcessorServer = new CRemoteProcessorServer(iPortNumber, _pCommandHandler); diff --git a/tools/xmlGenerator/hostDomainGenerator.sh b/tools/xmlGenerator/hostDomainGenerator.sh index 7ee2966..47bfa82 100755 --- a/tools/xmlGenerator/hostDomainGenerator.sh +++ b/tools/xmlGenerator/hostDomainGenerator.sh @@ -191,6 +191,7 @@ launchTestPlatform () { $TPSendCommand setFailureOnMissingSubsystem false $TPSendCommand setFailureOnFailedSettingsLoad false + $TPSendCommand setValidateSchemasOnStart false echo "Asking test-platform (port $TPSocket) to start a new PFW instance (listening on port $PFWSocket) ..." $TPSendCommand start -- cgit v1.1