| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/auto_reset.h" | 5 #include "base/auto_reset.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "chrome/browser/content_settings/content_settings_mock_observer.h" | 10 #include "chrome/browser/content_settings/content_settings_mock_observer.h" |
| 11 #include "chrome/browser/content_settings/cookie_settings_factory.h" | 11 #include "chrome/browser/content_settings/cookie_settings_factory.h" |
| 12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 13 #include "chrome/browser/content_settings/mock_settings_observer.h" | 13 #include "chrome/browser/content_settings/mock_settings_observer.h" |
| 14 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 15 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "components/content_settings/core/browser/content_settings_details.h" | 16 #include "components/content_settings/core/browser/content_settings_details.h" |
| 17 #include "components/content_settings/core/browser/cookie_settings.h" | 17 #include "components/content_settings/core/browser/cookie_settings.h" |
| 18 #include "components/content_settings/core/browser/host_content_settings_map.h" | 18 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 19 #include "components/content_settings/core/browser/website_settings_info.h" | 19 #include "components/content_settings/core/browser/website_settings_info.h" |
| 20 #include "components/content_settings/core/browser/website_settings_registry.h" | 20 #include "components/content_settings/core/browser/website_settings_registry.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/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
| 23 #include "components/prefs/scoped_user_pref_update.h" | 24 #include "components/prefs/scoped_user_pref_update.h" |
| 24 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 25 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
| 25 #include "content/public/test/test_browser_thread.h" | 26 #include "content/public/test/test_browser_thread.h" |
| 26 #include "net/base/static_cookie_policy.h" | 27 #include "net/base/static_cookie_policy.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 28 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 29 | 30 |
| 30 using content::BrowserThread; | 31 using content::BrowserThread; |
| 31 | 32 |
| (...skipping 734 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 766 scoped_ptr<base::Value> stored_value = | 767 scoped_ptr<base::Value> stored_value = |
| 767 host_content_settings_map->GetWebsiteSetting( | 768 host_content_settings_map->GetWebsiteSetting( |
| 768 host, host, CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA, std::string(), | 769 host, host, CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA, std::string(), |
| 769 nullptr); | 770 nullptr); |
| 770 EXPECT_TRUE(stored_value && stored_value->Equals(&test_value)); | 771 EXPECT_TRUE(stored_value && stored_value->Equals(&test_value)); |
| 771 EXPECT_EQ(nullptr, otr_map->GetWebsiteSetting( | 772 EXPECT_EQ(nullptr, otr_map->GetWebsiteSetting( |
| 772 host, host, CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA, | 773 host, host, CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA, |
| 773 std::string(), nullptr)); | 774 std::string(), nullptr)); |
| 774 } | 775 } |
| 775 | 776 |
| 777 TEST_F(HostContentSettingsMapTest, AreUserExceptionsAllowedForType) { |
| 778 struct { |
| 779 ContentSettingsType content_type; |
| 780 const char* policy_default_setting; |
| 781 const char* pref_exception_setting; |
| 782 } kTypeInfos[] = { |
| 783 {CONTENT_SETTINGS_TYPE_COOKIES, prefs::kManagedDefaultCookiesSetting, |
| 784 prefs::kUserCookiesExceptionsUsage}, |
| 785 {CONTENT_SETTINGS_TYPE_POPUPS, prefs::kManagedDefaultPopupsSetting, |
| 786 prefs::kUserPopupsExceptionsUsage}, |
| 787 }; |
| 788 |
| 789 TestingProfile profile; |
| 790 syncable_prefs::TestingPrefServiceSyncable* prefs = |
| 791 profile.GetTestingPrefService(); |
| 792 HostContentSettingsMap* host_content_settings_map = |
| 793 HostContentSettingsMapFactory::GetForProfile(&profile); |
| 794 |
| 795 for (const auto& type_info : kTypeInfos) { |
| 796 // Lambdas to simplify setting valuesfor current test case. |
| 797 auto clear_policy_default = [&]() { |
| 798 prefs->RemoveManagedPref(type_info.policy_default_setting); |
| 799 }; |
| 800 auto set_policy_default = [&](ContentSetting setting) { |
| 801 prefs->SetManagedPref(type_info.policy_default_setting, |
| 802 new base::FundamentalValue(setting)); |
| 803 }; |
| 804 auto clear_allow_user_exceptions = [&]() { |
| 805 prefs->RemoveManagedPref(type_info.pref_exception_setting); |
| 806 }; |
| 807 auto set_allow_user_exceptions = [&](ContentSetting setting) { |
| 808 prefs->SetManagedPref(type_info.pref_exception_setting, |
| 809 new base::FundamentalValue(setting)); |
| 810 }; |
| 811 |
| 812 // Main function to test: Can user edit pref exceptions? |
| 813 auto are_pref_exceptions_allowed = [&]()->bool { |
| 814 return host_content_settings_map->AreUserExceptionsAllowedForType( |
| 815 type_info.content_type); |
| 816 }; |
| 817 |
| 818 // No settings: Yes. |
| 819 clear_allow_user_exceptions(); |
| 820 clear_policy_default(); |
| 821 EXPECT_TRUE(are_pref_exceptions_allowed()); |
| 822 |
| 823 // Policy enforces default value: No. |
| 824 set_policy_default(CONTENT_SETTING_ALLOW); |
| 825 EXPECT_FALSE(are_pref_exceptions_allowed()); |
| 826 set_policy_default(CONTENT_SETTING_BLOCK); |
| 827 EXPECT_FALSE(are_pref_exceptions_allowed()); |
| 828 |
| 829 // Allow exceptions: Yes, regardless of policy default. |
| 830 set_allow_user_exceptions(CONTENT_SETTING_ALLOW); |
| 831 clear_policy_default(); |
| 832 EXPECT_TRUE(are_pref_exceptions_allowed()); |
| 833 set_policy_default(CONTENT_SETTING_ALLOW); |
| 834 EXPECT_TRUE(are_pref_exceptions_allowed()); |
| 835 set_policy_default(CONTENT_SETTING_BLOCK); |
| 836 EXPECT_TRUE(are_pref_exceptions_allowed()); |
| 837 |
| 838 // Not bothering caller clear_default() for this case; let it set. |
| 839 } |
| 840 } |
| 841 |
| 842 TEST_F(HostContentSettingsMapTest, PrefExceptionsOperation) { |
| 843 using content_settings::TestUtils; |
| 844 // Arbitrarily using cookies as content type to test. |
| 845 const ContentSettingsType kContentType = CONTENT_SETTINGS_TYPE_COOKIES; |
| 846 const char* kPolicyDefaultSetting = prefs::kManagedDefaultCookiesSetting; |
| 847 const char* kUserExceptionsUsage = prefs::kUserCookiesExceptionsUsage; |
| 848 const char kUrlUserException[] = "http://user_except.com"; |
| 849 const char kUrlNonUserException[] = "http://non_user_except.com"; |
| 850 |
| 851 TestingProfile profile; |
| 852 syncable_prefs::TestingPrefServiceSyncable* prefs = |
| 853 profile.GetTestingPrefService(); |
| 854 HostContentSettingsMap* host_content_settings_map = |
| 855 HostContentSettingsMapFactory::GetForProfile(&profile); |
| 856 |
| 857 // Helpers to change policy settings. |
| 858 auto clear_policy_default = [&]() { |
| 859 prefs->RemoveManagedPref(kPolicyDefaultSetting); |
| 860 }; |
| 861 auto set_policy_default = [&](ContentSetting setting) { |
| 862 prefs->SetManagedPref(kPolicyDefaultSetting, |
| 863 new base::FundamentalValue(setting)); |
| 864 }; |
| 865 auto clear_allow_user_exceptions = [&]() { |
| 866 prefs->RemoveManagedPref(kUserExceptionsUsage); |
| 867 }; |
| 868 auto set_allow_user_exceptions = [&](ContentSetting setting) { |
| 869 prefs->SetManagedPref(kUserExceptionsUsage, |
| 870 new base::FundamentalValue(setting)); |
| 871 }; |
| 872 |
| 873 // Wrapper to query GetWebsiteSetting(), and get the source. |
| 874 auto get_source = [&](const std::string& url_str) -> |
| 875 content_settings::SettingSource { |
| 876 GURL host(url_str); |
| 877 content_settings::SettingInfo setting_info; |
| 878 scoped_ptr<base::Value> result = |
| 879 host_content_settings_map->GetWebsiteSetting( |
| 880 host, host, kContentType, std::string(), |
| 881 &setting_info); |
| 882 return setting_info.source; |
| 883 }; |
| 884 |
| 885 // Add user exception. |
| 886 TestUtils::AddPrefException(kUrlUserException, kContentType, |
| 887 CONTENT_SETTING_ALLOW, host_content_settings_map); |
| 888 |
| 889 using content_settings::SETTING_SOURCE_POLICY; |
| 890 using content_settings::SETTING_SOURCE_USER; |
| 891 |
| 892 // No policy setting: follow users settings. |
| 893 clear_allow_user_exceptions(); |
| 894 clear_policy_default(); |
| 895 // User exceptions. |
| 896 EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlUserException)); |
| 897 // User default. |
| 898 EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlNonUserException)); |
| 899 |
| 900 // Policy overrids users always. |
| 901 set_policy_default(CONTENT_SETTING_ALLOW); |
| 902 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlUserException)); |
| 903 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlNonUserException)); |
| 904 set_policy_default(CONTENT_SETTING_BLOCK); |
| 905 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlUserException)); |
| 906 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlNonUserException)); |
| 907 |
| 908 // Allow exceptions: user if exception found, else policy default (if set). |
| 909 set_allow_user_exceptions(CONTENT_SETTING_ALLOW); |
| 910 clear_policy_default(); |
| 911 EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlUserException)); |
| 912 EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlNonUserException)); |
| 913 set_policy_default(CONTENT_SETTING_ALLOW); |
| 914 EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlUserException)); |
| 915 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlNonUserException)); |
| 916 set_policy_default(CONTENT_SETTING_BLOCK); |
| 917 EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlUserException)); |
| 918 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlNonUserException)); |
| 919 } |
| 920 |
| 776 // For a single Unicode encoded pattern, check if it gets converted to punycode | 921 // For a single Unicode encoded pattern, check if it gets converted to punycode |
| 777 // and old pattern gets deleted. | 922 // and old pattern gets deleted. |
| 778 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) { | 923 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) { |
| 779 TestingProfile profile; | 924 TestingProfile profile; |
| 780 PrefService* prefs = profile.GetPrefs(); | 925 PrefService* prefs = profile.GetPrefs(); |
| 781 | 926 |
| 782 // Set utf-8 data. | 927 // Set utf-8 data. |
| 783 { | 928 { |
| 784 DictionaryPrefUpdate update(prefs, | 929 DictionaryPrefUpdate update(prefs, |
| 785 GetPrefName(CONTENT_SETTINGS_TYPE_PLUGINS)); | 930 GetPrefName(CONTENT_SETTINGS_TYPE_PLUGINS)); |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1184 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string())); | 1329 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string())); |
| 1185 | 1330 |
| 1186 // After migrating old settings, changes to the setting works. | 1331 // After migrating old settings, changes to the setting works. |
| 1187 host_content_settings_map->SetContentSettingDefaultScope( | 1332 host_content_settings_map->SetContentSettingDefaultScope( |
| 1188 host, GURL(), CONTENT_SETTINGS_TYPE_KEYGEN, std::string(), | 1333 host, GURL(), CONTENT_SETTINGS_TYPE_KEYGEN, std::string(), |
| 1189 CONTENT_SETTING_BLOCK); | 1334 CONTENT_SETTING_BLOCK); |
| 1190 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 1335 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 1191 host_content_settings_map->GetContentSetting( | 1336 host_content_settings_map->GetContentSetting( |
| 1192 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string())); | 1337 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string())); |
| 1193 } | 1338 } |
| OLD | NEW |