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 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
16 #include "base/location.h" | 16 #include "base/location.h" |
| 17 #include "base/memory/ptr_util.h" |
17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
18 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
19 #include "base/sequenced_task_runner.h" | 20 #include "base/sequenced_task_runner.h" |
20 #include "base/synchronization/waitable_event.h" | 21 #include "base/synchronization/waitable_event.h" |
21 #include "base/task_scheduler/post_task.h" | 22 #include "base/task_scheduler/post_task.h" |
22 #include "base/test/scoped_task_environment.h" | 23 #include "base/test/scoped_task_environment.h" |
23 #include "base/time/time.h" | 24 #include "base/time/time.h" |
24 #include "crypto/encryptor.h" | 25 #include "crypto/encryptor.h" |
25 #include "crypto/symmetric_key.h" | 26 #include "crypto/symmetric_key.h" |
26 #include "net/cookies/canonical_cookie.h" | 27 #include "net/cookies/canonical_cookie.h" |
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 | 474 |
474 ASSERT_EQ(1U, cookies.size()); | 475 ASSERT_EQ(1U, cookies.size()); |
475 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); | 476 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); |
476 ASSERT_STREQ("C", cookies[0]->Name().c_str()); | 477 ASSERT_STREQ("C", cookies[0]->Name().c_str()); |
477 ASSERT_STREQ("D", cookies[0]->Value().c_str()); | 478 ASSERT_STREQ("D", cookies[0]->Value().c_str()); |
478 ASSERT_EQ(COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); | 479 ASSERT_EQ(COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); |
479 | 480 |
480 cookies.clear(); | 481 cookies.clear(); |
481 } | 482 } |
482 | 483 |
483 // Test loading old session cookies from the disk. | 484 // Test refusing to load old session cookies from the disk. |
484 // TODO(mattcary): disabled for possibly causing iOS timeouts: crbug.com/727566. | 485 // TODO(mattcary): disabled for possibly causing iOS timeouts: crbug.com/727566. |
485 TEST_F(SQLitePersistentCookieStoreTest, | 486 TEST_F(SQLitePersistentCookieStoreTest, |
486 DISABLED_TestDontLoadOldSessionCookies) { | 487 DISABLED_TestDontLoadOldSessionCookies) { |
487 InitializeStore(false, true); | 488 InitializeStore(false, true); |
488 | 489 |
489 // Add a session cookie. | 490 // Add a session cookie. |
490 store_->AddCookie( | 491 store_->AddCookie( |
491 CanonicalCookie("C", "D", "sessioncookie.com", "/", base::Time::Now(), | 492 CanonicalCookie("C", "D", "sessioncookie.com", "/", base::Time::Now(), |
492 base::Time(), base::Time(), false, false, | 493 base::Time(), base::Time(), false, false, |
493 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); | 494 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); |
(...skipping 10 matching lines...) Expand all Loading... |
504 // The store should also delete the session cookie. Wait until that has been | 505 // The store should also delete the session cookie. Wait until that has been |
505 // done. | 506 // done. |
506 DestroyStore(); | 507 DestroyStore(); |
507 | 508 |
508 // Create a store that loads old session cookies and test that the session | 509 // Create a store that loads old session cookies and test that the session |
509 // cookie is gone. | 510 // cookie is gone. |
510 CreateAndLoad(false, true, &cookies); | 511 CreateAndLoad(false, true, &cookies); |
511 ASSERT_EQ(0U, cookies.size()); | 512 ASSERT_EQ(0U, cookies.size()); |
512 } | 513 } |
513 | 514 |
| 515 // Confirm bad cookies on disk don't get looaded |
| 516 TEST_F(SQLitePersistentCookieStoreTest, FilterBadCookies) { |
| 517 // Create an on-disk store. |
| 518 InitializeStore(false, true); |
| 519 DestroyStore(); |
| 520 |
| 521 // Add some cookies in by hand. |
| 522 base::FilePath store_name(temp_dir_.GetPath().Append(kCookieFilename)); |
| 523 std::unique_ptr<sql::Connection> db(base::MakeUnique<sql::Connection>()); |
| 524 ASSERT_TRUE(db->Open(store_name)); |
| 525 sql::Statement stmt(db->GetUniqueStatement( |
| 526 "INSERT INTO cookies (creation_utc, host_key, name, value, " |
| 527 "encrypted_value, path, expires_utc, secure, httponly, " |
| 528 "firstpartyonly, last_access_utc, has_expires, persistent, priority) " |
| 529 "VALUES (?,'google.izzle',?,?,'',?,0,0,0,0,0,1,1,0)")); |
| 530 ASSERT_TRUE(stmt.is_valid()); |
| 531 |
| 532 struct CookieInfo { |
| 533 const char* name; |
| 534 const char* value; |
| 535 const char* path; |
| 536 } cookies_info[] = {// A couple non-canonical cookies. |
| 537 {"A=", "B", "/path"}, |
| 538 {"C ", "D", "/path"}, |
| 539 |
| 540 // A canonical cookie. |
| 541 {"E", "F", "/path"}}; |
| 542 |
| 543 int64_t creation_time = 1; |
| 544 for (auto& cookie_info : cookies_info) { |
| 545 stmt.Reset(true); |
| 546 |
| 547 stmt.BindInt64(0, creation_time++); |
| 548 stmt.BindString(1, cookie_info.name); |
| 549 stmt.BindString(2, cookie_info.value); |
| 550 stmt.BindString(3, cookie_info.path); |
| 551 ASSERT_TRUE(stmt.Run()); |
| 552 } |
| 553 stmt.Clear(); |
| 554 db.reset(); |
| 555 |
| 556 // Reopen the store and confirm that the only cookie loaded is the |
| 557 // canonical one. |
| 558 CanonicalCookieVector cookies; |
| 559 CreateAndLoad(false, false, &cookies); |
| 560 ASSERT_EQ(1U, cookies.size()); |
| 561 EXPECT_STREQ("E", cookies[0]->Name().c_str()); |
| 562 EXPECT_STREQ("F", cookies[0]->Value().c_str()); |
| 563 EXPECT_STREQ("/path", cookies[0]->Path().c_str()); |
| 564 DestroyStore(); |
| 565 } |
| 566 |
514 // TODO(mattcary): disabled for possibly causing iOS timeouts: crbug.com/727566. | 567 // TODO(mattcary): disabled for possibly causing iOS timeouts: crbug.com/727566. |
515 TEST_F(SQLitePersistentCookieStoreTest, DISABLED_PersistIsPersistent) { | 568 TEST_F(SQLitePersistentCookieStoreTest, DISABLED_PersistIsPersistent) { |
516 InitializeStore(false, true); | 569 InitializeStore(false, true); |
517 static const char kSessionName[] = "session"; | 570 static const char kSessionName[] = "session"; |
518 static const char kPersistentName[] = "persistent"; | 571 static const char kPersistentName[] = "persistent"; |
519 | 572 |
520 // Add a session cookie. | 573 // Add a session cookie. |
521 store_->AddCookie(CanonicalCookie( | 574 store_->AddCookie(CanonicalCookie( |
522 kSessionName, "val", "sessioncookie.com", "/", base::Time::Now(), | 575 kSessionName, "val", "sessioncookie.com", "/", base::Time::Now(), |
523 base::Time(), base::Time(), false, false, CookieSameSite::DEFAULT_MODE, | 576 base::Time(), base::Time(), false, false, CookieSameSite::DEFAULT_MODE, |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
823 EXPECT_TRUE(was_called_with_no_cookies); | 876 EXPECT_TRUE(was_called_with_no_cookies); |
824 | 877 |
825 // Same with trying to load a specific cookie. | 878 // Same with trying to load a specific cookie. |
826 was_called_with_no_cookies = false; | 879 was_called_with_no_cookies = false; |
827 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, | 880 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, |
828 &was_called_with_no_cookies)); | 881 &was_called_with_no_cookies)); |
829 EXPECT_TRUE(was_called_with_no_cookies); | 882 EXPECT_TRUE(was_called_with_no_cookies); |
830 } | 883 } |
831 | 884 |
832 } // namespace net | 885 } // namespace net |
OLD | NEW |