LCOV - code coverage report
Current view: top level - writerperfect/qa/unit - WPXSvStreamTest.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 156 156 100.0 %
Date: 2014-04-11 Functions: 16 17 94.1 %
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             : 
      10             : #include <algorithm>
      11             : #include <cassert>
      12             : 
      13             : #include <boost/shared_ptr.hpp>
      14             : 
      15             : #include <cppunit/extensions/HelperMacros.h>
      16             : #include <cppunit/plugin/TestPlugIn.h>
      17             : 
      18             : #include "com/sun/star/io/XInputStream.hpp"
      19             : #include "com/sun/star/ucb/XSimpleFileAccess.hpp"
      20             : #include "com/sun/star/uno/Reference.hxx"
      21             : 
      22             : #include "comphelper/processfactory.hxx"
      23             : #include "comphelper/seqstream.hxx"
      24             : 
      25             : #include "rtl/ref.hxx"
      26             : 
      27             : #include "test/bootstrapfixture.hxx"
      28             : 
      29             : #include "WPXSvStream.hxx"
      30             : 
      31             : namespace io = com::sun::star::io;
      32             : namespace ucb = com::sun::star::ucb;
      33             : namespace uno = com::sun::star::uno;
      34             : 
      35             : using boost::shared_ptr;
      36             : 
      37             : using std::equal;
      38             : 
      39             : namespace
      40             : {
      41             : 
      42          15 : class WPXSvStreamTest : public test::BootstrapFixture
      43             : {
      44             : public:
      45           2 :     CPPUNIT_TEST_SUITE(WPXSvStreamTest);
      46           1 :     CPPUNIT_TEST(testRead);
      47           1 :     CPPUNIT_TEST(testSeekSet);
      48           1 :     CPPUNIT_TEST(testSeekCur);
      49           1 :     CPPUNIT_TEST(testSeekEnd);
      50           1 :     CPPUNIT_TEST(testStructured);
      51           2 :     CPPUNIT_TEST_SUITE_END();
      52             : 
      53             : private:
      54             :     void testRead();
      55             :     void testSeekSet();
      56             :     void testSeekCur();
      57             :     void testSeekEnd();
      58             :     void testStructured();
      59             : };
      60             : 
      61             : static const char aText[] = "hello world";
      62             : static const char aOLEFile[] = "/writerperfect/qa/unit/data/fdo40686-1.doc";
      63             : static const char aZipFile[] = "/writerperfect/qa/unit/data/test.odt";
      64             : 
      65           5 : shared_ptr<WPXInputStream> lcl_createStream()
      66             : {
      67             :     using comphelper::SequenceInputStream;
      68             : 
      69           5 :     const css::uno::Sequence<sal_Int8> aData(reinterpret_cast<const sal_Int8*>(aText), sizeof aText);
      70          10 :     const uno::Reference<io::XInputStream> xInputStream(new SequenceInputStream(aData));
      71             : 
      72           5 :     shared_ptr<WPXInputStream> pInputStream;
      73           5 :     if (xInputStream.is())
      74           5 :         pInputStream.reset(new WPXSvInputStream(xInputStream));
      75             : 
      76          10 :     return pInputStream;
      77             : }
      78             : 
      79           2 : const shared_ptr<WPXInputStream> lcl_createStreamForURL(const rtl::OUString &rURL)
      80             : {
      81             :     using uno::Reference;
      82             :     using uno::UNO_QUERY_THROW;
      83             : 
      84           2 :     const Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext(), UNO_QUERY_THROW);
      85             :     const Reference<ucb::XSimpleFileAccess> xFileAccess(
      86           4 :             xContext->getServiceManager()->createInstanceWithContext("com.sun.star.ucb.SimpleFileAccess", xContext),
      87           4 :             UNO_QUERY_THROW);
      88           4 :     const Reference<io::XInputStream> xInputStream(xFileAccess->openFileRead(rURL), UNO_QUERY_THROW);
      89             : 
      90           2 :     const shared_ptr<WPXInputStream> pInput(new WPXSvInputStream(xInputStream));
      91           4 :     return pInput;
      92             : }
      93             : 
      94           1 : void WPXSvStreamTest::testRead()
      95             : {
      96           1 :     const shared_ptr<WPXInputStream> pInput(lcl_createStream());
      97           1 :     const unsigned long nLen = sizeof aText;
      98             : 
      99           1 :     unsigned long nReadBytes = 0;
     100           1 :     const unsigned char* pData = 0;
     101           1 :     const unsigned char* const pTextOrig = reinterpret_cast<const unsigned char*>(aText);
     102           1 :     const unsigned char* pText = pTextOrig;
     103             : 
     104             :     // reading by small pieces
     105           1 :     pData = pInput->read(1UL, nReadBytes);
     106           1 :     CPPUNIT_ASSERT_EQUAL(1UL, nReadBytes);
     107           1 :     CPPUNIT_ASSERT(equal(pText, pText + nReadBytes, pData));
     108           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     109           1 :     pText += nReadBytes;
     110             : 
     111           1 :     pData = pInput->read(2UL, nReadBytes);
     112           1 :     CPPUNIT_ASSERT_EQUAL(2UL, nReadBytes);
     113           1 :     CPPUNIT_ASSERT(equal(pText, pText + nReadBytes, pData));
     114           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     115           1 :     pText += nReadBytes;
     116             : 
     117           1 :     pData = pInput->read(3UL, nReadBytes);
     118           1 :     CPPUNIT_ASSERT_EQUAL(3UL, nReadBytes);
     119           1 :     CPPUNIT_ASSERT(equal(pText, pText + nReadBytes, pData));
     120           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     121           1 :     pText += nReadBytes;
     122             : 
     123             :     assert(nLen > 6);
     124           1 :     pData = pInput->read(nLen - 6, nReadBytes);
     125           1 :     CPPUNIT_ASSERT_EQUAL(nLen - 6, nReadBytes);
     126           1 :     CPPUNIT_ASSERT(equal(pText, pText + nReadBytes, pData));
     127           1 :     CPPUNIT_ASSERT(pInput->atEOS());
     128             : 
     129             :     // reading everything at once
     130           1 :     pInput->seek(0, WPX_SEEK_SET);
     131           1 :     pText = pTextOrig;
     132             : 
     133           1 :     pData = pInput->read(nLen, nReadBytes);
     134           1 :     CPPUNIT_ASSERT_EQUAL(nLen, nReadBytes);
     135           1 :     CPPUNIT_ASSERT(equal(pText, pText + nReadBytes, pData));
     136           1 :     CPPUNIT_ASSERT(pInput->atEOS());
     137             : 
     138             :     // trying to read too much
     139           1 :     pInput->seek(0, WPX_SEEK_SET);
     140           1 :     pText = pTextOrig;
     141             : 
     142           1 :     pData = pInput->read(nLen + 1, nReadBytes);
     143           1 :     CPPUNIT_ASSERT_EQUAL(nLen, nReadBytes);
     144           1 :     CPPUNIT_ASSERT(equal(pText, pText + nReadBytes, pData));
     145           1 :     CPPUNIT_ASSERT(pInput->atEOS());
     146             : 
     147             :     // trying to read nothing
     148           1 :     pInput->seek(0, WPX_SEEK_SET);
     149           1 :     pText = pTextOrig;
     150             : 
     151           1 :     pData = pInput->read(0UL, nReadBytes);
     152           1 :     CPPUNIT_ASSERT_EQUAL(0UL, nReadBytes);
     153           1 :     CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
     154           1 :     CPPUNIT_ASSERT_EQUAL(pData, static_cast<const unsigned char*>(0));
     155           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     156           1 : }
     157             : 
     158           1 : void WPXSvStreamTest::testSeekSet()
     159             : {
     160           1 :     const shared_ptr<WPXInputStream> pInput(lcl_createStream());
     161           1 :     const long nLen = sizeof aText;
     162             : 
     163             :     // check initial state
     164           1 :     CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
     165           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     166             : 
     167             :     // valid seeks
     168           1 :     CPPUNIT_ASSERT_EQUAL(0, pInput->seek(0, WPX_SEEK_SET));
     169           1 :     CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
     170           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     171             : 
     172           1 :     CPPUNIT_ASSERT_EQUAL(0, pInput->seek(1, WPX_SEEK_SET));
     173           1 :     CPPUNIT_ASSERT_EQUAL(1L, pInput->tell());
     174           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     175             : 
     176           1 :     CPPUNIT_ASSERT_EQUAL(0, pInput->seek(nLen, WPX_SEEK_SET));
     177           1 :     CPPUNIT_ASSERT(nLen == pInput->tell());
     178           1 :     CPPUNIT_ASSERT(pInput->atEOS());
     179             : 
     180             :     // go back to the beginning
     181           1 :     CPPUNIT_ASSERT_EQUAL(0, pInput->seek(0, WPX_SEEK_SET));
     182           1 :     CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
     183             : 
     184             :     // invalid seeks
     185           1 :     CPPUNIT_ASSERT(0 != pInput->seek(-1, WPX_SEEK_SET));
     186             :     // Okay, this is not defined. But it is what the WPXSvInputStream
     187             :     // does ATM and it is reasonable.
     188           1 :     CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
     189           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     190             : 
     191           1 :     CPPUNIT_ASSERT(0 != pInput->seek(nLen + 1, WPX_SEEK_SET));
     192           1 :     CPPUNIT_ASSERT(nLen == pInput->tell());
     193           1 :     CPPUNIT_ASSERT(pInput->atEOS());
     194           1 : }
     195             : 
     196           1 : void WPXSvStreamTest::testSeekCur()
     197             : {
     198           1 :     const shared_ptr<WPXInputStream> pInput(lcl_createStream());
     199           1 :     const long nLen = sizeof aText;
     200             : 
     201             :     // check initial state
     202           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     203           1 :     CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
     204             : 
     205             :     // valid seeks
     206             : 
     207           1 :     CPPUNIT_ASSERT_EQUAL(0, pInput->seek(0, WPX_SEEK_CUR));
     208           1 :     CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
     209           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     210             : 
     211           1 :     CPPUNIT_ASSERT_EQUAL(0, pInput->seek(1, WPX_SEEK_CUR));
     212           1 :     CPPUNIT_ASSERT_EQUAL(1L, pInput->tell());
     213           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     214             : 
     215           1 :     CPPUNIT_ASSERT_EQUAL(0, pInput->seek(-1, WPX_SEEK_CUR));
     216           1 :     CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
     217           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     218             : 
     219             :     // go back to the beginning
     220           1 :     CPPUNIT_ASSERT_EQUAL(0, pInput->seek(0, WPX_SEEK_SET));
     221           1 :     CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
     222             : 
     223             :     // invalid seeks
     224           1 :     CPPUNIT_ASSERT(0 != pInput->seek(-1, WPX_SEEK_CUR));
     225           1 :     CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
     226           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     227             : 
     228           1 :     CPPUNIT_ASSERT(0 != pInput->seek(nLen + 1, WPX_SEEK_CUR));
     229           1 :     CPPUNIT_ASSERT(nLen == pInput->tell());
     230           1 :     CPPUNIT_ASSERT(pInput->atEOS());
     231           1 : }
     232             : 
     233           1 : void WPXSvStreamTest::testSeekEnd()
     234             : {
     235           1 :     const shared_ptr<WPXInputStream> pInput(lcl_createStream());
     236           1 :     const long nLen = sizeof aText;
     237             : 
     238             :     // check initial state
     239           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     240           1 :     CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
     241             : 
     242             :     // valid seeks
     243           1 :     CPPUNIT_ASSERT_EQUAL(0, pInput->seek(0, WPX_SEEK_END));
     244           1 :     CPPUNIT_ASSERT(nLen == pInput->tell());
     245           1 :     CPPUNIT_ASSERT(pInput->atEOS());
     246             : 
     247           1 :     CPPUNIT_ASSERT_EQUAL(0, pInput->seek(-1, WPX_SEEK_END));
     248           1 :     CPPUNIT_ASSERT((nLen - 1) == pInput->tell());
     249           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     250             : 
     251           1 :     CPPUNIT_ASSERT_EQUAL(0, pInput->seek(-nLen, WPX_SEEK_END));
     252           1 :     CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
     253           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     254             : 
     255             :     // go back to the beginning
     256           1 :     CPPUNIT_ASSERT_EQUAL(0, pInput->seek(0, WPX_SEEK_SET));
     257           1 :     CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
     258             : 
     259             :     // invalid seeks
     260           1 :     CPPUNIT_ASSERT(0 != pInput->seek(1, WPX_SEEK_END));
     261           1 :     CPPUNIT_ASSERT(nLen == pInput->tell());
     262           1 :     CPPUNIT_ASSERT(pInput->atEOS());
     263             : 
     264           1 :     CPPUNIT_ASSERT(0 != pInput->seek(-nLen - 1, WPX_SEEK_END));
     265           1 :     CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
     266           1 :     CPPUNIT_ASSERT(!pInput->atEOS());
     267           1 : }
     268             : 
     269           1 : void WPXSvStreamTest::testStructured()
     270             : {
     271             :     // OLE2
     272             :     {
     273           1 :         const shared_ptr<WPXInputStream> pInput(lcl_createStreamForURL(getURLFromSrc(aOLEFile)));
     274             :         assert(bool(pInput));
     275             : 
     276           1 :         CPPUNIT_ASSERT(pInput->isOLEStream());
     277           2 :         shared_ptr<WPXInputStream> pSubStream(pInput->getDocumentOLEStream("WordDocument"));
     278           1 :         CPPUNIT_ASSERT(bool(pSubStream));
     279           1 :         pSubStream.reset(pInput->getDocumentOLEStream("foo"));
     280           2 :         CPPUNIT_ASSERT(!pSubStream);
     281             :     }
     282             : 
     283             :     // Zip
     284             :     {
     285           1 :         const shared_ptr<WPXInputStream> pInput(lcl_createStreamForURL(getURLFromSrc(aZipFile)));
     286             :         assert(bool(pInput));
     287             : 
     288           1 :         CPPUNIT_ASSERT(pInput->isOLEStream());
     289           2 :         shared_ptr<WPXInputStream> pSubStream(pInput->getDocumentOLEStream("content.xml"));
     290           1 :         CPPUNIT_ASSERT(bool(pSubStream));
     291           1 :         pSubStream.reset(pInput->getDocumentOLEStream("foo"));
     292           2 :         CPPUNIT_ASSERT(!pSubStream);
     293             :     }
     294             : 
     295             :     // not structured
     296             :     {
     297           1 :         const shared_ptr<WPXInputStream> pInput(lcl_createStream());
     298             : 
     299           1 :         CPPUNIT_ASSERT(!pInput->isOLEStream());
     300           1 :         CPPUNIT_ASSERT(0 == pInput->getDocumentOLEStream("foo"));
     301             :     }
     302           1 : }
     303             : 
     304           1 : CPPUNIT_TEST_SUITE_REGISTRATION(WPXSvStreamTest);
     305             : 
     306             : }
     307             : 
     308           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     309             : 
     310             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10