summaryrefslogtreecommitdiffstats
path: root/browser/components/doh/test/browser/browser_trrSelect.js
blob: 68861be8b8f86f936c636536242a280cd666d3bc (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

"use strict";

async function waitForStartup() {
  await ensureTRRMode(2);
  await checkHeuristicsTelemetry("enable_doh", "startup");
}

async function setPrefAndWaitForConfigFlush(pref, value) {
  let configFlushed = DoHTestUtils.waitForConfigFlush();
  if (value) {
    Preferences.set(pref, value);
  } else {
    Preferences.reset(pref);
  }
  await configFlushed;
  await waitForStartup();
}

add_task(setup);

add_task(async function testTRRSelect() {
  // Clean start: doh-rollout.uri should be set after init.
  setPassingHeuristics();
  let prefPromise = TestUtils.waitForPrefChange(prefs.BREADCRUMB_PREF);
  Preferences.set(prefs.ENABLED_PREF, true);
  await prefPromise;
  is(Preferences.get(prefs.BREADCRUMB_PREF), true, "Breadcrumb saved.");
  is(
    Preferences.get(prefs.TRR_SELECT_URI_PREF),
    "https://example.com/dns-query",
    "TRR selection complete."
  );

  // Wait for heuristics to complete.
  await ensureTRRMode(2);
  await checkHeuristicsTelemetry("enable_doh", "startup");

  // Reset and restart the controller for good measure.
  Preferences.reset(prefs.TRR_SELECT_DRY_RUN_RESULT_PREF);
  Preferences.reset(prefs.TRR_SELECT_URI_PREF);
  await restartDoHController();
  await waitForStartup();

  is(
    Preferences.get(prefs.TRR_SELECT_URI_PREF),
    "https://example.com/dns-query",
    "TRR selection complete."
  );

  // Disable committing. The committed URI should be reset to the
  // default provider and the dry-run-result should persist.
  prefPromise = TestUtils.waitForPrefChange(
    prefs.TRR_SELECT_URI_PREF,
    newVal => newVal == "https://example.com/1"
  );
  await setPrefAndWaitForConfigFlush(prefs.TRR_SELECT_COMMIT_PREF, false);
  await prefPromise;
  is(
    Preferences.get(prefs.TRR_SELECT_URI_PREF),
    "https://example.com/1",
    "Default TRR selected."
  );
  try {
    await BrowserTestUtils.waitForCondition(() => {
      return !Preferences.isSet(prefs.TRR_SELECT_DRY_RUN_RESULT_PREF);
    });
    ok(false, "Dry run result was cleared, fail!");
  } catch (e) {
    ok(true, "Dry run result was not cleared.");
  }
  is(
    Preferences.get(prefs.TRR_SELECT_DRY_RUN_RESULT_PREF),
    "https://example.com/dns-query",
    "dry-run result has the correct value."
  );

  // Reset again, dry-run-result should be recorded but not
  // be committed. Committing is still disabled from above.
  Preferences.reset(prefs.TRR_SELECT_DRY_RUN_RESULT_PREF);
  Preferences.reset(prefs.TRR_SELECT_URI_PREF);
  await restartDoHController();
  await waitForStartup();

  try {
    await BrowserTestUtils.waitForCondition(() => {
      return (
        Preferences.get(prefs.TRR_SELECT_URI_PREF) ==
        "https://example.com/dns-query"
      );
    });
    ok(false, "Dry run result got committed, fail!");
  } catch (e) {
    ok(true, "Dry run result did not get committed");
  }
  is(
    Preferences.get(prefs.TRR_SELECT_URI_PREF),
    "https://example.com/1",
    "Default TRR selected."
  );
  is(
    Preferences.get(prefs.TRR_SELECT_DRY_RUN_RESULT_PREF),
    "https://example.com/dns-query",
    "TRR selection complete, dry-run result recorded."
  );

  // Reset doh-rollout.uri, and change the dry-run-result to another one on the
  // default list. After init, the existing dry-run-result should be committed.
  Preferences.reset(prefs.TRR_SELECT_URI_PREF);
  Preferences.set(
    prefs.TRR_SELECT_DRY_RUN_RESULT_PREF,
    "https://example.com/2"
  );
  prefPromise = TestUtils.waitForPrefChange(
    prefs.TRR_SELECT_URI_PREF,
    newVal => newVal == "https://example.com/2"
  );
  await setPrefAndWaitForConfigFlush(prefs.TRR_SELECT_COMMIT_PREF, true);
  await prefPromise;
  is(
    Preferences.get(prefs.TRR_SELECT_URI_PREF),
    "https://example.com/2",
    "TRR selection complete, existing dry-run-result committed."
  );

  // Reset doh-rollout.uri, and change the dry-run-result to another one NOT on
  // default list. After init, a new TRR should be selected and committed.
  prefPromise = TestUtils.waitForPrefChange(
    prefs.TRR_SELECT_URI_PREF,
    newVal => newVal == "https://example.com/dns-query"
  );
  Preferences.reset(prefs.TRR_SELECT_URI_PREF);
  Preferences.set(
    prefs.TRR_SELECT_DRY_RUN_RESULT_PREF,
    "https://example.com/4"
  );
  await restartDoHController();
  await prefPromise;
  is(
    Preferences.get(prefs.TRR_SELECT_URI_PREF),
    "https://example.com/dns-query",
    "TRR selection complete, existing dry-run-result discarded and refreshed."
  );
});