Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * Copyright (C) 2011 Red Hat, Inc., Stephan Bergmann <sbergman@redhat.com>
17 : * (initial developer)
18 : *
19 : * All Rights Reserved.
20 : *
21 : * For minor contributions see the git repository.
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
25 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
26 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
27 : * instead of those above.
28 : */
29 :
30 : #include <unotest/filters-test.hxx>
31 : #include <unotest/bootstrapfixturebase.hxx>
32 :
33 : #include <osl/file.hxx>
34 : #include <osl/process.h>
35 : #include <sot/storage.hxx>
36 : #include <sot/storinfo.hxx>
37 :
38 : using namespace ::com::sun::star;
39 :
40 : namespace
41 : {
42 2 : class SotTest
43 : : public test::FiltersTest
44 : , public test::BootstrapFixtureBase
45 : {
46 : public:
47 1 : SotTest() {}
48 :
49 : bool checkStream( const SotStorageRef &xObjStor,
50 : const String &rStreamName,
51 : sal_uLong nSize );
52 : bool checkStorage( const SotStorageRef &xObjStor );
53 :
54 : virtual bool load(const rtl::OUString &,
55 : const rtl::OUString &rURL, const rtl::OUString &,
56 : unsigned int, unsigned int, unsigned int);
57 :
58 : void test();
59 :
60 2 : CPPUNIT_TEST_SUITE(SotTest);
61 1 : CPPUNIT_TEST(test);
62 2 : CPPUNIT_TEST_SUITE_END();
63 : };
64 :
65 61 : bool SotTest::checkStream( const SotStorageRef &xObjStor,
66 : const String &rStreamName,
67 : sal_uLong nSize )
68 : {
69 61 : unsigned char *pData = (unsigned char*)malloc( nSize );
70 61 : sal_uLong nReadableSize = 0;
71 61 : if( !pData )
72 0 : return true;
73 :
74 : { // Read the data in one block
75 61 : SotStorageStreamRef xStream( xObjStor->OpenSotStream( rStreamName ) );
76 61 : xStream->Seek(0);
77 61 : sal_uLong nRemaining = xStream->GetSize() - xStream->Tell();
78 :
79 61 : CPPUNIT_ASSERT_MESSAGE( "check size", nRemaining == nSize );
80 61 : CPPUNIT_ASSERT_MESSAGE( "check size #2", xStream->remainingSize() == nSize );
81 :
82 : // Read as much as we can, a corrupted FAT chain can cause real grief here
83 61 : nReadableSize = xStream->Read( (void *)pData, nSize );
84 : // fprintf(stderr, "readable size %d vs size %d remaining %d\n", nReadableSize, nSize, nReadableSize);
85 : }
86 : { // Read the data backwards as well
87 61 : SotStorageStreamRef xStream( xObjStor->OpenSotStream( rStreamName ) );
88 683602 : for( sal_uLong i = nReadableSize; i > 0; i-- )
89 : {
90 683541 : CPPUNIT_ASSERT_MESSAGE( "sot reading error", !xStream->GetError() );
91 : unsigned char c;
92 683541 : xStream->Seek( i - 1 );
93 1367082 : CPPUNIT_ASSERT_MESSAGE( "sot storage reading byte",
94 683541 : xStream->Read( &c, 1 ) == 1);
95 1367082 : CPPUNIT_ASSERT_MESSAGE( "mismatching data storage reading byte",
96 683541 : pData[i - 1] == c );
97 61 : }
98 : }
99 :
100 61 : return true;
101 : }
102 :
103 14 : bool SotTest::checkStorage( const SotStorageRef &xObjStor )
104 : {
105 14 : SvStorageInfoList aInfoList;
106 14 : xObjStor->FillInfoList( &aInfoList );
107 :
108 258 : for( SvStorageInfoList::iterator aIt = aInfoList.begin();
109 172 : aIt != aInfoList.end(); ++aIt )
110 : {
111 72 : if( aIt->IsStorage() )
112 : {
113 10 : SotStorageRef xChild( xObjStor->OpenSotStorage( aIt->GetName() ) );
114 10 : checkStorage( xChild );
115 : }
116 62 : else if( aIt->IsStream() )
117 61 : checkStream( xObjStor, aIt->GetName(), aIt->GetSize() );
118 : }
119 :
120 14 : return true;
121 : }
122 :
123 4 : bool SotTest::load(const rtl::OUString &,
124 : const rtl::OUString &rURL, const rtl::OUString &,
125 : unsigned int, unsigned int, unsigned int)
126 : {
127 4 : SvFileStream aStream(rURL, STREAM_READ);
128 4 : SotStorageRef xObjStor = new SotStorage(aStream);
129 4 : if (!xObjStor.Is() || xObjStor->GetError())
130 0 : return false;
131 :
132 4 : CPPUNIT_ASSERT_MESSAGE("sot storage is not valid", xObjStor->Validate());
133 4 : return checkStorage (xObjStor);
134 : }
135 :
136 1 : void SotTest::test()
137 : {
138 : testDir(rtl::OUString(),
139 : getURLFromSrc("/sot/qa/cppunit/data/"),
140 1 : rtl::OUString());
141 1 : }
142 :
143 1 : CPPUNIT_TEST_SUITE_REGISTRATION(SotTest);
144 : }
145 :
146 4 : CPPUNIT_PLUGIN_IMPLEMENT();
147 :
148 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|