Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: chrome/common/extensions/extension_unittest.cc

Issue 10443105: Take 2 at implementing activeTab. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: many more tests Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/common/extensions/extension.h" 5 #include "chrome/common/extensions/extension.h"
6 6
7 #include "base/format_macros.h" 7 #include "base/format_macros.h"
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/json/json_file_value_serializer.h" 10 #include "base/json/json_file_value_serializer.h"
(...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 #endif 608 #endif
609 } 609 }
610 610
611 TEST(ExtensionTest, WantsFileAccess) { 611 TEST(ExtensionTest, WantsFileAccess) {
612 scoped_refptr<Extension> extension; 612 scoped_refptr<Extension> extension;
613 GURL file_url("file:///etc/passwd"); 613 GURL file_url("file:///etc/passwd");
614 614
615 // <all_urls> permission 615 // <all_urls> permission
616 extension = LoadManifest("permissions", "permissions_all_urls.json"); 616 extension = LoadManifest("permissions", "permissions_all_urls.json");
617 EXPECT_TRUE(extension->wants_file_access()); 617 EXPECT_TRUE(extension->wants_file_access());
618 EXPECT_FALSE(extension->CanExecuteScriptOnPage(file_url, NULL, NULL)); 618 EXPECT_FALSE(extension->CanExecuteScriptOnPage(file_url, -1, NULL, NULL));
619 extension = LoadManifest( 619 extension = LoadManifest(
620 "permissions", "permissions_all_urls.json", Extension::ALLOW_FILE_ACCESS); 620 "permissions", "permissions_all_urls.json", Extension::ALLOW_FILE_ACCESS);
621 EXPECT_TRUE(extension->wants_file_access()); 621 EXPECT_TRUE(extension->wants_file_access());
622 EXPECT_TRUE(extension->CanExecuteScriptOnPage(file_url, NULL, NULL)); 622 EXPECT_TRUE(extension->CanExecuteScriptOnPage(file_url, -1, NULL, NULL));
623 623
624 // file:///* permission 624 // file:///* permission
625 extension = LoadManifest("permissions", "permissions_file_scheme.json"); 625 extension = LoadManifest("permissions", "permissions_file_scheme.json");
626 EXPECT_TRUE(extension->wants_file_access()); 626 EXPECT_TRUE(extension->wants_file_access());
627 EXPECT_FALSE(extension->CanExecuteScriptOnPage(file_url, NULL, NULL)); 627 EXPECT_FALSE(extension->CanExecuteScriptOnPage(file_url, -1, NULL, NULL));
628 extension = LoadManifest("permissions", "permissions_file_scheme.json", 628 extension = LoadManifest("permissions", "permissions_file_scheme.json",
629 Extension::ALLOW_FILE_ACCESS); 629 Extension::ALLOW_FILE_ACCESS);
630 EXPECT_TRUE(extension->wants_file_access()); 630 EXPECT_TRUE(extension->wants_file_access());
631 EXPECT_TRUE(extension->CanExecuteScriptOnPage(file_url, NULL, NULL)); 631 EXPECT_TRUE(extension->CanExecuteScriptOnPage(file_url, -1, NULL, NULL));
632 632
633 // http://* permission 633 // http://* permission
634 extension = LoadManifest("permissions", "permissions_http_scheme.json"); 634 extension = LoadManifest("permissions", "permissions_http_scheme.json");
635 EXPECT_FALSE(extension->wants_file_access()); 635 EXPECT_FALSE(extension->wants_file_access());
636 EXPECT_FALSE(extension->CanExecuteScriptOnPage(file_url, NULL, NULL)); 636 EXPECT_FALSE(extension->CanExecuteScriptOnPage(file_url, -1, NULL, NULL));
637 extension = LoadManifest("permissions", "permissions_http_scheme.json", 637 extension = LoadManifest("permissions", "permissions_http_scheme.json",
638 Extension::ALLOW_FILE_ACCESS); 638 Extension::ALLOW_FILE_ACCESS);
639 EXPECT_FALSE(extension->wants_file_access()); 639 EXPECT_FALSE(extension->wants_file_access());
640 EXPECT_FALSE(extension->CanExecuteScriptOnPage(file_url, NULL, NULL)); 640 EXPECT_FALSE(extension->CanExecuteScriptOnPage(file_url, -1, NULL, NULL));
641 641
642 // <all_urls> content script match 642 // <all_urls> content script match
643 extension = LoadManifest("permissions", "content_script_all_urls.json"); 643 extension = LoadManifest("permissions", "content_script_all_urls.json");
644 EXPECT_TRUE(extension->wants_file_access()); 644 EXPECT_TRUE(extension->wants_file_access());
645 EXPECT_FALSE(extension->CanExecuteScriptOnPage( 645 EXPECT_FALSE(extension->CanExecuteScriptOnPage(
646 file_url, &extension->content_scripts()[0], NULL)); 646 file_url, -1, &extension->content_scripts()[0], NULL));
647 extension = LoadManifest("permissions", "content_script_all_urls.json", 647 extension = LoadManifest("permissions", "content_script_all_urls.json",
648 Extension::ALLOW_FILE_ACCESS); 648 Extension::ALLOW_FILE_ACCESS);
649 EXPECT_TRUE(extension->wants_file_access()); 649 EXPECT_TRUE(extension->wants_file_access());
650 EXPECT_TRUE(extension->CanExecuteScriptOnPage( 650 EXPECT_TRUE(extension->CanExecuteScriptOnPage(
651 file_url, &extension->content_scripts()[0], NULL)); 651 file_url, -1, &extension->content_scripts()[0], NULL));
652 652
653 // file:///* content script match 653 // file:///* content script match
654 extension = LoadManifest("permissions", "content_script_file_scheme.json"); 654 extension = LoadManifest("permissions", "content_script_file_scheme.json");
655 EXPECT_TRUE(extension->wants_file_access()); 655 EXPECT_TRUE(extension->wants_file_access());
656 EXPECT_FALSE(extension->CanExecuteScriptOnPage( 656 EXPECT_FALSE(extension->CanExecuteScriptOnPage(
657 file_url, &extension->content_scripts()[0], NULL)); 657 file_url, -1, &extension->content_scripts()[0], NULL));
658 extension = LoadManifest("permissions", "content_script_file_scheme.json", 658 extension = LoadManifest("permissions", "content_script_file_scheme.json",
659 Extension::ALLOW_FILE_ACCESS); 659 Extension::ALLOW_FILE_ACCESS);
660 EXPECT_TRUE(extension->wants_file_access()); 660 EXPECT_TRUE(extension->wants_file_access());
661 EXPECT_TRUE(extension->CanExecuteScriptOnPage( 661 EXPECT_TRUE(extension->CanExecuteScriptOnPage(
662 file_url, &extension->content_scripts()[0], NULL)); 662 file_url, -1, &extension->content_scripts()[0], NULL));
663 663
664 // http://* content script match 664 // http://* content script match
665 extension = LoadManifest("permissions", "content_script_http_scheme.json"); 665 extension = LoadManifest("permissions", "content_script_http_scheme.json");
666 EXPECT_FALSE(extension->wants_file_access()); 666 EXPECT_FALSE(extension->wants_file_access());
667 EXPECT_FALSE(extension->CanExecuteScriptOnPage( 667 EXPECT_FALSE(extension->CanExecuteScriptOnPage(
668 file_url, &extension->content_scripts()[0], NULL)); 668 file_url, -1, &extension->content_scripts()[0], NULL));
669 extension = LoadManifest("permissions", "content_script_http_scheme.json", 669 extension = LoadManifest("permissions", "content_script_http_scheme.json",
670 Extension::ALLOW_FILE_ACCESS); 670 Extension::ALLOW_FILE_ACCESS);
671 EXPECT_FALSE(extension->wants_file_access()); 671 EXPECT_FALSE(extension->wants_file_access());
672 EXPECT_FALSE(extension->CanExecuteScriptOnPage( 672 EXPECT_FALSE(extension->CanExecuteScriptOnPage(
673 file_url, &extension->content_scripts()[0], NULL)); 673 file_url, -1, &extension->content_scripts()[0], NULL));
674 } 674 }
675 675
676 TEST(ExtensionTest, ExtraFlags) { 676 TEST(ExtensionTest, ExtraFlags) {
677 scoped_refptr<Extension> extension; 677 scoped_refptr<Extension> extension;
678 extension = LoadManifest("app", "manifest.json", Extension::FROM_WEBSTORE); 678 extension = LoadManifest("app", "manifest.json", Extension::FROM_WEBSTORE);
679 EXPECT_TRUE(extension->from_webstore()); 679 EXPECT_TRUE(extension->from_webstore());
680 680
681 extension = LoadManifest("app", "manifest.json", Extension::FROM_BOOKMARK); 681 extension = LoadManifest("app", "manifest.json", Extension::FROM_BOOKMARK);
682 EXPECT_TRUE(extension->from_bookmark()); 682 EXPECT_TRUE(extension->from_bookmark());
683 683
684 extension = LoadManifest("app", "manifest.json", Extension::NO_FLAGS); 684 extension = LoadManifest("app", "manifest.json", Extension::NO_FLAGS);
685 EXPECT_FALSE(extension->from_bookmark()); 685 EXPECT_FALSE(extension->from_bookmark());
686 EXPECT_FALSE(extension->from_webstore()); 686 EXPECT_FALSE(extension->from_webstore());
687 } 687 }
688 688
689 // Base class for testing the CanExecuteScriptOnPage and CanCaptureVisiblePage 689 // Base class for testing the CanExecuteScriptOnPage and CanCaptureVisiblePage
690 // methods of Extension for extensions with various permissions. 690 // methods of Extension for extensions with various permissions.
691 class ExtensionScriptAndCaptureVisibleTest : public testing::Test { 691 class ExtensionScriptAndCaptureVisibleTest : public testing::Test {
692 public: 692 protected:
693 ExtensionScriptAndCaptureVisibleTest() { 693 ExtensionScriptAndCaptureVisibleTest()
694 PathService::Get(chrome::DIR_TEST_DATA, &dirpath_); 694 : http_url("http://www.google.com"),
695 http_url_with_path("http://www.google.com/index.html"),
696 https_url("https://www.google.com"),
697 file_url("file:///foo/bar"),
698 favicon_url("chrome://favicon/http://www.google.com"),
699 extension_url("chrome-extension://" +
700 Extension::GenerateIdForPath(FilePath(FILE_PATH_LITERAL("foo")))),
701 settings_url("chrome://settings"),
702 about_url("about:flags") {
703 urls_.insert(http_url);
704 urls_.insert(http_url_with_path);
705 urls_.insert(https_url);
706 urls_.insert(file_url);
707 urls_.insert(favicon_url);
708 urls_.insert(extension_url);
709 urls_.insert(settings_url);
710 urls_.insert(about_url);
695 } 711 }
696 712
697 bool Allowed(const Extension* extension, const GURL& url) { 713 bool Allowed(const Extension* extension, const GURL& url) {
698 return (extension->CanExecuteScriptOnPage(url, NULL, NULL) && 714 return Allowed(extension, url, -1);
699 extension->CanCaptureVisiblePage(url, NULL)); 715 }
716
717 bool Allowed(const Extension* extension, const GURL& url, int tab_id) {
718 return (extension->CanExecuteScriptOnPage(url, tab_id, NULL, NULL) &&
719 extension->CanCaptureVisiblePage(url, tab_id, NULL));
700 } 720 }
701 721
702 bool CaptureOnly(const Extension* extension, const GURL& url) { 722 bool CaptureOnly(const Extension* extension, const GURL& url) {
703 return !extension->CanExecuteScriptOnPage(url, NULL, NULL) && 723 return CaptureOnly(extension, url, -1);
704 extension->CanCaptureVisiblePage(url, NULL); 724 }
725
726 bool CaptureOnly(const Extension* extension, const GURL& url, int tab_id) {
727 return !extension->CanExecuteScriptOnPage(url, tab_id, NULL, NULL) &&
728 extension->CanCaptureVisiblePage(url, tab_id, NULL);
705 } 729 }
706 730
707 bool Blocked(const Extension* extension, const GURL& url) { 731 bool Blocked(const Extension* extension, const GURL& url) {
708 return !(extension->CanExecuteScriptOnPage(url, NULL, NULL) || 732 return Blocked(extension, url, -1);
709 extension->CanCaptureVisiblePage(url, NULL));
710 } 733 }
711 734
712 protected: 735 bool Blocked(const Extension* extension, const GURL& url, int tab_id) {
713 FilePath dirpath_; 736 return !(extension->CanExecuteScriptOnPage(url, tab_id, NULL, NULL) ||
737 extension->CanCaptureVisiblePage(url, tab_id, NULL));
738 }
739
740 bool AllowedExclusivelyOnTab(
741 const Extension* extension,
742 const std::set<GURL>& allowed_urls,
743 int tab_id) {
744 bool result = true;
745 for (std::set<GURL>::iterator it = urls_.begin(); it != urls_.end(); ++it) {
746 const GURL& url = *it;
747 if (allowed_urls.count(url))
748 result &= Allowed(extension, url, tab_id);
749 else
750 result &= Blocked(extension, url, tab_id);
751 }
752 return result;
753 }
754
755 // URLs that are "safe" to provide scripting and capture visible tab access
756 // to if the permissions allow it.
757 const GURL http_url;
758 const GURL http_url_with_path;
759 const GURL https_url;
760 const GURL file_url;
761
762 // We should allow host permission but not scripting permission for favicon
763 // urls.
764 const GURL favicon_url;
765
766 // URLs that regular extensions should never get access to.
767 const GURL extension_url;
768 const GURL settings_url;
769 const GURL about_url;
770
771 private:
772 // The set of all URLs above.
773 std::set<GURL> urls_;
714 }; 774 };
715 775
716 TEST_F(ExtensionScriptAndCaptureVisibleTest, Permissions) { 776 TEST_F(ExtensionScriptAndCaptureVisibleTest, Permissions) {
717 scoped_refptr<Extension> extension; 777 scoped_refptr<Extension> extension;
718 // URLs that are "safe" to provide scripting and capture visible tab access
719 // to if the permissions allow it.
720 GURL http_url("http://www.google.com");
721 GURL https_url("https://www.google.com");
722 GURL file_url("file:///foo/bar");
723
724 // We should allow host permission but not scripting permission for favicon
725 // urls.
726 GURL favicon_url("chrome://favicon/http://www.google.com");
727
728 std::string dummy_id =
729 Extension::GenerateIdForPath(FilePath(FILE_PATH_LITERAL("whatever")));
730
731 // URLs that regular extensions should never get access to.
732 GURL extension_url("chrome-extension://" + dummy_id);
733 GURL settings_url("chrome://settings");
734 GURL about_url("about:flags");
735 778
736 // Test <all_urls> for regular extensions. 779 // Test <all_urls> for regular extensions.
737 extension = LoadManifestStrict("script_and_capture", 780 extension = LoadManifestStrict("script_and_capture",
738 "extension_regular_all.json"); 781 "extension_regular_all.json");
739 EXPECT_TRUE(Allowed(extension, http_url)); 782 EXPECT_TRUE(Allowed(extension, http_url));
740 EXPECT_TRUE(Allowed(extension, https_url)); 783 EXPECT_TRUE(Allowed(extension, https_url));
741 EXPECT_TRUE(Blocked(extension, file_url)); 784 EXPECT_TRUE(Blocked(extension, file_url));
742 EXPECT_TRUE(Blocked(extension, settings_url)); 785 EXPECT_TRUE(Blocked(extension, settings_url));
743 EXPECT_TRUE(CaptureOnly(extension, favicon_url)); 786 EXPECT_TRUE(CaptureOnly(extension, favicon_url));
744 EXPECT_TRUE(Blocked(extension, about_url)); 787 EXPECT_TRUE(Blocked(extension, about_url));
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 EXPECT_TRUE(Allowed(extension, http_url)); 846 EXPECT_TRUE(Allowed(extension, http_url));
804 EXPECT_TRUE(Blocked(extension, https_url)); 847 EXPECT_TRUE(Blocked(extension, https_url));
805 EXPECT_TRUE(Blocked(extension, file_url)); 848 EXPECT_TRUE(Blocked(extension, file_url));
806 EXPECT_TRUE(Blocked(extension, settings_url)); 849 EXPECT_TRUE(Blocked(extension, settings_url));
807 EXPECT_TRUE(Blocked(extension, favicon_url)); 850 EXPECT_TRUE(Blocked(extension, favicon_url));
808 EXPECT_TRUE(Blocked(extension, about_url)); 851 EXPECT_TRUE(Blocked(extension, about_url));
809 EXPECT_TRUE(Blocked(extension, extension_url)); 852 EXPECT_TRUE(Blocked(extension, extension_url));
810 EXPECT_FALSE(extension->HasHostPermission(settings_url)); 853 EXPECT_FALSE(extension->HasHostPermission(settings_url));
811 } 854 }
812 855
856 TEST_F(ExtensionScriptAndCaptureVisibleTest, TabSpecific) {
857 scoped_refptr<Extension> extension =
858 LoadManifestStrict("script_and_capture", "tab_specific.json");
859
860 EXPECT_EQ(NULL, extension->GetTabSpecificHostPermissions(0));
861 EXPECT_EQ(NULL, extension->GetTabSpecificHostPermissions(1));
862 EXPECT_EQ(NULL, extension->GetTabSpecificHostPermissions(2));
863
864 std::set<GURL> no_urls;
865
866 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 0));
867 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 1));
868 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2));
869
870 URLPatternSet allowed_hosts;
871 allowed_hosts.AddPattern(URLPattern(URLPattern::SCHEME_ALL,
872 http_url.spec()));
873 std::set<GURL> allowed_urls;
874 allowed_urls.insert(http_url);
875 // http_url_with_path() will also be allowed, because Extension should be
876 // considering the security origin of the URL not the URL itself, and
877 // http_url is in allowed_hosts.
878 allowed_urls.insert(http_url_with_path);
879
880 extension->SetTabSpecificHostPermissions(0, allowed_hosts);
881 EXPECT_EQ(allowed_hosts, *extension->GetTabSpecificHostPermissions(0));
882
883 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, allowed_urls, 0));
884 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 1));
885 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2));
886
887 extension->ClearTabSpecificHostPermissions(0);
888 EXPECT_EQ(NULL, extension->GetTabSpecificHostPermissions(0));
889
890 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 0));
891 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 1));
892 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2));
893
894 std::set<GURL> more_allowed_urls = allowed_urls;
895 more_allowed_urls.insert(https_url);
896 URLPatternSet more_allowed_hosts = allowed_hosts;
897 more_allowed_hosts.AddPattern(URLPattern(URLPattern::SCHEME_ALL,
898 https_url.spec()));
899
900 extension->SetTabSpecificHostPermissions(0, allowed_hosts);
901 EXPECT_EQ(allowed_hosts, *extension->GetTabSpecificHostPermissions(0));
902 extension->SetTabSpecificHostPermissions(1, more_allowed_hosts);
903 EXPECT_EQ(more_allowed_hosts, *extension->GetTabSpecificHostPermissions(1));
904
905 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, allowed_urls, 0));
906 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, more_allowed_urls, 1));
907 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2));
908
909 extension->ClearTabSpecificHostPermissions(0);
910 EXPECT_EQ(NULL, extension->GetTabSpecificHostPermissions(0));
911
912 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 0));
913 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, more_allowed_urls, 1));
914 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2));
915
916 extension->ClearTabSpecificHostPermissions(1);
917 EXPECT_EQ(NULL, extension->GetTabSpecificHostPermissions(1));
918
919 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 0));
920 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 1));
921 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2));
922 }
923
924 TEST_F(ExtensionScriptAndCaptureVisibleTest, TabSpecificActiveHostPermissions) {
925 // - no active permissions.
926 // - no tab specific permissions
927 // - changing active permissions.
928 // - changing tab specific permissions.
929 // - lots of tab specific permissions (test merge algorithm).
not at google - send to devlin 2012/06/07 08:49:22 I've deleted this, just didn't upload yet so as to
930 }
931
813 TEST(ExtensionTest, GenerateId) { 932 TEST(ExtensionTest, GenerateId) {
814 std::string result; 933 std::string result;
815 EXPECT_TRUE(Extension::GenerateId("", &result)); 934 EXPECT_TRUE(Extension::GenerateId("", &result));
816 935
817 EXPECT_TRUE(Extension::GenerateId("test", &result)); 936 EXPECT_TRUE(Extension::GenerateId("test", &result));
818 EXPECT_EQ(result, "jpignaibiiemhngfjkcpokkamffknabf"); 937 EXPECT_EQ(result, "jpignaibiiemhngfjkcpokkamffknabf");
819 938
820 EXPECT_TRUE(Extension::GenerateId("_", &result)); 939 EXPECT_TRUE(Extension::GenerateId("_", &result));
821 EXPECT_EQ(result, "ncocknphbhhlhkikpnnlmbcnbgdempcd"); 940 EXPECT_EQ(result, "ncocknphbhhlhkikpnnlmbcnbgdempcd");
822 941
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
968 } 1087 }
969 1088
970 TEST(ExtensionTest, GetSyncTypeExtensionWithTwoPlugins) { 1089 TEST(ExtensionTest, GetSyncTypeExtensionWithTwoPlugins) {
971 scoped_refptr<Extension> extension( 1090 scoped_refptr<Extension> extension(
972 MakeSyncTestExtension(EXTENSION, GURL(), GURL(), 1091 MakeSyncTestExtension(EXTENSION, GURL(), GURL(),
973 Extension::INTERNAL, 2, FilePath())); 1092 Extension::INTERNAL, 2, FilePath()));
974 if (extension) 1093 if (extension)
975 EXPECT_EQ(extension->GetSyncType(), Extension::SYNC_TYPE_NONE); 1094 EXPECT_EQ(extension->GetSyncType(), Extension::SYNC_TYPE_NONE);
976 } 1095 }
977 #endif // !defined(OS_CHROMEOS) 1096 #endif // !defined(OS_CHROMEOS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698