diff options
| author | Linus Jahn <lnj@kaidan.im> | 2020-04-01 01:27:20 +0200 |
|---|---|---|
| committer | Linus Jahn <lnj@kaidan.im> | 2020-04-01 01:27:20 +0200 |
| commit | 03764c9abcd3c79873038935566f4e31a845ada9 (patch) | |
| tree | be9cfece301df46606ba6eef62dec3d60b5397b7 /tests | |
| parent | 553b95576ee5cc3e74a47a221bdc2627de739cfe (diff) | |
| parent | 3ab1d34eceb8776193fe1cdc2070264d7dfbad30 (diff) | |
| download | qxmpp-03764c9abcd3c79873038935566f4e31a845ada9.tar.gz | |
Merge branch 'stable'
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | tests/qxmppmessagereceiptmanager/tst_qxmppmessagereceiptmanager.cpp | 85 | ||||
| -rw-r--r-- | tests/qxmppregistrationmanager/tst_qxmppregistrationmanager.cpp | 38 | ||||
| -rw-r--r-- | tests/qxmpprostermanager/tst_qxmpprostermanager.cpp | 110 | ||||
| -rw-r--r-- | tests/qxmppvcardmanager/tst_qxmppvcardmanager.cpp | 108 | ||||
| -rw-r--r-- | tests/util.h | 14 |
6 files changed, 329 insertions, 28 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0f81c6e5..07e12ef1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -40,6 +40,7 @@ add_simple_test(qxmppregisteriq) add_simple_test(qxmppregistrationmanager) add_simple_test(qxmppresultset) add_simple_test(qxmpprosteriq) +add_simple_test(qxmpprostermanager) add_simple_test(qxmpprpciq) add_simple_test(qxmppserver) add_simple_test(qxmppsessioniq) @@ -49,6 +50,7 @@ add_simple_test(qxmppstarttlspacket) add_simple_test(qxmppstreamfeatures) add_simple_test(qxmppstunmessage) add_simple_test(qxmppvcardiq) +add_simple_test(qxmppvcardmanager) add_simple_test(qxmppversioniq) if(WITH_GSTREAMER) diff --git a/tests/qxmppmessagereceiptmanager/tst_qxmppmessagereceiptmanager.cpp b/tests/qxmppmessagereceiptmanager/tst_qxmppmessagereceiptmanager.cpp index 750628dc..0aff489c 100644 --- a/tests/qxmppmessagereceiptmanager/tst_qxmppmessagereceiptmanager.cpp +++ b/tests/qxmppmessagereceiptmanager/tst_qxmppmessagereceiptmanager.cpp @@ -41,15 +41,32 @@ private slots: { m_messageDelivered = true; } + void onLoggerMessage(QXmppLogger::MessageType type, const QString& text) + { + m_receiptSent = true; + } private: - QXmppMessageReceiptManager m_manager; + QXmppMessageReceiptManager* m_manager; + QXmppClient m_client; + QXmppLogger* m_logger; bool m_messageDelivered = false; + bool m_receiptSent = false; }; void tst_QXmppMessageReceiptManager::initTestCase() { - connect(&m_manager, &QXmppMessageReceiptManager::messageDelivered, + m_manager = new QXmppMessageReceiptManager(); + m_logger = new QXmppLogger(); + + m_client.addExtension(m_manager); + m_logger->setLoggingType(QXmppLogger::SignalLogging); + m_client.setLogger(m_logger); + + connect(m_logger, &QXmppLogger::message, + this, &tst_QXmppMessageReceiptManager::onLoggerMessage); + + connect(m_manager, &QXmppMessageReceiptManager::messageDelivered, this, &tst_QXmppMessageReceiptManager::handleMessageDelivered); } @@ -57,6 +74,8 @@ void tst_QXmppMessageReceiptManager::testReceipt_data() { QTest::addColumn<QByteArray>("xml"); QTest::addColumn<bool>("accept"); + QTest::addColumn<bool>("sent"); + QTest::addColumn<bool>("handled"); QTest::newRow("correct") << QByteArray( @@ -66,6 +85,8 @@ void tst_QXmppMessageReceiptManager::testReceipt_data() "type=\"normal\">" "<received xmlns=\"urn:xmpp:receipts\" id=\"richard2-4.1.247\"/>" "</message>") + << true + << false << true; QTest::newRow("from-to-equal") << QByteArray( @@ -75,22 +96,80 @@ void tst_QXmppMessageReceiptManager::testReceipt_data() "type=\"normal\">" "<received xmlns=\"urn:xmpp:receipts\" id=\"richard2-4.1.247\"/>" "</message>") + << false + << false + << true; + QTest::newRow("error") + << QByteArray( + "<message xml:lang=\"en\" " + "to=\"northumberland@shakespeare.lit/westminster\" " + "from=\"kingrichard@royalty.england.lit/throne\" " + "type=\"error\" id=\"bi29sg183b4v\" " + "> " + "<archived xmlns=\"urn:xmpp:mam:tmp\" by=\"kingrichard@royalty.england.lit\" id=\"1585254642941569\"/> " + "<stanza-id xmlns=\"urn:xmpp:sid:0\" by=\"kingrichard@royalty.england.lit\" id=\"1585254642941569\"/> " + "<delay xmlns=\"urn:xmpp:delay\" stamp=\"2020-03-26T20:30:41.678Z\"/> " + "<request xmlns=\"urn:xmpp:receipts\"/> " + "<error code=\"500\" type=\"wait\"> " + "<resource-constraint xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/> " + "<text xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\" xml:lang=\"en\">" + "Your contact offline message queue is full. The message has been discarded." + "</text>" + "</error>" + "<body>1</body> " + "</message>") + << false + << false + << false; + QTest::newRow("message with receipt request") + << QByteArray( + "<message xml:lang=\"en\" " + "to=\"northumberland@shakespeare.lit/westminster\" " + "from=\"kingrichard@royalty.england.lit/throne\" " + "type=\"chat\" id=\"bi29sg183b4v\" " + "> " + "<archived xmlns=\"urn:xmpp:mam:tmp\" by=\"kingrichard@royalty.england.lit\" id=\"1585254642941569\"/> " + "<stanza-id xmlns=\"urn:xmpp:sid:0\" by=\"kingrichard@royalty.england.lit\" id=\"1585254642941569\"/> " + "<request xmlns=\"urn:xmpp:receipts\"/> " + "<body>1</body> " + "</message>") + << false + << true + << false; + + QTest::newRow("message with no receipt request") + << QByteArray( + "<message xml:lang=\"en\" " + "to=\"northumberland@shakespeare.lit/westminster\" " + "from=\"kingrichard@royalty.england.lit/throne\" " + "type=\"chat\" id=\"bi29sg183b4v\" " + "> " + "<archived xmlns=\"urn:xmpp:mam:tmp\" by=\"kingrichard@royalty.england.lit\" id=\"1585254642941569\"/> " + "<stanza-id xmlns=\"urn:xmpp:sid:0\" by=\"kingrichard@royalty.england.lit\" id=\"1585254642941569\"/> " + "<body>1</body> " + "</message>") + << false + << false << false; } void tst_QXmppMessageReceiptManager::testReceipt() { m_messageDelivered = false; + m_receiptSent = false; QFETCH(QByteArray, xml); QFETCH(bool, accept); + QFETCH(bool, sent); + QFETCH(bool, handled); QDomDocument doc; QCOMPARE(doc.setContent(xml, true), true); QDomElement element = doc.documentElement(); - QVERIFY(m_manager.handleStanza(element)); + QCOMPARE(m_manager->handleStanza(element), handled); QCOMPARE(m_messageDelivered, accept); + QCOMPARE(m_receiptSent, sent); } QTEST_MAIN(tst_QXmppMessageReceiptManager) diff --git a/tests/qxmppregistrationmanager/tst_qxmppregistrationmanager.cpp b/tests/qxmppregistrationmanager/tst_qxmppregistrationmanager.cpp index 077b65d4..4eda6579 100644 --- a/tests/qxmppregistrationmanager/tst_qxmppregistrationmanager.cpp +++ b/tests/qxmppregistrationmanager/tst_qxmppregistrationmanager.cpp @@ -29,20 +29,6 @@ #include "util.h" -template<class T> -QDomElement writePacketToDom(T packet) -{ - QBuffer buffer; - buffer.open(QIODevice::ReadWrite); - QXmlStreamWriter writer(&buffer); - packet.toXml(&writer); - - QDomDocument doc; - doc.setContent(buffer.data(), true); - - return doc.documentElement(); -} - class tst_QXmppRegistrationManager : public QObject { Q_OBJECT @@ -350,17 +336,17 @@ void tst_QXmppRegistrationManager::testRegistrationResult() registrationRequestForm.setEmail(QStringLiteral("1234@example.org")); registrationRequestForm.setId(QStringLiteral("register1")); - bool signalCalled = false; + bool succeededSignalCalled = false; + bool failedSignalCalled = false; + QObject *context = new QObject(this); - if (isSuccess) { - connect(manager, &QXmppRegistrationManager::registrationSucceeded, context, [&]() { - signalCalled = true; - }); - } else { - connect(manager, &QXmppRegistrationManager::registrationFailed, context, [&](const QXmppStanza::Error &) { - signalCalled = true; - }); - } + + connect(manager, &QXmppRegistrationManager::registrationSucceeded, context, [&]() { + succeededSignalCalled = true; + }); + connect(manager, &QXmppRegistrationManager::registrationFailed, context, [&](const QXmppStanza::Error &) { + failedSignalCalled = true; + }); manager->setRegistrationFormToSend(registrationRequestForm); manager->sendCachedRegistrationForm(); @@ -370,7 +356,9 @@ void tst_QXmppRegistrationManager::testRegistrationResult() manager->handleStanza(writePacketToDom(serverResult)); - QVERIFY(signalCalled); + QCOMPARE(succeededSignalCalled, isSuccess); + QCOMPARE(failedSignalCalled, !isSuccess); + delete context; } diff --git a/tests/qxmpprostermanager/tst_qxmpprostermanager.cpp b/tests/qxmpprostermanager/tst_qxmpprostermanager.cpp new file mode 100644 index 00000000..f8a62aa7 --- /dev/null +++ b/tests/qxmpprostermanager/tst_qxmpprostermanager.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2008-2020 The QXmpp developers + * + * Authors: + * Jeremy Lainé + * Manjeet Dahiya + * + * 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 "QXmppRosterManager.h" + +#include "util.h" + +class tst_QXmppRosterManager : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase(); + + void testDiscoFeatures(); + void testRenameItem(); + +private: + QXmppClient client; + QXmppLogger logger; + QXmppRosterManager *manager; +}; + +void tst_QXmppRosterManager::initTestCase() +{ + logger.setLoggingType(QXmppLogger::SignalLogging); + client.setLogger(&logger); + + manager = client.findExtension<QXmppRosterManager>(); +} + +void tst_QXmppRosterManager::testDiscoFeatures() +{ + QCOMPARE(manager->discoveryFeatures(), QStringList()); +} + +void tst_QXmppRosterManager::testRenameItem() +{ + // used to clean up lambda signal connections + QObject context; + + auto createItem = [](const QString &jid, const QString &ask = {}) -> QXmppRosterIq::Item { + QXmppRosterIq::Item item; + item.setBareJid(jid); + item.setSubscriptionStatus(ask); + return item; + }; + + // fill roster with initial contacts to rename + QXmppRosterIq initialItems; + initialItems.setType(QXmppIq::Result); + initialItems.addItem(createItem("stpeter@jabber.org")); + initialItems.addItem(createItem("bob@qxmpp.org")); + + QVERIFY(manager->handleStanza(writePacketToDom(initialItems))); + + // set a subscription state for bob (the subscription state MUST NOT be + // sent when renaming an item, so we need to check that it's not) + QXmppRosterIq bobAsk; + bobAsk.setType(QXmppIq::Set); + bobAsk.addItem(createItem("bob@qxmpp.org", "subscribe")); + + QVERIFY(manager->handleStanza(writePacketToDom(bobAsk))); + QCOMPARE(manager->getRosterEntry("bob@qxmpp.org").subscriptionStatus(), "subscribe"); + + // rename bob + bool requestSent = false; + connect(&logger, &QXmppLogger::message, &context, [&](QXmppLogger::MessageType type, const QString &text) { + if (type == QXmppLogger::SentMessage) { + requestSent = true; + + QXmppRosterIq renameRequest; + parsePacket(renameRequest, text.toUtf8()); + QCOMPARE(renameRequest.items().size(), 1); + QCOMPARE(renameRequest.items().first().bareJid(), "bob@qxmpp.org"); + QCOMPARE(renameRequest.items().first().name(), "Bob"); + // check that subscription state ('ask') for bob is not included + QVERIFY(renameRequest.items().first().subscriptionStatus().isNull()); + } + }); + + manager->renameItem("bob@qxmpp.org", "Bob"); + QVERIFY(requestSent); +} + +QTEST_MAIN(tst_QXmppRosterManager) +#include "tst_qxmpprostermanager.moc" diff --git a/tests/qxmppvcardmanager/tst_qxmppvcardmanager.cpp b/tests/qxmppvcardmanager/tst_qxmppvcardmanager.cpp new file mode 100644 index 00000000..ec079e93 --- /dev/null +++ b/tests/qxmppvcardmanager/tst_qxmppvcardmanager.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2008-2020 The QXmpp developers + * + * Authors: + * Melvin Keskin + * 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 <QObject> +#include "QXmppClient.h" +#include "QXmppVCardIq.h" +#include "QXmppVCardManager.h" +#include "util.h" + +Q_DECLARE_METATYPE(QXmppVCardIq); + +class tst_QXmppVCardManager : public QObject +{ + Q_OBJECT + +private slots: + void testHandleStanza_data(); + void testHandleStanza(); + +private: + QXmppClient m_client; +}; + +void tst_QXmppVCardManager::testHandleStanza_data() +{ + QTest::addColumn<QXmppVCardIq>("expectedIq"); + QTest::addColumn<bool>("isClientVCard"); + +#define ROW(name, iq, clientVCard) \ + QTest::newRow(QT_STRINGIFY(name)) << iq << clientVCard + + QXmppVCardIq iq; + iq.setType(QXmppIq::Result); + iq.setTo("stpeter@jabber.org/roundabout"); + iq.setFullName("Jeremie Miller"); + + auto iqFromBare = iq; + iqFromBare.setFrom("stpeter@jabber.org"); + + auto iqFromFull = iq; + iqFromFull.setFrom("stpeter@jabber.org/roundabout"); + + ROW(client-vcard-from-empty, iq, true); + ROW(client-vcard-from-bare, iqFromBare, true); + ROW(client-vcard-from-full, iqFromFull, false); + +#undef ROW +} + +void tst_QXmppVCardManager::testHandleStanza() +{ + QFETCH(QXmppVCardIq, expectedIq); + QFETCH(bool, isClientVCard); + + // initialize new manager to clear internal values + QXmppVCardManager *manager = new QXmppVCardManager(); + m_client.addExtension(manager); + + // sets own jid internally + m_client.connectToServer("stpeter@jabber.org", {}); + m_client.disconnectFromServer(); + + bool vCardReceived = false; + bool clientVCardReceived = false; + + QObject context; + connect(manager, &QXmppVCardManager::vCardReceived, &context, [&](QXmppVCardIq iq) { + vCardReceived = true; + QCOMPARE(iq, expectedIq); + }); + connect(manager, &QXmppVCardManager::clientVCardReceived, &context, [&]() { + clientVCardReceived = true; + QCOMPARE(manager->clientVCard(), expectedIq); + }); + + bool accepted = manager->handleStanza(writePacketToDom(expectedIq)); + + QVERIFY(accepted); + QVERIFY(vCardReceived); + QCOMPARE(clientVCardReceived, isClientVCard); + + // clean up (client deletes manager) + m_client.removeExtension(manager); +} + +QTEST_MAIN(tst_QXmppVCardManager) +#include "tst_qxmppvcardmanager.moc" diff --git a/tests/util.h b/tests/util.h index 9d1cee90..1268e1ad 100644 --- a/tests/util.h +++ b/tests/util.h @@ -49,6 +49,20 @@ static void serializePacket(T &packet, const QByteArray &xml) QCOMPARE(buffer.data(), xml); } +template<class T> +QDomElement writePacketToDom(T packet) +{ + QBuffer buffer; + buffer.open(QIODevice::ReadWrite); + QXmlStreamWriter writer(&buffer); + packet.toXml(&writer); + + QDomDocument doc; + doc.setContent(buffer.data(), true); + + return doc.documentElement(); +} + class TestPasswordChecker : public QXmppPasswordChecker { public: |
