Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_policy_provider_unittest.cc
blob0bf6e4dbda785f8bddf310f2287e84cb055154fa
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"
7 #include <string>
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"
24 #include "url/gurl.h"
26 using ::testing::_;
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);
37 Rules rules;
39 scoped_ptr<RuleIterator> rule_iterator(
40 provider.GetRuleIterator(
41 CONTENT_SETTINGS_TYPE_GEOLOCATION,
42 std::string(),
43 false));
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));
50 rule_iterator.reset(
51 provider.GetRuleIterator(
52 CONTENT_SETTINGS_TYPE_GEOLOCATION,
53 std::string(),
54 false));
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,
77 std::string(),
78 false));
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,
103 ""));
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,
114 ""));
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,
127 value);
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,
138 youtube_url,
139 youtube_url,
140 CONTENT_SETTINGS_TYPE_COOKIES,
141 std::string(),
142 false));
143 EXPECT_EQ(NULL,
144 GetContentSettingValue(&provider,
145 youtube_url,
146 youtube_url,
147 CONTENT_SETTINGS_TYPE_COOKIES,
148 std::string(),
149 false));
151 EXPECT_EQ(CONTENT_SETTING_BLOCK,
152 GetContentSetting(&provider,
153 google_url,
154 google_url,
155 CONTENT_SETTINGS_TYPE_IMAGES,
156 std::string(),
157 false));
158 scoped_ptr<base::Value> value_ptr(
159 GetContentSettingValue(&provider,
160 google_url,
161 google_url,
162 CONTENT_SETTINGS_TYPE_IMAGES,
163 std::string(),
164 false));
166 int int_value = -1;
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,
176 yt_url_pattern,
177 CONTENT_SETTINGS_TYPE_COOKIES,
178 std::string(),
179 value_block.get());
180 EXPECT_FALSE(owned);
181 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
182 GetContentSetting(&provider,
183 youtube_url,
184 youtube_url,
185 CONTENT_SETTINGS_TYPE_COOKIES,
186 std::string(),
187 false));
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,
199 value);
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,
207 GetContentSetting(
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,
215 google_url,
216 google_url,
217 CONTENT_SETTINGS_TYPE_PLUGINS,
218 std::string(),
219 false));
221 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
222 GetContentSetting(
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
236 EXPECT_EQ(
237 NULL,
238 GetContentSettingValue(&provider,
239 google_url,
240 google_url,
241 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
242 std::string(),
243 false));
245 // Set the content settings pattern list for origins to auto select
246 // certificates.
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();
250 value->Append(
251 new base::StringValue("{" + pattern_str + "," + filter_str + "}"));
252 prefs->SetManagedPref(prefs::kManagedAutoSelectCertificateForUrls,
253 value);
254 GURL youtube_url("https://www.youtube.com");
255 EXPECT_EQ(
256 NULL,
257 GetContentSettingValue(&provider,
258 youtube_url,
259 youtube_url,
260 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
261 std::string(),
262 false));
263 scoped_ptr<base::Value> cert_filter(
264 GetContentSettingValue(&provider,
265 google_url,
266 google_url,
267 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
268 std::string(),
269 false));
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