1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/*
This file is part of the KDE project
SPDX-FileCopyrightText: 2008 Kevin Ottens <ervin@kde.org>
SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
SPDX-FileContributor: Kevin Ottens <kevin@kdab.com>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include <QEventLoop>
#include <QObject>
#include <QSignalSpy>
#include <QTest>
#include "job.h"
#include "kimaptest/fakeserver.h"
#include "kimaptest/mockjob.h"
#include "session.h"
Q_DECLARE_METATYPE(KIMAP::Session::State)
Q_DECLARE_METATYPE(KJob *)
class SessionTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase()
{
qRegisterMetaType<KIMAP::Session::State>();
}
void shouldStartDisconnected()
{
FakeServer fakeServer;
fakeServer.setScenario(QList<QByteArray>() << FakeServer::greeting());
fakeServer.startAndWait();
KIMAP::Session s(QStringLiteral("127.0.0.1"), 5989);
QSignalSpy spy(&s, &KIMAP::Session::stateChanged);
QCOMPARE((int)s.state(), (int)KIMAP::Session::Disconnected);
QTest::qWait(600);
QCOMPARE((int)s.state(), (int)KIMAP::Session::NotAuthenticated);
QCOMPARE(spy.count(), 1); // NotAuthenticated
QList<QVariant> arguments = spy.takeFirst();
QCOMPARE((int)qvariant_cast<KIMAP::Session::State>(arguments.at(0)), (int)KIMAP::Session::NotAuthenticated);
QCOMPARE((int)qvariant_cast<KIMAP::Session::State>(arguments.at(1)), (int)KIMAP::Session::Disconnected);
}
void shouldFailForInvalidHosts()
{
#ifdef Q_OS_FREEBSD
QSKIP("QTest::qWait in this test triggers a busy-loop within Qt");
#endif
KIMAP::Session s(QStringLiteral("0.0.0.0"), 1234);
s.setTimeout(1); // 1 second timeout
QSignalSpy spyFail(&s, &KIMAP::Session::connectionFailed);
QSignalSpy spyLost(&s, &KIMAP::Session::connectionLost);
QSignalSpy spyState(&s, &KIMAP::Session::stateChanged);
QCOMPARE((int)s.state(), (int)KIMAP::Session::Disconnected);
QTest::qWait(500);
QCOMPARE((int)s.state(), (int)KIMAP::Session::Disconnected);
QCOMPARE(spyFail.count(), 1);
QCOMPARE(spyLost.count(), 0);
QCOMPARE(spyState.count(), 0);
// Wait 800ms more. So now it's 1.3 seconds, check that the socket timeout has correctly been
// disabled, and that it hadn't fired unexpectedly.
QTest::qWait(800);
QCOMPARE(spyFail.count(), 1);
}
/**
Checks that the timeout works when the connection succeeds, but the server doesn't sends anything
back to the client. This could happen for example if we connected to a non-IMAP server.
*/
void shouldTimeoutOnNoGreeting()
{
FakeServer fakeServer;
fakeServer.setScenario(QList<QByteArray>());
fakeServer.startAndWait();
KIMAP::Session s(QStringLiteral("127.0.0.1"), 5989);
s.setTimeout(2);
QSignalSpy spyFail(&s, &KIMAP::Session::connectionFailed);
QSignalSpy spyLost(&s, &KIMAP::Session::connectionLost);
QSignalSpy spyState(&s, &KIMAP::Session::stateChanged);
QCOMPARE((int)s.state(), (int)KIMAP::Session::Disconnected);
// Wait 1.8 second. Since the timeout is set to 2 seconds, the socket should be still
// disconnected at this point, yet the connectionFailed() signal shouldn't have been emitted.
QTest::qWait(1800);
QCOMPARE((int)s.state(), (int)KIMAP::Session::Disconnected);
QCOMPARE(spyFail.count(), 0);
QCOMPARE(spyLost.count(), 0);
QCOMPARE(spyState.count(), 0);
// Wait 0.5 second more. Now we are at 2.3 seconds, the socket should have timed out, and the
// connectionFailed() signal should have been emitted.
QTest::qWait(500);
QCOMPARE((int)s.state(), (int)KIMAP::Session::Disconnected);
QCOMPARE(spyFail.count(), 0);
QCOMPARE(spyLost.count(), 1);
QCOMPARE(spyState.count(), 0);
}
void shouldSupportPreauth()
{
FakeServer fakeServer;
fakeServer.setScenario(QList<QByteArray>() << FakeServer::preauth());
fakeServer.startAndWait();
KIMAP::Session s(QStringLiteral("127.0.0.1"), 5989);
QSignalSpy spy(&s, &KIMAP::Session::stateChanged);
QCOMPARE((int)s.state(), (int)KIMAP::Session::Disconnected);
QTest::qWait(500);
QCOMPARE((int)s.state(), (int)KIMAP::Session::Authenticated);
QCOMPARE(spy.count(), 1); // Authenticated
QList<QVariant> arguments = spy.takeFirst();
QCOMPARE((int)qvariant_cast<KIMAP::Session::State>(arguments.at(0)), (int)KIMAP::Session::Authenticated);
QCOMPARE((int)qvariant_cast<KIMAP::Session::State>(arguments.at(1)), (int)KIMAP::Session::Disconnected);
}
void shouldRespectStartOrder()
{
FakeServer fakeServer;
fakeServer.setScenario(QList<QByteArray>() << FakeServer::greeting());
fakeServer.startAndWait();
KIMAP::Session s(QStringLiteral("127.0.0.1"), 5989);
auto j1 = new MockJob(&s);
connect(j1, &KJob::result, this, &SessionTest::jobDone);
auto j2 = new MockJob(&s);
connect(j2, &KJob::result, this, &SessionTest::jobDone);
auto j3 = new MockJob(&s);
connect(j3, &KJob::result, this, &SessionTest::jobDone);
auto j4 = new MockJob(&s);
connect(j4, &KJob::result, this, &SessionTest::jobDone);
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<QByteArray>() << FakeServer::greeting());
fakeServer.startAndWait();
KIMAP::Session s(QStringLiteral("127.0.0.1"), 5989);
QSignalSpy queueSpy(&s, &KIMAP::Session::jobQueueSizeChanged);
QCOMPARE(s.jobQueueSize(), 0);
auto j1 = new MockJob(&s);
auto j2 = new MockJob(&s);
auto j3 = new MockJob(&s);
auto j4 = new MockJob(&s);
connect(j4, &KJob::result, &m_eventLoop, &QEventLoop::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<QByteArray>() << 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.startAndWait();
KIMAP::Session s(QStringLiteral("127.0.0.1"), 5989);
QSignalSpy spyFail(&s, &KIMAP::Session::connectionFailed);
QSignalSpy spyLost(&s, &KIMAP::Session::connectionLost);
QSignalSpy spyState(&s, &KIMAP::Session::stateChanged);
auto 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(spyFail.count(), 0);
QCOMPARE(spyLost.count(), 1);
QCOMPARE(spyState.count(), 2); // Authenticated, Disconnected
}
void shouldFailFirstJobOnConnectionFailed()
{
qRegisterMetaType<KJob *>();
FakeServer fakeServer;
fakeServer.setScenario(QList<QByteArray>());
fakeServer.startAndWait();
KIMAP::Session s(QStringLiteral("127.0.0.1"), 5989);
s.setTimeout(1);
auto j1 = new MockJob(&s);
QSignalSpy spyResult1(j1, &KJob::result);
QSignalSpy spyDestroyed1(j1, &QObject::destroyed);
auto j2 = new MockJob(&s);
QSignalSpy spyResult2(j2, &KJob::result);
QSignalSpy spyDestroyed2(j2, &QObject::destroyed);
auto j3 = new MockJob(&s);
QSignalSpy spyResult3(j3, &KJob::result);
QSignalSpy spyDestroyed3(j3, &QObject::destroyed);
j1->start();
j2->start();
j3->start();
QCOMPARE(s.jobQueueSize(), 3);
QTest::qWait(1100);
// Check that only the first job has emitted it's result
QCOMPARE(spyResult1.count(), 1);
QCOMPARE(spyResult2.count(), 0);
QCOMPARE(spyResult3.count(), 0);
// Check that all jobs have been deleted
QCOMPARE(spyDestroyed1.count(), 1);
QCOMPARE(spyDestroyed2.count(), 1);
QCOMPARE(spyDestroyed3.count(), 1);
QCOMPARE(s.jobQueueSize(), 0);
}
void shouldCloseOnInconsistency()
{
for (int count = 0; count < 10; count++) {
FakeServer fakeServer;
fakeServer.setScenario(QList<QByteArray>() << FakeServer::preauth() << "C: A000001 DUMMY"
<< "S: * DUMMY %"
<< "S: DUMMY)");
fakeServer.startAndWait();
KIMAP::Session s(QStringLiteral("127.0.0.1"), 5989);
QSignalSpy spyFail(&s, &KIMAP::Session::connectionFailed);
QSignalSpy spyLost(&s, &KIMAP::Session::connectionLost);
QSignalSpy spyState(&s, &KIMAP::Session::stateChanged);
auto mock = new MockJob(&s);
mock->setTimeout(5000);
mock->setCommand("DUMMY");
mock->setAutoDelete(false);
mock->start();
QTest::qWait(250); // Should be plenty
// We expect to get an error here due to the inconsistency
QVERIFY(mock->error() != 0);
QCOMPARE(spyFail.count(), 0);
QCOMPARE(spyLost.count(), 1);
QCOMPARE(spyState.count(), 2); // Authenticated, Disconnected
delete mock;
}
}
public Q_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<KJob *> m_jobs;
};
QTEST_GUILESS_MAIN(SessionTest)
#include "testsession.moc"
|