| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/pepper_permission_util.h" | 5 #include "chrome/common/pepper_permission_util.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "chrome/common/extensions/features/feature_channel.h" | 10 #include "chrome/common/extensions/features/feature_channel.h" |
| 11 #include "components/crx_file/id_util.h" |
| 11 #include "extensions/common/extension_builder.h" | 12 #include "extensions/common/extension_builder.h" |
| 12 #include "extensions/common/extension_set.h" | 13 #include "extensions/common/extension_set.h" |
| 13 #include "extensions/common/id_util.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 using chrome::IsExtensionOrSharedModuleWhitelisted; | 16 using chrome::IsExtensionOrSharedModuleWhitelisted; |
| 17 | 17 |
| 18 namespace extensions { | 18 namespace extensions { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // Return an extension with |id| which imports a module with the given | 22 // Return an extension with |id| which imports a module with the given |
| 23 // |import_id|. | 23 // |import_id|. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 38 .AddFlags(Extension::FROM_WEBSTORE) | 38 .AddFlags(Extension::FROM_WEBSTORE) |
| 39 .SetID(id) | 39 .SetID(id) |
| 40 .Build(); | 40 .Build(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 } // namespace | 43 } // namespace |
| 44 | 44 |
| 45 TEST(PepperPermissionUtilTest, ExtensionWhitelisting) { | 45 TEST(PepperPermissionUtilTest, ExtensionWhitelisting) { |
| 46 ScopedCurrentChannel current_channel(chrome::VersionInfo::CHANNEL_UNKNOWN); | 46 ScopedCurrentChannel current_channel(chrome::VersionInfo::CHANNEL_UNKNOWN); |
| 47 ExtensionSet extensions; | 47 ExtensionSet extensions; |
| 48 std::string whitelisted_id = id_util::GenerateId("whitelisted_extension"); | 48 std::string whitelisted_id = |
| 49 crx_file::id_util::GenerateId("whitelisted_extension"); |
| 49 scoped_ptr<base::DictionaryValue> manifest = | 50 scoped_ptr<base::DictionaryValue> manifest = |
| 50 DictionaryBuilder() | 51 DictionaryBuilder() |
| 51 .Set("name", "Whitelisted Extension") | 52 .Set("name", "Whitelisted Extension") |
| 52 .Set("version", "1.0") | 53 .Set("version", "1.0") |
| 53 .Set("manifest_version", 2) | 54 .Set("manifest_version", 2) |
| 54 .Build(); | 55 .Build(); |
| 55 scoped_refptr<Extension> ext = ExtensionBuilder() | 56 scoped_refptr<Extension> ext = ExtensionBuilder() |
| 56 .SetManifest(manifest.Pass()) | 57 .SetManifest(manifest.Pass()) |
| 57 .SetID(whitelisted_id) | 58 .SetID(whitelisted_id) |
| 58 .Build(); | 59 .Build(); |
| 59 extensions.Insert(ext); | 60 extensions.Insert(ext); |
| 60 std::set<std::string> whitelist; | 61 std::set<std::string> whitelist; |
| 61 std::string url = std::string("chrome-extension://") + whitelisted_id + | 62 std::string url = std::string("chrome-extension://") + whitelisted_id + |
| 62 std::string("/manifest.nmf"); | 63 std::string("/manifest.nmf"); |
| 63 std::string bad_scheme_url = | 64 std::string bad_scheme_url = |
| 64 std::string("http://") + whitelisted_id + std::string("/manifest.nmf"); | 65 std::string("http://") + whitelisted_id + std::string("/manifest.nmf"); |
| 65 std::string bad_host_url = | 66 std::string bad_host_url = std::string("chrome-extension://") + |
| 66 std::string("chrome-extension://") + id_util::GenerateId("bad_host"); | 67 crx_file::id_util::GenerateId("bad_host"); |
| 67 std::string("/manifest.nmf"); | 68 std::string("/manifest.nmf"); |
| 68 | 69 |
| 69 EXPECT_FALSE( | 70 EXPECT_FALSE( |
| 70 IsExtensionOrSharedModuleWhitelisted(GURL(url), &extensions, whitelist)); | 71 IsExtensionOrSharedModuleWhitelisted(GURL(url), &extensions, whitelist)); |
| 71 whitelist.insert(whitelisted_id); | 72 whitelist.insert(whitelisted_id); |
| 72 EXPECT_TRUE( | 73 EXPECT_TRUE( |
| 73 IsExtensionOrSharedModuleWhitelisted(GURL(url), &extensions, whitelist)); | 74 IsExtensionOrSharedModuleWhitelisted(GURL(url), &extensions, whitelist)); |
| 74 EXPECT_FALSE(IsExtensionOrSharedModuleWhitelisted( | 75 EXPECT_FALSE(IsExtensionOrSharedModuleWhitelisted( |
| 75 GURL(bad_scheme_url), &extensions, whitelist)); | 76 GURL(bad_scheme_url), &extensions, whitelist)); |
| 76 EXPECT_FALSE(IsExtensionOrSharedModuleWhitelisted( | 77 EXPECT_FALSE(IsExtensionOrSharedModuleWhitelisted( |
| 77 GURL(bad_host_url), &extensions, whitelist)); | 78 GURL(bad_host_url), &extensions, whitelist)); |
| 78 } | 79 } |
| 79 | 80 |
| 80 TEST(PepperPermissionUtilTest, SharedModuleWhitelisting) { | 81 TEST(PepperPermissionUtilTest, SharedModuleWhitelisting) { |
| 81 ScopedCurrentChannel current_channel(chrome::VersionInfo::CHANNEL_UNKNOWN); | 82 ScopedCurrentChannel current_channel(chrome::VersionInfo::CHANNEL_UNKNOWN); |
| 82 ExtensionSet extensions; | 83 ExtensionSet extensions; |
| 83 std::string whitelisted_id = id_util::GenerateId("extension_id"); | 84 std::string whitelisted_id = crx_file::id_util::GenerateId("extension_id"); |
| 84 std::string bad_id = id_util::GenerateId("bad_id"); | 85 std::string bad_id = crx_file::id_util::GenerateId("bad_id"); |
| 85 | 86 |
| 86 scoped_ptr<base::DictionaryValue> shared_module_manifest = | 87 scoped_ptr<base::DictionaryValue> shared_module_manifest = |
| 87 DictionaryBuilder() | 88 DictionaryBuilder() |
| 88 .Set("name", "Whitelisted Shared Module") | 89 .Set("name", "Whitelisted Shared Module") |
| 89 .Set("version", "1.0") | 90 .Set("version", "1.0") |
| 90 .Set("manifest_version", 2) | 91 .Set("manifest_version", 2) |
| 91 .Set("export", | 92 .Set("export", |
| 92 DictionaryBuilder() | 93 DictionaryBuilder() |
| 93 .Set("resources", ListBuilder().Append("*")) | 94 .Set("resources", ListBuilder().Append("*")) |
| 94 // Add the extension to the whitelist. This | 95 // Add the extension to the whitelist. This |
| (...skipping 30 matching lines...) Expand all Loading... |
| 125 GURL(bad_extension_url), &extensions, whitelist)); | 126 GURL(bad_extension_url), &extensions, whitelist)); |
| 126 | 127 |
| 127 // Note that the whitelist should be empty after this call, so tests checking | 128 // Note that the whitelist should be empty after this call, so tests checking |
| 128 // for failure to import will fail because of this. | 129 // for failure to import will fail because of this. |
| 129 whitelist.erase(shared_module->id()); | 130 whitelist.erase(shared_module->id()); |
| 130 EXPECT_FALSE(IsExtensionOrSharedModuleWhitelisted( | 131 EXPECT_FALSE(IsExtensionOrSharedModuleWhitelisted( |
| 131 GURL(extension_url), &extensions, whitelist)); | 132 GURL(extension_url), &extensions, whitelist)); |
| 132 } | 133 } |
| 133 | 134 |
| 134 } // namespace extensions | 135 } // namespace extensions |
| OLD | NEW |