summaryrefslogtreecommitdiffstats
path: root/writerperfect/qa/unit/WPXSvStreamTest.cxx
blob: 0bd7b7b159fa1d13bbdb25eef9a5462307fbd2b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <algorithm>
#include <cassert>
#include <sstream>

#include <memory>

#include <cppunit/extensions/HelperMacros.h>

#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/ucb/XSimpleFileAccess.hpp>
#include <com/sun/star/uno/Reference.hxx>

#include <comphelper/processfactory.hxx>
#include <comphelper/seqstream.hxx>

#include <test/bootstrapfixture.hxx>

#include <WPXSvInputStream.hxx>

namespace io = com::sun::star::io;
namespace ucb = com::sun::star::ucb;
namespace uno = com::sun::star::uno;

using std::shared_ptr;
using std::unique_ptr;

using librevenge::RVNGInputStream;
using librevenge::RVNG_SEEK_CUR;
using librevenge::RVNG_SEEK_END;
using librevenge::RVNG_SEEK_SET;

using std::equal;

using writerperfect::WPXSvInputStream;

namespace
{
class WPXSvStreamTest : public test::BootstrapFixture
{
public:
    CPPUNIT_TEST_SUITE(WPXSvStreamTest);
    CPPUNIT_TEST(testRead);
    CPPUNIT_TEST(testSeekSet);
    CPPUNIT_TEST(testSeekCur);
    CPPUNIT_TEST(testSeekEnd);
    CPPUNIT_TEST(testStructured);
    CPPUNIT_TEST_SUITE_END();

private:
    void testRead();
    void testSeekSet();
    void testSeekCur();
    void testSeekEnd();
    void testStructured();
};

const char aText[] = "hello world";
constexpr OUStringLiteral aOLEFile = u"/writerperfect/qa/unit/data/stream/fdo40686-1.doc";
constexpr OUStringLiteral aZipFile = u"/writerperfect/qa/unit/data/stream/test.odt";

shared_ptr<RVNGInputStream> lcl_createStream()
{
    using comphelper::SequenceInputStream;

    const css::uno::Sequence<sal_Int8> aData(reinterpret_cast<const sal_Int8*>(aText),
                                             sizeof aText);
    const uno::Reference<io::XInputStream> xInputStream(new SequenceInputStream(aData));

    shared_ptr<RVNGInputStream> pInputStream;
    if (xInputStream.is())
        pInputStream = std::make_shared<WPXSvInputStream>(xInputStream);

    return pInputStream;
}

shared_ptr<RVNGInputStream> lcl_createStreamForURL(const OUString& rURL)
{
    using uno::Reference;
    using uno::UNO_QUERY_THROW;

    const Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext(),
                                                     css::uno::UNO_SET_THROW);
    const Reference<ucb::XSimpleFileAccess> xFileAccess(
        xContext->getServiceManager()->createInstanceWithContext(
            "com.sun.star.ucb.SimpleFileAccess", xContext),
        UNO_QUERY_THROW);
    const Reference<io::XInputStream> xInputStream(xFileAccess->openFileRead(rURL),
                                                   css::uno::UNO_SET_THROW);

    return std::make_shared<WPXSvInputStream>(xInputStream);
}

void lcl_testSubStreams(const shared_ptr<RVNGInputStream>& pInput)
{
    shared_ptr<RVNGInputStream> pSubStream;

    // all valid substreams can be read
    for (std::size_t i = 0; i != pInput->subStreamCount(); ++i)
    {
        std::ostringstream msg("opening substream ");
        msg << i;
        pSubStream.reset(pInput->getSubStreamById(i));
        CPPUNIT_ASSERT_MESSAGE(msg.str(), bool(pSubStream));
    }

    // invalid substreams cannot be read
    pSubStream.reset(pInput->getSubStreamById(pInput->subStreamCount()));
    CPPUNIT_ASSERT(!pSubStream);
}

void WPXSvStreamTest::testRead()
{
    const shared_ptr<RVNGInputStream> pInput(lcl_createStream());
    const unsigned long nLen = sizeof aText;

    unsigned long nReadBytes = 0;
    const unsigned char* pData = nullptr;
    const unsigned char* const pTextOrig = reinterpret_cast<const unsigned char*>(aText);
    const unsigned char* pText = pTextOrig;

    // reading by small pieces
    pData = pInput->read(1UL, nReadBytes);
    CPPUNIT_ASSERT_EQUAL(1UL, nReadBytes);
    CPPUNIT_ASSERT(equal(pText, pText + nReadBytes, pData));
    CPPUNIT_ASSERT(!pInput->isEnd());
    pText += nReadBytes;

    pData = pInput->read(2UL, nReadBytes);
    CPPUNIT_ASSERT_EQUAL(2UL, nReadBytes);
    CPPUNIT_ASSERT(equal(pText, pText + nReadBytes, pData));
    CPPUNIT_ASSERT(!pInput->isEnd());
    pText += nReadBytes;

    pData = pInput->read(3UL, nReadBytes);
    CPPUNIT_ASSERT_EQUAL(3UL, nReadBytes);
    CPPUNIT_ASSERT(equal(pText, pText + nReadBytes, pData));
    CPPUNIT_ASSERT(!pInput->isEnd());
    pText += nReadBytes;

    assert(nLen > 6);
    pData = pInput->read(nLen - 6, nReadBytes);
    CPPUNIT_ASSERT_EQUAL(nLen - 6, nReadBytes);
    CPPUNIT_ASSERT(equal(pText, pText + nReadBytes, pData));
    CPPUNIT_ASSERT(pInput->isEnd());

    // reading everything at once
    pInput->seek(0, RVNG_SEEK_SET);
    pText = pTextOrig;

    pData = pInput->read(nLen, nReadBytes);
    CPPUNIT_ASSERT_EQUAL(nLen, nReadBytes);
    CPPUNIT_ASSERT(equal(pText, pText + nReadBytes, pData));
    CPPUNIT_ASSERT(pInput->isEnd());

    // trying to read too much
    pInput->seek(0, RVNG_SEEK_SET);
    pText = pTextOrig;

    pData = pInput->read(nLen + 1, nReadBytes);
    CPPUNIT_ASSERT_EQUAL(nLen, nReadBytes);
    CPPUNIT_ASSERT(equal(pText, pText + nReadBytes, pData));
    CPPUNIT_ASSERT(pInput->isEnd());

    // trying to read nothing
    pInput->seek(0, RVNG_SEEK_SET);
    pData = pInput->read(0UL, nReadBytes);
    CPPUNIT_ASSERT_EQUAL(0UL, nReadBytes);
    CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
    CPPUNIT_ASSERT_EQUAL(pData, static_cast<const unsigned char*>(nullptr));
    CPPUNIT_ASSERT(!pInput->isEnd());
}

void WPXSvStreamTest::testSeekSet()
{
    const shared_ptr<RVNGInputStream> pInput(lcl_createStream());
    const long nLen = sizeof aText;

    // check initial state
    CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
    CPPUNIT_ASSERT(!pInput->isEnd());

    // valid seeks
    CPPUNIT_ASSERT_EQUAL(0, pInput->seek(0, RVNG_SEEK_SET));
    CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
    CPPUNIT_ASSERT(!pInput->isEnd());

    CPPUNIT_ASSERT_EQUAL(0, pInput->seek(1, RVNG_SEEK_SET));
    CPPUNIT_ASSERT_EQUAL(1L, pInput->tell());
    CPPUNIT_ASSERT(!pInput->isEnd());

    CPPUNIT_ASSERT_EQUAL(0, pInput->seek(nLen, RVNG_SEEK_SET));
    CPPUNIT_ASSERT_EQUAL(nLen, pInput->tell());
    CPPUNIT_ASSERT(pInput->isEnd());

    // go back to the beginning
    CPPUNIT_ASSERT_EQUAL(0, pInput->seek(0, RVNG_SEEK_SET));
    CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());

    // invalid seeks
    CPPUNIT_ASSERT(0 != pInput->seek(-1, RVNG_SEEK_SET));
    // Okay, this is not defined. But it is what the WPXSvInputStream
    // does ATM and it is reasonable.
    CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
    CPPUNIT_ASSERT(!pInput->isEnd());

    CPPUNIT_ASSERT(0 != pInput->seek(nLen + 1, RVNG_SEEK_SET));
    CPPUNIT_ASSERT_EQUAL(nLen, pInput->tell());
    CPPUNIT_ASSERT(pInput->isEnd());
}

void WPXSvStreamTest::testSeekCur()
{
    const shared_ptr<RVNGInputStream> pInput(lcl_createStream());
    const long nLen = sizeof aText;

    // check initial state
    CPPUNIT_ASSERT(!pInput->isEnd());
    CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());

    // valid seeks

    CPPUNIT_ASSERT_EQUAL(0, pInput->seek(0, RVNG_SEEK_CUR));
    CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
    CPPUNIT_ASSERT(!pInput->isEnd());

    CPPUNIT_ASSERT_EQUAL(0, pInput->seek(1, RVNG_SEEK_CUR));
    CPPUNIT_ASSERT_EQUAL(1L, pInput->tell());
    CPPUNIT_ASSERT(!pInput->isEnd());

    CPPUNIT_ASSERT_EQUAL(0, pInput->seek(-1, RVNG_SEEK_CUR));
    CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
    CPPUNIT_ASSERT(!pInput->isEnd());

    // go back to the beginning
    CPPUNIT_ASSERT_EQUAL(0, pInput->seek(0, RVNG_SEEK_SET));
    CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());

    // invalid seeks
    CPPUNIT_ASSERT(0 != pInput->seek(-1, RVNG_SEEK_CUR));
    CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
    CPPUNIT_ASSERT(!pInput->isEnd());

    CPPUNIT_ASSERT(0 != pInput->seek(nLen + 1, RVNG_SEEK_CUR));
    CPPUNIT_ASSERT_EQUAL(nLen, pInput->tell());
    CPPUNIT_ASSERT(pInput->isEnd());
}

void WPXSvStreamTest::testSeekEnd()
{
    const shared_ptr<RVNGInputStream> pInput(lcl_createStream());
    const long nLen = sizeof aText;

    // check initial state
    CPPUNIT_ASSERT(!pInput->isEnd());
    CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());

    // valid seeks
    CPPUNIT_ASSERT_EQUAL(0, pInput->seek(0, RVNG_SEEK_END));
    CPPUNIT_ASSERT_EQUAL(nLen, pInput->tell());
    CPPUNIT_ASSERT(pInput->isEnd());

    CPPUNIT_ASSERT_EQUAL(0, pInput->seek(-1, RVNG_SEEK_END));
    CPPUNIT_ASSERT_EQUAL(nLen - 1, pInput->tell());
    CPPUNIT_ASSERT(!pInput->isEnd());

    CPPUNIT_ASSERT_EQUAL(0, pInput->seek(-nLen, RVNG_SEEK_END));
    CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
    CPPUNIT_ASSERT(!pInput->isEnd());

    // go back to the beginning
    CPPUNIT_ASSERT_EQUAL(0, pInput->seek(0, RVNG_SEEK_SET));
    CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());

    // invalid seeks
    CPPUNIT_ASSERT(0 != pInput->seek(1, RVNG_SEEK_END));
    CPPUNIT_ASSERT_EQUAL(nLen, pInput->tell());
    CPPUNIT_ASSERT(pInput->isEnd());

    CPPUNIT_ASSERT(0 != pInput->seek(-nLen - 1, RVNG_SEEK_END));
    CPPUNIT_ASSERT_EQUAL(0L, pInput->tell());
    CPPUNIT_ASSERT(!pInput->isEnd());
}

void WPXSvStreamTest::testStructured()
{
    // OLE2
    {
        const shared_ptr<RVNGInputStream> pInput(
            lcl_createStreamForURL(m_directories.getURLFromSrc(aOLEFile)));
        assert(bool(pInput));

        CPPUNIT_ASSERT(pInput->isStructured());
        CPPUNIT_ASSERT_EQUAL(static_cast<unsigned>(2), pInput->subStreamCount());
        lcl_testSubStreams(pInput);

        // check for existing substream
        CPPUNIT_ASSERT(pInput->existsSubStream("WordDocument"));
        unique_ptr<RVNGInputStream> pSubStream(pInput->getSubStreamByName("WordDocument"));
        CPPUNIT_ASSERT(bool(pSubStream));
        CPPUNIT_ASSERT(!pSubStream->isEnd());

        // check for not existing substream
        CPPUNIT_ASSERT(!pInput->existsSubStream("foo"));
        pSubStream.reset(pInput->getSubStreamByName("foo"));
        CPPUNIT_ASSERT(!pSubStream);
    }

    // Zip
    {
        const shared_ptr<RVNGInputStream> pInput(
            lcl_createStreamForURL(m_directories.getURLFromSrc(aZipFile)));
        assert(bool(pInput));

        CPPUNIT_ASSERT(pInput->isStructured());
        CPPUNIT_ASSERT_EQUAL(static_cast<unsigned>(9), pInput->subStreamCount());
        lcl_testSubStreams(pInput);

        // check for existing substream
        CPPUNIT_ASSERT(pInput->existsSubStream("content.xml"));
        unique_ptr<RVNGInputStream> pSubStream(pInput->getSubStreamByName("content.xml"));
        CPPUNIT_ASSERT(bool(pSubStream));
        CPPUNIT_ASSERT(!pSubStream->isEnd());

        // check for not existing substream
        CPPUNIT_ASSERT(!pInput->existsSubStream("foo"));
        pSubStream.reset(pInput->getSubStreamByName("foo"));
        CPPUNIT_ASSERT(!pSubStream);
    }

    // not structured
    {
        const shared_ptr<RVNGInputStream> pInput(lcl_createStream());

        CPPUNIT_ASSERT(!pInput->isStructured());
        CPPUNIT_ASSERT_EQUAL(static_cast<unsigned>(0), pInput->subStreamCount());
        CPPUNIT_ASSERT(!pInput->existsSubStream("foo"));
        CPPUNIT_ASSERT(!pInput->getSubStreamByName("foo"));
        CPPUNIT_ASSERT(!pInput->getSubStreamById(42));
        CPPUNIT_ASSERT(!pInput->subStreamName(42));
    }
}

CPPUNIT_TEST_SUITE_REGISTRATION(WPXSvStreamTest);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */