diff --git a/tests/capabilitiesjobtest.cpp b/tests/capabilitiesjobtest.cpp index facde7d..d4ad29d 100644 --- a/tests/capabilitiesjobtest.cpp +++ b/tests/capabilitiesjobtest.cpp @@ -1,99 +1,99 @@ /* Copyright (C) 2009 Andras Mantia Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "kimaptest/fakeserver.h" #include "kimap/session.h" #include "kimap/capabilitiesjob.h" #include #include #include class CapabilitiesJobTest: public QObject { Q_OBJECT private Q_SLOTS: void testCapabilities_data() { QTest::addColumn >( "scenario" ); QTest::addColumn( "capabilities" ); QList scenario; scenario << "S: * PREAUTH" << "C: A000001 CAPABILITY" << "S: * CAPABILITY IMAP4rev1 STARTTLS AUTH=GSSAPI" << "S: A000001 OK CAPABILITY completed"; QStringList capabilities; capabilities << "IMAP4REV1" << "STARTTLS" << "AUTH=GSSAPI"; QTest::newRow( "good" ) << scenario << capabilities; scenario.clear(); capabilities.clear(); scenario << "S: * PREAUTH" << "C: A000001 CAPABILITY" << "S: A000001 BAD command unknown or arguments invalid"; QTest::newRow( "bad" ) << scenario << capabilities; scenario.clear(); capabilities.clear(); scenario << "S: * PREAUTH" << "C: A000001 CAPABILITY" << "S: * CAPABILITY IMAP4rev1 STARTTLS AUTH=PLAIN" << "S: * some response" << "S: A000001 OK CAPABILITY completed"; capabilities << "IMAP4REV1" << "STARTTLS" << "AUTH=PLAIN"; QTest::newRow( "extra-untagged" ) << scenario << capabilities;; } void testCapabilities() { QFETCH( QList, scenario ); QFETCH( QStringList, capabilities ); FakeServer fakeServer; fakeServer.setScenario( scenario ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session session("127.0.0.1", 5989); KIMAP::CapabilitiesJob *job = new KIMAP::CapabilitiesJob(&session); bool result = job->exec(); QEXPECT_FAIL("bad" , "Expected failure on BAD response", Continue); QVERIFY(result); if (result) { QCOMPARE(job->capabilities(), capabilities); } fakeServer.quit(); } }; QTEST_KDEMAIN_CORE( CapabilitiesJobTest ) #include "capabilitiesjobtest.moc" diff --git a/tests/createjobtest.cpp b/tests/createjobtest.cpp index 27d50c3..09f50c7 100644 --- a/tests/createjobtest.cpp +++ b/tests/createjobtest.cpp @@ -1,88 +1,88 @@ /* Copyright (C) 2009 Andras Mantia Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "kimaptest/fakeserver.h" #include "kimap/session.h" #include "kimap/createjob.h" #include #include #include class CreateJobTest: public QObject { Q_OBJECT private Q_SLOTS: void testCreate_data() { QTest::addColumn( "mailbox" ); QTest::addColumn >( "scenario" ); QList scenario; scenario << FakeServer::preauth() << "C: A000001 CREATE \"INBOX\"" << "S: A000001 OK CREATE completed"; QTest::newRow( "good" ) << "INBOX" << scenario; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 CREATE \"INBOX-FAIL-BAD\"" << "S: A000001 BAD command unknown or arguments invalid"; QTest::newRow( "bad" ) << "INBOX-FAIL-BAD" << scenario; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 CREATE \"INBOX-FAIL-NO\"" << "S: A000001 NO create failure"; QTest::newRow( "no" ) << "INBOX-FAIL-NO" << scenario; } void testCreate() { QFETCH( QString, mailbox ); QFETCH( QList, scenario ); FakeServer fakeServer; fakeServer.setScenario( scenario ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session session("127.0.0.1", 5989); KIMAP::CreateJob *job = new KIMAP::CreateJob(&session); job->setMailBox(mailbox); bool result = job->exec(); QEXPECT_FAIL("bad" , "Expected failure on BAD response", Continue); QEXPECT_FAIL("no" , "Expected failure on NO response", Continue); QVERIFY(result); if (result) { QCOMPARE(job->mailBox(), mailbox); } fakeServer.quit(); } }; QTEST_KDEMAIN_CORE( CreateJobTest ) #include "createjobtest.moc" diff --git a/tests/deletejobtest.cpp b/tests/deletejobtest.cpp index e22e2da..3c8ed46 100644 --- a/tests/deletejobtest.cpp +++ b/tests/deletejobtest.cpp @@ -1,95 +1,95 @@ /* Copyright (C) 2009 Andras Mantia Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "kimaptest/fakeserver.h" #include "kimap/session.h" #include "kimap/deletejob.h" #include #include #include class DeleteJobTest: public QObject { Q_OBJECT private Q_SLOTS: void testDelete_data() { QTest::addColumn( "mailbox" ); QTest::addColumn >( "scenario" ); QList scenario; scenario << FakeServer::preauth() << "C: A000001 DELETE \"foo\"" << "S: A000001 OK DELETE completed"; QTest::newRow( "good" ) << "foo" << scenario; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 DELETE \"foo-BAD\"" << "S: A000001 BAD command unknown or arguments invalid"; QTest::newRow( "bad" ) << "foo-BAD" << scenario; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 DELETE \"foo\"" << "S: A000001 Name \"foo\" has inferior hierarchical names"; QTest::newRow( "no" ) << "foo" << scenario; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 DELETE \"foo/bar\"" << "S: A000001 OK DELETE completed"; QTest::newRow( "hierarchical" ) << "foo/bar" << scenario; } void testDelete() { QFETCH( QString, mailbox ); QFETCH( QList, scenario ); FakeServer fakeServer; fakeServer.setScenario( scenario ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session session("127.0.0.1", 5989); KIMAP::DeleteJob *job = new KIMAP::DeleteJob(&session); job->setMailBox(mailbox); bool result = job->exec(); QEXPECT_FAIL("bad" , "Expected failure on BAD response", Continue); QEXPECT_FAIL("no" , "Expected failure on NO response", Continue); QVERIFY(result); if (result) { QCOMPARE(job->mailBox(), mailbox); } fakeServer.quit(); } }; QTEST_KDEMAIN_CORE( DeleteJobTest ) #include "deletejobtest.moc" diff --git a/tests/fakeservertest.cpp b/tests/fakeservertest.cpp index bf21b69..1399615 100644 --- a/tests/fakeservertest.cpp +++ b/tests/fakeservertest.cpp @@ -1,68 +1,68 @@ /* Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "kimaptest/fakeserver.h" #include "kimap/session.h" #include "kimap/listjob.h" #include class FakeServerTest: public QObject { Q_OBJECT private Q_SLOTS: void testLoadScenario() { KIMAP::MailBoxDescriptor descriptor; QList listresult; descriptor.separator = '/'; descriptor.name = "INBOX"; listresult << descriptor; descriptor.separator = '/'; descriptor.name = QString::fromUtf8( "INBOX/ä ö ü @ €" ); listresult << descriptor; descriptor.separator = '/'; descriptor.name = "INBOX/lost+found"; listresult << descriptor; descriptor.separator = '/'; descriptor.name = "INBOX/lost+found/Calendar Public-20080128"; listresult << descriptor; FakeServer fakeServer; fakeServer.addScenarioFromFile( TEST_DATA "/fakeserverscenario.log" ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session session("127.0.0.1", 5989); KIMAP::ListJob *job = new KIMAP::ListJob(&session); job->setIncludeUnsubscribed(true); QVERIFY(job->exec()); fakeServer.quit(); } }; QTEST_KDEMAIN_CORE( FakeServerTest ) #include "fakeservertest.moc" diff --git a/tests/kimaptest/fakeserver.cpp b/tests/kimaptest/fakeserver.cpp index 5eff9be..412f677 100644 --- a/tests/kimaptest/fakeserver.cpp +++ b/tests/kimaptest/fakeserver.cpp @@ -1,205 +1,217 @@ /* Copyright (C) 2008 Omat Holding B.V. Copyright (C) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ // Own #include "fakeserver.h" // Qt #include #include // KDE #include #include #include "kimap/imapstreamparser.h" QByteArray FakeServer::preauth() { return "S: * PREAUTH localhost Test Library server ready"; } QByteArray FakeServer::greeting() { return "S: * OK localhost Test Library server ready"; } FakeServer::FakeServer( QObject* parent ) : QThread( parent ) { moveToThread(this); } FakeServer::~FakeServer() { quit(); wait(); } +void FakeServer::startAndWait() +{ + start(); + // this will block until the event queue starts + QMetaObject::invokeMethod( this, "started", Qt::BlockingQueuedConnection ); +} + void FakeServer::dataAvailable() { QMutexLocker locker(&m_mutex); QTcpSocket *socket = qobject_cast( sender() ); Q_ASSERT( socket!=0 ); int scenarioNumber = m_clientSockets.indexOf( socket ); QVERIFY( !m_scenarios[scenarioNumber].isEmpty() ); readClientPart( scenarioNumber ); writeServerPart( scenarioNumber ); } void FakeServer::newConnection() { QMutexLocker locker(&m_mutex); m_clientSockets << m_tcpServer->nextPendingConnection(); connect(m_clientSockets.last(), SIGNAL(readyRead()), this, SLOT(dataAvailable())); m_clientParsers << new KIMAP::ImapStreamParser( m_clientSockets.last() ); QVERIFY( m_clientSockets.size() <= m_scenarios.size() ); writeServerPart( m_clientSockets.size() - 1 ); } void FakeServer::run() { m_tcpServer = new QTcpServer(); if ( !m_tcpServer->listen( QHostAddress( QHostAddress::LocalHost ), 5989 ) ) { kFatal() << "Unable to start the server"; } connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection())); exec(); qDeleteAll( m_clientParsers ); qDeleteAll( m_clientSockets ); delete m_tcpServer; } +void FakeServer::started() +{ + // do nothing: this is a dummy slot used by startAndWait() +} + void FakeServer::setScenario( const QList &scenario ) { QMutexLocker locker(&m_mutex); m_scenarios.clear(); m_scenarios << scenario; } void FakeServer::addScenario( const QList &scenario ) { QMutexLocker locker(&m_mutex); m_scenarios << scenario; } void FakeServer::addScenarioFromFile( const QString &fileName ) { QFile file( fileName ); file.open( QFile::ReadOnly ); QList scenario; // When loading from files we never have the authentication phase // force jumping directly to authenticated state. scenario << preauth(); while ( !file.atEnd() ) { scenario << file.readLine().trimmed(); } file.close(); addScenario( scenario ); } bool FakeServer::isScenarioDone( int scenarioNumber ) const { QMutexLocker locker(&m_mutex); if ( scenarioNumber < m_scenarios.size() ) { return m_scenarios[scenarioNumber].isEmpty(); } else { return true; // Non existent hence empty, right? } } bool FakeServer::isAllScenarioDone() const { QMutexLocker locker( &m_mutex ); foreach ( const QList &scenario, m_scenarios ) { if ( !scenario.isEmpty() ) { return false; } } return true; } void FakeServer::writeServerPart( int scenarioNumber ) { QList scenario = m_scenarios[scenarioNumber]; QTcpSocket *clientSocket = m_clientSockets[scenarioNumber]; while ( !scenario.isEmpty() && scenario.first().startsWith( "S: " ) ) { QByteArray payload = scenario.takeFirst().mid( 3 ); clientSocket->write( payload + "\r\n" ); } if ( !scenario.isEmpty() && scenario.first().startsWith( "X" ) ) { scenario.takeFirst(); clientSocket->close(); } if ( !scenario.isEmpty() ) { QVERIFY( scenario.first().startsWith( "C: " ) ); } m_scenarios[scenarioNumber] = scenario; } void FakeServer::readClientPart( int scenarioNumber ) { QList scenario = m_scenarios[scenarioNumber]; KIMAP::ImapStreamParser *clientParser = m_clientParsers[scenarioNumber]; while ( !scenario.isEmpty() && scenario.first().startsWith( "C: " ) ) { QByteArray received = "C: "+clientParser->readUntilCommandEnd().trimmed(); QByteArray expected = scenario.takeFirst(); QCOMPARE( QString::fromUtf8( received ), QString::fromUtf8( expected ) ); QCOMPARE( received, expected ); } if ( !scenario.isEmpty() ) { QVERIFY( scenario.first().startsWith( "S: " ) ); } m_scenarios[scenarioNumber] = scenario; } #include "fakeserver.moc" diff --git a/tests/kimaptest/fakeserver.h b/tests/kimaptest/fakeserver.h index 3ce16fc..22b970c 100644 --- a/tests/kimaptest/fakeserver.h +++ b/tests/kimaptest/fakeserver.h @@ -1,214 +1,222 @@ /* Copyright (C) 2008 Omat Holding B.V. Copyright (C) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef FAKESERVER_H #define FAKESERVER_H #include #include #include #include #include namespace KIMAP { class ImapStreamParser; } Q_DECLARE_METATYPE( QList ) /** * Pretends to be an IMAP server for the purposes of unit tests. * * FakeServer does not really understand the IMAP protocol. Instead, * you give it a script, or scenario, that lists how an IMAP session * exchange should go. When it receives the client parts of the * scenario, it will respond with the following server parts. * * The server can be furnished with several scenarios. The first * scenario will be played out to the first client that connects, the * second scenario to the second client connection and so on. * * The fake server runs as a separate thread in the same process it * is started from, and listens for connections on port 5989 on the * local machine. * * Scenarios are in the form of protocol messages, with a tag at the * start to indicate whether it is message that will be sent by the * client ("C:") or a response that should be sent by the server * ("S:"). For example: * @code * C: A000001 LIST "" * * S: * LIST ( \HasChildren ) / INBOX * S: * LIST ( \HasNoChildren ) / INBOX/&AOQ- &APY- &APw- @ &IKw- * S: * LIST ( \HasChildren ) / INBOX/lost+found * S: * LIST ( \HasNoChildren ) / "INBOX/lost+found/Calendar Public-20080128" * S: A000001 OK LIST completed * @endcode * * A line starting with X indicates that the connection should be * closed by the server. This should be the last line in the * scenario. For example, the following simulates the server closing * the connection after receiving too many bad commands: * @code * C: A000001 madhatter * S: A000001 BAD Command madhatter * X * @endcode * * FakeServer::preauth() and FakeServer::greeting() provide standard * PREAUTH and OK responses, respectively, that can be used (unmodified) * as the first line of a scenario. * * A typical usage is something like * @code * QList scenario; * scenario << FakeServer::preauth() * << "C: A000001 CAPABILITY" * << "S: * CAPABILITY IMAP4rev1 STARTTLS AUTH=GSSAPI" * << "S: A000001 OK CAPABILITY completed"; * * FakeServer fakeServer; * fakeServer.setScenario( scenario ); - * fakeServer.start(); + * fakeServer.startAndWait(); * * KIMAP::Session session( "127.0.0.1", 5989 ); * KIMAP::CapabilitiesJob *job = new KIMAP::CapabilitiesJob(&session); * QVERIFY( job->exec() ); * // check the returned capabilities * * fakeServer.quit(); * @endcode */ class FakeServer : public QThread { Q_OBJECT public: /** * Get the default PREAUTH response * * This is the initial PREAUTH message that the server * sends at the start of a session to indicate that the * user is already authenticated by some other mechanism. * * Can be used as the first line in a scenario where * you want to skip the LOGIN stage of the protocol. */ static QByteArray preauth(); /** * Get the default greeting * * This is the initial OK message that the server sends at the * start of a session to indicate that a LOGIN is required. * * Can be used as the first line in a scenario where * you want to use the LOGIN command. */ static QByteArray greeting(); FakeServer( QObject* parent = 0 ); ~FakeServer(); + /** + * Starts the server and waits for it to be ready + * + * You should use this instead of start() to avoid race conditions. + */ + void startAndWait(); + /** * Starts the fake IMAP server * * You should not call this directly. Use start() instead. * * @reimp */ virtual void run(); /** * Removes any previously-added scenarios, and adds a new one * * After this, there will only be one scenario, and so the fake * server will only be able to service a single request. More * scenarios can be added with addScenario, though. * * @see addScenario()\n * addScenarioFromFile() */ void setScenario( const QList &scenario ); /** * Adds a new scenario * * Note that scenarios will be used in the order that clients * connect. If this is the 5th scenario that has been added * (bearing in mind that setScenario() resets the scenario * count), it will be used to service the 5th client that * connects. * * @see addScenarioFromFile() * * @param scenario the scenario as a list of messages */ void addScenario( const QList &scenario ); /** * Adds a new scenario from a local file * * Note that scenarios will be used in the order that clients * connect. If this is the 5th scenario that has been added * (bearing in mind that setScenario() resets the scenario * count), it will be used to service the 5th client that * connects. * * @see addScenario() * * @param fileName the name of the file that contains the * scenario; it will be split at line * boundaries, and excess whitespace will * be trimmed from the start and end of lines */ void addScenarioFromFile( const QString &fileName ); /** * Checks whether a particular scenario has completed * * @param scenarioNumber the number of the scenario to check, * in order of addition/client connection */ bool isScenarioDone( int scenarioNumber ) const; /** * Whether all the scenarios that were added to the fake * server have been completed. */ bool isAllScenarioDone() const; private Q_SLOTS: void newConnection(); void dataAvailable(); + void started(); private: void writeServerPart( int scenarioNumber ); void readClientPart( int scenarioNumber ); QList< QList > m_scenarios; QTcpServer *m_tcpServer; mutable QMutex m_mutex; QList m_clientSockets; QList m_clientParsers; }; #endif diff --git a/tests/listjobtest.cpp b/tests/listjobtest.cpp index 15864b9..4eb6a21 100644 --- a/tests/listjobtest.cpp +++ b/tests/listjobtest.cpp @@ -1,159 +1,159 @@ /* Copyright (C) 2009 Andras Mantia Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "kimaptest/fakeserver.h" #include "kimap/session.h" #include "kimap/listjob.h" #include #include #include Q_DECLARE_METATYPE(QList) class ListJobTest: public QObject { Q_OBJECT private Q_SLOTS: void testList_data() { QTest::addColumn( "unsubscribed" ); QTest::addColumn >( "scenario" ); QTest::addColumn >( "listresult" ); QList scenario; scenario << FakeServer::preauth() << "C: A000001 LIST \"\" *" << "S: * LIST ( \\HasChildren ) / INBOX" << "S: * LIST ( \\HasNoChildren ) / INBOX/&AOQ- &APY- &APw- @ &IKw-" << "S: * LIST ( \\HasChildren ) / INBOX/lost+found" << "S: * LIST ( \\HasNoChildren ) / \"INBOX/lost+found/Calendar Public-20080128\"" << "S: A000001 OK LIST completed"; KIMAP::MailBoxDescriptor descriptor; QList listresult; descriptor.separator = '/'; descriptor.name = "INBOX"; listresult << descriptor; descriptor.separator = '/'; descriptor.name = QString::fromUtf8( "INBOX/ä ö ü @ €" ); listresult << descriptor; descriptor.separator = '/'; descriptor.name = "INBOX/lost+found"; listresult << descriptor; descriptor.separator = '/'; descriptor.name = "INBOX/lost+found/Calendar Public-20080128"; listresult << descriptor; QTest::newRow( "normal" ) << true << scenario << listresult; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 LSUB \"\" *" << "S: * LSUB ( \\HasChildren ) / INBOX" << "S: * LSUB ( ) / INBOX/Calendar/3196" << "S: * LSUB ( \\HasChildren ) / INBOX/Calendar/ff" << "S: * LSUB ( ) / INBOX/Calendar/ff/hgh" << "S: * LSUB ( ) / user/test2/Calendar" << "S: A000001 OK LSUB completed"; listresult.clear(); descriptor.separator = '/'; descriptor.name = "INBOX"; listresult << descriptor; descriptor.separator = '/'; descriptor.name = "INBOX/Calendar/3196"; listresult << descriptor; descriptor.separator = '/'; descriptor.name = "INBOX/Calendar/ff"; listresult << descriptor; descriptor.separator = '/'; descriptor.name = "INBOX/Calendar/ff/hgh"; listresult << descriptor; descriptor.separator = '/'; descriptor.name = "user/test2/Calendar"; listresult << descriptor; QTest::newRow( "subscribed" ) << false << scenario << listresult; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 LIST \"\" *" << "S: * LIST ( \\HasNoChildren ) / INBOX/lost+found/Calendar Public-20080128" << "S: A000001 OK LIST completed"; listresult.clear(); descriptor.separator = '/'; descriptor.name = "INBOX/lost+found/Calendar Public-20080128"; listresult << descriptor; QTest::newRow( "unquoted-space" ) << true << scenario << listresult; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 LIST \"\" *" << "S: A000001 BAD command unknown or arguments invalid"; listresult.clear(); QTest::newRow( "bad" ) << true << scenario << listresult; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 LIST \"\" *" << "S: A000001 NO list failure"; QTest::newRow( "no" ) << true << scenario << listresult; } void testList() { QFETCH( bool, unsubscribed); QFETCH( QList, scenario ); QFETCH( QList, listresult ); FakeServer fakeServer; fakeServer.setScenario( scenario ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session session("127.0.0.1", 5989); KIMAP::ListJob *job = new KIMAP::ListJob(&session); job->setIncludeUnsubscribed(unsubscribed); bool result = job->exec(); QEXPECT_FAIL("bad" , "Expected failure on BAD response", Continue); QEXPECT_FAIL("no" , "Expected failure on NO response", Continue); QVERIFY(result); if (result) { //kDebug() << job->mailBoxes().first().name; //kDebug() << listresult.first().name; QCOMPARE(job->mailBoxes(), listresult); // kDebug() << job->flags(); } // QCOMPARE(job->mailBox(), mailbox); fakeServer.quit(); } }; QTEST_KDEMAIN_CORE( ListJobTest ) #include "listjobtest.moc" diff --git a/tests/loginjobtest.cpp b/tests/loginjobtest.cpp index b338760..2de063b 100644 --- a/tests/loginjobtest.cpp +++ b/tests/loginjobtest.cpp @@ -1,92 +1,92 @@ /* Copyright (C) 2009 Andras Mantia Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "kimaptest/fakeserver.h" #include "kimap/session.h" #include "kimap/loginjob.h" #include #include #include class LoginJobTest: public QObject { Q_OBJECT private Q_SLOTS: void shouldHandleLogin_data() { QTest::addColumn( "user" ); QTest::addColumn( "password" ); QTest::addColumn< QList >( "scenario" ); QList scenario; scenario << FakeServer::greeting() << "C: A000001 LOGIN user password" << "S: A000001 OK User logged in"; QTest::newRow( "success" ) << "user" << "password" << scenario; scenario.clear(); scenario << FakeServer::greeting() << "C: A000001 LOGIN user_bad password" << "S: A000001 NO Login failed: authentication failure"; QTest::newRow( "wrong login" ) << "user_bad" << "password" << scenario; scenario.clear(); scenario << FakeServer::preauth(); QTest::newRow( "already authenticated" ) << "user" << "password" << scenario; } void shouldHandleLogin() { QFETCH( QString, user ); QFETCH( QString, password ); QFETCH( QList, scenario ); FakeServer fakeServer; fakeServer.setScenario( scenario ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session *session = new KIMAP::Session("127.0.0.1", 5989); KIMAP::LoginJob *login = new KIMAP::LoginJob(session); login->setUserName(user); login->setPassword(password); bool result = login->exec(); QEXPECT_FAIL("wrong login","Login with bad user name", Continue); QEXPECT_FAIL("already authenticated","Trying to log on an already authenticated session", Continue); QVERIFY(result); fakeServer.quit(); delete session; } }; QTEST_KDEMAIN_CORE( LoginJobTest ) #include "loginjobtest.moc" diff --git a/tests/logoutjobtest.cpp b/tests/logoutjobtest.cpp index 718d20a..9028265 100644 --- a/tests/logoutjobtest.cpp +++ b/tests/logoutjobtest.cpp @@ -1,81 +1,81 @@ /* Copyright (C) 2009 Andras Mantia Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "kimaptest/fakeserver.h" #include "kimap/session.h" #include "kimap/logoutjob.h" #include "kimap/loginjob.h" #include #include #include class LogoutJobTest: public QObject { Q_OBJECT private Q_SLOTS: void testLogout() { FakeServer fakeServer; fakeServer.setScenario( QList() << FakeServer::preauth() << "C: A000001 LOGOUT" << "S: A000001 OK LOGOUT completed" ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session *session = new KIMAP::Session("127.0.0.1", 5989); KIMAP::LogoutJob *logout = new KIMAP::LogoutJob(session); QVERIFY(logout->exec()); fakeServer.quit(); delete session; } void testLogoutUntagged() { FakeServer fakeServer; fakeServer.setScenario( QList() << FakeServer::preauth() << "C: A000001 LOGOUT" << "S: * some untagged response" << "S: A000001 OK LOGOUT completed" ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session *session = new KIMAP::Session("127.0.0.1", 5989); KIMAP::LogoutJob *logout = new KIMAP::LogoutJob(session); QVERIFY(logout->exec()); fakeServer.quit(); delete session; } }; QTEST_KDEMAIN_CORE( LogoutJobTest ) #include "logoutjobtest.moc" diff --git a/tests/renamejobtest.cpp b/tests/renamejobtest.cpp index fe5f83d..a139a24 100644 --- a/tests/renamejobtest.cpp +++ b/tests/renamejobtest.cpp @@ -1,91 +1,91 @@ /* Copyright (C) 2009 Andras Mantia Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "kimaptest/fakeserver.h" #include "kimap/session.h" #include "kimap/renamejob.h" #include #include #include class RenameJobTest: public QObject { Q_OBJECT private Q_SLOTS: void testRename_data() { QTest::addColumn( "mailbox" ); QTest::addColumn( "newname" ); QTest::addColumn >( "scenario" ); QList scenario; scenario << FakeServer::preauth() << "C: A000001 RENAME \"INBOX\" \"oldmail\"" << "S: A000001 OK RENAME completed"; QTest::newRow( "good" ) << "INBOX" << "oldmail" << scenario; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 RENAME \"INBOX-FAIL-BAD\" \"oldmail-bad\"" << "S: A000001 BAD command unknown or arguments invalid"; QTest::newRow( "bad" ) << "INBOX-FAIL-BAD" << "oldmail-bad" << scenario; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 RENAME \"INBOX-FAIL-NO\" \"oldmail-no\"" << "S: A000001 NO rename failure"; QTest::newRow( "no" ) << "INBOX-FAIL-NO" << "oldmail-no" << scenario; } void testRename() { QFETCH( QString, mailbox ); QFETCH( QString, newname ); QFETCH( QList, scenario ); FakeServer fakeServer; fakeServer.setScenario( scenario ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session session("127.0.0.1", 5989); KIMAP::RenameJob *job = new KIMAP::RenameJob(&session); job->setSourceMailBox(mailbox); job->setDestinationMailBox(newname); bool result = job->exec(); QEXPECT_FAIL("bad" , "Expected failure on BAD response", Continue); QEXPECT_FAIL("no" , "Expected failure on NO response", Continue); QVERIFY(result); QCOMPARE(job->sourceMailBox(), mailbox); QCOMPARE(job->destinationMailBox(), newname); fakeServer.quit(); } }; QTEST_KDEMAIN_CORE( RenameJobTest ) #include "renamejobtest.moc" diff --git a/tests/selectjobtest.cpp b/tests/selectjobtest.cpp index 5107f9f..9ee2a80 100644 --- a/tests/selectjobtest.cpp +++ b/tests/selectjobtest.cpp @@ -1,147 +1,147 @@ /* Copyright (C) 2009 Andras Mantia Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "kimaptest/fakeserver.h" #include "kimap/loginjob.h" #include "kimap/session.h" #include "kimap/selectjob.h" #include #include #include class SelectJobTest: public QObject { Q_OBJECT private Q_SLOTS: void testSingleSelect_data() { QTest::addColumn >( "scenario" ); QTest::addColumn >( "flags" ); QTest::addColumn >( "permanentflags" ); QTest::addColumn( "messagecount" ); QTest::addColumn( "recentcount" ); QTest::addColumn( "firstUnseenIndex" ); QTest::addColumn( "uidValidity" ); QTest::addColumn( "nextUid" ); QList scenario; QList flags; QList permanentflags; scenario << FakeServer::preauth() << "C: A000001 SELECT \"INBOX\"" << "S: * 172 EXISTS" << "S: * 1 RECENT" << "S: * OK [UNSEEN 12] Message 12 is first unseen" << "S: * OK [UIDVALIDITY 3857529045] UIDs valid" << "S: * OK [UIDNEXT 4392] Predicted next UID" << "S: * FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)" << "S: * OK [PERMANENTFLAGS (\\Deleted \\Seen \\*)] Limited" << "S: A000001 OK [READ-WRITE] SELECT completed"; flags << "\\Answered" << "\\Flagged" << "\\Deleted" << "\\Seen" << "\\Draft"; permanentflags << "\\Deleted" << "\\Seen" << "\\*"; QTest::newRow( "good" ) << scenario << flags << permanentflags << 172 << 1 << 12 << (qint64)3857529045 << (qint64)4392; scenario.clear(); flags.clear(); permanentflags.clear(); scenario << FakeServer::preauth() << "C: A000001 SELECT \"INBOX\"" << "S: A000001 BAD command unknown or arguments invalid"; QTest::newRow( "bad" ) << scenario << flags << permanentflags << 0 << 0 << 0 << (qint64)0 << (qint64)0; scenario.clear(); flags.clear(); permanentflags.clear(); scenario << FakeServer::preauth() << "C: A000001 SELECT \"INBOX\"" << "S: A000001 NO select failure"; QTest::newRow( "no" ) << scenario << flags << permanentflags << 0 << 0 << 0 << (qint64)0 << (qint64)0; } void testSingleSelect() { QFETCH( QList, scenario ); QFETCH( QList, flags ); QFETCH( QList, permanentflags ); QFETCH( int, messagecount); QFETCH( int, recentcount); QFETCH( int, firstUnseenIndex); QFETCH( qint64, uidValidity); QFETCH( qint64, nextUid); FakeServer fakeServer; fakeServer.setScenario( scenario ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session session("127.0.0.1", 5989); KIMAP::SelectJob *job = new KIMAP::SelectJob(&session); job->setMailBox("INBOX"); bool result = job->exec(); QEXPECT_FAIL("bad" , "Expected failure on BAD scenario", Continue); QEXPECT_FAIL("no" , "Expected failure on NO scenario", Continue); QVERIFY(result); if (result) { QCOMPARE(job->flags(), flags); QCOMPARE(job->permanentFlags(), permanentflags); QCOMPARE(job->messageCount(), messagecount); QCOMPARE(job->recentCount(), recentcount); QCOMPARE(job->firstUnseenIndex(), firstUnseenIndex); QCOMPARE(job->uidValidity(), uidValidity); QCOMPARE(job->nextUid(), nextUid); } fakeServer.quit(); } void testSeveralSelect() { FakeServer fakeServer; fakeServer.setScenario( QList() << FakeServer::preauth() << "C: A000001 SELECT \"INBOX\"" << "S: A000001 OK [READ-WRITE] SELECT completed" << "C: A000002 SELECT \"INBOX/Foo\"" << "S: A000002 OK [READ-WRITE] SELECT completed" ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session session("127.0.0.1", 5989); KIMAP::SelectJob *job = new KIMAP::SelectJob(&session); job->setMailBox("INBOX"); QVERIFY(job->exec()); job = new KIMAP::SelectJob(&session); job->setMailBox("INBOX/Foo"); QVERIFY(job->exec()); } }; QTEST_KDEMAIN_CORE( SelectJobTest ) #include "selectjobtest.moc" diff --git a/tests/storejobtest.cpp b/tests/storejobtest.cpp index fa51eab..fca7691 100644 --- a/tests/storejobtest.cpp +++ b/tests/storejobtest.cpp @@ -1,100 +1,100 @@ /* Copyright (C) 2009 Kevin Ottens Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "kimaptest/fakeserver.h" #include "kimap/session.h" #include "kimap/storejob.h" #include #include #include class StoreJobTest: public QObject { Q_OBJECT private Q_SLOTS: void testStore_data() { QTest::addColumn( "uidBased" ); QTest::addColumn( "id" ); QTest::addColumn( "uid" ); QTest::addColumn< QList >( "flags" ); QTest::addColumn< QList >( "scenario" ); QList scenario; scenario << FakeServer::preauth() << "C: A000001 STORE 3 FLAGS (\\Seen \\Foo)" << "S: * 3 FETCH (FLAGS (\\Seen \\Foo) UID 1096)" << "S: A000001 OK STORE completed"; QTest::newRow( "not uid based" ) << false << qint64(3) << qint64(1096) << ( QList() << "\\Seen" << "\\Foo" ) << scenario; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 UID STORE 1096 FLAGS (\\Seen \\Foo)" << "S: * 3 FETCH (FLAGS (\\Seen \\Foo) UID 1096)" << "S: A000001 OK STORE completed"; QTest::newRow( "uid based" ) << true << qint64(3) << qint64(1096) << ( QList() << "\\Seen" << "\\Foo" ) << scenario; } void testStore() { QFETCH( bool, uidBased ); QFETCH( qint64, id ); QFETCH( qint64, uid ); QFETCH( QList, flags ); QFETCH( QList, scenario ); FakeServer fakeServer; fakeServer.setScenario( scenario ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session session("127.0.0.1", 5989); KIMAP::StoreJob *job = new KIMAP::StoreJob(&session); job->setUidBased( uidBased ); job->setSequenceSet( KIMAP::ImapSet( uidBased ? uid : id ) ); job->setFlags( flags ); job->setMode( KIMAP::StoreJob::SetFlags ); bool result = job->exec(); QVERIFY(result); if ( uidBased ) { QVERIFY( job->resultingFlags().contains( uid ) ); } else { QVERIFY( job->resultingFlags().contains( id ) ); } fakeServer.quit(); } }; QTEST_KDEMAIN_CORE( StoreJobTest ) #include "storejobtest.moc" diff --git a/tests/subscribejobtest.cpp b/tests/subscribejobtest.cpp index b8d46e3..f314345 100644 --- a/tests/subscribejobtest.cpp +++ b/tests/subscribejobtest.cpp @@ -1,87 +1,87 @@ /* Copyright (C) 2009 Andras Mantia Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "kimaptest/fakeserver.h" #include "kimap/session.h" #include "kimap/subscribejob.h" #include #include #include class SubscribeJobTest: public QObject { Q_OBJECT private Q_SLOTS: void testSubscribe_data() { QTest::addColumn( "mailbox" ); QTest::addColumn< QList >( "scenario" ); QList scenario; scenario << FakeServer::preauth() << "C: A000001 SUBSCRIBE \"INBOX/foo\"" << "S: A000001 OK CREATE completed"; QTest::newRow( "good" ) << "INBOX/foo" << scenario ; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 SUBSCRIBE \"INBOX-FAIL-BAD\"" << "S: A000001 BAD command unknown or arguments invalid"; QTest::newRow( "bad" ) << "INBOX-FAIL-BAD" << scenario; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 SUBSCRIBE \"INBOX-FAIL-NO\"" << "S: A000001 NO subscribe failure"; QTest::newRow( "no" ) << "INBOX-FAIL-NO" << scenario ; } void testSubscribe() { QFETCH( QString, mailbox ); QFETCH( QList, scenario ); FakeServer fakeServer; fakeServer.setScenario( scenario ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session session("127.0.0.1", 5989); KIMAP::SubscribeJob *job = new KIMAP::SubscribeJob(&session); job->setMailBox(mailbox); bool result = job->exec(); QEXPECT_FAIL("bad" , "Expected failure on BAD scenario", Continue); QEXPECT_FAIL("no" , "Expected failure on NO scenario", Continue); QVERIFY(result); QCOMPARE(job->mailBox(), mailbox); fakeServer.quit(); } }; QTEST_KDEMAIN_CORE( SubscribeJobTest ) #include "subscribejobtest.moc" diff --git a/tests/testsession.cpp b/tests/testsession.cpp index c065302..6610a2e 100644 --- a/tests/testsession.cpp +++ b/tests/testsession.cpp @@ -1,205 +1,205 @@ /* This file is part of the KDE project Copyright (C) 2008 Kevin Ottens Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include "session.h" #include "job.h" #include "kimaptest/fakeserver.h" #include "kimaptest/mockjob.h" class SessionTest : public QObject { Q_OBJECT private slots: void shouldStartDisconnected() { FakeServer fakeServer; fakeServer.setScenario( QList() << FakeServer::greeting() ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session s( "127.0.0.1", 5989 ); QCOMPARE( ( int )s.state(), ( int )KIMAP::Session::Disconnected ); QTest::qWait( 500 ); QCOMPARE( ( int )s.state(), ( int )KIMAP::Session::NotAuthenticated ); } void shouldSupportPreauth() { FakeServer fakeServer; fakeServer.setScenario( QList() << FakeServer::preauth() ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session s( "127.0.0.1", 5989 ); QCOMPARE( ( int )s.state(), ( int )KIMAP::Session::Disconnected ); QTest::qWait( 500 ); QCOMPARE( ( int )s.state(), ( int )KIMAP::Session::Authenticated ); } void shouldRespectStartOrder() { FakeServer fakeServer; fakeServer.setScenario( QList() << FakeServer::greeting() ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session s("127.0.0.1", 5989); MockJob *j1 = new MockJob(&s); connect(j1, SIGNAL(result(KJob*)), this, SLOT(jobDone(KJob*))); MockJob *j2 = new MockJob(&s); connect(j2, SIGNAL(result(KJob*)), this, SLOT(jobDone(KJob*))); MockJob *j3 = new MockJob(&s); connect(j3, SIGNAL(result(KJob*)), this, SLOT(jobDone(KJob*))); MockJob *j4 = new MockJob(&s); connect(j4, SIGNAL(result(KJob*)), this, SLOT(jobDone(KJob*))); j4->start(); j2->start(); j3->start(); j1->start(); m_expectedCalls = 4; m_eventLoop.exec(); QCOMPARE(m_jobs.size(), 4); QCOMPARE(m_jobs[0], j4); QCOMPARE(m_jobs[1], j2); QCOMPARE(m_jobs[2], j3); QCOMPARE(m_jobs[3], j1); } void shouldManageQueueSize() { FakeServer fakeServer; fakeServer.setScenario( QList() << FakeServer::greeting() ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session s("127.0.0.1", 5989); QSignalSpy queueSpy(&s, SIGNAL(jobQueueSizeChanged(int))); QCOMPARE( s.jobQueueSize(), 0 ); MockJob *j1 = new MockJob(&s); MockJob *j2 = new MockJob(&s); MockJob *j3 = new MockJob(&s); MockJob *j4 = new MockJob(&s); connect(j4, SIGNAL(result(KJob*)), &m_eventLoop, SLOT(quit())); QCOMPARE( s.jobQueueSize(), 0 ); j1->start(); QCOMPARE( s.jobQueueSize(), 1 ); QCOMPARE( queueSpy.size(), 1 ); QCOMPARE( queueSpy.at( 0 ).at( 0 ).toInt(), 1 ); j2->start(); QCOMPARE( s.jobQueueSize(), 2 ); QCOMPARE( queueSpy.size(), 2 ); QCOMPARE( queueSpy.at( 1 ).at( 0 ).toInt(), 2 ); j3->start(); QCOMPARE( s.jobQueueSize(), 3 ); QCOMPARE( queueSpy.size(), 3 ); QCOMPARE( queueSpy.at( 2 ).at( 0 ).toInt(), 3 ); j4->start(); QCOMPARE( s.jobQueueSize(), 4 ); QCOMPARE( queueSpy.size(), 4 ); QCOMPARE( queueSpy.at( 3 ).at( 0 ).toInt(), 4 ); queueSpy.clear(); m_eventLoop.exec(); QCOMPARE( s.jobQueueSize(), 0 ); QCOMPARE( queueSpy.at( 0 ).at( 0 ).toInt(), 3 ); QCOMPARE( queueSpy.at( 1 ).at( 0 ).toInt(), 2 ); QCOMPARE( queueSpy.at( 2 ).at( 0 ).toInt(), 1 ); QCOMPARE( queueSpy.at( 3 ).at( 0 ).toInt(), 0 ); } void shouldTimeoutOnNoReply() { FakeServer fakeServer; fakeServer.setScenario( QList() << FakeServer::preauth() << "C: A000001 DUMMY" << "S: * DUMMY" << "S: * DUMMY" << "S: * DUMMY" << "S: * DUMMY" << "S: * DUMMY" << "S: * DUMMY" << "S: * DUMMY" << "S: * DUMMY" << "S: * DUMMY" << "S: * DUMMY" << "S: * DUMMY" << "S: * DUMMY" << "S: * DUMMY" // We never get a OK or anything, so the job can't normally complete ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session s( "127.0.0.1", 5989 ); QSignalSpy spy(&s, SIGNAL(connectionLost())); MockJob *mock = new MockJob(&s); mock->setCommand("DUMMY"); mock->exec(); // We expect to get an error here due to some timeout QVERIFY( mock->error()!=0 ); QCOMPARE( spy.count(), 1 ); } public slots: void jobDone(KJob *job) { m_jobs << job; if (m_expectedCalls==m_jobs.size()) { m_eventLoop.quit(); } } private: QEventLoop m_eventLoop; int m_expectedCalls; QList m_jobs; }; QTEST_MAIN(SessionTest) #include "testsession.moc" diff --git a/tests/unsubscribejobtest.cpp b/tests/unsubscribejobtest.cpp index 4a3e8bc..1b4afe4 100644 --- a/tests/unsubscribejobtest.cpp +++ b/tests/unsubscribejobtest.cpp @@ -1,87 +1,87 @@ /* Copyright (C) 2009 Andras Mantia Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "kimaptest/fakeserver.h" #include "kimap/session.h" #include "kimap/unsubscribejob.h" #include #include #include class UnsubscribeJobTest: public QObject { Q_OBJECT private Q_SLOTS: void testUnsubscribe_data() { QTest::addColumn( "mailbox" ); QTest::addColumn< QList >( "scenario" ); QList scenario; scenario << FakeServer::preauth() << "C: A000001 UNSUBSCRIBE \"#news.comp.mail.mime\"" << "S: A000001 OK UNSUBSCRIBE completed"; QTest::newRow( "good" ) << "#news.comp.mail.mime" << scenario; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 UNSUBSCRIBE \"INBOX-FAIL-BAD\"" << "S: A000001 BAD command unknown or arguments invalid"; QTest::newRow( "bad" ) << "INBOX-FAIL-BAD" << scenario; scenario.clear(); scenario << FakeServer::preauth() << "C: A000001 UNSUBSCRIBE \"INBOX-FAIL-NO\"" << "S: A000001 NO unsubscribe failure"; QTest::newRow( "no" ) << "INBOX-FAIL-NO" << scenario; } void testUnsubscribe() { QFETCH( QString, mailbox ); QFETCH( QList, scenario ); FakeServer fakeServer; fakeServer.setScenario( scenario ); - fakeServer.start(); + fakeServer.startAndWait(); KIMAP::Session session("127.0.0.1", 5989); KIMAP::UnsubscribeJob *job = new KIMAP::UnsubscribeJob(&session); job->setMailBox(mailbox); bool result = job->exec(); QEXPECT_FAIL("bad" , "Expected failure on BAD scenario", Continue); QEXPECT_FAIL("no" , "Expected failure on NO scenario", Continue); QVERIFY(result); QCOMPARE(job->mailBox(), mailbox); fakeServer.quit(); } }; QTEST_KDEMAIN_CORE( UnsubscribeJobTest ) #include "unsubscribejobtest.moc"