Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
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 | Cu.import("resource://gre/modules/FxAccounts.jsm"); |
michael@0 | 5 | Cu.import("resource://services-sync/browserid_identity.js"); |
michael@0 | 6 | Cu.import("resource://services-sync/rest.js"); |
michael@0 | 7 | Cu.import("resource://services-sync/util.js"); |
michael@0 | 8 | Cu.import("resource://services-common/utils.js"); |
michael@0 | 9 | Cu.import("resource://services-crypto/utils.js"); |
michael@0 | 10 | Cu.import("resource://testing-common/services/sync/utils.js"); |
michael@0 | 11 | Cu.import("resource://testing-common/services/sync/fxa_utils.js"); |
michael@0 | 12 | Cu.import("resource://services-common/hawkclient.js"); |
michael@0 | 13 | Cu.import("resource://gre/modules/FxAccounts.jsm"); |
michael@0 | 14 | Cu.import("resource://gre/modules/FxAccountsClient.jsm"); |
michael@0 | 15 | Cu.import("resource://gre/modules/FxAccountsCommon.js"); |
michael@0 | 16 | Cu.import("resource://services-sync/service.js"); |
michael@0 | 17 | Cu.import("resource://services-sync/status.js"); |
michael@0 | 18 | Cu.import("resource://services-sync/constants.js"); |
michael@0 | 19 | |
michael@0 | 20 | const SECOND_MS = 1000; |
michael@0 | 21 | const MINUTE_MS = SECOND_MS * 60; |
michael@0 | 22 | const HOUR_MS = MINUTE_MS * 60; |
michael@0 | 23 | |
michael@0 | 24 | let identityConfig = makeIdentityConfig(); |
michael@0 | 25 | let browseridManager = new BrowserIDManager(); |
michael@0 | 26 | configureFxAccountIdentity(browseridManager, identityConfig); |
michael@0 | 27 | |
michael@0 | 28 | /** |
michael@0 | 29 | * Mock client clock and skew vs server in FxAccounts signed-in user module and |
michael@0 | 30 | * API client. browserid_identity.js queries these values to construct HAWK |
michael@0 | 31 | * headers. We will use this to test clock skew compensation in these headers |
michael@0 | 32 | * below. |
michael@0 | 33 | */ |
michael@0 | 34 | let MockFxAccountsClient = function() { |
michael@0 | 35 | FxAccountsClient.apply(this); |
michael@0 | 36 | }; |
michael@0 | 37 | MockFxAccountsClient.prototype = { |
michael@0 | 38 | __proto__: FxAccountsClient.prototype |
michael@0 | 39 | }; |
michael@0 | 40 | |
michael@0 | 41 | function MockFxAccounts() { |
michael@0 | 42 | let fxa = new FxAccounts({ |
michael@0 | 43 | _now_is: Date.now(), |
michael@0 | 44 | |
michael@0 | 45 | now: function () { |
michael@0 | 46 | return this._now_is; |
michael@0 | 47 | }, |
michael@0 | 48 | |
michael@0 | 49 | fxAccountsClient: new MockFxAccountsClient() |
michael@0 | 50 | }); |
michael@0 | 51 | fxa.internal.currentAccountState.getCertificate = function(data, keyPair, mustBeValidUntil) { |
michael@0 | 52 | this.cert = { |
michael@0 | 53 | validUntil: fxa.internal.now() + CERT_LIFETIME, |
michael@0 | 54 | cert: "certificate", |
michael@0 | 55 | }; |
michael@0 | 56 | return Promise.resolve(this.cert.cert); |
michael@0 | 57 | }; |
michael@0 | 58 | return fxa; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | function run_test() { |
michael@0 | 62 | initTestLogging("Trace"); |
michael@0 | 63 | Log.repository.getLogger("Sync.Identity").level = Log.Level.Trace; |
michael@0 | 64 | Log.repository.getLogger("Sync.BrowserIDManager").level = Log.Level.Trace; |
michael@0 | 65 | run_next_test(); |
michael@0 | 66 | }; |
michael@0 | 67 | |
michael@0 | 68 | add_test(function test_initial_state() { |
michael@0 | 69 | _("Verify initial state"); |
michael@0 | 70 | do_check_false(!!browseridManager._token); |
michael@0 | 71 | do_check_false(browseridManager.hasValidToken()); |
michael@0 | 72 | run_next_test(); |
michael@0 | 73 | } |
michael@0 | 74 | ); |
michael@0 | 75 | |
michael@0 | 76 | add_task(function test_initialializeWithCurrentIdentity() { |
michael@0 | 77 | _("Verify start after initializeWithCurrentIdentity"); |
michael@0 | 78 | browseridManager.initializeWithCurrentIdentity(); |
michael@0 | 79 | yield browseridManager.whenReadyToAuthenticate.promise; |
michael@0 | 80 | do_check_true(!!browseridManager._token); |
michael@0 | 81 | do_check_true(browseridManager.hasValidToken()); |
michael@0 | 82 | do_check_eq(browseridManager.account, identityConfig.fxaccount.user.email); |
michael@0 | 83 | } |
michael@0 | 84 | ); |
michael@0 | 85 | |
michael@0 | 86 | |
michael@0 | 87 | add_test(function test_getResourceAuthenticator() { |
michael@0 | 88 | _("BrowserIDManager supplies a Resource Authenticator callback which returns a Hawk header."); |
michael@0 | 89 | let authenticator = browseridManager.getResourceAuthenticator(); |
michael@0 | 90 | do_check_true(!!authenticator); |
michael@0 | 91 | let req = {uri: CommonUtils.makeURI( |
michael@0 | 92 | "https://example.net/somewhere/over/the/rainbow"), |
michael@0 | 93 | method: 'GET'}; |
michael@0 | 94 | let output = authenticator(req, 'GET'); |
michael@0 | 95 | do_check_true('headers' in output); |
michael@0 | 96 | do_check_true('authorization' in output.headers); |
michael@0 | 97 | do_check_true(output.headers.authorization.startsWith('Hawk')); |
michael@0 | 98 | _("Expected internal state after successful call."); |
michael@0 | 99 | do_check_eq(browseridManager._token.uid, identityConfig.fxaccount.token.uid); |
michael@0 | 100 | run_next_test(); |
michael@0 | 101 | } |
michael@0 | 102 | ); |
michael@0 | 103 | |
michael@0 | 104 | add_test(function test_getRESTRequestAuthenticator() { |
michael@0 | 105 | _("BrowserIDManager supplies a REST Request Authenticator callback which sets a Hawk header on a request object."); |
michael@0 | 106 | let request = new SyncStorageRequest( |
michael@0 | 107 | "https://example.net/somewhere/over/the/rainbow"); |
michael@0 | 108 | let authenticator = browseridManager.getRESTRequestAuthenticator(); |
michael@0 | 109 | do_check_true(!!authenticator); |
michael@0 | 110 | let output = authenticator(request, 'GET'); |
michael@0 | 111 | do_check_eq(request.uri, output.uri); |
michael@0 | 112 | do_check_true(output._headers.authorization.startsWith('Hawk')); |
michael@0 | 113 | do_check_true(output._headers.authorization.contains('nonce')); |
michael@0 | 114 | do_check_true(browseridManager.hasValidToken()); |
michael@0 | 115 | run_next_test(); |
michael@0 | 116 | } |
michael@0 | 117 | ); |
michael@0 | 118 | |
michael@0 | 119 | add_test(function test_resourceAuthenticatorSkew() { |
michael@0 | 120 | _("BrowserIDManager Resource Authenticator compensates for clock skew in Hawk header."); |
michael@0 | 121 | |
michael@0 | 122 | // Clock is skewed 12 hours into the future |
michael@0 | 123 | // We pick a date in the past so we don't risk concealing bugs in code that |
michael@0 | 124 | // uses new Date() instead of our given date. |
michael@0 | 125 | let now = new Date("Fri Apr 09 2004 00:00:00 GMT-0700").valueOf() + 12 * HOUR_MS; |
michael@0 | 126 | let browseridManager = new BrowserIDManager(); |
michael@0 | 127 | let hawkClient = new HawkClient("https://example.net/v1", "/foo"); |
michael@0 | 128 | |
michael@0 | 129 | // mock fxa hawk client skew |
michael@0 | 130 | hawkClient.now = function() { |
michael@0 | 131 | dump("mocked client now: " + now + '\n'); |
michael@0 | 132 | return now; |
michael@0 | 133 | } |
michael@0 | 134 | // Imagine there's already been one fxa request and the hawk client has |
michael@0 | 135 | // already detected skew vs the fxa auth server. |
michael@0 | 136 | let localtimeOffsetMsec = -1 * 12 * HOUR_MS; |
michael@0 | 137 | hawkClient._localtimeOffsetMsec = localtimeOffsetMsec; |
michael@0 | 138 | |
michael@0 | 139 | let fxaClient = new MockFxAccountsClient(); |
michael@0 | 140 | fxaClient.hawk = hawkClient; |
michael@0 | 141 | |
michael@0 | 142 | // Sanity check |
michael@0 | 143 | do_check_eq(hawkClient.now(), now); |
michael@0 | 144 | do_check_eq(hawkClient.localtimeOffsetMsec, localtimeOffsetMsec); |
michael@0 | 145 | |
michael@0 | 146 | // Properly picked up by the client |
michael@0 | 147 | do_check_eq(fxaClient.now(), now); |
michael@0 | 148 | do_check_eq(fxaClient.localtimeOffsetMsec, localtimeOffsetMsec); |
michael@0 | 149 | |
michael@0 | 150 | let fxa = new MockFxAccounts(); |
michael@0 | 151 | fxa.internal._now_is = now; |
michael@0 | 152 | fxa.internal.fxAccountsClient = fxaClient; |
michael@0 | 153 | |
michael@0 | 154 | // Picked up by the signed-in user module |
michael@0 | 155 | do_check_eq(fxa.internal.now(), now); |
michael@0 | 156 | do_check_eq(fxa.internal.localtimeOffsetMsec, localtimeOffsetMsec); |
michael@0 | 157 | |
michael@0 | 158 | do_check_eq(fxa.now(), now); |
michael@0 | 159 | do_check_eq(fxa.localtimeOffsetMsec, localtimeOffsetMsec); |
michael@0 | 160 | |
michael@0 | 161 | // Mocks within mocks... |
michael@0 | 162 | configureFxAccountIdentity(browseridManager, identityConfig); |
michael@0 | 163 | |
michael@0 | 164 | // Ensure the new FxAccounts mock has a signed-in user. |
michael@0 | 165 | fxa.internal.currentAccountState.signedInUser = browseridManager._fxaService.internal.currentAccountState.signedInUser; |
michael@0 | 166 | |
michael@0 | 167 | browseridManager._fxaService = fxa; |
michael@0 | 168 | |
michael@0 | 169 | do_check_eq(browseridManager._fxaService.internal.now(), now); |
michael@0 | 170 | do_check_eq(browseridManager._fxaService.internal.localtimeOffsetMsec, |
michael@0 | 171 | localtimeOffsetMsec); |
michael@0 | 172 | |
michael@0 | 173 | do_check_eq(browseridManager._fxaService.now(), now); |
michael@0 | 174 | do_check_eq(browseridManager._fxaService.localtimeOffsetMsec, |
michael@0 | 175 | localtimeOffsetMsec); |
michael@0 | 176 | |
michael@0 | 177 | let request = new SyncStorageRequest("https://example.net/i/like/pie/"); |
michael@0 | 178 | let authenticator = browseridManager.getResourceAuthenticator(); |
michael@0 | 179 | let output = authenticator(request, 'GET'); |
michael@0 | 180 | dump("output" + JSON.stringify(output)); |
michael@0 | 181 | let authHeader = output.headers.authorization; |
michael@0 | 182 | do_check_true(authHeader.startsWith('Hawk')); |
michael@0 | 183 | |
michael@0 | 184 | // Skew correction is applied in the header and we're within the two-minute |
michael@0 | 185 | // window. |
michael@0 | 186 | do_check_eq(getTimestamp(authHeader), now - 12 * HOUR_MS); |
michael@0 | 187 | do_check_true( |
michael@0 | 188 | (getTimestampDelta(authHeader, now) - 12 * HOUR_MS) < 2 * MINUTE_MS); |
michael@0 | 189 | |
michael@0 | 190 | run_next_test(); |
michael@0 | 191 | }); |
michael@0 | 192 | |
michael@0 | 193 | add_test(function test_RESTResourceAuthenticatorSkew() { |
michael@0 | 194 | _("BrowserIDManager REST Resource Authenticator compensates for clock skew in Hawk header."); |
michael@0 | 195 | |
michael@0 | 196 | // Clock is skewed 12 hours into the future from our arbitary date |
michael@0 | 197 | let now = new Date("Fri Apr 09 2004 00:00:00 GMT-0700").valueOf() + 12 * HOUR_MS; |
michael@0 | 198 | let browseridManager = new BrowserIDManager(); |
michael@0 | 199 | let hawkClient = new HawkClient("https://example.net/v1", "/foo"); |
michael@0 | 200 | |
michael@0 | 201 | // mock fxa hawk client skew |
michael@0 | 202 | hawkClient.now = function() { |
michael@0 | 203 | return now; |
michael@0 | 204 | } |
michael@0 | 205 | // Imagine there's already been one fxa request and the hawk client has |
michael@0 | 206 | // already detected skew vs the fxa auth server. |
michael@0 | 207 | hawkClient._localtimeOffsetMsec = -1 * 12 * HOUR_MS; |
michael@0 | 208 | |
michael@0 | 209 | let fxaClient = new MockFxAccountsClient(); |
michael@0 | 210 | fxaClient.hawk = hawkClient; |
michael@0 | 211 | let fxa = new MockFxAccounts(); |
michael@0 | 212 | fxa.internal._now_is = now; |
michael@0 | 213 | fxa.internal.fxAccountsClient = fxaClient; |
michael@0 | 214 | |
michael@0 | 215 | configureFxAccountIdentity(browseridManager, identityConfig); |
michael@0 | 216 | |
michael@0 | 217 | // Ensure the new FxAccounts mock has a signed-in user. |
michael@0 | 218 | fxa.internal.currentAccountState.signedInUser = browseridManager._fxaService.internal.currentAccountState.signedInUser; |
michael@0 | 219 | |
michael@0 | 220 | browseridManager._fxaService = fxa; |
michael@0 | 221 | |
michael@0 | 222 | do_check_eq(browseridManager._fxaService.internal.now(), now); |
michael@0 | 223 | |
michael@0 | 224 | let request = new SyncStorageRequest("https://example.net/i/like/pie/"); |
michael@0 | 225 | let authenticator = browseridManager.getResourceAuthenticator(); |
michael@0 | 226 | let output = authenticator(request, 'GET'); |
michael@0 | 227 | dump("output" + JSON.stringify(output)); |
michael@0 | 228 | let authHeader = output.headers.authorization; |
michael@0 | 229 | do_check_true(authHeader.startsWith('Hawk')); |
michael@0 | 230 | |
michael@0 | 231 | // Skew correction is applied in the header and we're within the two-minute |
michael@0 | 232 | // window. |
michael@0 | 233 | do_check_eq(getTimestamp(authHeader), now - 12 * HOUR_MS); |
michael@0 | 234 | do_check_true( |
michael@0 | 235 | (getTimestampDelta(authHeader, now) - 12 * HOUR_MS) < 2 * MINUTE_MS); |
michael@0 | 236 | |
michael@0 | 237 | run_next_test(); |
michael@0 | 238 | }); |
michael@0 | 239 | |
michael@0 | 240 | add_task(function test_ensureLoggedIn() { |
michael@0 | 241 | configureFxAccountIdentity(browseridManager); |
michael@0 | 242 | yield browseridManager.initializeWithCurrentIdentity(); |
michael@0 | 243 | Assert.equal(Status.login, LOGIN_SUCCEEDED, "original initialize worked"); |
michael@0 | 244 | yield browseridManager.ensureLoggedIn(); |
michael@0 | 245 | Assert.equal(Status.login, LOGIN_SUCCEEDED, "original ensureLoggedIn worked"); |
michael@0 | 246 | Assert.ok(browseridManager._shouldHaveSyncKeyBundle, |
michael@0 | 247 | "_shouldHaveSyncKeyBundle should always be true after ensureLogin completes."); |
michael@0 | 248 | |
michael@0 | 249 | // arrange for no logged in user. |
michael@0 | 250 | let fxa = browseridManager._fxaService |
michael@0 | 251 | let signedInUser = fxa.internal.currentAccountState.signedInUser; |
michael@0 | 252 | fxa.internal.currentAccountState.signedInUser = null; |
michael@0 | 253 | browseridManager.initializeWithCurrentIdentity(); |
michael@0 | 254 | Assert.ok(!browseridManager._shouldHaveSyncKeyBundle, |
michael@0 | 255 | "_shouldHaveSyncKeyBundle should be false so we know we are testing what we think we are."); |
michael@0 | 256 | Status.login = LOGIN_FAILED_NO_USERNAME; |
michael@0 | 257 | yield Assert_rejects(browseridManager.ensureLoggedIn(), "expecting rejection due to no user"); |
michael@0 | 258 | Assert.ok(browseridManager._shouldHaveSyncKeyBundle, |
michael@0 | 259 | "_shouldHaveSyncKeyBundle should always be true after ensureLogin completes."); |
michael@0 | 260 | fxa.internal.currentAccountState.signedInUser = signedInUser; |
michael@0 | 261 | Status.login = LOGIN_FAILED_LOGIN_REJECTED; |
michael@0 | 262 | yield Assert_rejects(browseridManager.ensureLoggedIn(), |
michael@0 | 263 | "LOGIN_FAILED_LOGIN_REJECTED should have caused immediate rejection"); |
michael@0 | 264 | Assert.equal(Status.login, LOGIN_FAILED_LOGIN_REJECTED, |
michael@0 | 265 | "status should remain LOGIN_FAILED_LOGIN_REJECTED"); |
michael@0 | 266 | Status.login = LOGIN_FAILED_NETWORK_ERROR; |
michael@0 | 267 | yield browseridManager.ensureLoggedIn(); |
michael@0 | 268 | Assert.equal(Status.login, LOGIN_SUCCEEDED, "final ensureLoggedIn worked"); |
michael@0 | 269 | }); |
michael@0 | 270 | |
michael@0 | 271 | add_test(function test_tokenExpiration() { |
michael@0 | 272 | _("BrowserIDManager notices token expiration:"); |
michael@0 | 273 | let bimExp = new BrowserIDManager(); |
michael@0 | 274 | configureFxAccountIdentity(bimExp, identityConfig); |
michael@0 | 275 | |
michael@0 | 276 | let authenticator = bimExp.getResourceAuthenticator(); |
michael@0 | 277 | do_check_true(!!authenticator); |
michael@0 | 278 | let req = {uri: CommonUtils.makeURI( |
michael@0 | 279 | "https://example.net/somewhere/over/the/rainbow"), |
michael@0 | 280 | method: 'GET'}; |
michael@0 | 281 | authenticator(req, 'GET'); |
michael@0 | 282 | |
michael@0 | 283 | // Mock the clock. |
michael@0 | 284 | _("Forcing the token to expire ..."); |
michael@0 | 285 | Object.defineProperty(bimExp, "_now", { |
michael@0 | 286 | value: function customNow() { |
michael@0 | 287 | return (Date.now() + 3000001); |
michael@0 | 288 | }, |
michael@0 | 289 | writable: true, |
michael@0 | 290 | }); |
michael@0 | 291 | do_check_true(bimExp._token.expiration < bimExp._now()); |
michael@0 | 292 | _("... means BrowserIDManager knows to re-fetch it on the next call."); |
michael@0 | 293 | do_check_false(bimExp.hasValidToken()); |
michael@0 | 294 | run_next_test(); |
michael@0 | 295 | } |
michael@0 | 296 | ); |
michael@0 | 297 | |
michael@0 | 298 | add_test(function test_sha256() { |
michael@0 | 299 | // Test vectors from http://www.bichlmeier.info/sha256test.html |
michael@0 | 300 | let vectors = [ |
michael@0 | 301 | ["", |
michael@0 | 302 | "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"], |
michael@0 | 303 | ["abc", |
michael@0 | 304 | "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"], |
michael@0 | 305 | ["message digest", |
michael@0 | 306 | "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650"], |
michael@0 | 307 | ["secure hash algorithm", |
michael@0 | 308 | "f30ceb2bb2829e79e4ca9753d35a8ecc00262d164cc077080295381cbd643f0d"], |
michael@0 | 309 | ["SHA256 is considered to be safe", |
michael@0 | 310 | "6819d915c73f4d1e77e4e1b52d1fa0f9cf9beaead3939f15874bd988e2a23630"], |
michael@0 | 311 | ["abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
michael@0 | 312 | "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"], |
michael@0 | 313 | ["For this sample, this 63-byte string will be used as input data", |
michael@0 | 314 | "f08a78cbbaee082b052ae0708f32fa1e50c5c421aa772ba5dbb406a2ea6be342"], |
michael@0 | 315 | ["This is exactly 64 bytes long, not counting the terminating byte", |
michael@0 | 316 | "ab64eff7e88e2e46165e29f2bce41826bd4c7b3552f6b382a9e7d3af47c245f8"] |
michael@0 | 317 | ]; |
michael@0 | 318 | let bidUser = new BrowserIDManager(); |
michael@0 | 319 | for (let [input,output] of vectors) { |
michael@0 | 320 | do_check_eq(CommonUtils.bytesAsHex(bidUser._sha256(input)), output); |
michael@0 | 321 | } |
michael@0 | 322 | run_next_test(); |
michael@0 | 323 | }); |
michael@0 | 324 | |
michael@0 | 325 | add_test(function test_computeXClientStateHeader() { |
michael@0 | 326 | let kBhex = "fd5c747806c07ce0b9d69dcfea144663e630b65ec4963596a22f24910d7dd15d"; |
michael@0 | 327 | let kB = CommonUtils.hexToBytes(kBhex); |
michael@0 | 328 | |
michael@0 | 329 | let bidUser = new BrowserIDManager(); |
michael@0 | 330 | let header = bidUser._computeXClientState(kB); |
michael@0 | 331 | |
michael@0 | 332 | do_check_eq(header, "6ae94683571c7a7c54dab4700aa3995f"); |
michael@0 | 333 | run_next_test(); |
michael@0 | 334 | }); |
michael@0 | 335 | |
michael@0 | 336 | add_task(function test_getTokenErrors() { |
michael@0 | 337 | _("BrowserIDManager correctly handles various failures to get a token."); |
michael@0 | 338 | |
michael@0 | 339 | _("Arrange for a 401 - Sync should reflect an auth error."); |
michael@0 | 340 | initializeIdentityWithTokenServerResponse({ |
michael@0 | 341 | status: 401, |
michael@0 | 342 | headers: {"content-type": "application/json"}, |
michael@0 | 343 | body: JSON.stringify({}), |
michael@0 | 344 | }); |
michael@0 | 345 | let browseridManager = Service.identity; |
michael@0 | 346 | |
michael@0 | 347 | yield browseridManager.initializeWithCurrentIdentity(); |
michael@0 | 348 | yield Assert_rejects(browseridManager.whenReadyToAuthenticate.promise, |
michael@0 | 349 | "should reject due to 401"); |
michael@0 | 350 | Assert.equal(Status.login, LOGIN_FAILED_LOGIN_REJECTED, "login was rejected"); |
michael@0 | 351 | |
michael@0 | 352 | // XXX - other interesting responses to return? |
michael@0 | 353 | |
michael@0 | 354 | // And for good measure, some totally "unexpected" errors - we generally |
michael@0 | 355 | // assume these problems are going to magically go away at some point. |
michael@0 | 356 | _("Arrange for an empty body with a 200 response - should reflect a network error."); |
michael@0 | 357 | initializeIdentityWithTokenServerResponse({ |
michael@0 | 358 | status: 200, |
michael@0 | 359 | headers: [], |
michael@0 | 360 | body: "", |
michael@0 | 361 | }); |
michael@0 | 362 | browseridManager = Service.identity; |
michael@0 | 363 | yield browseridManager.initializeWithCurrentIdentity(); |
michael@0 | 364 | yield Assert_rejects(browseridManager.whenReadyToAuthenticate.promise, |
michael@0 | 365 | "should reject due to non-JSON response"); |
michael@0 | 366 | Assert.equal(Status.login, LOGIN_FAILED_NETWORK_ERROR, "login state is LOGIN_FAILED_NETWORK_ERROR"); |
michael@0 | 367 | }); |
michael@0 | 368 | |
michael@0 | 369 | add_task(function test_getTokenErrorWithRetry() { |
michael@0 | 370 | _("tokenserver sends an observer notification on various backoff headers."); |
michael@0 | 371 | |
michael@0 | 372 | // Set Sync's backoffInterval to zero - after we simulated the backoff header |
michael@0 | 373 | // it should reflect the value we sent. |
michael@0 | 374 | Status.backoffInterval = 0; |
michael@0 | 375 | _("Arrange for a 503 with a Retry-After header."); |
michael@0 | 376 | initializeIdentityWithTokenServerResponse({ |
michael@0 | 377 | status: 503, |
michael@0 | 378 | headers: {"content-type": "application/json", |
michael@0 | 379 | "retry-after": "100"}, |
michael@0 | 380 | body: JSON.stringify({}), |
michael@0 | 381 | }); |
michael@0 | 382 | let browseridManager = Service.identity; |
michael@0 | 383 | |
michael@0 | 384 | yield browseridManager.initializeWithCurrentIdentity(); |
michael@0 | 385 | yield Assert_rejects(browseridManager.whenReadyToAuthenticate.promise, |
michael@0 | 386 | "should reject due to 503"); |
michael@0 | 387 | |
michael@0 | 388 | // The observer should have fired - check it got the value in the response. |
michael@0 | 389 | Assert.equal(Status.login, LOGIN_FAILED_NETWORK_ERROR, "login was rejected"); |
michael@0 | 390 | // Sync will have the value in ms with some slop - so check it is at least that. |
michael@0 | 391 | Assert.ok(Status.backoffInterval >= 100000); |
michael@0 | 392 | |
michael@0 | 393 | _("Arrange for a 200 with an X-Backoff header."); |
michael@0 | 394 | Status.backoffInterval = 0; |
michael@0 | 395 | initializeIdentityWithTokenServerResponse({ |
michael@0 | 396 | status: 503, |
michael@0 | 397 | headers: {"content-type": "application/json", |
michael@0 | 398 | "x-backoff": "200"}, |
michael@0 | 399 | body: JSON.stringify({}), |
michael@0 | 400 | }); |
michael@0 | 401 | browseridManager = Service.identity; |
michael@0 | 402 | |
michael@0 | 403 | yield browseridManager.initializeWithCurrentIdentity(); |
michael@0 | 404 | yield Assert_rejects(browseridManager.whenReadyToAuthenticate.promise, |
michael@0 | 405 | "should reject due to no token in response"); |
michael@0 | 406 | |
michael@0 | 407 | // The observer should have fired - check it got the value in the response. |
michael@0 | 408 | Assert.ok(Status.backoffInterval >= 200000); |
michael@0 | 409 | }); |
michael@0 | 410 | |
michael@0 | 411 | add_task(function test_getKeysErrorWithBackoff() { |
michael@0 | 412 | _("Auth server (via hawk) sends an observer notification on backoff headers."); |
michael@0 | 413 | |
michael@0 | 414 | // Set Sync's backoffInterval to zero - after we simulated the backoff header |
michael@0 | 415 | // it should reflect the value we sent. |
michael@0 | 416 | Status.backoffInterval = 0; |
michael@0 | 417 | _("Arrange for a 503 with a X-Backoff header."); |
michael@0 | 418 | |
michael@0 | 419 | let config = makeIdentityConfig(); |
michael@0 | 420 | // We want no kA or kB so we attempt to fetch them. |
michael@0 | 421 | delete config.fxaccount.user.kA; |
michael@0 | 422 | delete config.fxaccount.user.kB; |
michael@0 | 423 | config.fxaccount.user.keyFetchToken = "keyfetchtoken"; |
michael@0 | 424 | yield initializeIdentityWithHAWKResponseFactory(config, function(method, data, uri) { |
michael@0 | 425 | Assert.equal(method, "get"); |
michael@0 | 426 | Assert.equal(uri, "http://mockedserver:9999/account/keys") |
michael@0 | 427 | return { |
michael@0 | 428 | status: 503, |
michael@0 | 429 | headers: {"content-type": "application/json", |
michael@0 | 430 | "x-backoff": "100"}, |
michael@0 | 431 | body: "{}", |
michael@0 | 432 | } |
michael@0 | 433 | }); |
michael@0 | 434 | |
michael@0 | 435 | let browseridManager = Service.identity; |
michael@0 | 436 | yield Assert_rejects(browseridManager.whenReadyToAuthenticate.promise, |
michael@0 | 437 | "should reject due to 503"); |
michael@0 | 438 | |
michael@0 | 439 | // The observer should have fired - check it got the value in the response. |
michael@0 | 440 | Assert.equal(Status.login, LOGIN_FAILED_NETWORK_ERROR, "login was rejected"); |
michael@0 | 441 | // Sync will have the value in ms with some slop - so check it is at least that. |
michael@0 | 442 | Assert.ok(Status.backoffInterval >= 100000); |
michael@0 | 443 | }); |
michael@0 | 444 | |
michael@0 | 445 | add_task(function test_getKeysErrorWithRetry() { |
michael@0 | 446 | _("Auth server (via hawk) sends an observer notification on retry headers."); |
michael@0 | 447 | |
michael@0 | 448 | // Set Sync's backoffInterval to zero - after we simulated the backoff header |
michael@0 | 449 | // it should reflect the value we sent. |
michael@0 | 450 | Status.backoffInterval = 0; |
michael@0 | 451 | _("Arrange for a 503 with a Retry-After header."); |
michael@0 | 452 | |
michael@0 | 453 | let config = makeIdentityConfig(); |
michael@0 | 454 | // We want no kA or kB so we attempt to fetch them. |
michael@0 | 455 | delete config.fxaccount.user.kA; |
michael@0 | 456 | delete config.fxaccount.user.kB; |
michael@0 | 457 | config.fxaccount.user.keyFetchToken = "keyfetchtoken"; |
michael@0 | 458 | yield initializeIdentityWithHAWKResponseFactory(config, function(method, data, uri) { |
michael@0 | 459 | Assert.equal(method, "get"); |
michael@0 | 460 | Assert.equal(uri, "http://mockedserver:9999/account/keys") |
michael@0 | 461 | return { |
michael@0 | 462 | status: 503, |
michael@0 | 463 | headers: {"content-type": "application/json", |
michael@0 | 464 | "retry-after": "100"}, |
michael@0 | 465 | body: "{}", |
michael@0 | 466 | } |
michael@0 | 467 | }); |
michael@0 | 468 | |
michael@0 | 469 | let browseridManager = Service.identity; |
michael@0 | 470 | yield Assert_rejects(browseridManager.whenReadyToAuthenticate.promise, |
michael@0 | 471 | "should reject due to 503"); |
michael@0 | 472 | |
michael@0 | 473 | // The observer should have fired - check it got the value in the response. |
michael@0 | 474 | Assert.equal(Status.login, LOGIN_FAILED_NETWORK_ERROR, "login was rejected"); |
michael@0 | 475 | // Sync will have the value in ms with some slop - so check it is at least that. |
michael@0 | 476 | Assert.ok(Status.backoffInterval >= 100000); |
michael@0 | 477 | }); |
michael@0 | 478 | |
michael@0 | 479 | add_task(function test_getHAWKErrors() { |
michael@0 | 480 | _("BrowserIDManager correctly handles various HAWK failures."); |
michael@0 | 481 | |
michael@0 | 482 | _("Arrange for a 401 - Sync should reflect an auth error."); |
michael@0 | 483 | let config = makeIdentityConfig(); |
michael@0 | 484 | yield initializeIdentityWithHAWKResponseFactory(config, function(method, data, uri) { |
michael@0 | 485 | Assert.equal(method, "post"); |
michael@0 | 486 | Assert.equal(uri, "http://mockedserver:9999/certificate/sign") |
michael@0 | 487 | return { |
michael@0 | 488 | status: 401, |
michael@0 | 489 | headers: {"content-type": "application/json"}, |
michael@0 | 490 | body: JSON.stringify({}), |
michael@0 | 491 | } |
michael@0 | 492 | }); |
michael@0 | 493 | Assert.equal(Status.login, LOGIN_FAILED_LOGIN_REJECTED, "login was rejected"); |
michael@0 | 494 | |
michael@0 | 495 | // XXX - other interesting responses to return? |
michael@0 | 496 | |
michael@0 | 497 | // And for good measure, some totally "unexpected" errors - we generally |
michael@0 | 498 | // assume these problems are going to magically go away at some point. |
michael@0 | 499 | _("Arrange for an empty body with a 200 response - should reflect a network error."); |
michael@0 | 500 | yield initializeIdentityWithHAWKResponseFactory(config, function(method, data, uri) { |
michael@0 | 501 | Assert.equal(method, "post"); |
michael@0 | 502 | Assert.equal(uri, "http://mockedserver:9999/certificate/sign") |
michael@0 | 503 | return { |
michael@0 | 504 | status: 200, |
michael@0 | 505 | headers: [], |
michael@0 | 506 | body: "", |
michael@0 | 507 | } |
michael@0 | 508 | }); |
michael@0 | 509 | Assert.equal(Status.login, LOGIN_FAILED_NETWORK_ERROR, "login state is LOGIN_FAILED_NETWORK_ERROR"); |
michael@0 | 510 | }); |
michael@0 | 511 | |
michael@0 | 512 | add_task(function test_getGetKeysFailing401() { |
michael@0 | 513 | _("BrowserIDManager correctly handles 401 responses fetching keys."); |
michael@0 | 514 | |
michael@0 | 515 | _("Arrange for a 401 - Sync should reflect an auth error."); |
michael@0 | 516 | let config = makeIdentityConfig(); |
michael@0 | 517 | // We want no kA or kB so we attempt to fetch them. |
michael@0 | 518 | delete config.fxaccount.user.kA; |
michael@0 | 519 | delete config.fxaccount.user.kB; |
michael@0 | 520 | config.fxaccount.user.keyFetchToken = "keyfetchtoken"; |
michael@0 | 521 | yield initializeIdentityWithHAWKResponseFactory(config, function(method, data, uri) { |
michael@0 | 522 | Assert.equal(method, "get"); |
michael@0 | 523 | Assert.equal(uri, "http://mockedserver:9999/account/keys") |
michael@0 | 524 | return { |
michael@0 | 525 | status: 401, |
michael@0 | 526 | headers: {"content-type": "application/json"}, |
michael@0 | 527 | body: "{}", |
michael@0 | 528 | } |
michael@0 | 529 | }); |
michael@0 | 530 | Assert.equal(Status.login, LOGIN_FAILED_LOGIN_REJECTED, "login was rejected"); |
michael@0 | 531 | }); |
michael@0 | 532 | |
michael@0 | 533 | add_task(function test_getGetKeysFailing503() { |
michael@0 | 534 | _("BrowserIDManager correctly handles 5XX responses fetching keys."); |
michael@0 | 535 | |
michael@0 | 536 | _("Arrange for a 503 - Sync should reflect a network error."); |
michael@0 | 537 | let config = makeIdentityConfig(); |
michael@0 | 538 | // We want no kA or kB so we attempt to fetch them. |
michael@0 | 539 | delete config.fxaccount.user.kA; |
michael@0 | 540 | delete config.fxaccount.user.kB; |
michael@0 | 541 | config.fxaccount.user.keyFetchToken = "keyfetchtoken"; |
michael@0 | 542 | yield initializeIdentityWithHAWKResponseFactory(config, function(method, data, uri) { |
michael@0 | 543 | Assert.equal(method, "get"); |
michael@0 | 544 | Assert.equal(uri, "http://mockedserver:9999/account/keys") |
michael@0 | 545 | return { |
michael@0 | 546 | status: 503, |
michael@0 | 547 | headers: {"content-type": "application/json"}, |
michael@0 | 548 | body: "{}", |
michael@0 | 549 | } |
michael@0 | 550 | }); |
michael@0 | 551 | Assert.equal(Status.login, LOGIN_FAILED_NETWORK_ERROR, "state reflects network error"); |
michael@0 | 552 | }); |
michael@0 | 553 | |
michael@0 | 554 | add_task(function test_getKeysMissing() { |
michael@0 | 555 | _("BrowserIDManager correctly handles getKeys succeeding but not returning keys."); |
michael@0 | 556 | |
michael@0 | 557 | let browseridManager = new BrowserIDManager(); |
michael@0 | 558 | let identityConfig = makeIdentityConfig(); |
michael@0 | 559 | // our mock identity config already has kA and kB - remove them or we never |
michael@0 | 560 | // try and fetch them. |
michael@0 | 561 | delete identityConfig.fxaccount.user.kA; |
michael@0 | 562 | delete identityConfig.fxaccount.user.kB; |
michael@0 | 563 | identityConfig.fxaccount.user.keyFetchToken = 'keyFetchToken'; |
michael@0 | 564 | |
michael@0 | 565 | configureFxAccountIdentity(browseridManager, identityConfig); |
michael@0 | 566 | |
michael@0 | 567 | // Mock a fxAccounts object that returns no keys |
michael@0 | 568 | let fxa = new FxAccounts({ |
michael@0 | 569 | fetchAndUnwrapKeys: function () { |
michael@0 | 570 | return Promise.resolve({}); |
michael@0 | 571 | }, |
michael@0 | 572 | fxAccountsClient: new MockFxAccountsClient() |
michael@0 | 573 | }); |
michael@0 | 574 | |
michael@0 | 575 | // Add a mock to the currentAccountState object. |
michael@0 | 576 | fxa.internal.currentAccountState.getCertificate = function(data, keyPair, mustBeValidUntil) { |
michael@0 | 577 | this.cert = { |
michael@0 | 578 | validUntil: fxa.internal.now() + CERT_LIFETIME, |
michael@0 | 579 | cert: "certificate", |
michael@0 | 580 | }; |
michael@0 | 581 | return Promise.resolve(this.cert.cert); |
michael@0 | 582 | }; |
michael@0 | 583 | |
michael@0 | 584 | // Ensure the new FxAccounts mock has a signed-in user. |
michael@0 | 585 | fxa.internal.currentAccountState.signedInUser = browseridManager._fxaService.internal.currentAccountState.signedInUser; |
michael@0 | 586 | |
michael@0 | 587 | browseridManager._fxaService = fxa; |
michael@0 | 588 | |
michael@0 | 589 | yield browseridManager.initializeWithCurrentIdentity(); |
michael@0 | 590 | |
michael@0 | 591 | let ex; |
michael@0 | 592 | try { |
michael@0 | 593 | yield browseridManager.whenReadyToAuthenticate.promise; |
michael@0 | 594 | } catch (e) { |
michael@0 | 595 | ex = e; |
michael@0 | 596 | } |
michael@0 | 597 | |
michael@0 | 598 | Assert.ok(ex.message.indexOf("missing kA or kB") >= 0); |
michael@0 | 599 | }); |
michael@0 | 600 | |
michael@0 | 601 | // End of tests |
michael@0 | 602 | // Utility functions follow |
michael@0 | 603 | |
michael@0 | 604 | // Create a new browserid_identity object and initialize it with a |
michael@0 | 605 | // hawk mock that simulates HTTP responses. |
michael@0 | 606 | // The callback function will be called each time the mocked hawk server wants |
michael@0 | 607 | // to make a request. The result of the callback should be the mock response |
michael@0 | 608 | // object that will be returned to hawk. |
michael@0 | 609 | // A token server mock will be used that doesn't hit a server, so we move |
michael@0 | 610 | // directly to a hawk request. |
michael@0 | 611 | function* initializeIdentityWithHAWKResponseFactory(config, cbGetResponse) { |
michael@0 | 612 | // A mock request object. |
michael@0 | 613 | function MockRESTRequest(uri, credentials, extra) { |
michael@0 | 614 | this._uri = uri; |
michael@0 | 615 | this._credentials = credentials; |
michael@0 | 616 | this._extra = extra; |
michael@0 | 617 | }; |
michael@0 | 618 | MockRESTRequest.prototype = { |
michael@0 | 619 | setHeader: function() {}, |
michael@0 | 620 | post: function(data, callback) { |
michael@0 | 621 | this.response = cbGetResponse("post", data, this._uri, this._credentials, this._extra); |
michael@0 | 622 | callback.call(this); |
michael@0 | 623 | }, |
michael@0 | 624 | get: function(callback) { |
michael@0 | 625 | this.response = cbGetResponse("get", null, this._uri, this._credentials, this._extra); |
michael@0 | 626 | callback.call(this); |
michael@0 | 627 | } |
michael@0 | 628 | } |
michael@0 | 629 | |
michael@0 | 630 | // The hawk client. |
michael@0 | 631 | function MockedHawkClient() {} |
michael@0 | 632 | MockedHawkClient.prototype = new HawkClient("http://mockedserver:9999"); |
michael@0 | 633 | MockedHawkClient.prototype.constructor = MockedHawkClient; |
michael@0 | 634 | MockedHawkClient.prototype.newHAWKAuthenticatedRESTRequest = function(uri, credentials, extra) { |
michael@0 | 635 | return new MockRESTRequest(uri, credentials, extra); |
michael@0 | 636 | } |
michael@0 | 637 | // Arrange for the same observerPrefix as FxAccountsClient uses |
michael@0 | 638 | MockedHawkClient.prototype.observerPrefix = "FxA:hawk"; |
michael@0 | 639 | |
michael@0 | 640 | // tie it all together - configureFxAccountIdentity isn't useful here :( |
michael@0 | 641 | let fxaClient = new MockFxAccountsClient(); |
michael@0 | 642 | fxaClient.hawk = new MockedHawkClient(); |
michael@0 | 643 | let internal = { |
michael@0 | 644 | fxAccountsClient: fxaClient, |
michael@0 | 645 | } |
michael@0 | 646 | let fxa = new FxAccounts(internal); |
michael@0 | 647 | fxa.internal.currentAccountState.signedInUser = { |
michael@0 | 648 | accountData: config.fxaccount.user, |
michael@0 | 649 | }; |
michael@0 | 650 | |
michael@0 | 651 | browseridManager._fxaService = fxa; |
michael@0 | 652 | browseridManager._signedInUser = null; |
michael@0 | 653 | yield browseridManager.initializeWithCurrentIdentity(); |
michael@0 | 654 | yield Assert_rejects(browseridManager.whenReadyToAuthenticate.promise, |
michael@0 | 655 | "expecting rejection due to hawk error"); |
michael@0 | 656 | } |
michael@0 | 657 | |
michael@0 | 658 | |
michael@0 | 659 | function getTimestamp(hawkAuthHeader) { |
michael@0 | 660 | return parseInt(/ts="(\d+)"/.exec(hawkAuthHeader)[1], 10) * SECOND_MS; |
michael@0 | 661 | } |
michael@0 | 662 | |
michael@0 | 663 | function getTimestampDelta(hawkAuthHeader, now=Date.now()) { |
michael@0 | 664 | return Math.abs(getTimestamp(hawkAuthHeader) - now); |
michael@0 | 665 | } |
michael@0 | 666 |