1 // Copyright (c) 2011 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 "components/content_settings/core/browser/content_settings_policy_provider.h"
9 #include "base/auto_reset.h"
10 #include "base/command_line.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/prefs/pref_service.h"
14 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/common/url_constants.h"
18 #include "chrome/test/base/testing_pref_service_syncable.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "components/content_settings/core/browser/content_settings_rule.h"
21 #include "components/content_settings/core/browser/content_settings_utils.h"
22 #include "components/content_settings/core/test/content_settings_test_utils.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "testing/gtest/include/gtest/gtest.h"
29 namespace content_settings
{
31 typedef std::vector
<Rule
> Rules
;
33 class PolicyProviderTest
: public testing::Test
{
34 content::TestBrowserThreadBundle thread_bundle_
;
37 TEST_F(PolicyProviderTest
, DefaultGeolocationContentSetting
) {
38 TestingProfile profile
;
39 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
40 PolicyProvider
provider(prefs
);
44 scoped_ptr
<RuleIterator
> rule_iterator(
45 provider
.GetRuleIterator(
46 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
49 EXPECT_FALSE(rule_iterator
->HasNext());
51 // Change the managed value of the default geolocation setting
52 prefs
->SetManagedPref(prefs::kManagedDefaultGeolocationSetting
,
53 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
56 provider
.GetRuleIterator(
57 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
60 EXPECT_TRUE(rule_iterator
->HasNext());
61 Rule rule
= rule_iterator
->Next();
62 EXPECT_FALSE(rule_iterator
->HasNext());
64 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.primary_pattern
);
65 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.secondary_pattern
);
66 EXPECT_EQ(CONTENT_SETTING_BLOCK
, ValueToContentSetting(rule
.value
.get()));
68 provider
.ShutdownOnUIThread();
71 TEST_F(PolicyProviderTest
, ManagedDefaultContentSettings
) {
72 TestingProfile profile
;
73 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
74 PolicyProvider
provider(prefs
);
76 prefs
->SetManagedPref(prefs::kManagedDefaultPluginsSetting
,
77 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
79 scoped_ptr
<RuleIterator
> rule_iterator(
80 provider
.GetRuleIterator(
81 CONTENT_SETTINGS_TYPE_PLUGINS
,
84 EXPECT_TRUE(rule_iterator
->HasNext());
85 Rule rule
= rule_iterator
->Next();
86 EXPECT_FALSE(rule_iterator
->HasNext());
88 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.primary_pattern
);
89 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.secondary_pattern
);
90 EXPECT_EQ(CONTENT_SETTING_BLOCK
, ValueToContentSetting(rule
.value
.get()));
92 provider
.ShutdownOnUIThread();
95 // When a default-content-setting is set to a managed setting a
96 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen
97 // if the managed setting is removed.
98 TEST_F(PolicyProviderTest
, ObserveManagedSettingsChange
) {
99 TestingProfile profile
;
100 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
101 PolicyProvider
provider(prefs
);
103 MockObserver mock_observer
;
104 EXPECT_CALL(mock_observer
,
105 OnContentSettingChanged(_
,
107 CONTENT_SETTINGS_TYPE_DEFAULT
,
109 provider
.AddObserver(&mock_observer
);
111 // Set the managed default-content-setting.
112 prefs
->SetManagedPref(prefs::kManagedDefaultImagesSetting
,
113 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
114 ::testing::Mock::VerifyAndClearExpectations(&mock_observer
);
115 EXPECT_CALL(mock_observer
,
116 OnContentSettingChanged(_
,
118 CONTENT_SETTINGS_TYPE_DEFAULT
,
120 // Remove the managed default-content-setting.
121 prefs
->RemoveManagedPref(prefs::kManagedDefaultImagesSetting
);
122 provider
.ShutdownOnUIThread();
125 TEST_F(PolicyProviderTest
, GettingManagedContentSettings
) {
126 TestingProfile profile
;
127 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
129 base::ListValue
* value
= new base::ListValue();
130 value
->Append(new base::StringValue("[*.]google.com"));
131 prefs
->SetManagedPref(prefs::kManagedImagesBlockedForUrls
,
134 PolicyProvider
provider(prefs
);
136 ContentSettingsPattern yt_url_pattern
=
137 ContentSettingsPattern::FromString("www.youtube.com");
138 GURL
youtube_url("http://www.youtube.com");
139 GURL
google_url("http://mail.google.com");
141 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
142 GetContentSetting(&provider
,
145 CONTENT_SETTINGS_TYPE_COOKIES
,
149 GetContentSettingValue(&provider
,
152 CONTENT_SETTINGS_TYPE_COOKIES
,
156 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
157 GetContentSetting(&provider
,
160 CONTENT_SETTINGS_TYPE_IMAGES
,
163 scoped_ptr
<base::Value
> value_ptr(
164 GetContentSettingValue(&provider
,
167 CONTENT_SETTINGS_TYPE_IMAGES
,
172 value_ptr
->GetAsInteger(&int_value
);
173 EXPECT_EQ(CONTENT_SETTING_BLOCK
, IntToContentSetting(int_value
));
175 // The PolicyProvider does not allow setting content settings as they are
176 // enforced via policies and not set by the user or extension. So a call to
177 // SetWebsiteSetting does nothing.
178 scoped_ptr
<base::Value
> value_block(
179 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
180 bool owned
= provider
.SetWebsiteSetting(yt_url_pattern
,
182 CONTENT_SETTINGS_TYPE_COOKIES
,
186 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
187 GetContentSetting(&provider
,
190 CONTENT_SETTINGS_TYPE_COOKIES
,
194 provider
.ShutdownOnUIThread();
197 TEST_F(PolicyProviderTest
, ResourceIdentifier
) {
198 TestingProfile profile
;
199 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
201 base::ListValue
* value
= new base::ListValue();
202 value
->Append(new base::StringValue("[*.]google.com"));
203 prefs
->SetManagedPref(prefs::kManagedPluginsAllowedForUrls
,
206 PolicyProvider
provider(prefs
);
208 GURL
youtube_url("http://www.youtube.com");
209 GURL
google_url("http://mail.google.com");
211 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
213 &provider
, youtube_url
, youtube_url
,
214 CONTENT_SETTINGS_TYPE_PLUGINS
, "someplugin", false));
216 // There is currently no policy support for resource content settings.
217 // Resource identifiers are simply ignored by the PolicyProvider.
218 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
219 GetContentSetting(&provider
,
222 CONTENT_SETTINGS_TYPE_PLUGINS
,
226 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
228 &provider
, google_url
, google_url
,
229 CONTENT_SETTINGS_TYPE_PLUGINS
, "someplugin", false));
231 provider
.ShutdownOnUIThread();
234 TEST_F(PolicyProviderTest
, AutoSelectCertificateList
) {
235 TestingProfile profile
;
236 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
238 PolicyProvider
provider(prefs
);
239 GURL
google_url("https://mail.google.com");
240 // Tests the default setting for auto selecting certificates
243 GetContentSettingValue(&provider
,
246 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
250 // Set the content settings pattern list for origins to auto select
252 std::string
pattern_str("\"pattern\":\"[*.]google.com\"");
253 std::string
filter_str("\"filter\":{\"ISSUER\":{\"CN\":\"issuer name\"}}");
254 base::ListValue
* value
= new base::ListValue();
256 new base::StringValue("{" + pattern_str
+ "," + filter_str
+ "}"));
257 prefs
->SetManagedPref(prefs::kManagedAutoSelectCertificateForUrls
,
259 GURL
youtube_url("https://www.youtube.com");
262 GetContentSettingValue(&provider
,
265 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
268 scoped_ptr
<base::Value
> cert_filter(
269 GetContentSettingValue(&provider
,
272 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
276 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, cert_filter
->GetType());
277 base::DictionaryValue
* dict_value
=
278 static_cast<base::DictionaryValue
*>(cert_filter
.get());
279 std::string actual_common_name
;
280 ASSERT_TRUE(dict_value
->GetString("ISSUER.CN", &actual_common_name
));
281 EXPECT_EQ("issuer name", actual_common_name
);
282 provider
.ShutdownOnUIThread();
285 } // namespace content_settings