Fix bugs in ImapResponseParser
This commit is contained in:
parent
7d429d565b
commit
98b59f1709
3 changed files with 64 additions and 5 deletions
|
@ -339,7 +339,18 @@ class ImapResponseParser {
|
|||
*/
|
||||
private Object parseLiteral() throws IOException {
|
||||
expect('{');
|
||||
int size = Integer.parseInt(readStringUntil('}'));
|
||||
|
||||
int size;
|
||||
try {
|
||||
size = Integer.parseInt(readStringUntil('}'));
|
||||
} catch (NumberFormatException e) {
|
||||
throw new ImapResponseParserException("Invalid value for size of literal string", e);
|
||||
}
|
||||
|
||||
if (size < 0) {
|
||||
throw new ImapResponseParserException("Invalid value for size of literal string");
|
||||
}
|
||||
|
||||
expect('\r');
|
||||
expect('\n');
|
||||
|
||||
|
@ -479,9 +490,9 @@ class ImapResponseParser {
|
|||
|
||||
}
|
||||
|
||||
private void checkTokenIsString(Object token) throws IOException {
|
||||
private void checkTokenIsString(Object token) {
|
||||
if (!(token instanceof String)) {
|
||||
throw new IOException("Unexpected non-string token: " + token.getClass().getSimpleName() + " - " + token);
|
||||
throw new ImapResponseParserException("Unexpected non-string token");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,10 @@ package com.fsck.k9.mail.store.imap;
|
|||
|
||||
|
||||
public class ImapResponseParserException extends RuntimeException {
|
||||
public ImapResponseParserException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ImapResponseParserException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
|
|
@ -236,6 +236,39 @@ class ImapResponseParserTest {
|
|||
assertThatAllInputWasConsumed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `readResponse() with literal containing negative size`() {
|
||||
val parser = createParserWithResponses("* {-1}")
|
||||
|
||||
assertThat {
|
||||
parser.readResponse()
|
||||
}.isFailure()
|
||||
.isInstanceOf(ImapResponseParserException::class)
|
||||
.hasMessage("Invalid value for size of literal string")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `readResponse() with literal size exceeding Int`() {
|
||||
val parser = createParserWithResponses("* {2147483648}")
|
||||
|
||||
assertThat {
|
||||
parser.readResponse()
|
||||
}.isFailure()
|
||||
.isInstanceOf(ImapResponseParserException::class)
|
||||
.hasMessage("Invalid value for size of literal string")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `readResponse() with invalid characters for literal size`() {
|
||||
val parser = createParserWithResponses("* {invalid}")
|
||||
|
||||
assertThat {
|
||||
parser.readResponse()
|
||||
}.isFailure()
|
||||
.isInstanceOf(ImapResponseParserException::class)
|
||||
.hasMessage("Invalid value for size of literal string")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `readResponse() should throw when end of stream is reached while reading literal`() {
|
||||
val parser = createParserWithData("* {4}\r\nabc")
|
||||
|
@ -452,6 +485,17 @@ class ImapResponseParserTest {
|
|||
assertThatAllInputWasConsumed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `readResponse() with tagged response missing completion code should throw`() {
|
||||
val parser = createParserWithResponses("tag ")
|
||||
|
||||
assertThat {
|
||||
parser.readResponse()
|
||||
}.isFailure()
|
||||
.isInstanceOf(ImapResponseParserException::class)
|
||||
.hasMessage("Unexpected non-string token")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `readResponse() with list as first token should throw`() {
|
||||
val parser = createParserWithResponses("* [1 2] 3")
|
||||
|
@ -459,8 +503,8 @@ class ImapResponseParserTest {
|
|||
assertThat {
|
||||
parser.readResponse()
|
||||
}.isFailure()
|
||||
.isInstanceOf(IOException::class)
|
||||
.hasMessage("Unexpected non-string token: ImapList - [1, 2]")
|
||||
.isInstanceOf(ImapResponseParserException::class)
|
||||
.hasMessage("Unexpected non-string token")
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in a new issue