OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "net/base/cookie_monster.h" | 7 #include "base/bind.h" |
8 | 8 #include "base/message_loop.h" |
9 #include "base/perftimer.h" | 9 #include "base/perftimer.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
13 #include "net/base/cookie_monster.h" | 13 #include "net/base/cookie_monster.h" |
14 #include "net/base/cookie_monster_store_test.h" | 14 #include "net/base/cookie_monster_store_test.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 class ParsedCookieTest : public testing::Test { }; | 18 class CookieMonsterTest : public testing::Test { |
19 class CookieMonsterTest : public testing::Test { }; | 19 public: |
| 20 CookieMonsterTest() : message_loop_(new MessageLoopForIO()) {} |
| 21 |
| 22 private: |
| 23 scoped_ptr<MessageLoop> message_loop_; |
| 24 }; |
20 } | 25 } |
21 | 26 |
22 static const int kNumCookies = 20000; | 27 static const int kNumCookies = 20000; |
23 static const char kCookieLine[] = "A = \"b=;\\\"\" ;secure;;;"; | 28 static const char kCookieLine[] = "A = \"b=;\\\"\" ;secure;;;"; |
24 | 29 |
25 namespace net { | 30 namespace net { |
26 | 31 |
27 TEST(ParsedCookieTest, TestParseCookies) { | 32 TEST(ParsedCookieTest, TestParseCookies) { |
28 std::string cookie(kCookieLine); | 33 std::string cookie(kCookieLine); |
29 PerfTimeLogger timer("Parsed_cookie_parse_cookies"); | 34 PerfTimeLogger timer("Parsed_cookie_parse_cookies"); |
(...skipping 10 matching lines...) Expand all Loading... |
40 PerfTimeLogger timer("Parsed_cookie_parse_big_cookies"); | 45 PerfTimeLogger timer("Parsed_cookie_parse_big_cookies"); |
41 for (int i = 0; i < kNumCookies; ++i) { | 46 for (int i = 0; i < kNumCookies; ++i) { |
42 CookieMonster::ParsedCookie pc(cookie); | 47 CookieMonster::ParsedCookie pc(cookie); |
43 EXPECT_TRUE(pc.IsValid()); | 48 EXPECT_TRUE(pc.IsValid()); |
44 } | 49 } |
45 timer.Done(); | 50 timer.Done(); |
46 } | 51 } |
47 | 52 |
48 static const GURL kUrlGoogle("http://www.google.izzle"); | 53 static const GURL kUrlGoogle("http://www.google.izzle"); |
49 | 54 |
50 TEST(CookieMonsterTest, TestAddCookiesOnSingleHost) { | 55 class BaseCallback { |
| 56 public: |
| 57 BaseCallback() : has_run_(false) {} |
| 58 |
| 59 protected: |
| 60 void WaitForCallback() { |
| 61 // Note that the performance tests currently all operate on a loaded cookie |
| 62 // store (or, more precisely, one that has no backing persistent store). |
| 63 // Therefore, callbacks will actually always complete synchronously. If the |
| 64 // tests get more advanced we need to add other means of signaling |
| 65 // completion. |
| 66 EXPECT_TRUE(has_run_); |
| 67 has_run_ = false; |
| 68 } |
| 69 |
| 70 void Run() { |
| 71 has_run_ = true; |
| 72 } |
| 73 |
| 74 bool has_run_; |
| 75 }; |
| 76 |
| 77 |
| 78 class SetCookieCallback : public BaseCallback { |
| 79 public: |
| 80 void SetCookie( |
| 81 CookieMonster* cm, const GURL& gurl, const std::string& cookie) { |
| 82 cm->SetCookieWithOptionsAsync(gurl, cookie, options_, base::Bind( |
| 83 &SetCookieCallback::Run, base::Unretained(this))); |
| 84 WaitForCallback(); |
| 85 } |
| 86 private: |
| 87 void Run(bool success) { |
| 88 EXPECT_TRUE(success); |
| 89 BaseCallback::Run(); |
| 90 } |
| 91 net::CookieOptions options_; |
| 92 }; |
| 93 |
| 94 class GetCookiesCallback : public BaseCallback { |
| 95 public: |
| 96 const std::string& GetCookies(CookieMonster* cm, const GURL& gurl) { |
| 97 cm->GetCookiesWithOptionsAsync(gurl, options_, base::Bind( |
| 98 &GetCookiesCallback::Run, base::Unretained(this))); |
| 99 WaitForCallback(); |
| 100 return cookies_; |
| 101 } |
| 102 |
| 103 private: |
| 104 void Run(const std::string& cookies) { |
| 105 cookies_ = cookies; |
| 106 BaseCallback::Run(); |
| 107 } |
| 108 std::string cookies_; |
| 109 net::CookieOptions options_; |
| 110 }; |
| 111 |
| 112 class GetCookiesWithInfoCallback : public BaseCallback { |
| 113 public: |
| 114 const std::string& GetCookiesWithInfo(CookieMonster* cm, const GURL& gurl) { |
| 115 cm->GetCookiesWithInfoAsync(gurl, options_, base::Bind( |
| 116 &GetCookiesWithInfoCallback::Run, |
| 117 base::Unretained(this))); |
| 118 WaitForCallback(); |
| 119 return cookies_; |
| 120 } |
| 121 |
| 122 private: |
| 123 void Run( |
| 124 const std::string& cookie_line, |
| 125 const std::vector<CookieStore::CookieInfo>& cookie_infos) { |
| 126 cookies_ = cookie_line; |
| 127 BaseCallback::Run(); |
| 128 } |
| 129 |
| 130 std::string cookies_; |
| 131 net::CookieOptions options_; |
| 132 }; |
| 133 |
| 134 |
| 135 TEST_F(CookieMonsterTest, TestAddCookiesOnSingleHost) { |
51 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 136 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
52 std::vector<std::string> cookies; | 137 std::vector<std::string> cookies; |
53 for (int i = 0; i < kNumCookies; i++) { | 138 for (int i = 0; i < kNumCookies; i++) { |
54 cookies.push_back(base::StringPrintf("a%03d=b", i)); | 139 cookies.push_back(base::StringPrintf("a%03d=b", i)); |
55 } | 140 } |
56 | 141 |
| 142 SetCookieCallback setCookieCallback; |
| 143 |
57 // Add a bunch of cookies on a single host | 144 // Add a bunch of cookies on a single host |
58 PerfTimeLogger timer("Cookie_monster_add_single_host"); | 145 PerfTimeLogger timer("Cookie_monster_add_single_host"); |
| 146 |
59 for (std::vector<std::string>::const_iterator it = cookies.begin(); | 147 for (std::vector<std::string>::const_iterator it = cookies.begin(); |
60 it != cookies.end(); ++it) { | 148 it != cookies.end(); ++it) { |
61 EXPECT_TRUE(cm->SetCookie(kUrlGoogle, *it)); | 149 setCookieCallback.SetCookie(cm, kUrlGoogle, *it); |
62 } | 150 } |
63 timer.Done(); | 151 timer.Done(); |
64 | 152 |
| 153 GetCookiesCallback getCookiesCallback; |
| 154 |
65 PerfTimeLogger timer2("Cookie_monster_query_single_host"); | 155 PerfTimeLogger timer2("Cookie_monster_query_single_host"); |
66 for (std::vector<std::string>::const_iterator it = cookies.begin(); | 156 for (std::vector<std::string>::const_iterator it = cookies.begin(); |
67 it != cookies.end(); ++it) { | 157 it != cookies.end(); ++it) { |
68 cm->GetCookies(kUrlGoogle); | 158 getCookiesCallback.GetCookies(cm, kUrlGoogle); |
69 } | 159 } |
70 timer2.Done(); | 160 timer2.Done(); |
71 | 161 |
72 PerfTimeLogger timer3("Cookie_monster_deleteall_single_host"); | 162 PerfTimeLogger timer3("Cookie_monster_deleteall_single_host"); |
73 cm->DeleteAll(false); | 163 cm->DeleteAllAsync(CookieMonster::DeleteCallback()); |
| 164 MessageLoop::current()->RunAllPending(); |
74 timer3.Done(); | 165 timer3.Done(); |
75 } | 166 } |
76 | 167 |
77 TEST(CookieMonsterTest, TestAddCookieOnManyHosts) { | 168 TEST_F(CookieMonsterTest, TestAddCookieOnManyHosts) { |
78 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 169 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
79 std::string cookie(kCookieLine); | 170 std::string cookie(kCookieLine); |
80 std::vector<GURL> gurls; // just wanna have ffffuunnn | 171 std::vector<GURL> gurls; // just wanna have ffffuunnn |
81 for (int i = 0; i < kNumCookies; ++i) { | 172 for (int i = 0; i < kNumCookies; ++i) { |
82 gurls.push_back(GURL(base::StringPrintf("http://a%04d.izzle", i))); | 173 gurls.push_back(GURL(base::StringPrintf("https://a%04d.izzle", i))); |
83 } | 174 } |
84 | 175 |
| 176 SetCookieCallback setCookieCallback; |
| 177 |
85 // Add a cookie on a bunch of host | 178 // Add a cookie on a bunch of host |
86 PerfTimeLogger timer("Cookie_monster_add_many_hosts"); | 179 PerfTimeLogger timer("Cookie_monster_add_many_hosts"); |
87 for (std::vector<GURL>::const_iterator it = gurls.begin(); | 180 for (std::vector<GURL>::const_iterator it = gurls.begin(); |
88 it != gurls.end(); ++it) { | 181 it != gurls.end(); ++it) { |
89 EXPECT_TRUE(cm->SetCookie(*it, cookie)); | 182 setCookieCallback.SetCookie(cm, *it, cookie); |
90 } | 183 } |
91 timer.Done(); | 184 timer.Done(); |
92 | 185 |
| 186 GetCookiesCallback getCookiesCallback; |
| 187 |
93 PerfTimeLogger timer2("Cookie_monster_query_many_hosts"); | 188 PerfTimeLogger timer2("Cookie_monster_query_many_hosts"); |
94 for (std::vector<GURL>::const_iterator it = gurls.begin(); | 189 for (std::vector<GURL>::const_iterator it = gurls.begin(); |
95 it != gurls.end(); ++it) { | 190 it != gurls.end(); ++it) { |
96 cm->GetCookies(*it); | 191 getCookiesCallback.GetCookies(cm, *it); |
97 } | 192 } |
98 timer2.Done(); | 193 timer2.Done(); |
99 | 194 |
100 PerfTimeLogger timer3("Cookie_monster_deleteall_many_hosts"); | 195 PerfTimeLogger timer3("Cookie_monster_deleteall_many_hosts"); |
101 cm->DeleteAll(false); | 196 cm->DeleteAllAsync(CookieMonster::DeleteCallback()); |
| 197 MessageLoop::current()->RunAllPending(); |
102 timer3.Done(); | 198 timer3.Done(); |
103 } | 199 } |
104 | 200 |
105 TEST(CookieMonsterTest, TestGetCookiesWithOptions) { | 201 TEST_F(CookieMonsterTest, TestGetCookiesWithInfo) { |
106 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 202 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
107 std::string cookie(kCookieLine); | 203 |
108 std::vector<GURL> gurls; | 204 std::vector<GURL> gurls; |
109 for (int i = 0; i < kNumCookies; ++i) | 205 for (int i = 0; i < kNumCookies; ++i) |
110 gurls.push_back(GURL(base::StringPrintf("http://a%04d.izzle", i))); | 206 gurls.push_back(GURL(base::StringPrintf("https://a%04d.izzle", i))); |
| 207 |
| 208 SetCookieCallback setCookieCallback; |
111 | 209 |
112 for (std::vector<GURL>::const_iterator it = gurls.begin(); | 210 for (std::vector<GURL>::const_iterator it = gurls.begin(); |
113 it != gurls.end(); ++it) { | 211 it != gurls.end(); ++it) { |
114 EXPECT_TRUE(cm->SetCookie(*it, cookie)); | 212 setCookieCallback.SetCookie(cm, *it, kCookieLine); |
115 } | 213 } |
116 | 214 |
| 215 GetCookiesWithInfoCallback getCookiesCallback; |
| 216 |
117 PerfTimeLogger timer("Cookie_monster_get_cookie_info"); | 217 PerfTimeLogger timer("Cookie_monster_get_cookie_info"); |
118 for (std::vector<GURL>::const_iterator it = gurls.begin(); | 218 for (std::vector<GURL>::const_iterator it = gurls.begin(); |
119 it != gurls.end(); ++it) { | 219 it != gurls.end(); ++it) { |
120 CookieOptions options; | 220 getCookiesCallback.GetCookiesWithInfo(cm, *it); |
121 std::string cookie_line; | |
122 cookie_line = cm->GetCookiesWithOptions(*it, options); | |
123 } | 221 } |
124 timer.Done(); | 222 timer.Done(); |
125 } | 223 } |
126 | |
127 TEST(CookieMonsterTest, TestGetCookiesWithInfo) { | |
128 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | |
129 std::string cookie(kCookieLine); | |
130 std::vector<GURL> gurls; | |
131 for (int i = 0; i < kNumCookies; ++i) | |
132 gurls.push_back(GURL(base::StringPrintf("http://a%04d.izzle", i))); | |
133 | |
134 for (std::vector<GURL>::const_iterator it = gurls.begin(); | |
135 it != gurls.end(); ++it) { | |
136 EXPECT_TRUE(cm->SetCookie(*it, cookie)); | |
137 } | |
138 | |
139 PerfTimeLogger timer("Cookie_monster_get_cookie_info"); | |
140 for (std::vector<GURL>::const_iterator it = gurls.begin(); | |
141 it != gurls.end(); ++it) { | |
142 CookieOptions options; | |
143 std::string cookie_line; | |
144 std::vector<CookieStore::CookieInfo> cookie_infos; | |
145 cm->GetCookiesWithInfo(*it, options, &cookie_line, &cookie_infos); | |
146 } | |
147 timer.Done(); | |
148 } | |
149 | 224 |
150 static int CountInString(const std::string& str, char c) { | 225 static int CountInString(const std::string& str, char c) { |
151 return std::count(str.begin(), str.end(), c); | 226 return std::count(str.begin(), str.end(), c); |
152 } | 227 } |
153 | 228 |
154 TEST(CookieMonsterTest, TestDomainTree) { | 229 TEST_F(CookieMonsterTest, TestDomainTree) { |
155 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 230 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 231 GetCookiesCallback getCookiesCallback; |
| 232 SetCookieCallback setCookieCallback; |
156 const char* domain_cookie_format_tree = "a=b; domain=%s"; | 233 const char* domain_cookie_format_tree = "a=b; domain=%s"; |
157 const std::string domain_base("top.com"); | 234 const std::string domain_base("top.com"); |
158 | 235 |
159 std::vector<std::string> domain_list; | 236 std::vector<std::string> domain_list; |
160 | 237 |
161 // Create a balanced binary tree of domains on which the cookie is set. | 238 // Create a balanced binary tree of domains on which the cookie is set. |
162 domain_list.push_back(domain_base); | 239 domain_list.push_back(domain_base); |
163 for (int i1 = 0; i1 < 2; i1++) { | 240 for (int i1 = 0; i1 < 2; i1++) { |
164 std::string domain_base_1((i1 ? "a." : "b.") + domain_base); | 241 std::string domain_base_1((i1 ? "a." : "b.") + domain_base); |
165 EXPECT_EQ("top.com", cm->GetKey(domain_base_1)); | 242 EXPECT_EQ("top.com", cm->GetKey(domain_base_1)); |
(...skipping 15 matching lines...) Expand all Loading... |
181 } | 258 } |
182 } | 259 } |
183 | 260 |
184 | 261 |
185 EXPECT_EQ(31u, domain_list.size()); | 262 EXPECT_EQ(31u, domain_list.size()); |
186 for (std::vector<std::string>::const_iterator it = domain_list.begin(); | 263 for (std::vector<std::string>::const_iterator it = domain_list.begin(); |
187 it != domain_list.end(); it++) { | 264 it != domain_list.end(); it++) { |
188 GURL gurl("https://" + *it + "/"); | 265 GURL gurl("https://" + *it + "/"); |
189 const std::string cookie = base::StringPrintf(domain_cookie_format_tree, | 266 const std::string cookie = base::StringPrintf(domain_cookie_format_tree, |
190 it->c_str()); | 267 it->c_str()); |
191 EXPECT_TRUE(cm->SetCookie(gurl, cookie)); | 268 setCookieCallback.SetCookie(cm, gurl, cookie); |
192 } | 269 } |
193 EXPECT_EQ(31u, cm->GetAllCookies().size()); | 270 EXPECT_EQ(31u, cm->GetAllCookies().size()); |
194 | 271 |
195 GURL probe_gurl("https://b.a.b.a.top.com/"); | 272 GURL probe_gurl("https://b.a.b.a.top.com/"); |
196 std::string cookie_line; | 273 std::string cookie_line = getCookiesCallback.GetCookies(cm, probe_gurl); |
197 cookie_line = cm->GetCookies(probe_gurl); | 274 EXPECT_EQ(5, CountInString(cookie_line, '=')) << "Cookie line: " << |
198 EXPECT_EQ(5, CountInString(cookie_line, '=')) << "Cookie line: " | 275 cookie_line; |
199 << cookie_line; | |
200 PerfTimeLogger timer("Cookie_monster_query_domain_tree"); | 276 PerfTimeLogger timer("Cookie_monster_query_domain_tree"); |
201 for (int i = 0; i < kNumCookies; i++) { | 277 for (int i = 0; i < kNumCookies; i++) { |
202 cm->GetCookies(probe_gurl); | 278 getCookiesCallback.GetCookies(cm, probe_gurl); |
203 } | 279 } |
204 timer.Done(); | 280 timer.Done(); |
205 | |
206 } | 281 } |
207 | 282 |
208 TEST(CookieMonsterTest, TestDomainLine) { | 283 TEST_F(CookieMonsterTest, TestDomainLine) { |
209 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 284 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 285 SetCookieCallback setCookieCallback; |
| 286 GetCookiesCallback getCookiesCallback; |
210 std::vector<std::string> domain_list; | 287 std::vector<std::string> domain_list; |
211 GURL probe_gurl("https://b.a.b.a.top.com/"); | 288 GURL probe_gurl("https://b.a.b.a.top.com/"); |
212 std::string cookie_line; | 289 std::string cookie_line; |
213 | 290 |
214 // Create a line of 32 domain cookies such that all cookies stored | 291 // Create a line of 32 domain cookies such that all cookies stored |
215 // by effective TLD+1 will apply to probe GURL. | 292 // by effective TLD+1 will apply to probe GURL. |
216 // (TLD + 1 is the level above .com/org/net/etc, e.g. "top.com" | 293 // (TLD + 1 is the level above .com/org/net/etc, e.g. "top.com" |
217 // or "google.com". "Effective" is added to include sites like | 294 // or "google.com". "Effective" is added to include sites like |
218 // bbc.co.uk, where the effetive TLD+1 is more than one level | 295 // bbc.co.uk, where the effetive TLD+1 is more than one level |
219 // below the top level.) | 296 // below the top level.) |
220 domain_list.push_back("a.top.com"); | 297 domain_list.push_back("a.top.com"); |
221 domain_list.push_back("b.a.top.com"); | 298 domain_list.push_back("b.a.top.com"); |
222 domain_list.push_back("a.b.a.top.com"); | 299 domain_list.push_back("a.b.a.top.com"); |
223 domain_list.push_back("b.a.b.a.top.com"); | 300 domain_list.push_back("b.a.b.a.top.com"); |
224 EXPECT_EQ(4u, domain_list.size()); | 301 EXPECT_EQ(4u, domain_list.size()); |
225 | 302 |
226 const char* domain_cookie_format_line = "a%03d=b; domain=%s"; | 303 const char* domain_cookie_format_line = "a%03d=b; domain=%s"; |
227 for (int i = 0; i < 8; i++) { | 304 for (int i = 0; i < 8; i++) { |
228 for (std::vector<std::string>::const_iterator it = domain_list.begin(); | 305 for (std::vector<std::string>::const_iterator it = domain_list.begin(); |
229 it != domain_list.end(); it++) { | 306 it != domain_list.end(); it++) { |
230 GURL gurl("https://" + *it + "/"); | 307 GURL gurl("https://" + *it + "/"); |
231 const std::string cookie = base::StringPrintf(domain_cookie_format_line, | 308 const std::string cookie = base::StringPrintf(domain_cookie_format_line, |
232 i, it->c_str()); | 309 i, it->c_str()); |
233 EXPECT_TRUE(cm->SetCookie(gurl, cookie)); | 310 setCookieCallback.SetCookie(cm, gurl, cookie); |
234 } | 311 } |
235 } | 312 } |
236 EXPECT_EQ(32u, cm->GetAllCookies().size()); | |
237 | 313 |
238 cookie_line = cm->GetCookies(probe_gurl); | 314 cookie_line = getCookiesCallback.GetCookies(cm, probe_gurl); |
239 EXPECT_EQ(32, CountInString(cookie_line, '=')); | 315 EXPECT_EQ(32, CountInString(cookie_line, '=')); |
240 PerfTimeLogger timer2("Cookie_monster_query_domain_line"); | 316 PerfTimeLogger timer2("Cookie_monster_query_domain_line"); |
241 for (int i = 0; i < kNumCookies; i++) { | 317 for (int i = 0; i < kNumCookies; i++) { |
242 cm->GetCookies(probe_gurl); | 318 getCookiesCallback.GetCookies(cm, probe_gurl); |
243 } | 319 } |
244 timer2.Done(); | 320 timer2.Done(); |
245 } | 321 } |
246 | 322 |
247 TEST(CookieMonsterTest, TestImport) { | 323 TEST_F(CookieMonsterTest, TestImport) { |
248 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore); | 324 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore); |
249 std::vector<CookieMonster::CanonicalCookie*> initial_cookies; | 325 std::vector<CookieMonster::CanonicalCookie*> initial_cookies; |
| 326 GetCookiesCallback getCookiesCallback; |
250 | 327 |
251 // We want to setup a fairly large backing store, with 300 domains of 50 | 328 // We want to setup a fairly large backing store, with 300 domains of 50 |
252 // cookies each. Creation times must be unique. | 329 // cookies each. Creation times must be unique. |
253 int64 time_tick(base::Time::Now().ToInternalValue()); | 330 int64 time_tick(base::Time::Now().ToInternalValue()); |
254 | 331 |
255 for (int domain_num = 0; domain_num < 300; domain_num++) { | 332 for (int domain_num = 0; domain_num < 300; domain_num++) { |
256 std::string domain_name(base::StringPrintf(".Domain_%d.com", domain_num)); | 333 std::string domain_name(base::StringPrintf(".Domain_%d.com", domain_num)); |
257 std::string gurl("www" + domain_name); | 334 std::string gurl("www" + domain_name); |
258 for (int cookie_num = 0; cookie_num < 50; cookie_num++) { | 335 for (int cookie_num = 0; cookie_num < 50; cookie_num++) { |
259 std::string cookie_line(base::StringPrintf("Cookie_%d=1; Path=/", | 336 std::string cookie_line(base::StringPrintf("Cookie_%d=1; Path=/", |
260 cookie_num)); | 337 cookie_num)); |
261 AddCookieToList(gurl, cookie_line, | 338 AddCookieToList(gurl, cookie_line, |
262 base::Time::FromInternalValue(time_tick++), | 339 base::Time::FromInternalValue(time_tick++), |
263 &initial_cookies); | 340 &initial_cookies); |
264 } | 341 } |
265 } | 342 } |
266 | 343 |
267 store->SetLoadExpectation(true, initial_cookies); | 344 store->SetLoadExpectation(true, initial_cookies); |
268 | 345 |
269 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); | 346 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); |
270 | 347 |
271 // Import will happen on first access. | 348 // Import will happen on first access. |
272 GURL gurl("www.google.com"); | 349 GURL gurl("www.google.com"); |
273 CookieOptions options; | 350 CookieOptions options; |
274 PerfTimeLogger timer("Cookie_monster_import_from_store"); | 351 PerfTimeLogger timer("Cookie_monster_import_from_store"); |
275 cm->GetCookiesWithOptions(gurl, options); | 352 getCookiesCallback.GetCookies(cm, gurl); |
276 timer.Done(); | 353 timer.Done(); |
277 | 354 |
278 // Just confirm keys were set as expected. | 355 // Just confirm keys were set as expected. |
279 EXPECT_EQ("domain_1.com", cm->GetKey("www.Domain_1.com")); | 356 EXPECT_EQ("domain_1.com", cm->GetKey("www.Domain_1.com")); |
280 } | 357 } |
281 | 358 |
282 TEST(CookieMonsterTest, TestGetKey) { | 359 TEST_F(CookieMonsterTest, TestGetKey) { |
283 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 360 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
284 PerfTimeLogger timer("Cookie_monster_get_key"); | 361 PerfTimeLogger timer("Cookie_monster_get_key"); |
285 for (int i = 0; i < kNumCookies; i++) | 362 for (int i = 0; i < kNumCookies; i++) |
286 cm->GetKey("www.google.com"); | 363 cm->GetKey("www.google.com"); |
287 timer.Done(); | 364 timer.Done(); |
288 } | 365 } |
289 | 366 |
290 // This test is probing for whether garbage collection happens when it | 367 // This test is probing for whether garbage collection happens when it |
291 // shouldn't. This will not in general be visible functionally, since | 368 // shouldn't. This will not in general be visible functionally, since |
292 // if GC runs twice in a row without any change to the store, the second | 369 // if GC runs twice in a row without any change to the store, the second |
293 // GC run will not do anything the first one didn't. That's why this is | 370 // GC run will not do anything the first one didn't. That's why this is |
294 // a performance test. The test should be considered to pass if all the | 371 // a performance test. The test should be considered to pass if all the |
295 // times reported are approximately the same--this indicates that no GC | 372 // times reported are approximately the same--this indicates that no GC |
296 // happened repeatedly for any case. | 373 // happened repeatedly for any case. |
297 TEST(CookieMonsterTest, TestGCTimes) { | 374 TEST_F(CookieMonsterTest, TestGCTimes) { |
| 375 SetCookieCallback setCookieCallback; |
| 376 |
298 const struct TestCase { | 377 const struct TestCase { |
299 const char* name; | 378 const char* name; |
300 int num_cookies; | 379 int num_cookies; |
301 int num_old_cookies; | 380 int num_old_cookies; |
302 } test_cases[] = { | 381 } test_cases[] = { |
303 { | 382 { |
304 // A whole lot of recent cookies; gc shouldn't happen. | 383 // A whole lot of recent cookies; gc shouldn't happen. |
305 "all_recent", | 384 "all_recent", |
306 CookieMonster::kMaxCookies * 2, | 385 CookieMonster::kMaxCookies * 2, |
307 0, | 386 0, |
(...skipping 23 matching lines...) Expand all Loading... |
331 for (int ci = 0; ci < static_cast<int>(ARRAYSIZE_UNSAFE(test_cases)); ++ci) { | 410 for (int ci = 0; ci < static_cast<int>(ARRAYSIZE_UNSAFE(test_cases)); ++ci) { |
332 const TestCase& test_case(test_cases[ci]); | 411 const TestCase& test_case(test_cases[ci]); |
333 scoped_refptr<CookieMonster> cm( | 412 scoped_refptr<CookieMonster> cm( |
334 CreateMonsterFromStoreForGC( | 413 CreateMonsterFromStoreForGC( |
335 test_case.num_cookies, test_case.num_old_cookies, | 414 test_case.num_cookies, test_case.num_old_cookies, |
336 CookieMonster::kSafeFromGlobalPurgeDays * 2)); | 415 CookieMonster::kSafeFromGlobalPurgeDays * 2)); |
337 | 416 |
338 GURL gurl("http://google.com"); | 417 GURL gurl("http://google.com"); |
339 std::string cookie_line("z=3"); | 418 std::string cookie_line("z=3"); |
340 // Trigger the Garbage collection we're allowed. | 419 // Trigger the Garbage collection we're allowed. |
341 EXPECT_TRUE(cm->SetCookie(gurl, cookie_line)); | 420 setCookieCallback.SetCookie(cm, gurl, cookie_line); |
342 | 421 |
343 PerfTimeLogger timer((std::string("GC_") + test_case.name).c_str()); | 422 PerfTimeLogger timer((std::string("GC_") + test_case.name).c_str()); |
344 for (int i = 0; i < kNumCookies; i++) | 423 for (int i = 0; i < kNumCookies; i++) |
345 EXPECT_TRUE(cm->SetCookie(gurl, cookie_line)); | 424 setCookieCallback.SetCookie(cm, gurl, cookie_line); |
346 timer.Done(); | 425 timer.Done(); |
347 } | 426 } |
348 } | 427 } |
349 | 428 |
350 } // namespace | 429 } // namespace |
OLD | NEW |