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 <time.h> | 7 #include <time.h> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 1758 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1769 false, false)); | 1769 false, false)); |
1770 | 1770 |
1771 CookieList cookies = GetAllCookiesForURL(cm, url_google_foo_); | 1771 CookieList cookies = GetAllCookiesForURL(cm, url_google_foo_); |
1772 CookieList::iterator it = cookies.begin(); | 1772 CookieList::iterator it = cookies.begin(); |
1773 | 1773 |
1774 ASSERT_TRUE(it != cookies.end()); | 1774 ASSERT_TRUE(it != cookies.end()); |
1775 EXPECT_EQ("A", it->Name()); | 1775 EXPECT_EQ("A", it->Name()); |
1776 EXPECT_EQ("B", it->Value()); | 1776 EXPECT_EQ("B", it->Value()); |
1777 EXPECT_EQ("www.google.izzle", it->Domain()); | 1777 EXPECT_EQ("www.google.izzle", it->Domain()); |
1778 EXPECT_EQ("/foo", it->Path()); | 1778 EXPECT_EQ("/foo", it->Path()); |
1779 EXPECT_FALSE(it->DoesExpire()); | 1779 EXPECT_FALSE(it->IsPersistent()); |
1780 EXPECT_FALSE(it->IsSecure()); | 1780 EXPECT_FALSE(it->IsSecure()); |
1781 EXPECT_FALSE(it->IsHttpOnly()); | 1781 EXPECT_FALSE(it->IsHttpOnly()); |
1782 | 1782 |
1783 ASSERT_TRUE(++it == cookies.end()); | 1783 ASSERT_TRUE(++it == cookies.end()); |
1784 | 1784 |
1785 cookies = GetAllCookiesForURL(cm, url_google_bar_); | 1785 cookies = GetAllCookiesForURL(cm, url_google_bar_); |
1786 it = cookies.begin(); | 1786 it = cookies.begin(); |
1787 | 1787 |
1788 ASSERT_TRUE(it != cookies.end()); | 1788 ASSERT_TRUE(it != cookies.end()); |
1789 EXPECT_EQ("C", it->Name()); | 1789 EXPECT_EQ("C", it->Name()); |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2133 static_cast<int>(GetAllCookies(cm).size())) | 2133 static_cast<int>(GetAllCookies(cm).size())) |
2134 << "For test case " << ci; | 2134 << "For test case " << ci; |
2135 // Will trigger GC | 2135 // Will trigger GC |
2136 SetCookie(cm, GURL("http://newdomain.com"), "b=2"); | 2136 SetCookie(cm, GURL("http://newdomain.com"), "b=2"); |
2137 EXPECT_EQ(test_case->expected_cookies_after_set, | 2137 EXPECT_EQ(test_case->expected_cookies_after_set, |
2138 static_cast<int>((GetAllCookies(cm).size()))) | 2138 static_cast<int>((GetAllCookies(cm).size()))) |
2139 << "For test case " << ci; | 2139 << "For test case " << ci; |
2140 } | 2140 } |
2141 } | 2141 } |
2142 | 2142 |
2143 // This test checks that setting a cookie forcing it to be a session only | |
2144 // cookie works as expected. | |
2145 TEST_F(CookieMonsterTest, ForceSessionOnly) { | |
2146 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | |
2147 CookieOptions options; | |
2148 | |
2149 // Set a persistent cookie, but force it to be a session cookie. | |
2150 options.set_force_session(); | |
2151 ASSERT_TRUE(SetCookieWithOptions( | |
2152 cm, url_google_, | |
2153 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-22 22:50:13 GMT", | |
2154 options)); | |
2155 | |
2156 // Get the canonical cookie. | |
2157 CookieList cookie_list = GetAllCookies(cm); | |
2158 ASSERT_EQ(1U, cookie_list.size()); | |
2159 ASSERT_FALSE(cookie_list[0].IsPersistent()); | |
2160 | |
2161 // Use a past expiry date to delete the cookie, but force it to session only. | |
2162 ASSERT_TRUE(SetCookieWithOptions( | |
2163 cm, url_google_, | |
2164 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-1977 22:50:13 GMT", | |
2165 options)); | |
2166 | |
2167 // Check that the cookie was deleted. | |
2168 cookie_list = GetAllCookies(cm); | |
2169 ASSERT_EQ(0U, cookie_list.size()); | |
2170 } | |
2171 | |
2172 // This test checks that keep expired cookies flag is working. | 2143 // This test checks that keep expired cookies flag is working. |
2173 TEST_F(CookieMonsterTest, KeepExpiredCookies) { | 2144 TEST_F(CookieMonsterTest, KeepExpiredCookies) { |
2174 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 2145 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
2175 cm->SetKeepExpiredCookies(); | 2146 cm->SetKeepExpiredCookies(); |
2176 CookieOptions options; | 2147 CookieOptions options; |
2177 | 2148 |
2178 // Set a persistent cookie. | 2149 // Set a persistent cookie. |
2179 ASSERT_TRUE(SetCookieWithOptions( | 2150 ASSERT_TRUE(SetCookieWithOptions( |
2180 cm, url_google_, | 2151 cm, url_google_, |
2181 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-22 22:50:13 GMT", | 2152 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-22 22:50:13 GMT", |
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2595 it = cookies.begin(); | 2566 it = cookies.begin(); |
2596 base::Closure task = base::Bind( | 2567 base::Closure task = base::Bind( |
2597 &net::MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask, | 2568 &net::MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask, |
2598 base::Unretained(this), | 2569 base::Unretained(this), |
2599 cm, *it, &callback); | 2570 cm, *it, &callback); |
2600 RunOnOtherThread(task); | 2571 RunOnOtherThread(task); |
2601 EXPECT_TRUE(callback.did_run()); | 2572 EXPECT_TRUE(callback.did_run()); |
2602 EXPECT_TRUE(callback.result()); | 2573 EXPECT_TRUE(callback.result()); |
2603 } | 2574 } |
2604 | 2575 |
2605 TEST_F(CookieMonsterTest, ShortLivedSessionCookies) { | |
2606 scoped_refptr<MockPersistentCookieStore> store( | |
2607 new MockPersistentCookieStore); | |
2608 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); | |
2609 | |
2610 // Create a short-lived session cookie. | |
2611 CookieOptions options; | |
2612 options.set_force_session(); | |
2613 Time current = Time::Now(); | |
2614 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, | |
2615 std::string(kValidCookieLine) + | |
2616 "; max-age=10", | |
2617 options)); | |
2618 | |
2619 // FindCookiesForKey asserts that its caller holds this lock. | |
2620 base::AutoLock auto_lock(cm->lock_); | |
2621 | |
2622 // Get cookies before the cookie has expired. | |
2623 std::vector<CookieMonster::CanonicalCookie*> cookies; | |
2624 cm->FindCookiesForKey(cm->GetKey(url_google_.host()), url_google_, | |
2625 CookieOptions(), current, false, &cookies); | |
2626 EXPECT_EQ(1U, cookies.size()); | |
2627 | |
2628 // Get cookies after the cookie has expired. | |
2629 cookies.clear(); | |
2630 cm->FindCookiesForKey(cm->GetKey(url_google_.host()), url_google_, | |
2631 CookieOptions(), current + TimeDelta::FromSeconds(20), | |
2632 false, &cookies); | |
2633 EXPECT_EQ(0U, cookies.size()); | |
2634 } | |
2635 | |
2636 TEST_F(CookieMonsterTest, InvalidExpiryTime) { | 2576 TEST_F(CookieMonsterTest, InvalidExpiryTime) { |
2637 CookieMonster::ParsedCookie pc( | 2577 CookieMonster::ParsedCookie pc( |
2638 std::string(kValidCookieLine) + "; expires=Blarg arg arg"); | 2578 std::string(kValidCookieLine) + "; expires=Blarg arg arg"); |
2639 scoped_ptr<CookieMonster::CanonicalCookie> cookie( | 2579 scoped_ptr<CookieMonster::CanonicalCookie> cookie( |
2640 CookieMonster::CanonicalCookie::Create(url_google_, pc)); | 2580 CookieMonster::CanonicalCookie::Create(url_google_, pc)); |
2641 | 2581 |
2642 ASSERT_FALSE(cookie->DoesExpire()); | 2582 ASSERT_FALSE(cookie->IsPersistent()); |
2643 } | 2583 } |
2644 | 2584 |
2645 // Test that CookieMonster writes session cookies into the underlying | 2585 // Test that CookieMonster writes session cookies into the underlying |
2646 // CookieStore if the "persist session cookies" option is on. | 2586 // CookieStore if the "persist session cookies" option is on. |
2647 TEST_F(CookieMonsterTest, PersistSessionCookies) { | 2587 TEST_F(CookieMonsterTest, PersistSessionCookies) { |
2648 scoped_refptr<MockPersistentCookieStore> store( | 2588 scoped_refptr<MockPersistentCookieStore> store( |
2649 new MockPersistentCookieStore); | 2589 new MockPersistentCookieStore); |
2650 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); | 2590 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); |
2651 cm->SetPersistSessionCookies(true); | 2591 cm->SetPersistSessionCookies(true); |
2652 | 2592 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2713 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); | 2653 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); |
2714 | 2654 |
2715 // Create some non-persistent cookies and check that they don't go to the | 2655 // Create some non-persistent cookies and check that they don't go to the |
2716 // persistent storage. | 2656 // persistent storage. |
2717 EXPECT_TRUE(SetCookie(cm, url_google_, "B=Bar")); | 2657 EXPECT_TRUE(SetCookie(cm, url_google_, "B=Bar")); |
2718 this->MatchCookieLines("A=Foo; B=Bar", GetCookies(cm, url_google_)); | 2658 this->MatchCookieLines("A=Foo; B=Bar", GetCookies(cm, url_google_)); |
2719 EXPECT_EQ(5u, store->commands().size()); | 2659 EXPECT_EQ(5u, store->commands().size()); |
2720 } | 2660 } |
2721 | 2661 |
2722 } // namespace net | 2662 } // namespace net |
OLD | NEW |