diff --git a/extensions/cookie/test/unit/test_bug481775.js b/extensions/cookie/test/unit/test_bug481775.js new file mode 100644 index 000000000000..86dd1663513f --- /dev/null +++ b/extensions/cookie/test/unit/test_bug481775.js @@ -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); +} + diff --git a/netwerk/cookie/src/nsCookieService.cpp b/netwerk/cookie/src/nsCookieService.cpp index 63315ad573c4..a094929fa393 100644 --- a/netwerk/cookie/src/nsCookieService.cpp +++ b/netwerk/cookie/src/nsCookieService.cpp @@ -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); }