-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathtestjlcompress.cpp
767 lines (727 loc) · 32.9 KB
/
testjlcompress.cpp
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/*
Copyright (C) 2005-2014 Sergey A. Tachenov
This file is part of QuaZip test suite.
QuaZip is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.
QuaZip is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with QuaZip. If not, see <http://www.gnu.org/licenses/>.
See COPYING file for the full LGPL text.
Original ZIP package is copyrighted by Gilles Vollant and contributors,
see quazip/(un)zip.h files for details. Basically it's the zlib license.
*/
#include "testjlcompress.h"
#include "qztest.h"
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QMetaType>
#include <quazip_qt_compat.h>
#include <QtTest/QtTest>
#include <JlCompress.h>
#ifdef Q_OS_WIN
#include <windows.h>
#endif
Q_DECLARE_METATYPE(JlCompress::Options::CompressionStrategy)
void TestJlCompress::compressFile_data()
{
QTest::addColumn<QString>("zipName");
QTest::addColumn<QString>("fileName");
QTest::newRow("simple") << "jlsimplefile.zip" << "test0.txt";
}
void TestJlCompress::compressFile()
{
QFETCH(QString, zipName);
QFETCH(QString, fileName);
QDir curDir;
if (curDir.exists(zipName)) {
if (!curDir.remove(zipName))
QFAIL("Can't remove zip file");
}
if (!createTestFiles(QStringList() << fileName)) {
QFAIL("Can't create test file");
}
QVERIFY(JlCompress::compressFile(zipName, "tmp/" + fileName));
// get the file list and check it
QStringList fileList = JlCompress::getFileList(zipName);
QCOMPARE(fileList.count(), 1);
QVERIFY(fileList[0] == fileName);
// now test the QIODevice* overload of getFileList()
QFile zipFile(zipName);
QVERIFY(zipFile.open(QIODevice::ReadOnly));
fileList = JlCompress::getFileList(zipName);
QCOMPARE(fileList.count(), 1);
QVERIFY(fileList[0] == fileName);
zipFile.close();
removeTestFiles(QStringList() << fileName);
curDir.remove(zipName);
}
void TestJlCompress::compressFileOptions_data()
{
QTest::addColumn<QString>("zipName");
QTest::addColumn<QString>("fileName");
QTest::addColumn<QDateTime>("dateTime");
QTest::addColumn<JlCompress::Options::CompressionStrategy>("strategy");
QTest::addColumn<bool>("utf8");
QTest::addColumn<QString>("sha256sum_unix"); // Due to extra data archives are not identical
QTest::addColumn<QString>("sha256sum_win");
QTest::newRow("simple") << "jlsimplefile.zip"
<< "test0.txt"
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Default
<< false
<< "5eedd83aee92cf3381155d167fee54a4ef6e43b8bc7a979c903611d9aa28610a"
<< "cb1847dff1a5c33a805efde2558fc74024ad4c64c8607f8b12903e4d92385955";
QTest::newRow("simple-utf8") << "jlsimplefile-utf8.zip"
<< QString::fromUtf8("ありがとう。.txt")
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Default
<< true
<< ""
<< "";
QTest::newRow("simple-utf8-bad") << "jlsimplefile-utf8-bad.zip"
<< QString::fromUtf8("ありがとう。.txt")
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Default
<< false
<< ""
<< "";
QTest::newRow("simple-storage") << "jlsimplefile-storage.zip"
<< "test0.txt"
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Storage
<< false
<< ""
<< "";
QTest::newRow("simple-fastest") << "jlsimplefile-fastest.zip"
<< "test0.txt"
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Fastest
<< false
<< ""
<< "";
QTest::newRow("simple-faster") << "jlsimplefile-faster.zip"
<< "test0.txt"
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Faster
<< false
<< ""
<< "";
QTest::newRow("simple-standard") << "jlsimplefile-standard.zip"
<< "test0.txt"
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Standard
<< false
<< "5eedd83aee92cf3381155d167fee54a4ef6e43b8bc7a979c903611d9aa28610a"
<< "cb1847dff1a5c33a805efde2558fc74024ad4c64c8607f8b12903e4d92385955";
QTest::newRow("simple-better") << "jlsimplefile-better.zip"
<< "test0.txt"
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Better
<< false
<< ""
<< "";
QTest::newRow("simple-best") << "jlsimplefile-best.zip"
<< "test0.txt"
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Best
<< false
<< ""
<< "";
}
void TestJlCompress::compressFileOptions()
{
QFETCH(QString, zipName);
QFETCH(QString, fileName);
QFETCH(QDateTime, dateTime);
QFETCH(JlCompress::Options::CompressionStrategy, strategy);
QFETCH(bool, utf8);
QFETCH(QString, sha256sum_unix);
QFETCH(QString, sha256sum_win);
QDir curDir;
if (curDir.exists(zipName)) {
if (!curDir.remove(zipName))
QFAIL("Can't remove zip file");
}
if (!createTestFiles(QStringList() << fileName)) {
QFAIL("Can't create test file");
}
const JlCompress::Options options(dateTime, strategy, utf8);
QVERIFY(JlCompress::compressFile(zipName, "tmp/" + fileName, options));
// get the file list and check it
QStringList fileList = JlCompress::getFileList(zipName);
QCOMPARE(fileList.count(), 1);
QVERIFY(fileList[0] == fileName);
// now test the QIODevice* overload of getFileList()
QFile zipFile(zipName);
QVERIFY(zipFile.open(QIODevice::ReadOnly));
fileList = JlCompress::getFileList(zipName);
QCOMPARE(fileList.count(), 1);
QVERIFY(fileList[0] == fileName);
// Hash is computed on the resulting file externally, then hardcoded in the test data
// This should help detecting any library breakage since we compare against a well-known stable result
QString hash = QCryptographicHash::hash(zipFile.readAll(), QCryptographicHash::Sha256).toHex();
#ifdef Q_OS_WIN
if (!sha256sum_win.isEmpty()) QCOMPARE(hash, sha256sum_win);
#else
if (!sha256sum_unix.isEmpty()) QCOMPARE(hash, sha256sum_unix);
#endif
zipFile.close();
// Extract
QString flist = JlCompress::extractFile(zipName, fileName);
QFileInfo fileInfo(flist);
QVERIFY(fileInfo.fileName() == fileName);
removeTestFiles(QStringList() << fileName << flist);
curDir.remove(zipName);
}
void TestJlCompress::compressFiles_data()
{
QTest::addColumn<QString>("zipName");
QTest::addColumn<QStringList>("fileNames");
QTest::newRow("simple") << "jlsimplefiles.zip" <<
(QStringList() << "test0.txt" << "test00.txt");
QTest::newRow("different subdirs") << "jlsubdirfiles.zip" <<
(QStringList() << "subdir1/test1.txt" << "subdir2/test2.txt");
}
void TestJlCompress::compressFiles()
{
QFETCH(QString, zipName);
QFETCH(QStringList, fileNames);
QDir curDir;
if (curDir.exists(zipName)) {
if (!curDir.remove(zipName))
QFAIL("Can't remove zip file");
}
if (!createTestFiles(fileNames)) {
QFAIL("Can't create test files");
}
QStringList realNamesList, shortNamesList;
foreach (QString fileName, fileNames) {
QString realName = "tmp/" + fileName;
realNamesList += realName;
shortNamesList += QFileInfo(realName).fileName();
}
QVERIFY(JlCompress::compressFiles(zipName, realNamesList));
// get the file list and check it
QStringList fileList = JlCompress::getFileList(zipName);
QCOMPARE(fileList, shortNamesList);
removeTestFiles(fileNames);
curDir.remove(zipName);
}
void TestJlCompress::compressDir_data()
{
QTest::addColumn<QString>("zipName");
QTest::addColumn<QStringList>("fileNames");
QTest::addColumn<QStringList>("expected");
QTest::newRow("simple") << "jldir.zip"
<< (QStringList() << "test0.txt" << "testdir1/test1.txt"
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
<< (QStringList() << "test0.txt"
<< "testdir1/" << "testdir1/test1.txt"
<< "testdir2/" << "testdir2/test2.txt"
<< "testdir2/subdir/" << "testdir2/subdir/test2sub.txt");
QTest::newRow("empty dirs") << "jldir_empty.zip"
<< (QStringList() << "testdir1/" << "testdir2/testdir3/")
<< (QStringList() << "testdir1/" << "testdir2/"
<< "testdir2/testdir3/");
QTest::newRow("hidden files") << "jldir_hidden.zip"
<< (QStringList() << ".test0.txt" << "test1.txt")
<< (QStringList() << ".test0.txt" << "test1.txt");
}
void TestJlCompress::compressDir()
{
QFETCH(QString, zipName);
QFETCH(QStringList, fileNames);
QFETCH(QStringList, expected);
QDir curDir;
if (curDir.exists(zipName)) {
if (!curDir.remove(zipName))
QFAIL("Can't remove zip file");
}
if (!createTestFiles(fileNames, -1, "compressDir_tmp")) {
QFAIL("Can't create test files");
}
#ifdef Q_OS_WIN
for (int i = 0; i < fileNames.size(); ++i) {
if (fileNames.at(i).startsWith(".")) {
QString fn = "compressDir_tmp\\" + fileNames.at(i);
SetFileAttributesW(reinterpret_cast<LPCWSTR>(fn.utf16()),
FILE_ATTRIBUTE_HIDDEN);
}
}
#endif
QVERIFY(JlCompress::compressDir(zipName, "compressDir_tmp", true, QDir::Hidden));
// get the file list and check it
QStringList fileList = JlCompress::getFileList(zipName);
fileList.sort();
expected.sort();
QCOMPARE(fileList, expected);
removeTestFiles(fileNames, "compressDir_tmp");
curDir.remove(zipName);
}
void TestJlCompress::compressDirOptions_data()
{
QTest::addColumn<QString>("zipName");
QTest::addColumn<QStringList>("fileNames");
QTest::addColumn<QStringList>("expected");
QTest::addColumn<QDateTime>("dateTime");
QTest::addColumn<JlCompress::Options::CompressionStrategy>("strategy");
QTest::addColumn<QString>("sha256sum_unix");
QTest::addColumn<QString>("sha256sum_win");
QTest::newRow("simple") << "jldir.zip"
<< (QStringList() << "test0.txt" << "testdir1/test1.txt"
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
<< (QStringList() << "test0.txt"
<< "testdir1/" << "testdir1/test1.txt"
<< "testdir2/" << "testdir2/test2.txt"
<< "testdir2/subdir/" << "testdir2/subdir/test2sub.txt")
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Default
<< "ed0d5921b2fc11b6b4cb214b3e43ea3ea28987d6ff8080faab54c4756de30af6"
<< "1eba110a33718c07a4ddf3fa515d1b4c6e3f4fc912b2e29e5e32783e2cddf852";
QTest::newRow("simple-storage") << "jldir-storage.zip"
<< (QStringList() << "test0.txt" << "testdir1/test1.txt"
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
<< (QStringList() << "test0.txt"
<< "testdir1/" << "testdir1/test1.txt"
<< "testdir2/" << "testdir2/test2.txt"
<< "testdir2/subdir/" << "testdir2/subdir/test2sub.txt")
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Storage
<< ""
<< "";
QTest::newRow("simple-fastest") << "jldir-fastest.zip"
<< (QStringList() << "test0.txt" << "testdir1/test1.txt"
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
<< (QStringList() << "test0.txt"
<< "testdir1/" << "testdir1/test1.txt"
<< "testdir2/" << "testdir2/test2.txt"
<< "testdir2/subdir/" << "testdir2/subdir/test2sub.txt")
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Fastest
<< ""
<< "";
QTest::newRow("simple-faster") << "jldir-faster.zip"
<< (QStringList() << "test0.txt" << "testdir1/test1.txt"
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
<< (QStringList() << "test0.txt"
<< "testdir1/" << "testdir1/test1.txt"
<< "testdir2/" << "testdir2/test2.txt"
<< "testdir2/subdir/" << "testdir2/subdir/test2sub.txt")
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Faster
<< ""
<< "";
QTest::newRow("simple-standard") << "jldir-standard.zip"
<< (QStringList() << "test0.txt" << "testdir1/test1.txt"
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
<< (QStringList() << "test0.txt"
<< "testdir1/" << "testdir1/test1.txt"
<< "testdir2/" << "testdir2/test2.txt"
<< "testdir2/subdir/" << "testdir2/subdir/test2sub.txt")
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Standard
<< "ed0d5921b2fc11b6b4cb214b3e43ea3ea28987d6ff8080faab54c4756de30af6"
<< "1eba110a33718c07a4ddf3fa515d1b4c6e3f4fc912b2e29e5e32783e2cddf852";
QTest::newRow("simple-better") << "jldir-better.zip"
<< (QStringList() << "test0.txt" << "testdir1/test1.txt"
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
<< (QStringList() << "test0.txt"
<< "testdir1/" << "testdir1/test1.txt"
<< "testdir2/" << "testdir2/test2.txt"
<< "testdir2/subdir/" << "testdir2/subdir/test2sub.txt")
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Better
<< ""
<< "";
QTest::newRow("simple-best") << "jldir-best.zip"
<< (QStringList() << "test0.txt" << "testdir1/test1.txt"
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
<< (QStringList() << "test0.txt"
<< "testdir1/" << "testdir1/test1.txt"
<< "testdir2/" << "testdir2/test2.txt"
<< "testdir2/subdir/" << "testdir2/subdir/test2sub.txt")
<< QDateTime(QDate(2024, 9, 19), QTime(21, 0, 0), QTimeZone::utc())
<< JlCompress::Options::Best
<< ""
<< "";
}
void TestJlCompress::compressDirOptions()
{
QFETCH(QString, zipName);
QFETCH(QStringList, fileNames);
QFETCH(QStringList, expected);
QFETCH(QDateTime, dateTime);
QFETCH(JlCompress::Options::CompressionStrategy, strategy);
QFETCH(QString, sha256sum_unix);
QFETCH(QString, sha256sum_win);
QDir curDir;
if (curDir.exists(zipName)) {
if (!curDir.remove(zipName))
QFAIL("Can't remove zip file");
}
if (!createTestFiles(fileNames, -1, "compressDir_tmp")) {
QFAIL("Can't create test files");
}
#ifdef Q_OS_WIN
for (int i = 0; i < fileNames.size(); ++i) {
if (fileNames.at(i).startsWith(".")) {
QString fn = "compressDir_tmp\\" + fileNames.at(i);
SetFileAttributesW(reinterpret_cast<LPCWSTR>(fn.utf16()),
FILE_ATTRIBUTE_HIDDEN);
}
}
#endif
const JlCompress::Options options(dateTime, strategy);
QVERIFY(JlCompress::compressDir(zipName, "compressDir_tmp", true, QDir::Hidden, options));
// get the file list and check it
QStringList fileList = JlCompress::getFileList(zipName);
fileList.sort();
expected.sort();
QCOMPARE(fileList, expected);
QFile zipFile(curDir.absoluteFilePath(zipName));
if (!zipFile.open(QIODevice::ReadOnly)) {
QFAIL("Can't read output zip file");
}
QString hash = QCryptographicHash::hash(zipFile.readAll(), QCryptographicHash::Sha256).toHex();
#ifdef Q_OS_WIN
if (!sha256sum_win.isEmpty()) QCOMPARE(hash, sha256sum_win);
#else
if (!sha256sum_unix.isEmpty()) QCOMPARE(hash, sha256sum_unix);
#endif
zipFile.close();
removeTestFiles(fileNames, "compressDir_tmp");
curDir.remove(zipName);
}
void TestJlCompress::extractFile_data()
{
QTest::addColumn<QString>("zipName");
QTest::addColumn<QStringList>("fileNames");
QTest::addColumn<QString>("fileToExtract");
QTest::addColumn<QString>("destName");
QTest::addColumn<QByteArray>("encoding");
QTest::newRow("simple") << "jlextfile.zip" << (
QStringList() << "test0.txt" << "testdir1/test1.txt"
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
<< "testdir2/test2.txt" << "test2.txt" << QByteArray();
QTest::newRow("russian") << "jlextfilerus.zip" << (
QStringList() << "test0.txt" << "testdir1/test1.txt"
<< QString::fromUtf8("testdir2/тест2.txt")
<< "testdir2/subdir/test2sub.txt")
<< QString::fromUtf8("testdir2/тест2.txt")
<< QString::fromUtf8("тест2.txt") << QByteArray("IBM866");
QTest::newRow("extract dir") << "jlextdir.zip" << (
QStringList() << "testdir1/")
<< "testdir1/" << "testdir1/" << QByteArray();
}
void TestJlCompress::extractFile()
{
QFETCH(QString, zipName);
QFETCH(QStringList, fileNames);
QFETCH(QString, fileToExtract);
QFETCH(QString, destName);
QFETCH(QByteArray, encoding);
QDir curDir;
if (!curDir.mkpath("jlext/jlfile")) {
QFAIL("Couldn't mkpath jlext/jlfile");
}
if (!createTestFiles(fileNames)) {
QFAIL("Couldn't create test files");
}
QFile srcFile("tmp/" + fileToExtract);
QFile::Permissions srcPerm = srcFile.permissions();
// Invert the "write other" flag so permissions
// are NOT default any more. Otherwise it's impossible
// to figure out whether the permissions were set correctly
// or JlCompress failed to set them completely,
// thus leaving them at the default setting.
srcPerm ^= QFile::WriteOther;
QVERIFY(srcFile.setPermissions(srcPerm));
if (!createTestArchive(zipName, fileNames,
QTextCodec::codecForName(encoding))) {
QFAIL("Can't create test archive");
}
QuaZip::setDefaultFileNameCodec(encoding);
QVERIFY(!JlCompress::extractFile(zipName, fileToExtract,
"jlext/jlfile/" + destName).isEmpty());
QFileInfo destInfo("jlext/jlfile/" + destName), srcInfo("tmp/" +
fileToExtract);
QCOMPARE(destInfo.size(), srcInfo.size());
QCOMPARE(destInfo.permissions(), srcInfo.permissions());
curDir.remove("jlext/jlfile/" + destName);
// now test the QIODevice* overload
QFile zipFile(zipName);
QVERIFY(zipFile.open(QIODevice::ReadOnly));
QVERIFY(!JlCompress::extractFile(&zipFile, fileToExtract,
"jlext/jlfile/" + destName).isEmpty());
destInfo = QFileInfo("jlext/jlfile/" + destName);
QCOMPARE(destInfo.size(), srcInfo.size());
QCOMPARE(destInfo.permissions(), srcInfo.permissions());
curDir.remove("jlext/jlfile/" + destName);
if (!fileToExtract.endsWith("/")) {
// If we aren't extracting a directory, we need to check
// that extractFile() fails if there is a directory
// with the same name as the file being extracted.
curDir.mkdir("jlext/jlfile/" + destName);
QVERIFY(JlCompress::extractFile(zipName, fileToExtract,
"jlext/jlfile/" + destName).isEmpty());
}
zipFile.close();
// Here we either delete the target dir or the dir created in the
// test above.
curDir.rmpath("jlext/jlfile/" + destName);
removeTestFiles(fileNames);
curDir.remove(zipName);
}
void TestJlCompress::extractFiles_data()
{
QTest::addColumn<QString>("zipName");
QTest::addColumn<QStringList>("fileNames");
QTest::addColumn<QStringList>("filesToExtract");
QTest::newRow("simple") << "jlextfiles.zip" << (
QStringList() << "test0.txt" << "testdir1/test1.txt"
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
<< (QStringList() << "testdir2/test2.txt" << "testdir1/test1.txt");
}
void TestJlCompress::extractFiles()
{
QFETCH(QString, zipName);
QFETCH(QStringList, fileNames);
QFETCH(QStringList, filesToExtract);
QDir curDir;
if (!curDir.mkpath("jlext/jlfiles")) {
QFAIL("Couldn't mkpath jlext/jlfiles");
}
if (!createTestFiles(fileNames)) {
QFAIL("Couldn't create test files");
}
if (!JlCompress::compressDir(zipName, "tmp")) {
QFAIL("Couldn't create test archive");
}
QVERIFY(!JlCompress::extractFiles(zipName, filesToExtract,
"jlext/jlfiles").isEmpty());
foreach (QString fileName, filesToExtract) {
QFileInfo fileInfo("jlext/jlfiles/" + fileName);
QFileInfo extInfo("tmp/" + fileName);
QCOMPARE(fileInfo.size(), extInfo.size());
QCOMPARE(fileInfo.permissions(), extInfo.permissions());
curDir.remove("jlext/jlfiles/" + fileName);
curDir.rmpath(fileInfo.dir().path());
}
// now test the QIODevice* overload
QFile zipFile(zipName);
QVERIFY(zipFile.open(QIODevice::ReadOnly));
QVERIFY(!JlCompress::extractFiles(&zipFile, filesToExtract,
"jlext/jlfiles").isEmpty());
foreach (QString fileName, filesToExtract) {
QFileInfo fileInfo("jlext/jlfiles/" + fileName);
QFileInfo extInfo("tmp/" + fileName);
QCOMPARE(fileInfo.size(), extInfo.size());
QCOMPARE(fileInfo.permissions(), extInfo.permissions());
curDir.remove("jlext/jlfiles/" + fileName);
curDir.rmpath(fileInfo.dir().path());
}
zipFile.close();
curDir.rmpath("jlext/jlfiles");
removeTestFiles(fileNames);
curDir.remove(zipName);
}
void TestJlCompress::extractDir_data()
{
QTest::addColumn<QString>("zipName");
QTest::addColumn<QStringList>("fileNames");
QTest::addColumn<QStringList>("expectedExtracted");
QTest::addColumn<QString>("extDir");
QTest::addColumn<QByteArray>("fileNameCodecName");
QTest::newRow("simple") << "jlextdir.zip"
<< (QStringList() << "test0.txt" << "testdir1/test1.txt"
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
<< (QStringList() << "test0.txt" << "testdir1/test1.txt"
<< "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
<< "tmp/jlext/jldir/"
<< QByteArray();
QTest::newRow("separate dir") << "sepdir.zip"
<< (QStringList() << "laj/" << "laj/lajfile.txt")
<< (QStringList() << "laj/" << "laj/lajfile.txt")
<< "tmp/jlext/jldir/"
<< QByteArray();
QTest::newRow("Zip Slip") << "zipslip.zip"
<< (QStringList() << "test0.txt" << "../zipslip.txt")
<< (QStringList() << "test0.txt")
<< "tmp/jlext/jldir/"
<< QByteArray();
QTest::newRow("Cyrillic") << "cyrillic.zip"
<< (QStringList() << QString::fromUtf8("Ще не вмерла Україна"))
<< (QStringList() << QString::fromUtf8("Ще не вмерла Україна"))
<< "tmp/jlext/jldir/"
<< QByteArray("KOI8-U");
QTest::newRow("Japaneses") << "japanese.zip"
<< (QStringList() << QString::fromUtf8("日本"))
<< (QStringList() << QString::fromUtf8("日本"))
<< "tmp/jlext/jldir/"
<< QByteArray("UTF-8");
#ifdef QUAZIP_EXTRACT_TO_ROOT_TEST
QTest::newRow("Exract to root") << "tmp/quazip-root-test.zip"
<< (QStringList() << "tmp/quazip-root-test/test.txt")
<< (QStringList() << "tmp/quazip-root-test/test.txt")
<< QString()
<< QByteArray("UTF-8");
#endif
}
class TemporarilyChangeToRoot {
QString previousDir;
bool success{false};
public:
~TemporarilyChangeToRoot() {
if (success) {
QDir::setCurrent(previousDir); // Let's HOPE it succeeds.
}
}
void changeToRoot() {
previousDir = QDir::currentPath();
success = QDir::setCurrent("/");
}
inline bool isSuccess() const { return success; }
};
void TestJlCompress::extractDir()
{
QFETCH(QString, zipName);
QFETCH(QStringList, fileNames);
QFETCH(QStringList, expectedExtracted);
QFETCH(QString, extDir);
QFETCH(QByteArray, fileNameCodecName);
TemporarilyChangeToRoot changeToRoot;
if (zipName == "tmp/quazip-root-test.zip") {
changeToRoot.changeToRoot();
if (!changeToRoot.isSuccess()) {
QFAIL("Couldn't change to /");
}
}
QTextCodec *fileNameCodec = NULL;
if (!fileNameCodecName.isEmpty())
fileNameCodec = QTextCodec::codecForName(fileNameCodecName);
QDir curDir;
if (!extDir.isEmpty() && !curDir.mkpath(extDir)) {
QFAIL("Couldn't mkpath extDir");
}
if (!createTestFiles(fileNames)) {
QFAIL("Couldn't create test files");
}
if (!createTestArchive(zipName, fileNames, fileNameCodec)) {
QFAIL("Couldn't create test archive");
}
QStringList extracted;
if (fileNameCodec == NULL)
extracted = JlCompress::extractDir(zipName, extDir);
else // test both overloads here
extracted = JlCompress::extractDir(zipName, fileNameCodec, extDir);
QCOMPARE(extracted.count(), expectedExtracted.count());
const QString dir = extDir;
foreach (QString fileName, expectedExtracted) {
QString fullName = dir + fileName;
QFileInfo fileInfo(fullName);
QFileInfo extInfo("tmp/" + fileName);
if (!fileInfo.isDir())
QCOMPARE(fileInfo.size(), extInfo.size());
QCOMPARE(fileInfo.permissions(), extInfo.permissions());
curDir.remove(fullName);
curDir.rmpath(fileInfo.dir().path());
QString absolutePath = QDir(dir).absoluteFilePath(fileName);
if (fileInfo.isDir() && !absolutePath.endsWith('/'))
absolutePath += '/';
QVERIFY(extracted.contains(absolutePath));
}
// now test the QIODevice* overload
QFile zipFile(zipName);
QVERIFY(zipFile.open(QIODevice::ReadOnly));
if (fileNameCodec == NULL)
extracted = JlCompress::extractDir(&zipFile, extDir);
else // test both overloads here
extracted = JlCompress::extractDir(&zipFile, fileNameCodec, extDir);
QCOMPARE(extracted.count(), expectedExtracted.count());
foreach (QString fileName, expectedExtracted) {
QString fullName = dir + fileName;
QFileInfo fileInfo(fullName);
QFileInfo extInfo("tmp/" + fileName);
if (!fileInfo.isDir())
QCOMPARE(fileInfo.size(), extInfo.size());
QCOMPARE(fileInfo.permissions(), extInfo.permissions());
curDir.remove(fullName);
curDir.rmpath(fileInfo.dir().path());
QString absolutePath = QDir(dir).absoluteFilePath(fileName);
if (fileInfo.isDir() && !absolutePath.endsWith('/'))
absolutePath += '/';
QVERIFY(extracted.contains(absolutePath));
}
zipFile.close();
curDir.rmpath(extDir);
removeTestFiles(fileNames);
//curDir.remove(zipName);
}
void TestJlCompress::zeroPermissions()
{
QuaZip zipCreator("zero.zip");
QVERIFY(zipCreator.open(QuaZip::mdCreate));
QuaZipFile zeroFile(&zipCreator);
QuaZipNewInfo newInfo("zero.txt");
newInfo.externalAttr = 0; // should be zero anyway, but just in case
QVERIFY(zeroFile.open(QIODevice::WriteOnly, newInfo));
zeroFile.close();
zipCreator.close();
QVERIFY(!JlCompress::extractFile("zero.zip", "zero.txt").isEmpty());
QVERIFY(QFile("zero.txt").permissions() != 0);
QDir curDir;
curDir.remove("zero.zip");
curDir.remove("zero.txt");
}
#ifdef QUAZIP_SYMLINK_TEST
void TestJlCompress::symlinkHandling()
{
QStringList fileNames { "file.txt" };
if (!createTestFiles(fileNames)) {
QFAIL("Couldn't create test files");
}
QVERIFY(QFile::link("file.txt", "tmp/link.txt"));
fileNames << "link.txt";
QVERIFY(JlCompress::compressDir("symlink.zip", "tmp"));
QDir curDir;
QVERIFY(curDir.mkpath("extsymlink"));
QVERIFY(!JlCompress::extractDir("symlink.zip", "extsymlink").isEmpty());
QFileInfo linkInfo("extsymlink/link.txt");
QVERIFY(quazip_is_symlink(linkInfo));
removeTestFiles(fileNames, "extsymlink");
removeTestFiles(fileNames, "tmp");
curDir.remove("symlink.zip");
}
#endif
#ifdef QUAZIP_SYMLINK_EXTRACTION_ON_WINDOWS_TEST
void TestJlCompress::symlinkExtractionOnWindows()
{
QuaZip zipWithSymlinks("withSymlinks.zip");
QVERIFY(zipWithSymlinks.open(QuaZip::mdCreate));
QuaZipFile file(&zipWithSymlinks);
QVERIFY(file.open(QIODevice::WriteOnly, QuaZipNewInfo("file.txt")));
file.write("contents");
file.close();
QuaZipNewInfo symlinkInfo("symlink.txt");
symlinkInfo.externalAttr |= 0120000 << 16; // symlink attr
QuaZipFile symlink(&zipWithSymlinks);
QVERIFY(symlink.open(QIODevice::WriteOnly, symlinkInfo));
symlink.write("file.txt"); // link target goes into contents
symlink.close();
zipWithSymlinks.close();
QCOMPARE(zipWithSymlinks.getZipError(), ZIP_OK);
// The best we can do here is to test that extraction works at all,
// because it's hard to say what should be the “correct” result when
// trying to extract symbolic links on Windows.
QVERIFY(!JlCompress::extractDir("withSymlinks.zip", "symlinksOnWindows").isEmpty());
QDir curDir;
curDir.remove("withSymlinks.zip");
removeTestFiles(QStringList() << "file.txt" << "symlink.txt", "symlinksOnWindows");
}
#endif