Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
4 Cu.import("resource://services-sync/constants.js");
5 Cu.import("resource://services-sync/identity.js");
6 Cu.import("resource://services-sync/util.js");
8 let identity = new IdentityManager();
10 function run_test() {
11 initTestLogging("Trace");
12 Log.repository.getLogger("Sync.Identity").level = Log.Level.Trace;
14 run_next_test();
15 }
17 add_test(function test_username_from_account() {
18 _("Ensure usernameFromAccount works properly.");
20 do_check_eq(identity.usernameFromAccount(null), null);
21 do_check_eq(identity.usernameFromAccount("user"), "user");
22 do_check_eq(identity.usernameFromAccount("User"), "user");
23 do_check_eq(identity.usernameFromAccount("john@doe.com"),
24 "7wohs32cngzuqt466q3ge7indszva4of");
26 run_next_test();
27 });
29 add_test(function test_account_username() {
30 _("Ensure the account and username attributes work properly.");
32 _("Verify initial state");
33 do_check_eq(Svc.Prefs.get("account"), undefined);
34 do_check_eq(Svc.Prefs.get("username"), undefined);
35 do_check_eq(identity.account, null);
36 do_check_eq(identity.username, null);
38 _("The 'username' attribute is normalized to lower case, updates preferences and identities.");
39 identity.username = "TarZan";
40 do_check_eq(identity.username, "tarzan");
41 do_check_eq(Svc.Prefs.get("username"), "tarzan");
42 do_check_eq(identity.username, "tarzan");
44 _("If not set, the 'account attribute' falls back to the username for backwards compatibility.");
45 do_check_eq(identity.account, "tarzan");
47 _("Setting 'username' to a non-truthy value resets the pref.");
48 identity.username = null;
49 do_check_eq(identity.username, null);
50 do_check_eq(identity.account, null);
51 const default_marker = {};
52 do_check_eq(Svc.Prefs.get("username", default_marker), default_marker);
53 do_check_eq(identity.username, null);
55 _("The 'account' attribute will set the 'username' if it doesn't contain characters that aren't allowed in the username.");
56 identity.account = "johndoe";
57 do_check_eq(identity.account, "johndoe");
58 do_check_eq(identity.username, "johndoe");
59 do_check_eq(Svc.Prefs.get("username"), "johndoe");
60 do_check_eq(identity.username, "johndoe");
62 _("If 'account' contains disallowed characters such as @, 'username' will the base32 encoded SHA1 hash of 'account'");
63 identity.account = "John@Doe.com";
64 do_check_eq(identity.account, "john@doe.com");
65 do_check_eq(identity.username, "7wohs32cngzuqt466q3ge7indszva4of");
67 _("Setting 'account' to a non-truthy value resets the pref.");
68 identity.account = null;
69 do_check_eq(identity.account, null);
70 do_check_eq(Svc.Prefs.get("account", default_marker), default_marker);
71 do_check_eq(identity.username, null);
72 do_check_eq(Svc.Prefs.get("username", default_marker), default_marker);
74 Svc.Prefs.resetBranch("");
75 run_next_test();
76 });
78 add_test(function test_basic_password() {
79 _("Ensure basic password setting works as expected.");
81 identity.account = null;
82 do_check_eq(identity.currentAuthState, LOGIN_FAILED_NO_USERNAME);
83 let thrown = false;
84 try {
85 identity.basicPassword = "foobar";
86 } catch (ex) {
87 thrown = true;
88 }
90 do_check_true(thrown);
91 thrown = false;
93 identity.account = "johndoe";
94 do_check_eq(identity.currentAuthState, LOGIN_FAILED_NO_PASSWORD);
95 identity.basicPassword = "password";
96 do_check_eq(identity.basicPassword, "password");
97 do_check_eq(identity.currentAuthState, LOGIN_FAILED_NO_PASSPHRASE);
98 do_check_true(identity.hasBasicCredentials());
100 identity.account = null;
102 run_next_test();
103 });
105 add_test(function test_basic_password_persistence() {
106 _("Ensure credentials are saved and restored to the login manager properly.");
108 // Just in case.
109 identity.account = null;
110 identity.deleteSyncCredentials();
112 identity.account = "janesmith";
113 identity.basicPassword = "ilovejohn";
114 identity.persistCredentials();
116 let im1 = new IdentityManager();
117 do_check_eq(im1._basicPassword, null);
118 do_check_eq(im1.username, "janesmith");
119 do_check_eq(im1.basicPassword, "ilovejohn");
121 let im2 = new IdentityManager();
122 do_check_eq(im2._basicPassword, null);
124 _("Now remove the password and ensure it is deleted from storage.");
125 identity.basicPassword = null;
126 identity.persistCredentials(); // This should nuke from storage.
127 do_check_eq(im2.basicPassword, null);
129 _("Ensure that retrieving an unset but unpersisted removal returns null.");
130 identity.account = "janesmith";
131 identity.basicPassword = "myotherpassword";
132 identity.persistCredentials();
134 identity.basicPassword = null;
135 do_check_eq(identity.basicPassword, null);
137 // Reset for next test.
138 identity.account = null;
139 identity.persistCredentials();
141 run_next_test();
142 });
144 add_test(function test_sync_key() {
145 _("Ensure Sync Key works as advertised.");
147 _("Ensure setting a Sync Key before an account throws.");
148 let thrown = false;
149 try {
150 identity.syncKey = "blahblah";
151 } catch (ex) {
152 thrown = true;
153 }
154 do_check_true(thrown);
155 thrown = false;
157 identity.account = "johnsmith";
158 identity.basicPassword = "johnsmithpw";
160 do_check_eq(identity.syncKey, null);
161 do_check_eq(identity.syncKeyBundle, null);
163 _("An invalid Sync Key is silently accepted for historical reasons.");
164 identity.syncKey = "synckey";
165 do_check_eq(identity.syncKey, "synckey");
167 _("But the SyncKeyBundle should not be created from bad keys.");
168 do_check_eq(identity.syncKeyBundle, null);
170 let syncKey = Utils.generatePassphrase();
171 identity.syncKey = syncKey;
172 do_check_eq(identity.syncKey, syncKey);
173 do_check_neq(identity.syncKeyBundle, null);
175 let im = new IdentityManager();
176 im.account = "pseudojohn";
177 do_check_eq(im.syncKey, null);
178 do_check_eq(im.syncKeyBundle, null);
180 identity.account = null;
182 run_next_test();
183 });
185 add_test(function test_sync_key_changes() {
186 _("Ensure changes to Sync Key have appropriate side-effects.");
188 let im = new IdentityManager();
189 let sk1 = Utils.generatePassphrase();
190 let sk2 = Utils.generatePassphrase();
192 im.account = "johndoe";
193 do_check_eq(im.syncKey, null);
194 do_check_eq(im.syncKeyBundle, null);
196 im.syncKey = sk1;
197 do_check_neq(im.syncKeyBundle, null);
199 let ek1 = im.syncKeyBundle.encryptionKeyB64;
200 let hk1 = im.syncKeyBundle.hmacKeyB64;
202 // Change the Sync Key and ensure the Sync Key Bundle is updated.
203 im.syncKey = sk2;
204 let ek2 = im.syncKeyBundle.encryptionKeyB64;
205 let hk2 = im.syncKeyBundle.hmacKeyB64;
207 do_check_neq(ek1, ek2);
208 do_check_neq(hk1, hk2);
210 im.account = null;
212 run_next_test();
213 });
215 add_test(function test_current_auth_state() {
216 _("Ensure current auth state is reported properly.");
218 let im = new IdentityManager();
219 do_check_eq(im.currentAuthState, LOGIN_FAILED_NO_USERNAME);
221 im.account = "johndoe";
222 do_check_eq(im.currentAuthState, LOGIN_FAILED_NO_PASSWORD);
224 im.basicPassword = "ilovejane";
225 do_check_eq(im.currentAuthState, LOGIN_FAILED_NO_PASSPHRASE);
227 im.syncKey = "foobar";
228 do_check_eq(im.currentAuthState, LOGIN_FAILED_INVALID_PASSPHRASE);
230 im.syncKey = null;
231 do_check_eq(im.currentAuthState, LOGIN_FAILED_NO_PASSPHRASE);
233 im.syncKey = Utils.generatePassphrase();
234 do_check_eq(im.currentAuthState, STATUS_OK);
236 im.account = null;
238 run_next_test();
239 });
241 add_test(function test_sync_key_persistence() {
242 _("Ensure Sync Key persistence works as expected.");
244 identity.account = "pseudojohn";
245 identity.password = "supersecret";
247 let syncKey = Utils.generatePassphrase();
248 identity.syncKey = syncKey;
250 identity.persistCredentials();
252 let im = new IdentityManager();
253 im.account = "pseudojohn";
254 do_check_eq(im.syncKey, syncKey);
255 do_check_neq(im.syncKeyBundle, null);
257 let kb1 = identity.syncKeyBundle;
258 let kb2 = im.syncKeyBundle;
260 do_check_eq(kb1.encryptionKeyB64, kb2.encryptionKeyB64);
261 do_check_eq(kb1.hmacKeyB64, kb2.hmacKeyB64);
263 identity.account = null;
264 identity.persistCredentials();
266 let im2 = new IdentityManager();
267 im2.account = "pseudojohn";
268 do_check_eq(im2.syncKey, null);
270 im2.account = null;
272 _("Ensure deleted but not persisted value is retrieved.");
273 identity.account = "someoneelse";
274 identity.syncKey = Utils.generatePassphrase();
275 identity.persistCredentials();
276 identity.syncKey = null;
277 do_check_eq(identity.syncKey, null);
279 // Clean up.
280 identity.account = null;
281 identity.persistCredentials();
283 run_next_test();
284 });