summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/unit/test_sync.js
blob: bc9467d7c9a003cf327df9171e5fee7987ca3331 (plain)
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
/**
 * Tests sync functionality.
 */

/* import-globals-from ../../../../../services/sync/tests/unit/head_appinfo.js */
/* import-globals-from ../../../../../services/common/tests/unit/head_helpers.js */
/* import-globals-from ../../../../../services/sync/tests/unit/head_helpers.js */
/* import-globals-from ../../../../../services/sync/tests/unit/head_http_server.js */

"use strict";

const { Service } = ChromeUtils.importESModule(
  "resource://services-sync/service.sys.mjs"
);
const { SCORE_INCREMENT_XLARGE } = ChromeUtils.importESModule(
  "resource://services-sync/constants.sys.mjs"
);

const { sanitizeStorageObject, AutofillRecord, AddressesEngine } =
  ChromeUtils.importESModule("resource://autofill/FormAutofillSync.sys.mjs");

Services.prefs.setCharPref("extensions.formautofill.loglevel", "Trace");
initTestLogging("Trace");

const TEST_STORE_FILE_NAME = "test-profile.json";

const TEST_PROFILE_1 = {
  "given-name": "Timothy",
  "additional-name": "John",
  "family-name": "Berners-Lee",
  organization: "World Wide Web Consortium",
  "street-address": "32 Vassar Street\nMIT Room 32-G524",
  "address-level2": "Cambridge",
  "address-level1": "MA",
  "postal-code": "02139",
  country: "US",
  tel: "+16172535702",
  email: "timbl@w3.org",
  // A field this client doesn't "understand" from another client
  "unknown-1": "some unknown data from another client",
};

const TEST_PROFILE_2 = {
  "street-address": "Some Address",
  country: "US",
};

async function expectLocalProfiles(profileStorage, expected) {
  let profiles = await profileStorage.addresses.getAll({
    rawData: true,
    includeDeleted: true,
  });
  expected.sort((a, b) => a.guid.localeCompare(b.guid));
  profiles.sort((a, b) => a.guid.localeCompare(b.guid));
  try {
    deepEqual(
      profiles.map(p => p.guid),
      expected.map(p => p.guid)
    );
    for (let i = 0; i < expected.length; i++) {
      let thisExpected = expected[i];
      let thisGot = profiles[i];
      // always check "deleted".
      equal(thisExpected.deleted, thisGot.deleted);
      ok(objectMatches(thisGot, thisExpected));
    }
  } catch (ex) {
    info("Comparing expected profiles:");
    info(JSON.stringify(expected, undefined, 2));
    info("against actual profiles:");
    info(JSON.stringify(profiles, undefined, 2));
    throw ex;
  }
}

async function setup() {
  let profileStorage = await initProfileStorage(TEST_STORE_FILE_NAME);
  // should always start with no profiles.
  Assert.equal(
    (await profileStorage.addresses.getAll({ includeDeleted: true })).length,
    0
  );

  Services.prefs.setCharPref(
    "services.sync.log.logger.engine.addresses",
    "Trace"
  );
  let engine = new AddressesEngine(Service);
  await engine.initialize();
  // Avoid accidental automatic sync due to our own changes
  Service.scheduler.syncThreshold = 10000000;
  let syncID = await engine.resetLocalSyncID();
  let server = serverForUsers(
    { foo: "password" },
    {
      meta: {
        global: { engines: { addresses: { version: engine.version, syncID } } },
      },
      addresses: {},
    }
  );

  Service.engineManager._engines.addresses = engine;
  engine.enabled = true;
  engine._store._storage = profileStorage.addresses;

  generateNewKeys(Service.collectionKeys);

  await SyncTestingInfrastructure(server);

  let collection = server.user("foo").collection("addresses");

  return { profileStorage, server, collection, engine };
}

async function cleanup(server) {
  let promiseStartOver = promiseOneObserver("weave:service:start-over:finish");
  await Service.startOver();
  await promiseStartOver;
  await promiseStopServer(server);
}

add_task(async function test_log_sanitization() {
  let sanitized = sanitizeStorageObject(TEST_PROFILE_1);
  // all strings have been mangled.
  for (let key of Object.keys(TEST_PROFILE_1)) {
    let val = TEST_PROFILE_1[key];
    if (typeof val == "string") {
      notEqual(sanitized[key], val);
    }
  }
  // And check that stringifying a sync record is sanitized.
  let record = new AutofillRecord("collection", "some-id");
  record.entry = TEST_PROFILE_1;
  let serialized = record.toString();
  // None of the string values should appear in the output.
  for (let key of Object.keys(TEST_PROFILE_1)) {
    let val = TEST_PROFILE_1[key];
    if (typeof val == "string") {
      ok(!serialized.includes(val), `"${val}" shouldn't be in: ${serialized}`);
    }
  }
});

add_task(async function test_outgoing() {
  let { profileStorage, server, collection, engine } = await setup();
  try {
    equal(engine._tracker.score, 0);
    let existingGUID = await profileStorage.addresses.add(TEST_PROFILE_1);
    // And a deleted item.
    let deletedGUID = profileStorage.addresses._generateGUID();
    await profileStorage.addresses.add({ guid: deletedGUID, deleted: true });

    await expectLocalProfiles(profileStorage, [
      {
        guid: existingGUID,
      },
      {
        guid: deletedGUID,
        deleted: true,
      },
    ]);

    await engine._tracker.asyncObserver.promiseObserversComplete();
    // The tracker should have a score recorded for the 2 additions we had.
    equal(engine._tracker.score, SCORE_INCREMENT_XLARGE * 2);

    await engine.setLastSync(0);
    await engine.sync();

    Assert.equal(collection.count(), 2);
    Assert.ok(collection.wbo(existingGUID));
    Assert.ok(collection.wbo(deletedGUID));

    await expectLocalProfiles(profileStorage, [
      {
        guid: existingGUID,
      },
      {
        guid: deletedGUID,
        deleted: true,
      },
    ]);

    strictEqual(
      getSyncChangeCounter(profileStorage.addresses, existingGUID),
      0
    );
    strictEqual(getSyncChangeCounter(profileStorage.addresses, deletedGUID), 0);
  } finally {
    await cleanup(server);
  }
});

add_task(async function test_incoming_new() {
  let { profileStorage, server, engine } = await setup();
  try {
    let profileID = Utils.makeGUID();
    let deletedID = Utils.makeGUID();

    server.insertWBO(
      "foo",
      "addresses",
      new ServerWBO(
        profileID,
        encryptPayload({
          id: profileID,
          entry: Object.assign(
            {
              version: 1,
            },
            TEST_PROFILE_1
          ),
        }),
        getDateForSync()
      )
    );
    server.insertWBO(
      "foo",
      "addresses",
      new ServerWBO(
        deletedID,
        encryptPayload({
          id: deletedID,
          deleted: true,
        }),
        getDateForSync()
      )
    );

    // The tracker should start with no score.
    equal(engine._tracker.score, 0);

    await engine.setLastSync(0);
    await engine.sync();

    await expectLocalProfiles(profileStorage, [
      {
        guid: profileID,
      },
      {
        guid: deletedID,
        deleted: true,
      },
    ]);

    strictEqual(getSyncChangeCounter(profileStorage.addresses, profileID), 0);
    strictEqual(getSyncChangeCounter(profileStorage.addresses, deletedID), 0);

    // Validate incoming records with unknown fields get stored
    let localRecord = await profileStorage.addresses.get(profileID);
    equal(localRecord["unknown-1"], TEST_PROFILE_1["unknown-1"]);

    // The sync applied new records - ensure our tracker knew it came from
    // sync and didn't bump the score.
    equal(engine._tracker.score, 0);
  } finally {
    await cleanup(server);
  }
});

add_task(async function test_incoming_existing() {
  let { profileStorage, server, engine } = await setup();
  try {
    let guid1 = await profileStorage.addresses.add(TEST_PROFILE_1);
    let guid2 = await profileStorage.addresses.add(TEST_PROFILE_2);

    // an initial sync so we don't think they are locally modified.
    await engine.setLastSync(0);
    await engine.sync();

    // now server records that modify the existing items.
    let modifiedEntry1 = Object.assign({}, TEST_PROFILE_1, {
      version: 1,
      "given-name": "NewName",
    });

    let lastSync = await engine.getLastSync();
    server.insertWBO(
      "foo",
      "addresses",
      new ServerWBO(
        guid1,
        encryptPayload({
          id: guid1,
          entry: modifiedEntry1,
        }),
        lastSync + 10
      )
    );
    server.insertWBO(
      "foo",
      "addresses",
      new ServerWBO(
        guid2,
        encryptPayload({
          id: guid2,
          deleted: true,
        }),
        lastSync + 10
      )
    );

    await engine.sync();

    await expectLocalProfiles(profileStorage, [
      Object.assign({}, modifiedEntry1, { guid: guid1 }),
      { guid: guid2, deleted: true },
    ]);
  } finally {
    await cleanup(server);
  }
});

add_task(async function test_tombstones() {
  let { profileStorage, server, collection, engine } = await setup();
  try {
    let existingGUID = await profileStorage.addresses.add(TEST_PROFILE_1);

    await engine.setLastSync(0);
    await engine.sync();

    Assert.equal(collection.count(), 1);
    let payload = collection.payloads()[0];
    equal(payload.id, existingGUID);
    equal(payload.deleted, undefined);

    profileStorage.addresses.remove(existingGUID);
    await engine.sync();

    // should still exist, but now be a tombstone.
    Assert.equal(collection.count(), 1);
    payload = collection.payloads()[0];
    equal(payload.id, existingGUID);
    equal(payload.deleted, true);
  } finally {
    await cleanup(server);
  }
});

add_task(async function test_applyIncoming_both_deleted() {
  let { profileStorage, server, engine } = await setup();
  try {
    let guid = await profileStorage.addresses.add(TEST_PROFILE_1);

    await engine.setLastSync(0);
    await engine.sync();

    // Delete synced record locally.
    profileStorage.addresses.remove(guid);

    // Delete same record remotely.
    let lastSync = await engine.getLastSync();
    let collection = server.user("foo").collection("addresses");
    collection.insert(
      guid,
      encryptPayload({
        id: guid,
        deleted: true,
      }),
      lastSync + 10
    );

    await engine.sync();

    ok(
      !(await await profileStorage.addresses.get(guid)),
      "Should not return record for locally deleted item"
    );

    let localRecords = await profileStorage.addresses.getAll({
      includeDeleted: true,
    });
    equal(localRecords.length, 1, "Only tombstone should exist locally");

    equal(collection.count(), 1, "Only tombstone should exist on server");
  } finally {
    await cleanup(server);
  }
});

add_task(async function test_applyIncoming_nonexistent_tombstone() {
  let { profileStorage, server, engine } = await setup();
  try {
    let guid = profileStorage.addresses._generateGUID();
    let collection = server.user("foo").collection("addresses");
    collection.insert(
      guid,
      encryptPayload({
        id: guid,
        deleted: true,
      }),
      getDateForSync()
    );

    await engine.setLastSync(0);
    await engine.sync();

    ok(
      !(await profileStorage.addresses.get(guid)),
      "Should not return record for uknown deleted item"
    );
    let localTombstone = (
      await profileStorage.addresses.getAll({
        includeDeleted: true,
      })
    ).find(record => record.guid == guid);
    ok(localTombstone, "Should store tombstone for unknown item");
  } finally {
    await cleanup(server);
  }
});

add_task(async function test_applyIncoming_incoming_deleted() {
  let { profileStorage, server, engine } = await setup();
  try {
    let guid = await profileStorage.addresses.add(TEST_PROFILE_1);

    await engine.setLastSync(0);
    await engine.sync();

    // Delete the record remotely.
    let lastSync = await engine.getLastSync();
    let collection = server.user("foo").collection("addresses");
    collection.insert(
      guid,
      encryptPayload({
        id: guid,
        deleted: true,
      }),
      lastSync + 10
    );

    await engine.sync();

    ok(
      !(await profileStorage.addresses.get(guid)),
      "Should delete unmodified item locally"
    );

    let localTombstone = (
      await profileStorage.addresses.getAll({
        includeDeleted: true,
      })
    ).find(record => record.guid == guid);
    ok(localTombstone, "Should keep local tombstone for remotely deleted item");
    strictEqual(
      getSyncChangeCounter(profileStorage.addresses, guid),
      0,
      "Local tombstone should be marked as syncing"
    );
  } finally {
    await cleanup(server);
  }
});

add_task(async function test_applyIncoming_incoming_restored() {
  let { profileStorage, server, engine } = await setup();
  try {
    let guid = await profileStorage.addresses.add(TEST_PROFILE_1);

    // Upload the record to the server.
    await engine.setLastSync(0);
    await engine.sync();

    // Removing a synced record should write a tombstone.
    profileStorage.addresses.remove(guid);

    // Modify the deleted record remotely.
    let collection = server.user("foo").collection("addresses");
    let serverPayload = JSON.parse(
      JSON.parse(collection.payload(guid)).ciphertext
    );
    serverPayload.entry["street-address"] = "I moved!";
    let lastSync = await engine.getLastSync();
    collection.insert(guid, encryptPayload(serverPayload), lastSync + 10);

    // Sync again.
    await engine.sync();

    // We should replace our tombstone with the server's version.
    let localRecord = await profileStorage.addresses.get(guid);
    ok(
      objectMatches(localRecord, {
        "given-name": "Timothy",
        "family-name": "Berners-Lee",
        "street-address": "I moved!",
      })
    );

    let maybeNewServerPayload = JSON.parse(
      JSON.parse(collection.payload(guid)).ciphertext
    );
    deepEqual(
      maybeNewServerPayload,
      serverPayload,
      "Should not change record on server"
    );
  } finally {
    await cleanup(server);
  }
});

add_task(async function test_applyIncoming_outgoing_restored() {
  let { profileStorage, server, engine } = await setup();
  try {
    let guid = await profileStorage.addresses.add(TEST_PROFILE_1);

    // Upload the record to the server.
    await engine.setLastSync(0);
    await engine.sync();

    // Modify the local record.
    let localCopy = Object.assign({}, TEST_PROFILE_1);
    localCopy["street-address"] = "I moved!";
    await profileStorage.addresses.update(guid, localCopy);

    // Replace the record with a tombstone on the server.
    let lastSync = await engine.getLastSync();
    let collection = server.user("foo").collection("addresses");
    collection.insert(
      guid,
      encryptPayload({
        id: guid,
        deleted: true,
      }),
      lastSync + 10
    );

    // Sync again.
    await engine.sync();

    // We should resurrect the record on the server.
    let serverPayload = JSON.parse(
      JSON.parse(collection.payload(guid)).ciphertext
    );
    ok(!serverPayload.deleted, "Should resurrect record on server");
    ok(
      objectMatches(serverPayload.entry, {
        "given-name": "Timothy",
        "family-name": "Berners-Lee",
        "street-address": "I moved!",
        // resurrection also beings back any unknown fields we had
        "unknown-1": "some unknown data from another client",
      })
    );

    let localRecord = await profileStorage.addresses.get(guid);
    ok(localRecord, "Modified record should not be deleted locally");
  } finally {
    await cleanup(server);
  }
});

// Unlike most sync engines, we want "both modified" to inspect the records,
// and if materially different, create a duplicate.
add_task(async function test_reconcile_both_modified_identical() {
  let { profileStorage, server, engine } = await setup();
  try {
    // create a record locally.
    let guid = await profileStorage.addresses.add(TEST_PROFILE_1);

    // and an identical record on the server.
    server.insertWBO(
      "foo",
      "addresses",
      new ServerWBO(
        guid,
        encryptPayload({
          id: guid,
          entry: TEST_PROFILE_1,
        }),
        getDateForSync()
      )
    );

    await engine.setLastSync(0);
    await engine.sync();

    await expectLocalProfiles(profileStorage, [{ guid }]);
  } finally {
    await cleanup(server);
  }
});

add_task(async function test_incoming_dupes() {
  let { profileStorage, server, engine } = await setup();
  try {
    // Create a profile locally, then sync to upload the new profile to the
    // server.
    let guid1 = await profileStorage.addresses.add(TEST_PROFILE_1);

    await engine.setLastSync(0);
    await engine.sync();

    // Create another profile locally, but don't sync it yet.
    await profileStorage.addresses.add(TEST_PROFILE_2);

    // Now create two records on the server with the same contents as our local
    // profiles, but different GUIDs.
    let lastSync = await engine.getLastSync();
    let guid1_dupe = Utils.makeGUID();
    server.insertWBO(
      "foo",
      "addresses",
      new ServerWBO(
        guid1_dupe,
        encryptPayload({
          id: guid1_dupe,
          entry: Object.assign(
            {
              version: 1,
            },
            TEST_PROFILE_1
          ),
        }),
        lastSync + 10
      )
    );
    let guid2_dupe = Utils.makeGUID();
    server.insertWBO(
      "foo",
      "addresses",
      new ServerWBO(
        guid2_dupe,
        encryptPayload({
          id: guid2_dupe,
          entry: Object.assign(
            {
              version: 1,
            },
            TEST_PROFILE_2
          ),
        }),
        lastSync + 10
      )
    );

    // Sync again. We should download `guid1_dupe` and `guid2_dupe`, then
    // reconcile changes.
    await engine.sync();

    await expectLocalProfiles(profileStorage, [
      // We uploaded `guid1` during the first sync. Even though its contents
      // are the same as `guid1_dupe`, we keep both.
      Object.assign({}, TEST_PROFILE_1, { guid: guid1 }),
      Object.assign({}, TEST_PROFILE_1, { guid: guid1_dupe }),
      // However, we didn't upload `guid2` before downloading `guid2_dupe`, so
      // we *should* dedupe `guid2` to `guid2_dupe`.
      Object.assign({}, TEST_PROFILE_2, { guid: guid2_dupe }),
    ]);
  } finally {
    await cleanup(server);
  }
});

add_task(async function test_dedupe_identical_unsynced() {
  let { profileStorage, server, engine } = await setup();
  try {
    // create a record locally.
    let localGuid = await profileStorage.addresses.add(TEST_PROFILE_1);

    // and an identical record on the server but different GUID.
    let remoteGuid = Utils.makeGUID();
    notEqual(localGuid, remoteGuid);
    server.insertWBO(
      "foo",
      "addresses",
      new ServerWBO(
        remoteGuid,
        encryptPayload({
          id: remoteGuid,
          entry: Object.assign(
            {
              version: 1,
            },
            TEST_PROFILE_1
          ),
        }),
        getDateForSync()
      )
    );

    await engine.setLastSync(0);
    await engine.sync();

    // Should have 1 item locally with GUID changed to the remote one.
    // There's no tombstone as the original was unsynced.
    await expectLocalProfiles(profileStorage, [
      {
        guid: remoteGuid,
      },
    ]);
  } finally {
    await cleanup(server);
  }
});

add_task(async function test_dedupe_identical_synced() {
  let { profileStorage, server, engine } = await setup();
  try {
    // create a record locally.
    let localGuid = await profileStorage.addresses.add(TEST_PROFILE_1);

    // sync it - it will no longer be a candidate for de-duping.
    await engine.setLastSync(0);
    await engine.sync();

    // and an identical record on the server but different GUID.
    let lastSync = await engine.getLastSync();
    let remoteGuid = Utils.makeGUID();
    server.insertWBO(
      "foo",
      "addresses",
      new ServerWBO(
        remoteGuid,
        encryptPayload({
          id: remoteGuid,
          entry: Object.assign(
            {
              version: 1,
            },
            TEST_PROFILE_1
          ),
        }),
        lastSync + 10
      )
    );

    await engine.sync();

    // Should have 2 items locally, since the first was synced.
    await expectLocalProfiles(profileStorage, [
      { guid: localGuid },
      { guid: remoteGuid },
    ]);
  } finally {
    await cleanup(server);
  }
});

add_task(async function test_dedupe_multiple_candidates() {
  let { profileStorage, server, engine } = await setup();
  try {
    // It's possible to have duplicate local profiles, with the same fields but
    // different GUIDs. After a node reassignment, or after disconnecting and
    // reconnecting to Sync, we might dedupe a local record A to a remote record
    // B, if we see B before we download and apply A. Since A and B are dupes,
    // that's OK. We'll write a tombstone for A when we dedupe A to B, and
    // overwrite that tombstone when we see A.

    let localRecord = {
      "given-name": "Mark",
      "family-name": "Hammond",
      organization: "Mozilla",
      country: "AU",
      tel: "+12345678910",
    };
    let serverRecord = Object.assign(
      {
        version: 1,
      },
      localRecord
    );

    // We don't pass `sourceSync` so that the records are marked as NEW.
    let aGuid = await profileStorage.addresses.add(localRecord);
    let bGuid = await profileStorage.addresses.add(localRecord);

    // Insert B before A.
    server.insertWBO(
      "foo",
      "addresses",
      new ServerWBO(
        bGuid,
        encryptPayload({
          id: bGuid,
          entry: serverRecord,
        }),
        getDateForSync()
      )
    );
    server.insertWBO(
      "foo",
      "addresses",
      new ServerWBO(
        aGuid,
        encryptPayload({
          id: aGuid,
          entry: serverRecord,
        }),
        getDateForSync()
      )
    );

    await engine.setLastSync(0);
    await engine.sync();

    await expectLocalProfiles(profileStorage, [
      {
        guid: aGuid,
        "given-name": "Mark",
        "family-name": "Hammond",
        organization: "Mozilla",
        country: "AU",
        tel: "+12345678910",
      },
      {
        guid: bGuid,
        "given-name": "Mark",
        "family-name": "Hammond",
        organization: "Mozilla",
        country: "AU",
        tel: "+12345678910",
      },
    ]);
    // Make sure these are both syncing.
    strictEqual(
      getSyncChangeCounter(profileStorage.addresses, aGuid),
      0,
      "A should be marked as syncing"
    );
    strictEqual(
      getSyncChangeCounter(profileStorage.addresses, bGuid),
      0,
      "B should be marked as syncing"
    );
  } finally {
    await cleanup(server);
  }
});

// Unlike most sync engines, we want "both modified" to inspect the records,
// and if materially different, create a duplicate.
add_task(async function test_reconcile_both_modified_conflict() {
  let { profileStorage, server, engine } = await setup();
  try {
    // create a record locally.
    let guid = await profileStorage.addresses.add(TEST_PROFILE_1);

    // Upload the record to the server.
    await engine.setLastSync(0);
    await engine.sync();

    strictEqual(
      getSyncChangeCounter(profileStorage.addresses, guid),
      0,
      "Original record should be marked as syncing"
    );

    // Change the same field locally and on the server.
    let localCopy = Object.assign({}, TEST_PROFILE_1);
    localCopy["street-address"] = "I moved!";
    await profileStorage.addresses.update(guid, localCopy);

    let lastSync = await engine.getLastSync();
    let collection = server.user("foo").collection("addresses");
    let serverPayload = JSON.parse(
      JSON.parse(collection.payload(guid)).ciphertext
    );
    serverPayload.entry["street-address"] = "I moved, too!";
    collection.insert(guid, encryptPayload(serverPayload), lastSync + 10);

    // Sync again.
    await engine.sync();

    // Since we wait to pull changes until we're ready to upload, both records
    // should now exist on the server; we don't need a follow-up sync.
    let serverPayloads = collection.payloads();
    equal(serverPayloads.length, 2, "Both records should exist on server");

    let forkedPayload = serverPayloads.find(payload => payload.id != guid);
    ok(forkedPayload, "Forked record should exist on server");

    await expectLocalProfiles(profileStorage, [
      {
        guid,
        "given-name": "Timothy",
        "family-name": "Berners-Lee",
        "street-address": "I moved, too!",
      },
      {
        guid: forkedPayload.id,
        "given-name": "Timothy",
        "family-name": "Berners-Lee",
        "street-address": "I moved!",
      },
    ]);

    let changeCounter = getSyncChangeCounter(
      profileStorage.addresses,
      forkedPayload.id
    );
    strictEqual(changeCounter, 0, "Forked record should be marked as syncing");
  } finally {
    await cleanup(server);
  }
});

add_task(async function test_wipe() {
  let { profileStorage, server, engine } = await setup();
  try {
    let guid = await profileStorage.addresses.add(TEST_PROFILE_1);

    await expectLocalProfiles(profileStorage, [{ guid }]);

    let promiseObserved = promiseOneObserver("formautofill-storage-changed");

    await engine._wipeClient();

    let { subject, data } = await promiseObserved;
    Assert.equal(
      subject.wrappedJSObject.sourceSync,
      true,
      "it should be noted this came from sync"
    );
    Assert.equal(
      subject.wrappedJSObject.collectionName,
      "addresses",
      "got the correct collection"
    );
    Assert.equal(data, "removeAll", "a removeAll should be noted");

    await expectLocalProfiles(profileStorage, []);
  } finally {
    await cleanup(server);
  }
});

// Other clients might have data that we aren't able to process/understand yet
// We should keep that data and ensure when we sync we don't lose that data
add_task(async function test_full_roundtrip_unknown_data() {
  let { profileStorage, server, engine } = await setup();
  try {
    let profileID = Utils.makeGUID();

    info("Incoming records with unknown fields are properly stored");
    // Insert a record onto the server
    server.insertWBO(
      "foo",
      "addresses",
      new ServerWBO(
        profileID,
        encryptPayload({
          id: profileID,
          entry: Object.assign(
            {
              version: 1,
            },
            TEST_PROFILE_1
          ),
        }),
        getDateForSync()
      )
    );

    // The tracker should start with no score.
    equal(engine._tracker.score, 0);

    await engine.setLastSync(0);
    await engine.sync();

    await expectLocalProfiles(profileStorage, [
      {
        guid: profileID,
      },
    ]);

    strictEqual(getSyncChangeCounter(profileStorage.addresses, profileID), 0);

    // The sync applied new records - ensure our tracker knew it came from
    // sync and didn't bump the score.
    equal(engine._tracker.score, 0);

    // Validate incoming records with unknown fields are correctly stored
    let localRecord = await profileStorage.addresses.get(profileID);
    equal(localRecord["unknown-1"], TEST_PROFILE_1["unknown-1"]);

    let onChanged = TestUtils.topicObserved(
      "formautofill-storage-changed",
      (subject, data) => data == "update"
    );

    // Validate we can update the records locally and not drop any unknown fields
    info("Unknown fields are sent back up to the server");

    // Modify the local copy
    let localCopy = Object.assign({}, TEST_PROFILE_1);
    localCopy["street-address"] = "I moved!";
    await profileStorage.addresses.update(profileID, localCopy);
    await onChanged;
    await profileStorage._saveImmediately();

    let updatedCopy = await profileStorage.addresses.get(profileID);
    equal(updatedCopy["street-address"], "I moved!");

    // Sync our changes to the server
    await engine.setLastSync(0);
    await engine.sync();

    let collection = server.user("foo").collection("addresses");

    Assert.ok(collection.wbo(profileID));
    let serverPayload = JSON.parse(
      JSON.parse(collection.payload(profileID)).ciphertext
    );

    // The server has the updated field as well as any unknown fields
    equal(
      serverPayload.entry["unknown-1"],
      "some unknown data from another client"
    );
    equal(serverPayload.entry["street-address"], "I moved!");
  } finally {
    await cleanup(server);
  }
});