From 02a0edb60d0a0dfeffd2bf09ed2ba51f34926ded Mon Sep 17 00:00:00 2001 From: Andy Stadler Date: Mon, 27 Sep 2010 13:31:04 -0700 Subject: [PATCH] Allow spaces in passwords DO NOT MERGE * Separate/identical fixes for incoming, outgoing, exchange * Unit tests * Some protocols will fail anyway (e.g. POP3) Bug: 2981433 Backport from: I82984e5912fc7fcb88e747815d0fe33cb36605e7 Change-Id: I49d5c13137e4f78b6fa0f9ce288c1a78ff028f88 --- .../activity/setup/AccountSetupExchange.java | 4 +- .../activity/setup/AccountSetupIncoming.java | 4 +- .../activity/setup/AccountSetupOutgoing.java | 5 +-- .../setup/AccountSetupExchangeTests.java | 42 +++++++++++++++++++ .../setup/AccountSetupIncomingTests.java | 42 +++++++++++++++++++ .../setup/AccountSetupOutgoingTests.java | 42 +++++++++++++++++++ 6 files changed, 132 insertions(+), 7 deletions(-) diff --git a/src/com/android/email/activity/setup/AccountSetupExchange.java b/src/com/android/email/activity/setup/AccountSetupExchange.java index cb4b59091..c532220b8 100644 --- a/src/com/android/email/activity/setup/AccountSetupExchange.java +++ b/src/com/android/email/activity/setup/AccountSetupExchange.java @@ -423,7 +423,7 @@ public class AccountSetupExchange extends Activity implements OnClickListener, * a problem with the user input. * @return a URI built from the account setup fields */ - private URI getUri() throws URISyntaxException { + /* package */ URI getUri() throws URISyntaxException { boolean sslRequired = mSslSecurityView.isChecked(); boolean trustCertificates = mTrustCertificatesView.isChecked(); String scheme = (sslRequired) @@ -436,7 +436,7 @@ public class AccountSetupExchange extends Activity implements OnClickListener, userName = userName.substring(1); } mCacheLoginCredential = userName; - String userInfo = userName + ":" + mPasswordView.getText().toString().trim(); + String userInfo = userName + ":" + mPasswordView.getText(); String host = mServerView.getText().toString().trim(); String path = null; diff --git a/src/com/android/email/activity/setup/AccountSetupIncoming.java b/src/com/android/email/activity/setup/AccountSetupIncoming.java index 3efc790a2..518d76084 100644 --- a/src/com/android/email/activity/setup/AccountSetupIncoming.java +++ b/src/com/android/email/activity/setup/AccountSetupIncoming.java @@ -373,7 +373,7 @@ public class AccountSetupIncoming extends Activity implements OnClickListener { * a problem with the user input. * @return a URI built from the account setup fields */ - private URI getUri() throws URISyntaxException { + /* package */ URI getUri() throws URISyntaxException { int securityType = (Integer)((SpinnerOption)mSecurityTypeView.getSelectedItem()).value; String path = null; if (mAccountSchemes[securityType].startsWith("imap")) { @@ -383,7 +383,7 @@ public class AccountSetupIncoming extends Activity implements OnClickListener { mCacheLoginCredential = userName; URI uri = new URI( mAccountSchemes[securityType], - userName + ":" + mPasswordView.getText().toString().trim(), + userName + ":" + mPasswordView.getText(), mServerView.getText().toString().trim(), Integer.parseInt(mPortView.getText().toString().trim()), path, // path diff --git a/src/com/android/email/activity/setup/AccountSetupOutgoing.java b/src/com/android/email/activity/setup/AccountSetupOutgoing.java index e647d5569..ea4170e52 100644 --- a/src/com/android/email/activity/setup/AccountSetupOutgoing.java +++ b/src/com/android/email/activity/setup/AccountSetupOutgoing.java @@ -272,12 +272,11 @@ public class AccountSetupOutgoing extends Activity implements OnClickListener, * a problem with the user input. * @return a URI built from the account setup fields */ - private URI getUri() throws URISyntaxException { + /* package */ URI getUri() throws URISyntaxException { int securityType = (Integer)((SpinnerOption)mSecurityTypeView.getSelectedItem()).value; String userInfo = null; if (mRequireLoginView.isChecked()) { - userInfo = mUsernameView.getText().toString().trim() + ":" - + mPasswordView.getText().toString().trim(); + userInfo = mUsernameView.getText().toString().trim() + ":" + mPasswordView.getText(); } URI uri = new URI( SMTP_SCHEMES[securityType], diff --git a/tests/src/com/android/email/activity/setup/AccountSetupExchangeTests.java b/tests/src/com/android/email/activity/setup/AccountSetupExchangeTests.java index c4b15f831..f18207689 100644 --- a/tests/src/com/android/email/activity/setup/AccountSetupExchangeTests.java +++ b/tests/src/com/android/email/activity/setup/AccountSetupExchangeTests.java @@ -31,6 +31,9 @@ import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; +import java.net.URI; +import java.net.URISyntaxException; + /** * Tests of the basic UI logic in the Account Setup Incoming (IMAP / POP3) screen. */ @@ -40,6 +43,7 @@ public class AccountSetupExchangeTests extends //EXCHANGE-REMOVE-SECTION-START private AccountSetupExchange mActivity; private EditText mServerView; + private EditText mPasswordView; private Button mNextButton; private CheckBox mSslRequiredCheckbox; private CheckBox mTrustAllCertificatesCheckbox; @@ -160,6 +164,43 @@ public class AccountSetupExchangeTests extends assertTrue(mTrustAllCertificatesCheckbox.getVisibility() == View.VISIBLE); } + /** + * Test to confirm that passwords with leading or trailing spaces are accepted verbatim. + */ + @UiThreadTest + public void testPasswordNoTrim() throws URISyntaxException { + getActivityAndFields(); + + // Clear the password - should disable + checkPassword(null, false); + + // Various combinations of spaces should be OK + checkPassword(" leading", true); + checkPassword("trailing ", true); + checkPassword("em bedded", true); + checkPassword(" ", true); + } + + /** + * Check password field for a given password. Should be called in UI thread. Confirms that + * the password has not been trimmed. + * + * @param password the password to test with + * @param expectNext true if expected that this password will enable the "next" button + */ + private void checkPassword(String password, boolean expectNext) throws URISyntaxException { + mPasswordView.setText(password); + if (expectNext) { + assertTrue(mNextButton.isEnabled()); + URI uri = mActivity.getUri(); + String actualUserInfo = uri.getUserInfo(); + String actualPassword = actualUserInfo.split(":", 2)[1]; + assertEquals(password, actualPassword); + } else { + assertFalse(mNextButton.isEnabled()); + } + } + /** * TODO: Directly test validateFields() checking boolean result */ @@ -170,6 +211,7 @@ public class AccountSetupExchangeTests extends private void getActivityAndFields() { mActivity = getActivity(); mServerView = (EditText) mActivity.findViewById(R.id.account_server); + mPasswordView = (EditText) mActivity.findViewById(R.id.account_password); mNextButton = (Button) mActivity.findViewById(R.id.next); mSslRequiredCheckbox = (CheckBox) mActivity.findViewById(R.id.account_ssl); mTrustAllCertificatesCheckbox = diff --git a/tests/src/com/android/email/activity/setup/AccountSetupIncomingTests.java b/tests/src/com/android/email/activity/setup/AccountSetupIncomingTests.java index 96753f489..dfab45cb8 100644 --- a/tests/src/com/android/email/activity/setup/AccountSetupIncomingTests.java +++ b/tests/src/com/android/email/activity/setup/AccountSetupIncomingTests.java @@ -26,6 +26,9 @@ import android.test.suitebuilder.annotation.MediumTest; import android.widget.Button; import android.widget.EditText; +import java.net.URI; +import java.net.URISyntaxException; + /** * Tests of the basic UI logic in the Account Setup Incoming (IMAP / POP3) screen. */ @@ -38,6 +41,7 @@ public class AccountSetupIncomingTests extends private AccountSetupIncoming mActivity; private EditText mServerView; + private EditText mPasswordView; private Button mNextButton; public AccountSetupIncomingTests() { @@ -126,6 +130,43 @@ public class AccountSetupIncomingTests extends assertFalse(mNextButton.isEnabled()); } + /** + * Test to confirm that passwords with leading or trailing spaces are accepted verbatim. + */ + @UiThreadTest + public void testPasswordNoTrim() throws URISyntaxException { + getActivityAndFields(); + + // Clear the password - should disable + checkPassword(null, false); + + // Various combinations of spaces should be OK + checkPassword(" leading", true); + checkPassword("trailing ", true); + checkPassword("em bedded", true); + checkPassword(" ", true); + } + + /** + * Check password field for a given password. Should be called in UI thread. Confirms that + * the password has not been trimmed. + * + * @param password the password to test with + * @param expectNext true if expected that this password will enable the "next" button + */ + private void checkPassword(String password, boolean expectNext) throws URISyntaxException { + mPasswordView.setText(password); + if (expectNext) { + assertTrue(mNextButton.isEnabled()); + URI uri = mActivity.getUri(); + String actualUserInfo = uri.getUserInfo(); + String actualPassword = actualUserInfo.split(":", 2)[1]; + assertEquals(password, actualPassword); + } else { + assertFalse(mNextButton.isEnabled()); + } + } + /** * TODO: A series of tests to explore the logic around security models & ports * TODO: A series of tests exploring differences between IMAP and POP3 @@ -137,6 +178,7 @@ public class AccountSetupIncomingTests extends private void getActivityAndFields() { mActivity = getActivity(); mServerView = (EditText) mActivity.findViewById(R.id.account_server); + mPasswordView = (EditText) mActivity.findViewById(R.id.account_password); mNextButton = (Button) mActivity.findViewById(R.id.next); } diff --git a/tests/src/com/android/email/activity/setup/AccountSetupOutgoingTests.java b/tests/src/com/android/email/activity/setup/AccountSetupOutgoingTests.java index d1f0a0a46..9e1f39d57 100644 --- a/tests/src/com/android/email/activity/setup/AccountSetupOutgoingTests.java +++ b/tests/src/com/android/email/activity/setup/AccountSetupOutgoingTests.java @@ -26,6 +26,9 @@ import android.test.suitebuilder.annotation.MediumTest; import android.widget.Button; import android.widget.EditText; +import java.net.URI; +import java.net.URISyntaxException; + /** * Tests of the basic UI logic in the Account Setup Outgoing (SMTP) screen. */ @@ -35,6 +38,7 @@ public class AccountSetupOutgoingTests extends private AccountSetupOutgoing mActivity; private EditText mServerView; + private EditText mPasswordView; private Button mNextButton; public AccountSetupOutgoingTests() { @@ -121,6 +125,43 @@ public class AccountSetupOutgoingTests extends assertFalse(mNextButton.isEnabled()); } + /** + * Test to confirm that passwords with leading or trailing spaces are accepted verbatim. + */ + @UiThreadTest + public void testPasswordNoTrim() throws URISyntaxException { + getActivityAndFields(); + + // Clear the password - should disable + checkPassword(null, false); + + // Various combinations of spaces should be OK + checkPassword(" leading", true); + checkPassword("trailing ", true); + checkPassword("em bedded", true); + checkPassword(" ", true); + } + + /** + * Check password field for a given password. Should be called in UI thread. Confirms that + * the password has not been trimmed. + * + * @param password the password to test with + * @param expectNext true if expected that this password will enable the "next" button + */ + private void checkPassword(String password, boolean expectNext) throws URISyntaxException { + mPasswordView.setText(password); + if (expectNext) { + assertTrue(mNextButton.isEnabled()); + URI uri = mActivity.getUri(); + String actualUserInfo = uri.getUserInfo(); + String actualPassword = actualUserInfo.split(":", 2)[1]; + assertEquals(password, actualPassword); + } else { + assertFalse(mNextButton.isEnabled()); + } + } + /** * TODO: A series of tests to explore the logic around security models & ports */ @@ -131,6 +172,7 @@ public class AccountSetupOutgoingTests extends private void getActivityAndFields() { mActivity = getActivity(); mServerView = (EditText) mActivity.findViewById(R.id.account_server); + mPasswordView = (EditText) mActivity.findViewById(R.id.account_password); mNextButton = (Button) mActivity.findViewById(R.id.next); }