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 4 : class SotTest
43 : : public test::FiltersTest
44 : , public test::BootstrapFixtureBase
45 : {
46 : public:
47 2 : 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 4 : CPPUNIT_TEST_SUITE(SotTest);
61 2 : CPPUNIT_TEST(test);
62 4 : CPPUNIT_TEST_SUITE_END();
63 : };
64 :
65 122 : bool SotTest::checkStream( const SotStorageRef &xObjStor,
66 : const String &rStreamName,
67 : sal_uLong nSize )
68 : {
69 122 : unsigned char *pData = (unsigned char*)malloc( nSize );
70 122 : sal_uLong nReadableSize = 0;
71 122 : if( !pData )
72 0 : return true;
73 :
74 : { // Read the data in one block
75 122 : SotStorageStreamRef xStream( xObjStor->OpenSotStream( rStreamName ) );
76 122 : xStream->Seek(0);
77 122 : sal_uLong nRemaining = xStream->GetSize() - xStream->Tell();
78 :
79 122 : CPPUNIT_ASSERT_MESSAGE( "check size", nRemaining == nSize );
80 122 : 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 122 : 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 122 : SotStorageStreamRef xStream( xObjStor->OpenSotStream( rStreamName ) );
88 1367204 : for( sal_uLong i = nReadableSize; i > 0; i-- )
89 : {
90 1367082 : CPPUNIT_ASSERT_MESSAGE( "sot reading error", !xStream->GetError() );
91 : unsigned char c;
92 1367082 : xStream->Seek( i - 1 );
93 2734164 : CPPUNIT_ASSERT_MESSAGE( "sot storage reading byte",
94 1367082 : xStream->Read( &c, 1 ) == 1);
95 2734164 : CPPUNIT_ASSERT_MESSAGE( "mismatching data storage reading byte",
96 1367082 : pData[i - 1] == c );
97 122 : }
98 : }
99 :
100 122 : return true;
101 : }
102 :
103 28 : bool SotTest::checkStorage( const SotStorageRef &xObjStor )
104 : {
105 28 : SvStorageInfoList aInfoList;
106 28 : xObjStor->FillInfoList( &aInfoList );
107 :
108 516 : for( SvStorageInfoList::iterator aIt = aInfoList.begin();
109 344 : aIt != aInfoList.end(); ++aIt )
110 : {
111 144 : if( aIt->IsStorage() )
112 : {
113 20 : SotStorageRef xChild( xObjStor->OpenSotStorage( aIt->GetName() ) );
114 20 : checkStorage( xChild );
115 : }
116 124 : else if( aIt->IsStream() )
117 122 : checkStream( xObjStor, aIt->GetName(), aIt->GetSize() );
118 : }
119 :
120 28 : return true;
121 : }
122 :
123 8 : bool SotTest::load(const rtl::OUString &,
124 : const rtl::OUString &rURL, const rtl::OUString &,
125 : unsigned int, unsigned int, unsigned int)
126 : {
127 8 : SvFileStream aStream(rURL, STREAM_READ);
128 8 : SotStorageRef xObjStor = new SotStorage(aStream);
129 8 : if (!xObjStor.Is() || xObjStor->GetError())
130 0 : return false;
131 :
132 8 : CPPUNIT_ASSERT_MESSAGE("sot storage is not valid", xObjStor->Validate());
133 8 : return checkStorage (xObjStor);
134 : }
135 :
136 2 : void SotTest::test()
137 : {
138 : testDir(rtl::OUString(),
139 : getURLFromSrc("/sot/qa/cppunit/data/"),
140 2 : rtl::OUString());
141 2 : }
142 :
143 2 : CPPUNIT_TEST_SUITE_REGISTRATION(SotTest);
144 : }
145 :
146 8 : CPPUNIT_PLUGIN_IMPLEMENT();
147 :
148 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|