2012-03-22 15:49:50 -07:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
2010-08-25 15:49:45 -07:00
|
|
|
Cu.import("resource://services-sync/main.js");
|
2010-08-23 06:20:39 -07:00
|
|
|
Cu.import("resource://services-sync/util.js");
|
2010-11-29 16:41:17 -08:00
|
|
|
Cu.import("resource://services-sync/status.js");
|
|
|
|
Cu.import("resource://services-sync/constants.js");
|
2012-03-22 15:49:50 -07:00
|
|
|
Cu.import("resource://services-sync/keys.js");
|
2012-04-05 23:26:06 -07:00
|
|
|
Cu.import("resource://services-common/log4moz.js");
|
2010-08-23 06:20:39 -07:00
|
|
|
|
|
|
|
function run_test() {
|
|
|
|
let logger = Log4Moz.repository.rootLogger;
|
|
|
|
Log4Moz.repository.rootLogger.addAppender(new Log4Moz.DumpAppender());
|
|
|
|
|
|
|
|
let guidSvc = new FakeGUIDService();
|
|
|
|
let clients = new ServerCollection();
|
|
|
|
let meta_global = new ServerWBO('global');
|
|
|
|
|
2010-12-09 10:32:03 -08:00
|
|
|
let collectionsHelper = track_collections_helper();
|
|
|
|
let upd = collectionsHelper.with_updated_collection;
|
|
|
|
let collections = collectionsHelper.collections;
|
2012-03-22 15:49:50 -07:00
|
|
|
|
2010-08-23 06:20:39 -07:00
|
|
|
function wasCalledHandler(wbo) {
|
|
|
|
let handler = wbo.handler();
|
|
|
|
return function() {
|
|
|
|
wbo.wasCalled = true;
|
|
|
|
handler.apply(this, arguments);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-12-10 16:53:40 -08:00
|
|
|
let keysWBO = new ServerWBO("keys");
|
|
|
|
let cryptoColl = new ServerCollection({keys: keysWBO});
|
|
|
|
let metaColl = new ServerCollection({global: meta_global});
|
2010-08-23 06:20:39 -07:00
|
|
|
do_test_pending();
|
2011-10-27 22:25:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the bulk DELETE request sent by wipeServer.
|
|
|
|
*/
|
|
|
|
function storageHandler(request, response) {
|
|
|
|
do_check_eq("DELETE", request.method);
|
|
|
|
do_check_true(request.hasHeader("X-Confirm-Delete"));
|
|
|
|
|
|
|
|
_("Wiping out all collections.");
|
|
|
|
cryptoColl.delete({});
|
|
|
|
clients.delete({});
|
|
|
|
metaColl.delete({});
|
|
|
|
|
|
|
|
let ts = new_timestamp();
|
|
|
|
collectionsHelper.update_collection("crypto", ts);
|
|
|
|
collectionsHelper.update_collection("clients", ts);
|
|
|
|
collectionsHelper.update_collection("meta", ts);
|
|
|
|
return_timestamp(request, response, ts);
|
|
|
|
}
|
|
|
|
|
2010-08-23 06:20:39 -07:00
|
|
|
let server = httpd_setup({
|
2011-10-27 22:25:00 -07:00
|
|
|
"/1.1/johndoe/storage": storageHandler,
|
2011-04-08 14:52:08 -07:00
|
|
|
"/1.1/johndoe/storage/crypto/keys": upd("crypto", keysWBO.handler()),
|
|
|
|
"/1.1/johndoe/storage/crypto": upd("crypto", cryptoColl.handler()),
|
|
|
|
"/1.1/johndoe/storage/clients": upd("clients", clients.handler()),
|
|
|
|
"/1.1/johndoe/storage/meta/global": upd("meta", wasCalledHandler(meta_global)),
|
|
|
|
"/1.1/johndoe/storage/meta": upd("meta", wasCalledHandler(metaColl)),
|
|
|
|
"/1.1/johndoe/info/collections": collectionsHelper.handler
|
2010-08-23 06:20:39 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
_("Log in.");
|
2012-02-06 09:50:11 -08:00
|
|
|
Weave.Service.serverURL = TEST_SERVER_URL;
|
|
|
|
Weave.Service.clusterURL = TEST_CLUSTER_URL;
|
2012-03-22 15:49:50 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
_("Checking Status.sync with no credentials.");
|
|
|
|
Weave.Service.verifyAndFetchSymmetricKeys();
|
|
|
|
do_check_eq(Status.sync, CREDENTIALS_CHANGED);
|
2012-03-22 15:49:50 -07:00
|
|
|
do_check_eq(Status.login, LOGIN_FAILED_NO_PASSPHRASE);
|
2010-11-29 16:41:17 -08:00
|
|
|
|
2010-12-13 18:09:21 -08:00
|
|
|
_("Log in with an old secret phrase, is upgraded to Sync Key.");
|
|
|
|
Weave.Service.login("johndoe", "ilovejane", "my old secret phrase!!1!");
|
2012-03-22 15:49:50 -07:00
|
|
|
_("End of login");
|
2010-12-13 18:09:21 -08:00
|
|
|
do_check_true(Weave.Service.isLoggedIn);
|
2012-03-22 15:49:50 -07:00
|
|
|
do_check_true(Utils.isPassphrase(Identity.syncKey));
|
|
|
|
let syncKey = Identity.syncKey;
|
2010-12-13 18:09:21 -08:00
|
|
|
Weave.Service.startOver();
|
|
|
|
|
2012-02-06 09:50:11 -08:00
|
|
|
Weave.Service.serverURL = TEST_SERVER_URL;
|
|
|
|
Weave.Service.clusterURL = TEST_CLUSTER_URL;
|
2010-12-13 18:09:21 -08:00
|
|
|
Weave.Service.login("johndoe", "ilovejane", syncKey);
|
2010-08-23 06:20:39 -07:00
|
|
|
do_check_true(Weave.Service.isLoggedIn);
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
_("Checking that remoteSetup returns true when credentials have changed.");
|
|
|
|
Records.get(Weave.Service.metaURL).payload.syncID = "foobar";
|
|
|
|
do_check_true(Weave.Service._remoteSetup());
|
2012-03-22 15:49:50 -07:00
|
|
|
|
2010-08-23 06:20:39 -07:00
|
|
|
_("Do an initial sync.");
|
|
|
|
let beforeSync = Date.now()/1000;
|
|
|
|
Weave.Service.sync();
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
_("Checking that remoteSetup returns true.");
|
|
|
|
do_check_true(Weave.Service._remoteSetup());
|
|
|
|
|
|
|
|
_("Verify that the meta record was uploaded.");
|
2010-08-23 06:20:39 -07:00
|
|
|
do_check_eq(meta_global.data.syncID, Weave.Service.syncID);
|
|
|
|
do_check_eq(meta_global.data.storageVersion, Weave.STORAGE_VERSION);
|
|
|
|
do_check_eq(meta_global.data.engines.clients.version, Weave.Clients.version);
|
|
|
|
do_check_eq(meta_global.data.engines.clients.syncID, Weave.Clients.syncID);
|
|
|
|
|
|
|
|
_("Set the collection info hash so that sync() will remember the modified times for future runs.");
|
2010-12-09 10:32:03 -08:00
|
|
|
collections.meta = Weave.Clients.lastSync;
|
|
|
|
collections.clients = Weave.Clients.lastSync;
|
2010-08-23 06:20:39 -07:00
|
|
|
Weave.Service.sync();
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
_("Sync again and verify that meta/global wasn't downloaded again");
|
2010-08-23 06:20:39 -07:00
|
|
|
meta_global.wasCalled = false;
|
|
|
|
Weave.Service.sync();
|
|
|
|
do_check_false(meta_global.wasCalled);
|
|
|
|
|
|
|
|
_("Fake modified records. This will cause a redownload, but not reupload since it hasn't changed.");
|
|
|
|
collections.meta += 42;
|
|
|
|
meta_global.wasCalled = false;
|
|
|
|
|
|
|
|
let metaModified = meta_global.modified;
|
|
|
|
|
|
|
|
Weave.Service.sync();
|
|
|
|
do_check_true(meta_global.wasCalled);
|
|
|
|
do_check_eq(metaModified, meta_global.modified);
|
2010-11-29 16:41:17 -08:00
|
|
|
|
|
|
|
_("Checking bad passphrases.");
|
2012-03-22 15:49:50 -07:00
|
|
|
let pp = Identity.syncKey;
|
|
|
|
Identity.syncKey = "notvalid";
|
2010-11-29 16:41:17 -08:00
|
|
|
do_check_false(Weave.Service.verifyAndFetchSymmetricKeys());
|
|
|
|
do_check_eq(Status.sync, CREDENTIALS_CHANGED);
|
|
|
|
do_check_eq(Status.login, LOGIN_FAILED_INVALID_PASSPHRASE);
|
2012-03-22 15:49:50 -07:00
|
|
|
Identity.syncKey = pp;
|
2010-11-29 16:41:17 -08:00
|
|
|
do_check_true(Weave.Service.verifyAndFetchSymmetricKeys());
|
2012-03-22 15:49:50 -07:00
|
|
|
|
2010-12-10 16:53:40 -08:00
|
|
|
// changePassphrase wipes our keys, and they're regenerated on next sync.
|
|
|
|
_("Checking changed passphrase.");
|
|
|
|
let existingDefault = CollectionKeys.keyForCollection();
|
|
|
|
let existingKeysPayload = keysWBO.payload;
|
|
|
|
let newPassphrase = "bbbbbabcdeabcdeabcdeabcdea";
|
|
|
|
Weave.Service.changePassphrase(newPassphrase);
|
2012-03-22 15:49:50 -07:00
|
|
|
|
2010-12-10 16:53:40 -08:00
|
|
|
_("Local key cache is full, but different.");
|
|
|
|
do_check_true(!!CollectionKeys._default);
|
|
|
|
do_check_false(CollectionKeys._default.equals(existingDefault));
|
2012-03-22 15:49:50 -07:00
|
|
|
|
2010-12-10 16:53:40 -08:00
|
|
|
_("Server has new keys.");
|
|
|
|
do_check_true(!!keysWBO.payload);
|
|
|
|
do_check_true(!!keysWBO.modified);
|
|
|
|
do_check_neq(keysWBO.payload, existingKeysPayload);
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Try to screw up HMAC calculation.
|
|
|
|
// Re-encrypt keys with a new random keybundle, and upload them to the
|
|
|
|
// server, just as might happen with a second client.
|
|
|
|
_("Attempting to screw up HMAC by re-encrypting keys.");
|
|
|
|
let keys = CollectionKeys.asWBO();
|
2012-03-22 15:49:50 -07:00
|
|
|
let b = new BulkKeyBundle("hmacerror");
|
2010-11-29 16:41:17 -08:00
|
|
|
b.generateRandom();
|
|
|
|
collections.crypto = keys.modified = 100 + (Date.now()/1000); // Future modification time.
|
|
|
|
keys.encrypt(b);
|
|
|
|
keys.upload(Weave.Service.cryptoKeysURL);
|
2012-03-22 15:49:50 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
do_check_false(Weave.Service.verifyAndFetchSymmetricKeys());
|
|
|
|
do_check_eq(Status.login, LOGIN_FAILED_INVALID_PASSPHRASE);
|
2010-08-23 06:20:39 -07:00
|
|
|
|
|
|
|
} finally {
|
|
|
|
Weave.Svc.Prefs.resetBranch("");
|
|
|
|
server.stop(do_test_finished);
|
|
|
|
}
|
|
|
|
}
|