OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/net/sqlite_persistent_cookie_store.h" | 5 #include "content/browser/net/sqlite_persistent_cookie_store.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 void AddCookie(const std::string& name, | 157 void AddCookie(const std::string& name, |
158 const std::string& value, | 158 const std::string& value, |
159 const std::string& domain, | 159 const std::string& domain, |
160 const std::string& path, | 160 const std::string& path, |
161 const base::Time& creation) { | 161 const base::Time& creation) { |
162 store_->AddCookie(net::CanonicalCookie( | 162 store_->AddCookie(net::CanonicalCookie( |
163 GURL(), name, value, domain, path, creation, creation, creation, false, | 163 GURL(), name, value, domain, path, creation, creation, creation, false, |
164 false, false, net::COOKIE_PRIORITY_DEFAULT)); | 164 false, false, net::COOKIE_PRIORITY_DEFAULT)); |
165 } | 165 } |
166 | 166 |
167 void AddCookieWithExpiration(const std::string& name, | |
168 const std::string& value, | |
169 const std::string& domain, | |
170 const std::string& path, | |
171 const base::Time& creation, | |
172 const base::Time& expiration) { | |
173 store_->AddCookie(net::CanonicalCookie( | |
174 GURL(), name, value, domain, path, creation, expiration, creation, | |
175 false, false, false, net::COOKIE_PRIORITY_DEFAULT)); | |
176 } | |
177 | |
178 std::string ReadRawDBContents() { | 167 std::string ReadRawDBContents() { |
179 std::string contents; | 168 std::string contents; |
180 if (!base::ReadFileToString(temp_dir_.path().Append(kCookieFilename), | 169 if (!base::ReadFileToString(temp_dir_.path().Append(kCookieFilename), |
181 &contents)) | 170 &contents)) |
182 return std::string(); | 171 return std::string(); |
183 return contents; | 172 return contents; |
184 } | 173 } |
185 | 174 |
186 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } | 175 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
187 | 176 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 // Now delete the cookie and check persistence again. | 250 // Now delete the cookie and check persistence again. |
262 store_->DeleteCookie(*cookies[0]); | 251 store_->DeleteCookie(*cookies[0]); |
263 DestroyStore(); | 252 DestroyStore(); |
264 STLDeleteElements(&cookies); | 253 STLDeleteElements(&cookies); |
265 | 254 |
266 // Reload and check if the cookie has been removed. | 255 // Reload and check if the cookie has been removed. |
267 CreateAndLoad(false, false, &cookies); | 256 CreateAndLoad(false, false, &cookies); |
268 ASSERT_EQ(0U, cookies.size()); | 257 ASSERT_EQ(0U, cookies.size()); |
269 } | 258 } |
270 | 259 |
271 TEST_F(SQLitePersistentCookieStoreTest, TestSessionCookiesDeletedOnStartup) { | |
272 // Initialize the cookie store with 3 persistent cookies, 5 transient | |
273 // cookies. | |
274 InitializeStore(false, false); | |
275 // Add persistent cookies. | |
276 AddCookieWithExpiration("A", "B", "a1.com", "/", base::Time::Now(), | |
277 base::Time::Now()); | |
278 AddCookieWithExpiration("A", "B", "a2.com", "/", base::Time::Now(), | |
279 base::Time::Now()); | |
280 AddCookieWithExpiration("A", "B", "a3.com", "/", base::Time::Now(), | |
281 base::Time::Now()); | |
282 // Add transient cookies. | |
283 AddCookieWithExpiration("A", "B", "b1.com", "/", base::Time::Now(), | |
284 base::Time()); | |
285 AddCookieWithExpiration("A", "B", "b2.com", "/", base::Time::Now(), | |
286 base::Time()); | |
287 AddCookieWithExpiration("A", "B", "b3.com", "/", base::Time::Now(), | |
288 base::Time()); | |
289 AddCookieWithExpiration("A", "B", "b4.com", "/", base::Time::Now(), | |
290 base::Time()); | |
291 AddCookieWithExpiration("A", "B", "b5.com", "/", base::Time::Now(), | |
292 base::Time()); | |
293 DestroyStore(); | |
294 | |
295 // Load the store a second time. Before the store finishes loading, add a | |
296 // transient cookie and flush it to disk. | |
297 store_ = new SQLitePersistentCookieStore( | |
298 temp_dir_.path().Append(kCookieFilename), | |
299 client_task_runner(), | |
300 background_task_runner(), | |
301 false, NULL, NULL); | |
302 | |
303 // Posting a blocking task to db_thread_ makes sure that the DB thread waits | |
304 // until both Load and Flush have been posted to its task queue. | |
305 background_task_runner()->PostTask( | |
306 FROM_HERE, | |
307 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, | |
308 base::Unretained(this))); | |
309 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | |
310 base::Unretained(this))); | |
311 AddCookieWithExpiration("A", "B", "c.com", "/", base::Time::Now(), | |
312 base::Time()); | |
313 base::WaitableEvent event(false, false); | |
314 store_->Flush(base::Bind(&base::WaitableEvent::Signal, | |
315 base::Unretained(&event))); | |
316 | |
317 // Now the DB-thread queue contains: | |
318 // (active:) | |
319 // 1. Wait (on db_event) | |
320 // (pending:) | |
321 // 2. "Init And Chain-Load First Domain" | |
322 // 3. Add Cookie (c.com) | |
323 // 4. Flush Cookie (c.com) | |
324 db_thread_event_.Signal(); | |
325 event.Wait(); | |
326 loaded_event_.Wait(); | |
327 STLDeleteElements(&cookies_); | |
328 DestroyStore(); | |
329 | |
330 // Load the store a third time, this time restoring session cookies. The | |
331 // store should contain exactly 4 cookies: the 3 persistent, and "c.com", | |
332 // which was added during the second cookie store load. | |
333 store_ = new SQLitePersistentCookieStore( | |
334 temp_dir_.path().Append(kCookieFilename), | |
335 client_task_runner(), | |
336 background_task_runner(), | |
337 true, NULL, NULL); | |
338 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | |
339 base::Unretained(this))); | |
340 loaded_event_.Wait(); | |
341 ASSERT_EQ(4u, cookies_.size()); | |
342 STLDeleteElements(&cookies_); | |
343 } | |
344 | |
345 // Test that priority load of cookies for a specfic domain key could be | 260 // Test that priority load of cookies for a specfic domain key could be |
346 // completed before the entire store is loaded | 261 // completed before the entire store is loaded |
347 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { | 262 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { |
348 InitializeStore(false, false); | 263 InitializeStore(false, false); |
349 base::Time t = base::Time::Now(); | 264 base::Time t = base::Time::Now(); |
350 AddCookie("A", "B", "foo.bar", "/", t); | 265 AddCookie("A", "B", "foo.bar", "/", t); |
351 t += base::TimeDelta::FromInternalValue(10); | 266 t += base::TimeDelta::FromInternalValue(10); |
352 AddCookie("A", "B", "www.aaa.com", "/", t); | 267 AddCookie("A", "B", "www.aaa.com", "/", t); |
353 t += base::TimeDelta::FromInternalValue(10); | 268 t += base::TimeDelta::FromInternalValue(10); |
354 AddCookie("A", "B", "travel.aaa.com", "/", t); | 269 AddCookie("A", "B", "travel.aaa.com", "/", t); |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
670 EXPECT_EQ(2, resultcount); | 585 EXPECT_EQ(2, resultcount); |
671 | 586 |
672 // Verify that "encrypted_value" is NOT visible in the file. | 587 // Verify that "encrypted_value" is NOT visible in the file. |
673 contents = ReadRawDBContents(); | 588 contents = ReadRawDBContents(); |
674 EXPECT_NE(0U, contents.length()); | 589 EXPECT_NE(0U, contents.length()); |
675 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); | 590 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); |
676 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); | 591 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); |
677 } | 592 } |
678 | 593 |
679 } // namespace content | 594 } // namespace content |
OLD | NEW |