diff options
| author | Melvin Keskin <melvo@olomono.de> | 2021-06-30 17:38:22 +0200 |
|---|---|---|
| committer | Linus Jahn <lnj@kaidan.im> | 2021-07-06 22:35:53 +0200 |
| commit | 92427f63b3458fac76f64f2993db81d8c4c5d84c (patch) | |
| tree | 4b8aefb1fe5df7610f539331866f825bdfdda8ba | |
| parent | cf1cc5326e1526dbee697ed45200a2c05bc98e01 (diff) | |
| download | qxmpp-92427f63b3458fac76f64f2993db81d8c4c5d84c.tar.gz | |
Add QXmppTrustMessageKeyOwner
| -rw-r--r-- | src/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/base/QXmppConstants.cpp | 2 | ||||
| -rw-r--r-- | src/base/QXmppConstants_p.h | 2 | ||||
| -rw-r--r-- | src/base/QXmppTrustMessageKeyOwner.cpp | 178 | ||||
| -rw-r--r-- | src/base/QXmppTrustMessageKeyOwner.h | 66 | ||||
| -rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | tests/qxmpptrustmessages/tst_qxmpptrustmessages.cpp | 130 |
7 files changed, 381 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 695ac5c5..cee15612 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -60,6 +60,7 @@ set(INSTALL_HEADER_FILES base/QXmppStream.h base/QXmppStreamFeatures.h base/QXmppStun.h + base/QXmppTrustMessageKeyOwner.h base/QXmppUtils.h base/QXmppVCardIq.h base/QXmppVersionIq.h @@ -146,6 +147,7 @@ set(SOURCE_FILES base/QXmppStreamInitiationIq.cpp base/QXmppStreamManagement.cpp base/QXmppStun.cpp + base/QXmppTrustMessageKeyOwner.cpp base/QXmppUtils.cpp base/QXmppVCardIq.cpp base/QXmppVersionIq.cpp diff --git a/src/base/QXmppConstants.cpp b/src/base/QXmppConstants.cpp index 1df63f68..ff7a7de8 100644 --- a/src/base/QXmppConstants.cpp +++ b/src/base/QXmppConstants.cpp @@ -176,3 +176,5 @@ const char* ns_mix_presence = "urn:xmpp:presence:0"; const char* ns_mix_misc = "urn:xmpp:mix:misc:0"; // XEP-0428: Fallback Indication const char* ns_fallback_indication = "urn:xmpp:fallback:0"; +// XEP-0434: Trust Messages (TM) +const char* ns_tm = "urn:xmpp:tm:0"; diff --git a/src/base/QXmppConstants_p.h b/src/base/QXmppConstants_p.h index 3a95ce82..cdef36ee 100644 --- a/src/base/QXmppConstants_p.h +++ b/src/base/QXmppConstants_p.h @@ -188,5 +188,7 @@ extern const char* ns_mix_presence; extern const char* ns_mix_misc; // XEP-0428: Fallback Indication extern const char* ns_fallback_indication; +// XEP-0434: Trust Messages (TM) +extern const char* ns_tm; #endif // QXMPPCONSTANTS_H diff --git a/src/base/QXmppTrustMessageKeyOwner.cpp b/src/base/QXmppTrustMessageKeyOwner.cpp new file mode 100644 index 00000000..d2633cde --- /dev/null +++ b/src/base/QXmppTrustMessageKeyOwner.cpp @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2008-2021 The QXmpp developers + * + * Author: + * Melvin Keskin + * + * 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 "QXmppTrustMessageKeyOwner.h" + +#include "QXmppConstants_p.h" +#include "QXmppUtils.h" + +#include <QDomElement> + +/// +/// \class QXmppTrustMessageKeyOwner +/// +/// \brief The QXmppTrustMessageKeyOwner class represents a key owner of the +/// trust message as defined by \xep{0434, Trust Messages (TM)}. +/// +/// \since QXmpp 1.5 +/// + +class QXmppTrustMessageKeyOwnerPrivate : public QSharedData +{ +public: + QString jid; + QList<QString> trustedKeys; + QList<QString> distrustedKeys; +}; + +/// +/// Constructs a trust message key owner. +/// +QXmppTrustMessageKeyOwner::QXmppTrustMessageKeyOwner() + : d(new QXmppTrustMessageKeyOwnerPrivate) +{ +} + +/// +/// Constructs a copy of \a other. +/// +/// \param other +/// +QXmppTrustMessageKeyOwner::QXmppTrustMessageKeyOwner(const QXmppTrustMessageKeyOwner &other) = default; + +QXmppTrustMessageKeyOwner::~QXmppTrustMessageKeyOwner() = default; + +/// +/// Assigns \a other to this trust message key owner. +/// +/// \param other +/// +QXmppTrustMessageKeyOwner &QXmppTrustMessageKeyOwner::operator=(const QXmppTrustMessageKeyOwner &other) = default; + +/// +/// Returns the bare JID of the key owner. +/// +/// \return the key owner's bare JID +/// +QString QXmppTrustMessageKeyOwner::jid() const +{ + return d->jid; +} + +/// +/// Sets the bare JID of the key owner. +/// +/// If a full JID is passed, it is converted into a bare JID. +/// +/// \param jid key owner's bare JID +/// +void QXmppTrustMessageKeyOwner::setJid(const QString &jid) +{ + d->jid = QXmppUtils::jidToBareJid(jid); +} + +/// +/// Returns the IDs of the keys that are trusted. +/// +/// \return the IDs of trusted keys +/// +QList<QString> QXmppTrustMessageKeyOwner::trustedKeys() const +{ + return d->trustedKeys; +} + +/// +/// Sets the IDs of keys that are trusted. +/// +/// \param keyIds IDs of trusted keys +/// +void QXmppTrustMessageKeyOwner::setTrustedKeys(const QList<QString> &keyIds) +{ + d->trustedKeys = keyIds; +} + +/// +/// Returns the IDs of the keys that are distrusted. +/// +/// \return the IDs of distrusted keys +/// +QList<QString> QXmppTrustMessageKeyOwner::distrustedKeys() const +{ + return d->distrustedKeys; +} + +/// +/// Sets the IDs of keys that are distrusted. +/// +/// \param keyIds IDs of distrusted keys +/// +void QXmppTrustMessageKeyOwner::setDistrustedKeys(const QList<QString> &keyIds) +{ + d->distrustedKeys = keyIds; +} + +/// \cond +void QXmppTrustMessageKeyOwner::parse(const QDomElement &element) +{ + d->jid = element.attribute("jid"); + + for (auto childElement = element.firstChildElement(); + !childElement.isNull(); + childElement = childElement.nextSiblingElement()) { + if (childElement.tagName() == "trust") { + d->trustedKeys.append(childElement.text()); + } else if (childElement.tagName() == "distrust") { + d->distrustedKeys.append(childElement.text()); + } + } +} + +void QXmppTrustMessageKeyOwner::toXml(QXmlStreamWriter *writer) const +{ + writer->writeStartElement("key-owner"); + writer->writeAttribute("jid", d->jid); + + for (const auto &keyIdentifier : d->trustedKeys) { + writer->writeTextElement("trust", keyIdentifier); + } + + for (const auto &keyIdentifier : d->distrustedKeys) { + writer->writeTextElement("distrust", keyIdentifier); + } + + writer->writeEndElement(); +} +/// \endcond + +/// +/// Determines whether the given DOM element is a trust message key owner. +/// +/// \param element DOM element being checked +/// +/// \return true if element is a trust message key owner, otherwise false +/// +bool QXmppTrustMessageKeyOwner::isTrustMessageKeyOwner(const QDomElement &element) +{ + return element.tagName() == QStringLiteral("key-owner") && + element.namespaceURI() == ns_tm; +} diff --git a/src/base/QXmppTrustMessageKeyOwner.h b/src/base/QXmppTrustMessageKeyOwner.h new file mode 100644 index 00000000..c4350a4b --- /dev/null +++ b/src/base/QXmppTrustMessageKeyOwner.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2008-2021 The QXmpp developers + * + * Author: + * Melvin Keskin + * + * 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. + * + */ + +#ifndef QXMPPTRUSTMESSAGEKEYOWNER_H +#define QXMPPTRUSTMESSAGEKEYOWNER_H + +#include "QXmppGlobal.h" + +#include <QDomElement> +#include <QSharedDataPointer> +#include <QXmlStreamWriter> + +class QXmppTrustMessageKeyOwnerPrivate; + +class QXMPP_EXPORT QXmppTrustMessageKeyOwner +{ +public: + QXmppTrustMessageKeyOwner(); + QXmppTrustMessageKeyOwner(const QXmppTrustMessageKeyOwner &other); + ~QXmppTrustMessageKeyOwner(); + + QXmppTrustMessageKeyOwner &operator=(const QXmppTrustMessageKeyOwner &other); + + QString jid() const; + void setJid(const QString &jid); + + QList<QString> trustedKeys() const; + void setTrustedKeys(const QList<QString> &keyIds); + + QList<QString> distrustedKeys() const; + void setDistrustedKeys(const QList<QString> &keyIds); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + + static bool isTrustMessageKeyOwner(const QDomElement &element); + +private: + QSharedDataPointer<QXmppTrustMessageKeyOwnerPrivate> d; +}; + +Q_DECLARE_TYPEINFO(QXmppTrustMessageKeyOwner, Q_MOVABLE_TYPE); + +#endif // QXMPPTRUSTMESSAGEKEYOWNER_H diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3e61c7f5..56273185 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -54,6 +54,7 @@ add_simple_test(qxmppstarttlspacket) add_simple_test(qxmppstream) add_simple_test(qxmppstreamfeatures) add_simple_test(qxmppstunmessage) +add_simple_test(qxmpptrustmessages) add_simple_test(qxmppvcardiq) add_simple_test(qxmppvcardmanager) add_simple_test(qxmppversioniq) diff --git a/tests/qxmpptrustmessages/tst_qxmpptrustmessages.cpp b/tests/qxmpptrustmessages/tst_qxmpptrustmessages.cpp new file mode 100644 index 00000000..2eff6c1c --- /dev/null +++ b/tests/qxmpptrustmessages/tst_qxmpptrustmessages.cpp @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2008-2021 The QXmpp developers + * + * Authors: + * Melvin Keskin + * + * 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 "QXmppTrustMessageElement.h" +#include "QXmppTrustMessageKeyOwner.h" + +#include "util.h" +#include <QObject> + +class tst_QXmppTrustMessages : public QObject +{ + Q_OBJECT + +private slots: + void testIsTrustMessageKeyOwner_data(); + void testIsTrustMessageKeyOwner(); + void testTrustMessageKeyOwner_data(); + void testTrustMessageKeyOwner(); +}; + +void tst_QXmppTrustMessages::testIsTrustMessageKeyOwner_data() +{ + QTest::addColumn<QByteArray>("xml"); + QTest::addColumn<bool>("isValid"); + + QTest::newRow("valid") + << QByteArrayLiteral("<key-owner xmlns=\"urn:xmpp:tm:0\"/>") + << true; + QTest::newRow("invalidTag") + << QByteArrayLiteral("<invalid xmlns=\"urn:xmpp:tm:0\"/>") + << false; + QTest::newRow("invalidNamespace") + << QByteArrayLiteral("<key-owner xmlns=\"invalid\"/>") + << false; +} + +void tst_QXmppTrustMessages::testIsTrustMessageKeyOwner() +{ + QFETCH(QByteArray, xml); + QFETCH(bool, isValid); + + QCOMPARE(QXmppTrustMessageKeyOwner::isTrustMessageKeyOwner(xmlToDom(xml)), isValid); +} + +void tst_QXmppTrustMessages::testTrustMessageKeyOwner_data() +{ + QTest::addColumn<QByteArray>("xml"); + QTest::addColumn<QString>("keyOwnerJid"); + QTest::addColumn<QList<QString>>("trustedKeys"); + QTest::addColumn<QList<QString>>("distrustedKeys"); + + QTest::newRow("trustedKeys") + << QByteArrayLiteral( + "<key-owner jid=\"alice@example.org\">" + "<trust>6850019d7ed0feb6d3823072498ceb4f616c6025586f8f666dc6b9c81ef7e0a4</trust>" + "<trust>221a4f8e228b72182b006e5ca527d3bddccf8d9e6feaf4ce96e1c451e8648020</trust>" + "</key-owner>") + << QStringLiteral("alice@example.org") + << QList<QString>({ QStringLiteral("6850019d7ed0feb6d3823072498ceb4f616c6025586f8f666dc6b9c81ef7e0a4"), + QStringLiteral("221a4f8e228b72182b006e5ca527d3bddccf8d9e6feaf4ce96e1c451e8648020") }) + << QList<QString>(); + QTest::newRow("distrustedKeys") + << QByteArrayLiteral( + "<key-owner jid=\"bob@example.com\">" + "<distrust>b423f5088de9a924d51b31581723d850c7cc67d0a4fe6b267c3d301ff56d2413</distrust>" + "<distrust>d9f849b6b828309c5f2c8df4f38fd891887da5aaa24a22c50d52f69b4a80817e</distrust>" + "</key-owner>") + << QStringLiteral("bob@example.com") + << QList<QString>() + << QList<QString>({ QStringLiteral("b423f5088de9a924d51b31581723d850c7cc67d0a4fe6b267c3d301ff56d2413"), + QStringLiteral("d9f849b6b828309c5f2c8df4f38fd891887da5aaa24a22c50d52f69b4a80817e") }); + QTest::newRow("trustedAndDistrustedKeys") + << QByteArrayLiteral( + "<key-owner jid=\"bob@example.com\">" + "<trust>623548d3835c6d33ef5cb680f7944ef381cf712bf23a0119dabe5c4f252cd02f</trust>" + "<distrust>b423f5088de9a924d51b31581723d850c7cc67d0a4fe6b267c3d301ff56d2413</distrust>" + "<distrust>d9f849b6b828309c5f2c8df4f38fd891887da5aaa24a22c50d52f69b4a80817e</distrust>" + "</key-owner>") + << QStringLiteral("bob@example.com") + << QList<QString>({ QStringLiteral("623548d3835c6d33ef5cb680f7944ef381cf712bf23a0119dabe5c4f252cd02f") }) + << QList<QString>({ QStringLiteral("b423f5088de9a924d51b31581723d850c7cc67d0a4fe6b267c3d301ff56d2413"), + QStringLiteral("d9f849b6b828309c5f2c8df4f38fd891887da5aaa24a22c50d52f69b4a80817e") }); +} + +void tst_QXmppTrustMessages::testTrustMessageKeyOwner() +{ + QFETCH(QByteArray, xml); + QFETCH(QString, keyOwnerJid); + QFETCH(QList<QString>, trustedKeys); + QFETCH(QList<QString>, distrustedKeys); + + QXmppTrustMessageKeyOwner keyOwner1; + parsePacket(keyOwner1, xml); + QCOMPARE(keyOwner1.jid(), keyOwnerJid); + QCOMPARE(keyOwner1.trustedKeys(), trustedKeys); + QCOMPARE(keyOwner1.distrustedKeys(), distrustedKeys); + serializePacket(keyOwner1, xml); + + QXmppTrustMessageKeyOwner keyOwner2; + keyOwner2.setJid(keyOwnerJid); + keyOwner2.setTrustedKeys(trustedKeys); + keyOwner2.setDistrustedKeys(distrustedKeys); + QCOMPARE(keyOwner2.jid(), keyOwnerJid); + QCOMPARE(keyOwner2.trustedKeys(), trustedKeys); + QCOMPARE(keyOwner2.distrustedKeys(), distrustedKeys); + serializePacket(keyOwner2, xml); +} + +QTEST_MAIN(tst_QXmppTrustMessages) +#include "tst_qxmpptrustmessages.moc" |
