From 6a01a0f7bfd03deb3d0e6ed3e107ddbc4e07e5c8 Mon Sep 17 00:00:00 2001 From: Jeremy Lainé Date: Thu, 27 Sep 2012 20:50:17 +0200 Subject: move sasl test code --- tests/all/all.pro | 5 - tests/all/sasl.cpp | 401 -------------------------------- tests/all/sasl.h | 69 ------ tests/all/tests.cpp | 15 +- tests/qxmppsasl/tst_qxmppsasl.cpp | 447 ++++++++++++++++++++++++++++++++++++ tests/qxmpputils/tst_qxmpputils.cpp | 136 +++++++++++ tests/tests.pro | 1 + 7 files changed, 585 insertions(+), 489 deletions(-) delete mode 100644 tests/all/sasl.cpp delete mode 100644 tests/all/sasl.h create mode 100644 tests/qxmppsasl/tst_qxmppsasl.cpp create mode 100644 tests/qxmpputils/tst_qxmpputils.cpp (limited to 'tests') diff --git a/tests/all/all.pro b/tests/all/all.pro index 4d2b678e..13afe30f 100644 --- a/tests/all/all.pro +++ b/tests/all/all.pro @@ -4,8 +4,3 @@ TARGET = tst_all SOURCES += tests.cpp HEADERS += tests.h - -!isEmpty(QXMPP_AUTOTEST_INTERNAL) { - HEADERS += sasl.h - SOURCES += sasl.cpp -} diff --git a/tests/all/sasl.cpp b/tests/all/sasl.cpp deleted file mode 100644 index b276ddcd..00000000 --- a/tests/all/sasl.cpp +++ /dev/null @@ -1,401 +0,0 @@ -/* - * Copyright (C) 2008-2012 The QXmpp developers - * - * 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 "QXmppSasl_p.h" - -#include "sasl.h" -#include "util.h" - -void tst_QXmppSasl::testParsing() -{ - // empty - QMap empty = QXmppSaslDigestMd5::parseMessage(QByteArray()); - QCOMPARE(empty.size(), 0); - QCOMPARE(QXmppSaslDigestMd5::serializeMessage(empty), QByteArray()); - - // non-empty - const QByteArray bytes("number=12345,quoted_plain=\"quoted string\",quoted_quote=\"quoted\\\\slash\\\"quote\",string=string"); - - QMap map = QXmppSaslDigestMd5::parseMessage(bytes); - QCOMPARE(map.size(), 4); - QCOMPARE(map["number"], QByteArray("12345")); - QCOMPARE(map["quoted_plain"], QByteArray("quoted string")); - QCOMPARE(map["quoted_quote"], QByteArray("quoted\\slash\"quote")); - QCOMPARE(map["string"], QByteArray("string")); - QCOMPARE(QXmppSaslDigestMd5::serializeMessage(map), bytes); -} - -void tst_QXmppSasl::testAuth_data() -{ - QTest::addColumn("xml"); - QTest::addColumn("mechanism"); - QTest::addColumn("value"); - - QTest::newRow("plain") - << QByteArray("AGZvbwBiYXI=") - << "PLAIN" << QByteArray("\0foo\0bar", 8); - - QTest::newRow("digest-md5") - << QByteArray("") - << "DIGEST-MD5" << QByteArray(); -} - -void tst_QXmppSasl::testAuth() -{ - QFETCH(QByteArray, xml); - QFETCH(QString, mechanism); - QFETCH(QByteArray, value); - - // no condition - QXmppSaslAuth auth; - parsePacket(auth, xml); - QCOMPARE(auth.mechanism(), mechanism); - QCOMPARE(auth.value(), value); - serializePacket(auth, xml); -} - -void tst_QXmppSasl::testChallenge_data() -{ - QTest::addColumn("xml"); - QTest::addColumn("value"); - - QTest::newRow("empty") - << QByteArray("") - << QByteArray(); - - QTest::newRow("value") - << QByteArray("AGZvbwBiYXI=") - << QByteArray("\0foo\0bar", 8); -} - -void tst_QXmppSasl::testChallenge() -{ - QFETCH(QByteArray, xml); - QFETCH(QByteArray, value); - - // no condition - QXmppSaslChallenge challenge; - parsePacket(challenge, xml); - QCOMPARE(challenge.value(), value); - serializePacket(challenge, xml); -} - -void tst_QXmppSasl::testFailure() -{ - // no condition - const QByteArray xml = ""; - QXmppSaslFailure failure; - parsePacket(failure, xml); - QCOMPARE(failure.condition(), QString()); - serializePacket(failure, xml); - - // not authorized - const QByteArray xml2 = ""; - QXmppSaslFailure failure2; - parsePacket(failure2, xml2); - QCOMPARE(failure2.condition(), QLatin1String("not-authorized")); - serializePacket(failure2, xml2); -} - -void tst_QXmppSasl::testResponse_data() -{ - QTest::addColumn("xml"); - QTest::addColumn("value"); - - QTest::newRow("empty") - << QByteArray("") - << QByteArray(); - - QTest::newRow("value") - << QByteArray("AGZvbwBiYXI=") - << QByteArray("\0foo\0bar", 8); -} - -void tst_QXmppSasl::testResponse() -{ - QFETCH(QByteArray, xml); - QFETCH(QByteArray, value); - - // no condition - QXmppSaslResponse response; - parsePacket(response, xml); - QCOMPARE(response.value(), value); - serializePacket(response, xml); -} - -void tst_QXmppSasl::testSuccess() -{ - const QByteArray xml = ""; - QXmppSaslSuccess stanza; - parsePacket(stanza, xml); - serializePacket(stanza, xml); -} - -void tst_QXmppSaslClient::testAvailableMechanisms() -{ - QCOMPARE(QXmppSaslClient::availableMechanisms(), QStringList() << "PLAIN" << "DIGEST-MD5" << "ANONYMOUS" << "X-FACEBOOK-PLATFORM" << "X-MESSENGER-OAUTH2" << "X-OAUTH2"); -} - -void tst_QXmppSaslClient::testBadMechanism() -{ - QXmppSaslClient *client = QXmppSaslClient::create("BAD-MECH"); - QVERIFY(client == 0); -} - -void tst_QXmppSaslClient::testAnonymous() -{ - QXmppSaslClient *client = QXmppSaslClient::create("ANONYMOUS"); - QVERIFY(client != 0); - QCOMPARE(client->mechanism(), QLatin1String("ANONYMOUS")); - - // initial step returns nothing - QByteArray response; - QVERIFY(client->respond(QByteArray(), response)); - QCOMPARE(response, QByteArray()); - - // any further step is an error - QVERIFY(!client->respond(QByteArray(), response)); - - delete client; -} - -void tst_QXmppSaslClient::testDigestMd5_data() -{ - QTest::addColumn("qop"); - QTest::newRow("qop-none") << QByteArray(); - QTest::newRow("qop-auth") << QByteArray(",qop=\"auth\""); - QTest::newRow("qop-multi") << QByteArray(",qop=\"auth,auth-int\""); -} - -void tst_QXmppSaslClient::testDigestMd5() -{ - QFETCH(QByteArray, qop); - - QXmppSaslDigestMd5::setNonce("AMzVG8Oibf+sVUCPPlWLR8lZQvbbJtJB9vJd+u3c6dw="); - - QXmppSaslClient *client = QXmppSaslClient::create("DIGEST-MD5"); - QVERIFY(client != 0); - QCOMPARE(client->mechanism(), QLatin1String("DIGEST-MD5")); - - client->setUsername("qxmpp1"); - client->setPassword("qxmpp123"); - client->setHost("jabber.ru"); - client->setServiceType("xmpp"); - - // initial step returns nothing - QByteArray response; - QVERIFY(client->respond(QByteArray(), response)); - QCOMPARE(response, QByteArray()); - - QVERIFY(client->respond(QByteArray("nonce=\"2530347127\"") + qop + QByteArray("charset=utf-8,algorithm=md5-sess"), response)); - QCOMPARE(response, QByteArray("charset=utf-8,cnonce=\"AMzVG8Oibf+sVUCPPlWLR8lZQvbbJtJB9vJd+u3c6dw=\",digest-uri=\"xmpp/jabber.ru\",nc=00000001,nonce=2530347127,qop=auth,response=a61fbf4320577d74038b71a8546bc7ae,username=qxmpp1")); - - QVERIFY(client->respond(QByteArray("rspauth=d92bf7f4331700c24799cbab364a14b7"), response)); - QCOMPARE(response, QByteArray()); - - // any further step is an error - QVERIFY(!client->respond(QByteArray(), response)); - - delete client; -} - -void tst_QXmppSaslClient::testFacebook() -{ - QXmppSaslClient *client = QXmppSaslClient::create("X-FACEBOOK-PLATFORM"); - QVERIFY(client != 0); - QCOMPARE(client->mechanism(), QLatin1String("X-FACEBOOK-PLATFORM")); - - client->setUsername("123456789012345"); - client->setPassword("abcdefghijlkmno"); - - // initial step returns nothing - QByteArray response; - QVERIFY(client->respond(QByteArray(), response)); - QCOMPARE(response, QByteArray()); - - // challenge response - QVERIFY(client->respond(QByteArray("version=1&method=auth.xmpp_login&nonce=AA4EFEE16F2AB64B131EEFFE6EACDDB8"), response)); - QCOMPARE(response, QByteArray("access_token=abcdefghijlkmno&api_key=123456789012345&call_id=&method=auth.xmpp_login&nonce=AA4EFEE16F2AB64B131EEFFE6EACDDB8&v=1.0")); - - // any further step is an error - QVERIFY(!client->respond(QByteArray(), response)); - - delete client; -} - -void tst_QXmppSaslClient::testGoogle() -{ - QXmppSaslClient *client = QXmppSaslClient::create("X-OAUTH2"); - QVERIFY(client != 0); - QCOMPARE(client->mechanism(), QLatin1String("X-OAUTH2")); - - client->setUsername("foo"); - client->setPassword("bar"); - - // initial step returns data - QByteArray response; - QVERIFY(client->respond(QByteArray(), response)); - QCOMPARE(response, QByteArray("\0foo\0bar", 8)); - - // any further step is an error - QVERIFY(!client->respond(QByteArray(), response)); - - delete client; -} - -void tst_QXmppSaslClient::testPlain() -{ - QXmppSaslClient *client = QXmppSaslClient::create("PLAIN"); - QVERIFY(client != 0); - QCOMPARE(client->mechanism(), QLatin1String("PLAIN")); - - client->setUsername("foo"); - client->setPassword("bar"); - - // initial step returns data - QByteArray response; - QVERIFY(client->respond(QByteArray(), response)); - QCOMPARE(response, QByteArray("\0foo\0bar", 8)); - - // any further step is an error - QVERIFY(!client->respond(QByteArray(), response)); - - delete client; -} - -void tst_QXmppSaslClient::testWindowsLive() -{ - QXmppSaslClient *client = QXmppSaslClient::create("X-MESSENGER-OAUTH2"); - QVERIFY(client != 0); - QCOMPARE(client->mechanism(), QLatin1String("X-MESSENGER-OAUTH2")); - - client->setPassword(QByteArray("footoken").toBase64()); - - // initial step returns data - QByteArray response; - QVERIFY(client->respond(QByteArray(), response)); - QCOMPARE(response, QByteArray("footoken", 8)); - - // any further step is an error - QVERIFY(!client->respond(QByteArray(), response)); - - delete client; -} - -void tst_QXmppSaslServer::testBadMechanism() -{ - QXmppSaslServer *server = QXmppSaslServer::create("BAD-MECH"); - QVERIFY(server == 0); -} - -void tst_QXmppSaslServer::testAnonymous() -{ - QXmppSaslServer *server = QXmppSaslServer::create("ANONYMOUS"); - QVERIFY(server != 0); - QCOMPARE(server->mechanism(), QLatin1String("ANONYMOUS")); - - // initial step returns success - QByteArray response; - QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Succeeded); - QCOMPARE(response, QByteArray()); - - // any further step is an error - QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Failed); - - delete server; -} - -void tst_QXmppSaslServer::testDigestMd5() -{ - QXmppSaslDigestMd5::setNonce("OI08/m+QRm6Ma+fKOjuqVXtz40sR5u9/u5GN6sSW0rs="); - - QXmppSaslServer *server = QXmppSaslServer::create("DIGEST-MD5"); - QVERIFY(server != 0); - QCOMPARE(server->mechanism(), QLatin1String("DIGEST-MD5")); - - // initial step returns challenge - QByteArray response; - QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Challenge); - QCOMPARE(response, QByteArray("algorithm=md5-sess,charset=utf-8,nonce=\"OI08/m+QRm6Ma+fKOjuqVXtz40sR5u9/u5GN6sSW0rs=\",qop=auth")); - - // password needed - const QByteArray request = QByteArray("charset=utf-8,cnonce=\"AMzVG8Oibf+sVUCPPlWLR8lZQvbbJtJB9vJd+u3c6dw=\",digest-uri=\"xmpp/jabber.ru\",nc=00000001,nonce=\"OI08/m+QRm6Ma+fKOjuqVXtz40sR5u9/u5GN6sSW0rs=\",qop=auth,response=70e9063257ee2bf6bfd108975b917410,username=qxmpp1"); - QCOMPARE(server->respond(request, response), QXmppSaslServer::InputNeeded); - QCOMPARE(server->username(), QLatin1String("qxmpp1")); - server->setPassword("qxmpp123"); - - // second challenge - QCOMPARE(server->respond(request, response), QXmppSaslServer::Challenge); - QCOMPARE(response, QByteArray("rspauth=2821a3add271b9ae02b813bed57ec878")); - - // success - QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Succeeded); - QCOMPARE(response, QByteArray()); - - // any further step is an error - QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Failed); - - delete server; -} - -void tst_QXmppSaslServer::testPlain() -{ - QXmppSaslServer *server = QXmppSaslServer::create("PLAIN"); - QVERIFY(server != 0); - QCOMPARE(server->mechanism(), QLatin1String("PLAIN")); - - // initial step returns success - QByteArray response; - QCOMPARE(server->respond(QByteArray("\0foo\0bar", 8), response), QXmppSaslServer::InputNeeded); - QCOMPARE(response, QByteArray()); - QCOMPARE(server->username(), QLatin1String("foo")); - QCOMPARE(server->password(), QLatin1String("bar")); - - // any further step is an error - QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Failed); - - delete server; -} - -void tst_QXmppSaslServer::testPlainChallenge() -{ - QXmppSaslServer *server = QXmppSaslServer::create("PLAIN"); - QVERIFY(server != 0); - QCOMPARE(server->mechanism(), QLatin1String("PLAIN")); - - // initial step returns challenge - QByteArray response; - QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Challenge); - QCOMPARE(response, QByteArray()); - - // initial step returns success - QCOMPARE(server->respond(QByteArray("\0foo\0bar", 8), response), QXmppSaslServer::InputNeeded); - QCOMPARE(response, QByteArray()); - QCOMPARE(server->username(), QLatin1String("foo")); - QCOMPARE(server->password(), QLatin1String("bar")); - - // any further step is an error - QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Failed); - - delete server; -} diff --git a/tests/all/sasl.h b/tests/all/sasl.h deleted file mode 100644 index f70ff9af..00000000 --- a/tests/all/sasl.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2008-2012 The QXmpp developers - * - * 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 - -class tst_QXmppSasl : public QObject -{ - Q_OBJECT - -private slots: - void testParsing(); - void testAuth_data(); - void testAuth(); - void testChallenge_data(); - void testChallenge(); - void testFailure(); - void testResponse_data(); - void testResponse(); - void testSuccess(); -}; - -class tst_QXmppSaslClient : public QObject -{ - Q_OBJECT - -private slots: - void testAvailableMechanisms(); - void testBadMechanism(); - void testAnonymous(); - void testDigestMd5(); - void testDigestMd5_data(); - void testFacebook(); - void testGoogle(); - void testPlain(); - void testWindowsLive(); -}; - -class tst_QXmppSaslServer : public QObject -{ - Q_OBJECT - -private slots: - void testBadMechanism(); - void testAnonymous(); - void testDigestMd5(); - void testPlain(); - void testPlainChallenge(); -}; - diff --git a/tests/all/tests.cpp b/tests/all/tests.cpp index 640c26e4..f191dfde 100644 --- a/tests/all/tests.cpp +++ b/tests/all/tests.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include "QXmppArchiveIq.h" #include "QXmppBindIq.h" @@ -41,10 +41,8 @@ #include "QXmppStreamFeatures.h" #include "QXmppUtils.h" #include "QXmppVersionIq.h" -#include "QXmppGlobal.h" #include "QXmppEntityTimeIq.h" -#include "sasl.h" #include "tests.h" #include "util.h" @@ -783,17 +781,6 @@ int main(int argc, char *argv[]) TestPubSub testPubSub; errors += QTest::qExec(&testPubSub); -#ifdef QXMPP_AUTOTEST_INTERNAL - tst_QXmppSasl testSasl; - errors += QTest::qExec(&testSasl); - - tst_QXmppSaslClient testSaslClient; - errors += QTest::qExec(&testSaslClient); - - tst_QXmppSaslServer testSaslServer; - errors += QTest::qExec(&testSaslServer); -#endif - TestServer testServer; errors += QTest::qExec(&testServer); diff --git a/tests/qxmppsasl/tst_qxmppsasl.cpp b/tests/qxmppsasl/tst_qxmppsasl.cpp new file mode 100644 index 00000000..b85e49ee --- /dev/null +++ b/tests/qxmppsasl/tst_qxmppsasl.cpp @@ -0,0 +1,447 @@ +/* + * Copyright (C) 2008-2012 The QXmpp developers + * + * 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 "QXmppSasl_p.h" +#include "util.h" + +class tst_QXmppSasl : public QObject +{ + Q_OBJECT + +private slots: + void testParsing(); + void testAuth_data(); + void testAuth(); + void testChallenge_data(); + void testChallenge(); + void testFailure(); + void testResponse_data(); + void testResponse(); + void testSuccess(); +}; + +class tst_QXmppSaslClient : public QObject +{ + Q_OBJECT + +private slots: + void testAvailableMechanisms(); + void testBadMechanism(); + void testAnonymous(); + void testDigestMd5(); + void testDigestMd5_data(); + void testFacebook(); + void testGoogle(); + void testPlain(); + void testWindowsLive(); +}; + +class tst_QXmppSaslServer : public QObject +{ + Q_OBJECT + +private slots: + void testBadMechanism(); + void testAnonymous(); + void testDigestMd5(); + void testPlain(); + void testPlainChallenge(); +}; + +void tst_QXmppSasl::testParsing() +{ + // empty + QMap empty = QXmppSaslDigestMd5::parseMessage(QByteArray()); + QCOMPARE(empty.size(), 0); + QCOMPARE(QXmppSaslDigestMd5::serializeMessage(empty), QByteArray()); + + // non-empty + const QByteArray bytes("number=12345,quoted_plain=\"quoted string\",quoted_quote=\"quoted\\\\slash\\\"quote\",string=string"); + + QMap map = QXmppSaslDigestMd5::parseMessage(bytes); + QCOMPARE(map.size(), 4); + QCOMPARE(map["number"], QByteArray("12345")); + QCOMPARE(map["quoted_plain"], QByteArray("quoted string")); + QCOMPARE(map["quoted_quote"], QByteArray("quoted\\slash\"quote")); + QCOMPARE(map["string"], QByteArray("string")); + QCOMPARE(QXmppSaslDigestMd5::serializeMessage(map), bytes); +} + +void tst_QXmppSasl::testAuth_data() +{ + QTest::addColumn("xml"); + QTest::addColumn("mechanism"); + QTest::addColumn("value"); + + QTest::newRow("plain") + << QByteArray("AGZvbwBiYXI=") + << "PLAIN" << QByteArray("\0foo\0bar", 8); + + QTest::newRow("digest-md5") + << QByteArray("") + << "DIGEST-MD5" << QByteArray(); +} + +void tst_QXmppSasl::testAuth() +{ + QFETCH(QByteArray, xml); + QFETCH(QString, mechanism); + QFETCH(QByteArray, value); + + // no condition + QXmppSaslAuth auth; + parsePacket(auth, xml); + QCOMPARE(auth.mechanism(), mechanism); + QCOMPARE(auth.value(), value); + serializePacket(auth, xml); +} + +void tst_QXmppSasl::testChallenge_data() +{ + QTest::addColumn("xml"); + QTest::addColumn("value"); + + QTest::newRow("empty") + << QByteArray("") + << QByteArray(); + + QTest::newRow("value") + << QByteArray("AGZvbwBiYXI=") + << QByteArray("\0foo\0bar", 8); +} + +void tst_QXmppSasl::testChallenge() +{ + QFETCH(QByteArray, xml); + QFETCH(QByteArray, value); + + // no condition + QXmppSaslChallenge challenge; + parsePacket(challenge, xml); + QCOMPARE(challenge.value(), value); + serializePacket(challenge, xml); +} + +void tst_QXmppSasl::testFailure() +{ + // no condition + const QByteArray xml = ""; + QXmppSaslFailure failure; + parsePacket(failure, xml); + QCOMPARE(failure.condition(), QString()); + serializePacket(failure, xml); + + // not authorized + const QByteArray xml2 = ""; + QXmppSaslFailure failure2; + parsePacket(failure2, xml2); + QCOMPARE(failure2.condition(), QLatin1String("not-authorized")); + serializePacket(failure2, xml2); +} + +void tst_QXmppSasl::testResponse_data() +{ + QTest::addColumn("xml"); + QTest::addColumn("value"); + + QTest::newRow("empty") + << QByteArray("") + << QByteArray(); + + QTest::newRow("value") + << QByteArray("AGZvbwBiYXI=") + << QByteArray("\0foo\0bar", 8); +} + +void tst_QXmppSasl::testResponse() +{ + QFETCH(QByteArray, xml); + QFETCH(QByteArray, value); + + // no condition + QXmppSaslResponse response; + parsePacket(response, xml); + QCOMPARE(response.value(), value); + serializePacket(response, xml); +} + +void tst_QXmppSasl::testSuccess() +{ + const QByteArray xml = ""; + QXmppSaslSuccess stanza; + parsePacket(stanza, xml); + serializePacket(stanza, xml); +} + +void tst_QXmppSaslClient::testAvailableMechanisms() +{ + QCOMPARE(QXmppSaslClient::availableMechanisms(), QStringList() << "PLAIN" << "DIGEST-MD5" << "ANONYMOUS" << "X-FACEBOOK-PLATFORM" << "X-MESSENGER-OAUTH2" << "X-OAUTH2"); +} + +void tst_QXmppSaslClient::testBadMechanism() +{ + QXmppSaslClient *client = QXmppSaslClient::create("BAD-MECH"); + QVERIFY(client == 0); +} + +void tst_QXmppSaslClient::testAnonymous() +{ + QXmppSaslClient *client = QXmppSaslClient::create("ANONYMOUS"); + QVERIFY(client != 0); + QCOMPARE(client->mechanism(), QLatin1String("ANONYMOUS")); + + // initial step returns nothing + QByteArray response; + QVERIFY(client->respond(QByteArray(), response)); + QCOMPARE(response, QByteArray()); + + // any further step is an error + QVERIFY(!client->respond(QByteArray(), response)); + + delete client; +} + +void tst_QXmppSaslClient::testDigestMd5_data() +{ + QTest::addColumn("qop"); + QTest::newRow("qop-none") << QByteArray(); + QTest::newRow("qop-auth") << QByteArray(",qop=\"auth\""); + QTest::newRow("qop-multi") << QByteArray(",qop=\"auth,auth-int\""); +} + +void tst_QXmppSaslClient::testDigestMd5() +{ + QFETCH(QByteArray, qop); + + QXmppSaslDigestMd5::setNonce("AMzVG8Oibf+sVUCPPlWLR8lZQvbbJtJB9vJd+u3c6dw="); + + QXmppSaslClient *client = QXmppSaslClient::create("DIGEST-MD5"); + QVERIFY(client != 0); + QCOMPARE(client->mechanism(), QLatin1String("DIGEST-MD5")); + + client->setUsername("qxmpp1"); + client->setPassword("qxmpp123"); + client->setHost("jabber.ru"); + client->setServiceType("xmpp"); + + // initial step returns nothing + QByteArray response; + QVERIFY(client->respond(QByteArray(), response)); + QCOMPARE(response, QByteArray()); + + QVERIFY(client->respond(QByteArray("nonce=\"2530347127\"") + qop + QByteArray("charset=utf-8,algorithm=md5-sess"), response)); + QCOMPARE(response, QByteArray("charset=utf-8,cnonce=\"AMzVG8Oibf+sVUCPPlWLR8lZQvbbJtJB9vJd+u3c6dw=\",digest-uri=\"xmpp/jabber.ru\",nc=00000001,nonce=2530347127,qop=auth,response=a61fbf4320577d74038b71a8546bc7ae,username=qxmpp1")); + + QVERIFY(client->respond(QByteArray("rspauth=d92bf7f4331700c24799cbab364a14b7"), response)); + QCOMPARE(response, QByteArray()); + + // any further step is an error + QVERIFY(!client->respond(QByteArray(), response)); + + delete client; +} + +void tst_QXmppSaslClient::testFacebook() +{ + QXmppSaslClient *client = QXmppSaslClient::create("X-FACEBOOK-PLATFORM"); + QVERIFY(client != 0); + QCOMPARE(client->mechanism(), QLatin1String("X-FACEBOOK-PLATFORM")); + + client->setUsername("123456789012345"); + client->setPassword("abcdefghijlkmno"); + + // initial step returns nothing + QByteArray response; + QVERIFY(client->respond(QByteArray(), response)); + QCOMPARE(response, QByteArray()); + + // challenge response + QVERIFY(client->respond(QByteArray("version=1&method=auth.xmpp_login&nonce=AA4EFEE16F2AB64B131EEFFE6EACDDB8"), response)); + QCOMPARE(response, QByteArray("access_token=abcdefghijlkmno&api_key=123456789012345&call_id=&method=auth.xmpp_login&nonce=AA4EFEE16F2AB64B131EEFFE6EACDDB8&v=1.0")); + + // any further step is an error + QVERIFY(!client->respond(QByteArray(), response)); + + delete client; +} + +void tst_QXmppSaslClient::testGoogle() +{ + QXmppSaslClient *client = QXmppSaslClient::create("X-OAUTH2"); + QVERIFY(client != 0); + QCOMPARE(client->mechanism(), QLatin1String("X-OAUTH2")); + + client->setUsername("foo"); + client->setPassword("bar"); + + // initial step returns data + QByteArray response; + QVERIFY(client->respond(QByteArray(), response)); + QCOMPARE(response, QByteArray("\0foo\0bar", 8)); + + // any further step is an error + QVERIFY(!client->respond(QByteArray(), response)); + + delete client; +} + +void tst_QXmppSaslClient::testPlain() +{ + QXmppSaslClient *client = QXmppSaslClient::create("PLAIN"); + QVERIFY(client != 0); + QCOMPARE(client->mechanism(), QLatin1String("PLAIN")); + + client->setUsername("foo"); + client->setPassword("bar"); + + // initial step returns data + QByteArray response; + QVERIFY(client->respond(QByteArray(), response)); + QCOMPARE(response, QByteArray("\0foo\0bar", 8)); + + // any further step is an error + QVERIFY(!client->respond(QByteArray(), response)); + + delete client; +} + +void tst_QXmppSaslClient::testWindowsLive() +{ + QXmppSaslClient *client = QXmppSaslClient::create("X-MESSENGER-OAUTH2"); + QVERIFY(client != 0); + QCOMPARE(client->mechanism(), QLatin1String("X-MESSENGER-OAUTH2")); + + client->setPassword(QByteArray("footoken").toBase64()); + + // initial step returns data + QByteArray response; + QVERIFY(client->respond(QByteArray(), response)); + QCOMPARE(response, QByteArray("footoken", 8)); + + // any further step is an error + QVERIFY(!client->respond(QByteArray(), response)); + + delete client; +} + +void tst_QXmppSaslServer::testBadMechanism() +{ + QXmppSaslServer *server = QXmppSaslServer::create("BAD-MECH"); + QVERIFY(server == 0); +} + +void tst_QXmppSaslServer::testAnonymous() +{ + QXmppSaslServer *server = QXmppSaslServer::create("ANONYMOUS"); + QVERIFY(server != 0); + QCOMPARE(server->mechanism(), QLatin1String("ANONYMOUS")); + + // initial step returns success + QByteArray response; + QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Succeeded); + QCOMPARE(response, QByteArray()); + + // any further step is an error + QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Failed); + + delete server; +} + +void tst_QXmppSaslServer::testDigestMd5() +{ + QXmppSaslDigestMd5::setNonce("OI08/m+QRm6Ma+fKOjuqVXtz40sR5u9/u5GN6sSW0rs="); + + QXmppSaslServer *server = QXmppSaslServer::create("DIGEST-MD5"); + QVERIFY(server != 0); + QCOMPARE(server->mechanism(), QLatin1String("DIGEST-MD5")); + + // initial step returns challenge + QByteArray response; + QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Challenge); + QCOMPARE(response, QByteArray("algorithm=md5-sess,charset=utf-8,nonce=\"OI08/m+QRm6Ma+fKOjuqVXtz40sR5u9/u5GN6sSW0rs=\",qop=auth")); + + // password needed + const QByteArray request = QByteArray("charset=utf-8,cnonce=\"AMzVG8Oibf+sVUCPPlWLR8lZQvbbJtJB9vJd+u3c6dw=\",digest-uri=\"xmpp/jabber.ru\",nc=00000001,nonce=\"OI08/m+QRm6Ma+fKOjuqVXtz40sR5u9/u5GN6sSW0rs=\",qop=auth,response=70e9063257ee2bf6bfd108975b917410,username=qxmpp1"); + QCOMPARE(server->respond(request, response), QXmppSaslServer::InputNeeded); + QCOMPARE(server->username(), QLatin1String("qxmpp1")); + server->setPassword("qxmpp123"); + + // second challenge + QCOMPARE(server->respond(request, response), QXmppSaslServer::Challenge); + QCOMPARE(response, QByteArray("rspauth=2821a3add271b9ae02b813bed57ec878")); + + // success + QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Succeeded); + QCOMPARE(response, QByteArray()); + + // any further step is an error + QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Failed); + + delete server; +} + +void tst_QXmppSaslServer::testPlain() +{ + QXmppSaslServer *server = QXmppSaslServer::create("PLAIN"); + QVERIFY(server != 0); + QCOMPARE(server->mechanism(), QLatin1String("PLAIN")); + + // initial step returns success + QByteArray response; + QCOMPARE(server->respond(QByteArray("\0foo\0bar", 8), response), QXmppSaslServer::InputNeeded); + QCOMPARE(response, QByteArray()); + QCOMPARE(server->username(), QLatin1String("foo")); + QCOMPARE(server->password(), QLatin1String("bar")); + + // any further step is an error + QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Failed); + + delete server; +} + +void tst_QXmppSaslServer::testPlainChallenge() +{ + QXmppSaslServer *server = QXmppSaslServer::create("PLAIN"); + QVERIFY(server != 0); + QCOMPARE(server->mechanism(), QLatin1String("PLAIN")); + + // initial step returns challenge + QByteArray response; + QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Challenge); + QCOMPARE(response, QByteArray()); + + // initial step returns success + QCOMPARE(server->respond(QByteArray("\0foo\0bar", 8), response), QXmppSaslServer::InputNeeded); + QCOMPARE(response, QByteArray()); + QCOMPARE(server->username(), QLatin1String("foo")); + QCOMPARE(server->password(), QLatin1String("bar")); + + // any further step is an error + QCOMPARE(server->respond(QByteArray(), response), QXmppSaslServer::Failed); + + delete server; +} + +QTEST_MAIN(tst_QXmppSasl) +#include "tst_qxmppsasl.moc" diff --git a/tests/qxmpputils/tst_qxmpputils.cpp b/tests/qxmpputils/tst_qxmpputils.cpp new file mode 100644 index 00000000..bf1c80dc --- /dev/null +++ b/tests/qxmpputils/tst_qxmpputils.cpp @@ -0,0 +1,136 @@ +/* + * 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 "QXmppUtils.h" +#include "util.h" + +class tst_QXmppUtils : public QObject +{ + Q_OBJECT + +private slots: + void testCrc32(); + void testHmac(); + void testJid(); + void testMime(); + void testLibVersion(); + void testTimezoneOffset(); +}; + +void tst_QXmppUtils::testCrc32() +{ + quint32 crc = QXmppUtils::generateCrc32(QByteArray()); + QCOMPARE(crc, 0u); + + crc = QXmppUtils::generateCrc32(QByteArray("Hi There")); + QCOMPARE(crc, 0xDB143BBEu); +} + +void tst_QXmppUtils::testHmac() +{ + QByteArray hmac = QXmppUtils::generateHmacMd5(QByteArray(16, 0x0b), QByteArray("Hi There")); + QCOMPARE(hmac, QByteArray::fromHex("9294727a3638bb1c13f48ef8158bfc9d")); + + hmac = QXmppUtils::generateHmacMd5(QByteArray("Jefe"), QByteArray("what do ya want for nothing?")); + QCOMPARE(hmac, QByteArray::fromHex("750c783e6ab0b503eaa86e310a5db738")); + + hmac = QXmppUtils::generateHmacMd5(QByteArray(16, 0xaa), QByteArray(50, 0xdd)); + QCOMPARE(hmac, QByteArray::fromHex("56be34521d144c88dbb8c733f0e8b3f6")); +} + +void tst_QXmppUtils::testJid() +{ + QCOMPARE(QXmppUtils::jidToBareJid("foo@example.com/resource"), QLatin1String("foo@example.com")); + QCOMPARE(QXmppUtils::jidToBareJid("foo@example.com"), QLatin1String("foo@example.com")); + QCOMPARE(QXmppUtils::jidToBareJid("example.com"), QLatin1String("example.com")); + QCOMPARE(QXmppUtils::jidToBareJid(QString()), QString()); + + QCOMPARE(QXmppUtils::jidToDomain("foo@example.com/resource"), QLatin1String("example.com")); + QCOMPARE(QXmppUtils::jidToDomain("foo@example.com"), QLatin1String("example.com")); + QCOMPARE(QXmppUtils::jidToDomain("example.com"), QLatin1String("example.com")); + QCOMPARE(QXmppUtils::jidToDomain(QString()), QString()); + + QCOMPARE(QXmppUtils::jidToResource("foo@example.com/resource"), QLatin1String("resource")); + QCOMPARE(QXmppUtils::jidToResource("foo@example.com"), QString()); + QCOMPARE(QXmppUtils::jidToResource("example.com"), QString()); + QCOMPARE(QXmppUtils::jidToResource(QString()), QString()); + + QCOMPARE(QXmppUtils::jidToUser("foo@example.com/resource"), QLatin1String("foo")); + QCOMPARE(QXmppUtils::jidToUser("foo@example.com"), QLatin1String("foo")); + QCOMPARE(QXmppUtils::jidToUser("example.com"), QString()); + QCOMPARE(QXmppUtils::jidToUser(QString()), QString()); +} + +// FIXME: how should we test MIME detection without expose getImageType? +#if 0 +QString getImageType(const QByteArray &contents); + +static void testMimeType(const QString &fileName, const QString fileType) +{ + // load file from resources + QFile file(":/" + fileName); + QCOMPARE(file.open(QIODevice::ReadOnly), true); + QCOMPARE(getImageType(file.readAll()), fileType); + file.close(); +} + +void tst_QXmppUtils::testMime() +{ + testMimeType("test.bmp", "image/bmp"); + testMimeType("test.gif", "image/gif"); + testMimeType("test.jpg", "image/jpeg"); + testMimeType("test.mng", "video/x-mng"); + testMimeType("test.png", "image/png"); + testMimeType("test.svg", "image/svg+xml"); + testMimeType("test.xpm", "image/x-xpm"); +} +#else +void tst_QXmppUtils::testMime() +{ +} +#endif + +void tst_QXmppUtils::testLibVersion() +{ + QCOMPARE(QXmppVersion(), QString("0.7.3")); +} + +void tst_QXmppUtils::testTimezoneOffset() +{ + // parsing + QCOMPARE(QXmppUtils::timezoneOffsetFromString("Z"), 0); + QCOMPARE(QXmppUtils::timezoneOffsetFromString("+00:00"), 0); + QCOMPARE(QXmppUtils::timezoneOffsetFromString("-00:00"), 0); + QCOMPARE(QXmppUtils::timezoneOffsetFromString("+01:30"), 5400); + QCOMPARE(QXmppUtils::timezoneOffsetFromString("-01:30"), -5400); + + // serialization + QCOMPARE(QXmppUtils::timezoneOffsetToString(0), QLatin1String("Z")); + QCOMPARE(QXmppUtils::timezoneOffsetToString(5400), QLatin1String("+01:30")); + QCOMPARE(QXmppUtils::timezoneOffsetToString(-5400), QLatin1String("-01:30")); +} + +QTEST_MAIN(tst_QXmppUtils) +#include "tst_qxmpputils.moc" diff --git a/tests/tests.pro b/tests/tests.pro index 090daef6..7bfeba14 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -18,5 +18,6 @@ SUBDIRS = \ !isEmpty(QXMPP_AUTOTEST_INTERNAL) { SUBDIRS += qxmppcodec + SUBDIRS += qxmppsasl SUBDIRS += qxmppstreaminitiationiq } -- cgit v1.2.3