services/sync/tests/unit/test_identity_manager.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_identity_manager.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,284 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +Cu.import("resource://services-sync/constants.js");
     1.8 +Cu.import("resource://services-sync/identity.js");
     1.9 +Cu.import("resource://services-sync/util.js");
    1.10 +
    1.11 +let identity = new IdentityManager();
    1.12 +
    1.13 +function run_test() {
    1.14 +  initTestLogging("Trace");
    1.15 +  Log.repository.getLogger("Sync.Identity").level = Log.Level.Trace;
    1.16 +
    1.17 +  run_next_test();
    1.18 +}
    1.19 +
    1.20 +add_test(function test_username_from_account() {
    1.21 +  _("Ensure usernameFromAccount works properly.");
    1.22 +
    1.23 +  do_check_eq(identity.usernameFromAccount(null), null);
    1.24 +  do_check_eq(identity.usernameFromAccount("user"), "user");
    1.25 +  do_check_eq(identity.usernameFromAccount("User"), "user");
    1.26 +  do_check_eq(identity.usernameFromAccount("john@doe.com"),
    1.27 +                                           "7wohs32cngzuqt466q3ge7indszva4of");
    1.28 +
    1.29 +  run_next_test();
    1.30 +});
    1.31 +
    1.32 +add_test(function test_account_username() {
    1.33 +  _("Ensure the account and username attributes work properly.");
    1.34 +
    1.35 +  _("Verify initial state");
    1.36 +  do_check_eq(Svc.Prefs.get("account"), undefined);
    1.37 +  do_check_eq(Svc.Prefs.get("username"), undefined);
    1.38 +  do_check_eq(identity.account, null);
    1.39 +  do_check_eq(identity.username, null);
    1.40 +
    1.41 +  _("The 'username' attribute is normalized to lower case, updates preferences and identities.");
    1.42 +  identity.username = "TarZan";
    1.43 +  do_check_eq(identity.username, "tarzan");
    1.44 +  do_check_eq(Svc.Prefs.get("username"), "tarzan");
    1.45 +  do_check_eq(identity.username, "tarzan");
    1.46 +
    1.47 +  _("If not set, the 'account attribute' falls back to the username for backwards compatibility.");
    1.48 +  do_check_eq(identity.account, "tarzan");
    1.49 +
    1.50 +  _("Setting 'username' to a non-truthy value resets the pref.");
    1.51 +  identity.username = null;
    1.52 +  do_check_eq(identity.username, null);
    1.53 +  do_check_eq(identity.account, null);
    1.54 +  const default_marker = {};
    1.55 +  do_check_eq(Svc.Prefs.get("username", default_marker), default_marker);
    1.56 +  do_check_eq(identity.username, null);
    1.57 +
    1.58 +  _("The 'account' attribute will set the 'username' if it doesn't contain characters that aren't allowed in the username.");
    1.59 +  identity.account = "johndoe";
    1.60 +  do_check_eq(identity.account, "johndoe");
    1.61 +  do_check_eq(identity.username, "johndoe");
    1.62 +  do_check_eq(Svc.Prefs.get("username"), "johndoe");
    1.63 +  do_check_eq(identity.username, "johndoe");
    1.64 +
    1.65 +  _("If 'account' contains disallowed characters such as @, 'username' will the base32 encoded SHA1 hash of 'account'");
    1.66 +  identity.account = "John@Doe.com";
    1.67 +  do_check_eq(identity.account, "john@doe.com");
    1.68 +  do_check_eq(identity.username, "7wohs32cngzuqt466q3ge7indszva4of");
    1.69 +
    1.70 +  _("Setting 'account' to a non-truthy value resets the pref.");
    1.71 +  identity.account = null;
    1.72 +  do_check_eq(identity.account, null);
    1.73 +  do_check_eq(Svc.Prefs.get("account", default_marker), default_marker);
    1.74 +  do_check_eq(identity.username, null);
    1.75 +  do_check_eq(Svc.Prefs.get("username", default_marker), default_marker);
    1.76 +
    1.77 +  Svc.Prefs.resetBranch("");
    1.78 +  run_next_test();
    1.79 +});
    1.80 +
    1.81 +add_test(function test_basic_password() {
    1.82 +  _("Ensure basic password setting works as expected.");
    1.83 +
    1.84 +  identity.account = null;
    1.85 +  do_check_eq(identity.currentAuthState, LOGIN_FAILED_NO_USERNAME);
    1.86 +  let thrown = false;
    1.87 +  try {
    1.88 +    identity.basicPassword = "foobar";
    1.89 +  } catch (ex) {
    1.90 +    thrown = true;
    1.91 +  }
    1.92 +
    1.93 +  do_check_true(thrown);
    1.94 +  thrown = false;
    1.95 +
    1.96 +  identity.account = "johndoe";
    1.97 +  do_check_eq(identity.currentAuthState, LOGIN_FAILED_NO_PASSWORD);
    1.98 +  identity.basicPassword = "password";
    1.99 +  do_check_eq(identity.basicPassword, "password");
   1.100 +  do_check_eq(identity.currentAuthState, LOGIN_FAILED_NO_PASSPHRASE);
   1.101 +  do_check_true(identity.hasBasicCredentials());
   1.102 +
   1.103 +  identity.account = null;
   1.104 +
   1.105 +  run_next_test();
   1.106 +});
   1.107 +
   1.108 +add_test(function test_basic_password_persistence() {
   1.109 +  _("Ensure credentials are saved and restored to the login manager properly.");
   1.110 +
   1.111 +  // Just in case.
   1.112 +  identity.account = null;
   1.113 +  identity.deleteSyncCredentials();
   1.114 +
   1.115 +  identity.account = "janesmith";
   1.116 +  identity.basicPassword = "ilovejohn";
   1.117 +  identity.persistCredentials();
   1.118 +
   1.119 +  let im1 = new IdentityManager();
   1.120 +  do_check_eq(im1._basicPassword, null);
   1.121 +  do_check_eq(im1.username, "janesmith");
   1.122 +  do_check_eq(im1.basicPassword, "ilovejohn");
   1.123 +
   1.124 +  let im2 = new IdentityManager();
   1.125 +  do_check_eq(im2._basicPassword, null);
   1.126 +
   1.127 +  _("Now remove the password and ensure it is deleted from storage.");
   1.128 +  identity.basicPassword = null;
   1.129 +  identity.persistCredentials(); // This should nuke from storage.
   1.130 +  do_check_eq(im2.basicPassword, null);
   1.131 +
   1.132 +  _("Ensure that retrieving an unset but unpersisted removal returns null.");
   1.133 +  identity.account = "janesmith";
   1.134 +  identity.basicPassword = "myotherpassword";
   1.135 +  identity.persistCredentials();
   1.136 +
   1.137 +  identity.basicPassword = null;
   1.138 +  do_check_eq(identity.basicPassword, null);
   1.139 +
   1.140 +  // Reset for next test.
   1.141 +  identity.account = null;
   1.142 +  identity.persistCredentials();
   1.143 +
   1.144 +  run_next_test();
   1.145 +});
   1.146 +
   1.147 +add_test(function test_sync_key() {
   1.148 +  _("Ensure Sync Key works as advertised.");
   1.149 +
   1.150 +  _("Ensure setting a Sync Key before an account throws.");
   1.151 +  let thrown = false;
   1.152 +  try {
   1.153 +    identity.syncKey = "blahblah";
   1.154 +  } catch (ex) {
   1.155 +    thrown = true;
   1.156 +  }
   1.157 +  do_check_true(thrown);
   1.158 +  thrown = false;
   1.159 +
   1.160 +  identity.account = "johnsmith";
   1.161 +  identity.basicPassword = "johnsmithpw";
   1.162 +
   1.163 +  do_check_eq(identity.syncKey, null);
   1.164 +  do_check_eq(identity.syncKeyBundle, null);
   1.165 +
   1.166 +  _("An invalid Sync Key is silently accepted for historical reasons.");
   1.167 +  identity.syncKey = "synckey";
   1.168 +  do_check_eq(identity.syncKey, "synckey");
   1.169 +
   1.170 +  _("But the SyncKeyBundle should not be created from bad keys.");
   1.171 +  do_check_eq(identity.syncKeyBundle, null);
   1.172 +
   1.173 +  let syncKey = Utils.generatePassphrase();
   1.174 +  identity.syncKey = syncKey;
   1.175 +  do_check_eq(identity.syncKey, syncKey);
   1.176 +  do_check_neq(identity.syncKeyBundle, null);
   1.177 +
   1.178 +  let im = new IdentityManager();
   1.179 +  im.account = "pseudojohn";
   1.180 +  do_check_eq(im.syncKey, null);
   1.181 +  do_check_eq(im.syncKeyBundle, null);
   1.182 +
   1.183 +  identity.account = null;
   1.184 +
   1.185 +  run_next_test();
   1.186 +});
   1.187 +
   1.188 +add_test(function test_sync_key_changes() {
   1.189 +  _("Ensure changes to Sync Key have appropriate side-effects.");
   1.190 +
   1.191 +  let im = new IdentityManager();
   1.192 +  let sk1 = Utils.generatePassphrase();
   1.193 +  let sk2 = Utils.generatePassphrase();
   1.194 +
   1.195 +  im.account = "johndoe";
   1.196 +  do_check_eq(im.syncKey, null);
   1.197 +  do_check_eq(im.syncKeyBundle, null);
   1.198 +
   1.199 +  im.syncKey = sk1;
   1.200 +  do_check_neq(im.syncKeyBundle, null);
   1.201 +
   1.202 +  let ek1 = im.syncKeyBundle.encryptionKeyB64;
   1.203 +  let hk1 = im.syncKeyBundle.hmacKeyB64;
   1.204 +
   1.205 +  // Change the Sync Key and ensure the Sync Key Bundle is updated.
   1.206 +  im.syncKey = sk2;
   1.207 +  let ek2 = im.syncKeyBundle.encryptionKeyB64;
   1.208 +  let hk2 = im.syncKeyBundle.hmacKeyB64;
   1.209 +
   1.210 +  do_check_neq(ek1, ek2);
   1.211 +  do_check_neq(hk1, hk2);
   1.212 +
   1.213 +  im.account = null;
   1.214 +
   1.215 +  run_next_test();
   1.216 +});
   1.217 +
   1.218 +add_test(function test_current_auth_state() {
   1.219 +  _("Ensure current auth state is reported properly.");
   1.220 +
   1.221 +  let im = new IdentityManager();
   1.222 +  do_check_eq(im.currentAuthState, LOGIN_FAILED_NO_USERNAME);
   1.223 +
   1.224 +  im.account = "johndoe";
   1.225 +  do_check_eq(im.currentAuthState, LOGIN_FAILED_NO_PASSWORD);
   1.226 +
   1.227 +  im.basicPassword = "ilovejane";
   1.228 +  do_check_eq(im.currentAuthState, LOGIN_FAILED_NO_PASSPHRASE);
   1.229 +
   1.230 +  im.syncKey = "foobar";
   1.231 +  do_check_eq(im.currentAuthState, LOGIN_FAILED_INVALID_PASSPHRASE);
   1.232 +
   1.233 +  im.syncKey = null;
   1.234 +  do_check_eq(im.currentAuthState, LOGIN_FAILED_NO_PASSPHRASE);
   1.235 +
   1.236 +  im.syncKey = Utils.generatePassphrase();
   1.237 +  do_check_eq(im.currentAuthState, STATUS_OK);
   1.238 +
   1.239 +  im.account = null;
   1.240 +
   1.241 +  run_next_test();
   1.242 +});
   1.243 +
   1.244 +add_test(function test_sync_key_persistence() {
   1.245 +  _("Ensure Sync Key persistence works as expected.");
   1.246 +
   1.247 +  identity.account = "pseudojohn";
   1.248 +  identity.password = "supersecret";
   1.249 +
   1.250 +  let syncKey = Utils.generatePassphrase();
   1.251 +  identity.syncKey = syncKey;
   1.252 +
   1.253 +  identity.persistCredentials();
   1.254 +
   1.255 +  let im = new IdentityManager();
   1.256 +  im.account = "pseudojohn";
   1.257 +  do_check_eq(im.syncKey, syncKey);
   1.258 +  do_check_neq(im.syncKeyBundle, null);
   1.259 +
   1.260 +  let kb1 = identity.syncKeyBundle;
   1.261 +  let kb2 = im.syncKeyBundle;
   1.262 +
   1.263 +  do_check_eq(kb1.encryptionKeyB64, kb2.encryptionKeyB64);
   1.264 +  do_check_eq(kb1.hmacKeyB64, kb2.hmacKeyB64);
   1.265 +
   1.266 +  identity.account = null;
   1.267 +  identity.persistCredentials();
   1.268 +
   1.269 +  let im2 = new IdentityManager();
   1.270 +  im2.account = "pseudojohn";
   1.271 +  do_check_eq(im2.syncKey, null);
   1.272 +
   1.273 +  im2.account = null;
   1.274 +
   1.275 +  _("Ensure deleted but not persisted value is retrieved.");
   1.276 +  identity.account = "someoneelse";
   1.277 +  identity.syncKey = Utils.generatePassphrase();
   1.278 +  identity.persistCredentials();
   1.279 +  identity.syncKey = null;
   1.280 +  do_check_eq(identity.syncKey, null);
   1.281 +
   1.282 +  // Clean up.
   1.283 +  identity.account = null;
   1.284 +  identity.persistCredentials();
   1.285 +
   1.286 +  run_next_test();
   1.287 +});

mercurial