File indexing completed on 2024-05-05 05:53:33

0001 /***************************************************************************
0002  *   Copyright (C) 2016 by Arnav Dhamija <arnav.dhamija@gmail.com>         *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
0018  ***************************************************************************/
0019 
0020 #include "workertest.h"
0021 
0022 #include <QDBusConnection>
0023 #include <QDBusMessage>
0024 #include <QDebug>
0025 #include <QDir>
0026 #include <QFile>
0027 #include <QFileInfo>
0028 #include <QStandardPaths>
0029 #include <QTemporaryFile>
0030 #include <QTest>
0031 
0032 #include <KFileItem>
0033 #include <KIO/CopyJob>
0034 #include <KIO/DeleteJob>
0035 #include <KIO/Job>
0036 #include <KIO/ListJob>
0037 #include <KIO/MkdirJob>
0038 #include <KIO/StatJob>
0039 
0040 WorkerTest::WorkerTest()
0041     : tmpFolder("WorkerTest")
0042     , m_fileTestFile("TestFile")
0043     , m_fileTestFolder("TestSubFolder")
0044     , m_stashTestFolder("StashTestFolder")
0045     , m_stashTestSymlink("StashTestSymlink")
0046     , m_stashTestFile("StashFile")
0047     , m_stashTestFileInSubDirectory("SubTestFile")
0048     , m_newStashFileName("NewStashFile")
0049     , m_stashFileForRename("StashRenameFile")
0050     , m_absolutePath(QDir::currentPath())
0051 {
0052 }
0053 
0054 void WorkerTest::initTestCase()
0055 {
0056     // enclose a check statement around this block to see if kded5 is not already there
0057     QDBusMessage msg;
0058     QDBusMessage replyMessage;
0059 
0060     stashDaemonProcess = new QProcess();
0061 
0062     msg = QDBusMessage::createMethodCall("org.kde.kio.StashNotifier", "/StashNotifier", "", "pingDaemon");
0063     replyMessage = QDBusConnection::sessionBus().call(msg);
0064     if (replyMessage.type() == QDBusMessage::ErrorMessage) {
0065         qDebug() << "Launching fallback daemon";
0066         const QString program = "./testdaemon";
0067         stashDaemonProcess->start(program, QStringList{});
0068     }
0069 
0070     replyMessage = QDBusConnection::sessionBus().call(msg);
0071 
0072     if (replyMessage.type() != QDBusMessage::ErrorMessage) {
0073         qDebug() << "Test case initialised";
0074     } else {
0075         qDebug() << "Something is wrong!";
0076     }
0077     createTestFiles();
0078 }
0079 
0080 void WorkerTest::createTestFiles() // also find a way to reset the directory prior to use
0081 {
0082     QDir tmpDir;
0083     tmpDir.mkdir(tmpDirPath()); // creates test dir
0084     tmpDir.mkdir(tmpDirPath() + m_fileTestFolder);
0085 
0086     QFile tmpFile;
0087     stashDirectory('/' + m_stashTestFolder);
0088 
0089     QUrl src = QUrl::fromLocalFile(tmpDirPath() + m_fileTestFile); // creates a file to be tested
0090     tmpFile.setFileName(src.path());
0091     QVERIFY(tmpFile.open(QIODevice::ReadWrite));
0092     tmpFile.close();
0093 
0094     src = QUrl::fromLocalFile(tmpDirPath() + m_stashTestFile); // creates a file to be stashed
0095     tmpFile.setFileName(src.path());
0096     QVERIFY(tmpFile.open(QIODevice::ReadWrite));
0097     tmpFile.close();
0098     stashFile(src.path(), '/' + m_stashTestFile);
0099 
0100     src = QUrl::fromLocalFile(tmpDirPath() + m_stashFileForRename);
0101     tmpFile.setFileName(src.path());
0102     QVERIFY(tmpFile.open(QIODevice::ReadWrite));
0103     tmpFile.close();
0104     stashFile(src.path(), '/' + m_stashFileForRename);
0105 
0106     src = QUrl::fromLocalFile(tmpDirPath() + m_stashTestFileInSubDirectory);
0107     tmpFile.setFileName(src.path());
0108     QVERIFY(tmpFile.open(QIODevice::ReadWrite));
0109     tmpFile.close();
0110 
0111     stashFile(src.path(), '/' + m_stashTestFolder + '/' + m_stashTestFileInSubDirectory);
0112 }
0113 
0114 void WorkerTest::cleanupTestCase()
0115 {
0116     QDir dir(tmpDirPath());
0117     dir.removeRecursively();
0118     stashDaemonProcess->terminate();
0119 }
0120 
0121 QString WorkerTest::tmpDirPath()
0122 {
0123     return m_absolutePath + '/' + tmpFolder + '/';
0124 }
0125 
0126 void WorkerTest::statItem(const QUrl &url, const int &type)
0127 {
0128     KIO::UDSEntry entry;
0129     QVERIFY(statUrl(url, entry));
0130     KFileItem item(entry, url);
0131     switch (type) {
0132     case NodeType::DirectoryNode:
0133         QVERIFY(item.isDir());
0134         break;
0135     case NodeType::SymlinkNode:
0136         QVERIFY(item.isLink()); // don't worry this is intentional :)
0137     case NodeType::FileNode:
0138         QVERIFY(item.isFile());
0139     }
0140     QVERIFY(item.isReadable());
0141     QVERIFY(!item.isHidden());
0142     QCOMPARE(item.text(), url.fileName());
0143 }
0144 
0145 void WorkerTest::stashFile(const QString &realPath, const QString &stashPath)
0146 {
0147     QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kio.StashNotifier", "/StashNotifier", "", "addPath");
0148     msg << realPath << stashPath << NodeType::FileNode;
0149     bool queued = QDBusConnection::sessionBus().send(msg);
0150     QVERIFY(queued);
0151 }
0152 
0153 void WorkerTest::stashDirectory(const QString &path)
0154 {
0155     QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kio.StashNotifier", "/StashNotifier", "", "addPath");
0156     QString destinationPath = path;
0157     msg << "" << destinationPath << NodeType::DirectoryNode;
0158     bool queued = QDBusConnection::sessionBus().send(msg);
0159     QVERIFY(queued);
0160 }
0161 
0162 void WorkerTest::stashSymlink(const QString &realPath, const QString &stashPath)
0163 {
0164     QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kio.StashNotifier", "/StashNotifier", "", "addPath");
0165     msg << realPath << stashPath << NodeType::SymlinkNode;
0166     bool queued = QDBusConnection::sessionBus().send(msg);
0167     QVERIFY(queued);
0168 }
0169 
0170 bool WorkerTest::statUrl(const QUrl &url, KIO::UDSEntry &entry)
0171 {
0172     KIO::StatJob *statJob = KIO::stat(url, KIO::HideProgressInfo);
0173     bool ok = statJob->exec();
0174     if (ok) {
0175         entry = statJob->statResult();
0176     }
0177     return ok;
0178 }
0179 
0180 void WorkerTest::nukeStash()
0181 {
0182     QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kio.StashNotifier", "/StashNotifier", "", "nukeStash");
0183     bool queued = QDBusConnection::sessionBus().send(msg);
0184     QVERIFY(queued);
0185 }
0186 
0187 void WorkerTest::stashCopy(const QUrl &src, const QUrl &dest)
0188 {
0189     KIO::CopyJob *job = KIO::copy(src, dest, KIO::HideProgressInfo);
0190     bool ok = job->exec();
0191     QVERIFY(ok);
0192 }
0193 
0194 void WorkerTest::moveFromStash(const QUrl &src, const QUrl &dest) // make this work
0195 {
0196     KIO::Job *job = KIO::move(src, dest, KIO::HideProgressInfo);
0197     bool ok = job->exec();
0198     QVERIFY(ok);
0199 }
0200 
0201 void WorkerTest::deleteFromStash(const QUrl &url)
0202 {
0203     KIO::Job *delJob = KIO::del(url, KIO::HideProgressInfo);
0204     bool ok = delJob->exec();
0205     QVERIFY(ok);
0206 }
0207 
0208 void WorkerTest::listRootDir()
0209 {
0210     KIO::ListJob *job = KIO::listDir(QUrl(QStringLiteral("stash:/")), KIO::HideProgressInfo);
0211     connect(job, SIGNAL(entries(KIO::Job *, KIO::UDSEntryList)), SLOT(slotEntries(KIO::Job *, KIO::UDSEntryList)));
0212     bool ok = job->exec();
0213     QVERIFY(ok);
0214 }
0215 
0216 void WorkerTest::listSubDir()
0217 {
0218     KIO::ListJob *job = KIO::listDir(QUrl("stash:/" + m_stashTestFolder), KIO::HideProgressInfo);
0219     connect(job, SIGNAL(entries(KIO::Job *, KIO::UDSEntryList)), SLOT(slotEntries(KIO::Job *, KIO::UDSEntryList)));
0220     bool ok = job->exec();
0221     QVERIFY(ok);
0222 }
0223 
0224 void WorkerTest::createDirectory()
0225 {
0226     QUrl directoryPath = QUrl("");
0227     KIO::SimpleJob *job = KIO::mkdir(directoryPath);
0228     bool ok = job->exec();
0229     QVERIFY(QFile::exists(directoryPath.path()));
0230     QCOMPARE(ok, true);
0231 }
0232 
0233 void WorkerTest::statRoot()
0234 {
0235     QUrl url("stash:/");
0236     KIO::UDSEntry entry;
0237     QVERIFY(statUrl(url, entry));
0238     KFileItem item(entry, url);
0239     QVERIFY(item.isDir());
0240     QVERIFY(!item.isLink());
0241     QVERIFY(item.isReadable());
0242     QVERIFY(item.isWritable());
0243     QVERIFY(!item.isHidden());
0244     QCOMPARE(item.name(), QStringLiteral("."));
0245 }
0246 
0247 void WorkerTest::statFileInRoot()
0248 {
0249     QFile file;
0250     QUrl url("stash:/" + m_stashTestFile);
0251     stashFile(url.path(), url.path());
0252     KIO::UDSEntry entry;
0253     QVERIFY(statUrl(url, entry));
0254     KFileItem item(entry, url);
0255     QVERIFY(item.isFile());
0256     QVERIFY(!item.isDir());
0257     QVERIFY(!item.isLink());
0258     QVERIFY(item.isReadable());
0259     QVERIFY(!item.isHidden());
0260 }
0261 
0262 void WorkerTest::statDirectoryInRoot()
0263 {
0264     QUrl url("stash:/" + m_stashTestFolder);
0265     stashDirectory(url.path());
0266     KIO::UDSEntry entry;
0267     QVERIFY(statUrl(url, entry));
0268     KFileItem item(entry, url);
0269     QVERIFY(!item.isFile());
0270     QVERIFY(item.isDir());
0271     QVERIFY(!item.isLink());
0272     QVERIFY(item.isReadable());
0273     QVERIFY(!item.isHidden());
0274 }
0275 
0276 void WorkerTest::statSymlinkInRoot()
0277 {
0278     QUrl url("stash:/" + m_stashTestSymlink);
0279     stashSymlink(url.path(), url.path());
0280     KIO::UDSEntry entry;
0281     QVERIFY(statUrl(url, entry));
0282     KFileItem item(entry, url);
0283     QVERIFY(item.isFile());
0284     QVERIFY(!item.isDir());
0285     QVERIFY(item.isReadable());
0286     QVERIFY(!item.isHidden());
0287 }
0288 
0289 void WorkerTest::statFileInDirectory()
0290 {
0291     QUrl url("stash:/" + m_stashTestFolder + '/' + m_stashTestFileInSubDirectory);
0292     KIO::UDSEntry entry;
0293     QVERIFY(statUrl(url, entry));
0294     KFileItem item(entry, url);
0295     QVERIFY(item.isFile());
0296     QVERIFY(!item.isDir());
0297     QVERIFY(!item.isLink());
0298     QVERIFY(item.isReadable());
0299     QVERIFY(!item.isHidden());
0300 }
0301 
0302 void WorkerTest::copyFileToStash()
0303 {
0304     QUrl src = QUrl::fromLocalFile(tmpDirPath() + m_fileTestFile);
0305     QFile testFile(src.path());
0306     QVERIFY(testFile.open(QIODevice::WriteOnly));
0307     QUrl dest("stash:/" + m_fileTestFile);
0308 
0309     stashCopy(src, dest);
0310     QVERIFY(testFile.exists());
0311     statItem(dest, NodeType::FileNode);
0312 }
0313 
0314 void WorkerTest::copySymlinkFromStashToFile() // create test case
0315 {
0316     stashSymlink(tmpDirPath() + m_fileTestFile, '/' + m_stashTestSymlink);
0317     QUrl src("stash:/" + m_stashTestSymlink);
0318     QUrl dest = QUrl::fromLocalFile(tmpDirPath() + m_fileTestFolder + '/' + m_stashTestSymlink);
0319 
0320     stashCopy(src, dest);
0321     QVERIFY(QFile::exists(dest.path()));
0322 }
0323 
0324 void WorkerTest::copyStashToFile()
0325 {
0326     QUrl src("stash:/" + m_stashTestFile);
0327     QUrl dest = QUrl::fromLocalFile(tmpDirPath() + m_fileTestFolder + '/' + m_stashTestFile);
0328     KIO::UDSEntry entry;
0329     stashCopy(src, dest);
0330     QVERIFY(QFile::exists(dest.toLocalFile()));
0331     QFile(dest.path()).remove();
0332 }
0333 
0334 void WorkerTest::moveToFileFromStash()
0335 {
0336     QUrl src("stash:/" + m_stashTestFile);
0337     QUrl dest = QUrl::fromLocalFile(tmpDirPath() + m_fileTestFolder + '/' + m_stashTestFile);
0338 
0339     moveFromStash(src, dest);
0340     KIO::UDSEntry entry;
0341     QVERIFY(!statUrl(src, entry));
0342     QVERIFY(QFile::exists(dest.toLocalFile()));
0343 }
0344 
0345 void WorkerTest::copyStashToStash()
0346 {
0347     QUrl src("stash:/" + m_stashTestFile);
0348     QUrl dest("stash:/" + m_stashTestFolder + '/' + m_stashTestFile);
0349     stashCopy(src, dest);
0350     KIO::UDSEntry entry;
0351     statUrl(src, entry);
0352     KFileItem item(entry, src);
0353     QVERIFY(item.name() == m_stashTestFile);
0354 }
0355 
0356 void WorkerTest::renameFileInStash()
0357 {
0358     QUrl src("stash:/" + m_stashFileForRename);
0359     QUrl dest("stash:/" + m_newStashFileName);
0360 
0361     KIO::UDSEntry entry;
0362 
0363     moveFromStash(src, dest);
0364 
0365     QVERIFY(!statUrl(src, entry));
0366 
0367     statUrl(dest, entry);
0368     KFileItem item(entry, src);
0369     QVERIFY(item.name() == m_newStashFileName);
0370 }
0371 
0372 void WorkerTest::delRootFile()
0373 {
0374     QUrl url("stash:/" + m_stashTestFile);
0375     deleteFromStash(url);
0376     KIO::UDSEntry entry;
0377     QVERIFY(!statUrl(url, entry));
0378 }
0379 
0380 void WorkerTest::delFileInDirectory()
0381 {
0382     QUrl url("stash:/" + m_stashTestFolder + '/' + m_stashTestFileInSubDirectory);
0383     deleteFromStash(url);
0384     KIO::UDSEntry entry;
0385     QVERIFY(!statUrl(url, entry));
0386 }
0387 
0388 void WorkerTest::delDirectory()
0389 {
0390     QUrl url("stash:/" + m_stashTestFolder);
0391     deleteFromStash(url);
0392     KIO::UDSEntry entry;
0393     QVERIFY(!statUrl(url, entry));
0394 }
0395 
0396 void WorkerTest::init()
0397 {
0398     createTestFiles();
0399 }
0400 
0401 void WorkerTest::cleanup()
0402 {
0403     QDir dir(tmpDirPath());
0404     dir.removeRecursively();
0405 }
0406 
0407 QTEST_MAIN(WorkerTest)