tor-browser

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

test_bug248970_cookie.js (4335B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const { HttpServer } = ChromeUtils.importESModule(
      8  "resource://testing-common/httpd.sys.mjs"
      9 );
     10 
     11 var httpserver;
     12 
     13 function inChildProcess() {
     14  return Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
     15 }
     16 function makeChan(path) {
     17  return NetUtil.newChannel({
     18    uri: "http://localhost:" + httpserver.identity.primaryPort + "/" + path,
     19    loadUsingSystemPrincipal: true,
     20  }).QueryInterface(Ci.nsIHttpChannel);
     21 }
     22 
     23 function setup_chan(path, isPrivate, callback) {
     24  var chan = makeChan(path);
     25  chan.QueryInterface(Ci.nsIPrivateBrowsingChannel).setPrivate(isPrivate);
     26  chan.asyncOpen(new ChannelListener(callback));
     27 }
     28 
     29 function set_cookie(value, callback) {
     30  return setup_chan("set?cookie=" + value, false, callback);
     31 }
     32 
     33 function set_private_cookie(value, callback) {
     34  return setup_chan("set?cookie=" + value, true, callback);
     35 }
     36 
     37 function check_cookie_presence(value, isPrivate, expected, callback) {
     38  setup_chan(
     39    "present?cookie=" + value.replace("=", "|"),
     40    isPrivate,
     41    function (req) {
     42      req.QueryInterface(Ci.nsIHttpChannel);
     43      Assert.equal(req.responseStatus, expected ? 200 : 404);
     44      callback(req);
     45    }
     46  );
     47 }
     48 
     49 function presentHandler(metadata, response) {
     50  var present = false;
     51  var match = /cookie=([^&]*)/.exec(metadata.queryString);
     52  if (match) {
     53    try {
     54      present = metadata
     55        .getHeader("Cookie")
     56        .includes(match[1].replace("|", "="));
     57    } catch (x) {}
     58  }
     59  response.setStatusLine("1.0", present ? 200 : 404, "");
     60 }
     61 
     62 function setHandler(metadata, response) {
     63  response.setStatusLine("1.0", 200, "Cookie set");
     64  var match = /cookie=([^&]*)/.exec(metadata.queryString);
     65  if (match) {
     66    response.setHeader("Set-Cookie", match[1]);
     67  }
     68 }
     69 
     70 function run_test() {
     71  // Allow all cookies if the pref service is available in this process.
     72  if (!inChildProcess()) {
     73    Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
     74    Services.prefs.setBoolPref(
     75      "network.cookieJarSettings.unblocked_for_testing",
     76      true
     77    );
     78  }
     79 
     80  httpserver = new HttpServer();
     81  httpserver.registerPathHandler("/set", setHandler);
     82  httpserver.registerPathHandler("/present", presentHandler);
     83  httpserver.start(-1);
     84 
     85  do_test_pending();
     86 
     87  function check_cookie(req) {
     88    req.QueryInterface(Ci.nsIHttpChannel);
     89    Assert.equal(req.responseStatus, 200);
     90    try {
     91      Assert.notEqual(
     92        req.getResponseHeader("Set-Cookie"),
     93        "",
     94        "expected a Set-Cookie header"
     95      );
     96    } catch (x) {
     97      do_throw("missing Set-Cookie header");
     98    }
     99 
    100    runNextTest();
    101  }
    102 
    103  let tests = [];
    104 
    105  function runNextTest() {
    106    executeSoon(tests.shift());
    107  }
    108 
    109  tests.push(function () {
    110    set_cookie("C1=V1", check_cookie);
    111  });
    112  tests.push(function () {
    113    set_private_cookie("C2=V2", check_cookie);
    114  });
    115  tests.push(function () {
    116    // Check that the first cookie is present in a non-private request
    117    check_cookie_presence("C1=V1", false, true, runNextTest);
    118  });
    119  tests.push(function () {
    120    // Check that the second cookie is present in a private request
    121    check_cookie_presence("C2=V2", true, true, runNextTest);
    122  });
    123  tests.push(function () {
    124    // Check that the first cookie is not present in a private request
    125    check_cookie_presence("C1=V1", true, false, runNextTest);
    126  });
    127  tests.push(function () {
    128    // Check that the second cookie is not present in a non-private request
    129    check_cookie_presence("C2=V2", false, false, runNextTest);
    130  });
    131 
    132  // The following test only works in a non-e10s situation at the moment,
    133  // since the notification needs to run in the parent process but there is
    134  // no existing mechanism to make that happen.
    135  if (!inChildProcess()) {
    136    tests.push(function () {
    137      // Simulate all private browsing instances being closed
    138      Services.obs.notifyObservers(null, "last-pb-context-exited");
    139      // Check that all private cookies are now unavailable in new private requests
    140      check_cookie_presence("C2=V2", true, false, runNextTest);
    141    });
    142  }
    143 
    144  tests.push(function () {
    145    httpserver.stop(do_test_finished);
    146  });
    147 
    148  runNextTest();
    149 }