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

0001 /*
0002    SPDX-FileCopyrightText: 2009 Andras Mantia <amantia@kde.org>
0003 
0004    SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0005    SPDX-FileContributor: Kevin Ottens <kevin@kdab.com>
0006 
0007    SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include <QTest>
0011 
0012 #include "kimap/listjob.h"
0013 #include "kimap/session.h"
0014 #include "kimaptest/fakeserver.h"
0015 
0016 #include <QDebug>
0017 #include <QSignalSpy>
0018 #include <QTest>
0019 
0020 Q_DECLARE_METATYPE(QList<KIMAP::MailBoxDescriptor>)
0021 Q_DECLARE_METATYPE(QList<QList<QByteArray>>)
0022 
0023 class ListJobTest : public QObject
0024 {
0025     Q_OBJECT
0026 
0027 private Q_SLOTS:
0028 
0029     void testList_data()
0030     {
0031         QTest::addColumn<bool>("unsubscribed");
0032         QTest::addColumn<QList<QByteArray>>("scenario");
0033         QTest::addColumn<QList<KIMAP::MailBoxDescriptor>>("listresult");
0034 
0035         QList<QByteArray> scenario;
0036         scenario << FakeServer::preauth() << "C: A000001 LIST \"\" *"
0037                  << "S: * LIST ( \\HasChildren ) / INBOX"
0038                  << "S: * LIST ( \\HasNoChildren ) / INBOX/&AOQ- &APY- &APw- @ &IKw-"
0039                  << "S: * LIST ( \\HasChildren ) / INBOX/lost+found"
0040                  << R"(S: * LIST ( \HasNoChildren ) / "INBOX/lost+found/Calendar Public-20080128")"
0041                  << "S: A000001 OK LIST completed";
0042         KIMAP::MailBoxDescriptor descriptor;
0043         QList<KIMAP::MailBoxDescriptor> listresult;
0044 
0045         descriptor.separator = QLatin1Char('/');
0046         descriptor.name = QStringLiteral("INBOX");
0047         listresult << descriptor;
0048         descriptor.separator = QLatin1Char('/');
0049         descriptor.name = QString::fromUtf8("INBOX/ä ö ü @ €");
0050         listresult << descriptor;
0051         descriptor.separator = QLatin1Char('/');
0052         descriptor.name = QStringLiteral("INBOX/lost+found");
0053         listresult << descriptor;
0054         descriptor.separator = QLatin1Char('/');
0055         descriptor.name = QStringLiteral("INBOX/lost+found/Calendar Public-20080128");
0056         listresult << descriptor;
0057 
0058         QTest::newRow("normal") << true << scenario << listresult;
0059 
0060         scenario.clear();
0061         scenario << FakeServer::preauth() << "C: A000001 LIST \"\" *"
0062                  << "S: * LIST ( \\HasChildren ) / Inbox"
0063                  << "S: * LIST ( \\HasNoChildren ) / Inbox/&AOQ- &APY- &APw- @ &IKw-"
0064                  << "S: * LIST ( \\HasChildren ) / Inbox/lost+found"
0065                  << R"(S: * LIST ( \HasNoChildren ) / "Inbox/lost+found/Calendar Public-20080128")"
0066                  << "S: A000001 OK LIST completed";
0067         listresult.clear();
0068 
0069         descriptor.separator = QLatin1Char('/');
0070         descriptor.name = QStringLiteral("INBOX");
0071         listresult << descriptor;
0072         descriptor.separator = QLatin1Char('/');
0073         descriptor.name = QString::fromUtf8("INBOX/ä ö ü @ €");
0074         listresult << descriptor;
0075         descriptor.separator = QLatin1Char('/');
0076         descriptor.name = QStringLiteral("INBOX/lost+found");
0077         listresult << descriptor;
0078         descriptor.separator = QLatin1Char('/');
0079         descriptor.name = QStringLiteral("INBOX/lost+found/Calendar Public-20080128");
0080         listresult << descriptor;
0081 
0082         QTest::newRow("lowercase Inbox") << true << scenario << listresult;
0083 
0084         scenario.clear();
0085         scenario << FakeServer::preauth() << "C: A000001 LSUB \"\" *"
0086                  << "S: * LSUB ( \\HasChildren ) / INBOX"
0087                  << "S: * LSUB ( ) / INBOX/Calendar/3196"
0088                  << "S: * LSUB ( \\HasChildren ) / INBOX/Calendar/ff"
0089                  << "S: * LSUB ( ) / INBOX/Calendar/ff/hgh"
0090                  << "S: * LSUB ( ) / user/test2/Calendar"
0091                  << "S: A000001 OK LSUB completed";
0092         listresult.clear();
0093 
0094         descriptor.separator = QLatin1Char('/');
0095         descriptor.name = QStringLiteral("INBOX");
0096         listresult << descriptor;
0097         descriptor.separator = QLatin1Char('/');
0098         descriptor.name = QStringLiteral("INBOX/Calendar/3196");
0099         listresult << descriptor;
0100         descriptor.separator = QLatin1Char('/');
0101         descriptor.name = QStringLiteral("INBOX/Calendar/ff");
0102         listresult << descriptor;
0103         descriptor.separator = QLatin1Char('/');
0104         descriptor.name = QStringLiteral("INBOX/Calendar/ff/hgh");
0105         listresult << descriptor;
0106         descriptor.separator = QLatin1Char('/');
0107         descriptor.name = QStringLiteral("user/test2/Calendar");
0108         listresult << descriptor;
0109 
0110         QTest::newRow("subscribed") << false << scenario << listresult;
0111 
0112         scenario.clear();
0113         scenario << FakeServer::preauth() << "C: A000001 LSUB \"\" *"
0114                  << "S: * LSUB ( \\HasChildren ) / Inbox"
0115                  << "S: * LSUB ( ) / Inbox/Calendar/3196"
0116                  << "S: * LSUB ( \\HasChildren ) / Inbox/Calendar/ff"
0117                  << "S: * LSUB ( ) / Inbox/Calendar/ff/hgh"
0118                  << "S: * LSUB ( ) / user/test2/Calendar"
0119                  << "S: A000001 OK LSUB completed";
0120         listresult.clear();
0121 
0122         descriptor.separator = QLatin1Char('/');
0123         descriptor.name = QStringLiteral("INBOX");
0124         listresult << descriptor;
0125         descriptor.separator = QLatin1Char('/');
0126         descriptor.name = QStringLiteral("INBOX/Calendar/3196");
0127         listresult << descriptor;
0128         descriptor.separator = QLatin1Char('/');
0129         descriptor.name = QStringLiteral("INBOX/Calendar/ff");
0130         listresult << descriptor;
0131         descriptor.separator = QLatin1Char('/');
0132         descriptor.name = QStringLiteral("INBOX/Calendar/ff/hgh");
0133         listresult << descriptor;
0134         descriptor.separator = QLatin1Char('/');
0135         descriptor.name = QStringLiteral("user/test2/Calendar");
0136         listresult << descriptor;
0137 
0138         QTest::newRow("subscribed, lowercase Inbox") << false << scenario << listresult;
0139 
0140         scenario.clear();
0141         scenario << FakeServer::preauth() << "C: A000001 LIST \"\" *"
0142                  << "S: * LIST ( \\HasNoChildren ) / INBOX/lost+found/Calendar Public-20080128"
0143                  << "S: A000001 OK LIST completed";
0144         listresult.clear();
0145         descriptor.separator = QLatin1Char('/');
0146         descriptor.name = QStringLiteral("INBOX/lost+found/Calendar Public-20080128");
0147         listresult << descriptor;
0148 
0149         QTest::newRow("unquoted-space") << true << scenario << listresult;
0150 
0151         scenario.clear();
0152         scenario << FakeServer::preauth() << "C: A000001 LIST \"\" *"
0153                  << "S: * LIST ( \\NoInferiors ) ( ) INBOX"
0154                  << "S: A000001 OK LIST completed";
0155         listresult.clear();
0156         descriptor.separator = QLatin1Char('/');
0157         descriptor.name = QStringLiteral("INBOX");
0158         listresult << descriptor;
0159 
0160         QTest::newRow("separator is empty list") << true << scenario << listresult;
0161 
0162         scenario.clear();
0163         scenario << FakeServer::preauth() << "C: A000001 LIST \"\" *"
0164                  << "S: A000001 BAD command unknown or arguments invalid";
0165         listresult.clear();
0166         QTest::newRow("bad") << true << scenario << listresult;
0167 
0168         scenario.clear();
0169         scenario << FakeServer::preauth() << "C: A000001 LIST \"\" *"
0170                  << "S: A000001 NO list failure";
0171         QTest::newRow("no") << true << scenario << listresult;
0172     }
0173 
0174     void testList()
0175     {
0176         QFETCH(bool, unsubscribed);
0177         QFETCH(QList<QByteArray>, scenario);
0178         QFETCH(QList<KIMAP::MailBoxDescriptor>, listresult);
0179 
0180         FakeServer fakeServer;
0181         fakeServer.setScenario(scenario);
0182         fakeServer.startAndWait();
0183 
0184         KIMAP::Session session(QStringLiteral("127.0.0.1"), 5989);
0185 
0186         auto job = new KIMAP::ListJob(&session);
0187         job->setOption(unsubscribed ? KIMAP::ListJob::IncludeUnsubscribed : KIMAP::ListJob::NoOption);
0188 
0189         QSignalSpy spy(job, &KIMAP::ListJob::mailBoxesReceived);
0190 
0191         bool result = job->exec();
0192         QEXPECT_FAIL("bad", "Expected failure on BAD response", Continue);
0193         QEXPECT_FAIL("no", "Expected failure on NO response", Continue);
0194         QVERIFY(result);
0195         if (result) {
0196             QVERIFY(spy.count() > 0);
0197             QList<KIMAP::MailBoxDescriptor> mailBoxes;
0198 
0199             for (int i = 0; i < spy.count(); ++i) {
0200                 mailBoxes += spy.at(i).at(0).value<QList<KIMAP::MailBoxDescriptor>>();
0201             }
0202 
0203             // qDebug() << mailBoxes.first().name;
0204             // qDebug() << listresult.first().name;
0205             QCOMPARE(mailBoxes, listresult);
0206         }
0207         //     QCOMPARE(job->mailBox(), mailbox);
0208 
0209         fakeServer.quit();
0210     }
0211 };
0212 
0213 QTEST_GUILESS_MAIN(ListJobTest)
0214 
0215 #include "listjobtest.moc"