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.UnsupportedEncodingException; |
michael@0 | 7 | import java.security.NoSuchAlgorithmException; |
michael@0 | 8 | import java.util.concurrent.TimeUnit; |
michael@0 | 9 | |
michael@0 | 10 | import org.mozilla.gecko.background.common.GlobalConstants; |
michael@0 | 11 | import org.mozilla.gecko.background.helpers.AndroidSyncTestCase; |
michael@0 | 12 | import org.mozilla.gecko.sync.ExtendedJSONObject; |
michael@0 | 13 | import org.mozilla.gecko.sync.SyncConfiguration; |
michael@0 | 14 | import org.mozilla.gecko.sync.SyncConstants; |
michael@0 | 15 | import org.mozilla.gecko.sync.Utils; |
michael@0 | 16 | import org.mozilla.gecko.sync.setup.Constants; |
michael@0 | 17 | import org.mozilla.gecko.sync.setup.SyncAccounts; |
michael@0 | 18 | import org.mozilla.gecko.sync.setup.SyncAccounts.SyncAccountParameters; |
michael@0 | 19 | |
michael@0 | 20 | import android.accounts.Account; |
michael@0 | 21 | import android.accounts.AccountManager; |
michael@0 | 22 | import android.accounts.AccountManagerCallback; |
michael@0 | 23 | import android.accounts.AccountManagerFuture; |
michael@0 | 24 | import android.content.Context; |
michael@0 | 25 | import android.content.Intent; |
michael@0 | 26 | import android.content.SharedPreferences; |
michael@0 | 27 | import android.test.InstrumentationTestCase; |
michael@0 | 28 | |
michael@0 | 29 | /** |
michael@0 | 30 | * We can use <code>performWait</code> and <code>performNotify</code> here if we |
michael@0 | 31 | * are careful about threading issues with <code>AsyncTask</code>. We need to |
michael@0 | 32 | * take some care to both create and run certain tasks on the main thread -- |
michael@0 | 33 | * moving the object allocation out of the UI thread causes failures! |
michael@0 | 34 | * <p> |
michael@0 | 35 | * @see "<a href='http://stackoverflow.com/questions/2321829/android-asynctask-testing-problem-with-android-test-framework'> |
michael@0 | 36 | * http://stackoverflow.com/questions/2321829/android-asynctask-testing-problem-with-android-test-framework</a>." |
michael@0 | 37 | */ |
michael@0 | 38 | public class TestSyncAccounts extends AndroidSyncTestCase { |
michael@0 | 39 | private static final String TEST_USERNAME = "testAccount@mozilla.com"; |
michael@0 | 40 | private static final String TEST_SYNCKEY = "testSyncKey"; |
michael@0 | 41 | private static final String TEST_PASSWORD = "testPassword"; |
michael@0 | 42 | private static final String TEST_SERVERURL = "test.server.url/"; |
michael@0 | 43 | private static final String TEST_CLUSTERURL = "test.cluster.url/"; |
michael@0 | 44 | |
michael@0 | 45 | public static final String TEST_ACCOUNTTYPE = SyncConstants.ACCOUNTTYPE_SYNC; |
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 = Constants.DEFAULT_PROFILE; |
michael@0 | 49 | public static final long TEST_VERSION = SyncConfiguration.CURRENT_PREFS_VERSION; |
michael@0 | 50 | |
michael@0 | 51 | public static final String TEST_PREFERENCE = "testPreference"; |
michael@0 | 52 | public static final String TEST_SYNC_ID = "testSyncID"; |
michael@0 | 53 | |
michael@0 | 54 | private Account account; |
michael@0 | 55 | private Context context; |
michael@0 | 56 | private AccountManager accountManager; |
michael@0 | 57 | private SyncAccountParameters syncAccount; |
michael@0 | 58 | |
michael@0 | 59 | public void setUp() { |
michael@0 | 60 | account = null; |
michael@0 | 61 | context = getApplicationContext(); |
michael@0 | 62 | accountManager = AccountManager.get(context); |
michael@0 | 63 | syncAccount = new SyncAccountParameters(context, null, |
michael@0 | 64 | TEST_USERNAME, TEST_SYNCKEY, TEST_PASSWORD, null); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | public static void deleteAccount(final InstrumentationTestCase test, final AccountManager accountManager, final Account account) { |
michael@0 | 68 | performWait(new Runnable() { |
michael@0 | 69 | @Override |
michael@0 | 70 | public void run() { |
michael@0 | 71 | try { |
michael@0 | 72 | test.runTestOnUiThread(new Runnable() { |
michael@0 | 73 | final AccountManagerCallback<Boolean> callback = new AccountManagerCallback<Boolean>() { |
michael@0 | 74 | @Override |
michael@0 | 75 | public void run(AccountManagerFuture<Boolean> future) { |
michael@0 | 76 | try { |
michael@0 | 77 | future.getResult(5L, TimeUnit.SECONDS); |
michael@0 | 78 | } catch (Exception e) { |
michael@0 | 79 | } |
michael@0 | 80 | performNotify(); |
michael@0 | 81 | } |
michael@0 | 82 | }; |
michael@0 | 83 | |
michael@0 | 84 | @Override |
michael@0 | 85 | public void run() { |
michael@0 | 86 | accountManager.removeAccount(account, callback, null); |
michael@0 | 87 | } |
michael@0 | 88 | }); |
michael@0 | 89 | } catch (Throwable e) { |
michael@0 | 90 | performNotify(e); |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | }); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | public void tearDown() { |
michael@0 | 97 | if (account == null) { |
michael@0 | 98 | return; |
michael@0 | 99 | } |
michael@0 | 100 | deleteAccount(this, accountManager, account); |
michael@0 | 101 | account = null; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | public void testSyncAccountParameters() { |
michael@0 | 105 | assertEquals(TEST_USERNAME, syncAccount.username); |
michael@0 | 106 | assertNull(syncAccount.accountManager); |
michael@0 | 107 | assertNull(syncAccount.serverURL); |
michael@0 | 108 | |
michael@0 | 109 | try { |
michael@0 | 110 | syncAccount = new SyncAccountParameters(context, null, |
michael@0 | 111 | null, TEST_SYNCKEY, TEST_PASSWORD, TEST_SERVERURL); |
michael@0 | 112 | } catch (IllegalArgumentException e) { |
michael@0 | 113 | return; |
michael@0 | 114 | } catch (Exception e) { |
michael@0 | 115 | fail("Did not expect exception: " + e); |
michael@0 | 116 | } |
michael@0 | 117 | fail("Expected IllegalArgumentException."); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | public void testCreateAccount() { |
michael@0 | 121 | int before = accountManager.getAccountsByType(TEST_ACCOUNTTYPE).length; |
michael@0 | 122 | account = SyncAccounts.createSyncAccount(syncAccount, false); |
michael@0 | 123 | int afterCreate = accountManager.getAccountsByType(TEST_ACCOUNTTYPE).length; |
michael@0 | 124 | assertTrue(afterCreate > before); |
michael@0 | 125 | deleteAccount(this, accountManager, account); |
michael@0 | 126 | account = null; |
michael@0 | 127 | int afterDelete = accountManager.getAccountsByType(TEST_ACCOUNTTYPE).length; |
michael@0 | 128 | assertEquals(before, afterDelete); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | public void testCreateSecondAccount() { |
michael@0 | 132 | int before = accountManager.getAccountsByType(TEST_ACCOUNTTYPE).length; |
michael@0 | 133 | account = SyncAccounts.createSyncAccount(syncAccount, false); |
michael@0 | 134 | int afterFirst = accountManager.getAccountsByType(TEST_ACCOUNTTYPE).length; |
michael@0 | 135 | assertTrue(afterFirst > before); |
michael@0 | 136 | |
michael@0 | 137 | SyncAccountParameters secondSyncAccount = new SyncAccountParameters(context, null, |
michael@0 | 138 | "second@username.com", TEST_SYNCKEY, TEST_PASSWORD, null); |
michael@0 | 139 | |
michael@0 | 140 | Account second = SyncAccounts.createSyncAccount(secondSyncAccount, false); |
michael@0 | 141 | assertNotNull(second); |
michael@0 | 142 | int afterSecond = accountManager.getAccountsByType(TEST_ACCOUNTTYPE).length; |
michael@0 | 143 | assertTrue(afterSecond > afterFirst); |
michael@0 | 144 | |
michael@0 | 145 | deleteAccount(this, accountManager, second); |
michael@0 | 146 | deleteAccount(this, accountManager, account); |
michael@0 | 147 | account = null; |
michael@0 | 148 | |
michael@0 | 149 | int afterDelete = accountManager.getAccountsByType(TEST_ACCOUNTTYPE).length; |
michael@0 | 150 | assertEquals(before, afterDelete); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | public void testCreateDuplicateAccount() { |
michael@0 | 154 | int before = accountManager.getAccountsByType(TEST_ACCOUNTTYPE).length; |
michael@0 | 155 | account = SyncAccounts.createSyncAccount(syncAccount, false); |
michael@0 | 156 | int afterCreate = accountManager.getAccountsByType(TEST_ACCOUNTTYPE).length; |
michael@0 | 157 | assertTrue(afterCreate > before); |
michael@0 | 158 | |
michael@0 | 159 | Account dupe = SyncAccounts.createSyncAccount(syncAccount, false); |
michael@0 | 160 | assertNull(dupe); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | public void testClientRecord() throws NoSuchAlgorithmException, UnsupportedEncodingException { |
michael@0 | 164 | final String TEST_NAME = "testName"; |
michael@0 | 165 | final String TEST_GUID = "testGuid"; |
michael@0 | 166 | syncAccount = new SyncAccountParameters(context, null, |
michael@0 | 167 | TEST_USERNAME, TEST_SYNCKEY, TEST_PASSWORD, null, null, TEST_NAME, TEST_GUID); |
michael@0 | 168 | account = SyncAccounts.createSyncAccount(syncAccount, false); |
michael@0 | 169 | assertNotNull(account); |
michael@0 | 170 | |
michael@0 | 171 | SharedPreferences prefs = Utils.getSharedPreferences(context, TEST_PRODUCT, TEST_USERNAME, |
michael@0 | 172 | SyncConstants.DEFAULT_AUTH_SERVER, TEST_PROFILE, TEST_VERSION); |
michael@0 | 173 | |
michael@0 | 174 | // Verify that client record is set. |
michael@0 | 175 | assertEquals(TEST_GUID, prefs.getString(SyncConfiguration.PREF_ACCOUNT_GUID, null)); |
michael@0 | 176 | assertEquals(TEST_NAME, prefs.getString(SyncConfiguration.PREF_CLIENT_NAME, null)); |
michael@0 | 177 | |
michael@0 | 178 | // Let's verify that clusterURL is correctly not set. |
michael@0 | 179 | String clusterURL = prefs.getString(SyncConfiguration.PREF_CLUSTER_URL, null); |
michael@0 | 180 | assertNull(clusterURL); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | public void testClusterURL() throws NoSuchAlgorithmException, UnsupportedEncodingException { |
michael@0 | 184 | syncAccount = new SyncAccountParameters(context, null, |
michael@0 | 185 | TEST_USERNAME, TEST_SYNCKEY, TEST_PASSWORD, TEST_SERVERURL, TEST_CLUSTERURL, null, null); |
michael@0 | 186 | account = SyncAccounts.createSyncAccount(syncAccount, false); |
michael@0 | 187 | assertNotNull(account); |
michael@0 | 188 | |
michael@0 | 189 | SharedPreferences prefs = Utils.getSharedPreferences(context, TEST_PRODUCT, TEST_USERNAME, |
michael@0 | 190 | TEST_SERVERURL, TEST_PROFILE, TEST_VERSION); |
michael@0 | 191 | String clusterURL = prefs.getString(SyncConfiguration.PREF_CLUSTER_URL, null); |
michael@0 | 192 | assertNotNull(clusterURL); |
michael@0 | 193 | assertEquals(TEST_CLUSTERURL, clusterURL); |
michael@0 | 194 | |
michael@0 | 195 | // Let's verify that client name and GUID are not set. |
michael@0 | 196 | assertNull(prefs.getString(SyncConfiguration.PREF_ACCOUNT_GUID, null)); |
michael@0 | 197 | assertNull(prefs.getString(SyncConfiguration.PREF_CLIENT_NAME, null)); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | /** |
michael@0 | 201 | * Verify that creating an account wipes stale settings in Shared Preferences. |
michael@0 | 202 | */ |
michael@0 | 203 | public void testCreatingWipesSharedPrefs() throws Exception { |
michael@0 | 204 | final String TEST_PREFERENCE = "testPreference"; |
michael@0 | 205 | final String TEST_SYNC_ID = "testSyncID"; |
michael@0 | 206 | |
michael@0 | 207 | SharedPreferences prefs = Utils.getSharedPreferences(context, TEST_PRODUCT, TEST_USERNAME, |
michael@0 | 208 | TEST_SERVERURL, TEST_PROFILE, TEST_VERSION); |
michael@0 | 209 | prefs.edit().putString(SyncConfiguration.PREF_SYNC_ID, TEST_SYNC_ID).commit(); |
michael@0 | 210 | prefs.edit().putString(TEST_PREFERENCE, TEST_SYNC_ID).commit(); |
michael@0 | 211 | |
michael@0 | 212 | syncAccount = new SyncAccountParameters(context, null, |
michael@0 | 213 | TEST_USERNAME, TEST_SYNCKEY, TEST_PASSWORD, TEST_SERVERURL); |
michael@0 | 214 | account = SyncAccounts.createSyncAccount(syncAccount, false); |
michael@0 | 215 | assertNotNull(account); |
michael@0 | 216 | |
michael@0 | 217 | // All values deleted (known and unknown). |
michael@0 | 218 | assertNull(prefs.getString(SyncConfiguration.PREF_SYNC_ID, null)); |
michael@0 | 219 | assertNull(prefs.getString(TEST_SYNC_ID, null)); |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | /** |
michael@0 | 223 | * Verify that creating an account preserves settings in Shared Preferences when asked. |
michael@0 | 224 | */ |
michael@0 | 225 | public void testCreateSyncAccountWithExistingPreferences() throws Exception { |
michael@0 | 226 | |
michael@0 | 227 | SharedPreferences prefs = Utils.getSharedPreferences(context, TEST_PRODUCT, TEST_USERNAME, |
michael@0 | 228 | TEST_SERVERURL, TEST_PROFILE, TEST_VERSION); |
michael@0 | 229 | |
michael@0 | 230 | prefs.edit().putString(SyncConfiguration.PREF_SYNC_ID, TEST_SYNC_ID).commit(); |
michael@0 | 231 | prefs.edit().putString(TEST_PREFERENCE, TEST_SYNC_ID).commit(); |
michael@0 | 232 | |
michael@0 | 233 | assertNotNull(prefs.getString(TEST_PREFERENCE, null)); |
michael@0 | 234 | assertNotNull(prefs.getString(SyncConfiguration.PREF_SYNC_ID, null)); |
michael@0 | 235 | |
michael@0 | 236 | syncAccount = new SyncAccountParameters(context, null, |
michael@0 | 237 | TEST_USERNAME, TEST_SYNCKEY, TEST_PASSWORD, TEST_SERVERURL); |
michael@0 | 238 | account = SyncAccounts.createSyncAccountPreservingExistingPreferences(syncAccount, false); |
michael@0 | 239 | assertNotNull(account); |
michael@0 | 240 | |
michael@0 | 241 | // All values remain (known and unknown). |
michael@0 | 242 | assertNotNull(prefs.getString(TEST_PREFERENCE, null)); |
michael@0 | 243 | assertNotNull(prefs.getString(SyncConfiguration.PREF_SYNC_ID, null)); |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | protected void assertParams(final SyncAccountParameters params) throws Exception { |
michael@0 | 247 | assertNotNull(params); |
michael@0 | 248 | assertEquals(context, params.context); |
michael@0 | 249 | assertEquals(Utils.usernameFromAccount(TEST_USERNAME), params.username); |
michael@0 | 250 | assertEquals(TEST_PASSWORD, params.password); |
michael@0 | 251 | assertEquals(TEST_SERVERURL, params.serverURL); |
michael@0 | 252 | assertEquals(TEST_SYNCKEY, params.syncKey); |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | public void testBlockingFromAndroidAccountV0() throws Throwable { |
michael@0 | 256 | syncAccount = new SyncAccountParameters(context, null, |
michael@0 | 257 | TEST_USERNAME, TEST_SYNCKEY, TEST_PASSWORD, TEST_SERVERURL, TEST_CLUSTERURL, null, null); |
michael@0 | 258 | try { |
michael@0 | 259 | account = SyncAccounts.createSyncAccount(syncAccount); |
michael@0 | 260 | assertNotNull(account); |
michael@0 | 261 | |
michael@0 | 262 | // Test fetching parameters multiple times. Historically, we needed to |
michael@0 | 263 | // invalidate this token type every fetch; now we don't, but we'd like |
michael@0 | 264 | // to ensure multiple fetches work. |
michael@0 | 265 | SyncAccountParameters params = SyncAccounts.blockingFromAndroidAccountV0(context, accountManager, account); |
michael@0 | 266 | assertParams(params); |
michael@0 | 267 | |
michael@0 | 268 | params = SyncAccounts.blockingFromAndroidAccountV0(context, accountManager, account); |
michael@0 | 269 | assertParams(params); |
michael@0 | 270 | |
michael@0 | 271 | // Test this works on the main thread. |
michael@0 | 272 | this.runTestOnUiThread(new Runnable() { |
michael@0 | 273 | @Override |
michael@0 | 274 | public void run() { |
michael@0 | 275 | SyncAccountParameters params; |
michael@0 | 276 | try { |
michael@0 | 277 | params = SyncAccounts.blockingFromAndroidAccountV0(context, accountManager, account); |
michael@0 | 278 | assertParams(params); |
michael@0 | 279 | } catch (Exception e) { |
michael@0 | 280 | fail("Fetching Sync account parameters failed on UI thread."); |
michael@0 | 281 | } |
michael@0 | 282 | } |
michael@0 | 283 | }); |
michael@0 | 284 | } finally { |
michael@0 | 285 | if (account != null) { |
michael@0 | 286 | deleteAccount(this, accountManager, account); |
michael@0 | 287 | account = null; |
michael@0 | 288 | } |
michael@0 | 289 | } |
michael@0 | 290 | } |
michael@0 | 291 | |
michael@0 | 292 | public void testMakeSyncAccountDeletedIntent() throws Throwable { |
michael@0 | 293 | syncAccount = new SyncAccountParameters(context, null, |
michael@0 | 294 | TEST_USERNAME, TEST_SYNCKEY, TEST_PASSWORD, TEST_SERVERURL, TEST_CLUSTERURL, null, null); |
michael@0 | 295 | try { |
michael@0 | 296 | account = SyncAccounts.createSyncAccount(syncAccount); |
michael@0 | 297 | assertNotNull(account); |
michael@0 | 298 | |
michael@0 | 299 | Intent intent = SyncAccounts.makeSyncAccountDeletedIntent(context, accountManager, account); |
michael@0 | 300 | assertEquals(SyncConstants.SYNC_ACCOUNT_DELETED_ACTION, intent.getAction()); |
michael@0 | 301 | assertEquals(SyncConstants.SYNC_ACCOUNT_DELETED_INTENT_VERSION, intent.getLongExtra(Constants.JSON_KEY_VERSION, 0)); |
michael@0 | 302 | assertEquals(TEST_USERNAME, intent.getStringExtra(Constants.JSON_KEY_ACCOUNT)); |
michael@0 | 303 | assertTrue(Math.abs(intent.getLongExtra(Constants.JSON_KEY_TIMESTAMP, 0) - System.currentTimeMillis()) < 1000); |
michael@0 | 304 | |
michael@0 | 305 | String payload = intent.getStringExtra(Constants.JSON_KEY_PAYLOAD); |
michael@0 | 306 | assertNotNull(payload); |
michael@0 | 307 | |
michael@0 | 308 | SyncAccountParameters params = new SyncAccountParameters(context, accountManager, ExtendedJSONObject.parseJSONObject(payload)); |
michael@0 | 309 | // Can't use assertParams because Sync key is deleted. |
michael@0 | 310 | assertNotNull(params); |
michael@0 | 311 | assertEquals(context, params.context); |
michael@0 | 312 | assertEquals(Utils.usernameFromAccount(TEST_USERNAME), params.username); |
michael@0 | 313 | assertEquals(TEST_PASSWORD, params.password); |
michael@0 | 314 | assertEquals(TEST_SERVERURL, params.serverURL); |
michael@0 | 315 | assertEquals("", params.syncKey); |
michael@0 | 316 | } finally { |
michael@0 | 317 | if (account != null) { |
michael@0 | 318 | deleteAccount(this, accountManager, account); |
michael@0 | 319 | account = null; |
michael@0 | 320 | } |
michael@0 | 321 | } |
michael@0 | 322 | } |
michael@0 | 323 | |
michael@0 | 324 | public void testBlockingPrefsFromAndroidAccountV0() throws Exception { |
michael@0 | 325 | // Create test account with prefs. We use a different username to avoid a |
michael@0 | 326 | // timing issue, where the delayed clean-up of the account created by the |
michael@0 | 327 | // previous test deletes the preferences for this account. |
michael@0 | 328 | SharedPreferences prefs = Utils.getSharedPreferences(context, TEST_PRODUCT, |
michael@0 | 329 | TEST_USERNAME + "2", TEST_SERVERURL, TEST_PROFILE, TEST_VERSION); |
michael@0 | 330 | prefs.edit().putString(TEST_PREFERENCE, TEST_SYNC_ID).commit(); |
michael@0 | 331 | |
michael@0 | 332 | syncAccount = new SyncAccountParameters(context, null, |
michael@0 | 333 | TEST_USERNAME + "2", TEST_SYNCKEY, TEST_PASSWORD, TEST_SERVERURL); |
michael@0 | 334 | account = SyncAccounts.createSyncAccountPreservingExistingPreferences(syncAccount, false); |
michael@0 | 335 | assertNotNull(account); |
michael@0 | 336 | |
michael@0 | 337 | // Fetch account and check prefs. |
michael@0 | 338 | SharedPreferences sharedPreferences = SyncAccounts.blockingPrefsFromAndroidAccountV0(context, accountManager, |
michael@0 | 339 | account, TEST_PRODUCT, TEST_PROFILE, TEST_VERSION); |
michael@0 | 340 | assertNotNull(sharedPreferences); |
michael@0 | 341 | assertEquals(TEST_SYNC_ID, sharedPreferences.getString(TEST_PREFERENCE, null)); |
michael@0 | 342 | } |
michael@0 | 343 | } |