/* * 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 #include #include #include #include #include "QXmppArchiveIq.h" #include "QXmppBindIq.h" #include "QXmppClient.h" #include "QXmppDiscoveryIq.h" #include "QXmppNonSASLAuth.h" #include "QXmppPasswordChecker.h" #include "QXmppPubSubIq.h" #include "QXmppSessionIq.h" #include "QXmppServer.h" #include "QXmppStreamFeatures.h" #include "QXmppUtils.h" #include "QXmppVersionIq.h" #include "QXmppEntityTimeIq.h" #include "tests.h" #include "util.h" void TestPackets::testArchiveList_data() { QTest::addColumn("xml"); QTest::addColumn("max"); QTest::newRow("no rsm") << QByteArray( "" "" "") << -1; QTest::newRow("with rsm") << QByteArray( "" "" "" "30" "" "" "") << 30; } void TestPackets::testArchiveList() { QFETCH(QByteArray, xml); QFETCH(int, max); QXmppArchiveListIq iq; parsePacket(iq, xml); QCOMPARE(iq.type(), QXmppIq::Get); QCOMPARE(iq.id(), QLatin1String("list_1")); QCOMPARE(iq.with(), QLatin1String("juliet@capulet.com")); QCOMPARE(iq.start(), QDateTime(QDate(1469, 7, 21), QTime(2, 0, 0), Qt::UTC)); QCOMPARE(iq.end(), QDateTime(QDate(1479, 7, 21), QTime(4, 0, 0), Qt::UTC)); QCOMPARE(iq.resultSetQuery().max(), max); serializePacket(iq, xml); } void TestPackets::testArchiveChat_data() { QTest::addColumn("xml"); QTest::addColumn("count"); QTest::newRow("no rsm") << QByteArray( "" "" "Art thou not Romeo, and a Montague?" "Neither, fair saint, if either thee dislike." "How cam'st thou hither, tell me, and wherefore?" "" "") << -1; QTest::newRow("with rsm") << QByteArray( "" "" "Art thou not Romeo, and a Montague?" "Neither, fair saint, if either thee dislike." "How cam'st thou hither, tell me, and wherefore?" "" "3" "" "" "") << 3; } void TestPackets::testArchiveChat() { QFETCH(QByteArray, xml); QFETCH(int, count); QXmppArchiveChatIq iq; parsePacket(iq, xml); QCOMPARE(iq.type(), QXmppIq::Result); QCOMPARE(iq.id(), QLatin1String("chat_1")); QCOMPARE(iq.chat().with(), QLatin1String("juliet@capulet.com")); QCOMPARE(iq.chat().messages().size(), 3); QCOMPARE(iq.chat().messages()[0].isReceived(), true); QCOMPARE(iq.chat().messages()[0].body(), QLatin1String("Art thou not Romeo, and a Montague?")); QCOMPARE(iq.chat().messages()[0].date(), QDateTime(QDate(1469, 7, 21), QTime(2, 56, 15), Qt::UTC)); QCOMPARE(iq.chat().messages()[1].isReceived(), false); QCOMPARE(iq.chat().messages()[1].date(), QDateTime(QDate(1469, 7, 21), QTime(2, 56, 26), Qt::UTC)); QCOMPARE(iq.chat().messages()[1].body(), QLatin1String("Neither, fair saint, if either thee dislike.")); QCOMPARE(iq.chat().messages()[2].isReceived(), true); QCOMPARE(iq.chat().messages()[2].date(), QDateTime(QDate(1469, 7, 21), QTime(2, 56, 33), Qt::UTC)); QCOMPARE(iq.chat().messages()[2].body(), QLatin1String("How cam'st thou hither, tell me, and wherefore?")); QCOMPARE(iq.resultSetReply().count(), count); serializePacket(iq, xml); } void TestPackets::testArchiveRemove() { const QByteArray xml( "" "" ""); QXmppArchiveRemoveIq iq; parsePacket(iq, xml); QCOMPARE(iq.type(), QXmppIq::Set); QCOMPARE(iq.id(), QLatin1String("remove_1")); QCOMPARE(iq.with(), QLatin1String("juliet@capulet.com")); QCOMPARE(iq.start(), QDateTime(QDate(1469, 7, 21), QTime(2, 0, 0), Qt::UTC)); QCOMPARE(iq.end(), QDateTime(QDate(1479, 7, 21), QTime(4, 0, 0), Qt::UTC)); serializePacket(iq, xml); } void TestPackets::testArchiveRetrieve_data() { QTest::addColumn("xml"); QTest::addColumn("max"); QTest::newRow("no rsm") << QByteArray( "" "" "") << -1; QTest::newRow("with rsm") << QByteArray( "" "" "" "30" "" "" "") << 30; } void TestPackets::testArchiveRetrieve() { QFETCH(QByteArray, xml); QFETCH(int, max); QXmppArchiveRetrieveIq iq; parsePacket(iq, xml); QCOMPARE(iq.type(), QXmppIq::Get); QCOMPARE(iq.id(), QLatin1String("retrieve_1")); QCOMPARE(iq.with(), QLatin1String("juliet@capulet.com")); QCOMPARE(iq.start(), QDateTime(QDate(1469, 7, 21), QTime(2, 0, 0), Qt::UTC)); QCOMPARE(iq.resultSetQuery().max(), max); serializePacket(iq, xml); } 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); } void TestPubSub::testItems() { const QByteArray xml( "" "" "" "" ""); QXmppPubSubIq iq; parsePacket(iq, xml); QCOMPARE(iq.id(), QLatin1String("items1")); QCOMPARE(iq.to(), QLatin1String("pubsub.shakespeare.lit")); QCOMPARE(iq.from(), QLatin1String("francisco@denmark.lit/barracks")); QCOMPARE(iq.type(), QXmppIq::Get); QCOMPARE(iq.queryType(), QXmppPubSubIq::ItemsQuery); QCOMPARE(iq.queryJid(), QString()); QCOMPARE(iq.queryNode(), QLatin1String("storage:bookmarks")); serializePacket(iq, xml); } void TestPubSub::testItemsResponse() { const QByteArray xml( "" "" "" "" "" "" "JC" "" "" "" "" "" ""); QXmppPubSubIq iq; parsePacket(iq, xml); QCOMPARE(iq.id(), QLatin1String("items1")); QCOMPARE(iq.to(), QLatin1String("francisco@denmark.lit/barracks")); QCOMPARE(iq.from(), QLatin1String("pubsub.shakespeare.lit")); QCOMPARE(iq.type(), QXmppIq::Result); QCOMPARE(iq.queryType(), QXmppPubSubIq::ItemsQuery); QCOMPARE(iq.queryJid(), QString()); QCOMPARE(iq.queryNode(), QLatin1String("storage:bookmarks")); serializePacket(iq, xml); } void TestPubSub::testPublish() { const QByteArray xml( "" "" "" "" "" "" "JC" "" "" "" "" "" ""); QXmppPubSubIq iq; parsePacket(iq, xml); QCOMPARE(iq.id(), QLatin1String("items1")); QCOMPARE(iq.to(), QLatin1String("pubsub.shakespeare.lit")); QCOMPARE(iq.from(), QLatin1String("francisco@denmark.lit/barracks")); QCOMPARE(iq.type(), QXmppIq::Result); QCOMPARE(iq.queryType(), QXmppPubSubIq::PublishQuery); QCOMPARE(iq.queryJid(), QString()); QCOMPARE(iq.queryNode(), QLatin1String("storage:bookmarks")); serializePacket(iq, xml); } void TestPubSub::testSubscribe() { const QByteArray xml( "" "" "" "" ""); QXmppPubSubIq iq; parsePacket(iq, xml); QCOMPARE(iq.id(), QLatin1String("sub1")); QCOMPARE(iq.to(), QLatin1String("pubsub.shakespeare.lit")); QCOMPARE(iq.from(), QLatin1String("francisco@denmark.lit/barracks")); QCOMPARE(iq.type(), QXmppIq::Set); QCOMPARE(iq.queryType(), QXmppPubSubIq::SubscribeQuery); QCOMPARE(iq.queryJid(), QLatin1String("francisco@denmark.lit")); QCOMPARE(iq.queryNode(), QLatin1String("princely_musings")); serializePacket(iq, xml); } void TestPubSub::testSubscription() { const QByteArray xml( "" "" "" "" ""); QXmppPubSubIq iq; parsePacket(iq, xml); QCOMPARE(iq.id(), QLatin1String("sub1")); QCOMPARE(iq.to(), QLatin1String("francisco@denmark.lit/barracks")); QCOMPARE(iq.from(), QLatin1String("pubsub.shakespeare.lit")); QCOMPARE(iq.type(), QXmppIq::Result); QCOMPARE(iq.queryType(), QXmppPubSubIq::SubscriptionQuery); QCOMPARE(iq.queryJid(), QLatin1String("francisco@denmark.lit")); QCOMPARE(iq.queryNode(), QLatin1String("princely_musings")); QCOMPARE(iq.subscriptionId(), QLatin1String("ba49252aaa4f5d320c24d3766f0bdcade78c78d3")); serializePacket(iq, xml); } void TestPubSub::testSubscriptions() { const QByteArray xml( "" "" "" "" ""); QXmppPubSubIq iq; parsePacket(iq, xml); QCOMPARE(iq.id(), QLatin1String("subscriptions1")); QCOMPARE(iq.to(), QLatin1String("pubsub.shakespeare.lit")); QCOMPARE(iq.from(), QLatin1String("francisco@denmark.lit/barracks")); QCOMPARE(iq.type(), QXmppIq::Get); QCOMPARE(iq.queryType(), QXmppPubSubIq::SubscriptionsQuery); QCOMPARE(iq.queryJid(), QString()); QCOMPARE(iq.queryNode(), QString()); serializePacket(iq, xml); } class TestPasswordChecker : public QXmppPasswordChecker { public: TestPasswordChecker(const QString &username, const QString &password) : m_getPassword(true), m_username(username), m_password(password) { }; /// Retrieves the password for the given username. QXmppPasswordReply::Error getPassword(const QXmppPasswordRequest &request, QString &password) { if (request.username() == m_username) { password = m_password; return QXmppPasswordReply::NoError; } else { return QXmppPasswordReply::AuthorizationError; } }; /// Sets whether getPassword() is enabled. void setGetPassword(bool getPassword) { m_getPassword = getPassword; } /// Returns whether getPassword() is enabled. bool hasGetPassword() const { return m_getPassword; }; private: bool m_getPassword; QString m_username; QString m_password; }; void TestServer::testConnect_data() { QTest::addColumn("username"); QTest::addColumn("password"); QTest::addColumn("mechanism"); QTest::addColumn("connected"); QTest::newRow("plain-good") << "testuser" << "testpwd" << "PLAIN" << true; QTest::newRow("plain-bad-username") << "baduser" << "testpwd" << "PLAIN" << false; QTest::newRow("plain-bad-password") << "testuser" << "badpwd" << "PLAIN" << false; QTest::newRow("digest-good") << "testuser" << "testpwd" << "DIGEST-MD5" << true; QTest::newRow("digest-bad-username") << "baduser" << "testpwd" << "DIGEST-MD5" << false; QTest::newRow("digest-bad-password") << "testuser" << "badpwd" << "DIGEST-MD5" << false; } void TestServer::testConnect() { QFETCH(QString, username); QFETCH(QString, password); QFETCH(QString, mechanism); QFETCH(bool, connected); const QString testDomain("localhost"); const QHostAddress testHost(QHostAddress::LocalHost); const quint16 testPort = 12345; QXmppLogger logger; logger.setLoggingType(QXmppLogger::StdoutLogging); // prepare server TestPasswordChecker passwordChecker("testuser", "testpwd"); QXmppServer server; server.setDomain(testDomain); server.setLogger(&logger); server.setPasswordChecker(&passwordChecker); server.listenForClients(testHost, testPort); // prepare client QXmppClient client; client.setLogger(&logger); QEventLoop loop; connect(&client, SIGNAL(connected()), &loop, SLOT(quit())); connect(&client, SIGNAL(disconnected()), &loop, SLOT(quit())); QXmppConfiguration config; config.setDomain(testDomain); config.setHost(testHost.toString()); config.setPort(testPort); config.setUser(username); config.setPassword(password); config.setSaslAuthMechanism(mechanism); client.connectToServer(config); loop.exec(); QCOMPARE(client.isConnected(), connected); } int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QXmppPresence pres; pres.availableStatusType(); // run tests int errors = 0; TestPackets testPackets; errors += QTest::qExec(&testPackets); TestPubSub testPubSub; errors += QTest::qExec(&testPubSub); TestServer testServer; errors += QTest::qExec(&testServer); if (errors) { qWarning() << "Total failed tests:" << errors; return EXIT_FAILURE; } return EXIT_SUCCESS; };