File indexing completed on 2024-04-21 11:32:22

0001 /*
0002     SPDX-FileCopyrightText: 2002 David Faure <faure@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include <KConfig>
0008 #include <KConfigGroup>
0009 #include <KPluginMetaData>
0010 #include <QDebug>
0011 #include <QFile>
0012 #include <QJsonObject>
0013 #include <QStandardPaths>
0014 #include <QTest>
0015 #include <QUrl>
0016 #include <algorithm>
0017 #include <kprotocolmanager.h>
0018 
0019 // Tests both KProtocolInfo and KProtocolManager
0020 
0021 class KProtocolInfoTest : public QObject
0022 {
0023     Q_OBJECT
0024 private Q_SLOTS:
0025     void initTestCase();
0026 
0027     void testBasic();
0028     void testExtraFields();
0029     void testShowFilePreview();
0030     void testWorkerProtocol();
0031     void testProxySettings_data();
0032     void testProxySettings();
0033     void testCapabilities();
0034     void testProtocolForArchiveMimetype();
0035     void testHelperProtocols();
0036 };
0037 
0038 void KProtocolInfoTest::initTestCase()
0039 {
0040     QString configFile = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/kioslaverc";
0041     QFile::remove(configFile);
0042 }
0043 
0044 void KProtocolInfoTest::testBasic()
0045 {
0046     QVERIFY(KProtocolInfo::isKnownProtocol(QUrl(QStringLiteral("http:/"))));
0047     QVERIFY(KProtocolInfo::isKnownProtocol(QUrl(QStringLiteral("file:/"))));
0048     QVERIFY(KProtocolInfo::exec(QStringLiteral("file")).contains(QStringLiteral("kf" QT_STRINGIFY(QT_VERSION_MAJOR) "/kio/kio_file")));
0049     QCOMPARE(KProtocolInfo::protocolClass(QStringLiteral("file")), QStringLiteral(":local"));
0050 
0051     QCOMPARE(KProtocolInfo::protocolClass(QStringLiteral("http")), QStringLiteral(":internet"));
0052 
0053     QCOMPARE(KProtocolInfo::defaultMimetype(QStringLiteral("help")), QString("text/html"));
0054     QCOMPARE(KProtocolInfo::defaultMimetype(QStringLiteral("http")), QString("application/octet-stream"));
0055 
0056     QVERIFY(KProtocolManager::supportsListing(QUrl(QStringLiteral("ftp://10.1.1.10"))));
0057 
0058     const QUrl url = QUrl::fromLocalFile(QStringLiteral("/tmp"));
0059     QCOMPARE(KProtocolManager::inputType(url), KProtocolInfo::T_NONE);
0060     QCOMPARE(KProtocolManager::outputType(url), KProtocolInfo::T_FILESYSTEM);
0061     QVERIFY(KProtocolManager::supportsReading(url));
0062 }
0063 
0064 void KProtocolInfoTest::testExtraFields()
0065 {
0066     KProtocolInfo::ExtraFieldList extraFields = KProtocolInfo::extraFields(QUrl(QStringLiteral("trash:/")));
0067     KProtocolInfo::ExtraFieldList::Iterator extraFieldsIt = extraFields.begin();
0068     for (; extraFieldsIt != extraFields.end(); ++extraFieldsIt) {
0069         qDebug() << (*extraFieldsIt).name << " " << (*extraFieldsIt).type;
0070     }
0071     // TODO
0072 }
0073 
0074 void KProtocolInfoTest::testShowFilePreview()
0075 {
0076     QVERIFY(KProtocolInfo::showFilePreview(QStringLiteral("file")));
0077     QVERIFY(!KProtocolInfo::showFilePreview(QStringLiteral("audiocd")));
0078 }
0079 
0080 void KProtocolInfoTest::testWorkerProtocol()
0081 {
0082     QString proxy;
0083     QString protocol = KProtocolManager::workerProtocol(QUrl(QStringLiteral("http://bugs.kde.org")), proxy);
0084     QCOMPARE(protocol, QStringLiteral("http"));
0085     QVERIFY(!KProtocolManager::useProxy());
0086 
0087     // Just to test it doesn't deadlock
0088     KProtocolManager::reparseConfiguration();
0089     protocol = KProtocolManager::workerProtocol(QUrl(QStringLiteral("http://bugs.kde.org")), proxy);
0090     QCOMPARE(protocol, QStringLiteral("http"));
0091 }
0092 
0093 void KProtocolInfoTest::testProxySettings_data()
0094 {
0095     QTest::addColumn<int>("proxyType");
0096 
0097     // Just to test it doesn't deadlock (bug 346214)
0098     QTest::newRow("manual") << static_cast<int>(KProtocolManager::ManualProxy);
0099     QTest::newRow("wpad") << static_cast<int>(KProtocolManager::WPADProxy);
0100     // Same for bug 350890
0101     QTest::newRow("envvar") << static_cast<int>(KProtocolManager::EnvVarProxy);
0102 }
0103 
0104 void KProtocolInfoTest::testProxySettings()
0105 {
0106     QFETCH(int, proxyType);
0107     KConfig config(QStringLiteral("kioslaverc"), KConfig::NoGlobals);
0108     KConfigGroup cfg(&config, "Proxy Settings");
0109     cfg.writeEntry("ProxyType", proxyType);
0110     cfg.sync();
0111     KProtocolManager::reparseConfiguration();
0112     QString proxy;
0113     QString protocol = KProtocolManager::workerProtocol(QUrl(QStringLiteral("http://bugs.kde.org")), proxy);
0114     QCOMPARE(protocol, QStringLiteral("http"));
0115     QVERIFY(KProtocolManager::useProxy());
0116 
0117     // restore
0118     cfg.writeEntry("ProxyType", static_cast<int>(KProtocolManager::NoProxy));
0119     cfg.sync();
0120     KProtocolManager::reparseConfiguration();
0121 }
0122 
0123 void KProtocolInfoTest::testCapabilities()
0124 {
0125     QStringList capabilities = KProtocolInfo::capabilities(QStringLiteral("imap"));
0126     qDebug() << "kio_imap capabilities: " << capabilities;
0127     // QVERIFY(capabilities.contains("ACL"));
0128 }
0129 
0130 void KProtocolInfoTest::testProtocolForArchiveMimetype()
0131 {
0132     // The zip protocol is available at least with the kio_archive worker from kio-extras repo (11 2022)
0133     auto supportsZipProtocol = [](const KPluginMetaData &metaData) {
0134         const QJsonObject protocols = metaData.rawData().value(QStringLiteral("KDE-KIO-Protocols")).toObject();
0135         return (protocols.find(QLatin1String("zip")) != protocols.end());
0136     };
0137 
0138     const QVector<KPluginMetaData> workers = KPluginMetaData::findPlugins(QStringLiteral("kf" QT_STRINGIFY(QT_VERSION_MAJOR) "/kio"));
0139     if (std::none_of(workers.cbegin(), workers.cend(), supportsZipProtocol)) {
0140         QSKIP("kio-extras not installed");
0141     } else {
0142         const QString zip = KProtocolManager::protocolForArchiveMimetype(QStringLiteral("application/zip"));
0143         // Krusader's kio_krarc.so also provides the zip protocol and might be found before/instead
0144         QVERIFY(zip == QLatin1String("zip") || zip == QLatin1String("krarc"));
0145     }
0146 }
0147 
0148 void KProtocolInfoTest::testHelperProtocols()
0149 {
0150     QVERIFY(!KProtocolInfo::isHelperProtocol(QStringLiteral("http")));
0151     QVERIFY(!KProtocolInfo::isHelperProtocol(QStringLiteral("ftp")));
0152     QVERIFY(!KProtocolInfo::isHelperProtocol(QStringLiteral("file")));
0153     QVERIFY(!KProtocolInfo::isHelperProtocol(QStringLiteral("unknown")));
0154     // Comes from ktelnetservice.desktop:MimeType=x-scheme-handler/telnet;x-scheme-handler/rlogin;x-scheme-handler/ssh;
0155     // TODO: this logic has moved to KRun. Should it be public API, so we can unittest it?
0156     // QVERIFY(KProtocolInfo::isHelperProtocol("telnet"));
0157 
0158     // To test that compat still works
0159     if (KProtocolInfo::isKnownProtocol(QStringLiteral("tel"))) {
0160         QVERIFY(KProtocolInfo::isHelperProtocol(QStringLiteral("tel")));
0161     }
0162 }
0163 
0164 QTEST_MAIN(KProtocolInfoTest)
0165 
0166 #include "kprotocolinfotest.moc"