/*
* Copyright (C) 2008-2012 The QXmpp developers
*
* Authors:
* Jeremy Lainé
* Manjeet Dahiya
*
* 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 "QXmppBindIq.h"
#include "QXmppDiscoveryIq.h"
#include "QXmppEntityTimeIq.h"
#include "QXmppNonSASLAuth.h"
#include "QXmppSessionIq.h"
#include "QXmppStreamFeatures.h"
#include "QXmppUtils.h"
#include "QXmppVersionIq.h"
#include "util.h"
class TestPackets : public QObject
{
Q_OBJECT
private slots:
void testBindNoResource();
void testBindResource();
void testBindResult();
void testDiscovery();
void testDiscoveryWithForm();
void testNonSaslAuth();
void testSession();
void testStreamFeatures();
void testVersionGet();
void testVersionResult();
void testEntityTimeGet();
void testEntityTimeResult();
};
void TestPackets::testBindNoResource()
{
const QByteArray xml(
""
""
"");
QXmppBindIq 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"
""
"");
QXmppBindIq 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"
""
"");
QXmppBindIq 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::testDiscovery()
{
const QByteArray xml(
""
""
""
""
""
""
""
""
"");
QXmppDiscoveryIq disco;
parsePacket(disco, xml);
QCOMPARE(disco.verificationString(), QByteArray::fromBase64("QgayPKawpkPSDYmwT/WM94uAlu0="));
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::testNonSaslAuth()
{
// Client Requests Authentication Fields from Server
const QByteArray xml1(
""
""
"");
QXmppNonSASLAuthIq iq1;
parsePacket(iq1, xml1);
serializePacket(iq1, xml1);
// Client Provides Required Information (Plaintext)
const QByteArray xml3(
""
""
"bill"
"Calli0pe"
"globe"
""
"");
QXmppNonSASLAuthIq iq3;
parsePacket(iq3, xml3);
QCOMPARE(iq3.username(), QLatin1String("bill"));
QCOMPARE(iq3.digest(), QByteArray());
QCOMPARE(iq3.password(), QLatin1String("Calli0pe"));
QCOMPARE(iq3.resource(), QLatin1String("globe"));
serializePacket(iq3, xml3);
// Client Provides Required Information (Plaintext)
const QByteArray xml4(
""
""
"bill"
"48fc78be9ec8f86d8ce1c39c320c97c21d62334d"
"globe"
""
"");
QXmppNonSASLAuthIq iq4;
parsePacket(iq4, xml4);
QCOMPARE(iq4.username(), QLatin1String("bill"));
QCOMPARE(iq4.digest(), QByteArray("\x48\xfc\x78\xbe\x9e\xc8\xf8\x6d\x8c\xe1\xc3\x9c\x32\x0c\x97\xc2\x1d\x62\x33\x4d"));
QCOMPARE(iq4.password(), QString());
QCOMPARE(iq4.resource(), QLatin1String("globe"));
serializePacket(iq4, xml4);
}
void TestPackets::testSession()
{
const QByteArray xml(
""
""
"");
QXmppSessionIq 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 TestPackets::testStreamFeatures()
{
const QByteArray xml("");
QXmppStreamFeatures features;
parsePacket(features, xml);
QCOMPARE(features.bindMode(), QXmppStreamFeatures::Disabled);
QCOMPARE(features.sessionMode(), QXmppStreamFeatures::Disabled);
QCOMPARE(features.nonSaslAuthMode(), QXmppStreamFeatures::Disabled);
QCOMPARE(features.tlsMode(), QXmppStreamFeatures::Disabled);
QCOMPARE(features.authMechanisms(), QStringList());
QCOMPARE(features.compressionMethods(), QStringList());
serializePacket(features, xml);
const QByteArray xml2(""
""
""
""
""
"zlib"
"PLAIN"
"");
QXmppStreamFeatures features2;
parsePacket(features2, xml2);
QCOMPARE(features2.bindMode(), QXmppStreamFeatures::Enabled);
QCOMPARE(features2.sessionMode(), QXmppStreamFeatures::Enabled);
QCOMPARE(features2.nonSaslAuthMode(), QXmppStreamFeatures::Enabled);
QCOMPARE(features2.tlsMode(), QXmppStreamFeatures::Enabled);
QCOMPARE(features2.authMechanisms(), QStringList() << "PLAIN");
QCOMPARE(features2.compressionMethods(), QStringList() << "zlib");
serializePacket(features2, xml2);
}
void TestPackets::testVersionGet()
{
const QByteArray xmlGet(
""
"");
QXmppVersionIq verIqGet;
parsePacket(verIqGet, xmlGet);
QCOMPARE(verIqGet.id(), QLatin1String("version_1"));
QCOMPARE(verIqGet.to(), QLatin1String("juliet@capulet.com/balcony"));
QCOMPARE(verIqGet.from(), QLatin1String("romeo@montague.net/orchard"));
QCOMPARE(verIqGet.type(), QXmppIq::Get);
serializePacket(verIqGet, xmlGet);
}
void TestPackets::testVersionResult()
{
const QByteArray xmlResult(
""
""
"qxmpp"
"Windows-XP"
"0.2.0"
"");
QXmppVersionIq verIqResult;
parsePacket(verIqResult, xmlResult);
QCOMPARE(verIqResult.id(), QLatin1String("version_1"));
QCOMPARE(verIqResult.to(), QLatin1String("romeo@montague.net/orchard"));
QCOMPARE(verIqResult.from(), QLatin1String("juliet@capulet.com/balcony"));
QCOMPARE(verIqResult.type(), QXmppIq::Result);
QCOMPARE(verIqResult.name(), QString("qxmpp"));
QCOMPARE(verIqResult.version(), QString("0.2.0"));
QCOMPARE(verIqResult.os(), QString("Windows-XP"));
serializePacket(verIqResult, xmlResult);
}
void TestPackets::testEntityTimeGet()
{
const QByteArray xml(""
"");
QXmppEntityTimeIq entityTime;
parsePacket(entityTime, xml);
QCOMPARE(entityTime.id(), QLatin1String("time_1"));
QCOMPARE(entityTime.to(), QLatin1String("juliet@capulet.com/balcony"));
QCOMPARE(entityTime.from(), QLatin1String("romeo@montague.net/orchard"));
QCOMPARE(entityTime.type(), QXmppIq::Get);
serializePacket(entityTime, xml);
}
void TestPackets::testEntityTimeResult()
{
const QByteArray xml(
""
""
"");
QXmppEntityTimeIq entityTime;
parsePacket(entityTime, xml);
QCOMPARE(entityTime.id(), QLatin1String("time_1"));
QCOMPARE(entityTime.from(), QLatin1String("juliet@capulet.com/balcony"));
QCOMPARE(entityTime.to(), QLatin1String("romeo@montague.net/orchard"));
QCOMPARE(entityTime.type(), QXmppIq::Result);
QCOMPARE(entityTime.tzo(), -21600);
QCOMPARE(entityTime.utc(), QDateTime(QDate(2006, 12, 19), QTime(17, 58, 35), Qt::UTC));
serializePacket(entityTime, xml);
}
QTEST_MAIN(TestPackets)
#include "tests.moc"