From 34f29c8a7478cf8c85578d176ac27d973ecca7e4 Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Mon, 14 Jun 2010 09:55:41 -0700 Subject: [PATCH] Test for interaction between ImapStore and vendor policy. Change-Id: I092b3a0f2f40d9aa19f2f61066362099c8b3f50b --- .../android/email/mail/store/ImapStore.java | 12 ++--- .../email/mail/store/ImapStoreUnitTests.java | 48 +++++++++++++++++-- .../email/mail/store/imap/ImapTestUtils.java | 14 ++++++ 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/com/android/email/mail/store/ImapStore.java b/src/com/android/email/mail/store/ImapStore.java index 20c87ba9e..c2557fa11 100644 --- a/src/com/android/email/mail/store/ImapStore.java +++ b/src/com/android/email/mail/store/ImapStore.java @@ -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) { diff --git a/tests/src/com/android/email/mail/store/ImapStoreUnitTests.java b/tests/src/com/android/email/mail/store/ImapStoreUnitTests.java index d81765480..6d6113ac2 100644 --- a/tests/src/com/android/email/mail/store/ImapStoreUnitTests.java +++ b/tests/src/com/android/email/mail/store/ImapStoreUnitTests.java @@ -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 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"); diff --git a/tests/src/com/android/email/mail/store/imap/ImapTestUtils.java b/tests/src/com/android/email/mail/store/imap/ImapTestUtils.java index 17162101f..16d056375 100644 --- a/tests/src/com/android/email/mail/store/imap/ImapTestUtils.java +++ b/tests/src/com/android/email/mail/store/imap/ImapTestUtils.java @@ -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.