| 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" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 16 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
| 19 #include "base/test/sequenced_worker_pool_owner.h" | 19 #include "base/test/sequenced_worker_pool_owner.h" |
| 20 #include "base/threading/sequenced_worker_pool.h" | 20 #include "base/threading/sequenced_worker_pool.h" |
| 21 #include "base/time.h" | 21 #include "base/time.h" |
| 22 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
| 23 #include "net/cookies/canonical_cookie.h" | 23 #include "net/cookies/canonical_cookie.h" |
| 24 #include "net/cookies/cookie_constants.h" |
| 24 #include "sql/connection.h" | 25 #include "sql/connection.h" |
| 25 #include "sql/meta_table.h" | 26 #include "sql/meta_table.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 28 |
| 28 namespace content { | 29 namespace content { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 const base::FilePath::CharType kCookieFilename[] = FILE_PATH_LITERAL("Cookies"); | 33 const base::FilePath::CharType kCookieFilename[] = FILE_PATH_LITERAL("Cookies"); |
| 33 | 34 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 } | 114 } |
| 114 | 115 |
| 115 // Adds a persistent cookie to store_. | 116 // Adds a persistent cookie to store_. |
| 116 void AddCookie(const std::string& name, | 117 void AddCookie(const std::string& name, |
| 117 const std::string& value, | 118 const std::string& value, |
| 118 const std::string& domain, | 119 const std::string& domain, |
| 119 const std::string& path, | 120 const std::string& path, |
| 120 const base::Time& creation) { | 121 const base::Time& creation) { |
| 121 store_->AddCookie( | 122 store_->AddCookie( |
| 122 net::CanonicalCookie(GURL(), name, value, domain, path, creation, | 123 net::CanonicalCookie(GURL(), name, value, domain, path, creation, |
| 123 creation, creation, false, false)); | 124 creation, creation, false, false, |
| 125 net::PRIORITY_DEFAULT)); |
| 124 } | 126 } |
| 125 | 127 |
| 126 virtual void SetUp() OVERRIDE { | 128 virtual void SetUp() OVERRIDE { |
| 127 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 129 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 128 } | 130 } |
| 129 | 131 |
| 130 virtual void TearDown() OVERRIDE { | 132 virtual void TearDown() OVERRIDE { |
| 131 DestroyStore(); | 133 DestroyStore(); |
| 132 pool_owner_->pool()->Shutdown(); | 134 pool_owner_->pool()->Shutdown(); |
| 133 } | 135 } |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 } | 309 } |
| 308 | 310 |
| 309 // Test loading old session cookies from the disk. | 311 // Test loading old session cookies from the disk. |
| 310 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { | 312 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { |
| 311 InitializeStore(true); | 313 InitializeStore(true); |
| 312 | 314 |
| 313 // Add a session cookie. | 315 // Add a session cookie. |
| 314 store_->AddCookie( | 316 store_->AddCookie( |
| 315 net::CanonicalCookie( | 317 net::CanonicalCookie( |
| 316 GURL(), "C", "D", "sessioncookie.com", "/", base::Time::Now(), | 318 GURL(), "C", "D", "sessioncookie.com", "/", base::Time::Now(), |
| 317 base::Time(), base::Time::Now(), false, false)); | 319 base::Time(), base::Time::Now(), false, false, |
| 320 net::PRIORITY_DEFAULT)); |
| 318 | 321 |
| 319 // Force the store to write its data to the disk. | 322 // Force the store to write its data to the disk. |
| 320 DestroyStore(); | 323 DestroyStore(); |
| 321 | 324 |
| 322 // Create a store that loads session cookies and test that the session cookie | 325 // Create a store that loads session cookies and test that the session cookie |
| 323 // was loaded. | 326 // was loaded. |
| 324 CanonicalCookieVector cookies; | 327 CanonicalCookieVector cookies; |
| 325 CreateAndLoad(true, &cookies); | 328 CreateAndLoad(true, &cookies); |
| 326 | 329 |
| 327 ASSERT_EQ(1U, cookies.size()); | 330 ASSERT_EQ(1U, cookies.size()); |
| 328 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); | 331 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); |
| 329 ASSERT_STREQ("C", cookies[0]->Name().c_str()); | 332 ASSERT_STREQ("C", cookies[0]->Name().c_str()); |
| 330 ASSERT_STREQ("D", cookies[0]->Value().c_str()); | 333 ASSERT_STREQ("D", cookies[0]->Value().c_str()); |
| 331 | 334 |
| 332 STLDeleteElements(&cookies); | 335 STLDeleteElements(&cookies); |
| 333 } | 336 } |
| 334 | 337 |
| 335 // Test loading old session cookies from the disk. | 338 // Test loading old session cookies from the disk. |
| 336 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { | 339 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { |
| 337 InitializeStore(true); | 340 InitializeStore(true); |
| 338 | 341 |
| 339 // Add a session cookie. | 342 // Add a session cookie. |
| 340 store_->AddCookie( | 343 store_->AddCookie( |
| 341 net::CanonicalCookie( | 344 net::CanonicalCookie( |
| 342 GURL(), "C", "D", "sessioncookie.com", "/", base::Time::Now(), | 345 GURL(), "C", "D", "sessioncookie.com", "/", base::Time::Now(), |
| 343 base::Time(), base::Time::Now(), false, false)); | 346 base::Time(), base::Time::Now(), false, false, |
| 347 net::PRIORITY_DEFAULT)); |
| 344 | 348 |
| 345 // Force the store to write its data to the disk. | 349 // Force the store to write its data to the disk. |
| 346 DestroyStore(); | 350 DestroyStore(); |
| 347 | 351 |
| 348 // Create a store that doesn't load old session cookies and test that the | 352 // Create a store that doesn't load old session cookies and test that the |
| 349 // session cookie was not loaded. | 353 // session cookie was not loaded. |
| 350 CanonicalCookieVector cookies; | 354 CanonicalCookieVector cookies; |
| 351 CreateAndLoad(false, &cookies); | 355 CreateAndLoad(false, &cookies); |
| 352 ASSERT_EQ(0U, cookies.size()); | 356 ASSERT_EQ(0U, cookies.size()); |
| 353 | 357 |
| 354 // The store should also delete the session cookie. Wait until that has been | 358 // The store should also delete the session cookie. Wait until that has been |
| 355 // done. | 359 // done. |
| 356 DestroyStore(); | 360 DestroyStore(); |
| 357 | 361 |
| 358 // Create a store that loads old session cookies and test that the session | 362 // Create a store that loads old session cookies and test that the session |
| 359 // cookie is gone. | 363 // cookie is gone. |
| 360 CreateAndLoad(true, &cookies); | 364 CreateAndLoad(true, &cookies); |
| 361 ASSERT_EQ(0U, cookies.size()); | 365 ASSERT_EQ(0U, cookies.size()); |
| 362 } | 366 } |
| 363 | 367 |
| 364 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { | 368 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { |
| 365 InitializeStore(true); | 369 InitializeStore(true); |
| 366 static const char kSessionName[] = "session"; | 370 static const char kSessionName[] = "session"; |
| 367 static const char kPersistentName[] = "persistent"; | 371 static const char kPersistentName[] = "persistent"; |
| 368 | 372 |
| 369 // Add a session cookie. | 373 // Add a session cookie. |
| 370 store_->AddCookie( | 374 store_->AddCookie( |
| 371 net::CanonicalCookie( | 375 net::CanonicalCookie( |
| 372 GURL(), kSessionName, "val", "sessioncookie.com", "/", | 376 GURL(), kSessionName, "val", "sessioncookie.com", "/", |
| 373 base::Time::Now(), base::Time(), base::Time::Now(), false, false)); | 377 base::Time::Now(), base::Time(), base::Time::Now(), false, false, |
| 378 net::PRIORITY_DEFAULT)); |
| 374 // Add a persistent cookie. | 379 // Add a persistent cookie. |
| 375 store_->AddCookie( | 380 store_->AddCookie( |
| 376 net::CanonicalCookie( | 381 net::CanonicalCookie( |
| 377 GURL(), kPersistentName, "val", "sessioncookie.com", "/", | 382 GURL(), kPersistentName, "val", "sessioncookie.com", "/", |
| 378 base::Time::Now() - base::TimeDelta::FromDays(1), base::Time::Now(), | 383 base::Time::Now() - base::TimeDelta::FromDays(1), base::Time::Now(), |
| 379 base::Time::Now(), false, false)); | 384 base::Time::Now(), false, false, |
| 385 net::PRIORITY_DEFAULT)); |
| 380 | 386 |
| 381 // Create a store that loads session cookie and test that the the IsPersistent | 387 // Create a store that loads session cookie and test that the the IsPersistent |
| 382 // attribute is restored. | 388 // attribute is restored. |
| 383 CanonicalCookieVector cookies; | 389 CanonicalCookieVector cookies; |
| 384 CreateAndLoad(true, &cookies); | 390 CreateAndLoad(true, &cookies); |
| 385 ASSERT_EQ(2U, cookies.size()); | 391 ASSERT_EQ(2U, cookies.size()); |
| 386 | 392 |
| 387 std::map<std::string, net::CanonicalCookie*> cookie_map; | 393 std::map<std::string, net::CanonicalCookie*> cookie_map; |
| 388 for (CanonicalCookieVector::const_iterator it = cookies.begin(); | 394 for (CanonicalCookieVector::const_iterator it = cookies.begin(); |
| 389 it != cookies.end(); | 395 it != cookies.end(); |
| 390 ++it) { | 396 ++it) { |
| 391 cookie_map[(*it)->Name()] = *it; | 397 cookie_map[(*it)->Name()] = *it; |
| 392 } | 398 } |
| 393 | 399 |
| 394 std::map<std::string, net::CanonicalCookie*>::const_iterator it = | 400 std::map<std::string, net::CanonicalCookie*>::const_iterator it = |
| 395 cookie_map.find(kSessionName); | 401 cookie_map.find(kSessionName); |
| 396 ASSERT_TRUE(it != cookie_map.end()); | 402 ASSERT_TRUE(it != cookie_map.end()); |
| 397 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); | 403 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); |
| 398 | 404 |
| 399 it = cookie_map.find(kPersistentName); | 405 it = cookie_map.find(kPersistentName); |
| 400 ASSERT_TRUE(it != cookie_map.end()); | 406 ASSERT_TRUE(it != cookie_map.end()); |
| 401 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); | 407 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); |
| 402 | 408 |
| 403 STLDeleteElements(&cookies); | 409 STLDeleteElements(&cookies); |
| 404 } | 410 } |
| 405 | 411 |
| 406 } // namespace content | 412 } // namespace content |
| OLD | NEW |