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/.
14 #include <boost/shared_ptr.hpp>
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 boost::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
;
49 class WPXSvStreamTest
: public test::BootstrapFixture
52 CPPUNIT_TEST_SUITE(WPXSvStreamTest
);
53 CPPUNIT_TEST(testRead
);
54 CPPUNIT_TEST(testSeekSet
);
55 CPPUNIT_TEST(testSeekCur
);
56 CPPUNIT_TEST(testSeekEnd
);
57 CPPUNIT_TEST(testStructured
);
58 CPPUNIT_TEST_SUITE_END();
65 void testStructured();
68 static const char aText
[] = "hello world";
69 static const char aOLEFile
[] = "/writerperfect/qa/unit/data/stream/fdo40686-1.doc";
70 static const char aZipFile
[] = "/writerperfect/qa/unit/data/stream/test.odt";
72 shared_ptr
<RVNGInputStream
> lcl_createStream()
74 using comphelper::SequenceInputStream
;
76 const css::uno::Sequence
<sal_Int8
> aData(reinterpret_cast<const sal_Int8
*>(aText
), sizeof 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 const shared_ptr
<RVNGInputStream
> lcl_createStreamForURL(const rtl::OUString
&rURL
)
89 using uno::UNO_QUERY_THROW
;
91 const Reference
<uno::XComponentContext
> xContext(comphelper::getProcessComponentContext(), UNO_QUERY_THROW
);
92 const Reference
<ucb::XSimpleFileAccess
> xFileAccess(
93 xContext
->getServiceManager()->createInstanceWithContext("com.sun.star.ucb.SimpleFileAccess", xContext
),
95 const Reference
<io::XInputStream
> xInputStream(xFileAccess
->openFileRead(rURL
), UNO_QUERY_THROW
);
97 const shared_ptr
<RVNGInputStream
> pInput(new WPXSvInputStream(xInputStream
));
101 void lcl_testSubStreams(const shared_ptr
<RVNGInputStream
> &pInput
)
103 shared_ptr
<RVNGInputStream
> pSubStream
;
105 // all valid substreams can be read
106 for (std::size_t i
= 0; i
!= pInput
->subStreamCount(); ++i
)
108 std::ostringstream
msg("opening substream ");
110 pSubStream
.reset(pInput
->getSubStreamById(i
));
111 CPPUNIT_ASSERT_MESSAGE(msg
.str(), bool(pSubStream
));
114 // invalid substreams cannot be read
115 pSubStream
.reset(pInput
->getSubStreamById(pInput
->subStreamCount()));
116 CPPUNIT_ASSERT(!pSubStream
);
119 void WPXSvStreamTest::testRead()
121 const shared_ptr
<RVNGInputStream
> pInput(lcl_createStream());
122 const unsigned long nLen
= sizeof aText
;
124 unsigned long nReadBytes
= 0;
125 const unsigned char *pData
= 0;
126 const unsigned char *const pTextOrig
= reinterpret_cast<const unsigned char *>(aText
);
127 const unsigned char *pText
= pTextOrig
;
129 // reading by small pieces
130 pData
= pInput
->read(1UL, nReadBytes
);
131 CPPUNIT_ASSERT_EQUAL(1UL, nReadBytes
);
132 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
133 CPPUNIT_ASSERT(!pInput
->isEnd());
136 pData
= pInput
->read(2UL, nReadBytes
);
137 CPPUNIT_ASSERT_EQUAL(2UL, nReadBytes
);
138 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
139 CPPUNIT_ASSERT(!pInput
->isEnd());
142 pData
= pInput
->read(3UL, nReadBytes
);
143 CPPUNIT_ASSERT_EQUAL(3UL, nReadBytes
);
144 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
145 CPPUNIT_ASSERT(!pInput
->isEnd());
149 pData
= pInput
->read(nLen
- 6, nReadBytes
);
150 CPPUNIT_ASSERT_EQUAL(nLen
- 6, nReadBytes
);
151 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
152 CPPUNIT_ASSERT(pInput
->isEnd());
154 // reading everything at once
155 pInput
->seek(0, RVNG_SEEK_SET
);
158 pData
= pInput
->read(nLen
, nReadBytes
);
159 CPPUNIT_ASSERT_EQUAL(nLen
, nReadBytes
);
160 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
161 CPPUNIT_ASSERT(pInput
->isEnd());
163 // trying to read too much
164 pInput
->seek(0, RVNG_SEEK_SET
);
167 pData
= pInput
->read(nLen
+ 1, nReadBytes
);
168 CPPUNIT_ASSERT_EQUAL(nLen
, nReadBytes
);
169 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
170 CPPUNIT_ASSERT(pInput
->isEnd());
172 // trying to read nothing
173 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 *>(0));
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(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(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(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(nLen
== pInput
->tell());
270 CPPUNIT_ASSERT(pInput
->isEnd());
272 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(-1, RVNG_SEEK_END
));
273 CPPUNIT_ASSERT((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(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(lcl_createStreamForURL(getURLFromSrc(aOLEFile
)));
299 assert(bool(pInput
));
301 CPPUNIT_ASSERT(pInput
->isStructured());
302 CPPUNIT_ASSERT(2 == pInput
->subStreamCount());
303 lcl_testSubStreams(pInput
);
305 // check for existing substream
306 CPPUNIT_ASSERT(pInput
->existsSubStream("WordDocument"));
307 shared_ptr
<RVNGInputStream
> pSubStream(pInput
->getSubStreamByName("WordDocument"));
308 CPPUNIT_ASSERT(bool(pSubStream
));
309 CPPUNIT_ASSERT(!pSubStream
->isEnd());
311 // check for not existing substream
312 CPPUNIT_ASSERT(!pInput
->existsSubStream("foo"));
313 pSubStream
.reset(pInput
->getSubStreamByName("foo"));
314 CPPUNIT_ASSERT(!pSubStream
);
319 const shared_ptr
<RVNGInputStream
> pInput(lcl_createStreamForURL(getURLFromSrc(aZipFile
)));
320 assert(bool(pInput
));
322 CPPUNIT_ASSERT(pInput
->isStructured());
323 CPPUNIT_ASSERT(9 == pInput
->subStreamCount());
324 lcl_testSubStreams(pInput
);
326 // check for existing substream
327 CPPUNIT_ASSERT(pInput
->existsSubStream("content.xml"));
328 shared_ptr
<RVNGInputStream
> pSubStream(pInput
->getSubStreamByName("content.xml"));
329 CPPUNIT_ASSERT(bool(pSubStream
));
330 CPPUNIT_ASSERT(!pSubStream
->isEnd());
332 // check for not existing substream
333 CPPUNIT_ASSERT(pInput
->existsSubStream("content.xml"));
334 CPPUNIT_ASSERT(!pInput
->existsSubStream("foo"));
335 pSubStream
.reset(pInput
->getSubStreamByName("foo"));
336 CPPUNIT_ASSERT(!pSubStream
);
341 const shared_ptr
<RVNGInputStream
> pInput(lcl_createStream());
343 CPPUNIT_ASSERT(!pInput
->isStructured());
344 CPPUNIT_ASSERT(0 == pInput
->subStreamCount());
345 CPPUNIT_ASSERT(!pInput
->existsSubStream("foo"));
346 CPPUNIT_ASSERT(0 == pInput
->getSubStreamByName("foo"));
347 CPPUNIT_ASSERT(0 == pInput
->getSubStreamById(42));
348 CPPUNIT_ASSERT(0 == pInput
->subStreamName(42));
352 CPPUNIT_TEST_SUITE_REGISTRATION(WPXSvStreamTest
);
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */