| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <vector> |
| 6 |
| 5 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 6 #include "base/file_util.h" | 8 #include "base/memory/ref_counted.h" |
| 7 #include "base/format_macros.h" | 9 #include "base/string16.h" |
| 8 #include "base/path_service.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/stringprintf.h" | |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "chrome/common/chrome_paths.h" | |
| 13 #include "chrome/common/chrome_switches.h" | 11 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/extensions/command.h" | 12 #include "chrome/common/chrome_version_info.h" |
| 15 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/extensions/extension_file_util.h" | |
| 17 #include "chrome/common/extensions/extension_manifest_constants.h" | 14 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 18 #include "chrome/common/extensions/extension_test_util.h" | 15 #include "chrome/common/extensions/extension_test_util.h" |
| 19 #include "chrome/common/extensions/features/feature.h" | 16 #include "chrome/common/extensions/features/feature.h" |
| 20 #include "chrome/common/extensions/manifest.h" | |
| 21 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" | |
| 22 #include "chrome/common/extensions/permissions/api_permission.h" | 17 #include "chrome/common/extensions/permissions/api_permission.h" |
| 23 #include "chrome/common/extensions/permissions/permission_set.h" | 18 #include "chrome/common/extensions/permissions/permission_set.h" |
| 19 #include "chrome/common/extensions/permissions/permissions_data.h" |
| 24 #include "chrome/common/extensions/permissions/socket_permission.h" | 20 #include "chrome/common/extensions/permissions/socket_permission.h" |
| 25 #include "chrome/common/extensions/permissions/usb_device_permission.h" | 21 #include "content/public/common/socket_permission_request.h" |
| 26 #include "chrome/common/url_constants.h" | |
| 27 #include "extensions/common/error_utils.h" | 22 #include "extensions/common/error_utils.h" |
| 28 #include "extensions/common/extension_resource.h" | |
| 29 #include "extensions/common/id_util.h" | 23 #include "extensions/common/id_util.h" |
| 30 #include "googleurl/src/gurl.h" | 24 #include "extensions/common/url_pattern_set.h" |
| 31 #include "net/base/mime_sniffer.h" | |
| 32 #include "net/dns/mock_host_resolver.h" | |
| 33 #include "skia/ext/image_operations.h" | |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 35 #include "third_party/skia/include/core/SkBitmap.h" | |
| 36 #include "ui/gfx/codec/png_codec.h" | |
| 37 | 26 |
| 38 using content::SocketPermissionRequest; | 27 using content::SocketPermissionRequest; |
| 39 using extension_test_util::LoadManifest; | 28 using extension_test_util::LoadManifest; |
| 40 using extension_test_util::LoadManifestUnchecked; | 29 using extension_test_util::LoadManifestUnchecked; |
| 41 using extension_test_util::LoadManifestStrict; | 30 using extension_test_util::LoadManifestStrict; |
| 42 | 31 |
| 43 namespace keys = extension_manifest_keys; | |
| 44 namespace values = extension_manifest_values; | |
| 45 namespace errors = extension_manifest_errors; | |
| 46 | |
| 47 namespace extensions { | 32 namespace extensions { |
| 48 | 33 |
| 49 // We persist location values in the preferences, so this is a sanity test that | 34 namespace { |
| 50 // someone doesn't accidentally change them. | 35 |
| 51 TEST(ExtensionTest, LocationValuesTest) { | 36 bool CheckSocketPermission( |
| 52 ASSERT_EQ(0, Manifest::INVALID_LOCATION); | 37 scoped_refptr<Extension> extension, |
| 53 ASSERT_EQ(1, Manifest::INTERNAL); | 38 SocketPermissionRequest::OperationType type, |
| 54 ASSERT_EQ(2, Manifest::EXTERNAL_PREF); | 39 const char* host, |
| 55 ASSERT_EQ(3, Manifest::EXTERNAL_REGISTRY); | 40 int port) { |
| 56 ASSERT_EQ(4, Manifest::UNPACKED); | 41 SocketPermission::CheckParam param(type, host, port); |
| 57 ASSERT_EQ(5, Manifest::COMPONENT); | 42 return PermissionsData::CheckAPIPermissionWithParam( |
| 58 ASSERT_EQ(6, Manifest::EXTERNAL_PREF_DOWNLOAD); | 43 extension, APIPermission::kSocket, ¶m); |
| 59 ASSERT_EQ(7, Manifest::EXTERNAL_POLICY_DOWNLOAD); | |
| 60 ASSERT_EQ(8, Manifest::COMMAND_LINE); | |
| 61 } | 44 } |
| 62 | 45 |
| 63 TEST(ExtensionTest, LocationPriorityTest) { | 46 } // namespace |
| 64 for (int i = 0; i < Manifest::NUM_LOCATIONS; i++) { | |
| 65 Manifest::Location loc = static_cast<Manifest::Location>(i); | |
| 66 | 47 |
| 67 // INVALID is not a valid location. | 48 TEST(ExtensionPermissionsTest, EffectiveHostPermissions) { |
| 68 if (loc == Manifest::INVALID_LOCATION) | |
| 69 continue; | |
| 70 | |
| 71 // Comparing a location that has no rank will hit a CHECK. Do a | |
| 72 // compare with every valid location, to be sure each one is covered. | |
| 73 | |
| 74 // Check that no install source can override a componenet extension. | |
| 75 ASSERT_EQ(Manifest::COMPONENT, | |
| 76 Manifest::GetHigherPriorityLocation(Manifest::COMPONENT, loc)); | |
| 77 ASSERT_EQ(Manifest::COMPONENT, | |
| 78 Manifest::GetHigherPriorityLocation(loc, Manifest::COMPONENT)); | |
| 79 | |
| 80 // Check that any source can override a user install. This might change | |
| 81 // in the future, in which case this test should be updated. | |
| 82 ASSERT_EQ(loc, | |
| 83 Manifest::GetHigherPriorityLocation(Manifest::INTERNAL, loc)); | |
| 84 ASSERT_EQ(loc, | |
| 85 Manifest::GetHigherPriorityLocation(loc, Manifest::INTERNAL)); | |
| 86 } | |
| 87 | |
| 88 // Check a few interesting cases that we know can happen: | |
| 89 ASSERT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, | |
| 90 Manifest::GetHigherPriorityLocation( | |
| 91 Manifest::EXTERNAL_POLICY_DOWNLOAD, | |
| 92 Manifest::EXTERNAL_PREF)); | |
| 93 | |
| 94 ASSERT_EQ(Manifest::EXTERNAL_PREF, | |
| 95 Manifest::GetHigherPriorityLocation( | |
| 96 Manifest::INTERNAL, | |
| 97 Manifest::EXTERNAL_PREF)); | |
| 98 } | |
| 99 | |
| 100 TEST(ExtensionTest, GetResourceURLAndPath) { | |
| 101 scoped_refptr<Extension> extension = LoadManifestStrict("empty_manifest", | |
| 102 "empty.json"); | |
| 103 EXPECT_TRUE(extension.get()); | |
| 104 | |
| 105 EXPECT_EQ(extension->url().spec() + "bar/baz.js", | |
| 106 Extension::GetResourceURL(extension->url(), "bar/baz.js").spec()); | |
| 107 EXPECT_EQ(extension->url().spec() + "baz.js", | |
| 108 Extension::GetResourceURL(extension->url(), | |
| 109 "bar/../baz.js").spec()); | |
| 110 EXPECT_EQ(extension->url().spec() + "baz.js", | |
| 111 Extension::GetResourceURL(extension->url(), "../baz.js").spec()); | |
| 112 | |
| 113 // Test that absolute-looking paths ("/"-prefixed) are pasted correctly. | |
| 114 EXPECT_EQ(extension->url().spec() + "test.html", | |
| 115 extension->GetResourceURL("/test.html").spec()); | |
| 116 } | |
| 117 | |
| 118 TEST(ExtensionTest, GetAbsolutePathNoError) { | |
| 119 scoped_refptr<Extension> extension = LoadManifestStrict("absolute_path", | |
| 120 "absolute.json"); | |
| 121 EXPECT_TRUE(extension.get()); | |
| 122 std::string err; | |
| 123 std::vector<InstallWarning> warnings; | |
| 124 EXPECT_TRUE(extension_file_util::ValidateExtension(extension.get(), | |
| 125 &err, &warnings)); | |
| 126 EXPECT_EQ(0U, warnings.size()); | |
| 127 | |
| 128 EXPECT_EQ(extension->path().AppendASCII("test.html").value(), | |
| 129 extension->GetResource("test.html").GetFilePath().value()); | |
| 130 EXPECT_EQ(extension->path().AppendASCII("test.js").value(), | |
| 131 extension->GetResource("test.js").GetFilePath().value()); | |
| 132 } | |
| 133 | |
| 134 | |
| 135 TEST(ExtensionTest, IdIsValid) { | |
| 136 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | |
| 137 EXPECT_TRUE(Extension::IdIsValid("pppppppppppppppppppppppppppppppp")); | |
| 138 EXPECT_TRUE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnop")); | |
| 139 EXPECT_TRUE(Extension::IdIsValid("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")); | |
| 140 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno")); | |
| 141 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnopa")); | |
| 142 EXPECT_FALSE(Extension::IdIsValid("0123456789abcdef0123456789abcdef")); | |
| 143 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnoq")); | |
| 144 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno0")); | |
| 145 } | |
| 146 | |
| 147 | |
| 148 // This test ensures that the mimetype sniffing code stays in sync with the | |
| 149 // actual crx files that we test other parts of the system with. | |
| 150 TEST(ExtensionTest, MimeTypeSniffing) { | |
| 151 base::FilePath path; | |
| 152 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | |
| 153 path = path.AppendASCII("extensions").AppendASCII("good.crx"); | |
| 154 | |
| 155 std::string data; | |
| 156 ASSERT_TRUE(file_util::ReadFileToString(path, &data)); | |
| 157 | |
| 158 std::string result; | |
| 159 EXPECT_TRUE(net::SniffMimeType(data.c_str(), | |
| 160 data.size(), | |
| 161 GURL("http://www.example.com/foo.crx"), | |
| 162 std::string(), | |
| 163 &result)); | |
| 164 EXPECT_EQ(std::string(Extension::kMimeType), result); | |
| 165 | |
| 166 data.clear(); | |
| 167 result.clear(); | |
| 168 path = path.DirName().AppendASCII("bad_magic.crx"); | |
| 169 ASSERT_TRUE(file_util::ReadFileToString(path, &data)); | |
| 170 EXPECT_TRUE(net::SniffMimeType(data.c_str(), | |
| 171 data.size(), | |
| 172 GURL("http://www.example.com/foo.crx"), | |
| 173 std::string(), | |
| 174 &result)); | |
| 175 EXPECT_EQ("application/octet-stream", result); | |
| 176 } | |
| 177 | |
| 178 TEST(ExtensionTest, EffectiveHostPermissions) { | |
| 179 scoped_refptr<Extension> extension; | 49 scoped_refptr<Extension> extension; |
| 180 URLPatternSet hosts; | 50 URLPatternSet hosts; |
| 181 | 51 |
| 182 extension = LoadManifest("effective_host_permissions", "empty.json"); | 52 extension = LoadManifest("effective_host_permissions", "empty.json"); |
| 183 EXPECT_EQ(0u, extension->GetEffectiveHostPermissions().patterns().size()); | 53 EXPECT_EQ( |
| 54 0u, |
| 55 PermissionsData::GetEffectiveHostPermissions( |
| 56 extension).patterns().size()); |
| 184 EXPECT_FALSE(hosts.MatchesURL(GURL("http://www.google.com"))); | 57 EXPECT_FALSE(hosts.MatchesURL(GURL("http://www.google.com"))); |
| 185 EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); | 58 EXPECT_FALSE(PermissionsData::HasEffectiveAccessToAllHosts(extension)); |
| 186 | 59 |
| 187 extension = LoadManifest("effective_host_permissions", "one_host.json"); | 60 extension = LoadManifest("effective_host_permissions", "one_host.json"); |
| 188 hosts = extension->GetEffectiveHostPermissions(); | 61 hosts = PermissionsData::GetEffectiveHostPermissions(extension); |
| 189 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); | 62 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); |
| 190 EXPECT_FALSE(hosts.MatchesURL(GURL("https://www.google.com"))); | 63 EXPECT_FALSE(hosts.MatchesURL(GURL("https://www.google.com"))); |
| 191 EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); | 64 EXPECT_FALSE(PermissionsData::HasEffectiveAccessToAllHosts(extension)); |
| 192 | 65 |
| 193 extension = LoadManifest("effective_host_permissions", | 66 extension = LoadManifest("effective_host_permissions", |
| 194 "one_host_wildcard.json"); | 67 "one_host_wildcard.json"); |
| 195 hosts = extension->GetEffectiveHostPermissions(); | 68 hosts = PermissionsData::GetEffectiveHostPermissions(extension); |
| 196 EXPECT_TRUE(hosts.MatchesURL(GURL("http://google.com"))); | 69 EXPECT_TRUE(hosts.MatchesURL(GURL("http://google.com"))); |
| 197 EXPECT_TRUE(hosts.MatchesURL(GURL("http://foo.google.com"))); | 70 EXPECT_TRUE(hosts.MatchesURL(GURL("http://foo.google.com"))); |
| 198 EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); | 71 EXPECT_FALSE(PermissionsData::HasEffectiveAccessToAllHosts(extension)); |
| 199 | 72 |
| 200 extension = LoadManifest("effective_host_permissions", "two_hosts.json"); | 73 extension = LoadManifest("effective_host_permissions", "two_hosts.json"); |
| 201 hosts = extension->GetEffectiveHostPermissions(); | 74 hosts = PermissionsData::GetEffectiveHostPermissions(extension); |
| 202 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); | 75 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); |
| 203 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.reddit.com"))); | 76 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.reddit.com"))); |
| 204 EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); | 77 EXPECT_FALSE(PermissionsData::HasEffectiveAccessToAllHosts(extension)); |
| 205 | 78 |
| 206 extension = LoadManifest("effective_host_permissions", | 79 extension = LoadManifest("effective_host_permissions", |
| 207 "https_not_considered.json"); | 80 "https_not_considered.json"); |
| 208 hosts = extension->GetEffectiveHostPermissions(); | 81 hosts = PermissionsData::GetEffectiveHostPermissions(extension); |
| 209 EXPECT_TRUE(hosts.MatchesURL(GURL("http://google.com"))); | 82 EXPECT_TRUE(hosts.MatchesURL(GURL("http://google.com"))); |
| 210 EXPECT_TRUE(hosts.MatchesURL(GURL("https://google.com"))); | 83 EXPECT_TRUE(hosts.MatchesURL(GURL("https://google.com"))); |
| 211 EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); | 84 EXPECT_FALSE(PermissionsData::HasEffectiveAccessToAllHosts(extension)); |
| 212 | 85 |
| 213 extension = LoadManifest("effective_host_permissions", | 86 extension = LoadManifest("effective_host_permissions", |
| 214 "two_content_scripts.json"); | 87 "two_content_scripts.json"); |
| 215 hosts = extension->GetEffectiveHostPermissions(); | 88 hosts = PermissionsData::GetEffectiveHostPermissions(extension); |
| 216 EXPECT_TRUE(hosts.MatchesURL(GURL("http://google.com"))); | 89 EXPECT_TRUE(hosts.MatchesURL(GURL("http://google.com"))); |
| 217 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.reddit.com"))); | 90 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.reddit.com"))); |
| 218 EXPECT_TRUE(extension->GetActivePermissions()->HasEffectiveAccessToURL( | 91 EXPECT_TRUE(extension->GetActivePermissions()->HasEffectiveAccessToURL( |
| 219 GURL("http://www.reddit.com"))); | 92 GURL("http://www.reddit.com"))); |
| 220 EXPECT_TRUE(hosts.MatchesURL(GURL("http://news.ycombinator.com"))); | 93 EXPECT_TRUE(hosts.MatchesURL(GURL("http://news.ycombinator.com"))); |
| 221 EXPECT_TRUE(extension->GetActivePermissions()->HasEffectiveAccessToURL( | 94 EXPECT_TRUE(extension->GetActivePermissions()->HasEffectiveAccessToURL( |
| 222 GURL("http://news.ycombinator.com"))); | 95 GURL("http://news.ycombinator.com"))); |
| 223 EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); | 96 EXPECT_FALSE(PermissionsData::HasEffectiveAccessToAllHosts(extension)); |
| 224 | 97 |
| 225 extension = LoadManifest("effective_host_permissions", "all_hosts.json"); | 98 extension = LoadManifest("effective_host_permissions", "all_hosts.json"); |
| 226 hosts = extension->GetEffectiveHostPermissions(); | 99 hosts = PermissionsData::GetEffectiveHostPermissions(extension); |
| 227 EXPECT_TRUE(hosts.MatchesURL(GURL("http://test/"))); | 100 EXPECT_TRUE(hosts.MatchesURL(GURL("http://test/"))); |
| 228 EXPECT_FALSE(hosts.MatchesURL(GURL("https://test/"))); | 101 EXPECT_FALSE(hosts.MatchesURL(GURL("https://test/"))); |
| 229 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); | 102 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); |
| 230 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); | 103 EXPECT_TRUE(PermissionsData::HasEffectiveAccessToAllHosts(extension)); |
| 231 | 104 |
| 232 extension = LoadManifest("effective_host_permissions", "all_hosts2.json"); | 105 extension = LoadManifest("effective_host_permissions", "all_hosts2.json"); |
| 233 hosts = extension->GetEffectiveHostPermissions(); | 106 hosts = PermissionsData::GetEffectiveHostPermissions(extension); |
| 234 EXPECT_TRUE(hosts.MatchesURL(GURL("http://test/"))); | 107 EXPECT_TRUE(hosts.MatchesURL(GURL("http://test/"))); |
| 235 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); | 108 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); |
| 236 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); | 109 EXPECT_TRUE(PermissionsData::HasEffectiveAccessToAllHosts(extension)); |
| 237 | 110 |
| 238 extension = LoadManifest("effective_host_permissions", "all_hosts3.json"); | 111 extension = LoadManifest("effective_host_permissions", "all_hosts3.json"); |
| 239 hosts = extension->GetEffectiveHostPermissions(); | 112 hosts = PermissionsData::GetEffectiveHostPermissions(extension); |
| 240 EXPECT_FALSE(hosts.MatchesURL(GURL("http://test/"))); | 113 EXPECT_FALSE(hosts.MatchesURL(GURL("http://test/"))); |
| 241 EXPECT_TRUE(hosts.MatchesURL(GURL("https://test/"))); | 114 EXPECT_TRUE(hosts.MatchesURL(GURL("https://test/"))); |
| 242 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); | 115 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); |
| 243 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); | 116 EXPECT_TRUE(PermissionsData::HasEffectiveAccessToAllHosts(extension)); |
| 244 } | 117 } |
| 245 | 118 |
| 246 static bool CheckSocketPermission(scoped_refptr<Extension> extension, | 119 TEST(ExtensionPermissionsTest, SocketPermissions) { |
| 247 SocketPermissionRequest::OperationType type, | |
| 248 const char* host, | |
| 249 int port) { | |
| 250 SocketPermission::CheckParam param(type, host, port); | |
| 251 return extension->CheckAPIPermissionWithParam( | |
| 252 APIPermission::kSocket, ¶m); | |
| 253 } | |
| 254 | |
| 255 TEST(ExtensionTest, SocketPermissions) { | |
| 256 // Set feature current channel to appropriate value. | 120 // Set feature current channel to appropriate value. |
| 257 Feature::ScopedCurrentChannel scoped_channel( | 121 Feature::ScopedCurrentChannel scoped_channel( |
| 258 chrome::VersionInfo::CHANNEL_DEV); | 122 chrome::VersionInfo::CHANNEL_DEV); |
| 259 scoped_refptr<Extension> extension; | 123 scoped_refptr<Extension> extension; |
| 260 std::string error; | 124 std::string error; |
| 261 | 125 |
| 262 extension = LoadManifest("socket_permissions", "empty.json"); | 126 extension = LoadManifest("socket_permissions", "empty.json"); |
| 263 EXPECT_FALSE(CheckSocketPermission(extension, | 127 EXPECT_FALSE(CheckSocketPermission(extension, |
| 264 SocketPermissionRequest::TCP_CONNECT, "www.example.com", 80)); | 128 SocketPermissionRequest::TCP_CONNECT, "www.example.com", 80)); |
| 265 | 129 |
| 266 extension = LoadManifestUnchecked("socket_permissions", | 130 extension = LoadManifestUnchecked("socket_permissions", |
| 267 "socket1.json", | 131 "socket1.json", |
| 268 Manifest::INTERNAL, Extension::NO_FLAGS, | 132 Manifest::INTERNAL, Extension::NO_FLAGS, |
| 269 &error); | 133 &error); |
| 270 EXPECT_TRUE(extension == NULL); | 134 EXPECT_TRUE(extension == NULL); |
| 271 ASSERT_EQ(ErrorUtils::FormatErrorMessage( | 135 ASSERT_EQ(ErrorUtils::FormatErrorMessage( |
| 272 errors::kInvalidPermission, "socket"), error); | 136 extension_manifest_errors::kInvalidPermission, "socket"), error); |
| 273 | 137 |
| 274 extension = LoadManifest("socket_permissions", "socket2.json"); | 138 extension = LoadManifest("socket_permissions", "socket2.json"); |
| 275 EXPECT_TRUE(CheckSocketPermission(extension, | 139 EXPECT_TRUE(CheckSocketPermission(extension, |
| 276 SocketPermissionRequest::TCP_CONNECT, "www.example.com", 80)); | 140 SocketPermissionRequest::TCP_CONNECT, "www.example.com", 80)); |
| 277 EXPECT_FALSE(CheckSocketPermission( | 141 EXPECT_FALSE(CheckSocketPermission( |
| 278 extension, SocketPermissionRequest::UDP_BIND, "", 80)); | 142 extension, SocketPermissionRequest::UDP_BIND, "", 80)); |
| 279 EXPECT_TRUE(CheckSocketPermission( | 143 EXPECT_TRUE(CheckSocketPermission( |
| 280 extension, SocketPermissionRequest::UDP_BIND, "", 8888)); | 144 extension, SocketPermissionRequest::UDP_BIND, "", 8888)); |
| 281 | 145 |
| 282 EXPECT_FALSE(CheckSocketPermission( | 146 EXPECT_FALSE(CheckSocketPermission( |
| 283 extension, SocketPermissionRequest::UDP_SEND_TO, "example.com", 1900)); | 147 extension, SocketPermissionRequest::UDP_SEND_TO, "example.com", 1900)); |
| 284 EXPECT_TRUE(CheckSocketPermission( | 148 EXPECT_TRUE(CheckSocketPermission( |
| 285 extension, | 149 extension, |
| 286 SocketPermissionRequest::UDP_SEND_TO, | 150 SocketPermissionRequest::UDP_SEND_TO, |
| 287 "239.255.255.250", 1900)); | 151 "239.255.255.250", 1900)); |
| 288 } | 152 } |
| 289 | 153 |
| 290 // This tests the API permissions with an empty manifest (one that just | 154 // This tests the API permissions with an empty manifest (one that just |
| 291 // specifies a name and a version and nothing else). | 155 // specifies a name and a version and nothing else). |
| 292 TEST(ExtensionTest, ApiPermissions) { | 156 TEST(ExtensionPermissionsTest, ApiPermissions) { |
| 293 const struct { | 157 const struct { |
| 294 const char* permission_name; | 158 const char* permission_name; |
| 295 bool expect_success; | 159 bool expect_success; |
| 296 } kTests[] = { | 160 } kTests[] = { |
| 297 // Negative test. | 161 // Negative test. |
| 298 { "non_existing_permission", false }, | 162 { "non_existing_permission", false }, |
| 299 // Test default module/package permission. | 163 // Test default module/package permission. |
| 300 { "browserAction", true }, | 164 { "browserAction", true }, |
| 301 { "devtools", true }, | 165 { "devtools", true }, |
| 302 { "extension", true }, | 166 { "extension", true }, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 327 scoped_refptr<Extension> extension; | 191 scoped_refptr<Extension> extension; |
| 328 extension = LoadManifest("empty_manifest", "empty.json"); | 192 extension = LoadManifest("empty_manifest", "empty.json"); |
| 329 | 193 |
| 330 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { | 194 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { |
| 331 EXPECT_EQ(kTests[i].expect_success, | 195 EXPECT_EQ(kTests[i].expect_success, |
| 332 extension->HasAPIPermission(kTests[i].permission_name)) | 196 extension->HasAPIPermission(kTests[i].permission_name)) |
| 333 << "Permission being tested: " << kTests[i].permission_name; | 197 << "Permission being tested: " << kTests[i].permission_name; |
| 334 } | 198 } |
| 335 } | 199 } |
| 336 | 200 |
| 337 TEST(ExtensionTest, GetPermissionMessages_ManyApiPermissions) { | 201 TEST(ExtensionPermissionsTest, GetPermissionMessages_ManyAPIPermissions) { |
| 338 scoped_refptr<Extension> extension; | 202 scoped_refptr<Extension> extension; |
| 339 extension = LoadManifest("permissions", "many-apis.json"); | 203 extension = LoadManifest("permissions", "many-apis.json"); |
| 340 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); | 204 std::vector<string16> warnings = |
| 205 PermissionsData::GetPermissionMessageStrings(extension); |
| 341 ASSERT_EQ(6u, warnings.size()); | 206 ASSERT_EQ(6u, warnings.size()); |
| 342 EXPECT_EQ("Access your data on api.flickr.com", | 207 EXPECT_EQ("Access your data on api.flickr.com", |
| 343 UTF16ToUTF8(warnings[0])); | 208 UTF16ToUTF8(warnings[0])); |
| 344 EXPECT_EQ("Read and modify your bookmarks", UTF16ToUTF8(warnings[1])); | 209 EXPECT_EQ("Read and modify your bookmarks", UTF16ToUTF8(warnings[1])); |
| 345 EXPECT_EQ("Detect your physical location", UTF16ToUTF8(warnings[2])); | 210 EXPECT_EQ("Detect your physical location", UTF16ToUTF8(warnings[2])); |
| 346 EXPECT_EQ("Read and modify your browsing history", UTF16ToUTF8(warnings[3])); | 211 EXPECT_EQ("Read and modify your browsing history", UTF16ToUTF8(warnings[3])); |
| 347 EXPECT_EQ("Access your tabs and browsing activity", UTF16ToUTF8(warnings[4])); | 212 EXPECT_EQ("Access your tabs and browsing activity", UTF16ToUTF8(warnings[4])); |
| 348 EXPECT_EQ("Manage your apps, extensions, and themes", | 213 EXPECT_EQ("Manage your apps, extensions, and themes", |
| 349 UTF16ToUTF8(warnings[5])); | 214 UTF16ToUTF8(warnings[5])); |
| 350 } | 215 } |
| 351 | 216 |
| 352 TEST(ExtensionTest, GetPermissionMessages_LocationApiPermission) { | 217 TEST(ExtensionPermissionsTest, GetPermissionMessages_LocationApiPermission) { |
| 353 scoped_refptr<Extension> extension; | 218 scoped_refptr<Extension> extension; |
| 354 extension = LoadManifest("permissions", | 219 extension = LoadManifest("permissions", |
| 355 "location-api.json", | 220 "location-api.json", |
| 356 Manifest::COMPONENT, | 221 Manifest::COMPONENT, |
| 357 Extension::NO_FLAGS); | 222 Extension::NO_FLAGS); |
| 358 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); | 223 std::vector<string16> warnings = |
| 224 PermissionsData::GetPermissionMessageStrings(extension); |
| 359 ASSERT_EQ(1u, warnings.size()); | 225 ASSERT_EQ(1u, warnings.size()); |
| 360 EXPECT_EQ("Detect your physical location", UTF16ToUTF8(warnings[0])); | 226 EXPECT_EQ("Detect your physical location", UTF16ToUTF8(warnings[0])); |
| 361 } | 227 } |
| 362 | 228 |
| 363 TEST(ExtensionTest, GetPermissionMessages_ManyHosts) { | 229 TEST(ExtensionPermissionsTest, GetPermissionMessages_ManyHosts) { |
| 364 scoped_refptr<Extension> extension; | 230 scoped_refptr<Extension> extension; |
| 365 extension = LoadManifest("permissions", "many-hosts.json"); | 231 extension = LoadManifest("permissions", "many-hosts.json"); |
| 366 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); | 232 std::vector<string16> warnings = |
| 233 PermissionsData::GetPermissionMessageStrings(extension); |
| 367 ASSERT_EQ(1u, warnings.size()); | 234 ASSERT_EQ(1u, warnings.size()); |
| 368 EXPECT_EQ("Access your data on encrypted.google.com and www.google.com", | 235 EXPECT_EQ("Access your data on encrypted.google.com and www.google.com", |
| 369 UTF16ToUTF8(warnings[0])); | 236 UTF16ToUTF8(warnings[0])); |
| 370 } | 237 } |
| 371 | 238 |
| 372 TEST(ExtensionTest, GetPermissionMessages_Plugins) { | 239 TEST(ExtensionPermissionsTest, GetPermissionMessages_Plugins) { |
| 373 scoped_refptr<Extension> extension; | 240 scoped_refptr<Extension> extension; |
| 374 extension = LoadManifest("permissions", "plugins.json"); | 241 extension = LoadManifest("permissions", "plugins.json"); |
| 375 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); | 242 std::vector<string16> warnings = |
| 243 PermissionsData::GetPermissionMessageStrings(extension); |
| 376 // We don't parse the plugins key on Chrome OS, so it should not ask for any | 244 // We don't parse the plugins key on Chrome OS, so it should not ask for any |
| 377 // permissions. | 245 // permissions. |
| 378 #if defined(OS_CHROMEOS) | 246 #if defined(OS_CHROMEOS) |
| 379 ASSERT_EQ(0u, warnings.size()); | 247 ASSERT_EQ(0u, warnings.size()); |
| 380 #else | 248 #else |
| 381 ASSERT_EQ(1u, warnings.size()); | 249 ASSERT_EQ(1u, warnings.size()); |
| 382 EXPECT_EQ("Access all data on your computer and the websites you visit", | 250 EXPECT_EQ("Access all data on your computer and the websites you visit", |
| 383 UTF16ToUTF8(warnings[0])); | 251 UTF16ToUTF8(warnings[0])); |
| 384 #endif | 252 #endif |
| 385 } | 253 } |
| 386 | 254 |
| 387 TEST(ExtensionTest, WantsFileAccess) { | 255 TEST(ExtensionPermissionsTest, OptionalOnlyPermission) { |
| 256 // Set feature current channel to dev because the only permission that must |
| 257 // be optional (usbDevices) is only available on dev channel. |
| 258 Feature::ScopedCurrentChannel scoped_channel( |
| 259 chrome::VersionInfo::CHANNEL_DEV); |
| 260 |
| 388 scoped_refptr<Extension> extension; | 261 scoped_refptr<Extension> extension; |
| 389 GURL file_url("file:///etc/passwd"); | 262 std::string error; |
| 263 extension = LoadManifestUnchecked("optional_only_permission", |
| 264 "manifest1.json", |
| 265 Manifest::INTERNAL, Extension::NO_FLAGS, |
| 266 &error); |
| 267 EXPECT_TRUE(extension == NULL); |
| 268 ASSERT_EQ( |
| 269 ErrorUtils::FormatErrorMessage( |
| 270 extension_manifest_errors::kPermissionMustBeOptional, |
| 271 "usbDevices"), |
| 272 error); |
| 390 | 273 |
| 391 // <all_urls> permission | 274 error.clear(); |
| 392 extension = LoadManifest("permissions", "permissions_all_urls.json"); | 275 extension = LoadManifestUnchecked("optional_only_permission", |
| 393 EXPECT_TRUE(extension->wants_file_access()); | 276 "manifest2.json", |
| 394 EXPECT_FALSE(extension->CanExecuteScriptOnPage( | 277 Manifest::INTERNAL, Extension::NO_FLAGS, |
| 395 file_url, file_url, -1, NULL, NULL)); | 278 &error); |
| 396 extension = LoadManifest( | 279 EXPECT_TRUE(extension != NULL); |
| 397 "permissions", "permissions_all_urls.json", Extension::ALLOW_FILE_ACCESS); | 280 EXPECT_TRUE(error.empty()); |
| 398 EXPECT_TRUE(extension->wants_file_access()); | |
| 399 EXPECT_TRUE(extension->CanExecuteScriptOnPage( | |
| 400 file_url, file_url, -1, NULL, NULL)); | |
| 401 | |
| 402 // file:///* permission | |
| 403 extension = LoadManifest("permissions", "permissions_file_scheme.json"); | |
| 404 EXPECT_TRUE(extension->wants_file_access()); | |
| 405 EXPECT_FALSE(extension->CanExecuteScriptOnPage( | |
| 406 file_url, file_url, -1, NULL, NULL)); | |
| 407 extension = LoadManifest("permissions", "permissions_file_scheme.json", | |
| 408 Extension::ALLOW_FILE_ACCESS); | |
| 409 EXPECT_TRUE(extension->wants_file_access()); | |
| 410 EXPECT_TRUE(extension->CanExecuteScriptOnPage( | |
| 411 file_url, file_url, -1, NULL, NULL)); | |
| 412 | |
| 413 // http://* permission | |
| 414 extension = LoadManifest("permissions", "permissions_http_scheme.json"); | |
| 415 EXPECT_FALSE(extension->wants_file_access()); | |
| 416 EXPECT_FALSE(extension->CanExecuteScriptOnPage( | |
| 417 file_url, file_url, -1, NULL, NULL)); | |
| 418 extension = LoadManifest("permissions", "permissions_http_scheme.json", | |
| 419 Extension::ALLOW_FILE_ACCESS); | |
| 420 EXPECT_FALSE(extension->wants_file_access()); | |
| 421 EXPECT_FALSE(extension->CanExecuteScriptOnPage( | |
| 422 file_url, file_url, -1, NULL, NULL)); | |
| 423 | |
| 424 // <all_urls> content script match | |
| 425 extension = LoadManifest("permissions", "content_script_all_urls.json"); | |
| 426 EXPECT_TRUE(extension->wants_file_access()); | |
| 427 EXPECT_FALSE(extension->CanExecuteScriptOnPage( | |
| 428 file_url, | |
| 429 file_url, | |
| 430 -1, | |
| 431 &ContentScriptsInfo::GetContentScripts(extension)[0], | |
| 432 NULL)); | |
| 433 extension = LoadManifest("permissions", "content_script_all_urls.json", | |
| 434 Extension::ALLOW_FILE_ACCESS); | |
| 435 EXPECT_TRUE(extension->wants_file_access()); | |
| 436 EXPECT_TRUE(extension->CanExecuteScriptOnPage( | |
| 437 file_url, | |
| 438 file_url, | |
| 439 -1, | |
| 440 &ContentScriptsInfo::GetContentScripts(extension)[0], | |
| 441 NULL)); | |
| 442 | |
| 443 // file:///* content script match | |
| 444 extension = LoadManifest("permissions", "content_script_file_scheme.json"); | |
| 445 EXPECT_TRUE(extension->wants_file_access()); | |
| 446 EXPECT_FALSE(extension->CanExecuteScriptOnPage( | |
| 447 file_url, | |
| 448 file_url, | |
| 449 -1, | |
| 450 &ContentScriptsInfo::GetContentScripts(extension)[0], | |
| 451 NULL)); | |
| 452 extension = LoadManifest("permissions", "content_script_file_scheme.json", | |
| 453 Extension::ALLOW_FILE_ACCESS); | |
| 454 EXPECT_TRUE(extension->wants_file_access()); | |
| 455 EXPECT_TRUE(extension->CanExecuteScriptOnPage( | |
| 456 file_url, | |
| 457 file_url, | |
| 458 -1, | |
| 459 &ContentScriptsInfo::GetContentScripts(extension)[0], | |
| 460 NULL)); | |
| 461 | |
| 462 // http://* content script match | |
| 463 extension = LoadManifest("permissions", "content_script_http_scheme.json"); | |
| 464 EXPECT_FALSE(extension->wants_file_access()); | |
| 465 EXPECT_FALSE(extension->CanExecuteScriptOnPage( | |
| 466 file_url, | |
| 467 file_url, | |
| 468 -1, | |
| 469 &ContentScriptsInfo::GetContentScripts(extension)[0], | |
| 470 NULL)); | |
| 471 extension = LoadManifest("permissions", "content_script_http_scheme.json", | |
| 472 Extension::ALLOW_FILE_ACCESS); | |
| 473 EXPECT_FALSE(extension->wants_file_access()); | |
| 474 EXPECT_FALSE(extension->CanExecuteScriptOnPage( | |
| 475 file_url, | |
| 476 file_url, | |
| 477 -1, | |
| 478 &ContentScriptsInfo::GetContentScripts(extension)[0], | |
| 479 NULL)); | |
| 480 } | |
| 481 | |
| 482 TEST(ExtensionTest, ExtraFlags) { | |
| 483 scoped_refptr<Extension> extension; | |
| 484 extension = LoadManifest("app", "manifest.json", Extension::FROM_WEBSTORE); | |
| 485 EXPECT_TRUE(extension->from_webstore()); | |
| 486 | |
| 487 extension = LoadManifest("app", "manifest.json", Extension::FROM_BOOKMARK); | |
| 488 EXPECT_TRUE(extension->from_bookmark()); | |
| 489 | |
| 490 extension = LoadManifest("app", "manifest.json", Extension::NO_FLAGS); | |
| 491 EXPECT_FALSE(extension->from_bookmark()); | |
| 492 EXPECT_FALSE(extension->from_webstore()); | |
| 493 } | 281 } |
| 494 | 282 |
| 495 // Base class for testing the CanExecuteScriptOnPage and CanCaptureVisiblePage | 283 // Base class for testing the CanExecuteScriptOnPage and CanCaptureVisiblePage |
| 496 // methods of Extension for extensions with various permissions. | 284 // methods of Extension for extensions with various permissions. |
| 497 class ExtensionScriptAndCaptureVisibleTest : public testing::Test { | 285 class ExtensionScriptAndCaptureVisibleTest : public testing::Test { |
| 498 protected: | 286 protected: |
| 499 ExtensionScriptAndCaptureVisibleTest() | 287 ExtensionScriptAndCaptureVisibleTest() |
| 500 : http_url("http://www.google.com"), | 288 : http_url("http://www.google.com"), |
| 501 http_url_with_path("http://www.google.com/index.html"), | 289 http_url_with_path("http://www.google.com/index.html"), |
| 502 https_url("https://www.google.com"), | 290 https_url("https://www.google.com"), |
| 503 file_url("file:///foo/bar"), | 291 file_url("file:///foo/bar"), |
| 504 favicon_url("chrome://favicon/http://www.google.com"), | 292 favicon_url("chrome://favicon/http://www.google.com"), |
| 505 extension_url("chrome-extension://" + | 293 extension_url("chrome-extension://" + |
| 506 id_util::GenerateIdForPath( | 294 id_util::GenerateIdForPath( |
| 507 base::FilePath(FILE_PATH_LITERAL("foo")))), | 295 base::FilePath(FILE_PATH_LITERAL("foo")))), |
| 508 settings_url("chrome://settings"), | 296 settings_url("chrome://settings"), |
| 509 about_url("about:flags") { | 297 about_url("about:flags") { |
| 510 urls_.insert(http_url); | 298 urls_.insert(http_url); |
| 511 urls_.insert(http_url_with_path); | 299 urls_.insert(http_url_with_path); |
| 512 urls_.insert(https_url); | 300 urls_.insert(https_url); |
| 513 urls_.insert(file_url); | 301 urls_.insert(file_url); |
| 514 urls_.insert(favicon_url); | 302 urls_.insert(favicon_url); |
| 515 urls_.insert(extension_url); | 303 urls_.insert(extension_url); |
| 516 urls_.insert(settings_url); | 304 urls_.insert(settings_url); |
| 517 urls_.insert(about_url); | 305 urls_.insert(about_url); |
| 518 } | 306 } |
| 519 | 307 |
| 520 bool AllowedScript(const Extension* extension, const GURL& url, | 308 bool AllowedScript(const Extension* extension, const GURL& url, |
| 521 const GURL& top_url) { | 309 const GURL& top_url) { |
| 522 return extension->CanExecuteScriptOnPage(url, top_url, -1, NULL, NULL); | 310 return PermissionsData::CanExecuteScriptOnPage( |
| 311 extension, url, top_url, -1, NULL, NULL); |
| 523 } | 312 } |
| 524 | 313 |
| 525 bool BlockedScript(const Extension* extension, const GURL& url, | 314 bool BlockedScript(const Extension* extension, const GURL& url, |
| 526 const GURL& top_url) { | 315 const GURL& top_url) { |
| 527 return !extension->CanExecuteScriptOnPage(url, top_url, -1, NULL, NULL); | 316 return !PermissionsData::CanExecuteScriptOnPage( |
| 317 extension, url, top_url, -1, NULL, NULL); |
| 528 } | 318 } |
| 529 | 319 |
| 530 bool Allowed(const Extension* extension, const GURL& url) { | 320 bool Allowed(const Extension* extension, const GURL& url) { |
| 531 return Allowed(extension, url, -1); | 321 return Allowed(extension, url, -1); |
| 532 } | 322 } |
| 533 | 323 |
| 534 bool Allowed(const Extension* extension, const GURL& url, int tab_id) { | 324 bool Allowed(const Extension* extension, const GURL& url, int tab_id) { |
| 535 return (extension->CanExecuteScriptOnPage(url, url, tab_id, NULL, NULL) && | 325 return (PermissionsData::CanExecuteScriptOnPage( |
| 536 extension->CanCaptureVisiblePage(url, tab_id, NULL)); | 326 extension, url, url, tab_id, NULL, NULL) && |
| 327 PermissionsData::CanCaptureVisiblePage( |
| 328 extension, url, tab_id, NULL)); |
| 537 } | 329 } |
| 538 | 330 |
| 539 bool CaptureOnly(const Extension* extension, const GURL& url) { | 331 bool CaptureOnly(const Extension* extension, const GURL& url) { |
| 540 return CaptureOnly(extension, url, -1); | 332 return CaptureOnly(extension, url, -1); |
| 541 } | 333 } |
| 542 | 334 |
| 543 bool CaptureOnly(const Extension* extension, const GURL& url, int tab_id) { | 335 bool CaptureOnly(const Extension* extension, const GURL& url, int tab_id) { |
| 544 return !extension->CanExecuteScriptOnPage(url, url, tab_id, NULL, NULL) && | 336 return !PermissionsData::CanExecuteScriptOnPage( |
| 545 extension->CanCaptureVisiblePage(url, tab_id, NULL); | 337 extension, url, url, tab_id, NULL, NULL) && |
| 338 PermissionsData::CanCaptureVisiblePage(extension, url, tab_id, NULL); |
| 546 } | 339 } |
| 547 | 340 |
| 548 bool Blocked(const Extension* extension, const GURL& url) { | 341 bool Blocked(const Extension* extension, const GURL& url) { |
| 549 return Blocked(extension, url, -1); | 342 return Blocked(extension, url, -1); |
| 550 } | 343 } |
| 551 | 344 |
| 552 bool Blocked(const Extension* extension, const GURL& url, int tab_id) { | 345 bool Blocked(const Extension* extension, const GURL& url, int tab_id) { |
| 553 return !(extension->CanExecuteScriptOnPage(url, url, tab_id, NULL, NULL) || | 346 return !(PermissionsData::CanExecuteScriptOnPage( |
| 554 extension->CanCaptureVisiblePage(url, tab_id, NULL)); | 347 extension, url, url, tab_id, NULL, NULL) || |
| 348 PermissionsData::CanCaptureVisiblePage( |
| 349 extension, url, tab_id, NULL)); |
| 555 } | 350 } |
| 556 | 351 |
| 557 bool AllowedExclusivelyOnTab( | 352 bool AllowedExclusivelyOnTab( |
| 558 const Extension* extension, | 353 const Extension* extension, |
| 559 const std::set<GURL>& allowed_urls, | 354 const std::set<GURL>& allowed_urls, |
| 560 int tab_id) { | 355 int tab_id) { |
| 561 bool result = true; | 356 bool result = true; |
| 562 for (std::set<GURL>::iterator it = urls_.begin(); it != urls_.end(); ++it) { | 357 for (std::set<GURL>::iterator it = urls_.begin(); it != urls_.end(); ++it) { |
| 563 const GURL& url = *it; | 358 const GURL& url = *it; |
| 564 if (allowed_urls.count(url)) | 359 if (allowed_urls.count(url)) |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 | 400 |
| 606 // Test access to iframed content. | 401 // Test access to iframed content. |
| 607 GURL within_extension_url = extension->GetResourceURL("page.html"); | 402 GURL within_extension_url = extension->GetResourceURL("page.html"); |
| 608 EXPECT_TRUE(AllowedScript(extension, http_url, http_url_with_path)); | 403 EXPECT_TRUE(AllowedScript(extension, http_url, http_url_with_path)); |
| 609 EXPECT_TRUE(AllowedScript(extension, https_url, http_url_with_path)); | 404 EXPECT_TRUE(AllowedScript(extension, https_url, http_url_with_path)); |
| 610 EXPECT_TRUE(AllowedScript(extension, http_url, within_extension_url)); | 405 EXPECT_TRUE(AllowedScript(extension, http_url, within_extension_url)); |
| 611 EXPECT_TRUE(AllowedScript(extension, https_url, within_extension_url)); | 406 EXPECT_TRUE(AllowedScript(extension, https_url, within_extension_url)); |
| 612 EXPECT_TRUE(BlockedScript(extension, http_url, extension_url)); | 407 EXPECT_TRUE(BlockedScript(extension, http_url, extension_url)); |
| 613 EXPECT_TRUE(BlockedScript(extension, https_url, extension_url)); | 408 EXPECT_TRUE(BlockedScript(extension, https_url, extension_url)); |
| 614 | 409 |
| 615 EXPECT_FALSE(extension->HasHostPermission(settings_url)); | 410 EXPECT_FALSE(PermissionsData::HasHostPermission(extension, settings_url)); |
| 616 EXPECT_FALSE(extension->HasHostPermission(about_url)); | 411 EXPECT_FALSE(PermissionsData::HasHostPermission(extension, about_url)); |
| 617 EXPECT_TRUE(extension->HasHostPermission(favicon_url)); | 412 EXPECT_TRUE(PermissionsData::HasHostPermission(extension, favicon_url)); |
| 618 | 413 |
| 619 // Test * for scheme, which implies just the http/https schemes. | 414 // Test * for scheme, which implies just the http/https schemes. |
| 620 extension = LoadManifestStrict("script_and_capture", | 415 extension = LoadManifestStrict("script_and_capture", |
| 621 "extension_wildcard.json"); | 416 "extension_wildcard.json"); |
| 622 EXPECT_TRUE(Allowed(extension, http_url)); | 417 EXPECT_TRUE(Allowed(extension, http_url)); |
| 623 EXPECT_TRUE(Allowed(extension, https_url)); | 418 EXPECT_TRUE(Allowed(extension, https_url)); |
| 624 EXPECT_TRUE(Blocked(extension, settings_url)); | 419 EXPECT_TRUE(Blocked(extension, settings_url)); |
| 625 EXPECT_TRUE(Blocked(extension, about_url)); | 420 EXPECT_TRUE(Blocked(extension, about_url)); |
| 626 EXPECT_TRUE(Blocked(extension, file_url)); | 421 EXPECT_TRUE(Blocked(extension, file_url)); |
| 627 EXPECT_TRUE(Blocked(extension, favicon_url)); | 422 EXPECT_TRUE(Blocked(extension, favicon_url)); |
| 628 extension = LoadManifest("script_and_capture", | 423 extension = LoadManifest("script_and_capture", |
| 629 "extension_wildcard_settings.json"); | 424 "extension_wildcard_settings.json"); |
| 630 EXPECT_TRUE(Blocked(extension, settings_url)); | 425 EXPECT_TRUE(Blocked(extension, settings_url)); |
| 631 | 426 |
| 632 // Having chrome://*/ should not work for regular extensions. Note that | 427 // Having chrome://*/ should not work for regular extensions. Note that |
| 633 // for favicon access, we require the explicit pattern chrome://favicon/*. | 428 // for favicon access, we require the explicit pattern chrome://favicon/*. |
| 634 std::string error; | 429 std::string error; |
| 635 extension = LoadManifestUnchecked("script_and_capture", | 430 extension = LoadManifestUnchecked("script_and_capture", |
| 636 "extension_wildcard_chrome.json", | 431 "extension_wildcard_chrome.json", |
| 637 Manifest::INTERNAL, Extension::NO_FLAGS, | 432 Manifest::INTERNAL, Extension::NO_FLAGS, |
| 638 &error); | 433 &error); |
| 639 EXPECT_TRUE(extension == NULL); | 434 EXPECT_TRUE(extension == NULL); |
| 640 EXPECT_EQ(ErrorUtils::FormatErrorMessage( | 435 EXPECT_EQ( |
| 641 errors::kInvalidPermissionScheme, "chrome://*/"), error); | 436 ErrorUtils::FormatErrorMessage( |
| 437 extension_manifest_errors::kInvalidPermissionScheme, |
| 438 "chrome://*/"), |
| 439 error); |
| 642 | 440 |
| 643 // Having chrome://favicon/* should not give you chrome://* | 441 // Having chrome://favicon/* should not give you chrome://* |
| 644 extension = LoadManifestStrict("script_and_capture", | 442 extension = LoadManifestStrict("script_and_capture", |
| 645 "extension_chrome_favicon_wildcard.json"); | 443 "extension_chrome_favicon_wildcard.json"); |
| 646 EXPECT_TRUE(Blocked(extension, settings_url)); | 444 EXPECT_TRUE(Blocked(extension, settings_url)); |
| 647 EXPECT_TRUE(CaptureOnly(extension, favicon_url)); | 445 EXPECT_TRUE(CaptureOnly(extension, favicon_url)); |
| 648 EXPECT_TRUE(Blocked(extension, about_url)); | 446 EXPECT_TRUE(Blocked(extension, about_url)); |
| 649 EXPECT_TRUE(extension->HasHostPermission(favicon_url)); | 447 EXPECT_TRUE(PermissionsData::HasHostPermission(extension, favicon_url)); |
| 650 | 448 |
| 651 // Having http://favicon should not give you chrome://favicon | 449 // Having http://favicon should not give you chrome://favicon |
| 652 extension = LoadManifestStrict("script_and_capture", | 450 extension = LoadManifestStrict("script_and_capture", |
| 653 "extension_http_favicon.json"); | 451 "extension_http_favicon.json"); |
| 654 EXPECT_TRUE(Blocked(extension, settings_url)); | 452 EXPECT_TRUE(Blocked(extension, settings_url)); |
| 655 EXPECT_TRUE(Blocked(extension, favicon_url)); | 453 EXPECT_TRUE(Blocked(extension, favicon_url)); |
| 656 | 454 |
| 657 // Component extensions with <all_urls> should get everything. | 455 // Component extensions with <all_urls> should get everything. |
| 658 extension = LoadManifest("script_and_capture", "extension_component_all.json", | 456 extension = LoadManifest("script_and_capture", "extension_component_all.json", |
| 659 Manifest::COMPONENT, Extension::NO_FLAGS); | 457 Manifest::COMPONENT, Extension::NO_FLAGS); |
| 660 EXPECT_TRUE(Allowed(extension, http_url)); | 458 EXPECT_TRUE(Allowed(extension, http_url)); |
| 661 EXPECT_TRUE(Allowed(extension, https_url)); | 459 EXPECT_TRUE(Allowed(extension, https_url)); |
| 662 EXPECT_TRUE(Allowed(extension, settings_url)); | 460 EXPECT_TRUE(Allowed(extension, settings_url)); |
| 663 EXPECT_TRUE(Allowed(extension, about_url)); | 461 EXPECT_TRUE(Allowed(extension, about_url)); |
| 664 EXPECT_TRUE(Allowed(extension, favicon_url)); | 462 EXPECT_TRUE(Allowed(extension, favicon_url)); |
| 665 EXPECT_TRUE(extension->HasHostPermission(favicon_url)); | 463 EXPECT_TRUE(PermissionsData::HasHostPermission(extension, favicon_url)); |
| 666 | 464 |
| 667 // Component extensions should only get access to what they ask for. | 465 // Component extensions should only get access to what they ask for. |
| 668 extension = LoadManifest("script_and_capture", | 466 extension = LoadManifest("script_and_capture", |
| 669 "extension_component_google.json", Manifest::COMPONENT, | 467 "extension_component_google.json", Manifest::COMPONENT, |
| 670 Extension::NO_FLAGS); | 468 Extension::NO_FLAGS); |
| 671 EXPECT_TRUE(Allowed(extension, http_url)); | 469 EXPECT_TRUE(Allowed(extension, http_url)); |
| 672 EXPECT_TRUE(Blocked(extension, https_url)); | 470 EXPECT_TRUE(Blocked(extension, https_url)); |
| 673 EXPECT_TRUE(Blocked(extension, file_url)); | 471 EXPECT_TRUE(Blocked(extension, file_url)); |
| 674 EXPECT_TRUE(Blocked(extension, settings_url)); | 472 EXPECT_TRUE(Blocked(extension, settings_url)); |
| 675 EXPECT_TRUE(Blocked(extension, favicon_url)); | 473 EXPECT_TRUE(Blocked(extension, favicon_url)); |
| 676 EXPECT_TRUE(Blocked(extension, about_url)); | 474 EXPECT_TRUE(Blocked(extension, about_url)); |
| 677 EXPECT_TRUE(Blocked(extension, extension_url)); | 475 EXPECT_TRUE(Blocked(extension, extension_url)); |
| 678 EXPECT_FALSE(extension->HasHostPermission(settings_url)); | 476 EXPECT_FALSE(PermissionsData::HasHostPermission(extension, settings_url)); |
| 679 } | 477 } |
| 680 | 478 |
| 681 | 479 |
| 682 TEST_F(ExtensionScriptAndCaptureVisibleTest, PermissionsWithChromeURLsEnabled) { | 480 TEST_F(ExtensionScriptAndCaptureVisibleTest, PermissionsWithChromeURLsEnabled) { |
| 683 CommandLine::ForCurrentProcess()->AppendSwitch( | 481 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 684 switches::kExtensionsOnChromeURLs); | 482 switches::kExtensionsOnChromeURLs); |
| 685 | 483 |
| 686 scoped_refptr<Extension> extension; | 484 scoped_refptr<Extension> extension; |
| 687 | 485 |
| 688 // Test <all_urls> for regular extensions. | 486 // Test <all_urls> for regular extensions. |
| 689 extension = LoadManifestStrict("script_and_capture", | 487 extension = LoadManifestStrict("script_and_capture", |
| 690 "extension_regular_all.json"); | 488 "extension_regular_all.json"); |
| 691 EXPECT_TRUE(Allowed(extension, http_url)); | 489 EXPECT_TRUE(Allowed(extension, http_url)); |
| 692 EXPECT_TRUE(Allowed(extension, https_url)); | 490 EXPECT_TRUE(Allowed(extension, https_url)); |
| 693 EXPECT_TRUE(Blocked(extension, file_url)); | 491 EXPECT_TRUE(Blocked(extension, file_url)); |
| 694 EXPECT_TRUE(Blocked(extension, settings_url)); | 492 EXPECT_TRUE(Blocked(extension, settings_url)); |
| 695 EXPECT_TRUE(Allowed(extension, favicon_url)); // chrome:// requested | 493 EXPECT_TRUE(Allowed(extension, favicon_url)); // chrome:// requested |
| 696 EXPECT_TRUE(Blocked(extension, about_url)); | 494 EXPECT_TRUE(Blocked(extension, about_url)); |
| 697 EXPECT_TRUE(Blocked(extension, extension_url)); | 495 EXPECT_TRUE(Blocked(extension, extension_url)); |
| 698 | 496 |
| 699 // Test access to iframed content. | 497 // Test access to iframed content. |
| 700 GURL within_extension_url = extension->GetResourceURL("page.html"); | 498 GURL within_extension_url = extension->GetResourceURL("page.html"); |
| 701 EXPECT_TRUE(AllowedScript(extension, http_url, http_url_with_path)); | 499 EXPECT_TRUE(AllowedScript(extension, http_url, http_url_with_path)); |
| 702 EXPECT_TRUE(AllowedScript(extension, https_url, http_url_with_path)); | 500 EXPECT_TRUE(AllowedScript(extension, https_url, http_url_with_path)); |
| 703 EXPECT_TRUE(AllowedScript(extension, http_url, within_extension_url)); | 501 EXPECT_TRUE(AllowedScript(extension, http_url, within_extension_url)); |
| 704 EXPECT_TRUE(AllowedScript(extension, https_url, within_extension_url)); | 502 EXPECT_TRUE(AllowedScript(extension, https_url, within_extension_url)); |
| 705 EXPECT_TRUE(BlockedScript(extension, http_url, extension_url)); | 503 EXPECT_TRUE(BlockedScript(extension, http_url, extension_url)); |
| 706 EXPECT_TRUE(BlockedScript(extension, https_url, extension_url)); | 504 EXPECT_TRUE(BlockedScript(extension, https_url, extension_url)); |
| 707 | 505 |
| 708 EXPECT_FALSE(extension->HasHostPermission(settings_url)); | 506 EXPECT_FALSE(PermissionsData::HasHostPermission(extension, settings_url)); |
| 709 EXPECT_FALSE(extension->HasHostPermission(about_url)); | 507 EXPECT_FALSE(PermissionsData::HasHostPermission(extension, about_url)); |
| 710 EXPECT_TRUE(extension->HasHostPermission(favicon_url)); | 508 EXPECT_TRUE(PermissionsData::HasHostPermission(extension, favicon_url)); |
| 711 | 509 |
| 712 // Test * for scheme, which implies just the http/https schemes. | 510 // Test * for scheme, which implies just the http/https schemes. |
| 713 extension = LoadManifestStrict("script_and_capture", | 511 extension = LoadManifestStrict("script_and_capture", |
| 714 "extension_wildcard.json"); | 512 "extension_wildcard.json"); |
| 715 EXPECT_TRUE(Allowed(extension, http_url)); | 513 EXPECT_TRUE(Allowed(extension, http_url)); |
| 716 EXPECT_TRUE(Allowed(extension, https_url)); | 514 EXPECT_TRUE(Allowed(extension, https_url)); |
| 717 EXPECT_TRUE(Blocked(extension, settings_url)); | 515 EXPECT_TRUE(Blocked(extension, settings_url)); |
| 718 EXPECT_TRUE(Blocked(extension, about_url)); | 516 EXPECT_TRUE(Blocked(extension, about_url)); |
| 719 EXPECT_TRUE(Blocked(extension, file_url)); | 517 EXPECT_TRUE(Blocked(extension, file_url)); |
| 720 EXPECT_TRUE(Blocked(extension, favicon_url)); | 518 EXPECT_TRUE(Blocked(extension, favicon_url)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 736 EXPECT_TRUE(Blocked(extension, about_url)); | 534 EXPECT_TRUE(Blocked(extension, about_url)); |
| 737 EXPECT_TRUE(Blocked(extension, file_url)); | 535 EXPECT_TRUE(Blocked(extension, file_url)); |
| 738 EXPECT_TRUE(Allowed(extension, favicon_url)); // chrome:// requested | 536 EXPECT_TRUE(Allowed(extension, favicon_url)); // chrome:// requested |
| 739 | 537 |
| 740 // Having chrome://favicon/* should not give you chrome://* | 538 // Having chrome://favicon/* should not give you chrome://* |
| 741 extension = LoadManifestStrict("script_and_capture", | 539 extension = LoadManifestStrict("script_and_capture", |
| 742 "extension_chrome_favicon_wildcard.json"); | 540 "extension_chrome_favicon_wildcard.json"); |
| 743 EXPECT_TRUE(Blocked(extension, settings_url)); | 541 EXPECT_TRUE(Blocked(extension, settings_url)); |
| 744 EXPECT_TRUE(Allowed(extension, favicon_url)); // chrome:// requested | 542 EXPECT_TRUE(Allowed(extension, favicon_url)); // chrome:// requested |
| 745 EXPECT_TRUE(Blocked(extension, about_url)); | 543 EXPECT_TRUE(Blocked(extension, about_url)); |
| 746 EXPECT_TRUE(extension->HasHostPermission(favicon_url)); | 544 EXPECT_TRUE(PermissionsData::HasHostPermission(extension, favicon_url)); |
| 747 | 545 |
| 748 // Having http://favicon should not give you chrome://favicon | 546 // Having http://favicon should not give you chrome://favicon |
| 749 extension = LoadManifestStrict("script_and_capture", | 547 extension = LoadManifestStrict("script_and_capture", |
| 750 "extension_http_favicon.json"); | 548 "extension_http_favicon.json"); |
| 751 EXPECT_TRUE(Blocked(extension, settings_url)); | 549 EXPECT_TRUE(Blocked(extension, settings_url)); |
| 752 EXPECT_TRUE(Blocked(extension, favicon_url)); | 550 EXPECT_TRUE(Blocked(extension, favicon_url)); |
| 753 | 551 |
| 754 // Component extensions with <all_urls> should get everything. | 552 // Component extensions with <all_urls> should get everything. |
| 755 extension = LoadManifest("script_and_capture", "extension_component_all.json", | 553 extension = LoadManifest("script_and_capture", "extension_component_all.json", |
| 756 Manifest::COMPONENT, Extension::NO_FLAGS); | 554 Manifest::COMPONENT, Extension::NO_FLAGS); |
| 757 EXPECT_TRUE(Allowed(extension, http_url)); | 555 EXPECT_TRUE(Allowed(extension, http_url)); |
| 758 EXPECT_TRUE(Allowed(extension, https_url)); | 556 EXPECT_TRUE(Allowed(extension, https_url)); |
| 759 EXPECT_TRUE(Allowed(extension, settings_url)); | 557 EXPECT_TRUE(Allowed(extension, settings_url)); |
| 760 EXPECT_TRUE(Allowed(extension, about_url)); | 558 EXPECT_TRUE(Allowed(extension, about_url)); |
| 761 EXPECT_TRUE(Allowed(extension, favicon_url)); | 559 EXPECT_TRUE(Allowed(extension, favicon_url)); |
| 762 EXPECT_TRUE(extension->HasHostPermission(favicon_url)); | 560 EXPECT_TRUE(PermissionsData::HasHostPermission(extension, favicon_url)); |
| 763 | 561 |
| 764 // Component extensions should only get access to what they ask for. | 562 // Component extensions should only get access to what they ask for. |
| 765 extension = LoadManifest("script_and_capture", | 563 extension = LoadManifest("script_and_capture", |
| 766 "extension_component_google.json", Manifest::COMPONENT, | 564 "extension_component_google.json", Manifest::COMPONENT, |
| 767 Extension::NO_FLAGS); | 565 Extension::NO_FLAGS); |
| 768 EXPECT_TRUE(Allowed(extension, http_url)); | 566 EXPECT_TRUE(Allowed(extension, http_url)); |
| 769 EXPECT_TRUE(Blocked(extension, https_url)); | 567 EXPECT_TRUE(Blocked(extension, https_url)); |
| 770 EXPECT_TRUE(Blocked(extension, file_url)); | 568 EXPECT_TRUE(Blocked(extension, file_url)); |
| 771 EXPECT_TRUE(Blocked(extension, settings_url)); | 569 EXPECT_TRUE(Blocked(extension, settings_url)); |
| 772 EXPECT_TRUE(Blocked(extension, favicon_url)); | 570 EXPECT_TRUE(Blocked(extension, favicon_url)); |
| 773 EXPECT_TRUE(Blocked(extension, about_url)); | 571 EXPECT_TRUE(Blocked(extension, about_url)); |
| 774 EXPECT_TRUE(Blocked(extension, extension_url)); | 572 EXPECT_TRUE(Blocked(extension, extension_url)); |
| 775 EXPECT_FALSE(extension->HasHostPermission(settings_url)); | 573 EXPECT_FALSE(PermissionsData::HasHostPermission(extension, settings_url)); |
| 776 } | 574 } |
| 777 | 575 |
| 778 TEST_F(ExtensionScriptAndCaptureVisibleTest, TabSpecific) { | 576 TEST_F(ExtensionScriptAndCaptureVisibleTest, TabSpecific) { |
| 779 scoped_refptr<Extension> extension = | 577 scoped_refptr<Extension> extension = |
| 780 LoadManifestStrict("script_and_capture", "tab_specific.json"); | 578 LoadManifestStrict("script_and_capture", "tab_specific.json"); |
| 781 | 579 |
| 782 EXPECT_FALSE(extension->GetTabSpecificPermissions(0).get()); | 580 EXPECT_FALSE(PermissionsData::GetTabSpecificPermissions(extension, 0).get()); |
| 783 EXPECT_FALSE(extension->GetTabSpecificPermissions(1).get()); | 581 EXPECT_FALSE(PermissionsData::GetTabSpecificPermissions(extension, 1).get()); |
| 784 EXPECT_FALSE(extension->GetTabSpecificPermissions(2).get()); | 582 EXPECT_FALSE(PermissionsData::GetTabSpecificPermissions(extension, 2).get()); |
| 785 | 583 |
| 786 std::set<GURL> no_urls; | 584 std::set<GURL> no_urls; |
| 787 | 585 |
| 788 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 0)); | 586 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 0)); |
| 789 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 1)); | 587 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 1)); |
| 790 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2)); | 588 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2)); |
| 791 | 589 |
| 792 URLPatternSet allowed_hosts; | 590 URLPatternSet allowed_hosts; |
| 793 allowed_hosts.AddPattern(URLPattern(URLPattern::SCHEME_ALL, | 591 allowed_hosts.AddPattern(URLPattern(URLPattern::SCHEME_ALL, |
| 794 http_url.spec())); | 592 http_url.spec())); |
| 795 std::set<GURL> allowed_urls; | 593 std::set<GURL> allowed_urls; |
| 796 allowed_urls.insert(http_url); | 594 allowed_urls.insert(http_url); |
| 797 // http_url_with_path() will also be allowed, because Extension should be | 595 // http_url_with_path() will also be allowed, because Extension should be |
| 798 // considering the security origin of the URL not the URL itself, and | 596 // considering the security origin of the URL not the URL itself, and |
| 799 // http_url is in allowed_hosts. | 597 // http_url is in allowed_hosts. |
| 800 allowed_urls.insert(http_url_with_path); | 598 allowed_urls.insert(http_url_with_path); |
| 801 | 599 |
| 802 { | 600 { |
| 803 scoped_refptr<PermissionSet> permissions( | 601 scoped_refptr<PermissionSet> permissions( |
| 804 new PermissionSet(APIPermissionSet(), allowed_hosts, URLPatternSet())); | 602 new PermissionSet(APIPermissionSet(), allowed_hosts, URLPatternSet())); |
| 805 extension->UpdateTabSpecificPermissions(0, permissions); | 603 PermissionsData::UpdateTabSpecificPermissions(extension, 0, permissions); |
| 806 EXPECT_EQ(permissions->explicit_hosts(), | 604 EXPECT_EQ(permissions->explicit_hosts(), |
| 807 extension->GetTabSpecificPermissions(0)->explicit_hosts()); | 605 PermissionsData::GetTabSpecificPermissions( |
| 606 extension, 0)->explicit_hosts()); |
| 808 } | 607 } |
| 809 | 608 |
| 810 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, allowed_urls, 0)); | 609 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, allowed_urls, 0)); |
| 811 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 1)); | 610 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 1)); |
| 812 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2)); | 611 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2)); |
| 813 | 612 |
| 814 extension->ClearTabSpecificPermissions(0); | 613 PermissionsData::ClearTabSpecificPermissions(extension, 0); |
| 815 EXPECT_FALSE(extension->GetTabSpecificPermissions(0).get()); | 614 EXPECT_FALSE(PermissionsData::GetTabSpecificPermissions(extension, 0).get()); |
| 816 | 615 |
| 817 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 0)); | 616 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 0)); |
| 818 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 1)); | 617 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 1)); |
| 819 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2)); | 618 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2)); |
| 820 | 619 |
| 821 std::set<GURL> more_allowed_urls = allowed_urls; | 620 std::set<GURL> more_allowed_urls = allowed_urls; |
| 822 more_allowed_urls.insert(https_url); | 621 more_allowed_urls.insert(https_url); |
| 823 URLPatternSet more_allowed_hosts = allowed_hosts; | 622 URLPatternSet more_allowed_hosts = allowed_hosts; |
| 824 more_allowed_hosts.AddPattern(URLPattern(URLPattern::SCHEME_ALL, | 623 more_allowed_hosts.AddPattern(URLPattern(URLPattern::SCHEME_ALL, |
| 825 https_url.spec())); | 624 https_url.spec())); |
| 826 | 625 |
| 827 { | 626 { |
| 828 scoped_refptr<PermissionSet> permissions( | 627 scoped_refptr<PermissionSet> permissions( |
| 829 new PermissionSet(APIPermissionSet(), allowed_hosts, URLPatternSet())); | 628 new PermissionSet(APIPermissionSet(), allowed_hosts, URLPatternSet())); |
| 830 extension->UpdateTabSpecificPermissions(0, permissions); | 629 PermissionsData::UpdateTabSpecificPermissions(extension, 0, permissions); |
| 831 EXPECT_EQ(permissions->explicit_hosts(), | 630 EXPECT_EQ(permissions->explicit_hosts(), |
| 832 extension->GetTabSpecificPermissions(0)->explicit_hosts()); | 631 PermissionsData::GetTabSpecificPermissions( |
| 632 extension, 0)->explicit_hosts()); |
| 833 | 633 |
| 834 permissions = new PermissionSet(APIPermissionSet(), | 634 permissions = new PermissionSet(APIPermissionSet(), |
| 835 more_allowed_hosts, | 635 more_allowed_hosts, |
| 836 URLPatternSet()); | 636 URLPatternSet()); |
| 837 extension->UpdateTabSpecificPermissions(1, permissions); | 637 PermissionsData::UpdateTabSpecificPermissions(extension, 1, permissions); |
| 838 EXPECT_EQ(permissions->explicit_hosts(), | 638 EXPECT_EQ(permissions->explicit_hosts(), |
| 839 extension->GetTabSpecificPermissions(1)->explicit_hosts()); | 639 PermissionsData::GetTabSpecificPermissions( |
| 640 extension, 1)->explicit_hosts()); |
| 840 } | 641 } |
| 841 | 642 |
| 842 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, allowed_urls, 0)); | 643 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, allowed_urls, 0)); |
| 843 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, more_allowed_urls, 1)); | 644 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, more_allowed_urls, 1)); |
| 844 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2)); | 645 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2)); |
| 845 | 646 |
| 846 extension->ClearTabSpecificPermissions(0); | 647 PermissionsData::ClearTabSpecificPermissions(extension, 0); |
| 847 EXPECT_FALSE(extension->GetTabSpecificPermissions(0).get()); | 648 EXPECT_FALSE(PermissionsData::GetTabSpecificPermissions(extension, 0).get()); |
| 848 | 649 |
| 849 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 0)); | 650 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 0)); |
| 850 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, more_allowed_urls, 1)); | 651 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, more_allowed_urls, 1)); |
| 851 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2)); | 652 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2)); |
| 852 | 653 |
| 853 extension->ClearTabSpecificPermissions(1); | 654 PermissionsData::ClearTabSpecificPermissions(extension, 1); |
| 854 EXPECT_FALSE(extension->GetTabSpecificPermissions(1).get()); | 655 EXPECT_FALSE(PermissionsData::GetTabSpecificPermissions(extension, 1).get()); |
| 855 | 656 |
| 856 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 0)); | 657 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 0)); |
| 857 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 1)); | 658 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 1)); |
| 858 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2)); | 659 EXPECT_TRUE(AllowedExclusivelyOnTab(extension, no_urls, 2)); |
| 859 } | 660 } |
| 860 | 661 |
| 861 TEST(ExtensionTest, OptionalOnlyPermission) { | |
| 862 // Set feature current channel to dev because the only permission that must | |
| 863 // be optional (usbDevices) is only available on dev channel. | |
| 864 Feature::ScopedCurrentChannel scoped_channel( | |
| 865 chrome::VersionInfo::CHANNEL_DEV); | |
| 866 | |
| 867 scoped_refptr<Extension> extension; | |
| 868 std::string error; | |
| 869 extension = LoadManifestUnchecked("optional_only_permission", | |
| 870 "manifest1.json", | |
| 871 Manifest::INTERNAL, Extension::NO_FLAGS, | |
| 872 &error); | |
| 873 EXPECT_TRUE(extension == NULL); | |
| 874 ASSERT_EQ(ErrorUtils::FormatErrorMessage( | |
| 875 errors::kPermissionMustBeOptional, "usbDevices"), error); | |
| 876 | |
| 877 error.clear(); | |
| 878 extension = LoadManifestUnchecked("optional_only_permission", | |
| 879 "manifest2.json", | |
| 880 Manifest::INTERNAL, Extension::NO_FLAGS, | |
| 881 &error); | |
| 882 EXPECT_TRUE(extension != NULL); | |
| 883 EXPECT_TRUE(error.empty()); | |
| 884 } | |
| 885 | |
| 886 } // namespace extensions | 662 } // namespace extensions |
| OLD | NEW |