services/sync/tests/unit/test_service_sync_updateEnabledEngines.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 Cu.import("resource://services-sync/constants.js");
michael@0 5 Cu.import("resource://services-sync/engines.js");
michael@0 6 Cu.import("resource://services-sync/engines/clients.js");
michael@0 7 Cu.import("resource://services-sync/record.js");
michael@0 8 Cu.import("resource://services-sync/service.js");
michael@0 9 Cu.import("resource://services-sync/util.js");
michael@0 10 Cu.import("resource://testing-common/services/sync/utils.js");
michael@0 11
michael@0 12 initTestLogging();
michael@0 13 Service.engineManager.clear();
michael@0 14
michael@0 15 function QuietStore() {
michael@0 16 Store.call("Quiet");
michael@0 17 }
michael@0 18 QuietStore.prototype = {
michael@0 19 getAllIDs: function getAllIDs() {
michael@0 20 return [];
michael@0 21 }
michael@0 22 }
michael@0 23
michael@0 24 function SteamEngine() {
michael@0 25 SyncEngine.call(this, "Steam", Service);
michael@0 26 }
michael@0 27 SteamEngine.prototype = {
michael@0 28 __proto__: SyncEngine.prototype,
michael@0 29 // We're not interested in engine sync but what the service does.
michael@0 30 _storeObj: QuietStore,
michael@0 31
michael@0 32 _sync: function _sync() {
michael@0 33 this._syncStartup();
michael@0 34 }
michael@0 35 };
michael@0 36 Service.engineManager.register(SteamEngine);
michael@0 37
michael@0 38 function StirlingEngine() {
michael@0 39 SyncEngine.call(this, "Stirling", Service);
michael@0 40 }
michael@0 41 StirlingEngine.prototype = {
michael@0 42 __proto__: SteamEngine.prototype,
michael@0 43 // This engine's enabled state is the same as the SteamEngine's.
michael@0 44 get prefName() "steam"
michael@0 45 };
michael@0 46 Service.engineManager.register(StirlingEngine);
michael@0 47
michael@0 48 // Tracking info/collections.
michael@0 49 let collectionsHelper = track_collections_helper();
michael@0 50 let upd = collectionsHelper.with_updated_collection;
michael@0 51
michael@0 52 function sync_httpd_setup(handlers) {
michael@0 53
michael@0 54 handlers["/1.1/johndoe/info/collections"] = collectionsHelper.handler;
michael@0 55 delete collectionsHelper.collections.crypto;
michael@0 56 delete collectionsHelper.collections.meta;
michael@0 57
michael@0 58 let cr = new ServerWBO("keys");
michael@0 59 handlers["/1.1/johndoe/storage/crypto/keys"] =
michael@0 60 upd("crypto", cr.handler());
michael@0 61
michael@0 62 let cl = new ServerCollection();
michael@0 63 handlers["/1.1/johndoe/storage/clients"] =
michael@0 64 upd("clients", cl.handler());
michael@0 65
michael@0 66 return httpd_setup(handlers);
michael@0 67 }
michael@0 68
michael@0 69 function setUp(server) {
michael@0 70 new SyncTestingInfrastructure(server, "johndoe", "ilovejane",
michael@0 71 "abcdeabcdeabcdeabcdeabcdea");
michael@0 72 // Ensure that the server has valid keys so that logging in will work and not
michael@0 73 // result in a server wipe, rendering many of these tests useless.
michael@0 74 generateNewKeys(Service.collectionKeys);
michael@0 75 let serverKeys = Service.collectionKeys.asWBO("crypto", "keys");
michael@0 76 serverKeys.encrypt(Service.identity.syncKeyBundle);
michael@0 77 return serverKeys.upload(Service.resource(Service.cryptoKeysURL)).success;
michael@0 78 }
michael@0 79
michael@0 80 const PAYLOAD = 42;
michael@0 81
michael@0 82
michael@0 83 function run_test() {
michael@0 84 initTestLogging("Trace");
michael@0 85 Log.repository.getLogger("Sync.Service").level = Log.Level.Trace;
michael@0 86 Log.repository.getLogger("Sync.ErrorHandler").level = Log.Level.Trace;
michael@0 87
michael@0 88 run_next_test();
michael@0 89 }
michael@0 90
michael@0 91 add_test(function test_newAccount() {
michael@0 92 _("Test: New account does not disable locally enabled engines.");
michael@0 93 let engine = Service.engineManager.get("steam");
michael@0 94 let server = sync_httpd_setup({
michael@0 95 "/1.1/johndoe/storage/meta/global": new ServerWBO("global", {}).handler(),
michael@0 96 "/1.1/johndoe/storage/steam": new ServerWBO("steam", {}).handler()
michael@0 97 });
michael@0 98 setUp(server);
michael@0 99
michael@0 100 try {
michael@0 101 _("Engine is enabled from the beginning.");
michael@0 102 Service._ignorePrefObserver = true;
michael@0 103 engine.enabled = true;
michael@0 104 Service._ignorePrefObserver = false;
michael@0 105
michael@0 106 _("Sync.");
michael@0 107 Service.sync();
michael@0 108
michael@0 109 _("Engine continues to be enabled.");
michael@0 110 do_check_true(engine.enabled);
michael@0 111 } finally {
michael@0 112 Service.startOver();
michael@0 113 server.stop(run_next_test);
michael@0 114 }
michael@0 115 });
michael@0 116
michael@0 117 add_test(function test_enabledLocally() {
michael@0 118 _("Test: Engine is disabled on remote clients and enabled locally");
michael@0 119 Service.syncID = "abcdefghij";
michael@0 120 let engine = Service.engineManager.get("steam");
michael@0 121 let metaWBO = new ServerWBO("global", {syncID: Service.syncID,
michael@0 122 storageVersion: STORAGE_VERSION,
michael@0 123 engines: {}});
michael@0 124 let server = sync_httpd_setup({
michael@0 125 "/1.1/johndoe/storage/meta/global": metaWBO.handler(),
michael@0 126 "/1.1/johndoe/storage/steam": new ServerWBO("steam", {}).handler()
michael@0 127 });
michael@0 128 setUp(server);
michael@0 129
michael@0 130 try {
michael@0 131 _("Enable engine locally.");
michael@0 132 engine.enabled = true;
michael@0 133
michael@0 134 _("Sync.");
michael@0 135 Service.sync();
michael@0 136
michael@0 137 _("Meta record now contains the new engine.");
michael@0 138 do_check_true(!!metaWBO.data.engines.steam);
michael@0 139
michael@0 140 _("Engine continues to be enabled.");
michael@0 141 do_check_true(engine.enabled);
michael@0 142 } finally {
michael@0 143 Service.startOver();
michael@0 144 server.stop(run_next_test);
michael@0 145 }
michael@0 146 });
michael@0 147
michael@0 148 add_test(function test_disabledLocally() {
michael@0 149 _("Test: Engine is enabled on remote clients and disabled locally");
michael@0 150 Service.syncID = "abcdefghij";
michael@0 151 let engine = Service.engineManager.get("steam");
michael@0 152 let metaWBO = new ServerWBO("global", {
michael@0 153 syncID: Service.syncID,
michael@0 154 storageVersion: STORAGE_VERSION,
michael@0 155 engines: {steam: {syncID: engine.syncID,
michael@0 156 version: engine.version}}
michael@0 157 });
michael@0 158 let steamCollection = new ServerWBO("steam", PAYLOAD);
michael@0 159
michael@0 160 let server = sync_httpd_setup({
michael@0 161 "/1.1/johndoe/storage/meta/global": metaWBO.handler(),
michael@0 162 "/1.1/johndoe/storage/steam": steamCollection.handler()
michael@0 163 });
michael@0 164 setUp(server);
michael@0 165
michael@0 166 try {
michael@0 167 _("Disable engine locally.");
michael@0 168 Service._ignorePrefObserver = true;
michael@0 169 engine.enabled = true;
michael@0 170 Service._ignorePrefObserver = false;
michael@0 171 engine.enabled = false;
michael@0 172
michael@0 173 _("Sync.");
michael@0 174 Service.sync();
michael@0 175
michael@0 176 _("Meta record no longer contains engine.");
michael@0 177 do_check_false(!!metaWBO.data.engines.steam);
michael@0 178
michael@0 179 _("Server records are wiped.");
michael@0 180 do_check_eq(steamCollection.payload, undefined);
michael@0 181
michael@0 182 _("Engine continues to be disabled.");
michael@0 183 do_check_false(engine.enabled);
michael@0 184 } finally {
michael@0 185 Service.startOver();
michael@0 186 server.stop(run_next_test);
michael@0 187 }
michael@0 188 });
michael@0 189
michael@0 190 add_test(function test_disabledLocally_wipe503() {
michael@0 191 _("Test: Engine is enabled on remote clients and disabled locally");
michael@0 192 Service.syncID = "abcdefghij";
michael@0 193 let engine = Service.engineManager.get("steam");
michael@0 194 let metaWBO = new ServerWBO("global", {
michael@0 195 syncID: Service.syncID,
michael@0 196 storageVersion: STORAGE_VERSION,
michael@0 197 engines: {steam: {syncID: engine.syncID,
michael@0 198 version: engine.version}}
michael@0 199 });
michael@0 200 let steamCollection = new ServerWBO("steam", PAYLOAD);
michael@0 201
michael@0 202 function service_unavailable(request, response) {
michael@0 203 let body = "Service Unavailable";
michael@0 204 response.setStatusLine(request.httpVersion, 503, "Service Unavailable");
michael@0 205 response.setHeader("Retry-After", "23");
michael@0 206 response.bodyOutputStream.write(body, body.length);
michael@0 207 }
michael@0 208
michael@0 209 let server = sync_httpd_setup({
michael@0 210 "/1.1/johndoe/storage/meta/global": metaWBO.handler(),
michael@0 211 "/1.1/johndoe/storage/steam": service_unavailable
michael@0 212 });
michael@0 213 setUp(server);
michael@0 214
michael@0 215 _("Disable engine locally.");
michael@0 216 Service._ignorePrefObserver = true;
michael@0 217 engine.enabled = true;
michael@0 218 Service._ignorePrefObserver = false;
michael@0 219 engine.enabled = false;
michael@0 220
michael@0 221 Svc.Obs.add("weave:ui:sync:error", function onSyncError() {
michael@0 222 Svc.Obs.remove("weave:ui:sync:error", onSyncError);
michael@0 223
michael@0 224 do_check_eq(Service.status.sync, SERVER_MAINTENANCE);
michael@0 225
michael@0 226 Service.startOver();
michael@0 227 server.stop(run_next_test);
michael@0 228 });
michael@0 229
michael@0 230 _("Sync.");
michael@0 231 Service.errorHandler.syncAndReportErrors();
michael@0 232 });
michael@0 233
michael@0 234 add_test(function test_enabledRemotely() {
michael@0 235 _("Test: Engine is disabled locally and enabled on a remote client");
michael@0 236 Service.syncID = "abcdefghij";
michael@0 237 let engine = Service.engineManager.get("steam");
michael@0 238 let metaWBO = new ServerWBO("global", {
michael@0 239 syncID: Service.syncID,
michael@0 240 storageVersion: STORAGE_VERSION,
michael@0 241 engines: {steam: {syncID: engine.syncID,
michael@0 242 version: engine.version}}
michael@0 243 });
michael@0 244 let server = sync_httpd_setup({
michael@0 245 "/1.1/johndoe/storage/meta/global":
michael@0 246 upd("meta", metaWBO.handler()),
michael@0 247
michael@0 248 "/1.1/johndoe/storage/steam":
michael@0 249 upd("steam", new ServerWBO("steam", {}).handler())
michael@0 250 });
michael@0 251 setUp(server);
michael@0 252
michael@0 253 // We need to be very careful how we do this, so that we don't trigger a
michael@0 254 // fresh start!
michael@0 255 try {
michael@0 256 _("Upload some keys to avoid a fresh start.");
michael@0 257 let wbo = Service.collectionKeys.generateNewKeysWBO();
michael@0 258 wbo.encrypt(Service.identity.syncKeyBundle);
michael@0 259 do_check_eq(200, wbo.upload(Service.resource(Service.cryptoKeysURL)).status);
michael@0 260
michael@0 261 _("Engine is disabled.");
michael@0 262 do_check_false(engine.enabled);
michael@0 263
michael@0 264 _("Sync.");
michael@0 265 Service.sync();
michael@0 266
michael@0 267 _("Engine is enabled.");
michael@0 268 do_check_true(engine.enabled);
michael@0 269
michael@0 270 _("Meta record still present.");
michael@0 271 do_check_eq(metaWBO.data.engines.steam.syncID, engine.syncID);
michael@0 272 } finally {
michael@0 273 Service.startOver();
michael@0 274 server.stop(run_next_test);
michael@0 275 }
michael@0 276 });
michael@0 277
michael@0 278 add_test(function test_disabledRemotelyTwoClients() {
michael@0 279 _("Test: Engine is enabled locally and disabled on a remote client... with two clients.");
michael@0 280 Service.syncID = "abcdefghij";
michael@0 281 let engine = Service.engineManager.get("steam");
michael@0 282 let metaWBO = new ServerWBO("global", {syncID: Service.syncID,
michael@0 283 storageVersion: STORAGE_VERSION,
michael@0 284 engines: {}});
michael@0 285 let server = sync_httpd_setup({
michael@0 286 "/1.1/johndoe/storage/meta/global":
michael@0 287 upd("meta", metaWBO.handler()),
michael@0 288
michael@0 289 "/1.1/johndoe/storage/steam":
michael@0 290 upd("steam", new ServerWBO("steam", {}).handler())
michael@0 291 });
michael@0 292 setUp(server);
michael@0 293
michael@0 294 try {
michael@0 295 _("Enable engine locally.");
michael@0 296 Service._ignorePrefObserver = true;
michael@0 297 engine.enabled = true;
michael@0 298 Service._ignorePrefObserver = false;
michael@0 299
michael@0 300 _("Sync.");
michael@0 301 Service.sync();
michael@0 302
michael@0 303 _("Disable engine by deleting from meta/global.");
michael@0 304 let d = metaWBO.data;
michael@0 305 delete d.engines["steam"];
michael@0 306 metaWBO.payload = JSON.stringify(d);
michael@0 307 metaWBO.modified = Date.now() / 1000;
michael@0 308
michael@0 309 _("Add a second client and verify that the local pref is changed.");
michael@0 310 Service.clientsEngine._store._remoteClients["foobar"] = {name: "foobar", type: "desktop"};
michael@0 311 Service.sync();
michael@0 312
michael@0 313 _("Engine is disabled.");
michael@0 314 do_check_false(engine.enabled);
michael@0 315
michael@0 316 } finally {
michael@0 317 Service.startOver();
michael@0 318 server.stop(run_next_test);
michael@0 319 }
michael@0 320 });
michael@0 321
michael@0 322 add_test(function test_disabledRemotely() {
michael@0 323 _("Test: Engine is enabled locally and disabled on a remote client");
michael@0 324 Service.syncID = "abcdefghij";
michael@0 325 let engine = Service.engineManager.get("steam");
michael@0 326 let metaWBO = new ServerWBO("global", {syncID: Service.syncID,
michael@0 327 storageVersion: STORAGE_VERSION,
michael@0 328 engines: {}});
michael@0 329 let server = sync_httpd_setup({
michael@0 330 "/1.1/johndoe/storage/meta/global": metaWBO.handler(),
michael@0 331 "/1.1/johndoe/storage/steam": new ServerWBO("steam", {}).handler()
michael@0 332 });
michael@0 333 setUp(server);
michael@0 334
michael@0 335 try {
michael@0 336 _("Enable engine locally.");
michael@0 337 Service._ignorePrefObserver = true;
michael@0 338 engine.enabled = true;
michael@0 339 Service._ignorePrefObserver = false;
michael@0 340
michael@0 341 _("Sync.");
michael@0 342 Service.sync();
michael@0 343
michael@0 344 _("Engine is not disabled: only one client.");
michael@0 345 do_check_true(engine.enabled);
michael@0 346
michael@0 347 } finally {
michael@0 348 Service.startOver();
michael@0 349 server.stop(run_next_test);
michael@0 350 }
michael@0 351 });
michael@0 352
michael@0 353 add_test(function test_dependentEnginesEnabledLocally() {
michael@0 354 _("Test: Engine is disabled on remote clients and enabled locally");
michael@0 355 Service.syncID = "abcdefghij";
michael@0 356 let steamEngine = Service.engineManager.get("steam");
michael@0 357 let stirlingEngine = Service.engineManager.get("stirling");
michael@0 358 let metaWBO = new ServerWBO("global", {syncID: Service.syncID,
michael@0 359 storageVersion: STORAGE_VERSION,
michael@0 360 engines: {}});
michael@0 361 let server = sync_httpd_setup({
michael@0 362 "/1.1/johndoe/storage/meta/global": metaWBO.handler(),
michael@0 363 "/1.1/johndoe/storage/steam": new ServerWBO("steam", {}).handler(),
michael@0 364 "/1.1/johndoe/storage/stirling": new ServerWBO("stirling", {}).handler()
michael@0 365 });
michael@0 366 setUp(server);
michael@0 367
michael@0 368 try {
michael@0 369 _("Enable engine locally. Doing it on one is enough.");
michael@0 370 steamEngine.enabled = true;
michael@0 371
michael@0 372 _("Sync.");
michael@0 373 Service.sync();
michael@0 374
michael@0 375 _("Meta record now contains the new engines.");
michael@0 376 do_check_true(!!metaWBO.data.engines.steam);
michael@0 377 do_check_true(!!metaWBO.data.engines.stirling);
michael@0 378
michael@0 379 _("Engines continue to be enabled.");
michael@0 380 do_check_true(steamEngine.enabled);
michael@0 381 do_check_true(stirlingEngine.enabled);
michael@0 382 } finally {
michael@0 383 Service.startOver();
michael@0 384 server.stop(run_next_test);
michael@0 385 }
michael@0 386 });
michael@0 387
michael@0 388 add_test(function test_dependentEnginesDisabledLocally() {
michael@0 389 _("Test: Two dependent engines are enabled on remote clients and disabled locally");
michael@0 390 Service.syncID = "abcdefghij";
michael@0 391 let steamEngine = Service.engineManager.get("steam");
michael@0 392 let stirlingEngine = Service.engineManager.get("stirling");
michael@0 393 let metaWBO = new ServerWBO("global", {
michael@0 394 syncID: Service.syncID,
michael@0 395 storageVersion: STORAGE_VERSION,
michael@0 396 engines: {steam: {syncID: steamEngine.syncID,
michael@0 397 version: steamEngine.version},
michael@0 398 stirling: {syncID: stirlingEngine.syncID,
michael@0 399 version: stirlingEngine.version}}
michael@0 400 });
michael@0 401
michael@0 402 let steamCollection = new ServerWBO("steam", PAYLOAD);
michael@0 403 let stirlingCollection = new ServerWBO("stirling", PAYLOAD);
michael@0 404
michael@0 405 let server = sync_httpd_setup({
michael@0 406 "/1.1/johndoe/storage/meta/global": metaWBO.handler(),
michael@0 407 "/1.1/johndoe/storage/steam": steamCollection.handler(),
michael@0 408 "/1.1/johndoe/storage/stirling": stirlingCollection.handler()
michael@0 409 });
michael@0 410 setUp(server);
michael@0 411
michael@0 412 try {
michael@0 413 _("Disable engines locally. Doing it on one is enough.");
michael@0 414 Service._ignorePrefObserver = true;
michael@0 415 steamEngine.enabled = true;
michael@0 416 do_check_true(stirlingEngine.enabled);
michael@0 417 Service._ignorePrefObserver = false;
michael@0 418 steamEngine.enabled = false;
michael@0 419 do_check_false(stirlingEngine.enabled);
michael@0 420
michael@0 421 _("Sync.");
michael@0 422 Service.sync();
michael@0 423
michael@0 424 _("Meta record no longer contains engines.");
michael@0 425 do_check_false(!!metaWBO.data.engines.steam);
michael@0 426 do_check_false(!!metaWBO.data.engines.stirling);
michael@0 427
michael@0 428 _("Server records are wiped.");
michael@0 429 do_check_eq(steamCollection.payload, undefined);
michael@0 430 do_check_eq(stirlingCollection.payload, undefined);
michael@0 431
michael@0 432 _("Engines continue to be disabled.");
michael@0 433 do_check_false(steamEngine.enabled);
michael@0 434 do_check_false(stirlingEngine.enabled);
michael@0 435 } finally {
michael@0 436 Service.startOver();
michael@0 437 server.stop(run_next_test);
michael@0 438 }
michael@0 439 });

mercurial