tor-browser

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

test_redirect-caching_failure.js (2461B)


      1 "use strict";
      2 
      3 const { HttpServer } = ChromeUtils.importESModule(
      4  "resource://testing-common/httpd.sys.mjs"
      5 );
      6 /*
      7 * The test is checking async redirect code path that is loading a cached
      8 * redirect.  But creation of the target channel fails before we even try
      9 * to do async open on it. We force the creation error by forbidding
     10 * the port number the URI contains. It must be done only after we have
     11 * attempted to do the redirect (open the target URL) otherwise it's not
     12 * cached.
     13 */
     14 
     15 function inChildProcess() {
     16  return Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
     17 }
     18 
     19 ChromeUtils.defineLazyGetter(this, "URL", function () {
     20  return "http://localhost:" + httpServer.identity.primaryPort;
     21 });
     22 
     23 var httpServer = null;
     24 // Need to randomize, because apparently no one clears our cache
     25 var randomPath = "/redirect/" + Math.random();
     26 
     27 ChromeUtils.defineLazyGetter(this, "randomURI", function () {
     28  return URL + randomPath;
     29 });
     30 
     31 function make_channel(url) {
     32  return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
     33 }
     34 
     35 var serverRequestCount = 0;
     36 
     37 function redirectHandler(metadata, response) {
     38  ++serverRequestCount;
     39  response.setStatusLine(metadata.httpVersion, 301, "Moved");
     40  response.setHeader("Location", "http://non-existent.tld:65400", false);
     41  response.setHeader("Cache-control", "max-age=1000", false);
     42 }
     43 
     44 function firstTimeThrough(request) {
     45  Assert.equal(request.status, Cr.NS_ERROR_UNKNOWN_HOST);
     46  Assert.equal(serverRequestCount, 1);
     47 
     48  const nextHop = () => {
     49    var chan = make_channel(randomURI);
     50    chan.loadFlags |= Ci.nsIRequest.LOAD_FROM_CACHE;
     51    chan.asyncOpen(new ChannelListener(finish_test, null, CL_EXPECT_FAILURE));
     52  };
     53 
     54  if (inChildProcess()) {
     55    do_send_remote_message("disable-ports");
     56    do_await_remote_message("disable-ports-done").then(nextHop);
     57  } else {
     58    Services.prefs.setCharPref("network.security.ports.banned", "65400");
     59    nextHop();
     60  }
     61 }
     62 
     63 function finish_test(request, buffer) {
     64  Assert.equal(request.status, Cr.NS_ERROR_PORT_ACCESS_NOT_ALLOWED);
     65  Assert.equal(serverRequestCount, 1);
     66  Assert.equal(buffer, "");
     67 
     68  httpServer.stop(do_test_finished);
     69 }
     70 
     71 function run_test() {
     72  httpServer = new HttpServer();
     73  httpServer.registerPathHandler(randomPath, redirectHandler);
     74  httpServer.start(-1);
     75 
     76  var chan = make_channel(randomURI);
     77  chan.asyncOpen(
     78    new ChannelListener(firstTimeThrough, null, CL_EXPECT_FAILURE)
     79  );
     80  do_test_pending();
     81 }