summaryrefslogtreecommitdiffstats
path: root/browser/components/doh/DoHTestUtils.sys.mjs
blob: b89c1fe966ab30473d48eafdf1766845e2038ec0 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  RemoteSettings: "resource://services-settings/remote-settings.sys.mjs",
  TestUtils: "resource://testing-common/TestUtils.sys.mjs",
});

const kConfigCollectionKey = "doh-config";
const kProviderCollectionKey = "doh-providers";

const kConfigUpdateTopic = "doh-config-updated";
const kControllerReloadedTopic = "doh:controller-reloaded";

/*
 * Some helpers for loading and modifying DoH config in
 * Remote Settings. Call resetRemoteSettingsConfig to set up
 * basic default config that omits external URLs. Use
 * waitForConfigFlush to wait for DoH actors to pick up changes.
 *
 * Some tests need to load/reset config while DoH actors are
 * uninitialized. Pass waitForConfigFlushes = false in these cases.
 */
export const DoHTestUtils = {
  providers: [
    {
      uri: "https://example.com/1",
      UIName: "Example 1",
      autoDefault: false,
      canonicalName: "",
      id: "example-1",
    },
    {
      uri: "https://example.com/2",
      UIName: "Example 2",
      autoDefault: false,
      canonicalName: "",
      id: "example-2",
    },
  ],

  async loadRemoteSettingsProviders(providers, waitForConfigFlushes = true) {
    let configFlushedPromise = this.waitForConfigFlush(waitForConfigFlushes);

    let providerRS = lazy.RemoteSettings(kProviderCollectionKey);
    let db = await providerRS.db;
    await db.importChanges({}, Date.now(), providers, { clear: true });

    // Trigger a sync.
    await this.triggerSync(providerRS);

    await configFlushedPromise;
  },

  async loadRemoteSettingsConfig(config, waitForConfigFlushes = true) {
    let configFlushedPromise = this.waitForConfigFlush(waitForConfigFlushes);

    let configRS = lazy.RemoteSettings(kConfigCollectionKey);
    let db = await configRS.db;
    await db.importChanges({}, Date.now(), [config]);

    // Trigger a sync.
    await this.triggerSync(configRS);

    await configFlushedPromise;
  },

  // Loads default config for testing without clearing existing entries.
  async loadDefaultRemoteSettingsConfig(waitForConfigFlushes = true) {
    await this.loadRemoteSettingsProviders(
      this.providers,
      waitForConfigFlushes
    );

    await this.loadRemoteSettingsConfig(
      {
        providers: "example-1, example-2",
        rolloutEnabled: false,
        steeringEnabled: false,
        steeringProviders: "",
        autoDefaultEnabled: false,
        autoDefaultProviders: "",
        id: "global",
      },
      waitForConfigFlushes
    );
  },

  // Clears existing config AND loads defaults.
  async resetRemoteSettingsConfig(waitForConfigFlushes = true) {
    let providerRS = lazy.RemoteSettings(kProviderCollectionKey);
    let configRS = lazy.RemoteSettings(kConfigCollectionKey);
    for (let rs of [providerRS, configRS]) {
      let configFlushedPromise = this.waitForConfigFlush(waitForConfigFlushes);
      await rs.db.importChanges({}, Date.now(), [], { clear: true });
      // Trigger a sync to clear.
      await this.triggerSync(rs);
      await configFlushedPromise;
    }

    await this.loadDefaultRemoteSettingsConfig(waitForConfigFlushes);
  },

  triggerSync(rs) {
    return rs.emit("sync", {
      data: {
        current: [],
      },
    });
  },

  waitForConfigUpdate() {
    return lazy.TestUtils.topicObserved(kConfigUpdateTopic);
  },

  waitForControllerReload() {
    return lazy.TestUtils.topicObserved(kControllerReloadedTopic);
  },

  waitForConfigFlush(shouldWait = true) {
    if (!shouldWait) {
      return Promise.resolve();
    }

    return Promise.all([
      this.waitForConfigUpdate(),
      this.waitForControllerReload(),
    ]);
  },
};