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 SotStorageRef &xObjStor,
30 : const OUString &rStreamName,
31 : sal_uLong nSize );
32 : bool checkStorage( const SotStorageRef &xObjStor );
33 :
34 : virtual bool load(const OUString &,
35 : const OUString &rURL, const OUString &,
36 : unsigned int, unsigned int, unsigned int) SAL_OVERRIDE;
37 :
38 : void test();
39 :
40 4 : CPPUNIT_TEST_SUITE(SotTest);
41 2 : CPPUNIT_TEST(test);
42 4 : CPPUNIT_TEST_SUITE_END();
43 : };
44 :
45 122 : bool SotTest::checkStream( const SotStorageRef &xObjStor,
46 : const OUString &rStreamName,
47 : sal_uLong nSize )
48 : {
49 122 : unsigned char *pData = (unsigned char*)malloc( nSize );
50 122 : sal_uLong nReadableSize = 0;
51 122 : if( !pData )
52 0 : return true;
53 :
54 : { // Read the data in one block
55 122 : SotStorageStreamRef xStream( xObjStor->OpenSotStream( rStreamName ) );
56 122 : xStream->Seek(0);
57 122 : sal_uLong nRemaining = xStream->GetSize() - xStream->Tell();
58 :
59 122 : CPPUNIT_ASSERT_MESSAGE( "check size", nRemaining == nSize );
60 122 : CPPUNIT_ASSERT_MESSAGE( "check size #2", xStream->remainingSize() == nSize );
61 :
62 : // Read as much as we can, a corrupted FAT chain can cause real grief here
63 122 : nReadableSize = xStream->Read( (void *)pData, nSize );
64 : // fprintf(stderr, "readable size %d vs size %d remaining %d\n", nReadableSize, nSize, nReadableSize);
65 : }
66 : { // Read the data backwards as well
67 122 : SotStorageStreamRef xStream( xObjStor->OpenSotStream( rStreamName ) );
68 1367204 : for( sal_uLong i = nReadableSize; i > 0; i-- )
69 : {
70 1367082 : CPPUNIT_ASSERT_MESSAGE( "sot reading error", !xStream->GetError() );
71 : unsigned char c;
72 1367082 : xStream->Seek( i - 1 );
73 2734164 : CPPUNIT_ASSERT_MESSAGE( "sot storage reading byte",
74 1367082 : xStream->Read( &c, 1 ) == 1);
75 2734164 : CPPUNIT_ASSERT_MESSAGE( "mismatching data storage reading byte",
76 1367082 : pData[i - 1] == c );
77 122 : }
78 : }
79 122 : free(pData);
80 122 : return true;
81 : }
82 :
83 28 : bool SotTest::checkStorage( const SotStorageRef &xObjStor )
84 : {
85 28 : SvStorageInfoList aInfoList;
86 28 : xObjStor->FillInfoList( &aInfoList );
87 :
88 516 : for( SvStorageInfoList::iterator aIt = aInfoList.begin();
89 344 : aIt != aInfoList.end(); ++aIt )
90 : {
91 144 : if( aIt->IsStorage() )
92 : {
93 20 : SotStorageRef xChild( xObjStor->OpenSotStorage( aIt->GetName() ) );
94 20 : checkStorage( xChild );
95 : }
96 124 : else if( aIt->IsStream() )
97 122 : checkStream( xObjStor, aIt->GetName(), aIt->GetSize() );
98 : }
99 :
100 28 : return true;
101 : }
102 :
103 10 : bool SotTest::load(const OUString &,
104 : const OUString &rURL, const OUString &,
105 : unsigned int, unsigned int, unsigned int)
106 : {
107 10 : SvFileStream aStream(rURL, STREAM_READ);
108 20 : SotStorageRef xObjStor = new SotStorage(aStream);
109 10 : if (!xObjStor.Is() || xObjStor->GetError())
110 2 : return false;
111 :
112 8 : CPPUNIT_ASSERT_MESSAGE("sot storage is not valid", xObjStor->Validate());
113 18 : return checkStorage (xObjStor);
114 : }
115 :
116 2 : void SotTest::test()
117 : {
118 : testDir(OUString(),
119 : getURLFromSrc("/sot/qa/cppunit/data/"),
120 2 : OUString());
121 2 : }
122 :
123 2 : CPPUNIT_TEST_SUITE_REGISTRATION(SotTest);
124 : }
125 :
126 8 : CPPUNIT_PLUGIN_IMPLEMENT();
127 :
128 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|