diff options
author | Mohammed Abdul Azeem <azeemmysore@gmail.com> | 2016-07-01 16:41:53 +0530 |
---|---|---|
committer | Michael Meeks <michael.meeks@collabora.com> | 2016-07-08 19:52:17 +0000 |
commit | 731be5ca597a5c83d5bb82478058458a3779345b (patch) | |
tree | d903117803d7a39a89ca1f4c523d471948dddaf2 /sax | |
parent | 1e0f1b62c3162b858a331ebf823b5c1b61a4cf4e (diff) |
GSOC: Added test case for mishandle of namespaces.
Added a test case for misuse of namespace declaration and
resolving an element name.
Change-Id: Iadda81a82353de45418fff6315c2d9b94626126a
Reviewed-on: https://gerrit.libreoffice.org/26661
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noelgrandin@gmail.com>
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
Diffstat (limited to 'sax')
-rw-r--r-- | sax/qa/cppunit/xmlimport.cxx | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/sax/qa/cppunit/xmlimport.cxx b/sax/qa/cppunit/xmlimport.cxx index bc2d70bf7a46..e5f858fc3054 100644 --- a/sax/qa/cppunit/xmlimport.cxx +++ b/sax/qa/cppunit/xmlimport.cxx @@ -347,6 +347,70 @@ void SAL_CALL TestLegacyDocumentHandler::endElement( const OUString& aName ) setString( getString() + aName ); } +class NSDocumentHandler : public cppu::WeakImplHelper< XDocumentHandler > +{ +private: + OUString resolveNamespace( const OUString& rName ); + OUString getNamespaceValue( const OUString& rNamespacePrefix ); + +public: + NSDocumentHandler() {} + + // XDocumentHandler + virtual void SAL_CALL startDocument() throw (SAXException, RuntimeException, exception) override {} + virtual void SAL_CALL endDocument() throw (SAXException, RuntimeException, exception) override {} + virtual void SAL_CALL startElement( const OUString& aName, const Reference< XAttributeList >& xAttribs ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL endElement( const OUString& /* aName */ ) throw (SAXException, RuntimeException, exception) override {} + virtual void SAL_CALL characters( const OUString& /* aChars */ ) throw (SAXException, RuntimeException, exception) override {} + virtual void SAL_CALL ignorableWhitespace( const OUString& /* aWhitespaces */ ) throw (SAXException, RuntimeException, exception) override {} + virtual void SAL_CALL processingInstruction( const OUString& /* aTarget */, const OUString& /* aData */ ) throw (SAXException, RuntimeException, exception) override {} + virtual void SAL_CALL setDocumentLocator( const Reference< XLocator >& /* xLocator */ ) throw (SAXException, RuntimeException, exception) override {} +}; + +void SAL_CALL NSDocumentHandler::startElement( const OUString& aName, const Reference< XAttributeList >&/* xAttribs */ ) + throw( SAXException, RuntimeException, exception ) +{ + if (! (aName == "office:document" || aName == "office:body" || aName == "office:text" || + aName == "text:p" || aName == "note:p") ) + CPPUNIT_ASSERT(false); + + OUString sResolvedName = resolveNamespace(aName); + if (! ( sResolvedName == "urn:oasis:names:tc:opendocument:xmlns:office:1.0:document" || + sResolvedName == "urn:oasis:names:tc:opendocument:xmlns:office:1.0:body" || + sResolvedName == "urn:oasis:names:tc:opendocument:xmlns:office:1.0:text" || + sResolvedName == "urn:oasis:names:tc:opendocument:xmlns:text:1.0:p") ) + CPPUNIT_ASSERT(false); +} + +OUString NSDocumentHandler::resolveNamespace( const OUString& aName ) +{ + int index; + if (( index = aName.indexOf( ':' )) > 0 ) + { + if ( aName.getLength() > index + 1 ) + { + OUString aAttributeName = getNamespaceValue( aName.copy( 0, index ) ); + aAttributeName += ":"; + aAttributeName += aName.copy( index + 1 ); + return aAttributeName; + } + } + return aName; +} + +OUString NSDocumentHandler::getNamespaceValue( const OUString& rNamespacePrefix ) +{ + OUString aNamespaceURI; + if (rNamespacePrefix == "office") + aNamespaceURI = "urn:oasis:names:tc:opendocument:xmlns:office:1.0"; + else if (rNamespacePrefix == "text") + aNamespaceURI = "urn:oasis:names:tc:opendocument:xmlns:text:1.0"; + else if (rNamespacePrefix == "note") + aNamespaceURI = "urn:oasis:names:tc:opendocument:xmlns:text:1.0"; + return aNamespaceURI; +} + + class XMLImportTest : public test::BootstrapFixture { private: @@ -365,9 +429,11 @@ public: XMLImportTest() : BootstrapFixture(true, false) {} void parse(); + void testIllegalNamespaceUse(); CPPUNIT_TEST_SUITE( XMLImportTest ); CPPUNIT_TEST( parse ); + CPPUNIT_TEST( testIllegalNamespaceUse ); CPPUNIT_TEST_SUITE_END(); }; @@ -425,6 +491,18 @@ void XMLImportTest::parse() } } +void XMLImportTest::testIllegalNamespaceUse() +{ + rtl::Reference< NSDocumentHandler > m_xNSDocumentHandler; + m_xNSDocumentHandler.set( new NSDocumentHandler() ); + m_xParser->setDocumentHandler( m_xNSDocumentHandler.get() ); + InputSource source; + source.sSystemId = "internal"; + + source.aInputStream = createStreamFromFile( m_sDirPath + "multiplepfx.xml" ); + m_xParser->parseStream(source); +} + CPPUNIT_TEST_SUITE_REGISTRATION( XMLImportTest ); } //namespace |