1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/path_service.h"
6 #include "base/prefs/pref_service.h"
7 #include "base/prefs/scoped_user_pref_update.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/extension_service_unittest.h"
11 #include "chrome/browser/extensions/unpacked_installer.h"
12 #include "chrome/browser/managed_mode/custodian_profile_downloader_service.h"
13 #include "chrome/browser/managed_mode/custodian_profile_downloader_service_factory.h"
14 #include "chrome/browser/managed_mode/managed_user_service.h"
15 #include "chrome/browser/managed_mode/managed_user_service_factory.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
18 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
19 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
20 #include "chrome/browser/ui/browser_list.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/extensions/features/feature_channel.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "content/public/test/test_utils.h"
27 #include "extensions/common/extension.h"
28 #include "extensions/common/extension_builder.h"
29 #include "extensions/common/manifest_constants.h"
30 #include "testing/gtest/include/gtest/gtest.h"
32 using content::MessageLoopRunner
;
36 void OnProfileDownloadedFail(const base::string16
& full_name
) {
37 ASSERT_TRUE(false) << "Profile download should not have succeeded.";
40 class ManagedModeURLFilterObserver
: public ManagedModeURLFilter::Observer
{
42 explicit ManagedModeURLFilterObserver(ManagedModeURLFilter
* url_filter
)
43 : url_filter_(url_filter
) {
45 url_filter_
->AddObserver(this);
48 ~ManagedModeURLFilterObserver() {
49 url_filter_
->RemoveObserver(this);
53 message_loop_runner_
->Run();
57 // ManagedModeURLFilter::Observer
58 virtual void OnSiteListUpdated() OVERRIDE
{
59 message_loop_runner_
->Quit();
64 message_loop_runner_
= new MessageLoopRunner
;
67 ManagedModeURLFilter
* url_filter_
;
68 scoped_refptr
<MessageLoopRunner
> message_loop_runner_
;
71 class ManagedUserServiceTest
: public ::testing::Test
{
73 ManagedUserServiceTest() {}
75 virtual void SetUp() OVERRIDE
{
76 TestingProfile::Builder builder
;
77 builder
.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
78 BuildFakeProfileOAuth2TokenService
);
79 profile_
= builder
.Build();
80 managed_user_service_
=
81 ManagedUserServiceFactory::GetForProfile(profile_
.get());
84 virtual void TearDown() OVERRIDE
{
88 virtual ~ManagedUserServiceTest() {}
91 content::TestBrowserThreadBundle thread_bundle_
;
92 scoped_ptr
<TestingProfile
> profile_
;
93 ManagedUserService
* managed_user_service_
;
98 TEST_F(ManagedUserServiceTest
, GetManualExceptionsForHost
) {
99 GURL
kExampleFooURL("http://www.example.com/foo");
100 GURL
kExampleBarURL("http://www.example.com/bar");
101 GURL
kExampleFooNoWWWURL("http://example.com/foo");
102 GURL
kBlurpURL("http://blurp.net/bla");
103 GURL
kMooseURL("http://moose.org/baz");
105 DictionaryPrefUpdate
update(profile_
->GetPrefs(),
106 prefs::kManagedModeManualURLs
);
107 base::DictionaryValue
* dict
= update
.Get();
108 dict
->SetBooleanWithoutPathExpansion(kExampleFooURL
.spec(), true);
109 dict
->SetBooleanWithoutPathExpansion(kExampleBarURL
.spec(), false);
110 dict
->SetBooleanWithoutPathExpansion(kExampleFooNoWWWURL
.spec(), true);
111 dict
->SetBooleanWithoutPathExpansion(kBlurpURL
.spec(), true);
114 EXPECT_EQ(ManagedUserService::MANUAL_ALLOW
,
115 managed_user_service_
->GetManualBehaviorForURL(kExampleFooURL
));
116 EXPECT_EQ(ManagedUserService::MANUAL_BLOCK
,
117 managed_user_service_
->GetManualBehaviorForURL(kExampleBarURL
));
118 EXPECT_EQ(ManagedUserService::MANUAL_ALLOW
,
119 managed_user_service_
->GetManualBehaviorForURL(
120 kExampleFooNoWWWURL
));
121 EXPECT_EQ(ManagedUserService::MANUAL_ALLOW
,
122 managed_user_service_
->GetManualBehaviorForURL(kBlurpURL
));
123 EXPECT_EQ(ManagedUserService::MANUAL_NONE
,
124 managed_user_service_
->GetManualBehaviorForURL(kMooseURL
));
125 std::vector
<GURL
> exceptions
;
126 managed_user_service_
->GetManualExceptionsForHost("www.example.com",
128 ASSERT_EQ(2u, exceptions
.size());
129 EXPECT_EQ(kExampleBarURL
, exceptions
[0]);
130 EXPECT_EQ(kExampleFooURL
, exceptions
[1]);
133 DictionaryPrefUpdate
update(profile_
->GetPrefs(),
134 prefs::kManagedModeManualURLs
);
135 base::DictionaryValue
* dict
= update
.Get();
136 for (std::vector
<GURL
>::iterator it
= exceptions
.begin();
137 it
!= exceptions
.end(); ++it
) {
138 dict
->RemoveWithoutPathExpansion(it
->spec(), NULL
);
142 EXPECT_EQ(ManagedUserService::MANUAL_NONE
,
143 managed_user_service_
->GetManualBehaviorForURL(kExampleFooURL
));
144 EXPECT_EQ(ManagedUserService::MANUAL_NONE
,
145 managed_user_service_
->GetManualBehaviorForURL(kExampleBarURL
));
146 EXPECT_EQ(ManagedUserService::MANUAL_ALLOW
,
147 managed_user_service_
->GetManualBehaviorForURL(
148 kExampleFooNoWWWURL
));
149 EXPECT_EQ(ManagedUserService::MANUAL_ALLOW
,
150 managed_user_service_
->GetManualBehaviorForURL(kBlurpURL
));
151 EXPECT_EQ(ManagedUserService::MANUAL_NONE
,
152 managed_user_service_
->GetManualBehaviorForURL(kMooseURL
));
155 // Ensure that the CustodianProfileDownloaderService shuts down cleanly. If no
156 // DCHECK is hit when the service is destroyed, this test passed.
157 TEST_F(ManagedUserServiceTest
, ShutDownCustodianProfileDownloader
) {
158 CustodianProfileDownloaderService
* downloader_service
=
159 CustodianProfileDownloaderServiceFactory::GetForProfile(profile_
.get());
161 // Emulate being logged in, then start to download a profile so a
162 // ProfileDownloader gets created.
163 profile_
->GetPrefs()->SetString(prefs::kGoogleServicesUsername
, "Logged In");
164 downloader_service
->DownloadProfile(base::Bind(&OnProfileDownloadedFail
));
167 #if !defined(OS_ANDROID)
168 class ManagedUserServiceExtensionTestBase
: public ExtensionServiceTestBase
{
170 explicit ManagedUserServiceExtensionTestBase(bool is_managed
)
171 : is_managed_(is_managed
),
172 channel_(chrome::VersionInfo::CHANNEL_DEV
) {}
173 virtual ~ManagedUserServiceExtensionTestBase() {}
175 virtual void SetUp() OVERRIDE
{
176 ExtensionServiceTestBase::SetUp();
177 ExtensionServiceTestBase::ExtensionServiceInitParams params
=
178 CreateDefaultInitParams();
179 params
.profile_is_managed
= is_managed_
;
180 InitializeExtensionService(params
);
181 ManagedUserServiceFactory::GetForProfile(profile_
.get())->Init();
185 ScopedVector
<ManagedModeSiteList
> GetActiveSiteLists(
186 ManagedUserService
* managed_user_service
) {
187 return managed_user_service
->GetActiveSiteLists();
190 scoped_refptr
<extensions::Extension
> MakeThemeExtension() {
191 scoped_ptr
<base::DictionaryValue
> source(new base::DictionaryValue());
192 source
->SetString(extensions::manifest_keys::kName
, "Theme");
193 source
->Set(extensions::manifest_keys::kTheme
, new base::DictionaryValue());
194 source
->SetString(extensions::manifest_keys::kVersion
, "1.0");
195 extensions::ExtensionBuilder builder
;
196 scoped_refptr
<extensions::Extension
> extension
=
197 builder
.SetManifest(source
.Pass()).Build();
201 scoped_refptr
<extensions::Extension
> MakeExtension() {
202 scoped_ptr
<base::DictionaryValue
> manifest
= extensions::DictionaryBuilder()
203 .Set(extensions::manifest_keys::kName
, "Extension")
204 .Set(extensions::manifest_keys::kVersion
, "1.0")
206 extensions::ExtensionBuilder builder
;
207 scoped_refptr
<extensions::Extension
> extension
=
208 builder
.SetManifest(manifest
.Pass()).Build();
213 extensions::ScopedCurrentChannel channel_
;
216 class ManagedUserServiceExtensionTestUnmanaged
217 : public ManagedUserServiceExtensionTestBase
{
219 ManagedUserServiceExtensionTestUnmanaged()
220 : ManagedUserServiceExtensionTestBase(false) {}
223 class ManagedUserServiceExtensionTest
224 : public ManagedUserServiceExtensionTestBase
{
226 ManagedUserServiceExtensionTest()
227 : ManagedUserServiceExtensionTestBase(true) {}
230 TEST_F(ManagedUserServiceExtensionTestUnmanaged
,
231 ExtensionManagementPolicyProvider
) {
232 ManagedUserService
* managed_user_service
=
233 ManagedUserServiceFactory::GetForProfile(profile_
.get());
234 EXPECT_FALSE(profile_
->IsManaged());
236 scoped_refptr
<extensions::Extension
> extension
= MakeExtension();
237 base::string16 error_1
;
238 EXPECT_TRUE(managed_user_service
->UserMayLoad(extension
.get(), &error_1
));
239 EXPECT_EQ(base::string16(), error_1
);
241 base::string16 error_2
;
243 managed_user_service
->UserMayModifySettings(extension
.get(), &error_2
));
244 EXPECT_EQ(base::string16(), error_2
);
247 TEST_F(ManagedUserServiceExtensionTest
, ExtensionManagementPolicyProvider
) {
248 ManagedUserService
* managed_user_service
=
249 ManagedUserServiceFactory::GetForProfile(profile_
.get());
250 ManagedModeURLFilterObserver
observer(
251 managed_user_service
->GetURLFilterForUIThread());
252 ASSERT_TRUE(profile_
->IsManaged());
253 // Wait for the initial update to finish (otherwise we'll get leaks).
256 // Check that a supervised user can install a theme.
257 scoped_refptr
<extensions::Extension
> theme
= MakeThemeExtension();
258 base::string16 error_1
;
259 EXPECT_TRUE(managed_user_service
->UserMayLoad(theme
.get(), &error_1
));
260 EXPECT_TRUE(error_1
.empty());
262 managed_user_service
->UserMayModifySettings(theme
.get(), &error_1
));
263 EXPECT_TRUE(error_1
.empty());
265 // Now check a different kind of extension.
266 scoped_refptr
<extensions::Extension
> extension
= MakeExtension();
267 EXPECT_FALSE(managed_user_service
->UserMayLoad(extension
.get(), &error_1
));
268 EXPECT_FALSE(error_1
.empty());
270 base::string16 error_2
;
272 managed_user_service
->UserMayModifySettings(extension
.get(), &error_2
));
273 EXPECT_FALSE(error_2
.empty());
276 EXPECT_FALSE(managed_user_service
->GetDebugPolicyProviderName().empty());
280 TEST_F(ManagedUserServiceExtensionTest
, NoContentPacks
) {
281 ManagedUserService
* managed_user_service
=
282 ManagedUserServiceFactory::GetForProfile(profile_
.get());
283 ManagedModeURLFilter
* url_filter
=
284 managed_user_service
->GetURLFilterForUIThread();
286 GURL
url("http://youtube.com");
287 ScopedVector
<ManagedModeSiteList
> site_lists
=
288 GetActiveSiteLists(managed_user_service
);
289 ASSERT_EQ(0u, site_lists
.size());
290 EXPECT_EQ(ManagedModeURLFilter::ALLOW
,
291 url_filter
->GetFilteringBehaviorForURL(url
));
294 TEST_F(ManagedUserServiceExtensionTest
, InstallContentPacks
) {
295 ManagedUserService
* managed_user_service
=
296 ManagedUserServiceFactory::GetForProfile(profile_
.get());
297 ManagedModeURLFilter
* url_filter
=
298 managed_user_service
->GetURLFilterForUIThread();
299 ManagedModeURLFilterObserver
observer(url_filter
);
302 GURL
example_url("http://example.com");
303 GURL
moose_url("http://moose.org");
304 EXPECT_EQ(ManagedModeURLFilter::ALLOW
,
305 url_filter
->GetFilteringBehaviorForURL(example_url
));
307 profile_
->GetPrefs()->SetInteger(prefs::kDefaultManagedModeFilteringBehavior
,
308 ManagedModeURLFilter::BLOCK
);
309 EXPECT_EQ(ManagedModeURLFilter::BLOCK
,
310 url_filter
->GetFilteringBehaviorForURL(example_url
));
312 profile_
->GetPrefs()->SetInteger(prefs::kDefaultManagedModeFilteringBehavior
,
313 ManagedModeURLFilter::WARN
);
314 EXPECT_EQ(ManagedModeURLFilter::WARN
,
315 url_filter
->GetFilteringBehaviorForURL(example_url
));
317 managed_user_service
->set_elevated_for_testing(true);
319 // Load a content pack.
320 scoped_refptr
<extensions::UnpackedInstaller
> installer(
321 extensions::UnpackedInstaller::Create(service_
));
322 installer
->set_prompt_for_plugins(false);
323 base::FilePath test_data_dir
;
324 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
));
325 base::FilePath extension_path
=
326 test_data_dir
.AppendASCII("extensions/managed_mode/content_pack");
327 content::WindowedNotificationObserver
extension_load_observer(
328 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
,
329 content::Source
<Profile
>(profile_
.get()));
330 installer
->Load(extension_path
);
331 extension_load_observer
.Wait();
333 content::Details
<extensions::Extension
> details
=
334 extension_load_observer
.details();
335 scoped_refptr
<extensions::Extension
> extension
=
336 make_scoped_refptr(details
.ptr());
337 ASSERT_TRUE(extension
.get());
339 ScopedVector
<ManagedModeSiteList
> site_lists
=
340 GetActiveSiteLists(managed_user_service
);
341 ASSERT_EQ(1u, site_lists
.size());
342 std::vector
<ManagedModeSiteList::Site
> sites
;
343 site_lists
[0]->GetSites(&sites
);
344 ASSERT_EQ(3u, sites
.size());
345 EXPECT_EQ(base::ASCIIToUTF16("YouTube"), sites
[0].name
);
346 EXPECT_EQ(base::ASCIIToUTF16("Homestar Runner"), sites
[1].name
);
347 EXPECT_EQ(base::string16(), sites
[2].name
);
349 EXPECT_EQ(ManagedModeURLFilter::ALLOW
,
350 url_filter
->GetFilteringBehaviorForURL(example_url
));
351 EXPECT_EQ(ManagedModeURLFilter::WARN
,
352 url_filter
->GetFilteringBehaviorForURL(moose_url
));
354 // Load a second content pack.
355 installer
= extensions::UnpackedInstaller::Create(service_
);
357 test_data_dir
.AppendASCII("extensions/managed_mode/content_pack_2");
358 installer
->Load(extension_path
);
361 site_lists
= GetActiveSiteLists(managed_user_service
);
362 ASSERT_EQ(2u, site_lists
.size());
364 site_lists
[0]->GetSites(&sites
);
365 site_lists
[1]->GetSites(&sites
);
366 ASSERT_EQ(4u, sites
.size());
367 // The site lists might be returned in any order, so we put them into a set.
368 std::set
<std::string
> site_names
;
369 for (std::vector
<ManagedModeSiteList::Site
>::const_iterator it
=
370 sites
.begin(); it
!= sites
.end(); ++it
) {
371 site_names
.insert(base::UTF16ToUTF8(it
->name
));
373 EXPECT_TRUE(site_names
.count("YouTube") == 1u);
374 EXPECT_TRUE(site_names
.count("Homestar Runner") == 1u);
375 EXPECT_TRUE(site_names
.count(std::string()) == 1u);
376 EXPECT_TRUE(site_names
.count("Moose") == 1u);
378 EXPECT_EQ(ManagedModeURLFilter::ALLOW
,
379 url_filter
->GetFilteringBehaviorForURL(example_url
));
380 EXPECT_EQ(ManagedModeURLFilter::ALLOW
,
381 url_filter
->GetFilteringBehaviorForURL(moose_url
));
383 // Disable the first content pack.
384 service_
->DisableExtension(extension
->id(),
385 extensions::Extension::DISABLE_USER_ACTION
);
388 site_lists
= GetActiveSiteLists(managed_user_service
);
389 ASSERT_EQ(1u, site_lists
.size());
391 site_lists
[0]->GetSites(&sites
);
392 ASSERT_EQ(1u, sites
.size());
393 EXPECT_EQ(base::ASCIIToUTF16("Moose"), sites
[0].name
);
395 EXPECT_EQ(ManagedModeURLFilter::WARN
,
396 url_filter
->GetFilteringBehaviorForURL(example_url
));
397 EXPECT_EQ(ManagedModeURLFilter::ALLOW
,
398 url_filter
->GetFilteringBehaviorForURL(moose_url
));
400 #endif // !defined(OS_ANDROID)