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 <unotest/filters-test.hxx>
11 : #include <unotest/bootstrapfixturebase.hxx>
12 :
13 : #include <osl/file.hxx>
14 : #include <osl/process.h>
15 : #include <sot/storage.hxx>
16 : #include <sot/storinfo.hxx>
17 :
18 : using namespace ::com::sun::star;
19 :
20 : namespace
21 : {
22 4 : class SotTest
23 : : public test::FiltersTest
24 : , public test::BootstrapFixtureBase
25 : {
26 : public:
27 2 : SotTest() {}
28 :
29 : bool checkStream( const tools::SvRef<SotStorage> &xObjStor,
30 : const OUString &rStreamName,
31 : sal_uLong nSize );
32 : bool checkStorage( const tools::SvRef<SotStorage> &xObjStor );
33 :
34 : virtual bool load(const OUString &,
35 : const OUString &rURL, const OUString &,
36 : SfxFilterFlags, SotClipboardFormatId, unsigned int) SAL_OVERRIDE;
37 :
38 : void test();
39 : void testSize();
40 :
41 2 : CPPUNIT_TEST_SUITE(SotTest);
42 1 : CPPUNIT_TEST(test);
43 1 : CPPUNIT_TEST(testSize);
44 5 : CPPUNIT_TEST_SUITE_END();
45 : };
46 :
47 64 : bool SotTest::checkStream( const tools::SvRef<SotStorage> &xObjStor,
48 : const OUString &rStreamName,
49 : sal_uLong nSize )
50 : {
51 64 : unsigned char *pData = static_cast<unsigned char*>(malloc( nSize ));
52 64 : sal_uLong nReadableSize = 0;
53 64 : if( !pData )
54 0 : return true;
55 :
56 : { // Read the data in one block
57 64 : tools::SvRef<SotStorageStream> xStream( xObjStor->OpenSotStream( rStreamName ) );
58 64 : xStream->Seek(0);
59 64 : sal_uLong nRemaining = xStream->GetSize() - xStream->Tell();
60 :
61 64 : CPPUNIT_ASSERT_MESSAGE( "check size", nRemaining == nSize );
62 64 : CPPUNIT_ASSERT_MESSAGE( "check size #2", xStream->remainingSize() == nSize );
63 :
64 : // Read as much as we can, a corrupted FAT chain can cause real grief here
65 64 : nReadableSize = xStream->Read( static_cast<void *>(pData), nSize );
66 : // fprintf(stderr, "readable size %d vs size %d remaining %d\n", nReadableSize, nSize, nReadableSize);
67 : }
68 : { // Read the data backwards as well
69 64 : tools::SvRef<SotStorageStream> xStream( xObjStor->OpenSotStream( rStreamName ) );
70 1142357 : for( sal_uLong i = nReadableSize; i > 0; i-- )
71 : {
72 1142293 : CPPUNIT_ASSERT_MESSAGE( "sot reading error", !xStream->GetError() );
73 : unsigned char c;
74 1142293 : xStream->Seek( i - 1 );
75 2284586 : CPPUNIT_ASSERT_MESSAGE( "sot storage reading byte",
76 1142293 : xStream->Read( &c, 1 ) == 1);
77 2284586 : CPPUNIT_ASSERT_MESSAGE( "mismatching data storage reading byte",
78 1142293 : pData[i - 1] == c );
79 64 : }
80 : }
81 64 : free(pData);
82 64 : return true;
83 : }
84 :
85 15 : bool SotTest::checkStorage( const tools::SvRef<SotStorage> &xObjStor )
86 : {
87 15 : SvStorageInfoList aInfoList;
88 15 : xObjStor->FillInfoList( &aInfoList );
89 :
90 270 : for( SvStorageInfoList::iterator aIt = aInfoList.begin();
91 180 : aIt != aInfoList.end(); ++aIt )
92 : {
93 75 : if( aIt->IsStorage() )
94 : {
95 10 : tools::SvRef<SotStorage> xChild( xObjStor->OpenSotStorage( aIt->GetName() ) );
96 10 : checkStorage( xChild );
97 : }
98 65 : else if( aIt->IsStream() )
99 64 : checkStream( xObjStor, aIt->GetName(), aIt->GetSize() );
100 : }
101 :
102 15 : return true;
103 : }
104 :
105 6 : bool SotTest::load(const OUString &,
106 : const OUString &rURL, const OUString &,
107 : SfxFilterFlags, SotClipboardFormatId, unsigned int)
108 : {
109 6 : SvFileStream aStream(rURL, StreamMode::READ);
110 12 : tools::SvRef<SotStorage> xObjStor = new SotStorage(aStream);
111 6 : if (!xObjStor.Is() || xObjStor->GetError())
112 1 : return false;
113 :
114 5 : CPPUNIT_ASSERT_MESSAGE("sot storage is not valid", xObjStor->Validate());
115 11 : return checkStorage (xObjStor);
116 : }
117 :
118 1 : void SotTest::test()
119 : {
120 : testDir(OUString(),
121 : getURLFromSrc("/sot/qa/cppunit/data/"),
122 1 : OUString());
123 1 : }
124 :
125 1 : void SotTest::testSize()
126 : {
127 1 : OUString aURL(getURLFromSrc("/sot/qa/cppunit/data/pass/fdo84229-1.compound"));
128 2 : SvFileStream aStream(aURL, StreamMode::READ);
129 2 : tools::SvRef<SotStorage> xObjStor = new SotStorage(aStream);
130 2 : CPPUNIT_ASSERT_MESSAGE("sot storage failed to open",
131 1 : xObjStor.Is() && !xObjStor->GetError());
132 2 : tools::SvRef<SotStorageStream> xStream = xObjStor->OpenSotStream("Book");
133 2 : CPPUNIT_ASSERT_MESSAGE("stream failed to open",
134 1 : xStream.Is() && !xObjStor->GetError());
135 1 : CPPUNIT_ASSERT_MESSAGE("error in opened stream", !xStream->GetError());
136 1 : sal_uLong nPos = xStream->GetSize();
137 1 : CPPUNIT_ASSERT_MESSAGE("odd stream length", nPos == 13312);
138 :
139 1 : xStream->Seek(STREAM_SEEK_TO_END);
140 1 : CPPUNIT_ASSERT_MESSAGE("error seeking to end", !xStream->GetError());
141 : // cf. comment in Pos2Page, not extremely intuitive ...
142 1 : CPPUNIT_ASSERT_MESSAGE("stream not at beginning", xStream->Tell() == xStream->GetSize());
143 1 : xStream->Seek(STREAM_SEEK_TO_BEGIN);
144 :
145 1 : CPPUNIT_ASSERT_MESSAGE("error seeking to beginning", !xStream->GetError());
146 2 : CPPUNIT_ASSERT_MESSAGE("stream not at beginning", xStream->Tell() == 0);
147 1 : }
148 :
149 1 : CPPUNIT_TEST_SUITE_REGISTRATION(SotTest);
150 : }
151 :
152 4 : CPPUNIT_PLUGIN_IMPLEMENT();
153 :
154 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|