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. 4 5#include "chrome/browser/content_settings/content_settings_policy_provider.h" 6 7#include <string> 8 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_utils.h" 16#include "chrome/common/chrome_switches.h" 17#include "chrome/common/pref_names.h" 18#include "chrome/common/url_constants.h" 19#include "chrome/test/base/testing_pref_service_syncable.h" 20#include "chrome/test/base/testing_profile.h" 21#include "components/content_settings/core/browser/content_settings_rule.h" 22#include "content/public/test/test_browser_thread.h" 23#include "testing/gtest/include/gtest/gtest.h" 24#include "url/gurl.h" 25 26using ::testing::_; 27using content::BrowserThread; 28 29namespace content_settings { 30 31typedef std::vector<Rule> Rules; 32 33class PolicyProviderTest : public testing::Test { 34 public: 35 PolicyProviderTest() 36 : ui_thread_(BrowserThread::UI, &message_loop_) { 37 } 38 39 protected: 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 43 // fields. 44 base::MessageLoop message_loop_; 45 content::TestBrowserThread ui_thread_; 46}; 47 48TEST_F(PolicyProviderTest, DefaultGeolocationContentSetting) { 49 TestingProfile profile; 50 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); 51 PolicyProvider provider(prefs); 52 53 Rules rules; 54 55 scoped_ptr<RuleIterator> rule_iterator( 56 provider.GetRuleIterator( 57 CONTENT_SETTINGS_TYPE_GEOLOCATION, 58 std::string(), 59 false)); 60 EXPECT_FALSE(rule_iterator->HasNext()); 61 62 // Change the managed value of the default geolocation setting 63 prefs->SetManagedPref(prefs::kManagedDefaultGeolocationSetting, 64 new base::FundamentalValue(CONTENT_SETTING_BLOCK)); 65 66 rule_iterator.reset( 67 provider.GetRuleIterator( 68 CONTENT_SETTINGS_TYPE_GEOLOCATION, 69 std::string(), 70 false)); 71 EXPECT_TRUE(rule_iterator->HasNext()); 72 Rule rule = rule_iterator->Next(); 73 EXPECT_FALSE(rule_iterator->HasNext()); 74 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())); 78 79 provider.ShutdownOnUIThread(); 80} 81 82TEST_F(PolicyProviderTest, ManagedDefaultContentSettings) { 83 TestingProfile profile; 84 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); 85 PolicyProvider provider(prefs); 86 87 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, 88 new base::FundamentalValue(CONTENT_SETTING_BLOCK)); 89 90 scoped_ptr<RuleIterator> rule_iterator( 91 provider.GetRuleIterator( 92 CONTENT_SETTINGS_TYPE_PLUGINS, 93 std::string(), 94 false)); 95 EXPECT_TRUE(rule_iterator->HasNext()); 96 Rule rule = rule_iterator->Next(); 97 EXPECT_FALSE(rule_iterator->HasNext()); 98 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())); 102 103 provider.ShutdownOnUIThread(); 104} 105 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. 109TEST_F(PolicyProviderTest, ObserveManagedSettingsChange) { 110 TestingProfile profile; 111 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); 112 PolicyProvider provider(prefs); 113 114 MockObserver mock_observer; 115 EXPECT_CALL(mock_observer, 116 OnContentSettingChanged(_, 117 _, 118 CONTENT_SETTINGS_TYPE_DEFAULT, 119 "")); 120 provider.AddObserver(&mock_observer); 121 122 // Set the managed default-content-setting. 123 prefs->SetManagedPref(prefs::kManagedDefaultImagesSetting, 124 new base::FundamentalValue(CONTENT_SETTING_BLOCK)); 125 ::testing::Mock::VerifyAndClearExpectations(&mock_observer); 126 EXPECT_CALL(mock_observer, 127 OnContentSettingChanged(_, 128 _, 129 CONTENT_SETTINGS_TYPE_DEFAULT, 130 "")); 131 // Remove the managed default-content-setting. 132 prefs->RemoveManagedPref(prefs::kManagedDefaultImagesSetting); 133 provider.ShutdownOnUIThread(); 134} 135 136TEST_F(PolicyProviderTest, GettingManagedContentSettings) { 137 TestingProfile profile; 138 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); 139 140 base::ListValue* value = new base::ListValue(); 141 value->Append(new base::StringValue("[*.]google.com")); 142 prefs->SetManagedPref(prefs::kManagedImagesBlockedForUrls, 143 value); 144 145 PolicyProvider provider(prefs); 146 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"); 151 152 EXPECT_EQ(CONTENT_SETTING_DEFAULT, 153 GetContentSetting(&provider, 154 youtube_url, 155 youtube_url, 156 CONTENT_SETTINGS_TYPE_COOKIES, 157 std::string(), 158 false)); 159 EXPECT_EQ(NULL, 160 GetContentSettingValue(&provider, 161 youtube_url, 162 youtube_url, 163 CONTENT_SETTINGS_TYPE_COOKIES, 164 std::string(), 165 false)); 166 167 EXPECT_EQ(CONTENT_SETTING_BLOCK, 168 GetContentSetting(&provider, 169 google_url, 170 google_url, 171 CONTENT_SETTINGS_TYPE_IMAGES, 172 std::string(), 173 false)); 174 scoped_ptr<base::Value> value_ptr( 175 GetContentSettingValue(&provider, 176 google_url, 177 google_url, 178 CONTENT_SETTINGS_TYPE_IMAGES, 179 std::string(), 180 false)); 181 182 int int_value = -1; 183 value_ptr->GetAsInteger(&int_value); 184 EXPECT_EQ(CONTENT_SETTING_BLOCK, IntToContentSetting(int_value)); 185 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 new base::FundamentalValue(CONTENT_SETTING_BLOCK)); 191 bool owned = provider.SetWebsiteSetting(yt_url_pattern, 192 yt_url_pattern, 193 CONTENT_SETTINGS_TYPE_COOKIES, 194 std::string(), 195 value_block.get()); 196 EXPECT_FALSE(owned); 197 EXPECT_EQ(CONTENT_SETTING_DEFAULT, 198 GetContentSetting(&provider, 199 youtube_url, 200 youtube_url, 201 CONTENT_SETTINGS_TYPE_COOKIES, 202 std::string(), 203 false)); 204 205 provider.ShutdownOnUIThread(); 206} 207 208TEST_F(PolicyProviderTest, ResourceIdentifier) { 209 TestingProfile profile; 210 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); 211 212 base::ListValue* value = new base::ListValue(); 213 value->Append(new base::StringValue("[*.]google.com")); 214 prefs->SetManagedPref(prefs::kManagedPluginsAllowedForUrls, 215 value); 216 217 PolicyProvider provider(prefs); 218 219 GURL youtube_url("http://www.youtube.com"); 220 GURL google_url("http://mail.google.com"); 221 222 EXPECT_EQ(CONTENT_SETTING_DEFAULT, 223 GetContentSetting( 224 &provider, youtube_url, youtube_url, 225 CONTENT_SETTINGS_TYPE_PLUGINS, "someplugin", false)); 226 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, 231 google_url, 232 google_url, 233 CONTENT_SETTINGS_TYPE_PLUGINS, 234 std::string(), 235 false)); 236 237 EXPECT_EQ(CONTENT_SETTING_DEFAULT, 238 GetContentSetting( 239 &provider, google_url, google_url, 240 CONTENT_SETTINGS_TYPE_PLUGINS, "someplugin", false)); 241 242 provider.ShutdownOnUIThread(); 243} 244 245TEST_F(PolicyProviderTest, AutoSelectCertificateList) { 246 TestingProfile profile; 247 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); 248 249 PolicyProvider provider(prefs); 250 GURL google_url("https://mail.google.com"); 251 // Tests the default setting for auto selecting certificates 252 EXPECT_EQ( 253 NULL, 254 GetContentSettingValue(&provider, 255 google_url, 256 google_url, 257 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, 258 std::string(), 259 false)); 260 261 // Set the content settings pattern list for origins to auto select 262 // certificates. 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( 267 new base::StringValue("{" + pattern_str + "," + filter_str + "}")); 268 prefs->SetManagedPref(prefs::kManagedAutoSelectCertificateForUrls, 269 value); 270 GURL youtube_url("https://www.youtube.com"); 271 EXPECT_EQ( 272 NULL, 273 GetContentSettingValue(&provider, 274 youtube_url, 275 youtube_url, 276 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, 277 std::string(), 278 false)); 279 scoped_ptr<base::Value> cert_filter( 280 GetContentSettingValue(&provider, 281 google_url, 282 google_url, 283 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, 284 std::string(), 285 false)); 286 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(); 294} 295 296} // namespace content_settings 297