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" |
16 #include "third_party/WebKit/public/web/WebView.h" | 17 #include "third_party/WebKit/public/web/WebView.h" |
17 | 18 |
18 using testing::_; | 19 using testing::_; |
19 using testing::DeleteArg; | 20 using testing::DeleteArg; |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 class MockContentSettingsObserver : public ContentSettingsObserver { | 24 class MockContentSettingsObserver : public ContentSettingsObserver { |
24 public: | 25 public: |
25 explicit MockContentSettingsObserver(content::RenderFrame* render_frame); | 26 explicit MockContentSettingsObserver(content::RenderFrame* render_frame); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 | 82 |
82 // Accessing localStorage from the same origin again shouldn't result in a | 83 // Accessing localStorage from the same origin again shouldn't result in a |
83 // new IPC. | 84 // new IPC. |
84 observer.allowStorage(true); | 85 observer.allowStorage(true); |
85 ::testing::Mock::VerifyAndClearExpectations(&observer); | 86 ::testing::Mock::VerifyAndClearExpectations(&observer); |
86 } | 87 } |
87 | 88 |
88 // Regression test for http://crbug.com/35011 | 89 // Regression test for http://crbug.com/35011 |
89 TEST_F(ChromeRenderViewTest, JSBlockSentAfterPageLoad) { | 90 TEST_F(ChromeRenderViewTest, JSBlockSentAfterPageLoad) { |
90 // 1. Load page with JS. | 91 // 1. Load page with JS. |
91 std::string html = "<html>" | 92 const char kHtml[] = |
92 "<head>" | 93 "<html>" |
93 "<script>document.createElement('div');</script>" | 94 "<head>" |
94 "</head>" | 95 "<script>document.createElement('div');</script>" |
95 "<body>" | 96 "</head>" |
96 "</body>" | 97 "<body>" |
97 "</html>"; | 98 "</body>" |
| 99 "</html>"; |
98 render_thread_->sink().ClearMessages(); | 100 render_thread_->sink().ClearMessages(); |
99 LoadHTML(html.c_str()); | 101 LoadHTML(kHtml); |
100 | 102 |
101 // 2. Block JavaScript. | 103 // 2. Block JavaScript. |
102 RendererContentSettingRules content_setting_rules; | 104 RendererContentSettingRules content_setting_rules; |
103 ContentSettingsForOneType& script_setting_rules = | 105 ContentSettingsForOneType& script_setting_rules = |
104 content_setting_rules.script_rules; | 106 content_setting_rules.script_rules; |
105 script_setting_rules.push_back( | 107 script_setting_rules.push_back( |
106 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 108 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
107 ContentSettingsPattern::Wildcard(), | 109 ContentSettingsPattern::Wildcard(), |
108 CONTENT_SETTING_BLOCK, | 110 CONTENT_SETTING_BLOCK, |
109 std::string(), | 111 std::string(), |
110 false)); | 112 false)); |
111 ContentSettingsObserver* observer = ContentSettingsObserver::Get( | 113 ContentSettingsObserver* observer = ContentSettingsObserver::Get( |
112 view_->GetMainRenderFrame()); | 114 view_->GetMainRenderFrame()); |
113 observer->SetContentSettingRules(&content_setting_rules); | 115 observer->SetContentSettingRules(&content_setting_rules); |
114 | 116 |
115 // Make sure no pending messages are in the queue. | 117 // Make sure no pending messages are in the queue. |
116 ProcessPendingMessages(); | 118 ProcessPendingMessages(); |
117 render_thread_->sink().ClearMessages(); | 119 render_thread_->sink().ClearMessages(); |
118 | 120 |
119 // 3. Reload page. | 121 // 3. Reload page. |
120 std::string url_str = "data:text/html;charset=utf-8,"; | 122 std::string url_str = "data:text/html;charset=utf-8,"; |
121 url_str.append(html); | 123 url_str.append(kHtml); |
122 GURL url(url_str); | 124 GURL url(url_str); |
123 Reload(url); | 125 Reload(url); |
124 ProcessPendingMessages(); | 126 ProcessPendingMessages(); |
125 | 127 |
126 // 4. Verify that the notification that javascript was blocked is sent after | 128 // 4. Verify that the notification that javascript was blocked is sent after |
127 // the navigation notification is sent. | 129 // the navigation notification is sent. |
128 int navigation_index = -1; | 130 int navigation_index = -1; |
129 int block_index = -1; | 131 int block_index = -1; |
130 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | 132 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
131 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | 133 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 ContentSettingsPattern::Wildcard(), | 264 ContentSettingsPattern::Wildcard(), |
263 CONTENT_SETTING_BLOCK, | 265 CONTENT_SETTING_BLOCK, |
264 std::string(), | 266 std::string(), |
265 false)); | 267 false)); |
266 | 268 |
267 ContentSettingsObserver* observer = | 269 ContentSettingsObserver* observer = |
268 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); | 270 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
269 observer->SetContentSettingRules(&content_setting_rules); | 271 observer->SetContentSettingRules(&content_setting_rules); |
270 | 272 |
271 // Load a page which contains a script. | 273 // Load a page which contains a script. |
272 std::string html = "<html>" | 274 const char kHtml[] = |
273 "<head>" | 275 "<html>" |
274 "<script src='data:foo'></script>" | 276 "<head>" |
275 "</head>" | 277 "<script src='data:foo'></script>" |
276 "<body>" | 278 "</head>" |
277 "</body>" | 279 "<body>" |
278 "</html>"; | 280 "</body>" |
279 LoadHTML(html.c_str()); | 281 "</html>"; |
| 282 LoadHTML(kHtml); |
280 | 283 |
281 // Verify that the script was blocked. | 284 // Verify that the script was blocked. |
282 bool was_blocked = false; | 285 bool was_blocked = false; |
283 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | 286 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
284 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | 287 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
285 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) | 288 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
286 was_blocked = true; | 289 was_blocked = true; |
287 } | 290 } |
288 EXPECT_TRUE(was_blocked); | 291 EXPECT_TRUE(was_blocked); |
289 } | 292 } |
290 | 293 |
291 TEST_F(ChromeRenderViewTest, ContentSettingsAllowScripts) { | 294 TEST_F(ChromeRenderViewTest, ContentSettingsAllowScripts) { |
292 // Set the content settings for scripts. | 295 // Set the content settings for scripts. |
293 RendererContentSettingRules content_setting_rules; | 296 RendererContentSettingRules content_setting_rules; |
294 ContentSettingsForOneType& script_setting_rules = | 297 ContentSettingsForOneType& script_setting_rules = |
295 content_setting_rules.script_rules; | 298 content_setting_rules.script_rules; |
296 script_setting_rules.push_back( | 299 script_setting_rules.push_back( |
297 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 300 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
298 ContentSettingsPattern::Wildcard(), | 301 ContentSettingsPattern::Wildcard(), |
299 CONTENT_SETTING_ALLOW, | 302 CONTENT_SETTING_ALLOW, |
300 std::string(), | 303 std::string(), |
301 false)); | 304 false)); |
302 | 305 |
303 ContentSettingsObserver* observer = | 306 ContentSettingsObserver* observer = |
304 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); | 307 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
305 observer->SetContentSettingRules(&content_setting_rules); | 308 observer->SetContentSettingRules(&content_setting_rules); |
306 | 309 |
307 // Load a page which contains a script. | 310 // Load a page which contains a script. |
308 std::string html = "<html>" | 311 const char kHtml[] = |
309 "<head>" | 312 "<html>" |
310 "<script src='data:foo'></script>" | 313 "<head>" |
311 "</head>" | 314 "<script src='data:foo'></script>" |
312 "<body>" | 315 "</head>" |
313 "</body>" | 316 "<body>" |
314 "</html>"; | 317 "</body>" |
315 LoadHTML(html.c_str()); | 318 "</html>"; |
| 319 LoadHTML(kHtml); |
316 | 320 |
317 // Verify that the script was not blocked. | 321 // Verify that the script was not blocked. |
318 bool was_blocked = false; | 322 bool was_blocked = false; |
319 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | 323 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
320 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | 324 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
321 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) | 325 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
322 was_blocked = true; | 326 was_blocked = true; |
323 } | 327 } |
324 EXPECT_FALSE(was_blocked); | 328 EXPECT_FALSE(was_blocked); |
325 } | 329 } |
326 | 330 |
| 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 |
327 TEST_F(ChromeRenderViewTest, ContentSettingsInterstitialPages) { | 435 TEST_F(ChromeRenderViewTest, ContentSettingsInterstitialPages) { |
328 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); | 436 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); |
329 // Block scripts. | 437 // Block scripts. |
330 RendererContentSettingRules content_setting_rules; | 438 RendererContentSettingRules content_setting_rules; |
331 ContentSettingsForOneType& script_setting_rules = | 439 ContentSettingsForOneType& script_setting_rules = |
332 content_setting_rules.script_rules; | 440 content_setting_rules.script_rules; |
333 script_setting_rules.push_back( | 441 script_setting_rules.push_back( |
334 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 442 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
335 ContentSettingsPattern::Wildcard(), | 443 ContentSettingsPattern::Wildcard(), |
336 CONTENT_SETTING_BLOCK, | 444 CONTENT_SETTING_BLOCK, |
337 std::string(), | 445 std::string(), |
338 false)); | 446 false)); |
339 // Block images. | 447 // Block images. |
340 ContentSettingsForOneType& image_setting_rules = | 448 ContentSettingsForOneType& image_setting_rules = |
341 content_setting_rules.image_rules; | 449 content_setting_rules.image_rules; |
342 image_setting_rules.push_back( | 450 image_setting_rules.push_back( |
343 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 451 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
344 ContentSettingsPattern::Wildcard(), | 452 ContentSettingsPattern::Wildcard(), |
345 CONTENT_SETTING_BLOCK, | 453 CONTENT_SETTING_BLOCK, |
346 std::string(), | 454 std::string(), |
347 false)); | 455 false)); |
348 | 456 |
349 ContentSettingsObserver* observer = | 457 ContentSettingsObserver* observer = |
350 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); | 458 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
351 observer->SetContentSettingRules(&content_setting_rules); | 459 observer->SetContentSettingRules(&content_setting_rules); |
352 observer->OnSetAsInterstitial(); | 460 observer->OnSetAsInterstitial(); |
353 | 461 |
354 // Load a page which contains a script. | 462 // Load a page which contains a script. |
355 std::string html = "<html>" | 463 const char kHtml[] = |
356 "<head>" | 464 "<html>" |
357 "<script src='data:foo'></script>" | 465 "<head>" |
358 "</head>" | 466 "<script src='data:foo'></script>" |
359 "<body>" | 467 "</head>" |
360 "</body>" | 468 "<body>" |
361 "</html>"; | 469 "</body>" |
362 LoadHTML(html.c_str()); | 470 "</html>"; |
| 471 LoadHTML(kHtml); |
363 | 472 |
364 // Verify that the script was allowed. | 473 // Verify that the script was allowed. |
365 bool was_blocked = false; | 474 bool was_blocked = false; |
366 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | 475 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
367 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | 476 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
368 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) | 477 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
369 was_blocked = true; | 478 was_blocked = true; |
370 } | 479 } |
371 EXPECT_FALSE(was_blocked); | 480 EXPECT_FALSE(was_blocked); |
372 | 481 |
373 // Verify that images are allowed. | 482 // Verify that images are allowed. |
374 EXPECT_CALL(mock_observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, | 483 EXPECT_CALL(mock_observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, |
375 base::string16())).Times(0); | 484 base::string16())).Times(0); |
376 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); | 485 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); |
377 ::testing::Mock::VerifyAndClearExpectations(&observer); | 486 ::testing::Mock::VerifyAndClearExpectations(&observer); |
378 } | 487 } |
OLD | NEW |