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