netwerk/test/unit/test_bug248970_cookie.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_bug248970_cookie.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,134 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +Cu.import("resource://testing-common/httpd.js");
     1.9 +Cu.import("resource://gre/modules/Services.jsm");
    1.10 +
    1.11 +var httpserver;
    1.12 +
    1.13 +function inChildProcess() {
    1.14 +  return Cc["@mozilla.org/xre/app-info;1"]
    1.15 +           .getService(Ci.nsIXULRuntime)
    1.16 +           .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;  
    1.17 +}
    1.18 +function makeChan(path) {
    1.19 +  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    1.20 +  var chan = ios.newChannel("http://localhost:" + httpserver.identity.primaryPort + "/" + path, null, null)
    1.21 +                .QueryInterface(Ci.nsIHttpChannel);
    1.22 +  return chan;
    1.23 +}
    1.24 +
    1.25 +function setup_chan(path, isPrivate, callback) {
    1.26 +  var chan = makeChan(path);
    1.27 +  chan.QueryInterface(Ci.nsIPrivateBrowsingChannel).setPrivate(isPrivate);
    1.28 +  chan.asyncOpen(new ChannelListener(callback), null);  
    1.29 + }
    1.30 +
    1.31 +function set_cookie(value, callback) {
    1.32 +  return setup_chan('set?cookie=' + value, false, callback);
    1.33 +}
    1.34 +
    1.35 +function set_private_cookie(value, callback) {
    1.36 +  return setup_chan('set?cookie=' + value, true, callback);
    1.37 +}
    1.38 +
    1.39 +function check_cookie_presence(value, isPrivate, expected, callback) {
    1.40 +  var chan = setup_chan('present?cookie=' + value.replace('=','|'), isPrivate, function(req) {
    1.41 +    req.QueryInterface(Ci.nsIHttpChannel);
    1.42 +    do_check_eq(req.responseStatus, expected ? 200 : 404);
    1.43 +    callback(req);
    1.44 +  });
    1.45 +}
    1.46 +
    1.47 +function presentHandler(metadata, response) {
    1.48 +  var present = false;
    1.49 +  var match = /cookie=([^&]*)/.exec(metadata.queryString);
    1.50 +  if (match) {
    1.51 +    try {
    1.52 +      present = metadata.getHeader("Cookie").indexOf(match[1].replace("|","=")) != -1;
    1.53 +    } catch (x) {
    1.54 +    }
    1.55 +  }
    1.56 +  response.setStatusLine("1.0", present ? 200 : 404, "");
    1.57 +}
    1.58 +
    1.59 +function setHandler(metadata, response) {
    1.60 +  response.setStatusLine("1.0", 200, "Cookie set");
    1.61 +  var match = /cookie=([^&]*)/.exec(metadata.queryString);
    1.62 +  if (match) {
    1.63 +    response.setHeader("Set-Cookie", match[1]);
    1.64 +  }
    1.65 +}
    1.66 +
    1.67 +function run_test() {
    1.68 +  // Allow all cookies if the pref service is available in this process.
    1.69 +  if (!inChildProcess())
    1.70 +    Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
    1.71 +
    1.72 +  httpserver = new HttpServer();
    1.73 +  httpserver.registerPathHandler("/set", setHandler);
    1.74 +  httpserver.registerPathHandler("/present", presentHandler);
    1.75 +  httpserver.start(-1);
    1.76 +  
    1.77 +  do_test_pending();
    1.78 +  
    1.79 +  function check_cookie(req) {
    1.80 +    req.QueryInterface(Ci.nsIHttpChannel);
    1.81 +    do_check_eq(req.responseStatus, 200);
    1.82 +    try {
    1.83 +      do_check_true(req.getResponseHeader("Set-Cookie") != "", "expected a Set-Cookie header");
    1.84 +    } catch (x) {
    1.85 +      do_throw("missing Set-Cookie header");
    1.86 +    }
    1.87 +
    1.88 +    runNextTest();
    1.89 +  }
    1.90 +
    1.91 +  let tests = [];
    1.92 +  
    1.93 +  function runNextTest() {
    1.94 +    do_execute_soon(tests.shift());
    1.95 +  }
    1.96 +  
    1.97 +  tests.push(function() {
    1.98 +    set_cookie("C1=V1", check_cookie);
    1.99 +  });
   1.100 +  tests.push(function() {
   1.101 +    set_private_cookie("C2=V2", check_cookie);
   1.102 +  });
   1.103 +  tests.push(function() {
   1.104 +    // Check that the first cookie is present in a non-private request
   1.105 +    check_cookie_presence("C1=V1", false, true, runNextTest);
   1.106 +  });
   1.107 +  tests.push(function() {
   1.108 +    // Check that the second cookie is present in a private request
   1.109 +    check_cookie_presence("C2=V2", true, true, runNextTest);
   1.110 +  });
   1.111 +  tests.push(function() {
   1.112 +    // Check that the first cookie is not present in a private request
   1.113 +    check_cookie_presence("C1=V1", true, false, runNextTest);
   1.114 +  });
   1.115 +  tests.push(function() {
   1.116 +    // Check that the second cookie is not present in a non-private request
   1.117 +    check_cookie_presence("C2=V2", false, false, runNextTest);
   1.118 +  });
   1.119 +
   1.120 +  // The following test only works in a non-e10s situation at the moment,
   1.121 +  // since the notification needs to run in the parent process but there is
   1.122 +  // no existing mechanism to make that happen.  
   1.123 +  if (!inChildProcess()) {
   1.124 +    tests.push(function() {
   1.125 +      // Simulate all private browsing instances being closed
   1.126 +      var obsvc = Cc["@mozilla.org/observer-service;1"].
   1.127 +        getService(Ci.nsIObserverService);
   1.128 +      obsvc.notifyObservers(null, "last-pb-context-exited", null);
   1.129 +      // Check that all private cookies are now unavailable in new private requests
   1.130 +      check_cookie_presence("C2=V2", true, false, runNextTest);
   1.131 +    });
   1.132 +  }
   1.133 +  
   1.134 +  tests.push(function() { httpserver.stop(do_test_finished); });
   1.135 +  
   1.136 +  runNextTest();
   1.137 +}

mercurial