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