| 1 |
/* |
| 2 |
Copyright (C) 2005-2014 Sergey A. Tachenov |
| 3 |
|
| 4 |
This file is part of QuaZIP test suite. |
| 5 |
|
| 6 |
QuaZIP is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU Lesser General Public License as published by |
| 8 |
the Free Software Foundation, either version 2.1 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
QuaZIP is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU Lesser General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Lesser General Public License |
| 17 |
along with QuaZIP. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
See COPYING file for the full LGPL text. |
| 20 |
|
| 21 |
Original ZIP package is copyrighted by Gilles Vollant and contributors, |
| 22 |
see quazip/(un)zip.h files for details. Basically it's the zlib license. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "testquaziodevice.h" |
| 26 |
#include <quazip/quaziodevice.h> |
| 27 |
#include <QBuffer> |
| 28 |
#include <QByteArray> |
| 29 |
#include <QtTest/QtTest> |
| 30 |
|
| 31 |
void TestQuaZIODevice::read() |
| 32 |
{ |
| 33 |
QByteArray buf(256, 0); |
| 34 |
z_stream zouts; |
| 35 |
zouts.zalloc = (alloc_func) NULL; |
| 36 |
zouts.zfree = (free_func) NULL; |
| 37 |
zouts.opaque = NULL; |
| 38 |
deflateInit(&zouts, Z_DEFAULT_COMPRESSION); |
| 39 |
zouts.next_in = reinterpret_cast<Bytef*>(const_cast<char*>("test")); |
| 40 |
zouts.avail_in = 4; |
| 41 |
zouts.next_out = reinterpret_cast<Bytef*>(buf.data()); |
| 42 |
zouts.avail_out = buf.size(); |
| 43 |
deflate(&zouts, Z_FINISH); |
| 44 |
deflateEnd(&zouts); |
| 45 |
QBuffer testBuffer(&buf); |
| 46 |
testBuffer.open(QIODevice::ReadOnly); |
| 47 |
QuaZIODevice testDevice(&testBuffer); |
| 48 |
QVERIFY(testDevice.open(QIODevice::ReadOnly)); |
| 49 |
char outBuf[5]; |
| 50 |
QCOMPARE(testDevice.read(outBuf, 5), static_cast<qint64>(4)); |
| 51 |
testDevice.close(); |
| 52 |
QVERIFY(!testDevice.isOpen()); |
| 53 |
} |
| 54 |
|
| 55 |
void TestQuaZIODevice::readMany() |
| 56 |
{ |
| 57 |
QByteArray buf(256, 0); |
| 58 |
z_stream zouts; |
| 59 |
zouts.zalloc = (alloc_func) NULL; |
| 60 |
zouts.zfree = (free_func) NULL; |
| 61 |
zouts.opaque = NULL; |
| 62 |
deflateInit(&zouts, Z_DEFAULT_COMPRESSION); |
| 63 |
zouts.next_in = reinterpret_cast<Bytef*>(const_cast<char*>("testtest")); |
| 64 |
zouts.avail_in = 8; |
| 65 |
zouts.next_out = reinterpret_cast<Bytef*>(buf.data()); |
| 66 |
zouts.avail_out = buf.size(); |
| 67 |
deflate(&zouts, Z_FINISH); |
| 68 |
deflateEnd(&zouts); |
| 69 |
QBuffer testBuffer(&buf); |
| 70 |
testBuffer.open(QIODevice::ReadOnly); |
| 71 |
QuaZIODevice testDevice(&testBuffer); |
| 72 |
QVERIFY(testDevice.open(QIODevice::ReadOnly)); |
| 73 |
char outBuf[4]; |
| 74 |
QCOMPARE(testDevice.read(outBuf, 4), static_cast<qint64>(4)); |
| 75 |
QVERIFY(!testDevice.atEnd()); |
| 76 |
QVERIFY(testDevice.bytesAvailable() > 0); |
| 77 |
QCOMPARE(testDevice.read(4).size(), 4); |
| 78 |
QCOMPARE(testDevice.bytesAvailable(), static_cast<qint64>(0)); |
| 79 |
QVERIFY(testDevice.atEnd()); |
| 80 |
testDevice.close(); |
| 81 |
QVERIFY(!testDevice.isOpen()); |
| 82 |
} |
| 83 |
|
| 84 |
void TestQuaZIODevice::write() |
| 85 |
{ |
| 86 |
QByteArray buf(256, 0); |
| 87 |
QBuffer testBuffer(&buf); |
| 88 |
testBuffer.open(QIODevice::WriteOnly); |
| 89 |
QuaZIODevice *testDevice = new QuaZIODevice(&testBuffer); |
| 90 |
QCOMPARE(testDevice->getIoDevice(), &testBuffer); |
| 91 |
QVERIFY(testDevice->open(QIODevice::WriteOnly)); |
| 92 |
QCOMPARE(testDevice->write("test", 4), static_cast<qint64>(4)); |
| 93 |
testDevice->close(); |
| 94 |
QVERIFY(!testDevice->isOpen()); |
| 95 |
z_stream zins; |
| 96 |
zins.zalloc = (alloc_func) NULL; |
| 97 |
zins.zfree = (free_func) NULL; |
| 98 |
zins.opaque = NULL; |
| 99 |
inflateInit(&zins); |
| 100 |
zins.next_in = reinterpret_cast<Bytef*>(buf.data()); |
| 101 |
zins.avail_in = testBuffer.pos(); |
| 102 |
char outBuf[5]; |
| 103 |
zins.next_out = reinterpret_cast<Bytef*>(outBuf); |
| 104 |
zins.avail_out = 5; |
| 105 |
inflate(&zins, Z_FINISH); |
| 106 |
inflateEnd(&zins); |
| 107 |
int size = 5 - zins.avail_out; |
| 108 |
QCOMPARE(size, 4); |
| 109 |
outBuf[4] = '\0'; |
| 110 |
QCOMPARE(static_cast<const char*>(outBuf), "test"); |
| 111 |
delete testDevice; // Test D0 destructor |
| 112 |
} |