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