aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Jahn <lnj@kaidan.im>2021-10-17 17:19:48 +0200
committerLinus Jahn <lnj@kaidan.im>2021-10-18 19:23:21 +0200
commitba1e5dff3ad852573aadad2e378899f7d9b03355 (patch)
tree0e55993d20380cd1867a80343022b58d18e0ac2d
parente4970a79535dd5cfff2d010049cf39b644a87850 (diff)
downloadqxmpp-ba1e5dff3ad852573aadad2e378899f7d9b03355.tar.gz
Add parsing/serialization for SCE envelope
-rw-r--r--src/client/QXmppSceEnvelope_p.h128
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/qxmppsceenvelope/tst_qxmppsceenvelope.cpp92
3 files changed, 221 insertions, 0 deletions
diff --git a/src/client/QXmppSceEnvelope_p.h b/src/client/QXmppSceEnvelope_p.h
new file mode 100644
index 00000000..0c552821
--- /dev/null
+++ b/src/client/QXmppSceEnvelope_p.h
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2008-2021 The QXmpp developers
+ *
+ * Author:
+ * 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.
+ *
+ */
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the QXmpp API.
+//
+// This header file may change from version to version without notice,
+// or even be removed.
+//
+// We mean it.
+//
+
+#ifndef QXMPPSCEENVELOPE_P_H
+#define QXMPPSCEENVELOPE_P_H
+
+#include <QDomElement>
+#include <QDateTime>
+#include "QXmppUtils.h"
+
+class QXmppSceEnvelopeReader
+{
+public:
+ QXmppSceEnvelopeReader(const QDomElement &element)
+ : element(element)
+ {
+ }
+
+ inline QDomElement contentElement()
+ {
+ return element.firstChildElement(QStringLiteral("content"));
+ }
+ inline QString from()
+ {
+ return element.firstChildElement(QStringLiteral("from")).attribute(QStringLiteral("jid"));
+ }
+ inline QString to()
+ {
+ return element.firstChildElement(QStringLiteral("to")).attribute(QStringLiteral("jid"));
+ }
+ inline QDateTime timestamp()
+ {
+ return QXmppUtils::datetimeFromString(
+ element.firstChildElement(QStringLiteral("time")).attribute(QStringLiteral("stamp")));
+ }
+
+ // rpad is usually not needed (but can be parsed manually if really needed)
+
+private:
+ const QDomElement &element;
+};
+
+class QXmppSceEnvelopeWriter
+{
+public:
+ QXmppSceEnvelopeWriter(QXmlStreamWriter &writer)
+ : writer(writer)
+ {
+ }
+
+ inline void start()
+ {
+ writer.writeStartElement(QStringLiteral("envelope"));
+ writer.writeDefaultNamespace(QStringLiteral("urn:xmpp:sce:1"));
+ }
+ inline void end()
+ {
+ writer.writeEndElement();
+ }
+ template<typename Functor>
+ void writeContent(Functor writeContent)
+ {
+ writer.writeStartElement(QStringLiteral("content"));
+ writeContent();
+ writer.writeEndElement();
+ }
+ inline void writeFrom(const QString &jid)
+ {
+ writer.writeStartElement(QStringLiteral("from"));
+ writer.writeAttribute(QStringLiteral("jid"), jid);
+ writer.writeEndElement();
+ }
+ inline void writeTo(const QString &jid)
+ {
+ writer.writeStartElement(QStringLiteral("to"));
+ writer.writeAttribute(QStringLiteral("jid"), jid);
+ writer.writeEndElement();
+ }
+ inline void writeTimestamp(const QDateTime &timestamp)
+ {
+ writer.writeStartElement(QStringLiteral("time"));
+ writer.writeAttribute(QStringLiteral("stamp"), QXmppUtils::datetimeToString(timestamp));
+ writer.writeEndElement();
+ }
+ inline void writeRpad(const QString &value)
+ {
+ writer.writeStartElement(QStringLiteral("rpad"));
+ writer.writeCharacters(value);
+ writer.writeEndElement();
+ }
+
+private:
+ QXmlStreamWriter &writer;
+};
+
+#endif // QXMPPSCEENVELOPE_P_H
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index b01b691d..0cce4582 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -52,6 +52,7 @@ add_simple_test(qxmppresultset)
add_simple_test(qxmpprosteriq)
add_simple_test(qxmpprostermanager TestClient.h)
add_simple_test(qxmpprpciq)
+add_simple_test(qxmppsceenvelope)
add_simple_test(qxmppserver)
add_simple_test(qxmppsessioniq)
add_simple_test(qxmppsocks)
diff --git a/tests/qxmppsceenvelope/tst_qxmppsceenvelope.cpp b/tests/qxmppsceenvelope/tst_qxmppsceenvelope.cpp
new file mode 100644
index 00000000..69ec8981
--- /dev/null
+++ b/tests/qxmppsceenvelope/tst_qxmppsceenvelope.cpp
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2008-2021 The QXmpp developers
+ *
+ * Author:
+ * 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 "QXmppSceEnvelope_p.h"
+
+#include "util.h"
+#include <QObject>
+
+class tst_QXmppSceEnvelope : public QObject
+{
+ Q_OBJECT
+
+private:
+ Q_SLOT void testReader();
+ Q_SLOT void testWriter();
+};
+
+void tst_QXmppSceEnvelope::testReader()
+{
+ const auto xml = QStringLiteral(
+ "<envelope xmlns=\"urn:xmpp:sce:1\">"
+ "<content><body xmlns=\"jabber:client\">Hello</body><x xmlns=\"jabber:x:oob\"><url>https://en.wikipedia.org/wiki/Fight_Club#Plot</url></x></content>"
+ "<time stamp=\"2004-01-25T06:05:00+01:00\"/>"
+ "<to jid=\"missioncontrol@houston.nasa.gov\"/>"
+ "<from jid=\"opportunity@mars.planet\"/>"
+ "<rpad>C1DHN9HK-9A25tSmwK4hU!Jji9%GKYK^syIlHJT9TnI4</rpad>"
+ "</envelope>");
+ const auto dom = xmlToDom(xml);
+
+ QXmppSceEnvelopeReader reader(dom);
+ QCOMPARE(reader.from(), QStringLiteral("opportunity@mars.planet"));
+ QCOMPARE(reader.to(), QStringLiteral("missioncontrol@houston.nasa.gov"));
+ QCOMPARE(reader.timestamp(), QDateTime({2004, 01, 25}, {05, 05, 00}, Qt::UTC));
+ QCOMPARE(reader.contentElement().firstChildElement().tagName(), QStringLiteral("body"));
+}
+
+void tst_QXmppSceEnvelope::testWriter()
+{
+ const auto expectedXml = QStringLiteral(
+ "<envelope xmlns=\"urn:xmpp:sce:1\">"
+ "<content><body xmlns=\"jabber:client\">Hello</body><x xmlns=\"jabber:x:oob\"><url>https://en.wikipedia.org/wiki/Fight_Club#Plot</url></x></content>"
+ "<time stamp=\"2004-01-25T05:05:00Z\"/>"
+ "<to jid=\"missioncontrol@houston.nasa.gov\"/>"
+ "<from jid=\"opportunity@mars.planet\"/>"
+ "<rpad>C1DHN9HK-9A25tSmwK4hU!Jji9%GKYK^syIlHJT9TnI4</rpad>"
+ "</envelope>");
+
+ QString out;
+ QXmlStreamWriter writer(&out);
+ QXmppSceEnvelopeWriter envelope(writer);
+ envelope.start();
+ envelope.writeContent([&writer] {
+ writer.writeStartElement("body");
+ writer.writeDefaultNamespace("jabber:client");
+ writer.writeCharacters("Hello");
+ writer.writeEndElement();
+ writer.writeStartElement("x");
+ writer.writeDefaultNamespace("jabber:x:oob");
+ writer.writeTextElement("url", "https://en.wikipedia.org/wiki/Fight_Club#Plot");
+ writer.writeEndElement();
+ });
+ envelope.writeTimestamp(QDateTime({2004, 01, 25}, {05, 05, 00}, Qt::UTC));
+ envelope.writeTo("missioncontrol@houston.nasa.gov");
+ envelope.writeFrom("opportunity@mars.planet");
+ envelope.writeRpad("C1DHN9HK-9A25tSmwK4hU!Jji9%GKYK^syIlHJT9TnI4");
+ envelope.end();
+
+ QCOMPARE(out, expectedXml);
+}
+
+QTEST_MAIN(tst_QXmppSceEnvelope)
+#include "tst_qxmppsceenvelope.moc"