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 "net/extras/sqlite/sqlite_persistent_cookie_store.h" | 5 #include "net/extras/sqlite/sqlite_persistent_cookie_store.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/memory/ptr_util.h" |
16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
17 #include "base/sequenced_task_runner.h" | 18 #include "base/sequenced_task_runner.h" |
18 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
19 #include "base/test/sequenced_worker_pool_owner.h" | 20 #include "base/test/sequenced_worker_pool_owner.h" |
20 #include "base/threading/sequenced_worker_pool.h" | 21 #include "base/threading/sequenced_worker_pool.h" |
21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
22 #include "crypto/encryptor.h" | 23 #include "crypto/encryptor.h" |
23 #include "crypto/symmetric_key.h" | 24 #include "crypto/symmetric_key.h" |
24 #include "net/cookies/canonical_cookie.h" | 25 #include "net/cookies/canonical_cookie.h" |
25 #include "net/cookies/cookie_constants.h" | 26 #include "net/cookies/cookie_constants.h" |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 | 463 |
463 ASSERT_EQ(1U, cookies.size()); | 464 ASSERT_EQ(1U, cookies.size()); |
464 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); | 465 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); |
465 ASSERT_STREQ("C", cookies[0]->Name().c_str()); | 466 ASSERT_STREQ("C", cookies[0]->Name().c_str()); |
466 ASSERT_STREQ("D", cookies[0]->Value().c_str()); | 467 ASSERT_STREQ("D", cookies[0]->Value().c_str()); |
467 ASSERT_EQ(COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); | 468 ASSERT_EQ(COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); |
468 | 469 |
469 cookies.clear(); | 470 cookies.clear(); |
470 } | 471 } |
471 | 472 |
472 // Test loading old session cookies from the disk. | 473 // Test refusing to load old session cookies from the disk. |
473 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { | 474 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { |
474 InitializeStore(false, true); | 475 InitializeStore(false, true); |
475 | 476 |
476 // Add a session cookie. | 477 // Add a session cookie. |
477 store_->AddCookie( | 478 store_->AddCookie( |
478 CanonicalCookie("C", "D", "sessioncookie.com", "/", base::Time::Now(), | 479 CanonicalCookie("C", "D", "sessioncookie.com", "/", base::Time::Now(), |
479 base::Time(), base::Time(), false, false, | 480 base::Time(), base::Time(), false, false, |
480 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); | 481 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); |
481 | 482 |
482 // Force the store to write its data to the disk. | 483 // Force the store to write its data to the disk. |
483 DestroyStore(); | 484 DestroyStore(); |
484 | 485 |
485 // Create a store that doesn't load old session cookies and test that the | 486 // Create a store that doesn't load old session cookies and test that the |
486 // session cookie was not loaded. | 487 // session cookie was not loaded. |
487 CanonicalCookieVector cookies; | 488 CanonicalCookieVector cookies; |
488 CreateAndLoad(false, false, &cookies); | 489 CreateAndLoad(false, false, &cookies); |
489 ASSERT_EQ(0U, cookies.size()); | 490 ASSERT_EQ(0U, cookies.size()); |
490 | 491 |
491 // The store should also delete the session cookie. Wait until that has been | 492 // The store should also delete the session cookie. Wait until that has been |
492 // done. | 493 // done. |
493 DestroyStore(); | 494 DestroyStore(); |
494 | 495 |
495 // Create a store that loads old session cookies and test that the session | 496 // Create a store that loads old session cookies and test that the session |
496 // cookie is gone. | 497 // cookie is gone. |
497 CreateAndLoad(false, true, &cookies); | 498 CreateAndLoad(false, true, &cookies); |
498 ASSERT_EQ(0U, cookies.size()); | 499 ASSERT_EQ(0U, cookies.size()); |
499 } | 500 } |
500 | 501 |
| 502 // Confirm bad cookies on disk don't get looaded |
| 503 TEST_F(SQLitePersistentCookieStoreTest, FilterBadCookies) { |
| 504 // Create an on-disk store. |
| 505 InitializeStore(false, true); |
| 506 DestroyStore(); |
| 507 |
| 508 // Add some cookies in by hand. |
| 509 base::FilePath store_name(temp_dir_.GetPath().Append(kCookieFilename)); |
| 510 std::unique_ptr<sql::Connection> db(base::MakeUnique<sql::Connection>()); |
| 511 ASSERT_TRUE(db->Open(store_name)); |
| 512 sql::Statement stmt(db->GetUniqueStatement( |
| 513 "INSERT INTO cookies (creation_utc, host_key, name, value, " |
| 514 "encrypted_value, path, expires_utc, secure, httponly, " |
| 515 "firstpartyonly, last_access_utc, has_expires, persistent, priority) " |
| 516 "VALUES (?,'google.izzle',?,?,'',?,0,0,0,0,0,1,1,0)")); |
| 517 ASSERT_TRUE(stmt.is_valid()); |
| 518 |
| 519 struct CookieInfo { |
| 520 const char* name; |
| 521 const char* value; |
| 522 const char* path; |
| 523 } cookies_info[] = {// A couple non-canonical cookies. |
| 524 {"", "B", "/path"}, |
| 525 {"C ", "D", "/path"}, |
| 526 |
| 527 // A canonical cookie. |
| 528 {"E", "F", "/path"}}; |
| 529 |
| 530 int64_t creation_time = 1; |
| 531 for (int i = 0; |
| 532 static_cast<unsigned int>(i) < sizeof(cookies_info) / sizeof(CookieInfo); |
| 533 i++) { |
| 534 stmt.Reset(true); |
| 535 |
| 536 stmt.BindInt64(0, creation_time++); |
| 537 stmt.BindString(1, cookies_info[i].name); |
| 538 stmt.BindString(2, cookies_info[i].value); |
| 539 stmt.BindString(3, cookies_info[i].path); |
| 540 ASSERT_TRUE(stmt.Run()); |
| 541 } |
| 542 stmt.Clear(); |
| 543 db.reset(); |
| 544 |
| 545 // Reopen the store and confirm that the only cookie loaded is the |
| 546 // canonical one. |
| 547 CanonicalCookieVector cookies; |
| 548 CreateAndLoad(false, false, &cookies); |
| 549 ASSERT_EQ(1U, cookies.size()); |
| 550 EXPECT_STREQ("E", cookies[0]->Name().c_str()); |
| 551 EXPECT_STREQ("F", cookies[0]->Value().c_str()); |
| 552 EXPECT_STREQ("/path", cookies[0]->Path().c_str()); |
| 553 DestroyStore(); |
| 554 } |
| 555 |
501 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { | 556 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { |
502 InitializeStore(false, true); | 557 InitializeStore(false, true); |
503 static const char kSessionName[] = "session"; | 558 static const char kSessionName[] = "session"; |
504 static const char kPersistentName[] = "persistent"; | 559 static const char kPersistentName[] = "persistent"; |
505 | 560 |
506 // Add a session cookie. | 561 // Add a session cookie. |
507 store_->AddCookie(CanonicalCookie( | 562 store_->AddCookie(CanonicalCookie( |
508 kSessionName, "val", "sessioncookie.com", "/", base::Time::Now(), | 563 kSessionName, "val", "sessioncookie.com", "/", base::Time::Now(), |
509 base::Time(), base::Time(), false, false, CookieSameSite::DEFAULT_MODE, | 564 base::Time(), base::Time(), false, false, CookieSameSite::DEFAULT_MODE, |
510 COOKIE_PRIORITY_DEFAULT)); | 565 COOKIE_PRIORITY_DEFAULT)); |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
804 EXPECT_TRUE(was_called_with_no_cookies); | 859 EXPECT_TRUE(was_called_with_no_cookies); |
805 | 860 |
806 // Same with trying to load a specific cookie. | 861 // Same with trying to load a specific cookie. |
807 was_called_with_no_cookies = false; | 862 was_called_with_no_cookies = false; |
808 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, | 863 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, |
809 &was_called_with_no_cookies)); | 864 &was_called_with_no_cookies)); |
810 EXPECT_TRUE(was_called_with_no_cookies); | 865 EXPECT_TRUE(was_called_with_no_cookies); |
811 } | 866 } |
812 | 867 |
813 } // namespace net | 868 } // namespace net |
OLD | NEW |