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 "chrome/common/extensions/extension.h" | 5 #include "chrome/common/extensions/extension.h" |
6 | 6 |
7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "chrome/common/chrome_paths.h" | 15 #include "chrome/common/chrome_paths.h" |
16 #include "chrome/common/extensions/command.h" | 16 #include "chrome/common/extensions/command.h" |
17 #include "chrome/common/extensions/extension_action.h" | 17 #include "chrome/common/extensions/extension_action.h" |
18 #include "chrome/common/extensions/extension_error_utils.h" | 18 #include "chrome/common/extensions/extension_error_utils.h" |
19 #include "chrome/common/extensions/extension_file_util.h" | 19 #include "chrome/common/extensions/extension_file_util.h" |
20 #include "chrome/common/extensions/extension_manifest_constants.h" | 20 #include "chrome/common/extensions/extension_manifest_constants.h" |
21 #include "chrome/common/extensions/extension_resource.h" | 21 #include "chrome/common/extensions/extension_resource.h" |
| 22 #include "chrome/common/extensions/features/feature.h" |
22 #include "chrome/common/extensions/permissions/api_permission.h" | 23 #include "chrome/common/extensions/permissions/api_permission.h" |
23 #include "chrome/common/extensions/permissions/permission_set.h" | 24 #include "chrome/common/extensions/permissions/permission_set.h" |
| 25 #include "chrome/common/extensions/permissions/socket_permission.h" |
24 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
25 #include "googleurl/src/gurl.h" | 27 #include "googleurl/src/gurl.h" |
26 #include "net/base/mime_sniffer.h" | 28 #include "net/base/mime_sniffer.h" |
27 #include "skia/ext/image_operations.h" | 29 #include "skia/ext/image_operations.h" |
28 #include "net/base/mock_host_resolver.h" | 30 #include "net/base/mock_host_resolver.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
30 #include "third_party/skia/include/core/SkBitmap.h" | 32 #include "third_party/skia/include/core/SkBitmap.h" |
31 #include "ui/gfx/codec/png_codec.h" | 33 #include "ui/gfx/codec/png_codec.h" |
32 | 34 |
| 35 using extensions::APIPermission; |
33 using extensions::APIPermissionSet; | 36 using extensions::APIPermissionSet; |
34 using extensions::Extension; | 37 using extensions::Extension; |
| 38 using extensions::Feature; |
35 using extensions::PermissionSet; | 39 using extensions::PermissionSet; |
| 40 using extensions::SocketPermission; |
| 41 using extensions::SocketPermissionData; |
36 | 42 |
37 namespace keys = extension_manifest_keys; | 43 namespace keys = extension_manifest_keys; |
38 namespace values = extension_manifest_values; | 44 namespace values = extension_manifest_values; |
39 namespace errors = extension_manifest_errors; | 45 namespace errors = extension_manifest_errors; |
40 | 46 |
41 namespace { | 47 namespace { |
42 | 48 |
43 void CompareLists(const std::vector<std::string>& expected, | 49 void CompareLists(const std::vector<std::string>& expected, |
44 const std::vector<std::string>& actual) { | 50 const std::vector<std::string>& actual) { |
45 ASSERT_EQ(expected.size(), actual.size()); | 51 ASSERT_EQ(expected.size(), actual.size()); |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); | 443 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); |
438 | 444 |
439 extension = LoadManifest("effective_host_permissions", "all_hosts3.json"); | 445 extension = LoadManifest("effective_host_permissions", "all_hosts3.json"); |
440 hosts = extension->GetEffectiveHostPermissions(); | 446 hosts = extension->GetEffectiveHostPermissions(); |
441 EXPECT_FALSE(hosts.MatchesURL(GURL("http://test/"))); | 447 EXPECT_FALSE(hosts.MatchesURL(GURL("http://test/"))); |
442 EXPECT_TRUE(hosts.MatchesURL(GURL("https://test/"))); | 448 EXPECT_TRUE(hosts.MatchesURL(GURL("https://test/"))); |
443 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); | 449 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); |
444 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); | 450 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); |
445 } | 451 } |
446 | 452 |
| 453 static bool CheckSocketPermission(scoped_refptr<Extension> extension, |
| 454 SocketPermissionData::OperationType type, |
| 455 const char* host, |
| 456 int port) { |
| 457 SocketPermission::CheckParam param(type, host, port); |
| 458 return extension->CheckAPIPermissionWithDetail( |
| 459 APIPermission::kSocket, ¶m); |
| 460 } |
| 461 |
| 462 TEST(ExtensionTest, SocketPermissions) { |
| 463 // Set feature current channel to appropriate value. |
| 464 Feature::ScopedCurrentChannel scoped_channel( |
| 465 chrome::VersionInfo::GetChannel()); |
| 466 scoped_refptr<Extension> extension; |
| 467 std::string error; |
| 468 |
| 469 extension = LoadManifest("socket_permissions", "empty.json"); |
| 470 EXPECT_FALSE(CheckSocketPermission( |
| 471 extension, SocketPermissionData::TCP_CONNECT, "www.example.com", 80)); |
| 472 |
| 473 extension = LoadManifestUnchecked("socket_permissions", |
| 474 "socket1.json", |
| 475 Extension::INTERNAL, Extension::NO_FLAGS, |
| 476 &error); |
| 477 EXPECT_TRUE(extension == NULL); |
| 478 ASSERT_EQ(ExtensionErrorUtils::FormatErrorMessage( |
| 479 errors::kInvalidPermission, base::IntToString(0)), error); |
| 480 |
| 481 extension = LoadManifest("socket_permissions", "socket2.json"); |
| 482 EXPECT_TRUE(CheckSocketPermission( |
| 483 extension, SocketPermissionData::TCP_CONNECT, "www.example.com", 80)); |
| 484 EXPECT_FALSE(CheckSocketPermission( |
| 485 extension, SocketPermissionData::UDP_BIND, "", 80)); |
| 486 EXPECT_TRUE(CheckSocketPermission( |
| 487 extension, SocketPermissionData::UDP_BIND, "", 8888)); |
| 488 |
| 489 EXPECT_FALSE(CheckSocketPermission( |
| 490 extension, SocketPermissionData::UDP_SEND_TO, "example.com", 1900)); |
| 491 EXPECT_TRUE(CheckSocketPermission( |
| 492 extension, SocketPermissionData::UDP_SEND_TO, "239.255.255.250", 1900)); |
| 493 } |
| 494 |
447 // Returns a copy of |source| resized to |size| x |size|. | 495 // Returns a copy of |source| resized to |size| x |size|. |
448 static SkBitmap ResizedCopy(const SkBitmap& source, int size) { | 496 static SkBitmap ResizedCopy(const SkBitmap& source, int size) { |
449 return skia::ImageOperations::Resize(source, | 497 return skia::ImageOperations::Resize(source, |
450 skia::ImageOperations::RESIZE_LANCZOS3, | 498 skia::ImageOperations::RESIZE_LANCZOS3, |
451 size, | 499 size, |
452 size); | 500 size); |
453 } | 501 } |
454 | 502 |
455 static bool SizeEquals(const SkBitmap& bitmap, const gfx::Size& size) { | 503 static bool SizeEquals(const SkBitmap& bitmap, const gfx::Size& size) { |
456 return bitmap.width() == size.width() && bitmap.height() == size.height(); | 504 return bitmap.width() == size.width() && bitmap.height() == size.height(); |
(...skipping 654 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1111 } | 1159 } |
1112 | 1160 |
1113 TEST(ExtensionTest, GetSyncTypeExtensionWithTwoPlugins) { | 1161 TEST(ExtensionTest, GetSyncTypeExtensionWithTwoPlugins) { |
1114 scoped_refptr<Extension> extension( | 1162 scoped_refptr<Extension> extension( |
1115 MakeSyncTestExtension(EXTENSION, GURL(), GURL(), | 1163 MakeSyncTestExtension(EXTENSION, GURL(), GURL(), |
1116 Extension::INTERNAL, 2, FilePath())); | 1164 Extension::INTERNAL, 2, FilePath())); |
1117 if (extension) | 1165 if (extension) |
1118 EXPECT_EQ(extension->GetSyncType(), Extension::SYNC_TYPE_NONE); | 1166 EXPECT_EQ(extension->GetSyncType(), Extension::SYNC_TYPE_NONE); |
1119 } | 1167 } |
1120 #endif // !defined(OS_CHROMEOS) | 1168 #endif // !defined(OS_CHROMEOS) |
OLD | NEW |