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

"use strict";

const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
const { PromiseUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/PromiseUtils.sys.mjs"
);

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

let h2Port, trrServer1, trrServer2, trrList;
let DNSLookup, LookupAggregator, TRRRacer;

function readFile(file) {
  let fstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
    Ci.nsIFileInputStream
  );
  fstream.init(file, -1, 0, 0);
  let data = NetUtil.readInputStreamToString(fstream, fstream.available());
  fstream.close();
  return data;
}

function addCertFromFile(certdb, filename, trustString) {
  let certFile = do_get_file(filename, false);
  let pem = readFile(certFile)
    .replace(/-----BEGIN CERTIFICATE-----/, "")
    .replace(/-----END CERTIFICATE-----/, "")
    .replace(/[\r\n]/g, "");
  certdb.addCertFromBase64(pem, trustString);
}

function ensureNoTelemetry() {
  let events =
    Services.telemetry.snapshotEvents(
      Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
      true
    ).parent || [];
  events = events.filter(e => e[1] == "security.doh.trrPerformance");
  Assert.ok(!events.length);
}

function setup() {
  h2Port = Services.env.get("MOZHTTP2_PORT");
  Assert.notEqual(h2Port, null);
  Assert.notEqual(h2Port, "");

  // Set to allow the cert presented by our H2 server
  do_get_profile();

  Services.prefs.setBoolPref("network.http.http2.enabled", true);

  // use the h2 server as DOH provider
  trrServer1 = `https://foo.example.com:${h2Port}/doh?responseIP=1.1.1.1`;
  trrServer2 = `https://foo.example.com:${h2Port}/doh?responseIP=2.2.2.2`;
  trrList = [trrServer1, trrServer2];
  // make all native resolve calls "secretly" resolve localhost instead
  Services.prefs.setBoolPref("network.dns.native-is-localhost", true);

  // The moz-http2 cert is for foo.example.com and is signed by http2-ca.pem
  // so add that cert to the trust list as a signing cert.  // the foo.example.com domain name.
  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
    Ci.nsIX509CertDB
  );
  addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");

  Services.prefs.setIntPref("doh-rollout.trrRace.randomSubdomainCount", 2);

  Services.prefs.setCharPref(
    "doh-rollout.trrRace.popularDomains",
    "foo.example.com., bar.example.com."
  );

  Services.prefs.setCharPref(
    "doh-rollout.trrRace.canonicalDomain",
    "firefox-dns-perf-test.net."
  );

  let TRRPerformance = ChromeUtils.importESModule(
    "resource:///modules/TRRPerformance.sys.mjs"
  );

  DNSLookup = TRRPerformance.DNSLookup;
  LookupAggregator = TRRPerformance.LookupAggregator;
  TRRRacer = TRRPerformance.TRRRacer;

  let oldCanRecord = Services.telemetry.canRecordExtended;
  Services.telemetry.canRecordExtended = true;

  registerCleanupFunction(() => {
    Services.prefs.clearUserPref("network.http.http2.enabled");
    Services.prefs.clearUserPref("network.dns.native-is-localhost");

    Services.telemetry.canRecordExtended = oldCanRecord;
  });
}