1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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
;
23 : public test::FiltersTest
24 , public test::BootstrapFixtureBase
29 bool checkStream( const tools::SvRef
<SotStorage
> &xObjStor
,
30 const OUString
&rStreamName
,
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
;
41 CPPUNIT_TEST_SUITE(SotTest
);
43 CPPUNIT_TEST(testSize
);
44 CPPUNIT_TEST_SUITE_END();
47 bool SotTest::checkStream( const tools::SvRef
<SotStorage
> &xObjStor
,
48 const OUString
&rStreamName
,
51 unsigned char *pData
= static_cast<unsigned char*>(malloc( nSize
));
52 sal_uLong nReadableSize
= 0;
56 { // Read the data in one block
57 tools::SvRef
<SotStorageStream
> xStream( xObjStor
->OpenSotStream( rStreamName
) );
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() );
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",
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() );
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())
114 CPPUNIT_ASSERT_MESSAGE("sot storage is not valid", xObjStor
->Validate());
115 return checkStorage (xObjStor
);
121 getURLFromSrc("/sot/qa/cppunit/data/"),
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: */