From 4635f616d5912432ff9712ab714dac529e91b49a Mon Sep 17 00:00:00 2001 From: Jeremy Lainé Date: Mon, 19 Jul 2010 13:44:46 +0000 Subject: move tests.cpp --- example/tests/tests.cpp | 346 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 346 insertions(+) create mode 100644 example/tests/tests.cpp (limited to 'example/tests/tests.cpp') diff --git a/example/tests/tests.cpp b/example/tests/tests.cpp new file mode 100644 index 00000000..1f49b274 --- /dev/null +++ b/example/tests/tests.cpp @@ -0,0 +1,346 @@ +/* + * Copyright (C) 2010 Bolloré telecom + * + * Author: + * Jeremy Lainé + * + * Source: + * http://code.google.com/p/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 + +#include +#include +#include +#include + +#include "QXmppBind.h" +#include "QXmppJingleIq.h" +#include "QXmppMessage.h" +#include "QXmppPresence.h" +#include "QXmppSession.h" +#include "QXmppUtils.h" +#include "tests.h" + +void TestUtils::testHmac() +{ + QByteArray hmac = generateHmacMd5(QByteArray(16, 0x0b), QByteArray("Hi There")); + QCOMPARE(hmac, QByteArray::fromHex("9294727a3638bb1c13f48ef8158bfc9d")); + + hmac = generateHmacMd5(QByteArray("Jefe"), QByteArray("what do ya want for nothing?")); + QCOMPARE(hmac, QByteArray::fromHex("750c783e6ab0b503eaa86e310a5db738")); + + hmac = generateHmacMd5(QByteArray(16, 0xaa), QByteArray(50, 0xdd)); + QCOMPARE(hmac, QByteArray::fromHex("56be34521d144c88dbb8c733f0e8b3f6")); +} + +template +static void parsePacket(T &packet, const QByteArray &xml) +{ + //qDebug() << "parsing" << xml; + QDomDocument doc; + QCOMPARE(doc.setContent(xml, true), true); + QDomElement element = doc.documentElement(); + packet.parse(element); +} + +template +static void serializePacket(T &packet, const QByteArray &xml) +{ + QBuffer buffer; + buffer.open(QIODevice::ReadWrite); + QXmlStreamWriter writer(&buffer); + packet.toXml(&writer); + qDebug() << "expect " << xml; + qDebug() << "writing" << buffer.data(); + QCOMPARE(buffer.data(), xml); +} + +void TestPackets::testBindNoResource() +{ + const QByteArray xml( + "" + "" + ""); + + QXmppBind bind; + parsePacket(bind, xml); + QCOMPARE(bind.type(), QXmppIq::Set); + QCOMPARE(bind.id(), QString("bind_1")); + QCOMPARE(bind.jid(), QString()); + QCOMPARE(bind.resource(), QString()); + serializePacket(bind, xml); +} + +void TestPackets::testBindResource() +{ + const QByteArray xml( + "" + "" + "someresource" + "" + ""); + + QXmppBind bind; + parsePacket(bind, xml); + QCOMPARE(bind.type(), QXmppIq::Set); + QCOMPARE(bind.id(), QString("bind_2")); + QCOMPARE(bind.jid(), QString()); + QCOMPARE(bind.resource(), QString("someresource")); + serializePacket(bind, xml); +} + +void TestPackets::testBindResult() +{ + const QByteArray xml( + "" + "" + "somenode@example.com/someresource" + "" + ""); + + QXmppBind bind; + parsePacket(bind, xml); + QCOMPARE(bind.type(), QXmppIq::Result); + QCOMPARE(bind.id(), QString("bind_2")); + QCOMPARE(bind.jid(), QString("somenode@example.com/someresource")); + QCOMPARE(bind.resource(), QString()); + serializePacket(bind, xml); +} + +void TestPackets::testMessage() +{ + const QByteArray xml( + ""); + + QXmppMessage message; + parsePacket(message, xml); + QCOMPARE(message.to(), QString("foo@example.com/QXmpp")); + QCOMPARE(message.from(), QString("bar@example.com/QXmpp")); + serializePacket(message, xml); +} + +void TestPackets::testMessageFull() +{ + const QByteArray xml( + "" + "test subject" + "test body" + "test thread" + "" + ""); + + QXmppMessage message; + parsePacket(message, xml); + QCOMPARE(message.to(), QString("foo@example.com/QXmpp")); + QCOMPARE(message.from(), QString("bar@example.com/QXmpp")); + QCOMPARE(message.type(), QXmppMessage::Normal); + QCOMPARE(message.body(), QString("test body")); + QCOMPARE(message.subject(), QString("test subject")); + QCOMPARE(message.thread(), QString("test thread")); + QCOMPARE(message.state(), QXmppMessage::Composing); + serializePacket(message, xml); +} + +void TestPackets::testMessageDelay() +{ + const QByteArray xml( + "" + "" + ""); + + QXmppMessage message; + parsePacket(message, xml); + QCOMPARE(message.stamp(), QDateTime(QDate(2010, 06, 29), QTime(8, 23, 6), Qt::UTC)); + serializePacket(message, xml); +} + +void TestPackets::testMessageLegacyDelay() +{ + const QByteArray xml( + "" + "" + ""); + + QXmppMessage message; + parsePacket(message, xml); + QCOMPARE(message.stamp(), QDateTime(QDate(2010, 06, 29), QTime(8, 23, 6), Qt::UTC)); + serializePacket(message, xml); +} + +void TestPackets::testPresence() +{ + const QByteArray xml( + ""); + + QXmppPresence presence; + parsePacket(presence, xml); + QCOMPARE(presence.to(), QString("foo@example.com/QXmpp")); + QCOMPARE(presence.from(), QString("bar@example.com/QXmpp")); + serializePacket(presence, xml); +} + +void TestPackets::testPresenceFull() +{ + const QByteArray xml( + "" + "away" + "In a meeting" + "5" + ""); + + QXmppPresence presence; + parsePacket(presence, xml); + QCOMPARE(presence.to(), QString("foo@example.com/QXmpp")); + QCOMPARE(presence.from(), QString("bar@example.com/QXmpp")); + QCOMPARE(presence.status().type(), QXmppPresence::Status::Away); + QCOMPARE(presence.status().statusText(), QString("In a meeting")); + QCOMPARE(presence.status().priority(), 5); + serializePacket(presence, xml); +} + +void TestPackets::testSession() +{ + const QByteArray xml( + "" + "" + ""); + + QXmppSession session; + parsePacket(session, xml); + QCOMPARE(session.id(), QString("session_1")); + QCOMPARE(session.to(), QString("example.com")); + QCOMPARE(session.type(), QXmppIq::Set); + serializePacket(session, xml); +} + +void TestJingle::testSession() +{ + const QByteArray xml( + "" + "" + "" + "" + "" + "" + "" + ""); + + QXmppJingleIq session; + parsePacket(session, xml); + QCOMPARE(session.action(), QXmppJingleIq::SessionInitiate); + QCOMPARE(session.initiator(), QLatin1String("romeo@montague.lit/orchard")); + QCOMPARE(session.sid(), QLatin1String("a73sjjvkla37jfea")); + QCOMPARE(session.content().creator(), QLatin1String("initiator")); + QCOMPARE(session.content().name(), QLatin1String("this-is-a-stub")); + QCOMPARE(session.reason().text(), QString()); + QCOMPARE(session.reason().type(), QXmppJingleIq::Reason::None); + serializePacket(session, xml); +} + +void TestJingle::testTerminate() +{ + const QByteArray xml( + "" + "" + "" + "" + "" + "" + ""); + + QXmppJingleIq session; + parsePacket(session, xml); + QCOMPARE(session.action(), QXmppJingleIq::SessionTerminate); + QCOMPARE(session.initiator(), QString()); + QCOMPARE(session.sid(), QLatin1String("a73sjjvkla37jfea")); + QCOMPARE(session.reason().text(), QString()); + QCOMPARE(session.reason().type(), QXmppJingleIq::Reason::Success); + serializePacket(session, xml); +} + +void TestJingle::testPayloadType() +{ + const QByteArray xml(""); + QXmppJinglePayloadType payload; + parsePacket(payload, xml); + QCOMPARE(payload.id(), static_cast(103)); + QCOMPARE(payload.name(), QLatin1String("L16")); + QCOMPARE(payload.channels(), static_cast(2)); + QCOMPARE(payload.clockrate(), 16000u); + serializePacket(payload, xml); +} + +void TestJingle::testRinging() +{ + const QByteArray xml( + "" + "" + "" + "" + ""); + + QXmppJingleIq iq; + parsePacket(iq, xml); + QCOMPARE(iq.ringing(), true); + serializePacket(iq, xml); +} + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + + // run tests + int errors = 0; + + TestUtils testUtils; + errors += QTest::qExec(&testUtils); + + TestPackets testPackets; + errors += QTest::qExec(&testPackets); + + TestJingle testJingle; + errors += QTest::qExec(&testJingle); + + if (errors) + { + qWarning() << "Total failed tests:" << errors; + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +}; + -- cgit v1.2.3