Chromium Code Reviews| 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" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 CookieMonster::CanonicalCookie(GURL(), name, value, domain, path, |
| 100 std::string(), std::string(), | 100 std::string(), std::string(), |
| 101 creation, creation, creation, | 101 creation, creation, creation, |
| 102 false, false, true, true)); | 102 false, false)); |
| 103 } | 103 } |
| 104 | 104 |
| 105 virtual void SetUp() { | 105 virtual void SetUp() { |
| 106 ui_thread_.Start(); | 106 ui_thread_.Start(); |
| 107 db_thread_.Start(); | 107 db_thread_.Start(); |
| 108 io_thread_.Start(); | 108 io_thread_.Start(); |
| 109 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 109 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 110 } | 110 } |
| 111 | 111 |
| 112 protected: | 112 protected: |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 } | 328 } |
| 329 | 329 |
| 330 // Test loading old session cookies from the disk. | 330 // Test loading old session cookies from the disk. |
| 331 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { | 331 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { |
| 332 InitializeStore(true); | 332 InitializeStore(true); |
| 333 | 333 |
| 334 // Add a session cookie. | 334 // Add a session cookie. |
| 335 store_->AddCookie( | 335 store_->AddCookie( |
| 336 CookieMonster::CanonicalCookie( | 336 CookieMonster::CanonicalCookie( |
| 337 GURL(), "C", "D", "sessioncookie.com", "/", std::string(), | 337 GURL(), "C", "D", "sessioncookie.com", "/", std::string(), |
| 338 std::string(), base::Time::Now(), base::Time::Now(), | 338 std::string(), base::Time::Now(), base::Time(), |
| 339 base::Time::Now(), false, false, true, false /*is_persistent*/)); | 339 base::Time::Now(), false, false)); |
| 340 | 340 |
| 341 // Force the store to write its data to the disk. | 341 // Force the store to write its data to the disk. |
| 342 DestroyStore(); | 342 DestroyStore(); |
| 343 | 343 |
| 344 // Create a store that loads session cookies and test that the session cookie | 344 // Create a store that loads session cookies and test that the session cookie |
| 345 // was loaded. | 345 // was loaded. |
| 346 CanonicalCookieVector cookies; | 346 CanonicalCookieVector cookies; |
| 347 CreateAndLoad(true, &cookies); | 347 CreateAndLoad(true, &cookies); |
| 348 | 348 |
| 349 ASSERT_EQ(1U, cookies.size()); | 349 ASSERT_EQ(1U, cookies.size()); |
| 350 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); | 350 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); |
| 351 ASSERT_STREQ("C", cookies[0]->Name().c_str()); | 351 ASSERT_STREQ("C", cookies[0]->Name().c_str()); |
| 352 ASSERT_STREQ("D", cookies[0]->Value().c_str()); | 352 ASSERT_STREQ("D", cookies[0]->Value().c_str()); |
| 353 | 353 |
| 354 STLDeleteElements(&cookies); | 354 STLDeleteElements(&cookies); |
| 355 } | 355 } |
| 356 | 356 |
| 357 // Test loading old session cookies from the disk. | 357 // Test loading old session cookies from the disk. |
| 358 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { | 358 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { |
| 359 InitializeStore(true); | 359 InitializeStore(true); |
| 360 | 360 |
| 361 // Add a session cookie. | 361 // Add a session cookie. |
| 362 store_->AddCookie( | 362 store_->AddCookie( |
| 363 CookieMonster::CanonicalCookie( | 363 CookieMonster::CanonicalCookie( |
| 364 GURL(), "C", "D", "sessioncookie.com", "/", std::string(), | 364 GURL(), "C", "D", "sessioncookie.com", "/", std::string(), |
| 365 std::string(), base::Time::Now(), base::Time::Now(), | 365 std::string(), base::Time::Now(), base::Time(), |
| 366 base::Time::Now(), false, false, true, false /*is_persistent*/)); | 366 base::Time::Now(), false, false)); |
| 367 | 367 |
| 368 // Force the store to write its data to the disk. | 368 // Force the store to write its data to the disk. |
| 369 DestroyStore(); | 369 DestroyStore(); |
| 370 | 370 |
| 371 // Create a store that doesn't load old session cookies and test that the | 371 // Create a store that doesn't load old session cookies and test that the |
| 372 // session cookie was not loaded. | 372 // session cookie was not loaded. |
| 373 CanonicalCookieVector cookies; | 373 CanonicalCookieVector cookies; |
| 374 CreateAndLoad(false, &cookies); | 374 CreateAndLoad(false, &cookies); |
| 375 ASSERT_EQ(0U, cookies.size()); | 375 ASSERT_EQ(0U, cookies.size()); |
| 376 | 376 |
| 377 // The store should also delete the session cookie. Wait until that has been | 377 // The store should also delete the session cookie. Wait until that has been |
| 378 // done. | 378 // done. |
| 379 DestroyStore(); | 379 DestroyStore(); |
| 380 | 380 |
| 381 // Create a store that loads old session cookies and test that the session | 381 // Create a store that loads old session cookies and test that the session |
| 382 // cookie is gone. | 382 // cookie is gone. |
| 383 CreateAndLoad(true, &cookies); | 383 CreateAndLoad(true, &cookies); |
| 384 ASSERT_EQ(0U, cookies.size()); | 384 ASSERT_EQ(0U, cookies.size()); |
| 385 } | 385 } |
| 386 | 386 |
| 387 TEST_F(SQLitePersistentCookieStoreTest, PersistHasExpiresAndIsPersistent) { | 387 TEST_F(SQLitePersistentCookieStoreTest, PersistHasExpires) { |
|
erikwright (departed)
2012/07/11 13:56:34
nit: rename to PersistIsPersistent
| |
| 388 InitializeStore(true); | 388 InitializeStore(true); |
| 389 static const char kSessionHasExpiresName[] = "session-hasexpires"; | 389 static const char kSessionName[] = "session"; |
| 390 static const char kSessionNoExpiresName[] = "session-noexpires"; | |
| 391 static const char kPersistentName[] = "persistent"; | 390 static const char kPersistentName[] = "persistent"; |
| 392 | 391 |
| 393 // Add a session cookie with has_expires = false, and another session cookie | 392 // Add a session cookie. |
| 394 // with has_expires = true. | |
| 395 store_->AddCookie( | 393 store_->AddCookie( |
| 396 CookieMonster::CanonicalCookie( | 394 CookieMonster::CanonicalCookie( |
| 397 GURL(), kSessionHasExpiresName, "val", "sessioncookie.com", "/", | 395 GURL(), kSessionName, "val", "sessioncookie.com", "/", |
| 398 std::string(), std::string(), | 396 std::string(), std::string(), |
| 399 base::Time::Now() - base::TimeDelta::FromDays(3), base::Time::Now(), | 397 base::Time::Now(), base::Time(), base::Time::Now(), |
| 400 base::Time::Now(), false, false, true /* has_expires */, | 398 false, false)); |
| 401 false /* is_persistent */)); | |
| 402 store_->AddCookie( | |
| 403 CookieMonster::CanonicalCookie( | |
| 404 GURL(), kSessionNoExpiresName, "val", "sessioncookie.com", "/", | |
| 405 std::string(), std::string(), | |
| 406 base::Time::Now() - base::TimeDelta::FromDays(2), base::Time::Now(), | |
| 407 base::Time::Now(), false, false, false /* has_expires */, | |
| 408 false /* is_persistent */)); | |
| 409 // Add a persistent cookie. | 399 // Add a persistent cookie. |
| 410 store_->AddCookie( | 400 store_->AddCookie( |
| 411 CookieMonster::CanonicalCookie( | 401 CookieMonster::CanonicalCookie( |
| 412 GURL(), kPersistentName, "val", "sessioncookie.com", "/", | 402 GURL(), kPersistentName, "val", "sessioncookie.com", "/", |
| 413 std::string(), std::string(), | 403 std::string(), std::string(), |
| 414 base::Time::Now() - base::TimeDelta::FromDays(1), base::Time::Now(), | 404 base::Time::Now() - base::TimeDelta::FromDays(1), base::Time::Now(), |
| 415 base::Time::Now(), false, false, true /* has_expires */, | 405 base::Time::Now(), false, false)); |
| 416 true /* is_persistent */)); | |
| 417 | 406 |
| 418 // Force the store to write its data to the disk. | 407 // Create a store that loads session cookie and test that the the IsPersistent |
| 419 DestroyStore(); | 408 // attribute is restored. |
| 420 | |
| 421 // Create a store that loads session cookies and test that the the DoesExpire | |
| 422 // and IsPersistent attributes are restored. | |
| 423 CanonicalCookieVector cookies; | 409 CanonicalCookieVector cookies; |
| 424 CreateAndLoad(true, &cookies); | 410 CreateAndLoad(true, &cookies); |
| 425 ASSERT_EQ(3U, cookies.size()); | 411 ASSERT_EQ(2U, cookies.size()); |
| 426 | 412 |
| 427 std::map<std::string, CookieMonster::CanonicalCookie*> cookie_map; | 413 std::map<std::string, CookieMonster::CanonicalCookie*> cookie_map; |
| 428 for (CanonicalCookieVector::const_iterator it = cookies.begin(); | 414 for (CanonicalCookieVector::const_iterator it = cookies.begin(); |
| 429 it != cookies.end(); | 415 it != cookies.end(); |
| 430 ++it) { | 416 ++it) { |
| 431 cookie_map[(*it)->Name()] = *it; | 417 cookie_map[(*it)->Name()] = *it; |
| 432 } | 418 } |
| 433 | 419 |
| 434 std::map<std::string, CookieMonster::CanonicalCookie*>::const_iterator it = | 420 std::map<std::string, CookieMonster::CanonicalCookie*>::const_iterator it = |
| 435 cookie_map.find(kSessionHasExpiresName); | 421 cookie_map.find(kSessionName); |
| 436 ASSERT_TRUE(it != cookie_map.end()); | 422 ASSERT_TRUE(it != cookie_map.end()); |
| 437 EXPECT_TRUE(cookie_map[kSessionHasExpiresName]->DoesExpire()); | 423 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); |
| 438 EXPECT_FALSE(cookie_map[kSessionHasExpiresName]->IsPersistent()); | |
| 439 | |
| 440 it = cookie_map.find(kSessionNoExpiresName); | |
| 441 ASSERT_TRUE(it != cookie_map.end()); | |
| 442 EXPECT_FALSE(cookie_map[kSessionNoExpiresName]->DoesExpire()); | |
| 443 EXPECT_FALSE(cookie_map[kSessionNoExpiresName]->IsPersistent()); | |
| 444 | 424 |
| 445 it = cookie_map.find(kPersistentName); | 425 it = cookie_map.find(kPersistentName); |
| 446 ASSERT_TRUE(it != cookie_map.end()); | 426 ASSERT_TRUE(it != cookie_map.end()); |
| 447 EXPECT_TRUE(cookie_map[kPersistentName]->DoesExpire()); | |
| 448 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); | 427 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); |
| 449 | 428 |
| 450 STLDeleteElements(&cookies); | 429 STLDeleteElements(&cookies); |
| 451 } | 430 } |
| 452 | 431 |
| 453 namespace { | 432 namespace { |
| 454 | 433 |
| 455 // True if the given cookie is in the vector. | 434 // True if the given cookie is in the vector. |
| 456 bool IsCookiePresent(const CanonicalCookieVector& cookies, | 435 bool IsCookiePresent(const CanonicalCookieVector& cookies, |
| 457 const std::string& domain, | 436 const std::string& domain, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 t += base::TimeDelta::FromInternalValue(10); | 478 t += base::TimeDelta::FromInternalValue(10); |
| 500 AddCookie("B", "2", session_origin, "/", t); | 479 AddCookie("B", "2", session_origin, "/", t); |
| 501 t += base::TimeDelta::FromInternalValue(10); | 480 t += base::TimeDelta::FromInternalValue(10); |
| 502 AddCookie("C", "3", other_origin, "/", t); | 481 AddCookie("C", "3", other_origin, "/", t); |
| 503 // A secure cookie on session_origin. | 482 // A secure cookie on session_origin. |
| 504 t += base::TimeDelta::FromInternalValue(10); | 483 t += base::TimeDelta::FromInternalValue(10); |
| 505 store_->AddCookie( | 484 store_->AddCookie( |
| 506 CookieMonster::CanonicalCookie(GURL(), "D", "4", session_origin, "/", | 485 CookieMonster::CanonicalCookie(GURL(), "D", "4", session_origin, "/", |
| 507 std::string(), std::string(), | 486 std::string(), std::string(), |
| 508 t, t, t, | 487 t, t, t, |
| 509 true, false, true, true)); | 488 true, false)); |
| 510 | 489 |
| 511 // First, check that we can override the policy. | 490 // First, check that we can override the policy. |
| 512 store_->SetForceKeepSessionState(); | 491 store_->SetForceKeepSessionState(); |
| 513 | 492 |
| 514 // Force the store to write its data to the disk. | 493 // Force the store to write its data to the disk. |
| 515 DestroyStore(); | 494 DestroyStore(); |
| 516 | 495 |
| 517 // Create a store test that the cookie on session_origin does not exist. | 496 // Create a store test that the cookie on session_origin does not exist. |
| 518 store_ = new SQLitePersistentCookieStore( | 497 store_ = new SQLitePersistentCookieStore( |
| 519 temp_dir_.path().Append(chrome::kCookieFilename), | 498 temp_dir_.path().Append(chrome::kCookieFilename), |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 539 Load(&cookies); | 518 Load(&cookies); |
| 540 | 519 |
| 541 EXPECT_EQ(3U, cookies.size()); | 520 EXPECT_EQ(3U, cookies.size()); |
| 542 EXPECT_TRUE(IsCookiePresent(cookies, protected_origin, "A", "1", false)); | 521 EXPECT_TRUE(IsCookiePresent(cookies, protected_origin, "A", "1", false)); |
| 543 EXPECT_TRUE(IsCookiePresent(cookies, other_origin, "C", "3", false)); | 522 EXPECT_TRUE(IsCookiePresent(cookies, other_origin, "C", "3", false)); |
| 544 EXPECT_TRUE(IsCookiePresent(cookies, session_origin, "D", "4", true)); | 523 EXPECT_TRUE(IsCookiePresent(cookies, session_origin, "D", "4", true)); |
| 545 | 524 |
| 546 DestroyStore(); | 525 DestroyStore(); |
| 547 STLDeleteElements(&cookies); | 526 STLDeleteElements(&cookies); |
| 548 } | 527 } |
| OLD | NEW |