selectjobtest.cpp 11 KB
Newer Older
Andras Mantia's avatar
Andras Mantia committed
1
/*
2
   SPDX-FileCopyrightText: 2009 Andras Mantia <amantia@kde.org>
Andras Mantia's avatar
Andras Mantia committed
3

4 5
   SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
   SPDX-FileContributor: Kevin Ottens <kevin@kdab.com>
6

7
   SPDX-License-Identifier: GPL-2.0-or-later
Andras Mantia's avatar
Andras Mantia committed
8 9
*/

Laurent Montel's avatar
Laurent Montel committed
10
#include <QTest>
Andras Mantia's avatar
Andras Mantia committed
11

12
#include "kimaptest/fakeserver.h"
13 14 15
#include "kimap/loginjob.h"
#include "kimap/session.h"
#include "kimap/selectjob.h"
Andras Mantia's avatar
Andras Mantia committed
16

17
#include <QTest>
18 19 20 21 22 23
#include <QSignalSpy>


using Messages = QMap<qint64, KIMAP::Message>;

Q_DECLARE_METATYPE(KIMAP::Message)
Laurent Montel's avatar
Laurent Montel committed
24
Q_DECLARE_METATYPE(Messages)
Andras Mantia's avatar
Andras Mantia committed
25

Laurent Montel's avatar
Laurent Montel committed
26 27 28
class SelectJobTest: public QObject
{
    Q_OBJECT
Andras Mantia's avatar
Andras Mantia committed
29

30 31 32 33 34 35 36 37
public:
    explicit SelectJobTest()
    {
        qRegisterMetaType<KIMAP::ImapSet>();
        qRegisterMetaType<KIMAP::Message>();
        qRegisterMetaType<QMap<qint64, KIMAP::Message>>("QMap<qint64,KIMAP::Message>");
    }

38
private Q_SLOTS:
Andras Mantia's avatar
Andras Mantia committed
39

Laurent Montel's avatar
Laurent Montel committed
40 41 42 43 44 45 46 47 48 49 50 51
    void testSingleSelect_data()
    {
        QTest::addColumn<QList<QByteArray> >("scenario");
        QTest::addColumn<QList<QByteArray> >("flags");
        QTest::addColumn<QList<QByteArray> >("permanentflags");
        QTest::addColumn<int>("messagecount");
        QTest::addColumn<int>("recentcount");
        QTest::addColumn<int>("firstUnseenIndex");
        QTest::addColumn<qint64>("uidValidity");
        QTest::addColumn<qint64>("nextUid");
        QTest::addColumn<quint64>("highestmodseq");
        QTest::addColumn<bool>("condstoreEnabled");
52
        QTest::addColumn<bool>("readonly");
53 54 55 56 57
        QTest::addColumn<qint64>("lastUidvalidity");
        QTest::addColumn<quint64>("lastModseq");
        QTest::addColumn<KIMAP::ImapSet>("vanished");
        QTest::addColumn<Messages>("modified");

Laurent Montel's avatar
Laurent Montel committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

        QList<QByteArray> scenario;
        QList<QByteArray> flags;
        QList<QByteArray> permanentflags;
        scenario << FakeServer::preauth()
                 << "C: A000001 SELECT \"INBOX\" (CONDSTORE)"
                 << "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: * OK [HIGHESTMODSEQ 123456789]"
                 << "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" << "\\*";
76 77
        QTest::newRow("good") << scenario << flags << permanentflags << 172 << 1 << 12 << 3857529045LL << 4392LL << 123456789ULL << true << false
                              << -1LL << 0ULL << KIMAP::ImapSet{} << Messages{};
Laurent Montel's avatar
Laurent Montel committed
78 79 80 81 82 83 84

        scenario.clear();
        flags.clear();
        permanentflags.clear();
        scenario << FakeServer::preauth()
                 << "C: A000001 SELECT \"INBOX\""
                 << "S: A000001 BAD command unknown or arguments invalid";
85 86
        QTest::newRow("bad") << scenario << flags << permanentflags << 0 << 0 << 0 << 0LL << 0LL << 0ULL << false << false
                             << -1LL << 0ULL << KIMAP::ImapSet{} << Messages{};
Laurent Montel's avatar
Laurent Montel committed
87 88 89 90 91 92 93

        scenario.clear();
        flags.clear();
        permanentflags.clear();
        scenario << FakeServer::preauth()
                 << "C: A000001 SELECT \"INBOX\""
                 << "S: A000001 NO select failure";
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
        QTest::newRow("no") << scenario << flags << permanentflags << 0 << 0 << 0 << 0LL << 0LL << 0ULL << false << false
                            << -1LL << 0ULL << KIMAP::ImapSet{} << Messages{};

        scenario.clear();
        scenario << FakeServer::preauth()
                 << "C: A000001 SELECT \"INBOX\" (QRESYNC (67890007 90060115194045000))"
                 << "S: * 314 EXISTS"
                 << "S: * 15 RECENT"
                 << "S: * OK [UIDVALIDITY 67890007] UIDVALIDITY"
                 << "S: * OK [UIDNEXT 567] Predicted next UID"
                 << "S: * OK [HIGHESTMODSEQ 90060115205545359]"
                 << "S: * OK [UNSEEN 7] There are some unseen messages in the mailbox"
                 << "S: * FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)"
                 << "S: * OK [PERMANENTFLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen \\*)] Permanent flags"
                 << "S: * VANISHED (EARLIER) 41,43:116,118,120:211,214:540"
                 << "S: * 49 FETCH (UID 117 FLAGS (\\Seen \\Answered) MODSEQ (90060115194045001))"
                 << "S: * 50 FETCH (UID 119 FLAGS (\\Draft $MDNSent) MODSEQ (90060115194045308))"
                 << "S: * 100 FETCH (UID 541 FLAGS (\\Seen $Forwarded) MODSEQ (90060115194045001))"
                 << "S: A000001 OK [READ-WRITE] mailbox selected";
        permanentflags = {"\\Answered", "\\Flagged", "\\Draft", "\\Deleted", "\\Seen", "\\*"};
        flags = {"\\Answered", "\\Flagged", "\\Draft", "\\Deleted", "\\Seen"};
        KIMAP::ImapSet vanished;
        vanished.add(41);
        vanished.add(KIMAP::ImapInterval{43, 116});
        vanished.add(118);
        vanished.add(KIMAP::ImapInterval{120, 211});
        vanished.add(KIMAP::ImapInterval{214, 540});
        Messages modified = {
            {49, KIMAP::Message{117, 0, {"\\Seen", "\\Answered"}, {{"MODSEQ", QVariant{90060115194045001ULL}}}, {}, {}}},
            {50, KIMAP::Message{119, 0, {"\\Draft", "$MDNSent"}, {{"MODSEQ", QVariant{90060115194045308ULL}}}, {}, {}}},
            {100, KIMAP::Message{541, 0, {"\\Seen", "$Forwarded"}, {{"MODSEQ", QVariant{90060115194045001ULL}}}, {}, {}}}
        };
        QTest::newRow("QResync") << scenario << flags << permanentflags << 314 << 15 << 7 << 67890007LL << 567LL << 90060115205545359ULL << false << false
                                 << 67890007LL << 90060115194045000ULL << vanished << modified;
Andras Mantia's avatar
Andras Mantia committed
128
    }
129

Laurent Montel's avatar
Laurent Montel committed
130 131 132 133 134 135 136 137 138 139 140 141
    void testSingleSelect()
    {
        QFETCH(QList<QByteArray>, scenario);
        QFETCH(QList<QByteArray>, flags);
        QFETCH(QList<QByteArray>, permanentflags);
        QFETCH(int, messagecount);
        QFETCH(int, recentcount);
        QFETCH(int, firstUnseenIndex);
        QFETCH(qint64, uidValidity);
        QFETCH(qint64, nextUid);
        QFETCH(quint64, highestmodseq);
        QFETCH(bool, condstoreEnabled);
142 143 144 145
        QFETCH(qint64, lastUidvalidity);
        QFETCH(quint64, lastModseq);
        QFETCH(KIMAP::ImapSet, vanished);
        QFETCH(Messages, modified);
Laurent Montel's avatar
Laurent Montel committed
146 147 148 149 150

        FakeServer fakeServer;
        fakeServer.setScenario(scenario);
        fakeServer.startAndWait();

Laurent Montel's avatar
Laurent Montel committed
151
        KIMAP::Session session(QStringLiteral("127.0.0.1"), 5989);
Laurent Montel's avatar
Laurent Montel committed
152 153 154

        KIMAP::SelectJob *job = new KIMAP::SelectJob(&session);
        job->setCondstoreEnabled(condstoreEnabled);
Laurent Montel's avatar
Laurent Montel committed
155
        job->setMailBox(QStringLiteral("INBOX"));
156 157 158 159 160 161 162 163 164 165
        if (lastUidvalidity > -1 && lastModseq > 0) {
            job->setQResync(lastUidvalidity, lastModseq);
        }

        QSignalSpy vanishedSpy(job, &KIMAP::SelectJob::vanished);
        QVERIFY(vanishedSpy.isValid());

        QSignalSpy modifiedSpy(job, &KIMAP::SelectJob::modified);
        QVERIFY(modifiedSpy.isValid());

Laurent Montel's avatar
Laurent Montel committed
166 167 168 169 170 171 172 173 174 175 176 177 178
        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);
            QCOMPARE(job->highestModSequence(), highestmodseq);
179 180 181 182 183 184 185 186 187

            if (!vanished.isEmpty()) {
                QCOMPARE(vanishedSpy.size(), 1);
                QCOMPARE(vanishedSpy.at(0).at(0).value<KIMAP::ImapSet>(), vanished);
            }

            if (!modified.isEmpty()) {
                Messages collectedModified;
                for (const auto &modifiedSpyCatch : modifiedSpy) {
Luca Beltrame's avatar
Luca Beltrame committed
188
                    collectedModified.insert(modifiedSpyCatch.at(0).value<Messages>());
189 190 191 192
                }

                QCOMPARE(collectedModified, modified);
            }
Laurent Montel's avatar
Laurent Montel committed
193 194 195 196
        }

        fakeServer.quit();
    }
Andras Mantia's avatar
Andras Mantia committed
197

Laurent Montel's avatar
Laurent Montel committed
198 199 200 201 202 203 204 205 206 207 208 209
    void testSeveralSelect()
    {
        FakeServer fakeServer;
        fakeServer.setScenario(QList<QByteArray>()
                               << 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.startAndWait();

Laurent Montel's avatar
Laurent Montel committed
210
        KIMAP::Session session(QStringLiteral("127.0.0.1"), 5989);
Laurent Montel's avatar
Laurent Montel committed
211 212

        KIMAP::SelectJob *job = new KIMAP::SelectJob(&session);
Laurent Montel's avatar
Laurent Montel committed
213
        job->setMailBox(QStringLiteral("INBOX"));
Laurent Montel's avatar
Laurent Montel committed
214 215 216
        QVERIFY(job->exec());

        job = new KIMAP::SelectJob(&session);
Laurent Montel's avatar
Laurent Montel committed
217
        job->setMailBox(QStringLiteral("INBOX/Foo"));
Laurent Montel's avatar
Laurent Montel committed
218 219
        QVERIFY(job->exec());
    }
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
    void testReadOnlySelect_data()
    {
        QTest::addColumn<QList<QByteArray>>("scenario");
        QTest::addColumn<bool>("examine");
        QTest::addColumn<bool>("isReadOnly");

        {
            QList<QByteArray> scenario;
            scenario << FakeServer::preauth()
                     << "C: A000001 SELECT \"INBOX\""
                     << "S: A000001 OK [READ-WRITE] SELECT ok";
            QTest::newRow("SELECT rw") << scenario << false << false;
        }

        {
            QList<QByteArray> scenario;
            scenario << FakeServer::preauth()
                     << "C: A000001 SELECT \"INBOX\""
                     << "S: A000001 OK SELECT ok";
            QTest::newRow("SELECT rw (without code)") << scenario << false << false;
        }

        {
            QList<QByteArray> scenario;
            scenario << FakeServer::preauth()
                     << "C: A000001 SELECT \"INBOX\""
                     << "S: A000001 OK [READ-ONLY] SELECT ok";
            QTest::newRow("SELECT ro") << scenario << false << true;
        }

        {
            QList<QByteArray> scenario;
            scenario << FakeServer::preauth()
                     << "C: A000001 EXAMINE \"INBOX\""
                     << "S: A000001 OK [READ-ONLY] EXAMINE ok";
            QTest::newRow("EXAMINE ro") << scenario << true << true;
        }
    }

    void testReadOnlySelect()
    {
        QFETCH(QList<QByteArray>, scenario);
        QFETCH(bool, examine);
        QFETCH(bool, isReadOnly);

        FakeServer fakeServer;
        fakeServer.setScenario(scenario);
        fakeServer.startAndWait();

        KIMAP::Session session(QStringLiteral("127.0.0.1"), 5989);

        KIMAP::SelectJob select(&session);
        select.setOpenReadOnly(examine);
        select.setMailBox(QStringLiteral("INBOX"));
        QVERIFY(select.exec());

        QCOMPARE(select.isOpenReadOnly(), isReadOnly);
    }
Andras Mantia's avatar
Andras Mantia committed
279 280
};

Laurent Montel's avatar
Laurent Montel committed
281
QTEST_GUILESS_MAIN(SelectJobTest)
Andras Mantia's avatar
Andras Mantia committed
282 283

#include "selectjobtest.moc"