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 "chrome/browser/content_settings/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/browser/content_settings/content_settings_rule.h"
16 #include "chrome/browser/content_settings/content_settings_utils.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/common/url_constants.h"
20 #include "chrome/test/base/testing_pref_service_syncable.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/test/test_browser_thread.h"
23 #include "testing/gtest/include/gtest/gtest.h"
27 using content::BrowserThread
;
29 namespace content_settings
{
31 typedef std::vector
<Rule
> Rules
;
33 class PolicyProviderTest
: public testing::Test
{
36 : ui_thread_(BrowserThread::UI
, &message_loop_
) {
40 // TODO(markusheintz): Check if it's possible to derive the provider class
41 // from NonThreadSafe and to use native thread identifiers instead of
42 // BrowserThread IDs. Then we could get rid of the message_loop and ui_thread
44 base::MessageLoop message_loop_
;
45 content::TestBrowserThread ui_thread_
;
48 TEST_F(PolicyProviderTest
, DefaultGeolocationContentSetting
) {
49 TestingProfile profile
;
50 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
51 PolicyProvider
provider(prefs
);
55 scoped_ptr
<RuleIterator
> rule_iterator(
56 provider
.GetRuleIterator(
57 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
60 EXPECT_FALSE(rule_iterator
->HasNext());
62 // Change the managed value of the default geolocation setting
63 prefs
->SetManagedPref(prefs::kManagedDefaultGeolocationSetting
,
64 base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK
));
67 provider
.GetRuleIterator(
68 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
71 EXPECT_TRUE(rule_iterator
->HasNext());
72 Rule rule
= rule_iterator
->Next();
73 EXPECT_FALSE(rule_iterator
->HasNext());
75 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.primary_pattern
);
76 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.secondary_pattern
);
77 EXPECT_EQ(CONTENT_SETTING_BLOCK
, ValueToContentSetting(rule
.value
.get()));
79 provider
.ShutdownOnUIThread();
82 TEST_F(PolicyProviderTest
, ManagedDefaultContentSettings
) {
83 TestingProfile profile
;
84 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
85 PolicyProvider
provider(prefs
);
87 prefs
->SetManagedPref(prefs::kManagedDefaultPluginsSetting
,
88 base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK
));
90 scoped_ptr
<RuleIterator
> rule_iterator(
91 provider
.GetRuleIterator(
92 CONTENT_SETTINGS_TYPE_PLUGINS
,
95 EXPECT_TRUE(rule_iterator
->HasNext());
96 Rule rule
= rule_iterator
->Next();
97 EXPECT_FALSE(rule_iterator
->HasNext());
99 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.primary_pattern
);
100 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule
.secondary_pattern
);
101 EXPECT_EQ(CONTENT_SETTING_BLOCK
, ValueToContentSetting(rule
.value
.get()));
103 provider
.ShutdownOnUIThread();
106 // When a default-content-setting is set to a managed setting a
107 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen
108 // if the managed setting is removed.
109 TEST_F(PolicyProviderTest
, ObserveManagedSettingsChange
) {
110 TestingProfile profile
;
111 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
112 PolicyProvider
provider(prefs
);
114 MockObserver mock_observer
;
115 EXPECT_CALL(mock_observer
,
116 OnContentSettingChanged(_
,
118 CONTENT_SETTINGS_TYPE_DEFAULT
,
120 provider
.AddObserver(&mock_observer
);
122 // Set the managed default-content-setting.
123 prefs
->SetManagedPref(prefs::kManagedDefaultImagesSetting
,
124 base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK
));
125 ::testing::Mock::VerifyAndClearExpectations(&mock_observer
);
126 EXPECT_CALL(mock_observer
,
127 OnContentSettingChanged(_
,
129 CONTENT_SETTINGS_TYPE_DEFAULT
,
131 // Remove the managed default-content-setting.
132 prefs
->RemoveManagedPref(prefs::kManagedDefaultImagesSetting
);
133 provider
.ShutdownOnUIThread();
136 TEST_F(PolicyProviderTest
, GettingManagedContentSettings
) {
137 TestingProfile profile
;
138 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
140 base::ListValue
* value
= new base::ListValue();
141 value
->Append(base::Value::CreateStringValue("[*.]google.com"));
142 prefs
->SetManagedPref(prefs::kManagedImagesBlockedForUrls
,
145 PolicyProvider
provider(prefs
);
147 ContentSettingsPattern yt_url_pattern
=
148 ContentSettingsPattern::FromString("www.youtube.com");
149 GURL
youtube_url("http://www.youtube.com");
150 GURL
google_url("http://mail.google.com");
152 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
153 GetContentSetting(&provider
,
156 CONTENT_SETTINGS_TYPE_COOKIES
,
160 GetContentSettingValue(&provider
,
163 CONTENT_SETTINGS_TYPE_COOKIES
,
167 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
168 GetContentSetting(&provider
,
171 CONTENT_SETTINGS_TYPE_IMAGES
,
174 scoped_ptr
<base::Value
> value_ptr(
175 GetContentSettingValue(&provider
,
178 CONTENT_SETTINGS_TYPE_IMAGES
,
183 value_ptr
->GetAsInteger(&int_value
);
184 EXPECT_EQ(CONTENT_SETTING_BLOCK
, IntToContentSetting(int_value
));
186 // The PolicyProvider does not allow setting content settings as they are
187 // enforced via policies and not set by the user or extension. So a call to
188 // SetWebsiteSetting does nothing.
189 scoped_ptr
<base::Value
> value_block(
190 base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK
));
191 bool owned
= provider
.SetWebsiteSetting(yt_url_pattern
,
193 CONTENT_SETTINGS_TYPE_COOKIES
,
197 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
198 GetContentSetting(&provider
,
201 CONTENT_SETTINGS_TYPE_COOKIES
,
205 provider
.ShutdownOnUIThread();
208 TEST_F(PolicyProviderTest
, ResourceIdentifier
) {
209 TestingProfile profile
;
210 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
212 base::ListValue
* value
= new base::ListValue();
213 value
->Append(base::Value::CreateStringValue("[*.]google.com"));
214 prefs
->SetManagedPref(prefs::kManagedPluginsAllowedForUrls
,
217 PolicyProvider
provider(prefs
);
219 GURL
youtube_url("http://www.youtube.com");
220 GURL
google_url("http://mail.google.com");
222 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
224 &provider
, youtube_url
, youtube_url
,
225 CONTENT_SETTINGS_TYPE_PLUGINS
, "someplugin", false));
227 // There is currently no policy support for resource content settings.
228 // Resource identifiers are simply ignored by the PolicyProvider.
229 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
230 GetContentSetting(&provider
,
233 CONTENT_SETTINGS_TYPE_PLUGINS
,
237 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
239 &provider
, google_url
, google_url
,
240 CONTENT_SETTINGS_TYPE_PLUGINS
, "someplugin", false));
242 provider
.ShutdownOnUIThread();
245 TEST_F(PolicyProviderTest
, AutoSelectCertificateList
) {
246 TestingProfile profile
;
247 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
249 PolicyProvider
provider(prefs
);
250 GURL
google_url("https://mail.google.com");
251 // Tests the default setting for auto selecting certificates
254 GetContentSettingValue(&provider
,
257 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
261 // Set the content settings pattern list for origins to auto select
263 std::string
pattern_str("\"pattern\":\"[*.]google.com\"");
264 std::string
filter_str("\"filter\":{\"ISSUER\":{\"CN\":\"issuer name\"}}");
265 base::ListValue
* value
= new base::ListValue();
266 value
->Append(base::Value::CreateStringValue(
267 "{" + pattern_str
+ "," + filter_str
+ "}"));
268 prefs
->SetManagedPref(prefs::kManagedAutoSelectCertificateForUrls
,
270 GURL
youtube_url("https://www.youtube.com");
273 GetContentSettingValue(&provider
,
276 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
279 scoped_ptr
<base::Value
> cert_filter(
280 GetContentSettingValue(&provider
,
283 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
287 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, cert_filter
->GetType());
288 base::DictionaryValue
* dict_value
=
289 static_cast<base::DictionaryValue
*>(cert_filter
.get());
290 std::string actual_common_name
;
291 ASSERT_TRUE(dict_value
->GetString("ISSUER.CN", &actual_common_name
));
292 EXPECT_EQ("issuer name", actual_common_name
);
293 provider
.ShutdownOnUIThread();
296 } // namespace content_settings