Bump version to 5.0-14
[LibreOffice.git] / unotest / source / cpp / filters-test.cxx
blobbc4292c989fc99bc08b1faa88ea8993965e261b9
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/filters-test.hxx>
11 #include <osl/file.hxx>
12 #include <osl/thread.h>
13 #include <rtl/cipher.h>
15 #include "cppunit/TestAssert.h"
17 namespace test {
19 void decode(const OUString& rIn, const OUString &rOut)
21 rtlCipher cipher = rtl_cipher_create(rtl_Cipher_AlgorithmARCFOUR, rtl_Cipher_ModeStream);
22 CPPUNIT_ASSERT_MESSAGE("cipher creation failed", cipher != 0);
24 //mcrypt --bare -a arcfour -o hex -k 435645 -s 3
25 const sal_uInt8 aKey[3] = {'C', 'V', 'E'};
27 rtlCipherError result = rtl_cipher_init(cipher, rtl_Cipher_DirectionDecode, aKey, SAL_N_ELEMENTS(aKey), 0, 0);
29 CPPUNIT_ASSERT_MESSAGE("cipher init failed", result == rtl_Cipher_E_None);
31 osl::File aIn(rIn);
32 CPPUNIT_ASSERT(osl::FileBase::E_None == aIn.open(osl_File_OpenFlag_Read));
34 osl::File aOut(rOut);
35 CPPUNIT_ASSERT(osl::FileBase::E_None == aOut.open(osl_File_OpenFlag_Write));
37 sal_uInt8 in[8192];
38 sal_uInt8 out[8192];
39 sal_uInt64 nBytesRead, nBytesWritten;
40 while(true)
42 CPPUNIT_ASSERT(osl::FileBase::E_None == aIn.read(in, sizeof(in), nBytesRead));
43 if (!nBytesRead)
44 break;
45 CPPUNIT_ASSERT(rtl_Cipher_E_None == rtl_cipher_decode(cipher, in, nBytesRead, out, sizeof(out)));
46 CPPUNIT_ASSERT(osl::FileBase::E_None == aOut.write(out, nBytesRead, nBytesWritten));
47 CPPUNIT_ASSERT(nBytesRead == nBytesWritten);
50 rtl_cipher_destroy(cipher);
53 void FiltersTest::recursiveScan(filterStatus nExpected,
54 const OUString &rFilter, const OUString &rURL,
55 const OUString &rUserData, SfxFilterFlags nFilterFlags,
56 SotClipboardFormatId nClipboardID, unsigned int nFilterVersion, bool bExport)
58 osl::Directory aDir(rURL);
60 CPPUNIT_ASSERT(osl::FileBase::E_None == aDir.open());
61 osl::DirectoryItem aItem;
62 osl::FileStatus aFileStatus(osl_FileStatus_Mask_FileURL|osl_FileStatus_Mask_Type);
63 while (aDir.getNextItem(aItem) == osl::FileBase::E_None)
65 aItem.getFileStatus(aFileStatus);
66 OUString sURL = aFileStatus.getFileURL();
67 if (aFileStatus.getFileType() == osl::FileStatus::Directory)
69 recursiveScan(nExpected, rFilter, sURL, rUserData,
70 nFilterFlags, nClipboardID, nFilterVersion, bExport);
72 else
74 OUString sTmpFile;
75 bool bEncrypted = false;
77 sal_Int32 nLastSlash = sURL.lastIndexOf('/');
79 if ((nLastSlash != -1) && (nLastSlash+1 < sURL.getLength()))
81 //ignore .files
82 if (sURL[nLastSlash+1] == '.')
83 continue;
85 if (
86 (sURL.match("BID", nLastSlash+1)) ||
87 (sURL.match("CVE", nLastSlash+1)) ||
88 (sURL.match("EDB", nLastSlash+1))
91 bEncrypted = true;
95 OString aRes(OUStringToOString(sURL,
96 osl_getThreadTextEncoding()));
98 if (bEncrypted)
100 CPPUNIT_ASSERT(osl::FileBase::E_None == osl::FileBase::createTempFile(NULL, NULL, &sTmpFile));
101 decode(sURL, sTmpFile);
102 sURL = sTmpFile;
105 //output name early, so in the case of a hang, the name of
106 //the hanging input file is visible
107 fprintf(stderr, "%s,", aRes.getStr());
108 sal_uInt32 nStartTime = osl_getGlobalTimer();
109 bool bRes;
110 if (!bExport)
111 bRes = load(rFilter, sURL, rUserData, nFilterFlags,
112 nClipboardID, nFilterVersion);
113 else
114 bRes = save(rFilter, sURL, rUserData, nFilterFlags,
115 nClipboardID, nFilterVersion);
116 sal_uInt32 nEndTime = osl_getGlobalTimer();
118 if (bEncrypted)
119 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, osl::File::remove(sTmpFile));
121 fprintf(stderr, "%s,%" SAL_PRIuUINT32"\n",
122 bRes?"Pass":"Fail",nEndTime-nStartTime);
123 if (nExpected == test::indeterminate)
124 continue;
125 filterStatus nResult = bRes ? test::pass : test::fail;
126 CPPUNIT_ASSERT_MESSAGE(aRes.getStr(), nResult == nExpected);
129 CPPUNIT_ASSERT(osl::FileBase::E_None == aDir.close());
132 void FiltersTest::testDir(const OUString &rFilter,
133 const OUString &rURL, const OUString &rUserData,
134 SfxFilterFlags nFilterFlags, SotClipboardFormatId nClipboardID,
135 unsigned int nFilterVersion, bool bExport)
137 fprintf(stderr, "File tested,Test Result,Execution tools::Time (ms)\n");
138 recursiveScan(test::pass, rFilter,
139 rURL + "pass",
140 rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport);
141 recursiveScan(test::fail, rFilter,
142 rURL + "fail",
143 rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport);
144 recursiveScan(test::indeterminate, rFilter,
145 rURL + "indeterminate",
146 rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport);
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */