1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string16.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_service_test_base.h"
#include "chrome/browser/extensions/pending_extension_manager.h"
#include "chrome/browser/extensions/shared_module_service.h"
#include "chrome/common/extensions/features/feature_channel.h"
#include "components/crx_file/id_util.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/install_flag.h"
#include "extensions/browser/uninstall_reason.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/value_builder.h"
#include "sync/api/string_ordinal.h"
namespace extensions {
namespace {
// Return an extension with |id| which imports a module with the given
// |import_id|.
scoped_refptr<Extension> CreateExtensionImportingModule(
const std::string& import_id,
const std::string& id,
const std::string& version) {
DictionaryBuilder builder;
builder.Set("name", "Has Dependent Modules")
.Set("version", version)
.Set("manifest_version", 2);
if (!import_id.empty()) {
builder.Set("import",
ListBuilder().Append(DictionaryBuilder().Set("id", import_id)));
}
scoped_ptr<base::DictionaryValue> manifest = builder.Build();
return ExtensionBuilder().SetManifest(manifest.Pass())
.AddFlags(Extension::FROM_WEBSTORE)
.SetID(id)
.Build();
}
} // namespace
class SharedModuleServiceUnitTest : public ExtensionServiceTestBase {
public:
SharedModuleServiceUnitTest() :
// The "export" key is open for dev-channel only, but unit tests
// run as stable channel on the official Windows build.
current_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {}
protected:
virtual void SetUp() override;
// Install an extension and notify the ExtensionService.
testing::AssertionResult InstallExtension(const Extension* extension,
bool is_update);
ScopedCurrentChannel current_channel_;
};
void SharedModuleServiceUnitTest::SetUp() {
ExtensionServiceTestBase::SetUp();
InitializeGoodInstalledExtensionService();
service()->Init();
}
testing::AssertionResult SharedModuleServiceUnitTest::InstallExtension(
const Extension* extension,
bool is_update) {
const Extension* old = registry()->GetExtensionById(
extension->id(),
ExtensionRegistry::ENABLED);
// Verify the extension is not already installed, if it is not update.
if (!is_update) {
if (old)
return testing::AssertionFailure() << "Extension already installed.";
} else {
if (!old)
return testing::AssertionFailure() << "The extension does not exist.";
}
// Notify the service that the extension is installed. This adds it to the
// registry, notifies interested parties, etc.
service()->OnExtensionInstalled(
extension, syncer::StringOrdinal(), kInstallFlagInstallImmediately);
// Verify that the extension is now installed.
if (!registry()->GetExtensionById(extension->id(),
ExtensionRegistry::ENABLED)) {
return testing::AssertionFailure() << "Could not install extension.";
}
return testing::AssertionSuccess();
}
TEST_F(SharedModuleServiceUnitTest, AddDependentSharedModules) {
// Create an extension that has a dependency.
std::string import_id = crx_file::id_util::GenerateId("id");
std::string extension_id = crx_file::id_util::GenerateId("extension_id");
scoped_refptr<Extension> extension =
CreateExtensionImportingModule(import_id, extension_id, "1.0");
PendingExtensionManager* pending_extension_manager =
service()->pending_extension_manager();
// Verify that we don't currently want to install the imported module.
EXPECT_FALSE(pending_extension_manager->IsIdPending(import_id));
// Try to satisfy imports for the extension. This should queue the imported
// module's installation.
service()->shared_module_service()->SatisfyImports(extension.get());
EXPECT_TRUE(pending_extension_manager->IsIdPending(import_id));
}
TEST_F(SharedModuleServiceUnitTest, PruneSharedModulesOnUninstall) {
// Create a module which exports a resource, and install it.
scoped_ptr<base::DictionaryValue> manifest =
DictionaryBuilder()
.Set("name", "Shared Module")
.Set("version", "1.0")
.Set("manifest_version", 2)
.Set("export",
DictionaryBuilder().Set("resources",
ListBuilder().Append("foo.js"))).Build();
scoped_refptr<Extension> shared_module =
ExtensionBuilder()
.SetManifest(manifest.Pass())
.AddFlags(Extension::FROM_WEBSTORE)
.SetID(crx_file::id_util::GenerateId("shared_module"))
.Build();
EXPECT_TRUE(InstallExtension(shared_module.get(), false));
std::string extension_id = crx_file::id_util::GenerateId("extension_id");
// Create and install an extension that imports our new module.
scoped_refptr<Extension> importing_extension =
CreateExtensionImportingModule(shared_module->id(), extension_id, "1.0");
EXPECT_TRUE(InstallExtension(importing_extension.get(), false));
// Uninstall the extension that imports our module.
base::string16 error;
service()->UninstallExtension(importing_extension->id(),
extensions::UNINSTALL_REASON_FOR_TESTING,
base::Bind(&base::DoNothing),
&error);
EXPECT_TRUE(error.empty());
// Since the module was only referenced by that single extension, it should
// have been uninstalled as a side-effect of uninstalling the extension that
// depended upon it.
EXPECT_FALSE(registry()->GetExtensionById(shared_module->id(),
ExtensionRegistry::EVERYTHING));
}
TEST_F(SharedModuleServiceUnitTest, PruneSharedModulesOnUpdate) {
// Create two modules which export a resource, and install them.
scoped_ptr<base::DictionaryValue> manifest_1 =
DictionaryBuilder()
.Set("name", "Shared Module 1")
.Set("version", "1.0")
.Set("manifest_version", 2)
.Set("export",
DictionaryBuilder().Set("resources",
ListBuilder().Append("foo.js"))).Build();
scoped_refptr<Extension> shared_module_1 =
ExtensionBuilder()
.SetManifest(manifest_1.Pass())
.AddFlags(Extension::FROM_WEBSTORE)
.SetID(crx_file::id_util::GenerateId("shared_module_1"))
.Build();
EXPECT_TRUE(InstallExtension(shared_module_1.get(), false));
scoped_ptr<base::DictionaryValue> manifest_2 =
DictionaryBuilder()
.Set("name", "Shared Module 2")
.Set("version", "1.0")
.Set("manifest_version", 2)
.Set("export",
DictionaryBuilder().Set("resources",
ListBuilder().Append("foo.js"))).Build();
scoped_refptr<Extension> shared_module_2 =
ExtensionBuilder()
.SetManifest(manifest_2.Pass())
.AddFlags(Extension::FROM_WEBSTORE)
.SetID(crx_file::id_util::GenerateId("shared_module_2"))
.Build();
EXPECT_TRUE(InstallExtension(shared_module_2.get(), false));
std::string extension_id = crx_file::id_util::GenerateId("extension_id");
// Create and install an extension v1.0 that imports our new module 1.
scoped_refptr<Extension> importing_extension_1 =
CreateExtensionImportingModule(shared_module_1->id(),
extension_id,
"1.0");
EXPECT_TRUE(InstallExtension(importing_extension_1.get(), false));
// Create and install a new version of the extension that imports our new
// module 2.
scoped_refptr<Extension> importing_extension_2 =
CreateExtensionImportingModule(shared_module_2->id(),
extension_id,
"1.1");
EXPECT_TRUE(InstallExtension(importing_extension_2.get(), true));
// Since the extension v1.1 depends the module 2 insteand module 1.
// So the module 1 should be uninstalled.
EXPECT_FALSE(registry()->GetExtensionById(shared_module_1->id(),
ExtensionRegistry::EVERYTHING));
EXPECT_TRUE(registry()->GetExtensionById(shared_module_2->id(),
ExtensionRegistry::EVERYTHING));
// Create and install a new version of the extension that does not import any
// module.
scoped_refptr<Extension> importing_extension_3 =
CreateExtensionImportingModule("", extension_id, "1.2");
EXPECT_TRUE(InstallExtension(importing_extension_3.get(), true));
// Since the extension v1.2 does not depend any module, so the all models
// should have been uninstalled.
EXPECT_FALSE(registry()->GetExtensionById(shared_module_1->id(),
ExtensionRegistry::EVERYTHING));
EXPECT_FALSE(registry()->GetExtensionById(shared_module_2->id(),
ExtensionRegistry::EVERYTHING));
}
TEST_F(SharedModuleServiceUnitTest, WhitelistedImports) {
std::string whitelisted_id = crx_file::id_util::GenerateId("whitelisted");
std::string nonwhitelisted_id =
crx_file::id_util::GenerateId("nonwhitelisted");
// Create a module which exports to a restricted whitelist.
scoped_ptr<base::DictionaryValue> manifest =
DictionaryBuilder()
.Set("name", "Shared Module")
.Set("version", "1.0")
.Set("manifest_version", 2)
.Set("export",
DictionaryBuilder().Set("whitelist",
ListBuilder()
.Append(whitelisted_id))
.Set("resources",
ListBuilder().Append("*"))).Build();
scoped_refptr<Extension> shared_module =
ExtensionBuilder()
.SetManifest(manifest.Pass())
.AddFlags(Extension::FROM_WEBSTORE)
.SetID(crx_file::id_util::GenerateId("shared_module"))
.Build();
EXPECT_TRUE(InstallExtension(shared_module.get(), false));
// Create and install an extension with the whitelisted ID.
scoped_refptr<Extension> whitelisted_extension =
CreateExtensionImportingModule(shared_module->id(),
whitelisted_id,
"1.0");
EXPECT_TRUE(InstallExtension(whitelisted_extension.get(), false));
// Try to install an extension with an ID that is not whitelisted.
scoped_refptr<Extension> nonwhitelisted_extension =
CreateExtensionImportingModule(shared_module->id(),
nonwhitelisted_id,
"1.0");
// This should succeed because only CRX installer (and by extension the
// WebStore Installer) checks the shared module whitelist. InstallExtension
// bypasses the whitelist check because the SharedModuleService does not
// care about whitelists.
EXPECT_TRUE(InstallExtension(nonwhitelisted_extension.get(), false));
}
} // namespace extensions
|