/
ImapResponseParserTest.java
547 lines (428 loc) · 20.6 KB
/
ImapResponseParserTest.java
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package com.fsck.k9.mail.store.imap;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fsck.k9.mail.K9LibRobolectricTestRunner;
import com.fsck.k9.mail.filter.FixedLengthInputStream;
import com.fsck.k9.mail.filter.PeekableInputStream;
import org.junit.Test;
import org.junit.runner.RunWith;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@RunWith(K9LibRobolectricTestRunner.class)
public class ImapResponseParserTest {
private PeekableInputStream peekableInputStream;
@Test
public void testSimpleOkResponse() throws IOException {
ImapResponseParser parser = createParser("* OK\r\n");
ImapResponse response = parser.readResponse();
assertNotNull(response);
assertEquals(1, response.size());
assertEquals("OK", response.get(0));
}
@Test
public void testOkResponseWithText() throws IOException {
ImapResponseParser parser = createParser("* OK Some text here\r\n");
ImapResponse response = parser.readResponse();
assertNotNull(response);
assertEquals(2, response.size());
assertEquals("OK", response.get(0));
assertEquals("Some text here", response.get(1));
}
@Test
public void testOkResponseWithRespTextCode() throws IOException {
ImapResponseParser parser = createParser("* OK [UIDVALIDITY 3857529045]\r\n");
ImapResponse response = parser.readResponse();
assertNotNull(response);
assertEquals(2, response.size());
assertEquals("OK", response.get(0));
assertTrue(response.get(1) instanceof ImapList);
ImapList respTextCode = (ImapList) response.get(1);
assertEquals(2, respTextCode.size());
assertEquals("UIDVALIDITY", respTextCode.get(0));
assertEquals("3857529045", respTextCode.get(1));
}
@Test
public void testOkResponseWithRespTextCodeAndText() throws IOException {
ImapResponseParser parser = createParser("* OK [token1 token2] {x} test [...]\r\n");
ImapResponse response = parser.readResponse();
assertNotNull(response);
assertEquals(3, response.size());
assertEquals("OK", response.get(0));
assertTrue(response.get(1) instanceof ImapList);
assertEquals("{x} test [...]", response.get(2));
ImapList respTextCode = (ImapList) response.get(1);
assertEquals(2, respTextCode.size());
assertEquals("token1", respTextCode.get(0));
assertEquals("token2", respTextCode.get(1));
}
@Test
public void testReadStatusResponseWithOKResponse() throws Exception {
ImapResponseParser parser = createParser("* COMMAND BAR\tBAZ\r\n" +
"TAG OK COMMAND completed\r\n");
List<ImapResponse> responses = parser.readStatusResponse("TAG", null, null, null);
assertEquals(2, responses.size());
assertEquals(asList("COMMAND", "BAR", "BAZ"), responses.get(0));
assertEquals(asList("OK", "COMMAND completed"), responses.get(1));
}
@Test
public void testReadStatusResponseUntaggedHandlerGetsUntaggedOnly() throws Exception {
ImapResponseParser parser = createParser(
"* UNTAGGED\r\n" +
"A2 OK COMMAND completed\r\n");
TestUntaggedHandler untaggedHandler = new TestUntaggedHandler();
parser.readStatusResponse("A2", null, null, untaggedHandler);
assertEquals(1, untaggedHandler.responses.size());
assertEquals(asList("UNTAGGED"), untaggedHandler.responses.get(0));
}
@Test
public void testReadStatusResponseSkippingWrongTag() throws Exception {
ImapResponseParser parser = createParser("* UNTAGGED\r\n" +
"* 0 EXPUNGE\r\n" +
"* 42 EXISTS\r\n" +
"A1 COMMAND BAR BAZ\r\n" +
"A2 OK COMMAND completed\r\n");
TestUntaggedHandler untaggedHandler = new TestUntaggedHandler();
List<ImapResponse> responses = parser.readStatusResponse("A2", null, null, untaggedHandler);
assertEquals(3, responses.size());
assertEquals(asList("0", "EXPUNGE"), responses.get(0));
assertEquals(asList("42", "EXISTS"), responses.get(1));
assertEquals(asList("OK", "COMMAND completed"), responses.get(2));
assertEquals(asList("UNTAGGED"), untaggedHandler.responses.get(0));
assertEquals(responses.get(0), untaggedHandler.responses.get(1));
assertEquals(responses.get(1), untaggedHandler.responses.get(2));
}
@Test
public void testReadStatusResponseUntaggedHandlerStillCalledOnNegativeReply() throws Exception {
ImapResponseParser parser = createParser(
"+ text\r\n" +
"A2 NO Bad response\r\n");
TestUntaggedHandler untaggedHandler = new TestUntaggedHandler();
try {
List<ImapResponse> responses = parser.readStatusResponse("A2", null, null, untaggedHandler);
} catch (NegativeImapResponseException e) {
}
assertEquals(1, untaggedHandler.responses.size());
assertEquals(asList("text"), untaggedHandler.responses.get(0));
}
@Test(expected = NegativeImapResponseException.class)
public void testReadStatusResponseWithErrorResponse() throws Exception {
ImapResponseParser parser = createParser("* COMMAND BAR BAZ\r\nTAG ERROR COMMAND errored\r\n");
parser.readStatusResponse("TAG", null, null, null);
}
@Test
public void testRespTextCodeWithList() throws Exception {
ImapResponseParser parser = createParser("* OK [PERMANENTFLAGS (\\Answered \\Flagged \\Deleted \\Seen " +
"\\Draft NonJunk $MDNSent \\*)] Flags permitted.\r\n");
ImapResponse response = parser.readResponse();
assertEquals(3, response.size());
assertTrue(response.get(1) instanceof ImapList);
assertEquals(2, response.getList(1).size());
assertEquals("PERMANENTFLAGS", response.getList(1).getString(0));
assertTrue(response.getList(1).get(1) instanceof ImapList);
assertEquals("\\Answered", response.getList(1).getList(1).getString(0));
assertEquals("\\Flagged", response.getList(1).getList(1).getString(1));
assertEquals("\\Deleted", response.getList(1).getList(1).getString(2));
assertEquals("\\Seen", response.getList(1).getList(1).getString(3));
assertEquals("\\Draft", response.getList(1).getList(1).getString(4));
assertEquals("NonJunk", response.getList(1).getList(1).getString(5));
assertEquals("$MDNSent", response.getList(1).getList(1).getString(6));
assertEquals("\\*", response.getList(1).getList(1).getString(7));
}
@Test
public void testExistsResponse() throws Exception {
ImapResponseParser parser = createParser("* 23 EXISTS\r\n");
ImapResponse response = parser.readResponse();
assertEquals(2, response.size());
assertEquals(23, response.getNumber(0));
assertEquals("EXISTS", response.getString(1));
}
@Test(expected = IOException.class)
public void testReadStringUntilEndOfStream() throws IOException {
ImapResponseParser parser = createParser("* OK Some text ");
parser.readResponse();
}
@Test
public void testCommandContinuation() throws Exception {
ImapResponseParser parser = createParser("+ Ready for additional command text\r\n");
ImapResponse response = parser.readResponse();
assertEquals(1, response.size());
assertEquals("Ready for additional command text", response.getString(0));
}
@Test
public void testParseLiteral() throws Exception {
ImapResponseParser parser = createParser("* {4}\r\ntest\r\n");
ImapResponse response = parser.readResponse();
assertEquals(1, response.size());
assertEquals("test", response.getString(0));
}
@Test
public void testParseLiteralWithEmptyString() throws Exception {
ImapResponseParser parser = createParser("* {0}\r\n\r\n");
ImapResponse response = parser.readResponse();
assertEquals(1, response.size());
assertEquals("", response.getString(0));
}
@Test(expected = IOException.class)
public void testParseLiteralToEndOfStream() throws Exception {
ImapResponseParser parser = createParser("* {4}\r\nabc");
parser.readResponse();
}
@Test
public void testParseLiteralWithConsumingCallbackReturningNull() throws Exception {
ImapResponseParser parser = createParser("* {4}\r\ntest\r\n");
TestImapResponseCallback callback = TestImapResponseCallback.readBytesAndReturn(4, "cheeseburger");
ImapResponse response = parser.readResponse(callback);
assertEquals(1, response.size());
assertEquals("cheeseburger", response.getString(0));
}
@Test
public void testParseLiteralWithNonConsumingCallbackReturningNull() throws Exception {
ImapResponseParser parser = createParser("* {4}\r\ntest\r\n");
TestImapResponseCallback callback = TestImapResponseCallback.readBytesAndReturn(0, null);
ImapResponse response = parser.readResponse(callback);
assertEquals(1, response.size());
assertEquals("test", response.getString(0));
assertTrue(callback.foundLiteralCalled);
assertAllInputConsumed();
}
@Test
public void readResponse_withPartlyConsumingCallbackReturningNull_shouldThrow() throws Exception {
ImapResponseParser parser = createParser("* {4}\r\ntest\r\n");
TestImapResponseCallback callback = TestImapResponseCallback.readBytesAndReturn(2, null);
try {
parser.readResponse(callback);
fail();
} catch (AssertionError e) {
assertEquals("Callback consumed some data but returned no result", e.getMessage());
}
}
@Test
public void readResponse_withPartlyConsumingCallbackThatThrows_shouldReadAllDataAndThrow() throws Exception {
ImapResponseParser parser = createParser("* {4}\r\ntest\r\n");
TestImapResponseCallback callback = TestImapResponseCallback.readBytesAndThrow(2);
try {
parser.readResponse(callback);
fail();
} catch (ImapResponseParserException e) {
assertEquals("readResponse(): Exception in callback method", e.getMessage());
assertEquals(ImapResponseParserTestException.class, e.getCause().getClass());
}
assertAllInputConsumed();
}
@Test
public void readResponse_withCallbackThatThrowsRepeatedly_shouldConsumeAllInputAndThrowFirstException()
throws Exception {
ImapResponseParser parser = createParser("* {3}\r\none {3}\r\ntwo\r\n");
TestImapResponseCallback callback = TestImapResponseCallback.readBytesAndThrow(3);
try {
parser.readResponse(callback);
fail();
} catch (ImapResponseParserException e) {
assertEquals("readResponse(): Exception in callback method", e.getMessage());
assertEquals(ImapResponseParserTestException.class, e.getCause().getClass());
assertEquals(0, ((ImapResponseParserTestException) e.getCause()).instanceNumber);
}
assertAllInputConsumed();
}
@Test
public void testParseLiteralWithIncompleteConsumingCallbackReturningString() throws Exception {
ImapResponseParser parser = createParser("* {4}\r\ntest\r\n");
TestImapResponseCallback callback = TestImapResponseCallback.readBytesAndReturn(2, "ninja");
ImapResponse response = parser.readResponse(callback);
assertEquals(1, response.size());
assertEquals("ninja", response.getString(0));
assertAllInputConsumed();
}
@Test
public void testParseLiteralWithThrowingCallback() throws Exception {
ImapResponseParser parser = createParser("* {4}\r\ntest\r\n");
ImapResponseCallback callback = TestImapResponseCallback.readBytesAndThrow(0);
try {
parser.readResponse(callback);
fail();
} catch (ImapResponseParserException e) {
assertEquals("readResponse(): Exception in callback method", e.getMessage());
}
assertAllInputConsumed();
}
@Test(expected = IOException.class)
public void testParseLiteralWithCallbackThrowingIOException() throws Exception {
ImapResponseParser parser = createParser("* {4}\r\ntest\r\n");
ImapResponseCallback callback = new ImapResponseCallback() {
@Override
public Object foundLiteral(ImapResponse response, FixedLengthInputStream literal) throws Exception {
throw new IOException();
}
};
parser.readResponse(callback);
}
@Test
public void testParseQuoted() throws Exception {
ImapResponseParser parser = createParser("* \"qu\\\"oted\"\r\n");
ImapResponse response = parser.readResponse();
assertEquals(1, response.size());
assertEquals("qu\"oted", response.getString(0));
}
@Test(expected = IOException.class)
public void testParseQuotedToEndOfStream() throws Exception {
ImapResponseParser parser = createParser("* \"abc");
parser.readResponse();
}
@Test(expected = IOException.class)
public void testParseAtomToEndOfStream() throws Exception {
ImapResponseParser parser = createParser("* abc");
parser.readResponse();
}
@Test(expected = IOException.class)
public void testParseUntaggedResponseWithoutSpace() throws Exception {
ImapResponseParser parser = createParser("*\r\n");
parser.readResponse();
}
@Test
public void testListResponseContainingFolderNameWithBrackets() throws Exception {
ImapResponseParser parser = createParser("* LIST (\\HasNoChildren) \".\" [FolderName]\r\n");
ImapResponse response = parser.readResponse();
assertEquals(4, response.size());
assertEquals("LIST", response.get(0));
assertEquals(1, response.getList(1).size());
assertEquals("\\HasNoChildren", response.getList(1).getString(0));
assertEquals(".", response.get(2));
assertEquals("[FolderName]", response.get(3));
}
@Test
public void readResponseShouldReadWholeListResponseLine() throws Exception {
ImapResponseParser parser = createParser("* LIST (\\HasNoChildren) \".\" [FolderName]\r\n" +
"TAG OK [List complete]\r\n");
parser.readResponse();
ImapResponse responseTwo = parser.readResponse();
assertEquals("TAG", responseTwo.getTag());
}
@Test
public void readResponse_withListResponseContainingNil() throws Exception {
ImapResponseParser parser = createParser("* LIST (\\NoInferiors) NIL INBOX\r\n");
ImapResponse response = parser.readResponse();
assertEquals(4, response.size());
assertEquals("LIST", response.get(0));
assertEquals(1, response.getList(1).size());
assertEquals("\\NoInferiors", response.getList(1).getString(0));
assertEquals(null, response.get(2));
assertEquals("INBOX", response.get(3));
}
@Test
public void readResponse_withListAsFirstToken_shouldThrow() throws Exception {
ImapResponseParser parser = createParser("* [1 2] 3\r\n");
try {
parser.readResponse();
fail("Expected exception");
} catch (IOException e) {
assertEquals("Unexpected non-string token: [1, 2]", e.getMessage());
}
}
@Test
public void testFetchResponse() throws Exception {
ImapResponseParser parser = createParser("* 1 FETCH (" +
"UID 23 " +
"INTERNALDATE \"01-Jul-2015 12:34:56 +0200\" " +
"RFC822.SIZE 3456 " +
"BODY[HEADER.FIELDS (date subject from)] \"<headers>\" " +
"FLAGS (\\Seen))\r\n");
ImapResponse response = parser.readResponse();
assertEquals(3, response.size());
assertEquals("1", response.getString(0));
assertEquals("FETCH", response.getString(1));
assertEquals("UID", response.getList(2).getString(0));
assertEquals(23, response.getList(2).getNumber(1));
assertEquals("INTERNALDATE", response.getList(2).getString(2));
assertEquals("01-Jul-2015 12:34:56 +0200", response.getList(2).getString(3));
assertEquals("RFC822.SIZE", response.getList(2).getString(4));
assertEquals(3456, response.getList(2).getNumber(5));
assertEquals("BODY", response.getList(2).getString(6));
assertEquals(2, response.getList(2).getList(7).size());
assertEquals("HEADER.FIELDS", response.getList(2).getList(7).getString(0));
assertEquals(3, response.getList(2).getList(7).getList(1).size());
assertEquals("date", response.getList(2).getList(7).getList(1).getString(0));
assertEquals("subject", response.getList(2).getList(7).getList(1).getString(1));
assertEquals("from", response.getList(2).getList(7).getList(1).getString(2));
assertEquals("<headers>", response.getList(2).getString(8));
assertEquals("FLAGS", response.getList(2).getString(9));
assertEquals(1, response.getList(2).getList(10).size());
assertEquals("\\Seen", response.getList(2).getList(10).getString(0));
}
@Test
public void readStatusResponse_withNoResponse_shouldThrow() throws Exception {
ImapResponseParser parser = createParser("1 NO\r\n");
try {
parser.readStatusResponse("1", "COMMAND", "[logId]", null);
fail("Expected exception");
} catch (NegativeImapResponseException e) {
assertEquals("Command: COMMAND; response: #1# [NO]", e.getMessage());
}
}
@Test
public void readStatusResponse_withNoResponseAndAlertText_shouldThrowWithAlertText() throws Exception {
ImapResponseParser parser = createParser("1 NO [ALERT] Access denied\r\n");
try {
parser.readStatusResponse("1", "COMMAND", "[logId]", null);
fail("Expected exception");
} catch (NegativeImapResponseException e) {
assertEquals("Access denied", e.getAlertText());
}
}
private ImapResponseParser createParser(String response) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getBytes());
peekableInputStream = new PeekableInputStream(byteArrayInputStream);
return new ImapResponseParser(peekableInputStream);
}
private void assertAllInputConsumed() throws IOException {
assertEquals(0, peekableInputStream.available());
}
static class TestImapResponseCallback implements ImapResponseCallback {
private final int readNumberOfBytes;
private final Object returnValue;
private final boolean throwException;
private int exceptionCount = 0;
public boolean foundLiteralCalled = false;
public static TestImapResponseCallback readBytesAndReturn(int readNumberOfBytes, Object returnValue) {
return new TestImapResponseCallback(readNumberOfBytes, returnValue, false);
}
public static TestImapResponseCallback readBytesAndThrow(int readNumberOfBytes) {
return new TestImapResponseCallback(readNumberOfBytes, null, true);
}
private TestImapResponseCallback(int readNumberOfBytes, Object returnValue, boolean throwException) {
this.readNumberOfBytes = readNumberOfBytes;
this.returnValue = returnValue;
this.throwException = throwException;
}
@Override
public Object foundLiteral(ImapResponse response, FixedLengthInputStream literal) throws Exception {
foundLiteralCalled = true;
int skipBytes = readNumberOfBytes;
while (skipBytes > 0) {
long skippedBytes = literal.skip(skipBytes);
skipBytes -= skippedBytes;
}
if (throwException) {
throw new ImapResponseParserTestException(exceptionCount++);
}
return returnValue;
}
}
static class ImapResponseParserTestException extends RuntimeException {
public final int instanceNumber;
public ImapResponseParserTestException(int instanceNumber) {
this.instanceNumber = instanceNumber;
}
}
static class TestUntaggedHandler implements UntaggedHandler {
public final List<ImapResponse> responses = new ArrayList<ImapResponse>();
@Override
public void handleAsyncUntaggedResponse(ImapResponse response) {
responses.add(response);
}
}
}