Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | package org.mozilla.gecko.background.sync; |
michael@0 | 5 | |
michael@0 | 6 | import java.io.FileInputStream; |
michael@0 | 7 | import java.io.FileNotFoundException; |
michael@0 | 8 | import java.io.FileOutputStream; |
michael@0 | 9 | import java.io.PrintStream; |
michael@0 | 10 | import java.util.ArrayList; |
michael@0 | 11 | import java.util.List; |
michael@0 | 12 | |
michael@0 | 13 | import org.mozilla.gecko.background.common.GlobalConstants; |
michael@0 | 14 | import org.mozilla.gecko.background.helpers.AndroidSyncTestCase; |
michael@0 | 15 | import org.mozilla.gecko.db.BrowserContract; |
michael@0 | 16 | import org.mozilla.gecko.sync.ExtendedJSONObject; |
michael@0 | 17 | import org.mozilla.gecko.sync.SyncConfiguration; |
michael@0 | 18 | import org.mozilla.gecko.sync.SyncConstants; |
michael@0 | 19 | import org.mozilla.gecko.sync.Utils; |
michael@0 | 20 | import org.mozilla.gecko.sync.config.AccountPickler; |
michael@0 | 21 | import org.mozilla.gecko.sync.setup.Constants; |
michael@0 | 22 | import org.mozilla.gecko.sync.setup.SyncAccounts; |
michael@0 | 23 | import org.mozilla.gecko.sync.setup.SyncAccounts.SyncAccountParameters; |
michael@0 | 24 | |
michael@0 | 25 | import android.accounts.Account; |
michael@0 | 26 | import android.accounts.AccountManager; |
michael@0 | 27 | import android.content.ContentResolver; |
michael@0 | 28 | import android.content.Context; |
michael@0 | 29 | import android.content.SharedPreferences; |
michael@0 | 30 | |
michael@0 | 31 | public class TestAccountPickler extends AndroidSyncTestCase { |
michael@0 | 32 | public static final String TEST_FILENAME = "test.json"; |
michael@0 | 33 | public static final String TEST_ACCOUNTTYPE = SyncConstants.ACCOUNTTYPE_SYNC; |
michael@0 | 34 | |
michael@0 | 35 | // Test account names must start with TEST_USERNAME in order to be recognized |
michael@0 | 36 | // as test accounts and deleted in tearDown. |
michael@0 | 37 | public static final String TEST_USERNAME = "testAccount@mozilla.com"; |
michael@0 | 38 | public static final String TEST_USERNAME2 = TEST_USERNAME + "2"; |
michael@0 | 39 | |
michael@0 | 40 | public static final String TEST_SYNCKEY = "testSyncKey"; |
michael@0 | 41 | public static final String TEST_PASSWORD = "testPassword"; |
michael@0 | 42 | public static final String TEST_SERVER_URL = "test.server.url/"; |
michael@0 | 43 | public static final String TEST_CLUSTER_URL = "test.cluster.url/"; |
michael@0 | 44 | public static final String TEST_CLIENT_NAME = "testClientName"; |
michael@0 | 45 | public static final String TEST_CLIENT_GUID = "testClientGuid"; |
michael@0 | 46 | |
michael@0 | 47 | public static final String TEST_PRODUCT = GlobalConstants.BROWSER_INTENT_PACKAGE; |
michael@0 | 48 | public static final String TEST_PROFILE = "default"; |
michael@0 | 49 | public static final long TEST_VERSION = SyncConfiguration.CURRENT_PREFS_VERSION; |
michael@0 | 50 | |
michael@0 | 51 | protected SyncAccountParameters params; |
michael@0 | 52 | protected Context context; |
michael@0 | 53 | protected AccountManager accountManager; |
michael@0 | 54 | protected int numAccounts; |
michael@0 | 55 | |
michael@0 | 56 | public void setUp() { |
michael@0 | 57 | context = getApplicationContext(); |
michael@0 | 58 | accountManager = AccountManager.get(context); |
michael@0 | 59 | params = new SyncAccountParameters(context, accountManager, |
michael@0 | 60 | TEST_USERNAME, TEST_SYNCKEY, TEST_PASSWORD, TEST_SERVER_URL, |
michael@0 | 61 | TEST_CLUSTER_URL, TEST_CLIENT_NAME, TEST_CLIENT_GUID); |
michael@0 | 62 | numAccounts = accountManager.getAccountsByType(TEST_ACCOUNTTYPE).length; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | public static List<Account> getTestAccounts(final AccountManager accountManager) { |
michael@0 | 66 | final List<Account> testAccounts = new ArrayList<Account>(); |
michael@0 | 67 | |
michael@0 | 68 | final Account[] accounts = accountManager.getAccountsByType(TEST_ACCOUNTTYPE); |
michael@0 | 69 | for (Account account : accounts) { |
michael@0 | 70 | if (account.name.startsWith(TEST_USERNAME)) { |
michael@0 | 71 | testAccounts.add(account); |
michael@0 | 72 | } |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | return testAccounts; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | public void deleteTestAccounts() { |
michael@0 | 79 | for (Account account : getTestAccounts(accountManager)) { |
michael@0 | 80 | TestSyncAccounts.deleteAccount(this, accountManager, account); |
michael@0 | 81 | } |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | public void tearDown() { |
michael@0 | 85 | deleteTestAccounts(); |
michael@0 | 86 | assertEquals(numAccounts, accountManager.getAccountsByType(TEST_ACCOUNTTYPE).length); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | public static void assertFileNotPresent(final Context context, final String filename) throws Exception { |
michael@0 | 90 | // Verify file is not present. |
michael@0 | 91 | FileInputStream fis = null; |
michael@0 | 92 | try { |
michael@0 | 93 | fis = context.openFileInput(TEST_FILENAME); |
michael@0 | 94 | fail("Should get FileNotFoundException."); |
michael@0 | 95 | } catch (FileNotFoundException e) { |
michael@0 | 96 | // Do nothing; file should not exist. |
michael@0 | 97 | } finally { |
michael@0 | 98 | if (fis != null) { |
michael@0 | 99 | fis.close(); |
michael@0 | 100 | } |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | public void testPersist() throws Exception { |
michael@0 | 105 | context.deleteFile(TEST_FILENAME); |
michael@0 | 106 | assertFileNotPresent(context, TEST_FILENAME); |
michael@0 | 107 | |
michael@0 | 108 | AccountPickler.pickle(context, TEST_FILENAME, params, true); |
michael@0 | 109 | |
michael@0 | 110 | final String s = Utils.readFile(context, TEST_FILENAME); |
michael@0 | 111 | assertNotNull(s); |
michael@0 | 112 | |
michael@0 | 113 | final ExtendedJSONObject o = ExtendedJSONObject.parseJSONObject(s); |
michael@0 | 114 | assertEquals(TEST_USERNAME, o.getString(Constants.JSON_KEY_ACCOUNT)); |
michael@0 | 115 | assertEquals(TEST_PASSWORD, o.getString(Constants.JSON_KEY_PASSWORD)); |
michael@0 | 116 | assertEquals(TEST_SERVER_URL, o.getString(Constants.JSON_KEY_SERVER)); |
michael@0 | 117 | assertEquals(TEST_SYNCKEY, o.getString(Constants.JSON_KEY_SYNCKEY)); |
michael@0 | 118 | assertEquals(TEST_CLUSTER_URL, o.getString(Constants.JSON_KEY_CLUSTER)); |
michael@0 | 119 | assertEquals(TEST_CLIENT_NAME, o.getString(Constants.JSON_KEY_CLIENT_NAME)); |
michael@0 | 120 | assertEquals(TEST_CLIENT_GUID, o.getString(Constants.JSON_KEY_CLIENT_GUID)); |
michael@0 | 121 | assertEquals(Boolean.valueOf(true), o.get(Constants.JSON_KEY_SYNC_AUTOMATICALLY)); |
michael@0 | 122 | assertEquals(Long.valueOf(AccountPickler.VERSION), o.getLong(Constants.JSON_KEY_VERSION)); |
michael@0 | 123 | assertTrue(o.containsKey(Constants.JSON_KEY_TIMESTAMP)); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | public void testDeletePickle() throws Exception { |
michael@0 | 127 | AccountPickler.pickle(context, TEST_FILENAME, params, false); |
michael@0 | 128 | |
michael@0 | 129 | // Verify file is present. |
michael@0 | 130 | final String s = Utils.readFile(context, TEST_FILENAME); |
michael@0 | 131 | assertNotNull(s); |
michael@0 | 132 | assertTrue(s.length() > 0); |
michael@0 | 133 | |
michael@0 | 134 | AccountPickler.deletePickle(context, TEST_FILENAME); |
michael@0 | 135 | assertFileNotPresent(context, TEST_FILENAME); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | public Account deleteAccountsAndUnpickle(final Context context, final AccountManager accountManager, final String filename) { |
michael@0 | 139 | deleteTestAccounts(); |
michael@0 | 140 | assertEquals(0, getTestAccounts(accountManager).size()); |
michael@0 | 141 | |
michael@0 | 142 | return AccountPickler.unpickle(context, filename); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | public void testUnpickleSuccess() throws Exception { |
michael@0 | 146 | AccountPickler.pickle(context, TEST_FILENAME, params, true); |
michael@0 | 147 | |
michael@0 | 148 | // Make sure we have no accounts hanging around. |
michael@0 | 149 | final Account account = deleteAccountsAndUnpickle(context, accountManager, TEST_FILENAME); |
michael@0 | 150 | assertNotNull(account); |
michael@0 | 151 | |
michael@0 | 152 | try { |
michael@0 | 153 | assertEquals(1, getTestAccounts(accountManager).size()); |
michael@0 | 154 | assertTrue(ContentResolver.getSyncAutomatically(account, BrowserContract.AUTHORITY)); |
michael@0 | 155 | assertEquals(account.name, TEST_USERNAME); |
michael@0 | 156 | |
michael@0 | 157 | // Verify Account parameters are in place. Not testing clusterURL since it's stored in |
michael@0 | 158 | // shared prefs and it's less critical. |
michael@0 | 159 | final String password = accountManager.getPassword(account); |
michael@0 | 160 | final String serverURL = accountManager.getUserData(account, Constants.OPTION_SERVER); |
michael@0 | 161 | final String syncKey = accountManager.getUserData(account, Constants.OPTION_SYNCKEY); |
michael@0 | 162 | |
michael@0 | 163 | assertEquals(TEST_PASSWORD, password); |
michael@0 | 164 | assertEquals(TEST_SERVER_URL, serverURL); |
michael@0 | 165 | assertEquals(TEST_SYNCKEY, syncKey); |
michael@0 | 166 | |
michael@0 | 167 | // Verify shared prefs parameters are in place. |
michael@0 | 168 | final SharedPreferences prefs = Utils.getSharedPreferences(context, TEST_PRODUCT, TEST_USERNAME, TEST_SERVER_URL, TEST_PROFILE, TEST_VERSION); |
michael@0 | 169 | final String clusterURL = prefs.getString(SyncConfiguration.PREF_CLUSTER_URL, null); |
michael@0 | 170 | final String clientName = prefs.getString(SyncConfiguration.PREF_CLIENT_NAME, null); |
michael@0 | 171 | final String clientGuid = prefs.getString(SyncConfiguration.PREF_ACCOUNT_GUID, null); |
michael@0 | 172 | |
michael@0 | 173 | assertEquals(TEST_CLUSTER_URL, clusterURL); |
michael@0 | 174 | assertEquals(TEST_CLIENT_NAME, clientName); |
michael@0 | 175 | assertEquals(TEST_CLIENT_GUID, clientGuid); |
michael@0 | 176 | } finally { |
michael@0 | 177 | TestSyncAccounts.deleteAccount(this, accountManager, account); |
michael@0 | 178 | } |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | public void testUnpickleNoAutomatic() throws Exception { |
michael@0 | 182 | AccountPickler.pickle(context, TEST_FILENAME, params, false); |
michael@0 | 183 | |
michael@0 | 184 | // Make sure we have no accounts hanging around. |
michael@0 | 185 | final Account account = deleteAccountsAndUnpickle(context, accountManager, TEST_FILENAME); |
michael@0 | 186 | assertNotNull(account); |
michael@0 | 187 | |
michael@0 | 188 | try { |
michael@0 | 189 | assertEquals(1, getTestAccounts(accountManager).size()); |
michael@0 | 190 | assertFalse(ContentResolver.getSyncAutomatically(account, BrowserContract.AUTHORITY)); |
michael@0 | 191 | } finally { |
michael@0 | 192 | TestSyncAccounts.deleteAccount(this, accountManager, account); |
michael@0 | 193 | } |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | public void testUnpickleNoFile() { |
michael@0 | 197 | // Just in case file is hanging around. |
michael@0 | 198 | context.deleteFile(TEST_FILENAME); |
michael@0 | 199 | |
michael@0 | 200 | final Account account = deleteAccountsAndUnpickle(context, accountManager, TEST_FILENAME); |
michael@0 | 201 | assertNull(account); |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | public void testUnpickleIncompleteUserData() throws Exception { |
michael@0 | 205 | final FileOutputStream fos = context.openFileOutput(TEST_FILENAME, Context.MODE_PRIVATE); |
michael@0 | 206 | final PrintStream ps = (new PrintStream(fos)); |
michael@0 | 207 | ps.print("{}"); // Valid JSON, just missing everything. |
michael@0 | 208 | ps.close(); |
michael@0 | 209 | fos.close(); |
michael@0 | 210 | |
michael@0 | 211 | final Account account = deleteAccountsAndUnpickle(context, accountManager, TEST_FILENAME); |
michael@0 | 212 | assertNull(account); |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | public void testUnpickleMalformedFile() throws Exception { |
michael@0 | 216 | final FileOutputStream fos = context.openFileOutput(TEST_FILENAME, Context.MODE_PRIVATE); |
michael@0 | 217 | final PrintStream ps = (new PrintStream(fos)); |
michael@0 | 218 | ps.print("{1:!}"); // Not valid JSON. |
michael@0 | 219 | ps.close(); |
michael@0 | 220 | fos.close(); |
michael@0 | 221 | |
michael@0 | 222 | final Account account = deleteAccountsAndUnpickle(context, accountManager, TEST_FILENAME); |
michael@0 | 223 | assertNull(account); |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | public void testUnpickleAccountAlreadyExists() { |
michael@0 | 227 | AccountPickler.pickle(context, TEST_FILENAME, params, false); |
michael@0 | 228 | |
michael@0 | 229 | // Make sure we have no test accounts hanging around. |
michael@0 | 230 | final Account account = deleteAccountsAndUnpickle(context, accountManager, TEST_FILENAME); |
michael@0 | 231 | assertNotNull(account); |
michael@0 | 232 | assertEquals(TEST_USERNAME, account.name); |
michael@0 | 233 | |
michael@0 | 234 | // Now replace file with new username. |
michael@0 | 235 | params = new SyncAccountParameters(context, accountManager, |
michael@0 | 236 | TEST_USERNAME2, TEST_SYNCKEY, TEST_PASSWORD, TEST_SERVER_URL, null, TEST_CLIENT_NAME, TEST_CLIENT_GUID); |
michael@0 | 237 | AccountPickler.pickle(context, TEST_FILENAME, params, false); |
michael@0 | 238 | |
michael@0 | 239 | // Checking if sync accounts exist could try to unpickle. That unpickle |
michael@0 | 240 | // would load an account with a different username, so we can check that |
michael@0 | 241 | // nothing was unpickled by verifying that the username has not changed. |
michael@0 | 242 | assertTrue(SyncAccounts.syncAccountsExist(context)); |
michael@0 | 243 | |
michael@0 | 244 | for (Account a : getTestAccounts(accountManager)) { |
michael@0 | 245 | assertEquals(TEST_USERNAME, a.name); |
michael@0 | 246 | } |
michael@0 | 247 | } |
michael@0 | 248 | } |