2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2008-10-20 01:12:53 +04:00
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
Cu.import("resource://testing-common/httpd.js");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2008-10-20 01:12:53 +04:00
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
var httpserver;
|
2008-10-20 01:12:53 +04:00
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
function inChildProcess() {
|
|
|
|
return Cc["@mozilla.org/xre/app-info;1"]
|
|
|
|
.getService(Ci.nsIXULRuntime)
|
|
|
|
.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
|
2008-10-20 01:12:53 +04:00
|
|
|
}
|
2012-02-08 22:37:07 +04:00
|
|
|
function makeChan(path) {
|
|
|
|
var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
|
2013-07-19 21:19:28 +04:00
|
|
|
var chan = ios.newChannel("http://localhost:" + httpserver.identity.primaryPort + "/" + path, null, null)
|
2012-02-08 22:37:07 +04:00
|
|
|
.QueryInterface(Ci.nsIHttpChannel);
|
|
|
|
return chan;
|
2012-02-08 22:37:07 +04:00
|
|
|
}
|
2008-11-04 02:22:22 +03:00
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
function setup_chan(path, isPrivate, callback) {
|
|
|
|
var chan = makeChan(path);
|
|
|
|
chan.QueryInterface(Ci.nsIPrivateBrowsingChannel).setPrivate(isPrivate);
|
|
|
|
chan.asyncOpen(new ChannelListener(callback), null);
|
|
|
|
}
|
2012-11-16 18:18:41 +04:00
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
function set_cookie(value, callback) {
|
|
|
|
return setup_chan('set?cookie=' + value, false, callback);
|
2012-02-08 22:37:07 +04:00
|
|
|
}
|
2008-10-20 01:12:53 +04:00
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
function set_private_cookie(value, callback) {
|
|
|
|
return setup_chan('set?cookie=' + value, true, callback);
|
2012-02-08 22:37:07 +04:00
|
|
|
}
|
2008-10-20 01:12:53 +04:00
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2008-11-04 02:22:22 +03:00
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
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, "");
|
|
|
|
}
|
2012-02-08 22:37:07 +04:00
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
2012-11-16 18:18:41 +04:00
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
function run_test() {
|
2013-02-22 20:16:01 +04:00
|
|
|
// Allow all cookies if the pref service is available in this process.
|
|
|
|
if (!inChildProcess())
|
|
|
|
Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
|
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
httpserver = new HttpServer();
|
|
|
|
httpserver.registerPathHandler("/set", setHandler);
|
|
|
|
httpserver.registerPathHandler("/present", presentHandler);
|
2013-07-19 21:19:28 +04:00
|
|
|
httpserver.start(-1);
|
2012-02-08 22:37:07 +04:00
|
|
|
|
|
|
|
do_test_pending();
|
|
|
|
|
|
|
|
function check_cookie(req) {
|
|
|
|
req.QueryInterface(Ci.nsIHttpChannel);
|
|
|
|
do_check_eq(req.responseStatus, 200);
|
2012-11-16 18:18:41 +04:00
|
|
|
try {
|
2012-02-08 22:37:07 +04:00
|
|
|
do_check_true(req.getResponseHeader("Set-Cookie") != "", "expected a Set-Cookie header");
|
|
|
|
} catch (x) {
|
|
|
|
do_throw("missing Set-Cookie header");
|
2012-11-16 18:18:41 +04:00
|
|
|
}
|
2012-02-08 22:37:07 +04:00
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
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);
|
|
|
|
});
|
2008-10-20 01:12:53 +04:00
|
|
|
}
|
2012-02-08 22:37:07 +04:00
|
|
|
|
|
|
|
tests.push(function() { httpserver.stop(do_test_finished); });
|
|
|
|
|
|
|
|
runNextTest();
|
2008-10-20 01:12:53 +04:00
|
|
|
}
|