| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "chrome/common/render_messages.h" | 7 #include "chrome/common/render_messages.h" |
| 8 #include "chrome/renderer/content_settings_observer.h" | 8 #include "chrome/renderer/content_settings_observer.h" |
| 9 #include "chrome/test/base/chrome_render_view_test.h" | 9 #include "chrome/test/base/chrome_render_view_test.h" |
| 10 #include "components/content_settings/content/common/content_settings_messages.h
" | 10 #include "components/content_settings/content/common/content_settings_messages.h
" |
| 11 #include "components/content_settings/core/common/content_settings.h" | 11 #include "components/content_settings/core/common/content_settings.h" |
| 12 #include "content/public/renderer/render_view.h" | 12 #include "content/public/renderer/render_view.h" |
| 13 #include "ipc/ipc_message_macros.h" | 13 #include "ipc/ipc_message_macros.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/WebKit/public/web/WebFrameContentDumper.h" | |
| 17 #include "third_party/WebKit/public/web/WebView.h" | 16 #include "third_party/WebKit/public/web/WebView.h" |
| 18 | 17 |
| 19 using testing::_; | 18 using testing::_; |
| 20 using testing::DeleteArg; | 19 using testing::DeleteArg; |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 class MockContentSettingsObserver : public ContentSettingsObserver { | 23 class MockContentSettingsObserver : public ContentSettingsObserver { |
| 25 public: | 24 public: |
| 26 explicit MockContentSettingsObserver(content::RenderFrame* render_frame); | 25 explicit MockContentSettingsObserver(content::RenderFrame* render_frame); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 | 81 |
| 83 // Accessing localStorage from the same origin again shouldn't result in a | 82 // Accessing localStorage from the same origin again shouldn't result in a |
| 84 // new IPC. | 83 // new IPC. |
| 85 observer.allowStorage(true); | 84 observer.allowStorage(true); |
| 86 ::testing::Mock::VerifyAndClearExpectations(&observer); | 85 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 87 } | 86 } |
| 88 | 87 |
| 89 // Regression test for http://crbug.com/35011 | 88 // Regression test for http://crbug.com/35011 |
| 90 TEST_F(ChromeRenderViewTest, JSBlockSentAfterPageLoad) { | 89 TEST_F(ChromeRenderViewTest, JSBlockSentAfterPageLoad) { |
| 91 // 1. Load page with JS. | 90 // 1. Load page with JS. |
| 92 const char kHtml[] = | 91 std::string html = "<html>" |
| 93 "<html>" | 92 "<head>" |
| 94 "<head>" | 93 "<script>document.createElement('div');</script>" |
| 95 "<script>document.createElement('div');</script>" | 94 "</head>" |
| 96 "</head>" | 95 "<body>" |
| 97 "<body>" | 96 "</body>" |
| 98 "</body>" | 97 "</html>"; |
| 99 "</html>"; | |
| 100 render_thread_->sink().ClearMessages(); | 98 render_thread_->sink().ClearMessages(); |
| 101 LoadHTML(kHtml); | 99 LoadHTML(html.c_str()); |
| 102 | 100 |
| 103 // 2. Block JavaScript. | 101 // 2. Block JavaScript. |
| 104 RendererContentSettingRules content_setting_rules; | 102 RendererContentSettingRules content_setting_rules; |
| 105 ContentSettingsForOneType& script_setting_rules = | 103 ContentSettingsForOneType& script_setting_rules = |
| 106 content_setting_rules.script_rules; | 104 content_setting_rules.script_rules; |
| 107 script_setting_rules.push_back( | 105 script_setting_rules.push_back( |
| 108 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 106 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 109 ContentSettingsPattern::Wildcard(), | 107 ContentSettingsPattern::Wildcard(), |
| 110 CONTENT_SETTING_BLOCK, | 108 CONTENT_SETTING_BLOCK, |
| 111 std::string(), | 109 std::string(), |
| 112 false)); | 110 false)); |
| 113 ContentSettingsObserver* observer = ContentSettingsObserver::Get( | 111 ContentSettingsObserver* observer = ContentSettingsObserver::Get( |
| 114 view_->GetMainRenderFrame()); | 112 view_->GetMainRenderFrame()); |
| 115 observer->SetContentSettingRules(&content_setting_rules); | 113 observer->SetContentSettingRules(&content_setting_rules); |
| 116 | 114 |
| 117 // Make sure no pending messages are in the queue. | 115 // Make sure no pending messages are in the queue. |
| 118 ProcessPendingMessages(); | 116 ProcessPendingMessages(); |
| 119 render_thread_->sink().ClearMessages(); | 117 render_thread_->sink().ClearMessages(); |
| 120 | 118 |
| 121 // 3. Reload page. | 119 // 3. Reload page. |
| 122 std::string url_str = "data:text/html;charset=utf-8,"; | 120 std::string url_str = "data:text/html;charset=utf-8,"; |
| 123 url_str.append(kHtml); | 121 url_str.append(html); |
| 124 GURL url(url_str); | 122 GURL url(url_str); |
| 125 Reload(url); | 123 Reload(url); |
| 126 ProcessPendingMessages(); | 124 ProcessPendingMessages(); |
| 127 | 125 |
| 128 // 4. Verify that the notification that javascript was blocked is sent after | 126 // 4. Verify that the notification that javascript was blocked is sent after |
| 129 // the navigation notification is sent. | 127 // the navigation notification is sent. |
| 130 int navigation_index = -1; | 128 int navigation_index = -1; |
| 131 int block_index = -1; | 129 int block_index = -1; |
| 132 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | 130 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 133 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | 131 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 ContentSettingsPattern::Wildcard(), | 262 ContentSettingsPattern::Wildcard(), |
| 265 CONTENT_SETTING_BLOCK, | 263 CONTENT_SETTING_BLOCK, |
| 266 std::string(), | 264 std::string(), |
| 267 false)); | 265 false)); |
| 268 | 266 |
| 269 ContentSettingsObserver* observer = | 267 ContentSettingsObserver* observer = |
| 270 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); | 268 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
| 271 observer->SetContentSettingRules(&content_setting_rules); | 269 observer->SetContentSettingRules(&content_setting_rules); |
| 272 | 270 |
| 273 // Load a page which contains a script. | 271 // Load a page which contains a script. |
| 274 const char kHtml[] = | 272 std::string html = "<html>" |
| 275 "<html>" | 273 "<head>" |
| 276 "<head>" | 274 "<script src='data:foo'></script>" |
| 277 "<script src='data:foo'></script>" | 275 "</head>" |
| 278 "</head>" | 276 "<body>" |
| 279 "<body>" | 277 "</body>" |
| 280 "</body>" | 278 "</html>"; |
| 281 "</html>"; | 279 LoadHTML(html.c_str()); |
| 282 LoadHTML(kHtml); | |
| 283 | 280 |
| 284 // Verify that the script was blocked. | 281 // Verify that the script was blocked. |
| 285 bool was_blocked = false; | 282 bool was_blocked = false; |
| 286 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | 283 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 287 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | 284 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 288 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) | 285 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 289 was_blocked = true; | 286 was_blocked = true; |
| 290 } | 287 } |
| 291 EXPECT_TRUE(was_blocked); | 288 EXPECT_TRUE(was_blocked); |
| 292 } | 289 } |
| 293 | 290 |
| 294 TEST_F(ChromeRenderViewTest, ContentSettingsAllowScripts) { | 291 TEST_F(ChromeRenderViewTest, ContentSettingsAllowScripts) { |
| 295 // Set the content settings for scripts. | 292 // Set the content settings for scripts. |
| 296 RendererContentSettingRules content_setting_rules; | 293 RendererContentSettingRules content_setting_rules; |
| 297 ContentSettingsForOneType& script_setting_rules = | 294 ContentSettingsForOneType& script_setting_rules = |
| 298 content_setting_rules.script_rules; | 295 content_setting_rules.script_rules; |
| 299 script_setting_rules.push_back( | 296 script_setting_rules.push_back( |
| 300 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 297 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 301 ContentSettingsPattern::Wildcard(), | 298 ContentSettingsPattern::Wildcard(), |
| 302 CONTENT_SETTING_ALLOW, | 299 CONTENT_SETTING_ALLOW, |
| 303 std::string(), | 300 std::string(), |
| 304 false)); | 301 false)); |
| 305 | 302 |
| 306 ContentSettingsObserver* observer = | 303 ContentSettingsObserver* observer = |
| 307 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); | 304 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
| 308 observer->SetContentSettingRules(&content_setting_rules); | 305 observer->SetContentSettingRules(&content_setting_rules); |
| 309 | 306 |
| 310 // Load a page which contains a script. | 307 // Load a page which contains a script. |
| 311 const char kHtml[] = | 308 std::string html = "<html>" |
| 312 "<html>" | 309 "<head>" |
| 313 "<head>" | 310 "<script src='data:foo'></script>" |
| 314 "<script src='data:foo'></script>" | 311 "</head>" |
| 315 "</head>" | 312 "<body>" |
| 316 "<body>" | 313 "</body>" |
| 317 "</body>" | 314 "</html>"; |
| 318 "</html>"; | 315 LoadHTML(html.c_str()); |
| 319 LoadHTML(kHtml); | |
| 320 | 316 |
| 321 // Verify that the script was not blocked. | 317 // Verify that the script was not blocked. |
| 322 bool was_blocked = false; | 318 bool was_blocked = false; |
| 323 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | 319 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 324 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | 320 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 325 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) | 321 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 326 was_blocked = true; | 322 was_blocked = true; |
| 327 } | 323 } |
| 328 EXPECT_FALSE(was_blocked); | 324 EXPECT_FALSE(was_blocked); |
| 329 } | 325 } |
| 330 | 326 |
| 331 // Regression test for crbug.com/232410: Load a page with JS blocked. Then, | |
| 332 // allow JS and reload the page. In each case, only one of noscript or script | |
| 333 // tags should be enabled, but never both. | |
| 334 TEST_F(ChromeRenderViewTest, ContentSettingsNoscriptTag) { | |
| 335 // 1. Block JavaScript. | |
| 336 RendererContentSettingRules content_setting_rules; | |
| 337 ContentSettingsForOneType& script_setting_rules = | |
| 338 content_setting_rules.script_rules; | |
| 339 script_setting_rules.push_back(ContentSettingPatternSource( | |
| 340 ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(), | |
| 341 CONTENT_SETTING_BLOCK, std::string(), false)); | |
| 342 | |
| 343 ContentSettingsObserver* observer = | |
| 344 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); | |
| 345 observer->SetContentSettingRules(&content_setting_rules); | |
| 346 | |
| 347 // 2. Load a page which contains a noscript tag and a script tag. Note that | |
| 348 // the page doesn't have a body tag. | |
| 349 const char kHtml[] = | |
| 350 "<html>" | |
| 351 "<noscript>JS_DISABLED</noscript>" | |
| 352 "<script>document.write('JS_ENABLED');</script>" | |
| 353 "</html>"; | |
| 354 LoadHTML(kHtml); | |
| 355 EXPECT_NE( | |
| 356 std::string::npos, | |
| 357 blink::WebFrameContentDumper::dumpLayoutTreeAsText( | |
| 358 GetMainFrame(), blink::WebFrameContentDumper::LayoutAsTextNormal) | |
| 359 .utf8() | |
| 360 .find("JS_DISABLED")); | |
| 361 EXPECT_EQ( | |
| 362 std::string::npos, | |
| 363 blink::WebFrameContentDumper::dumpLayoutTreeAsText( | |
| 364 GetMainFrame(), blink::WebFrameContentDumper::LayoutAsTextNormal) | |
| 365 .utf8() | |
| 366 .find("JS_ENABLED")); | |
| 367 | |
| 368 // 3. Allow JavaScript. | |
| 369 script_setting_rules.clear(); | |
| 370 script_setting_rules.push_back(ContentSettingPatternSource( | |
| 371 ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(), | |
| 372 CONTENT_SETTING_ALLOW, std::string(), false)); | |
| 373 observer->SetContentSettingRules(&content_setting_rules); | |
| 374 | |
| 375 // 4. Reload the page. | |
| 376 std::string url_str = "data:text/html;charset=utf-8,"; | |
| 377 url_str.append(kHtml); | |
| 378 GURL url(url_str); | |
| 379 Reload(url); | |
| 380 EXPECT_NE( | |
| 381 std::string::npos, | |
| 382 blink::WebFrameContentDumper::dumpLayoutTreeAsText( | |
| 383 GetMainFrame(), blink::WebFrameContentDumper::LayoutAsTextNormal) | |
| 384 .utf8() | |
| 385 .find("JS_ENABLED")); | |
| 386 EXPECT_EQ( | |
| 387 std::string::npos, | |
| 388 blink::WebFrameContentDumper::dumpLayoutTreeAsText( | |
| 389 GetMainFrame(), blink::WebFrameContentDumper::LayoutAsTextNormal) | |
| 390 .utf8() | |
| 391 .find("JS_DISABLED")); | |
| 392 } | |
| 393 | |
| 394 // Checks that same page navigations don't update content settings for the page. | |
| 395 TEST_F(ChromeRenderViewTest, ContentSettingsSamePageNavigation) { | |
| 396 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); | |
| 397 // Load a page which contains a script. | |
| 398 const char kHtml[] = | |
| 399 "<html>" | |
| 400 "<head>" | |
| 401 "<script src='data:foo'></script>" | |
| 402 "</head>" | |
| 403 "<body>" | |
| 404 "</body>" | |
| 405 "</html>"; | |
| 406 LoadHTML(kHtml); | |
| 407 | |
| 408 // Verify that the script was not blocked. | |
| 409 bool was_blocked = false; | |
| 410 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | |
| 411 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | |
| 412 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) | |
| 413 was_blocked = true; | |
| 414 } | |
| 415 EXPECT_FALSE(was_blocked); | |
| 416 | |
| 417 // Block JavaScript. | |
| 418 RendererContentSettingRules content_setting_rules; | |
| 419 ContentSettingsForOneType& script_setting_rules = | |
| 420 content_setting_rules.script_rules; | |
| 421 script_setting_rules.push_back(ContentSettingPatternSource( | |
| 422 ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(), | |
| 423 CONTENT_SETTING_BLOCK, std::string(), false)); | |
| 424 | |
| 425 ContentSettingsObserver* observer = | |
| 426 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); | |
| 427 observer->SetContentSettingRules(&content_setting_rules); | |
| 428 | |
| 429 // The page shouldn't see the change to script blocking setting after a | |
| 430 // same page navigation. | |
| 431 DidNavigateWithinPage(GetMainFrame(), true); | |
| 432 EXPECT_TRUE(observer->allowScript(true)); | |
| 433 } | |
| 434 | |
| 435 TEST_F(ChromeRenderViewTest, ContentSettingsInterstitialPages) { | 327 TEST_F(ChromeRenderViewTest, ContentSettingsInterstitialPages) { |
| 436 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); | 328 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); |
| 437 // Block scripts. | 329 // Block scripts. |
| 438 RendererContentSettingRules content_setting_rules; | 330 RendererContentSettingRules content_setting_rules; |
| 439 ContentSettingsForOneType& script_setting_rules = | 331 ContentSettingsForOneType& script_setting_rules = |
| 440 content_setting_rules.script_rules; | 332 content_setting_rules.script_rules; |
| 441 script_setting_rules.push_back( | 333 script_setting_rules.push_back( |
| 442 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 334 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 443 ContentSettingsPattern::Wildcard(), | 335 ContentSettingsPattern::Wildcard(), |
| 444 CONTENT_SETTING_BLOCK, | 336 CONTENT_SETTING_BLOCK, |
| 445 std::string(), | 337 std::string(), |
| 446 false)); | 338 false)); |
| 447 // Block images. | 339 // Block images. |
| 448 ContentSettingsForOneType& image_setting_rules = | 340 ContentSettingsForOneType& image_setting_rules = |
| 449 content_setting_rules.image_rules; | 341 content_setting_rules.image_rules; |
| 450 image_setting_rules.push_back( | 342 image_setting_rules.push_back( |
| 451 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 343 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 452 ContentSettingsPattern::Wildcard(), | 344 ContentSettingsPattern::Wildcard(), |
| 453 CONTENT_SETTING_BLOCK, | 345 CONTENT_SETTING_BLOCK, |
| 454 std::string(), | 346 std::string(), |
| 455 false)); | 347 false)); |
| 456 | 348 |
| 457 ContentSettingsObserver* observer = | 349 ContentSettingsObserver* observer = |
| 458 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); | 350 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
| 459 observer->SetContentSettingRules(&content_setting_rules); | 351 observer->SetContentSettingRules(&content_setting_rules); |
| 460 observer->OnSetAsInterstitial(); | 352 observer->OnSetAsInterstitial(); |
| 461 | 353 |
| 462 // Load a page which contains a script. | 354 // Load a page which contains a script. |
| 463 const char kHtml[] = | 355 std::string html = "<html>" |
| 464 "<html>" | 356 "<head>" |
| 465 "<head>" | 357 "<script src='data:foo'></script>" |
| 466 "<script src='data:foo'></script>" | 358 "</head>" |
| 467 "</head>" | 359 "<body>" |
| 468 "<body>" | 360 "</body>" |
| 469 "</body>" | 361 "</html>"; |
| 470 "</html>"; | 362 LoadHTML(html.c_str()); |
| 471 LoadHTML(kHtml); | |
| 472 | 363 |
| 473 // Verify that the script was allowed. | 364 // Verify that the script was allowed. |
| 474 bool was_blocked = false; | 365 bool was_blocked = false; |
| 475 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | 366 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 476 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | 367 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 477 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) | 368 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 478 was_blocked = true; | 369 was_blocked = true; |
| 479 } | 370 } |
| 480 EXPECT_FALSE(was_blocked); | 371 EXPECT_FALSE(was_blocked); |
| 481 | 372 |
| 482 // Verify that images are allowed. | 373 // Verify that images are allowed. |
| 483 EXPECT_CALL(mock_observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, | 374 EXPECT_CALL(mock_observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, |
| 484 base::string16())).Times(0); | 375 base::string16())).Times(0); |
| 485 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); | 376 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); |
| 486 ::testing::Mock::VerifyAndClearExpectations(&observer); | 377 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 487 } | 378 } |
| OLD | NEW |