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
Change-Id: If7403bf517477d2b03b21d71caab511fe45e234c
This commit is contained in:
Andy Stadler 2010-11-04 23:34:26 -07:00
parent 111d67acc5
commit 54ab7a27a3
2 changed files with 38 additions and 3 deletions

View File

@ -174,6 +174,10 @@ public class Pop3Store extends Store {
public Bundle checkSettings() throws MessagingException {
Pop3Folder folder = new Pop3Folder("INBOX");
Bundle bundle = null;
// 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);
bundle = folder.checkSettings();

View File

@ -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
*/