| 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 "testjlcompress.h" |
| 26 |
|
| 27 |
#include "qztest.h" |
| 28 |
|
| 29 |
#include <QDir> |
| 30 |
#include <QFileInfo> |
| 31 |
|
| 32 |
#include <QtTest/QtTest> |
| 33 |
|
| 34 |
#include <quazip/JlCompress.h> |
| 35 |
|
| 36 |
#ifdef Q_OS_WIN |
| 37 |
#include <Windows.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
void TestJlCompress::compressFile_data() |
| 41 |
{ |
| 42 |
QTest::addColumn<QString>("zipName"); |
| 43 |
QTest::addColumn<QString>("fileName"); |
| 44 |
QTest::newRow("simple") << "jlsimplefile.zip" << "test0.txt"; |
| 45 |
} |
| 46 |
|
| 47 |
void TestJlCompress::compressFile() |
| 48 |
{ |
| 49 |
QFETCH(QString, zipName); |
| 50 |
QFETCH(QString, fileName); |
| 51 |
QDir curDir; |
| 52 |
if (curDir.exists(zipName)) { |
| 53 |
if (!curDir.remove(zipName)) |
| 54 |
QFAIL("Can't remove zip file"); |
| 55 |
} |
| 56 |
if (!createTestFiles(QStringList() << fileName)) { |
| 57 |
QFAIL("Can't create test file"); |
| 58 |
} |
| 59 |
QVERIFY(JlCompress::compressFile(zipName, "tmp/" + fileName)); |
| 60 |
// get the file list and check it |
| 61 |
QStringList fileList = JlCompress::getFileList(zipName); |
| 62 |
QCOMPARE(fileList.count(), 1); |
| 63 |
QVERIFY(fileList[0] == fileName); |
| 64 |
// now test the QIODevice* overload of getFileList() |
| 65 |
QFile zipFile(zipName); |
| 66 |
QVERIFY(zipFile.open(QIODevice::ReadOnly)); |
| 67 |
fileList = JlCompress::getFileList(zipName); |
| 68 |
QCOMPARE(fileList.count(), 1); |
| 69 |
QVERIFY(fileList[0] == fileName); |
| 70 |
zipFile.close(); |
| 71 |
removeTestFiles(QStringList() << fileName); |
| 72 |
curDir.remove(zipName); |
| 73 |
} |
| 74 |
|
| 75 |
void TestJlCompress::compressFiles_data() |
| 76 |
{ |
| 77 |
QTest::addColumn<QString>("zipName"); |
| 78 |
QTest::addColumn<QStringList>("fileNames"); |
| 79 |
QTest::newRow("simple") << "jlsimplefiles.zip" << |
| 80 |
(QStringList() << "test0.txt" << "test00.txt"); |
| 81 |
QTest::newRow("different subdirs") << "jlsubdirfiles.zip" << |
| 82 |
(QStringList() << "subdir1/test1.txt" << "subdir2/test2.txt"); |
| 83 |
} |
| 84 |
|
| 85 |
void TestJlCompress::compressFiles() |
| 86 |
{ |
| 87 |
QFETCH(QString, zipName); |
| 88 |
QFETCH(QStringList, fileNames); |
| 89 |
QDir curDir; |
| 90 |
if (curDir.exists(zipName)) { |
| 91 |
if (!curDir.remove(zipName)) |
| 92 |
QFAIL("Can't remove zip file"); |
| 93 |
} |
| 94 |
if (!createTestFiles(fileNames)) { |
| 95 |
QFAIL("Can't create test files"); |
| 96 |
} |
| 97 |
QStringList realNamesList, shortNamesList; |
| 98 |
foreach (QString fileName, fileNames) { |
| 99 |
QString realName = "tmp/" + fileName; |
| 100 |
realNamesList += realName; |
| 101 |
shortNamesList += QFileInfo(realName).fileName(); |
| 102 |
} |
| 103 |
QVERIFY(JlCompress::compressFiles(zipName, realNamesList)); |
| 104 |
// get the file list and check it |
| 105 |
QStringList fileList = JlCompress::getFileList(zipName); |
| 106 |
QCOMPARE(fileList, shortNamesList); |
| 107 |
removeTestFiles(fileNames); |
| 108 |
curDir.remove(zipName); |
| 109 |
} |
| 110 |
|
| 111 |
void TestJlCompress::compressDir_data() |
| 112 |
{ |
| 113 |
QTest::addColumn<QString>("zipName"); |
| 114 |
QTest::addColumn<QStringList>("fileNames"); |
| 115 |
QTest::addColumn<QStringList>("expected"); |
| 116 |
QTest::newRow("simple") << "jldir.zip" |
| 117 |
<< (QStringList() << "test0.txt" << "testdir1/test1.txt" |
| 118 |
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt") |
| 119 |
<< (QStringList() << "test0.txt" |
| 120 |
<< "testdir1/" << "testdir1/test1.txt" |
| 121 |
<< "testdir2/" << "testdir2/test2.txt" |
| 122 |
<< "testdir2/subdir/" << "testdir2/subdir/test2sub.txt"); |
| 123 |
QTest::newRow("empty dirs") << "jldir_empty.zip" |
| 124 |
<< (QStringList() << "testdir1/" << "testdir2/testdir3/") |
| 125 |
<< (QStringList() << "testdir1/" << "testdir2/" |
| 126 |
<< "testdir2/testdir3/"); |
| 127 |
QTest::newRow("hidden files") << "jldir_hidden.zip" |
| 128 |
<< (QStringList() << ".test0.txt" << "test1.txt") |
| 129 |
<< (QStringList() << ".test0.txt" << "test1.txt"); |
| 130 |
} |
| 131 |
|
| 132 |
void TestJlCompress::compressDir() |
| 133 |
{ |
| 134 |
QFETCH(QString, zipName); |
| 135 |
QFETCH(QStringList, fileNames); |
| 136 |
QFETCH(QStringList, expected); |
| 137 |
QDir curDir; |
| 138 |
if (curDir.exists(zipName)) { |
| 139 |
if (!curDir.remove(zipName)) |
| 140 |
QFAIL("Can't remove zip file"); |
| 141 |
} |
| 142 |
if (!createTestFiles(fileNames, "compressDir_tmp")) { |
| 143 |
QFAIL("Can't create test files"); |
| 144 |
} |
| 145 |
#ifdef Q_OS_WIN |
| 146 |
for (int i = 0; i < fileNames.size(); ++i) { |
| 147 |
if (fileNames.at(i).startsWith(".")) { |
| 148 |
QString fn = "compressDir_tmp\\" + fileNames.at(i); |
| 149 |
SetFileAttributes(reinterpret_cast<LPCWSTR>(fn.utf16()), |
| 150 |
FILE_ATTRIBUTE_HIDDEN); |
| 151 |
} |
| 152 |
} |
| 153 |
#endif |
| 154 |
QVERIFY(JlCompress::compressDir(zipName, "compressDir_tmp", true, QDir::Hidden)); |
| 155 |
// get the file list and check it |
| 156 |
QStringList fileList = JlCompress::getFileList(zipName); |
| 157 |
qSort(fileList); |
| 158 |
qSort(expected); |
| 159 |
QCOMPARE(fileList, expected); |
| 160 |
removeTestFiles(fileNames, "compressDir_tmp"); |
| 161 |
curDir.remove(zipName); |
| 162 |
} |
| 163 |
|
| 164 |
void TestJlCompress::extractFile_data() |
| 165 |
{ |
| 166 |
QTest::addColumn<QString>("zipName"); |
| 167 |
QTest::addColumn<QStringList>("fileNames"); |
| 168 |
QTest::addColumn<QString>("fileToExtract"); |
| 169 |
QTest::addColumn<QString>("destName"); |
| 170 |
QTest::addColumn<QByteArray>("encoding"); |
| 171 |
QTest::newRow("simple") << "jlextfile.zip" << ( |
| 172 |
QStringList() << "test0.txt" << "testdir1/test1.txt" |
| 173 |
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt") |
| 174 |
<< "testdir2/test2.txt" << "test2.txt" << QByteArray(); |
| 175 |
QTest::newRow("russian") << "jlextfilerus.zip" << ( |
| 176 |
QStringList() << "test0.txt" << "testdir1/test1.txt" |
| 177 |
<< QString::fromUtf8("testdir2/тест2.txt") |
| 178 |
<< "testdir2/subdir/test2sub.txt") |
| 179 |
<< QString::fromUtf8("testdir2/тест2.txt") |
| 180 |
<< QString::fromUtf8("тест2.txt") << QByteArray("IBM866"); |
| 181 |
QTest::newRow("extract dir") << "jlextdir.zip" << ( |
| 182 |
QStringList() << "testdir1/") |
| 183 |
<< "testdir1/" << "testdir1/" << QByteArray(); |
| 184 |
} |
| 185 |
|
| 186 |
void TestJlCompress::extractFile() |
| 187 |
{ |
| 188 |
QFETCH(QString, zipName); |
| 189 |
QFETCH(QStringList, fileNames); |
| 190 |
QFETCH(QString, fileToExtract); |
| 191 |
QFETCH(QString, destName); |
| 192 |
QFETCH(QByteArray, encoding); |
| 193 |
QDir curDir; |
| 194 |
if (!curDir.mkpath("jlext/jlfile")) { |
| 195 |
QFAIL("Couldn't mkpath jlext/jlfile"); |
| 196 |
} |
| 197 |
if (!createTestFiles(fileNames)) { |
| 198 |
QFAIL("Couldn't create test files"); |
| 199 |
} |
| 200 |
QFile srcFile("tmp/" + fileToExtract); |
| 201 |
QFile::Permissions srcPerm = srcFile.permissions(); |
| 202 |
// Invert the "write other" flag so permissions |
| 203 |
// are NOT default any more. Otherwise it's impossible |
| 204 |
// to figure out whether the permissions were set correctly |
| 205 |
// or JlCompress failed to set them completely, |
| 206 |
// thus leaving them at the default setting. |
| 207 |
srcPerm ^= QFile::WriteOther; |
| 208 |
QVERIFY(srcFile.setPermissions(srcPerm)); |
| 209 |
if (!createTestArchive(zipName, fileNames, |
| 210 |
QTextCodec::codecForName(encoding))) { |
| 211 |
QFAIL("Can't create test archive"); |
| 212 |
} |
| 213 |
QuaZip::setDefaultFileNameCodec(encoding); |
| 214 |
QVERIFY(!JlCompress::extractFile(zipName, fileToExtract, |
| 215 |
"jlext/jlfile/" + destName).isEmpty()); |
| 216 |
QFileInfo destInfo("jlext/jlfile/" + destName), srcInfo("tmp/" + |
| 217 |
fileToExtract); |
| 218 |
QCOMPARE(destInfo.size(), srcInfo.size()); |
| 219 |
QCOMPARE(destInfo.permissions(), srcInfo.permissions()); |
| 220 |
curDir.remove("jlext/jlfile/" + destName); |
| 221 |
// now test the QIODevice* overload |
| 222 |
QFile zipFile(zipName); |
| 223 |
QVERIFY(zipFile.open(QIODevice::ReadOnly)); |
| 224 |
QVERIFY(!JlCompress::extractFile(&zipFile, fileToExtract, |
| 225 |
"jlext/jlfile/" + destName).isEmpty()); |
| 226 |
destInfo = QFileInfo("jlext/jlfile/" + destName); |
| 227 |
QCOMPARE(destInfo.size(), srcInfo.size()); |
| 228 |
QCOMPARE(destInfo.permissions(), srcInfo.permissions()); |
| 229 |
curDir.remove("jlext/jlfile/" + destName); |
| 230 |
if (!fileToExtract.endsWith("/")) { |
| 231 |
// If we aren't extracting a directory, we need to check |
| 232 |
// that extractFile() fails if there is a directory |
| 233 |
// with the same name as the file being extracted. |
| 234 |
curDir.mkdir("jlext/jlfile/" + destName); |
| 235 |
QVERIFY(JlCompress::extractFile(zipName, fileToExtract, |
| 236 |
"jlext/jlfile/" + destName).isEmpty()); |
| 237 |
} |
| 238 |
zipFile.close(); |
| 239 |
// Here we either delete the target dir or the dir created in the |
| 240 |
// test above. |
| 241 |
curDir.rmpath("jlext/jlfile/" + destName); |
| 242 |
removeTestFiles(fileNames); |
| 243 |
curDir.remove(zipName); |
| 244 |
} |
| 245 |
|
| 246 |
void TestJlCompress::extractFiles_data() |
| 247 |
{ |
| 248 |
QTest::addColumn<QString>("zipName"); |
| 249 |
QTest::addColumn<QStringList>("fileNames"); |
| 250 |
QTest::addColumn<QStringList>("filesToExtract"); |
| 251 |
QTest::newRow("simple") << "jlextfiles.zip" << ( |
| 252 |
QStringList() << "test0.txt" << "testdir1/test1.txt" |
| 253 |
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt") |
| 254 |
<< (QStringList() << "testdir2/test2.txt" << "testdir1/test1.txt"); |
| 255 |
} |
| 256 |
|
| 257 |
void TestJlCompress::extractFiles() |
| 258 |
{ |
| 259 |
QFETCH(QString, zipName); |
| 260 |
QFETCH(QStringList, fileNames); |
| 261 |
QFETCH(QStringList, filesToExtract); |
| 262 |
QDir curDir; |
| 263 |
if (!curDir.mkpath("jlext/jlfiles")) { |
| 264 |
QFAIL("Couldn't mkpath jlext/jlfiles"); |
| 265 |
} |
| 266 |
if (!createTestFiles(fileNames)) { |
| 267 |
QFAIL("Couldn't create test files"); |
| 268 |
} |
| 269 |
if (!JlCompress::compressDir(zipName, "tmp")) { |
| 270 |
QFAIL("Couldn't create test archive"); |
| 271 |
} |
| 272 |
QVERIFY(!JlCompress::extractFiles(zipName, filesToExtract, |
| 273 |
"jlext/jlfiles").isEmpty()); |
| 274 |
foreach (QString fileName, filesToExtract) { |
| 275 |
QFileInfo fileInfo("jlext/jlfiles/" + fileName); |
| 276 |
QFileInfo extInfo("tmp/" + fileName); |
| 277 |
QCOMPARE(fileInfo.size(), extInfo.size()); |
| 278 |
QCOMPARE(fileInfo.permissions(), extInfo.permissions()); |
| 279 |
curDir.remove("jlext/jlfiles/" + fileName); |
| 280 |
curDir.rmpath(fileInfo.dir().path()); |
| 281 |
} |
| 282 |
// now test the QIODevice* overload |
| 283 |
QFile zipFile(zipName); |
| 284 |
QVERIFY(zipFile.open(QIODevice::ReadOnly)); |
| 285 |
QVERIFY(!JlCompress::extractFiles(&zipFile, filesToExtract, |
| 286 |
"jlext/jlfiles").isEmpty()); |
| 287 |
foreach (QString fileName, filesToExtract) { |
| 288 |
QFileInfo fileInfo("jlext/jlfiles/" + fileName); |
| 289 |
QFileInfo extInfo("tmp/" + fileName); |
| 290 |
QCOMPARE(fileInfo.size(), extInfo.size()); |
| 291 |
QCOMPARE(fileInfo.permissions(), extInfo.permissions()); |
| 292 |
curDir.remove("jlext/jlfiles/" + fileName); |
| 293 |
curDir.rmpath(fileInfo.dir().path()); |
| 294 |
} |
| 295 |
zipFile.close(); |
| 296 |
curDir.rmpath("jlext/jlfiles"); |
| 297 |
removeTestFiles(fileNames); |
| 298 |
curDir.remove(zipName); |
| 299 |
} |
| 300 |
|
| 301 |
void TestJlCompress::extractDir_data() |
| 302 |
{ |
| 303 |
QTest::addColumn<QString>("zipName"); |
| 304 |
QTest::addColumn<QStringList>("fileNames"); |
| 305 |
QTest::newRow("simple") << "jlextdir.zip" << ( |
| 306 |
QStringList() << "test0.txt" << "testdir1/test1.txt" |
| 307 |
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt"); |
| 308 |
QTest::newRow("separate dir") << "sepdir.zip" << ( |
| 309 |
QStringList() << "laj/" << "laj/lajfile.txt"); |
| 310 |
} |
| 311 |
|
| 312 |
void TestJlCompress::extractDir() |
| 313 |
{ |
| 314 |
QFETCH(QString, zipName); |
| 315 |
QFETCH(QStringList, fileNames); |
| 316 |
QDir curDir; |
| 317 |
if (!curDir.mkpath("jlext/jldir")) { |
| 318 |
QFAIL("Couldn't mkpath jlext/jldir"); |
| 319 |
} |
| 320 |
if (!createTestFiles(fileNames)) { |
| 321 |
QFAIL("Couldn't create test files"); |
| 322 |
} |
| 323 |
if (!createTestArchive(zipName, fileNames)) { |
| 324 |
QFAIL("Couldn't create test archive"); |
| 325 |
} |
| 326 |
QStringList extracted; |
| 327 |
QCOMPARE((extracted = JlCompress::extractDir(zipName, "jlext/jldir")) |
| 328 |
.count(), fileNames.count()); |
| 329 |
foreach (QString fileName, fileNames) { |
| 330 |
QString fullName = "jlext/jldir/" + fileName; |
| 331 |
QFileInfo fileInfo(fullName); |
| 332 |
QFileInfo extInfo("tmp/" + fileName); |
| 333 |
if (!fileInfo.isDir()) |
| 334 |
QCOMPARE(fileInfo.size(), extInfo.size()); |
| 335 |
QCOMPARE(fileInfo.permissions(), extInfo.permissions()); |
| 336 |
curDir.remove(fullName); |
| 337 |
curDir.rmpath(fileInfo.dir().path()); |
| 338 |
QString absolutePath = fileInfo.absoluteFilePath(); |
| 339 |
if (fileInfo.isDir() && !absolutePath.endsWith('/')) |
| 340 |
absolutePath += '/'; |
| 341 |
QVERIFY(extracted.contains(absolutePath)); |
| 342 |
} |
| 343 |
// now test the QIODevice* overload |
| 344 |
QFile zipFile(zipName); |
| 345 |
QVERIFY(zipFile.open(QIODevice::ReadOnly)); |
| 346 |
QCOMPARE((extracted = JlCompress::extractDir(&zipFile, "jlext/jldir")) |
| 347 |
.count(), fileNames.count()); |
| 348 |
foreach (QString fileName, fileNames) { |
| 349 |
QString fullName = "jlext/jldir/" + fileName; |
| 350 |
QFileInfo fileInfo(fullName); |
| 351 |
QFileInfo extInfo("tmp/" + fileName); |
| 352 |
if (!fileInfo.isDir()) |
| 353 |
QCOMPARE(fileInfo.size(), extInfo.size()); |
| 354 |
QCOMPARE(fileInfo.permissions(), extInfo.permissions()); |
| 355 |
curDir.remove(fullName); |
| 356 |
curDir.rmpath(fileInfo.dir().path()); |
| 357 |
QString absolutePath = fileInfo.absoluteFilePath(); |
| 358 |
if (fileInfo.isDir() && !absolutePath.endsWith('/')) |
| 359 |
absolutePath += '/'; |
| 360 |
QVERIFY(extracted.contains(absolutePath)); |
| 361 |
} |
| 362 |
zipFile.close(); |
| 363 |
curDir.rmpath("jlext/jldir"); |
| 364 |
removeTestFiles(fileNames); |
| 365 |
curDir.remove(zipName); |
| 366 |
} |
| 367 |
|
| 368 |
void TestJlCompress::zeroPermissions() |
| 369 |
{ |
| 370 |
QuaZip zipCreator("zero.zip"); |
| 371 |
QVERIFY(zipCreator.open(QuaZip::mdCreate)); |
| 372 |
QuaZipFile zeroFile(&zipCreator); |
| 373 |
QuaZipNewInfo newInfo("zero.txt"); |
| 374 |
newInfo.externalAttr = 0; // should be zero anyway, but just in case |
| 375 |
QVERIFY(zeroFile.open(QIODevice::WriteOnly, newInfo)); |
| 376 |
zeroFile.close(); |
| 377 |
zipCreator.close(); |
| 378 |
QVERIFY(!JlCompress::extractFile("zero.zip", "zero.txt").isEmpty()); |
| 379 |
QVERIFY(QFile("zero.txt").permissions() != 0); |
| 380 |
QDir curDir; |
| 381 |
curDir.remove("zero.zip"); |
| 382 |
curDir.remove("zero.txt"); |
| 383 |
} |