| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <time.h> | 5 #include <time.h> |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/platform_thread.h" | 10 #include "base/platform_thread.h" |
| (...skipping 833 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 844 EXPECT_TRUE(cm.SetCookie(url_google, cookie)); | 844 EXPECT_TRUE(cm.SetCookie(url_google, cookie)); |
| 845 std::string cookies = cm.GetCookies(url_google); | 845 std::string cookies = cm.GetCookies(url_google); |
| 846 // Make sure we find it in the cookies. | 846 // Make sure we find it in the cookies. |
| 847 EXPECT_TRUE(cookies.find(cookie) != std::string::npos); | 847 EXPECT_TRUE(cookies.find(cookie) != std::string::npos); |
| 848 // Count the number of cookies. | 848 // Count the number of cookies. |
| 849 EXPECT_LE(CountInString(cookies, '='), 70); | 849 EXPECT_LE(CountInString(cookies, '='), 70); |
| 850 } | 850 } |
| 851 } | 851 } |
| 852 | 852 |
| 853 TEST(CookieMonsterTest, TestTotalGarbageCollection) { | 853 TEST(CookieMonsterTest, TestTotalGarbageCollection) { |
| 854 net::CookieMonster cm(kLastAccessThresholdSeconds); | 854 net::CookieMonster cm; |
| 855 // Add a bunch of cookies on a bunch of host, some should get purged. | 855 Time now = Time::Now(); |
| 856 const GURL sticky_cookie("http://a0000.izzle"); | 856 |
| 857 for (int i = 0; i < 4000; ++i) { | 857 // Be careful in this function! GetCookies() updates last-accessed dates and |
| 858 GURL url(StringPrintf("http://a%04d.izzle", i)); | 858 // thus can screw up expiry. GetAllCookies() doesn't and thus is safer. |
| 859 |
| 860 // Add a really old cookie. It should not get purged because there aren't |
| 861 // enough cookies to hit our max yet. |
| 862 GURL two_months_ago("http://twomonthsago"); |
| 863 EXPECT_TRUE(cm.SetCookieWithCreationTime(two_months_ago, "a=b", |
| 864 now - TimeDelta::FromDays(60))); |
| 865 EXPECT_EQ(1U, cm.GetAllCookies().size()); |
| 866 |
| 867 // Add a bunch of cookies on a bunch of hosts. The old cookie should get |
| 868 // purged, but none of the new ones should, because they're too new. |
| 869 for (int i = 0; i < 2500; ++i) { |
| 870 GURL url(StringPrintf("http://a%04d.now", i)); |
| 859 EXPECT_TRUE(cm.SetCookie(url, "a=b")); | 871 EXPECT_TRUE(cm.SetCookie(url, "a=b")); |
| 860 EXPECT_EQ("a=b", cm.GetCookies(url)); | 872 } |
| 873 EXPECT_EQ(2500U, cm.GetAllCookies().size()); |
| 874 EXPECT_TRUE(cm.GetCookies(two_months_ago).empty()); |
| 861 | 875 |
| 862 // Keep touching the first cookie to ensure it's not purged (since it will | 876 // Add more cookies. Even if we go over the cookie purge age, if we haven't |
| 863 // always have the most recent access time). | 877 // also covered the purge overage factor, we shouldn't garbage-collect. |
| 864 if (!(i % 500)) { | 878 const int64 one_week_from_now_internal = |
| 865 PlatformThread::Sleep(1500); // Ensure the timestamps will be different | 879 (now + TimeDelta::FromDays(7)).ToInternalValue(); |
| 866 // enough to update. | 880 for (int i = 0; i < 500; ++i) { |
| 867 EXPECT_EQ("a=b", cm.GetCookies(sticky_cookie)); | 881 GURL url(StringPrintf("http://a%04d.oneweekfromnow", i)); |
| 868 } | 882 // The FromInternalValue() incrementing is necessary because |
| 883 // SetCookieWithCreationTime() doesn't guarantee unique creation times like |
| 884 // SetCookie() does. |
| 885 EXPECT_TRUE(cm.SetCookieWithCreationTime(url, "a=b", |
| 886 Time::FromInternalValue(one_week_from_now_internal + i))); |
| 869 } | 887 } |
| 888 EXPECT_TRUE(cm.SetCookieWithCreationTime( |
| 889 GURL("http://test.thirtyonedaysfromnow"), "a=b", |
| 890 now + TimeDelta::FromDays(31))); |
| 891 EXPECT_EQ(3001U, cm.GetAllCookies().size()); // 2500 "now", 500 "+ 7 days", |
| 892 // 1 "+ 31 days" |
| 870 | 893 |
| 871 // Check that cookies that still exist. | 894 // Add more cookies in the future. Now that the date is far enough forward, |
| 872 for (int i = 0; i < 4000; ++i) { | 895 // we should collect until there are no more old enough cookies. |
| 873 GURL url(StringPrintf("http://a%04d.izzle", i)); | 896 const int64 thirty_five_days_from_now_internal = |
| 874 if ((i == 0) || (i > 1001)) { | 897 (now + TimeDelta::FromDays(35)).ToInternalValue(); |
| 875 // Cookies should still be around. | 898 for (int i = 0; i < 2499; ++i) { |
| 876 EXPECT_FALSE(cm.GetCookies(url).empty()); | 899 GURL url(StringPrintf("http://a%04d.thirtyfivedaysfromnow", i)); |
| 877 } else if (i < 701) { | 900 EXPECT_TRUE(cm.SetCookieWithCreationTime(url, "a=b", |
| 878 // Cookies should have gotten purged. | 901 Time::FromInternalValue(thirty_five_days_from_now_internal + i))); |
| 879 EXPECT_TRUE(cm.GetCookies(url).empty()); | |
| 880 } | |
| 881 } | 902 } |
| 903 EXPECT_EQ(3000U, cm.GetAllCookies().size()); // 500 "+ 7 days", |
| 904 // 1 "+ 31 days", |
| 905 // 2499 "+ 35 days" |
| 906 |
| 907 // If we manage to start garbage collection, then cookies should be eligible |
| 908 // once they're over the max age; they don't have to cover the overage factor. |
| 909 EXPECT_TRUE(cm.SetCookieWithCreationTime( |
| 910 GURL("http://test.sixtysixdaysfromnow"), "a=b", |
| 911 now + TimeDelta::FromDays(66))); |
| 912 EXPECT_EQ(2000U, cm.GetAllCookies().size()); // 1999 "+ 35 days", |
| 913 // 1 "+ 66 days" |
| 882 } | 914 } |
| 883 | 915 |
| 884 // Formerly NetUtilTest.CookieTest back when we used wininet's cookie handling. | 916 // Formerly NetUtilTest.CookieTest back when we used wininet's cookie handling. |
| 885 TEST(CookieMonsterTest, NetUtilCookieTest) { | 917 TEST(CookieMonsterTest, NetUtilCookieTest) { |
| 886 const GURL test_url("http://mojo.jojo.google.izzle/"); | 918 const GURL test_url("http://mojo.jojo.google.izzle/"); |
| 887 | 919 |
| 888 net::CookieMonster cm; | 920 net::CookieMonster cm; |
| 889 | 921 |
| 890 EXPECT_TRUE(cm.SetCookie(test_url, "foo=bar")); | 922 EXPECT_TRUE(cm.SetCookie(test_url, "foo=bar")); |
| 891 std::string value = cm.GetCookies(test_url); | 923 std::string value = cm.GetCookies(test_url); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 939 GURL foo_url("foo://host/path"); | 971 GURL foo_url("foo://host/path"); |
| 940 GURL http_url("http://host/path"); | 972 GURL http_url("http://host/path"); |
| 941 | 973 |
| 942 EXPECT_TRUE(cm.SetCookie(http_url, "x=1")); | 974 EXPECT_TRUE(cm.SetCookie(http_url, "x=1")); |
| 943 EXPECT_FALSE(cm.SetCookie(foo_url, "x=1")); | 975 EXPECT_FALSE(cm.SetCookie(foo_url, "x=1")); |
| 944 EXPECT_TRUE(cm_foo.SetCookie(foo_url, "x=1")); | 976 EXPECT_TRUE(cm_foo.SetCookie(foo_url, "x=1")); |
| 945 EXPECT_FALSE(cm_foo.SetCookie(http_url, "x=1")); | 977 EXPECT_FALSE(cm_foo.SetCookie(http_url, "x=1")); |
| 946 } | 978 } |
| 947 | 979 |
| 948 // TODO test overwrite cookie | 980 // TODO test overwrite cookie |
| OLD | NEW |