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
This commit is contained in:
Andy Stadler 2011-03-25 15:08:34 -07:00
parent 7331017905
commit 0408ee5b94
2 changed files with 38 additions and 3 deletions

View File

@ -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();

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