| 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/json/json_file_value_serializer.h" | 6 #include "base/json/json_file_value_serializer.h" |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/common/chrome_paths.h" | 10 #include "chrome/common/chrome_paths.h" |
| 11 #include "chrome/common/chrome_switches.h" | 11 #include "chrome/common/chrome_switches.h" |
| 12 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
| 13 #include "chrome/common/extensions/extension_test_util.h" | 13 #include "chrome/common/extensions/extension_test_util.h" |
| 14 #include "chrome/common/extensions/features/feature.h" | 14 #include "chrome/common/extensions/features/feature.h" |
| 15 #include "chrome/common/extensions/permissions/permission_set.h" | 15 #include "chrome/common/extensions/permissions/permission_set.h" |
| 16 #include "chrome/common/extensions/permissions/permissions_data.h" |
| 16 #include "chrome/common/extensions/permissions/permissions_info.h" | 17 #include "chrome/common/extensions/permissions/permissions_info.h" |
| 17 #include "chrome/common/extensions/permissions/socket_permission.h" | 18 #include "chrome/common/extensions/permissions/socket_permission.h" |
| 18 #include "extensions/common/error_utils.h" | 19 #include "extensions/common/error_utils.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 using extension_test_util::LoadManifest; | 22 using extension_test_util::LoadManifest; |
| 22 | 23 |
| 23 namespace extensions { | 24 namespace extensions { |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 EXPECT_EQ(APIPermission::kUnlimitedStorage, | 118 EXPECT_EQ(APIPermission::kUnlimitedStorage, |
| 118 info->GetByName("unlimited_storage")->id()); | 119 info->GetByName("unlimited_storage")->id()); |
| 119 } | 120 } |
| 120 | 121 |
| 121 TEST(PermissionsTest, EffectiveHostPermissions) { | 122 TEST(PermissionsTest, EffectiveHostPermissions) { |
| 122 scoped_refptr<Extension> extension; | 123 scoped_refptr<Extension> extension; |
| 123 scoped_refptr<const PermissionSet> permissions; | 124 scoped_refptr<const PermissionSet> permissions; |
| 124 | 125 |
| 125 extension = LoadManifest("effective_host_permissions", "empty.json"); | 126 extension = LoadManifest("effective_host_permissions", "empty.json"); |
| 126 permissions = extension->GetActivePermissions(); | 127 permissions = extension->GetActivePermissions(); |
| 127 EXPECT_EQ(0u, extension->GetEffectiveHostPermissions().patterns().size()); | 128 EXPECT_EQ( |
| 129 0u, |
| 130 PermissionsData::GetEffectiveHostPermissions( |
| 131 extension).patterns().size()); |
| 128 EXPECT_FALSE(permissions->HasEffectiveAccessToURL( | 132 EXPECT_FALSE(permissions->HasEffectiveAccessToURL( |
| 129 GURL("http://www.google.com"))); | 133 GURL("http://www.google.com"))); |
| 130 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts()); | 134 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts()); |
| 131 | 135 |
| 132 extension = LoadManifest("effective_host_permissions", "one_host.json"); | 136 extension = LoadManifest("effective_host_permissions", "one_host.json"); |
| 133 permissions = extension->GetActivePermissions(); | 137 permissions = extension->GetActivePermissions(); |
| 134 EXPECT_TRUE(permissions->HasEffectiveAccessToURL( | 138 EXPECT_TRUE(permissions->HasEffectiveAccessToURL( |
| 135 GURL("http://www.google.com"))); | 139 GURL("http://www.google.com"))); |
| 136 EXPECT_FALSE(permissions->HasEffectiveAccessToURL( | 140 EXPECT_FALSE(permissions->HasEffectiveAccessToURL( |
| 137 GURL("https://www.google.com"))); | 141 GURL("https://www.google.com"))); |
| (...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 799 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { | 803 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { |
| 800 EXPECT_EQ(kTests[i].expect_success, | 804 EXPECT_EQ(kTests[i].expect_success, |
| 801 empty->HasAnyAccessToAPI(kTests[i].api_name)); | 805 empty->HasAnyAccessToAPI(kTests[i].api_name)); |
| 802 } | 806 } |
| 803 } | 807 } |
| 804 | 808 |
| 805 TEST(PermissionsTest, GetWarningMessages_ManyHosts) { | 809 TEST(PermissionsTest, GetWarningMessages_ManyHosts) { |
| 806 scoped_refptr<Extension> extension; | 810 scoped_refptr<Extension> extension; |
| 807 | 811 |
| 808 extension = LoadManifest("permissions", "many-hosts.json"); | 812 extension = LoadManifest("permissions", "many-hosts.json"); |
| 809 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); | 813 std::vector<string16> warnings = |
| 814 PermissionsData::GetPermissionMessageStrings(extension); |
| 810 ASSERT_EQ(1u, warnings.size()); | 815 ASSERT_EQ(1u, warnings.size()); |
| 811 EXPECT_EQ("Access your data on encrypted.google.com and www.google.com", | 816 EXPECT_EQ("Access your data on encrypted.google.com and www.google.com", |
| 812 UTF16ToUTF8(warnings[0])); | 817 UTF16ToUTF8(warnings[0])); |
| 813 } | 818 } |
| 814 | 819 |
| 815 TEST(PermissionsTest, GetWarningMessages_Plugins) { | 820 TEST(PermissionsTest, GetWarningMessages_Plugins) { |
| 816 scoped_refptr<Extension> extension; | 821 scoped_refptr<Extension> extension; |
| 817 scoped_refptr<PermissionSet> permissions; | 822 scoped_refptr<PermissionSet> permissions; |
| 818 | 823 |
| 819 extension = LoadManifest("permissions", "plugins.json"); | 824 extension = LoadManifest("permissions", "plugins.json"); |
| 820 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); | 825 std::vector<string16> warnings = |
| 826 PermissionsData::GetPermissionMessageStrings(extension); |
| 821 // We don't parse the plugins key on Chrome OS, so it should not ask for any | 827 // We don't parse the plugins key on Chrome OS, so it should not ask for any |
| 822 // permissions. | 828 // permissions. |
| 823 #if defined(OS_CHROMEOS) | 829 #if defined(OS_CHROMEOS) |
| 824 ASSERT_EQ(0u, warnings.size()); | 830 ASSERT_EQ(0u, warnings.size()); |
| 825 #else | 831 #else |
| 826 ASSERT_EQ(1u, warnings.size()); | 832 ASSERT_EQ(1u, warnings.size()); |
| 827 EXPECT_EQ("Access all data on your computer and the websites you visit", | 833 EXPECT_EQ("Access all data on your computer and the websites you visit", |
| 828 UTF16ToUTF8(warnings[0])); | 834 UTF16ToUTF8(warnings[0])); |
| 829 #endif | 835 #endif |
| 830 } | 836 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 861 EXPECT_FALSE(Contains(warnings, "Use your microphone and camera")); | 867 EXPECT_FALSE(Contains(warnings, "Use your microphone and camera")); |
| 862 EXPECT_TRUE(Contains(warnings, "Use your camera")); | 868 EXPECT_TRUE(Contains(warnings, "Use your camera")); |
| 863 } | 869 } |
| 864 | 870 |
| 865 TEST(PermissionsTest, GetWarningMessages_Serial) { | 871 TEST(PermissionsTest, GetWarningMessages_Serial) { |
| 866 scoped_refptr<Extension> extension = | 872 scoped_refptr<Extension> extension = |
| 867 LoadManifest("permissions", "serial.json"); | 873 LoadManifest("permissions", "serial.json"); |
| 868 | 874 |
| 869 EXPECT_TRUE(extension->is_platform_app()); | 875 EXPECT_TRUE(extension->is_platform_app()); |
| 870 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSerial)); | 876 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSerial)); |
| 871 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); | 877 std::vector<string16> warnings = |
| 878 PermissionsData::GetPermissionMessageStrings(extension); |
| 872 EXPECT_TRUE(Contains(warnings, | 879 EXPECT_TRUE(Contains(warnings, |
| 873 "Use serial devices attached to your computer")); | 880 "Use serial devices attached to your computer")); |
| 874 ASSERT_EQ(1u, warnings.size()); | 881 ASSERT_EQ(1u, warnings.size()); |
| 875 } | 882 } |
| 876 | 883 |
| 877 TEST(PermissionsTest, GetWarningMessages_Socket_AnyHost) { | 884 TEST(PermissionsTest, GetWarningMessages_Socket_AnyHost) { |
| 878 Feature::ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_DEV); | 885 Feature::ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_DEV); |
| 879 | 886 |
| 880 scoped_refptr<Extension> extension = | 887 scoped_refptr<Extension> extension = |
| 881 LoadManifest("permissions", "socket_any_host.json"); | 888 LoadManifest("permissions", "socket_any_host.json"); |
| 882 EXPECT_TRUE(extension->is_platform_app()); | 889 EXPECT_TRUE(extension->is_platform_app()); |
| 883 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSocket)); | 890 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSocket)); |
| 884 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); | 891 std::vector<string16> warnings = |
| 892 PermissionsData::GetPermissionMessageStrings(extension); |
| 885 EXPECT_EQ(1u, warnings.size()); | 893 EXPECT_EQ(1u, warnings.size()); |
| 886 EXPECT_TRUE(Contains(warnings, "Exchange data with any computer " | 894 EXPECT_TRUE(Contains(warnings, "Exchange data with any computer " |
| 887 "on the local network or internet")); | 895 "on the local network or internet")); |
| 888 } | 896 } |
| 889 | 897 |
| 890 TEST(PermissionsTest, GetWarningMessages_Socket_OneDomainTwoHostnames) { | 898 TEST(PermissionsTest, GetWarningMessages_Socket_OneDomainTwoHostnames) { |
| 891 Feature::ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_DEV); | 899 Feature::ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_DEV); |
| 892 | 900 |
| 893 scoped_refptr<Extension> extension = | 901 scoped_refptr<Extension> extension = |
| 894 LoadManifest("permissions", "socket_one_domain_two_hostnames.json"); | 902 LoadManifest("permissions", "socket_one_domain_two_hostnames.json"); |
| 895 EXPECT_TRUE(extension->is_platform_app()); | 903 EXPECT_TRUE(extension->is_platform_app()); |
| 896 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSocket)); | 904 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSocket)); |
| 897 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); | 905 std::vector<string16> warnings = |
| 906 PermissionsData::GetPermissionMessageStrings(extension); |
| 898 | 907 |
| 899 // Verify the warnings, including support for unicode characters, the fact | 908 // Verify the warnings, including support for unicode characters, the fact |
| 900 // that domain host warnings come before specific host warnings, and the fact | 909 // that domain host warnings come before specific host warnings, and the fact |
| 901 // that domains and hostnames are in alphabetical order regardless of the | 910 // that domains and hostnames are in alphabetical order regardless of the |
| 902 // order in the manifest file. | 911 // order in the manifest file. |
| 903 EXPECT_EQ(2u, warnings.size()); | 912 EXPECT_EQ(2u, warnings.size()); |
| 904 if (warnings.size() > 0) | 913 if (warnings.size() > 0) |
| 905 EXPECT_EQ(warnings[0], | 914 EXPECT_EQ(warnings[0], |
| 906 UTF8ToUTF16("Exchange data with any computer in the domain " | 915 UTF8ToUTF16("Exchange data with any computer in the domain " |
| 907 "example.org")); | 916 "example.org")); |
| 908 if (warnings.size() > 1) | 917 if (warnings.size() > 1) |
| 909 EXPECT_EQ(warnings[1], | 918 EXPECT_EQ(warnings[1], |
| 910 UTF8ToUTF16("Exchange data with the computers named: " | 919 UTF8ToUTF16("Exchange data with the computers named: " |
| 911 "b\xC3\xA5r.example.com foo.example.com")); | 920 "b\xC3\xA5r.example.com foo.example.com")); |
| 912 // "\xC3\xA5" = UTF-8 for lowercase A with ring above | 921 // "\xC3\xA5" = UTF-8 for lowercase A with ring above |
| 913 } | 922 } |
| 914 | 923 |
| 915 TEST(PermissionsTest, GetWarningMessages_Socket_TwoDomainsOneHostname) { | 924 TEST(PermissionsTest, GetWarningMessages_Socket_TwoDomainsOneHostname) { |
| 916 Feature::ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_DEV); | 925 Feature::ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_DEV); |
| 917 | 926 |
| 918 scoped_refptr<Extension> extension = | 927 scoped_refptr<Extension> extension = |
| 919 LoadManifest("permissions", "socket_two_domains_one_hostname.json"); | 928 LoadManifest("permissions", "socket_two_domains_one_hostname.json"); |
| 920 EXPECT_TRUE(extension->is_platform_app()); | 929 EXPECT_TRUE(extension->is_platform_app()); |
| 921 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSocket)); | 930 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSocket)); |
| 922 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); | 931 std::vector<string16> warnings = |
| 932 PermissionsData::GetPermissionMessageStrings(extension); |
| 923 | 933 |
| 924 // Verify the warnings, including the fact that domain host warnings come | 934 // Verify the warnings, including the fact that domain host warnings come |
| 925 // before specific host warnings and the fact that domains and hostnames are | 935 // before specific host warnings and the fact that domains and hostnames are |
| 926 // in alphabetical order regardless of the order in the manifest file. | 936 // in alphabetical order regardless of the order in the manifest file. |
| 927 EXPECT_EQ(2u, warnings.size()); | 937 EXPECT_EQ(2u, warnings.size()); |
| 928 if (warnings.size() > 0) | 938 if (warnings.size() > 0) |
| 929 EXPECT_EQ(warnings[0], | 939 EXPECT_EQ(warnings[0], |
| 930 UTF8ToUTF16("Exchange data with any computer in the domains: " | 940 UTF8ToUTF16("Exchange data with any computer in the domains: " |
| 931 "example.com foo.example.org")); | 941 "example.com foo.example.org")); |
| 932 if (warnings.size() > 1) | 942 if (warnings.size() > 1) |
| 933 EXPECT_EQ(warnings[1], | 943 EXPECT_EQ(warnings[1], |
| 934 UTF8ToUTF16("Exchange data with the computer named " | 944 UTF8ToUTF16("Exchange data with the computer named " |
| 935 "bar.example.org")); | 945 "bar.example.org")); |
| 936 } | 946 } |
| 937 | 947 |
| 938 TEST(PermissionsTest, GetWarningMessages_PlatformApppHosts) { | 948 TEST(PermissionsTest, GetWarningMessages_PlatformApppHosts) { |
| 939 scoped_refptr<Extension> extension; | 949 scoped_refptr<Extension> extension; |
| 940 | 950 |
| 941 extension = LoadManifest("permissions", "platform_app_hosts.json"); | 951 extension = LoadManifest("permissions", "platform_app_hosts.json"); |
| 942 EXPECT_TRUE(extension->is_platform_app()); | 952 EXPECT_TRUE(extension->is_platform_app()); |
| 943 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); | 953 std::vector<string16> warnings = |
| 954 PermissionsData::GetPermissionMessageStrings(extension); |
| 944 ASSERT_EQ(0u, warnings.size()); | 955 ASSERT_EQ(0u, warnings.size()); |
| 945 | 956 |
| 946 extension = LoadManifest("permissions", "platform_app_all_urls.json"); | 957 extension = LoadManifest("permissions", "platform_app_all_urls.json"); |
| 947 EXPECT_TRUE(extension->is_platform_app()); | 958 EXPECT_TRUE(extension->is_platform_app()); |
| 948 warnings = extension->GetPermissionMessageStrings(); | 959 warnings = PermissionsData::GetPermissionMessageStrings(extension); |
| 949 ASSERT_EQ(0u, warnings.size()); | 960 ASSERT_EQ(0u, warnings.size()); |
| 950 } | 961 } |
| 951 | 962 |
| 952 TEST(PermissionsTest, GetDistinctHostsForDisplay) { | 963 TEST(PermissionsTest, GetDistinctHostsForDisplay) { |
| 953 scoped_refptr<PermissionSet> perm_set; | 964 scoped_refptr<PermissionSet> perm_set; |
| 954 APIPermissionSet empty_perms; | 965 APIPermissionSet empty_perms; |
| 955 std::set<std::string> expected; | 966 std::set<std::string> expected; |
| 956 expected.insert("www.foo.com"); | 967 expected.insert("www.foo.com"); |
| 957 expected.insert("www.bar.com"); | 968 expected.insert("www.bar.com"); |
| 958 expected.insert("www.baz.com"); | 969 expected.insert("www.baz.com"); |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1332 EXPECT_EQ(4U, perm_set->apis().size()); | 1343 EXPECT_EQ(4U, perm_set->apis().size()); |
| 1333 } | 1344 } |
| 1334 | 1345 |
| 1335 TEST(PermissionsTest, SyncFileSystemPermission) { | 1346 TEST(PermissionsTest, SyncFileSystemPermission) { |
| 1336 scoped_refptr<Extension> extension = LoadManifest( | 1347 scoped_refptr<Extension> extension = LoadManifest( |
| 1337 "permissions", "sync_file_system.json"); | 1348 "permissions", "sync_file_system.json"); |
| 1338 APIPermissionSet apis; | 1349 APIPermissionSet apis; |
| 1339 apis.insert(APIPermission::kSyncFileSystem); | 1350 apis.insert(APIPermission::kSyncFileSystem); |
| 1340 EXPECT_TRUE(extension->is_platform_app()); | 1351 EXPECT_TRUE(extension->is_platform_app()); |
| 1341 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSyncFileSystem)); | 1352 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSyncFileSystem)); |
| 1342 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); | 1353 std::vector<string16> warnings = |
| 1354 PermissionsData::GetPermissionMessageStrings(extension); |
| 1343 EXPECT_TRUE(Contains(warnings, "Store data in your Google Drive account")); | 1355 EXPECT_TRUE(Contains(warnings, "Store data in your Google Drive account")); |
| 1344 ASSERT_EQ(1u, warnings.size()); | 1356 ASSERT_EQ(1u, warnings.size()); |
| 1345 } | 1357 } |
| 1346 | 1358 |
| 1347 } // namespace extensions | 1359 } // namespace extensions |
| OLD | NEW |