summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_trr_enterprise_policy.js
blob: e96753d554495a748e5cb9e451e1f8cebfe522a7 (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
/* 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/. */

"use strict";

trr_test_setup();
registerCleanupFunction(async () => {
  trr_clear_prefs();
});

const { updateAppInfo } = ChromeUtils.importESModule(
  "resource://testing-common/AppInfo.sys.mjs"
);

updateAppInfo({
  name: "XPCShell",
  ID: "xpcshell@tests.mozilla.org",
  version: "48",
  platformVersion: "48",
});

const { EnterprisePolicyTesting } = ChromeUtils.importESModule(
  "resource://testing-common/EnterprisePolicyTesting.sys.mjs"
);

// This initializes the policy engine for xpcshell tests
let policies = Cc["@mozilla.org/enterprisepolicies;1"].getService(
  Ci.nsIObserver
);
policies.observe(null, "policies-startup", null);

add_task(async function test_enterprise_policy_unlocked() {
  await EnterprisePolicyTesting.setupPolicyEngineWithJson({
    policies: {
      DNSOverHTTPS: {
        Enabled: false,
        ProviderURL: "https://example.org/provider",
        ExcludedDomains: ["example.com", "example.org"],
      },
    },
  });

  equal(Services.prefs.getIntPref("network.trr.mode"), 5);
  equal(Services.prefs.prefIsLocked("network.trr.mode"), false);
  equal(
    Services.prefs.getStringPref("network.trr.uri"),
    "https://example.org/provider"
  );
  equal(Services.prefs.prefIsLocked("network.trr.uri"), false);
  equal(
    Services.prefs.getStringPref("network.trr.excluded-domains"),
    "example.com,example.org"
  );
  equal(Services.prefs.prefIsLocked("network.trr.excluded-domains"), false);
  equal(Services.dns.currentTrrMode, 5);
  equal(Services.dns.currentTrrURI, "https://example.org/provider");
  Services.dns.setDetectedTrrURI("https://autodetect.example.com/provider");
  equal(Services.dns.currentTrrMode, 5);
  equal(Services.dns.currentTrrURI, "https://example.org/provider");
});

add_task(async function test_enterprise_policy_locked() {
  // Read dns.currentTrrMode to make DNS service initialized earlier.
  info("Services.dns.currentTrrMode:" + Services.dns.currentTrrMode);
  await EnterprisePolicyTesting.setupPolicyEngineWithJson({
    policies: {
      DNSOverHTTPS: {
        Enabled: true,
        ProviderURL: "https://example.com/provider",
        ExcludedDomains: ["example.com", "example.org"],
        Locked: true,
      },
    },
  });

  equal(Services.prefs.getIntPref("network.trr.mode"), 2);
  equal(Services.prefs.prefIsLocked("network.trr.mode"), true);
  equal(
    Services.prefs.getStringPref("network.trr.uri"),
    "https://example.com/provider"
  );
  equal(Services.prefs.prefIsLocked("network.trr.uri"), true);
  equal(
    Services.prefs.getStringPref("network.trr.excluded-domains"),
    "example.com,example.org"
  );
  equal(Services.prefs.prefIsLocked("network.trr.excluded-domains"), true);
  equal(Services.dns.currentTrrMode, 2);
  equal(Services.dns.currentTrrURI, "https://example.com/provider");
  Services.dns.setDetectedTrrURI("https://autodetect.example.com/provider");
  equal(Services.dns.currentTrrURI, "https://example.com/provider");
});