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 3 // found in the LICENSE file. 4 5 #include "base/bind.h" 6 #include "base/bind_helpers.h" 7 #include "base/prefs/pref_service.h" 8 #include "chrome/browser/content_settings/host_content_settings_map.h" 9 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/ui/browser.h" 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" 12 #include "chrome/common/pref_names.h" 13 #include "chrome/test/base/in_process_browser_test.h" 14 #include "chrome/test/base/ui_test_utils.h" 15 #include "content/public/test/browser_test_utils.h" 16 #include "net/dns/mock_host_resolver.h" 17 #include "net/test/spawned_test_server/spawned_test_server.h" 18 19 using content::BrowserThread; 20 21 namespace { 22 23 class CookiePolicyBrowserTest : public InProcessBrowserTest { 24 protected: 25 CookiePolicyBrowserTest() {} 26 27 private: 28 DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest); 29 }; 30 31 // Visits a page that sets a first-party cookie. 32 IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) { 33 ASSERT_TRUE(test_server()->Start()); 34 35 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, 36 true); 37 38 GURL url(test_server()->GetURL("set-cookie?cookie1")); 39 40 std::string cookie = content::GetCookies(browser()->profile(), url); 41 ASSERT_EQ("", cookie); 42 43 ui_test_utils::NavigateToURL(browser(), url); 44 45 cookie = content::GetCookies(browser()->profile(), url); 46 EXPECT_EQ("cookie1", cookie); 47 } 48 49 // Visits a page that is a redirect across domain boundary to a page that sets 50 // a first-party cookie. 51 IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, 52 AllowFirstPartyCookiesRedirect) { 53 ASSERT_TRUE(test_server()->Start()); 54 55 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, 56 true); 57 58 GURL url(test_server()->GetURL("server-redirect?")); 59 GURL redirected_url(test_server()->GetURL("set-cookie?cookie2")); 60 61 // Change the host name from 127.0.0.1 to www.example.com so it triggers 62 // third-party cookie blocking if the first party for cookies URL is not 63 // changed when we follow a redirect. 64 ASSERT_EQ("127.0.0.1", redirected_url.host()); 65 GURL::Replacements replacements; 66 std::string new_host("www.example.com"); 67 replacements.SetHostStr(new_host); 68 redirected_url = redirected_url.ReplaceComponents(replacements); 69 70 std::string cookie = 71 content::GetCookies(browser()->profile(), redirected_url); 72 ASSERT_EQ("", cookie); 73 74 host_resolver()->AddRule("www.example.com", "127.0.0.1"); 75 76 ui_test_utils::NavigateToURL(browser(), 77 GURL(url.spec() + redirected_url.spec())); 78 79 cookie = content::GetCookies(browser()->profile(), redirected_url); 80 EXPECT_EQ("cookie2", cookie); 81 } 82 83 } // namespace 84