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

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  Heuristics: "resource:///modules/DoHHeuristics.sys.mjs",
});

add_task(setup);

add_task(async function testPlatformIndications() {
  // Check if the platform heuristics actually cause a "disable_doh" event
  let { MockRegistrar } = ChromeUtils.importESModule(
    "resource://testing-common/MockRegistrar.sys.mjs"
  );

  let mockedLinkService = {
    isLinkUp: true,
    linkStatusKnown: true,
    linkType: Ci.nsINetworkLinkService.LINK_TYPE_WIFI,
    networkID: "abcd",
    dnsSuffixList: [],
    platformDNSIndications: Ci.nsINetworkLinkService.NONE_DETECTED,
    QueryInterface: ChromeUtils.generateQI(["nsINetworkLinkService"]),
  };

  let networkLinkServiceCID = MockRegistrar.register(
    "@mozilla.org/network/network-link-service;1",
    mockedLinkService
  );

  Heuristics._setMockLinkService(mockedLinkService);
  registerCleanupFunction(async () => {
    MockRegistrar.unregister(networkLinkServiceCID);
    Heuristics._setMockLinkService(undefined);
  });

  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.");
  await checkHeuristicsTelemetry("enable_doh", "startup");

  checkScalars([
    ["networking.doh_heuristics_attempts", { value: 1 }],
    ["networking.doh_heuristics_pass_count", { value: 1 }],
    ["networking.doh_heuristics_result", { value: Heuristics.Telemetry.pass }],
    // All of the heuristics must be false.
    falseExpectations([]),
  ]);

  await ensureTRRMode(2);

  mockedLinkService.platformDNSIndications =
    Ci.nsINetworkLinkService.VPN_DETECTED;
  simulateNetworkChange();
  await ensureTRRMode(0);
  await checkHeuristicsTelemetry("disable_doh", "netchange");
  checkScalars(
    [
      ["networking.doh_heuristics_attempts", { value: 2 }],
      ["networking.doh_heuristics_pass_count", { value: 1 }],
      ["networking.doh_heuristics_result", { value: Heuristics.Telemetry.vpn }],
      ["networking.doh_heuristic_ever_tripped", { value: true, key: "vpn" }],
    ].concat(falseExpectations(["vpn"]))
  );

  mockedLinkService.platformDNSIndications =
    Ci.nsINetworkLinkService.PROXY_DETECTED;
  simulateNetworkChange();
  await ensureNoTRRModeChange(0);
  await checkHeuristicsTelemetry("disable_doh", "netchange");
  checkScalars(
    [
      ["networking.doh_heuristics_attempts", { value: 3 }],
      ["networking.doh_heuristics_pass_count", { value: 1 }],
      [
        "networking.doh_heuristics_result",
        { value: Heuristics.Telemetry.proxy },
      ],
      ["networking.doh_heuristic_ever_tripped", { value: true, key: "vpn" }], // Was tripped earlier this session
      ["networking.doh_heuristic_ever_tripped", { value: true, key: "proxy" }],
    ].concat(falseExpectations(["vpn", "proxy"]))
  );

  mockedLinkService.platformDNSIndications =
    Ci.nsINetworkLinkService.NRPT_DETECTED;
  simulateNetworkChange();
  await ensureNoTRRModeChange(0);
  await checkHeuristicsTelemetry("disable_doh", "netchange");
  checkScalars(
    [
      ["networking.doh_heuristics_attempts", { value: 4 }],
      ["networking.doh_heuristics_pass_count", { value: 1 }],
      [
        "networking.doh_heuristics_result",
        { value: Heuristics.Telemetry.nrpt },
      ],
      ["networking.doh_heuristic_ever_tripped", { value: true, key: "vpn" }], // Was tripped earlier this session
      ["networking.doh_heuristic_ever_tripped", { value: true, key: "proxy" }], // Was tripped earlier this session
      ["networking.doh_heuristic_ever_tripped", { value: true, key: "nrpt" }],
    ].concat(falseExpectations(["vpn", "proxy", "nrpt"]))
  );

  mockedLinkService.platformDNSIndications =
    Ci.nsINetworkLinkService.NONE_DETECTED;
  simulateNetworkChange();
  await ensureTRRMode(2);
  await checkHeuristicsTelemetry("enable_doh", "netchange");
  checkScalars(
    [
      ["networking.doh_heuristics_attempts", { value: 5 }],
      ["networking.doh_heuristics_pass_count", { value: 2 }],
      [
        "networking.doh_heuristics_result",
        { value: Heuristics.Telemetry.pass },
      ],
      ["networking.doh_heuristic_ever_tripped", { value: true, key: "vpn" }], // Was tripped earlier this session
      ["networking.doh_heuristic_ever_tripped", { value: true, key: "proxy" }], // Was tripped earlier this session
      ["networking.doh_heuristic_ever_tripped", { value: true, key: "nrpt" }], // Was tripped earlier this session
    ].concat(falseExpectations(["vpn", "proxy", "nrpt"]))
  );
});