| 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 "net/base/cookie_monster.h" | |
| 6 #include "base/perftimer.h" | 5 #include "base/perftimer.h" |
| 7 #include "base/string_util.h" | 6 #include "base/string_util.h" |
| 7 #include "base/stringprintf.h" |
| 8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
| 9 #include "net/base/cookie_monster.h" |
| 9 #include "net/base/cookie_monster_store_test.h" | 10 #include "net/base/cookie_monster_store_test.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 12 |
| 12 namespace { | 13 namespace { |
| 13 class ParsedCookieTest : public testing::Test { }; | 14 class ParsedCookieTest : public testing::Test { }; |
| 14 class CookieMonsterTest : public testing::Test { }; | 15 class CookieMonsterTest : public testing::Test { }; |
| 15 } | 16 } |
| 16 | 17 |
| 17 static const int kNumCookies = 20000; | 18 static const int kNumCookies = 20000; |
| 18 static const char kCookieLine[] = "A = \"b=;\\\"\" ;secure;;;"; | 19 static const char kCookieLine[] = "A = \"b=;\\\"\" ;secure;;;"; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 39 } | 40 } |
| 40 timer.Done(); | 41 timer.Done(); |
| 41 } | 42 } |
| 42 | 43 |
| 43 static const GURL kUrlGoogle("http://www.google.izzle"); | 44 static const GURL kUrlGoogle("http://www.google.izzle"); |
| 44 | 45 |
| 45 TEST(CookieMonsterTest, TestAddCookiesOnSingleHost) { | 46 TEST(CookieMonsterTest, TestAddCookiesOnSingleHost) { |
| 46 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 47 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 47 std::vector<std::string> cookies; | 48 std::vector<std::string> cookies; |
| 48 for (int i = 0; i < kNumCookies; i++) { | 49 for (int i = 0; i < kNumCookies; i++) { |
| 49 cookies.push_back(StringPrintf("a%03d=b", i)); | 50 cookies.push_back(base::StringPrintf("a%03d=b", i)); |
| 50 } | 51 } |
| 51 | 52 |
| 52 // Add a bunch of cookies on a single host | 53 // Add a bunch of cookies on a single host |
| 53 PerfTimeLogger timer("Cookie_monster_add_single_host"); | 54 PerfTimeLogger timer("Cookie_monster_add_single_host"); |
| 54 for (std::vector<std::string>::const_iterator it = cookies.begin(); | 55 for (std::vector<std::string>::const_iterator it = cookies.begin(); |
| 55 it != cookies.end(); ++it) { | 56 it != cookies.end(); ++it) { |
| 56 EXPECT_TRUE(cm->SetCookie(kUrlGoogle, *it)); | 57 EXPECT_TRUE(cm->SetCookie(kUrlGoogle, *it)); |
| 57 } | 58 } |
| 58 timer.Done(); | 59 timer.Done(); |
| 59 | 60 |
| 60 PerfTimeLogger timer2("Cookie_monster_query_single_host"); | 61 PerfTimeLogger timer2("Cookie_monster_query_single_host"); |
| 61 for (std::vector<std::string>::const_iterator it = cookies.begin(); | 62 for (std::vector<std::string>::const_iterator it = cookies.begin(); |
| 62 it != cookies.end(); ++it) { | 63 it != cookies.end(); ++it) { |
| 63 cm->GetCookies(kUrlGoogle); | 64 cm->GetCookies(kUrlGoogle); |
| 64 } | 65 } |
| 65 timer2.Done(); | 66 timer2.Done(); |
| 66 | 67 |
| 67 PerfTimeLogger timer3("Cookie_monster_deleteall_single_host"); | 68 PerfTimeLogger timer3("Cookie_monster_deleteall_single_host"); |
| 68 cm->DeleteAll(false); | 69 cm->DeleteAll(false); |
| 69 timer3.Done(); | 70 timer3.Done(); |
| 70 } | 71 } |
| 71 | 72 |
| 72 TEST(CookieMonsterTest, TestAddCookieOnManyHosts) { | 73 TEST(CookieMonsterTest, TestAddCookieOnManyHosts) { |
| 73 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 74 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 74 std::string cookie(kCookieLine); | 75 std::string cookie(kCookieLine); |
| 75 std::vector<GURL> gurls; // just wanna have ffffuunnn | 76 std::vector<GURL> gurls; // just wanna have ffffuunnn |
| 76 for (int i = 0; i < kNumCookies; ++i) { | 77 for (int i = 0; i < kNumCookies; ++i) { |
| 77 gurls.push_back(GURL(StringPrintf("http://a%04d.izzle", i))); | 78 gurls.push_back(GURL(base::StringPrintf("http://a%04d.izzle", i))); |
| 78 } | 79 } |
| 79 | 80 |
| 80 // Add a cookie on a bunch of host | 81 // Add a cookie on a bunch of host |
| 81 PerfTimeLogger timer("Cookie_monster_add_many_hosts"); | 82 PerfTimeLogger timer("Cookie_monster_add_many_hosts"); |
| 82 for (std::vector<GURL>::const_iterator it = gurls.begin(); | 83 for (std::vector<GURL>::const_iterator it = gurls.begin(); |
| 83 it != gurls.end(); ++it) { | 84 it != gurls.end(); ++it) { |
| 84 EXPECT_TRUE(cm->SetCookie(*it, cookie)); | 85 EXPECT_TRUE(cm->SetCookie(*it, cookie)); |
| 85 } | 86 } |
| 86 timer.Done(); | 87 timer.Done(); |
| 87 | 88 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 } | 130 } |
| 130 } | 131 } |
| 131 } | 132 } |
| 132 } | 133 } |
| 133 | 134 |
| 134 | 135 |
| 135 EXPECT_EQ(31u, domain_list.size()); | 136 EXPECT_EQ(31u, domain_list.size()); |
| 136 for (std::vector<std::string>::const_iterator it = domain_list.begin(); | 137 for (std::vector<std::string>::const_iterator it = domain_list.begin(); |
| 137 it != domain_list.end(); it++) { | 138 it != domain_list.end(); it++) { |
| 138 GURL gurl("https://" + *it + "/"); | 139 GURL gurl("https://" + *it + "/"); |
| 139 const std::string cookie = StringPrintf(domain_cookie_format_tree, | 140 const std::string cookie = base::StringPrintf(domain_cookie_format_tree, |
| 140 it->c_str()); | 141 it->c_str()); |
| 141 EXPECT_TRUE(cm->SetCookie(gurl, cookie)); | 142 EXPECT_TRUE(cm->SetCookie(gurl, cookie)); |
| 142 } | 143 } |
| 143 EXPECT_EQ(31u, cm->GetAllCookies().size()); | 144 EXPECT_EQ(31u, cm->GetAllCookies().size()); |
| 144 | 145 |
| 145 GURL probe_gurl("https://b.a.b.a.top.com/"); | 146 GURL probe_gurl("https://b.a.b.a.top.com/"); |
| 146 std::string cookie_line; | 147 std::string cookie_line; |
| 147 cookie_line = cm->GetCookies(probe_gurl); | 148 cookie_line = cm->GetCookies(probe_gurl); |
| 148 EXPECT_EQ(5, CountInString(cookie_line, '=')) << "Cookie line: " | 149 EXPECT_EQ(5, CountInString(cookie_line, '=')) << "Cookie line: " |
| 149 << cookie_line; | 150 << cookie_line; |
| 150 PerfTimeLogger timer("Cookie_monster_query_domain_tree"); | 151 PerfTimeLogger timer("Cookie_monster_query_domain_tree"); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 171 domain_list.push_back("b.a.top.com"); | 172 domain_list.push_back("b.a.top.com"); |
| 172 domain_list.push_back("a.b.a.top.com"); | 173 domain_list.push_back("a.b.a.top.com"); |
| 173 domain_list.push_back("b.a.b.a.top.com"); | 174 domain_list.push_back("b.a.b.a.top.com"); |
| 174 EXPECT_EQ(4u, domain_list.size()); | 175 EXPECT_EQ(4u, domain_list.size()); |
| 175 | 176 |
| 176 const char* domain_cookie_format_line = "a%03d=b; domain=%s"; | 177 const char* domain_cookie_format_line = "a%03d=b; domain=%s"; |
| 177 for (int i = 0; i < 8; i++) { | 178 for (int i = 0; i < 8; i++) { |
| 178 for (std::vector<std::string>::const_iterator it = domain_list.begin(); | 179 for (std::vector<std::string>::const_iterator it = domain_list.begin(); |
| 179 it != domain_list.end(); it++) { | 180 it != domain_list.end(); it++) { |
| 180 GURL gurl("https://" + *it + "/"); | 181 GURL gurl("https://" + *it + "/"); |
| 181 const std::string cookie = StringPrintf(domain_cookie_format_line, | 182 const std::string cookie = base::StringPrintf(domain_cookie_format_line, |
| 182 i, it->c_str()); | 183 i, it->c_str()); |
| 183 EXPECT_TRUE(cm->SetCookie(gurl, cookie)); | 184 EXPECT_TRUE(cm->SetCookie(gurl, cookie)); |
| 184 } | 185 } |
| 185 } | 186 } |
| 186 EXPECT_EQ(32u, cm->GetAllCookies().size()); | 187 EXPECT_EQ(32u, cm->GetAllCookies().size()); |
| 187 | 188 |
| 188 cookie_line = cm->GetCookies(probe_gurl); | 189 cookie_line = cm->GetCookies(probe_gurl); |
| 189 EXPECT_EQ(32, CountInString(cookie_line, '=')); | 190 EXPECT_EQ(32, CountInString(cookie_line, '=')); |
| 190 PerfTimeLogger timer2("Cookie_monster_query_domain_line"); | 191 PerfTimeLogger timer2("Cookie_monster_query_domain_line"); |
| 191 for (int i = 0; i < kNumCookies; i++) { | 192 for (int i = 0; i < kNumCookies; i++) { |
| 192 cm->GetCookies(probe_gurl); | 193 cm->GetCookies(probe_gurl); |
| 193 } | 194 } |
| 194 timer2.Done(); | 195 timer2.Done(); |
| 195 } | 196 } |
| 196 | 197 |
| 197 TEST(CookieMonsterTest, TestImport) { | 198 TEST(CookieMonsterTest, TestImport) { |
| 198 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore); | 199 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore); |
| 199 std::vector<CookieMonster::CanonicalCookie*> initial_cookies; | 200 std::vector<CookieMonster::CanonicalCookie*> initial_cookies; |
| 200 | 201 |
| 201 // We want to setup a fairly large backing store, with 300 domains of 50 | 202 // We want to setup a fairly large backing store, with 300 domains of 50 |
| 202 // cookies each. Creation times must be unique. | 203 // cookies each. Creation times must be unique. |
| 203 int64 time_tick(base::Time::Now().ToInternalValue()); | 204 int64 time_tick(base::Time::Now().ToInternalValue()); |
| 204 | 205 |
| 205 for (int domain_num = 0; domain_num < 300; domain_num++) { | 206 for (int domain_num = 0; domain_num < 300; domain_num++) { |
| 206 std::string domain_name(StringPrintf(".Domain_%d.com", domain_num)); | 207 std::string domain_name(base::StringPrintf(".Domain_%d.com", domain_num)); |
| 207 std::string gurl("www" + domain_name); | 208 std::string gurl("www" + domain_name); |
| 208 for (int cookie_num = 0; cookie_num < 50; cookie_num++) { | 209 for (int cookie_num = 0; cookie_num < 50; cookie_num++) { |
| 209 std::string cookie_line(StringPrintf("Cookie_%d=1; Path=/", cookie_num)); | 210 std::string cookie_line(base::StringPrintf("Cookie_%d=1; Path=/", |
| 211 cookie_num)); |
| 210 AddCookieToList(gurl, cookie_line, | 212 AddCookieToList(gurl, cookie_line, |
| 211 base::Time::FromInternalValue(time_tick++), | 213 base::Time::FromInternalValue(time_tick++), |
| 212 &initial_cookies); | 214 &initial_cookies); |
| 213 } | 215 } |
| 214 } | 216 } |
| 215 | 217 |
| 216 store->SetLoadExpectation(true, initial_cookies); | 218 store->SetLoadExpectation(true, initial_cookies); |
| 217 | 219 |
| 218 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); | 220 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); |
| 219 | 221 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 230 | 232 |
| 231 TEST(CookieMonsterTest, TestGetKey) { | 233 TEST(CookieMonsterTest, TestGetKey) { |
| 232 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 234 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 233 PerfTimeLogger timer("Cookie_monster_get_key"); | 235 PerfTimeLogger timer("Cookie_monster_get_key"); |
| 234 for (int i = 0; i < kNumCookies; i++) | 236 for (int i = 0; i < kNumCookies; i++) |
| 235 cm->GetKey("www.google.com"); | 237 cm->GetKey("www.google.com"); |
| 236 timer.Done(); | 238 timer.Done(); |
| 237 } | 239 } |
| 238 | 240 |
| 239 } // namespace | 241 } // namespace |
| OLD | NEW |