From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- unotest/source/cpp/bootstrapfixturebase.cxx | 43 ++++ unotest/source/cpp/directories.cxx | 66 +++++++ unotest/source/cpp/filters-test.cxx | 170 ++++++++++++++++ unotest/source/cpp/getargument.cxx | 36 ++++ unotest/source/cpp/gettestargument.cxx | 34 ++++ unotest/source/cpp/macros_test.cxx | 218 +++++++++++++++++++++ unotest/source/cpp/officeconnection.cxx | 152 ++++++++++++++ unotest/source/cpp/toabsolutefileurl.cxx | 61 ++++++ .../unobootstrapprotector.cxx | 93 +++++++++ .../unoexceptionprotector.cxx | 81 ++++++++ 10 files changed, 954 insertions(+) create mode 100644 unotest/source/cpp/bootstrapfixturebase.cxx create mode 100644 unotest/source/cpp/directories.cxx create mode 100644 unotest/source/cpp/filters-test.cxx create mode 100644 unotest/source/cpp/getargument.cxx create mode 100644 unotest/source/cpp/gettestargument.cxx create mode 100644 unotest/source/cpp/macros_test.cxx create mode 100644 unotest/source/cpp/officeconnection.cxx create mode 100644 unotest/source/cpp/toabsolutefileurl.cxx create mode 100644 unotest/source/cpp/unobootstrapprotector/unobootstrapprotector.cxx create mode 100644 unotest/source/cpp/unoexceptionprotector/unoexceptionprotector.cxx (limited to 'unotest/source/cpp') diff --git a/unotest/source/cpp/bootstrapfixturebase.cxx b/unotest/source/cpp/bootstrapfixturebase.cxx new file mode 100644 index 000000000..dd92d3822 --- /dev/null +++ b/unotest/source/cpp/bootstrapfixturebase.cxx @@ -0,0 +1,43 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include +#include + +#include +#include +#if HAVE_FEATURE_SCRIPTING +#include +#endif + +using namespace ::com::sun::star; + +// NB. this constructor is called before any tests are run, once for each +// test function in a rather non-intuitive way. This is why all the 'real' +// heavy lifting is deferred until setUp. setUp and tearDown are interleaved +// between the tests as you might expect. +test::BootstrapFixtureBase::BootstrapFixtureBase() {} + +test::BootstrapFixtureBase::~BootstrapFixtureBase() {} + +void test::BootstrapFixtureBase::setUp() +{ + m_xContext = comphelper::getProcessComponentContext(); + m_xFactory = m_xContext->getServiceManager(); + m_xSFactory.set(m_xFactory, uno::UNO_QUERY_THROW); +} + +void test::BootstrapFixtureBase::tearDown() +{ +#if HAVE_FEATURE_SCRIPTING + StarBASIC::DetachAllDocBasicItems(); +#endif +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotest/source/cpp/directories.cxx b/unotest/source/cpp/directories.cxx new file mode 100644 index 000000000..93bcd4dae --- /dev/null +++ b/unotest/source/cpp/directories.cxx @@ -0,0 +1,66 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +#include +#include +#include + +namespace +{ +OUString getFileURLFromSystemPath(OUString const& path) +{ + OUString url; + osl::FileBase::RC e = osl::FileBase::getFileURLFromSystemPath(path, url); + CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e); + if (!url.endsWith("/")) + { + url += "/"; + } + return url; +} +} + +test::Directories::Directories() +{ + const char* pSrcRoot = getenv("SRC_ROOT"); + CPPUNIT_ASSERT_MESSAGE("SRC_ROOT env variable not set", pSrcRoot != nullptr); + CPPUNIT_ASSERT_MESSAGE("SRC_ROOT env variable not set", pSrcRoot[0] != 0); + const char* pWorkdirRoot = getenv("WORKDIR_FOR_BUILD"); + CPPUNIT_ASSERT_MESSAGE("$WORKDIR_FOR_BUILD env variable not set", pWorkdirRoot != nullptr); + CPPUNIT_ASSERT_MESSAGE("$WORKDIR_FOR_BUILD env variable not set", pWorkdirRoot[0] != 0); + m_aSrcRootPath = OUString::createFromAscii(pSrcRoot); + m_aSrcRootURL = getFileURLFromSystemPath(m_aSrcRootPath); + + m_aWorkdirRootPath = OUString::createFromAscii(pWorkdirRoot); + m_aWorkdirRootURL = getFileURLFromSystemPath(m_aWorkdirRootPath); +} + +OUString test::Directories::getURLFromSrc(std::u16string_view rPath) const +{ + return m_aSrcRootURL + rPath; +} + +OUString test::Directories::getPathFromSrc(std::u16string_view rPath) const +{ + return m_aSrcRootPath + rPath; +} + +OUString test::Directories::getURLFromWorkdir(std::u16string_view rPath) const +{ + return m_aWorkdirRootURL + rPath; +} + +OUString test::Directories::getPathFromWorkdir(std::u16string_view rPath) const +{ + return m_aWorkdirRootPath + rPath; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotest/source/cpp/filters-test.cxx b/unotest/source/cpp/filters-test.cxx new file mode 100644 index 000000000..7adbd9ca4 --- /dev/null +++ b/unotest/source/cpp/filters-test.cxx @@ -0,0 +1,170 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +#include +#include + +#include +#include +#include +#include + +#include + +namespace test { + +static void decode(const OUString& rIn, const OUString &rOut) +{ + rtlCipher cipher = rtl_cipher_create(rtl_Cipher_AlgorithmARCFOUR, rtl_Cipher_ModeStream); + CPPUNIT_ASSERT_MESSAGE("cipher creation failed", cipher != nullptr); + + //mcrypt --bare -a arcfour -o hex -k 435645 -s 3 + const sal_uInt8 aKey[3] = {'C', 'V', 'E'}; + + rtlCipherError result = rtl_cipher_init(cipher, rtl_Cipher_DirectionDecode, aKey, SAL_N_ELEMENTS(aKey), nullptr, 0); + + CPPUNIT_ASSERT_EQUAL_MESSAGE("cipher init failed", rtl_Cipher_E_None, result); + + osl::File aIn(rIn); + CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aIn.open(osl_File_OpenFlag_Read)); + + osl::File aOut(rOut); + CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aOut.open(osl_File_OpenFlag_Write)); + + sal_uInt8 in[8192]; + sal_uInt8 out[8192]; + sal_uInt64 nBytesRead, nBytesWritten; + while(true) + { + CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aIn.read(in, sizeof(in), nBytesRead)); + if (!nBytesRead) + break; + CPPUNIT_ASSERT_EQUAL(rtl_Cipher_E_None, rtl_cipher_decode(cipher, in, nBytesRead, out, sizeof(out))); + CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aOut.write(out, nBytesRead, nBytesWritten)); + CPPUNIT_ASSERT_EQUAL(nBytesRead, nBytesWritten); + } + + rtl_cipher_destroy(cipher); +} + +void FiltersTest::recursiveScan(filterStatus nExpected, + const OUString &rFilter, const OUString &rURL, + const OUString &rUserData, SfxFilterFlags nFilterFlags, + SotClipboardFormatId nClipboardID, unsigned int nFilterVersion, bool bExport) +{ + osl::Directory aDir(rURL); + + CPPUNIT_ASSERT_EQUAL_MESSAGE(OUString("Failed to open directory " + rURL).toUtf8().getStr(), osl::FileBase::E_None, aDir.open()); + osl::DirectoryItem aItem; + osl::FileStatus aFileStatus(osl_FileStatus_Mask_FileURL|osl_FileStatus_Mask_Type); + std::set dirs; + std::set files; + while (aDir.getNextItem(aItem) == osl::FileBase::E_None) + { + aItem.getFileStatus(aFileStatus); + OUString sURL = aFileStatus.getFileURL(); + if (aFileStatus.getFileType() == osl::FileStatus::Directory) + { + dirs.insert(sURL); + } + else + { + files.insert(sURL); + } + } + for (auto const & sURL: dirs) { + recursiveScan(nExpected, rFilter, sURL, rUserData, + nFilterFlags, nClipboardID, nFilterVersion, bExport); + } + for (auto const & sURL: files) { + OUString sTmpFile; + bool bEncrypted = false; + + sal_Int32 nLastSlash = sURL.lastIndexOf('/'); + + if ((nLastSlash != -1) && (nLastSlash+1 < sURL.getLength())) + { + //ignore .files + if (sURL[nLastSlash+1] == '.') + continue; + + if ( + (sURL.match("BID", nLastSlash+1)) || + (sURL.match("CVE", nLastSlash+1)) || + (sURL.match("EDB", nLastSlash+1)) || + (sURL.match("RC4", nLastSlash+1)) // just means "encrypted" + ) + { + bEncrypted = true; + } + } + + OString aRes( + OString::Concat(bExport ? std::string_view("save") : std::string_view("load")) + " " + + OUStringToOString(sURL, osl_getThreadTextEncoding())); + + OUString realUrl; + if (bEncrypted) + { + CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, osl::FileBase::createTempFile(nullptr, nullptr, &sTmpFile)); + decode(sURL, sTmpFile); + realUrl = sTmpFile; + } + else + { + realUrl = sURL; + } + + //output name early, so in the case of a hang, the name of + //the hanging input file is visible + fprintf(stderr, "Testing %s:\n", aRes.getStr()); + sal_uInt32 nStartTime = osl_getGlobalTimer(); + bool bRes; + if (!bExport) + bRes = load(rFilter, realUrl, rUserData, nFilterFlags, + nClipboardID, nFilterVersion); + else + bRes = save(rFilter, realUrl, rUserData, nFilterFlags, + nClipboardID, nFilterVersion); + sal_uInt32 nEndTime = osl_getGlobalTimer(); + + if (bEncrypted) + CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, osl::File::remove(sTmpFile)); + + fprintf(stderr, "Tested %s: %s (%" SAL_PRIuUINT32 "ms)\n", + aRes.getStr(), bRes?"Pass":"Fail", nEndTime-nStartTime); + if (nExpected == test::indeterminate) + continue; + filterStatus nResult = bRes ? test::pass : test::fail; + CPPUNIT_ASSERT_EQUAL_MESSAGE(aRes.getStr(), nExpected, nResult); + } + CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aDir.close()); +} + +void FiltersTest::testDir(const OUString &rFilter, + std::u16string_view rURL, const OUString &rUserData, + SfxFilterFlags nFilterFlags, SotClipboardFormatId nClipboardID, + unsigned int nFilterVersion, bool bExport) +{ + recursiveScan(test::pass, rFilter, + OUString::Concat(rURL) + "pass", + rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport); + recursiveScan(test::fail, rFilter, + OUString::Concat(rURL) + "fail", + rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport); + recursiveScan(test::indeterminate, rFilter, + OUString::Concat(rURL) + "indeterminate", + rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotest/source/cpp/getargument.cxx b/unotest/source/cpp/getargument.cxx new file mode 100644 index 000000000..22cd39fc5 --- /dev/null +++ b/unotest/source/cpp/getargument.cxx @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include + +#include +#include +#include +#include + +namespace test +{ +bool getArgument(std::u16string_view name, OUString* value) +{ + OSL_ASSERT(value != nullptr); + return rtl::Bootstrap::get(OUString::Concat("arg-") + name, *value); +} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotest/source/cpp/gettestargument.cxx b/unotest/source/cpp/gettestargument.cxx new file mode 100644 index 000000000..0fa181678 --- /dev/null +++ b/unotest/source/cpp/gettestargument.cxx @@ -0,0 +1,34 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include + +#include +#include +#include + +namespace test +{ +bool getTestArgument(std::u16string_view name, OUString* value) +{ + return getArgument(OUStringConcatenation(OUString::Concat("testarg.") + name), value); +} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotest/source/cpp/macros_test.cxx b/unotest/source/cpp/macros_test.cxx new file mode 100644 index 000000000..76105b88b --- /dev/null +++ b/unotest/source/cpp/macros_test.cxx @@ -0,0 +1,218 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace css; + +namespace unotest +{ +MacrosTest::MacrosTest() + : mpDll(std::make_unique()) +{ +} + +MacrosTest::~MacrosTest() = default; + +uno::Reference +MacrosTest::loadFromDesktop(const OUString& rURL, const OUString& rDocService, + const uno::Sequence& rExtraArgs) +{ + CPPUNIT_ASSERT_MESSAGE("no desktop", mxDesktop.is()); + std::vector args; + beans::PropertyValue aMacroValue; + aMacroValue.Name = "MacroExecutionMode"; + aMacroValue.Handle = -1; + aMacroValue.Value <<= document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN; + aMacroValue.State = beans::PropertyState_DIRECT_VALUE; + args.push_back(aMacroValue); + + if (!rDocService.isEmpty()) + { + beans::PropertyValue aValue; + aValue.Name = "DocumentService"; + aValue.Handle = -1; + aValue.Value <<= rDocService; + aValue.State = beans::PropertyState_DIRECT_VALUE; + args.push_back(aValue); + } + + args.insert(args.end(), rExtraArgs.begin(), rExtraArgs.end()); + + uno::Reference xComponent = mxDesktop->loadComponentFromURL( + rURL, "_default", 0, comphelper::containerToSequence(args)); + OUString sMessage = "loading failed: " + rURL; + CPPUNIT_ASSERT_MESSAGE(OUStringToOString(sMessage, RTL_TEXTENCODING_UTF8).getStr(), + xComponent.is()); + return xComponent; +} + +void MacrosTest::dispatchCommand(const uno::Reference& xComponent, + const OUString& rCommand, + const uno::Sequence& rPropertyValues) +{ + uno::Reference xController + = uno::Reference(xComponent, uno::UNO_QUERY_THROW)->getCurrentController(); + CPPUNIT_ASSERT(xController.is()); + uno::Reference xFrame(xController->getFrame(), uno::UNO_QUERY); + CPPUNIT_ASSERT(xFrame.is()); + + uno::Reference xContext = ::comphelper::getProcessComponentContext(); + uno::Reference xDispatchHelper(frame::DispatchHelper::create(xContext)); + CPPUNIT_ASSERT(xDispatchHelper.is()); + + xDispatchHelper->executeDispatch(xFrame, rCommand, OUString(), 0, rPropertyValues); +} + +std::unique_ptr MacrosTest::parseExportStream(const utl::TempFile& rTempFile, + const OUString& rStreamName) +{ + const OUString aUrl = rTempFile.GetURL(); + uno::Reference xComponentContext + = comphelper::getProcessComponentContext(); + uno::Reference const xZipNames( + packages::zip::ZipFileAccess::createWithURL(xComponentContext, aUrl)); + uno::Reference const xInputStream(xZipNames->getByName(rStreamName), + uno::UNO_QUERY); + std::unique_ptr pStream(utl::UcbStreamHelper::CreateStream(xInputStream, true)); + return pStream; +} + +void MacrosTest::setUpNssGpg(const test::Directories& rDirectories, const OUString& rTestName) +{ + OUString aSourceDir = rDirectories.getURLFromSrc(u"/test/signing-keys/"); + OUString aTargetDir = rDirectories.getURLFromWorkdir( + OUStringConcatenation("CppunitTest/" + rTestName + ".test.user")); + + // Set up NSS database in workdir/CppunitTest/ + osl::File::copy(aSourceDir + "cert9.db", aTargetDir + "/cert9.db"); + osl::File::copy(aSourceDir + "key4.db", aTargetDir + "/key4.db"); + osl::File::copy(aSourceDir + "pkcs11.txt", aTargetDir + "/pkcs11.txt"); + + // Make gpg use our own defined setup & keys + osl::File::copy(aSourceDir + "pubring.gpg", aTargetDir + "/pubring.gpg"); + osl::File::copy(aSourceDir + "random_seed", aTargetDir + "/random_seed"); + osl::File::copy(aSourceDir + "secring.gpg", aTargetDir + "/secring.gpg"); + osl::File::copy(aSourceDir + "trustdb.gpg", aTargetDir + "/trustdb.gpg"); + + OUString aTargetPath; + osl::FileBase::getSystemPathFromFileURL(aTargetDir, aTargetPath); + +#ifdef _WIN32 + // CryptoAPI test certificates + osl::File::copy(aSourceDir + "test.p7b", aTargetDir + "/test.p7b"); + OUString caVar("LIBO_TEST_CRYPTOAPI_PKCS7"); + osl_setEnvironment(caVar.pData, aTargetPath.pData); +#else + OUString mozCertVar("MOZILLA_CERTIFICATE_FOLDER"); + // explicit prefix with "sql:" needed for CentOS7 system NSS 3.67 + osl_setEnvironment(mozCertVar.pData, OUString("sql:" + aTargetPath).pData); +#endif + OUString gpgHomeVar("GNUPGHOME"); + osl_setEnvironment(gpgHomeVar.pData, aTargetPath.pData); + +#if HAVE_GPGCONF_SOCKETDIR + auto const ldPath = std::getenv("LIBO_LD_PATH"); + m_gpgconfCommandPrefix + = ldPath == nullptr ? OString() : OString::Concat("LD_LIBRARY_PATH=") + ldPath + " "; + OString path; + bool ok = aTargetPath.convertToString(&path, osl_getThreadTextEncoding(), + RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR + | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR); + // if conversion fails, at least provide a best-effort conversion in the message here, for + // context + CPPUNIT_ASSERT_MESSAGE(OUStringToOString(aTargetPath, RTL_TEXTENCODING_UTF8).getStr(), ok); + m_gpgconfCommandPrefix += "GNUPGHOME=" + path + " " GPGME_GPGCONF; + // HAVE_GPGCONF_SOCKETDIR is only defined in configure.ac for Linux for now, so (a) std::system + // behavior will conform to POSIX (and the relevant env var to set is named LD_LIBRARY_PATH), and + // (b) gpgconf --create-socketdir should return zero: + OString cmd = m_gpgconfCommandPrefix + " --create-socketdir"; + int res = std::system(cmd.getStr()); + CPPUNIT_ASSERT_EQUAL_MESSAGE(cmd.getStr(), 0, res); +#else + (void)this; +#endif +} + +void MacrosTest::tearDownNssGpg() +{ +#if HAVE_GPGCONF_SOCKETDIR + // HAVE_GPGCONF_SOCKETDIR is only defined in configure.ac for Linux for now, so (a) std::system + // behavior will conform to POSIX, and (b) gpgconf --remove-socketdir should return zero: + OString cmd = m_gpgconfCommandPrefix + " --remove-socketdir"; + int res = std::system(cmd.getStr()); + CPPUNIT_ASSERT_EQUAL_MESSAGE(cmd.getStr(), 0, res); +#else + (void)this; +#endif +} + +namespace +{ +struct Valid +{ + DateTime now; + OUString subjectName; + Valid(const css::uno::Sequence& rFilterData) + : now(DateTime::SYSTEM) + { + for (const auto& propVal : rFilterData) + { + if (propVal.Name == "SignCertificateSubjectName") + propVal.Value >>= subjectName; + } + } + bool operator()(const css::uno::Reference& cert) const + { + if (!now.IsBetween(cert->getNotValidBefore(), cert->getNotValidAfter())) + return false; + if (!subjectName.isEmpty() && subjectName != cert->getSubjectName()) + return false; + return true; + } +}; +} + +bool MacrosTest::IsValid(const css::uno::Reference& cert) +{ + const Valid test({}); + return test(cert); +} + +css::uno::Reference MacrosTest::GetValidCertificate( + const css::uno::Sequence>& certs, + const css::uno::Sequence& rFilterData) +{ + if (auto it = std::find_if(certs.begin(), certs.end(), Valid(rFilterData)); it != certs.end()) + return *it; + return {}; +} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotest/source/cpp/officeconnection.cxx b/unotest/source/cpp/officeconnection.cxx new file mode 100644 index 000000000..649d636d0 --- /dev/null +++ b/unotest/source/cpp/officeconnection.cxx @@ -0,0 +1,152 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace test { + +OfficeConnection::OfficeConnection(): process_(nullptr) {} + +OfficeConnection::~OfficeConnection() {} + +void OfficeConnection::setUp() { + css::uno::Reference< css::bridge::XUnoUrlResolver > resolver( + css::bridge::UnoUrlResolver::create( + cppu::defaultBootstrap_InitialComponentContext())); + OUString desc; + OUString argSoffice; + CPPUNIT_ASSERT( + getArgument( + u"soffice", + &argSoffice)); + if (argSoffice.match("path:")) { + desc = "pipe,name=" + osl::test::uniquePipeName("oootest"); + OUString noquickArg("--quickstart=no"); + OUString norestoreArg("--norestore"); + OUString nologoArg("--nologo"); + // disable use of the unix standalone splash screen app for the + // tests (probably not needed in combination with --headless?) + OUString headlessArg("--headless"); + OUString acceptArg("--accept=" + desc + ";urp"); + OUString argUser; + CPPUNIT_ASSERT( + getArgument(u"user", &argUser)); + OUString userArg("-env:UserInstallation=" + toAbsoluteFileUrl(argUser)); + OUString jreArg( + "-env:UNO_JAVA_JFW_ENV_JREHOME=true"); + rtl_uString * args[] = { + noquickArg.pData, norestoreArg.pData, + nologoArg.pData, headlessArg.pData, acceptArg.pData, userArg.pData, + jreArg.pData }; + rtl_uString ** envs = nullptr; + OUString argEnv; + if (getArgument(u"env", &argEnv)) + { + envs = &argEnv.pData; + } + // coverity[callee_ptr_arith] - arith is fine + CPPUNIT_ASSERT_EQUAL( + osl_Process_E_None, + osl_executeProcess( + toAbsoluteFileUrl( + argSoffice.copy(RTL_CONSTASCII_LENGTH("path:"))).pData, + args, SAL_N_ELEMENTS(args), 0, nullptr, nullptr, envs, envs == nullptr ? 0 : 1, + &process_)); + } else if (argSoffice.match("connect:")) { + desc = argSoffice.copy(RTL_CONSTASCII_LENGTH("connect:")); + } else { + CPPUNIT_FAIL( + "\"soffice\" argument starts with neither \"path:\" nor" + " \"connect:\""); + } + for (;;) { + try { + context_ = + css::uno::Reference< css::uno::XComponentContext >( + resolver->resolve( + "uno:" + desc + ";urp;StarOffice.ComponentContext"), + css::uno::UNO_QUERY_THROW); + break; + } catch (css::connection::NoConnectException &) {} + if (process_ != nullptr) { + TimeValue delay = { 1, 0 }; // 1 sec + CPPUNIT_ASSERT_EQUAL( + osl_Process_E_TimedOut, + osl_joinProcessWithTimeout(process_, &delay)); + } + } +} + +void OfficeConnection::tearDown() { + if (process_ == nullptr) + return; + + if (context_.is()) { + css::uno::Reference< css::frame::XDesktop2 > desktop = css::frame::Desktop::create( context_ ); + context_.clear(); + try { + CPPUNIT_ASSERT(desktop->terminate()); + desktop.clear(); + } catch (css::lang::DisposedException &) {} + // it appears that DisposedExceptions can already happen while + // receiving the response of the terminate call + } + CPPUNIT_ASSERT_EQUAL(osl_Process_E_None, osl_joinProcess(process_)); + oslProcessInfo info; + info.Size = sizeof info; + CPPUNIT_ASSERT_EQUAL( + osl_Process_E_None, + osl_getProcessInfo(process_, osl_Process_EXITCODE, &info)); + CPPUNIT_ASSERT_EQUAL(oslProcessExitCode(0), info.Code); + osl_freeProcessHandle(process_); + process_ = nullptr; // guard against subsequent calls to isStillAlive +} + + +bool OfficeConnection::isStillAlive() const { + if (process_ == nullptr) { + // In case "soffice" argument starts with "connect:" we have no direct + // control over the liveness of the soffice.bin process (would need to + // directly monitor the bridge) so can only assume the best here: + return true; + } + TimeValue delay = { 0, 0 }; // 0 sec + oslProcessError e = osl_joinProcessWithTimeout(process_, &delay); + CPPUNIT_ASSERT(e == osl_Process_E_None || e == osl_Process_E_TimedOut); + return e == osl_Process_E_TimedOut; +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotest/source/cpp/toabsolutefileurl.cxx b/unotest/source/cpp/toabsolutefileurl.cxx new file mode 100644 index 000000000..ea8b6f66c --- /dev/null +++ b/unotest/source/cpp/toabsolutefileurl.cxx @@ -0,0 +1,61 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include + +#include +#include +#include +#include +#include + +namespace test { + +OUString toAbsoluteFileUrl(OUString const & relativePathname) { + OUString cwd; + oslProcessError e1 = osl_getProcessWorkingDir(&cwd.pData); + if (e1 != osl_Process_E_None) { + throw css::uno::RuntimeException( + "osl_getProcessWorkingDir failed with " + OUString::number(e1)); + } + OUString url; + osl::FileBase::RC e2 = osl::FileBase::getFileURLFromSystemPath( + relativePathname, url); + if (e2 != osl::FileBase::E_None) { + throw css::uno::RuntimeException( + "osl::FileBase::getFileURLFromSystemPath(" + + relativePathname + + ") failed with " + + OUString::number(e2)); + } + OUString absUrl; + e2 = osl::FileBase::getAbsoluteFileURL(cwd, url, absUrl); + if (e2 != osl::FileBase::E_None) { + throw css::uno::RuntimeException( + "osl::FileBase::getAbsoluteFileURL(" + + cwd + ", " + url + + ") failed with " + + OUString::number(e2)); + } + return absUrl; +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotest/source/cpp/unobootstrapprotector/unobootstrapprotector.cxx b/unotest/source/cpp/unobootstrapprotector/unobootstrapprotector.cxx new file mode 100644 index 000000000..5ad175b9d --- /dev/null +++ b/unotest/source/cpp/unobootstrapprotector/unobootstrapprotector.cxx @@ -0,0 +1,93 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include + +#include +#include + +#include +#include +#include + +#include + +#include + +namespace { + +using namespace com::sun::star; + +//cppunit calls instantiates a new TextFixture for each test and calls setUp +//and tearDown on that for every test in a fixture + +//We basically need to call dispose on our root component context to +//shut down cleanly in the right order. + +//But we can't setup and tear down the root component context for +//every test because all the uno singletons will be invalid after +//the first dispose. So lets setup the default context once before +//all tests are run, and tear it down once after all have finished + +class Prot : public CppUnit::Protector +{ +public: + Prot(); + + virtual ~Prot() override; + + Prot(const Prot&) = delete; + Prot& operator=(const Prot&) = delete; + + virtual bool protect( + CppUnit::Functor const & functor, + CppUnit::ProtectorContext const & context) override; +private: + uno::Reference m_xContext; +}; + + +Prot::Prot() + : m_xContext(cppu::defaultBootstrap_InitialComponentContext()) +{ + uno::Reference xFactory = m_xContext->getServiceManager(); + uno::Reference xSFactory(xFactory, uno::UNO_QUERY_THROW); + + comphelper::setProcessServiceFactory(xSFactory); +} + +bool Prot::protect( + CppUnit::Functor const & functor, CppUnit::ProtectorContext const &) +{ + return functor(); +} + +Prot::~Prot() +{ + uno::Reference< lang::XComponent >(m_xContext, uno::UNO_QUERY_THROW)->dispose(); +} + +} + +extern "C" SAL_DLLPUBLIC_EXPORT CppUnit::Protector * unobootstrapprotector() +{ + return new Prot; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotest/source/cpp/unoexceptionprotector/unoexceptionprotector.cxx b/unotest/source/cpp/unoexceptionprotector/unoexceptionprotector.cxx new file mode 100644 index 000000000..7a5b17b75 --- /dev/null +++ b/unotest/source/cpp/unoexceptionprotector/unoexceptionprotector.cxx @@ -0,0 +1,81 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +// Best effort conversion: +std::string convert(std::u16string_view s16) { + OString s8(OUStringToOString(s16, osl_getThreadTextEncoding())); + static_assert(sizeof (sal_Int32) <= sizeof (std::string::size_type), "got to be at least equal"); + // ensure following cast is legitimate + return std::string( + s8.getStr(), static_cast< std::string::size_type >(s8.getLength())); +} + +class Prot : public CppUnit::Protector +{ +public: + Prot() {} + + Prot(const Prot&) = delete; + Prot& operator=(const Prot&) = delete; + + virtual bool protect( + CppUnit::Functor const & functor, + CppUnit::ProtectorContext const & context) override; +}; + +bool Prot::protect( + CppUnit::Functor const & functor, CppUnit::ProtectorContext const & context) +{ + try { + return functor(); + } catch (const css::uno::Exception &e) { + css::uno::Any a(cppu::getCaughtException()); + reportError( + context, + CppUnit::Message( + "An uncaught exception of type " + convert(a.getValueTypeName()), + convert(e.Message))); + } + return false; +} + +} + +extern "C" SAL_DLLPUBLIC_EXPORT CppUnit::Protector * +unoexceptionprotector() { + return new Prot; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3