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/cookies/cookie_store_unittest.h" | 5 #include "net/cookies/cookie_store_unittest.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 2593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2604 it = cookies.begin(); | 2604 it = cookies.begin(); |
2605 base::Closure task = base::Bind( | 2605 base::Closure task = base::Bind( |
2606 &net::MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask, | 2606 &net::MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask, |
2607 base::Unretained(this), | 2607 base::Unretained(this), |
2608 cm, *it, &callback); | 2608 cm, *it, &callback); |
2609 RunOnOtherThread(task); | 2609 RunOnOtherThread(task); |
2610 EXPECT_TRUE(callback.did_run()); | 2610 EXPECT_TRUE(callback.did_run()); |
2611 EXPECT_TRUE(callback.result()); | 2611 EXPECT_TRUE(callback.result()); |
2612 } | 2612 } |
2613 | 2613 |
| 2614 // Ensure that cookies for http, https, ws, and wss all share the same storage |
| 2615 // and policies when GetAllCookiesForURLAsync is used. This test is part of |
| 2616 // MultiThreadedCookieMonsterTest in order to test and use |
| 2617 // GetAllCookiesForURLAsync, but it does not use any additional threads. |
| 2618 TEST_F(MultiThreadedCookieMonsterTest, GetAllCookiesForURLEffectiveDomain) { |
| 2619 std::vector<CanonicalCookie*> cookies; |
| 2620 // This cookie will be freed by the CookieMonster. |
| 2621 cookies.push_back(CanonicalCookie::Create(url_google_, kValidCookieLine, |
| 2622 Time::Now(), CookieOptions())); |
| 2623 CanonicalCookie cookie = *cookies[0]; |
| 2624 scoped_refptr<NewMockPersistentCookieStore> store( |
| 2625 new NewMockPersistentCookieStore); |
| 2626 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL)); |
| 2627 |
| 2628 CookieMonster::PersistentCookieStore::LoadedCallback loaded_callback; |
| 2629 ::testing::StrictMock<::testing::MockFunction<void(int)>> checkpoint; |
| 2630 const std::string key = |
| 2631 cookie_util::GetEffectiveDomain(url_google_.scheme(), url_google_.host()); |
| 2632 |
| 2633 ::testing::InSequence s; |
| 2634 EXPECT_CALL(checkpoint, Call(0)); |
| 2635 EXPECT_CALL(*store, Load(::testing::_)); |
| 2636 EXPECT_CALL(*store, LoadCookiesForKey(key, ::testing::_)) |
| 2637 .WillOnce(::testing::SaveArg<1>(&loaded_callback)); |
| 2638 EXPECT_CALL(checkpoint, Call(1)); |
| 2639 // LoadCookiesForKey will never be called after checkpoint.Call(1) although |
| 2640 // we will call GetAllCookiesForURLAsync again, because all URLs below share |
| 2641 // the same key. |
| 2642 EXPECT_CALL(*store, LoadCookiesForKey(::testing::_, ::testing::_)).Times(0); |
| 2643 |
| 2644 GetCookieListCallback callback; |
| 2645 checkpoint.Call(0); |
| 2646 GetAllCookiesForURLTask(cm.get(), url_google_, &callback); |
| 2647 checkpoint.Call(1); |
| 2648 ASSERT_FALSE(callback.did_run()); |
| 2649 // Pass the cookies to the CookieMonster. |
| 2650 loaded_callback.Run(cookies); |
| 2651 // Now GetAllCookiesForURLTask is done. |
| 2652 ASSERT_TRUE(callback.did_run()); |
| 2653 // See that the callback was called with the cookies. |
| 2654 ASSERT_EQ(1u, callback.cookies().size()); |
| 2655 EXPECT_TRUE(cookie.IsEquivalent(callback.cookies()[0])); |
| 2656 |
| 2657 // All urls in |urls| should share the same cookie domain. |
| 2658 const GURL kUrls[] = { |
| 2659 url_google_, |
| 2660 url_google_secure_, |
| 2661 GURL(kUrlGoogleWebSocket), |
| 2662 GURL(kUrlGoogleWebSocketSecure), |
| 2663 }; |
| 2664 for (const GURL& url : kUrls) { |
| 2665 // Call the function with |url| and verify it is done synchronously without |
| 2666 // calling LoadCookiesForKey. |
| 2667 GetCookieListCallback callback; |
| 2668 GetAllCookiesForURLTask(cm.get(), url, &callback); |
| 2669 ASSERT_TRUE(callback.did_run()); |
| 2670 ASSERT_EQ(1u, callback.cookies().size()); |
| 2671 EXPECT_TRUE(cookie.IsEquivalent(callback.cookies()[0])); |
| 2672 } |
| 2673 } |
| 2674 |
2614 TEST_F(CookieMonsterTest, InvalidExpiryTime) { | 2675 TEST_F(CookieMonsterTest, InvalidExpiryTime) { |
2615 std::string cookie_line = | 2676 std::string cookie_line = |
2616 std::string(kValidCookieLine) + "; expires=Blarg arg arg"; | 2677 std::string(kValidCookieLine) + "; expires=Blarg arg arg"; |
2617 scoped_ptr<CanonicalCookie> cookie( | 2678 scoped_ptr<CanonicalCookie> cookie( |
2618 CanonicalCookie::Create(url_google_, cookie_line, Time::Now(), | 2679 CanonicalCookie::Create(url_google_, cookie_line, Time::Now(), |
2619 CookieOptions())); | 2680 CookieOptions())); |
2620 ASSERT_FALSE(cookie->IsPersistent()); | 2681 ASSERT_FALSE(cookie->IsPersistent()); |
2621 } | 2682 } |
2622 | 2683 |
2623 // Test that CookieMonster writes session cookies into the underlying | 2684 // Test that CookieMonster writes session cookies into the underlying |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2879 scoped_ptr<CookieStore::CookieChangedSubscription> sub1( | 2940 scoped_ptr<CookieStore::CookieChangedSubscription> sub1( |
2880 monster()->AddCallbackForCookie(test_url_, "abc", | 2941 monster()->AddCallbackForCookie(test_url_, "abc", |
2881 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); | 2942 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); |
2882 SetCookie(monster(), test_url_, "abc=def"); | 2943 SetCookie(monster(), test_url_, "abc=def"); |
2883 base::MessageLoop::current()->RunUntilIdle(); | 2944 base::MessageLoop::current()->RunUntilIdle(); |
2884 EXPECT_EQ(1U, cookies0.size()); | 2945 EXPECT_EQ(1U, cookies0.size()); |
2885 EXPECT_EQ(1U, cookies0.size()); | 2946 EXPECT_EQ(1U, cookies0.size()); |
2886 } | 2947 } |
2887 | 2948 |
2888 } // namespace net | 2949 } // namespace net |
OLD | NEW |