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

0001 /*
0002  * Copyright (C) 2013  Daniel Vrátil <dvratil@redhat.com>
0003  *
0004  * This program is free software; you can redistribute it and/or modify
0005  * it under the terms of the GNU General Public License as published by
0006  * the Free Software Foundation; either version 2 of the License, or
0007  * (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
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License along
0015  * with this program; if not, write to the Free Software Foundation, Inc.,
0016  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0017  *
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/searchjob.h"
0026 
0027 #include <QtTest>
0028 
0029 Q_DECLARE_METATYPE(KIMAP2::Term)
0030 
0031 class SearchJobTest: public QObject
0032 {
0033     Q_OBJECT
0034 
0035 private Q_SLOTS:
0036 
0037     void testSearchTerm_data()
0038     {
0039         QTest::addColumn<QList<QByteArray> >("scenario");
0040         QTest::addColumn<bool>("uidbased");
0041         QTest::addColumn<int>("expectedResultsCount");
0042         QTest::addColumn<KIMAP2::Term>("searchTerm");
0043 
0044         {
0045             QList<QByteArray> scenario;
0046             scenario << FakeServer::preauth()
0047                      << "C: A000001 UID SEARCH HEADER Message-Id \"<12345678@mail.box>\""
0048                      << "S: * SEARCH 10 12"
0049                      << "S: A000001 OK search done";
0050 
0051             QTest::newRow("uidbased header search") << scenario << true << 2 << KIMAP2::Term(QLatin1String("Message-Id"), QLatin1String("<12345678@mail.box>"));
0052         }
0053         {
0054             QList<QByteArray> scenario;
0055             scenario << FakeServer::preauth()
0056                      << "C: A000001 SEARCH OR NEW HEADER Message-Id \"<12345678@mail.box>\""
0057                      << "S: * SEARCH"
0058                      << "S: A000001 OK search done";
0059 
0060             QTest::newRow("OR search with no results") << scenario << false << 0 << KIMAP2::Term(KIMAP2::Term::Or, QVector<KIMAP2::Term>() << KIMAP2::Term(KIMAP2::Term::New) << KIMAP2::Term(QLatin1String("Message-Id"), QLatin1String("<12345678@mail.box>")));
0061         }
0062         {
0063             QList<QByteArray> scenario;
0064             scenario << FakeServer::preauth()
0065                      << "C: A000001 SEARCH TO \"<testuser@kde.testserver>\""
0066                      << "S: * SEARCH 1"
0067                      << "S: A000001 OK search done";
0068             QTest::newRow("literal data search") << scenario << false << 1 << KIMAP2::Term(KIMAP2::Term::To, QLatin1String("<testuser@kde.testserver>"));
0069         }
0070         {
0071             QList<QByteArray> scenario;
0072             scenario << FakeServer::preauth()
0073                      << "C: A000001 UID SEARCH NOT NEW"
0074                      << "S: * SEARCH 1 2 3 4 5 6"
0075                      << "S: A000001 OK search done";
0076             QTest::newRow("uidbased NOT NEW search") << scenario << true << 6 << KIMAP2::Term(KIMAP2::Term::New).setNegated(true);
0077         }
0078 
0079         {
0080             QList<QByteArray> scenario;
0081             scenario << FakeServer::preauth()
0082                      << "C: A000001 UID SEARCH OR HEADER Message-Id \"<1234567@mail.box>\" (OR HEADER Message-Id \"<7654321@mail.box>\" (OR HEADER Message-Id \"<abcdefg@mail.box>\" HEADER Message-Id \"<gfedcba@mail.box>\"))"
0083                      << "S: * SEARCH 1 2 3 4"
0084                      << "S: A000001 OK search done";
0085             KIMAP2::Term term{KIMAP2::Term::Or, {KIMAP2::Term{QStringLiteral("Message-Id"), QStringLiteral("<1234567@mail.box>")},
0086                                                KIMAP2::Term{QStringLiteral("Message-Id"), QStringLiteral("<7654321@mail.box>")},
0087                                                KIMAP2::Term{QStringLiteral("Message-Id"), QStringLiteral("<abcdefg@mail.box>")},
0088                                                KIMAP2::Term{QStringLiteral("Message-Id"), QStringLiteral("<gfedcba@mail.box>")}}};
0089             QTest::newRow("OR with multiple subterms") << scenario << true << 4 << term;
0090         }
0091     }
0092 
0093     void testSearchTerm()
0094     {
0095         QFETCH(QList<QByteArray>, scenario);
0096         QFETCH(bool, uidbased);
0097         QFETCH(int, expectedResultsCount);
0098         QFETCH(KIMAP2::Term, searchTerm);
0099 
0100         FakeServer fakeServer;
0101         fakeServer.setScenario(scenario);
0102         fakeServer.startAndWait();
0103 
0104         KIMAP2::Session session(QLatin1String("127.0.0.1"), 5989);
0105 
0106         KIMAP2::SearchJob *job = new KIMAP2::SearchJob(&session);
0107         job->setUidBased(uidbased);
0108         job->setTerm(searchTerm);
0109 
0110         bool result = job->exec();
0111         QVERIFY(result);
0112         if (result) {
0113             QVector<qint64> foundItems = job->results();
0114             QCOMPARE(foundItems.size(), expectedResultsCount);
0115         }
0116 
0117         fakeServer.quit();
0118     }
0119 
0120 };
0121 
0122 QTEST_GUILESS_MAIN(SearchJobTest)
0123 
0124 #include "searchjobtest.moc"