| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/browser.h" |
| 6 #include "chrome/browser/net/url_request_context_getter.h" |
| 7 #include "chrome/browser/profile.h" |
| 8 #include "chrome/common/pref_names.h" |
| 9 #include "chrome/common/pref_service.h" |
| 10 #include "chrome/test/in_process_browser_test.h" |
| 11 #include "chrome/test/ui_test_utils.h" |
| 12 |
| 13 class CookiePolicyBrowserTest : public InProcessBrowserTest { |
| 14 public: |
| 15 CookiePolicyBrowserTest() {} |
| 16 |
| 17 private: |
| 18 DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest); |
| 19 }; |
| 20 |
| 21 // Visits a page that sets a first-party cookie. |
| 22 IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) { |
| 23 HTTPTestServer* server = StartHTTPServer(); |
| 24 ASSERT_TRUE(server != NULL); |
| 25 |
| 26 PrefService* prefs = browser()->profile()->GetPrefs(); |
| 27 prefs->SetInteger(prefs::kCookieBehavior, |
| 28 net::CookiePolicy::BLOCK_THIRD_PARTY_COOKIES); |
| 29 net::CookiePolicy::Type policy_type = net::CookiePolicy::FromInt( |
| 30 prefs->GetInteger(prefs::kCookieBehavior)); |
| 31 ASSERT_EQ(net::CookiePolicy::BLOCK_THIRD_PARTY_COOKIES, policy_type); |
| 32 |
| 33 net::CookieStore* cookie_store = |
| 34 browser()->profile()->GetRequestContext()->GetCookieStore(); |
| 35 |
| 36 GURL url = server->TestServerPage("set-cookie?cookie1"); |
| 37 |
| 38 std::string cookie = cookie_store->GetCookies(url); |
| 39 ASSERT_EQ("", cookie); |
| 40 |
| 41 ui_test_utils::NavigateToURL(browser(), url); |
| 42 |
| 43 cookie = cookie_store->GetCookies(url); |
| 44 EXPECT_EQ("cookie1", cookie); |
| 45 } |
| 46 |
| 47 |
| 48 // Visits a page that is a redirect across domain boundary to a page that sets |
| 49 // a first-party cookie. |
| 50 IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 51 AllowFirstPartyCookiesRedirect) { |
| 52 HTTPTestServer* server = StartHTTPServer(); |
| 53 ASSERT_TRUE(server != NULL); |
| 54 |
| 55 PrefService* prefs = browser()->profile()->GetPrefs(); |
| 56 prefs->SetInteger(prefs::kCookieBehavior, |
| 57 net::CookiePolicy::BLOCK_THIRD_PARTY_COOKIES); |
| 58 net::CookiePolicy::Type policy_type = net::CookiePolicy::FromInt( |
| 59 prefs->GetInteger(prefs::kCookieBehavior)); |
| 60 ASSERT_EQ(net::CookiePolicy::BLOCK_THIRD_PARTY_COOKIES, policy_type); |
| 61 |
| 62 net::CookieStore* cookie_store = |
| 63 browser()->profile()->GetRequestContext()->GetCookieStore(); |
| 64 |
| 65 GURL url = server->TestServerPage("server-redirect?"); |
| 66 |
| 67 GURL redirected_url = server->TestServerPage("set-cookie?cookie2"); |
| 68 // Change the host name from localhost to www.example.com so it triggers |
| 69 // third-party cookie blocking if the first party for cookies URL is not |
| 70 // changed when we follow a redirect. |
| 71 ASSERT_EQ("localhost", redirected_url.host()); |
| 72 GURL::Replacements replacements; |
| 73 std::string new_host("www.example.com"); |
| 74 replacements.SetHostStr(new_host); |
| 75 redirected_url = redirected_url.ReplaceComponents(replacements); |
| 76 |
| 77 std::string cookie = cookie_store->GetCookies(redirected_url); |
| 78 ASSERT_EQ("", cookie); |
| 79 |
| 80 host_resolver()->AddRule("www.example.com", "127.0.0.1"); |
| 81 |
| 82 ui_test_utils::NavigateToURL(browser(), |
| 83 GURL(url.spec() + redirected_url.spec())); |
| 84 |
| 85 cookie = cookie_store->GetCookies(redirected_url); |
| 86 EXPECT_EQ("cookie2", cookie); |
| 87 } |
| OLD | NEW |