LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / unotest / source / cpp / macros_test.cxx
blob4c48574e759a3601604f3e7d1f863d449752914d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <unotest/macros_test.hxx>
12 #include <vector>
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>
19 #include <basic/basrdll.hxx>
20 #include <cppunit/TestAssert.h>
21 #include <comphelper/sequence.hxx>
22 #include <comphelper/processfactory.hxx>
23 #include <unotest/directories.hxx>
24 #include <osl/file.hxx>
25 #include <osl/process.h>
26 #include <osl/thread.h>
27 #include <unotools/tempfile.hxx>
28 #include <unotools/ucbstreamhelper.hxx>
30 using namespace css;
32 namespace unotest
34 MacrosTest::MacrosTest()
35 : mpDll(std::make_unique<BasicDLL>())
39 MacrosTest::~MacrosTest() = default;
41 uno::Reference<css::lang::XComponent>
42 MacrosTest::loadFromDesktop(const OUString& rURL, const OUString& rDocService,
43 const uno::Sequence<beans::PropertyValue>& rExtraArgs)
45 CPPUNIT_ASSERT_MESSAGE("no desktop", mxDesktop.is());
46 std::vector<beans::PropertyValue> args;
47 beans::PropertyValue aMacroValue;
48 aMacroValue.Name = "MacroExecutionMode";
49 aMacroValue.Handle = -1;
50 aMacroValue.Value <<= document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN;
51 aMacroValue.State = beans::PropertyState_DIRECT_VALUE;
52 args.push_back(aMacroValue);
54 if (!rDocService.isEmpty())
56 beans::PropertyValue aValue;
57 aValue.Name = "DocumentService";
58 aValue.Handle = -1;
59 aValue.Value <<= rDocService;
60 aValue.State = beans::PropertyState_DIRECT_VALUE;
61 args.push_back(aValue);
64 args.insert(args.end(), rExtraArgs.begin(), rExtraArgs.end());
66 uno::Reference<lang::XComponent> xComponent = mxDesktop->loadComponentFromURL(
67 rURL, "_default", 0, comphelper::containerToSequence(args));
68 OUString sMessage = "loading failed: " + rURL;
69 CPPUNIT_ASSERT_MESSAGE(OUStringToOString(sMessage, RTL_TEXTENCODING_UTF8).getStr(),
70 xComponent.is());
71 return xComponent;
74 void MacrosTest::dispatchCommand(const uno::Reference<lang::XComponent>& xComponent,
75 const OUString& rCommand,
76 const uno::Sequence<beans::PropertyValue>& rPropertyValues)
78 uno::Reference<frame::XController> xController
79 = uno::Reference<frame::XModel>(xComponent, uno::UNO_QUERY_THROW)->getCurrentController();
80 CPPUNIT_ASSERT(xController.is());
81 uno::Reference<frame::XDispatchProvider> xFrame(xController->getFrame(), uno::UNO_QUERY);
82 CPPUNIT_ASSERT(xFrame.is());
84 uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
85 uno::Reference<frame::XDispatchHelper> xDispatchHelper(frame::DispatchHelper::create(xContext));
86 CPPUNIT_ASSERT(xDispatchHelper.is());
88 xDispatchHelper->executeDispatch(xFrame, rCommand, OUString(), 0, rPropertyValues);
91 std::unique_ptr<SvStream> MacrosTest::parseExportStream(const utl::TempFile& rTempFile,
92 const OUString& rStreamName)
94 const OUString aUrl = rTempFile.GetURL();
95 uno::Reference<uno::XComponentContext> xComponentContext
96 = comphelper::getProcessComponentContext();
97 uno::Reference<packages::zip::XZipFileAccess2> const xZipNames(
98 packages::zip::ZipFileAccess::createWithURL(xComponentContext, aUrl));
99 uno::Reference<io::XInputStream> const xInputStream(xZipNames->getByName(rStreamName),
100 uno::UNO_QUERY);
101 std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xInputStream, true));
102 return pStream;
105 void MacrosTest::setUpNssGpg(const test::Directories& rDirectories, const OUString& rTestName)
107 OUString aSourceDir = rDirectories.getURLFromSrc(u"/test/signing-keys/");
108 OUString aTargetDir = rDirectories.getURLFromWorkdir(
109 OUStringConcatenation("CppunitTest/" + rTestName + ".test.user"));
111 // Set up NSS database in workdir/CppunitTest/
112 osl::File::copy(aSourceDir + "cert9.db", aTargetDir + "/cert9.db");
113 osl::File::copy(aSourceDir + "key4.db", aTargetDir + "/key4.db");
114 osl::File::copy(aSourceDir + "pkcs11.txt", aTargetDir + "/pkcs11.txt");
116 // Make gpg use our own defined setup & keys
117 osl::File::copy(aSourceDir + "pubring.gpg", aTargetDir + "/pubring.gpg");
118 osl::File::copy(aSourceDir + "random_seed", aTargetDir + "/random_seed");
119 osl::File::copy(aSourceDir + "secring.gpg", aTargetDir + "/secring.gpg");
120 osl::File::copy(aSourceDir + "trustdb.gpg", aTargetDir + "/trustdb.gpg");
122 OUString aTargetPath;
123 osl::FileBase::getSystemPathFromFileURL(aTargetDir, aTargetPath);
125 #ifdef _WIN32
126 // CryptoAPI test certificates
127 osl::File::copy(aSourceDir + "test.p7b", aTargetDir + "/test.p7b");
128 OUString caVar("LIBO_TEST_CRYPTOAPI_PKCS7");
129 osl_setEnvironment(caVar.pData, aTargetPath.pData);
130 #else
131 OUString mozCertVar("MOZILLA_CERTIFICATE_FOLDER");
132 // explicit prefix with "sql:" needed for CentOS7 system NSS 3.67
133 osl_setEnvironment(mozCertVar.pData, OUString("sql:" + aTargetPath).pData);
134 #endif
135 OUString gpgHomeVar("GNUPGHOME");
136 osl_setEnvironment(gpgHomeVar.pData, aTargetPath.pData);
138 #if HAVE_GPGCONF_SOCKETDIR
139 auto const ldPath = std::getenv("LIBO_LD_PATH");
140 m_gpgconfCommandPrefix
141 = ldPath == nullptr ? OString() : OString::Concat("LD_LIBRARY_PATH=") + ldPath + " ";
142 OString path;
143 bool ok = aTargetPath.convertToString(&path, osl_getThreadTextEncoding(),
144 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
145 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR);
146 // if conversion fails, at least provide a best-effort conversion in the message here, for
147 // context
148 CPPUNIT_ASSERT_MESSAGE(OUStringToOString(aTargetPath, RTL_TEXTENCODING_UTF8).getStr(), ok);
149 m_gpgconfCommandPrefix += "GNUPGHOME=" + path + " " GPGME_GPGCONF;
150 // HAVE_GPGCONF_SOCKETDIR is only defined in configure.ac for Linux for now, so (a) std::system
151 // behavior will conform to POSIX (and the relevant env var to set is named LD_LIBRARY_PATH), and
152 // (b) gpgconf --create-socketdir should return zero:
153 OString cmd = m_gpgconfCommandPrefix + " --create-socketdir";
154 int res = std::system(cmd.getStr());
155 CPPUNIT_ASSERT_EQUAL_MESSAGE(cmd.getStr(), 0, res);
156 #else
157 (void)this;
158 #endif
161 void MacrosTest::tearDownNssGpg()
163 #if HAVE_GPGCONF_SOCKETDIR
164 // HAVE_GPGCONF_SOCKETDIR is only defined in configure.ac for Linux for now, so (a) std::system
165 // behavior will conform to POSIX, and (b) gpgconf --remove-socketdir should return zero:
166 OString cmd = m_gpgconfCommandPrefix + " --remove-socketdir";
167 int res = std::system(cmd.getStr());
168 CPPUNIT_ASSERT_EQUAL_MESSAGE(cmd.getStr(), 0, res);
169 #else
170 (void)this;
171 #endif
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */