| Index: net/base/cookie_monster_unittest.cc
|
| ===================================================================
|
| --- net/base/cookie_monster_unittest.cc (revision 23545)
|
| +++ net/base/cookie_monster_unittest.cc (working copy)
|
| @@ -851,34 +851,66 @@
|
| }
|
|
|
| TEST(CookieMonsterTest, TestTotalGarbageCollection) {
|
| - net::CookieMonster cm(kLastAccessThresholdSeconds);
|
| - // Add a bunch of cookies on a bunch of host, some should get purged.
|
| - const GURL sticky_cookie("http://a0000.izzle");
|
| - for (int i = 0; i < 4000; ++i) {
|
| - GURL url(StringPrintf("http://a%04d.izzle", i));
|
| + net::CookieMonster cm;
|
| + Time now = Time::Now();
|
| +
|
| + // Be careful in this function! GetCookies() updates last-accessed dates and
|
| + // thus can screw up expiry. GetAllCookies() doesn't and thus is safer.
|
| +
|
| + // Add a really old cookie. It should not get purged because there aren't
|
| + // enough cookies to hit our max yet.
|
| + GURL two_months_ago("http://twomonthsago");
|
| + EXPECT_TRUE(cm.SetCookieWithCreationTime(two_months_ago, "a=b",
|
| + now - TimeDelta::FromDays(60)));
|
| + EXPECT_EQ(1U, cm.GetAllCookies().size());
|
| +
|
| + // Add a bunch of cookies on a bunch of hosts. The old cookie should get
|
| + // purged, but none of the new ones should, because they're too new.
|
| + for (int i = 0; i < 2500; ++i) {
|
| + GURL url(StringPrintf("http://a%04d.now", i));
|
| EXPECT_TRUE(cm.SetCookie(url, "a=b"));
|
| - EXPECT_EQ("a=b", cm.GetCookies(url));
|
| + }
|
| + EXPECT_EQ(2500U, cm.GetAllCookies().size());
|
| + EXPECT_TRUE(cm.GetCookies(two_months_ago).empty());
|
|
|
| - // Keep touching the first cookie to ensure it's not purged (since it will
|
| - // always have the most recent access time).
|
| - if (!(i % 500)) {
|
| - PlatformThread::Sleep(1500); // Ensure the timestamps will be different
|
| - // enough to update.
|
| - EXPECT_EQ("a=b", cm.GetCookies(sticky_cookie));
|
| - }
|
| + // Add more cookies. Even if we go over the cookie purge age, if we haven't
|
| + // also covered the purge overage factor, we shouldn't garbage-collect.
|
| + const int64 one_week_from_now_internal =
|
| + (now + TimeDelta::FromDays(7)).ToInternalValue();
|
| + for (int i = 0; i < 500; ++i) {
|
| + GURL url(StringPrintf("http://a%04d.oneweekfromnow", i));
|
| + // The FromInternalValue() incrementing is necessary because
|
| + // SetCookieWithCreationTime() doesn't guarantee unique creation times like
|
| + // SetCookie() does.
|
| + EXPECT_TRUE(cm.SetCookieWithCreationTime(url, "a=b",
|
| + Time::FromInternalValue(one_week_from_now_internal + i)));
|
| }
|
| + EXPECT_TRUE(cm.SetCookieWithCreationTime(
|
| + GURL("http://test.thirtyonedaysfromnow"), "a=b",
|
| + now + TimeDelta::FromDays(31)));
|
| + EXPECT_EQ(3001U, cm.GetAllCookies().size()); // 2500 "now", 500 "+ 7 days",
|
| + // 1 "+ 31 days"
|
|
|
| - // Check that cookies that still exist.
|
| - for (int i = 0; i < 4000; ++i) {
|
| - GURL url(StringPrintf("http://a%04d.izzle", i));
|
| - if ((i == 0) || (i > 1001)) {
|
| - // Cookies should still be around.
|
| - EXPECT_FALSE(cm.GetCookies(url).empty());
|
| - } else if (i < 701) {
|
| - // Cookies should have gotten purged.
|
| - EXPECT_TRUE(cm.GetCookies(url).empty());
|
| - }
|
| + // Add more cookies in the future. Now that the date is far enough forward,
|
| + // we should collect until there are no more old enough cookies.
|
| + const int64 thirty_five_days_from_now_internal =
|
| + (now + TimeDelta::FromDays(35)).ToInternalValue();
|
| + for (int i = 0; i < 2499; ++i) {
|
| + GURL url(StringPrintf("http://a%04d.thirtyfivedaysfromnow", i));
|
| + EXPECT_TRUE(cm.SetCookieWithCreationTime(url, "a=b",
|
| + Time::FromInternalValue(thirty_five_days_from_now_internal + i)));
|
| }
|
| + EXPECT_EQ(3000U, cm.GetAllCookies().size()); // 500 "+ 7 days",
|
| + // 1 "+ 31 days",
|
| + // 2499 "+ 35 days"
|
| +
|
| + // If we manage to start garbage collection, then cookies should be eligible
|
| + // once they're over the max age; they don't have to cover the overage factor.
|
| + EXPECT_TRUE(cm.SetCookieWithCreationTime(
|
| + GURL("http://test.sixtysixdaysfromnow"), "a=b",
|
| + now + TimeDelta::FromDays(66)));
|
| + EXPECT_EQ(2000U, cm.GetAllCookies().size()); // 1999 "+ 35 days",
|
| + // 1 "+ 66 days"
|
| }
|
|
|
| // Formerly NetUtilTest.CookieTest back when we used wininet's cookie handling.
|
|
|