1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const { Service } = ChromeUtils.importESModule(
"resource://services-sync/service.sys.mjs"
);
const { EngineSynchronizer } = ChromeUtils.importESModule(
"resource://services-sync/stages/enginesync.sys.mjs"
);
function QuietStore() {
Store.call("Quiet");
}
QuietStore.prototype = {
async getAllIDs() {
return [];
},
};
function SteamEngine() {
SyncEngine.call(this, "Steam", Service);
}
SteamEngine.prototype = {
// We're not interested in engine sync but what the service does.
_storeObj: QuietStore,
_sync: async function _sync() {
await this._syncStartup();
},
};
Object.setPrototypeOf(SteamEngine.prototype, SyncEngine.prototype);
function StirlingEngine() {
SyncEngine.call(this, "Stirling", Service);
}
StirlingEngine.prototype = {
// This engine's enabled state is the same as the SteamEngine's.
get prefName() {
return "steam";
},
};
Object.setPrototypeOf(StirlingEngine.prototype, SteamEngine.prototype);
// Tracking info/collections.
var collectionsHelper = track_collections_helper();
var upd = collectionsHelper.with_updated_collection;
function sync_httpd_setup(handlers) {
handlers["/1.1/johndoe/info/collections"] = collectionsHelper.handler;
delete collectionsHelper.collections.crypto;
delete collectionsHelper.collections.meta;
let cr = new ServerWBO("keys");
handlers["/1.1/johndoe/storage/crypto/keys"] = upd("crypto", cr.handler());
let cl = new ServerCollection();
handlers["/1.1/johndoe/storage/clients"] = upd("clients", cl.handler());
return httpd_setup(handlers);
}
async function setUp(server) {
await SyncTestingInfrastructure(server, "johndoe", "ilovejane");
// Ensure that the server has valid keys so that logging in will work and not
// result in a server wipe, rendering many of these tests useless.
await generateNewKeys(Service.collectionKeys);
let serverKeys = Service.collectionKeys.asWBO("crypto", "keys");
await serverKeys.encrypt(Service.identity.syncKeyBundle);
let { success } = await serverKeys.upload(
Service.resource(Service.cryptoKeysURL)
);
ok(success);
}
const PAYLOAD = 42;
add_task(async function setup() {
await Service.engineManager.clear();
validate_all_future_pings();
await Service.engineManager.register(SteamEngine);
await Service.engineManager.register(StirlingEngine);
});
add_task(async function test_newAccount() {
enableValidationPrefs();
_("Test: New account does not disable locally enabled engines.");
let engine = Service.engineManager.get("steam");
let server = sync_httpd_setup({
"/1.1/johndoe/storage/meta/global": new ServerWBO("global", {}).handler(),
"/1.1/johndoe/storage/steam": new ServerWBO("steam", {}).handler(),
});
await setUp(server);
try {
_("Engine is enabled from the beginning.");
Service._ignorePrefObserver = true;
engine.enabled = true;
Service._ignorePrefObserver = false;
_("Sync.");
await Service.sync();
_("Engine continues to be enabled.");
Assert.ok(engine.enabled);
} finally {
await Service.startOver();
await promiseStopServer(server);
}
});
add_task(async function test_enabledLocally() {
enableValidationPrefs();
_("Test: Engine is disabled on remote clients and enabled locally");
Service.syncID = "abcdefghij";
let engine = Service.engineManager.get("steam");
let metaWBO = new ServerWBO("global", {
syncID: Service.syncID,
storageVersion: STORAGE_VERSION,
engines: {},
});
let server = sync_httpd_setup({
"/1.1/johndoe/storage/meta/global": metaWBO.handler(),
"/1.1/johndoe/storage/steam": new ServerWBO("steam", {}).handler(),
});
await setUp(server);
try {
_("Enable engine locally.");
engine.enabled = true;
_("Sync.");
await Service.sync();
_("Meta record now contains the new engine.");
Assert.ok(!!metaWBO.data.engines.steam);
_("Engine continues to be enabled.");
Assert.ok(engine.enabled);
} finally {
await Service.startOver();
await promiseStopServer(server);
}
});
add_task(async function test_disabledLocally() {
enableValidationPrefs();
_("Test: Engine is enabled on remote clients and disabled locally");
Service.syncID = "abcdefghij";
let engine = Service.engineManager.get("steam");
let syncID = await engine.resetLocalSyncID();
let metaWBO = new ServerWBO("global", {
syncID: Service.syncID,
storageVersion: STORAGE_VERSION,
engines: { steam: { syncID, version: engine.version } },
});
let steamCollection = new ServerWBO("steam", PAYLOAD);
let server = sync_httpd_setup({
"/1.1/johndoe/storage/meta/global": metaWBO.handler(),
"/1.1/johndoe/storage/steam": steamCollection.handler(),
});
await setUp(server);
try {
_("Disable engine locally.");
Service._ignorePrefObserver = true;
engine.enabled = true;
Service._ignorePrefObserver = false;
engine.enabled = false;
_("Sync.");
await Service.sync();
_("Meta record no longer contains engine.");
Assert.ok(!metaWBO.data.engines.steam);
_("Server records are wiped.");
Assert.equal(steamCollection.payload, undefined);
_("Engine continues to be disabled.");
Assert.ok(!engine.enabled);
} finally {
await Service.startOver();
await promiseStopServer(server);
}
});
add_task(async function test_disabledLocally_wipe503() {
enableValidationPrefs();
_("Test: Engine is enabled on remote clients and disabled locally");
Service.syncID = "abcdefghij";
let engine = Service.engineManager.get("steam");
let syncID = await engine.resetLocalSyncID();
let metaWBO = new ServerWBO("global", {
syncID: Service.syncID,
storageVersion: STORAGE_VERSION,
engines: { steam: { syncID, version: engine.version } },
});
function service_unavailable(request, response) {
let body = "Service Unavailable";
response.setStatusLine(request.httpVersion, 503, "Service Unavailable");
response.setHeader("Retry-After", "23");
response.bodyOutputStream.write(body, body.length);
}
let server = sync_httpd_setup({
"/1.1/johndoe/storage/meta/global": metaWBO.handler(),
"/1.1/johndoe/storage/steam": service_unavailable,
});
await setUp(server);
_("Disable engine locally.");
Service._ignorePrefObserver = true;
engine.enabled = true;
Service._ignorePrefObserver = false;
engine.enabled = false;
_("Sync.");
await Service.sync();
Assert.equal(Service.status.sync, SERVER_MAINTENANCE);
await Service.startOver();
await promiseStopServer(server);
});
add_task(async function test_enabledRemotely() {
enableValidationPrefs();
_("Test: Engine is disabled locally and enabled on a remote client");
Service.syncID = "abcdefghij";
let engine = Service.engineManager.get("steam");
let syncID = await engine.resetLocalSyncID();
let metaWBO = new ServerWBO("global", {
syncID: Service.syncID,
storageVersion: STORAGE_VERSION,
engines: { steam: { syncID, version: engine.version } },
});
let server = sync_httpd_setup({
"/1.1/johndoe/storage/meta/global": upd("meta", metaWBO.handler()),
"/1.1/johndoe/storage/steam": upd(
"steam",
new ServerWBO("steam", {}).handler()
),
});
await setUp(server);
// We need to be very careful how we do this, so that we don't trigger a
// fresh start!
try {
_("Upload some keys to avoid a fresh start.");
let wbo = await Service.collectionKeys.generateNewKeysWBO();
await wbo.encrypt(Service.identity.syncKeyBundle);
Assert.equal(
200,
(await wbo.upload(Service.resource(Service.cryptoKeysURL))).status
);
_("Engine is disabled.");
Assert.ok(!engine.enabled);
_("Sync.");
await Service.sync();
_("Engine is enabled.");
Assert.ok(engine.enabled);
_("Meta record still present.");
Assert.equal(metaWBO.data.engines.steam.syncID, await engine.getSyncID());
} finally {
await Service.startOver();
await promiseStopServer(server);
}
});
add_task(async function test_disabledRemotelyTwoClients() {
enableValidationPrefs();
_(
"Test: Engine is enabled locally and disabled on a remote client... with two clients."
);
Service.syncID = "abcdefghij";
let engine = Service.engineManager.get("steam");
let metaWBO = new ServerWBO("global", {
syncID: Service.syncID,
storageVersion: STORAGE_VERSION,
engines: {},
});
let server = sync_httpd_setup({
"/1.1/johndoe/storage/meta/global": upd("meta", metaWBO.handler()),
"/1.1/johndoe/storage/steam": upd(
"steam",
new ServerWBO("steam", {}).handler()
),
});
await setUp(server);
try {
_("Enable engine locally.");
Service._ignorePrefObserver = true;
engine.enabled = true;
Service._ignorePrefObserver = false;
_("Sync.");
await Service.sync();
_("Disable engine by deleting from meta/global.");
let d = metaWBO.data;
delete d.engines.steam;
metaWBO.payload = JSON.stringify(d);
metaWBO.modified = Date.now() / 1000;
_("Add a second client and verify that the local pref is changed.");
Service.clientsEngine._store._remoteClients.foobar = {
name: "foobar",
type: "desktop",
};
await Service.sync();
_("Engine is disabled.");
Assert.ok(!engine.enabled);
} finally {
await Service.startOver();
await promiseStopServer(server);
}
});
add_task(async function test_disabledRemotely() {
enableValidationPrefs();
_("Test: Engine is enabled locally and disabled on a remote client");
Service.syncID = "abcdefghij";
let engine = Service.engineManager.get("steam");
let metaWBO = new ServerWBO("global", {
syncID: Service.syncID,
storageVersion: STORAGE_VERSION,
engines: {},
});
let server = sync_httpd_setup({
"/1.1/johndoe/storage/meta/global": metaWBO.handler(),
"/1.1/johndoe/storage/steam": new ServerWBO("steam", {}).handler(),
});
await setUp(server);
try {
_("Enable engine locally.");
Service._ignorePrefObserver = true;
engine.enabled = true;
Service._ignorePrefObserver = false;
_("Sync.");
await Service.sync();
_("Engine is not disabled: only one client.");
Assert.ok(engine.enabled);
} finally {
await Service.startOver();
await promiseStopServer(server);
}
});
add_task(async function test_dependentEnginesEnabledLocally() {
enableValidationPrefs();
_("Test: Engine is disabled on remote clients and enabled locally");
Service.syncID = "abcdefghij";
let steamEngine = Service.engineManager.get("steam");
let stirlingEngine = Service.engineManager.get("stirling");
let metaWBO = new ServerWBO("global", {
syncID: Service.syncID,
storageVersion: STORAGE_VERSION,
engines: {},
});
let server = sync_httpd_setup({
"/1.1/johndoe/storage/meta/global": metaWBO.handler(),
"/1.1/johndoe/storage/steam": new ServerWBO("steam", {}).handler(),
"/1.1/johndoe/storage/stirling": new ServerWBO("stirling", {}).handler(),
});
await setUp(server);
try {
_("Enable engine locally. Doing it on one is enough.");
steamEngine.enabled = true;
_("Sync.");
await Service.sync();
_("Meta record now contains the new engines.");
Assert.ok(!!metaWBO.data.engines.steam);
Assert.ok(!!metaWBO.data.engines.stirling);
_("Engines continue to be enabled.");
Assert.ok(steamEngine.enabled);
Assert.ok(stirlingEngine.enabled);
} finally {
await Service.startOver();
await promiseStopServer(server);
}
});
add_task(async function test_dependentEnginesDisabledLocally() {
enableValidationPrefs();
_(
"Test: Two dependent engines are enabled on remote clients and disabled locally"
);
Service.syncID = "abcdefghij";
let steamEngine = Service.engineManager.get("steam");
let steamSyncID = await steamEngine.resetLocalSyncID();
let stirlingEngine = Service.engineManager.get("stirling");
let stirlingSyncID = await stirlingEngine.resetLocalSyncID();
let metaWBO = new ServerWBO("global", {
syncID: Service.syncID,
storageVersion: STORAGE_VERSION,
engines: {
steam: { syncID: steamSyncID, version: steamEngine.version },
stirling: { syncID: stirlingSyncID, version: stirlingEngine.version },
},
});
let steamCollection = new ServerWBO("steam", PAYLOAD);
let stirlingCollection = new ServerWBO("stirling", PAYLOAD);
let server = sync_httpd_setup({
"/1.1/johndoe/storage/meta/global": metaWBO.handler(),
"/1.1/johndoe/storage/steam": steamCollection.handler(),
"/1.1/johndoe/storage/stirling": stirlingCollection.handler(),
});
await setUp(server);
try {
_("Disable engines locally. Doing it on one is enough.");
Service._ignorePrefObserver = true;
steamEngine.enabled = true;
Assert.ok(stirlingEngine.enabled);
Service._ignorePrefObserver = false;
steamEngine.enabled = false;
Assert.ok(!stirlingEngine.enabled);
_("Sync.");
await Service.sync();
_("Meta record no longer contains engines.");
Assert.ok(!metaWBO.data.engines.steam);
Assert.ok(!metaWBO.data.engines.stirling);
_("Server records are wiped.");
Assert.equal(steamCollection.payload, undefined);
Assert.equal(stirlingCollection.payload, undefined);
_("Engines continue to be disabled.");
Assert.ok(!steamEngine.enabled);
Assert.ok(!stirlingEngine.enabled);
} finally {
await Service.startOver();
await promiseStopServer(server);
}
});
add_task(async function test_service_updateLocalEnginesState() {
Service.syncID = "abcdefghij";
const engine = Service.engineManager.get("steam");
const metaWBO = new ServerWBO("global", {
syncID: Service.syncID,
storageVersion: STORAGE_VERSION,
declined: ["steam"],
engines: {},
});
const server = httpd_setup({
"/1.1/johndoe/storage/meta/global": metaWBO.handler(),
});
await SyncTestingInfrastructure(server, "johndoe");
// Disconnect sync.
await Service.startOver();
Service._ignorePrefObserver = true;
// Steam engine is enabled on our machine.
engine.enabled = true;
Service._ignorePrefObserver = false;
Service.identity._findCluster = () => server.baseURI + "/1.1/johndoe/";
// Update engine state from the server.
await Service.updateLocalEnginesState();
// Now disabled.
Assert.ok(!engine.enabled);
});
add_task(async function test_service_enableAfterUpdateState() {
Service.syncID = "abcdefghij";
const engine = Service.engineManager.get("steam");
const metaWBO = new ServerWBO("global", {
syncID: Service.syncID,
storageVersion: STORAGE_VERSION,
declined: ["steam"],
engines: { someengine: {} },
});
const server = httpd_setup({
"/1.1/johndoe/storage/meta/global": metaWBO.handler(),
});
await SyncTestingInfrastructure(server, "johndoe");
// Disconnect sync.
await Service.startOver();
Service.identity._findCluster = () => server.baseURI + "/1.1/johndoe/";
// Update engine state from the server.
await Service.updateLocalEnginesState();
// Now disabled, reflecting what's on the server.
Assert.ok(!engine.enabled);
// Enable the engine, as though the user selected it via CWTS.
engine.enabled = true;
// Do the "reconcile local and remote states" dance.
let engineSync = new EngineSynchronizer(Service);
await engineSync._updateEnabledEngines();
await Service._maybeUpdateDeclined();
// engine should remain enabled.
Assert.ok(engine.enabled);
// engine should no longer appear in declined on the server.
Assert.deepEqual(metaWBO.data.declined, []);
});
add_task(async function test_service_disableAfterUpdateState() {
Service.syncID = "abcdefghij";
const engine = Service.engineManager.get("steam");
const metaWBO = new ServerWBO("global", {
syncID: Service.syncID,
storageVersion: STORAGE_VERSION,
declined: [],
engines: { steam: {} },
});
const server = httpd_setup({
"/1.1/johndoe/storage/meta/global": metaWBO.handler(),
});
await SyncTestingInfrastructure(server, "johndoe");
// Disconnect sync.
await Service.startOver();
Service.identity._findCluster = () => server.baseURI + "/1.1/johndoe/";
// Update engine state from the server.
await Service.updateLocalEnginesState();
// Now enabled, reflecting what's on the server.
Assert.ok(engine.enabled);
// Disable the engine, as though via CWTS.
engine.enabled = false;
// Do the "reconcile local and remote states" dance.
let engineSync = new EngineSynchronizer(Service);
await engineSync._updateEnabledEngines();
await Service._maybeUpdateDeclined();
// engine should remain disabled.
Assert.ok(!engine.enabled);
// engine should now appear in declined on the server.
Assert.deepEqual(metaWBO.data.declined, ["steam"]);
// and should have been removed from engines.
Assert.deepEqual(metaWBO.data.engines, {});
});
add_task(async function test_service_updateLocalEnginesState_no_meta_global() {
Service.syncID = "abcdefghij";
const engine = Service.engineManager.get("steam");
// The server doesn't contain /meta/global (sync was never enabled).
const server = httpd_setup({});
await SyncTestingInfrastructure(server, "johndoe");
// Disconnect sync.
await Service.startOver();
Service._ignorePrefObserver = true;
// Steam engine is enabled on our machine.
engine.enabled = true;
Service._ignorePrefObserver = false;
Service.identity._findCluster = () => server.baseURI + "/1.1/johndoe/";
// Update engine state from the server.
await Service.updateLocalEnginesState();
// Still enabled.
Assert.ok(engine.enabled);
});
|