diff options
author | Miklos Vajna <vmiklos@collabora.co.uk> | 2016-02-15 17:05:00 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.co.uk> | 2016-02-15 17:24:47 +0100 |
commit | 0d6328164e32dc8b5cd47e27bfcc2e2f6f906799 (patch) | |
tree | 1d8a445d903b84cbaa718b0e1b59c5f0d8efb80d /xmlsecurity | |
parent | 565346a4fe0a074807381c2d8ea48242e9e69f4f (diff) |
CppunitTest_xmlsecurity_signing: use DocumentSignatureManager
Change-Id: I97acb4a2e9aedb15671d65f6995faeab4ff5716f
Diffstat (limited to 'xmlsecurity')
-rw-r--r-- | xmlsecurity/qa/unit/signing/signing.cxx | 84 |
1 files changed, 20 insertions, 64 deletions
diff --git a/xmlsecurity/qa/unit/signing/signing.cxx b/xmlsecurity/qa/unit/signing/signing.cxx index 7d857505f7d4..6f2249cc2543 100644 --- a/xmlsecurity/qa/unit/signing/signing.cxx +++ b/xmlsecurity/qa/unit/signing/signing.cxx @@ -18,6 +18,7 @@ #include <com/sun/star/security/SerialNumberAdapter.hpp> #include <com/sun/star/xml/crypto/SEInitializer.hpp> #include <com/sun/star/io/TempFile.hpp> +#include <com/sun/star/packages/manifest/ManifestReader.hpp> #include <comphelper/processfactory.hxx> #include <sax/tools/converter.hxx> @@ -33,6 +34,7 @@ #include <xmlsecurity/documentsignaturehelper.hxx> #include <xmlsecurity/xmlsignaturehelper.hxx> +#include <documentsignaturemanager.hxx> using namespace com::sun::star; @@ -67,8 +69,6 @@ public: private: void createDoc(const OUString& rURL = OUString()); uno::Reference<security::XCertificate> getCertificate(XMLSignatureHelper& rSignatureHelper); - void sign(utl::TempFile& rTempFile, XMLSignatureHelper& rSignatureHelper, const uno::Reference<io::XOutputStream>& xOutputStream); - std::vector<SignatureInformation> verify(XMLSignatureHelper& rSignatureHelper, const uno::Reference<io::XInputStream>& xInputStream); }; SigningTest::SigningTest() @@ -119,58 +119,11 @@ uno::Reference<security::XCertificate> SigningTest::getCertificate(XMLSignatureH return xSecurityEnvironment->createCertificateFromAscii(aCertificate); } -void SigningTest::sign(utl::TempFile& rTempFile, XMLSignatureHelper& rSignatureHelper, const uno::Reference<io::XOutputStream>& xOutputStream) -{ - CPPUNIT_ASSERT(rSignatureHelper.Init()); - - SvStream* pStream = utl::UcbStreamHelper::CreateStream(rTempFile.GetURL(), StreamMode::READ); - uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(*pStream)); - uno::Reference<embed::XStorage> xStorage = comphelper::OStorageHelper::GetStorageOfFormatFromStream(ZIP_STORAGE_FORMAT_STRING, xStream); - CPPUNIT_ASSERT(xStorage.is()); - - rSignatureHelper.SetStorage(xStorage, "1.2"); - uno::Reference<security::XCertificate> xCertificate = getCertificate(rSignatureHelper); - CPPUNIT_ASSERT(xCertificate.is()); - - uno::Reference<security::XSerialNumberAdapter> xSerialNumberAdapter = security::SerialNumberAdapter::create(mxComponentContext); - OUString aCertSerial = xSerialNumberAdapter->toString(xCertificate->getSerialNumber()); - CPPUNIT_ASSERT(!aCertSerial.isEmpty()); - - rSignatureHelper.StartMission(); - sal_Int32 nSecurityId = rSignatureHelper.GetNewSecurityId(); - OUStringBuffer aStrBuffer; - sax::Converter::encodeBase64(aStrBuffer, xCertificate->getEncoded()); - rSignatureHelper.SetX509Certificate(nSecurityId, xCertificate->getIssuerName(), aCertSerial, aStrBuffer.makeStringAndClear()); - DocumentSignatureMode eSignatureMode = SignatureModeDocumentContent; - std::vector<OUString> aElements = DocumentSignatureHelper::CreateElementList(xStorage, eSignatureMode, OOo3_2Document); - for (size_t i = 0; i < aElements.size(); ++i) - { - bool bBinaryMode = aElements[i].startsWith("Thumbnails/"); - rSignatureHelper.AddForSigning(nSecurityId, aElements[i], aElements[i], bBinaryMode); - } - rSignatureHelper.SetDateTime(nSecurityId, Date(Date::SYSTEM), tools::Time(tools::Time::SYSTEM)); - rSignatureHelper.SetDescription(nSecurityId, "SigningTest::sign"); - - uno::Reference<xml::sax::XWriter> xWriter = rSignatureHelper.CreateDocumentHandlerWithHeader(xOutputStream); - uno::Reference<xml::sax::XDocumentHandler> xDocumentHandler(xWriter, uno::UNO_QUERY); - CPPUNIT_ASSERT(rSignatureHelper.CreateAndWriteSignature(xDocumentHandler)); - XMLSignatureHelper::CloseDocumentHandler(xDocumentHandler); - - rSignatureHelper.EndMission(); -} - -std::vector<SignatureInformation> SigningTest::verify(XMLSignatureHelper& rSignatureHelper, const uno::Reference<io::XInputStream>& xInputStream) -{ - rSignatureHelper.StartMission(); - rSignatureHelper.ReadAndVerifySignature(xInputStream); - rSignatureHelper.EndMission(); - return rSignatureHelper.GetSignatureInformations(); -} - void SigningTest::testDescription() { - // Create an empty document and store it to a tempfile. + // Create an empty document and store it to a tempfile, finally load it as a storage. createDoc(); + utl::TempFile aTempFile; aTempFile.EnableKillingFile(); uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY); @@ -178,22 +131,25 @@ void SigningTest::testDescription() aMediaDescriptor["FilterName"] <<= OUString("writer8"); xStorable->storeAsURL(aTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList()); - // Then sign the document, and store the signature it a second tempfile, so we don't write to the original storage when we read it as well. - XMLSignatureHelper aSignatureHelper(mxComponentContext); - uno::Reference<io::XStream> xSignatureStream(io::TempFile::create(mxComponentContext), uno::UNO_QUERY); - uno::Reference<io::XOutputStream> xOutputStream(xSignatureStream, uno::UNO_QUERY); - sign(aTempFile, aSignatureHelper, xOutputStream); + DocumentSignatureManager aManager(mxComponentContext, SignatureModeDocumentContent); + CPPUNIT_ASSERT(aManager.maSignatureHelper.Init()); + uno::Reference <embed::XStorage> xStorage = comphelper::OStorageHelper::GetStorageOfFormatFromURL(ZIP_STORAGE_FORMAT_STRING, aTempFile.GetURL(), embed::ElementModes::READWRITE); + CPPUNIT_ASSERT(xStorage.is()); + aManager.mxStore = xStorage; + aManager.maSignatureHelper.SetStorage(xStorage, "1.2"); - // Go back to the start of the signature. - uno::Reference<io::XSeekable> xSeekable(xSignatureStream, uno::UNO_QUERY); - CPPUNIT_ASSERT(xSeekable->getPosition() != 0); - xSeekable->seek(0); + // Then add a signature document. + uno::Reference<security::XCertificate> xCertificate = getCertificate(aManager.maSignatureHelper); + CPPUNIT_ASSERT(xCertificate.is()); + OUString aDescription("SigningTest::testDescription"); + sal_Int32 nSecurityId; + aManager.add(xCertificate, aDescription, nSecurityId); // Read back the signature and make sure that the description survives the roundtrip. - uno::Reference<io::XInputStream> xInputStream(xSignatureStream, uno::UNO_QUERY); - std::vector<SignatureInformation> aSignatureInformations = verify(aSignatureHelper, xInputStream); - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aSignatureInformations.size()); - CPPUNIT_ASSERT_EQUAL(OUString("SigningTest::sign"), aSignatureInformations[0].ouDescription); + aManager.read(/*bUseTempStream=*/true); + std::vector<SignatureInformation>& rInformations = aManager.maCurrentSignatureInformations; + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rInformations.size()); + CPPUNIT_ASSERT_EQUAL(aDescription, rInformations[0].ouDescription); } void SigningTest::testOOXMLPartial() |