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/.
16 #include <cppunit/extensions/HelperMacros.h>
18 #include <com/sun/star/io/XInputStream.hpp>
19 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
20 #include <com/sun/star/uno/Reference.hxx>
22 #include <comphelper/processfactory.hxx>
23 #include <comphelper/seqstream.hxx>
25 #include <rtl/ref.hxx>
27 #include <test/bootstrapfixture.hxx>
29 #include <WPXSvInputStream.hxx>
31 namespace io
= com::sun::star::io
;
32 namespace ucb
= com::sun::star::ucb
;
33 namespace uno
= com::sun::star::uno
;
35 using std::shared_ptr
;
37 using librevenge::RVNGInputStream
;
38 using librevenge::RVNG_SEEK_CUR
;
39 using librevenge::RVNG_SEEK_END
;
40 using librevenge::RVNG_SEEK_SET
;
44 using writerperfect::WPXSvInputStream
;
48 class WPXSvStreamTest
: public test::BootstrapFixture
51 CPPUNIT_TEST_SUITE(WPXSvStreamTest
);
52 CPPUNIT_TEST(testRead
);
53 CPPUNIT_TEST(testSeekSet
);
54 CPPUNIT_TEST(testSeekCur
);
55 CPPUNIT_TEST(testSeekEnd
);
56 CPPUNIT_TEST(testStructured
);
57 CPPUNIT_TEST_SUITE_END();
64 void testStructured();
67 static const char aText
[] = "hello world";
68 static const char aOLEFile
[] = "/writerperfect/qa/unit/data/stream/fdo40686-1.doc";
69 static const char aZipFile
[] = "/writerperfect/qa/unit/data/stream/test.odt";
71 shared_ptr
<RVNGInputStream
> lcl_createStream()
73 using comphelper::SequenceInputStream
;
75 const css::uno::Sequence
<sal_Int8
> aData(reinterpret_cast<const sal_Int8
*>(aText
),
77 const uno::Reference
<io::XInputStream
> xInputStream(new SequenceInputStream(aData
));
79 shared_ptr
<RVNGInputStream
> pInputStream
;
80 if (xInputStream
.is())
81 pInputStream
.reset(new WPXSvInputStream(xInputStream
));
86 shared_ptr
<RVNGInputStream
> lcl_createStreamForURL(const OUString
& rURL
)
89 using uno::UNO_QUERY_THROW
;
91 const Reference
<uno::XComponentContext
> xContext(comphelper::getProcessComponentContext(),
92 css::uno::UNO_SET_THROW
);
93 const Reference
<ucb::XSimpleFileAccess
> xFileAccess(
94 xContext
->getServiceManager()->createInstanceWithContext(
95 "com.sun.star.ucb.SimpleFileAccess", xContext
),
97 const Reference
<io::XInputStream
> xInputStream(xFileAccess
->openFileRead(rURL
),
98 css::uno::UNO_SET_THROW
);
100 return shared_ptr
<RVNGInputStream
>(new WPXSvInputStream(xInputStream
));
103 void lcl_testSubStreams(const shared_ptr
<RVNGInputStream
>& pInput
)
105 shared_ptr
<RVNGInputStream
> pSubStream
;
107 // all valid substreams can be read
108 for (std::size_t i
= 0; i
!= pInput
->subStreamCount(); ++i
)
110 std::ostringstream
msg("opening substream ");
112 pSubStream
.reset(pInput
->getSubStreamById(i
));
113 CPPUNIT_ASSERT_MESSAGE(msg
.str(), bool(pSubStream
));
116 // invalid substreams cannot be read
117 pSubStream
.reset(pInput
->getSubStreamById(pInput
->subStreamCount()));
118 CPPUNIT_ASSERT(!pSubStream
);
121 void WPXSvStreamTest::testRead()
123 const shared_ptr
<RVNGInputStream
> pInput(lcl_createStream());
124 const unsigned long nLen
= sizeof aText
;
126 unsigned long nReadBytes
= 0;
127 const unsigned char* pData
= nullptr;
128 const unsigned char* const pTextOrig
= reinterpret_cast<const unsigned char*>(aText
);
129 const unsigned char* pText
= pTextOrig
;
131 // reading by small pieces
132 pData
= pInput
->read(1UL, nReadBytes
);
133 CPPUNIT_ASSERT_EQUAL(1UL, nReadBytes
);
134 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
135 CPPUNIT_ASSERT(!pInput
->isEnd());
138 pData
= pInput
->read(2UL, nReadBytes
);
139 CPPUNIT_ASSERT_EQUAL(2UL, nReadBytes
);
140 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
141 CPPUNIT_ASSERT(!pInput
->isEnd());
144 pData
= pInput
->read(3UL, nReadBytes
);
145 CPPUNIT_ASSERT_EQUAL(3UL, nReadBytes
);
146 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
147 CPPUNIT_ASSERT(!pInput
->isEnd());
151 pData
= pInput
->read(nLen
- 6, nReadBytes
);
152 CPPUNIT_ASSERT_EQUAL(nLen
- 6, nReadBytes
);
153 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
154 CPPUNIT_ASSERT(pInput
->isEnd());
156 // reading everything at once
157 pInput
->seek(0, RVNG_SEEK_SET
);
160 pData
= pInput
->read(nLen
, nReadBytes
);
161 CPPUNIT_ASSERT_EQUAL(nLen
, nReadBytes
);
162 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
163 CPPUNIT_ASSERT(pInput
->isEnd());
165 // trying to read too much
166 pInput
->seek(0, RVNG_SEEK_SET
);
169 pData
= pInput
->read(nLen
+ 1, nReadBytes
);
170 CPPUNIT_ASSERT_EQUAL(nLen
, nReadBytes
);
171 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
172 CPPUNIT_ASSERT(pInput
->isEnd());
174 // trying to read nothing
175 pInput
->seek(0, RVNG_SEEK_SET
);
176 pData
= pInput
->read(0UL, nReadBytes
);
177 CPPUNIT_ASSERT_EQUAL(0UL, nReadBytes
);
178 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
179 CPPUNIT_ASSERT_EQUAL(pData
, static_cast<const unsigned char*>(nullptr));
180 CPPUNIT_ASSERT(!pInput
->isEnd());
183 void WPXSvStreamTest::testSeekSet()
185 const shared_ptr
<RVNGInputStream
> pInput(lcl_createStream());
186 const long nLen
= sizeof aText
;
188 // check initial state
189 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
190 CPPUNIT_ASSERT(!pInput
->isEnd());
193 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(0, RVNG_SEEK_SET
));
194 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
195 CPPUNIT_ASSERT(!pInput
->isEnd());
197 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(1, RVNG_SEEK_SET
));
198 CPPUNIT_ASSERT_EQUAL(1L, pInput
->tell());
199 CPPUNIT_ASSERT(!pInput
->isEnd());
201 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(nLen
, RVNG_SEEK_SET
));
202 CPPUNIT_ASSERT_EQUAL(nLen
, pInput
->tell());
203 CPPUNIT_ASSERT(pInput
->isEnd());
205 // go back to the beginning
206 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(0, RVNG_SEEK_SET
));
207 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
210 CPPUNIT_ASSERT(0 != pInput
->seek(-1, RVNG_SEEK_SET
));
211 // Okay, this is not defined. But it is what the WPXSvInputStream
212 // does ATM and it is reasonable.
213 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
214 CPPUNIT_ASSERT(!pInput
->isEnd());
216 CPPUNIT_ASSERT(0 != pInput
->seek(nLen
+ 1, RVNG_SEEK_SET
));
217 CPPUNIT_ASSERT_EQUAL(nLen
, pInput
->tell());
218 CPPUNIT_ASSERT(pInput
->isEnd());
221 void WPXSvStreamTest::testSeekCur()
223 const shared_ptr
<RVNGInputStream
> pInput(lcl_createStream());
224 const long nLen
= sizeof aText
;
226 // check initial state
227 CPPUNIT_ASSERT(!pInput
->isEnd());
228 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
232 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(0, RVNG_SEEK_CUR
));
233 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
234 CPPUNIT_ASSERT(!pInput
->isEnd());
236 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(1, RVNG_SEEK_CUR
));
237 CPPUNIT_ASSERT_EQUAL(1L, pInput
->tell());
238 CPPUNIT_ASSERT(!pInput
->isEnd());
240 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(-1, RVNG_SEEK_CUR
));
241 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
242 CPPUNIT_ASSERT(!pInput
->isEnd());
244 // go back to the beginning
245 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(0, RVNG_SEEK_SET
));
246 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
249 CPPUNIT_ASSERT(0 != pInput
->seek(-1, RVNG_SEEK_CUR
));
250 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
251 CPPUNIT_ASSERT(!pInput
->isEnd());
253 CPPUNIT_ASSERT(0 != pInput
->seek(nLen
+ 1, RVNG_SEEK_CUR
));
254 CPPUNIT_ASSERT_EQUAL(nLen
, pInput
->tell());
255 CPPUNIT_ASSERT(pInput
->isEnd());
258 void WPXSvStreamTest::testSeekEnd()
260 const shared_ptr
<RVNGInputStream
> pInput(lcl_createStream());
261 const long nLen
= sizeof aText
;
263 // check initial state
264 CPPUNIT_ASSERT(!pInput
->isEnd());
265 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
268 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(0, RVNG_SEEK_END
));
269 CPPUNIT_ASSERT_EQUAL(nLen
, pInput
->tell());
270 CPPUNIT_ASSERT(pInput
->isEnd());
272 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(-1, RVNG_SEEK_END
));
273 CPPUNIT_ASSERT_EQUAL(nLen
- 1, pInput
->tell());
274 CPPUNIT_ASSERT(!pInput
->isEnd());
276 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(-nLen
, RVNG_SEEK_END
));
277 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
278 CPPUNIT_ASSERT(!pInput
->isEnd());
280 // go back to the beginning
281 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(0, RVNG_SEEK_SET
));
282 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
285 CPPUNIT_ASSERT(0 != pInput
->seek(1, RVNG_SEEK_END
));
286 CPPUNIT_ASSERT_EQUAL(nLen
, pInput
->tell());
287 CPPUNIT_ASSERT(pInput
->isEnd());
289 CPPUNIT_ASSERT(0 != pInput
->seek(-nLen
- 1, RVNG_SEEK_END
));
290 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
291 CPPUNIT_ASSERT(!pInput
->isEnd());
294 void WPXSvStreamTest::testStructured()
298 const shared_ptr
<RVNGInputStream
> pInput(
299 lcl_createStreamForURL(m_directories
.getURLFromSrc(aOLEFile
)));
300 assert(bool(pInput
));
302 CPPUNIT_ASSERT(pInput
->isStructured());
303 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned>(2), pInput
->subStreamCount());
304 lcl_testSubStreams(pInput
);
306 // check for existing substream
307 CPPUNIT_ASSERT(pInput
->existsSubStream("WordDocument"));
308 shared_ptr
<RVNGInputStream
> pSubStream(pInput
->getSubStreamByName("WordDocument"));
309 CPPUNIT_ASSERT(bool(pSubStream
));
310 CPPUNIT_ASSERT(!pSubStream
->isEnd());
312 // check for not existing substream
313 CPPUNIT_ASSERT(!pInput
->existsSubStream("foo"));
314 pSubStream
.reset(pInput
->getSubStreamByName("foo"));
315 CPPUNIT_ASSERT(!pSubStream
);
320 const shared_ptr
<RVNGInputStream
> pInput(
321 lcl_createStreamForURL(m_directories
.getURLFromSrc(aZipFile
)));
322 assert(bool(pInput
));
324 CPPUNIT_ASSERT(pInput
->isStructured());
325 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned>(9), pInput
->subStreamCount());
326 lcl_testSubStreams(pInput
);
328 // check for existing substream
329 CPPUNIT_ASSERT(pInput
->existsSubStream("content.xml"));
330 shared_ptr
<RVNGInputStream
> pSubStream(pInput
->getSubStreamByName("content.xml"));
331 CPPUNIT_ASSERT(bool(pSubStream
));
332 CPPUNIT_ASSERT(!pSubStream
->isEnd());
334 // check for not existing substream
335 CPPUNIT_ASSERT(!pInput
->existsSubStream("foo"));
336 pSubStream
.reset(pInput
->getSubStreamByName("foo"));
337 CPPUNIT_ASSERT(!pSubStream
);
342 const shared_ptr
<RVNGInputStream
> pInput(lcl_createStream());
344 CPPUNIT_ASSERT(!pInput
->isStructured());
345 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned>(0), pInput
->subStreamCount());
346 CPPUNIT_ASSERT(!pInput
->existsSubStream("foo"));
347 CPPUNIT_ASSERT(!pInput
->getSubStreamByName("foo"));
348 CPPUNIT_ASSERT(!pInput
->getSubStreamById(42));
349 CPPUNIT_ASSERT(!pInput
->subStreamName(42));
353 CPPUNIT_TEST_SUITE_REGISTRATION(WPXSvStreamTest
);
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */