From 0408ee5b946142da9457b5bf596cb84d33d409e3 Mon Sep 17 00:00:00 2001 From: Andy Stadler Date: Fri, 25 Mar 2011 15:08:34 -0700 Subject: [PATCH] DO NOT MERGE - Fix NPE in pop3 checkcapabilities * This happens if an open fails immediately (error message in the initial banner) followed by a checkSettings. * The fix is to harden checkSettings to force a clean connection every time. Bug: 2170147 Backport of: If7403bf517477d2b03b21d71caab511fe45e234c Change-Id: Ia6cc0e3ab0c8a8a78b5d8b8fb7b8ba4b4cdd3ef2 --- .../android/email/mail/store/Pop3Store.java | 4 ++ .../email/mail/store/Pop3StoreUnitTests.java | 37 +++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/com/android/email/mail/store/Pop3Store.java b/src/com/android/email/mail/store/Pop3Store.java index 9990fde5b..7e6480d01 100644 --- a/src/com/android/email/mail/store/Pop3Store.java +++ b/src/com/android/email/mail/store/Pop3Store.java @@ -171,6 +171,10 @@ public class Pop3Store extends Store { @Override public void checkSettings() throws MessagingException { Pop3Folder folder = new Pop3Folder("INBOX"); + // Close any open or half-open connections - checkSettings should always be "fresh" + if (mTransport.isOpen()) { + folder.close(false); + } try { folder.open(OpenMode.READ_WRITE, null); folder.checkSettings(); diff --git a/tests/src/com/android/email/mail/store/Pop3StoreUnitTests.java b/tests/src/com/android/email/mail/store/Pop3StoreUnitTests.java index 4a210262d..75714f3d1 100644 --- a/tests/src/com/android/email/mail/store/Pop3StoreUnitTests.java +++ b/tests/src/com/android/email/mail/store/Pop3StoreUnitTests.java @@ -21,12 +21,12 @@ import com.android.email.mail.Address; import com.android.email.mail.FetchProfile; import com.android.email.mail.Flag; import com.android.email.mail.Folder; -import com.android.email.mail.Message; -import com.android.email.mail.MessagingException; -import com.android.email.mail.Transport; import com.android.email.mail.Folder.FolderType; import com.android.email.mail.Folder.OpenMode; +import com.android.email.mail.Message; import com.android.email.mail.Message.RecipientType; +import com.android.email.mail.MessagingException; +import com.android.email.mail.Transport; import com.android.email.mail.internet.MimeMessage; import com.android.email.mail.transport.MockTransport; @@ -204,6 +204,37 @@ public class Pop3StoreUnitTests extends AndroidTestCase { } } + /** + * Test a strange case that causes open to proceed without mCapabilities + * open - fail with "-" error code + * then check capabilities + */ + public void testCheckSettingsCapabilities() throws MessagingException { + + MockTransport mockTransport = openAndInjectMockTransport(); + + // First, preload an open that fails for some reason + mockTransport.expect(null, "-ERR from the Mock Transport."); + + // And watch it fail + try { + Pop3Store.Pop3Folder folder = mStore.new Pop3Folder("INBOX"); + folder.open(OpenMode.READ_WRITE, null); + fail("Should have thrown exception"); + } catch (MessagingException me) { + // Expected - continue. + } + + // Now try again (assuming a slightly different connection setup - successful) + // Note, checkSettings is going to try to close the connection again, so we expect + // one extra QUIT before we spin it up again + mockTransport.expect("QUIT", ""); + mockTransport.expectClose(); + setupOpenFolder(mockTransport, 0, "UIDL"); + mockTransport.expect("QUIT", ""); + mStore.checkSettings(); + } + /** * Test small Store & Folder functions that manage folders & namespace */