2011-06-27 06:22:49 -07:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
2011-06-28 18:47:22 -07:00
|
|
|
Cu.import("resource://services-sync/record.js");
|
|
|
|
Cu.import("resource://services-sync/identity.js");
|
|
|
|
Cu.import("resource://services-sync/engines.js");
|
2011-06-27 06:22:49 -07:00
|
|
|
Cu.import("resource://services-sync/engines/clients.js");
|
|
|
|
Cu.import("resource://services-sync/constants.js");
|
|
|
|
Cu.import("resource://services-sync/policies.js");
|
|
|
|
Cu.import("resource://services-sync/status.js");
|
|
|
|
|
2011-06-28 18:47:22 -07:00
|
|
|
Svc.DefaultPrefs.set("registerEngines", "");
|
|
|
|
Cu.import("resource://services-sync/service.js");
|
2011-06-27 06:26:04 -07:00
|
|
|
|
|
|
|
function sync_httpd_setup() {
|
2011-06-28 18:47:22 -07:00
|
|
|
let global = new ServerWBO("global", {
|
|
|
|
syncID: Service.syncID,
|
|
|
|
storageVersion: STORAGE_VERSION,
|
|
|
|
engines: {clients: {version: Clients.version,
|
|
|
|
syncID: Clients.syncID}}
|
|
|
|
});
|
|
|
|
let clientsColl = new ServerCollection({}, true);
|
|
|
|
|
|
|
|
// Tracking info/collections.
|
|
|
|
let collectionsHelper = track_collections_helper();
|
|
|
|
let upd = collectionsHelper.with_updated_collection;
|
|
|
|
|
|
|
|
return httpd_setup({
|
|
|
|
"/1.1/johndoe/storage/meta/global": upd("meta", global.handler()),
|
|
|
|
"/1.1/johndoe/info/collections": collectionsHelper.handler,
|
|
|
|
"/1.1/johndoe/storage/crypto/keys":
|
|
|
|
upd("crypto", (new ServerWBO("keys")).handler()),
|
|
|
|
"/1.1/johndoe/storage/clients": upd("clients", clientsColl.handler())
|
|
|
|
});
|
2011-06-27 06:26:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function setUp() {
|
|
|
|
Service.username = "johndoe";
|
|
|
|
Service.password = "ilovejane";
|
2011-06-28 18:47:22 -07:00
|
|
|
Service.passphrase = "abcdeabcdeabcdeabcdeabcdea";
|
2011-06-27 06:26:04 -07:00
|
|
|
Service.clusterURL = "http://localhost:8080/";
|
2011-06-28 18:47:22 -07:00
|
|
|
|
|
|
|
generateNewKeys();
|
|
|
|
let serverKeys = CollectionKeys.asWBO("crypto", "keys");
|
|
|
|
serverKeys.encrypt(Service.syncKeyBundle);
|
|
|
|
return serverKeys.upload(Service.cryptoKeysURL);
|
2011-06-27 06:26:04 -07:00
|
|
|
}
|
|
|
|
|
2011-06-27 06:22:49 -07:00
|
|
|
function run_test() {
|
|
|
|
initTestLogging("Trace");
|
|
|
|
|
|
|
|
Log4Moz.repository.getLogger("Sync.Service").level = Log4Moz.Level.Trace;
|
|
|
|
Log4Moz.repository.getLogger("Sync.SyncScheduler").level = Log4Moz.Level.Trace;
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
add_test(function test_prefAttributes() {
|
|
|
|
_("Test various attributes corresponding to preferences.");
|
|
|
|
|
|
|
|
const INTERVAL = 42 * 60 * 1000; // 42 minutes
|
|
|
|
const THRESHOLD = 3142;
|
|
|
|
const SCORE = 2718;
|
|
|
|
const TIMESTAMP1 = 1275493471649;
|
|
|
|
|
|
|
|
try {
|
|
|
|
_("'globalScore' corresponds to preference, defaults to zero.");
|
|
|
|
do_check_eq(Svc.Prefs.get('globalScore'), undefined);
|
|
|
|
do_check_eq(SyncScheduler.globalScore, 0);
|
|
|
|
SyncScheduler.globalScore = SCORE;
|
|
|
|
do_check_eq(SyncScheduler.globalScore, SCORE);
|
|
|
|
do_check_eq(Svc.Prefs.get('globalScore'), SCORE);
|
|
|
|
} finally {
|
|
|
|
Svc.Prefs.resetBranch("");
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
add_test(function test_updateClientMode() {
|
2011-06-27 06:24:33 -07:00
|
|
|
_("Test updateClientMode adjusts scheduling attributes based on # of clients appropriately");
|
2011-06-27 06:22:49 -07:00
|
|
|
do_check_eq(SyncScheduler.syncThreshold, SINGLE_USER_THRESHOLD);
|
2011-06-27 06:24:33 -07:00
|
|
|
do_check_eq(SyncScheduler.syncInterval, SINGLE_USER_SYNC);
|
|
|
|
do_check_false(SyncScheduler.numClients > 1);
|
|
|
|
do_check_true(SyncScheduler.idle);
|
2011-06-27 06:22:49 -07:00
|
|
|
|
|
|
|
// Trigger a change in interval & threshold by adding a client.
|
2011-06-28 18:47:22 -07:00
|
|
|
Clients._store.create({id: "foo", cleartext: "bar"});
|
2011-06-27 06:22:49 -07:00
|
|
|
SyncScheduler.updateClientMode();
|
|
|
|
|
|
|
|
do_check_eq(SyncScheduler.syncThreshold, MULTI_DEVICE_THRESHOLD);
|
2011-06-27 06:24:33 -07:00
|
|
|
do_check_eq(SyncScheduler.syncInterval, MULTI_DEVICE_IDLE_SYNC);
|
|
|
|
do_check_true(SyncScheduler.numClients > 1);
|
|
|
|
do_check_true(SyncScheduler.idle);
|
2011-06-27 06:22:49 -07:00
|
|
|
|
|
|
|
// Resets the number of clients to 0.
|
2011-06-28 18:47:22 -07:00
|
|
|
Clients.resetClient();
|
2011-06-27 06:22:49 -07:00
|
|
|
SyncScheduler.updateClientMode();
|
|
|
|
|
|
|
|
// Goes back to single user if # clients is 1.
|
2011-06-27 06:24:33 -07:00
|
|
|
do_check_eq(SyncScheduler.numClients, 1);
|
2011-06-27 06:22:49 -07:00
|
|
|
do_check_eq(SyncScheduler.syncThreshold, SINGLE_USER_THRESHOLD);
|
2011-06-27 06:24:33 -07:00
|
|
|
do_check_eq(SyncScheduler.syncInterval, SINGLE_USER_SYNC);
|
|
|
|
do_check_false(SyncScheduler.numClients > 1);
|
|
|
|
do_check_true(SyncScheduler.idle);
|
2011-06-28 18:47:22 -07:00
|
|
|
|
|
|
|
Svc.Prefs.resetBranch("");
|
|
|
|
Clients.resetClient();
|
2011-06-27 06:22:49 -07:00
|
|
|
run_next_test();
|
|
|
|
});
|
|
|
|
|
2011-06-27 06:26:04 -07:00
|
|
|
add_test(function test_masterpassword_locked_retry_interval() {
|
|
|
|
_("Test Status.login = MASTER_PASSWORD_LOCKED results in reschedule at MASTER_PASSWORD interval");
|
2011-06-27 06:22:49 -07:00
|
|
|
let loginFailed = false;
|
2011-06-28 18:47:22 -07:00
|
|
|
Svc.Obs.add("weave:service:login:error", function onLoginError() {
|
|
|
|
Svc.Obs.remove("weave:service:login:error", onLoginError);
|
2011-06-27 06:22:49 -07:00
|
|
|
loginFailed = true;
|
|
|
|
});
|
|
|
|
|
2011-06-27 06:26:04 -07:00
|
|
|
let rescheduleInterval = false;
|
2011-06-28 18:47:22 -07:00
|
|
|
SyncScheduler._scheduleAtInterval = SyncScheduler.scheduleAtInterval;
|
2011-06-27 06:22:49 -07:00
|
|
|
SyncScheduler.scheduleAtInterval = function (interval) {
|
2011-06-27 06:26:04 -07:00
|
|
|
rescheduleInterval = true;
|
2011-06-27 06:22:49 -07:00
|
|
|
do_check_eq(interval, MASTER_PASSWORD_LOCKED_RETRY_INTERVAL);
|
|
|
|
};
|
|
|
|
|
2011-06-28 18:47:22 -07:00
|
|
|
Service._verifyLogin = Service.verifyLogin;
|
2011-06-27 06:26:04 -07:00
|
|
|
Service.verifyLogin = function () {
|
|
|
|
Status.login = MASTER_PASSWORD_LOCKED;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
let server = sync_httpd_setup();
|
|
|
|
setUp();
|
|
|
|
|
2011-06-27 06:22:49 -07:00
|
|
|
Service.sync();
|
|
|
|
|
|
|
|
do_check_true(loginFailed);
|
2011-06-27 06:26:04 -07:00
|
|
|
do_check_eq(Status.login, MASTER_PASSWORD_LOCKED);
|
|
|
|
do_check_true(rescheduleInterval);
|
2011-06-28 18:47:22 -07:00
|
|
|
|
|
|
|
Service.verifyLogin = Service._verifyLogin;
|
|
|
|
SyncScheduler.scheduleAtInterval = SyncScheduler._scheduleAtInterval;
|
|
|
|
|
|
|
|
Svc.Prefs.resetBranch("");
|
|
|
|
Clients.resetClient();
|
2011-06-27 06:26:04 -07:00
|
|
|
server.stop(run_next_test);
|
2011-06-27 06:22:49 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
add_test(function test_calculateBackoff() {
|
|
|
|
do_check_eq(Status.backoffInterval, 0);
|
|
|
|
|
2011-06-28 18:47:22 -07:00
|
|
|
// Test no interval larger than the maximum backoff is used if
|
|
|
|
// Status.backoffInterval is smaller.
|
|
|
|
Status.backoffInterval = 5;
|
|
|
|
let backoffInterval = Utils.calculateBackoff(50, MAXIMUM_BACKOFF_INTERVAL);
|
2011-06-27 06:22:49 -07:00
|
|
|
|
2011-06-28 18:47:22 -07:00
|
|
|
do_check_eq(backoffInterval, MAXIMUM_BACKOFF_INTERVAL);
|
2011-06-27 06:22:49 -07:00
|
|
|
|
2011-06-28 18:47:22 -07:00
|
|
|
// Test Status.backoffInterval is used if it is
|
|
|
|
// larger than MAXIMUM_BACKOFF_INTERVAL.
|
|
|
|
Status.backoffInterval = MAXIMUM_BACKOFF_INTERVAL + 10;
|
|
|
|
backoffInterval = Utils.calculateBackoff(50, MAXIMUM_BACKOFF_INTERVAL);
|
2011-06-27 06:22:49 -07:00
|
|
|
|
2011-06-28 18:47:22 -07:00
|
|
|
do_check_eq(backoffInterval, MAXIMUM_BACKOFF_INTERVAL + 10);
|
|
|
|
|
|
|
|
Status.backoffInterval = 0;
|
|
|
|
Svc.Prefs.resetBranch("");
|
|
|
|
Clients.resetClient();
|
2011-06-27 06:22:49 -07:00
|
|
|
run_next_test();
|
|
|
|
});
|
|
|
|
|
|
|
|
add_test(function test_scheduleNextSync() {
|
|
|
|
// The pre-set value for nextSync
|
|
|
|
let initial_nextSync;
|
|
|
|
|
|
|
|
// The estimated syncInterval value after calling scheduleNextSync.
|
|
|
|
let syncInterval;
|
|
|
|
|
|
|
|
|
|
|
|
// Test backoffInterval is 0 as expected.
|
|
|
|
do_check_eq(Status.backoffInterval, 0);
|
|
|
|
|
|
|
|
_("Test setting sync interval when nextSync == 0");
|
|
|
|
initial_nextSync = SyncScheduler.nextSync = 0;
|
|
|
|
let expectedInterval = SINGLE_USER_SYNC;
|
|
|
|
SyncScheduler.scheduleNextSync();
|
|
|
|
|
|
|
|
// Test nextSync value was changed.
|
|
|
|
do_check_neq(SyncScheduler.nextSync, initial_nextSync);
|
|
|
|
syncInterval = SyncScheduler.nextSync - Date.now();
|
|
|
|
_("Sync Interval: " + syncInterval);
|
|
|
|
|
|
|
|
// syncInterval is smaller than expectedInterval
|
|
|
|
// since some time has passed.
|
|
|
|
do_check_true(syncInterval <= expectedInterval);
|
|
|
|
SyncScheduler._syncTimer.clear();
|
|
|
|
|
|
|
|
|
|
|
|
_("Test setting sync interval when nextSync != 0");
|
|
|
|
// Schedule next sync for some time in the future
|
|
|
|
expectedInterval = 5000;
|
|
|
|
initial_nextSync = SyncScheduler.nextSync = Date.now() + expectedInterval;
|
|
|
|
SyncScheduler.scheduleNextSync();
|
|
|
|
|
|
|
|
syncInterval = SyncScheduler.nextSync - Date.now();
|
|
|
|
_("Sync Interval: " + syncInterval);
|
|
|
|
|
|
|
|
// syncInterval is smaller than expectedInterval
|
|
|
|
// since some time has passed.
|
|
|
|
do_check_true(syncInterval <= expectedInterval);
|
|
|
|
SyncScheduler._syncTimer.clear();
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
});
|
|
|
|
|
|
|
|
add_test(function test_handleSyncError() {
|
|
|
|
let scheduleNextSyncCalled = 0;
|
|
|
|
let scheduleAtIntervalCalled = 0;
|
|
|
|
|
|
|
|
SyncScheduler.scheduleNextSync = function () {
|
|
|
|
scheduleNextSyncCalled++;
|
|
|
|
}
|
|
|
|
|
|
|
|
SyncScheduler.scheduleAtInterval = function () {
|
|
|
|
scheduleAtIntervalCalled++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure expected initial environment.
|
|
|
|
do_check_eq(SyncScheduler._syncErrors, 0);
|
|
|
|
do_check_false(Status.enforceBackoff);
|
|
|
|
|
|
|
|
// Call handleSyncError several times & ensure it
|
|
|
|
// functions correctly.
|
|
|
|
SyncScheduler.handleSyncError();
|
|
|
|
do_check_eq(scheduleNextSyncCalled, 1);
|
|
|
|
do_check_eq(scheduleAtIntervalCalled, 0);
|
|
|
|
do_check_eq(SyncScheduler._syncErrors, 1);
|
|
|
|
do_check_false(Status.enforceBackoff);
|
|
|
|
|
|
|
|
SyncScheduler.handleSyncError();
|
|
|
|
do_check_eq(scheduleNextSyncCalled, 2);
|
|
|
|
do_check_eq(scheduleAtIntervalCalled, 0);
|
|
|
|
do_check_eq(SyncScheduler._syncErrors, 2);
|
|
|
|
do_check_false(Status.enforceBackoff);
|
|
|
|
|
|
|
|
SyncScheduler.handleSyncError();
|
|
|
|
do_check_eq(scheduleNextSyncCalled, 2);
|
|
|
|
do_check_eq(scheduleAtIntervalCalled, 1);
|
|
|
|
do_check_eq(SyncScheduler._syncErrors, 3);
|
|
|
|
do_check_true(Status.enforceBackoff);
|
|
|
|
|
2011-06-27 06:24:33 -07:00
|
|
|
// Status.enforceBackoff is false but there are still errors.
|
2011-06-27 06:22:49 -07:00
|
|
|
Status.resetBackoff();
|
|
|
|
do_check_false(Status.enforceBackoff);
|
|
|
|
do_check_eq(SyncScheduler._syncErrors, 3);
|
|
|
|
|
|
|
|
SyncScheduler.handleSyncError();
|
|
|
|
do_check_eq(scheduleNextSyncCalled, 2);
|
|
|
|
do_check_eq(scheduleAtIntervalCalled, 2);
|
|
|
|
do_check_eq(SyncScheduler._syncErrors, 4);
|
|
|
|
do_check_true(Status.enforceBackoff);
|
|
|
|
|
2011-06-28 18:47:22 -07:00
|
|
|
Svc.Prefs.resetBranch("");
|
|
|
|
Clients.resetClient();
|
2011-06-27 06:22:49 -07:00
|
|
|
run_next_test();
|
|
|
|
});
|
2011-06-28 18:47:22 -07:00
|
|
|
|
|
|
|
add_test(function test_client_sync_finish_updateClientMode() {
|
|
|
|
let server = sync_httpd_setup();
|
|
|
|
setUp();
|
|
|
|
|
|
|
|
// Confirm defaults.
|
|
|
|
do_check_eq(SyncScheduler.syncThreshold, SINGLE_USER_THRESHOLD);
|
|
|
|
do_check_eq(SyncScheduler.syncInterval, SINGLE_USER_SYNC);
|
|
|
|
do_check_false(SyncScheduler.numClients > 1);
|
|
|
|
do_check_true(SyncScheduler.idle);
|
|
|
|
|
|
|
|
// Trigger a change in interval & threshold by adding a client.
|
|
|
|
Clients._store.create({id: "foo", cleartext: "bar"});
|
|
|
|
do_check_eq(SyncScheduler.numClients, 1);
|
|
|
|
SyncScheduler.updateClientMode();
|
|
|
|
Service.sync();
|
|
|
|
|
|
|
|
do_check_eq(SyncScheduler.syncThreshold, MULTI_DEVICE_THRESHOLD);
|
|
|
|
do_check_eq(SyncScheduler.syncInterval, MULTI_DEVICE_IDLE_SYNC);
|
|
|
|
do_check_true(SyncScheduler.numClients > 1);
|
|
|
|
do_check_true(SyncScheduler.idle);
|
|
|
|
|
|
|
|
// Resets the number of clients to 0.
|
|
|
|
Clients.resetClient();
|
|
|
|
Service.sync();
|
|
|
|
|
|
|
|
// Goes back to single user if # clients is 1.
|
|
|
|
do_check_eq(SyncScheduler.numClients, 1);
|
|
|
|
do_check_eq(SyncScheduler.syncThreshold, SINGLE_USER_THRESHOLD);
|
|
|
|
do_check_eq(SyncScheduler.syncInterval, SINGLE_USER_SYNC);
|
|
|
|
do_check_false(SyncScheduler.numClients > 1);
|
|
|
|
do_check_true(SyncScheduler.idle);
|
|
|
|
|
|
|
|
Svc.Prefs.resetBranch("");
|
|
|
|
Clients.resetClient();
|
|
|
|
server.stop(run_next_test);
|
|
|
|
});
|
|
|
|
|
|
|
|
add_test(function test_sync_at_startup() {
|
|
|
|
Svc.Obs.add("weave:service:sync:finish", function onSyncFinish() {
|
|
|
|
Svc.Obs.remove("weave:service:sync:finish", onSyncFinish);
|
|
|
|
Service.resetClient();
|
|
|
|
server.stop(run_next_test);
|
|
|
|
});
|
|
|
|
|
|
|
|
let server = sync_httpd_setup();
|
|
|
|
setUp();
|
|
|
|
|
|
|
|
Service.delayedAutoConnect(0);
|
|
|
|
});
|