summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJames Lemieux <jplemieux@google.com>2014-06-09 14:29:48 -0700
committerJames Lemieux <jplemieux@google.com>2014-06-09 16:18:27 -0700
commit9250f4787222f8bc3afe539a2f351fd8d9875fba (patch)
treebdddf31a50bd4e07377711d1a5473c594a681a08 /tests
parent11708a64a54fa75a27d16829bdf85a75acdc1916 (diff)
downloadandroid_packages_apps_Email-9250f4787222f8bc3afe539a2f351fd8d9875fba.tar.gz
android_packages_apps_Email-9250f4787222f8bc3afe539a2f351fd8d9875fba.tar.bz2
android_packages_apps_Email-9250f4787222f8bc3afe539a2f351fd8d9875fba.zip
Unit Tests for serialization of Account and HostAuth objects
b/14998528 These are particularly important since they are stored in JSON form within the Account Manager, so maintaining backward compatibility when future changes are introduced is crucial. Change-Id: I51333a364726a4c7e2fe88ee888e8c4cc11d962f
Diffstat (limited to 'tests')
-rw-r--r--tests/src/com/android/emailcommon/provider/AccountTest.java118
-rw-r--r--tests/src/com/android/emailcommon/provider/HostAuthTests.java67
2 files changed, 185 insertions, 0 deletions
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
+ }
}