[1049] | 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 "qztest.h"
|
---|
| 26 | #include "testquazip.h"
|
---|
| 27 | #include "testquazipfile.h"
|
---|
| 28 | #include "testquachecksum32.h"
|
---|
| 29 | #include "testjlcompress.h"
|
---|
| 30 | #include "testquazipdir.h"
|
---|
| 31 | #include "testquagzipfile.h"
|
---|
| 32 | #include "testquaziodevice.h"
|
---|
| 33 | #include "testquazipnewinfo.h"
|
---|
| 34 | #include "testquazipfileinfo.h"
|
---|
| 35 |
|
---|
| 36 | #include <quazip/quazip.h>
|
---|
| 37 | #include <quazip/quazipfile.h>
|
---|
| 38 |
|
---|
| 39 | #include <QCoreApplication>
|
---|
| 40 | #include <QDir>
|
---|
| 41 | #include <QFileInfo>
|
---|
| 42 | #include <QTextStream>
|
---|
| 43 |
|
---|
| 44 | #include <QtTest/QtTest>
|
---|
| 45 |
|
---|
| 46 | bool createTestFiles(const QStringList &fileNames, const QString &dir)
|
---|
| 47 | {
|
---|
| 48 | QDir curDir;
|
---|
| 49 | foreach (QString fileName, fileNames) {
|
---|
| 50 | QString filePath = QDir(dir).filePath(fileName);
|
---|
| 51 | QDir testDir = QFileInfo(filePath).dir();
|
---|
| 52 | if (!testDir.exists()) {
|
---|
| 53 | if (!curDir.mkpath(testDir.path())) {
|
---|
| 54 | qWarning("Couldn't mkpath %s",
|
---|
| 55 | testDir.path().toUtf8().constData());
|
---|
| 56 | return false;
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | if (fileName.endsWith('/')) {
|
---|
| 60 | if (!curDir.mkpath(filePath)) {
|
---|
| 61 | qWarning("Couldn't mkpath %s",
|
---|
| 62 | fileName.toUtf8().constData());
|
---|
| 63 | return false;
|
---|
| 64 | }
|
---|
| 65 | } else {
|
---|
| 66 | QFile testFile(filePath);
|
---|
| 67 | if (!testFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
---|
| 68 | qWarning("Couldn't create %s",
|
---|
| 69 | fileName.toUtf8().constData());
|
---|
| 70 | return false;
|
---|
| 71 | }
|
---|
| 72 | QTextStream testStream(&testFile);
|
---|
| 73 | testStream << "This is a test file named " << fileName << endl;
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | return true;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | bool createTestArchive(QuaZip &zip, const QString &zipName,
|
---|
| 80 | const QStringList &fileNames,
|
---|
| 81 | QTextCodec *codec,
|
---|
| 82 | const QString &dir)
|
---|
| 83 | {
|
---|
| 84 | if (codec != NULL) {
|
---|
| 85 | zip.setFileNameCodec(codec);
|
---|
| 86 | }
|
---|
| 87 | if (!zip.open(QuaZip::mdCreate)) {
|
---|
| 88 | qWarning("Couldn't open %s", zipName.toUtf8().constData());
|
---|
| 89 | return false;
|
---|
| 90 | }
|
---|
| 91 | int i = 0;
|
---|
| 92 | QDateTime dt1;
|
---|
| 93 | foreach (QString fileName, fileNames) {
|
---|
| 94 | QuaZipFile zipFile(&zip);
|
---|
| 95 | QString filePath = QDir(dir).filePath(fileName);
|
---|
| 96 | QFileInfo fileInfo(filePath);
|
---|
| 97 | QuaZipNewInfo newInfo(fileName, filePath);
|
---|
| 98 | if (i == 0) // to test code that needs different timestamps
|
---|
| 99 | newInfo.dateTime = newInfo.dateTime.addSecs(-60);
|
---|
| 100 | else if (i == 1) // will use for the next file too
|
---|
| 101 | dt1 = newInfo.dateTime;
|
---|
| 102 | else if (i == 2) // to test identical timestamps
|
---|
| 103 | newInfo.dateTime = dt1;
|
---|
| 104 | if (!zipFile.open(QIODevice::WriteOnly,
|
---|
| 105 | newInfo, NULL, 0,
|
---|
| 106 | fileInfo.isDir() ? 0 : 8)) {
|
---|
| 107 | qWarning("Couldn't open %s in %s", fileName.toUtf8()
|
---|
| 108 | .constData(), zipName.toUtf8().constData());
|
---|
| 109 | return false;
|
---|
| 110 | }
|
---|
| 111 | if (!fileInfo.isDir()) {
|
---|
| 112 | QFile file(filePath);
|
---|
| 113 | if (!file.open(QIODevice::ReadOnly)) {
|
---|
| 114 | qWarning("Couldn't open %s", filePath.toUtf8()
|
---|
| 115 | .constData());
|
---|
| 116 | return false;
|
---|
| 117 | }
|
---|
| 118 | while (!file.atEnd()) {
|
---|
| 119 | char buf[4096];
|
---|
| 120 | qint64 l = file.read(buf, 4096);
|
---|
| 121 | if (l <= 0) {
|
---|
| 122 | qWarning("Couldn't read %s", filePath.toUtf8()
|
---|
| 123 | .constData());
|
---|
| 124 | return false;
|
---|
| 125 | }
|
---|
| 126 | if (zipFile.write(buf, l) != l) {
|
---|
| 127 | qWarning("Couldn't write to %s in %s",
|
---|
| 128 | filePath.toUtf8().constData(),
|
---|
| 129 | zipName.toUtf8().constData());
|
---|
| 130 | return false;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | file.close();
|
---|
| 134 | }
|
---|
| 135 | zipFile.close();
|
---|
| 136 | ++i;
|
---|
| 137 | }
|
---|
| 138 | zip.setComment(QString("This is the test archive"));
|
---|
| 139 | zip.close();
|
---|
| 140 | if (zipName.startsWith("<")) { // something like "<QIODevice pointer>"
|
---|
| 141 | return true;
|
---|
| 142 | } else {
|
---|
| 143 | return QFileInfo(zipName).exists();
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | bool createTestArchive(const QString &zipName,
|
---|
| 148 | const QStringList &fileNames,
|
---|
| 149 | const QString &dir) {
|
---|
| 150 | return createTestArchive(zipName, fileNames, NULL, dir);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | bool createTestArchive(QIODevice *ioDevice,
|
---|
| 154 | const QStringList &fileNames,
|
---|
| 155 | QTextCodec *codec,
|
---|
| 156 | const QString &dir)
|
---|
| 157 | {
|
---|
| 158 | QuaZip zip(ioDevice);
|
---|
| 159 | return createTestArchive(zip, "<QIODevice pointer>", fileNames, codec, dir);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | bool createTestArchive(const QString &zipName,
|
---|
| 163 | const QStringList &fileNames,
|
---|
| 164 | QTextCodec *codec,
|
---|
| 165 | const QString &dir) {
|
---|
| 166 | QuaZip zip(zipName);
|
---|
| 167 | return createTestArchive(zip, zipName, fileNames, codec, dir);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | void removeTestFiles(const QStringList &fileNames, const QString &dir)
|
---|
| 171 | {
|
---|
| 172 | QDir curDir;
|
---|
| 173 | foreach (QString fileName, fileNames) {
|
---|
| 174 | curDir.remove(QDir(dir).filePath(fileName));
|
---|
| 175 | }
|
---|
| 176 | foreach (QString fileName, fileNames) {
|
---|
| 177 | QDir fileDir = QFileInfo(QDir(dir).filePath(fileName)).dir();
|
---|
| 178 | if (fileDir.exists()) {
|
---|
| 179 | // Non-empty dirs won't get removed, and that's good.
|
---|
| 180 | curDir.rmpath(fileDir.path());
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | int main(int argc, char **argv)
|
---|
| 186 | {
|
---|
| 187 | QCoreApplication app(argc, argv);
|
---|
| 188 | int err = 0;
|
---|
| 189 | {
|
---|
| 190 | TestQuaZip testQuaZip;
|
---|
| 191 | err = qMax(err, QTest::qExec(&testQuaZip, app.arguments()));
|
---|
| 192 | }
|
---|
| 193 | {
|
---|
| 194 | TestQuaZipFile testQuaZipFile;
|
---|
| 195 | err = qMax(err, QTest::qExec(&testQuaZipFile, app.arguments()));
|
---|
| 196 | }
|
---|
| 197 | {
|
---|
| 198 | TestQuaChecksum32 testQuaChecksum32;
|
---|
| 199 | err = qMax(err, QTest::qExec(&testQuaChecksum32, app.arguments()));
|
---|
| 200 | }
|
---|
| 201 | {
|
---|
| 202 | TestJlCompress testJlCompress;
|
---|
| 203 | err = qMax(err, QTest::qExec(&testJlCompress, app.arguments()));
|
---|
| 204 | }
|
---|
| 205 | {
|
---|
| 206 | TestQuaZipDir testQuaZipDir;
|
---|
| 207 | err = qMax(err, QTest::qExec(&testQuaZipDir, app.arguments()));
|
---|
| 208 | }
|
---|
| 209 | {
|
---|
| 210 | TestQuaZIODevice testQuaZIODevice;
|
---|
| 211 | err = qMax(err, QTest::qExec(&testQuaZIODevice, app.arguments()));
|
---|
| 212 | }
|
---|
| 213 | {
|
---|
| 214 | TestQuaGzipFile testQuaGzipFile;
|
---|
| 215 | err = qMax(err, QTest::qExec(&testQuaGzipFile, app.arguments()));
|
---|
| 216 | }
|
---|
| 217 | {
|
---|
| 218 | TestQuaZipNewInfo testQuaZipNewInfo;
|
---|
| 219 | err = qMax(err, QTest::qExec(&testQuaZipNewInfo, app.arguments()));
|
---|
| 220 | }
|
---|
| 221 | {
|
---|
| 222 | TestQuaZipFileInfo testQuaZipFileInfo;
|
---|
| 223 | err = qMax(err, QTest::qExec(&testQuaZipFileInfo, app.arguments()));
|
---|
| 224 | }
|
---|
| 225 | if (err == 0) {
|
---|
| 226 | qDebug("All tests executed successfully");
|
---|
| 227 | } else {
|
---|
| 228 | qWarning("There were errors in some of the tests above.");
|
---|
| 229 | }
|
---|
| 230 | return err;
|
---|
| 231 | }
|
---|