2010-06-21 17:50:35 -07:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
2010-11-12 09:32:36 -08:00
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
2009-12-15 14:21:28 -08:00
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
let test_generator = do_run_test();
|
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
function run_test()
|
|
|
|
{
|
2010-10-19 17:24:52 -07:00
|
|
|
do_test_pending();
|
2010-10-19 17:24:53 -07:00
|
|
|
do_run_generator(test_generator);
|
|
|
|
}
|
|
|
|
|
|
|
|
function continue_test()
|
|
|
|
{
|
|
|
|
do_run_generator(test_generator);
|
2010-10-19 17:24:52 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
function repeat_test()
|
|
|
|
{
|
|
|
|
// The test is probably going to fail because setting a batch of cookies took
|
|
|
|
// a significant fraction of 'gPurgeAge'. Compensate by rerunning the
|
|
|
|
// test with a larger purge age.
|
|
|
|
do_check_true(gPurgeAge < 64);
|
|
|
|
gPurgeAge *= 2;
|
2010-11-10 10:31:11 -08:00
|
|
|
gShortExpiry *= 2;
|
2010-10-19 17:24:53 -07:00
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
do_execute_soon(function() {
|
|
|
|
test_generator.close();
|
2010-10-19 17:24:53 -07:00
|
|
|
test_generator = do_run_test();
|
|
|
|
do_run_generator(test_generator);
|
2010-10-19 17:24:52 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
// Purge threshold, in seconds.
|
|
|
|
let gPurgeAge = 1;
|
|
|
|
|
2010-11-10 10:31:11 -08:00
|
|
|
// Short expiry age, in seconds.
|
|
|
|
let gShortExpiry = 2;
|
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
// Required delay to ensure a purge occurs, in milliseconds. This must be at
|
|
|
|
// least gPurgeAge + 10%, and includes a little fuzz to account for timer
|
|
|
|
// resolution and possible differences between PR_Now() and Date.now().
|
|
|
|
function get_purge_delay()
|
|
|
|
{
|
|
|
|
return gPurgeAge * 1100 + 100;
|
|
|
|
}
|
|
|
|
|
2010-11-10 10:31:11 -08:00
|
|
|
// Required delay to ensure a cookie set with an expiry time 'gShortExpiry' into
|
|
|
|
// the future will have expired.
|
|
|
|
function get_expiry_delay()
|
|
|
|
{
|
|
|
|
return gShortExpiry * 1000 + 100;
|
|
|
|
}
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
function do_run_test()
|
2009-12-15 14:21:28 -08:00
|
|
|
{
|
2010-10-19 17:24:52 -07:00
|
|
|
// Set up a profile.
|
|
|
|
let profile = do_get_profile();
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
// twiddle prefs to convenient values for this test
|
2010-10-19 17:24:53 -07:00
|
|
|
Services.prefs.setIntPref("network.cookie.purgeAge", gPurgeAge);
|
|
|
|
Services.prefs.setIntPref("network.cookie.maxNumber", 100);
|
|
|
|
|
|
|
|
let expiry = Date.now() / 1000 + 1000;
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
// eviction is performed based on two limits: when the total number of cookies
|
2010-10-19 17:24:53 -07:00
|
|
|
// exceeds maxNumber + 10% (110), and when cookies are older than purgeAge
|
2009-12-15 14:21:28 -08:00
|
|
|
// (1 second). purging is done when both conditions are satisfied, and only
|
|
|
|
// those cookies are purged.
|
|
|
|
|
|
|
|
// we test the following cases of eviction:
|
|
|
|
// 1) excess and age are satisfied, but only some of the excess are old enough
|
|
|
|
// to be purged.
|
2010-10-19 17:24:53 -07:00
|
|
|
Services.cookiemgr.removeAll();
|
|
|
|
if (!set_cookies(0, 5, expiry)) {
|
|
|
|
repeat_test();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Sleep a while, to make sure the first batch of cookies is older than
|
|
|
|
// the second (timer resolution varies on different platforms).
|
|
|
|
do_timeout(get_purge_delay(), continue_test);
|
|
|
|
yield;
|
|
|
|
if (!set_cookies(5, 111, expiry)) {
|
|
|
|
repeat_test();
|
|
|
|
return;
|
|
|
|
}
|
2010-10-19 17:24:52 -07:00
|
|
|
|
|
|
|
// Fake a profile change, to ensure eviction affects the database correctly.
|
|
|
|
do_close_profile(test_generator);
|
|
|
|
yield;
|
|
|
|
do_load_profile();
|
2010-10-19 17:24:53 -07:00
|
|
|
do_check_true(check_remaining_cookies(111, 5, 106));
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
// 2) excess and age are satisfied, and all of the excess are old enough
|
|
|
|
// to be purged.
|
2010-10-19 17:24:53 -07:00
|
|
|
Services.cookiemgr.removeAll();
|
|
|
|
if (!set_cookies(0, 10, expiry)) {
|
|
|
|
repeat_test();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
do_timeout(get_purge_delay(), continue_test);
|
|
|
|
yield;
|
|
|
|
if (!set_cookies(10, 111, expiry)) {
|
|
|
|
repeat_test();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
do_close_profile(test_generator);
|
|
|
|
yield;
|
|
|
|
do_load_profile();
|
2010-10-19 17:24:53 -07:00
|
|
|
do_check_true(check_remaining_cookies(111, 10, 101));
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
// 3) excess and age are satisfied, and more than the excess are old enough
|
|
|
|
// to be purged.
|
2010-10-19 17:24:53 -07:00
|
|
|
Services.cookiemgr.removeAll();
|
|
|
|
if (!set_cookies(0, 50, expiry)) {
|
|
|
|
repeat_test();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
do_timeout(get_purge_delay(), continue_test);
|
|
|
|
yield;
|
|
|
|
if (!set_cookies(50, 111, expiry)) {
|
|
|
|
repeat_test();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
do_close_profile(test_generator);
|
|
|
|
yield;
|
|
|
|
do_load_profile();
|
2010-10-19 17:24:53 -07:00
|
|
|
do_check_true(check_remaining_cookies(111, 50, 101));
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
// 4) excess but not age are satisfied.
|
2010-10-19 17:24:53 -07:00
|
|
|
Services.cookiemgr.removeAll();
|
|
|
|
if (!set_cookies(0, 120, expiry)) {
|
|
|
|
repeat_test();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
do_close_profile(test_generator);
|
|
|
|
yield;
|
|
|
|
do_load_profile();
|
2010-10-19 17:24:53 -07:00
|
|
|
do_check_true(check_remaining_cookies(120, 0, 120));
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
// 5) age but not excess are satisfied.
|
2010-10-19 17:24:53 -07:00
|
|
|
Services.cookiemgr.removeAll();
|
|
|
|
if (!set_cookies(0, 20, expiry)) {
|
|
|
|
repeat_test();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
do_timeout(get_purge_delay(), continue_test);
|
|
|
|
yield;
|
|
|
|
if (!set_cookies(20, 110, expiry)) {
|
|
|
|
repeat_test();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
do_close_profile(test_generator);
|
|
|
|
yield;
|
|
|
|
do_load_profile();
|
2010-10-19 17:24:53 -07:00
|
|
|
do_check_true(check_remaining_cookies(110, 20, 110));
|
2009-12-15 14:21:28 -08:00
|
|
|
|
2010-11-10 10:31:11 -08:00
|
|
|
// 6) Excess and age are satisfied, but the cookie limit can be satisfied by
|
|
|
|
// purging expired cookies.
|
|
|
|
Services.cookiemgr.removeAll();
|
|
|
|
let shortExpiry = Math.floor(Date.now() / 1000) + gShortExpiry;
|
|
|
|
if (!set_cookies(0, 20, shortExpiry)) {
|
|
|
|
repeat_test();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
do_timeout(get_expiry_delay(), continue_test);
|
|
|
|
yield;
|
|
|
|
if (!set_cookies(20, 110, expiry)) {
|
|
|
|
repeat_test();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
do_timeout(get_purge_delay(), continue_test);
|
|
|
|
yield;
|
|
|
|
if (!set_cookies(110, 111, expiry)) {
|
|
|
|
repeat_test();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
do_close_profile(test_generator);
|
|
|
|
yield;
|
|
|
|
do_load_profile();
|
|
|
|
do_check_true(check_remaining_cookies(111, 20, 91));
|
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
do_finish_generator_test(test_generator);
|
2009-12-15 14:21:28 -08:00
|
|
|
}
|
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
// Set 'end - begin' total cookies, with consecutively increasing hosts numbered
|
|
|
|
// 'begin' to 'end'.
|
|
|
|
function set_cookies(begin, end, expiry)
|
2009-12-15 14:21:28 -08:00
|
|
|
{
|
2010-10-19 17:24:53 -07:00
|
|
|
do_check_true(begin != end);
|
2009-12-15 14:21:28 -08:00
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
let beginTime;
|
|
|
|
for (let i = begin; i < end; ++i) {
|
|
|
|
let host = "eviction." + i + ".tests";
|
2010-10-19 17:24:52 -07:00
|
|
|
Services.cookiemgr.add(host, "", "test", "eviction", false, false, false,
|
|
|
|
expiry);
|
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
if (i == begin)
|
|
|
|
beginTime = get_creationTime(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
let endTime = get_creationTime(end - 1);
|
2010-11-10 10:31:11 -08:00
|
|
|
do_check_true(begin == end - 1 || endTime > beginTime);
|
2010-10-19 17:24:53 -07:00
|
|
|
if (endTime - beginTime > gPurgeAge * 1000000) {
|
|
|
|
// Setting cookies took an amount of time very close to the purge threshold.
|
|
|
|
// Retry the test with a larger threshold.
|
|
|
|
return false;
|
2009-12-15 14:21:28 -08:00
|
|
|
}
|
2010-10-19 17:24:53 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_creationTime(i)
|
|
|
|
{
|
|
|
|
let host = "eviction." + i + ".tests";
|
|
|
|
let enumerator = Services.cookiemgr.getCookiesFromHost(host);
|
|
|
|
do_check_true(enumerator.hasMoreElements());
|
|
|
|
let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
|
|
|
|
return cookie.creationTime;
|
2010-10-19 17:24:52 -07:00
|
|
|
}
|
2009-12-15 14:21:28 -08:00
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
// Test that 'aNumberToExpect' cookies remain after purging is complete, and
|
|
|
|
// that the cookies that remain consist of the set expected given the number of
|
|
|
|
// of older and newer cookies -- eviction should occur by order of lastAccessed
|
|
|
|
// time, if both the limit on total cookies (maxNumber + 10%) and the purge age
|
2010-10-19 17:24:53 -07:00
|
|
|
// + 10% are exceeded.
|
2010-10-19 17:24:52 -07:00
|
|
|
function check_remaining_cookies(aNumberTotal, aNumberOld, aNumberToExpect) {
|
|
|
|
var enumerator = Services.cookiemgr.enumerator;
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (enumerator.hasMoreElements()) {
|
|
|
|
var cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
|
|
|
|
++i;
|
|
|
|
|
|
|
|
if (aNumberTotal != aNumberToExpect) {
|
|
|
|
// make sure the cookie is one of the batch we expect was purged.
|
|
|
|
var hostNumber = new Number(cookie.rawHost.split(".")[1]);
|
|
|
|
if (hostNumber < (aNumberOld - aNumberToExpect)) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
return i == aNumberToExpect;
|
2009-12-15 14:21:28 -08:00
|
|
|
}
|