~ubuntu-branches/ubuntu/quantal/akonadi/quantal

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
    Copyright (c) 2006 Volker Krause <vkrause@kde.org>
    Copyright (c) 2009 Andras Mantia <amantia@kde.org>

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

    This library 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 Library General Public
    License for more details.

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

#include "imapstreamparsertest.h"

#include <QtCore/QDebug>
#include <QtCore/QVariant>
#include <QBuffer>

#include "imapstreamparser.h"
#include <aktest.h>

Q_DECLARE_METATYPE( QList<QByteArray> )
Q_DECLARE_METATYPE( QList<int> )

using namespace Akonadi;

#if 0
QString akBacktrace()
{
  QString s;

  /* FIXME: is there an equivalent for darwin, *BSD, or windows? */
#ifdef HAVE_EXECINFO_H
  void* trace[256];
  int n = backtrace(trace, 256);
  if (!n)
    return s;

  char** strings = backtrace_symbols (trace, n);

  s = QLatin1String("[\n");

  for ( int i = 0; i < n; ++i ) {
    s += QString::number(i) +
        QLatin1String(": ") +
        QLatin1String(strings[i]) + QLatin1String("\n");
  }
  s += QLatin1String("]\n");

  if (strings)
    free (strings);
#endif

  return s;
}
#endif

AKTEST_MAIN( ImapStreamParserTest )


void ImapStreamParserTest::testParseQuotedString( )
{
  QByteArray result;

  QBuffer buffer;
  buffer.open( QBuffer::ReadWrite);
  ImapStreamParser parser(&buffer);
  buffer.write( QByteArray( "\"quoted \\\"NIL\\\"   string inside\"" ) );
  buffer.write( QByteArray( "quoted") );
  buffer.write( QByteArray( "\"   string inside\"" ) );
  buffer.write( QByteArray( "   string " ) );
  buffer.write("NIL \"NIL\" \"\"");
  buffer.write( " string" );
  buffer.write( "\"\\\"some \\\\ quoted stuff\\\"\"" );
  buffer.write( "LOGOUT\nFOO" );
  buffer.seek(0);

  try {
  // the whole thing
  result = parser.parseQuotedString();
  QCOMPARE( result, QByteArray( "quoted \"NIL\"   string inside" ) );

  // unqoted string
  result = parser.parseQuotedString();
  QCOMPARE( result, QByteArray( "quoted" ) );

  // whitespaces in qouted string
  result = parser.parseQuotedString();
  QCOMPARE( result, QByteArray( "   string inside" ) );

  // whitespaces before unquoted string
  result = parser.parseQuotedString();
  QCOMPARE( result, QByteArray( "string" ) );


  // NIL and emptyness tests

  // unqoted NIL
  result = parser.parseQuotedString();
  QVERIFY( result.isNull() );

  // quoted NIL
  result = parser.parseQuotedString();
  QCOMPARE( result, QByteArray( "NIL" ) );

  // quoted empty string
  result = parser.parseQuotedString();
  QCOMPARE( result, QByteArray( "" ) );

  // unquoted string at input end
  result = parser.parseQuotedString();
  QCOMPARE( result, QByteArray( "string" ) );

//   // out of bounds access
//   result = parser.parseQuotedString();
//   QVERIFY( result.isEmpty() );

  // de-quoting
  result = parser.parseQuotedString();
  QCOMPARE( result, QByteArray( "\"some \\ quoted stuff\"" ) );

  // linebreak as separator
  result = parser.parseQuotedString();
  QCOMPARE( result, QByteArray("LOGOUT") );
  } catch ( const Akonadi::Exception &e ) {
    qDebug() << "Caught exception: " << e.type() << " : " << e.what();
  }
}


void ImapStreamParserTest::testParseString( )
{

  QBuffer buffer;
  buffer.open( QBuffer::ReadWrite);
  ImapStreamParser parser(&buffer);

  buffer.write( QByteArray( "\"quoted\" unquoted {7}\nliteral {0}\n " ) );
  buffer.seek(0);

  QByteArray result;
  bool exceptionExpected = false;

  try {
  // quoted strings
  result = parser.peekString();
  QCOMPARE( result, QByteArray( "quoted" ) );
  result = parser.readString();
  QCOMPARE( result, QByteArray( "quoted" ) );

  // unquoted string
  result = parser.peekString( );
  QCOMPARE( result, QByteArray( "unquoted" ) );
  result = parser.readString( );
  QCOMPARE( result, QByteArray( "unquoted" ) );

  // literal string
  result = parser.peekString( );
  QCOMPARE( result, QByteArray( "literal" ) );
  result = parser.readString( );
  QCOMPARE( result, QByteArray( "literal" ) );

  // empty literal string
  result = parser.peekString();
  QCOMPARE( result, QByteArray( "" ) );
  result = parser.readString();
  QCOMPARE( result, QByteArray( "" ) );

  // out of bounds access

  //this results in an ImapStreamParser exception
  exceptionExpected = true;
  result = parser.readString();
  QCOMPARE( result, QByteArray() );
  } catch ( const Akonadi::Exception &e ) {
    qDebug() << "Caught exception: " << e.type() << " : " << e.what();
    QVERIFY( exceptionExpected );
  }
}

void ImapStreamParserTest::testParseParenthesizedList( )
{
  QBuffer buffer;
  buffer.open( QBuffer::ReadWrite);
  ImapStreamParser parser(&buffer);

  buffer.write( QByteArray( "() ( )" ) );
  buffer.write( QByteArray( "(entry1 \"entry2()\" (sub list) \")))\" {6}\nentry3) " ) );
  buffer.write( QByteArray( "some_list-less_text" ) );
  buffer.write( QByteArray( "(foo {6}\n\n\nbar\n bla)" ));
  buffer.write( QByteArray( "(AA (\"BB)\" CC))" ));	// parenthesis inside quoted string
  buffer.seek(0);

  QList<QByteArray>result;

  try {
  // empty lists
  result = parser.readParenthesizedList();
  QVERIFY( result.isEmpty() );

  result = parser.readParenthesizedList();
  QVERIFY( result.isEmpty() );

  // complex list with all kind of entries
  qDebug() << "complex test";
  result = parser.readParenthesizedList();
  QList<QByteArray> reference;
  reference << "entry1";
  reference << "entry2()";
  reference << "(sub list)";
  reference << ")))";
  reference << "entry3";


  QCOMPARE( result, reference );

  // no list at all
  result = parser.readParenthesizedList(  );
  QVERIFY( result.isEmpty() );

  parser.readString(); //just to advance in the data

//   // out of bounds access
//   result = parser.readParenthesizedList( input, result, input.length() );
//   QVERIFY( result.isEmpty() );
//   QCOMPARE( consumed, input.length() );

  // newline literal (based on itemappendtest bug)
  result = parser.readParenthesizedList(  );
  reference.clear();
  reference << "foo";
  reference << "\n\nbar\n";
  reference << "bla";

  QCOMPARE( result, reference );

  // Don't try to parse characters inside a quoted-string
  result = parser.readParenthesizedList();
  reference.clear();
  reference << "AA";
  reference << "(\"BB)\" CC)";

  QCOMPARE( result, reference );
  } catch ( const Akonadi::Exception &e ) {
    qDebug() << "Caught exception: " << e.type() << " : " <<
        e.what();
    QVERIFY( false );
  }
}

void ImapStreamParserTest::testParseNumber_data()
{
  QTest::addColumn<QByteArray>( "input" );
  QTest::addColumn<int>( "startIndex" );
  QTest::addColumn<qint64>( "result" );
  QTest::addColumn<bool>( "success" );

  QTest::newRow( "empty" ) << QByteArray( " " ) << 0 << 0ll << false;
  QTest::newRow( "start" ) << QByteArray( "1 23 4\n" ) << 0 << 1ll << true;
  QTest::newRow( "middle" ) << QByteArray( "1 23 4\n" ) << 1 << 23ll << true;
  QTest::newRow( "middle2" ) << QByteArray( "1 23 4\n" ) << 2 << 23ll << true;
  QTest::newRow( "middle3" ) << QByteArray( "1 23 4\n" ) << 3 << 3ll << true;
  QTest::newRow( "end" ) << QByteArray( "1 23 4\n" ) << 4 << 4ll << true;
  QTest::newRow( "after end" ) << QByteArray( "1 23 4\n" ) << 6 << 0ll << false;
  QTest::newRow( "incomplete" ) << QByteArray( "1" ) << 0 << 0ll << false;
  QTest::newRow( "nan" ) << QByteArray( "foo" ) << 0 << 0ll << false;
}

void ImapStreamParserTest::testParseNumber()
{
  QFETCH( QByteArray, input );
  QFETCH( int, startIndex );
  QFETCH( qint64, result );
  QFETCH( bool, success );

  QBuffer buffer;
  buffer.open( QBuffer::ReadWrite | QIODevice::Truncate );
  ImapStreamParser parser(&buffer);

  buffer.write( input );
  buffer.seek( startIndex );

  bool failed = false;
  try {
    qint64 number = parser.readNumber();
    QCOMPARE( number, result );
  } catch ( const ImapParserException &e ) {
    if ( success )
      qDebug() << "Caught unexpected parser exception: " << " : " << e.what();
    failed = true;
  }
  QCOMPARE( failed, !success );
}


void ImapStreamParserTest::testParseSequenceSet_data()
{
  QTest::addColumn<QByteArray>( "data" );
  QTest::addColumn<ImapInterval::List>( "result" );
  QTest::addColumn<bool>( "exceptionExpected" );

  QByteArray data( " 1 0:* 3:4,8:* *:5,1" );

  QTest::newRow( "empty" ) << QByteArray() << ImapInterval::List() << true;

  ImapInterval::List result;
  result << ImapInterval( 1, 1 );
  data = " 1 ";
  QTest::newRow( "single value" ) << data  << result << false;

  result.clear();
  result << ImapInterval();
  data = "  0:* ";
  QTest::newRow( "full interval" ) << data << result << false;

  data = " 3:4,8:*";
  result.clear();
  result << ImapInterval( 3, 4 ) << ImapInterval( 8 );
  QTest::newRow( "complex 1" ) << data << result << false;

  data = " 1,5:*";
  result.clear();
  result << ImapInterval( 1, 1 ) << ImapInterval( 5, 0 );
  QTest::newRow( "complex 2" ) << data  << result << false;
}

void ImapStreamParserTest::testParseSequenceSet()
{
  QFETCH( QByteArray, data );
  QFETCH( ImapInterval::List, result );
  QFETCH( bool, exceptionExpected );

  QBuffer buffer;
  buffer.open( QBuffer::ReadWrite);
  ImapStreamParser parser(&buffer);
  buffer.write( data );
  buffer.seek(0);

  ImapSet res;
  bool exceptionCaught = false;
  try {
    res = parser.readSequenceSet();
    QCOMPARE( res.intervals(), result );
  } catch ( const Akonadi::Exception &e ) {
    qDebug() << "Caught exception: " << e.type() << " : " << e.what();
    exceptionCaught = true;
  }
  QCOMPARE( exceptionCaught, exceptionExpected );
}


void ImapStreamParserTest::testParseDateTime_data()
{
  QTest::addColumn<QByteArray>( "data" );
  QTest::addColumn<QDateTime>( "result" );
  QTest::addColumn<bool>( "exceptionExpected" );

  QTest::newRow( "empty" ) << QByteArray() <<  QDateTime() << true;

  QByteArray data( " \"28-May-2006 01:03:35 +0200\"" );
  QByteArray data2( "22-Jul-2008 16:31:48 +0000" );
  QByteArray data3( "ul-2008 16:31:48 +0000" );

  QDateTime dt( QDate( 2006, 5, 27 ), QTime( 23, 3, 35 ), Qt::UTC );
  QDateTime dt2( QDate( 2008, 7, 22 ), QTime( 16, 31, 48 ), Qt::UTC );

  QTest::newRow( "quoted" ) << data  << dt << false;
  QTest::newRow( "unquoted" ) << data2 <<  dt2 << false;
  QTest::newRow( "invalid" ) << data3 << QDateTime() << false;
}

void ImapStreamParserTest::testParseDateTime()
{
  QFETCH( QByteArray, data );
  QFETCH( QDateTime, result );
  QFETCH( bool, exceptionExpected );

  QBuffer buffer;
  buffer.open( QBuffer::ReadWrite);
  ImapStreamParser parser(&buffer);
  buffer.write( data );
  buffer.seek(0);

  QDateTime actualResult;
  bool exceptionCaught = false;
  try {
    actualResult = parser.readDateTime();
    QCOMPARE( actualResult, result );
  } catch ( const Akonadi::Exception &e ) {
    qDebug() << "Caught exception: " << e.type() << " : " << e.what();
    exceptionCaught = true;
  }
  QCOMPARE( exceptionCaught, exceptionExpected );
}

void ImapStreamParserTest::testReadUntilCommandEnd()
{
  QByteArray input( "2 UID STORE 2 NOREV SIZE 0 (+FLAGS.SILENT (\\Deleted))\n3 EXPUNGE\n" );
  QBuffer buffer( &input, this );
  buffer.open( QIODevice::ReadOnly );
  ImapStreamParser parser( &buffer );

  try {
    QCOMPARE( parser.readString(), QByteArray( "2" ) );
    QCOMPARE( parser.readString(), QByteArray( "UID" ) );
    QCOMPARE( parser.readString(), QByteArray( "STORE" ) );
    QCOMPARE( parser.readString(), QByteArray( "2" ) );
    QCOMPARE( parser.readString(), QByteArray( "NOREV" ) );
    QCOMPARE( parser.readString(), QByteArray( "SIZE" ) );
    QCOMPARE( parser.readString(), QByteArray( "0" ) );
    parser.stripLeadingSpaces();
    QCOMPARE( static_cast<char>( parser.readChar() ), '(' );
    QVERIFY( !parser.atCommandEnd() );
    parser.readUntilCommandEnd();
    QCOMPARE( parser.readString(), QByteArray( "3" ) );
  } catch ( const Akonadi::Exception &e ) {
    qDebug() << e.type() << e.what();
    QFAIL( "Exception caught" );
  }
}

// real bug, infinite loop with { as first char in a quoted string
void ImapStreamParserTest::testReadUntilCommandEnd2()
{
  QByteArray input( "4 MODIFY 595 MIMETYPE (message/rfc822 inode/directory) NAME \"child2\" REMOTEID \"{b42}\"\nNEXTCOMMAND\n" );
  QBuffer buffer( &input, this );
  buffer.open( QIODevice::ReadOnly );
  ImapStreamParser parser( &buffer );

  try {
    QCOMPARE( parser.readString(), QByteArray( "4" ) );
    QCOMPARE( parser.readString(), QByteArray( "MODIFY" ) );
    QCOMPARE( parser.readString(), QByteArray( "595" ) );
    QCOMPARE( parser.readUntilCommandEnd(), QByteArray( " MIMETYPE (message/rfc822 inode/directory) NAME \"child2\" REMOTEID \"{b42}\"\n" ) );
    QCOMPARE( parser.readString(), QByteArray( "NEXTCOMMAND" ) );
  } catch ( const Akonadi::Exception &e ) {
    qDebug() << e.type() << e.what();
    QFAIL( "Exception caught" );
  }
}

void ImapStreamParserTest::testAbortCommand()
{
  QByteArray input( "12 UID STORE 63696 REV 2 (REMOTEID.SILENT \"225\" REMOTEREVISION.SILENT "" FLAGS.SILENT (\\SEEN ) PLD:HEAD.SILENT {2226}\nNEXTCOMMAND " );
  QBuffer buffer( &input, this );
  buffer.open( QIODevice::ReadOnly );
  ImapStreamParser parser( &buffer );

  try {
    // read some stuff and then notice that we can't process the command and abort early
    QCOMPARE( parser.readString(), QByteArray( "12" ) );
    QCOMPARE( parser.readString(), QByteArray( "UID" ) );
    QCOMPARE( parser.readString(), QByteArray( "STORE" ) );
    qDebug() << "!!!";
    parser.skipCurrentCommand();
    QCOMPARE( parser.readString(), QByteArray( "NEXTCOMMAND" ) );
  } catch ( const Akonadi::Exception &e ) {
    qDebug() << e.type() << e.what();
    QFAIL( "Exception caught" );
  }  
}

#include "imapstreamparsertest.moc"