Add xpcshell test to make sure things stay in sync when switching PB mode. b=481775, r=sdwilsh

This commit is contained in:
Dan Witte 2009-10-22 13:53:58 -07:00
Родитель 63ccc788de
Коммит 580ef7cac5
2 изменённых файлов: 84 добавлений и 2 удалений

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

@ -0,0 +1,80 @@
const Cc = Components.classes;
const Ci = Components.interfaces;
function run_test() {
var cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
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);
var pb = null;
try {
pb = Cc["@mozilla.org/privatebrowsing;1"].getService(Ci.nsIPrivateBrowsingService);
} catch (e) {}
// accept all cookies and clear the table
prefs.setIntPref("network.cookie.lifetimePolicy", 0);
cs.removeAll();
// saturate the cookie table
addCookies(0, 5000);
// check how many cookies we have
var count = getCookieCount();
do_check_neq(count, 0);
// if private browsing is available
if (pb) {
// enter private browsing mode
pb.privateBrowsingEnabled = true;
// check that we have zero cookies
do_check_eq(getCookieCount(), 0);
// saturate the cookie table again
addCookies(5000, 5000);
// check we have the same number of cookies
do_check_eq(getCookieCount(), count);
// remove them all
cs.removeAll();
do_check_eq(getCookieCount(), 0);
// leave private browsing mode
pb.privateBrowsingEnabled = false;
}
// make sure our cookies are back
do_check_eq(getCookieCount(), count);
// set a few more, to trigger a purge
addCookies(10000, 1000);
// check we have the same number of cookies
var count = getCookieCount();
do_check_eq(getCookieCount(), count);
// remove them all
cs.removeAll();
do_check_eq(getCookieCount(), 0);
}
function getCookieCount() {
var count = 0;
var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
var enumerator = cm.enumerator;
while (enumerator.hasMoreElements()) {
if (!(enumerator.getNext() instanceof Ci.nsICookie2))
throw new Error("not a cookie");
++count;
}
return count;
}
function addCookies(start, count) {
var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
var expiry = (Date.now() + 1000) * 1000;
for (var i = start; i < start + count; ++i)
cm.add(i + ".bar", "", "foo", "bar", false, false, true, expiry);
}

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

@ -838,10 +838,10 @@ nsCookieService::PrefChanged(nsIPrefBranch *aPrefBranch)
mCookiesPermissions = (PRUint8) LIMIT(val, 0, 2, 0);
if (NS_SUCCEEDED(aPrefBranch->GetIntPref(kPrefMaxNumberOfCookies, &val)))
mMaxNumberOfCookies = (PRUint16) LIMIT(val, 0, 0xFFFF, 0xFFFF);
mMaxNumberOfCookies = (PRUint16) LIMIT(val, 1, 0xFFFF, kMaxNumberOfCookies);
if (NS_SUCCEEDED(aPrefBranch->GetIntPref(kPrefMaxCookiesPerHost, &val)))
mMaxCookiesPerHost = (PRUint16) LIMIT(val, 0, 0xFFFF, 0xFFFF);
mMaxCookiesPerHost = (PRUint16) LIMIT(val, 1, 0xFFFF, kMaxCookiesPerHost);
}
/******************************************************************************
@ -2130,6 +2130,7 @@ removeExpiredCallback(nsCookieEntry *aEntry,
void
nsCookieService::RemoveExpiredCookies(PRInt64 aCurrentTime)
{
NS_ASSERTION(mDBState->hostTable.Count() > 0, "table is empty");
#ifdef PR_LOGGING
PRUint32 initialCookieCount = mDBState->cookieCount;
#endif
@ -2418,5 +2419,6 @@ findOldestCallback(nsCookieEntry *aEntry,
void
nsCookieService::FindOldestCookie(nsEnumerationData &aData)
{
NS_ASSERTION(mDBState->hostTable.Count() > 0, "table is empty");
mDBState->hostTable.EnumerateEntries(findOldestCallback, &aData);
}