From a40682ebcdc4552b7185d4a6d0e9deb1dc5261cc Mon Sep 17 00:00:00 2001 From: Jeremy Lainé Date: Mon, 14 May 2012 11:20:09 +0200 Subject: implement/test XEP-0115 verification strings for dataforms (issue: #140) --- src/base/QXmppDiscoveryIq.cpp | 38 ++++++++++++++++++++++++++++++++++++++ tests/tests.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++- tests/tests.h | 3 ++- 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/base/QXmppDiscoveryIq.cpp b/src/base/QXmppDiscoveryIq.cpp index 05e407b3..228fd562 100644 --- a/src/base/QXmppDiscoveryIq.cpp +++ b/src/base/QXmppDiscoveryIq.cpp @@ -206,6 +206,33 @@ QByteArray QXmppDiscoveryIq::verificationString() const S += QString("%1/%2/%3/%4<").arg(identity.category(), identity.type(), identity.language(), identity.name()); foreach (const QString &feature, sortedFeatures) S += feature + QLatin1String("<"); + + if (!m_form.isNull()) { + QMap fieldMap; + foreach (const QXmppDataForm::Field &field, m_form.fields()) { + fieldMap.insert(field.key(), field); + } + + if (fieldMap.contains("FORM_TYPE")) { + const QXmppDataForm::Field field = fieldMap.take("FORM_TYPE"); + S += field.value().toString() + QLatin1String("<"); + + QStringList keys = fieldMap.keys(); + qSort(keys); + foreach (const QString &key, keys) { + const QXmppDataForm::Field field = fieldMap.value(key); + S += key + QLatin1String("<"); + if (field.value().canConvert()) + S += field.value().toStringList().join(QLatin1String("<")); + else + S += field.value().toString(); + S += QLatin1String("<"); + } + } else { + qWarning("QXmppDiscoveryIq form does not contain FORM_TYPE"); + } + } + QCryptographicHash hasher(QCryptographicHash::Sha1); hasher.addData(S.toUtf8()); return hasher.result(); @@ -241,6 +268,17 @@ void QXmppDiscoveryIq::parseElementFromChild(const QDomElement &element) identity.setCategory(itemElement.attribute("category")); identity.setName(itemElement.attribute("name")); identity.setType(itemElement.attribute("type")); + + // FIXME: for some reason the language does not found, + // so we are forced to use QDomNamedNodeMap + QDomNamedNodeMap m(itemElement.attributes()); + for (int i = 0; i < m.size(); ++i) { + if (m.item(i).nodeName() == "xml:lang") { + identity.setLanguage(m.item(i).nodeValue()); + break; + } + } + m_identities.append(identity); } else if (itemElement.tagName() == "item") diff --git a/tests/tests.cpp b/tests/tests.cpp index 5aea4132..29c0253d 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -334,7 +334,7 @@ void TestPackets::testBindResult() serializePacket(bind, xml); } -void TestPackets::testDiscoveryIq() +void TestPackets::testDiscovery() { const QByteArray xml( "" @@ -353,6 +353,47 @@ void TestPackets::testDiscoveryIq() serializePacket(disco, xml); } +void TestPackets::testDiscoveryWithForm() +{ + const QByteArray xml( + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "urn:xmpp:dataforms:softwareinfo" + "" + "" + "ipv4" + "ipv6" + "" + "" + "Mac" + "" + "" + "10.5.1" + "" + "" + "Psi" + "" + "" + "0.11" + "" + "" + "" + ""); + + QXmppDiscoveryIq disco; + parsePacket(disco, xml); + QCOMPARE(disco.verificationString(), QByteArray::fromBase64("q07IKJEyjvHSyhy//CH0CxmKi8w=")); + serializePacket(disco, xml); +} + void TestPackets::testMessage() { const QByteArray xml( diff --git a/tests/tests.h b/tests/tests.h index 0b91a11e..3816e6d9 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -50,7 +50,8 @@ private slots: void testBindNoResource(); void testBindResource(); void testBindResult(); - void testDiscoveryIq(); + void testDiscovery(); + void testDiscoveryWithForm(); void testMessage(); void testMessageFull(); void testMessageAttention(); -- cgit v1.2.3