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.fxa.authenticator; |
michael@0 | 5 | |
michael@0 | 6 | import org.mozilla.gecko.background.helpers.AndroidSyncTestCase; |
michael@0 | 7 | import org.mozilla.gecko.background.sync.TestSyncAccounts; |
michael@0 | 8 | import org.mozilla.gecko.fxa.FxAccountConstants; |
michael@0 | 9 | import org.mozilla.gecko.fxa.authenticator.AccountPickler; |
michael@0 | 10 | import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount; |
michael@0 | 11 | import org.mozilla.gecko.fxa.login.Separated; |
michael@0 | 12 | import org.mozilla.gecko.fxa.login.State; |
michael@0 | 13 | import org.mozilla.gecko.sync.Utils; |
michael@0 | 14 | |
michael@0 | 15 | import android.accounts.Account; |
michael@0 | 16 | import android.accounts.AccountManager; |
michael@0 | 17 | import android.test.InstrumentationTestCase; |
michael@0 | 18 | import android.test.RenamingDelegatingContext; |
michael@0 | 19 | |
michael@0 | 20 | public class TestAccountPickler extends AndroidSyncTestCase { |
michael@0 | 21 | private final static String FILENAME_PREFIX = "TestAccountPickler-"; |
michael@0 | 22 | private final static String PICKLE_FILENAME = "pickle"; |
michael@0 | 23 | |
michael@0 | 24 | public Account account; |
michael@0 | 25 | public RenamingDelegatingContext context; |
michael@0 | 26 | public AccountManager accountManager; |
michael@0 | 27 | |
michael@0 | 28 | @Override |
michael@0 | 29 | public void setUp() { |
michael@0 | 30 | this.account = null; |
michael@0 | 31 | // Randomize the filename prefix in case we don't clean up correctly. |
michael@0 | 32 | this.context = new RenamingDelegatingContext(getApplicationContext(), FILENAME_PREFIX + |
michael@0 | 33 | Math.random() * 1000001 + "-"); |
michael@0 | 34 | this.accountManager = AccountManager.get(context); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | @Override |
michael@0 | 38 | public void tearDown() { |
michael@0 | 39 | if (this.account != null) { |
michael@0 | 40 | deleteAccount(this, this.accountManager, this.account); |
michael@0 | 41 | this.account = null; |
michael@0 | 42 | } |
michael@0 | 43 | this.context.deleteFile(PICKLE_FILENAME); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | public static void deleteAccount(final InstrumentationTestCase test, |
michael@0 | 47 | final AccountManager accountManager, final Account account) { |
michael@0 | 48 | TestSyncAccounts.deleteAccount(test, accountManager, account); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | private boolean accountsExist() { |
michael@0 | 52 | // Note that we don't use FirefoxAccounts.firefoxAccountsExist because it unpickles. |
michael@0 | 53 | return AccountManager.get(context).getAccountsByType(FxAccountConstants.ACCOUNT_TYPE).length > 0; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | public AndroidFxAccount addDummyAccount() throws Exception { |
michael@0 | 57 | final String email = "iu@fakedomain.io"; |
michael@0 | 58 | final State state = new Separated(email, "uid", false); // State choice is arbitrary. |
michael@0 | 59 | final AndroidFxAccount account = AndroidFxAccount.addAndroidAccount(context, email, |
michael@0 | 60 | "profile", "serverURI", "tokenServerURI", state); |
michael@0 | 61 | assertNotNull(account); |
michael@0 | 62 | assertTrue(accountsExist()); // Sanity check. |
michael@0 | 63 | this.account = account.getAndroidAccount(); // To remove in tearDown() if we throw. |
michael@0 | 64 | return account; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | public void testPickleAndUnpickle() throws Exception { |
michael@0 | 68 | final AndroidFxAccount inputAccount = addDummyAccount(); |
michael@0 | 69 | // Sync is enabled by default so we do a more thorough test by disabling it. |
michael@0 | 70 | inputAccount.disableSyncing(); |
michael@0 | 71 | |
michael@0 | 72 | AccountPickler.pickle(inputAccount, PICKLE_FILENAME); |
michael@0 | 73 | |
michael@0 | 74 | // unpickle adds an account to the AccountManager so delete it first. |
michael@0 | 75 | deleteAccount(this, this.accountManager, inputAccount.getAndroidAccount()); |
michael@0 | 76 | assertFalse(accountsExist()); |
michael@0 | 77 | |
michael@0 | 78 | final AndroidFxAccount unpickledAccount = |
michael@0 | 79 | AccountPickler.unpickle(context, PICKLE_FILENAME); |
michael@0 | 80 | assertNotNull(unpickledAccount); |
michael@0 | 81 | this.account = unpickledAccount.getAndroidAccount(); // To remove in tearDown(). |
michael@0 | 82 | assertAccountsEquals(inputAccount, unpickledAccount); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | public void testDeletePickle() throws Exception { |
michael@0 | 86 | final AndroidFxAccount account = addDummyAccount(); |
michael@0 | 87 | AccountPickler.pickle(account, PICKLE_FILENAME); |
michael@0 | 88 | |
michael@0 | 89 | final String s = Utils.readFile(context, PICKLE_FILENAME); |
michael@0 | 90 | assertNotNull(s); |
michael@0 | 91 | assertTrue(s.length() > 0); |
michael@0 | 92 | |
michael@0 | 93 | AccountPickler.deletePickle(context, PICKLE_FILENAME); |
michael@0 | 94 | org.mozilla.gecko.background.sync.TestAccountPickler.assertFileNotPresent( |
michael@0 | 95 | context, PICKLE_FILENAME); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | private void assertAccountsEquals(final AndroidFxAccount expected, |
michael@0 | 99 | final AndroidFxAccount actual) throws Exception { |
michael@0 | 100 | // TODO: Write and use AndroidFxAccount.equals |
michael@0 | 101 | // TODO: protected. |
michael@0 | 102 | //assertEquals(expected.getAccountVersion(), actual.getAccountVersion()); |
michael@0 | 103 | assertEquals(expected.getProfile(), actual.getProfile()); |
michael@0 | 104 | assertEquals(expected.getAccountServerURI(), actual.getAccountServerURI()); |
michael@0 | 105 | assertEquals(expected.getAudience(), actual.getAudience()); |
michael@0 | 106 | assertEquals(expected.getTokenServerURI(), actual.getTokenServerURI()); |
michael@0 | 107 | assertEquals(expected.getSyncPrefsPath(), actual.getSyncPrefsPath()); |
michael@0 | 108 | assertEquals(expected.isSyncing(), actual.isSyncing()); |
michael@0 | 109 | assertEquals(expected.getEmail(), actual.getEmail()); |
michael@0 | 110 | assertStateEquals(expected.getState(), actual.getState()); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | private void assertStateEquals(final State expected, final State actual) throws Exception { |
michael@0 | 114 | // TODO: Write and use State.equals. Thus, this is only thorough for the State base class. |
michael@0 | 115 | assertEquals(expected.getStateLabel(), actual.getStateLabel()); |
michael@0 | 116 | assertEquals(expected.email, actual.email); |
michael@0 | 117 | assertEquals(expected.uid, actual.uid); |
michael@0 | 118 | assertEquals(expected.verified, actual.verified); |
michael@0 | 119 | } |
michael@0 | 120 | } |