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 "testing/gtest/include/gtest/gtest.h"
28 namespace content_settings
{
30 typedef std::vector
<Rule
> Rules
;
32 TEST(PolicyProviderTest
, DefaultGeolocationContentSetting
) {
33 TestingProfile profile
;
34 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
35 PolicyProvider
provider(prefs
);
39 scoped_ptr
<RuleIterator
> rule_iterator(
40 provider
.GetRuleIterator(
41 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
44 EXPECT_FALSE(rule_iterator
->HasNext());
46 // Change the managed value of the default geolocation setting
47 prefs
->SetManagedPref(prefs::kManagedDefaultGeolocationSetting
,
48 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
51 provider
.GetRuleIterator(
52 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
55 EXPECT_TRUE(rule_iterator
->HasNext());
56 Rule rule
= rule_iterator
->Next();
57 EXPECT_FALSE(rule_iterator
->HasNext());
59 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.primary_pattern
);
60 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.secondary_pattern
);
61 EXPECT_EQ(CONTENT_SETTING_BLOCK
, ValueToContentSetting(rule
.value
.get()));
63 provider
.ShutdownOnUIThread();
66 TEST(PolicyProviderTest
, ManagedDefaultContentSettings
) {
67 TestingProfile profile
;
68 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
69 PolicyProvider
provider(prefs
);
71 prefs
->SetManagedPref(prefs::kManagedDefaultPluginsSetting
,
72 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
74 scoped_ptr
<RuleIterator
> rule_iterator(
75 provider
.GetRuleIterator(
76 CONTENT_SETTINGS_TYPE_PLUGINS
,
79 EXPECT_TRUE(rule_iterator
->HasNext());
80 Rule rule
= rule_iterator
->Next();
81 EXPECT_FALSE(rule_iterator
->HasNext());
83 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.primary_pattern
);
84 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.secondary_pattern
);
85 EXPECT_EQ(CONTENT_SETTING_BLOCK
, ValueToContentSetting(rule
.value
.get()));
87 provider
.ShutdownOnUIThread();
90 // When a default-content-setting is set to a managed setting a
91 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen
92 // if the managed setting is removed.
93 TEST(PolicyProviderTest
, ObserveManagedSettingsChange
) {
94 TestingProfile profile
;
95 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
96 PolicyProvider
provider(prefs
);
98 MockObserver mock_observer
;
99 EXPECT_CALL(mock_observer
,
100 OnContentSettingChanged(_
,
102 CONTENT_SETTINGS_TYPE_DEFAULT
,
104 provider
.AddObserver(&mock_observer
);
106 // Set the managed default-content-setting.
107 prefs
->SetManagedPref(prefs::kManagedDefaultImagesSetting
,
108 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
109 ::testing::Mock::VerifyAndClearExpectations(&mock_observer
);
110 EXPECT_CALL(mock_observer
,
111 OnContentSettingChanged(_
,
113 CONTENT_SETTINGS_TYPE_DEFAULT
,
115 // Remove the managed default-content-setting.
116 prefs
->RemoveManagedPref(prefs::kManagedDefaultImagesSetting
);
117 provider
.ShutdownOnUIThread();
120 TEST(PolicyProviderTest
, GettingManagedContentSettings
) {
121 TestingProfile profile
;
122 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
124 base::ListValue
* value
= new base::ListValue();
125 value
->Append(new base::StringValue("[*.]google.com"));
126 prefs
->SetManagedPref(prefs::kManagedImagesBlockedForUrls
,
129 PolicyProvider
provider(prefs
);
131 ContentSettingsPattern yt_url_pattern
=
132 ContentSettingsPattern::FromString("www.youtube.com");
133 GURL
youtube_url("http://www.youtube.com");
134 GURL
google_url("http://mail.google.com");
136 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
137 GetContentSetting(&provider
,
140 CONTENT_SETTINGS_TYPE_COOKIES
,
144 GetContentSettingValue(&provider
,
147 CONTENT_SETTINGS_TYPE_COOKIES
,
151 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
152 GetContentSetting(&provider
,
155 CONTENT_SETTINGS_TYPE_IMAGES
,
158 scoped_ptr
<base::Value
> value_ptr(
159 GetContentSettingValue(&provider
,
162 CONTENT_SETTINGS_TYPE_IMAGES
,
167 value_ptr
->GetAsInteger(&int_value
);
168 EXPECT_EQ(CONTENT_SETTING_BLOCK
, IntToContentSetting(int_value
));
170 // The PolicyProvider does not allow setting content settings as they are
171 // enforced via policies and not set by the user or extension. So a call to
172 // SetWebsiteSetting does nothing.
173 scoped_ptr
<base::Value
> value_block(
174 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
175 bool owned
= provider
.SetWebsiteSetting(yt_url_pattern
,
177 CONTENT_SETTINGS_TYPE_COOKIES
,
181 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
182 GetContentSetting(&provider
,
185 CONTENT_SETTINGS_TYPE_COOKIES
,
189 provider
.ShutdownOnUIThread();
192 TEST(PolicyProviderTest
, ResourceIdentifier
) {
193 TestingProfile profile
;
194 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
196 base::ListValue
* value
= new base::ListValue();
197 value
->Append(new base::StringValue("[*.]google.com"));
198 prefs
->SetManagedPref(prefs::kManagedPluginsAllowedForUrls
,
201 PolicyProvider
provider(prefs
);
203 GURL
youtube_url("http://www.youtube.com");
204 GURL
google_url("http://mail.google.com");
206 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
208 &provider
, youtube_url
, youtube_url
,
209 CONTENT_SETTINGS_TYPE_PLUGINS
, "someplugin", false));
211 // There is currently no policy support for resource content settings.
212 // Resource identifiers are simply ignored by the PolicyProvider.
213 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
214 GetContentSetting(&provider
,
217 CONTENT_SETTINGS_TYPE_PLUGINS
,
221 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
223 &provider
, google_url
, google_url
,
224 CONTENT_SETTINGS_TYPE_PLUGINS
, "someplugin", false));
226 provider
.ShutdownOnUIThread();
229 TEST(PolicyProviderTest
, AutoSelectCertificateList
) {
230 TestingProfile profile
;
231 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
233 PolicyProvider
provider(prefs
);
234 GURL
google_url("https://mail.google.com");
235 // Tests the default setting for auto selecting certificates
238 GetContentSettingValue(&provider
,
241 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
245 // Set the content settings pattern list for origins to auto select
247 std::string
pattern_str("\"pattern\":\"[*.]google.com\"");
248 std::string
filter_str("\"filter\":{\"ISSUER\":{\"CN\":\"issuer name\"}}");
249 base::ListValue
* value
= new base::ListValue();
251 new base::StringValue("{" + pattern_str
+ "," + filter_str
+ "}"));
252 prefs
->SetManagedPref(prefs::kManagedAutoSelectCertificateForUrls
,
254 GURL
youtube_url("https://www.youtube.com");
257 GetContentSettingValue(&provider
,
260 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
263 scoped_ptr
<base::Value
> cert_filter(
264 GetContentSettingValue(&provider
,
267 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
271 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, cert_filter
->GetType());
272 base::DictionaryValue
* dict_value
=
273 static_cast<base::DictionaryValue
*>(cert_filter
.get());
274 std::string actual_common_name
;
275 ASSERT_TRUE(dict_value
->GetString("ISSUER.CN", &actual_common_name
));
276 EXPECT_EQ("issuer name", actual_common_name
);
277 provider
.ShutdownOnUIThread();
280 } // namespace content_settings