14#include <com/sun/star/document/MacroExecMode.hpp>
15#include <com/sun/star/uno/XComponentContext.hpp>
16#include <com/sun/star/frame/DispatchHelper.hpp>
17#include <com/sun/star/packages/zip/ZipFileAccess.hpp>
18#include <com/sun/star/security/CertificateValidity.hpp>
19#include <com/sun/star/security/XCertificate.hpp>
20#include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
23#include <cppunit/TestAssert.h>
27#include <osl/file.hxx>
28#include <osl/process.h>
29#include <osl/thread.h>
30#include <tools/datetime.hxx>
46uno::Reference<css::lang::XComponent>
48 const uno::Sequence<beans::PropertyValue>& rExtraArgs)
50 CPPUNIT_ASSERT_MESSAGE(
"no desktop",
mxDesktop.is());
51 std::vector<beans::PropertyValue>
args;
52 beans::PropertyValue aMacroValue;
53 aMacroValue.Name =
"MacroExecutionMode";
54 aMacroValue.Handle = -1;
55 aMacroValue.Value <<= document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN;
56 aMacroValue.State = beans::PropertyState_DIRECT_VALUE;
57 args.push_back(aMacroValue);
59 if (!rDocService.isEmpty())
61 beans::PropertyValue aValue;
62 aValue.Name =
"DocumentService";
64 aValue.Value <<= rDocService;
65 aValue.State = beans::PropertyState_DIRECT_VALUE;
66 args.push_back(aValue);
69 args.insert(
args.end(), rExtraArgs.begin(), rExtraArgs.end());
71 uno::Reference<lang::XComponent> xComponent =
mxDesktop->loadComponentFromURL(
73 OUString
sMessage =
"loading failed: " + rURL;
81 const OUString& rCommand,
82 const uno::Sequence<beans::PropertyValue>& rPropertyValues)
85 = uno::Reference<frame::XModel>(xComponent, uno::UNO_QUERY_THROW)->getCurrentController();
87 uno::Reference<frame::XDispatchProvider>
xFrame(
xController->getFrame(), uno::UNO_QUERY);
88 CPPUNIT_ASSERT(
xFrame.is());
90 uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
91 uno::Reference<frame::XDispatchHelper> xDispatchHelper(frame::DispatchHelper::create(xContext));
92 CPPUNIT_ASSERT(xDispatchHelper.is());
94 auto ret = xDispatchHelper->executeDispatch(
xFrame, rCommand, OUString(), 0, rPropertyValues);
101 const OUString& rStreamName)
103 uno::Reference<uno::XComponentContext> xComponentContext
105 uno::Reference<packages::zip::XZipFileAccess2>
const xZipNames(
106 packages::zip::ZipFileAccess::createWithURL(xComponentContext, url));
107 uno::Reference<io::XInputStream>
const xInputStream(xZipNames->getByName(rStreamName),
115 OUString aSourceDir = rDirectories.
getURLFromSrc(
u"/test/signing-keys/");
117 = rDirectories.
getURLFromWorkdir(Concat2View(
"CppunitTest/" + rTestName +
".test.user"));
120 osl::File::copy(aSourceDir +
"cert9.db", aTargetDir +
"/cert9.db");
121 osl::File::copy(aSourceDir +
"key4.db", aTargetDir +
"/key4.db");
122 osl::File::copy(aSourceDir +
"pkcs11.txt", aTargetDir +
"/pkcs11.txt");
125 osl::File::copy(aSourceDir +
"pubring.gpg", aTargetDir +
"/pubring.gpg");
126 osl::File::copy(aSourceDir +
"random_seed", aTargetDir +
"/random_seed");
127 osl::File::copy(aSourceDir +
"secring.gpg", aTargetDir +
"/secring.gpg");
128 osl::File::copy(aSourceDir +
"trustdb.gpg", aTargetDir +
"/trustdb.gpg");
130 OUString aTargetPath;
131 osl::FileBase::getSystemPathFromFileURL(aTargetDir, aTargetPath);
135 osl::File::copy(aSourceDir +
"test.p7b", aTargetDir +
"/test.p7b");
136 OUString caVar(
"LIBO_TEST_CRYPTOAPI_PKCS7");
137 osl_setEnvironment(caVar.pData, aTargetPath.pData);
139 OUString mozCertVar(
"MOZILLA_CERTIFICATE_FOLDER");
141 osl_setEnvironment(mozCertVar.pData, OUString(
"sql:" + aTargetPath).
pData);
143 OUString gpgHomeVar(
"GNUPGHOME");
144 osl_setEnvironment(gpgHomeVar.pData, aTargetPath.pData);
146#if HAVE_GPGCONF_SOCKETDIR
147 auto const ldPath = std::getenv(
"LIBO_LD_PATH");
148 m_gpgconfCommandPrefix
149 = ldPath ==
nullptr ? OString() : OString::Concat(
"LD_LIBRARY_PATH=") + ldPath +
" ";
151 bool ok = aTargetPath.convertToString(&path, osl_getThreadTextEncoding(),
152 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
153 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR);
156 CPPUNIT_ASSERT_MESSAGE(
OUStringToOString(aTargetPath, RTL_TEXTENCODING_UTF8).getStr(), ok);
157 m_gpgconfCommandPrefix +=
"GNUPGHOME=" + path +
" " GPGME_GPGCONF;
161 OString cmd = m_gpgconfCommandPrefix +
" --create-socketdir";
162 int res = std::system(cmd.getStr());
163 CPPUNIT_ASSERT_EQUAL_MESSAGE(cmd.getStr(), 0, res);
171#if HAVE_GPGCONF_SOCKETDIR
174 OString cmd = m_gpgconfCommandPrefix +
" --remove-socketdir";
175 int res = std::system(cmd.getStr());
176 CPPUNIT_ASSERT_EQUAL_MESSAGE(cmd.getStr(), 0, res);
188 const css::uno::Reference<css::xml::crypto::XSecurityEnvironment>&
env;
189 Valid(
const css::uno::Sequence<css::beans::PropertyValue>& rFilterData,
190 const css::uno::Reference<css::xml::crypto::XSecurityEnvironment>& rEnv)
194 for (
const auto& propVal : rFilterData)
196 if (propVal.Name ==
"SignCertificateSubjectName")
200 bool operator()(
const css::uno::Reference<css::security::XCertificate>& cert)
const
202 if (!
now.
IsBetween(cert->getNotValidBefore(), cert->getNotValidAfter()))
206 if (
env->verifyCertificate(cert, {}) != css::security::CertificateValidity::VALID)
214 const css::uno::Reference<css::xml::crypto::XSecurityEnvironment>&
env)
221 const css::uno::Sequence<css::uno::Reference<css::security::XCertificate>>& certs,
222 const css::uno::Reference<css::xml::crypto::XSecurityEnvironment>&
env,
223 const css::uno::Sequence<css::beans::PropertyValue>& rFilterData)
225 if (
auto it = std::find_if(certs.begin(), certs.end(), Valid(rFilterData,
env));
bool IsBetween(const DateTime &rFrom, const DateTime &rTo) const
static void ProcessEventsToIdle()
OUString getURLFromWorkdir(std::u16string_view rPath) const
OUString getURLFromSrc(std::u16string_view rPath) const
static css::uno::Any dispatchCommand(const css::uno::Reference< css::lang::XComponent > &xComponent, const OUString &rCommand, const css::uno::Sequence< css::beans::PropertyValue > &rPropertyValues)
static css::uno::Reference< css::security::XCertificate > GetValidCertificate(const css::uno::Sequence< css::uno::Reference< css::security::XCertificate > > &certs, const css::uno::Reference< css::xml::crypto::XSecurityEnvironment > &env, const css::uno::Sequence< css::beans::PropertyValue > &rFilterData={})
static bool IsValid(const css::uno::Reference< css::security::XCertificate > &cert, const css::uno::Reference< css::xml::crypto::XSecurityEnvironment > &env)
css::uno::Reference< css::frame::XDesktop2 > mxDesktop
static std::unique_ptr< SvStream > parseExportStream(const OUString &url, const OUString &rStreamName)
Opens rStreamName from rTempFile, assuming it's a ZIP storage.
void setUpNssGpg(const test::Directories &rDirectories, const OUString &rTestName)
css::uno::Reference< css::lang::XComponent > loadFromDesktop(const OUString &rURL, const OUString &rDocService=OUString(), const css::uno::Sequence< css::beans::PropertyValue > &rExtra_args=css::uno::Sequence< css::beans::PropertyValue >())
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
const css::uno::Reference< css::xml::crypto::XSecurityEnvironment > & env
std::unique_ptr< sal_Int32[]> pData
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Reference< XComponentContext > getProcessComponentContext()
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
Reference< XController > xController
Reference< XFrame > xFrame