/
tst_qdir.cpp
2384 lines (2017 loc) · 86.8 KB
/
tst_qdir.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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2017 Intel Corporation.
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <qcoreapplication.h>
#include <qdebug.h>
#include <qdir.h>
#include <qfileinfo.h>
#include <qregexp.h>
#include <qstringlist.h>
#if defined(Q_OS_WIN)
#include <QtCore/private/qfsfileengine_p.h>
#include "../../../network-settings.h"
#endif
#if defined(Q_OS_WIN) && !defined(_WIN32_WINNT)
#define _WIN32_WINNT 0x500
#endif
#include "../../../../shared/filesystem.h"
#if defined(Q_OS_UNIX)
# include <unistd.h>
# include <sys/stat.h>
#endif
#if defined(Q_OS_VXWORKS) || defined(Q_OS_WINRT)
#define Q_NO_SYMLINKS
#endif
#ifdef QT_BUILD_INTERNAL
QT_BEGIN_NAMESPACE
extern Q_AUTOTEST_EXPORT QString
qt_normalizePathSegments(const QString &path, bool allowUncPaths, bool *ok = nullptr);
QT_END_NAMESPACE
#endif
static QByteArray msgDoesNotExist(const QString &name)
{
return (QLatin1Char('"') + QDir::toNativeSeparators(name)
+ QLatin1String("\" does not exist.")).toLocal8Bit();
}
class tst_QDir : public QObject
{
Q_OBJECT
public:
enum UncHandling { HandleUnc, IgnoreUnc };
tst_QDir();
private slots:
void init();
void initTestCase();
void cleanupTestCase();
void getSetCheck();
void construction();
void setPath_data();
void setPath();
void entryList_data();
void entryList();
void entryListWithTestFiles_data();
void entryListWithTestFiles();
void entryListTimedSort();
void entryListSimple_data();
void entryListSimple();
void entryListWithSymLinks();
void mkdirRmdir_data();
void mkdirRmdir();
void mkdirOnSymlink();
void makedirReturnCode();
void removeRecursively_data();
void removeRecursively();
void removeRecursivelyFailure();
void removeRecursivelySymlink();
void exists_data();
void exists();
void isRelativePath_data();
void isRelativePath();
void canonicalPath_data();
void canonicalPath();
void current_data();
void current();
void cd_data();
void cd();
void setNameFilters_data();
void setNameFilters();
void cleanPath_data();
void cleanPath();
#ifdef QT_BUILD_INTERNAL
void normalizePathSegments_data();
void normalizePathSegments();
#endif
void compare();
void QDir_default();
void filePath_data();
void filePath();
void absoluteFilePath_data();
void absoluteFilePath();
void absolutePath_data();
void absolutePath();
void relativeFilePath_data();
void relativeFilePath();
void remove();
void rename();
void exists2_data();
void exists2();
void dirName_data();
void dirName();
void operator_eq();
void dotAndDotDot();
void homePath();
void tempPath();
void rootPath();
void nativeSeparators();
void searchPaths();
void searchPaths_data();
void entryListWithSearchPaths();
void longFileName_data();
void longFileName();
void updateFileLists();
void detachingOperations();
void testCaching();
void isRoot_data();
void isRoot();
#ifndef QT_NO_REGEXP
void match_data();
void match();
#endif
void drives();
void arrayOperator();
void equalityOperator_data();
void equalityOperator();
void isRelative_data();
void isRelative();
void isReadable();
void cdNonreadable();
void cdBelowRoot_data();
void cdBelowRoot();
void emptyDir();
void nonEmptyDir();
private:
#ifdef BUILTIN_TESTDATA
QString m_dataPath;
QSharedPointer<QTemporaryDir> m_dataDir;
#else
const QString m_dataPath;
#endif
};
Q_DECLARE_METATYPE(tst_QDir::UncHandling)
tst_QDir::tst_QDir()
#if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED)
: m_dataPath(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))
#elif !defined(BUILTIN_TESTDATA)
: m_dataPath(QFileInfo(QFINDTESTDATA("testData")).absolutePath())
#endif
{
#if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED)
QString resourceSourcePath = QStringLiteral(":/android_testdata/");
QDirIterator it(resourceSourcePath, QDirIterator::Subdirectories);
while (it.hasNext()) {
it.next();
QFileInfo fileInfo = it.fileInfo();
if (!fileInfo.isDir()) {
QString destination = m_dataPath + QLatin1Char('/') + fileInfo.filePath().mid(resourceSourcePath.length());
QFileInfo destinationFileInfo(destination);
if (!destinationFileInfo.exists()) {
QDir().mkpath(destinationFileInfo.path());
if (!QFile::copy(fileInfo.filePath(), destination))
qWarning("Failed to copy %s", qPrintable(fileInfo.filePath()));
}
}
}
if (!QDir::setCurrent(m_dataPath))
qWarning("Couldn't set current path to %s", qPrintable(m_dataPath));
#endif
}
void tst_QDir::init()
{
// Some tests want to use "." as relative path to data.
QVERIFY2(QDir::setCurrent(m_dataPath), qPrintable("Could not chdir to " + m_dataPath));
}
void tst_QDir::initTestCase()
{
#ifdef BUILTIN_TESTDATA
m_dataDir = QEXTRACTTESTDATA("/");
QVERIFY2(!m_dataDir.isNull(), qPrintable("Did not find testdata. Is this builtin?"));
m_dataPath = m_dataDir->path();
#endif
QVERIFY2(!m_dataPath.isEmpty(), "test data not found");
}
void tst_QDir::cleanupTestCase()
{
#ifdef BUILTIN_TESTDATA
// We need to reset the current directory outside of QTemporaryDir for successful deletion
QDir::setCurrent(QCoreApplication::applicationDirPath());
#else
QDir(QDir::currentPath() + "/tmpdir").removeRecursively();
#endif
}
// Testing get/set functions
void tst_QDir::getSetCheck()
{
QDir obj1;
// Filters QDir::filter()
// void QDir::setFilter(Filters)
obj1.setFilter(QDir::Filters(QDir::Dirs));
QCOMPARE(QDir::Filters(QDir::Dirs), obj1.filter());
obj1.setFilter(QDir::Filters(QDir::Dirs | QDir::Files));
QCOMPARE(QDir::Filters(QDir::Dirs | QDir::Files), obj1.filter());
obj1.setFilter(QDir::Filters(QDir::NoFilter));
QCOMPARE(QDir::Filters(QDir::NoFilter), obj1.filter());
// SortFlags QDir::sorting()
// void QDir::setSorting(SortFlags)
obj1.setSorting(QDir::SortFlags(QDir::Name));
QCOMPARE(QDir::SortFlags(QDir::Name), obj1.sorting());
obj1.setSorting(QDir::SortFlags(QDir::Name | QDir::IgnoreCase));
QCOMPARE(QDir::SortFlags(QDir::Name | QDir::IgnoreCase), obj1.sorting());
obj1.setSorting(QDir::SortFlags(QDir::NoSort));
QCOMPARE(QDir::SortFlags(QDir::NoSort), obj1.sorting());
}
void tst_QDir::construction()
{
QFileInfo myFileInfo("/machine/share/dir1/file1");
QDir myDir(myFileInfo.absoluteDir()); // this asserted
QCOMPARE(myFileInfo.absoluteDir().absolutePath(), myDir.absolutePath());
}
void tst_QDir::setPath_data()
{
QTest::addColumn<QString>("dir1");
QTest::addColumn<QString>("dir2");
QTest::newRow("data0") << QString(".") << QString("..");
#if defined(Q_OS_WIN)
QTest::newRow("data1") << QString("c:/") << QDir::currentPath();
#endif
}
void tst_QDir::setPath()
{
QFETCH(QString, dir1);
QFETCH(QString, dir2);
QDir shared;
QDir qDir1(dir1);
QStringList entries1 = qDir1.entryList();
shared.setPath(dir1);
QCOMPARE(shared.entryList(), entries1);
QDir qDir2(dir2);
QStringList entries2 = qDir2.entryList();
shared.setPath(dir2);
QCOMPARE(shared.entryList(), entries2);
}
void tst_QDir::mkdirRmdir_data()
{
QTest::addColumn<QString>("path");
QTest::addColumn<bool>("recurse");
QStringList dirs;
dirs << "testdir/one"
<< "testdir/two/three/four"
<< "testdir/../testdir/three";
QTest::newRow("plain") << QDir::currentPath() + "/" + dirs.at(0) << false;
QTest::newRow("recursive") << QDir::currentPath() + "/" + dirs.at(1) << true;
QTest::newRow("with-..") << QDir::currentPath() + "/" + dirs.at(2) << false;
QTest::newRow("relative-plain") << dirs.at(0) << false;
QTest::newRow("relative-recursive") << dirs.at(1) << true;
QTest::newRow("relative-with-..") << dirs.at(2) << false;
// Ensure that none of these directories already exist
for (int i = 0; i < dirs.count(); ++i)
QVERIFY(!QFile::exists(dirs.at(i)));
}
void tst_QDir::mkdirRmdir()
{
QFETCH(QString, path);
QFETCH(bool, recurse);
QDir dir;
dir.rmdir(path);
if (recurse)
QVERIFY(dir.mkpath(path));
else
QVERIFY(dir.mkdir(path));
//make sure it really exists (ie that mkdir returns the right value)
QFileInfo fi(path);
QVERIFY2(fi.exists() && fi.isDir(), msgDoesNotExist(path).constData());
if (recurse)
QVERIFY(dir.rmpath(path));
else
QVERIFY(dir.rmdir(path));
//make sure it really doesn't exist (ie that rmdir returns the right value)
fi.refresh();
QVERIFY(!fi.exists());
}
void tst_QDir::mkdirOnSymlink()
{
#if !defined(Q_OS_UNIX) || defined(Q_NO_SYMLINKS)
QSKIP("Test only valid on an OS that supports symlinks");
#else
// Create the structure:
// .
// ├── symlink -> two/three
// └── two
// └── three
// so when we mkdir("symlink/../four/five"), we end up with:
// .
// ├── symlink -> two/three
// └── two
// ├── four
// │ └── five
// └── three
QDir dir;
struct Clean {
QDir &dir;
Clean(QDir &dir) : dir(dir) {}
~Clean() { doClean(); }
void doClean() {
dir.rmpath("two/three");
dir.rmpath("two/four/five");
// in case the test fails, don't leave junk behind
dir.rmpath("four/five");
QFile::remove("symlink");
}
};
Clean clean(dir);
clean.doClean();
// create our structure:
dir.mkpath("two/three");
::symlink("two/three", "symlink");
// try it:
QString path = "symlink/../four/five";
QVERIFY(dir.mkpath(path));
QFileInfo fi(path);
QVERIFY2(fi.exists() && fi.isDir(), msgDoesNotExist(path).constData());
path = "two/four/five";
fi.setFile(path);
QVERIFY2(fi.exists() && fi.isDir(), msgDoesNotExist(path).constData());
#endif
}
void tst_QDir::makedirReturnCode()
{
QString dirName = QString::fromLatin1("makedirReturnCode");
QFile f(QDir::current().filePath(dirName));
// cleanup a previous run.
f.remove();
QDir::current().rmdir(dirName);
QDir dir(dirName);
QVERIFY(!dir.exists());
QVERIFY(QDir::current().mkdir(dirName));
QVERIFY(!QDir::current().mkdir(dirName)); // calling mkdir on an existing dir will fail.
QVERIFY(QDir::current().mkpath(dirName)); // calling mkpath on an existing dir will pass
// Remove the directory and create a file with the same path
QDir::current().rmdir(dirName);
QVERIFY(!f.exists());
f.open(QIODevice::WriteOnly);
f.write("test");
f.close();
QVERIFY2(f.exists(), msgDoesNotExist(f.fileName()).constData());
QVERIFY(!QDir::current().mkdir(dirName)); // calling mkdir on an existing file will fail.
QVERIFY(!QDir::current().mkpath(dirName)); // calling mkpath on an existing file will fail.
f.remove();
}
void tst_QDir::removeRecursively_data()
{
QTest::addColumn<QString>("path");
// Create dirs and files
const QString tmpdir = QDir::currentPath() + "/tmpdir/";
QStringList dirs;
dirs << tmpdir + "empty"
<< tmpdir + "one"
<< tmpdir + "two/three"
<< "relative";
QDir dir;
for (int i = 0; i < dirs.count(); ++i)
dir.mkpath(dirs.at(i));
QStringList files;
files << tmpdir + "one/file";
files << tmpdir + "two/three/file";
for (int i = 0; i < files.count(); ++i) {
QFile file(files.at(i));
QVERIFY(file.open(QIODevice::WriteOnly));
file.write("Hello");
}
QTest::newRow("empty") << tmpdir + "empty";
QTest::newRow("one") << tmpdir + "one";
QTest::newRow("two") << tmpdir + "two";
QTest::newRow("does not exist") << tmpdir + "doesnotexist";
QTest::newRow("relative") << "relative";
}
void tst_QDir::removeRecursively()
{
QFETCH(QString, path);
QDir dir(path);
QVERIFY(dir.removeRecursively());
//make sure it really doesn't exist (ie that remove worked)
QVERIFY(!dir.exists());
}
void tst_QDir::removeRecursivelyFailure()
{
#ifdef Q_OS_UNIX
if (::getuid() == 0)
QSKIP("Running this test as root doesn't make sense");
#endif
const QString tmpdir = QDir::currentPath() + "/tmpdir/";
const QString path = tmpdir + "undeletable";
QDir().mkpath(path);
// Need a file in there, otherwise rmdir works even w/o permissions
QFile file(path + "/file");
QVERIFY(file.open(QIODevice::WriteOnly));
file.write("Hello");
file.close();
#ifdef Q_OS_UNIX
QFile dirAsFile(path); // yay, I have to use QFile to change a dir's permissions...
QVERIFY(dirAsFile.setPermissions(QFile::Permissions(0))); // no permissions
QVERIFY(!QDir().rmdir(path));
QDir dir(path);
QVERIFY(!dir.removeRecursively()); // didn't work
QVERIFY2(dir.exists(), msgDoesNotExist(dir.absolutePath()).constData()); // still exists
QVERIFY(dirAsFile.setPermissions(QFile::Permissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner)));
QVERIFY(dir.removeRecursively());
QVERIFY(!dir.exists());
#else // Q_OS_UNIX
QVERIFY(file.setPermissions(QFile::ReadOwner));
QVERIFY(!QDir().rmdir(path));
QDir dir(path);
QVERIFY(dir.removeRecursively());
QVERIFY(!dir.exists());
#endif // !Q_OS_UNIX
}
void tst_QDir::removeRecursivelySymlink()
{
#ifndef Q_NO_SYMLINKS
const QString tmpdir = QDir::currentPath() + "/tmpdir/";
QDir().mkpath(tmpdir);
QDir currentDir;
currentDir.mkdir("myDir");
QFile("testfile").open(QIODevice::WriteOnly);
const QString link = tmpdir + "linkToDir.lnk";
const QString linkToFile = tmpdir + "linkToFile.lnk";
#ifndef Q_NO_SYMLINKS_TO_DIRS
QVERIFY(QFile::link("../myDir", link));
QVERIFY(QFile::link("../testfile", linkToFile));
#endif
QDir dir(tmpdir);
QVERIFY(dir.removeRecursively());
QVERIFY(QDir("myDir").exists()); // it didn't follow the symlink, good.
QVERIFY(QFile::exists("testfile"));
currentDir.rmdir("myDir");
QFile::remove("testfile");
#endif
}
void tst_QDir::exists_data()
{
QTest::addColumn<QString>("path");
QTest::addColumn<bool>("expected");
QTest::newRow("data0") << QDir::currentPath() << true;
QTest::newRow("data0.1") << QDir::currentPath() + "/" << true;
QTest::newRow("data1") << QString("/I/Do_not_expect_this_path_to_exist/") << false;
QTest::newRow("resource0") << QString(":/tst_qdir/") << true;
QTest::newRow("resource1") << QString(":/I/Do_not_expect_this_resource_to_exist/") << false;
QTest::newRow("simple dir") << (m_dataPath + "/resources") << true;
QTest::newRow("simple dir with slash") << (m_dataPath + "/resources/") << true;
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
const QString uncRoot = QStringLiteral("//") + QtNetworkSettings::winServerName();
QTest::newRow("unc 1") << uncRoot << true;
QTest::newRow("unc 2") << uncRoot + QLatin1Char('/') << true;
QTest::newRow("unc 3") << uncRoot + "/testshare" << true;
QTest::newRow("unc 4") << uncRoot + "/testshare/" << true;
QTest::newRow("unc 5") << uncRoot + "/testshare/tmp" << true;
QTest::newRow("unc 6") << uncRoot + "/testshare/tmp/" << true;
QTest::newRow("unc 7") << uncRoot + "/testshare/adirthatshouldnotexist" << false;
QTest::newRow("unc 8") << uncRoot + "/asharethatshouldnotexist" << false;
QTest::newRow("unc 9") << "//ahostthatshouldnotexist" << false;
#endif
#if (defined(Q_OS_WIN) && !defined(Q_OS_WINRT))
QTest::newRow("This drive should exist") << "C:/" << true;
// find a non-existing drive and check if it does not exist
#ifdef QT_BUILD_INTERNAL
QFileInfoList drives = QFSFileEngine::drives();
QStringList driveLetters;
for (int i = 0; i < drives.count(); ++i) {
driveLetters+=drives.at(i).absoluteFilePath();
}
char drive = 'Z';
QString driv;
do {
driv = drive + QLatin1String(":/");
if (!driveLetters.contains(driv)) break;
--drive;
} while (drive >= 'A');
if (drive >= 'A') {
QTest::newRow("This drive should not exist") << driv << false;
}
#endif
#endif
}
void tst_QDir::exists()
{
QFETCH(QString, path);
QFETCH(bool, expected);
QDir dir(path);
if (expected)
QVERIFY2(dir.exists(), msgDoesNotExist(path).constData());
else
QVERIFY(!dir.exists());
}
void tst_QDir::isRelativePath_data()
{
QTest::addColumn<QString>("path");
QTest::addColumn<bool>("relative");
QTest::newRow("data0") << "../somedir" << true;
#if defined(Q_OS_WIN)
QTest::newRow("data1") << "C:/sOmedir" << false;
#endif
QTest::newRow("data2") << "somedir" << true;
QTest::newRow("data3") << "/somedir" << false;
QTest::newRow("resource0") << ":/prefix" << false;
QTest::newRow("resource1") << ":/prefix/foo.bar" << false;
}
void tst_QDir::isRelativePath()
{
QFETCH(QString, path);
QFETCH(bool, relative);
QCOMPARE(QDir::isRelativePath(path),relative);
}
void tst_QDir::QDir_default()
{
//default constructor QDir();
QDir dir; // according to documentation should be currentDirPath
QCOMPARE(dir.absolutePath(), QDir::currentPath());
}
void tst_QDir::compare()
{
// operator==
// Not using QCOMPARE to test result of QDir::operator==
QDir dir;
dir.makeAbsolute();
QVERIFY(dir == QDir::currentPath());
QCOMPARE(QDir(), QDir(QDir::currentPath()));
QVERIFY(QDir("../") == QDir(QDir::currentPath() + "/.."));
}
static QStringList filterLinks(const QStringList &list)
{
#ifndef Q_NO_SYMLINKS
return list;
#else
QStringList result;
foreach (QString str, list) {
if (!str.endsWith(QLatin1String(".lnk")))
result.append(str);
}
return result;
#endif
}
void tst_QDir::entryList_data()
{
QTest::addColumn<QString>("dirName"); // relative from current path or abs
QTest::addColumn<QStringList>("nameFilters");
QTest::addColumn<int>("filterspec");
QTest::addColumn<int>("sortspec");
QTest::addColumn<QStringList>("expected");
QTest::newRow("spaces1") << (m_dataPath + "/testdir/spaces") << QStringList("*. bar")
<< (int)(QDir::NoFilter) << (int)(QDir::NoSort)
<< QStringList("foo. bar"); // notice how spaces5 works
QTest::newRow("spaces2") << (m_dataPath + "/testdir/spaces") << QStringList("*.bar")
<< (int)(QDir::NoFilter) << (int)(QDir::NoSort)
<< QStringList("foo.bar");
QTest::newRow("spaces3") << (m_dataPath + "/testdir/spaces") << QStringList("foo.*")
<< (int)(QDir::NoFilter) << (int)(QDir::NoSort)
<< QString("foo. bar,foo.bar").split(',');
QTest::newRow("files1") << (m_dataPath + "/testdir/dir") << QString("*r.cpp *.pro").split(" ")
<< (int)(QDir::NoFilter) << (int)(QDir::NoSort)
<< QString("qdir.pro,qrc_qdir.cpp,tst_qdir.cpp").split(',');
QTest::newRow("testdir1") << (m_dataPath + "/testdir") << QStringList()
<< (int)(QDir::AllDirs) << (int)(QDir::NoSort)
<< QString(".,..,dir,spaces").split(',');
QTest::newRow("resources1") << QString(":/tst_qdir/resources/entryList") << QStringList("*.data")
<< (int)(QDir::NoFilter) << (int)(QDir::NoSort)
<< QString("file1.data,file2.data,file3.data").split(',');
QTest::newRow("resources2") << QString(":/tst_qdir/resources/entryList") << QStringList("*.data")
<< (int)(QDir::Files) << (int)(QDir::NoSort)
<< QString("file1.data,file2.data,file3.data").split(',');
}
void tst_QDir::entryList()
{
QFETCH(QString, dirName);
QFETCH(QStringList, nameFilters);
QFETCH(int, filterspec);
QFETCH(int, sortspec);
QFETCH(QStringList, expected);
QDir dir(dirName);
QVERIFY2(dir.exists(), msgDoesNotExist(dirName).constData());
QStringList actual = dir.entryList(nameFilters, (QDir::Filters)filterspec,
(QDir::SortFlags)sortspec);
QCOMPARE(actual, expected);
}
void tst_QDir::entryListWithTestFiles_data()
{
QTest::addColumn<QString>("dirName"); // relative from current path or abs
QTest::addColumn<QStringList>("nameFilters");
QTest::addColumn<int>("filterspec");
QTest::addColumn<int>("sortspec");
QTest::addColumn<QStringList>("expected");
QTest::newRow("nofilter") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::NoFilter) << int(QDir::Name)
<< filterLinks(QString(".,..,directory,file,linktodirectory.lnk,linktofile.lnk,writable").split(','));
QTest::newRow("QDir::AllEntries") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::AllEntries) << int(QDir::Name)
<< filterLinks(QString(".,..,directory,file,linktodirectory.lnk,linktofile.lnk,writable").split(','));
QTest::newRow("QDir::Files") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::Files) << int(QDir::Name)
<< filterLinks(QString("file,linktofile.lnk,writable").split(','));
QTest::newRow("QDir::Dirs") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::Dirs) << int(QDir::Name)
<< filterLinks(QString(".,..,directory,linktodirectory.lnk").split(','));
QTest::newRow("QDir::Dirs | QDir::NoDotAndDotDot") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::Dirs | QDir::NoDotAndDotDot) << int(QDir::Name)
<< filterLinks(QString("directory,linktodirectory.lnk").split(','));
QTest::newRow("QDir::AllDirs") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::AllDirs) << int(QDir::Name)
<< filterLinks(QString(".,..,directory,linktodirectory.lnk").split(','));
QTest::newRow("QDir::AllDirs | QDir::Dirs") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::AllDirs | QDir::Dirs) << int(QDir::Name)
<< filterLinks(QString(".,..,directory,linktodirectory.lnk").split(','));
QTest::newRow("QDir::AllDirs | QDir::Files") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::AllDirs | QDir::Files) << int(QDir::Name)
<< filterLinks(QString(".,..,directory,file,linktodirectory.lnk,linktofile.lnk,writable").split(','));
QTest::newRow("QDir::AllEntries | QDir::NoSymLinks") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::AllEntries | QDir::NoSymLinks) << int(QDir::Name)
<< filterLinks(QString(".,..,directory,file,writable").split(','));
QTest::newRow("QDir::AllEntries | QDir::NoSymLinks | QDir::NoDotAndDotDot") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::AllEntries | QDir::NoSymLinks | QDir::NoDotAndDotDot) << int(QDir::Name)
<< filterLinks(QString("directory,file,writable").split(','));
QTest::newRow("QDir::Files | QDir::NoSymLinks") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::Files | QDir::NoSymLinks) << int(QDir::Name)
<< filterLinks(QString("file,writable").split(','));
QTest::newRow("QDir::Dirs | QDir::NoSymLinks") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::Dirs | QDir::NoSymLinks) << int(QDir::Name)
<< filterLinks(QString(".,..,directory").split(','));
QTest::newRow("QDir::Drives | QDir::Files | QDir::NoDotAndDotDot") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::Drives | QDir::Files | QDir::NoDotAndDotDot) << int(QDir::Name)
<< filterLinks(QString("file,linktofile.lnk,writable").split(','));
QTest::newRow("QDir::System") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::System) << int(QDir::Name)
<< filterLinks(QStringList("brokenlink.lnk"));
QTest::newRow("QDir::Hidden") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::Hidden) << int(QDir::Name)
<< QStringList();
QTest::newRow("QDir::System | QDir::Hidden") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::System | QDir::Hidden) << int(QDir::Name)
<< filterLinks(QStringList("brokenlink.lnk"));
QTest::newRow("QDir::AllDirs | QDir::NoSymLinks") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::AllDirs | QDir::NoSymLinks) << int(QDir::Name)
<< filterLinks(QString(".,..,directory").split(','));
QTest::newRow("QDir::AllEntries | QDir::Hidden | QDir::System") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::AllEntries | QDir::Hidden | QDir::System) << int(QDir::Name)
<< filterLinks(QString(".,..,brokenlink.lnk,directory,file,linktodirectory.lnk,linktofile.lnk,writable").split(','));
QTest::newRow("QDir::AllEntries | QDir::Readable") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::AllEntries | QDir::Readable) << int(QDir::Name)
<< filterLinks(QString(".,..,directory,file,linktodirectory.lnk,linktofile.lnk,writable").split(','));
QTest::newRow("QDir::AllEntries | QDir::Writable") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::AllEntries | QDir::Writable) << int(QDir::Name)
<< filterLinks(QString(".,..,directory,linktodirectory.lnk,writable").split(','));
QTest::newRow("QDir::Files | QDir::Readable") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::Files | QDir::Readable) << int(QDir::Name)
<< filterLinks(QString("file,linktofile.lnk,writable").split(','));
QTest::newRow("QDir::Dirs | QDir::Readable") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::Dirs | QDir::Readable) << int(QDir::Name)
<< filterLinks(QString(".,..,directory,linktodirectory.lnk").split(','));
QTest::newRow("Namefilters b*") << (m_dataPath + "/entrylist/") << QStringList("d*")
<< int(QDir::NoFilter) << int(QDir::Name)
<< filterLinks(QString("directory").split(','));
QTest::newRow("Namefilters f*") << (m_dataPath + "/entrylist/") << QStringList("f*")
<< int(QDir::NoFilter) << int(QDir::Name)
<< filterLinks(QString("file").split(','));
QTest::newRow("Namefilters link*") << (m_dataPath + "/entrylist/") << QStringList("link*")
<< int(QDir::NoFilter) << int(QDir::Name)
<< filterLinks(QString("linktodirectory.lnk,linktofile.lnk").split(','));
QTest::newRow("Namefilters *to*") << (m_dataPath + "/entrylist/") << QStringList("*to*")
<< int(QDir::NoFilter) << int(QDir::Name)
<< filterLinks(QString("directory,linktodirectory.lnk,linktofile.lnk").split(','));
QTest::newRow("Sorting QDir::Name") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::NoFilter) << int(QDir::Name)
<< filterLinks(QString(".,..,directory,file,linktodirectory.lnk,linktofile.lnk,writable").split(','));
QTest::newRow("Sorting QDir::Name | QDir::Reversed") << (m_dataPath + "/entrylist/") << QStringList("*")
<< int(QDir::NoFilter) << int(QDir::Name | QDir::Reversed)
<< filterLinks(QString("writable,linktofile.lnk,linktodirectory.lnk,file,directory,..,.").split(','));
QTest::newRow("Sorting QDir::Type") << (m_dataPath + "/types/") << QStringList("*")
<< int(QDir::NoFilter) << int(QDir::Type)
<< QString(".,..,a,b,c,d,e,f,a.a,b.a,c.a,d.a,e.a,f.a,a.b,b.b,c.b,d.b,e.b,f.b,a.c,b.c,c.c,d.c,e.c,f.c").split(',');
QTest::newRow("Sorting QDir::Type | QDir::Reversed") << (m_dataPath + "/types/") << QStringList("*")
<< int(QDir::NoFilter) << int(QDir::Type | QDir::Reversed)
<< QString("f.c,e.c,d.c,c.c,b.c,a.c,f.b,e.b,d.b,c.b,b.b,a.b,f.a,e.a,d.a,c.a,b.a,a.a,f,e,d,c,b,a,..,.").split(',');
QTest::newRow("Sorting QDir::Type | QDir::DirsLast") << (m_dataPath + "/types/") << QStringList("*")
<< int(QDir::NoFilter) << int(QDir::Type | QDir::DirsLast)
<< QString("a,b,c,a.a,b.a,c.a,a.b,b.b,c.b,a.c,b.c,c.c,.,..,d,e,f,d.a,e.a,f.a,d.b,e.b,f.b,d.c,e.c,f.c").split(',');
QTest::newRow("Sorting QDir::Type | QDir::DirsFirst") << (m_dataPath + "/types/") << QStringList("*")
<< int(QDir::NoFilter) << int(QDir::Type | QDir::DirsFirst)
<< QString(".,..,d,e,f,d.a,e.a,f.a,d.b,e.b,f.b,d.c,e.c,f.c,a,b,c,a.a,b.a,c.a,a.b,b.b,c.b,a.c,b.c,c.c").split(',');
QTest::newRow("Sorting QDir::Size") << (m_dataPath + "/types/") << QStringList("*")
<< int(QDir::AllEntries|QDir::NoDotAndDotDot) << int(QDir::Size | QDir::DirsFirst)
<< QString("d,d.a,d.b,d.c,e,e.a,e.b,e.c,f,f.a,f.b,f.c,c.a,c.b,c.c,b.a,b.c,b.b,a.c,a.b,a.a,a,b,c").split(',');
QTest::newRow("Sorting QDir::Size | QDir::Reversed") << (m_dataPath + "/types/") << QStringList("*")
<< int(QDir::AllEntries|QDir::NoDotAndDotDot) << int(QDir::Size | QDir::Reversed | QDir::DirsLast)
<< QString("c,b,a,a.a,a.b,a.c,b.b,b.c,b.a,c.c,c.b,c.a,f.c,f.b,f.a,f,e.c,e.b,e.a,e,d.c,d.b,d.a,d").split(',');
}
void tst_QDir::entryListWithTestFiles()
{
QFETCH(QString, dirName);
QFETCH(QStringList, nameFilters);
QFETCH(int, filterspec);
QFETCH(int, sortspec);
QFETCH(QStringList, expected);
QStringList testFiles;
QString entrylistPath = (m_dataPath + "/entrylist/");
{
const QString writableFileName = entrylistPath + "writable";
QFile writableFile(writableFileName);
testFiles.append(writableFileName);
QVERIFY2(writableFile.open(QIODevice::ReadWrite),
qPrintable(writableFile.errorString()));
}
{
QFile readOnlyFile(entrylistPath + "file");
QVERIFY2(readOnlyFile.setPermissions(QFile::ReadOwner | QFile::ReadUser),
qPrintable(readOnlyFile.errorString()));
}
#ifndef Q_NO_SYMLINKS
#if defined(Q_OS_WIN)
// ### Sadly, this is a platform difference right now.
// Note we are using capital L in entryList on one side here, to test case-insensitivity
const QVector<QPair<QString, QString> > symLinks =
{
{m_dataPath + "/entryList/file", entrylistPath + "linktofile.lnk"},
{m_dataPath + "/entryList/directory", entrylistPath + "linktodirectory.lnk"},
{m_dataPath + "/entryList/nothing", entrylistPath + "brokenlink.lnk"}
};
#else
const QVector<QPair<QString, QString> > symLinks =
{
{"file", entrylistPath + "linktofile.lnk"},
{"directory", entrylistPath + "linktodirectory.lnk"},
{"nothing", entrylistPath + "brokenlink.lnk"}
};
#endif
for (const auto &symLink : symLinks) {
QVERIFY2(QFile::link(symLink.first, symLink.second),
qPrintable(symLink.first + "->" + symLink.second));
testFiles.append(symLink.second);
}
#endif //Q_NO_SYMLINKS
QDir dir(dirName);
QVERIFY2(dir.exists(), msgDoesNotExist(dirName).constData());
QStringList actual = dir.entryList(nameFilters, (QDir::Filters)filterspec,
(QDir::SortFlags)sortspec);
bool doContentCheck = true;
#if defined(Q_OS_UNIX)
if (qstrcmp(QTest::currentDataTag(), "QDir::AllEntries | QDir::Writable") == 0) {
// for root, everything is writeable
if (::getuid() == 0)
doContentCheck = false;
}
#endif
for (int i = testFiles.size() - 1; i >= 0; --i)
QVERIFY2(QFile::remove(testFiles.at(i)), qPrintable(testFiles.at(i)));
if (doContentCheck)
QCOMPARE(actual, expected);
}
void tst_QDir::entryListTimedSort()
{
#if QT_CONFIG(process)
const QString touchBinary = "/bin/touch";
if (!QFile::exists(touchBinary))
QSKIP("/bin/touch not found");
const QString entrylistPath = m_dataPath + "/entrylist/";
QTemporaryFile aFile(entrylistPath + "A-XXXXXX.qws");
QTemporaryFile bFile(entrylistPath + "B-XXXXXX.qws");
QVERIFY2(aFile.open(), qPrintable(aFile.errorString()));
QVERIFY2(bFile.open(), qPrintable(bFile.errorString()));
{
QProcess p;
p.start(touchBinary, QStringList() << "-t" << "201306021513" << aFile.fileName());
QVERIFY(p.waitForFinished(1000));
}
{
QProcess p;
p.start(touchBinary, QStringList() << "-t" << "201504131513" << bFile.fileName());
QVERIFY(p.waitForFinished(1000));
}
QStringList actual = QDir(entrylistPath).entryList(QStringList() << "*.qws", QDir::NoFilter,
QDir::Time);
QFileInfo aFileInfo(aFile);
QFileInfo bFileInfo(bFile);
QVERIFY(bFileInfo.lastModified().msecsTo(aFileInfo.lastModified()) < 0);
QCOMPARE(actual.size(), 2);
QCOMPARE(actual.first(), bFileInfo.fileName());
QCOMPARE(actual.last(), aFileInfo.fileName());
#else
QSKIP("This test requires QProcess support.");
#endif // QT_CONFIG(process)
}
void tst_QDir::entryListSimple_data()
{
QTest::addColumn<QString>("dirName");
QTest::addColumn<int>("countMin");
QTest::newRow("data2") << "do_not_expect_this_path_to_exist/" << 0;
QTest::newRow("simple dir") << (m_dataPath + "/resources") << 2;
QTest::newRow("simple dir with slash") << (m_dataPath + "/resources/") << 2;
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
const QString uncRoot = QStringLiteral("//") + QtNetworkSettings::winServerName();
QTest::newRow("unc 1") << uncRoot << 2;
QTest::newRow("unc 2") << uncRoot + QLatin1Char('/') << 2;
QTest::newRow("unc 3") << uncRoot + "/testshare" << 2;
QTest::newRow("unc 4") << uncRoot + "/testshare/" << 2;
QTest::newRow("unc 5") << uncRoot + "/testshare/tmp" << 2;
QTest::newRow("unc 6") << uncRoot + "/testshare/tmp/" << 2;
QTest::newRow("unc 7") << uncRoot + "/testshare/adirthatshouldnotexist" << 0;
QTest::newRow("unc 8") << uncRoot + "/asharethatshouldnotexist" << 0;
QTest::newRow("unc 9") << "//ahostthatshouldnotexist" << 0;
#endif
}
static QByteArray msgEntryListFailed(int actual, int expectedMin, const QString &name)
{
return QByteArray::number(actual) + " < " + QByteArray::number(expectedMin) + " in \""
+ QFile::encodeName(QDir::toNativeSeparators(name)) + '"';
}
void tst_QDir::entryListSimple()
{
QFETCH(QString, dirName);