summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_trr_proxy.js
blob: c091239e8b6d4c4bb50e58c903cb54b0cd400018 (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
/* globals dnsResolve */

/* This test checks that using a PAC script still works when TRR is on.
   Steps:
     - Set the pac script
     - Do a request to make sure that the script is loaded
     - Set the TRR mode
     - Make a request that would lead to running the PAC script
   We run these steps for TRR mode 2 and 3, and with fetchOffMainThread = true/false
*/

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
const { MockRegistrar } = ChromeUtils.import(
  "resource://testing-common/MockRegistrar.jsm"
);
const dns = Cc["@mozilla.org/network/dns-service;1"].getService(
  Ci.nsIDNSService
);

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

function FindProxyForURL(url, host) {
  alert(`PAC resolving: ${host}`);
  alert(dnsResolve(host));
  return "DIRECT";
}

const CID = Components.ID("{5645d2c1-d6d8-4091-b117-fe7ee4027db7}");
XPCOMUtils.defineLazyGetter(this, "systemSettings", function() {
  return {
    QueryInterface: ChromeUtils.generateQI(["nsISystemProxySettings"]),

    mainThreadOnly: true,
    PACURI: `data:application/x-ns-proxy-autoconfig;charset=utf-8,${encodeURIComponent(
      FindProxyForURL.toString()
    )}`,
    getProxyForURI(aURI) {
      throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
    },
  };
});

const override = Cc["@mozilla.org/network/native-dns-override;1"].getService(
  Ci.nsINativeDNSResolverOverride
);

add_task(async function test_pac_dnsResolve() {
  Services.console.reset();
  // Create a console listener.
  let consolePromise = new Promise(resolve => {
    let listener = {
      observe(message) {
        // Ignore unexpected messages.
        if (!(message instanceof Ci.nsIConsoleMessage)) {
          return;
        }

        if (message.message.includes("PAC file installed from")) {
          Services.console.unregisterListener(listener);
          resolve();
        }
      },
    };

    Services.console.registerListener(listener);
  });

  MockRegistrar.register(
    "@mozilla.org/system-proxy-settings;1",
    systemSettings
  );
  Services.prefs.setIntPref(
    "network.proxy.type",
    Ci.nsIProtocolProxyService.PROXYCONFIG_SYSTEM
  );

  let httpserv = new HttpServer();
  httpserv.registerPathHandler("/", function handler(metadata, response) {
    let content = "ok";
    response.setHeader("Content-Length", `${content.length}`);
    response.bodyOutputStream.write(content, content.length);
  });
  httpserv.start(-1);

  Services.prefs.setBoolPref("network.dns.native-is-localhost", false);
  Services.prefs.setIntPref("network.trr.mode", 0); // Disable TRR until the PAC is loaded
  override.addIPOverride("example.org", "127.0.0.1");
  let chan = NetUtil.newChannel({
    uri: `http://example.org:${httpserv.identity.primaryPort}/`,
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
  await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
  await consolePromise;

  let env = Cc["@mozilla.org/process/environment;1"].getService(
    Ci.nsIEnvironment
  );
  let h2Port = env.get("MOZHTTP2_PORT");
  Assert.notEqual(h2Port, null);
  Assert.notEqual(h2Port, "");

  override.addIPOverride("foo.example.com", "127.0.0.1");
  Services.prefs.setCharPref(
    "network.trr.uri",
    `https://foo.example.com:${h2Port}/doh?responseIP=127.0.0.1`
  );

  async function test_with(DOMAIN, trrMode, fetchOffMainThread) {
    Services.prefs.setIntPref("network.trr.mode", trrMode); // TRR first
    Services.prefs.setBoolPref(
      "network.trr.fetch_off_main_thread",
      fetchOffMainThread
    );
    override.addIPOverride(DOMAIN, "127.0.0.1");

    chan = NetUtil.newChannel({
      uri: `http://${DOMAIN}:${httpserv.identity.primaryPort}/`,
      loadUsingSystemPrincipal: true,
    }).QueryInterface(Ci.nsIHttpChannel);
    await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));

    await override.clearHostOverride(DOMAIN);
  }

  await test_with("test1.com", 2, true);
  await test_with("test2.com", 3, true);
  await test_with("test3.com", 2, false);
  await test_with("test4.com", 3, false);
  await httpserv.stop();
});