Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(542)

Side by Side Diff: chrome/browser/content_settings/host_content_settings_map_unittest.cc

Issue 1865803002: [Policy Experimental] Add policies to allow Cookies and Pop-ups exceptions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix compiling issuese; another round of renaming; code cleanup. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 maybe_enable_pref_exceptions = [&]()->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(maybe_enable_pref_exceptions());
822
823 // Policy enforces default value: No.
824 set_policy_default(CONTENT_SETTING_ALLOW);
825 EXPECT_FALSE(maybe_enable_pref_exceptions());
826 set_policy_default(CONTENT_SETTING_BLOCK);
827 EXPECT_FALSE(maybe_enable_pref_exceptions());
828
829 // Allow exceptions: Yes, regardless of policy default.
830 set_allow_user_exceptions(CONTENT_SETTING_ALLOW);
831 clear_policy_default();
832 EXPECT_TRUE(maybe_enable_pref_exceptions());
833 set_policy_default(CONTENT_SETTING_ALLOW);
834 EXPECT_TRUE(maybe_enable_pref_exceptions());
835 set_policy_default(CONTENT_SETTING_BLOCK);
836 EXPECT_TRUE(maybe_enable_pref_exceptions());
837
838 // Block exceptions: No, regardless of policy default.
839 set_allow_user_exceptions(CONTENT_SETTING_BLOCK);
840 clear_policy_default();
841 EXPECT_FALSE(maybe_enable_pref_exceptions());
842 set_policy_default(CONTENT_SETTING_ALLOW);
843 EXPECT_FALSE(maybe_enable_pref_exceptions());
844 set_policy_default(CONTENT_SETTING_BLOCK);
845 EXPECT_FALSE(maybe_enable_pref_exceptions());
846
847 // Not bothering caller clear_default() for this case; let it set.
848 }
849 }
850
851 TEST_F(HostContentSettingsMapTest, PrefExceptionsOperation) {
852 using content_settings::TestUtils;
853 // Arbitrarily using cookies as content type to test.
854 const ContentSettingsType kContentType = CONTENT_SETTINGS_TYPE_COOKIES;
855 const char* kPolicyDefaultSetting = prefs::kManagedDefaultCookiesSetting;
856 const char* kUserExceptionsUsage = prefs::kUserCookiesExceptionsUsage;
857 const char kUrlUserException[] = "http://user_except.com";
858 const char kUrlNonUserException[] = "http://non_user_except.com";
859
860 TestingProfile profile;
861 syncable_prefs::TestingPrefServiceSyncable* prefs =
862 profile.GetTestingPrefService();
863 HostContentSettingsMap* host_content_settings_map =
864 HostContentSettingsMapFactory::GetForProfile(&profile);
865
866 // Helpers to change policy settings.
867 auto clear_policy_default = [&]() {
868 prefs->RemoveManagedPref(kPolicyDefaultSetting);
869 };
870 auto set_policy_default = [&](ContentSetting setting) {
871 prefs->SetManagedPref(kPolicyDefaultSetting,
872 new base::FundamentalValue(setting));
873 };
874 auto clear_allow_user_exceptions = [&]() {
875 prefs->RemoveManagedPref(kUserExceptionsUsage);
876 };
877 auto set_allow_user_exceptions = [&](ContentSetting setting) {
878 prefs->SetManagedPref(kUserExceptionsUsage,
879 new base::FundamentalValue(setting));
880 };
881
882 // Wrapper to query GetWebsiteSetting(), and get the source.
883 auto get_source = [&](const std::string& url_str) ->
884 content_settings::SettingSource {
885 GURL host(url_str);
886 content_settings::SettingInfo setting_info;
887 scoped_ptr<base::Value> result =
888 host_content_settings_map->GetWebsiteSetting(
889 host, host, kContentType, std::string(),
890 &setting_info);
891 return setting_info.source;
892 };
893
894 // Add user exception.
895 TestUtils::AddPrefException(kUrlUserException, kContentType,
896 CONTENT_SETTING_ALLOW, host_content_settings_map);
897
898 using content_settings::SETTING_SOURCE_POLICY;
899 using content_settings::SETTING_SOURCE_USER;
900
901 // No policy setting: follow users settings.
902 clear_allow_user_exceptions();
903 clear_policy_default();
904 // User exceptions.
905 EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlUserException));
906 // User default.
907 EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlNonUserException));
908
909 // Policy overrids users always.
910 set_policy_default(CONTENT_SETTING_ALLOW);
911 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlUserException));
912 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlNonUserException));
913 set_policy_default(CONTENT_SETTING_BLOCK);
914 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlUserException));
915 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlNonUserException));
916
917 // Allow exceptions: user if exception found, else policy default (if set).
918 set_allow_user_exceptions(CONTENT_SETTING_ALLOW);
919 clear_policy_default();
920 EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlUserException));
921 EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlNonUserException));
922 set_policy_default(CONTENT_SETTING_ALLOW);
923 EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlUserException));
924 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlNonUserException));
925 set_policy_default(CONTENT_SETTING_BLOCK);
926 EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlUserException));
927 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlNonUserException));
928
929 // Block exceptions: policy overrides users always, and when policy is not
930 // set, user default catches everything.
931 set_allow_user_exceptions(CONTENT_SETTING_BLOCK);
932 clear_policy_default();
933 // User default -- unfortunately kProviderNamesSourceMap makes it ambiguous.
934 EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlUserException));
935 // User default.
936 EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlNonUserException));
937 set_policy_default(CONTENT_SETTING_ALLOW);
938 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlUserException));
939 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlNonUserException));
940 set_policy_default(CONTENT_SETTING_BLOCK);
941 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlUserException));
942 EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlNonUserException));
943 }
944
776 // For a single Unicode encoded pattern, check if it gets converted to punycode 945 // For a single Unicode encoded pattern, check if it gets converted to punycode
777 // and old pattern gets deleted. 946 // and old pattern gets deleted.
778 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) { 947 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) {
779 TestingProfile profile; 948 TestingProfile profile;
780 PrefService* prefs = profile.GetPrefs(); 949 PrefService* prefs = profile.GetPrefs();
781 950
782 // Set utf-8 data. 951 // Set utf-8 data.
783 { 952 {
784 DictionaryPrefUpdate update(prefs, 953 DictionaryPrefUpdate update(prefs,
785 GetPrefName(CONTENT_SETTINGS_TYPE_PLUGINS)); 954 GetPrefName(CONTENT_SETTINGS_TYPE_PLUGINS));
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
1184 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string())); 1353 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string()));
1185 1354
1186 // After migrating old settings, changes to the setting works. 1355 // After migrating old settings, changes to the setting works.
1187 host_content_settings_map->SetContentSettingDefaultScope( 1356 host_content_settings_map->SetContentSettingDefaultScope(
1188 host, GURL(), CONTENT_SETTINGS_TYPE_KEYGEN, std::string(), 1357 host, GURL(), CONTENT_SETTINGS_TYPE_KEYGEN, std::string(),
1189 CONTENT_SETTING_BLOCK); 1358 CONTENT_SETTING_BLOCK);
1190 EXPECT_EQ(CONTENT_SETTING_BLOCK, 1359 EXPECT_EQ(CONTENT_SETTING_BLOCK,
1191 host_content_settings_map->GetContentSetting( 1360 host_content_settings_map->GetContentSetting(
1192 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string())); 1361 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string()));
1193 } 1362 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698