-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
197 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,46 @@ | |
// | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
#include "QXmppE2eeExtension.h" | ||
#include "QXmppFutureUtils_p.h" | ||
#include "QXmppMamManager.h" | ||
#include "QXmppMessage.h" | ||
|
||
#include "TestClient.h" | ||
#include "util.h" | ||
|
||
#include <QObject> | ||
|
||
using namespace QXmpp::Private; | ||
|
||
class EncryptionExtension : public QXmppE2eeExtension | ||
{ | ||
public: | ||
QXmppTask<MessageEncryptResult> encryptMessage(QXmppMessage &&, const std::optional<QXmppSendStanzaParams> &) override | ||
{ | ||
return makeReadyTask<MessageEncryptResult>(QXmppError { "it's only a test", QXmpp::SendError::EncryptionError }); | ||
} | ||
QXmppTask<MessageDecryptResult> decryptMessage(QXmppMessage &&m) override | ||
{ | ||
m.setBody(m.e2eeFallbackBody()); | ||
m.setE2eeFallbackBody({}); | ||
return makeReadyTask<MessageDecryptResult>(std::move(m)); | ||
} | ||
|
||
QXmppTask<IqEncryptResult> encryptIq(QXmppIq &&, const std::optional<QXmppSendStanzaParams> &) override | ||
{ | ||
return makeReadyTask<IqEncryptResult>(QXmppError { "it's only a test", QXmpp::SendError::EncryptionError }); | ||
} | ||
|
||
QXmppTask<IqDecryptResult> decryptIq(const QDomElement &) override | ||
{ | ||
return makeReadyTask<IqDecryptResult>(QXmppError { "it's only a test", QXmpp::SendError::EncryptionError }); | ||
} | ||
|
||
bool isEncrypted(const QDomElement &e) override { return !e.firstChildElement("test-encrypted").isNull(); }; | ||
bool isEncrypted(const QXmppMessage &) override { return false; }; | ||
}; | ||
|
||
class QXmppMamTestHelper : public QObject | ||
{ | ||
Q_OBJECT | ||
|
@@ -40,6 +73,10 @@ class tst_QXmppMamManager : public QObject | |
Q_SLOT void testHandleResultIq_data(); | ||
Q_SLOT void testHandleResultIq(); | ||
|
||
// test for task-based API | ||
Q_SLOT void retrieveMessagesUnencrypted(); | ||
Q_SLOT void retrieveMessagesEncrypted(); | ||
|
||
QXmppMamTestHelper m_helper; | ||
QXmppMamManager m_manager; | ||
}; | ||
|
@@ -206,6 +243,122 @@ void tst_QXmppMamManager::testHandleResultIq() | |
QCOMPARE(m_helper.m_signalTriggered, accept); | ||
} | ||
|
||
void tst_QXmppMamManager::retrieveMessagesUnencrypted() | ||
{ | ||
TestClient test; | ||
auto *mam = test.addNewExtension<QXmppMamManager>(); | ||
auto task = mam->retrieveMessages("mam.server.org"); | ||
test.expect("<iq id='qxmpp1' to='mam.server.org' type='set'>" | ||
"<query xmlns='urn:xmpp:mam:2' queryid='qxmpp1'>" | ||
"<x xmlns='jabber:x:data' type='submit'>" | ||
"<field type='hidden' var='FORM_TYPE'><value>urn:xmpp:mam:2</value></field>" | ||
"</x>" | ||
"</query>" | ||
"</iq>"); | ||
mam->handleStanza(xmlToDom("<message id='aeb213' to='[email protected]/chamber' from='mam.server.org'>" | ||
"<result xmlns='urn:xmpp:mam:2' queryid='qxmpp1' id='28482-98726-73623'>" | ||
"<forwarded xmlns='urn:xmpp:forward:0'>" | ||
"<delay xmlns='urn:xmpp:delay' stamp='2010-07-10T23:08:25Z'/>" | ||
"<message xmlns='jabber:client'" | ||
" to='[email protected]/balcony'" | ||
" from='[email protected]/orchard'" | ||
" type='chat'>" | ||
"<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>" | ||
"</message>" | ||
"</forwarded>" | ||
"</result>" | ||
"</message>")); | ||
mam->handleStanza(xmlToDom("<message id='aeb214' to='[email protected]/chamber' from='mam.server.org'>" | ||
"<result xmlns='urn:xmpp:mam:2' queryid='qxmpp1' id='5d398-28273-f7382'>" | ||
"<forwarded xmlns='urn:xmpp:forward:0'>" | ||
"<delay xmlns='urn:xmpp:delay' stamp='2010-07-10T23:09:32Z'/>" | ||
"<message xmlns='jabber:client'" | ||
" to='[email protected]/orchard'" | ||
" from='[email protected]/balcony'" | ||
" type='chat' id='8a54s'>" | ||
"<body>What man art thou that thus bescreen'd in night so stumblest on my counsel?</body>" | ||
"</message>" | ||
"</forwarded>" | ||
"</result>" | ||
"</message>")); | ||
test.inject("<iq type='result' id='qxmpp1'>" | ||
"<fin xmlns='urn:xmpp:mam:2'>" | ||
"<set xmlns='http://jabber.org/protocol/rsm'>" | ||
"<first index='0'>28482-98726-73623</first>" | ||
"<last>09af3-cc343-b409f</last>" | ||
"</set>" | ||
"</fin>" | ||
"</iq>"); | ||
|
||
auto retrieved = expectFutureVariant<QXmppMamManager::RetrievedMessages>(task); | ||
QCOMPARE(retrieved.messages.size(), 2); | ||
QCOMPARE(retrieved.messages.at(0).body(), "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."); | ||
QCOMPARE(retrieved.messages.at(1).body(), "What man art thou that thus bescreen'd in night so stumblest on my counsel?"); | ||
QCOMPARE(retrieved.result.resultSetReply().first(), "28482-98726-73623"); | ||
} | ||
|
||
void tst_QXmppMamManager::retrieveMessagesEncrypted() | ||
{ | ||
TestClient test; | ||
// e2ee | ||
auto e2ee = std::make_unique<EncryptionExtension>(); | ||
test.setEncryptionExtension(e2ee.get()); | ||
// mam manager | ||
auto *mam = test.addNewExtension<QXmppMamManager>(); | ||
|
||
// start request | ||
auto task = mam->retrieveMessages("mam.server.org"); | ||
test.expect("<iq id='qxmpp1' to='mam.server.org' type='set'>" | ||
"<query xmlns='urn:xmpp:mam:2' queryid='qxmpp1'>" | ||
"<x xmlns='jabber:x:data' type='submit'>" | ||
"<field type='hidden' var='FORM_TYPE'><value>urn:xmpp:mam:2</value></field>" | ||
"</x>" | ||
"</query>" | ||
"</iq>"); | ||
mam->handleStanza(xmlToDom("<message id='aeb213' to='[email protected]/chamber' from='mam.server.org'>" | ||
"<result xmlns='urn:xmpp:mam:2' queryid='qxmpp1' id='28482-98726-73623'>" | ||
"<forwarded xmlns='urn:xmpp:forward:0'>" | ||
"<delay xmlns='urn:xmpp:delay' stamp='2010-07-10T23:08:25Z'/>" | ||
"<message xmlns='jabber:client'" | ||
" to='[email protected]/balcony'" | ||
" from='[email protected]/orchard'" | ||
" type='chat'>" | ||
"<test-encrypted/>" | ||
"<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>" | ||
"</message>" | ||
"</forwarded>" | ||
"</result>" | ||
"</message>")); | ||
mam->handleStanza(xmlToDom("<message id='aeb214' to='[email protected]/chamber' from='mam.server.org'>" | ||
"<result xmlns='urn:xmpp:mam:2' queryid='qxmpp1' id='5d398-28273-f7382'>" | ||
"<forwarded xmlns='urn:xmpp:forward:0'>" | ||
"<delay xmlns='urn:xmpp:delay' stamp='2010-07-10T23:09:32Z'/>" | ||
"<message xmlns='jabber:client'" | ||
" to='[email protected]/orchard'" | ||
" from='[email protected]/balcony'" | ||
" type='chat' id='8a54s'>" | ||
"<body>What man art thou that thus bescreen'd in night so stumblest on my counsel?</body>" | ||
"</message>" | ||
"</forwarded>" | ||
"</result>" | ||
"</message>")); | ||
test.inject("<iq type='result' id='qxmpp1'>" | ||
"<fin xmlns='urn:xmpp:mam:2'>" | ||
"<set xmlns='http://jabber.org/protocol/rsm'>" | ||
"<first index='0'>28482-98726-73623</first>" | ||
"<last>09af3-cc343-b409f</last>" | ||
"</set>" | ||
"</fin>" | ||
"</iq>"); | ||
|
||
// check results | ||
auto retrieved = expectFutureVariant<QXmppMamManager::RetrievedMessages>(task); | ||
QCOMPARE(retrieved.messages.size(), 2); | ||
QCOMPARE(retrieved.messages.at(0).body(), "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."); | ||
QCOMPARE(retrieved.messages.at(1).body(), "What man art thou that thus bescreen'd in night so stumblest on my counsel?"); | ||
QCOMPARE(retrieved.result.resultSetReply().first(), "28482-98726-73623"); | ||
} | ||
|
||
void QXmppMamTestHelper::archivedMessageReceived(const QString &queryId, const QXmppMessage &message) | ||
{ | ||
m_signalTriggered = true; | ||
|