1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-24 08:38:51 -05:00

Added some error checks when processing IMAP FETCH responses

This commit is contained in:
cketti 2012-04-03 07:35:19 +02:00
parent 4612ceb3c8
commit 0caac114a6
3 changed files with 42 additions and 9 deletions

View File

@ -409,7 +409,7 @@ public class ImapResponseParser {
public Object getKeyedValue(Object key) {
for (int i = 0, count = size(); i < count; i++) {
for (int i = 0, count = size() - 1; i < count; i++) {
if (equalsIgnoreCase(get(i), key)) {
return get(i + 1);
}
@ -434,7 +434,7 @@ public class ImapResponseParser {
return false;
}
for (int i = 0, count = size(); i < count; i++) {
for (int i = 0, count = size() - 1; i < count; i++) {
if (equalsIgnoreCase(key, get(i))) {
return true;
}
@ -443,7 +443,7 @@ public class ImapResponseParser {
}
public int getKeyIndex(Object key) {
for (int i = 0, count = size(); i < count; i++) {
for (int i = 0, count = size() - 1; i < count; i++) {
if (equalsIgnoreCase(key, get(i))) {
return i;
}

View File

@ -1655,13 +1655,16 @@ public class ImapStore extends Store {
if (fetchList.containsKey("BODY")) {
int index = fetchList.getKeyIndex("BODY") + 2;
result = fetchList.getObject(index);
int size = fetchList.size();
if (index < size) {
result = fetchList.getObject(index);
// Check if there's an origin octet
if (result instanceof String) {
String originOctet = (String)result;
if (originOctet.startsWith("<")) {
result = fetchList.getObject(index + 1);
// Check if there's an origin octet
if (result instanceof String) {
String originOctet = (String) result;
if (originOctet.startsWith("<") && (index + 1) < size) {
result = fetchList.getObject(index + 1);
}
}
}
}

View File

@ -59,6 +59,36 @@ public class ImapResponseParserTest extends TestCase {
assertEquals("token2", respTextCode.get(1));
}
public void testImapListMethods() throws IOException {
ImapList list = new ImapList();
list.add("ONE");
list.add("TWO");
list.add("THREE");
assertTrue(list.containsKey("ONE"));
assertTrue(list.containsKey("TWO"));
assertFalse(list.containsKey("THREE"));
assertFalse(list.containsKey("nonexistent"));
assertEquals("TWO", list.getKeyedValue("ONE"));
assertEquals("THREE", list.getKeyedValue("TWO"));
assertNull(list.getKeyedValue("THREE"));
assertNull(list.getKeyedValue("nonexistent"));
assertEquals(0, list.getKeyIndex("ONE"));
assertEquals(1, list.getKeyIndex("TWO"));
try {
list.getKeyIndex("THREE");
fail("IllegalArgumentException should have been thrown");
} catch (IllegalArgumentException e) { /* do nothing */ }
try {
list.getKeyIndex("nonexistent");
fail("IllegalArgumentException should have been thrown");
} catch (IllegalArgumentException e) { /* do nothing */ }
}
private ImapResponseParser createParser(String response) {
ByteArrayInputStream in = new ByteArrayInputStream(response.getBytes());
PeekableInputStream pin = new PeekableInputStream(in);