LCOV - code coverage report
Current view: top level - libreoffice/unotest/source/cpp - filters-test.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 66 68 97.1 %
Date: 2012-12-27 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * Version: MPL 1.1 / GPLv3+ / LGPLv3+
       4             :  *
       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/
       9             :  *
      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.
      14             :  *
      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.
      19             :  *
      20             :  * Contributor(s):
      21             :  *   Caolán McNamara <caolanm@redhat.com>
      22             :  *
      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.
      28             :  */
      29             : 
      30             : #include <unotest/filters-test.hxx>
      31             : #include <osl/file.hxx>
      32             : #include <osl/thread.h>
      33             : #include <rtl/cipher.h>
      34             : 
      35             : #include "cppunit/TestAssert.h"
      36             : 
      37             : namespace test {
      38             : 
      39         118 : void decode(const rtl::OUString& rIn, const rtl::OUString &rOut)
      40             : {
      41         118 :     rtlCipher cipher = rtl_cipher_create(rtl_Cipher_AlgorithmARCFOUR, rtl_Cipher_ModeStream);
      42         118 :     CPPUNIT_ASSERT_MESSAGE("cipher creation failed", cipher != 0);
      43             : 
      44             :     //mcrypt --bare -a arcfour -o hex -k 435645 -s 3
      45         118 :     const sal_uInt8 aKey[3] = {'C', 'V', 'E'};
      46             : 
      47         118 :     rtlCipherError result = rtl_cipher_init(cipher, rtl_Cipher_DirectionDecode, aKey, SAL_N_ELEMENTS(aKey), 0, 0);
      48             : 
      49         118 :     CPPUNIT_ASSERT_MESSAGE("cipher init failed", result == rtl_Cipher_E_None);
      50             : 
      51         118 :     osl::File aIn(rIn);
      52         118 :     CPPUNIT_ASSERT(osl::FileBase::E_None == aIn.open(osl_File_OpenFlag_Read));
      53             : 
      54         118 :     osl::File aOut(rOut);
      55         118 :     CPPUNIT_ASSERT(osl::FileBase::E_None == aOut.open(osl_File_OpenFlag_Write));
      56             : 
      57             :     sal_uInt8 in[8192];
      58             :     sal_uInt8 out[8192];
      59             :     sal_uInt64 nBytesRead, nBytesWritten;
      60        2423 :     while(1)
      61             :     {
      62        2541 :         CPPUNIT_ASSERT(osl::FileBase::E_None == aIn.read(in, sizeof(in), nBytesRead));
      63        2541 :         if (!nBytesRead)
      64         118 :             break;
      65        2423 :         CPPUNIT_ASSERT(rtl_Cipher_E_None == rtl_cipher_decode(cipher, in, nBytesRead, out, sizeof(out)));
      66        2423 :         CPPUNIT_ASSERT(osl::FileBase::E_None == aOut.write(out, nBytesRead, nBytesWritten));
      67        2423 :         CPPUNIT_ASSERT(nBytesRead == nBytesWritten);
      68             :     }
      69             : 
      70         118 :     rtl_cipher_destroy(cipher);
      71         118 : }
      72             : 
      73          69 : 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)
      77             : {
      78          69 :     osl::Directory aDir(rURL);
      79             : 
      80          69 :     CPPUNIT_ASSERT(osl::FileBase::E_None == aDir.open());
      81          69 :     osl::DirectoryItem aItem;
      82          69 :     osl::FileStatus aFileStatus(osl_FileStatus_Mask_FileURL|osl_FileStatus_Mask_Type);
      83         340 :     while (aDir.getNextItem(aItem) == osl::FileBase::E_None)
      84             :     {
      85         202 :         aItem.getFileStatus(aFileStatus);
      86         202 :         rtl::OUString sURL = aFileStatus.getFileURL();
      87         202 :         if (aFileStatus.getFileType() == osl::FileStatus::Directory)
      88             :         {
      89             :             recursiveScan(nExpected, rFilter, sURL, rUserData,
      90           0 :                 nFilterFlags, nClipboardID, nFilterVersion);
      91             :         }
      92             :         else
      93             :         {
      94         202 :             rtl::OUString sTmpFile;
      95         202 :             bool bEncrypted = false;
      96             : 
      97         202 :             sal_Int32 nLastSlash = sURL.lastIndexOf('/');
      98             : 
      99         202 :             if ((nLastSlash != -1) && (nLastSlash+1 < sURL.getLength()))
     100             :             {
     101             :                 //ignore .files
     102         202 :                 if (sURL.getStr()[nLastSlash+1] == '.')
     103          60 :                     continue;
     104             : 
     105         319 :                 if (
     106         142 :                     (sURL.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("BID"), nLastSlash+1)) ||
     107         141 :                     (sURL.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("CVE"), nLastSlash+1)) ||
     108          36 :                     (sURL.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("EDB"), nLastSlash+1))
     109             :                    )
     110             :                 {
     111         118 :                     bEncrypted = true;
     112             :                 }
     113             :             }
     114             : 
     115             :             rtl::OString aRes(rtl::OUStringToOString(sURL,
     116         142 :                 osl_getThreadTextEncoding()));
     117             : 
     118         142 :             if (bEncrypted)
     119             :             {
     120         118 :                 CPPUNIT_ASSERT(osl::FileBase::E_None == osl::FileBase::createTempFile(NULL, NULL, &sTmpFile));
     121         118 :                 decode(sURL, sTmpFile);
     122         118 :                 sURL = sTmpFile;
     123             :             }
     124             : 
     125             :             //output name early, so in the case of a hang, the name of
     126             :             //the hanging input file is visible
     127         142 :             fprintf(stderr, "%s,", aRes.getStr());
     128         142 :             sal_uInt32 nStartTime = osl_getGlobalTimer();
     129             :             bool bRes = load(rFilter, sURL, rUserData, nFilterFlags,
     130         142 :                 nClipboardID, nFilterVersion);
     131         142 :             sal_uInt32 nEndTime = osl_getGlobalTimer();
     132             : 
     133         142 :             if (bEncrypted)
     134         118 :                 CPPUNIT_ASSERT(osl::FileBase::E_None == osl::File::remove(sTmpFile));
     135             : 
     136             :             fprintf(stderr, "%s,%" SAL_PRIuUINT32"\n",
     137         142 :                 bRes?"Pass":"Fail",nEndTime-nStartTime);
     138         142 :             if (nExpected == test::indeterminate)
     139           0 :                 continue;
     140         142 :             CPPUNIT_ASSERT_MESSAGE(aRes.getStr(), bRes == (nExpected == test::pass));
     141             :         }
     142         202 :     }
     143          69 :     CPPUNIT_ASSERT(osl::FileBase::E_None == aDir.close());
     144          69 : }
     145             : 
     146          23 : 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)
     150             : {
     151          23 :     fprintf(stderr, "File tested,Test Result,Execution Time (ms)\n");
     152             :     recursiveScan(test::pass, rFilter,
     153          46 :         rURL + rtl::OUString("pass"),
     154          23 :         rUserData, nFilterFlags, nClipboardID, nFilterVersion);
     155             :     recursiveScan(test::fail, rFilter,
     156          46 :         rURL + rtl::OUString("fail"),
     157          23 :         rUserData, nFilterFlags, nClipboardID, nFilterVersion);
     158             :     recursiveScan(test::indeterminate, rFilter,
     159          46 :         rURL + rtl::OUString("indeterminate"),
     160          23 :         rUserData, nFilterFlags, nClipboardID, nFilterVersion);
     161          23 : }
     162             : 
     163         111 : }
     164             : 
     165             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10