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/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/content_settings/cookie_settings.h" | 9 #include "chrome/browser/content_settings/cookie_settings.h" |
10 #include "chrome/browser/content_settings/host_content_settings_map.h" | 10 #include "chrome/browser/content_settings/host_content_settings_map.h" |
11 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 11 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
| 12 #include "chrome/browser/net/url_request_mock_util.h" |
12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/browser_commands.h" |
14 #include "chrome/browser/ui/browser_tabstrip.h" | 16 #include "chrome/browser/ui/browser_tabstrip.h" |
15 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 17 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 18 #include "chrome/common/chrome_notification_types.h" |
16 #include "chrome/common/render_messages.h" | 19 #include "chrome/common/render_messages.h" |
17 #include "chrome/test/base/in_process_browser_test.h" | 20 #include "chrome/test/base/in_process_browser_test.h" |
18 #include "chrome/test/base/ui_test_utils.h" | 21 #include "chrome/test/base/ui_test_utils.h" |
| 22 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
20 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
21 #include "content/public/common/content_switches.h" | 25 #include "content/public/common/content_switches.h" |
22 #include "content/public/test/browser_test_utils.h" | 26 #include "content/public/test/browser_test_utils.h" |
| 27 #include "content/public/test/test_utils.h" |
| 28 #include "content/test/net/url_request_mock_http_job.h" |
23 #include "net/test/test_server.h" | 29 #include "net/test/test_server.h" |
24 | 30 |
| 31 using content::BrowserThread; |
| 32 |
| 33 class ContentSettingsTest : public InProcessBrowserTest { |
| 34 public: |
| 35 ContentSettingsTest() |
| 36 : https_server_( |
| 37 net::TestServer::TYPE_HTTPS, |
| 38 net::TestServer::SSLOptions(net::TestServer::SSLOptions::CERT_OK), |
| 39 FilePath(FILE_PATH_LITERAL("chrome/test/data"))) { |
| 40 } |
| 41 |
| 42 virtual void SetUpOnMainThread() OVERRIDE { |
| 43 BrowserThread::PostTask( |
| 44 BrowserThread::IO, FROM_HERE, |
| 45 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| 46 } |
| 47 |
| 48 // Check the cookie for the given URL in an incognito window. |
| 49 void CookieCheckIncognitoWindow(const GURL& url, bool cookies_enabled) { |
| 50 ASSERT_TRUE(content::GetCookies(browser()->profile(), url).empty()); |
| 51 |
| 52 Browser* incognito = CreateIncognitoBrowser(); |
| 53 ASSERT_TRUE(content::GetCookies(incognito->profile(), url).empty()); |
| 54 ui_test_utils::NavigateToURL(incognito, url); |
| 55 ASSERT_EQ(cookies_enabled, |
| 56 !content::GetCookies(incognito->profile(), url).empty()); |
| 57 |
| 58 // Ensure incognito cookies don't leak to regular profile. |
| 59 ASSERT_TRUE(content::GetCookies(browser()->profile(), url).empty()); |
| 60 |
| 61 content::WindowedNotificationObserver signal( |
| 62 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 63 content::Source<Browser>(incognito)); |
| 64 |
| 65 chrome::CloseWindow(incognito); |
| 66 |
| 67 signal.Wait(); |
| 68 |
| 69 incognito = CreateIncognitoBrowser(); |
| 70 ASSERT_TRUE(content::GetCookies(incognito->profile(), url).empty()); |
| 71 chrome::CloseWindow(incognito); |
| 72 } |
| 73 |
| 74 void PreBasic(const GURL& url) { |
| 75 TabContents* tab = chrome::GetActiveTabContents(browser()); |
| 76 ASSERT_TRUE(GetCookies(tab->profile(), url).empty()); |
| 77 |
| 78 CookieCheckIncognitoWindow(url, true); |
| 79 |
| 80 ui_test_utils::NavigateToURL(browser(), url); |
| 81 ASSERT_FALSE(GetCookies(tab->profile(), url).empty()); |
| 82 } |
| 83 |
| 84 void Basic(const GURL& url) { |
| 85 TabContents* tab = chrome::GetActiveTabContents(browser()); |
| 86 ASSERT_FALSE(GetCookies(tab->profile(), url).empty()); |
| 87 } |
| 88 |
| 89 net::TestServer https_server_; |
| 90 }; |
| 91 |
| 92 // Sanity check on cookies before we do other tests. While these can be written |
| 93 // in content_browsertests, we want to verify Chrome's cookie storage and how it |
| 94 // handles incognito windows. |
| 95 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, PRE_BasicCookies) { |
| 96 ASSERT_TRUE(test_server()->Start()); |
| 97 GURL http_url = test_server()->GetURL("files/setcookie.html"); |
| 98 PreBasic(http_url); |
| 99 } |
| 100 |
| 101 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, BasicCookies) { |
| 102 ASSERT_TRUE(test_server()->Start()); |
| 103 GURL http_url = test_server()->GetURL("files/setcookie.html"); |
| 104 Basic(http_url); |
| 105 } |
| 106 |
| 107 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, PRE_BasicCookiesHttps) { |
| 108 ASSERT_TRUE(https_server_.Start()); |
| 109 GURL https_url = https_server_.GetURL("files/setcookie.html"); |
| 110 PreBasic(https_url); |
| 111 } |
| 112 |
| 113 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, BasicCookiesHttps) { |
| 114 ASSERT_TRUE(https_server_.Start()); |
| 115 GURL https_url = https_server_.GetURL("files/setcookie.html"); |
| 116 Basic(https_url); |
| 117 } |
| 118 |
| 119 // Verify that cookies are being blocked. |
| 120 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, PRE_BlockCookies) { |
| 121 ASSERT_TRUE(test_server()->Start()); |
| 122 CookieSettings::Factory::GetForProfile(browser()->profile())-> |
| 123 SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); |
| 124 GURL url = test_server()->GetURL("files/setcookie.html"); |
| 125 ui_test_utils::NavigateToURL(browser(), url); |
| 126 ASSERT_TRUE(GetCookies(browser()->profile(), url).empty()); |
| 127 CookieCheckIncognitoWindow(url, false); |
| 128 } |
| 129 |
| 130 // Ensure that the setting persists. |
| 131 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, BlockCookies) { |
| 132 ASSERT_EQ( |
| 133 CONTENT_SETTING_BLOCK, |
| 134 CookieSettings::Factory::GetForProfile(browser()->profile())-> |
| 135 GetDefaultCookieSetting(NULL)); |
| 136 } |
| 137 |
| 138 // Verify that cookies can be allowed and set using exceptions for particular |
| 139 // website(s) when all others are blocked. |
| 140 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, AllowCookiesUsingExceptions) { |
| 141 ASSERT_TRUE(test_server()->Start()); |
| 142 GURL url = test_server()->GetURL("files/setcookie.html"); |
| 143 CookieSettings* settings = |
| 144 CookieSettings::Factory::GetForProfile(browser()->profile()); |
| 145 settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); |
| 146 |
| 147 ui_test_utils::NavigateToURL(browser(), url); |
| 148 ASSERT_TRUE(GetCookies(browser()->profile(), url).empty()); |
| 149 |
| 150 settings->SetCookieSetting( |
| 151 ContentSettingsPattern::FromURL(url), |
| 152 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW); |
| 153 |
| 154 ui_test_utils::NavigateToURL(browser(), url); |
| 155 ASSERT_FALSE(GetCookies(browser()->profile(), url).empty()); |
| 156 } |
| 157 |
| 158 // Verify that cookies can be blocked for a specific website using exceptions. |
| 159 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, BlockCookiesUsingExceptions) { |
| 160 ASSERT_TRUE(test_server()->Start()); |
| 161 GURL url = test_server()->GetURL("files/setcookie.html"); |
| 162 CookieSettings* settings = |
| 163 CookieSettings::Factory::GetForProfile(browser()->profile()); |
| 164 settings->SetCookieSetting( |
| 165 ContentSettingsPattern::FromURL(url), |
| 166 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK); |
| 167 |
| 168 ui_test_utils::NavigateToURL(browser(), url); |
| 169 ASSERT_TRUE(GetCookies(browser()->profile(), url).empty()); |
| 170 |
| 171 ASSERT_TRUE(https_server_.Start()); |
| 172 GURL unblocked_url = https_server_.GetURL("files/cookie1.html"); |
| 173 |
| 174 ui_test_utils::NavigateToURL(browser(), unblocked_url); |
| 175 ASSERT_FALSE(GetCookies(browser()->profile(), unblocked_url).empty()); |
| 176 } |
| 177 |
| 178 // This fails on ChromeOS because kRestoreOnStartup is ignored and the startup |
| 179 // preference is always "continue where I left off. |
| 180 #if !defined(OS_CHROMEOS) |
| 181 |
| 182 // Verify that cookies can be allowed and set using exceptions for particular |
| 183 // website(s) only for a session when all others are blocked. |
| 184 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, |
| 185 PRE_AllowCookiesForASessionUsingExceptions) { |
| 186 // NOTE: don't use test_server here, since we need the port to be the same |
| 187 // across the restart. |
| 188 GURL url = URLRequestMockHTTPJob::GetMockUrl( |
| 189 FilePath(FILE_PATH_LITERAL("setcookie.html"))); |
| 190 CookieSettings* settings = |
| 191 CookieSettings::Factory::GetForProfile(browser()->profile()); |
| 192 settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); |
| 193 |
| 194 ui_test_utils::NavigateToURL(browser(), url); |
| 195 ASSERT_TRUE(GetCookies(browser()->profile(), url).empty()); |
| 196 |
| 197 settings->SetCookieSetting( |
| 198 ContentSettingsPattern::FromURL(url), |
| 199 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_SESSION_ONLY); |
| 200 ui_test_utils::NavigateToURL(browser(), url); |
| 201 ASSERT_FALSE(GetCookies(browser()->profile(), url).empty()); |
| 202 } |
| 203 |
| 204 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, |
| 205 AllowCookiesForASessionUsingExceptions) { |
| 206 GURL url = URLRequestMockHTTPJob::GetMockUrl( |
| 207 FilePath(FILE_PATH_LITERAL("setcookie.html"))); |
| 208 ASSERT_TRUE(GetCookies(browser()->profile(), url).empty()); |
| 209 } |
| 210 |
| 211 #endif // !CHROME_OS |
| 212 |
25 // Regression test for http://crbug.com/63649. | 213 // Regression test for http://crbug.com/63649. |
26 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, RedirectLoopCookies) { | 214 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, RedirectLoopCookies) { |
27 ASSERT_TRUE(test_server()->Start()); | 215 ASSERT_TRUE(test_server()->Start()); |
28 | 216 |
29 GURL test_url = test_server()->GetURL("files/redirect-loop.html"); | 217 GURL test_url = test_server()->GetURL("files/redirect-loop.html"); |
30 | 218 |
31 CookieSettings::Factory::GetForProfile(browser()->profile())-> | 219 CookieSettings::Factory::GetForProfile(browser()->profile())-> |
32 SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); | 220 SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); |
33 | 221 |
34 ui_test_utils::NavigateToURL(browser(), test_url); | 222 ui_test_utils::NavigateToURL(browser(), test_url); |
35 | 223 |
36 TabContents* tab_contents = chrome::GetActiveTabContents(browser()); | 224 TabContents* tab_contents = chrome::GetActiveTabContents(browser()); |
37 ASSERT_EQ(UTF8ToUTF16(test_url.spec() + " failed to load"), | 225 ASSERT_EQ(UTF8ToUTF16(test_url.spec() + " failed to load"), |
38 tab_contents->web_contents()->GetTitle()); | 226 tab_contents->web_contents()->GetTitle()); |
39 | 227 |
40 EXPECT_TRUE(tab_contents->content_settings()->IsContentBlocked( | 228 EXPECT_TRUE(tab_contents->content_settings()->IsContentBlocked( |
41 CONTENT_SETTINGS_TYPE_COOKIES)); | 229 CONTENT_SETTINGS_TYPE_COOKIES)); |
42 } | 230 } |
43 | 231 |
44 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, ContentSettingsBlockDataURLs) { | 232 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, ContentSettingsBlockDataURLs) { |
45 GURL url("data:text/html,<title>Data URL</title><script>alert(1)</script>"); | 233 GURL url("data:text/html,<title>Data URL</title><script>alert(1)</script>"); |
46 | 234 |
47 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting( | 235 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting( |
48 CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK); | 236 CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK); |
49 | 237 |
50 ui_test_utils::NavigateToURL(browser(), url); | 238 ui_test_utils::NavigateToURL(browser(), url); |
51 | 239 |
52 TabContents* tab_contents = chrome::GetActiveTabContents(browser()); | 240 TabContents* tab_contents = chrome::GetActiveTabContents(browser()); |
53 ASSERT_EQ(UTF8ToUTF16("Data URL"), tab_contents->web_contents()->GetTitle()); | 241 ASSERT_EQ(UTF8ToUTF16("Data URL"), tab_contents->web_contents()->GetTitle()); |
54 | 242 |
55 EXPECT_TRUE(tab_contents->content_settings()->IsContentBlocked( | 243 EXPECT_TRUE(tab_contents->content_settings()->IsContentBlocked( |
56 CONTENT_SETTINGS_TYPE_JAVASCRIPT)); | 244 CONTENT_SETTINGS_TYPE_JAVASCRIPT)); |
57 } | 245 } |
58 | 246 |
59 // Tests that if redirect across origins occurs, the new process still gets the | 247 // Tests that if redirect across origins occurs, the new process still gets the |
60 // content settings before the resource headers. | 248 // content settings before the resource headers. |
61 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, RedirectCrossOrigin) { | 249 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, RedirectCrossOrigin) { |
62 ASSERT_TRUE(test_server()->Start()); | 250 ASSERT_TRUE(test_server()->Start()); |
63 | 251 |
64 net::HostPortPair host_port = test_server()->host_port_pair(); | 252 net::HostPortPair host_port = test_server()->host_port_pair(); |
65 DCHECK_EQ(host_port.host(), std::string("127.0.0.1")); | 253 DCHECK_EQ(host_port.host(), std::string("127.0.0.1")); |
66 | 254 |
67 std::string redirect(base::StringPrintf( | 255 std::string redirect(base::StringPrintf( |
68 "http://localhost:%d/files/redirect-cross-origin.html", | 256 "http://localhost:%d/files/redirect-cross-origin.html", |
69 host_port.port())); | 257 host_port.port())); |
70 GURL test_url = test_server()->GetURL("server-redirect?" + redirect); | 258 GURL test_url = test_server()->GetURL("server-redirect?" + redirect); |
71 | 259 |
72 CookieSettings::Factory::GetForProfile(browser()->profile())-> | 260 CookieSettings::Factory::GetForProfile(browser()->profile())-> |
73 SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); | 261 SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); |
74 | 262 |
75 ui_test_utils::NavigateToURL(browser(), test_url); | 263 ui_test_utils::NavigateToURL(browser(), test_url); |
76 | 264 |
77 TabContents* tab_contents = chrome::GetActiveTabContents(browser()); | 265 TabContents* tab_contents = chrome::GetActiveTabContents(browser()); |
78 | 266 |
79 EXPECT_TRUE(tab_contents->content_settings()->IsContentBlocked( | 267 EXPECT_TRUE(tab_contents->content_settings()->IsContentBlocked( |
80 CONTENT_SETTINGS_TYPE_COOKIES)); | 268 CONTENT_SETTINGS_TYPE_COOKIES)); |
81 } | 269 } |
82 | 270 |
83 #if !defined(USE_AURA) // No NPAPI plugins with Aura. | 271 #if !defined(USE_AURA) // No NPAPI plugins with Aura. |
84 | 272 |
85 class ClickToPlayPluginTest : public InProcessBrowserTest { | 273 class ClickToPlayPluginTest : public ContentSettingsTest { |
86 public: | 274 public: |
87 ClickToPlayPluginTest() {} | 275 ClickToPlayPluginTest() {} |
88 | 276 |
89 #if defined(OS_MACOSX) | 277 #if defined(OS_MACOSX) |
90 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 278 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
91 FilePath plugin_dir; | 279 FilePath plugin_dir; |
92 PathService::Get(base::DIR_MODULE, &plugin_dir); | 280 PathService::Get(base::DIR_MODULE, &plugin_dir); |
93 plugin_dir = plugin_dir.AppendASCII("plugins"); | 281 plugin_dir = plugin_dir.AppendASCII("plugins"); |
94 // The plugins directory isn't read by default on the Mac, so it needs to be | 282 // The plugins directory isn't read by default on the Mac, so it needs to be |
95 // explicitly registered. | 283 // explicitly registered. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 354 |
167 content::RenderViewHost* host = | 355 content::RenderViewHost* host = |
168 chrome::GetActiveWebContents(browser())->GetRenderViewHost(); | 356 chrome::GetActiveWebContents(browser())->GetRenderViewHost(); |
169 host->Send(new ChromeViewMsg_LoadBlockedPlugins( | 357 host->Send(new ChromeViewMsg_LoadBlockedPlugins( |
170 host->GetRoutingID(), std::string())); | 358 host->GetRoutingID(), std::string())); |
171 | 359 |
172 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); | 360 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); |
173 } | 361 } |
174 | 362 |
175 #endif // !defined(USE_AURA) | 363 #endif // !defined(USE_AURA) |
OLD | NEW |