summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_bug248970_cookie.js
blob: 41c45e10593f42eed088e70434aa7c6176df6dbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

Cu.import("resource://testing-common/httpd.js");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");

var httpserver;

function inChildProcess() {
  return Cc["@mozilla.org/xre/app-info;1"]
           .getService(Ci.nsIXULRuntime)
           .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;  
}
function makeChan(path) {
  return NetUtil.newChannel({
    uri: "http://localhost:" + httpserver.identity.primaryPort + "/" + path,
    loadUsingSystemPrincipal: true
  }).QueryInterface(Ci.nsIHttpChannel);
}

function setup_chan(path, isPrivate, callback) {
  var chan = makeChan(path);
  chan.QueryInterface(Ci.nsIPrivateBrowsingChannel).setPrivate(isPrivate);
  chan.asyncOpen2(new ChannelListener(callback));  
 }

function set_cookie(value, callback) {
  return setup_chan('set?cookie=' + value, false, callback);
}

function set_private_cookie(value, callback) {
  return setup_chan('set?cookie=' + value, true, callback);
}

function check_cookie_presence(value, isPrivate, expected, callback) {
  var chan = setup_chan('present?cookie=' + value.replace('=','|'), isPrivate, function(req) {
    req.QueryInterface(Ci.nsIHttpChannel);
    do_check_eq(req.responseStatus, expected ? 200 : 404);
    callback(req);
  });
}

function presentHandler(metadata, response) {
  var present = false;
  var match = /cookie=([^&]*)/.exec(metadata.queryString);
  if (match) {
    try {
      present = metadata.getHeader("Cookie").indexOf(match[1].replace("|","=")) != -1;
    } catch (x) {
    }
  }
  response.setStatusLine("1.0", present ? 200 : 404, "");
}

function setHandler(metadata, response) {
  response.setStatusLine("1.0", 200, "Cookie set");
  var match = /cookie=([^&]*)/.exec(metadata.queryString);
  if (match) {
    response.setHeader("Set-Cookie", match[1]);
  }
}

function run_test() {
  // Allow all cookies if the pref service is available in this process.
  if (!inChildProcess())
    Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);

  httpserver = new HttpServer();
  httpserver.registerPathHandler("/set", setHandler);
  httpserver.registerPathHandler("/present", presentHandler);
  httpserver.start(-1);
  
  do_test_pending();
  
  function check_cookie(req) {
    req.QueryInterface(Ci.nsIHttpChannel);
    do_check_eq(req.responseStatus, 200);
    try {
      do_check_true(req.getResponseHeader("Set-Cookie") != "", "expected a Set-Cookie header");
    } catch (x) {
      do_throw("missing Set-Cookie header");
    }

    runNextTest();
  }

  let tests = [];
  
  function runNextTest() {
    do_execute_soon(tests.shift());
  }
  
  tests.push(function() {
    set_cookie("C1=V1", check_cookie);
  });
  tests.push(function() {
    set_private_cookie("C2=V2", check_cookie);
  });
  tests.push(function() {
    // Check that the first cookie is present in a non-private request
    check_cookie_presence("C1=V1", false, true, runNextTest);
  });
  tests.push(function() {
    // Check that the second cookie is present in a private request
    check_cookie_presence("C2=V2", true, true, runNextTest);
  });
  tests.push(function() {
    // Check that the first cookie is not present in a private request
    check_cookie_presence("C1=V1", true, false, runNextTest);
  });
  tests.push(function() {
    // Check that the second cookie is not present in a non-private request
    check_cookie_presence("C2=V2", false, false, runNextTest);
  });

  // The following test only works in a non-e10s situation at the moment,
  // since the notification needs to run in the parent process but there is
  // no existing mechanism to make that happen.  
  if (!inChildProcess()) {
    tests.push(function() {
      // Simulate all private browsing instances being closed
      var obsvc = Cc["@mozilla.org/observer-service;1"].
        getService(Ci.nsIObserverService);
      obsvc.notifyObservers(null, "last-pb-context-exited", null);
      // Check that all private cookies are now unavailable in new private requests
      check_cookie_presence("C2=V2", true, false, runNextTest);
    });
  }
  
  tests.push(function() { httpserver.stop(do_test_finished); });
  
  runNextTest();
}