diff --git a/emailcommon/src/com/android/emailcommon/provider/Account.java b/emailcommon/src/com/android/emailcommon/provider/Account.java index 615b47090..b103b6a58 100755 --- a/emailcommon/src/com/android/emailcommon/provider/Account.java +++ b/emailcommon/src/com/android/emailcommon/provider/Account.java @@ -35,6 +35,7 @@ import android.os.RemoteException; import com.android.emailcommon.utility.Utility; import com.android.mail.utils.LogUtils; +import com.google.common.annotations.VisibleForTesting; import org.json.JSONException; import org.json.JSONObject; @@ -141,8 +142,10 @@ public final class Account extends EmailContent implements Parcelable { public long mPolicyKey; public long mPingDuration; - private static final String JSON_TAG_HOST_AUTH_RECV = "hostAuthRecv"; - private static final String JSON_TAG_HOST_AUTH_SEND = "hostAuthSend"; + @VisibleForTesting + static final String JSON_TAG_HOST_AUTH_RECV = "hostAuthRecv"; + @VisibleForTesting + static final String JSON_TAG_HOST_AUTH_SEND = "hostAuthSend"; // Convenience for creating/working with an account public transient HostAuth mHostAuthRecv; diff --git a/emailcommon/src/com/android/emailcommon/provider/HostAuth.java b/emailcommon/src/com/android/emailcommon/provider/HostAuth.java index 6899a7d59..c23f276a8 100644 --- a/emailcommon/src/com/android/emailcommon/provider/HostAuth.java +++ b/emailcommon/src/com/android/emailcommon/provider/HostAuth.java @@ -27,6 +27,7 @@ import android.text.TextUtils; import com.android.emailcommon.utility.SSLUtils; import com.android.mail.utils.LogUtils; +import com.google.common.annotations.VisibleForTesting; import org.json.JSONException; import org.json.JSONObject; @@ -71,7 +72,8 @@ public class HostAuth extends EmailContent implements Parcelable { public byte[] mServerCert = null; public long mCredentialKey; - private static final String JSON_TAG_CREDENTIAL = "credential"; + @VisibleForTesting + static final String JSON_TAG_CREDENTIAL = "credential"; public transient Credential mCredential; public static final int CONTENT_ID_COLUMN = 0; diff --git a/tests/src/com/android/emailcommon/provider/AccountTest.java b/tests/src/com/android/emailcommon/provider/AccountTest.java new file mode 100644 index 000000000..8098ec37f --- /dev/null +++ b/tests/src/com/android/emailcommon/provider/AccountTest.java @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2014 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 android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; + +import org.json.JSONException; +import org.json.JSONObject; + +@SmallTest +public class AccountTest extends AndroidTestCase { + + public void testDeserializeFromJSON() throws JSONException { + final JSONObject json = new JSONObject(); + json.put(EmailContent.AccountColumns.DISPLAY_NAME, "David Hasselhoff"); + json.put(EmailContent.AccountColumns.EMAIL_ADDRESS, "dhoff@example.com"); + json.put(EmailContent.AccountColumns.SYNC_LOOKBACK, 42); + json.put(EmailContent.AccountColumns.SYNC_INTERVAL, 99); + json.put(Account.JSON_TAG_HOST_AUTH_RECV, getHostAuthJSON("receiver", "recpass").toJson()); + json.put(Account.JSON_TAG_HOST_AUTH_SEND, getHostAuthJSON("send", "sendpass").toJson()); + json.put(EmailContent.AccountColumns.FLAGS, 22); + json.put(EmailContent.AccountColumns.SENDER_NAME, "Friend of Kitt"); + json.put(EmailContent.AccountColumns.PROTOCOL_VERSION, "protocol version 3.14"); + json.put(EmailContent.AccountColumns.SIGNATURE, "David with a heart over the i"); + json.put(EmailContent.AccountColumns.PING_DURATION, 77); + + // deserialize the json + final Account a = Account.fromJson(json); + + // verify that all fields deserialized as expected + assertEquals("David Hasselhoff", a.getDisplayName()); + assertEquals("dhoff@example.com", a.getEmailAddress()); + assertEquals(42, a.getSyncLookback()); + assertEquals(99, a.getSyncInterval()); + assertEquals("receiver", a.mHostAuthRecv.mLogin); + assertEquals("recpass", a.mHostAuthRecv.mPassword); + assertEquals("send", a.mHostAuthSend.mLogin); + assertEquals("sendpass", a.mHostAuthSend.mPassword); + assertEquals(22, a.getFlags()); + assertEquals("Friend of Kitt", a.getSenderName()); + assertEquals("protocol version 3.14", a.mProtocolVersion); + assertEquals("David with a heart over the i", a.getSignature()); + assertEquals(77, a.mPingDuration); + } + + /** + * A factory method to generate HostAuth values that allow us to serialize the Account object. + * See {@link HostAuthTests} for tests that exercise serialization of HostAuth. + * + * @param username a given username + * @param password a given password + * @return a HostAuth that includes the given username and password + */ + private static HostAuth getHostAuthJSON(String username, String password) { + final HostAuth ha = new HostAuth(); + ha.setLogin(username, password); + ha.mProtocol = "IMAP"; + ha.mAddress = "dhoff@example.com"; + ha.mPort = 543; + ha.mFlags = 777; + return ha; + } + + public void testSerializeAndDeserializeWithJSON() { + // build an Account object with all fields set + final Account before = new Account(); + before.setDisplayName("David Hasselhoff"); + before.setEmailAddress("dhoff@example.com"); + before.mSyncKey = "syncKey"; + before.setSyncLookback(42); + before.setSyncInterval(99); + before.setFlags(1 << 5); + before.setSenderName("Friend of Kitt"); + before.mProtocolVersion = "protocol version 3.14"; + before.mSecuritySyncKey = "securitySyncKey"; + before.setSignature("David with a heart over the i"); + before.mPolicyKey = 66; + before.mPingDuration = 77; + before.mHostAuthRecv = getHostAuthJSON("receiver", "recpass"); + + // this must be called before serialization occurs + before.ensureLoaded(getContext()); + + // serialize and deserialize + final Account after = Account.fromJson(before.toJson()); + + assertEquals(before.getDisplayName(), after.getDisplayName()); + assertEquals(before.getEmailAddress(), after.getEmailAddress()); + assertEquals(before.getSyncLookback(), after.getSyncLookback()); + assertEquals(before.getSyncInterval(), after.getSyncInterval()); + assertEquals(before.mHostAuthSend, after.mHostAuthSend); + assertEquals(before.mHostAuthKeySend, after.mHostAuthKeySend); + assertEquals(before.mHostAuthKeyRecv, after.mHostAuthKeyRecv); + assertEquals(before.getFlags(), after.getFlags()); + assertEquals(before.getSenderName(), after.getSenderName()); + assertEquals(before.mProtocolVersion, after.mProtocolVersion); + assertEquals(before.getSignature(), after.getSignature()); + assertEquals(before.mPingDuration, after.mPingDuration); + + assertNull(after.mSyncKey); // sync key is not serialized; field defaults to null + assertEquals(0, after.mPolicyKey); // policy key is not serialized; field defaults to 0 + } +} diff --git a/tests/src/com/android/emailcommon/provider/HostAuthTests.java b/tests/src/com/android/emailcommon/provider/HostAuthTests.java index 8b5540b58..89a9b205e 100644 --- a/tests/src/com/android/emailcommon/provider/HostAuthTests.java +++ b/tests/src/com/android/emailcommon/provider/HostAuthTests.java @@ -21,6 +21,11 @@ import android.test.AndroidTestCase; import android.test.mock.MockContext; import android.test.suitebuilder.annotation.SmallTest; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.Arrays; + /** * Unit tests for the HostAuth inner class. * These tests must be locally complete - no server(s) required. @@ -214,5 +219,67 @@ public class HostAuthTests extends AndroidTestCase { assertEquals(orig.mCredentialKey, unparceled2.mCredentialKey); assertEquals(orig.mCredential, unparceled2.mCredential); } + + public void testDeserializeFromJSON() throws JSONException { + final JSONObject json = new JSONObject(); + json.put(EmailContent.HostAuthColumns.PROTOCOL, "IMAP"); + json.put(EmailContent.HostAuthColumns.ADDRESS, "dhoff@example.com"); + json.put(EmailContent.HostAuthColumns.PORT, 1337); + json.put(EmailContent.HostAuthColumns.FLAGS, 293847); + json.put(EmailContent.HostAuthColumns.LOGIN, "dhoff"); + json.put(EmailContent.HostAuthColumns.PASSWORD, "daknightrida"); + json.put(EmailContent.HostAuthColumns.DOMAIN, "example.com"); + json.put(EmailContent.HostAuthColumns.CLIENT_CERT_ALIAS, "I'm a client cert alias"); + json.put(HostAuth.JSON_TAG_CREDENTIAL, Credential.EMPTY.toJson()); + + // deserialize the json + final HostAuth ha = HostAuth.fromJson(json); + + // verify that all fields deserialized as expected + assertEquals("IMAP", ha.mProtocol); + assertEquals("dhoff@example.com", ha.mAddress); + assertEquals(1337, ha.mPort); + assertEquals(293847, ha.mFlags); + assertEquals("dhoff", ha.mLogin); + assertEquals("daknightrida", ha.mPassword); + assertEquals("example.com", ha.mDomain); + assertEquals("I'm a client cert alias", ha.mClientCertAlias); + assertEquals(Credential.EMPTY, ha.mCredential); + + assertNull(ha.mServerCert); // server cert is not serialized; field defaults to null + assertEquals(-1, ha.mCredentialKey); // cred key is not serialized; field defaults to -1 + } + + public void testSerializeAndDeserializeWithJSON() { + final HostAuth before = new HostAuth(); + before.mProtocol = "IMAP"; + before.mAddress = "dhoff@example.com"; + before.mPort = 1337; + before.mFlags = 293847; + before.setLogin("dhoff", "daknightrida"); + before.mDomain = "example.com"; + before.mClientCertAlias = "I'm a client cert alias"; + before.mServerCert = new byte[] {(byte) 0xFF, (byte) 0xAA}; + before.mCredentialKey = 9873425; + before.mCredential = Credential.EMPTY; + + // this must be called before serialization occurs + before.ensureLoaded(getContext()); + + // serialize and deserialize + final HostAuth after = HostAuth.fromJson(before.toJson()); + + assertEquals(before.mProtocol, after.mProtocol); + assertEquals(before.mAddress, after.mAddress); + assertEquals(before.mPort, after.mPort); + assertEquals(before.mFlags, after.mFlags); + assertTrue(Arrays.equals(before.getLogin(), after.getLogin())); + assertEquals(before.mDomain, after.mDomain); + assertEquals(before.mClientCertAlias, after.mClientCertAlias); + assertEquals(before.mCredential, after.mCredential); + + assertNull(after.mServerCert); // server cert is not serialized; field defaults to null + assertEquals(-1, after.mCredentialKey); // cred key is not serialized; field defaults to 0 + } }