File indexing completed on 2024-05-12 05:17:20

0001 /*
0002     Copyright (c) 2016 Daniel Vrátil <dvratil@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 
0020 #include <qtest.h>
0021 
0022 #include "kimap2test/fakeserver.h"
0023 #include "kimap2/loginjob.h"
0024 #include "kimap2/session.h"
0025 #include "kimap2/statusjob.h"
0026 
0027 typedef QList<QPair<QByteArray, qint64>> StatusMap;
0028 Q_DECLARE_METATYPE(StatusMap)
0029 
0030 class StatusJobTest: public QObject
0031 {
0032     Q_OBJECT
0033 
0034 private Q_SLOTS:
0035 
0036     void testStatus_data()
0037     {
0038         QTest::addColumn<QList<QByteArray>>("scenario");
0039         QTest::addColumn<QList<QByteArray>>("dataItems");
0040         QTest::addColumn<StatusMap>("results");
0041 
0042         QList<QByteArray> scenario;
0043         QList<QByteArray> dataItems;
0044         StatusMap results;
0045         scenario << FakeServer::preauth()
0046                  << "C: A000001 STATUS \"INBOX\" (MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)"
0047                  << "S: * STATUS \"INBOX\" (MESSAGES 294 RECENT 1 UIDNEXT 295 UIDVALIDITY 458587604 UNSEEN 181)"
0048                  << "S: A000001 OK STATUS Completed";
0049         dataItems = { "MESSAGES", "RECENT", "UIDNEXT", "UIDVALIDITY", "UNSEEN" };
0050         results = { { "MESSAGES", 294 }, { "RECENT", 1 }, { "UIDNEXT", 295 },
0051                     { "UIDVALIDITY", 458587604 }, { "UNSEEN", 181 } };
0052         QTest::newRow("good") << scenario << dataItems << results;
0053 
0054         scenario.clear();
0055         results.clear();
0056         scenario << FakeServer::preauth()
0057                  << "C: A000001 STATUS \"INBOX\" (MESSAGES UIDNEXT HIGHESTMODSEQ)"
0058                  << "S: * STATUS \"INBOX\" (MESSAGES 294 UIDNEXT 295)"
0059                  << "S: A000001 OK STATUS Completed";
0060         dataItems = { "MESSAGES", "UIDNEXT", "HIGHESTMODSEQ" };
0061         results = { { "MESSAGES", 294 }, { "UIDNEXT", 295 } };
0062         QTest::newRow("incomplete") << scenario << dataItems << results;
0063 
0064         scenario.clear();
0065         results.clear();
0066         scenario << FakeServer::preauth()
0067                  << "C: A000001 STATUS \"INBOX\" (HIGHESTMODSEQ)"
0068                  << "S: * STATUS \"INBOX\" ()"
0069                  << "S: A000001 OK STATUS Completed";
0070         dataItems = { "HIGHESTMODSEQ" };
0071         QTest::newRow("empty response") << scenario << dataItems << results;
0072 
0073         scenario.clear();
0074         results.clear();
0075         scenario << FakeServer::preauth()
0076                  << "C: A000001 STATUS \"INBOX\" (MESSAGES HIGHESTMODSEQ)"
0077                  << "S: A000001 NO status failure";
0078         dataItems = { "MESSAGES", "HIGHESTMODSEQ" };
0079         results.clear();
0080         QTest::newRow("no") << scenario << dataItems << results;
0081 
0082         scenario.clear();
0083         results.clear();
0084         scenario << FakeServer::preauth()
0085                  << "C: A000001 STATUS \"INBOX\" (UIDNEXT)"
0086                  << "S: A000001 NO bad command";
0087         dataItems = { "UIDNEXT" };
0088         QTest::newRow("bad") << scenario << dataItems << results;
0089     }
0090 
0091     void testStatus()
0092     {
0093         QFETCH(QList<QByteArray>, scenario);
0094         QFETCH(QList<QByteArray>, dataItems);
0095         QFETCH(StatusMap, results);
0096 
0097         FakeServer fakeServer;
0098         fakeServer.setScenario(scenario);
0099         fakeServer.startAndWait();
0100 
0101         KIMAP2::Session session(QStringLiteral("127.0.0.1"), 5989);
0102         KIMAP2::StatusJob *job = new KIMAP2::StatusJob(&session);
0103         job->setMailBox(QStringLiteral("INBOX"));
0104         job->setDataItems(dataItems);
0105         bool result = job->exec();
0106 
0107         QEXPECT_FAIL("bad" , "Expected failure on BAD scenario", Continue);
0108         QEXPECT_FAIL("no" , "Expected failure on NO scenario", Continue);
0109         QVERIFY(result);
0110 
0111         if (result) {
0112             const StatusMap status = job->status();
0113             QCOMPARE(status.count(), results.count());
0114             for (int i = 0; i < results.count(); ++i) {
0115                 QCOMPARE(results[i].first, status[i].first);
0116                 QCOMPARE(results[i].second, status[i].second);
0117             }
0118         }
0119 
0120         fakeServer.quit();
0121     }
0122 };
0123 
0124 QTEST_GUILESS_MAIN(StatusJobTest)
0125 
0126 #include "statusjobtest.moc"
0127