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

0001 /*
0002    Copyright (C) 2011 Andras Mantia <amantia@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    General Public License for more details.
0013 
0014    You should have received a copy of the GNU General Public License
0015    along with this program; if not, write to the Free Software
0016    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #include <qtest.h>
0020 
0021 #include "kimap2test/fakeserver.h"
0022 #include "kimap2/session.h"
0023 #include "kimap2/getquotarootjob.h"
0024 
0025 #include <QtTest>
0026 
0027 Q_DECLARE_METATYPE(QList<qint64>)
0028 
0029 class QuotaRootJobTest: public QObject
0030 {
0031     Q_OBJECT
0032 
0033 private Q_SLOTS:
0034 
0035     void testGetQuotaRoot_data()
0036     {
0037         QTest::addColumn<QString>("mailbox");
0038         QTest::addColumn<QList<QByteArray> >("roots");
0039         QTest::addColumn<QList<QByteArray> >("resources");
0040         QTest::addColumn<QList<qint64> >("usages");
0041         QTest::addColumn<QList<qint64> >("limits");
0042         QTest::addColumn<QList<QByteArray> >("scenario");
0043 
0044         QList<QByteArray> roots;
0045         QList<QByteArray> resources;
0046         QList<qint64> usages;
0047         QList<qint64> limits;
0048         QList<QByteArray> scenario;
0049         roots << "";
0050         resources << "STORAGE";
0051         limits << 512;
0052         usages << 10;
0053         scenario << FakeServer::preauth()
0054                  << "C: A000001 GETQUOTAROOT \"INBOX\""
0055                  << "S: * QUOTAROOT INBOX \"\" "
0056                  << "S: * QUOTA \"\" (STORAGE 10 512)"
0057                  << "S: A000001 OK GETQUOTA completed";
0058         QTest::newRow("one root, one resource") << "INBOX" << roots << resources << usages << limits << scenario;
0059 
0060         roots.clear();
0061         resources.clear();
0062         usages.clear();
0063         limits.clear();
0064         scenario.clear();
0065         roots << "";
0066         resources << "STORAGE" << "MESSAGE";
0067         usages << 10 << 8221;
0068         limits << 512 << 20000;
0069         scenario << FakeServer::preauth()
0070                  << "C: A000001 GETQUOTAROOT \"INBOX\""
0071                  << "S: * QUOTAROOT INBOX \"\" "
0072                  << "S: * QUOTA \"\" (STORAGE 10 512)"
0073                  << "S: * QUOTA \"\" ( MESSAGE 8221 20000 ) "
0074                  << "S: A000001 OK GETQUOTA completed";
0075         QTest::newRow("one root, multiple resource") << "INBOX" << roots << resources << usages << limits << scenario;
0076 
0077         roots.clear();
0078         resources.clear();
0079         usages.clear();
0080         limits.clear();
0081         scenario.clear();
0082         roots << "root1" << "root2";
0083         resources << "STORAGE" << "MESSAGE";
0084         usages << 10 << 8221 << 30 << 100;
0085         limits << 512 << 20000 << 5124 << 120000;
0086         scenario << FakeServer::preauth()
0087                  << "C: A000001 GETQUOTAROOT \"INBOX\""
0088                  << "S: * QUOTAROOT INBOX \"root1\" root2 "
0089                  << "S: * QUOTA \"root1\" (STORAGE 10 512)"
0090                  << "S: * QUOTA \"root1\" ( MESSAGE 8221 20000 ) "
0091                  << "S: * QUOTA \"root2\" ( MESSAGE 100 120000 ) "
0092                  << "S: * QUOTA \"root2\" (STORAGE 30 5124)"
0093                  << "S: A000001 OK GETQUOTA completed";
0094         QTest::newRow("multiple roots, multiple resource") << "INBOX" << roots << resources << usages << limits << scenario;
0095 
0096         roots.clear();
0097         resources.clear();
0098         usages.clear();
0099         limits.clear();
0100         scenario.clear();
0101         roots << "";
0102         resources << "STORAGE" << "MESSAGE";
0103         usages << 10 << 8221;
0104         limits << 512 << 20000;
0105         scenario << FakeServer::preauth()
0106                  << "C: A000001 GETQUOTAROOT \"INBOX\""
0107                  << "S: * QUOTAROOT INBOX"
0108                  << "S: * QUOTA (STORAGE 10 512)"
0109                  << "S: * QUOTA ( MESSAGE 8221 20000 ) "
0110                  << "S: A000001 OK GETQUOTA completed";
0111         QTest::newRow("no rootname, multiple resource") << "INBOX" << roots << resources << usages << limits << scenario;
0112 
0113         roots.clear();
0114         resources.clear();
0115         usages.clear();
0116         limits.clear();
0117         scenario.clear();
0118         roots << "";
0119         resources << "STORAGE";
0120         limits << 512;
0121         usages << 10;
0122         scenario << FakeServer::preauth()
0123                  << "C: A000001 GETQUOTAROOT \"INBOX\""
0124                  << "S: * QUOTAROOT INBOX \"\" "
0125                  << "S: * QUOTA (STORAGE 10 512)"
0126                  << "S: A000001 OK GETQUOTA completed";
0127         QTest::newRow("no rootname in QUOTA, one resource") << "INBOX" << roots << resources << usages << limits << scenario;
0128 
0129         roots.clear();
0130         resources.clear();
0131         usages.clear();
0132         limits.clear();
0133         scenario.clear();
0134         roots << "";
0135         resources << "STORAGE";
0136         limits << 512;
0137         usages << 10;
0138         scenario << FakeServer::preauth()
0139                  << "C: A000001 GETQUOTAROOT \"INBOX\""
0140                  << "S: * QUOTAROOT INBOX \"root1\" "
0141                  << "S: * QUOTA \"root2\" (STORAGE 10 512)"
0142                  << "S: A000001 OK GETQUOTA completed";
0143         QTest::newRow("rootname missmatch") << "INBOX" << roots << resources << usages << limits << scenario;
0144     }
0145 
0146     void testGetQuotaRoot()
0147     {
0148         QFETCH(QString, mailbox);
0149         QFETCH(QList<QByteArray>, roots);
0150         QFETCH(QList<QByteArray>, resources);
0151         QFETCH(QList<qint64>, usages);
0152         QFETCH(QList<qint64>, limits);
0153         QFETCH(QList<QByteArray>, scenario);
0154 
0155         FakeServer fakeServer;
0156         fakeServer.setScenario(scenario);
0157         fakeServer.startAndWait();
0158 
0159         KIMAP2::Session session(QStringLiteral("127.0.0.1"), 5989);
0160 
0161         KIMAP2::GetQuotaRootJob *job = new KIMAP2::GetQuotaRootJob(&session);
0162         job->setMailBox(mailbox);
0163         bool result = job->exec();
0164         QEXPECT_FAIL("bad" , "Expected failure on BAD response", Continue);
0165         QEXPECT_FAIL("no" , "Expected failure on NO response", Continue);
0166         QVERIFY(result);
0167         QEXPECT_FAIL("rootname missmatch" , "Expected failure on rootname missmatch in QUOTAROOT and QUOTA response", Abort);
0168         QCOMPARE(job->roots(), roots);
0169         for (int rootIdx = 0; rootIdx < roots.size(); rootIdx++) {
0170             const QByteArray &root = roots[rootIdx];
0171             for (int i = 0; i < resources.size(); i++) {
0172                 int idx = i + rootIdx * roots.size();
0173                 QByteArray resource = resources[i];
0174                 QCOMPARE(job->limit(root, resource), limits[idx]);
0175                 QCOMPARE(job->usage(root, resource), usages[idx]);
0176             }
0177         }
0178 
0179         fakeServer.quit();
0180     }
0181 
0182 };
0183 
0184 QTEST_GUILESS_MAIN(QuotaRootJobTest)
0185 
0186 #include "quotarootjobtest.moc"