filter/qa/pdf.cxx                   |    2 +-
 include/unotest/macros_test.hxx     |    8 ++++++++
 unotest/Library_unotest.mk          |    1 +
 unotest/source/cpp/macros_test.cxx  |   14 ++++++++++++++
 vcl/qa/cppunit/filter/ipdf/ipdf.cxx |   10 +++++-----
 5 files changed, 29 insertions(+), 6 deletions(-)

New commits:
commit ff49a550df626a4b02869b86f1ca9b9a5a421e44
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Mon Feb 28 13:59:05 2022 +0300
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Mon Feb 28 18:44:33 2022 +0100

    Fix the tests failing when invalid certificate is in cert store
    
    Change-Id: I72055c814a11a76c4934610bcad9c24aae21164e
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130690
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/filter/qa/pdf.cxx b/filter/qa/pdf.cxx
index 8eb64b08cc7a..ddf6c3bce066 100644
--- a/filter/qa/pdf.cxx
+++ b/filter/qa/pdf.cxx
@@ -65,7 +65,7 @@ CPPUNIT_TEST_FIXTURE(Test, testSignCertificateSubjectName)
         = xSEInitializer->createSecurityContext(OUString());
     uno::Reference<xml::crypto::XSecurityEnvironment> xSecurityEnvironment
         = xSecurityContext->getSecurityEnvironment();
-    if (!xSecurityEnvironment->getPersonalCertificates().hasElements())
+    if (!GetValidCertificate(xSecurityEnvironment->getPersonalCertificates()))
     {
         return;
     }
diff --git a/include/unotest/macros_test.hxx b/include/unotest/macros_test.hxx
index ed879b835a55..c6c810ad108b 100644
--- a/include/unotest/macros_test.hxx
+++ b/include/unotest/macros_test.hxx
@@ -39,6 +39,11 @@ namespace utl
 class TempFile;
 }
 
+namespace com::sun::star::security
+{
+class XCertificate;
+}
+
 namespace unotest
 {
 class OOO_DLLPUBLIC_UNOTEST MacrosTest
@@ -88,6 +93,9 @@ public:
     void setUpNssGpg(const test::Directories& rDirectories, const OUString& 
rTestName);
     void tearDownNssGpg();
 
+    static css::uno::Reference<css::security::XCertificate> 
GetValidCertificate(
+        const 
css::uno::Sequence<css::uno::Reference<css::security::XCertificate>>& certs);
+
 protected:
     css::uno::Reference<css::frame::XDesktop2> mxDesktop;
 
diff --git a/unotest/Library_unotest.mk b/unotest/Library_unotest.mk
index 3acfe2f69ed9..1a7f8ba5ab5f 100644
--- a/unotest/Library_unotest.mk
+++ b/unotest/Library_unotest.mk
@@ -22,6 +22,7 @@ $(eval $(call gb_Library_use_libraries,unotest,\
        cppuhelper \
        sal \
        sb \
+       tl \
        utl \
 ))
 
diff --git a/unotest/source/cpp/macros_test.cxx 
b/unotest/source/cpp/macros_test.cxx
index 718c0b1b8954..81738dc6dafe 100644
--- a/unotest/source/cpp/macros_test.cxx
+++ b/unotest/source/cpp/macros_test.cxx
@@ -15,6 +15,7 @@
 #include <com/sun/star/uno/XComponentContext.hpp>
 #include <com/sun/star/frame/DispatchHelper.hpp>
 #include <com/sun/star/packages/zip/ZipFileAccess.hpp>
+#include <com/sun/star/security/XCertificate.hpp>
 
 #include <basic/basrdll.hxx>
 #include <cppunit/TestAssert.h>
@@ -24,6 +25,7 @@
 #include <osl/file.hxx>
 #include <osl/process.h>
 #include <osl/thread.h>
+#include <tools/datetime.hxx>
 #include <unotools/tempfile.hxx>
 #include <unotools/ucbstreamhelper.hxx>
 
@@ -169,6 +171,18 @@ void MacrosTest::tearDownNssGpg()
     (void)this;
 #endif
 }
+
+css::uno::Reference<css::security::XCertificate> 
MacrosTest::GetValidCertificate(
+    const 
css::uno::Sequence<css::uno::Reference<css::security::XCertificate>>& certs)
+{
+    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())
+        return *it;
+    return {};
+}
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/qa/cppunit/filter/ipdf/ipdf.cxx 
b/vcl/qa/cppunit/filter/ipdf/ipdf.cxx
index 8ed49813e88b..a2732b58d862 100644
--- a/vcl/qa/cppunit/filter/ipdf/ipdf.cxx
+++ b/vcl/qa/cppunit/filter/ipdf/ipdf.cxx
@@ -115,17 +115,17 @@ 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()->getSecurityEnvironment()->getPersonalCertificates());
+    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();

Reply via email to