michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Cu.import("resource://services-sync/constants.js"); michael@0: Cu.import("resource://services-sync/jpakeclient.js"); michael@0: Cu.import("resource://services-sync/service.js"); michael@0: Cu.import("resource://services-sync/util.js"); michael@0: Cu.import("resource://testing-common/services/sync/utils.js"); michael@0: michael@0: function run_test() { michael@0: ensureLegacyIdentityManager(); michael@0: setBasicCredentials("johndoe", "ilovejane", Utils.generatePassphrase()); michael@0: Service.serverURL = "http://weave.server/"; michael@0: michael@0: initTestLogging("Trace"); michael@0: Log.repository.getLogger("Sync.SendCredentialsController").level = Log.Level.Trace; michael@0: Log.repository.getLogger("Sync.SyncScheduler").level = Log.Level.Trace; michael@0: run_next_test(); michael@0: } michael@0: michael@0: function make_sendCredentials_test(topic) { michael@0: return function test_sendCredentials() { michael@0: _("Test sending credentials on " + topic + " observer notification."); michael@0: michael@0: let sendAndCompleteCalled = false; michael@0: let jpakeclient = { michael@0: sendAndComplete: function sendAndComplete(data) { michael@0: // Verify that the controller unregisters itself as an observer michael@0: // when the exchange is complete by faking another notification. michael@0: do_check_false(sendAndCompleteCalled); michael@0: sendAndCompleteCalled = true; michael@0: michael@0: // Verify it sends the correct data. michael@0: do_check_eq(data.account, Service.identity.account); michael@0: do_check_eq(data.password, Service.identity.basicPassword); michael@0: do_check_eq(data.synckey, Service.identity.syncKey); michael@0: do_check_eq(data.serverURL, Service.serverURL); michael@0: michael@0: this.controller.onComplete(); michael@0: // Verify it schedules a sync for the expected interval. michael@0: let expectedInterval = Service.scheduler.activeInterval; michael@0: do_check_true(Service.scheduler.nextSync - Date.now() <= expectedInterval); michael@0: michael@0: // Signal the end of another sync. We shouldn't be registered anymore, michael@0: // so we shouldn't re-enter this method (cf sendAndCompleteCalled above) michael@0: Svc.Obs.notify(topic); michael@0: michael@0: Service.scheduler.setDefaults(); michael@0: Utils.nextTick(run_next_test); michael@0: } michael@0: }; michael@0: jpakeclient.controller = new SendCredentialsController(jpakeclient, Service); michael@0: Svc.Obs.notify(topic); michael@0: }; michael@0: } michael@0: michael@0: add_test(make_sendCredentials_test("weave:service:sync:finish")); michael@0: add_test(make_sendCredentials_test("weave:service:sync:error")); michael@0: michael@0: michael@0: add_test(function test_abort() { michael@0: _("Test aborting the J-PAKE exchange."); michael@0: michael@0: let jpakeclient = { michael@0: sendAndComplete: function sendAndComplete() { michael@0: do_throw("Shouldn't get here!"); michael@0: } michael@0: }; michael@0: jpakeclient.controller = new SendCredentialsController(jpakeclient, Service); michael@0: michael@0: // Verify that the controller unregisters itself when the exchange michael@0: // was aborted. michael@0: jpakeclient.controller.onAbort(JPAKE_ERROR_USERABORT); michael@0: Svc.Obs.notify("weave:service:sync:finish"); michael@0: Utils.nextTick(run_next_test); michael@0: }); michael@0: michael@0: michael@0: add_test(function test_startOver() { michael@0: _("Test wiping local Sync config aborts transaction."); michael@0: michael@0: let abortCalled = false; michael@0: let jpakeclient = { michael@0: abort: function abort() { michael@0: abortCalled = true; michael@0: this.controller.onAbort(JPAKE_ERROR_USERABORT); michael@0: }, michael@0: sendAndComplete: function sendAndComplete() { michael@0: do_throw("Shouldn't get here!"); michael@0: } michael@0: }; michael@0: jpakeclient.controller = new SendCredentialsController(jpakeclient, Service); michael@0: michael@0: Svc.Obs.notify("weave:service:start-over"); michael@0: do_check_true(abortCalled); michael@0: michael@0: // Ensure that the controller no longer does anything if a sync michael@0: // finishes now or -- more likely -- errors out. michael@0: Svc.Obs.notify("weave:service:sync:error"); michael@0: michael@0: Utils.nextTick(run_next_test); michael@0: });