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_profile.h"
19 #include "components/content_settings/core/browser/content_settings_rule.h"
20 #include "components/content_settings/core/browser/content_settings_utils.h"
21 #include "components/content_settings/core/common/content_settings_pattern.h"
22 #include "components/content_settings/core/test/content_settings_test_utils.h"
23 #include "components/syncable_prefs/testing_pref_service_syncable.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "testing/gtest/include/gtest/gtest.h"
30 namespace content_settings
{
32 typedef std::vector
<Rule
> Rules
;
34 class PolicyProviderTest
: public testing::Test
{
35 content::TestBrowserThreadBundle thread_bundle_
;
38 TEST_F(PolicyProviderTest
, DefaultGeolocationContentSetting
) {
39 TestingProfile profile
;
40 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
41 profile
.GetTestingPrefService();
42 PolicyProvider
provider(prefs
);
46 scoped_ptr
<RuleIterator
> rule_iterator(
47 provider
.GetRuleIterator(
48 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
51 EXPECT_FALSE(rule_iterator
->HasNext());
53 // Change the managed value of the default geolocation setting
54 prefs
->SetManagedPref(prefs::kManagedDefaultGeolocationSetting
,
55 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
58 provider
.GetRuleIterator(
59 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
62 EXPECT_TRUE(rule_iterator
->HasNext());
63 Rule rule
= rule_iterator
->Next();
64 EXPECT_FALSE(rule_iterator
->HasNext());
66 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.primary_pattern
);
67 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.secondary_pattern
);
68 EXPECT_EQ(CONTENT_SETTING_BLOCK
, ValueToContentSetting(rule
.value
.get()));
70 provider
.ShutdownOnUIThread();
73 TEST_F(PolicyProviderTest
, ManagedDefaultContentSettings
) {
74 TestingProfile profile
;
75 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
76 profile
.GetTestingPrefService();
77 PolicyProvider
provider(prefs
);
79 prefs
->SetManagedPref(prefs::kManagedDefaultPluginsSetting
,
80 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
82 scoped_ptr
<RuleIterator
> rule_iterator(
83 provider
.GetRuleIterator(
84 CONTENT_SETTINGS_TYPE_PLUGINS
,
87 EXPECT_TRUE(rule_iterator
->HasNext());
88 Rule rule
= rule_iterator
->Next();
89 EXPECT_FALSE(rule_iterator
->HasNext());
91 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.primary_pattern
);
92 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.secondary_pattern
);
93 EXPECT_EQ(CONTENT_SETTING_BLOCK
, ValueToContentSetting(rule
.value
.get()));
95 provider
.ShutdownOnUIThread();
98 // When a default-content-setting is set to a managed setting a
99 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen
100 // if the managed setting is removed.
101 TEST_F(PolicyProviderTest
, ObserveManagedSettingsChange
) {
102 TestingProfile profile
;
103 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
104 profile
.GetTestingPrefService();
105 PolicyProvider
provider(prefs
);
107 MockObserver mock_observer
;
108 EXPECT_CALL(mock_observer
,
109 OnContentSettingChanged(_
,
111 CONTENT_SETTINGS_TYPE_DEFAULT
,
113 provider
.AddObserver(&mock_observer
);
115 // Set the managed default-content-setting.
116 prefs
->SetManagedPref(prefs::kManagedDefaultImagesSetting
,
117 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
118 ::testing::Mock::VerifyAndClearExpectations(&mock_observer
);
119 EXPECT_CALL(mock_observer
,
120 OnContentSettingChanged(_
,
122 CONTENT_SETTINGS_TYPE_DEFAULT
,
124 // Remove the managed default-content-setting.
125 prefs
->RemoveManagedPref(prefs::kManagedDefaultImagesSetting
);
126 provider
.ShutdownOnUIThread();
129 TEST_F(PolicyProviderTest
, GettingManagedContentSettings
) {
130 TestingProfile profile
;
131 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
132 profile
.GetTestingPrefService();
134 base::ListValue
* value
= new base::ListValue();
135 value
->Append(new base::StringValue("[*.]google.com"));
136 prefs
->SetManagedPref(prefs::kManagedImagesBlockedForUrls
,
139 PolicyProvider
provider(prefs
);
141 ContentSettingsPattern yt_url_pattern
=
142 ContentSettingsPattern::FromString("www.youtube.com");
143 GURL
youtube_url("http://www.youtube.com");
144 GURL
google_url("http://mail.google.com");
146 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
147 GetContentSetting(&provider
,
150 CONTENT_SETTINGS_TYPE_COOKIES
,
154 GetContentSettingValue(&provider
,
157 CONTENT_SETTINGS_TYPE_COOKIES
,
161 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
162 GetContentSetting(&provider
,
165 CONTENT_SETTINGS_TYPE_IMAGES
,
168 scoped_ptr
<base::Value
> value_ptr(
169 GetContentSettingValue(&provider
,
172 CONTENT_SETTINGS_TYPE_IMAGES
,
177 value_ptr
->GetAsInteger(&int_value
);
178 EXPECT_EQ(CONTENT_SETTING_BLOCK
, IntToContentSetting(int_value
));
180 // The PolicyProvider does not allow setting content settings as they are
181 // enforced via policies and not set by the user or extension. So a call to
182 // SetWebsiteSetting does nothing.
183 scoped_ptr
<base::Value
> value_block(
184 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
185 bool owned
= provider
.SetWebsiteSetting(yt_url_pattern
,
187 CONTENT_SETTINGS_TYPE_COOKIES
,
191 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
192 GetContentSetting(&provider
,
195 CONTENT_SETTINGS_TYPE_COOKIES
,
199 provider
.ShutdownOnUIThread();
202 TEST_F(PolicyProviderTest
, ResourceIdentifier
) {
203 TestingProfile profile
;
204 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
205 profile
.GetTestingPrefService();
207 base::ListValue
* value
= new base::ListValue();
208 value
->Append(new base::StringValue("[*.]google.com"));
209 prefs
->SetManagedPref(prefs::kManagedPluginsAllowedForUrls
,
212 PolicyProvider
provider(prefs
);
214 GURL
youtube_url("http://www.youtube.com");
215 GURL
google_url("http://mail.google.com");
217 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
219 &provider
, youtube_url
, youtube_url
,
220 CONTENT_SETTINGS_TYPE_PLUGINS
, "someplugin", false));
222 // There is currently no policy support for resource content settings.
223 // Resource identifiers are simply ignored by the PolicyProvider.
224 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
225 GetContentSetting(&provider
,
228 CONTENT_SETTINGS_TYPE_PLUGINS
,
232 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
234 &provider
, google_url
, google_url
,
235 CONTENT_SETTINGS_TYPE_PLUGINS
, "someplugin", false));
237 provider
.ShutdownOnUIThread();
240 TEST_F(PolicyProviderTest
, AutoSelectCertificateList
) {
241 TestingProfile profile
;
242 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
243 profile
.GetTestingPrefService();
245 PolicyProvider
provider(prefs
);
246 GURL
google_url("https://mail.google.com");
247 // Tests the default setting for auto selecting certificates
250 GetContentSettingValue(&provider
,
253 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
257 // Set the content settings pattern list for origins to auto select
259 std::string
pattern_str("\"pattern\":\"[*.]google.com\"");
260 std::string
filter_str("\"filter\":{\"ISSUER\":{\"CN\":\"issuer name\"}}");
261 base::ListValue
* value
= new base::ListValue();
263 new base::StringValue("{" + pattern_str
+ "," + filter_str
+ "}"));
264 prefs
->SetManagedPref(prefs::kManagedAutoSelectCertificateForUrls
,
266 GURL
youtube_url("https://www.youtube.com");
269 GetContentSettingValue(&provider
,
272 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
275 scoped_ptr
<base::Value
> cert_filter(
276 GetContentSettingValue(&provider
,
279 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
283 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, cert_filter
->GetType());
284 base::DictionaryValue
* dict_value
=
285 static_cast<base::DictionaryValue
*>(cert_filter
.get());
286 std::string actual_common_name
;
287 ASSERT_TRUE(dict_value
->GetString("ISSUER.CN", &actual_common_name
));
288 EXPECT_EQ("issuer name", actual_common_name
);
289 provider
.ShutdownOnUIThread();
292 } // namespace content_settings