| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 public testing::WithParamInterface<TestConfig> { | 244 public testing::WithParamInterface<TestConfig> { |
| 245 public: | 245 public: |
| 246 void SetUpCommandLine(base::CommandLine* command_line) override { | 246 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 247 ExtensionApiTest::SetUpCommandLine(command_line); | 247 ExtensionApiTest::SetUpCommandLine(command_line); |
| 248 command_line->AppendSwitchASCII( | 248 command_line->AppendSwitchASCII( |
| 249 switches::kYieldBetweenContentScriptRuns, | 249 switches::kYieldBetweenContentScriptRuns, |
| 250 (GetParam() == TestConfig::kYieldBetweenContentScriptRunsEnabled) | 250 (GetParam() == TestConfig::kYieldBetweenContentScriptRunsEnabled) |
| 251 ? "1" | 251 ? "1" |
| 252 : "0"); | 252 : "0"); |
| 253 } | 253 } |
| 254 |
| 255 void SetUpOnMainThread() override { |
| 256 ExtensionApiTest::SetUpOnMainThread(); |
| 257 host_resolver()->AddRule("*", "127.0.0.1"); |
| 258 } |
| 254 }; | 259 }; |
| 255 | 260 |
| 256 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, ContentScriptAllFrames) { | 261 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, ContentScriptAllFrames) { |
| 257 ASSERT_TRUE(StartEmbeddedTestServer()); | 262 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 258 ASSERT_TRUE(RunExtensionTest("content_scripts/all_frames")) << message_; | 263 ASSERT_TRUE(RunExtensionTest("content_scripts/all_frames")) << message_; |
| 259 } | 264 } |
| 260 | 265 |
| 261 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, ContentScriptAboutBlankIframes) { | 266 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, ContentScriptAboutBlankIframes) { |
| 262 const char* testArg = | 267 const char* testArg = |
| 263 GetParam() == TestConfig::kYieldBetweenContentScriptRunsEnabled | 268 GetParam() == TestConfig::kYieldBetweenContentScriptRunsEnabled |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 ASSERT_TRUE(StartEmbeddedTestServer()); | 317 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 313 ASSERT_TRUE(RunExtensionTest("content_scripts/isolated_world1")) << message_; | 318 ASSERT_TRUE(RunExtensionTest("content_scripts/isolated_world1")) << message_; |
| 314 | 319 |
| 315 // Now load a different extension, inject into same page, verify worlds aren't | 320 // Now load a different extension, inject into same page, verify worlds aren't |
| 316 // shared. | 321 // shared. |
| 317 ASSERT_TRUE(RunExtensionTest("content_scripts/isolated_world2")) << message_; | 322 ASSERT_TRUE(RunExtensionTest("content_scripts/isolated_world2")) << message_; |
| 318 } | 323 } |
| 319 | 324 |
| 320 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, | 325 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, |
| 321 ContentScriptIgnoreHostPermissions) { | 326 ContentScriptIgnoreHostPermissions) { |
| 322 host_resolver()->AddRule("a.com", "127.0.0.1"); | |
| 323 host_resolver()->AddRule("b.com", "127.0.0.1"); | |
| 324 ASSERT_TRUE(StartEmbeddedTestServer()); | 327 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 325 ASSERT_TRUE(RunExtensionTest( | 328 ASSERT_TRUE(RunExtensionTest( |
| 326 "content_scripts/dont_match_host_permissions")) << message_; | 329 "content_scripts/dont_match_host_permissions")) << message_; |
| 327 } | 330 } |
| 328 | 331 |
| 329 // crbug.com/39249 -- content scripts js should not run on view source. | 332 // crbug.com/39249 -- content scripts js should not run on view source. |
| 330 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, ContentScriptViewSource) { | 333 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, ContentScriptViewSource) { |
| 331 ASSERT_TRUE(StartEmbeddedTestServer()); | 334 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 332 ASSERT_TRUE(RunExtensionTest("content_scripts/view_source")) << message_; | 335 ASSERT_TRUE(RunExtensionTest("content_scripts/view_source")) << message_; |
| 333 } | 336 } |
| 334 | 337 |
| 335 // crbug.com/126257 -- content scripts should not get injected into other | 338 // crbug.com/126257 -- content scripts should not get injected into other |
| 336 // extensions. | 339 // extensions. |
| 337 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, ContentScriptOtherExtensions) { | 340 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, ContentScriptOtherExtensions) { |
| 338 host_resolver()->AddRule("a.com", "127.0.0.1"); | |
| 339 ASSERT_TRUE(StartEmbeddedTestServer()); | 341 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 340 // First, load extension that sets up content script. | 342 // First, load extension that sets up content script. |
| 341 ASSERT_TRUE(RunExtensionTest("content_scripts/other_extensions/injector")) | 343 ASSERT_TRUE(RunExtensionTest("content_scripts/other_extensions/injector")) |
| 342 << message_; | 344 << message_; |
| 343 // Then load targeted extension to make sure its content isn't changed. | 345 // Then load targeted extension to make sure its content isn't changed. |
| 344 ASSERT_TRUE(RunExtensionTest("content_scripts/other_extensions/victim")) | 346 ASSERT_TRUE(RunExtensionTest("content_scripts/other_extensions/victim")) |
| 345 << message_; | 347 << message_; |
| 346 } | 348 } |
| 347 | 349 |
| 348 class ContentScriptCssInjectionTest : public ExtensionApiTest { | 350 class ContentScriptCssInjectionTest : public ExtensionApiTest { |
| 349 protected: | 351 protected: |
| 350 // TODO(rdevlin.cronin): Make a testing switch that looks like FeatureSwitch, | 352 // TODO(rdevlin.cronin): Make a testing switch that looks like FeatureSwitch, |
| 351 // but takes in an optional value so that we don't have to do this. | 353 // but takes in an optional value so that we don't have to do this. |
| 352 void SetUpCommandLine(base::CommandLine* command_line) override { | 354 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 353 ExtensionApiTest::SetUpCommandLine(command_line); | 355 ExtensionApiTest::SetUpCommandLine(command_line); |
| 354 // We change the Webstore URL to be http://cws.com. We need to do this so | 356 // We change the Webstore URL to be http://cws.com. We need to do this so |
| 355 // we can check that css injection is not allowed on the webstore (which | 357 // we can check that css injection is not allowed on the webstore (which |
| 356 // could lead to spoofing). Unfortunately, host_resolver seems to have | 358 // could lead to spoofing). Unfortunately, host_resolver seems to have |
| 357 // problems with redirecting "chrome.google.com" to the test server, so we | 359 // problems with redirecting "chrome.google.com" to the test server, so we |
| 358 // can't use the real Webstore's URL. If this changes, we could clean this | 360 // can't use the real Webstore's URL. If this changes, we could clean this |
| 359 // up. | 361 // up. |
| 360 command_line->AppendSwitchASCII( | 362 command_line->AppendSwitchASCII( |
| 361 ::switches::kAppsGalleryURL, | 363 ::switches::kAppsGalleryURL, |
| 362 base::StringPrintf("http://%s", kWebstoreDomain)); | 364 base::StringPrintf("http://%s", kWebstoreDomain)); |
| 363 } | 365 } |
| 366 |
| 367 void SetUpOnMainThread() override { |
| 368 ExtensionApiTest::SetUpOnMainThread(); |
| 369 host_resolver()->AddRule("*", "127.0.0.1"); |
| 370 } |
| 364 }; | 371 }; |
| 365 | 372 |
| 366 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, | 373 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, |
| 367 ContentScriptDuplicateScriptInjection) { | 374 ContentScriptDuplicateScriptInjection) { |
| 368 host_resolver()->AddRule("maps.google.com", "127.0.0.1"); | |
| 369 ASSERT_TRUE(StartEmbeddedTestServer()); | 375 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 370 | 376 |
| 371 GURL url( | 377 GURL url( |
| 372 base::StringPrintf("http://maps.google.com:%i/extensions/test_file.html", | 378 base::StringPrintf("http://maps.google.com:%i/extensions/test_file.html", |
| 373 embedded_test_server()->port())); | 379 embedded_test_server()->port())); |
| 374 | 380 |
| 375 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII( | 381 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII( |
| 376 "content_scripts/duplicate_script_injection"))); | 382 "content_scripts/duplicate_script_injection"))); |
| 377 | 383 |
| 378 ui_test_utils::NavigateToURL(browser(), url); | 384 ui_test_utils::NavigateToURL(browser(), url); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 396 "window.domAutomationController.send(" | 402 "window.domAutomationController.send(" |
| 397 "document.getElementsByClassName('injected-twice')" | 403 "document.getElementsByClassName('injected-twice')" |
| 398 ".length == 2)", | 404 ".length == 2)", |
| 399 &scripts_injected_twice)); | 405 &scripts_injected_twice)); |
| 400 ASSERT_TRUE(scripts_injected_twice); | 406 ASSERT_TRUE(scripts_injected_twice); |
| 401 } | 407 } |
| 402 | 408 |
| 403 IN_PROC_BROWSER_TEST_F(ContentScriptCssInjectionTest, | 409 IN_PROC_BROWSER_TEST_F(ContentScriptCssInjectionTest, |
| 404 ContentScriptInjectsStyles) { | 410 ContentScriptInjectsStyles) { |
| 405 ASSERT_TRUE(StartEmbeddedTestServer()); | 411 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 406 host_resolver()->AddRule(kWebstoreDomain, "127.0.0.1"); | |
| 407 | 412 |
| 408 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("content_scripts") | 413 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("content_scripts") |
| 409 .AppendASCII("css_injection"))); | 414 .AppendASCII("css_injection"))); |
| 410 | 415 |
| 411 // CSS injection should be allowed on an aribitrary web page. | 416 // CSS injection should be allowed on an aribitrary web page. |
| 412 GURL url = | 417 GURL url = |
| 413 embedded_test_server()->GetURL("/extensions/test_file_with_body.html"); | 418 embedded_test_server()->GetURL("/extensions/test_file_with_body.html"); |
| 414 EXPECT_TRUE(CheckStyleInjection(browser(), url, true)); | 419 EXPECT_TRUE(CheckStyleInjection(browser(), url, true)); |
| 415 | 420 |
| 416 // The loaded extension has an exclude match for "extensions/test_file.html", | 421 // The loaded extension has an exclude match for "extensions/test_file.html", |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 // Flaky on Windows. http://crbug.com/248418 | 498 // Flaky on Windows. http://crbug.com/248418 |
| 494 #if defined(OS_WIN) | 499 #if defined(OS_WIN) |
| 495 #define MAYBE_ContentScriptPermissionsApi DISABLED_ContentScriptPermissionsApi | 500 #define MAYBE_ContentScriptPermissionsApi DISABLED_ContentScriptPermissionsApi |
| 496 #else | 501 #else |
| 497 #define MAYBE_ContentScriptPermissionsApi ContentScriptPermissionsApi | 502 #define MAYBE_ContentScriptPermissionsApi ContentScriptPermissionsApi |
| 498 #endif | 503 #endif |
| 499 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, | 504 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, |
| 500 MAYBE_ContentScriptPermissionsApi) { | 505 MAYBE_ContentScriptPermissionsApi) { |
| 501 extensions::PermissionsRequestFunction::SetIgnoreUserGestureForTests(true); | 506 extensions::PermissionsRequestFunction::SetIgnoreUserGestureForTests(true); |
| 502 extensions::PermissionsRequestFunction::SetAutoConfirmForTests(true); | 507 extensions::PermissionsRequestFunction::SetAutoConfirmForTests(true); |
| 503 host_resolver()->AddRule("*.com", "127.0.0.1"); | |
| 504 ASSERT_TRUE(StartEmbeddedTestServer()); | 508 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 505 ASSERT_TRUE(RunExtensionTest("content_scripts/permissions")) << message_; | 509 ASSERT_TRUE(RunExtensionTest("content_scripts/permissions")) << message_; |
| 506 } | 510 } |
| 507 | 511 |
| 508 IN_PROC_BROWSER_TEST_F(ExtensionApiTestWithManagementPolicy, | 512 IN_PROC_BROWSER_TEST_F(ExtensionApiTestWithManagementPolicy, |
| 509 ContentScriptPolicy) { | 513 ContentScriptPolicy) { |
| 510 // Set enterprise policy to block injection to policy specified host. | 514 // Set enterprise policy to block injection to policy specified host. |
| 511 { | 515 { |
| 512 ExtensionManagementPolicyUpdater pref(&policy_provider_); | 516 ExtensionManagementPolicyUpdater pref(&policy_provider_); |
| 513 pref.AddRuntimeBlockedHost("*", "*://example.com/*"); | 517 pref.AddRuntimeBlockedHost("*", "*://example.com/*"); |
| 514 } | 518 } |
| 515 host_resolver()->AddRule("*.com", "127.0.0.1"); | |
| 516 ASSERT_TRUE(StartEmbeddedTestServer()); | 519 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 517 ASSERT_TRUE(RunExtensionTest("content_scripts/policy")) << message_; | 520 ASSERT_TRUE(RunExtensionTest("content_scripts/policy")) << message_; |
| 518 } | 521 } |
| 519 | 522 |
| 520 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, ContentScriptBypassPageCSP) { | 523 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, ContentScriptBypassPageCSP) { |
| 521 ASSERT_TRUE(StartEmbeddedTestServer()); | 524 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 522 ASSERT_TRUE(RunExtensionTest("content_scripts/bypass_page_csp")) << message_; | 525 ASSERT_TRUE(RunExtensionTest("content_scripts/bypass_page_csp")) << message_; |
| 523 } | 526 } |
| 524 | 527 |
| 525 // Test that when injecting a blocking content script, other scripts don't run | 528 // Test that when injecting a blocking content script, other scripts don't run |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 685 ui_test_utils::NavigateToURLWithDisposition( | 688 ui_test_utils::NavigateToURLWithDisposition( |
| 686 browser(), embedded_test_server()->GetURL("/empty.html"), | 689 browser(), embedded_test_server()->GetURL("/empty.html"), |
| 687 WindowOpenDisposition::CURRENT_TAB, | 690 WindowOpenDisposition::CURRENT_TAB, |
| 688 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 691 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 689 base::RunLoop().RunUntilIdle(); | 692 base::RunLoop().RunUntilIdle(); |
| 690 EXPECT_TRUE(listener.was_satisfied()); | 693 EXPECT_TRUE(listener.was_satisfied()); |
| 691 } | 694 } |
| 692 | 695 |
| 693 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, | 696 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, |
| 694 DontInjectContentScriptsInBackgroundPages) { | 697 DontInjectContentScriptsInBackgroundPages) { |
| 695 host_resolver()->AddRule("a.com", "127.0.0.1"); | |
| 696 ASSERT_TRUE(StartEmbeddedTestServer()); | 698 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 697 // Load two extensions, one with an iframe to a.com in its background page, | 699 // Load two extensions, one with an iframe to a.com in its background page, |
| 698 // the other, a content script for a.com. The latter should never be able to | 700 // the other, a content script for a.com. The latter should never be able to |
| 699 // inject the script, because scripts aren't allowed to run on foreign | 701 // inject the script, because scripts aren't allowed to run on foreign |
| 700 // extensions' pages. | 702 // extensions' pages. |
| 701 base::FilePath data_dir = test_data_dir_.AppendASCII("content_scripts"); | 703 base::FilePath data_dir = test_data_dir_.AppendASCII("content_scripts"); |
| 702 ExtensionTestMessageListener iframe_loaded_listener("iframe loaded", false); | 704 ExtensionTestMessageListener iframe_loaded_listener("iframe loaded", false); |
| 703 ExtensionTestMessageListener content_script_listener("script injected", | 705 ExtensionTestMessageListener content_script_listener("script injected", |
| 704 false); | 706 false); |
| 705 LoadExtension(data_dir.AppendASCII("script_a_com")); | 707 LoadExtension(data_dir.AppendASCII("script_a_com")); |
| 706 LoadExtension(data_dir.AppendASCII("background_page_iframe")); | 708 LoadExtension(data_dir.AppendASCII("background_page_iframe")); |
| 707 iframe_loaded_listener.WaitUntilSatisfied(); | 709 iframe_loaded_listener.WaitUntilSatisfied(); |
| 708 EXPECT_FALSE(content_script_listener.was_satisfied()); | 710 EXPECT_FALSE(content_script_listener.was_satisfied()); |
| 709 } | 711 } |
| 710 | 712 |
| 711 INSTANTIATE_TEST_CASE_P( | 713 INSTANTIATE_TEST_CASE_P( |
| 712 ContentScriptApiTests, | 714 ContentScriptApiTests, |
| 713 ContentScriptApiTest, | 715 ContentScriptApiTest, |
| 714 testing::Values(TestConfig::kDefault, | 716 testing::Values(TestConfig::kDefault, |
| 715 TestConfig::kYieldBetweenContentScriptRunsEnabled)); | 717 TestConfig::kYieldBetweenContentScriptRunsEnabled)); |
| 716 | 718 |
| 717 } // namespace extensions | 719 } // namespace extensions |
| OLD | NEW |