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