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 799 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
810 // The non-secure should overwrite the secure. | 810 // The non-secure should overwrite the secure. |
811 EXPECT_EQ("A=B", cm->GetCookies(url_google)); | 811 EXPECT_EQ("A=B", cm->GetCookies(url_google)); |
812 EXPECT_EQ("D=E; A=B", cm->GetCookies(url_google_secure)); | 812 EXPECT_EQ("D=E; A=B", cm->GetCookies(url_google_secure)); |
813 } | 813 } |
814 | 814 |
815 static Time GetFirstCookieAccessDate(net::CookieMonster* cm) { | 815 static Time GetFirstCookieAccessDate(net::CookieMonster* cm) { |
816 const net::CookieMonster::CookieList all_cookies(cm->GetAllCookies()); | 816 const net::CookieMonster::CookieList all_cookies(cm->GetAllCookies()); |
817 return all_cookies.front().second.LastAccessDate(); | 817 return all_cookies.front().second.LastAccessDate(); |
818 } | 818 } |
819 | 819 |
820 static const int kLastAccessThresholdSeconds = 1; | 820 static const int kLastAccessThresholdMilliseconds = 20; |
821 | 821 |
822 TEST(CookieMonsterTest, TestLastAccess) { | 822 TEST(CookieMonsterTest, TestLastAccess) { |
823 GURL url_google(kUrlGoogle); | 823 GURL url_google(kUrlGoogle); |
824 scoped_refptr<net::CookieMonster> cm( | 824 scoped_refptr<net::CookieMonster> cm( |
825 new net::CookieMonster(kLastAccessThresholdSeconds)); | 825 new net::CookieMonster(kLastAccessThresholdMilliseconds)); |
826 | 826 |
827 EXPECT_TRUE(cm->SetCookie(url_google, "A=B")); | 827 EXPECT_TRUE(cm->SetCookie(url_google, "A=B")); |
828 const Time last_access_date(GetFirstCookieAccessDate(cm)); | 828 const Time last_access_date(GetFirstCookieAccessDate(cm)); |
829 | 829 |
830 // Reading the cookie again immediately shouldn't update the access date, | 830 // Reading the cookie again immediately shouldn't update the access date, |
831 // since we're inside the threshold. | 831 // since we're inside the threshold. |
832 EXPECT_EQ("A=B", cm->GetCookies(url_google)); | 832 EXPECT_EQ("A=B", cm->GetCookies(url_google)); |
833 EXPECT_TRUE(last_access_date == GetFirstCookieAccessDate(cm)); | 833 EXPECT_TRUE(last_access_date == GetFirstCookieAccessDate(cm)); |
834 | 834 |
835 // Reading after a short wait should update the access date. | 835 // Reading after a short wait should update the access date. |
836 PlatformThread::Sleep(1500); | 836 PlatformThread::Sleep(kLastAccessThresholdMilliseconds + 10); |
Peter Kasting
2009/10/28 20:43:42
For safety due to Windows' low timer resolution, I
| |
837 EXPECT_EQ("A=B", cm->GetCookies(url_google)); | 837 EXPECT_EQ("A=B", cm->GetCookies(url_google)); |
838 EXPECT_FALSE(last_access_date == GetFirstCookieAccessDate(cm)); | 838 EXPECT_FALSE(last_access_date == GetFirstCookieAccessDate(cm)); |
839 } | 839 } |
840 | 840 |
841 static int CountInString(const std::string& str, char c) { | 841 static int CountInString(const std::string& str, char c) { |
842 int count = 0; | 842 int count = 0; |
843 for (std::string::const_iterator it = str.begin(); | 843 for (std::string::const_iterator it = str.begin(); |
844 it != str.end(); ++it) { | 844 it != str.end(); ++it) { |
845 if (*it == c) | 845 if (*it == c) |
846 ++count; | 846 ++count; |
(...skipping 11 matching lines...) Expand all Loading... | |
858 std::string cookies = cm->GetCookies(url_google); | 858 std::string cookies = cm->GetCookies(url_google); |
859 // Make sure we find it in the cookies. | 859 // Make sure we find it in the cookies. |
860 EXPECT_TRUE(cookies.find(cookie) != std::string::npos); | 860 EXPECT_TRUE(cookies.find(cookie) != std::string::npos); |
861 // Count the number of cookies. | 861 // Count the number of cookies. |
862 EXPECT_LE(CountInString(cookies, '='), 70); | 862 EXPECT_LE(CountInString(cookies, '='), 70); |
863 } | 863 } |
864 } | 864 } |
865 | 865 |
866 TEST(CookieMonsterTest, TestTotalGarbageCollection) { | 866 TEST(CookieMonsterTest, TestTotalGarbageCollection) { |
867 scoped_refptr<net::CookieMonster> cm( | 867 scoped_refptr<net::CookieMonster> cm( |
868 new net::CookieMonster(kLastAccessThresholdSeconds)); | 868 new net::CookieMonster(kLastAccessThresholdMilliseconds)); |
869 | 869 |
870 // Add a bunch of cookies on a bunch of host, some should get purged. | 870 // Add a bunch of cookies on a bunch of host, some should get purged. |
871 const GURL sticky_cookie("http://a0000.izzle"); | 871 const GURL sticky_cookie("http://a0000.izzle"); |
872 for (int i = 0; i < 4000; ++i) { | 872 for (int i = 0; i < 4000; ++i) { |
873 GURL url(StringPrintf("http://a%04d.izzle", i)); | 873 GURL url(StringPrintf("http://a%04d.izzle", i)); |
874 EXPECT_TRUE(cm->SetCookie(url, "a=b")); | 874 EXPECT_TRUE(cm->SetCookie(url, "a=b")); |
875 EXPECT_EQ("a=b", cm->GetCookies(url)); | 875 EXPECT_EQ("a=b", cm->GetCookies(url)); |
876 | 876 |
877 // Keep touching the first cookie to ensure it's not purged (since it will | 877 // Keep touching the first cookie to ensure it's not purged (since it will |
878 // always have the most recent access time). | 878 // always have the most recent access time). |
879 if (!(i % 500)) { | 879 if (!(i % 500)) { |
880 PlatformThread::Sleep(1500); // Ensure the timestamps will be different | 880 // Ensure the timestamps will be different enough to update. |
881 // enough to update. | 881 PlatformThread::Sleep(kLastAccessThresholdMilliseconds + 10); |
882 EXPECT_EQ("a=b", cm->GetCookies(sticky_cookie)); | 882 EXPECT_EQ("a=b", cm->GetCookies(sticky_cookie)); |
883 } | 883 } |
884 } | 884 } |
885 | 885 |
886 // Check that cookies that still exist. | 886 // Check that cookies that still exist. |
887 for (int i = 0; i < 4000; ++i) { | 887 for (int i = 0; i < 4000; ++i) { |
888 GURL url(StringPrintf("http://a%04d.izzle", i)); | 888 GURL url(StringPrintf("http://a%04d.izzle", i)); |
889 if ((i == 0) || (i > 1001)) { | 889 if ((i == 0) || (i > 1001)) { |
890 // Cookies should still be around. | 890 // Cookies should still be around. |
891 EXPECT_FALSE(cm->GetCookies(url).empty()); | 891 EXPECT_FALSE(cm->GetCookies(url).empty()); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
955 GURL foo_url("foo://host/path"); | 955 GURL foo_url("foo://host/path"); |
956 GURL http_url("http://host/path"); | 956 GURL http_url("http://host/path"); |
957 | 957 |
958 EXPECT_TRUE(cm->SetCookie(http_url, "x=1")); | 958 EXPECT_TRUE(cm->SetCookie(http_url, "x=1")); |
959 EXPECT_FALSE(cm->SetCookie(foo_url, "x=1")); | 959 EXPECT_FALSE(cm->SetCookie(foo_url, "x=1")); |
960 EXPECT_TRUE(cm_foo->SetCookie(foo_url, "x=1")); | 960 EXPECT_TRUE(cm_foo->SetCookie(foo_url, "x=1")); |
961 EXPECT_FALSE(cm_foo->SetCookie(http_url, "x=1")); | 961 EXPECT_FALSE(cm_foo->SetCookie(http_url, "x=1")); |
962 } | 962 } |
963 | 963 |
964 // TODO test overwrite cookie | 964 // TODO test overwrite cookie |
OLD | NEW |