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 <test/bootstrapfixture.hxx>
27 #include <WPXSvInputStream.hxx>
29 namespace io
= css::io
;
30 namespace ucb
= css::ucb
;
31 namespace uno
= css::uno
;
33 using std::shared_ptr
;
34 using std::unique_ptr
;
36 using librevenge::RVNGInputStream
;
37 using librevenge::RVNG_SEEK_CUR
;
38 using librevenge::RVNG_SEEK_END
;
39 using librevenge::RVNG_SEEK_SET
;
43 using writerperfect::WPXSvInputStream
;
47 class WPXSvStreamTest
: public test::BootstrapFixture
50 CPPUNIT_TEST_SUITE(WPXSvStreamTest
);
51 CPPUNIT_TEST(testRead
);
52 CPPUNIT_TEST(testSeekSet
);
53 CPPUNIT_TEST(testSeekCur
);
54 CPPUNIT_TEST(testSeekEnd
);
55 CPPUNIT_TEST(testStructured
);
56 CPPUNIT_TEST_SUITE_END();
63 void testStructured();
66 const char aText
[] = "hello world";
67 constexpr OUStringLiteral aOLEFile
= u
"/writerperfect/qa/unit/data/stream/fdo40686-1.doc";
68 constexpr OUStringLiteral aZipFile
= u
"/writerperfect/qa/unit/data/stream/test.odt";
70 shared_ptr
<RVNGInputStream
> lcl_createStream()
72 using comphelper::SequenceInputStream
;
74 const css::uno::Sequence
<sal_Int8
> aData(reinterpret_cast<const sal_Int8
*>(aText
),
76 const uno::Reference
<io::XInputStream
> xInputStream(new SequenceInputStream(aData
));
78 shared_ptr
<RVNGInputStream
> pInputStream
;
79 if (xInputStream
.is())
80 pInputStream
= std::make_shared
<WPXSvInputStream
>(xInputStream
);
85 shared_ptr
<RVNGInputStream
> lcl_createStreamForURL(const OUString
& rURL
)
88 using uno::UNO_QUERY_THROW
;
90 const Reference
<uno::XComponentContext
> xContext(comphelper::getProcessComponentContext(),
91 css::uno::UNO_SET_THROW
);
92 const Reference
<ucb::XSimpleFileAccess
> xFileAccess(
93 xContext
->getServiceManager()->createInstanceWithContext(
94 u
"com.sun.star.ucb.SimpleFileAccess"_ustr
, xContext
),
96 const Reference
<io::XInputStream
> xInputStream(xFileAccess
->openFileRead(rURL
),
97 css::uno::UNO_SET_THROW
);
99 return std::make_shared
<WPXSvInputStream
>(xInputStream
);
102 void lcl_testSubStreams(const shared_ptr
<RVNGInputStream
>& pInput
)
104 shared_ptr
<RVNGInputStream
> pSubStream
;
106 // all valid substreams can be read
107 for (std::size_t i
= 0; i
!= pInput
->subStreamCount(); ++i
)
109 std::ostringstream
msg("opening substream ");
111 pSubStream
.reset(pInput
->getSubStreamById(i
));
112 CPPUNIT_ASSERT_MESSAGE(msg
.str(), bool(pSubStream
));
115 // invalid substreams cannot be read
116 pSubStream
.reset(pInput
->getSubStreamById(pInput
->subStreamCount()));
117 CPPUNIT_ASSERT(!pSubStream
);
120 void WPXSvStreamTest::testRead()
122 const shared_ptr
<RVNGInputStream
> pInput(lcl_createStream());
123 const unsigned long nLen
= sizeof aText
;
125 unsigned long nReadBytes
= 0;
126 const unsigned char* pData
= nullptr;
127 const unsigned char* const pTextOrig
= reinterpret_cast<const unsigned char*>(aText
);
128 const unsigned char* pText
= pTextOrig
;
130 // reading by small pieces
131 pData
= pInput
->read(1UL, nReadBytes
);
132 CPPUNIT_ASSERT_EQUAL(1UL, nReadBytes
);
133 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
134 CPPUNIT_ASSERT(!pInput
->isEnd());
137 pData
= pInput
->read(2UL, nReadBytes
);
138 CPPUNIT_ASSERT_EQUAL(2UL, nReadBytes
);
139 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
140 CPPUNIT_ASSERT(!pInput
->isEnd());
143 pData
= pInput
->read(3UL, nReadBytes
);
144 CPPUNIT_ASSERT_EQUAL(3UL, nReadBytes
);
145 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
146 CPPUNIT_ASSERT(!pInput
->isEnd());
150 pData
= pInput
->read(nLen
- 6, nReadBytes
);
151 CPPUNIT_ASSERT_EQUAL(nLen
- 6, nReadBytes
);
152 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
153 CPPUNIT_ASSERT(pInput
->isEnd());
155 // reading everything at once
156 pInput
->seek(0, RVNG_SEEK_SET
);
159 pData
= pInput
->read(nLen
, nReadBytes
);
160 CPPUNIT_ASSERT_EQUAL(nLen
, nReadBytes
);
161 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
162 CPPUNIT_ASSERT(pInput
->isEnd());
164 // trying to read too much
165 pInput
->seek(0, RVNG_SEEK_SET
);
168 pData
= pInput
->read(nLen
+ 1, nReadBytes
);
169 CPPUNIT_ASSERT_EQUAL(nLen
, nReadBytes
);
170 CPPUNIT_ASSERT(equal(pText
, pText
+ nReadBytes
, pData
));
171 CPPUNIT_ASSERT(pInput
->isEnd());
173 // trying to read nothing
174 pInput
->seek(0, RVNG_SEEK_SET
);
175 pData
= pInput
->read(0UL, nReadBytes
);
176 CPPUNIT_ASSERT_EQUAL(0UL, nReadBytes
);
177 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
178 CPPUNIT_ASSERT_EQUAL(static_cast<const unsigned char*>(nullptr), pData
);
179 CPPUNIT_ASSERT(!pInput
->isEnd());
182 void WPXSvStreamTest::testSeekSet()
184 const shared_ptr
<RVNGInputStream
> pInput(lcl_createStream());
185 const long nLen
= sizeof aText
;
187 // check initial state
188 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
189 CPPUNIT_ASSERT(!pInput
->isEnd());
192 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(0, RVNG_SEEK_SET
));
193 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
194 CPPUNIT_ASSERT(!pInput
->isEnd());
196 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(1, RVNG_SEEK_SET
));
197 CPPUNIT_ASSERT_EQUAL(1L, pInput
->tell());
198 CPPUNIT_ASSERT(!pInput
->isEnd());
200 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(nLen
, RVNG_SEEK_SET
));
201 CPPUNIT_ASSERT_EQUAL(nLen
, pInput
->tell());
202 CPPUNIT_ASSERT(pInput
->isEnd());
204 // go back to the beginning
205 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(0, RVNG_SEEK_SET
));
206 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
209 CPPUNIT_ASSERT(0 != pInput
->seek(-1, RVNG_SEEK_SET
));
210 // Okay, this is not defined. But it is what the WPXSvInputStream
211 // does ATM and it is reasonable.
212 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
213 CPPUNIT_ASSERT(!pInput
->isEnd());
215 CPPUNIT_ASSERT(0 != pInput
->seek(nLen
+ 1, RVNG_SEEK_SET
));
216 CPPUNIT_ASSERT_EQUAL(nLen
, pInput
->tell());
217 CPPUNIT_ASSERT(pInput
->isEnd());
220 void WPXSvStreamTest::testSeekCur()
222 const shared_ptr
<RVNGInputStream
> pInput(lcl_createStream());
223 const long nLen
= sizeof aText
;
225 // check initial state
226 CPPUNIT_ASSERT(!pInput
->isEnd());
227 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
231 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(0, RVNG_SEEK_CUR
));
232 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
233 CPPUNIT_ASSERT(!pInput
->isEnd());
235 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(1, RVNG_SEEK_CUR
));
236 CPPUNIT_ASSERT_EQUAL(1L, pInput
->tell());
237 CPPUNIT_ASSERT(!pInput
->isEnd());
239 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(-1, RVNG_SEEK_CUR
));
240 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
241 CPPUNIT_ASSERT(!pInput
->isEnd());
243 // go back to the beginning
244 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(0, RVNG_SEEK_SET
));
245 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
248 CPPUNIT_ASSERT(0 != pInput
->seek(-1, RVNG_SEEK_CUR
));
249 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
250 CPPUNIT_ASSERT(!pInput
->isEnd());
252 CPPUNIT_ASSERT(0 != pInput
->seek(nLen
+ 1, RVNG_SEEK_CUR
));
253 CPPUNIT_ASSERT_EQUAL(nLen
, pInput
->tell());
254 CPPUNIT_ASSERT(pInput
->isEnd());
257 void WPXSvStreamTest::testSeekEnd()
259 const shared_ptr
<RVNGInputStream
> pInput(lcl_createStream());
260 const long nLen
= sizeof aText
;
262 // check initial state
263 CPPUNIT_ASSERT(!pInput
->isEnd());
264 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
267 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(0, RVNG_SEEK_END
));
268 CPPUNIT_ASSERT_EQUAL(nLen
, pInput
->tell());
269 CPPUNIT_ASSERT(pInput
->isEnd());
271 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(-1, RVNG_SEEK_END
));
272 CPPUNIT_ASSERT_EQUAL(nLen
- 1, pInput
->tell());
273 CPPUNIT_ASSERT(!pInput
->isEnd());
275 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(-nLen
, RVNG_SEEK_END
));
276 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
277 CPPUNIT_ASSERT(!pInput
->isEnd());
279 // go back to the beginning
280 CPPUNIT_ASSERT_EQUAL(0, pInput
->seek(0, RVNG_SEEK_SET
));
281 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
284 CPPUNIT_ASSERT(0 != pInput
->seek(1, RVNG_SEEK_END
));
285 CPPUNIT_ASSERT_EQUAL(nLen
, pInput
->tell());
286 CPPUNIT_ASSERT(pInput
->isEnd());
288 CPPUNIT_ASSERT(0 != pInput
->seek(-nLen
- 1, RVNG_SEEK_END
));
289 CPPUNIT_ASSERT_EQUAL(0L, pInput
->tell());
290 CPPUNIT_ASSERT(!pInput
->isEnd());
293 void WPXSvStreamTest::testStructured()
297 const shared_ptr
<RVNGInputStream
> pInput(
298 lcl_createStreamForURL(m_directories
.getURLFromSrc(aOLEFile
)));
299 assert(bool(pInput
));
301 CPPUNIT_ASSERT(pInput
->isStructured());
302 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned>(2), pInput
->subStreamCount());
303 lcl_testSubStreams(pInput
);
305 // check for existing substream
306 CPPUNIT_ASSERT(pInput
->existsSubStream("WordDocument"));
307 unique_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(
320 lcl_createStreamForURL(m_directories
.getURLFromSrc(aZipFile
)));
321 assert(bool(pInput
));
323 CPPUNIT_ASSERT(pInput
->isStructured());
324 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned>(9), pInput
->subStreamCount());
325 lcl_testSubStreams(pInput
);
327 // check for existing substream
328 CPPUNIT_ASSERT(pInput
->existsSubStream("content.xml"));
329 unique_ptr
<RVNGInputStream
> pSubStream(pInput
->getSubStreamByName("content.xml"));
330 CPPUNIT_ASSERT(bool(pSubStream
));
331 CPPUNIT_ASSERT(!pSubStream
->isEnd());
333 // check for not existing substream
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_EQUAL(static_cast<unsigned>(0), pInput
->subStreamCount());
345 CPPUNIT_ASSERT(!pInput
->existsSubStream("foo"));
346 CPPUNIT_ASSERT(!pInput
->getSubStreamByName("foo"));
347 CPPUNIT_ASSERT(!pInput
->getSubStreamById(42));
348 CPPUNIT_ASSERT(!pInput
->subStreamName(42));
352 CPPUNIT_TEST_SUITE_REGISTRATION(WPXSvStreamTest
);
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */