diff --git a/src/com/android/email/mail/store/Pop3Store.java b/src/com/android/email/mail/store/Pop3Store.java index 25e2e1c4e..8104e859f 100644 --- a/src/com/android/email/mail/store/Pop3Store.java +++ b/src/com/android/email/mail/store/Pop3Store.java @@ -511,6 +511,9 @@ public class Pop3Store extends Store { */ public boolean parseSingleLine(String response) { mErr = false; + if (response == null || response.length() == 0) { + return false; + } char first = response.charAt(0); if (first == '+') { String[] uidParts = response.split(" +"); @@ -536,6 +539,9 @@ public class Pop3Store extends Store { */ public boolean parseMultiLine(String response) { mErr = false; + if (response == null || response.length() == 0) { + return false; + } char first = response.charAt(0); if (first == '.') { mEndOfMessage = true; diff --git a/src/com/android/email/mail/transport/SmtpSender.java b/src/com/android/email/mail/transport/SmtpSender.java index 9aa4f1468..b7f585a74 100644 --- a/src/com/android/email/mail/transport/SmtpSender.java +++ b/src/com/android/email/mail/transport/SmtpSender.java @@ -280,9 +280,11 @@ public class SmtpSender extends Sender { result += line.substring(3); } - char c = result.charAt(0); - if ((c == '4') || (c == '5')) { - throw new MessagingException(result); + if (result.length() > 0) { + char c = result.charAt(0); + if ((c == '4') || (c == '5')) { + throw new MessagingException(result); + } } return result; diff --git a/tests/src/com/android/email/mail/store/Pop3StoreUnitTests.java b/tests/src/com/android/email/mail/store/Pop3StoreUnitTests.java index 4230871d9..da8fe4381 100644 --- a/tests/src/com/android/email/mail/store/Pop3StoreUnitTests.java +++ b/tests/src/com/android/email/mail/store/Pop3StoreUnitTests.java @@ -104,6 +104,42 @@ public class Pop3StoreUnitTests extends AndroidTestCase { assertTrue(parser.mErr); } + /** + * Test various rainy-day operations of the UIDL parser for multi-line responses + * TODO other malformed responses + */ + public void testUIDLParserMultiFail() { + // multi-line mode + Pop3Store.Pop3Folder.UidlParser parser = mFolder.new UidlParser(); + + // Test with null input + boolean result; + result = parser.parseMultiLine(null); + assertFalse(result); + + // Test with empty input + result = parser.parseMultiLine(""); + assertFalse(result); + } + + /** + * Test various rainy-day operations of the UIDL parser for single-line responses + * TODO other malformed responses + */ + public void testUIDLParserSingleFail() { + // single-line mode + Pop3Store.Pop3Folder.UidlParser parser = mFolder.new UidlParser(); + + // Test with null input + boolean result; + result = parser.parseSingleLine(null); + assertFalse(result); + + // Test with empty input + result = parser.parseSingleLine(""); + assertFalse(result); + } + /** * Tests that variants on the RFC-specified formatting of UIDL work properly. */ diff --git a/tests/src/com/android/email/mail/transport/SmtpSenderUnitTests.java b/tests/src/com/android/email/mail/transport/SmtpSenderUnitTests.java index 5414e7d07..08b932a62 100644 --- a/tests/src/com/android/email/mail/transport/SmtpSenderUnitTests.java +++ b/tests/src/com/android/email/mail/transport/SmtpSenderUnitTests.java @@ -109,6 +109,30 @@ public class SmtpSenderUnitTests extends AndroidTestCase { mSender.sendMessage(message); } + /** + * Test: Recover from a server closing early (or returning an empty string) + */ + public void testEmptyLineResponse() throws MessagingException { + MockTransport mockTransport = openAndInjectMockTransport(); + + // Since SmtpSender.sendMessage() does a close then open, we need to preset for the open + mockTransport.expectClose(); + + // Load up just the bare minimum to expose the error + mockTransport.expect(null, "220 MockTransport 2000 Ready To Assist You Peewee"); + mockTransport.expect("EHLO .*", ""); + + // Now trigger the transmission + // Note, a null message is sufficient here, as we won't even get past open() + try { + mSender.sendMessage(null); + fail("Should not be able to send with failed open()"); + } catch (MessagingException me) { + // good - expected + // TODO maybe expect a particular exception? + } + } + /** * Set up a basic MockTransport. open it, and inject it into mStore */