| 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 <map> | 5 #include <map> |
| 6 #include <set> | 6 #include <set> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/scoped_temp_dir.h" | 13 #include "base/scoped_temp_dir.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/test/thread_test_helper.h" | 16 #include "base/test/thread_test_helper.h" |
| 17 #include "base/time.h" | 17 #include "base/time.h" |
| 18 #include "chrome/browser/net/clear_on_exit_policy.h" | 18 #include "chrome/browser/net/clear_on_exit_policy.h" |
| 19 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" | 19 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" |
| 20 #include "chrome/common/chrome_constants.h" | 20 #include "chrome/common/chrome_constants.h" |
| 21 #include "content/public/test/test_browser_thread.h" | 21 #include "content/public/test/test_browser_thread.h" |
| 22 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
| 23 #include "net/cookies/canonical_cookie.h" |
| 23 #include "sql/connection.h" | 24 #include "sql/connection.h" |
| 24 #include "sql/meta_table.h" | 25 #include "sql/meta_table.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "webkit/quota/mock_special_storage_policy.h" | 27 #include "webkit/quota/mock_special_storage_policy.h" |
| 27 | 28 |
| 28 using content::BrowserThread; | 29 using content::BrowserThread; |
| 29 using net::CookieMonster; | |
| 30 | 30 |
| 31 typedef std::vector<CookieMonster::CanonicalCookie*> CanonicalCookieVector; | 31 typedef std::vector<net::CanonicalCookie*> CanonicalCookieVector; |
| 32 | 32 |
| 33 class SQLitePersistentCookieStoreTest : public testing::Test { | 33 class SQLitePersistentCookieStoreTest : public testing::Test { |
| 34 public: | 34 public: |
| 35 SQLitePersistentCookieStoreTest() | 35 SQLitePersistentCookieStoreTest() |
| 36 : ui_thread_(BrowserThread::UI), | 36 : ui_thread_(BrowserThread::UI), |
| 37 db_thread_(BrowserThread::DB), | 37 db_thread_(BrowserThread::DB), |
| 38 io_thread_(BrowserThread::IO), | 38 io_thread_(BrowserThread::IO), |
| 39 loaded_event_(false, false), | 39 loaded_event_(false, false), |
| 40 key_loaded_event_(false, false), | 40 key_loaded_event_(false, false), |
| 41 db_thread_event_(false, false) { | 41 db_thread_event_(false, false) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 db_thread_event_.Wait(); | 89 db_thread_event_.Wait(); |
| 90 } | 90 } |
| 91 | 91 |
| 92 // Adds a persistent cookie to store_. | 92 // Adds a persistent cookie to store_. |
| 93 void AddCookie(const std::string& name, | 93 void AddCookie(const std::string& name, |
| 94 const std::string& value, | 94 const std::string& value, |
| 95 const std::string& domain, | 95 const std::string& domain, |
| 96 const std::string& path, | 96 const std::string& path, |
| 97 const base::Time& creation) { | 97 const base::Time& creation) { |
| 98 store_->AddCookie( | 98 store_->AddCookie( |
| 99 CookieMonster::CanonicalCookie(GURL(), name, value, domain, path, | 99 net::CanonicalCookie(GURL(), name, value, domain, path, std::string(), |
| 100 std::string(), std::string(), | 100 std::string(), creation, creation, creation, false, |
| 101 creation, creation, creation, | 101 false)); |
| 102 false, false)); | |
| 103 } | 102 } |
| 104 | 103 |
| 105 virtual void SetUp() { | 104 virtual void SetUp() { |
| 106 ui_thread_.Start(); | 105 ui_thread_.Start(); |
| 107 db_thread_.Start(); | 106 db_thread_.Start(); |
| 108 io_thread_.Start(); | 107 io_thread_.Start(); |
| 109 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 108 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 110 } | 109 } |
| 111 | 110 |
| 112 protected: | 111 protected: |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 | 325 |
| 327 ASSERT_EQ(1, counter->callback_count()); | 326 ASSERT_EQ(1, counter->callback_count()); |
| 328 } | 327 } |
| 329 | 328 |
| 330 // Test loading old session cookies from the disk. | 329 // Test loading old session cookies from the disk. |
| 331 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { | 330 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { |
| 332 InitializeStore(true); | 331 InitializeStore(true); |
| 333 | 332 |
| 334 // Add a session cookie. | 333 // Add a session cookie. |
| 335 store_->AddCookie( | 334 store_->AddCookie( |
| 336 CookieMonster::CanonicalCookie( | 335 net::CanonicalCookie( |
| 337 GURL(), "C", "D", "sessioncookie.com", "/", std::string(), | 336 GURL(), "C", "D", "sessioncookie.com", "/", std::string(), |
| 338 std::string(), base::Time::Now(), base::Time(), | 337 std::string(), base::Time::Now(), base::Time(), |
| 339 base::Time::Now(), false, false)); | 338 base::Time::Now(), false, false)); |
| 340 | 339 |
| 341 // Force the store to write its data to the disk. | 340 // Force the store to write its data to the disk. |
| 342 DestroyStore(); | 341 DestroyStore(); |
| 343 | 342 |
| 344 // Create a store that loads session cookies and test that the session cookie | 343 // Create a store that loads session cookies and test that the session cookie |
| 345 // was loaded. | 344 // was loaded. |
| 346 CanonicalCookieVector cookies; | 345 CanonicalCookieVector cookies; |
| 347 CreateAndLoad(true, &cookies); | 346 CreateAndLoad(true, &cookies); |
| 348 | 347 |
| 349 ASSERT_EQ(1U, cookies.size()); | 348 ASSERT_EQ(1U, cookies.size()); |
| 350 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); | 349 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); |
| 351 ASSERT_STREQ("C", cookies[0]->Name().c_str()); | 350 ASSERT_STREQ("C", cookies[0]->Name().c_str()); |
| 352 ASSERT_STREQ("D", cookies[0]->Value().c_str()); | 351 ASSERT_STREQ("D", cookies[0]->Value().c_str()); |
| 353 | 352 |
| 354 STLDeleteElements(&cookies); | 353 STLDeleteElements(&cookies); |
| 355 } | 354 } |
| 356 | 355 |
| 357 // Test loading old session cookies from the disk. | 356 // Test loading old session cookies from the disk. |
| 358 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { | 357 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { |
| 359 InitializeStore(true); | 358 InitializeStore(true); |
| 360 | 359 |
| 361 // Add a session cookie. | 360 // Add a session cookie. |
| 362 store_->AddCookie( | 361 store_->AddCookie( |
| 363 CookieMonster::CanonicalCookie( | 362 net::CanonicalCookie( |
| 364 GURL(), "C", "D", "sessioncookie.com", "/", std::string(), | 363 GURL(), "C", "D", "sessioncookie.com", "/", std::string(), |
| 365 std::string(), base::Time::Now(), base::Time(), | 364 std::string(), base::Time::Now(), base::Time(), |
| 366 base::Time::Now(), false, false)); | 365 base::Time::Now(), false, false)); |
| 367 | 366 |
| 368 // Force the store to write its data to the disk. | 367 // Force the store to write its data to the disk. |
| 369 DestroyStore(); | 368 DestroyStore(); |
| 370 | 369 |
| 371 // Create a store that doesn't load old session cookies and test that the | 370 // Create a store that doesn't load old session cookies and test that the |
| 372 // session cookie was not loaded. | 371 // session cookie was not loaded. |
| 373 CanonicalCookieVector cookies; | 372 CanonicalCookieVector cookies; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 384 ASSERT_EQ(0U, cookies.size()); | 383 ASSERT_EQ(0U, cookies.size()); |
| 385 } | 384 } |
| 386 | 385 |
| 387 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { | 386 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { |
| 388 InitializeStore(true); | 387 InitializeStore(true); |
| 389 static const char kSessionName[] = "session"; | 388 static const char kSessionName[] = "session"; |
| 390 static const char kPersistentName[] = "persistent"; | 389 static const char kPersistentName[] = "persistent"; |
| 391 | 390 |
| 392 // Add a session cookie. | 391 // Add a session cookie. |
| 393 store_->AddCookie( | 392 store_->AddCookie( |
| 394 CookieMonster::CanonicalCookie( | 393 net::CanonicalCookie( |
| 395 GURL(), kSessionName, "val", "sessioncookie.com", "/", | 394 GURL(), kSessionName, "val", "sessioncookie.com", "/", |
| 396 std::string(), std::string(), | 395 std::string(), std::string(), |
| 397 base::Time::Now(), base::Time(), base::Time::Now(), | 396 base::Time::Now(), base::Time(), base::Time::Now(), |
| 398 false, false)); | 397 false, false)); |
| 399 // Add a persistent cookie. | 398 // Add a persistent cookie. |
| 400 store_->AddCookie( | 399 store_->AddCookie( |
| 401 CookieMonster::CanonicalCookie( | 400 net::CanonicalCookie( |
| 402 GURL(), kPersistentName, "val", "sessioncookie.com", "/", | 401 GURL(), kPersistentName, "val", "sessioncookie.com", "/", |
| 403 std::string(), std::string(), | 402 std::string(), std::string(), |
| 404 base::Time::Now() - base::TimeDelta::FromDays(1), base::Time::Now(), | 403 base::Time::Now() - base::TimeDelta::FromDays(1), base::Time::Now(), |
| 405 base::Time::Now(), false, false)); | 404 base::Time::Now(), false, false)); |
| 406 | 405 |
| 407 // Create a store that loads session cookie and test that the the IsPersistent | 406 // Create a store that loads session cookie and test that the the IsPersistent |
| 408 // attribute is restored. | 407 // attribute is restored. |
| 409 CanonicalCookieVector cookies; | 408 CanonicalCookieVector cookies; |
| 410 CreateAndLoad(true, &cookies); | 409 CreateAndLoad(true, &cookies); |
| 411 ASSERT_EQ(2U, cookies.size()); | 410 ASSERT_EQ(2U, cookies.size()); |
| 412 | 411 |
| 413 std::map<std::string, CookieMonster::CanonicalCookie*> cookie_map; | 412 std::map<std::string, net::CanonicalCookie*> cookie_map; |
| 414 for (CanonicalCookieVector::const_iterator it = cookies.begin(); | 413 for (CanonicalCookieVector::const_iterator it = cookies.begin(); |
| 415 it != cookies.end(); | 414 it != cookies.end(); |
| 416 ++it) { | 415 ++it) { |
| 417 cookie_map[(*it)->Name()] = *it; | 416 cookie_map[(*it)->Name()] = *it; |
| 418 } | 417 } |
| 419 | 418 |
| 420 std::map<std::string, CookieMonster::CanonicalCookie*>::const_iterator it = | 419 std::map<std::string, net::CanonicalCookie*>::const_iterator it = |
| 421 cookie_map.find(kSessionName); | 420 cookie_map.find(kSessionName); |
| 422 ASSERT_TRUE(it != cookie_map.end()); | 421 ASSERT_TRUE(it != cookie_map.end()); |
| 423 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); | 422 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); |
| 424 | 423 |
| 425 it = cookie_map.find(kPersistentName); | 424 it = cookie_map.find(kPersistentName); |
| 426 ASSERT_TRUE(it != cookie_map.end()); | 425 ASSERT_TRUE(it != cookie_map.end()); |
| 427 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); | 426 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); |
| 428 | 427 |
| 429 STLDeleteElements(&cookies); | 428 STLDeleteElements(&cookies); |
| 430 } | 429 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 // Add some cookies. | 474 // Add some cookies. |
| 476 base::Time t = base::Time::Now(); | 475 base::Time t = base::Time::Now(); |
| 477 AddCookie("A", "1", protected_origin, "/", t); | 476 AddCookie("A", "1", protected_origin, "/", t); |
| 478 t += base::TimeDelta::FromInternalValue(10); | 477 t += base::TimeDelta::FromInternalValue(10); |
| 479 AddCookie("B", "2", session_origin, "/", t); | 478 AddCookie("B", "2", session_origin, "/", t); |
| 480 t += base::TimeDelta::FromInternalValue(10); | 479 t += base::TimeDelta::FromInternalValue(10); |
| 481 AddCookie("C", "3", other_origin, "/", t); | 480 AddCookie("C", "3", other_origin, "/", t); |
| 482 // A secure cookie on session_origin. | 481 // A secure cookie on session_origin. |
| 483 t += base::TimeDelta::FromInternalValue(10); | 482 t += base::TimeDelta::FromInternalValue(10); |
| 484 store_->AddCookie( | 483 store_->AddCookie( |
| 485 CookieMonster::CanonicalCookie(GURL(), "D", "4", session_origin, "/", | 484 net::CanonicalCookie(GURL(), "D", "4", session_origin, "/", std::string(), |
| 486 std::string(), std::string(), | 485 std::string(), t, t, t, true, false)); |
| 487 t, t, t, | |
| 488 true, false)); | |
| 489 | 486 |
| 490 // First, check that we can override the policy. | 487 // First, check that we can override the policy. |
| 491 store_->SetForceKeepSessionState(); | 488 store_->SetForceKeepSessionState(); |
| 492 | 489 |
| 493 // Force the store to write its data to the disk. | 490 // Force the store to write its data to the disk. |
| 494 DestroyStore(); | 491 DestroyStore(); |
| 495 | 492 |
| 496 // Create a store test that the cookie on session_origin does not exist. | 493 // Create a store test that the cookie on session_origin does not exist. |
| 497 store_ = new SQLitePersistentCookieStore( | 494 store_ = new SQLitePersistentCookieStore( |
| 498 temp_dir_.path().Append(chrome::kCookieFilename), | 495 temp_dir_.path().Append(chrome::kCookieFilename), |
| (...skipping 19 matching lines...) Expand all Loading... |
| 518 Load(&cookies); | 515 Load(&cookies); |
| 519 | 516 |
| 520 EXPECT_EQ(3U, cookies.size()); | 517 EXPECT_EQ(3U, cookies.size()); |
| 521 EXPECT_TRUE(IsCookiePresent(cookies, protected_origin, "A", "1", false)); | 518 EXPECT_TRUE(IsCookiePresent(cookies, protected_origin, "A", "1", false)); |
| 522 EXPECT_TRUE(IsCookiePresent(cookies, other_origin, "C", "3", false)); | 519 EXPECT_TRUE(IsCookiePresent(cookies, other_origin, "C", "3", false)); |
| 523 EXPECT_TRUE(IsCookiePresent(cookies, session_origin, "D", "4", true)); | 520 EXPECT_TRUE(IsCookiePresent(cookies, session_origin, "D", "4", true)); |
| 524 | 521 |
| 525 DestroyStore(); | 522 DestroyStore(); |
| 526 STLDeleteElements(&cookies); | 523 STLDeleteElements(&cookies); |
| 527 } | 524 } |
| OLD | NEW |