bump product version to 5.0.4.1
[LibreOffice.git] / sot / qa / cppunit / test_sot.cxx
blobf3c55157e147cea55330285be35cb69c30bdb391
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <unotest/filters-test.hxx>
11 #include <unotest/bootstrapfixturebase.hxx>
13 #include <osl/file.hxx>
14 #include <osl/process.h>
15 #include <sot/storage.hxx>
16 #include <sot/storinfo.hxx>
18 using namespace ::com::sun::star;
20 namespace
22 class SotTest
23 : public test::FiltersTest
24 , public test::BootstrapFixtureBase
26 public:
27 SotTest() {}
29 bool checkStream( const tools::SvRef<SotStorage> &xObjStor,
30 const OUString &rStreamName,
31 sal_uLong nSize );
32 bool checkStorage( const tools::SvRef<SotStorage> &xObjStor );
34 virtual bool load(const OUString &,
35 const OUString &rURL, const OUString &,
36 SfxFilterFlags, SotClipboardFormatId, unsigned int) SAL_OVERRIDE;
38 void test();
39 void testSize();
41 CPPUNIT_TEST_SUITE(SotTest);
42 CPPUNIT_TEST(test);
43 CPPUNIT_TEST(testSize);
44 CPPUNIT_TEST_SUITE_END();
47 bool SotTest::checkStream( const tools::SvRef<SotStorage> &xObjStor,
48 const OUString &rStreamName,
49 sal_uLong nSize )
51 unsigned char *pData = static_cast<unsigned char*>(malloc( nSize ));
52 sal_uLong nReadableSize = 0;
53 if( !pData )
54 return true;
56 { // Read the data in one block
57 tools::SvRef<SotStorageStream> xStream( xObjStor->OpenSotStream( rStreamName ) );
58 xStream->Seek(0);
59 sal_uLong nRemaining = xStream->GetSize() - xStream->Tell();
61 CPPUNIT_ASSERT_MESSAGE( "check size", nRemaining == nSize );
62 CPPUNIT_ASSERT_MESSAGE( "check size #2", xStream->remainingSize() == nSize );
64 // Read as much as we can, a corrupted FAT chain can cause real grief here
65 nReadableSize = xStream->Read( (void *)pData, nSize );
66 // fprintf(stderr, "readable size %d vs size %d remaining %d\n", nReadableSize, nSize, nReadableSize);
68 { // Read the data backwards as well
69 tools::SvRef<SotStorageStream> xStream( xObjStor->OpenSotStream( rStreamName ) );
70 for( sal_uLong i = nReadableSize; i > 0; i-- )
72 CPPUNIT_ASSERT_MESSAGE( "sot reading error", !xStream->GetError() );
73 unsigned char c;
74 xStream->Seek( i - 1 );
75 CPPUNIT_ASSERT_MESSAGE( "sot storage reading byte",
76 xStream->Read( &c, 1 ) == 1);
77 CPPUNIT_ASSERT_MESSAGE( "mismatching data storage reading byte",
78 pData[i - 1] == c );
81 free(pData);
82 return true;
85 bool SotTest::checkStorage( const tools::SvRef<SotStorage> &xObjStor )
87 SvStorageInfoList aInfoList;
88 xObjStor->FillInfoList( &aInfoList );
90 for( SvStorageInfoList::iterator aIt = aInfoList.begin();
91 aIt != aInfoList.end(); ++aIt )
93 if( aIt->IsStorage() )
95 tools::SvRef<SotStorage> xChild( xObjStor->OpenSotStorage( aIt->GetName() ) );
96 checkStorage( xChild );
98 else if( aIt->IsStream() )
99 checkStream( xObjStor, aIt->GetName(), aIt->GetSize() );
102 return true;
105 bool SotTest::load(const OUString &,
106 const OUString &rURL, const OUString &,
107 SfxFilterFlags, SotClipboardFormatId, unsigned int)
109 SvFileStream aStream(rURL, StreamMode::READ);
110 tools::SvRef<SotStorage> xObjStor = new SotStorage(aStream);
111 if (!xObjStor.Is() || xObjStor->GetError())
112 return false;
114 CPPUNIT_ASSERT_MESSAGE("sot storage is not valid", xObjStor->Validate());
115 return checkStorage (xObjStor);
118 void SotTest::test()
120 testDir(OUString(),
121 getURLFromSrc("/sot/qa/cppunit/data/"),
122 OUString());
125 void SotTest::testSize()
127 OUString aURL(getURLFromSrc("/sot/qa/cppunit/data/pass/fdo84229-1.compound"));
128 SvFileStream aStream(aURL, StreamMode::READ);
129 tools::SvRef<SotStorage> xObjStor = new SotStorage(aStream);
130 CPPUNIT_ASSERT_MESSAGE("sot storage failed to open",
131 xObjStor.Is() && !xObjStor->GetError());
132 tools::SvRef<SotStorageStream> xStream = xObjStor->OpenSotStream("Book");
133 CPPUNIT_ASSERT_MESSAGE("stream failed to open",
134 xStream.Is() && !xObjStor->GetError());
135 CPPUNIT_ASSERT_MESSAGE("error in opened stream", !xStream->GetError());
136 sal_uLong nPos = xStream->GetSize();
137 CPPUNIT_ASSERT_MESSAGE("odd stream length", nPos == 13312);
139 xStream->Seek(STREAM_SEEK_TO_END);
140 CPPUNIT_ASSERT_MESSAGE("error seeking to end", !xStream->GetError());
141 // cf. comment in Pos2Page, not extremely intuitive ...
142 CPPUNIT_ASSERT_MESSAGE("stream not at beginning", xStream->Tell() == xStream->GetSize());
143 xStream->Seek(STREAM_SEEK_TO_BEGIN);
145 CPPUNIT_ASSERT_MESSAGE("error seeking to beginning", !xStream->GetError());
146 CPPUNIT_ASSERT_MESSAGE("stream not at beginning", xStream->Tell() == 0);
149 CPPUNIT_TEST_SUITE_REGISTRATION(SotTest);
152 CPPUNIT_PLUGIN_IMPLEMENT();
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */