| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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" |
| 11 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
| 12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/stringprintf.h" |
| 14 #include "base/time.h" | 15 #include "base/time.h" |
| 15 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 16 #include "net/base/cookie_monster.h" | 17 #include "net/base/cookie_monster.h" |
| 17 #include "net/base/cookie_monster_store_test.h" // For CookieStore Mock | 18 #include "net/base/cookie_monster_store_test.h" // For CookieStore Mock |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 namespace net { | 21 namespace net { |
| 21 | 22 |
| 22 using base::Time; | 23 using base::Time; |
| 23 using base::TimeDelta; | 24 using base::TimeDelta; |
| (...skipping 1011 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1035 int domain_purge_cookies, | 1036 int domain_purge_cookies, |
| 1036 bool new_key_scheme) { | 1037 bool new_key_scheme) { |
| 1037 GURL url_google(kUrlGoogle); | 1038 GURL url_google(kUrlGoogle); |
| 1038 const int more_than_enough_cookies = | 1039 const int more_than_enough_cookies = |
| 1039 (domain_max_cookies + domain_purge_cookies) * 2; | 1040 (domain_max_cookies + domain_purge_cookies) * 2; |
| 1040 // Add a bunch of cookies on a single host, should purge them. | 1041 // Add a bunch of cookies on a single host, should purge them. |
| 1041 { | 1042 { |
| 1042 scoped_refptr<net::CookieMonster> cm(new net::CookieMonster(NULL, NULL)); | 1043 scoped_refptr<net::CookieMonster> cm(new net::CookieMonster(NULL, NULL)); |
| 1043 cm->SetKeyScheme(new_key_scheme); | 1044 cm->SetKeyScheme(new_key_scheme); |
| 1044 for (int i = 0; i < more_than_enough_cookies; ++i) { | 1045 for (int i = 0; i < more_than_enough_cookies; ++i) { |
| 1045 std::string cookie = StringPrintf("a%03d=b", i); | 1046 std::string cookie = base::StringPrintf("a%03d=b", i); |
| 1046 EXPECT_TRUE(cm->SetCookie(url_google, cookie)); | 1047 EXPECT_TRUE(cm->SetCookie(url_google, cookie)); |
| 1047 std::string cookies = cm->GetCookies(url_google); | 1048 std::string cookies = cm->GetCookies(url_google); |
| 1048 // Make sure we find it in the cookies. | 1049 // Make sure we find it in the cookies. |
| 1049 EXPECT_NE(cookies.find(cookie), std::string::npos); | 1050 EXPECT_NE(cookies.find(cookie), std::string::npos); |
| 1050 // Count the number of cookies. | 1051 // Count the number of cookies. |
| 1051 EXPECT_LE(CountInString(cookies, '='), domain_max_cookies); | 1052 EXPECT_LE(CountInString(cookies, '='), domain_max_cookies); |
| 1052 } | 1053 } |
| 1053 } | 1054 } |
| 1054 | 1055 |
| 1055 // Add a bunch of cookies on multiple hosts within a single eTLD. | 1056 // Add a bunch of cookies on multiple hosts within a single eTLD. |
| 1056 // Should keep at least kDomainMaxCookies - kDomainPurgeCookies | 1057 // Should keep at least kDomainMaxCookies - kDomainPurgeCookies |
| 1057 // between them. If we are using the (new) effective domain keying system | 1058 // between them. If we are using the (new) effective domain keying system |
| 1058 // we shouldn't go above kDomainMaxCookies for both together. | 1059 // we shouldn't go above kDomainMaxCookies for both together. |
| 1059 // If we're using the (old) domain keying system, each individual | 1060 // If we're using the (old) domain keying system, each individual |
| 1060 // domain shouldn't go above kDomainMaxCookies. | 1061 // domain shouldn't go above kDomainMaxCookies. |
| 1061 GURL url_google_specific(kUrlGoogleSpecific); | 1062 GURL url_google_specific(kUrlGoogleSpecific); |
| 1062 { | 1063 { |
| 1063 scoped_refptr<net::CookieMonster> cm(new net::CookieMonster(NULL, NULL)); | 1064 scoped_refptr<net::CookieMonster> cm(new net::CookieMonster(NULL, NULL)); |
| 1064 cm->SetKeyScheme(new_key_scheme); | 1065 cm->SetKeyScheme(new_key_scheme); |
| 1065 for (int i = 0; i < more_than_enough_cookies; ++i) { | 1066 for (int i = 0; i < more_than_enough_cookies; ++i) { |
| 1066 std::string cookie_general = StringPrintf("a%03d=b", i); | 1067 std::string cookie_general = base::StringPrintf("a%03d=b", i); |
| 1067 EXPECT_TRUE(cm->SetCookie(url_google, cookie_general)); | 1068 EXPECT_TRUE(cm->SetCookie(url_google, cookie_general)); |
| 1068 std::string cookie_specific = StringPrintf("c%03d=b", i); | 1069 std::string cookie_specific = base::StringPrintf("c%03d=b", i); |
| 1069 EXPECT_TRUE(cm->SetCookie(url_google_specific, cookie_specific)); | 1070 EXPECT_TRUE(cm->SetCookie(url_google_specific, cookie_specific)); |
| 1070 std::string cookies_general = cm->GetCookies(url_google); | 1071 std::string cookies_general = cm->GetCookies(url_google); |
| 1071 EXPECT_NE(cookies_general.find(cookie_general), std::string::npos); | 1072 EXPECT_NE(cookies_general.find(cookie_general), std::string::npos); |
| 1072 std::string cookies_specific = cm->GetCookies(url_google_specific); | 1073 std::string cookies_specific = cm->GetCookies(url_google_specific); |
| 1073 EXPECT_NE(cookies_specific.find(cookie_specific), std::string::npos); | 1074 EXPECT_NE(cookies_specific.find(cookie_specific), std::string::npos); |
| 1074 if (new_key_scheme) { | 1075 if (new_key_scheme) { |
| 1075 EXPECT_LE((CountInString(cookies_general, '=') + | 1076 EXPECT_LE((CountInString(cookies_general, '=') + |
| 1076 CountInString(cookies_specific, '=')), | 1077 CountInString(cookies_specific, '=')), |
| 1077 domain_max_cookies); | 1078 domain_max_cookies); |
| 1078 } else { | 1079 } else { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1110 CookieMonster::kDomainPurgeCookies, true); | 1111 CookieMonster::kDomainPurgeCookies, true); |
| 1111 } | 1112 } |
| 1112 | 1113 |
| 1113 TEST(CookieMonsterTest, TestTotalGarbageCollection) { | 1114 TEST(CookieMonsterTest, TestTotalGarbageCollection) { |
| 1114 scoped_refptr<net::CookieMonster> cm( | 1115 scoped_refptr<net::CookieMonster> cm( |
| 1115 new net::CookieMonster(NULL, NULL, kLastAccessThresholdMilliseconds)); | 1116 new net::CookieMonster(NULL, NULL, kLastAccessThresholdMilliseconds)); |
| 1116 | 1117 |
| 1117 // Add a bunch of cookies on a bunch of host, some should get purged. | 1118 // Add a bunch of cookies on a bunch of host, some should get purged. |
| 1118 const GURL sticky_cookie("http://a0000.izzle"); | 1119 const GURL sticky_cookie("http://a0000.izzle"); |
| 1119 for (int i = 0; i < 4000; ++i) { | 1120 for (int i = 0; i < 4000; ++i) { |
| 1120 GURL url(StringPrintf("http://a%04d.izzle", i)); | 1121 GURL url(base::StringPrintf("http://a%04d.izzle", i)); |
| 1121 EXPECT_TRUE(cm->SetCookie(url, "a=b")); | 1122 EXPECT_TRUE(cm->SetCookie(url, "a=b")); |
| 1122 EXPECT_EQ("a=b", cm->GetCookies(url)); | 1123 EXPECT_EQ("a=b", cm->GetCookies(url)); |
| 1123 | 1124 |
| 1124 // Keep touching the first cookie to ensure it's not purged (since it will | 1125 // Keep touching the first cookie to ensure it's not purged (since it will |
| 1125 // always have the most recent access time). | 1126 // always have the most recent access time). |
| 1126 if (!(i % 500)) { | 1127 if (!(i % 500)) { |
| 1127 // Ensure the timestamps will be different enough to update. | 1128 // Ensure the timestamps will be different enough to update. |
| 1128 PlatformThread::Sleep(kLastAccessThresholdMilliseconds + 20); | 1129 PlatformThread::Sleep(kLastAccessThresholdMilliseconds + 20); |
| 1129 EXPECT_EQ("a=b", cm->GetCookies(sticky_cookie)); | 1130 EXPECT_EQ("a=b", cm->GetCookies(sticky_cookie)); |
| 1130 } | 1131 } |
| 1131 } | 1132 } |
| 1132 | 1133 |
| 1133 // Check that cookies that still exist. | 1134 // Check that cookies that still exist. |
| 1134 for (int i = 0; i < 4000; ++i) { | 1135 for (int i = 0; i < 4000; ++i) { |
| 1135 GURL url(StringPrintf("http://a%04d.izzle", i)); | 1136 GURL url(base::StringPrintf("http://a%04d.izzle", i)); |
| 1136 if ((i == 0) || (i > 1001)) { | 1137 if ((i == 0) || (i > 1001)) { |
| 1137 // Cookies should still be around. | 1138 // Cookies should still be around. |
| 1138 EXPECT_FALSE(cm->GetCookies(url).empty()); | 1139 EXPECT_FALSE(cm->GetCookies(url).empty()); |
| 1139 } else if (i < 701) { | 1140 } else if (i < 701) { |
| 1140 // Cookies should have gotten purged. | 1141 // Cookies should have gotten purged. |
| 1141 EXPECT_TRUE(cm->GetCookies(url).empty()); | 1142 EXPECT_TRUE(cm->GetCookies(url).empty()); |
| 1142 } | 1143 } |
| 1143 } | 1144 } |
| 1144 } | 1145 } |
| 1145 | 1146 |
| (...skipping 874 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2020 EXPECT_EQ("d", cookies[i++].Name()); | 2021 EXPECT_EQ("d", cookies[i++].Name()); |
| 2021 EXPECT_EQ("a", cookies[i++].Name()); | 2022 EXPECT_EQ("a", cookies[i++].Name()); |
| 2022 EXPECT_EQ("e", cookies[i++].Name()); | 2023 EXPECT_EQ("e", cookies[i++].Name()); |
| 2023 EXPECT_EQ("g", cookies[i++].Name()); | 2024 EXPECT_EQ("g", cookies[i++].Name()); |
| 2024 EXPECT_EQ("b", cookies[i++].Name()); | 2025 EXPECT_EQ("b", cookies[i++].Name()); |
| 2025 EXPECT_EQ("c", cookies[i++].Name()); | 2026 EXPECT_EQ("c", cookies[i++].Name()); |
| 2026 } | 2027 } |
| 2027 } | 2028 } |
| 2028 | 2029 |
| 2029 } // namespace | 2030 } // namespace |
| OLD | NEW |