Merge pull request #1133 from k9mail/GH-811_fix_ClassCastException

Avoid ClassCastException in ImapResponseParser
This commit is contained in:
cketti 2016-03-03 01:48:15 +01:00
commit 150fc822c2
2 changed files with 27 additions and 4 deletions

View file

@ -127,12 +127,16 @@ class ImapResponseParser {
private void readTokens(ImapResponse response) throws IOException {
response.clear();
String firstToken = (String) readToken(response);
response.add(firstToken);
Object firstToken = readToken(response);
if (isStatusResponse(firstToken)) {
checkTokenIsString(firstToken);
String symbol = (String) firstToken;
response.add(symbol);
if (isStatusResponse(symbol)) {
parseResponseText(response);
} else if (equalsIgnoreCase(firstToken, Responses.LIST) || equalsIgnoreCase(firstToken, Responses.LSUB)) {
} else if (equalsIgnoreCase(symbol, Responses.LIST) || equalsIgnoreCase(symbol, Responses.LSUB)) {
parseListResponse(response);
} else {
Object token;
@ -452,4 +456,10 @@ class ImapResponseParser {
return symbol.equalsIgnoreCase((String) token);
}
private void checkTokenIsString(Object token) throws IOException {
if (!(token instanceof String)) {
throw new IOException("Unexpected non-string token: " + token);
}
}
}

View file

@ -17,6 +17,7 @@ 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(RobolectricTestRunner.class)
@ -312,6 +313,18 @@ public class ImapResponseParserTest {
assertEquals("TAG", responseTwo.getTag());
}
@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 (" +