OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <list> | 5 #include <list> |
6 #include <set> | 6 #include <set> |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 EXPECT_TRUE(unload_observer_->WaitForUnload(id)); | 486 EXPECT_TRUE(unload_observer_->WaitForUnload(id)); |
487 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); | 487 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
488 int reasons = prefs->GetDisableReasons(id); | 488 int reasons = prefs->GetDisableReasons(id); |
489 EXPECT_TRUE(reasons & Extension::DISABLE_CORRUPTED); | 489 EXPECT_TRUE(reasons & Extension::DISABLE_CORRUPTED); |
490 | 490 |
491 // This needs to happen before the ExtensionRegistry gets deleted, which | 491 // This needs to happen before the ExtensionRegistry gets deleted, which |
492 // happens before TearDownOnMainThread is called. | 492 // happens before TearDownOnMainThread is called. |
493 unload_observer_.reset(); | 493 unload_observer_.reset(); |
494 } | 494 } |
495 | 495 |
| 496 void TestContentScriptExtension(const std::string& crx_relpath, |
| 497 const std::string& id, |
| 498 const std::string& script_relpath) { |
| 499 VerifierObserver verifier_observer; |
| 500 ContentVerifier::SetObserverForTests(&verifier_observer); |
| 501 // Install the extension with content scripts. The initial read of the |
| 502 // content scripts will fail verification because they are read before the |
| 503 // content verification system has completed a one-time processing of the |
| 504 // expected hashes. (The extension only contains the root level hashes of |
| 505 // the merkle tree, but the content verification system builds the entire |
| 506 // tree and caches it in the extension install directory - see |
| 507 // ContentHashFetcher for more details). |
| 508 const Extension* extension = InstallExtensionFromWebstore( |
| 509 test_data_dir_.AppendASCII(crx_relpath), 1); |
| 510 ASSERT_TRUE(extension); |
| 511 ASSERT_EQ(extension->id(), id); |
| 512 |
| 513 // Wait for the content verification code to finish processing the hashes. |
| 514 if (!base::ContainsKey(verifier_observer.completed_fetches(), id)) |
| 515 verifier_observer.WaitForFetchComplete(id); |
| 516 |
| 517 // Now disable the extension, since content scripts are read at enable time, |
| 518 // set up our job observer, and re-enable, expecting a success this time. |
| 519 DisableExtension(id); |
| 520 JobObserver job_observer; |
| 521 ContentVerifyJob::SetObserverForTests(&job_observer); |
| 522 base::FilePath script_relfilepath = |
| 523 base::FilePath().AppendASCII(script_relpath); |
| 524 job_observer.ExpectJobResult(id, script_relfilepath, |
| 525 JobObserver::Result::SUCCESS); |
| 526 EnableExtension(id); |
| 527 EXPECT_TRUE(job_observer.WaitForExpectedJobs()); |
| 528 |
| 529 // Now alter the contents of the content script, reload the extension, and |
| 530 // expect to see a job failure due to the content script content hash not |
| 531 // being what was signed by the webstore. |
| 532 base::FilePath scriptfile = extension->path().AppendASCII(script_relpath); |
| 533 std::string extra = "some_extra_function_call();"; |
| 534 ASSERT_TRUE(base::AppendToFile(scriptfile, extra.data(), extra.size())); |
| 535 DisableExtension(id); |
| 536 job_observer.ExpectJobResult(id, script_relfilepath, |
| 537 JobObserver::Result::FAILURE); |
| 538 EnableExtension(id); |
| 539 EXPECT_TRUE(job_observer.WaitForExpectedJobs()); |
| 540 |
| 541 ContentVerifyJob::SetObserverForTests(NULL); |
| 542 } |
| 543 |
496 protected: | 544 protected: |
497 JobDelegate delegate_; | 545 JobDelegate delegate_; |
498 std::unique_ptr<RegistryObserver> unload_observer_; | 546 std::unique_ptr<RegistryObserver> unload_observer_; |
499 GURL page_url_; | 547 GURL page_url_; |
500 }; | 548 }; |
501 | 549 |
502 IN_PROC_BROWSER_TEST_F(ContentVerifierTest, FailOnRead) { | 550 IN_PROC_BROWSER_TEST_F(ContentVerifierTest, FailOnRead) { |
503 EXPECT_EQ(0, delegate_.bytes_read_failed()); | 551 EXPECT_EQ(0, delegate_.bytes_read_failed()); |
504 delegate_.fail_next_read(); | 552 delegate_.fail_next_read(); |
505 OpenPageAndWaitForUnload(); | 553 OpenPageAndWaitForUnload(); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 // read again. | 606 // read again. |
559 DisableExtension(id); | 607 DisableExtension(id); |
560 EnableExtension(id); | 608 EnableExtension(id); |
561 | 609 |
562 EXPECT_TRUE(job_observer.WaitForExpectedJobs()); | 610 EXPECT_TRUE(job_observer.WaitForExpectedJobs()); |
563 | 611 |
564 ContentVerifyJob::SetObserverForTests(NULL); | 612 ContentVerifyJob::SetObserverForTests(NULL); |
565 } | 613 } |
566 | 614 |
567 IN_PROC_BROWSER_TEST_F(ContentVerifierTest, ContentScripts) { | 615 IN_PROC_BROWSER_TEST_F(ContentVerifierTest, ContentScripts) { |
568 VerifierObserver verifier_observer; | 616 TestContentScriptExtension("content_verifier/content_script.crx", |
569 ContentVerifier::SetObserverForTests(&verifier_observer); | 617 "jmllhlobpjcnnomjlipadejplhmheiif", "script.js"); |
| 618 } |
570 | 619 |
571 // Install an extension with content scripts. The initial read of the content | 620 IN_PROC_BROWSER_TEST_F(ContentVerifierTest, ContentScriptsInLocales) { |
572 // scripts will fail verification because they are read before the content | 621 TestContentScriptExtension("content_verifier/content_script_locales.crx", |
573 // verification system has completed a one-time processing of the expected | 622 "jaghonccckpcikmliipifpoodmeofoon", |
574 // hashes. (The extension only contains the root level hashes of the merkle | 623 "_locales/en/content_script.js"); |
575 // tree, but the content verification system builds the entire tree and | |
576 // caches it in the extension install directory - see ContentHashFetcher for | |
577 // more details). | |
578 std::string id = "jmllhlobpjcnnomjlipadejplhmheiif"; | |
579 const Extension* extension = InstallExtensionFromWebstore( | |
580 test_data_dir_.AppendASCII("content_verifier/content_script.crx"), 1); | |
581 ASSERT_TRUE(extension); | |
582 ASSERT_EQ(extension->id(), id); | |
583 | |
584 // Wait for the content verification code to finish processing the hashes. | |
585 if (!base::ContainsKey(verifier_observer.completed_fetches(), id)) | |
586 verifier_observer.WaitForFetchComplete(id); | |
587 | |
588 // Now disable the extension, since content scripts are read at enable time, | |
589 // set up our job observer, and re-enable, expecting a success this time. | |
590 DisableExtension(id); | |
591 JobObserver job_observer; | |
592 ContentVerifyJob::SetObserverForTests(&job_observer); | |
593 job_observer.ExpectJobResult(id, | |
594 base::FilePath(FILE_PATH_LITERAL("script.js")), | |
595 JobObserver::Result::SUCCESS); | |
596 EnableExtension(id); | |
597 EXPECT_TRUE(job_observer.WaitForExpectedJobs()); | |
598 | |
599 // Now alter the contents of the content script, reload the extension, and | |
600 // expect to see a job failure due to the content script content hash not | |
601 // being what was signed by the webstore. | |
602 base::FilePath scriptfile = extension->path().AppendASCII("script.js"); | |
603 std::string extra = "some_extra_function_call();"; | |
604 ASSERT_TRUE(base::AppendToFile(scriptfile, extra.data(), extra.size())); | |
605 DisableExtension(id); | |
606 job_observer.ExpectJobResult(id, | |
607 base::FilePath(FILE_PATH_LITERAL("script.js")), | |
608 JobObserver::Result::FAILURE); | |
609 EnableExtension(id); | |
610 EXPECT_TRUE(job_observer.WaitForExpectedJobs()); | |
611 | |
612 ContentVerifyJob::SetObserverForTests(NULL); | |
613 } | 624 } |
614 | 625 |
615 // Tests the case of a corrupt extension that is force-installed by policy and | 626 // Tests the case of a corrupt extension that is force-installed by policy and |
616 // should not be allowed to be manually uninstalled/disabled by the user. | 627 // should not be allowed to be manually uninstalled/disabled by the user. |
617 IN_PROC_BROWSER_TEST_F(ContentVerifierTest, PolicyCorrupted) { | 628 IN_PROC_BROWSER_TEST_F(ContentVerifierTest, PolicyCorrupted) { |
618 ExtensionSystem* system = ExtensionSystem::Get(profile()); | 629 ExtensionSystem* system = ExtensionSystem::Get(profile()); |
619 ExtensionService* service = system->extension_service(); | 630 ExtensionService* service = system->extension_service(); |
620 | 631 |
621 // The id of our test extension. | 632 // The id of our test extension. |
622 std::string id("npnbmohejbjohgpjnmjagbafnjhkmgko"); | 633 std::string id("npnbmohejbjohgpjnmjagbafnjhkmgko"); |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 // Assert that the first reinstall action happened with a delay of 0, and | 819 // Assert that the first reinstall action happened with a delay of 0, and |
809 // then kept growing each additional time. | 820 // then kept growing each additional time. |
810 ASSERT_EQ(iterations, calls.size()); | 821 ASSERT_EQ(iterations, calls.size()); |
811 EXPECT_EQ(base::TimeDelta(), delay_tracker.calls()[0]); | 822 EXPECT_EQ(base::TimeDelta(), delay_tracker.calls()[0]); |
812 for (size_t i = 1; i < delay_tracker.calls().size(); i++) { | 823 for (size_t i = 1; i < delay_tracker.calls().size(); i++) { |
813 EXPECT_LT(calls[i - 1], calls[i]); | 824 EXPECT_LT(calls[i - 1], calls[i]); |
814 } | 825 } |
815 } | 826 } |
816 | 827 |
817 } // namespace extensions | 828 } // namespace extensions |
OLD | NEW |