Fix the tests failing when invalid certificate is in cert store

Change-Id: I72055c814a11a76c4934610bcad9c24aae21164e
diff --git a/filter/qa/pdf.cxx b/filter/qa/pdf.cxx
index 8eb64b0..7968c3d 100644
--- a/filter/qa/pdf.cxx
+++ b/filter/qa/pdf.cxx
@@ -19,6 +19,7 @@
#include <com/sun/star/graphic/XGraphic.hpp>

#include <comphelper/propertyvalue.hxx>
#include <tools/datetime.hxx>
#include <tools/stream.hxx>
#include <unotools/streamwrap.hxx>
#include <vcl/filter/PDFiumLibrary.hxx>
@@ -55,17 +56,26 @@ void Test::tearDown()
    test::BootstrapFixture::tearDown();
}

bool bHasValidCertificates(const css::uno::Reference<css::uno::XComponentContext>& xContext)
{
    css::uno::Reference<css::xml::crypto::XSEInitializer> xSEInitializer
        = css::xml::crypto::SEInitializer::create(xContext);
    css::uno::Reference<css::xml::crypto::XXMLSecurityContext> xSecurityContext
        = xSEInitializer->createSecurityContext({});
    const auto certs = xSecurityContext->getSecurityEnvironment()->getPersonalCertificates();
    return std::find_if(certs.begin(), certs.end(),
                        [now = DateTime(DateTime::SYSTEM)](auto& xCert) {
                            return now.IsBetween(xCert->getNotValidBefore(),
                                                 xCert->getNotValidAfter());
                        })
           != certs.end();
}

constexpr OUStringLiteral DATA_DIRECTORY = u"/filter/qa/data/";

CPPUNIT_TEST_FIXTURE(Test, testSignCertificateSubjectName)
{
    uno::Reference<xml::crypto::XSEInitializer> xSEInitializer
        = xml::crypto::SEInitializer::create(mxComponentContext);
    uno::Reference<xml::crypto::XXMLSecurityContext> xSecurityContext
        = xSEInitializer->createSecurityContext(OUString());
    uno::Reference<xml::crypto::XSecurityEnvironment> xSecurityEnvironment
        = xSecurityContext->getSecurityEnvironment();
    if (!xSecurityEnvironment->getPersonalCertificates().hasElements())
    if (!bHasValidCertificates(mxComponentContext))
    {
        return;
    }
diff --git a/vcl/qa/cppunit/filter/ipdf/ipdf.cxx b/vcl/qa/cppunit/filter/ipdf/ipdf.cxx
index 8ed4981..7b0ea7b 100644
--- a/vcl/qa/cppunit/filter/ipdf/ipdf.cxx
+++ b/vcl/qa/cppunit/filter/ipdf/ipdf.cxx
@@ -33,6 +33,20 @@ using namespace ::com::sun::star;
namespace
{
constexpr OUStringLiteral DATA_DIRECTORY = u"/vcl/qa/cppunit/filter/ipdf/data/";

css::uno::Reference<css::security::XCertificate>
GetValidCertificate(const css::uno::Reference<css::xml::crypto::XXMLSecurityContext>& xContext)
{
    const auto certs = xContext->getSecurityEnvironment()->getPersonalCertificates();
    css::uno::Reference<css::security::XCertificate> xRet;
    auto it
        = std::find_if(certs.begin(), certs.end(), [now = DateTime(DateTime::SYSTEM)](auto& xCert) {
              return now.IsBetween(xCert->getNotValidBefore(), xCert->getNotValidAfter());
          });
    if (it != certs.end())
        xRet = *it;
    return xRet;
}
}

/// Covers vcl/source/filter/ipdf/ fixes.
@@ -115,17 +129,16 @@ CPPUNIT_TEST_FIXTURE(VclFilterIpdfTest, testPDFAddVisibleSignatureLastPage)
    uno::Reference<view::XSelectionSupplier> xSelectionSupplier(pBaseModel->getCurrentController(),
                                                                uno::UNO_QUERY);
    xSelectionSupplier->select(uno::makeAny(xShape));
    uno::Sequence<uno::Reference<security::XCertificate>> aCertificates
        = getSecurityContext()->getSecurityEnvironment()->getPersonalCertificates();
    if (!aCertificates.hasElements())
    auto xCert = GetValidCertificate(getSecurityContext());
    if (!xCert)
    {
        return;
    }
    SdrView* pView = SfxViewShell::Current()->GetDrawView();
    svx::SignatureLineHelper::setShapeCertificate(pView, aCertificates[0]);
    svx::SignatureLineHelper::setShapeCertificate(pView, xCert);

    // When: do the actual signing.
    pObjectShell->SignDocumentContentUsingCertificate(aCertificates[0]);
    pObjectShell->SignDocumentContentUsingCertificate(xCert);

    // Then: count the # of shapes on the signature widget/annotation.
    std::shared_ptr<vcl::pdf::PDFium> pPDFium = vcl::pdf::PDFiumLibrary::get();