1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <unotest/macros_test.hxx>
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/XCertificate.hpp>
20 #include <basic/basrdll.hxx>
21 #include <cppunit/TestAssert.h>
22 #include <comphelper/sequence.hxx>
23 #include <comphelper/processfactory.hxx>
24 #include <unotest/directories.hxx>
25 #include <osl/file.hxx>
26 #include <osl/process.h>
27 #include <osl/thread.h>
28 #include <tools/datetime.hxx>
29 #include <unotools/tempfile.hxx>
30 #include <unotools/ucbstreamhelper.hxx>
36 MacrosTest::MacrosTest()
37 : mpDll(std::make_unique
<BasicDLL
>())
41 MacrosTest::~MacrosTest() = default;
43 uno::Reference
<css::lang::XComponent
>
44 MacrosTest::loadFromDesktop(const OUString
& rURL
, const OUString
& rDocService
,
45 const uno::Sequence
<beans::PropertyValue
>& rExtraArgs
)
47 CPPUNIT_ASSERT_MESSAGE("no desktop", mxDesktop
.is());
48 std::vector
<beans::PropertyValue
> args
;
49 beans::PropertyValue aMacroValue
;
50 aMacroValue
.Name
= "MacroExecutionMode";
51 aMacroValue
.Handle
= -1;
52 aMacroValue
.Value
<<= document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN
;
53 aMacroValue
.State
= beans::PropertyState_DIRECT_VALUE
;
54 args
.push_back(aMacroValue
);
56 if (!rDocService
.isEmpty())
58 beans::PropertyValue aValue
;
59 aValue
.Name
= "DocumentService";
61 aValue
.Value
<<= rDocService
;
62 aValue
.State
= beans::PropertyState_DIRECT_VALUE
;
63 args
.push_back(aValue
);
66 args
.insert(args
.end(), rExtraArgs
.begin(), rExtraArgs
.end());
68 uno::Reference
<lang::XComponent
> xComponent
= mxDesktop
->loadComponentFromURL(
69 rURL
, "_default", 0, comphelper::containerToSequence(args
));
70 OUString sMessage
= "loading failed: " + rURL
;
71 CPPUNIT_ASSERT_MESSAGE(OUStringToOString(sMessage
, RTL_TEXTENCODING_UTF8
).getStr(),
76 void MacrosTest::dispatchCommand(const uno::Reference
<lang::XComponent
>& xComponent
,
77 const OUString
& rCommand
,
78 const uno::Sequence
<beans::PropertyValue
>& rPropertyValues
)
80 uno::Reference
<frame::XController
> xController
81 = uno::Reference
<frame::XModel
>(xComponent
, uno::UNO_QUERY_THROW
)->getCurrentController();
82 CPPUNIT_ASSERT(xController
.is());
83 uno::Reference
<frame::XDispatchProvider
> xFrame(xController
->getFrame(), uno::UNO_QUERY
);
84 CPPUNIT_ASSERT(xFrame
.is());
86 uno::Reference
<uno::XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
87 uno::Reference
<frame::XDispatchHelper
> xDispatchHelper(frame::DispatchHelper::create(xContext
));
88 CPPUNIT_ASSERT(xDispatchHelper
.is());
90 xDispatchHelper
->executeDispatch(xFrame
, rCommand
, OUString(), 0, rPropertyValues
);
93 std::unique_ptr
<SvStream
> MacrosTest::parseExportStream(const utl::TempFile
& rTempFile
,
94 const OUString
& rStreamName
)
96 const OUString aUrl
= rTempFile
.GetURL();
97 uno::Reference
<uno::XComponentContext
> xComponentContext
98 = comphelper::getProcessComponentContext();
99 uno::Reference
<packages::zip::XZipFileAccess2
> const xZipNames(
100 packages::zip::ZipFileAccess::createWithURL(xComponentContext
, aUrl
));
101 uno::Reference
<io::XInputStream
> const xInputStream(xZipNames
->getByName(rStreamName
),
103 std::unique_ptr
<SvStream
> pStream(utl::UcbStreamHelper::CreateStream(xInputStream
, true));
107 void MacrosTest::setUpNssGpg(const test::Directories
& rDirectories
, const OUString
& rTestName
)
109 OUString aSourceDir
= rDirectories
.getURLFromSrc(u
"/test/signing-keys/");
110 OUString aTargetDir
= rDirectories
.getURLFromWorkdir(
111 OUStringConcatenation("CppunitTest/" + rTestName
+ ".test.user"));
113 // Set up NSS database in workdir/CppunitTest/
114 osl::File::copy(aSourceDir
+ "cert9.db", aTargetDir
+ "/cert9.db");
115 osl::File::copy(aSourceDir
+ "key4.db", aTargetDir
+ "/key4.db");
116 osl::File::copy(aSourceDir
+ "pkcs11.txt", aTargetDir
+ "/pkcs11.txt");
118 // Make gpg use our own defined setup & keys
119 osl::File::copy(aSourceDir
+ "pubring.gpg", aTargetDir
+ "/pubring.gpg");
120 osl::File::copy(aSourceDir
+ "random_seed", aTargetDir
+ "/random_seed");
121 osl::File::copy(aSourceDir
+ "secring.gpg", aTargetDir
+ "/secring.gpg");
122 osl::File::copy(aSourceDir
+ "trustdb.gpg", aTargetDir
+ "/trustdb.gpg");
124 OUString aTargetPath
;
125 osl::FileBase::getSystemPathFromFileURL(aTargetDir
, aTargetPath
);
128 // CryptoAPI test certificates
129 osl::File::copy(aSourceDir
+ "test.p7b", aTargetDir
+ "/test.p7b");
130 OUString
caVar("LIBO_TEST_CRYPTOAPI_PKCS7");
131 osl_setEnvironment(caVar
.pData
, aTargetPath
.pData
);
133 OUString
mozCertVar("MOZILLA_CERTIFICATE_FOLDER");
134 osl_setEnvironment(mozCertVar
.pData
, aTargetPath
.pData
);
136 OUString
gpgHomeVar("GNUPGHOME");
137 osl_setEnvironment(gpgHomeVar
.pData
, aTargetPath
.pData
);
139 #if HAVE_GPGCONF_SOCKETDIR
140 auto const ldPath
= std::getenv("LIBO_LD_PATH");
141 m_gpgconfCommandPrefix
142 = ldPath
== nullptr ? OString() : OString::Concat("LD_LIBRARY_PATH=") + ldPath
+ " ";
144 bool ok
= aTargetPath
.convertToString(&path
, osl_getThreadTextEncoding(),
145 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
146 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
);
147 // if conversion fails, at least provide a best-effort conversion in the message here, for
149 CPPUNIT_ASSERT_MESSAGE(OUStringToOString(aTargetPath
, RTL_TEXTENCODING_UTF8
).getStr(), ok
);
150 m_gpgconfCommandPrefix
+= "GNUPGHOME=" + path
+ " " GPGME_GPGCONF
;
151 // HAVE_GPGCONF_SOCKETDIR is only defined in configure.ac for Linux for now, so (a) std::system
152 // behavior will conform to POSIX (and the relevant env var to set is named LD_LIBRARY_PATH), and
153 // (b) gpgconf --create-socketdir should return zero:
154 OString cmd
= m_gpgconfCommandPrefix
+ " --create-socketdir";
155 int res
= std::system(cmd
.getStr());
156 CPPUNIT_ASSERT_EQUAL_MESSAGE(cmd
.getStr(), 0, res
);
162 void MacrosTest::tearDownNssGpg()
164 #if HAVE_GPGCONF_SOCKETDIR
165 // HAVE_GPGCONF_SOCKETDIR is only defined in configure.ac for Linux for now, so (a) std::system
166 // behavior will conform to POSIX, and (b) gpgconf --remove-socketdir should return zero:
167 OString cmd
= m_gpgconfCommandPrefix
+ " --remove-socketdir";
168 int res
= std::system(cmd
.getStr());
169 CPPUNIT_ASSERT_EQUAL_MESSAGE(cmd
.getStr(), 0, res
);
175 bool MacrosTest::IsValid(const css::uno::Reference
<css::security::XCertificate
>& cert
)
177 return DateTime(DateTime::SYSTEM
)
178 .IsBetween(cert
->getNotValidBefore(), cert
->getNotValidAfter());
181 css::uno::Reference
<css::security::XCertificate
> MacrosTest::GetValidCertificate(
182 const css::uno::Sequence
<css::uno::Reference
<css::security::XCertificate
>>& certs
)
184 if (auto it
= std::find_if(certs
.begin(), certs
.end(), IsValid
); it
!= certs
.end())
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */