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 169 : void decode(const OUString& rIn, const OUString &rOut)
20 : {
21 169 : rtlCipher cipher = rtl_cipher_create(rtl_Cipher_AlgorithmARCFOUR, rtl_Cipher_ModeStream);
22 169 : CPPUNIT_ASSERT_MESSAGE("cipher creation failed", cipher != 0);
23 :
24 : //mcrypt --bare -a arcfour -o hex -k 435645 -s 3
25 169 : const sal_uInt8 aKey[3] = {'C', 'V', 'E'};
26 :
27 169 : rtlCipherError result = rtl_cipher_init(cipher, rtl_Cipher_DirectionDecode, aKey, SAL_N_ELEMENTS(aKey), 0, 0);
28 :
29 169 : CPPUNIT_ASSERT_MESSAGE("cipher init failed", result == rtl_Cipher_E_None);
30 :
31 169 : osl::File aIn(rIn);
32 169 : CPPUNIT_ASSERT(osl::FileBase::E_None == aIn.open(osl_File_OpenFlag_Read));
33 :
34 338 : osl::File aOut(rOut);
35 169 : 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 4303 : CPPUNIT_ASSERT(osl::FileBase::E_None == aIn.read(in, sizeof(in), nBytesRead));
43 4303 : if (!nBytesRead)
44 169 : break;
45 4134 : CPPUNIT_ASSERT(rtl_Cipher_E_None == rtl_cipher_decode(cipher, in, nBytesRead, out, sizeof(out)));
46 4134 : CPPUNIT_ASSERT(osl::FileBase::E_None == aOut.write(out, nBytesRead, nBytesWritten));
47 4134 : CPPUNIT_ASSERT(nBytesRead == nBytesWritten);
48 : }
49 :
50 338 : rtl_cipher_destroy(cipher);
51 169 : }
52 :
53 189 : 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)
57 : {
58 189 : osl::Directory aDir(rURL);
59 :
60 189 : CPPUNIT_ASSERT(osl::FileBase::E_None == aDir.open());
61 378 : osl::DirectoryItem aItem;
62 378 : osl::FileStatus aFileStatus(osl_FileStatus_Mask_FileURL|osl_FileStatus_Mask_Type);
63 931 : while (aDir.getNextItem(aItem) == osl::FileBase::E_None)
64 : {
65 553 : aItem.getFileStatus(aFileStatus);
66 553 : OUString sURL = aFileStatus.getFileURL();
67 553 : if (aFileStatus.getFileType() == osl::FileStatus::Directory)
68 : {
69 : recursiveScan(nExpected, rFilter, sURL, rUserData,
70 0 : nFilterFlags, nClipboardID, nFilterVersion, bExport);
71 : }
72 : else
73 : {
74 553 : OUString sTmpFile;
75 553 : bool bEncrypted = false;
76 :
77 553 : sal_Int32 nLastSlash = sURL.lastIndexOf('/');
78 :
79 553 : if ((nLastSlash != -1) && (nLastSlash+1 < sURL.getLength()))
80 : {
81 : //ignore .files
82 553 : if (sURL[nLastSlash+1] == '.')
83 152 : continue;
84 :
85 401 : if (
86 801 : (sURL.match("BID", nLastSlash+1)) ||
87 663 : (sURL.match("CVE", nLastSlash+1)) ||
88 262 : (sURL.match("EDB", nLastSlash+1))
89 : )
90 : {
91 169 : bEncrypted = true;
92 : }
93 : }
94 :
95 : OString aRes(OUStringToOString(sURL,
96 794 : osl_getThreadTextEncoding()));
97 :
98 401 : if (bEncrypted)
99 : {
100 169 : CPPUNIT_ASSERT(osl::FileBase::E_None == osl::FileBase::createTempFile(NULL, NULL, &sTmpFile));
101 169 : decode(sURL, sTmpFile);
102 169 : 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 401 : fprintf(stderr, "%s,", aRes.getStr());
108 401 : sal_uInt32 nStartTime = osl_getGlobalTimer();
109 : bool bRes;
110 401 : if (!bExport)
111 : bRes = load(rFilter, sURL, rUserData, nFilterFlags,
112 389 : nClipboardID, nFilterVersion);
113 : else
114 : bRes = save(rFilter, sURL, rUserData, nFilterFlags,
115 12 : nClipboardID, nFilterVersion);
116 401 : sal_uInt32 nEndTime = osl_getGlobalTimer();
117 :
118 401 : if (bEncrypted)
119 169 : CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, osl::File::remove(sTmpFile));
120 :
121 : fprintf(stderr, "%s,%" SAL_PRIuUINT32"\n",
122 401 : bRes?"Pass":"Fail",nEndTime-nStartTime);
123 401 : if (nExpected == test::indeterminate)
124 8 : continue;
125 393 : filterStatus nResult = bRes ? test::pass : test::fail;
126 786 : CPPUNIT_ASSERT_MESSAGE(aRes.getStr(), nResult == nExpected);
127 : }
128 393 : }
129 378 : CPPUNIT_ASSERT(osl::FileBase::E_None == aDir.close());
130 189 : }
131 :
132 63 : void FiltersTest::testDir(const OUString &rFilter,
133 : const OUString &rURL, const OUString &rUserData,
134 : SfxFilterFlags nFilterFlags, SotClipboardFormatId nClipboardID,
135 : unsigned int nFilterVersion, bool bExport)
136 : {
137 63 : fprintf(stderr, "File tested,Test Result,Execution tools::Time (ms)\n");
138 : recursiveScan(test::pass, rFilter,
139 126 : rURL + "pass",
140 126 : rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport);
141 : recursiveScan(test::fail, rFilter,
142 126 : rURL + "fail",
143 126 : rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport);
144 : recursiveScan(test::indeterminate, rFilter,
145 126 : rURL + "indeterminate",
146 126 : rUserData, nFilterFlags, nClipboardID, nFilterVersion, bExport);
147 63 : }
148 :
149 402 : }
150 :
151 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|