aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJeremy Lainé <jeremy.laine@m4x.org>2010-10-11 11:00:35 +0000
committerJeremy Lainé <jeremy.laine@m4x.org>2010-10-11 11:00:35 +0000
commitbd6d9531fc0fdc0f7db22d6739f71b2eb995f202 (patch)
tree2fcfbcdd0c5f75e3d067dd6ffe5df593795fd87e /tests
parent064235d640fc0efb3c77c4d6e4198f05ab1e4122 (diff)
downloadqxmpp-bd6d9531fc0fdc0f7db22d6739f71b2eb995f202.tar.gz
add unit tests for STUN encoding / decoding
Diffstat (limited to 'tests')
-rw-r--r--tests/tests.cpp97
-rw-r--r--tests/tests.h13
2 files changed, 110 insertions, 0 deletions
diff --git a/tests/tests.cpp b/tests/tests.cpp
index 177d025e..b1d57255 100644
--- a/tests/tests.cpp
+++ b/tests/tests.cpp
@@ -40,6 +40,7 @@
#include "QXmppSaslAuth.h"
#include "QXmppSessionIq.h"
#include "QXmppStreamFeatures.h"
+#include "QXmppStun.h"
#include "QXmppUtils.h"
#include "QXmppVCardIq.h"
#include "QXmppVersionIq.h"
@@ -912,6 +913,99 @@ void TestPubSub::testSubscriptions()
serializePacket(iq, xml);
}
+void TestStun::testFingerprint()
+{
+ // without fingerprint
+ QXmppStunMessage msg;
+ msg.setType(0x0001);
+ QCOMPARE(msg.encode(QString(), false),
+ QByteArray("\x00\x01\x00\x00\x21\x12\xA4\x42\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 20));
+
+ // with fingerprint
+ QCOMPARE(msg.encode(QString(), true),
+ QByteArray("\x00\x01\x00\x08\x21\x12\xA4\x42\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x28\x00\x04\xB2\xAA\xF9\xF6", 28));
+}
+
+void TestStun::testIntegrity()
+{
+ QXmppStunMessage msg;
+ msg.setType(0x0001);
+ QCOMPARE(msg.encode(QString("somesecret"), false),
+ QByteArray("\x00\x01\x00\x18\x21\x12\xA4\x42\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x14\x96\x4B\x40\xD1\x84\x67\x6A\xFD\xB5\xE0\x7C\xC5\x1F\xFB\xBD\xA2\x61\xAF\xB1\x26", 44));
+}
+
+void TestStun::testIPv4Address()
+{
+ // encode
+ QXmppStunMessage msg;
+ msg.setType(0x0001);
+ msg.mappedHost = QHostAddress("127.0.0.1");
+ msg.mappedPort = 12345;
+ QByteArray packet = msg.encode(QString(), false);
+ QCOMPARE(packet,
+ QByteArray("\x00\x01\x00\x0C\x21\x12\xA4\x42\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x08\x00\x01\x30\x39\x7F\x00\x00\x01", 32));
+
+ // decode
+ QXmppStunMessage msg2;
+ msg2.decode(packet);
+ QCOMPARE(msg2.mappedHost, QHostAddress("127.0.0.1"));
+ QCOMPARE(msg2.mappedPort, quint16(12345));
+}
+
+void TestStun::testIPv6Address()
+{
+ // encode
+ QXmppStunMessage msg;
+ msg.setType(0x0001);
+ msg.mappedHost = QHostAddress("::1");
+ msg.mappedPort = 12345;
+ const QByteArray packet = msg.encode(QString(), false);
+ QCOMPARE(packet,
+ QByteArray("\x00\x01\x00\x18\x21\x12\xA4\x42\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x14\x00\x02\x30\x39\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 44));
+
+ // decode
+ QXmppStunMessage msg2;
+ msg2.decode(packet);
+ QCOMPARE(msg2.mappedHost, QHostAddress("::1"));
+ QCOMPARE(msg2.mappedPort, quint16(12345));
+}
+
+void TestStun::testXorIPv4Address()
+{
+ // encode
+ QXmppStunMessage msg;
+ msg.setType(0x0001);
+ msg.xorMappedHost = QHostAddress("127.0.0.1");
+ msg.xorMappedPort = 12345;
+ QByteArray packet = msg.encode(QString(), false);
+ QCOMPARE(packet,
+ QByteArray("\x00\x01\x00\x0C\x21\x12\xA4\x42\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x00\x08\x00\x01\x11\x2B\x5E\x12\xA4\x43", 32));
+
+ // decode
+ QXmppStunMessage msg2;
+ msg2.decode(packet);
+ QCOMPARE(msg2.xorMappedHost, QHostAddress("127.0.0.1"));
+ QCOMPARE(msg2.xorMappedPort, quint16(12345));
+}
+
+void TestStun::testXorIPv6Address()
+{
+ // encode
+ QXmppStunMessage msg;
+ msg.setType(0x0001);
+ msg.xorMappedHost = QHostAddress("::1");
+ msg.xorMappedPort = 12345;
+ const QByteArray packet = msg.encode(QString(), false);
+ QCOMPARE(packet,
+ QByteArray("\x00\x01\x00\x18\x21\x12\xA4\x42\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x00\x14\x00\x02\x11\x2B\x21\x12\xA4\x42\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 44));
+
+ // decode
+ QXmppStunMessage msg2;
+ msg2.decode(packet);
+ QCOMPARE(msg2.xorMappedHost, QHostAddress("::1"));
+ QCOMPARE(msg2.xorMappedPort, quint16(12345));
+}
+
static void checkVariant(const QVariant &value, const QByteArray &xml)
{
// serialise
@@ -1115,6 +1209,9 @@ int main(int argc, char *argv[])
TestPubSub testPubSub;
errors += QTest::qExec(&testPubSub);
+ TestStun testStun;
+ errors += QTest::qExec(&testStun);
+
TestXmlRpc testXmlRpc;
errors += QTest::qExec(&testXmlRpc);
diff --git a/tests/tests.h b/tests/tests.h
index ee7e8bdd..5be6b0af 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -90,6 +90,19 @@ private slots:
void testSubscriptions();
};
+class TestStun : public QObject
+{
+ Q_OBJECT
+
+private slots:
+ void testFingerprint();
+ void testIntegrity();
+ void testIPv4Address();
+ void testIPv6Address();
+ void testXorIPv4Address();
+ void testXorIPv6Address();
+};
+
class TestXmlRpc : public QObject
{
Q_OBJECT