Use HostAuth to determine settings changes

There were two TODO's from a prior CL where deprecated HostAuth methods
were removed.

1. Do not use a generated URI to determine if account settings have changed.
   Instead, use the HostAuth structure for this purpose.
2. The account key in the HostAuth structure has been deprecated. Remove as
   much of it as possible (until the schema of the host auth database changes,
   we must still refer to it when adding rows).

In the process, HostAuth tests were broken out into a separate unit test
file.

Change-Id: I4075da09af168f734db7b20a9ef63d4178ac2064
This commit is contained in:
Todd Kennedy 2011-02-17 10:27:31 -08:00
parent bfb76aa224
commit fe68c0e7c2
12 changed files with 608 additions and 493 deletions

View File

@ -53,9 +53,8 @@ public abstract class AccountServerBaseFragment extends Fragment
protected Context mContext;
protected Callback mCallback = EmptyCallback.INSTANCE;
protected boolean mSettingsMode;
// TODO See how to get rid of this. Maybe define an "equals()" for HostAuth?
// The URI that represents this account's currently saved settings
protected URI mLoadedUri;
/*package*/ HostAuth mLoadedSendAuth;
/*package*/ HostAuth mLoadedRecvAuth;
// This is null in the setup wizard screens, and non-null in AccountSettings mode
public Button mProceedButton;
@ -301,15 +300,15 @@ public abstract class AccountServerBaseFragment extends Fragment
* Returns whether or not any settings have changed.
*/
public boolean haveSettingsChanged() {
URI newUri = null;
Account account = SetupData.getAccount();
try {
newUri = getUri();
} catch (URISyntaxException ignore) {
// ignore
}
HostAuth sendAuth = account.getOrCreateHostAuthSend(mContext);
boolean sendChanged = (mLoadedSendAuth != null && !mLoadedSendAuth.equals(sendAuth));
return (mLoadedUri == null) || !mLoadedUri.equals(newUri);
HostAuth recvAuth = account.getOrCreateHostAuthRecv(mContext);
boolean recvChanged = (mLoadedRecvAuth != null && !mLoadedRecvAuth.equals(recvAuth));
return sendChanged || recvChanged;
}
/**

View File

@ -229,7 +229,7 @@ public class AccountSetupExchangeFragment extends AccountServerBaseFragment
*
* @return true if the loaded values pass validation
*/
/* package */ boolean loadSettings(Account account) {
/*package*/ boolean loadSettings(Account account) {
if (mLoaded) return validateFields();
HostAuth hostAuth = account.mHostAuthRecv;
@ -263,14 +263,7 @@ public class AccountSetupExchangeFragment extends AccountServerBaseFragment
mTrustCertificatesView.setChecked(trustCertificates);
showTrustCertificates(ssl);
// TODO See how to get rid of this. Maybe define an "equals()" for HostAuth?
// used to determine if these settings have changed
try {
mLoadedUri = getUri();
} catch (URISyntaxException ignore) {
// ignore; should not happen
}
mLoadedRecvAuth = hostAuth;
mLoaded = true;
return validateFields();
}

View File

@ -344,14 +344,7 @@ public class AccountSetupIncomingFragment extends AccountServerBaseFragment {
updatePortFromSecurityType();
}
// TODO See how to get rid of this. Maybe define an "equals()" for HostAuth?
// used to determine if these settings have changed
try {
mLoadedUri = getUri();
} catch (URISyntaxException ignore) {
// ignore; should not happen
}
mLoadedRecvAuth = recvAuth;
mLoaded = true;
validateFields();
}

View File

@ -256,43 +256,36 @@ public class AccountSetupOutgoingFragment extends AccountServerBaseFragment
private void loadSettings() {
if (mLoaded) return;
HostAuth senderAuth = SetupData.getAccount().getOrCreateHostAuthSend(mContext);
if ((senderAuth.mFlags & HostAuth.FLAG_AUTHENTICATE) != 0) {
String username = senderAuth.mLogin;
HostAuth sendAuth = SetupData.getAccount().getOrCreateHostAuthSend(mContext);
if ((sendAuth.mFlags & HostAuth.FLAG_AUTHENTICATE) != 0) {
String username = sendAuth.mLogin;
if (username != null) {
mUsernameView.setText(username);
mRequireLoginView.setChecked(true);
}
String password = senderAuth.mPassword;
String password = sendAuth.mPassword;
if (password != null) {
mPasswordView.setText(password);
}
}
int flags = senderAuth.mFlags & ~HostAuth.FLAG_AUTHENTICATE;
int flags = sendAuth.mFlags & ~HostAuth.FLAG_AUTHENTICATE;
SpinnerOption.setSpinnerOptionValue(mSecurityTypeView, flags);
String hostname = senderAuth.mAddress;
String hostname = sendAuth.mAddress;
if (hostname != null) {
mServerView.setText(hostname);
}
int port = senderAuth.mPort;
int port = sendAuth.mPort;
if (port != -1) {
mPortView.setText(Integer.toString(port));
} else {
updatePortFromSecurityType();
}
// TODO See how to get rid of this. Maybe define an "equals()" for HostAuth?
// used to determine if these settings have changed
try {
mLoadedUri = getUri();
} catch (URISyntaxException ignore) {
// ignore; should not happen
}
mLoadedSendAuth = sendAuth;
mLoaded = true;
validateFields();
}

View File

@ -2569,8 +2569,6 @@ public abstract class EmailContent {
public String mLogin;
public String mPassword;
public String mDomain;
// TODO Remove all vestiges of this
public long mAccountKey; // DEPRECATED - Will not be set or stored
public static final int CONTENT_ID_COLUMN = 0;
public static final int CONTENT_PROTOCOL_COLUMN = 1;
@ -2580,13 +2578,11 @@ public abstract class EmailContent {
public static final int CONTENT_LOGIN_COLUMN = 5;
public static final int CONTENT_PASSWORD_COLUMN = 6;
public static final int CONTENT_DOMAIN_COLUMN = 7;
public static final int CONTENT_ACCOUNT_KEY_COLUMN = 8;
public static final String[] CONTENT_PROJECTION = new String[] {
RECORD_ID, HostAuthColumns.PROTOCOL, HostAuthColumns.ADDRESS, HostAuthColumns.PORT,
HostAuthColumns.FLAGS, HostAuthColumns.LOGIN,
HostAuthColumns.PASSWORD, HostAuthColumns.DOMAIN,
HostAuthColumns.ACCOUNT_KEY
HostAuthColumns.PASSWORD, HostAuthColumns.DOMAIN
};
/**
@ -2678,7 +2674,6 @@ public abstract class EmailContent {
mLogin = cursor.getString(CONTENT_LOGIN_COLUMN);
mPassword = cursor.getString(CONTENT_PASSWORD_COLUMN);
mDomain = cursor.getString(CONTENT_DOMAIN_COLUMN);
mAccountKey = cursor.getLong(CONTENT_ACCOUNT_KEY_COLUMN);
}
@Override
@ -2691,7 +2686,7 @@ public abstract class EmailContent {
values.put(HostAuthColumns.LOGIN, mLogin);
values.put(HostAuthColumns.PASSWORD, mPassword);
values.put(HostAuthColumns.DOMAIN, mDomain);
values.put(HostAuthColumns.ACCOUNT_KEY, mAccountKey);
values.put(HostAuthColumns.ACCOUNT_KEY, 0); // Need something to satisfy the DB
return values;
}
@ -2832,7 +2827,6 @@ public abstract class EmailContent {
dest.writeString(mLogin);
dest.writeString(mPassword);
dest.writeString(mDomain);
dest.writeLong(mAccountKey);
}
/**
@ -2848,7 +2842,6 @@ public abstract class EmailContent {
mLogin = in.readString();
mPassword = in.readString();
mDomain = in.readString();
mAccountKey = in.readLong();
}
/**
@ -2858,5 +2851,20 @@ public abstract class EmailContent {
public String toString() {
return getStoreUri();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof HostAuth)) {
return false;
}
HostAuth that = (HostAuth)o;
return mPort == that.mPort
&& mFlags == that.mFlags
&& Utility.areStringsEqual(mProtocol, that.mProtocol)
&& Utility.areStringsEqual(mAddress, that.mAddress)
&& Utility.areStringsEqual(mLogin, that.mLogin)
&& Utility.areStringsEqual(mPassword, that.mPassword)
&& Utility.areStringsEqual(mDomain, that.mDomain);
}
}
}

View File

@ -109,15 +109,10 @@ public class Utility {
}
/**
* Combines the given array of Objects into a single string using the
* seperator character and each Object's toString() method. between each
* part.
*
* @param parts
* @param seperator
* @return
* Returns a concatenated string containing the output of every Object's
* toString() method, each separated by the given separator character.
*/
public static String combine(Object[] parts, char seperator) {
public static String combine(Object[] parts, char separator) {
if (parts == null) {
return null;
}
@ -125,7 +120,7 @@ public class Utility {
for (int i = 0; i < parts.length; i++) {
sb.append(parts[i].toString());
if (i < parts.length - 1) {
sb.append(seperator);
sb.append(separator);
}
}
return sb.toString();
@ -178,8 +173,6 @@ public class Utility {
* "sa"mp"le" -> "sa"mp"le"
* (empty string) -> ""
* " -> ""
* @param s
* @return
*/
public static String quoteString(String s) {
if (s == null) {
@ -389,7 +382,7 @@ public class Utility {
/**
* Generate a time in milliseconds from a date string that represents a date/time in GMT
* @param DateTime date string in format 20090211T180303Z (rfc2445, iCalendar).
* @param date string in format 20090211T180303Z (rfc2445, iCalendar).
* @return the time in milliseconds (since Jan 1, 1970)
*/
public static long parseDateTimeToMillis(String date) {
@ -399,7 +392,7 @@ public class Utility {
/**
* Generate a GregorianCalendar from a date string that represents a date/time in GMT
* @param DateTime date string in format 20090211T180303Z (rfc2445, iCalendar).
* @param date string in format 20090211T180303Z (rfc2445, iCalendar).
* @return the GregorianCalendar
*/
public static GregorianCalendar parseDateTimeToCalendar(String date) {
@ -413,7 +406,7 @@ public class Utility {
/**
* Generate a time in milliseconds from an email date string that represents a date/time in GMT
* @param Email style DateTime string in format 2010-02-23T16:00:00.000Z (ISO 8601, rfc3339)
* @param date string in format 2010-02-23T16:00:00.000Z (ISO 8601, rfc3339)
* @return the time in milliseconds (since Jan 1, 1970)
*/
public static long parseEmailDateTimeToMillis(String date) {
@ -1120,10 +1113,19 @@ public class Utility {
throws URISyntaxException {
URI uri = new URI(uriString);
String path = uri.getPath();
String domain = null;
if (path != null && path.length() > 0) {
auth.mDomain = path.substring(1);
domain = path.substring(1);
}
auth.mDomain = domain;
auth.setLogin(uri.getUserInfo());
auth.setConnection(uri.getScheme(), uri.getHost(), uri.getPort());
}
/**
* Test that the given strings are equal in a null-pointer safe fashion.
*/
public static boolean areStringsEqual(String s1, String s2) {
return (s1 != null && s1.equals(s2)) || (s1 == null && s2 == null);
}
}

View File

@ -190,7 +190,7 @@ public class AccountSetupExchangeTests extends
Account account =
ProviderTestUtils.setupAccount("account", false, mActivity.getBaseContext());
account.mHostAuthRecv = ProviderTestUtils.setupHostAuth(
"eas", "hostauth", 1, false, mActivity.getBaseContext());
"eas", "hostauth", false, mActivity.getBaseContext());
account.mHostAuthRecv.mFlags |= HostAuth.FLAG_SSL;
account.mHostAuthRecv.mFlags &= ~HostAuth.FLAG_TRUST_ALL;
mActivity.mFragment.mLoaded = false;

View File

@ -36,8 +36,8 @@ public class MailboxAccountLoaderTestCase extends LoaderTestCase {
private long createAccount(boolean isEas) {
Account acct = ProviderTestUtils.setupAccount("acct1", false, mProviderContext);
String proto = isEas ? "eas" : "non-eas";
acct.mHostAuthRecv = ProviderTestUtils.setupHostAuth(proto, "hostauth", -1, true,
mProviderContext);
acct.mHostAuthRecv =
ProviderTestUtils.setupHostAuth(proto, "hostauth", true, mProviderContext);
acct.mHostAuthKeyRecv = acct.mHostAuthRecv.mId;
acct.save(mProviderContext);
return acct.mId;

View File

@ -86,14 +86,14 @@ public class ProviderTestUtils extends Assert {
*/
public static HostAuth setupHostAuth(String name, long accountId, boolean saveIt,
Context context) {
return setupHostAuth("protocol", name, accountId, saveIt, context);
return setupHostAuth("protocol", name, saveIt, context);
}
/**
* Create a hostauth record for test purposes
*/
public static HostAuth setupHostAuth(String protocol, String name, long accountId,
boolean saveIt, Context context) {
public static HostAuth setupHostAuth(String protocol, String name, boolean saveIt,
Context context) {
HostAuth hostAuth = new HostAuth();
hostAuth.mProtocol = protocol;
@ -103,7 +103,6 @@ public class ProviderTestUtils extends Assert {
hostAuth.mLogin = "login-" + name;
hostAuth.mPassword = "password-" + name;
hostAuth.mDomain = "domain-" + name;
hostAuth.mAccountKey = accountId;
if (saveIt) {
hostAuth.save(context);

View File

@ -255,11 +255,11 @@ public class ProviderTests extends ProviderTestCase2<EmailProvider> {
public void testGetProtocol() {
Account account1 = ProviderTestUtils.setupAccount("account-hostauth", false, mMockContext);
// add hostauth data, with protocol
account1.mHostAuthRecv = ProviderTestUtils.setupHostAuth("eas", "account-hostauth-recv", -1,
account1.mHostAuthRecv = ProviderTestUtils.setupHostAuth("eas", "account-hostauth-recv",
false, mMockContext);
// Note that getProtocol uses the receive host auth, so the protocol here shouldn't matter
// to the test result
account1.mHostAuthSend = ProviderTestUtils.setupHostAuth("foo", "account-hostauth-send", -1,
account1.mHostAuthSend = ProviderTestUtils.setupHostAuth("foo", "account-hostauth-send",
false, mMockContext);
account1.save(mMockContext);
assertEquals("eas", Account.getProtocol(mMockContext, account1.mId));
@ -313,423 +313,6 @@ public class ProviderTests extends ProviderTestCase2<EmailProvider> {
return Integer.valueOf(text);
}
/**
* TODO: HostAuth tests
*/
/**
* Test the various combinations of SSL, TLS, and trust-certificates encoded as Uris
*/
public void testHostAuthSecurityUri()
throws URISyntaxException {
HostAuth ha = ProviderTestUtils.setupHostAuth("uri-security", 1, false, mMockContext);
final int MASK =
HostAuth.FLAG_SSL | HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL;
// Set various URIs and check the resulting flags
Utility.setHostAuthFromString(ha, "protocol://user:password@server:123");
assertEquals(0, ha.mFlags & MASK);
Utility.setHostAuthFromString(ha, "protocol+ssl+://user:password@server:123");
assertEquals(HostAuth.FLAG_SSL, ha.mFlags & MASK);
Utility.setHostAuthFromString(ha, "protocol+ssl+trustallcerts://user:password@server:123");
assertEquals(HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL, ha.mFlags & MASK);
Utility.setHostAuthFromString(ha, "protocol+tls+://user:password@server:123");
assertEquals(HostAuth.FLAG_TLS, ha.mFlags & MASK);
Utility.setHostAuthFromString(ha, "protocol+tls+trustallcerts://user:password@server:123");
assertEquals(HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL, ha.mFlags & MASK);
// Now check the retrival method (building URI from flags)
ha.mFlags &= ~MASK;
String uriString = ha.getStoreUri();
assertTrue(uriString.startsWith("protocol://"));
ha.mFlags |= HostAuth.FLAG_SSL;
uriString = ha.getStoreUri();
assertTrue(uriString.startsWith("protocol+ssl+://"));
ha.mFlags |= HostAuth.FLAG_TRUST_ALL;
uriString = ha.getStoreUri();
assertTrue(uriString.startsWith("protocol+ssl+trustallcerts://"));
ha.mFlags &= ~MASK;
ha.mFlags |= HostAuth.FLAG_TLS;
uriString = ha.getStoreUri();
assertTrue(uriString.startsWith("protocol+tls+://"));
ha.mFlags |= HostAuth.FLAG_TRUST_ALL;
uriString = ha.getStoreUri();
assertTrue(uriString.startsWith("protocol+tls+trustallcerts://"));
}
/**
* Test port assignments made from Uris
*/
public void testHostAuthPortAssignments()
throws URISyntaxException {
HostAuth ha = ProviderTestUtils.setupHostAuth("uri-port", 1, false, mMockContext);
// Set various URIs and check the resulting flags
// Hardwired port
Utility.setHostAuthFromString(ha, "imap://user:password@server:123");
assertEquals(123, ha.mPort);
// Auto-assigned ports
Utility.setHostAuthFromString(ha, "imap://user:password@server");
assertEquals(143, ha.mPort);
Utility.setHostAuthFromString(ha, "imap+ssl://user:password@server");
assertEquals(993, ha.mPort);
Utility.setHostAuthFromString(ha, "imap+ssl+trustallcerts://user:password@server");
assertEquals(993, ha.mPort);
Utility.setHostAuthFromString(ha, "imap+tls://user:password@server");
assertEquals(143, ha.mPort);
Utility.setHostAuthFromString(ha, "imap+tls+trustallcerts://user:password@server");
assertEquals(143, ha.mPort);
// Hardwired port
Utility.setHostAuthFromString(ha, "pop3://user:password@server:123");
assertEquals(123, ha.mPort);
// Auto-assigned ports
Utility.setHostAuthFromString(ha, "pop3://user:password@server");
assertEquals(110, ha.mPort);
Utility.setHostAuthFromString(ha, "pop3+ssl://user:password@server");
assertEquals(995, ha.mPort);
Utility.setHostAuthFromString(ha, "pop3+ssl+trustallcerts://user:password@server");
assertEquals(995, ha.mPort);
Utility.setHostAuthFromString(ha, "pop3+tls://user:password@server");
assertEquals(110, ha.mPort);
Utility.setHostAuthFromString(ha, "pop3+tls+trustallcerts://user:password@server");
assertEquals(110, ha.mPort);
// Hardwired port
Utility.setHostAuthFromString(ha, "eas://user:password@server:123");
assertEquals(123, ha.mPort);
// Auto-assigned ports
Utility.setHostAuthFromString(ha, "eas://user:password@server");
assertEquals(80, ha.mPort);
Utility.setHostAuthFromString(ha, "eas+ssl://user:password@server");
assertEquals(443, ha.mPort);
Utility.setHostAuthFromString(ha, "eas+ssl+trustallcerts://user:password@server");
assertEquals(443, ha.mPort);
// Hardwired port
Utility.setHostAuthFromString(ha, "smtp://user:password@server:123");
assertEquals(123, ha.mPort);
// Auto-assigned ports
Utility.setHostAuthFromString(ha, "smtp://user:password@server");
assertEquals(587, ha.mPort);
Utility.setHostAuthFromString(ha, "smtp+ssl://user:password@server");
assertEquals(465, ha.mPort);
Utility.setHostAuthFromString(ha, "smtp+ssl+trustallcerts://user:password@server");
assertEquals(465, ha.mPort);
Utility.setHostAuthFromString(ha, "smtp+tls://user:password@server");
assertEquals(587, ha.mPort);
Utility.setHostAuthFromString(ha, "smtp+tls+trustallcerts://user:password@server");
assertEquals(587, ha.mPort);
}
/**
* Test preservation of username & password in URI
*/
public void testHostAuthUri()
throws URISyntaxException {
HostAuth ha = new HostAuth();
Utility.setHostAuthFromString(ha, "protocol://user:password@server:123");
String getUri = ha.getStoreUri();
assertEquals("protocol://user:password@server:123", getUri);
// Now put spaces in/around username (they are trimmed)
Utility.setHostAuthFromString(ha, "protocol://%20us%20er%20:password@server:123");
getUri = ha.getStoreUri();
assertEquals("protocol://us%20er:password@server:123", getUri);
// Now put spaces around password (should not be trimmed)
Utility.setHostAuthFromString(ha, "protocol://user:%20pass%20word%20@server:123");
getUri = ha.getStoreUri();
assertEquals("protocol://user:%20pass%20word%20@server:123", getUri);
}
/**
* Test user name and password are set correctly
*/
public void testHostAuthSetLogin() {
HostAuth ha = new HostAuth();
ha.setLogin("user:password");
assertEquals("user", ha.mLogin);
assertEquals("password", ha.mPassword);
// special characters are not removed during insertion
ha.setLogin("%20us%20er%20:password");
assertEquals("%20us%20er%20", ha.mLogin);
assertEquals("password", ha.mPassword);
// special characters are not removed during insertion
ha.setLogin("user:%20pass%20word%20");
assertEquals("user", ha.mLogin);
assertEquals("%20pass%20word%20", ha.mPassword);
ha.setLogin("user:");
assertEquals("user", ha.mLogin);
assertEquals("", ha.mPassword);
ha.setLogin(":password");
assertEquals("", ha.mLogin);
assertEquals("password", ha.mPassword);
ha.setLogin("");
assertNull(ha.mLogin);
assertNull(ha.mPassword);
ha.setLogin(null);
assertNull(ha.mLogin);
assertNull(ha.mPassword);
ha.setLogin("userpassword");
assertEquals("userpassword", ha.mLogin);
assertNull(ha.mPassword);
}
/**
* Test the authentication flag is set correctly when setting user name and password
*/
public void testHostAuthSetLoginAuthenticate() {
HostAuth ha = new HostAuth();
ha.mFlags = 0x00000000;
ha.setLogin("user", "password");
assertEquals(HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
ha.mFlags = 0x00000000;
ha.setLogin("user", "");
assertEquals(HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
ha.mFlags = 0x00000000;
ha.setLogin("", "password");
assertEquals(HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
ha.mFlags = 0x00000000;
ha.setLogin("user", null);
assertEquals(HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
ha.mFlags = 0xffffffff;
ha.setLogin(null, "password");
assertEquals(~HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
ha.mFlags = 0xffffffff;
ha.setLogin(null, null);
assertEquals(~HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
}
/**
* Test setting the connection using a URI scheme
*/
public void testHostAuthSetConnectionScheme() {
HostAuth ha = new HostAuth();
// Set URIs for IMAP
// Hardwired port
ha.setConnection("imap", "server", 123);
assertEquals(0, ha.mFlags);
assertEquals(123, ha.mPort);
// Auto-assigned ports
ha.setConnection("imap", "server", -1);
assertEquals(0, ha.mFlags);
assertEquals(143, ha.mPort);
ha.setConnection("imap+ssl", "server", -1);
assertEquals(HostAuth.FLAG_SSL, ha.mFlags);
assertEquals(993, ha.mPort);
ha.setConnection("imap+ssl+trustallcerts", "server", -1);
assertEquals(HostAuth.FLAG_SSL|HostAuth.FLAG_TRUST_ALL, ha.mFlags);
assertEquals(993, ha.mPort);
ha.setConnection("imap+tls", "server", -1);
assertEquals(HostAuth.FLAG_TLS, ha.mFlags);
assertEquals(143, ha.mPort);
ha.setConnection("imap+tls+trustallcerts", "server", -1);
assertEquals(HostAuth.FLAG_TLS|HostAuth.FLAG_TRUST_ALL, ha.mFlags);
assertEquals(143, ha.mPort);
// Set URIs for POP3
// Hardwired port
ha.setConnection("pop3", "server", 123);
assertEquals(0, ha.mFlags);
assertEquals(123, ha.mPort);
// Auto-assigned ports
ha.setConnection("pop3", "server", -1);
assertEquals(0, ha.mFlags);
assertEquals(110, ha.mPort);
ha.setConnection("pop3+ssl", "server", -1);
assertEquals(HostAuth.FLAG_SSL, ha.mFlags);
assertEquals(995, ha.mPort);
ha.setConnection("pop3+ssl+trustallcerts", "server", -1);
assertEquals(HostAuth.FLAG_SSL|HostAuth.FLAG_TRUST_ALL, ha.mFlags);
assertEquals(995, ha.mPort);
ha.setConnection("pop3+tls", "server", -1);
assertEquals(HostAuth.FLAG_TLS, ha.mFlags);
assertEquals(110, ha.mPort);
ha.setConnection("pop3+tls+trustallcerts", "server", -1);
assertEquals(HostAuth.FLAG_TLS|HostAuth.FLAG_TRUST_ALL, ha.mFlags);
assertEquals(110, ha.mPort);
// Set URIs for Exchange
// Hardwired port
ha.setConnection("eas", "server", 123);
assertEquals(0, ha.mFlags);
assertEquals(123, ha.mPort);
// Auto-assigned ports
ha.setConnection("eas", "server", -1);
assertEquals(0, ha.mFlags);
assertEquals(80, ha.mPort);
ha.setConnection("eas+ssl", "server", -1);
assertEquals(HostAuth.FLAG_SSL, ha.mFlags);
assertEquals(443, ha.mPort);
ha.setConnection("eas+ssl+trustallcerts", "server", -1);
assertEquals(HostAuth.FLAG_SSL|HostAuth.FLAG_TRUST_ALL, ha.mFlags);
assertEquals(443, ha.mPort);
// Set URIs for SMTP
// Hardwired port
ha.setConnection("smtp", "server", 123);
assertEquals(0, ha.mFlags);
assertEquals(123, ha.mPort);
// Auto-assigned ports
ha.setConnection("smtp", "server", -1);
assertEquals(0, ha.mFlags);
assertEquals(587, ha.mPort);
ha.setConnection("smtp+ssl", "server", -1);
assertEquals(HostAuth.FLAG_SSL, ha.mFlags);
assertEquals(465, ha.mPort);
ha.setConnection("smtp+ssl+trustallcerts", "server", -1);
assertEquals(HostAuth.FLAG_SSL|HostAuth.FLAG_TRUST_ALL, ha.mFlags);
assertEquals(465, ha.mPort);
ha.setConnection("smtp+tls", "server", -1);
assertEquals(HostAuth.FLAG_TLS, ha.mFlags);
assertEquals(587, ha.mPort);
ha.setConnection("smtp+tls+trustallcerts", "server", -1);
assertEquals(HostAuth.FLAG_TLS|HostAuth.FLAG_TRUST_ALL, ha.mFlags);
assertEquals(587, ha.mPort);
}
/**
* Test setting the connection using a protocol and flags
*/
public void testHostAuthSetConnectionFlags() {
HostAuth ha = new HostAuth();
// Different port types don't affect flags
ha.setConnection("imap", "server", 123, 0);
assertEquals(0, ha.mFlags);
ha.setConnection("imap", "server", -1, 0);
assertEquals(0, ha.mFlags);
// Different protocol types don't affect flags
ha.setConnection("pop3", "server", 123, 0);
assertEquals(0, ha.mFlags);
ha.setConnection("pop3", "server", -1, 0);
assertEquals(0, ha.mFlags);
ha.setConnection("eas", "server", 123, 0);
assertEquals(0, ha.mFlags);
ha.setConnection("eas", "server", -1, 0);
assertEquals(0, ha.mFlags);
ha.setConnection("smtp", "server", 123, 0);
assertEquals(0, ha.mFlags);
ha.setConnection("smtp", "server", -1, 0);
assertEquals(0, ha.mFlags);
// Sets SSL flag
ha.setConnection("imap", "server", -1, HostAuth.FLAG_SSL);
assertEquals(HostAuth.FLAG_SSL, ha.mFlags);
// Sets SSL+Trusted flags
ha.setConnection("imap", "server", -1, HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL);
assertEquals(HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL, ha.mFlags);
// Sets TLS flag
ha.setConnection("imap", "server", -1, HostAuth.FLAG_TLS);
assertEquals(HostAuth.FLAG_TLS, ha.mFlags);
// Sets TLS+Trusted flags
ha.setConnection("imap", "server", -1, HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL);
assertEquals(HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL, ha.mFlags);
// Test other defined flags; should not affect mFlags
ha.setConnection("imap", "server", -1, HostAuth.FLAG_AUTHENTICATE);
assertEquals(0, ha.mFlags);
// Test every other bit; should not affect mFlags
ha.setConnection("imap", "server", -1, 0xfffffff4);
assertEquals(0, ha.mFlags);
}
public void testHostAuthGetSchemeString() {
String scheme;
scheme = HostAuth.getSchemeString("foo", 0);
assertEquals("foo", scheme);
scheme = HostAuth.getSchemeString("foo", HostAuth.FLAG_SSL);
assertEquals("foo+ssl+", scheme);
scheme = HostAuth.getSchemeString("foo", HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL);
assertEquals("foo+ssl+trustallcerts", scheme);
scheme = HostAuth.getSchemeString("foo", HostAuth.FLAG_TLS);
assertEquals("foo+tls+", scheme);
scheme = HostAuth.getSchemeString("foo", HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL);
assertEquals("foo+tls+trustallcerts", scheme);
// error cases; no security string appended to protocol
scheme = HostAuth.getSchemeString("foo", HostAuth.FLAG_TRUST_ALL);
assertEquals("foo", scheme);
scheme = HostAuth.getSchemeString("foo", HostAuth.FLAG_SSL | HostAuth.FLAG_TLS);
assertEquals("foo", scheme);
scheme = HostAuth.getSchemeString("foo", HostAuth.FLAG_SSL | HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL);
assertEquals("foo", scheme);
scheme = HostAuth.getSchemeString("foo", 0xfffffff4);
assertEquals("foo", scheme);
}
public void testHostAuthGetSchemeFlags() {
int flags;
flags = HostAuth.getSchemeFlags("");
assertEquals(0, flags);
flags = HostAuth.getSchemeFlags("+");
assertEquals(0, flags);
flags = HostAuth.getSchemeFlags("foo+");
assertEquals(0, flags);
flags = HostAuth.getSchemeFlags("foo+ssl");
assertEquals(HostAuth.FLAG_SSL, flags);
flags = HostAuth.getSchemeFlags("foo+ssl+");
assertEquals(HostAuth.FLAG_SSL, flags);
flags = HostAuth.getSchemeFlags("foo+ssl+trustallcerts");
assertEquals(HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL, flags);
flags = HostAuth.getSchemeFlags("foo+tls+");
assertEquals(HostAuth.FLAG_TLS, flags);
flags = HostAuth.getSchemeFlags("foo+tls+trustallcerts");
assertEquals(HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL, flags);
flags = HostAuth.getSchemeFlags("foo+bogus");
assertEquals(0, flags);
flags = HostAuth.getSchemeFlags("foo+bogus+trustallcerts");
assertEquals(HostAuth.FLAG_TRUST_ALL, flags);
flags = HostAuth.getSchemeFlags("foo+ssl+bogus");
assertEquals(HostAuth.FLAG_SSL, flags);
flags = HostAuth.getSchemeFlags("foo+ssl+trustallcerts+bogus");
assertEquals(HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL, flags);
flags = HostAuth.getSchemeFlags("foo+bogus+bogus");
assertEquals(0, flags);
flags = HostAuth.getSchemeFlags("foo+bogus+bogus+bogus");
assertEquals(0, flags);
}
/**
* Test simple mailbox save/retrieve
*/
@ -2475,7 +2058,7 @@ public class ProviderTests extends ProviderTestCase2<EmailProvider> {
private void checkAccountIsEasAccount(String protocol, boolean expected) {
Account account = ProviderTestUtils.setupAccount("account", false, mMockContext);
account.mHostAuthRecv = ProviderTestUtils.setupHostAuth(protocol, "account-hostauth-recv",
account.mId, false, mMockContext);
false, mMockContext);
account.save(mMockContext);
assertEquals(expected, account.isEasAccount(mMockContext));
}

View File

@ -0,0 +1,518 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.emailcommon.provider;
import com.android.email.provider.ProviderTestUtils;
import com.android.emailcommon.provider.EmailContent.HostAuth;
import com.android.emailcommon.utility.Utility;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import java.net.URISyntaxException;
/**
* Unit tests for the HostAuth inner class.
* These tests must be locally complete - no server(s) required.
*/
@SmallTest
public class HostAuthTests extends AndroidTestCase {
/**
* Test the various combinations of SSL, TLS, and trust-certificates encoded as Uris
*/
public void testSecurityUri()
throws URISyntaxException {
HostAuth ha = ProviderTestUtils.setupHostAuth("uri-security", 1, false, mContext);
final int MASK =
HostAuth.FLAG_SSL | HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL;
// Set various URIs and check the resulting flags
Utility.setHostAuthFromString(ha, "protocol://user:password@server:123");
assertEquals(0, ha.mFlags & MASK);
Utility.setHostAuthFromString(ha, "protocol+ssl+://user:password@server:123");
assertEquals(HostAuth.FLAG_SSL, ha.mFlags & MASK);
Utility.setHostAuthFromString(ha, "protocol+ssl+trustallcerts://user:password@server:123");
assertEquals(HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL, ha.mFlags & MASK);
Utility.setHostAuthFromString(ha, "protocol+tls+://user:password@server:123");
assertEquals(HostAuth.FLAG_TLS, ha.mFlags & MASK);
Utility.setHostAuthFromString(ha, "protocol+tls+trustallcerts://user:password@server:123");
assertEquals(HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL, ha.mFlags & MASK);
// Now check the retrival method (building URI from flags)
ha.mFlags &= ~MASK;
String uriString = ha.getStoreUri();
assertTrue(uriString.startsWith("protocol://"));
ha.mFlags |= HostAuth.FLAG_SSL;
uriString = ha.getStoreUri();
assertTrue(uriString.startsWith("protocol+ssl+://"));
ha.mFlags |= HostAuth.FLAG_TRUST_ALL;
uriString = ha.getStoreUri();
assertTrue(uriString.startsWith("protocol+ssl+trustallcerts://"));
ha.mFlags &= ~MASK;
ha.mFlags |= HostAuth.FLAG_TLS;
uriString = ha.getStoreUri();
assertTrue(uriString.startsWith("protocol+tls+://"));
ha.mFlags |= HostAuth.FLAG_TRUST_ALL;
uriString = ha.getStoreUri();
assertTrue(uriString.startsWith("protocol+tls+trustallcerts://"));
}
/**
* Test port assignments made from Uris
*/
public void testPortAssignments()
throws URISyntaxException {
HostAuth ha = ProviderTestUtils.setupHostAuth("uri-port", 1, false, mContext);
// Set various URIs and check the resulting flags
// Hardwired port
Utility.setHostAuthFromString(ha, "imap://user:password@server:123");
assertEquals(123, ha.mPort);
// Auto-assigned ports
Utility.setHostAuthFromString(ha, "imap://user:password@server");
assertEquals(143, ha.mPort);
Utility.setHostAuthFromString(ha, "imap+ssl://user:password@server");
assertEquals(993, ha.mPort);
Utility.setHostAuthFromString(ha, "imap+ssl+trustallcerts://user:password@server");
assertEquals(993, ha.mPort);
Utility.setHostAuthFromString(ha, "imap+tls://user:password@server");
assertEquals(143, ha.mPort);
Utility.setHostAuthFromString(ha, "imap+tls+trustallcerts://user:password@server");
assertEquals(143, ha.mPort);
// Hardwired port
Utility.setHostAuthFromString(ha, "pop3://user:password@server:123");
assertEquals(123, ha.mPort);
// Auto-assigned ports
Utility.setHostAuthFromString(ha, "pop3://user:password@server");
assertEquals(110, ha.mPort);
Utility.setHostAuthFromString(ha, "pop3+ssl://user:password@server");
assertEquals(995, ha.mPort);
Utility.setHostAuthFromString(ha, "pop3+ssl+trustallcerts://user:password@server");
assertEquals(995, ha.mPort);
Utility.setHostAuthFromString(ha, "pop3+tls://user:password@server");
assertEquals(110, ha.mPort);
Utility.setHostAuthFromString(ha, "pop3+tls+trustallcerts://user:password@server");
assertEquals(110, ha.mPort);
// Hardwired port
Utility.setHostAuthFromString(ha, "eas://user:password@server:123");
assertEquals(123, ha.mPort);
// Auto-assigned ports
Utility.setHostAuthFromString(ha, "eas://user:password@server");
assertEquals(80, ha.mPort);
Utility.setHostAuthFromString(ha, "eas+ssl://user:password@server");
assertEquals(443, ha.mPort);
Utility.setHostAuthFromString(ha, "eas+ssl+trustallcerts://user:password@server");
assertEquals(443, ha.mPort);
// Hardwired port
Utility.setHostAuthFromString(ha, "smtp://user:password@server:123");
assertEquals(123, ha.mPort);
// Auto-assigned ports
Utility.setHostAuthFromString(ha, "smtp://user:password@server");
assertEquals(587, ha.mPort);
Utility.setHostAuthFromString(ha, "smtp+ssl://user:password@server");
assertEquals(465, ha.mPort);
Utility.setHostAuthFromString(ha, "smtp+ssl+trustallcerts://user:password@server");
assertEquals(465, ha.mPort);
Utility.setHostAuthFromString(ha, "smtp+tls://user:password@server");
assertEquals(587, ha.mPort);
Utility.setHostAuthFromString(ha, "smtp+tls+trustallcerts://user:password@server");
assertEquals(587, ha.mPort);
}
/**
* Test preservation of username & password in URI
*/
public void testGetStoreUri()
throws URISyntaxException {
HostAuth ha = new HostAuth();
Utility.setHostAuthFromString(ha, "protocol://user:password@server:123");
String getUri = ha.getStoreUri();
assertEquals("protocol://user:password@server:123", getUri);
// Now put spaces in/around username (they are trimmed)
Utility.setHostAuthFromString(ha, "protocol://%20us%20er%20:password@server:123");
getUri = ha.getStoreUri();
assertEquals("protocol://us%20er:password@server:123", getUri);
// Now put spaces around password (should not be trimmed)
Utility.setHostAuthFromString(ha, "protocol://user:%20pass%20word%20@server:123");
getUri = ha.getStoreUri();
assertEquals("protocol://user:%20pass%20word%20@server:123", getUri);
}
/**
* Test user name and password are set correctly
*/
public void testSetLogin() {
HostAuth ha = new HostAuth();
ha.setLogin("user:password");
assertEquals("user", ha.mLogin);
assertEquals("password", ha.mPassword);
// special characters are not removed during insertion
ha.setLogin("%20us%20er%20:password");
assertEquals("%20us%20er%20", ha.mLogin);
assertEquals("password", ha.mPassword);
// special characters are not removed during insertion
ha.setLogin("user:%20pass%20word%20");
assertEquals("user", ha.mLogin);
assertEquals("%20pass%20word%20", ha.mPassword);
ha.setLogin("user:");
assertEquals("user", ha.mLogin);
assertEquals("", ha.mPassword);
ha.setLogin(":password");
assertEquals("", ha.mLogin);
assertEquals("password", ha.mPassword);
ha.setLogin("");
assertNull(ha.mLogin);
assertNull(ha.mPassword);
ha.setLogin(null);
assertNull(ha.mLogin);
assertNull(ha.mPassword);
ha.setLogin("userpassword");
assertEquals("userpassword", ha.mLogin);
assertNull(ha.mPassword);
}
/**
* Test the authentication flag is set correctly when setting user name and password
*/
public void testSetLoginAuthenticate() {
HostAuth ha = new HostAuth();
ha.mFlags = 0x00000000;
ha.setLogin("user", "password");
assertEquals(HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
ha.mFlags = 0x00000000;
ha.setLogin("user", "");
assertEquals(HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
ha.mFlags = 0x00000000;
ha.setLogin("", "password");
assertEquals(HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
ha.mFlags = 0x00000000;
ha.setLogin("user", null);
assertEquals(HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
ha.mFlags = 0xffffffff;
ha.setLogin(null, "password");
assertEquals(~HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
ha.mFlags = 0xffffffff;
ha.setLogin(null, null);
assertEquals(~HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
}
/**
* Test setting the connection using a URI scheme
*/
public void testSetConnectionScheme() {
HostAuth ha = new HostAuth();
// Set URIs for IMAP
// Hardwired port
ha.setConnection("imap", "server", 123);
assertEquals(0, ha.mFlags);
assertEquals(123, ha.mPort);
// Auto-assigned ports
ha.setConnection("imap", "server", -1);
assertEquals(0, ha.mFlags);
assertEquals(143, ha.mPort);
ha.setConnection("imap+ssl", "server", -1);
assertEquals(HostAuth.FLAG_SSL, ha.mFlags);
assertEquals(993, ha.mPort);
ha.setConnection("imap+ssl+trustallcerts", "server", -1);
assertEquals(HostAuth.FLAG_SSL|HostAuth.FLAG_TRUST_ALL, ha.mFlags);
assertEquals(993, ha.mPort);
ha.setConnection("imap+tls", "server", -1);
assertEquals(HostAuth.FLAG_TLS, ha.mFlags);
assertEquals(143, ha.mPort);
ha.setConnection("imap+tls+trustallcerts", "server", -1);
assertEquals(HostAuth.FLAG_TLS|HostAuth.FLAG_TRUST_ALL, ha.mFlags);
assertEquals(143, ha.mPort);
// Set URIs for POP3
// Hardwired port
ha.setConnection("pop3", "server", 123);
assertEquals(0, ha.mFlags);
assertEquals(123, ha.mPort);
// Auto-assigned ports
ha.setConnection("pop3", "server", -1);
assertEquals(0, ha.mFlags);
assertEquals(110, ha.mPort);
ha.setConnection("pop3+ssl", "server", -1);
assertEquals(HostAuth.FLAG_SSL, ha.mFlags);
assertEquals(995, ha.mPort);
ha.setConnection("pop3+ssl+trustallcerts", "server", -1);
assertEquals(HostAuth.FLAG_SSL|HostAuth.FLAG_TRUST_ALL, ha.mFlags);
assertEquals(995, ha.mPort);
ha.setConnection("pop3+tls", "server", -1);
assertEquals(HostAuth.FLAG_TLS, ha.mFlags);
assertEquals(110, ha.mPort);
ha.setConnection("pop3+tls+trustallcerts", "server", -1);
assertEquals(HostAuth.FLAG_TLS|HostAuth.FLAG_TRUST_ALL, ha.mFlags);
assertEquals(110, ha.mPort);
// Set URIs for Exchange
// Hardwired port
ha.setConnection("eas", "server", 123);
assertEquals(0, ha.mFlags);
assertEquals(123, ha.mPort);
// Auto-assigned ports
ha.setConnection("eas", "server", -1);
assertEquals(0, ha.mFlags);
assertEquals(80, ha.mPort);
ha.setConnection("eas+ssl", "server", -1);
assertEquals(HostAuth.FLAG_SSL, ha.mFlags);
assertEquals(443, ha.mPort);
ha.setConnection("eas+ssl+trustallcerts", "server", -1);
assertEquals(HostAuth.FLAG_SSL|HostAuth.FLAG_TRUST_ALL, ha.mFlags);
assertEquals(443, ha.mPort);
// Set URIs for SMTP
// Hardwired port
ha.setConnection("smtp", "server", 123);
assertEquals(0, ha.mFlags);
assertEquals(123, ha.mPort);
// Auto-assigned ports
ha.setConnection("smtp", "server", -1);
assertEquals(0, ha.mFlags);
assertEquals(587, ha.mPort);
ha.setConnection("smtp+ssl", "server", -1);
assertEquals(HostAuth.FLAG_SSL, ha.mFlags);
assertEquals(465, ha.mPort);
ha.setConnection("smtp+ssl+trustallcerts", "server", -1);
assertEquals(HostAuth.FLAG_SSL|HostAuth.FLAG_TRUST_ALL, ha.mFlags);
assertEquals(465, ha.mPort);
ha.setConnection("smtp+tls", "server", -1);
assertEquals(HostAuth.FLAG_TLS, ha.mFlags);
assertEquals(587, ha.mPort);
ha.setConnection("smtp+tls+trustallcerts", "server", -1);
assertEquals(HostAuth.FLAG_TLS|HostAuth.FLAG_TRUST_ALL, ha.mFlags);
assertEquals(587, ha.mPort);
}
/**
* Test setting the connection using a protocol and flags
*/
public void testSetConnectionFlags() {
HostAuth ha = new HostAuth();
// Different port types don't affect flags
ha.setConnection("imap", "server", 123, 0);
assertEquals(0, ha.mFlags);
ha.setConnection("imap", "server", -1, 0);
assertEquals(0, ha.mFlags);
// Different protocol types don't affect flags
ha.setConnection("pop3", "server", 123, 0);
assertEquals(0, ha.mFlags);
ha.setConnection("pop3", "server", -1, 0);
assertEquals(0, ha.mFlags);
ha.setConnection("eas", "server", 123, 0);
assertEquals(0, ha.mFlags);
ha.setConnection("eas", "server", -1, 0);
assertEquals(0, ha.mFlags);
ha.setConnection("smtp", "server", 123, 0);
assertEquals(0, ha.mFlags);
ha.setConnection("smtp", "server", -1, 0);
assertEquals(0, ha.mFlags);
// Sets SSL flag
ha.setConnection("imap", "server", -1, HostAuth.FLAG_SSL);
assertEquals(HostAuth.FLAG_SSL, ha.mFlags);
// Sets SSL+Trusted flags
ha.setConnection("imap", "server", -1, HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL);
assertEquals(HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL, ha.mFlags);
// Sets TLS flag
ha.setConnection("imap", "server", -1, HostAuth.FLAG_TLS);
assertEquals(HostAuth.FLAG_TLS, ha.mFlags);
// Sets TLS+Trusted flags
ha.setConnection("imap", "server", -1, HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL);
assertEquals(HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL, ha.mFlags);
// Test other defined flags; should not affect mFlags
ha.setConnection("imap", "server", -1, HostAuth.FLAG_AUTHENTICATE);
assertEquals(0, ha.mFlags);
// Test every other bit; should not affect mFlags
ha.setConnection("imap", "server", -1, 0xfffffff4);
assertEquals(0, ha.mFlags);
}
public void testGetSchemeString() {
String scheme;
scheme = HostAuth.getSchemeString("foo", 0);
assertEquals("foo", scheme);
scheme = HostAuth.getSchemeString("foo", HostAuth.FLAG_SSL);
assertEquals("foo+ssl+", scheme);
scheme = HostAuth.getSchemeString("foo", HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL);
assertEquals("foo+ssl+trustallcerts", scheme);
scheme = HostAuth.getSchemeString("foo", HostAuth.FLAG_TLS);
assertEquals("foo+tls+", scheme);
scheme = HostAuth.getSchemeString("foo", HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL);
assertEquals("foo+tls+trustallcerts", scheme);
// error cases; no security string appended to protocol
scheme = HostAuth.getSchemeString("foo", HostAuth.FLAG_TRUST_ALL);
assertEquals("foo", scheme);
scheme = HostAuth.getSchemeString("foo", HostAuth.FLAG_SSL | HostAuth.FLAG_TLS);
assertEquals("foo", scheme);
scheme = HostAuth.getSchemeString("foo",
HostAuth.FLAG_SSL | HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL);
assertEquals("foo", scheme);
scheme = HostAuth.getSchemeString("foo", 0xfffffff4);
assertEquals("foo", scheme);
}
public void testGetSchemeFlags() {
int flags;
flags = HostAuth.getSchemeFlags("");
assertEquals(0, flags);
flags = HostAuth.getSchemeFlags("+");
assertEquals(0, flags);
flags = HostAuth.getSchemeFlags("foo+");
assertEquals(0, flags);
flags = HostAuth.getSchemeFlags("foo+ssl");
assertEquals(HostAuth.FLAG_SSL, flags);
flags = HostAuth.getSchemeFlags("foo+ssl+");
assertEquals(HostAuth.FLAG_SSL, flags);
flags = HostAuth.getSchemeFlags("foo+ssl+trustallcerts");
assertEquals(HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL, flags);
flags = HostAuth.getSchemeFlags("foo+tls+");
assertEquals(HostAuth.FLAG_TLS, flags);
flags = HostAuth.getSchemeFlags("foo+tls+trustallcerts");
assertEquals(HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL, flags);
flags = HostAuth.getSchemeFlags("foo+bogus");
assertEquals(0, flags);
flags = HostAuth.getSchemeFlags("foo+bogus+trustallcerts");
assertEquals(HostAuth.FLAG_TRUST_ALL, flags);
flags = HostAuth.getSchemeFlags("foo+ssl+bogus");
assertEquals(HostAuth.FLAG_SSL, flags);
flags = HostAuth.getSchemeFlags("foo+ssl+trustallcerts+bogus");
assertEquals(HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL, flags);
flags = HostAuth.getSchemeFlags("foo+bogus+bogus");
assertEquals(0, flags);
flags = HostAuth.getSchemeFlags("foo+bogus+bogus+bogus");
assertEquals(0, flags);
}
public void testEquals() throws URISyntaxException {
HostAuth ha1;
HostAuth ha2;
ha1 = new HostAuth();
ha2 = new HostAuth();
assertTrue(ha1.equals(ha2));
assertTrue(ha2.equals(ha1));
Utility.setHostAuthFromString(ha1, "smtp+tls+://user:password@server/domain");
Utility.setHostAuthFromString(ha2, "smtp+tls+://user:password@server/domain");
assertTrue(ha1.equals(ha2));
assertTrue(ha2.equals(ha1));
// Different protocol
Utility.setHostAuthFromString(ha2, "imap+tls+://user:password@server/domain");
assertFalse(ha1.equals(ha2));
assertFalse(ha2.equals(ha1));
// Different domain
Utility.setHostAuthFromString(ha2, "smtp+tls+://user:password@server/domain2");
assertFalse(ha1.equals(ha2));
assertFalse(ha2.equals(ha1));
// Missing server
Utility.setHostAuthFromString(ha2, "smtp+tls+://user:password/domain");
assertFalse(ha1.equals(ha2));
assertFalse(ha2.equals(ha1));
// Missing domain
Utility.setHostAuthFromString(ha2, "smtp+tls+://user:password@server");
assertFalse(ha1.equals(ha2));
assertFalse(ha2.equals(ha1));
// Different server
Utility.setHostAuthFromString(ha2, "smtp+tls+://user:password@server2/domain");
assertFalse(ha1.equals(ha2));
assertFalse(ha2.equals(ha1));
// Different password
Utility.setHostAuthFromString(ha2, "smtp+tls+://user:password2@server/domain");
assertFalse(ha1.equals(ha2));
assertFalse(ha2.equals(ha1));
// Different user name
Utility.setHostAuthFromString(ha2, "smtp+tls+://user2:password@server/domain");
assertFalse(ha1.equals(ha2));
assertFalse(ha2.equals(ha1));
// Missing password
Utility.setHostAuthFromString(ha2, "smtp+tls+://user@server/domain");
assertFalse(ha1.equals(ha2));
assertFalse(ha2.equals(ha1));
// Missing user name
Utility.setHostAuthFromString(ha2, "smtp+tls+://password@server/domain");
assertFalse(ha1.equals(ha2));
assertFalse(ha2.equals(ha1));
// Missing user name & password
Utility.setHostAuthFromString(ha2, "smtp+tls+://server/domain");
assertFalse(ha1.equals(ha2));
assertFalse(ha2.equals(ha1));
// Added "trustallcerts"
Utility.setHostAuthFromString(ha2, "smtp+tls+trustallcerts://user:password@server/domain");
assertFalse(ha1.equals(ha2));
assertFalse(ha2.equals(ha1));
// Different authentication
Utility.setHostAuthFromString(ha2, "smtp+ssl+://user:password@server/domain");
assertFalse(ha1.equals(ha2));
assertFalse(ha2.equals(ha1));
// Missing authentication
Utility.setHostAuthFromString(ha2, "smtp+://user:password@server/domain");
assertFalse(ha1.equals(ha2));
assertFalse(ha2.equals(ha1));
ha2 = null;
assertFalse(ha1.equals(ha2));
}
}

View File

@ -502,6 +502,32 @@ public class UtilityUnitTests extends AndroidTestCase {
assertEquals(moreThan999, UiUtilities.getMessageCountForUi(c, 1001, false));
}
public void testAreStringsEqual() {
String s1;
String s2;
s1 = new String("Foo");
s2 = s1;
assertTrue(Utility.areStringsEqual(s1, s2));
s2 = new String("Foo");
assertTrue(Utility.areStringsEqual(s1, s2));
s2 = "Bar";
assertFalse(Utility.areStringsEqual(s1, s2));
s2 = null;
assertFalse(Utility.areStringsEqual(s1, s2));
s1 = null;
s2 = "Bar";
assertFalse(Utility.areStringsEqual(s1, s2));
s1 = null;
s2 = null;
assertTrue(Utility.areStringsEqual(s1, s2));
}
/**
* A {@link ListView} used by {@link #testListStateSaver}.
*/
@ -543,6 +569,7 @@ public class UtilityUnitTests extends AndroidTestCase {
mCustomData = out.readInt();
}
@SuppressWarnings({"hiding", "unused"})
public static final Parcelable.Creator<SavedState> CREATOR
= new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {