From 731be5ca597a5c83d5bb82478058458a3779345b Mon Sep 17 00:00:00 2001 From: Mohammed Abdul Azeem Date: Fri, 1 Jul 2016 16:41:53 +0530 Subject: 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 Reviewed-by: Noel Grandin Reviewed-by: Michael Meeks --- sax/qa/cppunit/xmlimport.cxx | 78 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'sax/qa/cppunit') 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 -- cgit