tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_pac_reload_after_network_change.js (2337B)


      1 "use strict";
      2 
      3 const { HttpServer } = ChromeUtils.importESModule(
      4  "resource://testing-common/httpd.sys.mjs"
      5 );
      6 XPCOMUtils.defineLazyServiceGetter(
      7  this,
      8  "gProxyService",
      9  "@mozilla.org/network/protocol-proxy-service;1",
     10  Ci.nsIProtocolProxyService
     11 );
     12 var { setTimeout } = ChromeUtils.importESModule(
     13  "resource://gre/modules/Timer.sys.mjs"
     14 );
     15 
     16 let pacServer;
     17 const proxyPort = 4433;
     18 
     19 add_setup(async function () {
     20  pacServer = new HttpServer();
     21  pacServer.registerPathHandler(
     22    "/proxy.pac",
     23    function handler(metadata, response) {
     24      let content = `function FindProxyForURL(url, host) { return "HTTPS localhost:${proxyPort}"; }`;
     25      response.setHeader("Content-Length", `${content.length}`);
     26      response.bodyOutputStream.write(content, content.length);
     27    }
     28  );
     29  pacServer.start(-1);
     30 });
     31 
     32 registerCleanupFunction(async () => {
     33  Services.prefs.clearUserPref("network.proxy.type");
     34  Services.prefs.clearUserPref("network.proxy.autoconfig_url");
     35  Services.prefs.clearUserPref("network.proxy.reload_pac_delay");
     36 });
     37 
     38 async function getProxyInfo() {
     39  return new Promise(resolve => {
     40    let uri = Services.io.newURI("http://www.mozilla.org/");
     41    gProxyService.asyncResolve(uri, 0, {
     42      onProxyAvailable(_req, _uri, pi, _status) {
     43        resolve(pi);
     44      },
     45    });
     46  });
     47 }
     48 
     49 // Test if we can successfully get PAC when the PAC server is available.
     50 add_task(async function testPAC() {
     51  // Configure PAC
     52  Services.prefs.setIntPref("network.proxy.type", 2);
     53  Services.prefs.setCharPref(
     54    "network.proxy.autoconfig_url",
     55    `http://localhost:${pacServer.identity.primaryPort}/proxy.pac`
     56  );
     57 
     58  let pi = await getProxyInfo();
     59  Assert.equal(pi.port, proxyPort, "Expected proxy port to be the same");
     60  Assert.equal(pi.type, "https", "Expected proxy type to be https");
     61 });
     62 
     63 // When PAC server is down, we should not use proxy at all.
     64 add_task(async function testWhenPACServerDown() {
     65  Services.prefs.setIntPref("network.proxy.reload_pac_delay", 0);
     66  await new Promise(resolve => pacServer.stop(resolve));
     67 
     68  Services.obs.notifyObservers(null, "network:link-status-changed", "changed");
     69 
     70  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
     71  await new Promise(resolve => setTimeout(resolve, 3000));
     72 
     73  let pi = await getProxyInfo();
     74  Assert.equal(pi, null, "should have no proxy");
     75 });