2010-09-11 09:39:21 -07:00
|
|
|
Cu.import("resource://services-sync/engines.js");
|
|
|
|
Cu.import("resource://services-sync/status.js");
|
|
|
|
Cu.import("resource://services-sync/constants.js");
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
Cu.import("resource://services-sync/util.js");
|
2010-10-18 15:10:22 -07:00
|
|
|
Svc.DefaultPrefs.set("registerEngines", "");
|
|
|
|
Cu.import("resource://services-sync/service.js");
|
2011-01-18 16:23:30 -08:00
|
|
|
Cu.import("resource://services-sync/record.js");
|
2010-10-18 15:10:22 -07:00
|
|
|
|
2010-09-11 09:39:21 -07:00
|
|
|
initTestLogging();
|
|
|
|
|
|
|
|
function CatapultEngine() {
|
|
|
|
SyncEngine.call(this, "Catapult");
|
|
|
|
}
|
|
|
|
CatapultEngine.prototype = {
|
|
|
|
__proto__: SyncEngine.prototype,
|
|
|
|
exception: null, // tests fill this in
|
|
|
|
sync: function sync() {
|
|
|
|
throw this.exception;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function sync_httpd_setup() {
|
2011-02-09 22:43:01 -08:00
|
|
|
let collectionsHelper = track_collections_helper();
|
|
|
|
let upd = collectionsHelper.with_updated_collection;
|
|
|
|
let collections = collectionsHelper.collections;
|
|
|
|
|
2011-02-09 22:43:21 -08:00
|
|
|
let catapultEngine = Engines.get("catapult");
|
|
|
|
let engines = {catapult: {version: catapultEngine.version,
|
|
|
|
syncID: catapultEngine.syncID}}
|
|
|
|
|
2011-02-09 22:43:01 -08:00
|
|
|
// Track these using the collections helper, which keeps modified times
|
|
|
|
// up-to-date.
|
|
|
|
let clientsColl = new ServerCollection({}, true);
|
2011-02-09 22:43:21 -08:00
|
|
|
let keysWBO = new ServerWBO("keys");
|
2011-02-09 22:43:01 -08:00
|
|
|
let globalWBO = new ServerWBO("global", {storageVersion: STORAGE_VERSION,
|
2011-02-09 22:43:21 -08:00
|
|
|
syncID: Utils.makeGUID(),
|
|
|
|
engines: engines});
|
2011-02-09 22:43:01 -08:00
|
|
|
|
|
|
|
let handlers = {
|
2011-04-08 14:52:08 -07:00
|
|
|
"/1.1/johndoe/info/collections": collectionsHelper.handler,
|
|
|
|
"/1.1/johndoe/storage/meta/global": upd("meta", globalWBO.handler()),
|
|
|
|
"/1.1/johndoe/storage/clients": upd("clients", clientsColl.handler()),
|
|
|
|
"/1.1/johndoe/storage/crypto/keys": upd("crypto", keysWBO.handler())
|
2011-02-09 22:43:01 -08:00
|
|
|
}
|
2010-09-11 09:39:21 -07:00
|
|
|
return httpd_setup(handlers);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setUp() {
|
|
|
|
Service.username = "johndoe";
|
|
|
|
Service.password = "ilovejane";
|
2010-11-29 16:41:17 -08:00
|
|
|
Service.passphrase = "aabcdeabcdeabcdeabcdeabcde";
|
2010-09-11 09:39:21 -07:00
|
|
|
Service.clusterURL = "http://localhost:8080/";
|
|
|
|
new FakeCryptoService();
|
|
|
|
}
|
|
|
|
|
2011-02-09 22:43:01 -08:00
|
|
|
function generateAndUploadKeys() {
|
|
|
|
CollectionKeys.generateNewKeys();
|
|
|
|
let serverKeys = CollectionKeys.asWBO("crypto", "keys");
|
|
|
|
serverKeys.encrypt(Weave.Service.syncKeyBundle);
|
2011-04-08 14:52:08 -07:00
|
|
|
return serverKeys.upload("http://localhost:8080/1.1/johndoe/storage/crypto/keys").success;
|
2011-02-09 22:43:01 -08:00
|
|
|
}
|
|
|
|
|
2011-01-28 13:21:21 -08:00
|
|
|
function test_backoff500(next) {
|
2010-09-11 09:39:21 -07:00
|
|
|
_("Test: HTTP 500 sets backoff status.");
|
|
|
|
let server = sync_httpd_setup();
|
|
|
|
setUp();
|
|
|
|
|
|
|
|
let engine = Engines.get("catapult");
|
|
|
|
engine.enabled = true;
|
|
|
|
engine.exception = {status: 500};
|
|
|
|
|
|
|
|
try {
|
|
|
|
do_check_false(Status.enforceBackoff);
|
2011-02-09 22:43:01 -08:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Forcibly create and upload keys here -- otherwise we don't get to the 500!
|
2011-02-09 22:43:01 -08:00
|
|
|
do_check_true(generateAndUploadKeys());
|
|
|
|
|
2010-09-11 09:39:21 -07:00
|
|
|
Service.login();
|
|
|
|
Service.sync();
|
|
|
|
do_check_true(Status.enforceBackoff);
|
|
|
|
} finally {
|
|
|
|
Status.resetBackoff();
|
|
|
|
Service.startOver();
|
|
|
|
}
|
2011-01-31 20:55:48 -08:00
|
|
|
server.stop(next);
|
2010-09-11 09:39:21 -07:00
|
|
|
}
|
|
|
|
|
2011-01-28 13:21:21 -08:00
|
|
|
function test_backoff503(next) {
|
2010-09-11 09:39:21 -07:00
|
|
|
_("Test: HTTP 503 with Retry-After header leads to backoff notification and sets backoff status.");
|
|
|
|
let server = sync_httpd_setup();
|
|
|
|
setUp();
|
|
|
|
|
|
|
|
const BACKOFF = 42;
|
|
|
|
let engine = Engines.get("catapult");
|
|
|
|
engine.enabled = true;
|
|
|
|
engine.exception = {status: 503,
|
|
|
|
headers: {"retry-after": BACKOFF}};
|
|
|
|
|
|
|
|
let backoffInterval;
|
|
|
|
Svc.Obs.add("weave:service:backoff:interval", function (subject) {
|
|
|
|
backoffInterval = subject;
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
do_check_false(Status.enforceBackoff);
|
|
|
|
|
2011-02-09 22:43:01 -08:00
|
|
|
do_check_true(generateAndUploadKeys());
|
|
|
|
|
2010-09-11 09:39:21 -07:00
|
|
|
Service.login();
|
|
|
|
Service.sync();
|
|
|
|
|
|
|
|
do_check_true(Status.enforceBackoff);
|
|
|
|
do_check_eq(backoffInterval, BACKOFF);
|
|
|
|
} finally {
|
|
|
|
Status.resetBackoff();
|
|
|
|
Service.startOver();
|
|
|
|
}
|
2011-01-31 20:55:48 -08:00
|
|
|
server.stop(next);
|
2010-09-11 09:39:21 -07:00
|
|
|
}
|
|
|
|
|
2011-01-28 13:21:21 -08:00
|
|
|
function test_overQuota(next) {
|
2010-09-11 09:39:21 -07:00
|
|
|
_("Test: HTTP 400 with body error code 14 means over quota.");
|
|
|
|
let server = sync_httpd_setup();
|
|
|
|
setUp();
|
|
|
|
|
|
|
|
let engine = Engines.get("catapult");
|
|
|
|
engine.enabled = true;
|
|
|
|
engine.exception = {status: 400,
|
|
|
|
toString: function() "14"};
|
|
|
|
|
|
|
|
try {
|
|
|
|
do_check_eq(Status.sync, SYNC_SUCCEEDED);
|
|
|
|
|
2011-02-09 22:43:01 -08:00
|
|
|
do_check_true(generateAndUploadKeys());
|
|
|
|
|
2010-09-11 09:39:21 -07:00
|
|
|
Service.login();
|
|
|
|
Service.sync();
|
|
|
|
|
|
|
|
do_check_eq(Status.sync, OVER_QUOTA);
|
|
|
|
} finally {
|
|
|
|
Status.resetSync();
|
|
|
|
Service.startOver();
|
|
|
|
}
|
2011-01-31 20:55:48 -08:00
|
|
|
server.stop(next);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_service_networkError(next) {
|
2011-02-03 10:23:39 -08:00
|
|
|
_("Test: Connection refused error from Service.sync() leads to the right status code.");
|
2011-01-31 20:55:48 -08:00
|
|
|
setUp();
|
|
|
|
// Provoke connection refused.
|
|
|
|
Service.clusterURL = "http://localhost:12345/";
|
2011-02-03 10:23:39 -08:00
|
|
|
Service._ignorableErrorCount = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
do_check_eq(Status.sync, SYNC_SUCCEEDED);
|
|
|
|
|
|
|
|
Service._loggedIn = true;
|
|
|
|
Service.sync();
|
|
|
|
|
|
|
|
do_check_eq(Status.sync, LOGIN_FAILED_NETWORK_ERROR);
|
|
|
|
do_check_eq(Service._ignorableErrorCount, 1);
|
|
|
|
} finally {
|
|
|
|
Status.resetSync();
|
|
|
|
Service.startOver();
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_service_offline(next) {
|
|
|
|
_("Test: Wanting to sync in offline mode leads to the right status code but does not increment the ignorable error count.");
|
|
|
|
setUp();
|
|
|
|
Svc.IO.offline = true;
|
|
|
|
Service._ignorableErrorCount = 0;
|
|
|
|
|
2011-01-31 20:55:48 -08:00
|
|
|
try {
|
|
|
|
do_check_eq(Status.sync, SYNC_SUCCEEDED);
|
|
|
|
|
|
|
|
Service._loggedIn = true;
|
|
|
|
Service.sync();
|
|
|
|
|
|
|
|
do_check_eq(Status.sync, LOGIN_FAILED_NETWORK_ERROR);
|
2011-02-03 10:23:39 -08:00
|
|
|
do_check_eq(Service._ignorableErrorCount, 0);
|
2011-01-31 20:55:48 -08:00
|
|
|
} finally {
|
|
|
|
Status.resetSync();
|
|
|
|
Service.startOver();
|
|
|
|
}
|
2011-02-03 10:23:39 -08:00
|
|
|
Svc.IO.offline = false;
|
2011-01-31 20:55:48 -08:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
2011-02-03 10:23:39 -08:00
|
|
|
function test_service_reset_ignorableErrorCount(next) {
|
|
|
|
_("Test: Successful sync resets the ignorable error count.");
|
|
|
|
let server = sync_httpd_setup();
|
|
|
|
setUp();
|
|
|
|
Service._ignorableErrorCount = 10;
|
|
|
|
|
2011-02-09 22:43:21 -08:00
|
|
|
// Disable the engine so that sync completes.
|
|
|
|
let engine = Engines.get("catapult");
|
|
|
|
engine.enabled = false;
|
|
|
|
|
2011-02-03 10:23:39 -08:00
|
|
|
try {
|
|
|
|
do_check_eq(Status.sync, SYNC_SUCCEEDED);
|
|
|
|
|
2011-02-09 22:43:01 -08:00
|
|
|
do_check_true(generateAndUploadKeys());
|
|
|
|
|
2011-02-03 10:23:39 -08:00
|
|
|
Service.login();
|
|
|
|
Service.sync();
|
|
|
|
|
|
|
|
do_check_eq(Status.sync, SYNC_SUCCEEDED);
|
|
|
|
do_check_eq(Service._ignorableErrorCount, 0);
|
|
|
|
} finally {
|
|
|
|
Status.resetSync();
|
|
|
|
Service.startOver();
|
|
|
|
}
|
|
|
|
server.stop(next);
|
|
|
|
}
|
|
|
|
|
2011-01-31 20:55:48 -08:00
|
|
|
function test_engine_networkError(next) {
|
|
|
|
_("Test: Network related exceptions from engine.sync() lead to the right status code.");
|
|
|
|
let server = sync_httpd_setup();
|
|
|
|
setUp();
|
2011-02-03 10:23:39 -08:00
|
|
|
Service._ignorableErrorCount = 0;
|
2011-01-31 20:55:48 -08:00
|
|
|
|
|
|
|
let engine = Engines.get("catapult");
|
|
|
|
engine.enabled = true;
|
|
|
|
engine.exception = Components.Exception("NS_ERROR_UNKNOWN_HOST",
|
|
|
|
Cr.NS_ERROR_UNKNOWN_HOST);
|
|
|
|
|
|
|
|
try {
|
|
|
|
do_check_eq(Status.sync, SYNC_SUCCEEDED);
|
|
|
|
|
2011-02-09 22:43:01 -08:00
|
|
|
do_check_true(generateAndUploadKeys());
|
|
|
|
|
2011-01-31 20:55:48 -08:00
|
|
|
Service.login();
|
|
|
|
Service.sync();
|
|
|
|
|
|
|
|
do_check_eq(Status.sync, LOGIN_FAILED_NETWORK_ERROR);
|
2011-02-03 10:23:39 -08:00
|
|
|
do_check_eq(Service._ignorableErrorCount, 1);
|
2011-01-31 20:55:48 -08:00
|
|
|
} finally {
|
|
|
|
Status.resetSync();
|
|
|
|
Service.startOver();
|
|
|
|
}
|
|
|
|
server.stop(next);
|
2010-09-11 09:39:21 -07:00
|
|
|
}
|
|
|
|
|
2011-01-28 09:39:12 -08:00
|
|
|
// Slightly misplaced test as it doesn't actually test checkServerError,
|
|
|
|
// but the observer for "weave:engine:sync:apply-failed".
|
2011-01-28 13:21:21 -08:00
|
|
|
function test_engine_applyFailed(next) {
|
2011-01-28 09:39:12 -08:00
|
|
|
let server = sync_httpd_setup();
|
|
|
|
setUp();
|
|
|
|
|
|
|
|
let engine = Engines.get("catapult");
|
|
|
|
engine.enabled = true;
|
2011-02-09 22:43:21 -08:00
|
|
|
delete engine.exception;
|
2011-01-28 09:39:12 -08:00
|
|
|
engine.sync = function sync() {
|
|
|
|
Svc.Obs.notify("weave:engine:sync:apply-failed", {}, "steam");
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
do_check_eq(Status.engines["steam"], undefined);
|
|
|
|
|
2011-02-09 22:43:01 -08:00
|
|
|
do_check_true(generateAndUploadKeys());
|
|
|
|
|
2011-01-28 09:39:12 -08:00
|
|
|
Service.login();
|
|
|
|
Service.sync();
|
|
|
|
|
|
|
|
do_check_eq(Status.engines["steam"], ENGINE_APPLY_FAIL);
|
|
|
|
} finally {
|
|
|
|
Status.resetSync();
|
|
|
|
Service.startOver();
|
|
|
|
}
|
2011-01-31 20:55:48 -08:00
|
|
|
server.stop(next);
|
2011-01-28 09:39:12 -08:00
|
|
|
}
|
|
|
|
|
2010-09-11 09:39:21 -07:00
|
|
|
function run_test() {
|
2010-10-21 04:47:47 -07:00
|
|
|
if (DISABLE_TESTS_BUG_604565)
|
|
|
|
return;
|
|
|
|
|
2011-01-28 13:21:21 -08:00
|
|
|
do_test_pending();
|
2011-02-09 22:43:21 -08:00
|
|
|
|
|
|
|
// Register engine once.
|
|
|
|
Engines.register(CatapultEngine);
|
2011-01-28 13:21:21 -08:00
|
|
|
asyncChainTests(test_backoff500,
|
|
|
|
test_backoff503,
|
|
|
|
test_overQuota,
|
2011-01-31 20:55:48 -08:00
|
|
|
test_service_networkError,
|
2011-02-03 10:23:39 -08:00
|
|
|
test_service_offline,
|
|
|
|
test_service_reset_ignorableErrorCount,
|
2011-01-31 20:55:48 -08:00
|
|
|
test_engine_networkError,
|
2011-01-28 13:21:21 -08:00
|
|
|
test_engine_applyFailed,
|
|
|
|
do_test_finished)();
|
2010-09-11 09:39:21 -07:00
|
|
|
}
|