| OLD | NEW | 
|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "components/content_settings/core/browser/content_settings_policy_provi
     der.h" | 5 #include "components/content_settings/core/browser/content_settings_policy_provi
     der.h" | 
| 6 | 6 | 
| 7 #include <memory> | 7 #include <memory> | 
| 8 #include <string> | 8 #include <string> | 
| 9 | 9 | 
| 10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" | 
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" | 
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" | 
| 13 #include "base/test/scoped_feature_list.h" | 13 #include "base/test/scoped_feature_list.h" | 
| 14 #include "chrome/browser/content_settings/content_settings_mock_observer.h" | 14 #include "chrome/browser/content_settings/content_settings_mock_observer.h" | 
| 15 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" | 
| 16 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" | 
| 17 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" | 
| 18 #include "components/content_settings/core/browser/content_settings_rule.h" | 18 #include "components/content_settings/core/browser/content_settings_rule.h" | 
| 19 #include "components/content_settings/core/browser/content_settings_utils.h" | 19 #include "components/content_settings/core/browser/content_settings_utils.h" | 
| 20 #include "components/content_settings/core/common/content_settings_pattern.h" | 20 #include "components/content_settings/core/common/content_settings_pattern.h" | 
| 21 #include "components/content_settings/core/common/pref_names.h" | 21 #include "components/content_settings/core/common/pref_names.h" | 
| 22 #include "components/content_settings/core/test/content_settings_test_utils.h" | 22 #include "components/content_settings/core/test/content_settings_test_utils.h" | 
| 23 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" | 
| 24 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 24 #include "components/sync_preferences/testing_pref_service_syncable.h" | 
| 25 #include "content/public/test/test_browser_thread_bundle.h" | 25 #include "content/public/test/test_browser_thread_bundle.h" | 
| 26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" | 
| 27 #include "url/gurl.h" | 27 #include "url/gurl.h" | 
| 28 | 28 | 
| 29 using ::testing::_; | 29 using ::testing::_; | 
| 30 | 30 | 
| 31 namespace content_settings { | 31 namespace content_settings { | 
| 32 | 32 | 
| 33 typedef std::vector<Rule> Rules; | 33 typedef std::vector<Rule> Rules; | 
| 34 | 34 | 
| 35 class PolicyProviderTest : public testing::Test { | 35 class PolicyProviderTest : public testing::Test { | 
| 36   content::TestBrowserThreadBundle thread_bundle_; | 36   content::TestBrowserThreadBundle thread_bundle_; | 
| 37 }; | 37 }; | 
| 38 | 38 | 
| 39 TEST_F(PolicyProviderTest, DefaultGeolocationContentSetting) { | 39 TEST_F(PolicyProviderTest, DefaultGeolocationContentSetting) { | 
| 40   TestingProfile profile; | 40   TestingProfile profile; | 
| 41   syncable_prefs::TestingPrefServiceSyncable* prefs = | 41   sync_preferences::TestingPrefServiceSyncable* prefs = | 
| 42       profile.GetTestingPrefService(); | 42       profile.GetTestingPrefService(); | 
| 43   PolicyProvider provider(prefs); | 43   PolicyProvider provider(prefs); | 
| 44 | 44 | 
| 45   std::unique_ptr<RuleIterator> rule_iterator(provider.GetRuleIterator( | 45   std::unique_ptr<RuleIterator> rule_iterator(provider.GetRuleIterator( | 
| 46       CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), false)); | 46       CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), false)); | 
| 47   EXPECT_FALSE(rule_iterator); | 47   EXPECT_FALSE(rule_iterator); | 
| 48 | 48 | 
| 49   // Change the managed value of the default geolocation setting | 49   // Change the managed value of the default geolocation setting | 
| 50   prefs->SetManagedPref(prefs::kManagedDefaultGeolocationSetting, | 50   prefs->SetManagedPref(prefs::kManagedDefaultGeolocationSetting, | 
| 51                         new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | 51                         new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | 
| 52 | 52 | 
| 53   rule_iterator = provider.GetRuleIterator(CONTENT_SETTINGS_TYPE_GEOLOCATION, | 53   rule_iterator = provider.GetRuleIterator(CONTENT_SETTINGS_TYPE_GEOLOCATION, | 
| 54                                            std::string(), false); | 54                                            std::string(), false); | 
| 55   ASSERT_TRUE(rule_iterator); | 55   ASSERT_TRUE(rule_iterator); | 
| 56   EXPECT_TRUE(rule_iterator->HasNext()); | 56   EXPECT_TRUE(rule_iterator->HasNext()); | 
| 57   Rule rule = rule_iterator->Next(); | 57   Rule rule = rule_iterator->Next(); | 
| 58   EXPECT_FALSE(rule_iterator->HasNext()); | 58   EXPECT_FALSE(rule_iterator->HasNext()); | 
| 59 | 59 | 
| 60   EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern); | 60   EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern); | 
| 61   EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern); | 61   EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern); | 
| 62   EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get())); | 62   EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get())); | 
| 63 | 63 | 
| 64   provider.ShutdownOnUIThread(); | 64   provider.ShutdownOnUIThread(); | 
| 65 } | 65 } | 
| 66 | 66 | 
| 67 TEST_F(PolicyProviderTest, ManagedDefaultContentSettings) { | 67 TEST_F(PolicyProviderTest, ManagedDefaultContentSettings) { | 
| 68   TestingProfile profile; | 68   TestingProfile profile; | 
| 69   syncable_prefs::TestingPrefServiceSyncable* prefs = | 69   sync_preferences::TestingPrefServiceSyncable* prefs = | 
| 70       profile.GetTestingPrefService(); | 70       profile.GetTestingPrefService(); | 
| 71   PolicyProvider provider(prefs); | 71   PolicyProvider provider(prefs); | 
| 72 | 72 | 
| 73   prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, | 73   prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, | 
| 74                         new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | 74                         new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | 
| 75 | 75 | 
| 76   std::unique_ptr<RuleIterator> rule_iterator(provider.GetRuleIterator( | 76   std::unique_ptr<RuleIterator> rule_iterator(provider.GetRuleIterator( | 
| 77       CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), false)); | 77       CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), false)); | 
| 78   EXPECT_TRUE(rule_iterator->HasNext()); | 78   EXPECT_TRUE(rule_iterator->HasNext()); | 
| 79   Rule rule = rule_iterator->Next(); | 79   Rule rule = rule_iterator->Next(); | 
| 80   EXPECT_FALSE(rule_iterator->HasNext()); | 80   EXPECT_FALSE(rule_iterator->HasNext()); | 
| 81 | 81 | 
| 82   EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern); | 82   EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern); | 
| 83   EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern); | 83   EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern); | 
| 84   EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get())); | 84   EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get())); | 
| 85 | 85 | 
| 86   provider.ShutdownOnUIThread(); | 86   provider.ShutdownOnUIThread(); | 
| 87 } | 87 } | 
| 88 | 88 | 
| 89 TEST_F(PolicyProviderTest, ManagedDefaultPluginSettingsExperiment) { | 89 TEST_F(PolicyProviderTest, ManagedDefaultPluginSettingsExperiment) { | 
| 90   base::test::ScopedFeatureList scoped_feature_list; | 90   base::test::ScopedFeatureList scoped_feature_list; | 
| 91   scoped_feature_list.InitFromCommandLine("IgnoreDefaultPluginsSetting", | 91   scoped_feature_list.InitFromCommandLine("IgnoreDefaultPluginsSetting", | 
| 92                                           std::string()); | 92                                           std::string()); | 
| 93 | 93 | 
| 94   TestingProfile profile; | 94   TestingProfile profile; | 
| 95   syncable_prefs::TestingPrefServiceSyncable* prefs = | 95   sync_preferences::TestingPrefServiceSyncable* prefs = | 
| 96       profile.GetTestingPrefService(); | 96       profile.GetTestingPrefService(); | 
| 97   PolicyProvider provider(prefs); | 97   PolicyProvider provider(prefs); | 
| 98 | 98 | 
| 99   // ForceDefaultPluginsSettingAsk overrides this to ASK. | 99   // ForceDefaultPluginsSettingAsk overrides this to ASK. | 
| 100   prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, | 100   prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, | 
| 101                         new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | 101                         new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | 
| 102   prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting, | 102   prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting, | 
| 103                         new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | 103                         new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | 
| 104 | 104 | 
| 105   std::unique_ptr<RuleIterator> plugin_rule_iterator(provider.GetRuleIterator( | 105   std::unique_ptr<RuleIterator> plugin_rule_iterator(provider.GetRuleIterator( | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
| 119   EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get())); | 119   EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get())); | 
| 120 | 120 | 
| 121   provider.ShutdownOnUIThread(); | 121   provider.ShutdownOnUIThread(); | 
| 122 } | 122 } | 
| 123 | 123 | 
| 124 // When a default-content-setting is set to a managed setting a | 124 // When a default-content-setting is set to a managed setting a | 
| 125 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen | 125 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen | 
| 126 // if the managed setting is removed. | 126 // if the managed setting is removed. | 
| 127 TEST_F(PolicyProviderTest, ObserveManagedSettingsChange) { | 127 TEST_F(PolicyProviderTest, ObserveManagedSettingsChange) { | 
| 128   TestingProfile profile; | 128   TestingProfile profile; | 
| 129   syncable_prefs::TestingPrefServiceSyncable* prefs = | 129   sync_preferences::TestingPrefServiceSyncable* prefs = | 
| 130       profile.GetTestingPrefService(); | 130       profile.GetTestingPrefService(); | 
| 131   PolicyProvider provider(prefs); | 131   PolicyProvider provider(prefs); | 
| 132 | 132 | 
| 133   MockObserver mock_observer; | 133   MockObserver mock_observer; | 
| 134   EXPECT_CALL(mock_observer, | 134   EXPECT_CALL(mock_observer, | 
| 135               OnContentSettingChanged(_, | 135               OnContentSettingChanged(_, | 
| 136                                       _, | 136                                       _, | 
| 137                                       CONTENT_SETTINGS_TYPE_DEFAULT, | 137                                       CONTENT_SETTINGS_TYPE_DEFAULT, | 
| 138                                       "")); | 138                                       "")); | 
| 139   provider.AddObserver(&mock_observer); | 139   provider.AddObserver(&mock_observer); | 
| 140 | 140 | 
| 141   // Set the managed default-content-setting. | 141   // Set the managed default-content-setting. | 
| 142   prefs->SetManagedPref(prefs::kManagedDefaultImagesSetting, | 142   prefs->SetManagedPref(prefs::kManagedDefaultImagesSetting, | 
| 143                         new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | 143                         new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | 
| 144   ::testing::Mock::VerifyAndClearExpectations(&mock_observer); | 144   ::testing::Mock::VerifyAndClearExpectations(&mock_observer); | 
| 145   EXPECT_CALL(mock_observer, | 145   EXPECT_CALL(mock_observer, | 
| 146               OnContentSettingChanged(_, | 146               OnContentSettingChanged(_, | 
| 147                                       _, | 147                                       _, | 
| 148                                       CONTENT_SETTINGS_TYPE_DEFAULT, | 148                                       CONTENT_SETTINGS_TYPE_DEFAULT, | 
| 149                                       "")); | 149                                       "")); | 
| 150   // Remove the managed default-content-setting. | 150   // Remove the managed default-content-setting. | 
| 151   prefs->RemoveManagedPref(prefs::kManagedDefaultImagesSetting); | 151   prefs->RemoveManagedPref(prefs::kManagedDefaultImagesSetting); | 
| 152   provider.ShutdownOnUIThread(); | 152   provider.ShutdownOnUIThread(); | 
| 153 } | 153 } | 
| 154 | 154 | 
| 155 TEST_F(PolicyProviderTest, GettingManagedContentSettings) { | 155 TEST_F(PolicyProviderTest, GettingManagedContentSettings) { | 
| 156   TestingProfile profile; | 156   TestingProfile profile; | 
| 157   syncable_prefs::TestingPrefServiceSyncable* prefs = | 157   sync_preferences::TestingPrefServiceSyncable* prefs = | 
| 158       profile.GetTestingPrefService(); | 158       profile.GetTestingPrefService(); | 
| 159 | 159 | 
| 160   base::ListValue* value = new base::ListValue(); | 160   base::ListValue* value = new base::ListValue(); | 
| 161   value->AppendString("[*.]google.com"); | 161   value->AppendString("[*.]google.com"); | 
| 162   prefs->SetManagedPref(prefs::kManagedImagesBlockedForUrls, | 162   prefs->SetManagedPref(prefs::kManagedImagesBlockedForUrls, | 
| 163                         value); | 163                         value); | 
| 164 | 164 | 
| 165   PolicyProvider provider(prefs); | 165   PolicyProvider provider(prefs); | 
| 166 | 166 | 
| 167   ContentSettingsPattern yt_url_pattern = | 167   ContentSettingsPattern yt_url_pattern = | 
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 203   EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 203   EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 
| 204             TestUtils::GetContentSetting(&provider, youtube_url, youtube_url, | 204             TestUtils::GetContentSetting(&provider, youtube_url, youtube_url, | 
| 205                                          CONTENT_SETTINGS_TYPE_COOKIES, | 205                                          CONTENT_SETTINGS_TYPE_COOKIES, | 
| 206                                          std::string(), false)); | 206                                          std::string(), false)); | 
| 207 | 207 | 
| 208   provider.ShutdownOnUIThread(); | 208   provider.ShutdownOnUIThread(); | 
| 209 } | 209 } | 
| 210 | 210 | 
| 211 TEST_F(PolicyProviderTest, ResourceIdentifier) { | 211 TEST_F(PolicyProviderTest, ResourceIdentifier) { | 
| 212   TestingProfile profile; | 212   TestingProfile profile; | 
| 213   syncable_prefs::TestingPrefServiceSyncable* prefs = | 213   sync_preferences::TestingPrefServiceSyncable* prefs = | 
| 214       profile.GetTestingPrefService(); | 214       profile.GetTestingPrefService(); | 
| 215 | 215 | 
| 216   base::ListValue* value = new base::ListValue(); | 216   base::ListValue* value = new base::ListValue(); | 
| 217   value->AppendString("[*.]google.com"); | 217   value->AppendString("[*.]google.com"); | 
| 218   prefs->SetManagedPref(prefs::kManagedPluginsAllowedForUrls, | 218   prefs->SetManagedPref(prefs::kManagedPluginsAllowedForUrls, | 
| 219                         value); | 219                         value); | 
| 220 | 220 | 
| 221   PolicyProvider provider(prefs); | 221   PolicyProvider provider(prefs); | 
| 222 | 222 | 
| 223   GURL youtube_url("http://www.youtube.com"); | 223   GURL youtube_url("http://www.youtube.com"); | 
| (...skipping 14 matching lines...) Expand all  Loading... | 
| 238   EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 238   EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 
| 239             TestUtils::GetContentSetting(&provider, google_url, google_url, | 239             TestUtils::GetContentSetting(&provider, google_url, google_url, | 
| 240                                          CONTENT_SETTINGS_TYPE_PLUGINS, | 240                                          CONTENT_SETTINGS_TYPE_PLUGINS, | 
| 241                                          "someplugin", false)); | 241                                          "someplugin", false)); | 
| 242 | 242 | 
| 243   provider.ShutdownOnUIThread(); | 243   provider.ShutdownOnUIThread(); | 
| 244 } | 244 } | 
| 245 | 245 | 
| 246 TEST_F(PolicyProviderTest, AutoSelectCertificateList) { | 246 TEST_F(PolicyProviderTest, AutoSelectCertificateList) { | 
| 247   TestingProfile profile; | 247   TestingProfile profile; | 
| 248   syncable_prefs::TestingPrefServiceSyncable* prefs = | 248   sync_preferences::TestingPrefServiceSyncable* prefs = | 
| 249       profile.GetTestingPrefService(); | 249       profile.GetTestingPrefService(); | 
| 250 | 250 | 
| 251   PolicyProvider provider(prefs); | 251   PolicyProvider provider(prefs); | 
| 252   GURL google_url("https://mail.google.com"); | 252   GURL google_url("https://mail.google.com"); | 
| 253   // Tests the default setting for auto selecting certificates | 253   // Tests the default setting for auto selecting certificates | 
| 254   EXPECT_EQ(NULL, TestUtils::GetContentSettingValue( | 254   EXPECT_EQ(NULL, TestUtils::GetContentSettingValue( | 
| 255                       &provider, google_url, google_url, | 255                       &provider, google_url, google_url, | 
| 256                       CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, | 256                       CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, | 
| 257                       std::string(), false)); | 257                       std::string(), false)); | 
| 258 | 258 | 
| (...skipping 17 matching lines...) Expand all  Loading... | 
| 276   ASSERT_EQ(base::Value::TYPE_DICTIONARY, cert_filter->GetType()); | 276   ASSERT_EQ(base::Value::TYPE_DICTIONARY, cert_filter->GetType()); | 
| 277   base::DictionaryValue* dict_value = | 277   base::DictionaryValue* dict_value = | 
| 278       static_cast<base::DictionaryValue*>(cert_filter.get()); | 278       static_cast<base::DictionaryValue*>(cert_filter.get()); | 
| 279   std::string actual_common_name; | 279   std::string actual_common_name; | 
| 280   ASSERT_TRUE(dict_value->GetString("ISSUER.CN", &actual_common_name)); | 280   ASSERT_TRUE(dict_value->GetString("ISSUER.CN", &actual_common_name)); | 
| 281   EXPECT_EQ("issuer name", actual_common_name); | 281   EXPECT_EQ("issuer name", actual_common_name); | 
| 282   provider.ShutdownOnUIThread(); | 282   provider.ShutdownOnUIThread(); | 
| 283 } | 283 } | 
| 284 | 284 | 
| 285 }  // namespace content_settings | 285 }  // namespace content_settings | 
| OLD | NEW | 
|---|