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>
18 #include <basic/basrdll.hxx>
19 #include <cppunit/TestAssert.h>
20 #include <comphelper/sequence.hxx>
21 #include <comphelper/processfactory.hxx>
22 #include <unotest/directories.hxx>
23 #include <osl/file.hxx>
24 #include <osl/process.h>
25 #include <osl/thread.h>
31 MacrosTest::MacrosTest()
32 : mpDll(std::make_unique
<BasicDLL
>())
36 MacrosTest::~MacrosTest() = default;
38 uno::Reference
<css::lang::XComponent
>
39 MacrosTest::loadFromDesktop(const OUString
& rURL
, const OUString
& rDocService
,
40 const uno::Sequence
<beans::PropertyValue
>& rExtraArgs
)
42 CPPUNIT_ASSERT_MESSAGE("no desktop", mxDesktop
.is());
43 std::vector
<beans::PropertyValue
> args
;
44 beans::PropertyValue aMacroValue
;
45 aMacroValue
.Name
= "MacroExecutionMode";
46 aMacroValue
.Handle
= -1;
47 aMacroValue
.Value
<<= document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN
;
48 aMacroValue
.State
= beans::PropertyState_DIRECT_VALUE
;
49 args
.push_back(aMacroValue
);
51 if (!rDocService
.isEmpty())
53 beans::PropertyValue aValue
;
54 aValue
.Name
= "DocumentService";
56 aValue
.Value
<<= rDocService
;
57 aValue
.State
= beans::PropertyState_DIRECT_VALUE
;
58 args
.push_back(aValue
);
61 args
.insert(args
.end(), rExtraArgs
.begin(), rExtraArgs
.end());
63 uno::Reference
<lang::XComponent
> xComponent
= mxDesktop
->loadComponentFromURL(
64 rURL
, "_default", 0, comphelper::containerToSequence(args
));
65 OUString sMessage
= "loading failed: " + rURL
;
66 CPPUNIT_ASSERT_MESSAGE(OUStringToOString(sMessage
, RTL_TEXTENCODING_UTF8
).getStr(),
71 void MacrosTest::dispatchCommand(const uno::Reference
<lang::XComponent
>& xComponent
,
72 const OUString
& rCommand
,
73 const uno::Sequence
<beans::PropertyValue
>& rPropertyValues
)
75 uno::Reference
<frame::XController
> xController
76 = uno::Reference
<frame::XModel
>(xComponent
, uno::UNO_QUERY_THROW
)->getCurrentController();
77 CPPUNIT_ASSERT(xController
.is());
78 uno::Reference
<frame::XDispatchProvider
> xFrame(xController
->getFrame(), uno::UNO_QUERY
);
79 CPPUNIT_ASSERT(xFrame
.is());
81 uno::Reference
<uno::XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
82 uno::Reference
<frame::XDispatchHelper
> xDispatchHelper(frame::DispatchHelper::create(xContext
));
83 CPPUNIT_ASSERT(xDispatchHelper
.is());
85 xDispatchHelper
->executeDispatch(xFrame
, rCommand
, OUString(), 0, rPropertyValues
);
88 void MacrosTest::setUpNssGpg(const test::Directories
& rDirectories
, const OUString
& rTestName
)
90 OUString aSourceDir
= rDirectories
.getURLFromSrc(u
"/test/signing-keys/");
92 = rDirectories
.getURLFromWorkdir(OUString("CppunitTest/" + rTestName
+ ".test.user"));
94 // Set up NSS database in workdir/CppunitTest/
95 osl::File::copy(aSourceDir
+ "cert9.db", aTargetDir
+ "/cert9.db");
96 osl::File::copy(aSourceDir
+ "key4.db", aTargetDir
+ "/key4.db");
97 osl::File::copy(aSourceDir
+ "pkcs11.txt", aTargetDir
+ "/pkcs11.txt");
99 // Make gpg use our own defined setup & keys
100 osl::File::copy(aSourceDir
+ "pubring.gpg", aTargetDir
+ "/pubring.gpg");
101 osl::File::copy(aSourceDir
+ "random_seed", aTargetDir
+ "/random_seed");
102 osl::File::copy(aSourceDir
+ "secring.gpg", aTargetDir
+ "/secring.gpg");
103 osl::File::copy(aSourceDir
+ "trustdb.gpg", aTargetDir
+ "/trustdb.gpg");
105 OUString aTargetPath
;
106 osl::FileBase::getSystemPathFromFileURL(aTargetDir
, aTargetPath
);
109 // CryptoAPI test certificates
110 osl::File::copy(aSourceDir
+ "test.p7b", aTargetDir
+ "/test.p7b");
111 OUString
caVar("LIBO_TEST_CRYPTOAPI_PKCS7");
112 osl_setEnvironment(caVar
.pData
, aTargetPath
.pData
);
114 OUString
mozCertVar("MOZILLA_CERTIFICATE_FOLDER");
115 osl_setEnvironment(mozCertVar
.pData
, aTargetPath
.pData
);
117 OUString
gpgHomeVar("GNUPGHOME");
118 osl_setEnvironment(gpgHomeVar
.pData
, aTargetPath
.pData
);
120 #if HAVE_GPGCONF_SOCKETDIR
121 auto const ldPath
= std::getenv("LIBO_LD_PATH");
122 m_gpgconfCommandPrefix
123 = ldPath
== nullptr ? OString() : OString::Concat("LD_LIBRARY_PATH=") + ldPath
+ " ";
125 bool ok
= aTargetPath
.convertToString(&path
, osl_getThreadTextEncoding(),
126 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
127 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
);
128 // if conversion fails, at least provide a best-effort conversion in the message here, for
130 CPPUNIT_ASSERT_MESSAGE(OUStringToOString(aTargetPath
, RTL_TEXTENCODING_UTF8
).getStr(), ok
);
131 m_gpgconfCommandPrefix
+= "GNUPGHOME=" + path
+ " " GPGME_GPGCONF
;
132 // HAVE_GPGCONF_SOCKETDIR is only defined in configure.ac for Linux for now, so (a) std::system
133 // behavior will conform to POSIX (and the relevant env var to set is named LD_LIBRARY_PATH), and
134 // (b) gpgconf --create-socketdir should return zero:
135 OString cmd
= m_gpgconfCommandPrefix
+ " --create-socketdir";
136 int res
= std::system(cmd
.getStr());
137 CPPUNIT_ASSERT_EQUAL_MESSAGE(cmd
.getStr(), 0, res
);
143 void MacrosTest::tearDownNssGpg()
145 #if HAVE_GPGCONF_SOCKETDIR
146 // HAVE_GPGCONF_SOCKETDIR is only defined in configure.ac for Linux for now, so (a) std::system
147 // behavior will conform to POSIX, and (b) gpgconf --remove-socketdir should return zero:
148 OString cmd
= m_gpgconfCommandPrefix
+ " --remove-socketdir";
149 int res
= std::system(cmd
.getStr());
150 CPPUNIT_ASSERT_EQUAL_MESSAGE(cmd
.getStr(), 0, res
);
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */