2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/constants.js");
|
2012-04-05 23:26:06 -07:00
|
|
|
Cu.import("resource://services-common/log4moz.js");
|
2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/service.js");
|
|
|
|
Cu.import("resource://services-sync/status.js");
|
|
|
|
Cu.import("resource://services-sync/util.js");
|
2011-06-27 06:22:49 -07:00
|
|
|
Cu.import("resource://services-sync/policies.js");
|
2010-06-04 06:44:04 -07:00
|
|
|
|
2011-04-18 13:25:25 -07:00
|
|
|
function login_handling(handler) {
|
|
|
|
return function (request, response) {
|
2011-04-18 13:26:03 -07:00
|
|
|
if (basic_auth_matches(request, "johndoe", "ilovejane") ||
|
|
|
|
basic_auth_matches(request, "janedoe", "ilovejohn")) {
|
2011-04-18 13:25:25 -07:00
|
|
|
handler(request, response);
|
|
|
|
} else {
|
|
|
|
let body = "Unauthorized";
|
|
|
|
response.setStatusLine(request.httpVersion, 401, "Unauthorized");
|
2012-03-05 17:36:45 -08:00
|
|
|
response.setHeader("Content-Type", "text/plain");
|
2011-04-18 13:25:25 -07:00
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
}
|
|
|
|
};
|
2010-06-04 06:44:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function run_test() {
|
|
|
|
let logger = Log4Moz.repository.rootLogger;
|
|
|
|
Log4Moz.repository.rootLogger.addAppender(new Log4Moz.DumpAppender());
|
|
|
|
|
2011-07-13 15:20:07 -07:00
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
add_test(function test_offline() {
|
2011-02-03 10:23:39 -08:00
|
|
|
try {
|
|
|
|
_("The right bits are set when we're offline.");
|
2011-05-19 18:08:07 -07:00
|
|
|
Services.io.offline = true;
|
2011-02-03 10:23:39 -08:00
|
|
|
do_check_false(!!Service.login());
|
|
|
|
do_check_eq(Status.login, LOGIN_FAILED_NETWORK_ERROR);
|
2011-05-19 18:08:07 -07:00
|
|
|
Services.io.offline = false;
|
2011-02-03 10:23:39 -08:00
|
|
|
} finally {
|
|
|
|
Svc.Prefs.resetBranch("");
|
2011-07-13 15:20:07 -07:00
|
|
|
run_next_test();
|
2011-02-03 10:23:39 -08:00
|
|
|
}
|
2011-07-13 15:20:07 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
function setup() {
|
2012-02-06 09:50:11 -08:00
|
|
|
Service.serverURL = TEST_SERVER_URL;
|
|
|
|
Service.clusterURL = TEST_CLUSTER_URL;
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2011-04-18 13:25:25 -07:00
|
|
|
let janeHelper = track_collections_helper();
|
|
|
|
let janeU = janeHelper.with_updated_collection;
|
|
|
|
let janeColls = janeHelper.collections;
|
|
|
|
let johnHelper = track_collections_helper();
|
|
|
|
let johnU = johnHelper.with_updated_collection;
|
|
|
|
let johnColls = johnHelper.collections;
|
|
|
|
|
2011-07-13 15:20:07 -07:00
|
|
|
return httpd_setup({
|
2011-04-18 13:25:25 -07:00
|
|
|
"/1.1/johndoe/info/collections": login_handling(johnHelper.handler),
|
|
|
|
"/1.1/janedoe/info/collections": login_handling(janeHelper.handler),
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// We need these handlers because we test login, and login
|
|
|
|
// is where keys are generated or fetched.
|
|
|
|
// TODO: have Jane fetch her keys, not generate them...
|
2011-04-18 13:25:25 -07:00
|
|
|
"/1.1/johndoe/storage/crypto/keys": johnU("crypto", new ServerWBO("keys").handler()),
|
|
|
|
"/1.1/johndoe/storage/meta/global": johnU("meta", new ServerWBO("global").handler()),
|
|
|
|
"/1.1/janedoe/storage/crypto/keys": janeU("crypto", new ServerWBO("keys").handler()),
|
|
|
|
"/1.1/janedoe/storage/meta/global": janeU("meta", new ServerWBO("global").handler())
|
2010-06-04 06:44:04 -07:00
|
|
|
});
|
2011-07-13 15:20:07 -07:00
|
|
|
}
|
2010-06-04 06:44:04 -07:00
|
|
|
|
2011-07-13 15:20:07 -07:00
|
|
|
add_test(function test_login_logout() {
|
|
|
|
let server = setup();
|
2010-06-04 06:44:04 -07:00
|
|
|
|
2011-07-13 15:20:07 -07:00
|
|
|
try {
|
2010-08-06 08:31:21 -07:00
|
|
|
_("Force the initial state.");
|
|
|
|
Status.service = STATUS_OK;
|
2010-06-10 17:04:49 -07:00
|
|
|
do_check_eq(Status.service, STATUS_OK);
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
_("Try logging in. It won't work because we're not configured yet.");
|
2010-08-25 15:49:45 -07:00
|
|
|
Service.login();
|
2010-06-10 17:04:49 -07:00
|
|
|
do_check_eq(Status.service, CLIENT_NOT_CONFIGURED);
|
|
|
|
do_check_eq(Status.login, LOGIN_FAILED_NO_USERNAME);
|
2010-08-25 15:49:45 -07:00
|
|
|
do_check_false(Service.isLoggedIn);
|
2010-06-04 06:44:04 -07:00
|
|
|
|
|
|
|
_("Try again with username and password set.");
|
2012-03-22 15:49:50 -07:00
|
|
|
Identity.account = "johndoe";
|
|
|
|
Identity.basicPassword = "ilovejane";
|
2010-08-25 15:49:45 -07:00
|
|
|
Service.login();
|
2010-06-10 17:04:49 -07:00
|
|
|
do_check_eq(Status.service, CLIENT_NOT_CONFIGURED);
|
|
|
|
do_check_eq(Status.login, LOGIN_FAILED_NO_PASSPHRASE);
|
2010-08-25 15:49:45 -07:00
|
|
|
do_check_false(Service.isLoggedIn);
|
2010-06-10 17:04:49 -07:00
|
|
|
|
|
|
|
_("Success if passphrase is set.");
|
2012-03-22 15:49:50 -07:00
|
|
|
Identity.syncKey = "foo";
|
2010-08-25 15:49:45 -07:00
|
|
|
Service.login();
|
2010-06-10 17:04:49 -07:00
|
|
|
do_check_eq(Status.service, STATUS_OK);
|
|
|
|
do_check_eq(Status.login, LOGIN_SUCCEEDED);
|
2010-08-25 15:49:45 -07:00
|
|
|
do_check_true(Service.isLoggedIn);
|
2010-06-04 06:44:04 -07:00
|
|
|
|
2010-06-16 20:12:38 -07:00
|
|
|
_("We can also pass username, password and passphrase to login().");
|
2010-08-25 15:49:45 -07:00
|
|
|
Service.login("janedoe", "incorrectpassword", "bar");
|
2012-03-22 15:49:50 -07:00
|
|
|
setBasicCredentials("janedoe", "incorrectpassword", "bar");
|
2010-06-16 20:12:38 -07:00
|
|
|
do_check_eq(Status.service, LOGIN_FAILED);
|
|
|
|
do_check_eq(Status.login, LOGIN_FAILED_LOGIN_REJECTED);
|
2010-08-25 15:49:45 -07:00
|
|
|
do_check_false(Service.isLoggedIn);
|
2010-06-16 20:12:38 -07:00
|
|
|
|
|
|
|
_("Try again with correct password.");
|
2010-08-25 15:49:45 -07:00
|
|
|
Service.login("janedoe", "ilovejohn");
|
2010-06-16 20:12:38 -07:00
|
|
|
do_check_eq(Status.service, STATUS_OK);
|
|
|
|
do_check_eq(Status.login, LOGIN_SUCCEEDED);
|
2010-08-25 15:49:45 -07:00
|
|
|
do_check_true(Service.isLoggedIn);
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2010-08-09 16:46:54 -07:00
|
|
|
_("Calling login() with parameters when the client is unconfigured sends notification.");
|
|
|
|
let notified = false;
|
2010-08-25 15:49:45 -07:00
|
|
|
Svc.Obs.add("weave:service:setup-complete", function() {
|
2010-08-09 16:46:54 -07:00
|
|
|
notified = true;
|
|
|
|
});
|
2012-03-22 15:49:50 -07:00
|
|
|
setBasicCredentials(null, null, null);
|
2010-08-25 15:49:45 -07:00
|
|
|
Service.login("janedoe", "ilovejohn", "bar");
|
2010-08-09 16:46:54 -07:00
|
|
|
do_check_true(notified);
|
|
|
|
do_check_eq(Status.service, STATUS_OK);
|
|
|
|
do_check_eq(Status.login, LOGIN_SUCCEEDED);
|
2010-08-25 15:49:45 -07:00
|
|
|
do_check_true(Service.isLoggedIn);
|
2010-06-16 20:12:38 -07:00
|
|
|
|
|
|
|
_("Logout.");
|
2010-08-25 15:49:45 -07:00
|
|
|
Service.logout();
|
|
|
|
do_check_false(Service.isLoggedIn);
|
2010-06-16 20:12:38 -07:00
|
|
|
|
|
|
|
_("Logging out again won't do any harm.");
|
2010-08-25 15:49:45 -07:00
|
|
|
Service.logout();
|
|
|
|
do_check_false(Service.isLoggedIn);
|
2011-02-03 10:23:39 -08:00
|
|
|
|
2011-07-13 15:20:07 -07:00
|
|
|
} finally {
|
|
|
|
Svc.Prefs.resetBranch("");
|
|
|
|
server.stop(run_next_test);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
add_test(function test_login_on_sync() {
|
|
|
|
let server = setup();
|
2012-03-22 15:49:50 -07:00
|
|
|
setBasicCredentials("johndoe", "ilovejane", "bar");
|
2011-07-13 15:20:07 -07:00
|
|
|
|
|
|
|
try {
|
2010-12-20 20:01:34 -08:00
|
|
|
_("Sync calls login.");
|
|
|
|
let oldLogin = Service.login;
|
|
|
|
let loginCalled = false;
|
|
|
|
Service.login = function() {
|
|
|
|
loginCalled = true;
|
|
|
|
Status.login = LOGIN_SUCCEEDED;
|
|
|
|
this._loggedIn = false; // So that sync aborts.
|
|
|
|
return true;
|
2011-07-13 15:20:07 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Service.sync();
|
|
|
|
|
2010-12-20 20:01:34 -08:00
|
|
|
do_check_true(loginCalled);
|
|
|
|
Service.login = oldLogin;
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2010-12-20 20:01:34 -08:00
|
|
|
// Stub mpLocked.
|
|
|
|
let mpLockedF = Utils.mpLocked;
|
|
|
|
let mpLocked = true;
|
|
|
|
Utils.mpLocked = function() mpLocked;
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2010-12-20 20:01:34 -08:00
|
|
|
// Stub scheduleNextSync. This gets called within checkSyncStatus if we're
|
|
|
|
// ready to sync, so use it as an indicator.
|
2012-08-29 14:43:41 -07:00
|
|
|
let scheduleNextSyncF = Service.scheduler.scheduleNextSync;
|
2010-12-20 20:01:34 -08:00
|
|
|
let scheduleCalled = false;
|
2012-08-29 14:43:41 -07:00
|
|
|
Service.scheduler.scheduleNextSync = function(wait) {
|
2010-12-20 20:01:34 -08:00
|
|
|
scheduleCalled = true;
|
|
|
|
scheduleNextSyncF.call(this, wait);
|
2011-07-13 15:20:07 -07:00
|
|
|
};
|
|
|
|
|
2010-12-20 20:01:34 -08:00
|
|
|
// Autoconnect still tries to connect in the background (useful behavior:
|
|
|
|
// for non-MP users and unlocked MPs, this will detect version expiry
|
|
|
|
// earlier).
|
2011-07-13 15:20:07 -07:00
|
|
|
//
|
2010-12-20 20:01:34 -08:00
|
|
|
// Consequently, non-MP users will be logged in as in the pre-Bug 543784 world,
|
|
|
|
// and checkSyncStatus reflects that by waiting for login.
|
2011-07-13 15:20:07 -07:00
|
|
|
//
|
2010-12-20 20:01:34 -08:00
|
|
|
// This process doesn't apply if your MP is still locked, so we make
|
|
|
|
// checkSyncStatus accept a locked MP in place of being logged in.
|
2011-07-13 15:20:07 -07:00
|
|
|
//
|
2010-12-20 20:01:34 -08:00
|
|
|
// This test exercises these two branches.
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2010-12-20 20:01:34 -08:00
|
|
|
_("We're ready to sync if locked.");
|
|
|
|
Service.enabled = true;
|
2011-05-19 18:08:07 -07:00
|
|
|
Services.io.offline = false;
|
2012-08-29 14:43:41 -07:00
|
|
|
Service.scheduler.checkSyncStatus();
|
2010-12-20 20:01:34 -08:00
|
|
|
do_check_true(scheduleCalled);
|
2011-07-13 15:20:07 -07:00
|
|
|
|
|
|
|
_("... and also if we're not locked.");
|
2010-12-20 20:01:34 -08:00
|
|
|
scheduleCalled = false;
|
|
|
|
mpLocked = false;
|
2012-08-29 14:43:41 -07:00
|
|
|
Service.scheduler.checkSyncStatus();
|
2011-07-13 15:20:07 -07:00
|
|
|
do_check_true(scheduleCalled);
|
2012-08-29 14:43:41 -07:00
|
|
|
Service.scheduler.scheduleNextSync = scheduleNextSyncF;
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2010-12-20 20:01:34 -08:00
|
|
|
// TODO: need better tests around master password prompting. See Bug 620583.
|
2010-06-16 20:12:38 -07:00
|
|
|
|
2011-01-10 21:51:29 -08:00
|
|
|
mpLocked = true;
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2011-01-10 21:51:29 -08:00
|
|
|
// Testing exception handling if master password dialog is canceled.
|
2012-03-22 15:49:50 -07:00
|
|
|
// Do this by monkeypatching.
|
|
|
|
let oldGetter = Identity.__lookupGetter__("syncKey");
|
|
|
|
let oldSetter = Identity.__lookupSetter__("syncKey");
|
|
|
|
_("Old passphrase function is " + oldGetter);
|
|
|
|
Identity.__defineGetter__("syncKey",
|
2011-01-10 21:51:29 -08:00
|
|
|
function() {
|
|
|
|
throw "User canceled Master Password entry";
|
|
|
|
});
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2012-08-29 14:43:41 -07:00
|
|
|
let oldClearSyncTriggers = Service.scheduler.clearSyncTriggers;
|
2011-01-10 21:51:29 -08:00
|
|
|
let oldLockedSync = Service._lockedSync;
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2011-01-10 21:51:29 -08:00
|
|
|
let cSTCalled = false;
|
|
|
|
let lockedSyncCalled = false;
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2012-08-29 14:43:41 -07:00
|
|
|
Service.scheduler.clearSyncTriggers = function() { cSTCalled = true; };
|
2011-01-10 21:51:29 -08:00
|
|
|
Service._lockedSync = function() { lockedSyncCalled = true; };
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2011-01-10 21:51:29 -08:00
|
|
|
_("If master password is canceled, login fails and we report lockage.");
|
|
|
|
do_check_false(!!Service.login());
|
|
|
|
do_check_eq(Status.login, MASTER_PASSWORD_LOCKED);
|
|
|
|
do_check_eq(Status.service, LOGIN_FAILED);
|
|
|
|
_("Locked? " + Utils.mpLocked());
|
|
|
|
_("checkSync reports the correct term.");
|
|
|
|
do_check_eq(Service._checkSync(), kSyncMasterPasswordLocked);
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2011-01-10 21:51:29 -08:00
|
|
|
_("Sync doesn't proceed and clears triggers if MP is still locked.");
|
|
|
|
Service.sync();
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2011-01-10 21:51:29 -08:00
|
|
|
do_check_true(cSTCalled);
|
|
|
|
do_check_false(lockedSyncCalled);
|
|
|
|
|
2012-03-22 15:49:50 -07:00
|
|
|
Identity.__defineGetter__("syncKey", oldGetter);
|
|
|
|
Identity.__defineSetter__("syncKey", oldSetter);
|
|
|
|
|
2011-01-10 21:51:29 -08:00
|
|
|
// N.B., a bunch of methods are stubbed at this point. Be careful putting
|
|
|
|
// new tests after this point!
|
2011-07-13 15:20:07 -07:00
|
|
|
|
2010-06-04 06:44:04 -07:00
|
|
|
} finally {
|
|
|
|
Svc.Prefs.resetBranch("");
|
2011-07-13 15:20:07 -07:00
|
|
|
server.stop(run_next_test);
|
2010-06-04 06:44:04 -07:00
|
|
|
}
|
2011-07-13 15:20:07 -07:00
|
|
|
});
|