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/common/content_settings_pattern.h"
23 #include "components/content_settings/core/test/content_settings_test_utils.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 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
41 PolicyProvider
provider(prefs
);
45 scoped_ptr
<RuleIterator
> rule_iterator(
46 provider
.GetRuleIterator(
47 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
50 EXPECT_FALSE(rule_iterator
->HasNext());
52 // Change the managed value of the default geolocation setting
53 prefs
->SetManagedPref(prefs::kManagedDefaultGeolocationSetting
,
54 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
57 provider
.GetRuleIterator(
58 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
61 EXPECT_TRUE(rule_iterator
->HasNext());
62 Rule rule
= rule_iterator
->Next();
63 EXPECT_FALSE(rule_iterator
->HasNext());
65 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.primary_pattern
);
66 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.secondary_pattern
);
67 EXPECT_EQ(CONTENT_SETTING_BLOCK
, ValueToContentSetting(rule
.value
.get()));
69 provider
.ShutdownOnUIThread();
72 TEST_F(PolicyProviderTest
, ManagedDefaultContentSettings
) {
73 TestingProfile profile
;
74 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
75 PolicyProvider
provider(prefs
);
77 prefs
->SetManagedPref(prefs::kManagedDefaultPluginsSetting
,
78 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
80 scoped_ptr
<RuleIterator
> rule_iterator(
81 provider
.GetRuleIterator(
82 CONTENT_SETTINGS_TYPE_PLUGINS
,
85 EXPECT_TRUE(rule_iterator
->HasNext());
86 Rule rule
= rule_iterator
->Next();
87 EXPECT_FALSE(rule_iterator
->HasNext());
89 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.primary_pattern
);
90 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.secondary_pattern
);
91 EXPECT_EQ(CONTENT_SETTING_BLOCK
, ValueToContentSetting(rule
.value
.get()));
93 provider
.ShutdownOnUIThread();
96 // When a default-content-setting is set to a managed setting a
97 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen
98 // if the managed setting is removed.
99 TEST_F(PolicyProviderTest
, ObserveManagedSettingsChange
) {
100 TestingProfile profile
;
101 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
102 PolicyProvider
provider(prefs
);
104 MockObserver mock_observer
;
105 EXPECT_CALL(mock_observer
,
106 OnContentSettingChanged(_
,
108 CONTENT_SETTINGS_TYPE_DEFAULT
,
110 provider
.AddObserver(&mock_observer
);
112 // Set the managed default-content-setting.
113 prefs
->SetManagedPref(prefs::kManagedDefaultImagesSetting
,
114 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
115 ::testing::Mock::VerifyAndClearExpectations(&mock_observer
);
116 EXPECT_CALL(mock_observer
,
117 OnContentSettingChanged(_
,
119 CONTENT_SETTINGS_TYPE_DEFAULT
,
121 // Remove the managed default-content-setting.
122 prefs
->RemoveManagedPref(prefs::kManagedDefaultImagesSetting
);
123 provider
.ShutdownOnUIThread();
126 TEST_F(PolicyProviderTest
, GettingManagedContentSettings
) {
127 TestingProfile profile
;
128 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
130 base::ListValue
* value
= new base::ListValue();
131 value
->Append(new base::StringValue("[*.]google.com"));
132 prefs
->SetManagedPref(prefs::kManagedImagesBlockedForUrls
,
135 PolicyProvider
provider(prefs
);
137 ContentSettingsPattern yt_url_pattern
=
138 ContentSettingsPattern::FromString("www.youtube.com");
139 GURL
youtube_url("http://www.youtube.com");
140 GURL
google_url("http://mail.google.com");
142 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
143 GetContentSetting(&provider
,
146 CONTENT_SETTINGS_TYPE_COOKIES
,
150 GetContentSettingValue(&provider
,
153 CONTENT_SETTINGS_TYPE_COOKIES
,
157 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
158 GetContentSetting(&provider
,
161 CONTENT_SETTINGS_TYPE_IMAGES
,
164 scoped_ptr
<base::Value
> value_ptr(
165 GetContentSettingValue(&provider
,
168 CONTENT_SETTINGS_TYPE_IMAGES
,
173 value_ptr
->GetAsInteger(&int_value
);
174 EXPECT_EQ(CONTENT_SETTING_BLOCK
, IntToContentSetting(int_value
));
176 // The PolicyProvider does not allow setting content settings as they are
177 // enforced via policies and not set by the user or extension. So a call to
178 // SetWebsiteSetting does nothing.
179 scoped_ptr
<base::Value
> value_block(
180 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
181 bool owned
= provider
.SetWebsiteSetting(yt_url_pattern
,
183 CONTENT_SETTINGS_TYPE_COOKIES
,
187 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
188 GetContentSetting(&provider
,
191 CONTENT_SETTINGS_TYPE_COOKIES
,
195 provider
.ShutdownOnUIThread();
198 TEST_F(PolicyProviderTest
, ResourceIdentifier
) {
199 TestingProfile profile
;
200 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
202 base::ListValue
* value
= new base::ListValue();
203 value
->Append(new base::StringValue("[*.]google.com"));
204 prefs
->SetManagedPref(prefs::kManagedPluginsAllowedForUrls
,
207 PolicyProvider
provider(prefs
);
209 GURL
youtube_url("http://www.youtube.com");
210 GURL
google_url("http://mail.google.com");
212 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
214 &provider
, youtube_url
, youtube_url
,
215 CONTENT_SETTINGS_TYPE_PLUGINS
, "someplugin", false));
217 // There is currently no policy support for resource content settings.
218 // Resource identifiers are simply ignored by the PolicyProvider.
219 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
220 GetContentSetting(&provider
,
223 CONTENT_SETTINGS_TYPE_PLUGINS
,
227 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
229 &provider
, google_url
, google_url
,
230 CONTENT_SETTINGS_TYPE_PLUGINS
, "someplugin", false));
232 provider
.ShutdownOnUIThread();
235 TEST_F(PolicyProviderTest
, AutoSelectCertificateList
) {
236 TestingProfile profile
;
237 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
239 PolicyProvider
provider(prefs
);
240 GURL
google_url("https://mail.google.com");
241 // Tests the default setting for auto selecting certificates
244 GetContentSettingValue(&provider
,
247 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
251 // Set the content settings pattern list for origins to auto select
253 std::string
pattern_str("\"pattern\":\"[*.]google.com\"");
254 std::string
filter_str("\"filter\":{\"ISSUER\":{\"CN\":\"issuer name\"}}");
255 base::ListValue
* value
= new base::ListValue();
257 new base::StringValue("{" + pattern_str
+ "," + filter_str
+ "}"));
258 prefs
->SetManagedPref(prefs::kManagedAutoSelectCertificateForUrls
,
260 GURL
youtube_url("https://www.youtube.com");
263 GetContentSettingValue(&provider
,
266 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
269 scoped_ptr
<base::Value
> cert_filter(
270 GetContentSettingValue(&provider
,
273 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
277 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, cert_filter
->GetType());
278 base::DictionaryValue
* dict_value
=
279 static_cast<base::DictionaryValue
*>(cert_filter
.get());
280 std::string actual_common_name
;
281 ASSERT_TRUE(dict_value
->GetString("ISSUER.CN", &actual_common_name
));
282 EXPECT_EQ("issuer name", actual_common_name
);
283 provider
.ShutdownOnUIThread();
286 } // namespace content_settings