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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
// 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 <algorithm>
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/testing_pref_service.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_management.h"
#include "chrome/browser/extensions/external_policy_loader.h"
#include "extensions/browser/pref_names.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/url_pattern.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace extensions {
namespace {
const char kTargetExtension[] = "abcdefghijklmnopabcdefghijklmnop";
const char kOtherExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const char kExampleUpdateUrl[] = "http://example.com/update_url";
} // namespace
class ExtensionManagementTest : public testing::Test {
public:
ExtensionManagementTest() {}
virtual ~ExtensionManagementTest() {}
// testing::Test:
virtual void SetUp() OVERRIDE {
InitPrefService();
}
void InitPrefService() {
extension_management_.reset();
pref_service_.reset(new TestingPrefServiceSimple());
pref_service_->registry()->RegisterListPref(
pref_names::kAllowedInstallSites);
pref_service_->registry()->RegisterListPref(pref_names::kAllowedTypes);
pref_service_->registry()->RegisterListPref(pref_names::kInstallDenyList);
pref_service_->registry()->RegisterListPref(pref_names::kInstallAllowList);
pref_service_->registry()->RegisterDictionaryPref(
pref_names::kInstallForceList);
extension_management_.reset(new ExtensionManagement(pref_service_.get()));
}
void SetPref(bool managed, const char* path, base::Value* value) {
if (managed)
pref_service_->SetManagedPref(path, value);
else
pref_service_->SetUserPref(path, value);
}
void RemovePref(bool managed, const char* path) {
if (managed)
pref_service_->RemoveManagedPref(path);
else
pref_service_->RemoveUserPref(path);
}
protected:
scoped_ptr<TestingPrefServiceSimple> pref_service_;
scoped_ptr<ExtensionManagement> extension_management_;
};
class ExtensionAdminPolicyTest : public ExtensionManagementTest {
public:
ExtensionAdminPolicyTest() {}
virtual ~ExtensionAdminPolicyTest() {}
void CreateExtension(Manifest::Location location) {
base::DictionaryValue values;
CreateExtensionFromValues(location, &values);
}
void CreateHostedApp(Manifest::Location location) {
base::DictionaryValue values;
values.Set(extensions::manifest_keys::kWebURLs, new base::ListValue());
values.SetString(extensions::manifest_keys::kLaunchWebURL,
"http://www.example.com");
CreateExtensionFromValues(location, &values);
}
void CreateExtensionFromValues(Manifest::Location location,
base::DictionaryValue* values) {
values->SetString(extensions::manifest_keys::kName, "test");
values->SetString(extensions::manifest_keys::kVersion, "0.1");
std::string error;
extension_ = Extension::Create(base::FilePath(), location, *values,
Extension::NO_FLAGS, &error);
ASSERT_TRUE(extension_.get());
}
// Wrappers for legacy admin policy functions, for testing purpose only.
bool BlacklistedByDefault(const base::ListValue* blacklist);
bool UserMayLoad(const base::ListValue* blacklist,
const base::ListValue* whitelist,
const base::DictionaryValue* forcelist,
const base::ListValue* allowed_types,
const Extension* extension,
base::string16* error);
bool UserMayModifySettings(const Extension* extension, base::string16* error);
bool MustRemainEnabled(const Extension* extension, base::string16* error);
protected:
scoped_refptr<Extension> extension_;
};
bool ExtensionAdminPolicyTest::BlacklistedByDefault(
const base::ListValue* blacklist) {
InitPrefService();
if (blacklist)
SetPref(true, pref_names::kInstallDenyList, blacklist->DeepCopy());
return extension_management_->BlacklistedByDefault();
}
bool ExtensionAdminPolicyTest::UserMayLoad(
const base::ListValue* blacklist,
const base::ListValue* whitelist,
const base::DictionaryValue* forcelist,
const base::ListValue* allowed_types,
const Extension* extension,
base::string16* error) {
InitPrefService();
if (blacklist)
SetPref(true, pref_names::kInstallDenyList, blacklist->DeepCopy());
if (whitelist)
SetPref(true, pref_names::kInstallAllowList, whitelist->DeepCopy());
if (forcelist)
SetPref(true, pref_names::kInstallForceList, forcelist->DeepCopy());
if (allowed_types)
SetPref(true, pref_names::kAllowedTypes, allowed_types->DeepCopy());
return extension_management_->GetProvider()->UserMayLoad(extension, error);
}
bool ExtensionAdminPolicyTest::UserMayModifySettings(const Extension* extension,
base::string16* error) {
InitPrefService();
return extension_management_->GetProvider()->UserMayModifySettings(extension,
error);
}
bool ExtensionAdminPolicyTest::MustRemainEnabled(const Extension* extension,
base::string16* error) {
InitPrefService();
return extension_management_->GetProvider()->MustRemainEnabled(extension,
error);
}
// Verify that preference controlled by legacy ExtensionInstallSources policy is
// handled well.
TEST_F(ExtensionManagementTest, LegacyInstallSources) {
base::ListValue allowed_sites_pref;
allowed_sites_pref.AppendString("https://www.example.com/foo");
allowed_sites_pref.AppendString("https://corp.mycompany.com/*");
SetPref(
true, pref_names::kAllowedInstallSites, allowed_sites_pref.DeepCopy());
const URLPatternSet& allowed_sites =
extension_management_->ReadGlobalSettings().install_sources;
ASSERT_TRUE(extension_management_->ReadGlobalSettings()
.has_restricted_install_sources);
EXPECT_FALSE(allowed_sites.is_empty());
EXPECT_TRUE(allowed_sites.MatchesURL(GURL("https://www.example.com/foo")));
EXPECT_FALSE(allowed_sites.MatchesURL(GURL("https://www.example.com/bar")));
EXPECT_TRUE(
allowed_sites.MatchesURL(GURL("https://corp.mycompany.com/entry")));
EXPECT_FALSE(
allowed_sites.MatchesURL(GURL("https://www.mycompany.com/entry")));
}
// Verify that preference controlled by legacy ExtensionAllowedTypes policy is
// handled well.
TEST_F(ExtensionManagementTest, LegacyAllowedTypes) {
base::ListValue allowed_types_pref;
allowed_types_pref.AppendInteger(Manifest::TYPE_THEME);
allowed_types_pref.AppendInteger(Manifest::TYPE_USER_SCRIPT);
SetPref(true, pref_names::kAllowedTypes, allowed_types_pref.DeepCopy());
const std::vector<Manifest::Type>& allowed_types =
extension_management_->ReadGlobalSettings().allowed_types;
ASSERT_TRUE(
extension_management_->ReadGlobalSettings().has_restricted_allowed_types);
EXPECT_TRUE(allowed_types.size() == 2);
EXPECT_FALSE(std::find(allowed_types.begin(),
allowed_types.end(),
Manifest::TYPE_EXTENSION) != allowed_types.end());
EXPECT_TRUE(std::find(allowed_types.begin(),
allowed_types.end(),
Manifest::TYPE_THEME) != allowed_types.end());
EXPECT_TRUE(std::find(allowed_types.begin(),
allowed_types.end(),
Manifest::TYPE_USER_SCRIPT) != allowed_types.end());
}
// Verify that preference controlled by legacy ExtensionInstallBlacklist policy
// is handled well.
TEST_F(ExtensionManagementTest, LegacyInstallBlacklist) {
base::ListValue denied_list_pref;
denied_list_pref.AppendString(kTargetExtension);
SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy());
EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
ExtensionManagement::INSTALLATION_BLOCKED);
EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
ExtensionManagement::INSTALLATION_ALLOWED);
}
// Verify that preference controlled by legacy ExtensionInstallWhitelist policy
// is handled well.
TEST_F(ExtensionManagementTest, LegacyInstallWhitelist) {
base::ListValue denied_list_pref;
denied_list_pref.AppendString("*");
base::ListValue allowed_list_pref;
allowed_list_pref.AppendString(kTargetExtension);
SetPref(true, pref_names::kInstallDenyList, denied_list_pref.DeepCopy());
SetPref(true, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy());
EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
ExtensionManagement::INSTALLATION_ALLOWED);
EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
ExtensionManagement::INSTALLATION_BLOCKED);
// Verify that install whitelist preference set by user is ignored.
RemovePref(true, pref_names::kInstallAllowList);
SetPref(false, pref_names::kInstallAllowList, allowed_list_pref.DeepCopy());
EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
ExtensionManagement::INSTALLATION_BLOCKED);
}
// Verify that preference controlled by legacy ExtensionInstallForcelist policy
// is handled well.
TEST_F(ExtensionManagementTest, LegacyInstallForcelist) {
base::DictionaryValue forced_list_pref;
ExternalPolicyLoader::AddExtension(
&forced_list_pref, kTargetExtension, kExampleUpdateUrl);
SetPref(true, pref_names::kInstallForceList, forced_list_pref.DeepCopy());
EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
ExtensionManagement::INSTALLATION_FORCED);
EXPECT_EQ(extension_management_->ReadById(kTargetExtension).update_url,
kExampleUpdateUrl);
EXPECT_EQ(extension_management_->ReadById(kOtherExtension).installation_mode,
ExtensionManagement::INSTALLATION_ALLOWED);
// Verify that install forcelist preference set by user is ignored.
RemovePref(true, pref_names::kInstallForceList);
SetPref(false, pref_names::kInstallForceList, forced_list_pref.DeepCopy());
EXPECT_EQ(extension_management_->ReadById(kTargetExtension).installation_mode,
ExtensionManagement::INSTALLATION_ALLOWED);
}
// Tests the flag value indicating that extensions are blacklisted by default.
TEST_F(ExtensionAdminPolicyTest, BlacklistedByDefault) {
EXPECT_FALSE(BlacklistedByDefault(NULL));
base::ListValue blacklist;
blacklist.Append(new base::StringValue(kOtherExtension));
EXPECT_FALSE(BlacklistedByDefault(&blacklist));
blacklist.Append(new base::StringValue("*"));
EXPECT_TRUE(BlacklistedByDefault(&blacklist));
blacklist.Clear();
blacklist.Append(new base::StringValue("*"));
EXPECT_TRUE(BlacklistedByDefault(&blacklist));
}
// Tests UserMayLoad for required extensions.
TEST_F(ExtensionAdminPolicyTest, UserMayLoadRequired) {
CreateExtension(Manifest::COMPONENT);
EXPECT_TRUE(UserMayLoad(NULL, NULL, NULL, NULL, extension_.get(), NULL));
base::string16 error;
EXPECT_TRUE(UserMayLoad(NULL, NULL, NULL, NULL, extension_.get(), &error));
EXPECT_TRUE(error.empty());
// Required extensions may load even if they're on the blacklist.
base::ListValue blacklist;
blacklist.Append(new base::StringValue(extension_->id()));
EXPECT_TRUE(
UserMayLoad(&blacklist, NULL, NULL, NULL, extension_.get(), NULL));
blacklist.Append(new base::StringValue("*"));
EXPECT_TRUE(
UserMayLoad(&blacklist, NULL, NULL, NULL, extension_.get(), NULL));
}
// Tests UserMayLoad when no blacklist exists, or it's empty.
TEST_F(ExtensionAdminPolicyTest, UserMayLoadNoBlacklist) {
CreateExtension(Manifest::INTERNAL);
EXPECT_TRUE(UserMayLoad(NULL, NULL, NULL, NULL, extension_.get(), NULL));
base::ListValue blacklist;
EXPECT_TRUE(
UserMayLoad(&blacklist, NULL, NULL, NULL, extension_.get(), NULL));
base::string16 error;
EXPECT_TRUE(
UserMayLoad(&blacklist, NULL, NULL, NULL, extension_.get(), &error));
EXPECT_TRUE(error.empty());
}
// Tests UserMayLoad for an extension on the whitelist.
TEST_F(ExtensionAdminPolicyTest, UserMayLoadWhitelisted) {
CreateExtension(Manifest::INTERNAL);
base::ListValue whitelist;
whitelist.Append(new base::StringValue(extension_->id()));
EXPECT_TRUE(
UserMayLoad(NULL, &whitelist, NULL, NULL, extension_.get(), NULL));
base::ListValue blacklist;
blacklist.Append(new base::StringValue(extension_->id()));
EXPECT_TRUE(
UserMayLoad(NULL, &whitelist, NULL, NULL, extension_.get(), NULL));
base::string16 error;
EXPECT_TRUE(
UserMayLoad(NULL, &whitelist, NULL, NULL, extension_.get(), &error));
EXPECT_TRUE(error.empty());
}
// Tests UserMayLoad for an extension on the blacklist.
TEST_F(ExtensionAdminPolicyTest, UserMayLoadBlacklisted) {
CreateExtension(Manifest::INTERNAL);
// Blacklisted by default.
base::ListValue blacklist;
blacklist.Append(new base::StringValue("*"));
EXPECT_FALSE(
UserMayLoad(&blacklist, NULL, NULL, NULL, extension_.get(), NULL));
base::string16 error;
EXPECT_FALSE(
UserMayLoad(&blacklist, NULL, NULL, NULL, extension_.get(), &error));
EXPECT_FALSE(error.empty());
// Extension on the blacklist, with and without wildcard.
blacklist.Append(new base::StringValue(extension_->id()));
EXPECT_FALSE(
UserMayLoad(&blacklist, NULL, NULL, NULL, extension_.get(), NULL));
blacklist.Clear();
blacklist.Append(new base::StringValue(extension_->id()));
EXPECT_FALSE(
UserMayLoad(&blacklist, NULL, NULL, NULL, extension_.get(), NULL));
// With a whitelist. There's no such thing as a whitelist wildcard.
base::ListValue whitelist;
whitelist.Append(new base::StringValue("behllobkkfkfnphdnhnkndlbkcpglgmj"));
EXPECT_FALSE(
UserMayLoad(&blacklist, &whitelist, NULL, NULL, extension_.get(), NULL));
whitelist.Append(new base::StringValue("*"));
EXPECT_FALSE(
UserMayLoad(&blacklist, &whitelist, NULL, NULL, extension_.get(), NULL));
}
TEST_F(ExtensionAdminPolicyTest, UserMayLoadAllowedTypes) {
CreateExtension(Manifest::INTERNAL);
EXPECT_TRUE(UserMayLoad(NULL, NULL, NULL, NULL, extension_.get(), NULL));
base::ListValue allowed_types;
EXPECT_FALSE(
UserMayLoad(NULL, NULL, NULL, &allowed_types, extension_.get(), NULL));
allowed_types.AppendInteger(Manifest::TYPE_EXTENSION);
EXPECT_TRUE(
UserMayLoad(NULL, NULL, NULL, &allowed_types, extension_.get(), NULL));
CreateHostedApp(Manifest::INTERNAL);
EXPECT_FALSE(
UserMayLoad(NULL, NULL, NULL, &allowed_types, extension_.get(), NULL));
CreateHostedApp(Manifest::EXTERNAL_POLICY_DOWNLOAD);
EXPECT_FALSE(
UserMayLoad(NULL, NULL, NULL, &allowed_types, extension_.get(), NULL));
}
TEST_F(ExtensionAdminPolicyTest, UserMayModifySettings) {
CreateExtension(Manifest::INTERNAL);
EXPECT_TRUE(UserMayModifySettings(extension_.get(), NULL));
base::string16 error;
EXPECT_TRUE(UserMayModifySettings(extension_.get(), &error));
EXPECT_TRUE(error.empty());
CreateExtension(Manifest::EXTERNAL_POLICY_DOWNLOAD);
error.clear();
EXPECT_FALSE(UserMayModifySettings(extension_.get(), NULL));
EXPECT_FALSE(UserMayModifySettings(extension_.get(), &error));
EXPECT_FALSE(error.empty());
}
TEST_F(ExtensionAdminPolicyTest, MustRemainEnabled) {
CreateExtension(Manifest::EXTERNAL_POLICY_DOWNLOAD);
EXPECT_TRUE(MustRemainEnabled(extension_.get(), NULL));
base::string16 error;
EXPECT_TRUE(MustRemainEnabled(extension_.get(), &error));
EXPECT_FALSE(error.empty());
CreateExtension(Manifest::INTERNAL);
error.clear();
EXPECT_FALSE(MustRemainEnabled(extension_.get(), NULL));
EXPECT_FALSE(MustRemainEnabled(extension_.get(), &error));
EXPECT_TRUE(error.empty());
}
} // namespace extensions
|