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
|
// Copyright (c) 2013 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/path_service.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/extension_service_unittest.h"
#include "chrome/browser/extensions/unpacked_installer.h"
#include "chrome/browser/managed_mode/custodian_profile_downloader_service.h"
#include "chrome/browser/managed_mode/custodian_profile_downloader_service_factory.h"
#include "chrome/browser/managed_mode/managed_user_service.h"
#include "chrome/browser/managed_mode/managed_user_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/features/feature_channel.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_utils.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/manifest_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::MessageLoopRunner;
namespace {
void OnProfileDownloadedFail(const base::string16& full_name) {
ASSERT_TRUE(false) << "Profile download should not have succeeded.";
}
class ManagedModeURLFilterObserver : public ManagedModeURLFilter::Observer {
public:
explicit ManagedModeURLFilterObserver(ManagedModeURLFilter* url_filter)
: url_filter_(url_filter) {
Reset();
url_filter_->AddObserver(this);
}
~ManagedModeURLFilterObserver() {
url_filter_->RemoveObserver(this);
}
void Wait() {
message_loop_runner_->Run();
Reset();
}
// ManagedModeURLFilter::Observer
virtual void OnSiteListUpdated() OVERRIDE {
message_loop_runner_->Quit();
}
private:
void Reset() {
message_loop_runner_ = new MessageLoopRunner;
}
ManagedModeURLFilter* url_filter_;
scoped_refptr<MessageLoopRunner> message_loop_runner_;
};
class ManagedUserServiceTest : public ::testing::Test {
public:
ManagedUserServiceTest() {}
virtual void SetUp() OVERRIDE {
TestingProfile::Builder builder;
builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
FakeProfileOAuth2TokenService::Build);
profile_ = builder.Build();
managed_user_service_ =
ManagedUserServiceFactory::GetForProfile(profile_.get());
}
virtual void TearDown() OVERRIDE {
profile_.reset();
}
virtual ~ManagedUserServiceTest() {}
protected:
content::TestBrowserThreadBundle thread_bundle_;
scoped_ptr<TestingProfile> profile_;
ManagedUserService* managed_user_service_;
};
} // namespace
TEST_F(ManagedUserServiceTest, GetManualExceptionsForHost) {
GURL kExampleFooURL("http://www.example.com/foo");
GURL kExampleBarURL("http://www.example.com/bar");
GURL kExampleFooNoWWWURL("http://example.com/foo");
GURL kBlurpURL("http://blurp.net/bla");
GURL kMooseURL("http://moose.org/baz");
{
DictionaryPrefUpdate update(profile_->GetPrefs(),
prefs::kManagedModeManualURLs);
base::DictionaryValue* dict = update.Get();
dict->SetBooleanWithoutPathExpansion(kExampleFooURL.spec(), true);
dict->SetBooleanWithoutPathExpansion(kExampleBarURL.spec(), false);
dict->SetBooleanWithoutPathExpansion(kExampleFooNoWWWURL.spec(), true);
dict->SetBooleanWithoutPathExpansion(kBlurpURL.spec(), true);
}
EXPECT_EQ(ManagedUserService::MANUAL_ALLOW,
managed_user_service_->GetManualBehaviorForURL(kExampleFooURL));
EXPECT_EQ(ManagedUserService::MANUAL_BLOCK,
managed_user_service_->GetManualBehaviorForURL(kExampleBarURL));
EXPECT_EQ(ManagedUserService::MANUAL_ALLOW,
managed_user_service_->GetManualBehaviorForURL(
kExampleFooNoWWWURL));
EXPECT_EQ(ManagedUserService::MANUAL_ALLOW,
managed_user_service_->GetManualBehaviorForURL(kBlurpURL));
EXPECT_EQ(ManagedUserService::MANUAL_NONE,
managed_user_service_->GetManualBehaviorForURL(kMooseURL));
std::vector<GURL> exceptions;
managed_user_service_->GetManualExceptionsForHost("www.example.com",
&exceptions);
ASSERT_EQ(2u, exceptions.size());
EXPECT_EQ(kExampleBarURL, exceptions[0]);
EXPECT_EQ(kExampleFooURL, exceptions[1]);
{
DictionaryPrefUpdate update(profile_->GetPrefs(),
prefs::kManagedModeManualURLs);
base::DictionaryValue* dict = update.Get();
for (std::vector<GURL>::iterator it = exceptions.begin();
it != exceptions.end(); ++it) {
dict->RemoveWithoutPathExpansion(it->spec(), NULL);
}
}
EXPECT_EQ(ManagedUserService::MANUAL_NONE,
managed_user_service_->GetManualBehaviorForURL(kExampleFooURL));
EXPECT_EQ(ManagedUserService::MANUAL_NONE,
managed_user_service_->GetManualBehaviorForURL(kExampleBarURL));
EXPECT_EQ(ManagedUserService::MANUAL_ALLOW,
managed_user_service_->GetManualBehaviorForURL(
kExampleFooNoWWWURL));
EXPECT_EQ(ManagedUserService::MANUAL_ALLOW,
managed_user_service_->GetManualBehaviorForURL(kBlurpURL));
EXPECT_EQ(ManagedUserService::MANUAL_NONE,
managed_user_service_->GetManualBehaviorForURL(kMooseURL));
}
// Ensure that the CustodianProfileDownloaderService shuts down cleanly. If no
// DCHECK is hit when the service is destroyed, this test passed.
TEST_F(ManagedUserServiceTest, ShutDownCustodianProfileDownloader) {
CustodianProfileDownloaderService* downloader_service =
CustodianProfileDownloaderServiceFactory::GetForProfile(profile_.get());
// Emulate being logged in, then start to download a profile so a
// ProfileDownloader gets created.
profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "Logged In");
downloader_service->DownloadProfile(base::Bind(&OnProfileDownloadedFail));
}
#if !defined(OS_ANDROID)
class ManagedUserServiceExtensionTestBase : public ExtensionServiceTestBase {
public:
explicit ManagedUserServiceExtensionTestBase(bool is_managed)
: is_managed_(is_managed),
channel_(chrome::VersionInfo::CHANNEL_DEV) {}
virtual ~ManagedUserServiceExtensionTestBase() {}
virtual void SetUp() OVERRIDE {
ExtensionServiceTestBase::SetUp();
ExtensionServiceTestBase::ExtensionServiceInitParams params =
CreateDefaultInitParams();
params.profile_is_managed = is_managed_;
InitializeExtensionService(params);
ManagedUserServiceFactory::GetForProfile(profile_.get())->Init();
}
protected:
ScopedVector<ManagedModeSiteList> GetActiveSiteLists(
ManagedUserService* managed_user_service) {
return managed_user_service->GetActiveSiteLists();
}
scoped_refptr<extensions::Extension> MakeThemeExtension() {
scoped_ptr<base::DictionaryValue> source(new base::DictionaryValue());
source->SetString(extensions::manifest_keys::kName, "Theme");
source->Set(extensions::manifest_keys::kTheme, new base::DictionaryValue());
source->SetString(extensions::manifest_keys::kVersion, "1.0");
extensions::ExtensionBuilder builder;
scoped_refptr<extensions::Extension> extension =
builder.SetManifest(source.Pass()).Build();
return extension;
}
scoped_refptr<extensions::Extension> MakeExtension() {
scoped_ptr<base::DictionaryValue> manifest = extensions::DictionaryBuilder()
.Set(extensions::manifest_keys::kName, "Extension")
.Set(extensions::manifest_keys::kVersion, "1.0")
.Build();
extensions::ExtensionBuilder builder;
scoped_refptr<extensions::Extension> extension =
builder.SetManifest(manifest.Pass()).Build();
return extension;
}
bool is_managed_;
extensions::ScopedCurrentChannel channel_;
};
class ManagedUserServiceExtensionTestUnmanaged
: public ManagedUserServiceExtensionTestBase {
public:
ManagedUserServiceExtensionTestUnmanaged()
: ManagedUserServiceExtensionTestBase(false) {}
};
class ManagedUserServiceExtensionTest
: public ManagedUserServiceExtensionTestBase {
public:
ManagedUserServiceExtensionTest()
: ManagedUserServiceExtensionTestBase(true) {}
};
TEST_F(ManagedUserServiceExtensionTestUnmanaged,
ExtensionManagementPolicyProvider) {
ManagedUserService* managed_user_service =
ManagedUserServiceFactory::GetForProfile(profile_.get());
EXPECT_FALSE(profile_->IsManaged());
scoped_refptr<extensions::Extension> extension = MakeExtension();
base::string16 error_1;
EXPECT_TRUE(managed_user_service->UserMayLoad(extension.get(), &error_1));
EXPECT_EQ(base::string16(), error_1);
base::string16 error_2;
EXPECT_TRUE(
managed_user_service->UserMayModifySettings(extension.get(), &error_2));
EXPECT_EQ(base::string16(), error_2);
}
TEST_F(ManagedUserServiceExtensionTest, ExtensionManagementPolicyProvider) {
ManagedUserService* managed_user_service =
ManagedUserServiceFactory::GetForProfile(profile_.get());
ManagedModeURLFilterObserver observer(
managed_user_service->GetURLFilterForUIThread());
ASSERT_TRUE(profile_->IsManaged());
// Wait for the initial update to finish (otherwise we'll get leaks).
observer.Wait();
// Check that a supervised user can install a theme.
scoped_refptr<extensions::Extension> theme = MakeThemeExtension();
base::string16 error_1;
EXPECT_TRUE(managed_user_service->UserMayLoad(theme.get(), &error_1));
EXPECT_TRUE(error_1.empty());
EXPECT_TRUE(
managed_user_service->UserMayModifySettings(theme.get(), &error_1));
EXPECT_TRUE(error_1.empty());
// Now check a different kind of extension.
scoped_refptr<extensions::Extension> extension = MakeExtension();
EXPECT_FALSE(managed_user_service->UserMayLoad(extension.get(), &error_1));
EXPECT_FALSE(error_1.empty());
base::string16 error_2;
EXPECT_FALSE(
managed_user_service->UserMayModifySettings(extension.get(), &error_2));
EXPECT_FALSE(error_2.empty());
#ifndef NDEBUG
EXPECT_FALSE(managed_user_service->GetDebugPolicyProviderName().empty());
#endif
}
TEST_F(ManagedUserServiceExtensionTest, NoContentPacks) {
ManagedUserService* managed_user_service =
ManagedUserServiceFactory::GetForProfile(profile_.get());
ManagedModeURLFilter* url_filter =
managed_user_service->GetURLFilterForUIThread();
GURL url("http://youtube.com");
ScopedVector<ManagedModeSiteList> site_lists =
GetActiveSiteLists(managed_user_service);
ASSERT_EQ(0u, site_lists.size());
EXPECT_EQ(ManagedModeURLFilter::ALLOW,
url_filter->GetFilteringBehaviorForURL(url));
}
TEST_F(ManagedUserServiceExtensionTest, InstallContentPacks) {
ManagedUserService* managed_user_service =
ManagedUserServiceFactory::GetForProfile(profile_.get());
ManagedModeURLFilter* url_filter =
managed_user_service->GetURLFilterForUIThread();
ManagedModeURLFilterObserver observer(url_filter);
observer.Wait();
GURL example_url("http://example.com");
GURL moose_url("http://moose.org");
EXPECT_EQ(ManagedModeURLFilter::ALLOW,
url_filter->GetFilteringBehaviorForURL(example_url));
profile_->GetPrefs()->SetInteger(prefs::kDefaultManagedModeFilteringBehavior,
ManagedModeURLFilter::BLOCK);
EXPECT_EQ(ManagedModeURLFilter::BLOCK,
url_filter->GetFilteringBehaviorForURL(example_url));
profile_->GetPrefs()->SetInteger(prefs::kDefaultManagedModeFilteringBehavior,
ManagedModeURLFilter::WARN);
EXPECT_EQ(ManagedModeURLFilter::WARN,
url_filter->GetFilteringBehaviorForURL(example_url));
managed_user_service->set_elevated_for_testing(true);
// Load a content pack.
scoped_refptr<extensions::UnpackedInstaller> installer(
extensions::UnpackedInstaller::Create(service_));
installer->set_prompt_for_plugins(false);
base::FilePath test_data_dir;
ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
base::FilePath extension_path =
test_data_dir.AppendASCII("extensions/managed_mode/content_pack");
content::WindowedNotificationObserver extension_load_observer(
chrome::NOTIFICATION_EXTENSION_LOADED,
content::Source<Profile>(profile_.get()));
installer->Load(extension_path);
extension_load_observer.Wait();
observer.Wait();
content::Details<extensions::Extension> details =
extension_load_observer.details();
scoped_refptr<extensions::Extension> extension =
make_scoped_refptr(details.ptr());
ASSERT_TRUE(extension.get());
ScopedVector<ManagedModeSiteList> site_lists =
GetActiveSiteLists(managed_user_service);
ASSERT_EQ(1u, site_lists.size());
std::vector<ManagedModeSiteList::Site> sites;
site_lists[0]->GetSites(&sites);
ASSERT_EQ(3u, sites.size());
EXPECT_EQ(base::ASCIIToUTF16("YouTube"), sites[0].name);
EXPECT_EQ(base::ASCIIToUTF16("Homestar Runner"), sites[1].name);
EXPECT_EQ(base::string16(), sites[2].name);
EXPECT_EQ(ManagedModeURLFilter::ALLOW,
url_filter->GetFilteringBehaviorForURL(example_url));
EXPECT_EQ(ManagedModeURLFilter::WARN,
url_filter->GetFilteringBehaviorForURL(moose_url));
// Load a second content pack.
installer = extensions::UnpackedInstaller::Create(service_);
extension_path =
test_data_dir.AppendASCII("extensions/managed_mode/content_pack_2");
installer->Load(extension_path);
observer.Wait();
site_lists = GetActiveSiteLists(managed_user_service);
ASSERT_EQ(2u, site_lists.size());
sites.clear();
site_lists[0]->GetSites(&sites);
site_lists[1]->GetSites(&sites);
ASSERT_EQ(4u, sites.size());
// The site lists might be returned in any order, so we put them into a set.
std::set<std::string> site_names;
for (std::vector<ManagedModeSiteList::Site>::const_iterator it =
sites.begin(); it != sites.end(); ++it) {
site_names.insert(base::UTF16ToUTF8(it->name));
}
EXPECT_TRUE(site_names.count("YouTube") == 1u);
EXPECT_TRUE(site_names.count("Homestar Runner") == 1u);
EXPECT_TRUE(site_names.count(std::string()) == 1u);
EXPECT_TRUE(site_names.count("Moose") == 1u);
EXPECT_EQ(ManagedModeURLFilter::ALLOW,
url_filter->GetFilteringBehaviorForURL(example_url));
EXPECT_EQ(ManagedModeURLFilter::ALLOW,
url_filter->GetFilteringBehaviorForURL(moose_url));
// Disable the first content pack.
service_->DisableExtension(extension->id(),
extensions::Extension::DISABLE_USER_ACTION);
observer.Wait();
site_lists = GetActiveSiteLists(managed_user_service);
ASSERT_EQ(1u, site_lists.size());
sites.clear();
site_lists[0]->GetSites(&sites);
ASSERT_EQ(1u, sites.size());
EXPECT_EQ(base::ASCIIToUTF16("Moose"), sites[0].name);
EXPECT_EQ(ManagedModeURLFilter::WARN,
url_filter->GetFilteringBehaviorForURL(example_url));
EXPECT_EQ(ManagedModeURLFilter::ALLOW,
url_filter->GetFilteringBehaviorForURL(moose_url));
}
#endif // !defined(OS_ANDROID)
|