LibreOffice Module unotest (master) 1
filters-test.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 */
9
10#include <sal/config.h>
11
12#include <set>
13#include <string_view>
14
16#include <osl/file.hxx>
17#include <osl/thread.h>
18#include <rtl/cipher.h>
19
20#include <cppunit/TestAssert.h>
21
22namespace test {
23
24static void decode(const OUString& rIn, const OUString &rOut)
25{
26 rtlCipher cipher = rtl_cipher_create(rtl_Cipher_AlgorithmARCFOUR, rtl_Cipher_ModeStream);
27 CPPUNIT_ASSERT_MESSAGE("cipher creation failed", cipher != nullptr);
28
29 //mcrypt --bare -a arcfour -o hex -k 435645 -s 3
30 const sal_uInt8 aKey[3] = {'C', 'V', 'E'};
31
32 rtlCipherError result = rtl_cipher_init(cipher, rtl_Cipher_DirectionDecode, aKey, std::size(aKey), nullptr, 0);
33
34 CPPUNIT_ASSERT_EQUAL_MESSAGE("cipher init failed", rtl_Cipher_E_None, result);
35
36 osl::File aIn(rIn);
37 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aIn.open(osl_File_OpenFlag_Read));
38
39 osl::File aOut(rOut);
40 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aOut.open(osl_File_OpenFlag_Write));
41
42 sal_uInt8 in[8192];
43 sal_uInt8 out[8192];
44 sal_uInt64 nBytesRead, nBytesWritten;
45 while(true)
46 {
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);
53 }
54
55 rtl_cipher_destroy(cipher);
56}
57
59 const OUString &rFilter, const OUString &rURL,
60 const OUString &rUserData, SfxFilterFlags nFilterFlags,
61 SotClipboardFormatId nClipboardID, unsigned int nFilterVersion, bool bExport)
62{
63 osl::Directory aDir(rURL);
64
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)
71 {
72 aItem.getFileStatus(aFileStatus);
73 OUString sURL = aFileStatus.getFileURL();
74 if (aFileStatus.getFileType() == osl::FileStatus::Directory)
75 {
76 dirs.insert(sURL);
77 }
78 else
79 {
80 files.insert(sURL);
81 }
82 }
83 for (auto const & sURL: dirs) {
84 recursiveScan(nExpected, rFilter, sURL, rUserData,
85 nFilterFlags, nClipboardID, nFilterVersion, bExport);
86 }
87 for (auto const & sURL: files) {
88 OUString sTmpFile;
89 bool bEncrypted = false;
90
91 sal_Int32 nLastSlash = sURL.lastIndexOf('/');
92
93 if ((nLastSlash != -1) && (nLastSlash+1 < sURL.getLength()))
94 {
95 //ignore .files
96 if (sURL[nLastSlash+1] == '.')
97 continue;
98
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"
104 )
105 {
106 bEncrypted = true;
107 }
108 }
109
110 OString aRes(
111 OString::Concat(bExport ? std::string_view("save") : std::string_view("load")) + " "
112 + OUStringToOString(sURL, osl_getThreadTextEncoding()));
113
114 OUString realUrl;
115 if (bEncrypted)
116 {
117 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, osl::FileBase::createTempFile(nullptr, nullptr, &sTmpFile));
118 decode(sURL, sTmpFile);
119 realUrl = sTmpFile;
120 }
121 else
122 {
123 realUrl = sURL;
124 }
125
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();
138
139 if (bEncrypted)
140 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, osl::File::remove(sTmpFile));
141
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);
148 }
149 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aDir.close());
150}
151
152void FiltersTest::testDir(const OUString &rFilter,
153 std::u16string_view rURL, const OUString &rUserData,
154 SfxFilterFlags nFilterFlags, SotClipboardFormatId nClipboardID,
155 unsigned int nFilterVersion, bool bExport)
156{
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);
164 OUString::Concat(rURL) + "indeterminate",
165 rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport);
166}
167
168}
169
170/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool save(const OUString &, const OUString &, const OUString &, SfxFilterFlags, SotClipboardFormatId, unsigned int)
void recursiveScan(filterStatus nExpected, const OUString &rFilter, const OUString &rURL, const OUString &rUserData, SfxFilterFlags nFilterFlags, SotClipboardFormatId nClipboardID, unsigned int nFilterVersion, bool bExport)
virtual bool load(const OUString &rFilter, const OUString &rURL, const OUString &rUserData, SfxFilterFlags nFilterFlags, SotClipboardFormatId nClipboardID, unsigned int nFilterVersion)=0
void testDir(const OUString &rFilter, std::u16string_view rURL, const OUString &rUserData=OUString(), SfxFilterFlags nFilterFlags=SfxFilterFlags::IMPORT, SotClipboardFormatId nClipboardID=SotClipboardFormatId::NONE, unsigned int nFilterVersion=0, bool bExport=false)
SfxFilterFlags
SotClipboardFormatId
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
static void decode(const OUString &rIn, const OUString &rOut)
@ indeterminate
unsigned char sal_uInt8
Any result