File indexing completed on 2024-12-01 09:52:41
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2020-2021 David Faure <faure@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #include "mimetypefinderjobtest.h" 0009 #include "mimetypefinderjob.h" 0010 0011 #include <kio/global.h> 0012 0013 #include <KConfigGroup> 0014 #include <KSharedConfig> 0015 #include <QStandardPaths> 0016 #include <QTemporaryDir> 0017 #include <QTest> 0018 0019 QTEST_GUILESS_MAIN(MimeTypeFinderJobTest) 0020 0021 void MimeTypeFinderJobTest::initTestCase() 0022 { 0023 QStandardPaths::setTestModeEnabled(true); 0024 } 0025 0026 void MimeTypeFinderJobTest::cleanupTestCase() 0027 { 0028 } 0029 0030 void MimeTypeFinderJobTest::init() 0031 { 0032 } 0033 0034 static void createSrcFile(const QString &path) 0035 { 0036 QFile srcFile(path); 0037 QVERIFY2(srcFile.open(QFile::WriteOnly), qPrintable(srcFile.errorString())); 0038 srcFile.write("Hello world\n"); 0039 } 0040 0041 void MimeTypeFinderJobTest::determineMimeType_data() 0042 { 0043 QTest::addColumn<QString>("mimeType"); 0044 QTest::addColumn<QString>("fileName"); 0045 0046 /* clang-format off */ 0047 QTest::newRow("text_file") << "text/plain" << "srcfile.txt"; 0048 QTest::newRow("text_file_no_extension") << "text/plain" << "srcfile"; 0049 QTest::newRow("desktop_file") << "application/x-desktop" << "foo.desktop"; 0050 QTest::newRow("script") << "application/x-shellscript" << "srcfile.sh"; 0051 QTest::newRow("directory") << "inode/directory" << "srcdir"; 0052 /* clang-format on */ 0053 } 0054 0055 void MimeTypeFinderJobTest::determineMimeType() 0056 { 0057 QFETCH(QString, mimeType); 0058 QFETCH(QString, fileName); 0059 0060 // Given a file to open 0061 QTemporaryDir tempDir; 0062 const QString srcDir = tempDir.path(); 0063 const QString srcFile = srcDir + QLatin1Char('/') + fileName; 0064 if (mimeType == "inode/directory") { 0065 QVERIFY(QDir(srcDir).mkdir(fileName)); 0066 } else { 0067 createSrcFile(srcFile); 0068 } 0069 0070 QVERIFY(QFile::exists(srcFile)); 0071 const QUrl url = QUrl::fromLocalFile(srcFile); 0072 0073 // When running a MimeTypeFinderJob 0074 KIO::MimeTypeFinderJob *job = new KIO::MimeTypeFinderJob(url, this); 0075 QVERIFY2(job->exec(), qPrintable(job->errorString())); 0076 QCOMPARE(job->mimeType(), mimeType); 0077 0078 // Check that the result is the same when accessing the source 0079 // file through a symbolic link (bug #436708) 0080 const QString srcLink = srcDir + QLatin1String("/link_") + fileName; 0081 QVERIFY(QFile::link(srcFile, srcLink)); 0082 const QUrl linkUrl = QUrl::fromLocalFile(srcLink); 0083 0084 job = new KIO::MimeTypeFinderJob(linkUrl, this); 0085 QVERIFY2(job->exec(), qPrintable(job->errorString())); 0086 QCOMPARE(job->mimeType(), mimeType); 0087 } 0088 0089 void MimeTypeFinderJobTest::invalidUrl() 0090 { 0091 KIO::MimeTypeFinderJob *job = new KIO::MimeTypeFinderJob(QUrl(":/"), this); 0092 QVERIFY(!job->exec()); 0093 QCOMPARE(job->error(), KIO::ERR_MALFORMED_URL); 0094 QCOMPARE(job->errorString(), QStringLiteral("Malformed URL\nRelative URL's path component contains ':' before any '/'; source was \":/\"; path = \":/\"")); 0095 0096 QUrl u; 0097 u.setPath(QStringLiteral("/pathonly")); 0098 KIO::MimeTypeFinderJob *job2 = new KIO::MimeTypeFinderJob(u, this); 0099 QVERIFY(!job2->exec()); 0100 QCOMPARE(job2->error(), KIO::ERR_MALFORMED_URL); 0101 QCOMPARE(job2->errorString(), QStringLiteral("Malformed URL\n/pathonly")); 0102 } 0103 0104 void MimeTypeFinderJobTest::nonExistingFile() 0105 { 0106 KIO::MimeTypeFinderJob *job = new KIO::MimeTypeFinderJob(QUrl::fromLocalFile(QStringLiteral("/does/not/exist")), this); 0107 QVERIFY(!job->exec()); 0108 QCOMPARE(job->error(), KIO::ERR_DOES_NOT_EXIST); 0109 QCOMPARE(job->errorString(), "The file or folder /does/not/exist does not exist."); 0110 } 0111 0112 void MimeTypeFinderJobTest::httpUrlWithKIO() 0113 { 0114 // This tests the scanFileWithGet() code path 0115 const QUrl url(QStringLiteral("https://www.google.com/")); 0116 KIO::MimeTypeFinderJob *job = new KIO::MimeTypeFinderJob(url, this); 0117 job->setFollowRedirections(false); 0118 QVERIFY2(job->exec(), qPrintable(job->errorString())); 0119 QCOMPARE(job->mimeType(), QStringLiteral("text/html")); 0120 } 0121 0122 void MimeTypeFinderJobTest::killHttp() 0123 { 0124 // This tests the scanFileWithGet() code path 0125 const QUrl url(QStringLiteral("https://www.google.com/")); 0126 KIO::MimeTypeFinderJob *job = new KIO::MimeTypeFinderJob(url, this); 0127 job->start(); 0128 QVERIFY(job->kill()); 0129 } 0130 0131 void MimeTypeFinderJobTest::ftpUrlWithKIO() 0132 { 0133 // This is just to test the statFile() code at least a bit 0134 const QUrl url(QStringLiteral("ftp://localhost:2")); // unlikely that anything is running on that port 0135 KIO::MimeTypeFinderJob *job = new KIO::MimeTypeFinderJob(url, this); 0136 QVERIFY(!job->exec()); 0137 QVERIFY(job->errorString() == QLatin1String("Could not connect to host localhost: Connection refused.") 0138 || job->errorString() == QLatin1String("Could not connect to host localhost: Network unreachable.")); 0139 } 0140 0141 #include "moc_mimetypefinderjobtest.cpp"