This commit is contained in:
Dan Witte 2010-05-28 16:58:06 -07:00
Родитель 23d073d72a
Коммит cd856a4307
4 изменённых файлов: 23 добавлений и 217 удалений

Просмотреть файл

@ -1,82 +0,0 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
Components.utils.import("resource://gre/modules/Services.jsm");
const _Cc = Components.classes;
const _Ci = Components.interfaces;
// Ensure 'db' is closed; if not, spin until it is.
function do_wait_for_db_close(dbfile) {
// If there is no database then it cannot be locked.
if (!dbfile.exists())
return;
let thr = _Cc["@mozilla.org/thread-manager;1"].
getService(_Ci.nsIThreadManager).
mainThread;
// Wait until we can open a connection to the database
let db = null;
while (!db) {
// Poll for database
try {
db = Services.storage.openUnsharedDatabase(dbfile);
}
catch (e) {
if (thr.hasPendingEvents())
thr.processNextEvent(false);
}
}
// Wait until we can write to the database
while (db) {
// Poll for write access
try {
db.schemaVersion = 81;
if (db.schemaVersion != 81)
throw "Write poll loop schemaVersion not changed";
db.schemaVersion = 0; // harmless -- the cookieservice will reset it
db.close();
db = null;
}
catch (e) {
dump("Write poll loop threw error " + e + "\n");
if (thr.hasPendingEvents())
thr.processNextEvent(false);
}
}
}
// Reload the profile by calling the 'observe' method on the given service.
function do_reload_profile(profile, observer, cleanse) {
let dbfile = profile.QueryInterface(_Ci.nsILocalFile).clone();
dbfile.append("cookies.sqlite");
observer.observe(null, "profile-before-change", cleanse ? cleanse : "");
do_wait_for_db_close(dbfile);
observer.observe(null, "profile-do-change", "");
}
// Set four cookies; with & without channel, http and non-http; and test
// the cookie count against 'expected' after each set.
function do_set_cookies(uri, channel, session, expected) {
const cs = _Cc["@mozilla.org/cookieService;1"].getService(_Ci.nsICookieService);
var suffix = session ? "" : "; max-age=1000";
// without channel
cs.setCookieString(uri, null, "oh=hai" + suffix, null);
do_check_eq(cs.countCookiesFromHost(uri.host), expected[0]);
// with channel
cs.setCookieString(uri, null, "can=has" + suffix, channel);
do_check_eq(cs.countCookiesFromHost(uri.host), expected[1]);
// without channel, from http
cs.setCookieStringFromHttp(uri, null, null, "cheez=burger" + suffix, null, null);
do_check_eq(cs.countCookiesFromHost(uri.host), expected[2]);
// with channel, from http
cs.setCookieStringFromHttp(uri, null, null, "hot=dog" + suffix, null, channel);
do_check_eq(cs.countCookiesFromHost(uri.host), expected[3]);
}

Просмотреть файл

@ -1,60 +0,0 @@
// test for cookie persistence across sessions, for the cases:
// 1) network.cookie.lifetimePolicy = 0 (expire naturally)
// 2) network.cookie.lifetimePolicy = 2 (expire at end of session)
const Cc = Components.classes;
const Ci = Components.interfaces;
function run_test() {
// Set up a profile.
let profile = do_get_profile();
var cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
var cso = cs.QueryInterface(Ci.nsIObserver);
var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
// Create URIs and channels pointing to foo.com and bar.com.
// We will use these to put foo.com into first and third party contexts.
var spec1 = "http://foo.com/foo.html";
var spec2 = "http://bar.com/bar.html";
var uri1 = ios.newURI(spec1, null, null);
var uri2 = ios.newURI(spec2, null, null);
var channel1 = ios.newChannelFromURI(uri1);
var channel2 = ios.newChannelFromURI(uri2);
// Force the channel URI to be used when determining the originating URI of
// the channel.
var httpchannel1 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
var httpchannel2 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
httpchannel1.forceAllowThirdPartyCookie = true;
httpchannel2.forceAllowThirdPartyCookie = true;
// test with cookies enabled, and third party cookies persistent.
prefs.setIntPref("network.cookie.cookieBehavior", 0);
prefs.setBoolPref("network.cookie.thirdparty.sessionOnly", false);
do_set_cookies(uri1, channel1, false, [1, 2, 3, 4]);
do_set_cookies(uri2, channel2, true, [1, 2, 3, 4]);
// fake a profile change
do_reload_profile(profile, cso);
do_check_eq(cs.countCookiesFromHost(uri1.host), 4);
do_check_eq(cs.countCookiesFromHost(uri2.host), 0);
// cleanse them
do_reload_profile(profile, cso, "shutdown-cleanse");
do_check_eq(cs.countCookiesFromHost(uri1.host), 0);
do_check_eq(cs.countCookiesFromHost(uri2.host), 0);
// test with cookies set to session-only
prefs.setIntPref("network.cookie.lifetimePolicy", 2);
do_set_cookies(uri1, channel1, false, [1, 2, 3, 4]);
do_set_cookies(uri2, channel2, true, [1, 2, 3, 4]);
// fake a profile change
do_reload_profile(profile, cso);
do_check_eq(cs.countCookiesFromHost(uri1.host), 0);
do_check_eq(cs.countCookiesFromHost(uri2.host), 0);
}

Просмотреть файл

@ -22,17 +22,13 @@ function run_test() {
// test with cookies enabled
prefs.setIntPref("network.cookie.cookieBehavior", 0);
do_set_cookies(uri1, channel1, true, [1, 2, 3, 4]);
cs.removeAll();
do_set_cookies(uri1, channel2, true, [1, 2, 3, 4]);
cs.removeAll();
run_cookie_test(cs, uri1, channel1, [1, 2, 3, 4]);
run_cookie_test(cs, uri1, channel2, [1, 2, 3, 4]);
// test with third party cookies blocked
prefs.setIntPref("network.cookie.cookieBehavior", 1);
do_set_cookies(uri1, channel1, true, [0, 0, 0, 0]);
cs.removeAll();
do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]);
cs.removeAll();
run_cookie_test(cs, uri1, channel1, [0, 0, 0, 0]);
run_cookie_test(cs, uri1, channel2, [0, 0, 0, 0]);
// Force the channel URI to be used when determining the originating URI of
// the channel.
@ -43,16 +39,28 @@ function run_test() {
// test with cookies enabled
prefs.setIntPref("network.cookie.cookieBehavior", 0);
do_set_cookies(uri1, channel1, true, [1, 2, 3, 4]);
cs.removeAll();
do_set_cookies(uri1, channel2, true, [1, 2, 3, 4]);
cs.removeAll();
run_cookie_test(cs, uri1, channel1, [1, 2, 3, 4]);
run_cookie_test(cs, uri1, channel2, [1, 2, 3, 4]);
// test with third party cookies blocked
prefs.setIntPref("network.cookie.cookieBehavior", 1);
do_set_cookies(uri1, channel1, true, [0, 1, 1, 2]);
cs.removeAll();
do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]);
run_cookie_test(cs, uri1, channel1, [0, 1, 1, 2]);
run_cookie_test(cs, uri1, channel2, [0, 0, 0, 0]);
}
function run_cookie_test(cs, uri, channel, expected) {
// without channel
cs.setCookieString(uri, null, "oh=hai", null);
do_check_eq(cs.countCookiesFromHost("foo.com"), expected[0]);
// with channel
cs.setCookieString(uri, null, "can=has", channel);
do_check_eq(cs.countCookiesFromHost("foo.com"), expected[1]);
// without channel, from http
cs.setCookieStringFromHttp(uri, null, null, "cheez=burger", null, null);
do_check_eq(cs.countCookiesFromHost("foo.com"), expected[2]);
// with channel, from http
cs.setCookieStringFromHttp(uri, null, null, "hot=dog", null, channel);
do_check_eq(cs.countCookiesFromHost("foo.com"), expected[3]);
cs.removeAll();
}

Просмотреть файл

@ -1,60 +0,0 @@
// test third party persistence across sessions, for the cases:
// 1) network.cookie.thirdparty.sessionOnly = false
// 2) network.cookie.thirdparty.sessionOnly = true
const Cc = Components.classes;
const Ci = Components.interfaces;
function run_test() {
// Set up a profile.
let profile = do_get_profile();
var cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
var cso = cs.QueryInterface(Ci.nsIObserver);
var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
// Create URIs and channels pointing to foo.com and bar.com.
// We will use these to put foo.com into first and third party contexts.
var spec1 = "http://foo.com/foo.html";
var spec2 = "http://bar.com/bar.html";
var uri1 = ios.newURI(spec1, null, null);
var uri2 = ios.newURI(spec2, null, null);
var channel1 = ios.newChannelFromURI(uri1);
var channel2 = ios.newChannelFromURI(uri2);
// Force the channel URI to be used when determining the originating URI of
// the channel.
var httpchannel1 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
var httpchannel2 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
httpchannel1.forceAllowThirdPartyCookie = true;
httpchannel2.forceAllowThirdPartyCookie = true;
// test with cookies enabled, and third party cookies persistent.
prefs.setIntPref("network.cookie.cookieBehavior", 0);
prefs.setBoolPref("network.cookie.thirdparty.sessionOnly", false);
do_set_cookies(uri1, channel2, false, [1, 2, 3, 4]);
do_set_cookies(uri2, channel1, true, [1, 2, 3, 4]);
// fake a profile change
do_reload_profile(profile, cso);
do_check_eq(cs.countCookiesFromHost(uri1.host), 4);
do_check_eq(cs.countCookiesFromHost(uri2.host), 0);
// cleanse them
do_reload_profile(profile, cso, "shutdown-cleanse");
do_check_eq(cs.countCookiesFromHost(uri1.host), 0);
do_check_eq(cs.countCookiesFromHost(uri2.host), 0);
// test with third party cookies for session only.
prefs.setBoolPref("network.cookie.thirdparty.sessionOnly", true);
do_set_cookies(uri1, channel2, false, [1, 2, 3, 4]);
do_set_cookies(uri2, channel1, true, [1, 2, 3, 4]);
// fake a profile change
do_reload_profile(profile, cso);
do_check_eq(cs.countCookiesFromHost(uri1.host), 0);
do_check_eq(cs.countCookiesFromHost(uri2.host), 0);
}