DO NOT MERGE: Test for interaction between ImapStore and vendor policy.

Backport of I092b3a0f2f40d9aa19f2f61066362099c8b3f50b

Change-Id: I492f975e66d67aae62024804a294e796f23d2aad
This commit is contained in:
Makoto Onuki 2010-06-14 09:55:41 -07:00
parent 618f1d8ac5
commit 8aa79ba695
3 changed files with 62 additions and 12 deletions

View File

@ -57,11 +57,9 @@ import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@ -237,8 +235,8 @@ public class ImapStore extends Store {
* @param capability the capabilities string from the server
* @return a String for use in an IMAP ID message.
*/
public static String getImapId(Context context, String userName, String host,
String capability) {
/* package */ static String getImapId(Context context, String userName, String host,
ImapResponse capabilityResponse) {
// The first section is global to all IMAP connections, and generates the fixed
// values in any IMAP ID message
synchronized (ImapStore.class) {
@ -259,8 +257,8 @@ public class ImapStore extends Store {
StringBuilder id = new StringBuilder(sImapId);
// Optionally add any vendor-supplied id keys
String vendorId =
VendorPolicyLoader.getInstance(context).getImapIdValues(userName, host, capability);
String vendorId = VendorPolicyLoader.getInstance(context).getImapIdValues(userName, host,
capabilityResponse.flatten());
if (vendorId != null) {
id.append(' ');
id.append(vendorId);
@ -1431,7 +1429,7 @@ public class ImapStore extends Store {
// Assign user-agent string (for RFC2971 ID command)
String mUserAgent = getImapId(mContext, mUsername, mRootTransport.getHost(),
capabilityResponse.flatten());
capabilityResponse);
if (mUserAgent != null) {
mIdPhrase = ImapConstants.ID + " (" + mUserAgent + ")";
} else if (DEBUG_FORCE_SEND_ID) {

View File

@ -17,7 +17,9 @@
package com.android.email.mail.store;
import com.android.email.Email;
import com.android.email.MockVendorPolicy;
import com.android.email.Utility;
import com.android.email.VendorPolicyLoader;
import com.android.email.mail.Address;
import com.android.email.mail.AuthenticationFailedException;
import com.android.email.mail.Body;
@ -37,10 +39,13 @@ import com.android.email.mail.internet.MimeUtility;
import com.android.email.mail.internet.TextBody;
import com.android.email.mail.store.ImapStore.ImapConnection;
import com.android.email.mail.store.ImapStore.ImapMessage;
import com.android.email.mail.store.imap.ImapResponse;
import com.android.email.mail.store.imap.ImapTestUtils;
import com.android.email.mail.transport.MockTransport;
import org.apache.commons.io.IOUtils;
import android.os.Bundle;
import android.test.AndroidTestCase;
import android.test.MoreAsserts;
import android.test.suitebuilder.annotation.SmallTest;
@ -74,6 +79,9 @@ public class ImapStoreUnitTests extends AndroidTestCase {
*/
private final static String FOLDER_ENCODED = "&ZeU-";
private static ImapResponse CAPABILITY_RESPONSE = ImapTestUtils.parseResponse(
"* CAPABILITY IMAP4rev1 STARTTLS");
/* These values are provided by setUp() */
private ImapStore mStore = null;
private ImapStore.ImapFolder mFolder = null;
@ -153,8 +161,8 @@ public class ImapStoreUnitTests extends AndroidTestCase {
// x-android-device-model Model (Optional, so not tested here)
// x-android-net-operator Carrier (Unreliable, so not tested here)
// AGUID A device+account UID
String id = ImapStore.getImapId(getContext(),
"user-name", "host-name", "IMAP4rev1 STARTTLS");
String id = ImapStore.getImapId(getContext(), "user-name", "host-name",
CAPABILITY_RESPONSE);
HashMap<String, String> map = tokenizeImapId(id);
assertEquals(getContext().getPackageName(), map.get("name"));
assertEquals("android", map.get("os"));
@ -191,6 +199,36 @@ public class ImapStoreUnitTests extends AndroidTestCase {
assertEquals(null, map.get("AGUID"));
}
/**
* Test for the interaction between {@link ImapStore#getImapId} and a vendor policy.
*/
public void testImapIdWithVendorPolicy() {
try {
MockVendorPolicy.inject(getContext());
// Prepare mock result
Bundle result = new Bundle();
result.putString("getImapId", "\"test-key\" \"test-value\"");
MockVendorPolicy.mockResult = result;
// Invoke
String id = ImapStore.getImapId(getContext(), "user-name", "host-name",
ImapTestUtils.parseResponse("* CAPABILITY IMAP4rev1 XXX YYY Z"));
// Check the result
assertEquals("test-value", tokenizeImapId(id).get("test-key"));
// Verify what's passed to the policy
assertEquals("getImapId", MockVendorPolicy.passedPolicy);
assertEquals("user-name", MockVendorPolicy.passedBundle.getString("getImapId.user"));
assertEquals("host-name", MockVendorPolicy.passedBundle.getString("getImapId.host"));
assertEquals("[CAPABILITY,IMAP4rev1,XXX,YYY,Z]",
MockVendorPolicy.passedBundle.getString("getImapId.capabilities"));
} finally {
VendorPolicyLoader.clearInstanceForTest();
}
}
/**
* Test of the internal generator for IMAP ID strings, specifically looking for proper
* filtering of illegal values. This is required because we cannot necessarily trust
@ -225,9 +263,9 @@ public class ImapStoreUnitTests extends AndroidTestCase {
ImapStore store2 = (ImapStore) ImapStore.newInstance("imap://user2:password@server:999",
getContext(), null);
String id1a = ImapStore.getImapId(getContext(), "user1", "host-name", "IMAP4rev1");
String id1b = ImapStore.getImapId(getContext(), "user1", "host-name", "IMAP4rev1");
String id2 = ImapStore.getImapId(getContext(), "user2", "host-name", "IMAP4rev1");
String id1a = ImapStore.getImapId(getContext(), "user1", "host-name", CAPABILITY_RESPONSE);
String id1b = ImapStore.getImapId(getContext(), "user1", "host-name", CAPABILITY_RESPONSE);
String id2 = ImapStore.getImapId(getContext(), "user2", "host-name", CAPABILITY_RESPONSE);
String uid1a = tokenizeImapId(id1a).get("AGUID");
String uid1b = tokenizeImapId(id1b).get("AGUID");

View File

@ -23,6 +23,7 @@ import com.android.email.mail.store.imap.ImapList;
import com.android.email.mail.store.imap.ImapResponse;
import com.android.email.mail.store.imap.ImapSimpleString;
import com.android.email.mail.store.imap.ImapString;
import com.android.email.mail.transport.DiscourseLogger;
import java.io.ByteArrayInputStream;
@ -80,6 +81,19 @@ public final class ImapTestUtils {
return res;
}
/**
* Convenience method to build an {@link ImapResponse} from a single response.
*/
public static final ImapResponse parseResponse(String line) {
ImapResponseParser p = new ImapResponseParser(
new ByteArrayInputStream(Utility.toAscii(line + "\r\n")), new DiscourseLogger(4));
try {
return p.readResponse();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Convenience method to build an {@link FixedLengthInputStream} from a String, using
* US-ASCII.