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

0001 /*
0002    Copyright (C) 2009 Andras Mantia <amantia@kde.org>
0003 
0004    Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0005    Author: Kevin Ottens <kevin@kdab.com>
0006 
0007    This program is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU General Public
0009    License as published by the Free Software Foundation; either
0010    version 2 of the License, or (at your option) any later version.
0011 
0012    This program is distributed in the hope that it will be useful,
0013    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015    General Public License for more details.
0016 
0017    You should have received a copy of the GNU General Public License
0018    along with this program; if not, write to the Free Software
0019    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #include <qtest.h>
0023 
0024 #include "kimap2test/fakeserver.h"
0025 #include "kimap2/session.h"
0026 #include "kimap2/listjob.h"
0027 
0028 #include <QtTest>
0029 #include <QDebug>
0030 
0031 Q_DECLARE_METATYPE(QList<KIMAP2::MailBoxDescriptor>)
0032 Q_DECLARE_METATYPE(QList< QList<QByteArray> >)
0033 
0034 class ListJobTest: public QObject
0035 {
0036     Q_OBJECT
0037 
0038 private Q_SLOTS:
0039 
0040     void testList_data()
0041     {
0042         QTest::addColumn<bool>("unsubscribed");
0043         QTest::addColumn<QList<QByteArray> >("scenario");
0044         QTest::addColumn<QList<KIMAP2::MailBoxDescriptor> >("listresult");
0045 
0046         QList<QByteArray> scenario;
0047         scenario << FakeServer::preauth()
0048                  << "C: A000001 LIST \"\" *"
0049                  << "S: * LIST ( \\HasChildren ) / INBOX"
0050                  << "S: * LIST ( \\HasNoChildren ) / INBOX/&AOQ- &APY- &APw- @ &IKw-"
0051                  << "S: * LIST ( \\HasChildren ) / INBOX/lost+found"
0052                  << "S: * LIST ( \\HasNoChildren ) / \"INBOX/lost+found/Calendar Public-20080128\""
0053                  << "S: A000001 OK LIST completed";
0054         KIMAP2::MailBoxDescriptor descriptor;
0055         QList<KIMAP2::MailBoxDescriptor> listresult;
0056 
0057         descriptor.separator = QLatin1Char('/');
0058         descriptor.name = QStringLiteral("INBOX");
0059         listresult << descriptor;
0060         descriptor.separator = QLatin1Char('/');
0061         descriptor.name = QString::fromUtf8("INBOX/ä ö ü @ €");
0062         listresult << descriptor;
0063         descriptor.separator = QLatin1Char('/');
0064         descriptor.name = QStringLiteral("INBOX/lost+found");
0065         listresult << descriptor;
0066         descriptor.separator = QLatin1Char('/');
0067         descriptor.name = QStringLiteral("INBOX/lost+found/Calendar Public-20080128");
0068         listresult << descriptor;
0069 
0070         QTest::newRow("normal") << true << scenario << listresult;
0071 
0072         scenario.clear();;
0073         scenario << FakeServer::preauth()
0074                  << "C: A000001 LIST \"\" *"
0075                  << "S: * LIST ( \\HasChildren ) / Inbox"
0076                  << "S: * LIST ( \\HasNoChildren ) / Inbox/&AOQ- &APY- &APw- @ &IKw-"
0077                  << "S: * LIST ( \\HasChildren ) / Inbox/lost+found"
0078                  << "S: * LIST ( \\HasNoChildren ) / \"Inbox/lost+found/Calendar Public-20080128\""
0079                  << "S: A000001 OK LIST completed";
0080         listresult.clear();
0081 
0082         descriptor.separator = QLatin1Char('/');
0083         descriptor.name = QStringLiteral("INBOX");
0084         listresult << descriptor;
0085         descriptor.separator = QLatin1Char('/');
0086         descriptor.name = QString::fromUtf8("INBOX/ä ö ü @ €");
0087         listresult << descriptor;
0088         descriptor.separator = QLatin1Char('/');
0089         descriptor.name = QStringLiteral("INBOX/lost+found");
0090         listresult << descriptor;
0091         descriptor.separator = QLatin1Char('/');
0092         descriptor.name = QStringLiteral("INBOX/lost+found/Calendar Public-20080128");
0093         listresult << descriptor;
0094 
0095         QTest::newRow("lowercase Inbox") << true << scenario << listresult;
0096 
0097         scenario.clear();
0098         scenario << FakeServer::preauth()
0099                  << "C: A000001 LSUB \"\" *"
0100                  << "S: * LSUB ( \\HasChildren ) / INBOX"
0101                  << "S: * LSUB ( ) / INBOX/Calendar/3196"
0102                  << "S: * LSUB ( \\HasChildren ) / INBOX/Calendar/ff"
0103                  << "S: * LSUB ( ) / INBOX/Calendar/ff/hgh"
0104                  << "S: * LSUB ( ) / user/test2/Calendar"
0105                  << "S: A000001 OK LSUB completed";
0106         listresult.clear();
0107 
0108         descriptor.separator = QLatin1Char('/');
0109         descriptor.name = QStringLiteral("INBOX");
0110         listresult << descriptor;
0111         descriptor.separator = QLatin1Char('/');
0112         descriptor.name = QStringLiteral("INBOX/Calendar/3196");
0113         listresult << descriptor;
0114         descriptor.separator = QLatin1Char('/');
0115         descriptor.name = QStringLiteral("INBOX/Calendar/ff");
0116         listresult << descriptor;
0117         descriptor.separator = QLatin1Char('/');
0118         descriptor.name = QStringLiteral("INBOX/Calendar/ff/hgh");
0119         listresult << descriptor;
0120         descriptor.separator = QLatin1Char('/');
0121         descriptor.name = QStringLiteral("user/test2/Calendar");
0122         listresult << descriptor;
0123 
0124         QTest::newRow("subscribed") << false << scenario << listresult;
0125 
0126         scenario.clear();
0127         scenario << FakeServer::preauth()
0128                  << "C: A000001 LSUB \"\" *"
0129                  << "S: * LSUB ( \\HasChildren ) / Inbox"
0130                  << "S: * LSUB ( ) / Inbox/Calendar/3196"
0131                  << "S: * LSUB ( \\HasChildren ) / Inbox/Calendar/ff"
0132                  << "S: * LSUB ( ) / Inbox/Calendar/ff/hgh"
0133                  << "S: * LSUB ( ) / user/test2/Calendar"
0134                  << "S: A000001 OK LSUB completed";
0135         listresult.clear();
0136 
0137         descriptor.separator = QLatin1Char('/');
0138         descriptor.name = QStringLiteral("INBOX");
0139         listresult << descriptor;
0140         descriptor.separator = QLatin1Char('/');
0141         descriptor.name = QStringLiteral("INBOX/Calendar/3196");
0142         listresult << descriptor;
0143         descriptor.separator = QLatin1Char('/');
0144         descriptor.name = QStringLiteral("INBOX/Calendar/ff");
0145         listresult << descriptor;
0146         descriptor.separator = QLatin1Char('/');
0147         descriptor.name = QStringLiteral("INBOX/Calendar/ff/hgh");
0148         listresult << descriptor;
0149         descriptor.separator = QLatin1Char('/');
0150         descriptor.name = QStringLiteral("user/test2/Calendar");
0151         listresult << descriptor;
0152 
0153         QTest::newRow("subscribed, lowercase Inbox") << false << scenario << listresult;
0154 
0155         scenario.clear();
0156         scenario << FakeServer::preauth()
0157                  << "C: A000001 LIST \"\" *"
0158                  << "S: * LIST ( \\HasNoChildren ) / INBOX/lost+found/Calendar Public-20080128"
0159                  << "S: A000001 OK LIST completed";
0160         listresult.clear();
0161         descriptor.separator = QLatin1Char('/');
0162         descriptor.name = QStringLiteral("INBOX/lost+found/Calendar Public-20080128");
0163         listresult << descriptor;
0164 
0165         QTest::newRow("unquoted-space") << true << scenario << listresult;
0166 
0167         scenario.clear();
0168         scenario << FakeServer::preauth()
0169                  << "C: A000001 LIST \"\" *"
0170                  << "S: * LIST ( \\NoInferiors ) ( ) INBOX"
0171                  << "S: A000001 OK LIST completed";
0172         listresult.clear();
0173         descriptor.separator = QLatin1Char('/');
0174         descriptor.name = QStringLiteral("INBOX");
0175         listresult << descriptor;
0176 
0177         QTest::newRow("separator is empty list") << true << scenario << listresult;
0178 
0179         scenario.clear();
0180         scenario << FakeServer::preauth()
0181                  << "C: A000001 LIST \"\" *"
0182                  << "S: A000001 BAD command unknown or arguments invalid";
0183         listresult.clear();
0184         QTest::newRow("bad") << true << scenario << listresult;
0185 
0186         scenario.clear();
0187         scenario << FakeServer::preauth()
0188                  << "C: A000001 LIST \"\" *"
0189                  << "S: A000001 NO list failure";
0190         QTest::newRow("no") << true << scenario << listresult;
0191     }
0192 
0193     void testList()
0194     {
0195         QFETCH(bool, unsubscribed);
0196         QFETCH(QList<QByteArray>, scenario);
0197         QFETCH(QList<KIMAP2::MailBoxDescriptor>, listresult);
0198 
0199         FakeServer fakeServer;
0200         fakeServer.setScenario(scenario);
0201         fakeServer.startAndWait();
0202 
0203         KIMAP2::Session session(QStringLiteral("127.0.0.1"), 5989);
0204 
0205         KIMAP2::ListJob *job = new KIMAP2::ListJob(&session);
0206         if (unsubscribed) {
0207             job->setOption(KIMAP2::ListJob::IncludeUnsubscribed);
0208         }
0209 
0210         QSignalSpy spy(job, &KIMAP2::ListJob::resultReceived);
0211 
0212         bool result = job->exec();
0213         QEXPECT_FAIL("bad" , "Expected failure on BAD response", Continue);
0214         QEXPECT_FAIL("no" , "Expected failure on NO response", Continue);
0215         QVERIFY(result);
0216         if (result) {
0217             QVERIFY(spy.count() > 0);
0218             QList<KIMAP2::MailBoxDescriptor> mailBoxes;
0219 
0220             for (int i = 0; i < spy.count(); ++i) {
0221                 mailBoxes += spy.at(i).at(0).value<KIMAP2::MailBoxDescriptor>();
0222             }
0223 
0224             //qDebug() << mailBoxes.first().name;
0225             //qDebug() << listresult.first().name;
0226             QCOMPARE(mailBoxes, listresult);
0227         }
0228 //     QCOMPARE(job->mailBox(), mailbox);
0229 
0230         fakeServer.quit();
0231     }
0232 
0233 };
0234 
0235 QTEST_GUILESS_MAIN(ListJobTest)
0236 
0237 #include "listjobtest.moc"