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/engines.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 makeSteamEngine() { michael@0: return new SyncEngine('Steam', Service); michael@0: } michael@0: michael@0: let server; michael@0: michael@0: function test_url_attributes() { michael@0: _("SyncEngine url attributes"); michael@0: let syncTesting = new SyncTestingInfrastructure(server); michael@0: Service.clusterURL = "https://cluster/"; michael@0: let engine = makeSteamEngine(); michael@0: try { michael@0: do_check_eq(engine.storageURL, "https://cluster/1.1/foo/storage/"); michael@0: do_check_eq(engine.engineURL, "https://cluster/1.1/foo/storage/steam"); michael@0: do_check_eq(engine.metaURL, "https://cluster/1.1/foo/storage/meta/global"); michael@0: } finally { michael@0: Svc.Prefs.resetBranch(""); michael@0: } michael@0: } michael@0: michael@0: function test_syncID() { michael@0: _("SyncEngine.syncID corresponds to preference"); michael@0: let syncTesting = new SyncTestingInfrastructure(server); michael@0: let engine = makeSteamEngine(); michael@0: try { michael@0: // Ensure pristine environment michael@0: do_check_eq(Svc.Prefs.get("steam.syncID"), undefined); michael@0: michael@0: // Performing the first get on the attribute will generate a new GUID. michael@0: do_check_eq(engine.syncID, "fake-guid-0"); michael@0: do_check_eq(Svc.Prefs.get("steam.syncID"), "fake-guid-0"); michael@0: michael@0: Svc.Prefs.set("steam.syncID", Utils.makeGUID()); michael@0: do_check_eq(Svc.Prefs.get("steam.syncID"), "fake-guid-1"); michael@0: do_check_eq(engine.syncID, "fake-guid-1"); michael@0: } finally { michael@0: Svc.Prefs.resetBranch(""); michael@0: } michael@0: } michael@0: michael@0: function test_lastSync() { michael@0: _("SyncEngine.lastSync and SyncEngine.lastSyncLocal correspond to preferences"); michael@0: let syncTesting = new SyncTestingInfrastructure(server); michael@0: let engine = makeSteamEngine(); michael@0: try { michael@0: // Ensure pristine environment michael@0: do_check_eq(Svc.Prefs.get("steam.lastSync"), undefined); michael@0: do_check_eq(engine.lastSync, 0); michael@0: do_check_eq(Svc.Prefs.get("steam.lastSyncLocal"), undefined); michael@0: do_check_eq(engine.lastSyncLocal, 0); michael@0: michael@0: // Floats are properly stored as floats and synced with the preference michael@0: engine.lastSync = 123.45; michael@0: do_check_eq(engine.lastSync, 123.45); michael@0: do_check_eq(Svc.Prefs.get("steam.lastSync"), "123.45"); michael@0: michael@0: // Integer is properly stored michael@0: engine.lastSyncLocal = 67890; michael@0: do_check_eq(engine.lastSyncLocal, 67890); michael@0: do_check_eq(Svc.Prefs.get("steam.lastSyncLocal"), "67890"); michael@0: michael@0: // resetLastSync() resets the value (and preference) to 0 michael@0: engine.resetLastSync(); michael@0: do_check_eq(engine.lastSync, 0); michael@0: do_check_eq(Svc.Prefs.get("steam.lastSync"), "0"); michael@0: } finally { michael@0: Svc.Prefs.resetBranch(""); michael@0: } michael@0: } michael@0: michael@0: function test_toFetch() { michael@0: _("SyncEngine.toFetch corresponds to file on disk"); michael@0: let syncTesting = new SyncTestingInfrastructure(server); michael@0: const filename = "weave/toFetch/steam.json"; michael@0: let engine = makeSteamEngine(); michael@0: try { michael@0: // Ensure pristine environment michael@0: do_check_eq(engine.toFetch.length, 0); michael@0: michael@0: // Write file to disk michael@0: let toFetch = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()]; michael@0: engine.toFetch = toFetch; michael@0: do_check_eq(engine.toFetch, toFetch); michael@0: // toFetch is written asynchronously michael@0: engine._store._sleep(0); michael@0: let fakefile = syncTesting.fakeFilesystem.fakeContents[filename]; michael@0: do_check_eq(fakefile, JSON.stringify(toFetch)); michael@0: michael@0: // Read file from disk michael@0: toFetch = [Utils.makeGUID(), Utils.makeGUID()]; michael@0: syncTesting.fakeFilesystem.fakeContents[filename] = JSON.stringify(toFetch); michael@0: engine.loadToFetch(); michael@0: do_check_eq(engine.toFetch.length, 2); michael@0: do_check_eq(engine.toFetch[0], toFetch[0]); michael@0: do_check_eq(engine.toFetch[1], toFetch[1]); michael@0: } finally { michael@0: Svc.Prefs.resetBranch(""); michael@0: } michael@0: } michael@0: michael@0: function test_previousFailed() { michael@0: _("SyncEngine.previousFailed corresponds to file on disk"); michael@0: let syncTesting = new SyncTestingInfrastructure(server); michael@0: const filename = "weave/failed/steam.json"; michael@0: let engine = makeSteamEngine(); michael@0: try { michael@0: // Ensure pristine environment michael@0: do_check_eq(engine.previousFailed.length, 0); michael@0: michael@0: // Write file to disk michael@0: let previousFailed = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()]; michael@0: engine.previousFailed = previousFailed; michael@0: do_check_eq(engine.previousFailed, previousFailed); michael@0: // previousFailed is written asynchronously michael@0: engine._store._sleep(0); michael@0: let fakefile = syncTesting.fakeFilesystem.fakeContents[filename]; michael@0: do_check_eq(fakefile, JSON.stringify(previousFailed)); michael@0: michael@0: // Read file from disk michael@0: previousFailed = [Utils.makeGUID(), Utils.makeGUID()]; michael@0: syncTesting.fakeFilesystem.fakeContents[filename] = JSON.stringify(previousFailed); michael@0: engine.loadPreviousFailed(); michael@0: do_check_eq(engine.previousFailed.length, 2); michael@0: do_check_eq(engine.previousFailed[0], previousFailed[0]); michael@0: do_check_eq(engine.previousFailed[1], previousFailed[1]); michael@0: } finally { michael@0: Svc.Prefs.resetBranch(""); michael@0: } michael@0: } michael@0: michael@0: function test_resetClient() { michael@0: _("SyncEngine.resetClient resets lastSync and toFetch"); michael@0: let syncTesting = new SyncTestingInfrastructure(server); michael@0: let engine = makeSteamEngine(); michael@0: try { michael@0: // Ensure pristine environment michael@0: do_check_eq(Svc.Prefs.get("steam.lastSync"), undefined); michael@0: do_check_eq(Svc.Prefs.get("steam.lastSyncLocal"), undefined); michael@0: do_check_eq(engine.toFetch.length, 0); michael@0: michael@0: engine.lastSync = 123.45; michael@0: engine.lastSyncLocal = 67890; michael@0: engine.toFetch = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()]; michael@0: engine.previousFailed = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()]; michael@0: michael@0: engine.resetClient(); michael@0: do_check_eq(engine.lastSync, 0); michael@0: do_check_eq(engine.lastSyncLocal, 0); michael@0: do_check_eq(engine.toFetch.length, 0); michael@0: do_check_eq(engine.previousFailed.length, 0); michael@0: } finally { michael@0: Svc.Prefs.resetBranch(""); michael@0: } michael@0: } michael@0: michael@0: function test_wipeServer() { michael@0: _("SyncEngine.wipeServer deletes server data and resets the client."); michael@0: let engine = makeSteamEngine(); michael@0: michael@0: const PAYLOAD = 42; michael@0: let steamCollection = new ServerWBO("steam", PAYLOAD); michael@0: let server = httpd_setup({ michael@0: "/1.1/foo/storage/steam": steamCollection.handler() michael@0: }); michael@0: let syncTesting = new SyncTestingInfrastructure(server); michael@0: do_test_pending(); michael@0: michael@0: try { michael@0: // Some data to reset. michael@0: engine.lastSync = 123.45; michael@0: engine.toFetch = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()]; michael@0: michael@0: _("Wipe server data and reset client."); michael@0: engine.wipeServer(); michael@0: do_check_eq(steamCollection.payload, undefined); michael@0: do_check_eq(engine.lastSync, 0); michael@0: do_check_eq(engine.toFetch.length, 0); michael@0: michael@0: } finally { michael@0: server.stop(do_test_finished); michael@0: Svc.Prefs.resetBranch(""); michael@0: } michael@0: } michael@0: michael@0: function run_test() { michael@0: server = httpd_setup({}); michael@0: test_url_attributes(); michael@0: test_syncID(); michael@0: test_lastSync(); michael@0: test_toFetch(); michael@0: test_previousFailed(); michael@0: test_resetClient(); michael@0: test_wipeServer(); michael@0: michael@0: server.stop(run_next_test); michael@0: }