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/permissions/api_permission.h" | 22 #include "chrome/common/extensions/permissions/api_permission.h" |
23 #include "chrome/common/extensions/permissions/permission_set.h" | 23 #include "chrome/common/extensions/permissions/permission_set.h" |
24 #include "chrome/common/extensions/permissions/socket_permission.h" | |
24 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
25 #include "googleurl/src/gurl.h" | 26 #include "googleurl/src/gurl.h" |
26 #include "net/base/mime_sniffer.h" | 27 #include "net/base/mime_sniffer.h" |
27 #include "skia/ext/image_operations.h" | 28 #include "skia/ext/image_operations.h" |
28 #include "net/base/mock_host_resolver.h" | 29 #include "net/base/mock_host_resolver.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
30 #include "third_party/skia/include/core/SkBitmap.h" | 31 #include "third_party/skia/include/core/SkBitmap.h" |
31 #include "ui/gfx/codec/png_codec.h" | 32 #include "ui/gfx/codec/png_codec.h" |
32 | 33 |
34 using extensions::APIPermission; | |
33 using extensions::APIPermissionSet; | 35 using extensions::APIPermissionSet; |
34 using extensions::Extension; | 36 using extensions::Extension; |
35 using extensions::PermissionSet; | 37 using extensions::PermissionSet; |
38 using extensions::SocketPermission; | |
39 using extensions::SocketPermissionData; | |
36 | 40 |
37 namespace keys = extension_manifest_keys; | 41 namespace keys = extension_manifest_keys; |
38 namespace values = extension_manifest_values; | 42 namespace values = extension_manifest_values; |
39 namespace errors = extension_manifest_errors; | 43 namespace errors = extension_manifest_errors; |
40 | 44 |
41 namespace { | 45 namespace { |
42 | 46 |
43 void CompareLists(const std::vector<std::string>& expected, | 47 void CompareLists(const std::vector<std::string>& expected, |
44 const std::vector<std::string>& actual) { | 48 const std::vector<std::string>& actual) { |
45 ASSERT_EQ(expected.size(), actual.size()); | 49 ASSERT_EQ(expected.size(), actual.size()); |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
437 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); | 441 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); |
438 | 442 |
439 extension = LoadManifest("effective_host_permissions", "all_hosts3.json"); | 443 extension = LoadManifest("effective_host_permissions", "all_hosts3.json"); |
440 hosts = extension->GetEffectiveHostPermissions(); | 444 hosts = extension->GetEffectiveHostPermissions(); |
441 EXPECT_FALSE(hosts.MatchesURL(GURL("http://test/"))); | 445 EXPECT_FALSE(hosts.MatchesURL(GURL("http://test/"))); |
442 EXPECT_TRUE(hosts.MatchesURL(GURL("https://test/"))); | 446 EXPECT_TRUE(hosts.MatchesURL(GURL("https://test/"))); |
443 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); | 447 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); |
444 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); | 448 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); |
445 } | 449 } |
446 | 450 |
451 static bool CheckSocketPermission(scoped_refptr<Extension> extension, | |
452 SocketPermissionData::OperationType type, | |
453 const char* host, | |
454 int port) { | |
455 SocketPermission::CheckParam param(type, host, port); | |
456 return extension->CheckAPIPermissionWithDetail( | |
457 APIPermission::kSocket, ¶m); | |
458 } | |
459 | |
460 TEST(ExtensionTest, SocketPermissions) { | |
miket_OOO
2012/08/06 21:04:06
These are good because they're tests, but you can
Peng
2012/08/07 21:31:55
I added some unitttest for it. Done
| |
461 scoped_refptr<Extension> extension; | |
462 extension = LoadManifest("socket_permissions", "empty.json"); | |
463 EXPECT_FALSE(CheckSocketPermission( | |
464 extension, SocketPermissionData::TCP_CONNECT, "www.example.com", 80)); | |
465 | |
466 extension = LoadManifest("socket_permissions", "socket1.json"); | |
467 EXPECT_FALSE(CheckSocketPermission( | |
468 extension, SocketPermissionData::TCP_CONNECT, "www.example.com", 80)); | |
469 | |
470 extension = LoadManifest("socket_permissions", "socket2.json"); | |
471 EXPECT_TRUE(CheckSocketPermission( | |
472 extension, SocketPermissionData::TCP_CONNECT, "www.example.com", 80)); | |
473 EXPECT_FALSE(CheckSocketPermission( | |
474 extension, SocketPermissionData::UDP_BIND, "", 80)); | |
475 EXPECT_TRUE(CheckSocketPermission( | |
476 extension, SocketPermissionData::UDP_BIND, "", 8888)); | |
477 | |
478 EXPECT_FALSE(CheckSocketPermission( | |
479 extension, SocketPermissionData::UDP_SEND_TO, "example.com", 1900)); | |
480 EXPECT_TRUE(CheckSocketPermission( | |
481 extension, SocketPermissionData::UDP_SEND_TO, "239.255.255.250", 1900)); | |
482 } | |
483 | |
447 // Returns a copy of |source| resized to |size| x |size|. | 484 // Returns a copy of |source| resized to |size| x |size|. |
448 static SkBitmap ResizedCopy(const SkBitmap& source, int size) { | 485 static SkBitmap ResizedCopy(const SkBitmap& source, int size) { |
449 return skia::ImageOperations::Resize(source, | 486 return skia::ImageOperations::Resize(source, |
450 skia::ImageOperations::RESIZE_LANCZOS3, | 487 skia::ImageOperations::RESIZE_LANCZOS3, |
451 size, | 488 size, |
452 size); | 489 size); |
453 } | 490 } |
454 | 491 |
455 static bool SizeEquals(const SkBitmap& bitmap, const gfx::Size& size) { | 492 static bool SizeEquals(const SkBitmap& bitmap, const gfx::Size& size) { |
456 return bitmap.width() == size.width() && bitmap.height() == size.height(); | 493 return bitmap.width() == size.width() && bitmap.height() == size.height(); |
(...skipping 654 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1111 } | 1148 } |
1112 | 1149 |
1113 TEST(ExtensionTest, GetSyncTypeExtensionWithTwoPlugins) { | 1150 TEST(ExtensionTest, GetSyncTypeExtensionWithTwoPlugins) { |
1114 scoped_refptr<Extension> extension( | 1151 scoped_refptr<Extension> extension( |
1115 MakeSyncTestExtension(EXTENSION, GURL(), GURL(), | 1152 MakeSyncTestExtension(EXTENSION, GURL(), GURL(), |
1116 Extension::INTERNAL, 2, FilePath())); | 1153 Extension::INTERNAL, 2, FilePath())); |
1117 if (extension) | 1154 if (extension) |
1118 EXPECT_EQ(extension->GetSyncType(), Extension::SYNC_TYPE_NONE); | 1155 EXPECT_EQ(extension->GetSyncType(), Extension::SYNC_TYPE_NONE); |
1119 } | 1156 } |
1120 #endif // !defined(OS_CHROMEOS) | 1157 #endif // !defined(OS_CHROMEOS) |
OLD | NEW |