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

Generated by: LCOV version 1.10