[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 5 | #include "base/bind.h" |
[email protected] | ee61137 | 2011-11-29 05:41:17 | [diff] [blame] | 6 | #include "base/bind_helpers.h" |
[email protected] | 9d01a6a | 2010-11-30 12:03:33 | [diff] [blame] | 7 | #include "chrome/browser/content_settings/host_content_settings_map.h" |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 8 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 9 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 10 | #include "chrome/browser/ui/browser.h" |
[email protected] | 88509ab | 2012-08-27 15:04:14 | [diff] [blame] | 11 | #include "chrome/browser/ui/tab_contents/tab_contents.h" |
[email protected] | 59253a65 | 2012-11-20 00:17:26 | [diff] [blame^] | 12 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 13 | #include "chrome/common/pref_names.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 14 | #include "chrome/test/base/in_process_browser_test.h" |
| 15 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 88509ab | 2012-08-27 15:04:14 | [diff] [blame] | 16 | #include "content/public/test/browser_test_utils.h" |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 17 | #include "net/base/mock_host_resolver.h" |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 18 | #include "net/test/test_server.h" |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 19 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 20 | using content::BrowserThread; |
| 21 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 22 | namespace { |
| 23 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 24 | class CookiePolicyBrowserTest : public InProcessBrowserTest { |
| 25 | protected: |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 26 | CookiePolicyBrowserTest() {} |
| 27 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 28 | private: |
| 29 | DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest); |
| 30 | }; |
| 31 | |
| 32 | // Visits a page that sets a first-party cookie. |
| 33 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 34 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 35 | |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 36 | browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, |
| 37 | true); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 38 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 39 | GURL url(test_server()->GetURL("set-cookie?cookie1")); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 40 | |
[email protected] | 59253a65 | 2012-11-20 00:17:26 | [diff] [blame^] | 41 | TabContents* tab = browser()->tab_strip_model()->GetActiveTabContents(); |
[email protected] | 88509ab | 2012-08-27 15:04:14 | [diff] [blame] | 42 | std::string cookie = content::GetCookies(tab->profile(), url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 43 | ASSERT_EQ("", cookie); |
| 44 | |
| 45 | ui_test_utils::NavigateToURL(browser(), url); |
| 46 | |
[email protected] | 88509ab | 2012-08-27 15:04:14 | [diff] [blame] | 47 | cookie = content::GetCookies(tab->profile(), url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 48 | EXPECT_EQ("cookie1", cookie); |
| 49 | } |
| 50 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 51 | // Visits a page that is a redirect across domain boundary to a page that sets |
| 52 | // a first-party cookie. |
| 53 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 54 | AllowFirstPartyCookiesRedirect) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 55 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 56 | |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 57 | browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, |
| 58 | true); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 59 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 60 | GURL url(test_server()->GetURL("server-redirect?")); |
| 61 | GURL redirected_url(test_server()->GetURL("set-cookie?cookie2")); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 62 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 63 | // Change the host name from 127.0.0.1 to www.example.com so it triggers |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 64 | // third-party cookie blocking if the first party for cookies URL is not |
| 65 | // changed when we follow a redirect. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 66 | ASSERT_EQ("127.0.0.1", redirected_url.host()); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 67 | GURL::Replacements replacements; |
| 68 | std::string new_host("www.example.com"); |
| 69 | replacements.SetHostStr(new_host); |
| 70 | redirected_url = redirected_url.ReplaceComponents(replacements); |
| 71 | |
[email protected] | 59253a65 | 2012-11-20 00:17:26 | [diff] [blame^] | 72 | TabContents* tab = browser()->tab_strip_model()->GetActiveTabContents(); |
[email protected] | 88509ab | 2012-08-27 15:04:14 | [diff] [blame] | 73 | std::string cookie = content::GetCookies(tab->profile(), redirected_url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 74 | ASSERT_EQ("", cookie); |
| 75 | |
| 76 | host_resolver()->AddRule("www.example.com", "127.0.0.1"); |
| 77 | |
| 78 | ui_test_utils::NavigateToURL(browser(), |
| 79 | GURL(url.spec() + redirected_url.spec())); |
| 80 | |
[email protected] | 88509ab | 2012-08-27 15:04:14 | [diff] [blame] | 81 | cookie = content::GetCookies(tab->profile(), redirected_url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 82 | EXPECT_EQ("cookie2", cookie); |
| 83 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 84 | |
| 85 | } // namespace |