tor-browser

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

test_cookie_header_stripped.js (2889B)


      1 "use strict";
      2 
      3 const { HttpServer } = ChromeUtils.importESModule(
      4  "resource://testing-common/httpd.sys.mjs"
      5 );
      6 
      7 const TEST_DOMAIN = "www.example.com";
      8 ChromeUtils.defineLazyGetter(this, "URL", function () {
      9  return (
     10    "http://" + TEST_DOMAIN + ":" + httpserv.identity.primaryPort + "/path"
     11  );
     12 });
     13 
     14 const responseBody1 = "response";
     15 function requestHandler(metadata, response) {
     16  response.setHeader("Content-Type", "text/plain");
     17  response.setHeader("Set-Cookie", "tom=cool; Max-Age=10", true);
     18  response.bodyOutputStream.write(responseBody1, responseBody1.length);
     19 }
     20 
     21 let httpserv = null;
     22 
     23 function run_test() {
     24  httpserv = new HttpServer();
     25  httpserv.registerPathHandler("/path", requestHandler);
     26  httpserv.start(-1);
     27  httpserv.identity.add("http", TEST_DOMAIN, httpserv.identity.primaryPort);
     28 
     29  registerCleanupFunction(() => {
     30    Services.cookies.removeCookiesWithOriginAttributes("{}", TEST_DOMAIN);
     31    Services.prefs.clearUserPref("network.dns.localDomains");
     32    Services.prefs.clearUserPref("network.cookie.cookieBehavior");
     33    Services.prefs.clearUserPref(
     34      "network.cookieJarSettings.unblocked_for_testing"
     35    );
     36 
     37    httpserv.stop();
     38    httpserv = null;
     39  });
     40 
     41  Services.prefs.setBoolPref(
     42    "network.cookieJarSettings.unblocked_for_testing",
     43    true
     44  );
     45  Services.prefs.setCharPref("network.dns.localDomains", TEST_DOMAIN);
     46  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
     47  Services.cookies.removeCookiesWithOriginAttributes("{}", TEST_DOMAIN);
     48 
     49  // Sends back the URL to the child script
     50  do_await_remote_message("start-test").then(() => {
     51    do_send_remote_message("start-test-done", URL);
     52  });
     53 
     54  // Sends back the cookie count for the domain
     55  // Should only be one - from Set-Cookie
     56  do_await_remote_message("check-cookie-count").then(() => {
     57    do_send_remote_message(
     58      "check-cookie-count-done",
     59      Services.cookies.countCookiesFromHost(TEST_DOMAIN)
     60    );
     61  });
     62 
     63  // Sends back the cookie count for the domain
     64  // There should be 2 cookies. One from the Set-Cookie header, the other set
     65  // manually.
     66  do_await_remote_message("second-check-cookie-count").then(() => {
     67    do_send_remote_message(
     68      "second-check-cookie-count-done",
     69      Services.cookies.countCookiesFromHost(TEST_DOMAIN)
     70    );
     71  });
     72 
     73  // Sets a cookie for the test domain
     74  do_await_remote_message("set-cookie").then(() => {
     75    const expiry = Date.now() + 24 * 60 * 60 * 1000;
     76    const cv = Services.cookies.add(
     77      TEST_DOMAIN,
     78      "/",
     79      "cookieName",
     80      "cookieValue",
     81      false,
     82      false,
     83      false,
     84      expiry,
     85      {},
     86      Ci.nsICookie.SAMESITE_UNSET,
     87      Ci.nsICookie.SCHEME_HTTPS
     88    );
     89    Assert.equal(cv.result, Ci.nsICookieValidation.eOK, "Valid cookie");
     90    do_send_remote_message("set-cookie-done");
     91  });
     92 
     93  // Run the actual test logic
     94  run_test_in_child("child_cookie_header.js");
     95 }