Kaydet (Commit) 8aab6cbb authored tarafından Miklos Vajna's avatar Miklos Vajna

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/30126Reviewed-by: 's avatarMiklos Vajna <vmiklos@collabora.co.uk>
Tested-by: 's avatarJenkins <ci@libreoffice.org>
üst 7999da76
# -*- 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:
...@@ -17,6 +17,7 @@ $(eval $(call gb_Module_add_targets,xmlsecurity,\ ...@@ -17,6 +17,7 @@ $(eval $(call gb_Module_add_targets,xmlsecurity,\
$(eval $(call gb_Module_add_slowcheck_targets,xmlsecurity,\ $(eval $(call gb_Module_add_slowcheck_targets,xmlsecurity,\
CppunitTest_xmlsecurity_signing \ CppunitTest_xmlsecurity_signing \
CppunitTest_xmlsecurity_pdfsigning \
)) ))
$(eval $(call gb_Module_add_l10n_targets,xmlsecurity,\ $(eval $(call gb_Module_add_l10n_targets,xmlsecurity,\
......
/* -*- 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: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment