summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_collections_recovery.js
blob: fd923bc272927435c11268bf8b762e39e0f34b0f (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Verify that we wipe the server if we have to regenerate keys.
const { Service } = ChromeUtils.importESModule(
  "resource://services-sync/service.sys.mjs"
);

add_task(async function test_missing_crypto_collection() {
  enableValidationPrefs();

  let johnHelper = track_collections_helper();
  let johnU = johnHelper.with_updated_collection;
  let johnColls = johnHelper.collections;

  let empty = false;
  function maybe_empty(handler) {
    return function (request, response) {
      if (empty) {
        let body = "{}";
        response.setStatusLine(request.httpVersion, 200, "OK");
        response.bodyOutputStream.write(body, body.length);
      } else {
        handler(request, response);
      }
    };
  }

  let handlers = {
    "/1.1/johndoe/info/collections": maybe_empty(johnHelper.handler),
    "/1.1/johndoe/storage/crypto/keys": johnU(
      "crypto",
      new ServerWBO("keys").handler()
    ),
    "/1.1/johndoe/storage/meta/global": johnU(
      "meta",
      new ServerWBO("global").handler()
    ),
  };
  let collections = [
    "clients",
    "bookmarks",
    "forms",
    "history",
    "passwords",
    "prefs",
    "tabs",
  ];
  // Disable addon sync because AddonManager won't be initialized here.
  await Service.engineManager.unregister("addons");
  await Service.engineManager.unregister("extension-storage");

  for (let coll of collections) {
    handlers["/1.1/johndoe/storage/" + coll] = johnU(
      coll,
      new ServerCollection({}, true).handler()
    );
  }
  let server = httpd_setup(handlers);
  await configureIdentity({ username: "johndoe" }, server);

  try {
    let fresh = 0;
    let orig = Service._freshStart;
    Service._freshStart = async function () {
      _("Called _freshStart.");
      await orig.call(Service);
      fresh++;
    };

    _("Startup, no meta/global: freshStart called once.");
    await sync_and_validate_telem();
    Assert.equal(fresh, 1);
    fresh = 0;

    _("Regular sync: no need to freshStart.");
    await Service.sync();
    Assert.equal(fresh, 0);

    _("Simulate a bad info/collections.");
    delete johnColls.crypto;
    await sync_and_validate_telem();
    Assert.equal(fresh, 1);
    fresh = 0;

    _("Regular sync: no need to freshStart.");
    await sync_and_validate_telem();
    Assert.equal(fresh, 0);
  } finally {
    for (const pref of Svc.PrefBranch.getChildList("")) {
      Svc.PrefBranch.clearUserPref(pref);
    }
    await promiseStopServer(server);
  }
});