private void readInitialResponse() throws IOException { ImapResponse initialResponse = responseParser.readResponse(); if (K9MailLib.isDebug() && DEBUG_PROTOCOL_IMAP) { Timber.v("%s <<< %s", getLogId(), initialResponse); } extractCapabilities(Collections.singletonList(initialResponse)); }
public static ImapResponse createImapResponse(String response) throws IOException { String input = response + "\r\n"; PeekableInputStream inputStream = new PeekableInputStream(new ByteArrayInputStream(input.getBytes())); ImapResponseParser parser = new ImapResponseParser(inputStream); return parser.readResponse(); }
public ImapResponse readResponse(ImapResponseCallback callback) throws IOException { try { ImapResponse response = responseParser.readResponse(callback); if (K9MailLib.isDebug() && DEBUG_PROTOCOL_IMAP) { Timber.v("%s<<<%s", getLogId(), response); } return response; } catch (IOException e) { close(); throw e; } }
@Test(expected = IOException.class) public void testParseLiteralToEndOfStream() throws Exception { ImapResponseParser parser = createParser("* {4}\r\nabc"); parser.readResponse(); }
@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 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(expected = IOException.class) public void testParseUntaggedResponseWithoutSpace() throws Exception { ImapResponseParser parser = createParser("*\r\n"); parser.readResponse(); }
@Test(expected = IOException.class) public void testReadStringUntilEndOfStream() throws IOException { ImapResponseParser parser = createParser("* OK Some text "); parser.readResponse(); }
@Test(expected = IOException.class) public void testParseQuotedToEndOfStream() throws Exception { ImapResponseParser parser = createParser("* \"abc"); parser.readResponse(); }
@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: ImapList - [1, 2]", e.getMessage()); } }
@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 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 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 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 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 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 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 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 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 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)); }