tor-browser

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

test_redirect_failure.js (1751B)


      1 "use strict";
      2 
      3 const { HttpServer } = ChromeUtils.importESModule(
      4  "resource://testing-common/httpd.sys.mjs"
      5 );
      6 
      7 /*
      8 * The test is checking async redirect code path that is loading a
      9 * redirect.  But creation of the target channel fails before we even try
     10 * to do async open on it. We force the creation error by forbidding
     11 * the port number the URI contains.
     12 */
     13 
     14 function inChildProcess() {
     15  return Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
     16 }
     17 
     18 ChromeUtils.defineLazyGetter(this, "URL", function () {
     19  return "http://localhost:" + httpServer.identity.primaryPort;
     20 });
     21 
     22 var httpServer = null;
     23 // Need to randomize, because apparently no one clears our cache
     24 var randomPath = "/redirect/" + Math.random();
     25 
     26 ChromeUtils.defineLazyGetter(this, "randomURI", function () {
     27  return URL + randomPath;
     28 });
     29 
     30 function make_channel(url) {
     31  return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
     32 }
     33 
     34 function redirectHandler(metadata, response) {
     35  response.setStatusLine(metadata.httpVersion, 301, "Moved");
     36  response.setHeader("Location", "http://non-existent.tld:65400", false);
     37  response.setHeader("Cache-Control", "no-cache", false);
     38 }
     39 
     40 function finish_test(request, buffer) {
     41  Assert.equal(request.status, Cr.NS_ERROR_PORT_ACCESS_NOT_ALLOWED);
     42 
     43  Assert.equal(buffer, "");
     44  httpServer.stop(do_test_finished);
     45 }
     46 
     47 function run_test() {
     48  httpServer = new HttpServer();
     49  httpServer.registerPathHandler(randomPath, redirectHandler);
     50  httpServer.start(-1);
     51 
     52  if (!inChildProcess()) {
     53    Services.prefs.setCharPref("network.security.ports.banned", "65400");
     54  }
     55 
     56  var chan = make_channel(randomURI);
     57  chan.asyncOpen(new ChannelListener(finish_test, null, CL_EXPECT_FAILURE));
     58  do_test_pending();
     59 }