summaryrefslogtreecommitdiff
path: root/kimap/tests/selectjobtest.cpp
blob: 9ee2a806e1c6124f8d338c5d467160368127e4ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
   Copyright (C) 2009 Andras Mantia <amantia@kde.org>

   Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
   Author: Kevin Ottens <kevin@kdab.com>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <qtest_kde.h>

#include "kimaptest/fakeserver.h"
#include "kimap/loginjob.h"
#include "kimap/session.h"
#include "kimap/selectjob.h"

#include <QTcpSocket>
#include <QtTest>
#include <KDebug>

class SelectJobTest: public QObject {
  Q_OBJECT

private Q_SLOTS:

void testSingleSelect_data() {
  QTest::addColumn<QList<QByteArray> >( "scenario" );
  QTest::addColumn<QList<QByteArray> >( "flags" );
  QTest::addColumn<QList<QByteArray> >( "permanentflags" );
  QTest::addColumn<int>( "messagecount" );
  QTest::addColumn<int>( "recentcount" );
  QTest::addColumn<int>( "firstUnseenIndex" );
  QTest::addColumn<qint64>( "uidValidity" );
  QTest::addColumn<qint64>( "nextUid" );

  QList<QByteArray> scenario;
  QList<QByteArray> flags;
  QList<QByteArray> permanentflags;
  scenario << FakeServer::preauth()
           << "C: A000001 SELECT \"INBOX\""
           << "S: * 172 EXISTS"
           << "S: * 1 RECENT"
           << "S: * OK [UNSEEN 12] Message 12 is first unseen"
           << "S: * OK [UIDVALIDITY 3857529045] UIDs valid"
           << "S: * OK [UIDNEXT 4392] Predicted next UID"
           << "S: * FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)"
           << "S: * OK [PERMANENTFLAGS (\\Deleted \\Seen \\*)] Limited"
           << "S: A000001 OK [READ-WRITE] SELECT completed";

  flags << "\\Answered" << "\\Flagged" << "\\Deleted" << "\\Seen" << "\\Draft";
  permanentflags << "\\Deleted" << "\\Seen" << "\\*";
  QTest::newRow( "good" ) << scenario << flags << permanentflags << 172 << 1 << 12 << (qint64)3857529045 << (qint64)4392;

  scenario.clear();
  flags.clear();
  permanentflags.clear();
  scenario << FakeServer::preauth()
           << "C: A000001 SELECT \"INBOX\""
           << "S: A000001 BAD command unknown or arguments invalid";
  QTest::newRow( "bad" ) << scenario << flags << permanentflags << 0 << 0 << 0 << (qint64)0 << (qint64)0;

  scenario.clear();
  flags.clear();
  permanentflags.clear();
  scenario << FakeServer::preauth()
           << "C: A000001 SELECT \"INBOX\""
           << "S: A000001 NO select failure";
  QTest::newRow( "no" ) << scenario << flags << permanentflags << 0 << 0 << 0 << (qint64)0 << (qint64)0;
}

void testSingleSelect()
{
    QFETCH( QList<QByteArray>, scenario );
    QFETCH( QList<QByteArray>, flags );
    QFETCH( QList<QByteArray>, permanentflags );
    QFETCH( int, messagecount);
    QFETCH( int, recentcount);
    QFETCH( int, firstUnseenIndex);
    QFETCH( qint64, uidValidity);
    QFETCH( qint64, nextUid);

    FakeServer fakeServer;
    fakeServer.setScenario( scenario );
    fakeServer.startAndWait();

    KIMAP::Session session("127.0.0.1", 5989);

    KIMAP::SelectJob *job = new KIMAP::SelectJob(&session);
    job->setMailBox("INBOX");
    bool result = job->exec();
    QEXPECT_FAIL("bad" , "Expected failure on BAD scenario", Continue);
    QEXPECT_FAIL("no" , "Expected failure on NO scenario", Continue);
    QVERIFY(result);
    if (result) {
      QCOMPARE(job->flags(), flags);
      QCOMPARE(job->permanentFlags(), permanentflags);
      QCOMPARE(job->messageCount(), messagecount);
      QCOMPARE(job->recentCount(), recentcount);
      QCOMPARE(job->firstUnseenIndex(), firstUnseenIndex);
      QCOMPARE(job->uidValidity(), uidValidity);
      QCOMPARE(job->nextUid(), nextUid);
    }

    fakeServer.quit();
}

void testSeveralSelect()
{
    FakeServer fakeServer;
    fakeServer.setScenario( QList<QByteArray>()
        << FakeServer::preauth()
        << "C: A000001 SELECT \"INBOX\""
        << "S: A000001 OK [READ-WRITE] SELECT completed"
        << "C: A000002 SELECT \"INBOX/Foo\""
        << "S: A000002 OK [READ-WRITE] SELECT completed"
    );
    fakeServer.startAndWait();

    KIMAP::Session session("127.0.0.1", 5989);

    KIMAP::SelectJob *job = new KIMAP::SelectJob(&session);
    job->setMailBox("INBOX");
    QVERIFY(job->exec());

    job = new KIMAP::SelectJob(&session);
    job->setMailBox("INBOX/Foo");
    QVERIFY(job->exec());
}


};

QTEST_KDEMAIN_CORE( SelectJobTest )

#include "selectjobtest.moc"