From 0a584900db720454f06d136e6bdf445a8f87e8b7 Mon Sep 17 00:00:00 2001 From: Jeremy Lainé Date: Sat, 21 Jul 2012 22:16:24 +0200 Subject: split out RPC and STUN tests --- tests/tests.cpp | 282 +------------------------------------------------------- 1 file changed, 2 insertions(+), 280 deletions(-) (limited to 'tests/tests.cpp') diff --git a/tests/tests.cpp b/tests/tests.cpp index 652f95f3..c48acfbe 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -27,7 +27,6 @@ #include #include #include -#include #include #include "QXmppArchiveIq.h" @@ -37,11 +36,9 @@ #include "QXmppNonSASLAuth.h" #include "QXmppPasswordChecker.h" #include "QXmppPubSubIq.h" -#include "QXmppRpcIq.h" #include "QXmppSessionIq.h" #include "QXmppServer.h" #include "QXmppStreamFeatures.h" -#include "QXmppStun.h" #include "QXmppUtils.h" #include "QXmppVCardIq.h" #include "QXmppVersionIq.h" @@ -55,9 +52,11 @@ #include "message.h" #include "presence.h" #include "register.h" +#include "rpc.h" #include "rsm.h" #include "rtp.h" #include "sasl.h" +#include "stun.h" #include "tests.h" void TestUtils::testCrc32() @@ -910,283 +909,6 @@ void TestServer::testConnect() QCOMPARE(client.isConnected(), connected); } -void TestStun::testFingerprint() -{ - // without fingerprint - QXmppStunMessage msg; - msg.setType(0x0001); - QCOMPARE(msg.encode(QByteArray(), 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(QByteArray(), 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(QByteArray("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(QByteArray(), 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(QByteArray(), 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(QByteArray(), 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(QByteArray(), 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 - QBuffer buffer; - buffer.open(QIODevice::ReadWrite); - QXmlStreamWriter writer(&buffer); - QXmppRpcMarshaller::marshall(&writer, value); - qDebug() << "expect " << xml; - qDebug() << "writing" << buffer.data(); - QCOMPARE(buffer.data(), xml); - - // parse - QDomDocument doc; - QCOMPARE(doc.setContent(xml, true), true); - QDomElement element = doc.documentElement(); - QStringList errors; - QVariant test = QXmppRpcMarshaller::demarshall(element, errors); - if (!errors.isEmpty()) - qDebug() << errors; - QCOMPARE(errors, QStringList()); - QCOMPARE(test, value); -} - -void TestXmlRpc::testBase64() -{ - checkVariant(QByteArray("\0\1\2\3", 4), - QByteArray("AAECAw==")); -} - -void TestXmlRpc::testBool() -{ - checkVariant(false, - QByteArray("0")); - checkVariant(true, - QByteArray("1")); -} - -void TestXmlRpc::testDateTime() -{ - checkVariant(QDateTime(QDate(1998, 7, 17), QTime(14, 8, 55)), - QByteArray("1998-07-17T14:08:55")); -} - -void TestXmlRpc::testDouble() -{ - checkVariant(double(-12.214), - QByteArray("-12.214")); -} - -void TestXmlRpc::testInt() -{ - checkVariant(int(-12), - QByteArray("-12")); -} - -void TestXmlRpc::testNil() -{ - checkVariant(QVariant(), - QByteArray("")); -} - -void TestXmlRpc::testString() -{ - checkVariant(QString("hello world"), - QByteArray("hello world")); -} - -void TestXmlRpc::testArray() -{ - checkVariant(QVariantList() << QString("hello world") << double(-12.214), - QByteArray("" - "hello world" - "-12.214" - "")); -} - -void TestXmlRpc::testStruct() -{ - QMap map; - map["bar"] = QString("hello world"); - map["foo"] = double(-12.214); - checkVariant(map, - QByteArray("" - "" - "bar" - "hello world" - "" - "" - "foo" - "-12.214" - "" - "")); -} - -void TestXmlRpc::testInvoke() -{ - const QByteArray xml( - "" - "" - "" - "examples.getStateName" - "" - "" - "6" - "" - "" - "" - "" - ""); - - QXmppRpcInvokeIq iq; - parsePacket(iq, xml); - QCOMPARE(iq.method(), QLatin1String("examples.getStateName")); - QCOMPARE(iq.arguments(), QVariantList() << int(6)); - serializePacket(iq, xml); -} - -void TestXmlRpc::testResponse() -{ - const QByteArray xml( - "" - "" - "" - "" - "" - "Colorado" - "" - "" - "" - "" - ""); - - QXmppRpcResponseIq iq; - parsePacket(iq, xml); - QCOMPARE(iq.faultCode(), 0); - QCOMPARE(iq.faultString(), QString()); - QCOMPARE(iq.values(), QVariantList() << QString("Colorado")); - serializePacket(iq, xml); -} - -void TestXmlRpc::testResponseFault() -{ - const QByteArray xml( - "" - "" - "" - "" - "" - "" - "" - "faultCode" - "404" - "" - "" - "faultString" - "Not found" - "" - "" - "" - "" - "" - "" - ""); - - QXmppRpcResponseIq iq; - parsePacket(iq, xml); - QCOMPARE(iq.faultCode(), 404); - QCOMPARE(iq.faultString(), QLatin1String("Not found")); - QCOMPARE(iq.values(), QVariantList()); - serializePacket(iq, xml); -} - int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); -- cgit v1.2.3