tor-browser

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

file_testloadflags_chromescript.js (3075B)


      1 /* eslint-env mozilla/chrome-script */
      2 /* eslint-disable mozilla/use-services */
      3 
      4 "use strict";
      5 
      6 var gObs;
      7 
      8 function info(s) {
      9  sendAsyncMessage("info", { str: String(s) });
     10 }
     11 
     12 function ok(c, m) {
     13  sendAsyncMessage("ok", { c, m });
     14 }
     15 
     16 function is(a, b, m) {
     17  ok(Object.is(a, b), m + " (" + a + " === " + b + ")");
     18 }
     19 
     20 // Count headers.
     21 function obs() {
     22  info("adding observer");
     23 
     24  this.os = Cc["@mozilla.org/observer-service;1"].getService(
     25    Ci.nsIObserverService
     26  );
     27  this.os.addObserver(this, "http-on-modify-request");
     28 }
     29 
     30 obs.prototype = {
     31  observe(theSubject, theTopic, theData) {
     32    info("theSubject " + theSubject);
     33    info("theTopic " + theTopic);
     34    info("theData " + theData);
     35 
     36    var channel = theSubject.QueryInterface(Ci.nsIHttpChannel);
     37    info("channel " + channel);
     38    try {
     39      info("channel.URI " + channel.URI);
     40      info("channel.URI.spec " + channel.URI.spec);
     41      channel.visitRequestHeaders({
     42        visitHeader(aHeader, aValue) {
     43          info(aHeader + ": " + aValue);
     44        },
     45      });
     46    } catch (err) {
     47      ok(false, "catch error " + err);
     48    }
     49 
     50    // Ignore notifications we don't care about (like favicons)
     51    if (
     52      !channel.URI.spec.includes(
     53        "http://example.org/tests/netwerk/test/mochitests/"
     54      )
     55    ) {
     56      info("ignoring this one");
     57      return;
     58    }
     59 
     60    sendAsyncMessage("observer:gotCookie", {
     61      cookie: channel.getRequestHeader("Cookie"),
     62      uri: channel.URI.spec,
     63    });
     64  },
     65 
     66  remove() {
     67    info("removing observer");
     68 
     69    this.os.removeObserver(this, "http-on-modify-request");
     70    this.os = null;
     71  },
     72 };
     73 
     74 function getCookieCount(cs) {
     75  let count = 0;
     76  for (let cookie of cs.cookies) {
     77    info("cookie: " + cookie);
     78    info(
     79      "cookie host " +
     80        cookie.host +
     81        " path " +
     82        cookie.path +
     83        " name " +
     84        cookie.name +
     85        " value " +
     86        cookie.value +
     87        " isSecure " +
     88        cookie.isSecure +
     89        " expires " +
     90        cookie.expires
     91    );
     92    ++count;
     93  }
     94 
     95  return count;
     96 }
     97 
     98 addMessageListener("init", ({ domain }) => {
     99  let cs = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
    100 
    101  info("we are going to remove these cookies");
    102 
    103  let count = getCookieCount(cs);
    104  info(count + " cookies");
    105 
    106  cs.removeAll();
    107  cs.add(
    108    domain,
    109    "/",
    110    "oh",
    111    "hai",
    112    false,
    113    false,
    114    true,
    115    Math.pow(2, 62),
    116    {},
    117    Ci.nsICookie.SAMESITE_UNSET,
    118    Ci.nsICookie.SCHEME_HTTPS
    119  );
    120  is(
    121    cs.countCookiesFromHost(domain),
    122    1,
    123    "number of cookies for domain " + domain
    124  );
    125 
    126  gObs = new obs();
    127  sendAsyncMessage("init:return");
    128 });
    129 
    130 addMessageListener("getCookieCount", () => {
    131  let cs = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
    132  let count = getCookieCount(cs);
    133 
    134  cs.removeAll();
    135  sendAsyncMessage("getCookieCount:return", { count });
    136 });
    137 
    138 addMessageListener("shutdown", () => {
    139  gObs.remove();
    140 
    141  let cs = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
    142  cs.removeAll();
    143  sendAsyncMessage("shutdown:return");
    144 });