[Libreoffice-commits] core.git: xmlsecurity/CppunitTest_xmlsecurity_pdfsigning.mk xmlsecurity/Module_xmlsecurity.mk xmlsecurity/qa

Miklos Vajna vmiklos at collabora.co.uk
Fri Oct 21 12:05:38 UTC 2016


 xmlsecurity/CppunitTest_xmlsecurity_pdfsigning.mk |   55 ++++++++++
 xmlsecurity/Module_xmlsecurity.mk                 |    1 
 xmlsecurity/qa/unit/pdfsigning/data/cert8.db      |binary
 xmlsecurity/qa/unit/pdfsigning/data/key3.db       |binary
 xmlsecurity/qa/unit/pdfsigning/data/no.pdf        |binary
 xmlsecurity/qa/unit/pdfsigning/pdfsigning.cxx     |  111 ++++++++++++++++++++++
 6 files changed, 167 insertions(+)

New commits:
commit 8aab6cbba07106c97f0457c7a3c9df8f20367d20
Author: Miklos Vajna <vmiklos at collabora.co.uk>
Date:   Fri Oct 21 11:48:47 2016 +0200

    xmlsecurity: add PDF sign unit-test
    
    If the NSS binary files ever need re-generating, it's just a copy from a
    fresh Firefox user profile, after importing
    'example-xmlsecurity-Alice.cert.p12', as produced by
    './create-certs.sh'.
    
    Change-Id: Iabe15a816cb674d1691e3ff3e10aa2bd851f8e0d
    Reviewed-on: https://gerrit.libreoffice.org/30126
    Reviewed-by: Miklos Vajna <vmiklos at collabora.co.uk>
    Tested-by: Jenkins <ci at libreoffice.org>

diff --git a/xmlsecurity/CppunitTest_xmlsecurity_pdfsigning.mk b/xmlsecurity/CppunitTest_xmlsecurity_pdfsigning.mk
new file mode 100644
index 0000000..be4828e
--- /dev/null
+++ b/xmlsecurity/CppunitTest_xmlsecurity_pdfsigning.mk
@@ -0,0 +1,55 @@
+# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
+#*************************************************************************
+#
+# This file is part of the LibreOffice project.
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+#*************************************************************************
+
+$(eval $(call gb_CppunitTest_CppunitTest,xmlsecurity_pdfsigning))
+
+$(eval $(call gb_CppunitTest_add_exception_objects,xmlsecurity_pdfsigning, \
+	xmlsecurity/qa/unit/pdfsigning/pdfsigning \
+))
+
+$(eval $(call gb_CppunitTest_use_libraries,xmlsecurity_pdfsigning, \
+	comphelper \
+	cppuhelper \
+	cppu \
+	sal \
+	sax \
+	sfx \
+	test \
+	tl \
+	unotest \
+	utl \
+	xmlsecurity \
+	$(gb_UWINAPI) \
+))
+
+$(eval $(call gb_CppunitTest_use_externals,xmlsecurity_pdfsigning,\
+    boost_headers \
+))
+
+$(eval $(call gb_CppunitTest_set_include,xmlsecurity_pdfsigning,\
+	-I$(SRCDIR)/xmlsecurity/inc \
+	$$(INCLUDE) \
+))
+
+$(eval $(call gb_CppunitTest_use_sdk_api,xmlsecurity_pdfsigning))
+
+$(eval $(call gb_CppunitTest_use_ure,xmlsecurity_pdfsigning))
+$(eval $(call gb_CppunitTest_use_vcl,xmlsecurity_pdfsigning))
+
+$(eval $(call gb_CppunitTest_use_rdb,xmlsecurity_pdfsigning,services))
+
+$(eval $(call gb_CppunitTest_use_configuration,xmlsecurity_pdfsigning))
+
+ifeq ($(ENABLE_PDFIMPORT),TRUE)
+$(eval $(call gb_CppunitTest_use_executable,xmlsecurity_pdfsigning,xpdfimport))
+endif
+
+# vim: set noet sw=4 ts=4:
diff --git a/xmlsecurity/Module_xmlsecurity.mk b/xmlsecurity/Module_xmlsecurity.mk
index 04d1087..6fdc86e 100644
--- a/xmlsecurity/Module_xmlsecurity.mk
+++ b/xmlsecurity/Module_xmlsecurity.mk
@@ -17,6 +17,7 @@ $(eval $(call gb_Module_add_targets,xmlsecurity,\
 
 $(eval $(call gb_Module_add_slowcheck_targets,xmlsecurity,\
     CppunitTest_xmlsecurity_signing \
+    CppunitTest_xmlsecurity_pdfsigning \
 ))
 
 $(eval $(call gb_Module_add_l10n_targets,xmlsecurity,\
diff --git a/xmlsecurity/qa/unit/pdfsigning/data/cert8.db b/xmlsecurity/qa/unit/pdfsigning/data/cert8.db
new file mode 100644
index 0000000..8354fd3
Binary files /dev/null and b/xmlsecurity/qa/unit/pdfsigning/data/cert8.db differ
diff --git a/xmlsecurity/qa/unit/pdfsigning/data/key3.db b/xmlsecurity/qa/unit/pdfsigning/data/key3.db
new file mode 100644
index 0000000..8ab32c2
Binary files /dev/null and b/xmlsecurity/qa/unit/pdfsigning/data/key3.db differ
diff --git a/xmlsecurity/qa/unit/pdfsigning/data/no.pdf b/xmlsecurity/qa/unit/pdfsigning/data/no.pdf
new file mode 100644
index 0000000..5d55242
Binary files /dev/null and b/xmlsecurity/qa/unit/pdfsigning/data/no.pdf differ
diff --git a/xmlsecurity/qa/unit/pdfsigning/pdfsigning.cxx b/xmlsecurity/qa/unit/pdfsigning/pdfsigning.cxx
new file mode 100644
index 0000000..91a72b1
--- /dev/null
+++ b/xmlsecurity/qa/unit/pdfsigning/pdfsigning.cxx
@@ -0,0 +1,111 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <com/sun/star/xml/crypto/SEInitializer.hpp>
+
+#include <comphelper/processfactory.hxx>
+#include <osl/file.hxx>
+#include <test/bootstrapfixture.hxx>
+
+#include <pdfio/pdfdocument.hxx>
+
+using namespace com::sun::star;
+
+namespace
+{
+const char* DATA_DIRECTORY = "/xmlsecurity/qa/unit/pdfsigning/data/";
+}
+
+/// Testsuite for the PDF signing feature.
+class PDFSigningTest : public test::BootstrapFixture
+{
+    uno::Reference<uno::XComponentContext> mxComponentContext;
+
+public:
+    PDFSigningTest();
+    void setUp() override;
+
+    /// Test adding a new signature to a previously unsigned file.
+    void testPDFAdd();
+
+    CPPUNIT_TEST_SUITE(PDFSigningTest);
+    CPPUNIT_TEST(testPDFAdd);
+    CPPUNIT_TEST_SUITE_END();
+};
+
+PDFSigningTest::PDFSigningTest()
+{
+}
+
+void PDFSigningTest::setUp()
+{
+    test::BootstrapFixture::setUp();
+
+    mxComponentContext.set(comphelper::getComponentContext(getMultiServiceFactory()));
+
+#ifndef _WIN32
+    // Set up cert8.db and key3.db in workdir/CppunitTest/
+    OUString aSourceDir = m_directories.getURLFromSrc(DATA_DIRECTORY);
+    OUString aTargetDir = m_directories.getURLFromWorkdir(
+                              "/CppunitTest/xmlsecurity_signing.test.user/");
+    osl::File::copy(aSourceDir + "cert8.db", aTargetDir + "cert8.db");
+    osl::File::copy(aSourceDir + "key3.db", aTargetDir + "key3.db");
+    OUString aTargetPath;
+    osl::FileBase::getSystemPathFromFileURL(aTargetDir, aTargetPath);
+    setenv("MOZILLA_CERTIFICATE_FOLDER", aTargetPath.toUtf8().getStr(), 1);
+#endif
+}
+
+void PDFSigningTest::testPDFAdd()
+{
+#ifndef _WIN32
+    // Make sure that no.pdf has no signatures.
+    uno::Reference<xml::crypto::XSEInitializer> xSEInitializer = xml::crypto::SEInitializer::create(mxComponentContext);
+    uno::Reference<xml::crypto::XXMLSecurityContext> xSecurityContext = xSEInitializer->createSecurityContext(OUString());
+    xmlsecurity::pdfio::PDFDocument aDocument;
+    {
+        OUString aSourceDir = m_directories.getURLFromSrc(DATA_DIRECTORY);
+        OUString aInURL = aSourceDir + "no.pdf";
+        SvFileStream aStream(aInURL, StreamMode::READ);
+        CPPUNIT_ASSERT(aDocument.Read(aStream));
+        std::vector<xmlsecurity::pdfio::PDFObjectElement*> aSignatures = aDocument.GetSignatureWidgets();
+        CPPUNIT_ASSERT(aSignatures.empty());
+    }
+
+    // Sign it and write out the result as add.pdf.
+    OUString aTargetDir = m_directories.getURLFromWorkdir("/CppunitTest/xmlsecurity_signing.test.user/");
+    OUString aOutURL = aTargetDir + "add.pdf";
+    {
+        uno::Reference<xml::crypto::XSecurityEnvironment> xSecurityEnvironment = xSecurityContext->getSecurityEnvironment();
+        uno::Sequence<uno::Reference<security::XCertificate>> aCertificates = xSecurityEnvironment->getPersonalCertificates();
+        CPPUNIT_ASSERT(aCertificates.hasElements());
+        CPPUNIT_ASSERT(aDocument.Sign(aCertificates[0], "test"));
+        SvFileStream aOutStream(aOutURL, StreamMode::WRITE | StreamMode::TRUNC);
+        CPPUNIT_ASSERT(aDocument.Write(aOutStream));
+    }
+
+    // Read back the signed pdf and make sure that it has one valid signature.
+    {
+        SvFileStream aStream(aOutURL, StreamMode::READ);
+        xmlsecurity::pdfio::PDFDocument aVerifyDocument;
+        CPPUNIT_ASSERT(aVerifyDocument.Read(aStream));
+        std::vector<xmlsecurity::pdfio::PDFObjectElement*> aSignatures = aVerifyDocument.GetSignatureWidgets();
+        // This was 0 when PDFDocument::Sign() silently returned success, without doing anything.
+        CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aSignatures.size());
+        SignatureInformation aInfo(0);
+        CPPUNIT_ASSERT(xmlsecurity::pdfio::PDFDocument::ValidateSignature(aStream, aSignatures[0], aInfo));
+    }
+#endif
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(PDFSigningTest);
+
+CPPUNIT_PLUGIN_IMPLEMENT();
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list