|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 Cu.import("resource://services-sync/constants.js"); |
|
5 Cu.import("resource://services-sync/identity.js"); |
|
6 Cu.import("resource://services-sync/util.js"); |
|
7 |
|
8 let identity = new IdentityManager(); |
|
9 |
|
10 function run_test() { |
|
11 initTestLogging("Trace"); |
|
12 Log.repository.getLogger("Sync.Identity").level = Log.Level.Trace; |
|
13 |
|
14 run_next_test(); |
|
15 } |
|
16 |
|
17 add_test(function test_username_from_account() { |
|
18 _("Ensure usernameFromAccount works properly."); |
|
19 |
|
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"); |
|
25 |
|
26 run_next_test(); |
|
27 }); |
|
28 |
|
29 add_test(function test_account_username() { |
|
30 _("Ensure the account and username attributes work properly."); |
|
31 |
|
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); |
|
37 |
|
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"); |
|
43 |
|
44 _("If not set, the 'account attribute' falls back to the username for backwards compatibility."); |
|
45 do_check_eq(identity.account, "tarzan"); |
|
46 |
|
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); |
|
54 |
|
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"); |
|
61 |
|
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"); |
|
66 |
|
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); |
|
73 |
|
74 Svc.Prefs.resetBranch(""); |
|
75 run_next_test(); |
|
76 }); |
|
77 |
|
78 add_test(function test_basic_password() { |
|
79 _("Ensure basic password setting works as expected."); |
|
80 |
|
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 } |
|
89 |
|
90 do_check_true(thrown); |
|
91 thrown = false; |
|
92 |
|
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()); |
|
99 |
|
100 identity.account = null; |
|
101 |
|
102 run_next_test(); |
|
103 }); |
|
104 |
|
105 add_test(function test_basic_password_persistence() { |
|
106 _("Ensure credentials are saved and restored to the login manager properly."); |
|
107 |
|
108 // Just in case. |
|
109 identity.account = null; |
|
110 identity.deleteSyncCredentials(); |
|
111 |
|
112 identity.account = "janesmith"; |
|
113 identity.basicPassword = "ilovejohn"; |
|
114 identity.persistCredentials(); |
|
115 |
|
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"); |
|
120 |
|
121 let im2 = new IdentityManager(); |
|
122 do_check_eq(im2._basicPassword, null); |
|
123 |
|
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); |
|
128 |
|
129 _("Ensure that retrieving an unset but unpersisted removal returns null."); |
|
130 identity.account = "janesmith"; |
|
131 identity.basicPassword = "myotherpassword"; |
|
132 identity.persistCredentials(); |
|
133 |
|
134 identity.basicPassword = null; |
|
135 do_check_eq(identity.basicPassword, null); |
|
136 |
|
137 // Reset for next test. |
|
138 identity.account = null; |
|
139 identity.persistCredentials(); |
|
140 |
|
141 run_next_test(); |
|
142 }); |
|
143 |
|
144 add_test(function test_sync_key() { |
|
145 _("Ensure Sync Key works as advertised."); |
|
146 |
|
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; |
|
156 |
|
157 identity.account = "johnsmith"; |
|
158 identity.basicPassword = "johnsmithpw"; |
|
159 |
|
160 do_check_eq(identity.syncKey, null); |
|
161 do_check_eq(identity.syncKeyBundle, null); |
|
162 |
|
163 _("An invalid Sync Key is silently accepted for historical reasons."); |
|
164 identity.syncKey = "synckey"; |
|
165 do_check_eq(identity.syncKey, "synckey"); |
|
166 |
|
167 _("But the SyncKeyBundle should not be created from bad keys."); |
|
168 do_check_eq(identity.syncKeyBundle, null); |
|
169 |
|
170 let syncKey = Utils.generatePassphrase(); |
|
171 identity.syncKey = syncKey; |
|
172 do_check_eq(identity.syncKey, syncKey); |
|
173 do_check_neq(identity.syncKeyBundle, null); |
|
174 |
|
175 let im = new IdentityManager(); |
|
176 im.account = "pseudojohn"; |
|
177 do_check_eq(im.syncKey, null); |
|
178 do_check_eq(im.syncKeyBundle, null); |
|
179 |
|
180 identity.account = null; |
|
181 |
|
182 run_next_test(); |
|
183 }); |
|
184 |
|
185 add_test(function test_sync_key_changes() { |
|
186 _("Ensure changes to Sync Key have appropriate side-effects."); |
|
187 |
|
188 let im = new IdentityManager(); |
|
189 let sk1 = Utils.generatePassphrase(); |
|
190 let sk2 = Utils.generatePassphrase(); |
|
191 |
|
192 im.account = "johndoe"; |
|
193 do_check_eq(im.syncKey, null); |
|
194 do_check_eq(im.syncKeyBundle, null); |
|
195 |
|
196 im.syncKey = sk1; |
|
197 do_check_neq(im.syncKeyBundle, null); |
|
198 |
|
199 let ek1 = im.syncKeyBundle.encryptionKeyB64; |
|
200 let hk1 = im.syncKeyBundle.hmacKeyB64; |
|
201 |
|
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; |
|
206 |
|
207 do_check_neq(ek1, ek2); |
|
208 do_check_neq(hk1, hk2); |
|
209 |
|
210 im.account = null; |
|
211 |
|
212 run_next_test(); |
|
213 }); |
|
214 |
|
215 add_test(function test_current_auth_state() { |
|
216 _("Ensure current auth state is reported properly."); |
|
217 |
|
218 let im = new IdentityManager(); |
|
219 do_check_eq(im.currentAuthState, LOGIN_FAILED_NO_USERNAME); |
|
220 |
|
221 im.account = "johndoe"; |
|
222 do_check_eq(im.currentAuthState, LOGIN_FAILED_NO_PASSWORD); |
|
223 |
|
224 im.basicPassword = "ilovejane"; |
|
225 do_check_eq(im.currentAuthState, LOGIN_FAILED_NO_PASSPHRASE); |
|
226 |
|
227 im.syncKey = "foobar"; |
|
228 do_check_eq(im.currentAuthState, LOGIN_FAILED_INVALID_PASSPHRASE); |
|
229 |
|
230 im.syncKey = null; |
|
231 do_check_eq(im.currentAuthState, LOGIN_FAILED_NO_PASSPHRASE); |
|
232 |
|
233 im.syncKey = Utils.generatePassphrase(); |
|
234 do_check_eq(im.currentAuthState, STATUS_OK); |
|
235 |
|
236 im.account = null; |
|
237 |
|
238 run_next_test(); |
|
239 }); |
|
240 |
|
241 add_test(function test_sync_key_persistence() { |
|
242 _("Ensure Sync Key persistence works as expected."); |
|
243 |
|
244 identity.account = "pseudojohn"; |
|
245 identity.password = "supersecret"; |
|
246 |
|
247 let syncKey = Utils.generatePassphrase(); |
|
248 identity.syncKey = syncKey; |
|
249 |
|
250 identity.persistCredentials(); |
|
251 |
|
252 let im = new IdentityManager(); |
|
253 im.account = "pseudojohn"; |
|
254 do_check_eq(im.syncKey, syncKey); |
|
255 do_check_neq(im.syncKeyBundle, null); |
|
256 |
|
257 let kb1 = identity.syncKeyBundle; |
|
258 let kb2 = im.syncKeyBundle; |
|
259 |
|
260 do_check_eq(kb1.encryptionKeyB64, kb2.encryptionKeyB64); |
|
261 do_check_eq(kb1.hmacKeyB64, kb2.hmacKeyB64); |
|
262 |
|
263 identity.account = null; |
|
264 identity.persistCredentials(); |
|
265 |
|
266 let im2 = new IdentityManager(); |
|
267 im2.account = "pseudojohn"; |
|
268 do_check_eq(im2.syncKey, null); |
|
269 |
|
270 im2.account = null; |
|
271 |
|
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); |
|
278 |
|
279 // Clean up. |
|
280 identity.account = null; |
|
281 identity.persistCredentials(); |
|
282 |
|
283 run_next_test(); |
|
284 }); |