File indexing completed on 2022-10-04 17:24:44

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
0003 
0004 #include <QDebug>
0005 #include <QTest>
0006 
0007 #include "directoryIterator.h"
0008 #include "test-config.h"
0009 
0010 class DirectoryIteratorTest : public QObject
0011 {
0012     Q_OBJECT
0013     const QString m_tree = QFINDTESTDATA("iterator-tree");
0014 private Q_SLOTS:
0015     void testIterate()
0016     {
0017 #if defined(ITERATOR_TREE_WITH_SYMLINK)
0018         const auto withSymlink = true;
0019 #else
0020         const auto withSymlink = false;
0021 #endif
0022 #if defined(ITERATOR_TREE_WITH_LINK)
0023         const auto withLink = true;
0024 #else
0025         const auto withLink = false;
0026 #endif
0027 
0028         QMap<QByteArray, DirectoryEntry> entries;
0029         for (const auto &entry : DirectoryIterator(m_tree.toUtf8())) {
0030             qDebug() << entry.name;
0031             QVERIFY(!entries.contains(entry.name));
0032             entries.insert(entry.name, entry);
0033         }
0034         qDebug() << entries.keys();
0035 
0036         auto expectedEntries = 3;
0037         if (withSymlink) {
0038             ++expectedEntries;
0039         }
0040         if (withLink) {
0041             ++expectedEntries;
0042         }
0043         QCOMPARE(entries.size(), expectedEntries);
0044 
0045         QVERIFY(entries.contains(QByteArrayLiteral("foo")));
0046 
0047         QVERIFY(entries.contains(QByteArrayLiteral("Con 자백")));
0048         const auto dir = entries[QByteArrayLiteral("Con 자백")];
0049         QVERIFY(dir.isDir);
0050         QVERIFY(!dir.isFile);
0051         QVERIFY(!dir.isSkipable);
0052         // size doesn't matter, it's ignored
0053 
0054         QVERIFY(entries.contains(QByteArrayLiteral("bar")));
0055         const auto file = entries[QByteArrayLiteral("bar")];
0056         QVERIFY(!file.isDir);
0057         QVERIFY(file.isFile);
0058         QVERIFY(!file.isSkipable);
0059 #ifdef Q_OS_WINDOWS
0060         QCOMPARE(file.size, 7682);
0061 #elif defined(Q_OS_FREEBSD)
0062         QCOMPARE(file.size, 1 * S_BLKSIZE);
0063 #else
0064         QCOMPARE(file.size, 16 * S_BLKSIZE);
0065 #endif
0066 
0067         if (withSymlink) {
0068             QVERIFY(entries.contains(QByteArrayLiteral("symlink")));
0069             const auto symlink = entries[QByteArrayLiteral("symlink")];
0070             QVERIFY(!symlink.isDir);
0071             QVERIFY(!symlink.isFile);
0072             QVERIFY(symlink.isSkipable);
0073             // size of skippables doesn't matter
0074         }
0075 
0076         if (withLink) {
0077             QVERIFY(entries.contains(QByteArrayLiteral("link")));
0078             const auto symlink = entries[QByteArrayLiteral("link")];
0079             QVERIFY(!symlink.isDir);
0080             QVERIFY(symlink.isFile);
0081             QVERIFY(!symlink.isSkipable);
0082 #ifdef Q_OS_WINDOWS
0083             QCOMPARE(symlink.size, 7682);
0084 #elif defined(Q_OS_FREEBSD)
0085             QCOMPARE(file.size, 1 * S_BLKSIZE);
0086 #else
0087             QCOMPARE(symlink.size, 16 * S_BLKSIZE);
0088 #endif
0089         }
0090     }
0091 
0092     void testIterateInsideUnicode()
0093     {
0094         QByteArray tree = QFINDTESTDATA("iterator-tree/Con 자백").toUtf8();
0095         QMap<QByteArray, DirectoryEntry> entries;
0096         for (const auto &entry : DirectoryIterator(tree)) {
0097             qDebug() << entry.name;
0098             QVERIFY(!entries.contains(entry.name));
0099             entries.insert(entry.name, entry);
0100         }
0101         qDebug() << entries.keys();
0102     }
0103 
0104     // During development there were some bugs with iterating C:/, make sure this finishes eventually and has some entries.
0105     void testCDrive()
0106     {
0107         QMap<QByteArray, DirectoryEntry> entries;
0108         for (const auto &entry : DirectoryIterator(m_tree.toUtf8())) {
0109             QVERIFY(!entries.contains(entry.name));
0110             entries.insert(entry.name, entry);
0111         }
0112         QVERIFY(entries.size() > 3); // windows, programs, users
0113     }
0114 
0115     void testBadPath()
0116     {
0117         for (const auto &entry : DirectoryIterator(QByteArrayLiteral("/tmp/filelighttest1312312312313123123123"))) {
0118             Q_UNUSED(entry);
0119             QVERIFY(false);
0120         }
0121     }
0122 };
0123 
0124 QTEST_GUILESS_MAIN(DirectoryIteratorTest)
0125 
0126 #include "directoryIteratorTest.moc"