| 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 "base/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/strings/string16.h" | 7 #include "base/strings/string16.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_service_unittest.h" | 10 #include "chrome/browser/extensions/extension_service_unittest.h" |
| 11 #include "chrome/browser/extensions/pending_extension_manager.h" | 11 #include "chrome/browser/extensions/pending_extension_manager.h" |
| 12 #include "chrome/browser/extensions/shared_module_service.h" | 12 #include "chrome/browser/extensions/shared_module_service.h" |
| 13 #include "extensions/browser/extension_registry.h" | 13 #include "extensions/browser/extension_registry.h" |
| 14 #include "extensions/common/extension_builder.h" | 14 #include "extensions/common/extension_builder.h" |
| 15 #include "extensions/common/id_util.h" | 15 #include "extensions/common/id_util.h" |
| 16 #include "extensions/common/value_builder.h" | 16 #include "extensions/common/value_builder.h" |
| 17 #include "sync/api/string_ordinal.h" | 17 #include "sync/api/string_ordinal.h" |
| 18 | 18 |
| 19 namespace extensions { | 19 namespace extensions { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // Return an extension which imports a module with the given |id|. | 23 // Return an extension with |id| which imports a module with the given |
| 24 // |import_id|. |
| 24 scoped_refptr<Extension> CreateExtensionImportingModule( | 25 scoped_refptr<Extension> CreateExtensionImportingModule( |
| 25 const std::string& import_id) { | 26 const std::string& import_id, const std::string& id) { |
| 26 scoped_ptr<base::DictionaryValue> manifest = | 27 scoped_ptr<base::DictionaryValue> manifest = |
| 27 DictionaryBuilder() | 28 DictionaryBuilder() |
| 28 .Set("name", "Has Dependent Modules") | 29 .Set("name", "Has Dependent Modules") |
| 29 .Set("version", "1.0") | 30 .Set("version", "1.0") |
| 30 .Set("manifest_version", 2) | 31 .Set("manifest_version", 2) |
| 31 .Set("import", | 32 .Set("import", |
| 32 ListBuilder().Append(DictionaryBuilder().Set("id", import_id))) | 33 ListBuilder().Append(DictionaryBuilder().Set("id", import_id))) |
| 33 .Build(); | 34 .Build(); |
| 34 | 35 |
| 35 return ExtensionBuilder().SetManifest(manifest.Pass()) | 36 return ExtensionBuilder().SetManifest(manifest.Pass()) |
| 36 .AddFlags(Extension::FROM_WEBSTORE) | 37 .AddFlags(Extension::FROM_WEBSTORE) |
| 38 .SetID(id) |
| 37 .Build(); | 39 .Build(); |
| 38 } | 40 } |
| 39 | 41 |
| 40 } // namespace | 42 } // namespace |
| 41 | 43 |
| 42 class SharedModuleServiceUnitTest : public ExtensionServiceTestBase { | 44 class SharedModuleServiceUnitTest : public ExtensionServiceTestBase { |
| 43 protected: | 45 protected: |
| 44 virtual void SetUp() OVERRIDE; | 46 virtual void SetUp() OVERRIDE; |
| 45 | 47 |
| 46 // Install an extension and notify the ExtensionService. | 48 // Install an extension and notify the ExtensionService. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 74 ExtensionRegistry::ENABLED)) { | 76 ExtensionRegistry::ENABLED)) { |
| 75 return testing::AssertionFailure() << "Could not install extension."; | 77 return testing::AssertionFailure() << "Could not install extension."; |
| 76 } | 78 } |
| 77 | 79 |
| 78 return testing::AssertionSuccess(); | 80 return testing::AssertionSuccess(); |
| 79 } | 81 } |
| 80 | 82 |
| 81 TEST_F(SharedModuleServiceUnitTest, AddDependentSharedModules) { | 83 TEST_F(SharedModuleServiceUnitTest, AddDependentSharedModules) { |
| 82 // Create an extension that has a dependency. | 84 // Create an extension that has a dependency. |
| 83 std::string import_id = id_util::GenerateId("id"); | 85 std::string import_id = id_util::GenerateId("id"); |
| 86 std::string extension_id = id_util::GenerateId("extension_id"); |
| 84 scoped_refptr<Extension> extension = | 87 scoped_refptr<Extension> extension = |
| 85 CreateExtensionImportingModule(import_id); | 88 CreateExtensionImportingModule(import_id, extension_id); |
| 86 | 89 |
| 87 PendingExtensionManager* pending_extension_manager = | 90 PendingExtensionManager* pending_extension_manager = |
| 88 service_->pending_extension_manager(); | 91 service_->pending_extension_manager(); |
| 89 | 92 |
| 90 // Verify that we don't currently want to install the imported module. | 93 // Verify that we don't currently want to install the imported module. |
| 91 EXPECT_FALSE(pending_extension_manager->IsIdPending(import_id)); | 94 EXPECT_FALSE(pending_extension_manager->IsIdPending(import_id)); |
| 92 | 95 |
| 93 // Try to satisfy imports for the extension. This should queue the imported | 96 // Try to satisfy imports for the extension. This should queue the imported |
| 94 // module's installation. | 97 // module's installation. |
| 95 service_->shared_module_service()->SatisfyImports(extension); | 98 service_->shared_module_service()->SatisfyImports(extension); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 107 DictionaryBuilder().Set("resources", | 110 DictionaryBuilder().Set("resources", |
| 108 ListBuilder().Append("foo.js"))).Build(); | 111 ListBuilder().Append("foo.js"))).Build(); |
| 109 scoped_refptr<Extension> shared_module = | 112 scoped_refptr<Extension> shared_module = |
| 110 ExtensionBuilder().SetManifest(manifest.Pass()) | 113 ExtensionBuilder().SetManifest(manifest.Pass()) |
| 111 .AddFlags(Extension::FROM_WEBSTORE) | 114 .AddFlags(Extension::FROM_WEBSTORE) |
| 112 .SetID(id_util::GenerateId("shared_module")) | 115 .SetID(id_util::GenerateId("shared_module")) |
| 113 .Build(); | 116 .Build(); |
| 114 | 117 |
| 115 EXPECT_TRUE(InstallExtension(shared_module)); | 118 EXPECT_TRUE(InstallExtension(shared_module)); |
| 116 | 119 |
| 120 std::string extension_id = id_util::GenerateId("extension_id"); |
| 117 // Create and install an extension that imports our new module. | 121 // Create and install an extension that imports our new module. |
| 118 scoped_refptr<Extension> importing_extension = | 122 scoped_refptr<Extension> importing_extension = |
| 119 CreateExtensionImportingModule(shared_module->id()); | 123 CreateExtensionImportingModule(shared_module->id(), extension_id); |
| 120 EXPECT_TRUE(InstallExtension(importing_extension)); | 124 EXPECT_TRUE(InstallExtension(importing_extension)); |
| 121 | 125 |
| 122 // Uninstall the extension that imports our module. | 126 // Uninstall the extension that imports our module. |
| 123 base::string16 error; | 127 base::string16 error; |
| 124 service_->UninstallExtension(importing_extension->id(), | 128 service_->UninstallExtension(importing_extension->id(), |
| 125 false, // Not external uninstall. | 129 false, // Not external uninstall. |
| 126 &error); | 130 &error); |
| 127 EXPECT_TRUE(error.empty()); | 131 EXPECT_TRUE(error.empty()); |
| 128 | 132 |
| 129 // Since the module was only referenced by that single extension, it should | 133 // Since the module was only referenced by that single extension, it should |
| 130 // have been uninstalled as a side-effect of uninstalling the extension that | 134 // have been uninstalled as a side-effect of uninstalling the extension that |
| 131 // depended upon it. | 135 // depended upon it. |
| 132 EXPECT_FALSE(registry_->GetExtensionById(shared_module->id(), | 136 EXPECT_FALSE(registry_->GetExtensionById(shared_module->id(), |
| 133 ExtensionRegistry::EVERYTHING)); | 137 ExtensionRegistry::EVERYTHING)); |
| 134 } | 138 } |
| 135 | 139 |
| 140 TEST_F(SharedModuleServiceUnitTest, WhitelistedImports) { |
| 141 std::string whitelisted_id = id_util::GenerateId("whitelisted"); |
| 142 std::string nonwhitelisted_id = id_util::GenerateId("nonwhitelisted"); |
| 143 // Create a module which exports to a restricted whitelist. |
| 144 scoped_ptr<base::DictionaryValue> manifest = |
| 145 DictionaryBuilder() |
| 146 .Set("name", "Shared Module") |
| 147 .Set("version", "1.0") |
| 148 .Set("manifest_version", 2) |
| 149 .Set("export", |
| 150 DictionaryBuilder().Set("whitelist", |
| 151 ListBuilder() |
| 152 .Append(whitelisted_id)) |
| 153 .Set("resources", |
| 154 ListBuilder().Append("*"))).Build(); |
| 155 scoped_refptr<Extension> shared_module = |
| 156 ExtensionBuilder().SetManifest(manifest.Pass()) |
| 157 .AddFlags(Extension::FROM_WEBSTORE) |
| 158 .SetID(id_util::GenerateId("shared_module")) |
| 159 .Build(); |
| 160 |
| 161 EXPECT_TRUE(InstallExtension(shared_module)); |
| 162 |
| 163 // Create and install an extension with the whitelisted ID. |
| 164 scoped_refptr<Extension> whitelisted_extension = |
| 165 CreateExtensionImportingModule(shared_module->id(), whitelisted_id); |
| 166 EXPECT_TRUE(InstallExtension(whitelisted_extension)); |
| 167 |
| 168 // Try to install an extension with an ID that is not whitelisted. |
| 169 scoped_refptr<Extension> nonwhitelisted_extension = |
| 170 CreateExtensionImportingModule(shared_module->id(), nonwhitelisted_id); |
| 171 EXPECT_FALSE(InstallExtension(nonwhitelisted_extension)); |
| 172 } |
| 173 |
| 136 } // namespace extensions | 174 } // namespace extensions |
| OLD | NEW |