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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/* import-globals-from head_appinfo.js */
/* import-globals-from ../../../common/tests/unit/head_helpers.js */
/* import-globals-from head_errorhandler_common.js */
/* import-globals-from head_http_server.js */
// This file expects Service to be defined in the global scope when EHTestsCommon
// is used (from service.js).
/* global Service */
var { AddonTestUtils, MockAsyncShutdown } = ChromeUtils.importESModule(
"resource://testing-common/AddonTestUtils.sys.mjs"
);
var { Async } = ChromeUtils.importESModule(
"resource://services-common/async.sys.mjs"
);
var { CommonUtils } = ChromeUtils.importESModule(
"resource://services-common/utils.sys.mjs"
);
var { PlacesTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/PlacesTestUtils.sys.mjs"
);
var { sinon } = ChromeUtils.importESModule(
"resource://testing-common/Sinon.sys.mjs"
);
var { SerializableSet, Svc, Utils, getChromeWindow } =
ChromeUtils.importESModule("resource://services-sync/util.sys.mjs");
var { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
);
var { PlacesUtils } = ChromeUtils.importESModule(
"resource://gre/modules/PlacesUtils.sys.mjs"
);
var { PlacesSyncUtils } = ChromeUtils.importESModule(
"resource://gre/modules/PlacesSyncUtils.sys.mjs"
);
var { ObjectUtils } = ChromeUtils.importESModule(
"resource://gre/modules/ObjectUtils.sys.mjs"
);
var {
MockFxaStorageManager,
SyncTestingInfrastructure,
configureFxAccountIdentity,
configureIdentity,
encryptPayload,
getLoginTelemetryScalar,
makeFxAccountsInternalMock,
makeIdentityConfig,
promiseNamedTimer,
promiseZeroTimer,
sumHistogram,
syncTestLogging,
waitForZeroTimer,
} = ChromeUtils.importESModule(
"resource://testing-common/services/sync/utils.sys.mjs"
);
ChromeUtils.defineESModuleGetters(this, {
AddonManager: "resource://gre/modules/AddonManager.sys.mjs",
});
add_setup(async function head_setup() {
// Initialize logging. This will sometimes be reset by a pref reset,
// so it's also called as part of SyncTestingInfrastructure().
syncTestLogging();
// If a test imports Service, make sure it is initialized first.
if (typeof Service !== "undefined") {
await Service.promiseInitialized;
}
});
ChromeUtils.defineLazyGetter(this, "SyncPingSchema", function () {
let { FileUtils } = ChromeUtils.importESModule(
"resource://gre/modules/FileUtils.sys.mjs"
);
let { NetUtil } = ChromeUtils.importESModule(
"resource://gre/modules/NetUtil.sys.mjs"
);
let stream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
Ci.nsIFileInputStream
);
let schema;
try {
let schemaFile = do_get_file("sync_ping_schema.json");
stream.init(schemaFile, FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, 0);
let bytes = NetUtil.readInputStream(stream, stream.available());
schema = JSON.parse(new TextDecoder().decode(bytes));
} finally {
stream.close();
}
// Allow tests to make whatever engines they want, this shouldn't cause
// validation failure.
schema.definitions.engine.properties.name = { type: "string" };
return schema;
});
ChromeUtils.defineLazyGetter(this, "SyncPingValidator", function () {
const { JsonSchema } = ChromeUtils.importESModule(
"resource://gre/modules/JsonSchema.sys.mjs"
);
return new JsonSchema.Validator(SyncPingSchema);
});
// This is needed for loadAddonTestFunctions().
var gGlobalScope = this;
function ExtensionsTestPath(path) {
if (path[0] != "/") {
throw Error("Path must begin with '/': " + path);
}
return "../../../../toolkit/mozapps/extensions/test/xpcshell" + path;
}
function webExtensionsTestPath(path) {
if (path[0] != "/") {
throw Error("Path must begin with '/': " + path);
}
return "../../../../toolkit/components/extensions/test/xpcshell" + path;
}
/**
* Loads the WebExtension test functions by importing its test file.
*/
function loadWebExtensionTestFunctions() {
/* import-globals-from ../../../../toolkit/components/extensions/test/xpcshell/head_sync.js */
const path = webExtensionsTestPath("/head_sync.js");
let file = do_get_file(path);
let uri = Services.io.newFileURI(file);
Services.scriptloader.loadSubScript(uri.spec, gGlobalScope);
}
/**
* Installs an add-on from an addonInstall
*
* @param install addonInstall instance to install
*/
async function installAddonFromInstall(install) {
await install.install();
Assert.notEqual(null, install.addon);
Assert.notEqual(null, install.addon.syncGUID);
return install.addon;
}
/**
* Convenience function to install an add-on from the extensions unit tests.
*
* @param file
* Add-on file to install.
* @param reconciler
* addons reconciler, if passed we will wait on the events to be
* processed before resolving
* @return addon object that was installed
*/
async function installAddon(file, reconciler = null) {
let install = await AddonManager.getInstallForFile(file);
Assert.notEqual(null, install);
const addon = await installAddonFromInstall(install);
if (reconciler) {
await reconciler.queueCaller.promiseCallsComplete();
}
return addon;
}
/**
* Convenience function to uninstall an add-on.
*
* @param addon
* Addon instance to uninstall
* @param reconciler
* addons reconciler, if passed we will wait on the events to be
* processed before resolving
*/
async function uninstallAddon(addon, reconciler = null) {
const uninstallPromise = new Promise(res => {
let listener = {
onUninstalled(uninstalled) {
if (uninstalled.id == addon.id) {
AddonManager.removeAddonListener(listener);
res(uninstalled);
}
},
};
AddonManager.addAddonListener(listener);
});
addon.uninstall();
await uninstallPromise;
if (reconciler) {
await reconciler.queueCaller.promiseCallsComplete();
}
}
async function generateNewKeys(collectionKeys, collections = null) {
let wbo = await collectionKeys.generateNewKeysWBO(collections);
let modified = new_timestamp();
collectionKeys.setContents(wbo.cleartext, modified);
}
// Helpers for testing open tabs.
// These reflect part of the internal structure of TabEngine,
// and stub part of Service.wm.
function mockShouldSkipWindow(win) {
return win.closed || win.mockIsPrivate;
}
function mockGetTabState(tab) {
return tab;
}
function mockGetWindowEnumerator(urls) {
let elements = [];
const numWindows = 1;
for (let w = 0; w < numWindows; ++w) {
let tabs = [];
let win = {
closed: false,
mockIsPrivate: false,
gBrowser: {
tabs,
},
};
elements.push(win);
let lastAccessed = 2000;
for (let url of urls) {
tabs.push({
linkedBrowser: {
currentURI: Services.io.newURI(url),
contentTitle: "title",
},
lastAccessed,
});
lastAccessed += 1000;
}
}
// Always include a closed window and a private window.
elements.push({
closed: true,
mockIsPrivate: false,
gBrowser: {
tabs: [],
},
});
elements.push({
closed: false,
mockIsPrivate: true,
gBrowser: {
tabs: [],
},
});
return elements.values();
}
// Helper function to get the sync telemetry and add the typically used test
// engine names to its list of allowed engines.
function get_sync_test_telemetry() {
let { SyncTelemetry } = ChromeUtils.importESModule(
"resource://services-sync/telemetry.sys.mjs"
);
SyncTelemetry.tryRefreshDevices = function () {};
let testEngines = ["rotary", "steam", "sterling", "catapult", "nineties"];
for (let engineName of testEngines) {
SyncTelemetry.allowedEngines.add(engineName);
}
SyncTelemetry.submissionInterval = -1;
return SyncTelemetry;
}
function assert_valid_ping(record) {
// Our JSON validator does not like `undefined` values, even though they will
// be skipped when we serialize to JSON.
record = JSON.parse(JSON.stringify(record));
// This is called as the test harness tears down due to shutdown. This
// will typically have no recorded syncs, and the validator complains about
// it. So ignore such records (but only ignore when *both* shutdown and
// no Syncs - either of them not being true might be an actual problem)
if (record && (record.why != "shutdown" || !!record.syncs.length)) {
const result = SyncPingValidator.validate(record);
if (!result.valid) {
if (result.errors.length) {
// validation failed - using a simple |deepEqual([], errors)| tends to
// truncate the validation errors in the output and doesn't show that
// the ping actually was - so be helpful.
info("telemetry ping validation failed");
info("the ping data is: " + JSON.stringify(record, undefined, 2));
info(
"the validation failures: " +
JSON.stringify(result.errors, undefined, 2)
);
ok(
false,
"Sync telemetry ping validation failed - see output above for details"
);
}
}
equal(record.version, 1);
record.syncs.forEach(p => {
lessOrEqual(p.when, Date.now());
});
}
}
// Asserts that `ping` is a ping that doesn't contain any failure information
function assert_success_ping(ping) {
ok(!!ping);
assert_valid_ping(ping);
ping.syncs.forEach(record => {
ok(!record.failureReason, JSON.stringify(record.failureReason));
equal(undefined, record.status);
greater(record.engines.length, 0);
for (let e of record.engines) {
ok(!e.failureReason);
equal(undefined, e.status);
if (e.validation) {
equal(undefined, e.validation.problems);
equal(undefined, e.validation.failureReason);
}
if (e.outgoing) {
for (let o of e.outgoing) {
equal(undefined, o.failed);
notEqual(undefined, o.sent);
}
}
if (e.incoming) {
equal(undefined, e.incoming.failed);
equal(undefined, e.incoming.newFailed);
notEqual(undefined, e.incoming.applied || e.incoming.reconciled);
}
}
});
}
// Hooks into telemetry to validate all pings after calling.
function validate_all_future_pings() {
let telem = get_sync_test_telemetry();
telem.submit = assert_valid_ping;
}
function wait_for_pings(expectedPings) {
return new Promise(resolve => {
let telem = get_sync_test_telemetry();
let oldSubmit = telem.submit;
let pings = [];
telem.submit = function (record) {
pings.push(record);
if (pings.length == expectedPings) {
telem.submit = oldSubmit;
resolve(pings);
}
};
});
}
async function wait_for_ping(callback, allowErrorPings, getFullPing = false) {
let pingsPromise = wait_for_pings(1);
await callback();
let [record] = await pingsPromise;
if (allowErrorPings) {
assert_valid_ping(record);
} else {
assert_success_ping(record);
}
if (getFullPing) {
return record;
}
equal(record.syncs.length, 1);
return record.syncs[0];
}
// Perform a sync and validate all telemetry caused by the sync. If fnValidate
// is null, we just check the ping records success. If fnValidate is specified,
// then the sync must have recorded just a single sync, and that sync will be
// passed to the function to be checked.
async function sync_and_validate_telem(
fnValidate = null,
wantFullPing = false
) {
let numErrors = 0;
let telem = get_sync_test_telemetry();
let oldSubmit = telem.submit;
try {
telem.submit = function (record) {
// This is called via an observer, so failures here don't cause the test
// to fail :(
try {
// All pings must be valid.
assert_valid_ping(record);
if (fnValidate) {
// for historical reasons most of these callbacks expect a "sync"
// record, not the entire ping.
if (wantFullPing) {
fnValidate(record);
} else {
Assert.equal(record.syncs.length, 1);
fnValidate(record.syncs[0]);
}
} else {
// no validation function means it must be a "success" ping.
assert_success_ping(record);
}
} catch (ex) {
print("Failure in ping validation callback", ex, "\n", ex.stack);
numErrors += 1;
}
};
await Service.sync();
Assert.ok(numErrors == 0, "There were telemetry validation errors");
} finally {
telem.submit = oldSubmit;
}
}
// Used for the (many) cases where we do a 'partial' sync, where only a single
// engine is actually synced, but we still want to ensure we're generating a
// valid ping. Returns a promise that resolves to the ping, or rejects with the
// thrown error after calling an optional callback.
async function sync_engine_and_validate_telem(
engine,
allowErrorPings,
onError,
wantFullPing = false
) {
let telem = get_sync_test_telemetry();
let caughtError = null;
// Clear out status, so failures from previous syncs won't show up in the
// telemetry ping.
let { Status } = ChromeUtils.importESModule(
"resource://services-sync/status.sys.mjs"
);
Status._engines = {};
Status.partial = false;
// Ideally we'd clear these out like we do with engines, (probably via
// Status.resetSync()), but this causes *numerous* tests to fail, so we just
// assume that if no failureReason or engine failures are set, and the
// status properties are the same as they were initially, that it's just
// a leftover.
// This is only an issue since we're triggering the sync of just one engine,
// without doing any other parts of the sync.
let initialServiceStatus = Status._service;
let initialSyncStatus = Status._sync;
let oldSubmit = telem.submit;
let submitPromise = new Promise((resolve, reject) => {
telem.submit = function (ping) {
telem.submit = oldSubmit;
ping.syncs.forEach(record => {
if (record && record.status) {
// did we see anything to lead us to believe that something bad actually happened
let realProblem =
record.failureReason ||
record.engines.some(e => {
if (e.failureReason || e.status) {
return true;
}
if (e.outgoing && e.outgoing.some(o => o.failed > 0)) {
return true;
}
return e.incoming && e.incoming.failed;
});
if (!realProblem) {
// no, so if the status is the same as it was initially, just assume
// that its leftover and that we can ignore it.
if (record.status.sync && record.status.sync == initialSyncStatus) {
delete record.status.sync;
}
if (
record.status.service &&
record.status.service == initialServiceStatus
) {
delete record.status.service;
}
if (!record.status.sync && !record.status.service) {
delete record.status;
}
}
}
});
if (allowErrorPings) {
assert_valid_ping(ping);
} else {
assert_success_ping(ping);
}
equal(ping.syncs.length, 1);
if (caughtError) {
if (onError) {
onError(ping.syncs[0], ping);
}
reject(caughtError);
} else if (wantFullPing) {
resolve(ping);
} else {
resolve(ping.syncs[0]);
}
};
});
// neuter the scheduler as it interacts badly with some of the tests - the
// engine being synced usually isn't the registered engine, so we see
// scored incremented and not removed, which schedules unexpected syncs.
let oldObserve = Service.scheduler.observe;
Service.scheduler.observe = () => {};
try {
Svc.Obs.notify("weave:service:sync:start");
try {
await engine.sync();
} catch (e) {
caughtError = e;
}
if (caughtError) {
Svc.Obs.notify("weave:service:sync:error", caughtError);
} else {
Svc.Obs.notify("weave:service:sync:finish");
}
} finally {
Service.scheduler.observe = oldObserve;
}
return submitPromise;
}
// Returns a promise that resolves once the specified observer notification
// has fired.
function promiseOneObserver(topic) {
return new Promise(resolve => {
let observer = function (subject, data) {
Svc.Obs.remove(topic, observer);
resolve({ subject, data });
};
Svc.Obs.add(topic, observer);
});
}
async function registerRotaryEngine() {
let { RotaryEngine } = ChromeUtils.importESModule(
"resource://testing-common/services/sync/rotaryengine.sys.mjs"
);
await Service.engineManager.clear();
await Service.engineManager.register(RotaryEngine);
let engine = Service.engineManager.get("rotary");
let syncID = await engine.resetLocalSyncID();
engine.enabled = true;
return { engine, syncID, tracker: engine._tracker };
}
// Set the validation prefs to attempt validation every time to avoid non-determinism.
function enableValidationPrefs(engines = ["bookmarks"]) {
for (let engine of engines) {
Svc.PrefBranch.setIntPref(`engine.${engine}.validation.interval`, 0);
Svc.PrefBranch.setIntPref(
`engine.${engine}.validation.percentageChance`,
100
);
Svc.PrefBranch.setIntPref(`engine.${engine}.validation.maxRecords`, -1);
Svc.PrefBranch.setBoolPref(`engine.${engine}.validation.enabled`, true);
}
}
async function serverForEnginesWithKeys(users, engines, callback) {
// Generate and store a fake default key bundle to avoid resetting the client
// before the first sync.
let wbo = await Service.collectionKeys.generateNewKeysWBO();
let modified = new_timestamp();
Service.collectionKeys.setContents(wbo.cleartext, modified);
let allEngines = [Service.clientsEngine].concat(engines);
let globalEngines = {};
for (let engine of allEngines) {
let syncID = await engine.resetLocalSyncID();
globalEngines[engine.name] = { version: engine.version, syncID };
}
let contents = {
meta: {
global: {
syncID: Service.syncID,
storageVersion: STORAGE_VERSION,
engines: globalEngines,
},
},
crypto: {
keys: encryptPayload(wbo.cleartext),
},
};
for (let engine of allEngines) {
contents[engine.name] = {};
}
return serverForUsers(users, contents, callback);
}
async function serverForFoo(engine, callback) {
// The bookmarks engine *always* tracks changes, meaning we might try
// and sync due to the bookmarks we ourselves create! Worse, because we
// do an engine sync only, there's no locking - so we end up with multiple
// syncs running. Neuter that by making the threshold very large.
Service.scheduler.syncThreshold = 10000000;
return serverForEnginesWithKeys({ foo: "password" }, engine, callback);
}
// Places notifies history observers asynchronously, so `addVisits` might return
// before the tracker receives the notification. This helper registers an
// observer that resolves once the expected notification fires.
async function promiseVisit(expectedType, expectedURI) {
return new Promise(resolve => {
function done(type, uri) {
if (uri == expectedURI.spec && type == expectedType) {
PlacesObservers.removeListener(
["page-visited", "page-removed"],
observer.handlePlacesEvents
);
resolve();
}
}
let observer = {
handlePlacesEvents(events) {
Assert.equal(events.length, 1);
if (events[0].type === "page-visited") {
done("added", events[0].url);
} else if (events[0].type === "page-removed") {
Assert.ok(events[0].isRemovedFromStore);
done("removed", events[0].url);
}
},
};
PlacesObservers.addListener(
["page-visited", "page-removed"],
observer.handlePlacesEvents
);
});
}
async function addVisit(
suffix,
referrer = null,
transition = PlacesUtils.history.TRANSITION_LINK
) {
let uriString = "http://getfirefox.com/" + suffix;
let uri = CommonUtils.makeURI(uriString);
_("Adding visit for URI " + uriString);
let visitAddedPromise = promiseVisit("added", uri);
await PlacesTestUtils.addVisits({
uri,
visitDate: Date.now() * 1000,
transition,
referrer,
});
await visitAddedPromise;
return uri;
}
function bookmarkNodesToInfos(nodes) {
return nodes.map(node => {
let info = {
guid: node.guid,
index: node.index,
};
if (node.children) {
info.children = bookmarkNodesToInfos(node.children);
}
return info;
});
}
async function assertBookmarksTreeMatches(rootGuid, expected, message) {
let root = await PlacesUtils.promiseBookmarksTree(rootGuid, {
includeItemIds: true,
});
let actual = bookmarkNodesToInfos(root.children);
if (!ObjectUtils.deepEqual(actual, expected)) {
_(`Expected structure for ${rootGuid}`, JSON.stringify(expected));
_(`Actual structure for ${rootGuid}`, JSON.stringify(actual));
throw new Assert.constructor.AssertionError({ actual, expected, message });
}
}
function add_bookmark_test(task) {
const { BookmarksEngine } = ChromeUtils.importESModule(
"resource://services-sync/engines/bookmarks.sys.mjs"
);
add_task(async function () {
_(`Running bookmarks test ${task.name}`);
let engine = new BookmarksEngine(Service);
await engine.initialize();
await engine._resetClient();
try {
await task(engine);
} finally {
await engine.finalize();
}
});
}
|