From c600065569bba3c2bbbaceba0b7998f8ad9ec8f9 Mon Sep 17 00:00:00 2001 From: Xisco Fauli Date: Tue, 17 Sep 2024 02:02:38 +0200 Subject: [PATCH] unoxml: move checkRDFa from Java to CppUnittest Change-Id: Ife5cd7398bdbacbd8189c8451dab6351590052e4 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/173484 Tested-by: Jenkins Reviewed-by: Xisco Fauli --- unoxml/CppunitTest_unoxml_rdftest.mk | 3 + .../qa/complex/unoxml/RDFRepositoryTest.java | 126 ---------------- unoxml/qa/unit/rdftest.cxx | 134 +++++++++++++++++- 3 files changed, 135 insertions(+), 128 deletions(-) diff --git a/unoxml/CppunitTest_unoxml_rdftest.mk b/unoxml/CppunitTest_unoxml_rdftest.mk index e4e12e9864d4..a42ba472f9be 100644 --- a/unoxml/CppunitTest_unoxml_rdftest.mk +++ b/unoxml/CppunitTest_unoxml_rdftest.mk @@ -27,8 +27,11 @@ $(eval $(call gb_CppunitTest_use_libraries,unoxml_rdftest, \ cppuhelper \ sal \ sax \ + subsequenttest \ test \ + tl \ unotest \ + utl \ )) $(eval $(call gb_CppunitTest_use_configuration,unoxml_rdftest)) diff --git a/unoxml/qa/complex/unoxml/RDFRepositoryTest.java b/unoxml/qa/complex/unoxml/RDFRepositoryTest.java index a24e159af40b..987e8dc5f5f7 100644 --- a/unoxml/qa/complex/unoxml/RDFRepositoryTest.java +++ b/unoxml/qa/complex/unoxml/RDFRepositoryTest.java @@ -436,101 +436,6 @@ public class RDFRepositoryTest } } - @Test public void checkRDFa() - { - try { - System.out.println("Checking RDFa gunk..."); - - String content = "behold, for I am the content."; - XTextRange xTR = new TestRange(content); - XMetadatable xM = (XMetadatable) xTR; - - Pair result = - xRep.getStatementRDFa((XMetadatable)xTR); - assertTrue("RDFa: get: not empty (initial)", - 0 == result.First.length); - - try { - xRep.setStatementRDFa(foo, new XURI[] {}, xM, "", null); - fail("RDFa: set: no predicate"); - } catch (IllegalArgumentException e) { - // ignore - } - - try { - xRep.setStatementRDFa(foo, new XURI[] {bar}, null, "", null); - fail("RDFa: set: null"); - } catch (IllegalArgumentException e) { - // ignore - } - - XLiteral trlit = Literal.create(xContext, content); - Statement x_FooBarTRLit = new Statement(foo, bar, trlit, null); - xRep.setStatementRDFa(foo, new XURI[] { bar }, xM, "", null); - - result = xRep.getStatementRDFa((XMetadatable)xTR); - assertTrue("RDFa: get: without content", - !result.Second && (1 == result.First.length) - && eq(result.First[0], x_FooBarTRLit)); - - //FIXME: do this? - xTR.setString(lit.getStringValue()); -/* - Statement xFooBarLit = new Statement(foo, bar, lit, null); - result = xRep.getStatementRDFa((XMetadatable)xTR); - assertTrue("RDFa: get: change", - eq((Statement)result.First, xFooBarLit) && null == result.Second); -*/ - - Statement x_FooBarLittype = new Statement(foo, bar, littype, null); - xRep.setStatementRDFa(foo, new XURI[] { bar }, xM, "42", uint); - - result = xRep.getStatementRDFa((XMetadatable)xTR); - assertTrue("RDFa: get: with content", - result.Second && - (1 == result.First.length) && - eq(result.First[0], x_FooBarLittype)); - - //FIXME: do this? - xTR.setString(content); -/* - Statement xFooLabelTRLit = new Statement(foo, rdfslabel, trlit, null); - result = xRep.getStatementRDFa((XMetadatable)xTR); - assertTrue("RDFa: get: change (label)", - eq((Statement)result.First, xFooBarLittype) && - eq((Statement)result.Second, xFooLabelTRLit)); -*/ - - xRep.removeStatementRDFa((XMetadatable)xTR); - - result = xRep.getStatementRDFa((XMetadatable)xTR); - assertTrue("RDFa: get: not empty (removed)", - 0 == result.First.length); - - xRep.setStatementRDFa(foo, new XURI[] { foo, bar, baz }, xM, - "", null); - - Statement x_FooFooTRLit = new Statement(foo, foo, trlit, null); - Statement x_FooBazTRLit = new Statement(foo, baz, trlit, null); - result = xRep.getStatementRDFa((XMetadatable) xTR); - assertTrue("RDFa: get: without content (multiple predicates, reinsert)", - !result.Second && - eq(result.First, new Statement[] { - x_FooFooTRLit, x_FooBarTRLit, x_FooBazTRLit })); - - xRep.removeStatementRDFa((XMetadatable)xTR); - - result = xRep.getStatementRDFa((XMetadatable) xTR); - assertTrue("RDFa: get: not empty (re-removed)", - 0 == result.First.length); - - System.out.println("...done"); - - } catch (Exception e) { - report(e); - } - } - // utilities ------------------------------------------------------------- public void report(Exception e) { @@ -776,37 +681,6 @@ public class RDFRepositoryTest } } - private class TestRange implements XTextRange, XMetadatable, XServiceInfo - { - private String m_Stream; - String m_XmlId; - String m_Text; - TestRange(String i_Str) { m_Text = i_Str; } - - public String getStringValue() { return ""; } - public String getNamespace() { return ""; } - public String getLocalName() { return ""; } - - public StringPair getMetadataReference() - { return new StringPair(m_Stream, m_XmlId); } - public void setMetadataReference(StringPair i_Ref) - throws IllegalArgumentException - { m_Stream = i_Ref.First; m_XmlId = i_Ref.Second; } - public void ensureMetadataReference() - { m_Stream = "content.xml"; m_XmlId = "42"; } - - public String getImplementationName() { return null; } - public String[] getSupportedServiceNames() { return null; } - public boolean supportsService(String i_Svc) - { return i_Svc.equals("com.sun.star.text.Paragraph"); } - - public XText getText() { return null; } - public XTextRange getStart() { return null; } - public XTextRange getEnd() { return null; } - public String getString() { return m_Text; } - public void setString(String i_Str) { m_Text = i_Str; } - } - private XMultiServiceFactory getMSF() { return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); diff --git a/unoxml/qa/unit/rdftest.cxx b/unoxml/qa/unit/rdftest.cxx index 4595d78f5215..7e3b93e60875 100644 --- a/unoxml/qa/unit/rdftest.cxx +++ b/unoxml/qa/unit/rdftest.cxx @@ -7,7 +7,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include +#include #include @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -25,8 +26,13 @@ using namespace com::sun::star; namespace { -class RDFStreamTest : public test::BootstrapFixture +class RDFStreamTest : public UnoApiTest { +public: + RDFStreamTest() + : UnoApiTest(u"/unoxml/qa/unit/data/"_ustr) + { + } }; CPPUNIT_TEST_FIXTURE(RDFStreamTest, testCVE_2012_0037) @@ -60,6 +66,130 @@ CPPUNIT_TEST_FIXTURE(RDFStreamTest, testCVE_2012_0037) CPPUNIT_ASSERT_EQUAL(OUString("uri:bar"), aStatement.Predicate->getStringValue()); CPPUNIT_ASSERT_EQUAL(OUString("EVIL"), aStatement.Object->getStringValue()); } + +CPPUNIT_TEST_FIXTURE(RDFStreamTest, testRDFa) +{ + mxComponent = loadFromDesktop(u"private:factory/swriter"_ustr); + uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference xParaEnumAccess(xTextDocument->getText(), + uno::UNO_QUERY); + uno::Reference xParaEnum = xParaEnumAccess->createEnumeration(); + uno::Reference xTextRange(xParaEnum->nextElement(), uno::UNO_QUERY); + + xTextRange->setString(u"behold, for I am the content."_ustr); + + uno::Reference xDocumentMetadataAccess(mxComponent, + uno::UNO_QUERY); + uno::Reference xRepo = xDocumentMetadataAccess->getRDFRepository(); + uno::Reference xDocRepo(xRepo, uno::UNO_QUERY); + CPPUNIT_ASSERT(xDocRepo); + + uno::Reference xMeta(xTextRange, uno::UNO_QUERY); + + // 1. RDFa: get: not empty (initial) + ::beans::Pair, sal_Bool> xResult + = xDocRepo->getStatementRDFa(xMeta); + CPPUNIT_ASSERT_EQUAL(sal_uInt32(0), xResult.First.size()); + CPPUNIT_ASSERT(!xResult.Second); + + uno::Reference xContext = getComponentContext(); + uno::Reference xFoo = rdf::URI::create(xContext, "uri:foo"); + uno::Reference xBar = rdf::URI::create(xContext, "uri:bar"); + uno::Reference xBaz = rdf::URI::create(xContext, "uri:baz"); + uno::Reference xInt = rdf::URI::create(xContext, "uri:int"); + css::uno::Sequence> xURI{}; + + // 2. RDFa: set: no predicate + try + { + xDocRepo->setStatementRDFa(xFoo, xURI, xMeta, OUString(), nullptr); + CPPUNIT_FAIL("expected IllegalArgumentException"); + } + catch (css::lang::IllegalArgumentException&) + { + } + + css::uno::Sequence> xURI2{ xBar }; + + // 3. RDFa: set: null + try + { + xDocRepo->setStatementRDFa(xFoo, xURI2, nullptr, OUString(), nullptr); + CPPUNIT_FAIL("expected IllegalArgumentException"); + } + catch (css::lang::IllegalArgumentException&) + { + } + + xDocRepo->setStatementRDFa(xFoo, xURI2, xMeta, OUString(), nullptr); + + // 4. RDFa: get: without content + xResult = xDocRepo->getStatementRDFa(xMeta); + CPPUNIT_ASSERT_EQUAL(sal_uInt32(1), xResult.First.size()); + CPPUNIT_ASSERT(!xResult.Second); + + rdf::Statement aStatement = xResult.First[0]; + CPPUNIT_ASSERT_EQUAL(OUString("uri:foo"), aStatement.Subject->getStringValue()); + CPPUNIT_ASSERT_EQUAL(OUString("uri:bar"), aStatement.Predicate->getStringValue()); + CPPUNIT_ASSERT_EQUAL(OUString("behold, for I am the content."), + aStatement.Object->getStringValue()); + + xDocRepo->setStatementRDFa(xFoo, xURI2, xMeta, u"42"_ustr, xInt); + + // 5. RDFa: get: with content + xResult = xDocRepo->getStatementRDFa(xMeta); + CPPUNIT_ASSERT_EQUAL(sal_uInt32(1), xResult.First.size()); + CPPUNIT_ASSERT(xResult.Second); + + aStatement = xResult.First[0]; + CPPUNIT_ASSERT_EQUAL(OUString("uri:foo"), aStatement.Subject->getStringValue()); + CPPUNIT_ASSERT_EQUAL(OUString("uri:bar"), aStatement.Predicate->getStringValue()); + + uno::Reference xLitType + = rdf::Literal::createWithType(xContext, "42", xInt); + CPPUNIT_ASSERT_EQUAL(xLitType->getStringValue(), aStatement.Object->getStringValue()); + + xDocRepo->removeStatementRDFa(xMeta); + + // 6. RDFa: get: not empty (removed) + xResult = xDocRepo->getStatementRDFa(xMeta); + CPPUNIT_ASSERT_EQUAL(sal_uInt32(0), xResult.First.size()); + CPPUNIT_ASSERT(xResult.Second); + + css::uno::Sequence> xURI3{ xFoo, xBar, xBaz }; + + xDocRepo->setStatementRDFa(xFoo, xURI3, xMeta, OUString(), nullptr); + + // 7. RDFa: get: without content (multiple predicates, reinsert) + xResult = xDocRepo->getStatementRDFa(xMeta); + CPPUNIT_ASSERT_EQUAL(sal_uInt32(3), xResult.First.size()); + CPPUNIT_ASSERT(!xResult.Second); + + aStatement = xResult.First[0]; + CPPUNIT_ASSERT_EQUAL(OUString("uri:foo"), aStatement.Subject->getStringValue()); + CPPUNIT_ASSERT_EQUAL(OUString("uri:baz"), aStatement.Predicate->getStringValue()); + CPPUNIT_ASSERT_EQUAL(OUString("behold, for I am the content."), + aStatement.Object->getStringValue()); + + aStatement = xResult.First[1]; + CPPUNIT_ASSERT_EQUAL(OUString("uri:foo"), aStatement.Subject->getStringValue()); + CPPUNIT_ASSERT_EQUAL(OUString("uri:bar"), aStatement.Predicate->getStringValue()); + CPPUNIT_ASSERT_EQUAL(OUString("behold, for I am the content."), + aStatement.Object->getStringValue()); + + aStatement = xResult.First[2]; + CPPUNIT_ASSERT_EQUAL(OUString("uri:foo"), aStatement.Subject->getStringValue()); + CPPUNIT_ASSERT_EQUAL(OUString("uri:foo"), aStatement.Predicate->getStringValue()); + CPPUNIT_ASSERT_EQUAL(OUString("behold, for I am the content."), + aStatement.Object->getStringValue()); + + xDocRepo->removeStatementRDFa(xMeta); + + // 8. RDFa: get: not empty (re-removed) + xResult = xDocRepo->getStatementRDFa(xMeta); + CPPUNIT_ASSERT_EQUAL(sal_uInt32(0), xResult.First.size()); + CPPUNIT_ASSERT(!xResult.Second); +} } CPPUNIT_PLUGIN_IMPLEMENT();