/* * Copyright (C) 2008-2021 The QXmpp developers * * Author: * Yury Gubich * Linus Jahn * * Source: * https://github.com/qxmpp-project/qxmpp * * This file is a part of QXmpp library. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * */ #include "QXmppClient.h" #include "QXmppDiscoveryManager.h" #include "QXmppHttpUploadIq.h" #include "QXmppLogger.h" #include "QXmppUploadRequestManager.h" #include "util.h" #include #include #include class TestHelper : public QObject { Q_OBJECT public: TestHelper(bool expectedEvent, bool expectedError); ~TestHelper(); public slots: void onSlotReceived(const QXmppHttpUploadSlotIq& slot); void onRequestFailed(const QXmppHttpUploadRequestIq& request); private: bool expectedEvent; bool expectedError; bool event; bool error; }; TestHelper::TestHelper(bool p_expectedEvent, bool p_expectedError) : QObject(), expectedEvent(p_expectedEvent), expectedError(p_expectedError), event(false), error(false) { } TestHelper::~TestHelper() { QCOMPARE(event, expectedEvent); QCOMPARE(error, expectedError); } void TestHelper::onRequestFailed(const QXmppHttpUploadRequestIq& request) { event = true; error = true; } void TestHelper::onSlotReceived(const QXmppHttpUploadSlotIq& slot) { event = true; error = false; } class tst_QXmppUploadRequestManager : public QObject { Q_OBJECT protected slots: void onLoggerMessage(QXmppLogger::MessageType type, const QString& text) const; private slots: void initTestCase(); void testDiscoveryService_data(); void testDiscoveryService(); void testHandleStanza_data(); void testHandleStanza(); void testSending_data(); void testSending(); void testUploadService(); private: QXmppUploadRequestManager* manager; QXmppClient client; QXmppDiscoveryManager* discovery; QString uploadServiceName; qint64 maxFileSize; QMimeType lastMimeType; QString lastFileName; qint64 lastFileSize; }; void tst_QXmppUploadRequestManager::onLoggerMessage(QXmppLogger::MessageType type, const QString& text) const { QCOMPARE(type, QXmppLogger::SentMessage); QDomDocument doc; QCOMPARE(doc.setContent(text, true), true); QDomElement element = doc.documentElement(); QXmppHttpUploadRequestIq iq; iq.parse(element); QCOMPARE(iq.type(), QXmppIq::Get); QCOMPARE(iq.to(), uploadServiceName); QCOMPARE(iq.fileName(), lastFileName); QCOMPARE(iq.size(), lastFileSize); QCOMPARE(iq.contentType(), lastMimeType); } void tst_QXmppUploadRequestManager::initTestCase() { uploadServiceName = "upload.montague.tld"; maxFileSize = 500UL * 1024UL * 1024UL; manager = new QXmppUploadRequestManager(); discovery = client.findExtension(); client.addExtension(manager); } void tst_QXmppUploadRequestManager::testHandleStanza_data() { QTest::addColumn("xml"); QTest::addColumn("accepted"); QTest::addColumn("event"); QTest::addColumn("error"); QTest::newRow("notAccepted") << QByteArray("" "" "" "" "What man art thou that, thus bescreen'd in night, so stumblest on my counsel?" "0e3141cd80894871a68e6fe6b1ec56fa" "" "" "" "") << false << false << false; QTest::newRow("slotReceived") << QByteArray("" "" "" "
Basic Base64String==
" "
foo=bar; user=romeo
" "
" "" "
" "
") << true << true << false; QTest::newRow("tooLargeError") << QByteArray("" "" "" "" "File too large. The maximum file size is 20000 bytes" "" "20000" "" "" "") << true << true << true; QTest::newRow("quotaReachedError") << QByteArray("" "" "" "" "Quota reached. You can only upload 5 files in 5 minutes" "" "" "") << true << true << true; } void tst_QXmppUploadRequestManager::testHandleStanza() { QFETCH(QByteArray, xml); QFETCH(bool, accepted); QFETCH(bool, event); QFETCH(bool, error); TestHelper helper(event, error); connect(manager, &QXmppUploadRequestManager::slotReceived, &helper, &TestHelper::onSlotReceived); connect(manager, &QXmppUploadRequestManager::requestFailed, &helper, &TestHelper::onRequestFailed); QDomDocument doc; QCOMPARE(doc.setContent(xml, true), true); QDomElement element = doc.documentElement(); bool realAccepted = manager->handleStanza(element); QCOMPARE(realAccepted, accepted); } void tst_QXmppUploadRequestManager::testDiscoveryService_data() { QTest::addColumn("xml"); QTest::addColumn("discovered"); QTest::newRow("mixDiscoveryStanzaIq") << QByteArray("" "" "" "" "" "" "") << false; QTest::newRow("HTTPUploadDiscoveryStanzaIq") << QByteArray("" "" "" "" "" "" "urn:xmpp:http:upload:0" "" "" "" + QByteArray::number(maxFileSize) + "" "" "" "" "") << true; } void tst_QXmppUploadRequestManager::testDiscoveryService() { QFETCH(QByteArray, xml); QFETCH(bool, discovered); QDomDocument doc; QCOMPARE(doc.setContent(xml, true), true); QDomElement element = doc.documentElement(); bool accepted = discovery->handleStanza(element); QCOMPARE(accepted, true); QCOMPARE(manager->serviceFound(), discovered); if (manager->serviceFound()) { QCOMPARE(manager->uploadServices().at(0).jid(), uploadServiceName); QCOMPARE(manager->uploadServices().at(0).sizeLimit(), maxFileSize); } } void tst_QXmppUploadRequestManager::testSending_data() { QTest::addColumn("fileInfo"); QTest::addColumn("fileName"); QTest::addColumn("fileSize"); QTest::addColumn("fileType"); QTest::newRow("fileInfo") << QFileInfo(":/test.svg") << "test.svg" << 2280LL << "image/svg+xml"; QTest::newRow("fileWithSizeBelowLimit") << QFileInfo() << "whatever.jpeg" << 698547LL << "image/jpeg"; QTest::newRow("fileWithSizeAboveLimit") << QFileInfo() << "some.pdf" << 65896498547LL << "application/pdf"; // there is no size above limit handling in request manager // there is also no code that selects an upload service with proper // size limit above requesting file size. // Is it something to worry about? } void tst_QXmppUploadRequestManager::testSending() { QFETCH(QFileInfo, fileInfo); QFETCH(QString, fileName); QFETCH(qint64, fileSize); QFETCH(QString, fileType); QXmppLogger* logger = new QXmppLogger(); logger->setLoggingType(QXmppLogger::SignalLogging); client.setLogger(logger); lastMimeType = QMimeDatabase().mimeTypeForName(fileType); connect(logger, &QXmppLogger::message, this, &tst_QXmppUploadRequestManager::onLoggerMessage); lastFileName = fileName; lastFileSize = fileSize; QString returnId; if (!fileInfo.baseName().isEmpty()) returnId = manager->requestUploadSlot(fileInfo); else returnId = manager->requestUploadSlot(fileName, fileSize, lastMimeType); // The client is not connected, so we never get an ID back (the packet was not sent). QVERIFY(returnId.isNull()); } void tst_QXmppUploadRequestManager::testUploadService() { QXmppUploadService service; QCOMPARE(service.sizeLimit(), -1LL); QVERIFY(service.jid().isNull()); service.setSizeLimit(256LL * 1024LL * 1024LL); QCOMPARE(service.sizeLimit(), 256LL * 1024LL * 1024LL); service.setJid(QStringLiteral("upload.shakespeare.lit")); QCOMPARE(service.jid(), QStringLiteral("upload.shakespeare.lit")); } QTEST_MAIN(tst_QXmppUploadRequestManager) #include "tst_qxmppuploadrequestmanager.moc"