| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #if defined(TOOLKIT_GTK) | 7 #if defined(TOOLKIT_GTK) |
| 8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| 11 #include "app/l10n_util.h" | 11 #include "app/l10n_util.h" |
| 12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
| 13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
| 15 #include "base/i18n/rtl.h" | 15 #include "base/i18n/rtl.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 19 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
| 20 #include "chrome/common/extensions/extension_action.h" | 20 #include "chrome/common/extensions/extension_action.h" |
| 21 #include "chrome/common/extensions/extension_constants.h" | 21 #include "chrome/common/extensions/extension_constants.h" |
| 22 #include "chrome/common/extensions/extension_error_utils.h" | 22 #include "chrome/common/extensions/extension_error_utils.h" |
| 23 #include "chrome/common/extensions/extension_resource.h" | 23 #include "chrome/common/extensions/extension_resource.h" |
| 24 #include "chrome/common/json_value_serializer.h" | 24 #include "chrome/common/json_value_serializer.h" |
| 25 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
| 26 #include "gfx/codec/png_codec.h" | 26 #include "gfx/codec/png_codec.h" |
| 27 #include "net/base/mime_sniffer.h" | 27 #include "net/base/mime_sniffer.h" |
| 28 #include "skia/ext/image_operations.h" | 28 #include "skia/ext/image_operations.h" |
| 29 #include "chrome/test/ui_test_utils.h" | |
| 30 #include "net/base/mock_host_resolver.h" | |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 30 |
| 33 namespace keys = extension_manifest_keys; | 31 namespace keys = extension_manifest_keys; |
| 34 namespace values = extension_manifest_values; | 32 namespace values = extension_manifest_values; |
| 35 namespace errors = extension_manifest_errors; | 33 namespace errors = extension_manifest_errors; |
| 36 | 34 |
| 37 class ExtensionTest : public testing::Test { | 35 class ExtensionTest : public testing::Test { |
| 38 }; | 36 }; |
| 39 | 37 |
| 40 // We persist location values in the preferences, so this is a sanity test that | 38 // We persist location values in the preferences, so this is a sanity test that |
| (...skipping 684 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 725 std::string error; | 723 std::string error; |
| 726 scoped_ptr<Extension> extension(new Extension(path.DirName())); | 724 scoped_ptr<Extension> extension(new Extension(path.DirName())); |
| 727 extension->InitFromValue(*static_cast<DictionaryValue*>(result.get()), | 725 extension->InitFromValue(*static_cast<DictionaryValue*>(result.get()), |
| 728 false, &error); | 726 false, &error); |
| 729 | 727 |
| 730 return extension.release(); | 728 return extension.release(); |
| 731 } | 729 } |
| 732 | 730 |
| 733 TEST(ExtensionTest, EffectiveHostPermissions) { | 731 TEST(ExtensionTest, EffectiveHostPermissions) { |
| 734 scoped_ptr<Extension> extension; | 732 scoped_ptr<Extension> extension; |
| 735 ExtensionExtent hosts; | 733 std::set<std::string> hosts; |
| 736 | 734 |
| 737 extension.reset(LoadManifest("effective_host_permissions", "empty.json")); | 735 extension.reset(LoadManifest("effective_host_permissions", "empty.json")); |
| 738 EXPECT_EQ(0u, extension->GetEffectiveHostPermissions().patterns().size()); | 736 EXPECT_EQ(0u, extension->GetEffectiveHostPermissions().size()); |
| 739 EXPECT_FALSE(extension->HasAccessToAllHosts()); | 737 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 740 | 738 |
| 741 extension.reset(LoadManifest("effective_host_permissions", "one_host.json")); | 739 extension.reset(LoadManifest("effective_host_permissions", "one_host.json")); |
| 742 hosts = extension->GetEffectiveHostPermissions(); | 740 hosts = extension->GetEffectiveHostPermissions(); |
| 743 EXPECT_EQ(1u, hosts.patterns().size()); | 741 EXPECT_EQ(1u, hosts.size()); |
| 744 EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); | 742 EXPECT_TRUE(hosts.find("www.google.com") != hosts.end()); |
| 745 EXPECT_FALSE(extension->HasAccessToAllHosts()); | 743 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 746 | 744 |
| 747 extension.reset(LoadManifest("effective_host_permissions", | 745 extension.reset(LoadManifest("effective_host_permissions", |
| 748 "one_host_wildcard.json")); | 746 "one_host_wildcard.json")); |
| 749 hosts = extension->GetEffectiveHostPermissions(); | 747 hosts = extension->GetEffectiveHostPermissions(); |
| 750 EXPECT_EQ(1u, hosts.patterns().size()); | 748 EXPECT_EQ(1u, hosts.size()); |
| 751 EXPECT_TRUE(hosts.ContainsURL(GURL("http://google.com"))); | 749 EXPECT_TRUE(hosts.find("google.com") != hosts.end()); |
| 752 EXPECT_FALSE(extension->HasAccessToAllHosts()); | 750 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 753 | 751 |
| 754 extension.reset(LoadManifest("effective_host_permissions", | 752 extension.reset(LoadManifest("effective_host_permissions", |
| 755 "two_hosts.json")); | 753 "two_hosts.json")); |
| 756 hosts = extension->GetEffectiveHostPermissions(); | 754 hosts = extension->GetEffectiveHostPermissions(); |
| 757 EXPECT_EQ(2u, hosts.patterns().size()); | 755 EXPECT_EQ(2u, hosts.size()); |
| 758 EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); | 756 EXPECT_TRUE(hosts.find("www.google.com") != hosts.end()); |
| 759 EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.reddit.com"))); | 757 EXPECT_TRUE(hosts.find("www.reddit.com") != hosts.end()); |
| 760 EXPECT_FALSE(extension->HasAccessToAllHosts()); | 758 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 761 | 759 |
| 762 extension.reset(LoadManifest("effective_host_permissions", | 760 extension.reset(LoadManifest("effective_host_permissions", |
| 763 "duplicate_host.json")); | 761 "duplicate_host.json")); |
| 764 hosts = extension->GetEffectiveHostPermissions(); | 762 hosts = extension->GetEffectiveHostPermissions(); |
| 765 EXPECT_EQ(1u, hosts.patterns().size()); | 763 EXPECT_EQ(1u, hosts.size()); |
| 766 EXPECT_TRUE(hosts.ContainsURL(GURL("http://google.com"))); | 764 EXPECT_TRUE(hosts.find("google.com") != hosts.end()); |
| 767 EXPECT_FALSE(extension->HasAccessToAllHosts()); | 765 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 768 | 766 |
| 769 extension.reset(LoadManifest("effective_host_permissions", | 767 extension.reset(LoadManifest("effective_host_permissions", |
| 770 "https_not_considered.json")); | 768 "https_not_considered.json")); |
| 771 hosts = extension->GetEffectiveHostPermissions(); | 769 hosts = extension->GetEffectiveHostPermissions(); |
| 772 EXPECT_EQ(1u, hosts.patterns().size()); | 770 EXPECT_EQ(1u, hosts.size()); |
| 773 EXPECT_TRUE(hosts.ContainsURL(GURL("http://google.com"))); | 771 EXPECT_TRUE(hosts.find("google.com") != hosts.end()); |
| 774 EXPECT_FALSE(extension->HasAccessToAllHosts()); | 772 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 775 | 773 |
| 776 extension.reset(LoadManifest("effective_host_permissions", | 774 extension.reset(LoadManifest("effective_host_permissions", |
| 777 "two_content_scripts.json")); | 775 "two_content_scripts.json")); |
| 778 hosts = extension->GetEffectiveHostPermissions(); | 776 hosts = extension->GetEffectiveHostPermissions(); |
| 779 EXPECT_EQ(3u, hosts.patterns().size()); | 777 EXPECT_EQ(3u, hosts.size()); |
| 780 EXPECT_TRUE(hosts.ContainsURL(GURL("http://google.com"))); | 778 EXPECT_TRUE(hosts.find("google.com") != hosts.end()); |
| 781 EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.reddit.com"))); | 779 EXPECT_TRUE(hosts.find("www.reddit.com") != hosts.end()); |
| 782 EXPECT_TRUE(hosts.ContainsURL(GURL("http://news.ycombinator.com"))); | 780 EXPECT_TRUE(hosts.find("news.ycombinator.com") != hosts.end()); |
| 783 EXPECT_FALSE(extension->HasAccessToAllHosts()); | 781 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 784 | 782 |
| 785 extension.reset(LoadManifest("effective_host_permissions", | 783 extension.reset(LoadManifest("effective_host_permissions", |
| 786 "duplicate_content_script.json")); | 784 "duplicate_content_script.json")); |
| 787 hosts = extension->GetEffectiveHostPermissions(); | 785 hosts = extension->GetEffectiveHostPermissions(); |
| 788 EXPECT_EQ(3u, hosts.patterns().size()); | 786 EXPECT_EQ(2u, hosts.size()); |
| 789 EXPECT_TRUE(hosts.ContainsURL(GURL("http://google.com"))); | 787 EXPECT_TRUE(hosts.find("google.com") != hosts.end()); |
| 790 EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.reddit.com"))); | 788 EXPECT_TRUE(hosts.find("www.reddit.com") != hosts.end()); |
| 791 EXPECT_FALSE(extension->HasAccessToAllHosts()); | 789 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 792 | 790 |
| 793 extension.reset(LoadManifest("effective_host_permissions", | 791 extension.reset(LoadManifest("effective_host_permissions", |
| 794 "all_hosts.json")); | 792 "all_hosts.json")); |
| 795 hosts = extension->GetEffectiveHostPermissions(); | 793 hosts = extension->GetEffectiveHostPermissions(); |
| 796 EXPECT_EQ(1u, hosts.patterns().size()); | 794 EXPECT_EQ(1u, hosts.size()); |
| 797 EXPECT_TRUE(hosts.ContainsURL(GURL("http://test/"))); | 795 EXPECT_TRUE(hosts.find("") != hosts.end()); |
| 798 EXPECT_TRUE(extension->HasAccessToAllHosts()); | 796 EXPECT_TRUE(extension->HasAccessToAllHosts()); |
| 799 | 797 |
| 800 extension.reset(LoadManifest("effective_host_permissions", | 798 extension.reset(LoadManifest("effective_host_permissions", |
| 801 "all_hosts2.json")); | 799 "all_hosts2.json")); |
| 802 hosts = extension->GetEffectiveHostPermissions(); | 800 hosts = extension->GetEffectiveHostPermissions(); |
| 803 EXPECT_EQ(2u, hosts.patterns().size()); | 801 EXPECT_EQ(2u, hosts.size()); |
| 804 EXPECT_TRUE(hosts.ContainsURL(GURL("http://test/"))); | 802 EXPECT_TRUE(hosts.find("") != hosts.end()); |
| 805 EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); | 803 EXPECT_TRUE(hosts.find("www.google.com") != hosts.end()); |
| 806 EXPECT_TRUE(extension->HasAccessToAllHosts()); | 804 EXPECT_TRUE(extension->HasAccessToAllHosts()); |
| 807 | 805 |
| 808 extension.reset(LoadManifest("effective_host_permissions", | 806 extension.reset(LoadManifest("effective_host_permissions", |
| 809 "all_hosts3.json")); | 807 "all_hosts3.json")); |
| 810 hosts = extension->GetEffectiveHostPermissions(); | 808 hosts = extension->GetEffectiveHostPermissions(); |
| 811 EXPECT_EQ(2u, hosts.patterns().size()); | 809 EXPECT_EQ(2u, hosts.size()); |
| 812 EXPECT_TRUE(hosts.ContainsURL(GURL("https://test/"))); | 810 EXPECT_TRUE(hosts.find("") != hosts.end()); |
| 813 EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); | 811 EXPECT_TRUE(hosts.find("www.google.com") != hosts.end()); |
| 814 EXPECT_TRUE(extension->HasAccessToAllHosts()); | 812 EXPECT_TRUE(extension->HasAccessToAllHosts()); |
| 815 } | 813 } |
| 816 | 814 |
| 817 TEST(ExtensionTest, IsPrivilegeIncrease) { | 815 TEST(ExtensionTest, IsPrivilegeIncrease) { |
| 818 const struct { | 816 const struct { |
| 819 const char* base_name; | 817 const char* base_name; |
| 820 bool expect_success; | 818 bool expect_success; |
| 821 } kTests[] = { | 819 } kTests[] = { |
| 822 { "allhosts1", false }, // all -> all | 820 { "allhosts1", false }, // all -> all |
| 823 { "allhosts2", false }, // all -> one | 821 { "allhosts2", false }, // all -> one |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 927 extension->SetCachedImage(resource, image, original_size); | 925 extension->SetCachedImage(resource, image, original_size); |
| 928 EXPECT_TRUE(extension->HasCachedImage(resource, original_size)); | 926 EXPECT_TRUE(extension->HasCachedImage(resource, original_size)); |
| 929 EXPECT_TRUE(extension->HasCachedImage(resource, size128)); | 927 EXPECT_TRUE(extension->HasCachedImage(resource, size128)); |
| 930 EXPECT_TRUE(SizeEquals(extension->GetCachedImage(resource, original_size), | 928 EXPECT_TRUE(SizeEquals(extension->GetCachedImage(resource, original_size), |
| 931 original_size)); | 929 original_size)); |
| 932 EXPECT_TRUE(SizeEquals(extension->GetCachedImage(resource, size128), | 930 EXPECT_TRUE(SizeEquals(extension->GetCachedImage(resource, size128), |
| 933 original_size)); | 931 original_size)); |
| 934 EXPECT_EQ(extension->GetCachedImage(resource, original_size).getPixels(), | 932 EXPECT_EQ(extension->GetCachedImage(resource, original_size).getPixels(), |
| 935 extension->GetCachedImage(resource, size128).getPixels()); | 933 extension->GetCachedImage(resource, size128).getPixels()); |
| 936 } | 934 } |
| OLD | NEW |