From 6d01e3e4d7b07b7dfb11e76af1bd0585153d12af Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Sat, 18 Sep 2021 19:33:05 +0200 Subject: tests: Add minimal unit test for SCE hooks --- tests/qxmppclient/tst_qxmppclient.cpp | 72 +++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'tests/qxmppclient/tst_qxmppclient.cpp') diff --git a/tests/qxmppclient/tst_qxmppclient.cpp b/tests/qxmppclient/tst_qxmppclient.cpp index be28cfc2..45b4bfab 100644 --- a/tests/qxmppclient/tst_qxmppclient.cpp +++ b/tests/qxmppclient/tst_qxmppclient.cpp @@ -23,6 +23,8 @@ */ #include "QXmppClient.h" +#include "QXmppE2eeExtension.h" +#include "QXmppFutureUtils_p.h" #include "QXmppLogger.h" #include "QXmppMessage.h" #include "QXmppRosterManager.h" @@ -32,6 +34,8 @@ #include "util.h" #include +using namespace QXmpp::Private; + class tst_QXmppClient : public QObject { Q_OBJECT @@ -44,6 +48,8 @@ private slots: void testIndexOfExtension(); + void testE2eeEncryption(); + private: QXmppClient *client; }; @@ -104,5 +110,71 @@ void tst_QXmppClient::testIndexOfExtension() QCOMPARE(client->indexOfExtension(), 1); } +class EncryptionExtension : public QXmppE2eeExtension +{ +public: + bool messageCalled = false; + bool iqCalled = false; + + QFuture encryptMessage(QXmppMessage &&) override + { + messageCalled = true; + return makeReadyFuture(QXmpp::SendError { "it's only a test", QXmpp::SendError::EncryptionError }); + } + + QFuture encryptIq(QXmppIq &&) override + { + iqCalled = true; + return makeReadyFuture(QXmpp::SendError { "it's only a test", QXmpp::SendError::EncryptionError }); + } + + QFuture decryptIq(const QDomElement &) override + { + return makeReadyFuture(QXmpp::SendError { "it's only a test", QXmpp::SendError::EncryptionError }); + } +}; + +void tst_QXmppClient::testE2eeEncryption() +{ + QXmppClient client; + EncryptionExtension encrypter; + client.setEncryptionExtension(&encrypter); + + auto result = client.send(QXmppMessage("me@qxmpp.org", "somebody@qxmpp.org", "Hello")); + QVERIFY(encrypter.messageCalled); + QVERIFY(!encrypter.iqCalled); + QCoreApplication::processEvents(); + expectFutureVariant(result); + + encrypter.messageCalled = false; + result = client.send(QXmppPresence(QXmppPresence::Available)); + QVERIFY(!encrypter.messageCalled); + QVERIFY(!encrypter.iqCalled); + + auto createRequest = []() { + QXmppDiscoveryIq request; + request.setType(QXmppIq::Get); + request.setQueryType(QXmppDiscoveryIq::InfoQuery); + request.setTo("component.qxmpp.org"); + return request; + }; + + client.send(createRequest()); + QVERIFY(encrypter.iqCalled); + encrypter.iqCalled = false; + + client.sendUnencrypted(createRequest()); + QVERIFY(!encrypter.iqCalled); + encrypter.iqCalled = false; + + client.sendIq(createRequest()); + QVERIFY(!encrypter.iqCalled); + encrypter.iqCalled = false; + + client.sendSensitiveIq(createRequest()); + QVERIFY(encrypter.iqCalled); + encrypter.iqCalled = false; +} + QTEST_MAIN(tst_QXmppClient) #include "tst_qxmppclient.moc" -- cgit v1.2.3