File indexing completed on 2024-04-21 07:41:48

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