2010-11-12 20:32:35 +03:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
2010-11-12 20:32:36 +03:00
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
2010-11-12 20:32:35 +03:00
|
|
|
|
|
|
|
// Test private browsing mode.
|
|
|
|
|
2020-04-07 19:00:03 +03:00
|
|
|
"use strict";
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var test_generator = do_run_test();
|
2010-11-12 20:32:35 +03:00
|
|
|
|
|
|
|
function run_test() {
|
|
|
|
do_test_pending();
|
|
|
|
do_run_generator(test_generator);
|
|
|
|
}
|
|
|
|
|
|
|
|
function finish_test() {
|
2017-12-21 13:10:23 +03:00
|
|
|
executeSoon(function() {
|
2017-02-11 05:47:57 +03:00
|
|
|
test_generator.return();
|
2010-11-12 20:32:35 +03:00
|
|
|
do_test_finished();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
function make_channel(url) {
|
2016-03-08 01:01:26 +03:00
|
|
|
return NetUtil.newChannel({
|
|
|
|
uri: url,
|
|
|
|
loadUsingSystemPrincipal: true,
|
|
|
|
}).QueryInterface(Ci.nsIHttpChannel);
|
2012-02-08 22:37:07 +04:00
|
|
|
}
|
|
|
|
|
2017-02-11 05:47:57 +03:00
|
|
|
function* do_run_test() {
|
2010-11-12 20:32:35 +03:00
|
|
|
// Set up a profile.
|
|
|
|
let profile = do_get_profile();
|
|
|
|
|
2020-03-04 11:59:08 +03:00
|
|
|
// We don't want to have CookieJarSettings blocking this test.
|
2019-03-08 12:04:57 +03:00
|
|
|
Services.prefs.setBoolPref(
|
2020-03-04 11:59:08 +03:00
|
|
|
"network.cookieJarSettings.unblocked_for_testing",
|
2019-03-08 12:04:57 +03:00
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2010-11-12 20:32:35 +03:00
|
|
|
// Test with cookies enabled.
|
|
|
|
Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
|
|
|
|
|
|
|
|
// Create URIs pointing to foo.com and bar.com.
|
|
|
|
let uri1 = NetUtil.newURI("http://foo.com/foo.html");
|
|
|
|
let uri2 = NetUtil.newURI("http://bar.com/bar.html");
|
|
|
|
|
|
|
|
// Set a cookie for host 1.
|
2020-03-05 00:25:33 +03:00
|
|
|
Services.cookies.setCookieString(uri1, "oh=hai; max-age=1000", null);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
|
2010-11-12 20:32:35 +03:00
|
|
|
|
|
|
|
// Enter private browsing mode, set a cookie for host 2, and check the counts.
|
2012-02-08 22:37:07 +04:00
|
|
|
var chan1 = make_channel(uri1.spec);
|
|
|
|
chan1.QueryInterface(Ci.nsIPrivateBrowsingChannel);
|
|
|
|
chan1.setPrivate(true);
|
2010-11-12 20:32:35 +03:00
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
var chan2 = make_channel(uri2.spec);
|
|
|
|
chan2.QueryInterface(Ci.nsIPrivateBrowsingChannel);
|
|
|
|
chan2.setPrivate(true);
|
2010-11-12 20:32:35 +03:00
|
|
|
|
2020-03-05 00:25:33 +03:00
|
|
|
Services.cookies.setCookieString(uri2, "oh=hai; max-age=1000", chan2);
|
2019-05-31 12:36:44 +03:00
|
|
|
Assert.equal(Services.cookiemgr.getCookieString(uri1, chan1), "");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai");
|
2010-11-12 20:32:35 +03:00
|
|
|
|
2012-02-08 22:37:07 +04:00
|
|
|
// Remove cookies and check counts.
|
2017-04-14 22:51:39 +03:00
|
|
|
Services.obs.notifyObservers(null, "last-pb-context-exited");
|
2019-05-31 12:36:44 +03:00
|
|
|
Assert.equal(Services.cookiemgr.getCookieString(uri1, chan1), "");
|
|
|
|
Assert.equal(Services.cookiemgr.getCookieString(uri2, chan2), "");
|
2010-11-12 20:32:35 +03:00
|
|
|
|
2020-03-05 00:25:33 +03:00
|
|
|
Services.cookies.setCookieString(uri2, "oh=hai; max-age=1000", chan2);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai");
|
2010-11-12 20:32:35 +03:00
|
|
|
|
|
|
|
// Leave private browsing mode and check counts.
|
2017-04-14 22:51:39 +03:00
|
|
|
Services.obs.notifyObservers(null, "last-pb-context-exited");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
|
|
|
|
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
|
2010-11-12 20:32:35 +03:00
|
|
|
|
|
|
|
// Fake a profile change.
|
|
|
|
do_close_profile(test_generator);
|
|
|
|
yield;
|
|
|
|
do_load_profile();
|
|
|
|
|
|
|
|
// Check that the right cookie persisted.
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
|
|
|
|
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
|
2010-11-12 20:32:35 +03:00
|
|
|
|
|
|
|
// Enter private browsing mode, set a cookie for host 2, and check the counts.
|
2019-05-31 12:36:44 +03:00
|
|
|
Assert.equal(Services.cookiemgr.getCookieString(uri1, chan1), "");
|
|
|
|
Assert.equal(Services.cookiemgr.getCookieString(uri2, chan2), "");
|
2020-03-05 00:25:33 +03:00
|
|
|
Services.cookies.setCookieString(uri2, "oh=hai; max-age=1000", chan2);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai");
|
2010-11-12 20:32:35 +03:00
|
|
|
|
|
|
|
// Fake a profile change.
|
|
|
|
do_close_profile(test_generator);
|
|
|
|
yield;
|
|
|
|
do_load_profile();
|
|
|
|
|
|
|
|
// We're still in private browsing mode, but should have a new session.
|
|
|
|
// Check counts.
|
2019-05-31 12:36:44 +03:00
|
|
|
Assert.equal(Services.cookiemgr.getCookieString(uri1, chan1), "");
|
|
|
|
Assert.equal(Services.cookiemgr.getCookieString(uri2, chan2), "");
|
2010-11-12 20:32:35 +03:00
|
|
|
|
|
|
|
// Leave private browsing mode and check counts.
|
2017-04-14 22:51:39 +03:00
|
|
|
Services.obs.notifyObservers(null, "last-pb-context-exited");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
|
|
|
|
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
|
2010-11-12 20:32:35 +03:00
|
|
|
|
|
|
|
// Enter private browsing mode.
|
|
|
|
|
|
|
|
// Fake a profile change, but wait for async read completion.
|
|
|
|
do_close_profile(test_generator);
|
|
|
|
yield;
|
|
|
|
do_load_profile(test_generator);
|
|
|
|
yield;
|
|
|
|
|
|
|
|
// We're still in private browsing mode, but should have a new session.
|
|
|
|
// Check counts.
|
2019-05-31 12:36:44 +03:00
|
|
|
Assert.equal(Services.cookiemgr.getCookieString(uri1, chan1), "");
|
|
|
|
Assert.equal(Services.cookiemgr.getCookieString(uri2, chan2), "");
|
2010-11-12 20:32:35 +03:00
|
|
|
|
|
|
|
// Leave private browsing mode and check counts.
|
2017-04-14 22:51:39 +03:00
|
|
|
Services.obs.notifyObservers(null, "last-pb-context-exited");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
|
|
|
|
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
|
2010-11-12 20:32:35 +03:00
|
|
|
|
|
|
|
finish_test();
|
|
|
|
}
|