Line data Source code
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 <unotest/filters-test.hxx>
11 : #include <osl/file.hxx>
12 : #include <osl/thread.h>
13 : #include <rtl/cipher.h>
14 :
15 : #include "cppunit/TestAssert.h"
16 :
17 : namespace test {
18 :
19 132 : void decode(const OUString& rIn, const OUString &rOut)
20 : {
21 132 : rtlCipher cipher = rtl_cipher_create(rtl_Cipher_AlgorithmARCFOUR, rtl_Cipher_ModeStream);
22 132 : CPPUNIT_ASSERT_MESSAGE("cipher creation failed", cipher != 0);
23 :
24 : //mcrypt --bare -a arcfour -o hex -k 435645 -s 3
25 132 : const sal_uInt8 aKey[3] = {'C', 'V', 'E'};
26 :
27 132 : rtlCipherError result = rtl_cipher_init(cipher, rtl_Cipher_DirectionDecode, aKey, SAL_N_ELEMENTS(aKey), 0, 0);
28 :
29 132 : CPPUNIT_ASSERT_MESSAGE("cipher init failed", result == rtl_Cipher_E_None);
30 :
31 132 : osl::File aIn(rIn);
32 132 : CPPUNIT_ASSERT(osl::FileBase::E_None == aIn.open(osl_File_OpenFlag_Read));
33 :
34 264 : osl::File aOut(rOut);
35 132 : CPPUNIT_ASSERT(osl::FileBase::E_None == aOut.open(osl_File_OpenFlag_Write));
36 :
37 : sal_uInt8 in[8192];
38 : sal_uInt8 out[8192];
39 : sal_uInt64 nBytesRead, nBytesWritten;
40 : while(true)
41 : {
42 3082 : CPPUNIT_ASSERT(osl::FileBase::E_None == aIn.read(in, sizeof(in), nBytesRead));
43 3082 : if (!nBytesRead)
44 132 : break;
45 2950 : CPPUNIT_ASSERT(rtl_Cipher_E_None == rtl_cipher_decode(cipher, in, nBytesRead, out, sizeof(out)));
46 2950 : CPPUNIT_ASSERT(osl::FileBase::E_None == aOut.write(out, nBytesRead, nBytesWritten));
47 2950 : CPPUNIT_ASSERT(nBytesRead == nBytesWritten);
48 : }
49 :
50 264 : rtl_cipher_destroy(cipher);
51 132 : }
52 :
53 126 : void FiltersTest::recursiveScan(filterStatus nExpected,
54 : const OUString &rFilter, const OUString &rURL,
55 : const OUString &rUserData, unsigned int nFilterFlags,
56 : unsigned int nClipboardID, unsigned int nFilterVersion, bool bExport)
57 : {
58 126 : osl::Directory aDir(rURL);
59 :
60 126 : CPPUNIT_ASSERT(osl::FileBase::E_None == aDir.open());
61 252 : osl::DirectoryItem aItem;
62 252 : osl::FileStatus aFileStatus(osl_FileStatus_Mask_FileURL|osl_FileStatus_Mask_Type);
63 578 : while (aDir.getNextItem(aItem) == osl::FileBase::E_None)
64 : {
65 326 : aItem.getFileStatus(aFileStatus);
66 326 : OUString sURL = aFileStatus.getFileURL();
67 326 : if (aFileStatus.getFileType() == osl::FileStatus::Directory)
68 : {
69 : recursiveScan(nExpected, rFilter, sURL, rUserData,
70 0 : nFilterFlags, nClipboardID, nFilterVersion, bExport);
71 : }
72 : else
73 : {
74 326 : OUString sTmpFile;
75 326 : bool bEncrypted = false;
76 :
77 326 : sal_Int32 nLastSlash = sURL.lastIndexOf('/');
78 :
79 326 : if ((nLastSlash != -1) && (nLastSlash+1 < sURL.getLength()))
80 : {
81 : //ignore .files
82 326 : if (sURL[nLastSlash+1] == '.')
83 104 : continue;
84 :
85 222 : if (
86 443 : (sURL.match("BID", nLastSlash+1)) ||
87 332 : (sURL.match("CVE", nLastSlash+1)) ||
88 110 : (sURL.match("EDB", nLastSlash+1))
89 : )
90 : {
91 132 : bEncrypted = true;
92 : }
93 : }
94 :
95 : OString aRes(OUStringToOString(sURL,
96 444 : osl_getThreadTextEncoding()));
97 :
98 222 : if (bEncrypted)
99 : {
100 132 : CPPUNIT_ASSERT(osl::FileBase::E_None == osl::FileBase::createTempFile(NULL, NULL, &sTmpFile));
101 132 : decode(sURL, sTmpFile);
102 132 : sURL = sTmpFile;
103 : }
104 :
105 : //output name early, so in the case of a hang, the name of
106 : //the hanging input file is visible
107 222 : fprintf(stderr, "%s,", aRes.getStr());
108 222 : sal_uInt32 nStartTime = osl_getGlobalTimer();
109 : bool bRes;
110 222 : if (!bExport)
111 : bRes = load(rFilter, sURL, rUserData, nFilterFlags,
112 217 : nClipboardID, nFilterVersion);
113 : else
114 : bRes = save(rFilter, sURL, rUserData, nFilterFlags,
115 5 : nClipboardID, nFilterVersion);
116 222 : sal_uInt32 nEndTime = osl_getGlobalTimer();
117 :
118 222 : if (bEncrypted)
119 132 : CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, osl::File::remove(sTmpFile));
120 :
121 : fprintf(stderr, "%s,%" SAL_PRIuUINT32"\n",
122 222 : bRes?"Pass":"Fail",nEndTime-nStartTime);
123 222 : if (nExpected == test::indeterminate)
124 0 : continue;
125 444 : CPPUNIT_ASSERT_MESSAGE(aRes.getStr(), bRes == (nExpected == test::pass));
126 : }
127 222 : }
128 252 : CPPUNIT_ASSERT(osl::FileBase::E_None == aDir.close());
129 126 : }
130 :
131 42 : void FiltersTest::testDir(const OUString &rFilter,
132 : const OUString &rURL, const OUString &rUserData,
133 : unsigned int nFilterFlags, unsigned int nClipboardID,
134 : unsigned int nFilterVersion, bool bExport)
135 : {
136 42 : fprintf(stderr, "File tested,Test Result,Execution Time (ms)\n");
137 : recursiveScan(test::pass, rFilter,
138 84 : rURL + "pass",
139 84 : rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport);
140 : recursiveScan(test::fail, rFilter,
141 84 : rURL + "fail",
142 84 : rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport);
143 : recursiveScan(test::indeterminate, rFilter,
144 84 : rURL + "indeterminate",
145 84 : rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport);
146 42 : }
147 :
148 261 : }
149 :
150 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|