Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / unotest / source / cpp / filters-test.cxx
blob07467a37f4159dda168436e8fd5135fec476b828
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Initial Developer of the Original Code is
16 * Caolán McNamara <caolanm@redhat.com>
17 * Portions created by the Initial Developer are Copyright (C) 2011 the
18 * Initial Developer. All Rights Reserved.
20 * Contributor(s):
21 * Caolán McNamara <caolanm@redhat.com>
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
25 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
26 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
27 * instead of those above.
30 #include <unotest/filters-test.hxx>
31 #include <osl/file.hxx>
32 #include <osl/thread.h>
33 #include <rtl/cipher.h>
35 #include "cppunit/TestAssert.h"
37 namespace test {
39 void decode(const rtl::OUString& rIn, const rtl::OUString &rOut)
41 rtlCipher cipher = rtl_cipher_create(rtl_Cipher_AlgorithmARCFOUR, rtl_Cipher_ModeStream);
42 CPPUNIT_ASSERT_MESSAGE("cipher creation failed", cipher != 0);
44 //mcrypt --bare -a arcfour -o hex -k 435645 -s 3
45 const sal_uInt8 aKey[3] = {'C', 'V', 'E'};
47 rtlCipherError result = rtl_cipher_init(cipher, rtl_Cipher_DirectionDecode, aKey, SAL_N_ELEMENTS(aKey), 0, 0);
49 CPPUNIT_ASSERT_MESSAGE("cipher init failed", result == rtl_Cipher_E_None);
51 osl::File aIn(rIn);
52 CPPUNIT_ASSERT(osl::FileBase::E_None == aIn.open(osl_File_OpenFlag_Read));
54 osl::File aOut(rOut);
55 CPPUNIT_ASSERT(osl::FileBase::E_None == aOut.open(osl_File_OpenFlag_Write));
57 sal_uInt8 in[8192];
58 sal_uInt8 out[8192];
59 sal_uInt64 nBytesRead, nBytesWritten;
60 while(1)
62 CPPUNIT_ASSERT(osl::FileBase::E_None == aIn.read(in, sizeof(in), nBytesRead));
63 if (!nBytesRead)
64 break;
65 CPPUNIT_ASSERT(rtl_Cipher_E_None == rtl_cipher_decode(cipher, in, nBytesRead, out, sizeof(out)));
66 CPPUNIT_ASSERT(osl::FileBase::E_None == aOut.write(out, nBytesRead, nBytesWritten));
67 CPPUNIT_ASSERT(nBytesRead == nBytesWritten);
70 rtl_cipher_destroy(cipher);
73 void FiltersTest::recursiveScan(filterStatus nExpected,
74 const rtl::OUString &rFilter, const rtl::OUString &rURL,
75 const rtl::OUString &rUserData, unsigned int nFilterFlags,
76 unsigned int nClipboardID, unsigned int nFilterVersion)
78 osl::Directory aDir(rURL);
80 CPPUNIT_ASSERT(osl::FileBase::E_None == aDir.open());
81 osl::DirectoryItem aItem;
82 osl::FileStatus aFileStatus(osl_FileStatus_Mask_FileURL|osl_FileStatus_Mask_Type);
83 while (aDir.getNextItem(aItem) == osl::FileBase::E_None)
85 aItem.getFileStatus(aFileStatus);
86 rtl::OUString sURL = aFileStatus.getFileURL();
87 if (aFileStatus.getFileType() == osl::FileStatus::Directory)
89 recursiveScan(nExpected, rFilter, sURL, rUserData,
90 nFilterFlags, nClipboardID, nFilterVersion);
92 else
94 rtl::OUString sTmpFile;
95 bool bEncrypted = false;
97 sal_Int32 nLastSlash = sURL.lastIndexOf('/');
99 if ((nLastSlash != -1) && (nLastSlash+1 < sURL.getLength()))
101 //ignore .files
102 if (sURL.getStr()[nLastSlash+1] == '.')
103 continue;
105 if (
106 (sURL.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("BID"), nLastSlash+1)) ||
107 (sURL.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("CVE"), nLastSlash+1)) ||
108 (sURL.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("EDB"), nLastSlash+1))
111 bEncrypted = true;
115 rtl::OString aRes(rtl::OUStringToOString(sURL,
116 osl_getThreadTextEncoding()));
118 if (bEncrypted)
120 CPPUNIT_ASSERT(osl::FileBase::E_None == osl::FileBase::createTempFile(NULL, NULL, &sTmpFile));
121 decode(sURL, sTmpFile);
122 sURL = sTmpFile;
125 //output name early, so in the case of a hang, the name of
126 //the hanging input file is visible
127 fprintf(stderr, "%s,", aRes.getStr());
128 sal_uInt32 nStartTime = osl_getGlobalTimer();
129 bool bRes = load(rFilter, sURL, rUserData, nFilterFlags,
130 nClipboardID, nFilterVersion);
131 sal_uInt32 nEndTime = osl_getGlobalTimer();
133 if (bEncrypted)
134 CPPUNIT_ASSERT(osl::FileBase::E_None == osl::File::remove(sTmpFile));
136 fprintf(stderr, "%s,%" SAL_PRIuUINT32"\n",
137 bRes?"Pass":"Fail",nEndTime-nStartTime);
138 if (nExpected == test::indeterminate)
139 continue;
140 CPPUNIT_ASSERT_MESSAGE(aRes.getStr(), bRes == (nExpected == test::pass));
143 CPPUNIT_ASSERT(osl::FileBase::E_None == aDir.close());
146 void FiltersTest::testDir(const rtl::OUString &rFilter,
147 const rtl::OUString &rURL, const rtl::OUString &rUserData,
148 unsigned int nFilterFlags, unsigned int nClipboardID,
149 unsigned int nFilterVersion)
151 fprintf(stderr, "File tested,Test Result,Execution Time (ms)\n");
152 recursiveScan(test::pass, rFilter,
153 rURL + rtl::OUString("pass"),
154 rUserData, nFilterFlags, nClipboardID, nFilterVersion);
155 recursiveScan(test::fail, rFilter,
156 rURL + rtl::OUString("fail"),
157 rUserData, nFilterFlags, nClipboardID, nFilterVersion);
158 recursiveScan(test::indeterminate, rFilter,
159 rURL + rtl::OUString("indeterminate"),
160 rUserData, nFilterFlags, nClipboardID, nFilterVersion);
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */