tor-browser

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

test_bug1155169.js (2558B)


      1 const { NetUtil } = ChromeUtils.importESModule(
      2  "resource://gre/modules/NetUtil.sys.mjs"
      3 );
      4 
      5 const URI = Services.io.newURI("http://example.org/");
      6 
      7 const { COOKIE_CHANGED, COOKIE_ADDED } = Ci.nsICookieNotification;
      8 
      9 function run_test() {
     10  // Allow all cookies.
     11  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
     12 
     13  // Clear cookies.
     14  Services.cookies.removeAll();
     15 
     16  // Add a new cookie.
     17  setCookie("foo=bar", {
     18    type: COOKIE_ADDED,
     19    isSession: true,
     20    isSecure: false,
     21    isHttpOnly: false,
     22  });
     23 
     24  // Update cookie with isHttpOnly=true.
     25  setCookie("foo=bar; HttpOnly", {
     26    type: COOKIE_CHANGED,
     27    isSession: true,
     28    isSecure: false,
     29    isHttpOnly: true,
     30  });
     31 
     32  // Update cookie with isSecure=true.
     33  setCookie("foo=bar; Secure", {
     34    type: COOKIE_CHANGED,
     35    isSession: true,
     36    isSecure: true,
     37    isHttpOnly: false,
     38  });
     39 
     40  // Update cookie with isSession=false.
     41  let expiry = new Date();
     42  expiry.setUTCFullYear(expiry.getUTCFullYear() + 2);
     43  setCookie(`foo=bar; Expires=${expiry.toGMTString()}`, {
     44    type: COOKIE_CHANGED,
     45    isSession: false,
     46    isSecure: false,
     47    isHttpOnly: false,
     48  });
     49 
     50  // Reset cookie.
     51  setCookie("foo=bar", {
     52    type: COOKIE_CHANGED,
     53    isSession: true,
     54    isSecure: false,
     55    isHttpOnly: false,
     56  });
     57 }
     58 
     59 function setCookie(value, expected) {
     60  function setCookieInternal(valueInternal, expectedInternal = null) {
     61    function observer(subject) {
     62      if (!expectedInternal) {
     63        do_throw("no notification expected");
     64        return;
     65      }
     66 
     67      let notification = subject.QueryInterface(Ci.nsICookieNotification);
     68 
     69      // Check we saw the right notification.
     70      Assert.equal(notification.action, expectedInternal.type);
     71 
     72      // Check cookie details.
     73      let cookie = notification.cookie.QueryInterface(Ci.nsICookie);
     74      Assert.equal(cookie.isSession, expectedInternal.isSession);
     75      Assert.equal(cookie.isSecure, expectedInternal.isSecure);
     76      Assert.equal(cookie.isHttpOnly, expectedInternal.isHttpOnly);
     77    }
     78 
     79    Services.obs.addObserver(observer, "cookie-changed");
     80 
     81    let channel = NetUtil.newChannel({
     82      uri: URI,
     83      loadUsingSystemPrincipal: true,
     84      contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
     85    });
     86 
     87    Services.cookies.setCookieStringFromHttp(URI, valueInternal, channel);
     88    Services.obs.removeObserver(observer, "cookie-changed");
     89  }
     90 
     91  // Check that updating/inserting the cookie works.
     92  setCookieInternal(value, expected);
     93 
     94  // Check that we ignore identical cookies.
     95  setCookieInternal(value);
     96 }