File indexing completed on 2024-04-28 04:57:44

0001 /*  This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "testkioarchive.h"
0008 
0009 #include <KIO/CopyJob>
0010 #include <KIO/DeleteJob>
0011 #include <KIO/ListJob>
0012 #include <KIO/StatJob>
0013 #include <KTar>
0014 
0015 #include <QDebug>
0016 #include <QDir>
0017 #include <QFileInfo>
0018 #include <QStandardPaths>
0019 #include <QTest>
0020 
0021 QTEST_MAIN(TestKioArchive)
0022 static const char s_tarFileName[] = "karchivetest.tar";
0023 
0024 static void writeTestFilesToArchive(KArchive *archive)
0025 {
0026     bool ok;
0027     ok = archive->writeFile("empty", QByteArray(), 0100644, "weis", "users");
0028     QVERIFY(ok);
0029     ok = archive->writeFile("test1", QByteArrayLiteral("Hallo"), 0100644, "weis", "users");
0030     QVERIFY(ok);
0031     ok = archive->writeFile("mydir/subfile", QByteArrayLiteral("Bonjour"), 0100644, "dfaure", "users");
0032     QVERIFY(ok);
0033     ok = archive->writeSymLink("mydir/symlink", "subfile", "dfaure", "users");
0034     QVERIFY(ok);
0035 }
0036 
0037 void TestKioArchive::initTestCase()
0038 {
0039     QStandardPaths::setTestModeEnabled(true);
0040 
0041     qputenv("KIO_DISABLE_CACHE_CLEANER", "yes");
0042 
0043     // Make sure we start clean
0044     cleanupTestCase();
0045 
0046     QVERIFY(QDir().mkpath(tmpDir()));
0047 
0048     // Taken from KArchiveTest::testCreateTar
0049     KTar tar(s_tarFileName);
0050     bool ok = tar.open(QIODevice::WriteOnly);
0051     QVERIFY(ok);
0052     writeTestFilesToArchive(&tar);
0053     ok = tar.close();
0054     QVERIFY(ok);
0055     QFileInfo fileInfo(QFile::encodeName(s_tarFileName));
0056     QVERIFY(fileInfo.exists());
0057 }
0058 
0059 void TestKioArchive::testListTar()
0060 {
0061     m_listResult.clear();
0062     KIO::ListJob *job = KIO::listDir(tarUrl(), KIO::HideProgressInfo);
0063     connect(job, &KIO::ListJob::entries, this, &TestKioArchive::slotEntries);
0064     bool ok = job->exec();
0065     QVERIFY(ok);
0066     QVERIFY(m_listResult.count() > 1);
0067 
0068     QCOMPARE(m_listResult.count("."), 1); // found it, and only once
0069     QCOMPARE(m_listResult.count("empty"), 1);
0070     QCOMPARE(m_listResult.count("test1"), 1);
0071     QCOMPARE(m_listResult.count("mydir"), 1);
0072     QCOMPARE(m_listResult.count("mydir/subfile"), 0); // not a recursive listing
0073     QCOMPARE(m_listResult.count("mydir/symlink"), 0);
0074 }
0075 
0076 void TestKioArchive::testListRecursive()
0077 {
0078     m_listResult.clear();
0079     KIO::ListJob *job = KIO::listRecursive(tarUrl(), KIO::HideProgressInfo);
0080     connect(job, &KIO::ListJob::entries, this, &TestKioArchive::slotEntries);
0081     bool ok = job->exec();
0082     QVERIFY(ok);
0083     QVERIFY(m_listResult.count() > 1);
0084 
0085     QCOMPARE(m_listResult.count("."), 1); // found it, and only once
0086     QCOMPARE(m_listResult.count("empty"), 1);
0087     QCOMPARE(m_listResult.count("test1"), 1);
0088     QCOMPARE(m_listResult.count("mydir"), 1);
0089     QCOMPARE(m_listResult.count("mydir/subfile"), 1);
0090     QCOMPARE(m_listResult.count("mydir/symlink"), 1);
0091 }
0092 
0093 QUrl TestKioArchive::tarUrl() const
0094 {
0095     QUrl url;
0096     url.setScheme("tar");
0097     url.setPath(QDir::currentPath());
0098     url = url.adjusted(QUrl::StripTrailingSlash);
0099     url.setPath(url.path() + '/' + s_tarFileName);
0100     return url;
0101 }
0102 
0103 void TestKioArchive::slotEntries(KIO::Job *, const KIO::UDSEntryList &lst)
0104 {
0105     for (KIO::UDSEntryList::ConstIterator it = lst.begin(); it != lst.end(); ++it) {
0106         const KIO::UDSEntry &entry(*it);
0107         QString displayName = entry.stringValue(KIO::UDSEntry::UDS_NAME);
0108         m_listResult << displayName;
0109     }
0110 }
0111 
0112 QString TestKioArchive::tmpDir() const
0113 {
0114     return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/test_kio_archive/";
0115 }
0116 
0117 void TestKioArchive::cleanupTestCase()
0118 {
0119     QDir(tmpDir()).removeRecursively();
0120 }
0121 
0122 void TestKioArchive::copyFromTar(const QUrl &src, const QString &destPath)
0123 {
0124     QUrl dest = QUrl::fromLocalFile(destPath);
0125     qDebug() << src << "->" << dest;
0126     // Check that src exists
0127     KIO::StatJob *statJob = KIO::stat(src, KIO::StatJob::SourceSide, KIO::StatNoDetails, KIO::HideProgressInfo);
0128 
0129     bool ok = statJob->exec();
0130     QVERIFY(ok);
0131 
0132     KIO::Job *job = KIO::copyAs(src, dest, KIO::HideProgressInfo);
0133     qDebug() << "copyAs" << src << dest;
0134     ok = job->exec();
0135     QVERIFY(ok);
0136     QVERIFY(QFile::exists(destPath));
0137 }
0138 
0139 void TestKioArchive::testExtractFileFromTar()
0140 {
0141     const QString destPath = tmpDir() + "fileFromTar_copied";
0142     QUrl u = tarUrl();
0143     u = u.adjusted(QUrl::StripTrailingSlash);
0144     u.setPath(u.path() + '/' + "mydir/subfile");
0145     copyFromTar(u, destPath);
0146     QVERIFY(QFileInfo(destPath).isFile());
0147     QVERIFY(QFileInfo(destPath).size() == 7);
0148 }
0149 
0150 void TestKioArchive::testExtractSymlinkFromTar()
0151 {
0152     const QString destPath = tmpDir() + "symlinkFromTar_copied";
0153     QUrl u = tarUrl();
0154     u = u.adjusted(QUrl::StripTrailingSlash);
0155     u.setPath(u.path() + '/' + "mydir/symlink");
0156     copyFromTar(u, destPath);
0157     QVERIFY(QFileInfo(destPath).isFile());
0158     QEXPECT_FAIL("", "See #5601 -- on FTP we want to download the real file, not the symlink...", Continue);
0159     // See comment in 149903
0160     // Maybe this is something we can do depending on Class=:local and Class=:internet
0161     // (we already know if a protocol is local or remote).
0162     // So local->local should copy symlinks, while internet->local and internet->internet should
0163     // copy the actual file, I guess?
0164     // -> ### TODO
0165     QVERIFY(QFileInfo(destPath).isSymLink());
0166 }
0167 
0168 #include "moc_testkioarchive.cpp"