LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / unotest / source / cpp / filters-test.cxx
blob7adbd9ca4d4d839ef6314482e7d13f45986085f4
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 <sal/config.h>
12 #include <set>
13 #include <string_view>
15 #include <unotest/filters-test.hxx>
16 #include <osl/file.hxx>
17 #include <osl/thread.h>
18 #include <rtl/cipher.h>
20 #include <cppunit/TestAssert.h>
22 namespace test {
24 static void decode(const OUString& rIn, const OUString &rOut)
26 rtlCipher cipher = rtl_cipher_create(rtl_Cipher_AlgorithmARCFOUR, rtl_Cipher_ModeStream);
27 CPPUNIT_ASSERT_MESSAGE("cipher creation failed", cipher != nullptr);
29 //mcrypt --bare -a arcfour -o hex -k 435645 -s 3
30 const sal_uInt8 aKey[3] = {'C', 'V', 'E'};
32 rtlCipherError result = rtl_cipher_init(cipher, rtl_Cipher_DirectionDecode, aKey, SAL_N_ELEMENTS(aKey), nullptr, 0);
34 CPPUNIT_ASSERT_EQUAL_MESSAGE("cipher init failed", rtl_Cipher_E_None, result);
36 osl::File aIn(rIn);
37 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aIn.open(osl_File_OpenFlag_Read));
39 osl::File aOut(rOut);
40 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aOut.open(osl_File_OpenFlag_Write));
42 sal_uInt8 in[8192];
43 sal_uInt8 out[8192];
44 sal_uInt64 nBytesRead, nBytesWritten;
45 while(true)
47 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aIn.read(in, sizeof(in), nBytesRead));
48 if (!nBytesRead)
49 break;
50 CPPUNIT_ASSERT_EQUAL(rtl_Cipher_E_None, rtl_cipher_decode(cipher, in, nBytesRead, out, sizeof(out)));
51 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aOut.write(out, nBytesRead, nBytesWritten));
52 CPPUNIT_ASSERT_EQUAL(nBytesRead, nBytesWritten);
55 rtl_cipher_destroy(cipher);
58 void FiltersTest::recursiveScan(filterStatus nExpected,
59 const OUString &rFilter, const OUString &rURL,
60 const OUString &rUserData, SfxFilterFlags nFilterFlags,
61 SotClipboardFormatId nClipboardID, unsigned int nFilterVersion, bool bExport)
63 osl::Directory aDir(rURL);
65 CPPUNIT_ASSERT_EQUAL_MESSAGE(OUString("Failed to open directory " + rURL).toUtf8().getStr(), osl::FileBase::E_None, aDir.open());
66 osl::DirectoryItem aItem;
67 osl::FileStatus aFileStatus(osl_FileStatus_Mask_FileURL|osl_FileStatus_Mask_Type);
68 std::set<OUString> dirs;
69 std::set<OUString> files;
70 while (aDir.getNextItem(aItem) == osl::FileBase::E_None)
72 aItem.getFileStatus(aFileStatus);
73 OUString sURL = aFileStatus.getFileURL();
74 if (aFileStatus.getFileType() == osl::FileStatus::Directory)
76 dirs.insert(sURL);
78 else
80 files.insert(sURL);
83 for (auto const & sURL: dirs) {
84 recursiveScan(nExpected, rFilter, sURL, rUserData,
85 nFilterFlags, nClipboardID, nFilterVersion, bExport);
87 for (auto const & sURL: files) {
88 OUString sTmpFile;
89 bool bEncrypted = false;
91 sal_Int32 nLastSlash = sURL.lastIndexOf('/');
93 if ((nLastSlash != -1) && (nLastSlash+1 < sURL.getLength()))
95 //ignore .files
96 if (sURL[nLastSlash+1] == '.')
97 continue;
99 if (
100 (sURL.match("BID", nLastSlash+1)) ||
101 (sURL.match("CVE", nLastSlash+1)) ||
102 (sURL.match("EDB", nLastSlash+1)) ||
103 (sURL.match("RC4", nLastSlash+1)) // just means "encrypted"
106 bEncrypted = true;
110 OString aRes(
111 OString::Concat(bExport ? std::string_view("save") : std::string_view("load")) + " "
112 + OUStringToOString(sURL, osl_getThreadTextEncoding()));
114 OUString realUrl;
115 if (bEncrypted)
117 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, osl::FileBase::createTempFile(nullptr, nullptr, &sTmpFile));
118 decode(sURL, sTmpFile);
119 realUrl = sTmpFile;
121 else
123 realUrl = sURL;
126 //output name early, so in the case of a hang, the name of
127 //the hanging input file is visible
128 fprintf(stderr, "Testing %s:\n", aRes.getStr());
129 sal_uInt32 nStartTime = osl_getGlobalTimer();
130 bool bRes;
131 if (!bExport)
132 bRes = load(rFilter, realUrl, rUserData, nFilterFlags,
133 nClipboardID, nFilterVersion);
134 else
135 bRes = save(rFilter, realUrl, rUserData, nFilterFlags,
136 nClipboardID, nFilterVersion);
137 sal_uInt32 nEndTime = osl_getGlobalTimer();
139 if (bEncrypted)
140 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, osl::File::remove(sTmpFile));
142 fprintf(stderr, "Tested %s: %s (%" SAL_PRIuUINT32 "ms)\n",
143 aRes.getStr(), bRes?"Pass":"Fail", nEndTime-nStartTime);
144 if (nExpected == test::indeterminate)
145 continue;
146 filterStatus nResult = bRes ? test::pass : test::fail;
147 CPPUNIT_ASSERT_EQUAL_MESSAGE(aRes.getStr(), nExpected, nResult);
149 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aDir.close());
152 void FiltersTest::testDir(const OUString &rFilter,
153 std::u16string_view rURL, const OUString &rUserData,
154 SfxFilterFlags nFilterFlags, SotClipboardFormatId nClipboardID,
155 unsigned int nFilterVersion, bool bExport)
157 recursiveScan(test::pass, rFilter,
158 OUString::Concat(rURL) + "pass",
159 rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport);
160 recursiveScan(test::fail, rFilter,
161 OUString::Concat(rURL) + "fail",
162 rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport);
163 recursiveScan(test::indeterminate, rFilter,
164 OUString::Concat(rURL) + "indeterminate",
165 rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport);
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */