| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "ios/net/cookies/cookie_store_ios.h" | 5 #include "ios/net/cookies/cookie_store_ios.h" |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 | 366 |
| 367 class CookieStoreIOSWithBackend : public testing::Test { | 367 class CookieStoreIOSWithBackend : public testing::Test { |
| 368 public: | 368 public: |
| 369 CookieStoreIOSWithBackend() | 369 CookieStoreIOSWithBackend() |
| 370 : kTestCookieURL("http://foo.google.com/bar"), | 370 : kTestCookieURL("http://foo.google.com/bar"), |
| 371 kTestCookieURL2("http://foo.google.com/baz"), | 371 kTestCookieURL2("http://foo.google.com/baz"), |
| 372 kTestCookieURL3("http://foo.google.com"), | 372 kTestCookieURL3("http://foo.google.com"), |
| 373 kTestCookieURL4("http://bar.google.com/bar"), | 373 kTestCookieURL4("http://bar.google.com/bar"), |
| 374 backend_(new TestPersistentCookieStore), | 374 backend_(new TestPersistentCookieStore), |
| 375 store_(new net::CookieStoreIOS(backend_.get())) { | 375 store_(new net::CookieStoreIOS(backend_.get())) { |
| 376 net::CookieStoreIOS::SetCookiePolicy(net::CookieStoreIOS::ALLOW); | |
| 377 cookie_changed_callback_ = store_->AddCallbackForCookie( | 376 cookie_changed_callback_ = store_->AddCallbackForCookie( |
| 378 kTestCookieURL, "abc", | 377 kTestCookieURL, "abc", |
| 379 base::Bind(&RecordCookieChanges, &cookies_changed_, &cookies_removed_)); | 378 base::Bind(&RecordCookieChanges, &cookies_changed_, &cookies_removed_)); |
| 380 } | 379 } |
| 381 | 380 |
| 382 ~CookieStoreIOSWithBackend() override {} | 381 ~CookieStoreIOSWithBackend() override {} |
| 383 | 382 |
| 384 // Gets the cookies. |callback| will be called on completion. | 383 // Gets the cookies. |callback| will be called on completion. |
| 385 void GetCookies(const net::CookieStore::GetCookiesCallback& callback) { | 384 void GetCookies(const net::CookieStore::GetCookiesCallback& callback) { |
| 386 net::CookieOptions options; | 385 net::CookieOptions options; |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 537 base::MessageLoop loop; | 536 base::MessageLoop loop; |
| 538 const GURL kTestCookieURL("http://foo.google.com/bar"); | 537 const GURL kTestCookieURL("http://foo.google.com/bar"); |
| 539 ClearCookies(); | 538 ClearCookies(); |
| 540 std::unique_ptr<CookieStoreIOS> cookie_store(new CookieStoreIOS(nullptr)); | 539 std::unique_ptr<CookieStoreIOS> cookie_store(new CookieStoreIOS(nullptr)); |
| 541 CookieStoreIOS::SwitchSynchronizedStore(nullptr, cookie_store.get()); | 540 CookieStoreIOS::SwitchSynchronizedStore(nullptr, cookie_store.get()); |
| 542 // Add a cookie. | 541 // Add a cookie. |
| 543 net::CookieOptions options; | 542 net::CookieOptions options; |
| 544 options.set_include_httponly(); | 543 options.set_include_httponly(); |
| 545 cookie_store->SetCookieWithOptionsAsync( | 544 cookie_store->SetCookieWithOptionsAsync( |
| 546 kTestCookieURL, "a=b", options, net::CookieStore::SetCookiesCallback()); | 545 kTestCookieURL, "a=b", options, net::CookieStore::SetCookiesCallback()); |
| 547 // Disallow cookies. | 546 // Check we can get the cookie. |
| 548 CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::BLOCK); | |
| 549 // No cookie in the system store. | |
| 550 NSHTTPCookieStorage* system_store = | |
| 551 [NSHTTPCookieStorage sharedHTTPCookieStorage]; | |
| 552 EXPECT_EQ(0u, [[system_store cookies] count]); | |
| 553 // Flushing should not have any effect. | |
| 554 cookie_store->FlushStore(base::Closure()); | |
| 555 // Check we can get the cookie even though cookies are disabled. | |
| 556 GetAllCookiesCallback callback; | 547 GetAllCookiesCallback callback; |
| 557 cookie_store->GetAllCookiesForURLAsync( | 548 cookie_store->GetAllCookiesForURLAsync( |
| 558 kTestCookieURL, | 549 kTestCookieURL, |
| 559 base::Bind(&GetAllCookiesCallback::Run, base::Unretained(&callback))); | 550 base::Bind(&GetAllCookiesCallback::Run, base::Unretained(&callback))); |
| 560 EXPECT_TRUE(callback.did_run()); | 551 EXPECT_TRUE(callback.did_run()); |
| 561 EXPECT_EQ(1u, callback.cookie_list().size()); | 552 EXPECT_EQ(1u, callback.cookie_list().size()); |
| 562 net::CanonicalCookie cookie = callback.cookie_list()[0]; | 553 net::CanonicalCookie cookie = callback.cookie_list()[0]; |
| 563 EXPECT_EQ("a", cookie.Name()); | 554 EXPECT_EQ("a", cookie.Name()); |
| 564 EXPECT_EQ("b", cookie.Value()); | 555 EXPECT_EQ("b", cookie.Value()); |
| 565 // Re-enable cookies. | |
| 566 CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::ALLOW); | |
| 567 // Cookie is back in the system store. | |
| 568 EXPECT_EQ(1u, [[system_store cookies] count]); | |
| 569 cookie_store->UnSynchronize(); | |
| 570 } | 556 } |
| 571 | 557 |
| 572 // Tests that cookies can be read before the backend is loaded. | 558 // Tests that cookies can be read before the backend is loaded. |
| 573 TEST_F(CookieStoreIOSWithBackend, NotSynchronized) { | 559 TEST_F(CookieStoreIOSWithBackend, NotSynchronized) { |
| 574 // Start fetching the cookie. | 560 // Start fetching the cookie. |
| 575 GetCookieCallback callback; | 561 GetCookieCallback callback; |
| 576 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback))); | 562 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback))); |
| 577 // Backend loading completes. | 563 // Backend loading completes. |
| 578 backend_->RunLoadedCallback(); | 564 backend_->RunLoadedCallback(); |
| 579 EXPECT_TRUE(callback.did_run()); | 565 EXPECT_TRUE(callback.did_run()); |
| 580 EXPECT_EQ("a=b", callback.cookie_line()); | 566 EXPECT_EQ("a=b", callback.cookie_line()); |
| 581 } | 567 } |
| 582 | 568 |
| 583 // Tests that cookies can be read before synchronization is complete. | 569 // Tests that cookies can be read before synchronization is complete. |
| 584 TEST_F(CookieStoreIOSWithBackend, Synchronizing) { | 570 TEST_F(CookieStoreIOSWithBackend, Synchronizing) { |
| 585 // Start synchronization. | 571 // Start synchronization. |
| 586 CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get()); | 572 CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get()); |
| 587 GetCookieCallback callback; | 573 GetCookieCallback callback; |
| 588 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback))); | 574 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback))); |
| 589 // Backend loading completes (end of synchronization). | 575 // Backend loading completes (end of synchronization). |
| 590 backend_->RunLoadedCallback(); | 576 backend_->RunLoadedCallback(); |
| 591 EXPECT_TRUE(callback.did_run()); | 577 EXPECT_TRUE(callback.did_run()); |
| 592 EXPECT_EQ("a=b", callback.cookie_line()); | 578 EXPECT_EQ("a=b", callback.cookie_line()); |
| 593 store_->UnSynchronize(); | 579 store_->UnSynchronize(); |
| 594 } | 580 } |
| 595 | 581 |
| 596 // Tests that cookies can be read before synchronization is complete, when | |
| 597 // triggered by a change in cookie policy. | |
| 598 TEST_F(CookieStoreIOSWithBackend, SynchronizingAfterPolicyChange) { | |
| 599 ClearCookies(); | |
| 600 CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::BLOCK); | |
| 601 // SwitchSynchronizedStore() does nothing when cookies are blocked. | |
| 602 CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get()); | |
| 603 // Start synchronization by allowing cookies. | |
| 604 CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::ALLOW); | |
| 605 GetCookieCallback callback; | |
| 606 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback))); | |
| 607 // Backend loading completes (end of synchronization). | |
| 608 backend_->RunLoadedCallback(); | |
| 609 EXPECT_TRUE(callback.did_run()); | |
| 610 EXPECT_EQ("a=b", callback.cookie_line()); | |
| 611 store_->UnSynchronize(); | |
| 612 } | |
| 613 | |
| 614 // Tests that Synchronization can be "aborted" (i.e. the cookie store is | 582 // Tests that Synchronization can be "aborted" (i.e. the cookie store is |
| 615 // unsynchronized while synchronization is in progress). | 583 // unsynchronized while synchronization is in progress). |
| 616 TEST_F(CookieStoreIOSWithBackend, SyncThenUnsync) { | 584 TEST_F(CookieStoreIOSWithBackend, SyncThenUnsync) { |
| 617 ClearCookies(); | 585 ClearCookies(); |
| 618 std::unique_ptr<CookieStoreIOS> dummy_store(new CookieStoreIOS(nullptr)); | 586 std::unique_ptr<CookieStoreIOS> dummy_store(new CookieStoreIOS(nullptr)); |
| 619 // Switch back and forth before synchronization can complete. | 587 // Switch back and forth before synchronization can complete. |
| 620 CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get()); | 588 CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get()); |
| 621 CookieStoreIOS::SwitchSynchronizedStore(store_.get(), dummy_store.get()); | 589 CookieStoreIOS::SwitchSynchronizedStore(store_.get(), dummy_store.get()); |
| 622 backend_->RunLoadedCallback(); | 590 backend_->RunLoadedCallback(); |
| 623 // No cookie leak in the system store. | 591 // No cookie leak in the system store. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 645 // Cancel the synchronization. | 613 // Cancel the synchronization. |
| 646 CookieStoreIOS::SwitchSynchronizedStore(store_.get(), dummy_store.get()); | 614 CookieStoreIOS::SwitchSynchronizedStore(store_.get(), dummy_store.get()); |
| 647 // Synchronization completes after being cancelled. | 615 // Synchronization completes after being cancelled. |
| 648 backend_->RunLoadedCallback(); | 616 backend_->RunLoadedCallback(); |
| 649 // The task is not lost. | 617 // The task is not lost. |
| 650 EXPECT_TRUE(callback.did_run()); | 618 EXPECT_TRUE(callback.did_run()); |
| 651 EXPECT_EQ("a=b", callback.cookie_line()); | 619 EXPECT_EQ("a=b", callback.cookie_line()); |
| 652 dummy_store->UnSynchronize(); | 620 dummy_store->UnSynchronize(); |
| 653 } | 621 } |
| 654 | 622 |
| 655 TEST_F(CookieStoreIOSWithBackend, ChangePolicyOnceDuringSynchronization) { | |
| 656 // Start synchronization. | |
| 657 CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get()); | |
| 658 // Toggle cookie policy to trigger another synchronization while the first one | |
| 659 // is still in progress. | |
| 660 CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::BLOCK); | |
| 661 // Backend loading completes (end of synchronization). | |
| 662 backend_->RunLoadedCallback(); | |
| 663 CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::ALLOW); | |
| 664 GetCookieCallback callback; | |
| 665 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback))); | |
| 666 EXPECT_TRUE(callback.did_run()); | |
| 667 EXPECT_EQ("a=b", callback.cookie_line()); | |
| 668 store_->UnSynchronize(); | |
| 669 } | |
| 670 | |
| 671 TEST_F(CookieStoreIOSWithBackend, | |
| 672 ChangePolicyDuringSynchronizationWithPendingTask) { | |
| 673 // Start synchronization. | |
| 674 CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get()); | |
| 675 // Create a pending task while synchronization is in progress. | |
| 676 GetCookieCallback callback; | |
| 677 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback))); | |
| 678 // Toggle cookie policy to trigger another synchronization while the first one | |
| 679 // is still in progress. | |
| 680 CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::BLOCK); | |
| 681 // Backend loading completes (end of synchronization). | |
| 682 backend_->RunLoadedCallback(); | |
| 683 EXPECT_TRUE(callback.did_run()); | |
| 684 EXPECT_EQ("a=b", callback.cookie_line()); | |
| 685 store_->UnSynchronize(); | |
| 686 } | |
| 687 | |
| 688 TEST_F(CookieStoreIOSWithBackend, ChangePolicyTwiceDuringSynchronization) { | |
| 689 // Start synchronization. | |
| 690 CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get()); | |
| 691 // Toggle cookie policy to trigger another synchronization while the first one | |
| 692 // is still in progress. | |
| 693 CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::BLOCK); | |
| 694 CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::ALLOW); | |
| 695 // Backend loading completes (end of synchronization). | |
| 696 backend_->RunLoadedCallback(); | |
| 697 GetCookieCallback callback; | |
| 698 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback))); | |
| 699 EXPECT_TRUE(callback.did_run()); | |
| 700 EXPECT_EQ("a=b", callback.cookie_line()); | |
| 701 store_->UnSynchronize(); | |
| 702 } | |
| 703 | |
| 704 TEST_F(CookieStoreIOSWithBackend, UnSynchronizeBeforeLoadComplete) { | 623 TEST_F(CookieStoreIOSWithBackend, UnSynchronizeBeforeLoadComplete) { |
| 705 ClearCookies(); | 624 ClearCookies(); |
| 706 // Switch back and forth before synchronization can complete. | 625 // Switch back and forth before synchronization can complete. |
| 707 CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get()); | 626 CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get()); |
| 708 store_->UnSynchronize(); | 627 store_->UnSynchronize(); |
| 709 backend_->RunLoadedCallback(); | 628 backend_->RunLoadedCallback(); |
| 710 // No cookie lost. | 629 // No cookie lost. |
| 711 GetCookieCallback callback; | 630 GetCookieCallback callback; |
| 712 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback))); | 631 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback))); |
| 713 EXPECT_TRUE(callback.did_run()); | 632 EXPECT_TRUE(callback.did_run()); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 760 backend_->RunLoadedCallback(); | 679 backend_->RunLoadedCallback(); |
| 761 EXPECT_FALSE(backend_->flushed()); | 680 EXPECT_FALSE(backend_->flushed()); |
| 762 | 681 |
| 763 // The store should be flushed even if it is not dirty. | 682 // The store should be flushed even if it is not dirty. |
| 764 store_->FlushStore(base::Closure()); | 683 store_->FlushStore(base::Closure()); |
| 765 EXPECT_TRUE(backend_->flushed()); | 684 EXPECT_TRUE(backend_->flushed()); |
| 766 | 685 |
| 767 store_->UnSynchronize(); | 686 store_->UnSynchronize(); |
| 768 } | 687 } |
| 769 | 688 |
| 770 TEST_F(CookieStoreIOSWithBackend, FlushOnPolicyChange) { | |
| 771 // Start synchronization. | |
| 772 CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get()); | |
| 773 // Toggle cookie policy to trigger a flush. | |
| 774 EXPECT_FALSE(backend_->flushed()); | |
| 775 CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::BLOCK); | |
| 776 EXPECT_TRUE(backend_->flushed()); | |
| 777 store_->UnSynchronize(); | |
| 778 CookieStoreIOS::SetCookiePolicy(CookieStoreIOS::ALLOW); | |
| 779 } | |
| 780 | |
| 781 TEST_F(CookieStoreIOSWithBackend, NoInitialNotifyWithNoCookie) { | 689 TEST_F(CookieStoreIOSWithBackend, NoInitialNotifyWithNoCookie) { |
| 782 CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get()); | 690 CookieStoreIOS::SwitchSynchronizedStore(nullptr, store_.get()); |
| 783 std::vector<net::CanonicalCookie> cookies; | 691 std::vector<net::CanonicalCookie> cookies; |
| 784 store_->AddCallbackForCookie( | 692 store_->AddCallbackForCookie( |
| 785 kTestCookieURL, "abc", | 693 kTestCookieURL, "abc", |
| 786 base::Bind(&RecordCookieChanges, &cookies, nullptr)); | 694 base::Bind(&RecordCookieChanges, &cookies, nullptr)); |
| 787 EXPECT_EQ(0U, cookies.size()); | 695 EXPECT_EQ(0U, cookies.size()); |
| 788 store_->UnSynchronize(); | 696 store_->UnSynchronize(); |
| 789 } | 697 } |
| 790 | 698 |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 993 EXPECT_EQ(2U, cookies.size()); | 901 EXPECT_EQ(2U, cookies.size()); |
| 994 // this deletes the callback | 902 // this deletes the callback |
| 995 handle.reset(); | 903 handle.reset(); |
| 996 SetSystemCookie(kTestCookieURL, "abc", "jkl"); | 904 SetSystemCookie(kTestCookieURL, "abc", "jkl"); |
| 997 EXPECT_EQ(2U, cookies.size()); | 905 EXPECT_EQ(2U, cookies.size()); |
| 998 DeleteSystemCookie(kTestCookieURL, "abc"); | 906 DeleteSystemCookie(kTestCookieURL, "abc"); |
| 999 store_->UnSynchronize(); | 907 store_->UnSynchronize(); |
| 1000 } | 908 } |
| 1001 | 909 |
| 1002 } // namespace net | 910 } // namespace net |
| OLD | NEW |