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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const { Service } = ChromeUtils.import("resource://services-sync/service.js");
const { WBORecord } = ChromeUtils.import("resource://services-sync/record.js");
const { Resource } = ChromeUtils.import("resource://services-sync/resource.js");
const { RotaryEngine } = ChromeUtils.import(
"resource://testing-common/services/sync/rotaryengine.js"
);
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const { fxAccounts } = ChromeUtils.import(
"resource://gre/modules/FxAccounts.jsm"
);
function SteamStore(engine) {
Store.call(this, "Steam", engine);
}
SteamStore.prototype = {
__proto__: Store.prototype,
};
function SteamTracker(name, engine) {
LegacyTracker.call(this, name || "Steam", engine);
}
SteamTracker.prototype = {
__proto__: LegacyTracker.prototype,
};
function SteamEngine(service) {
SyncEngine.call(this, "steam", service);
}
SteamEngine.prototype = {
__proto__: SyncEngine.prototype,
_storeObj: SteamStore,
_trackerObj: SteamTracker,
_errToThrow: null,
problemsToReport: null,
async _sync() {
if (this._errToThrow) {
throw this._errToThrow;
}
},
getValidator() {
return new SteamValidator();
},
};
function BogusEngine(service) {
SyncEngine.call(this, "bogus", service);
}
BogusEngine.prototype = Object.create(SteamEngine.prototype);
class SteamValidator {
async canValidate() {
return true;
}
async validate(engine) {
return {
problems: new SteamValidationProblemData(engine.problemsToReport),
version: 1,
duration: 0,
recordCount: 0,
};
}
}
class SteamValidationProblemData {
constructor(problemsToReport = []) {
this.problemsToReport = problemsToReport;
}
getSummary() {
return this.problemsToReport;
}
}
async function cleanAndGo(engine, server) {
await engine._tracker.clearChangedIDs();
Svc.Prefs.resetBranch("");
syncTestLogging();
Service.recordManager.clearCache();
await promiseStopServer(server);
}
add_task(async function setup() {
// Avoid addon manager complaining about not being initialized
await Service.engineManager.unregister("addons");
await Service.engineManager.unregister("extension-storage");
});
add_task(async function test_basic() {
enableValidationPrefs();
let helper = track_collections_helper();
let upd = helper.with_updated_collection;
let handlers = {
"/1.1/johndoe/info/collections": helper.handler,
"/1.1/johndoe/storage/crypto/keys": upd(
"crypto",
new ServerWBO("keys").handler()
),
"/1.1/johndoe/storage/meta/global": upd(
"meta",
new ServerWBO("global").handler()
),
};
let collections = [
"clients",
"bookmarks",
"forms",
"history",
"passwords",
"prefs",
"tabs",
];
for (let coll of collections) {
handlers["/1.1/johndoe/storage/" + coll] = upd(
coll,
new ServerCollection({}, true).handler()
);
}
let server = httpd_setup(handlers);
await configureIdentity({ username: "johndoe" }, server);
let ping = await wait_for_ping(() => Service.sync(), true, true);
// Check the "os" block - we can't really check specific values, but can
// check it smells sane.
ok(ping.os, "there is an OS block");
ok("name" in ping.os, "there is an OS name");
ok("version" in ping.os, "there is an OS version");
ok("locale" in ping.os, "there is an OS locale");
Svc.Prefs.resetBranch("");
await promiseStopServer(server);
});
add_task(async function test_processIncoming_error() {
let engine = Service.engineManager.get("bookmarks");
await engine.initialize();
let store = engine._store;
let server = await serverForFoo(engine);
await SyncTestingInfrastructure(server);
let collection = server.user("foo").collection("bookmarks");
try {
// Create a bogus record that when synced down will provoke a
// network error which in turn provokes an exception in _processIncoming.
const BOGUS_GUID = "zzzzzzzzzzzz";
let bogus_record = collection.insert(BOGUS_GUID, "I'm a bogus record!");
bogus_record.get = function get() {
throw new Error("Sync this!");
};
// Make the 10 minutes old so it will only be synced in the toFetch phase.
bogus_record.modified = Date.now() / 1000 - 60 * 10;
await engine.setLastSync(Date.now() / 1000 - 60);
engine.toFetch = new SerializableSet([BOGUS_GUID]);
let error, pingPayload, fullPing;
try {
await sync_engine_and_validate_telem(
engine,
true,
(errPing, fullErrPing) => {
pingPayload = errPing;
fullPing = fullErrPing;
}
);
} catch (ex) {
error = ex;
}
ok(!!error);
ok(!!pingPayload);
equal(fullPing.uid, "f".repeat(32)); // as setup by SyncTestingInfrastructure
deepEqual(pingPayload.failureReason, {
name: "httperror",
code: 500,
});
equal(pingPayload.engines.length, 1);
equal(pingPayload.engines[0].name, "bookmarks-buffered");
deepEqual(pingPayload.engines[0].failureReason, {
name: "httperror",
code: 500,
});
} finally {
await store.wipe();
await cleanAndGo(engine, server);
}
});
add_task(async function test_uploading() {
let engine = Service.engineManager.get("bookmarks");
await engine.initialize();
let store = engine._store;
let server = await serverForFoo(engine);
await SyncTestingInfrastructure(server);
let bmk = await PlacesUtils.bookmarks.insert({
parentGuid: PlacesUtils.bookmarks.toolbarGuid,
url: "http://getfirefox.com/",
title: "Get Firefox!",
});
try {
let ping = await sync_engine_and_validate_telem(engine, false);
ok(!!ping);
equal(ping.engines.length, 1);
equal(ping.engines[0].name, "bookmarks-buffered");
ok(!!ping.engines[0].outgoing);
greater(ping.engines[0].outgoing[0].sent, 0);
ok(!ping.engines[0].incoming);
await PlacesUtils.bookmarks.update({
guid: bmk.guid,
title: "New Title",
});
await store.wipe();
await engine.resetClient();
// We don't sync via the service, so don't re-hit info/collections, so
// lastModified remaning at zero breaks things subtly...
engine.lastModified = null;
ping = await sync_engine_and_validate_telem(engine, false);
equal(ping.engines.length, 1);
equal(ping.engines[0].name, "bookmarks-buffered");
equal(ping.engines[0].outgoing.length, 1);
ok(!!ping.engines[0].incoming);
} finally {
// Clean up.
await store.wipe();
await cleanAndGo(engine, server);
}
});
add_task(async function test_upload_failed() {
let collection = new ServerCollection();
collection._wbos.flying = new ServerWBO("flying");
let server = sync_httpd_setup({
"/1.1/foo/storage/rotary": collection.handler(),
});
await SyncTestingInfrastructure(server);
await configureIdentity({ username: "foo" }, server);
let engine = new RotaryEngine(Service);
engine._store.items = {
flying: "LNER Class A3 4472",
scotsman: "Flying Scotsman",
peppercorn: "Peppercorn Class",
};
const FLYING_CHANGED = 12345;
const SCOTSMAN_CHANGED = 23456;
const PEPPERCORN_CHANGED = 34567;
await engine._tracker.addChangedID("flying", FLYING_CHANGED);
await engine._tracker.addChangedID("scotsman", SCOTSMAN_CHANGED);
await engine._tracker.addChangedID("peppercorn", PEPPERCORN_CHANGED);
let syncID = await engine.resetLocalSyncID();
let meta_global = Service.recordManager.set(
engine.metaURL,
new WBORecord(engine.metaURL)
);
meta_global.payload.engines = { rotary: { version: engine.version, syncID } };
try {
await engine.setLastSync(123); // needs to be non-zero so that tracker is queried
let changes = await engine._tracker.getChangedIDs();
_(
`test_upload_failed: Rotary tracker contents at first sync: ${JSON.stringify(
changes
)}`
);
engine.enabled = true;
let ping = await sync_engine_and_validate_telem(engine, true);
ok(!!ping);
equal(ping.engines.length, 1);
equal(ping.engines[0].incoming, null);
deepEqual(ping.engines[0].outgoing, [{ sent: 3, failed: 2 }]);
await engine.setLastSync(123);
changes = await engine._tracker.getChangedIDs();
_(
`test_upload_failed: Rotary tracker contents at second sync: ${JSON.stringify(
changes
)}`
);
ping = await sync_engine_and_validate_telem(engine, true);
ok(!!ping);
equal(ping.engines.length, 1);
equal(ping.engines[0].incoming.reconciled, 1);
deepEqual(ping.engines[0].outgoing, [{ sent: 2, failed: 2 }]);
} finally {
await cleanAndGo(engine, server);
await engine.finalize();
}
});
add_task(async function test_sync_partialUpload() {
let collection = new ServerCollection();
let server = sync_httpd_setup({
"/1.1/foo/storage/rotary": collection.handler(),
});
await SyncTestingInfrastructure(server);
await generateNewKeys(Service.collectionKeys);
let engine = new RotaryEngine(Service);
await engine.setLastSync(123);
// Create a bunch of records (and server side handlers)
for (let i = 0; i < 234; i++) {
let id = "record-no-" + i;
engine._store.items[id] = "Record No. " + i;
await engine._tracker.addChangedID(id, i);
// Let two items in the first upload batch fail.
if (i != 23 && i != 42) {
collection.insert(id);
}
}
let syncID = await engine.resetLocalSyncID();
let meta_global = Service.recordManager.set(
engine.metaURL,
new WBORecord(engine.metaURL)
);
meta_global.payload.engines = { rotary: { version: engine.version, syncID } };
try {
let changes = await engine._tracker.getChangedIDs();
_(
`test_sync_partialUpload: Rotary tracker contents at first sync: ${JSON.stringify(
changes
)}`
);
engine.enabled = true;
let ping = await sync_engine_and_validate_telem(engine, true);
ok(!!ping);
ok(!ping.failureReason);
equal(ping.engines.length, 1);
equal(ping.engines[0].name, "rotary");
ok(!ping.engines[0].incoming);
ok(!ping.engines[0].failureReason);
deepEqual(ping.engines[0].outgoing, [{ sent: 234, failed: 2 }]);
collection.post = function() {
throw new Error("Failure");
};
engine._store.items["record-no-1000"] = "Record No. 1000";
await engine._tracker.addChangedID("record-no-1000", 1000);
collection.insert("record-no-1000", 1000);
await engine.setLastSync(123);
ping = null;
changes = await engine._tracker.getChangedIDs();
_(
`test_sync_partialUpload: Rotary tracker contents at second sync: ${JSON.stringify(
changes
)}`
);
try {
// should throw
await sync_engine_and_validate_telem(
engine,
true,
errPing => (ping = errPing)
);
} catch (e) {}
// It would be nice if we had a more descriptive error for this...
let uploadFailureError = {
name: "httperror",
code: 500,
};
ok(!!ping);
deepEqual(ping.failureReason, uploadFailureError);
equal(ping.engines.length, 1);
equal(ping.engines[0].name, "rotary");
deepEqual(ping.engines[0].incoming, {
failed: 1,
newFailed: 1,
reconciled: 232,
});
ok(!ping.engines[0].outgoing);
deepEqual(ping.engines[0].failureReason, uploadFailureError);
} finally {
await cleanAndGo(engine, server);
await engine.finalize();
}
});
add_task(async function test_generic_engine_fail() {
enableValidationPrefs();
await Service.engineManager.register(SteamEngine);
let engine = Service.engineManager.get("steam");
engine.enabled = true;
let server = await serverForFoo(engine);
await SyncTestingInfrastructure(server);
let e = new Error("generic failure message");
engine._errToThrow = e;
try {
const changes = await engine._tracker.getChangedIDs();
_(
`test_generic_engine_fail: Steam tracker contents: ${JSON.stringify(
changes
)}`
);
await sync_and_validate_telem(ping => {
equal(ping.status.service, SYNC_FAILED_PARTIAL);
deepEqual(ping.engines.find(err => err.name === "steam").failureReason, {
name: "unexpectederror",
error: String(e),
});
});
} finally {
await cleanAndGo(engine, server);
await Service.engineManager.unregister(engine);
}
});
add_task(async function test_engine_fail_weird_errors() {
enableValidationPrefs();
await Service.engineManager.register(SteamEngine);
let engine = Service.engineManager.get("steam");
engine.enabled = true;
let server = await serverForFoo(engine);
await SyncTestingInfrastructure(server);
try {
let msg = "Bad things happened!";
engine._errToThrow = { message: msg };
await sync_and_validate_telem(ping => {
equal(ping.status.service, SYNC_FAILED_PARTIAL);
deepEqual(ping.engines.find(err => err.name === "steam").failureReason, {
name: "unexpectederror",
error: "Bad things happened!",
});
});
let e = { msg };
engine._errToThrow = e;
await sync_and_validate_telem(ping => {
deepEqual(ping.engines.find(err => err.name === "steam").failureReason, {
name: "unexpectederror",
error: JSON.stringify(e),
});
});
} finally {
await cleanAndGo(engine, server);
Service.engineManager.unregister(engine);
}
});
add_task(async function test_overrideTelemetryName() {
enableValidationPrefs(["steam"]);
await Service.engineManager.register(SteamEngine);
let engine = Service.engineManager.get("steam");
engine.overrideTelemetryName = "steam-but-better";
engine.enabled = true;
let server = await serverForFoo(engine);
await SyncTestingInfrastructure(server);
const problemsToReport = [
{ name: "someProblem", count: 123 },
{ name: "anotherProblem", count: 456 },
];
try {
info("Sync with validation problems");
engine.problemsToReport = problemsToReport;
await sync_and_validate_telem(ping => {
let enginePing = ping.engines.find(e => e.name === "steam-but-better");
ok(enginePing);
ok(!ping.engines.find(e => e.name === "steam"));
deepEqual(
enginePing.validation,
{
version: 1,
checked: 0,
problems: problemsToReport,
},
"Should include validation report with overridden name"
);
});
info("Sync without validation problems");
engine.problemsToReport = null;
await sync_and_validate_telem(ping => {
let enginePing = ping.engines.find(e => e.name === "steam-but-better");
ok(enginePing);
ok(!ping.engines.find(e => e.name === "steam"));
ok(
!enginePing.validation,
"Should not include validation report when there are no problems"
);
});
} finally {
await cleanAndGo(engine, server);
await Service.engineManager.unregister(engine);
}
});
add_task(async function test_engine_fail_ioerror() {
enableValidationPrefs();
await Service.engineManager.register(SteamEngine);
let engine = Service.engineManager.get("steam");
engine.enabled = true;
let server = await serverForFoo(engine);
await SyncTestingInfrastructure(server);
// create an IOError to re-throw as part of Sync.
try {
// (Note that fakeservices.js has replaced Utils.jsonMove etc, but for
// this test we need the real one so we get real exceptions from the
// filesystem.)
await Utils._real_jsonMove("file-does-not-exist", "anything", {});
} catch (ex) {
engine._errToThrow = ex;
}
ok(engine._errToThrow, "expecting exception");
try {
const changes = await engine._tracker.getChangedIDs();
_(
`test_engine_fail_ioerror: Steam tracker contents: ${JSON.stringify(
changes
)}`
);
await sync_and_validate_telem(ping => {
equal(ping.status.service, SYNC_FAILED_PARTIAL);
let failureReason = ping.engines.find(e => e.name === "steam")
.failureReason;
equal(failureReason.name, "unexpectederror");
// ensure the profile dir in the exception message has been stripped.
ok(
!failureReason.error.includes(OS.Constants.Path.profileDir),
failureReason.error
);
ok(failureReason.error.includes("[profileDir]"), failureReason.error);
});
} finally {
await cleanAndGo(engine, server);
await Service.engineManager.unregister(engine);
}
});
add_task(async function test_clean_urls() {
enableValidationPrefs();
await Service.engineManager.register(SteamEngine);
let engine = Service.engineManager.get("steam");
engine.enabled = true;
let server = await serverForFoo(engine);
await SyncTestingInfrastructure(server);
engine._errToThrow = new TypeError(
"http://www.google .com is not a valid URL."
);
try {
const changes = await engine._tracker.getChangedIDs();
_(`test_clean_urls: Steam tracker contents: ${JSON.stringify(changes)}`);
await sync_and_validate_telem(ping => {
equal(ping.status.service, SYNC_FAILED_PARTIAL);
let failureReason = ping.engines.find(e => e.name === "steam")
.failureReason;
equal(failureReason.name, "unexpectederror");
equal(failureReason.error, "<URL> is not a valid URL.");
});
// Handle other errors that include urls.
engine._errToThrow =
"Other error message that includes some:url/foo/bar/ in it.";
await sync_and_validate_telem(ping => {
equal(ping.status.service, SYNC_FAILED_PARTIAL);
let failureReason = ping.engines.find(e => e.name === "steam")
.failureReason;
equal(failureReason.name, "unexpectederror");
equal(
failureReason.error,
"Other error message that includes <URL> in it."
);
});
} finally {
await cleanAndGo(engine, server);
await Service.engineManager.unregister(engine);
}
});
add_task(async function test_initial_sync_engines() {
enableValidationPrefs();
await Service.engineManager.register(SteamEngine);
let engine = Service.engineManager.get("steam");
engine.enabled = true;
// These are the only ones who actually have things to sync at startup.
let telemetryEngineNames = ["clients", "prefs", "tabs", "bookmarks-buffered"];
let server = await serverForEnginesWithKeys(
{ foo: "password" },
["bookmarks", "prefs", "tabs"].map(name => Service.engineManager.get(name))
);
await SyncTestingInfrastructure(server);
try {
const changes = await engine._tracker.getChangedIDs();
_(
`test_initial_sync_engines: Steam tracker contents: ${JSON.stringify(
changes
)}`
);
let ping = await wait_for_ping(() => Service.sync(), true);
equal(ping.engines.find(e => e.name === "clients").outgoing[0].sent, 1);
equal(ping.engines.find(e => e.name === "tabs").outgoing[0].sent, 1);
// for the rest we don't care about specifics
for (let e of ping.engines) {
if (!telemetryEngineNames.includes(engine.name)) {
continue;
}
greaterOrEqual(e.took, 1);
ok(!!e.outgoing);
equal(e.outgoing.length, 1);
notEqual(e.outgoing[0].sent, undefined);
equal(e.outgoing[0].failed, undefined);
}
} finally {
await cleanAndGo(engine, server);
await Service.engineManager.unregister(engine);
}
});
add_task(async function test_nserror() {
enableValidationPrefs();
await Service.engineManager.register(SteamEngine);
let engine = Service.engineManager.get("steam");
engine.enabled = true;
let server = await serverForFoo(engine);
await SyncTestingInfrastructure(server);
engine._errToThrow = Components.Exception(
"NS_ERROR_UNKNOWN_HOST",
Cr.NS_ERROR_UNKNOWN_HOST
);
try {
const changes = await engine._tracker.getChangedIDs();
_(`test_nserror: Steam tracker contents: ${JSON.stringify(changes)}`);
await sync_and_validate_telem(ping => {
deepEqual(ping.status, {
service: SYNC_FAILED_PARTIAL,
sync: LOGIN_FAILED_NETWORK_ERROR,
});
let enginePing = ping.engines.find(e => e.name === "steam");
deepEqual(enginePing.failureReason, {
name: "nserror",
code: Cr.NS_ERROR_UNKNOWN_HOST,
});
});
} finally {
await cleanAndGo(engine, server);
await Service.engineManager.unregister(engine);
}
});
add_task(async function test_sync_why() {
enableValidationPrefs();
await Service.engineManager.register(SteamEngine);
let engine = Service.engineManager.get("steam");
engine.enabled = true;
let server = await serverForFoo(engine);
await SyncTestingInfrastructure(server);
let e = new Error("generic failure message");
engine._errToThrow = e;
try {
const changes = await engine._tracker.getChangedIDs();
_(
`test_generic_engine_fail: Steam tracker contents: ${JSON.stringify(
changes
)}`
);
let ping = await wait_for_ping(
() => Service.sync({ why: "user" }),
true,
false
);
_(JSON.stringify(ping));
equal(ping.why, "user");
} finally {
await cleanAndGo(engine, server);
await Service.engineManager.unregister(engine);
}
});
add_task(async function test_discarding() {
enableValidationPrefs();
let helper = track_collections_helper();
let upd = helper.with_updated_collection;
let telem = get_sync_test_telemetry();
telem.maxPayloadCount = 2;
telem.submissionInterval = Infinity;
let oldSubmit = telem.submit;
let server;
try {
let handlers = {
"/1.1/johndoe/info/collections": helper.handler,
"/1.1/johndoe/storage/crypto/keys": upd(
"crypto",
new ServerWBO("keys").handler()
),
"/1.1/johndoe/storage/meta/global": upd(
"meta",
new ServerWBO("global").handler()
),
};
let collections = [
"clients",
"bookmarks",
"forms",
"history",
"passwords",
"prefs",
"tabs",
];
for (let coll of collections) {
handlers["/1.1/johndoe/storage/" + coll] = upd(
coll,
new ServerCollection({}, true).handler()
);
}
server = httpd_setup(handlers);
await configureIdentity({ username: "johndoe" }, server);
telem.submit = p =>
ok(
false,
"Submitted telemetry ping when we should not have" + JSON.stringify(p)
);
for (let i = 0; i < 5; ++i) {
await Service.sync();
}
telem.submit = oldSubmit;
telem.submissionInterval = -1;
let ping = await wait_for_ping(() => Service.sync(), true, true); // with this we've synced 6 times
equal(ping.syncs.length, 2);
equal(ping.discarded, 4);
} finally {
telem.maxPayloadCount = 500;
telem.submissionInterval = -1;
telem.submit = oldSubmit;
if (server) {
await promiseStopServer(server);
}
}
});
add_task(async function test_submit_interval() {
let telem = get_sync_test_telemetry();
let oldSubmit = telem.submit;
let numSubmissions = 0;
telem.submit = function() {
numSubmissions += 1;
};
function notify(what, data = null) {
Svc.Obs.notify(what, JSON.stringify(data));
}
try {
// submissionInterval is set such that each sync should submit
notify("weave:service:sync:start", { why: "testing" });
notify("weave:service:sync:finish");
Assert.equal(numSubmissions, 1, "should submit this ping due to interval");
// As should each event outside of a sync.
Service.recordTelemetryEvent("object", "method");
Assert.equal(numSubmissions, 2);
// But events while we are syncing should not.
notify("weave:service:sync:start", { why: "testing" });
Service.recordTelemetryEvent("object", "method");
Assert.equal(numSubmissions, 2, "no submission for this event");
notify("weave:service:sync:finish");
Assert.equal(numSubmissions, 3, "was submitted after sync finish");
} finally {
telem.submit = oldSubmit;
}
});
add_task(async function test_no_foreign_engines_in_error_ping() {
enableValidationPrefs();
await Service.engineManager.register(BogusEngine);
let engine = Service.engineManager.get("bogus");
engine.enabled = true;
let server = await serverForFoo(engine);
engine._errToThrow = new Error("Oh no!");
await SyncTestingInfrastructure(server);
try {
await sync_and_validate_telem(ping => {
equal(ping.status.service, SYNC_FAILED_PARTIAL);
ok(ping.engines.every(e => e.name !== "bogus"));
});
} finally {
await cleanAndGo(engine, server);
await Service.engineManager.unregister(engine);
}
});
add_task(async function test_no_foreign_engines_in_success_ping() {
enableValidationPrefs();
await Service.engineManager.register(BogusEngine);
let engine = Service.engineManager.get("bogus");
engine.enabled = true;
let server = await serverForFoo(engine);
await SyncTestingInfrastructure(server);
try {
await sync_and_validate_telem(ping => {
ok(ping.engines.every(e => e.name !== "bogus"));
});
} finally {
await cleanAndGo(engine, server);
await Service.engineManager.unregister(engine);
}
});
add_task(async function test_events() {
enableValidationPrefs();
await Service.engineManager.register(BogusEngine);
let engine = Service.engineManager.get("bogus");
engine.enabled = true;
let server = await serverForFoo(engine);
await SyncTestingInfrastructure(server);
let telem = get_sync_test_telemetry();
telem.submissionInterval = Infinity;
try {
let serverTime = Resource.serverTime;
Service.recordTelemetryEvent("object", "method", "value", { foo: "bar" });
let ping = await wait_for_ping(() => Service.sync(), true, true);
equal(ping.events.length, 1);
let [timestamp, category, method, object, value, extra] = ping.events[0];
ok(typeof timestamp == "number" && timestamp > 0); // timestamp.
equal(category, "sync");
equal(method, "method");
equal(object, "object");
equal(value, "value");
deepEqual(extra, { foo: "bar", serverTime: String(serverTime) });
ping = await wait_for_ping(
() => {
// Test with optional values.
Service.recordTelemetryEvent("object", "method");
},
false,
true
);
equal(ping.events.length, 1);
equal(ping.events[0].length, 4);
ping = await wait_for_ping(
() => {
Service.recordTelemetryEvent("object", "method", "extra");
},
false,
true
);
equal(ping.events.length, 1);
equal(ping.events[0].length, 5);
ping = await wait_for_ping(
() => {
Service.recordTelemetryEvent("object", "method", undefined, {
foo: "bar",
});
},
false,
true
);
equal(ping.events.length, 1);
equal(ping.events[0].length, 6);
[timestamp, category, method, object, value, extra] = ping.events[0];
equal(value, null);
// Fake a submission due to shutdown.
ping = await wait_for_ping(
() => {
telem.submissionInterval = Infinity;
Service.recordTelemetryEvent("object", "method", undefined, {
foo: "bar",
});
telem.finish("shutdown");
},
false,
true
);
equal(ping.syncs.length, 0);
equal(ping.events.length, 1);
equal(ping.events[0].length, 6);
} finally {
await cleanAndGo(engine, server);
await Service.engineManager.unregister(engine);
}
});
add_task(async function test_histograms() {
enableValidationPrefs();
await Service.engineManager.register(BogusEngine);
let engine = Service.engineManager.get("bogus");
engine.enabled = true;
let server = await serverForFoo(engine);
await SyncTestingInfrastructure(server);
try {
let histId = "TELEMETRY_TEST_LINEAR";
Services.obs.notifyObservers(null, "weave:telemetry:histogram", histId);
let ping = await wait_for_ping(() => Service.sync(), true, true);
equal(Object.keys(ping.histograms).length, 1);
equal(ping.histograms[histId].sum, 0);
equal(ping.histograms[histId].histogram_type, 1);
} finally {
await cleanAndGo(engine, server);
await Service.engineManager.unregister(engine);
}
});
add_task(async function test_invalid_events() {
enableValidationPrefs();
await Service.engineManager.register(BogusEngine);
let engine = Service.engineManager.get("bogus");
engine.enabled = true;
let server = await serverForFoo(engine);
async function checkNotRecorded(...args) {
Service.recordTelemetryEvent.call(args);
let ping = await wait_for_ping(() => Service.sync(), false, true);
equal(ping.events, undefined);
}
await SyncTestingInfrastructure(server);
try {
let long21 = "l".repeat(21);
let long81 = "l".repeat(81);
let long86 = "l".repeat(86);
await checkNotRecorded("object");
await checkNotRecorded("object", 2);
await checkNotRecorded(2, "method");
await checkNotRecorded("object", "method", 2);
await checkNotRecorded("object", "method", "value", 2);
await checkNotRecorded("object", "method", "value", { foo: 2 });
await checkNotRecorded(long21, "method", "value");
await checkNotRecorded("object", long21, "value");
await checkNotRecorded("object", "method", long81);
let badextra = {};
badextra[long21] = "x";
await checkNotRecorded("object", "method", "value", badextra);
badextra = { x: long86 };
await checkNotRecorded("object", "method", "value", badextra);
for (let i = 0; i < 10; i++) {
badextra["name" + i] = "x";
}
await checkNotRecorded("object", "method", "value", badextra);
} finally {
await cleanAndGo(engine, server);
await Service.engineManager.unregister(engine);
}
});
add_task(async function test_no_ping_for_self_hosters() {
enableValidationPrefs();
let telem = get_sync_test_telemetry();
let oldSubmit = telem.submit;
await Service.engineManager.register(BogusEngine);
let engine = Service.engineManager.get("bogus");
engine.enabled = true;
let server = await serverForFoo(engine);
await SyncTestingInfrastructure(server);
try {
let submitPromise = new Promise(resolve => {
telem.submit = function() {
let result = oldSubmit.apply(this, arguments);
resolve(result);
};
});
await Service.sync();
let pingSubmitted = await submitPromise;
// The Sync testing infrastructure already sets up a custom token server,
// so we don't need to do anything to simulate a self-hosted user.
ok(!pingSubmitted, "Should not submit ping with custom token server URL");
} finally {
telem.submit = oldSubmit;
await cleanAndGo(engine, server);
await Service.engineManager.unregister(engine);
}
});
add_task(async function test_fxa_device_telem() {
let t = get_sync_test_telemetry();
let syncEnabled = true;
let oldGetClientsEngineRecords = t.getClientsEngineRecords;
let oldGetFxaDevices = t.getFxaDevices;
let oldSyncIsEnabled = t.syncIsEnabled;
let oldSanitizeFxaDeviceId = t.sanitizeFxaDeviceId;
t.syncIsEnabled = () => syncEnabled;
t.sanitizeFxaDeviceId = id => `So clean: ${id}`;
try {
let keep0 = Utils.makeGUID();
let keep1 = Utils.makeGUID();
let keep2 = Utils.makeGUID();
let curdev = Utils.makeGUID();
let keep1Sync = Utils.makeGUID();
let keep2Sync = Utils.makeGUID();
let curdevSync = Utils.makeGUID();
let fxaDevices = [
{
id: curdev,
isCurrentDevice: true,
lastAccessTime: Date.now() - 1000 * 60 * 60 * 24 * 1,
pushEndpointExpired: false,
type: "desktop",
name: "current device",
},
{
id: keep0,
isCurrentDevice: false,
lastAccessTime: Date.now() - 1000 * 60 * 60 * 24 * 10,
pushEndpointExpired: false,
type: "mobile",
name: "dupe",
},
// Valid 2
{
id: keep1,
isCurrentDevice: false,
lastAccessTime: Date.now() - 1000 * 60 * 60 * 24 * 1,
pushEndpointExpired: false,
type: "desktop",
name: "valid2",
},
// Valid 3
{
id: keep2,
isCurrentDevice: false,
lastAccessTime: Date.now() - 1000 * 60 * 60 * 24 * 5,
pushEndpointExpired: false,
type: "desktop",
name: "valid3",
},
];
let clientInfo = [
{
id: keep1Sync,
fxaDeviceId: keep1,
os: "Windows 30",
version: "Firefox 1 million",
},
{
id: keep2Sync,
fxaDeviceId: keep2,
os: "firefox, but an os",
verison: "twelve",
},
{
id: Utils.makeGUID(),
fxaDeviceId: null,
os: "apparently ios used to keep write these IDs as null.",
version: "Doesn't seem to anymore",
},
{
id: curdevSync,
fxaDeviceId: curdev,
os: "emacs",
version: "22",
},
{
id: Utils.makeGUID(),
fxaDeviceId: Utils.makeGUID(),
os: "not part of the fxa device set at all",
version: "foo bar baz",
},
// keep0 intententionally omitted.
];
t.getClientsEngineRecords = () => clientInfo;
let devInfo = t.updateFxaDevices(fxaDevices);
equal(devInfo.deviceID, t.sanitizeFxaDeviceId(curdev));
for (let d of devInfo.devices) {
ok(d.id.startsWith("So clean:"));
if (d.syncID) {
ok(d.syncID.startsWith("So clean:"));
}
}
equal(devInfo.devices.length, 4);
let k0 = devInfo.devices.find(d => d.id == t.sanitizeFxaDeviceId(keep0));
let k1 = devInfo.devices.find(d => d.id == t.sanitizeFxaDeviceId(keep1));
let k2 = devInfo.devices.find(d => d.id == t.sanitizeFxaDeviceId(keep2));
deepEqual(k0, {
id: t.sanitizeFxaDeviceId(keep0),
type: "mobile",
os: undefined,
version: undefined,
syncID: undefined,
});
deepEqual(k1, {
id: t.sanitizeFxaDeviceId(keep1),
type: "desktop",
os: clientInfo[0].os,
version: clientInfo[0].version,
syncID: t.sanitizeFxaDeviceId(keep1Sync),
});
deepEqual(k2, {
id: t.sanitizeFxaDeviceId(keep2),
type: "desktop",
os: clientInfo[1].os,
version: clientInfo[1].version,
syncID: t.sanitizeFxaDeviceId(keep2Sync),
});
let newCurId = Utils.makeGUID();
// Update the ID
fxaDevices[0].id = newCurId;
let keep3 = Utils.makeGUID();
fxaDevices.push({
id: keep3,
isCurrentDevice: false,
lastAccessTime: Date.now() - 1000 * 60 * 60 * 24 * 1,
pushEndpointExpired: false,
type: "desktop",
name: "valid 4",
});
devInfo = t.updateFxaDevices(fxaDevices);
let afterSubmit = [keep0, keep1, keep2, keep3, newCurId]
.map(id => t.sanitizeFxaDeviceId(id))
.sort();
deepEqual(devInfo.devices.map(d => d.id).sort(), afterSubmit);
// Reset this, as our override doesn't check for sync being enabled.
t.sanitizeFxaDeviceId = oldSanitizeFxaDeviceId;
syncEnabled = false;
fxAccounts.telemetry._setHashedUID(false);
devInfo = t.updateFxaDevices(fxaDevices);
equal(devInfo.deviceID, undefined);
equal(devInfo.devices.length, 5);
for (let d of devInfo.devices) {
equal(d.os, undefined);
equal(d.version, undefined);
equal(d.syncID, undefined);
// Type should still be present.
notEqual(d.type, undefined);
}
} finally {
t.getClientsEngineRecords = oldGetClientsEngineRecords;
t.getFxaDevices = oldGetFxaDevices;
t.syncIsEnabled = oldSyncIsEnabled;
t.sanitizeFxaDeviceId = oldSanitizeFxaDeviceId;
}
});
add_task(async function test_sanitize_fxa_device_id() {
let t = get_sync_test_telemetry();
fxAccounts.telemetry._setHashedUID(false);
sinon.stub(t, "syncIsEnabled").callsFake(() => true);
const rawDeviceId = "raw one two three";
try {
equal(t.sanitizeFxaDeviceId(rawDeviceId), null);
fxAccounts.telemetry._setHashedUID("mock uid");
const sanitizedDeviceId = t.sanitizeFxaDeviceId(rawDeviceId);
ok(sanitizedDeviceId);
notEqual(sanitizedDeviceId, rawDeviceId);
} finally {
t.syncIsEnabled.restore();
fxAccounts.telemetry._setHashedUID(false);
}
});
add_task(async function test_no_node_type() {
let server = sync_httpd_setup({});
await configureIdentity(null, server);
await sync_and_validate_telem(ping => {
ok(ping.syncNodeType === undefined);
}, true);
await promiseStopServer(server);
});
add_task(async function test_node_type() {
Service.identity.logout();
let server = sync_httpd_setup({});
await configureIdentity({ node_type: "the-node-type" }, server);
await sync_and_validate_telem(ping => {
equal(ping.syncNodeType, "the-node-type");
}, true);
await promiseStopServer(server);
});
add_task(async function test_node_type_change() {
let pingPromise = wait_for_pings(2);
Service.identity.logout();
let server = sync_httpd_setup({});
await configureIdentity({ node_type: "first-node-type" }, server);
// Default to submitting each hour - we should still submit on node change.
let telem = get_sync_test_telemetry();
telem.submissionInterval = 60 * 60 * 1000;
// reset the node type from previous test or our first sync will submit.
telem.lastSyncNodeType = null;
// do 2 syncs with the same node type.
await Service.sync();
await Service.sync();
// then another with a different node type.
Service.identity.logout();
await configureIdentity({ node_type: "second-node-type" }, server);
await Service.sync();
telem.finish();
let pings = await pingPromise;
equal(pings.length, 2);
equal(pings[0].syncs.length, 2, "2 syncs in first ping");
equal(pings[0].syncNodeType, "first-node-type");
equal(pings[1].syncs.length, 1, "1 sync in second ping");
equal(pings[1].syncNodeType, "second-node-type");
await promiseStopServer(server);
});
add_task(async function test_ids() {
let telem = get_sync_test_telemetry();
Assert.ok(!telem._shouldSubmitForDataChange());
fxAccounts.telemetry._setHashedUID("new_uid");
Assert.ok(telem._shouldSubmitForDataChange());
telem.maybeSubmitForDataChange();
// now it's been submitted the new uid is current.
Assert.ok(!telem._shouldSubmitForDataChange());
});
add_task(async function test_deletion_request_ping() {
async function assertRecordedSyncDeviceID(expected) {
// The scalar gets updated asynchronously, so wait a tick before checking.
await Promise.resolve();
const scalars =
Services.telemetry.getSnapshotForScalars("deletion-request").parent || {};
equal(scalars["deletion.request.sync_device_id"], expected);
}
const MOCK_HASHED_UID = "00112233445566778899aabbccddeeff";
const MOCK_DEVICE_ID1 = "ffeeddccbbaa99887766554433221100";
const MOCK_DEVICE_ID2 = "aabbccddeeff99887766554433221100";
// Calculated by hand using SHA256(DEVICE_ID + HASHED_UID)[:32]
const SANITIZED_DEVICE_ID1 = "dd7c845006df9baa1c6d756926519c8c";
const SANITIZED_DEVICE_ID2 = "0d06919a736fc029007e1786a091882c";
let currentDeviceID = null;
sinon.stub(fxAccounts.device, "getLocalId").callsFake(() => {
return Promise.resolve(currentDeviceID);
});
let telem = get_sync_test_telemetry();
sinon.stub(telem, "isProductionSyncUser").callsFake(() => true);
fxAccounts.telemetry._setHashedUID(false);
try {
// The scalar should start out undefined, since no user is actually logged in.
await assertRecordedSyncDeviceID(undefined);
// If we start up without knowing the hashed UID, it should stay undefined.
telem.observe(null, "weave:service:ready");
await assertRecordedSyncDeviceID(undefined);
// But now let's say we've discovered the hashed UID from the server.
fxAccounts.telemetry._setHashedUID(MOCK_HASHED_UID);
currentDeviceID = MOCK_DEVICE_ID1;
// Now when we load up, we'll record the sync device id.
telem.observe(null, "weave:service:ready");
await assertRecordedSyncDeviceID(SANITIZED_DEVICE_ID1);
// When the device-id changes we'll update it.
currentDeviceID = MOCK_DEVICE_ID2;
telem.observe(null, "fxaccounts:new_device_id");
await assertRecordedSyncDeviceID(SANITIZED_DEVICE_ID2);
// When the user signs out we'll clear it.
telem.observe(null, "fxaccounts:onlogout");
await assertRecordedSyncDeviceID("");
} finally {
fxAccounts.telemetry._setHashedUID(false);
telem.isProductionSyncUser.restore();
fxAccounts.device.getLocalId.restore();
}
});
|