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

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

Issue 1873343002: [Policy] HostContentSettingsMap: Add AreUserExceptionsAllowedForType() and tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 <memory>
6
5 #include "base/auto_reset.h" 7 #include "base/auto_reset.h"
6 #include "base/command_line.h" 8 #include "base/command_line.h"
7 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
9 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/content_settings/content_settings_mock_observer.h" 12 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
11 #include "chrome/browser/content_settings/cookie_settings_factory.h" 13 #include "chrome/browser/content_settings/cookie_settings_factory.h"
12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
13 #include "chrome/browser/content_settings/mock_settings_observer.h" 15 #include "chrome/browser/content_settings/mock_settings_observer.h"
14 #include "chrome/common/url_constants.h" 16 #include "chrome/common/url_constants.h"
15 #include "chrome/test/base/testing_profile.h" 17 #include "chrome/test/base/testing_profile.h"
16 #include "components/content_settings/core/browser/content_settings_details.h" 18 #include "components/content_settings/core/browser/content_settings_details.h"
17 #include "components/content_settings/core/browser/cookie_settings.h" 19 #include "components/content_settings/core/browser/cookie_settings.h"
18 #include "components/content_settings/core/browser/host_content_settings_map.h" 20 #include "components/content_settings/core/browser/host_content_settings_map.h"
19 #include "components/content_settings/core/browser/website_settings_info.h" 21 #include "components/content_settings/core/browser/website_settings_info.h"
20 #include "components/content_settings/core/browser/website_settings_registry.h" 22 #include "components/content_settings/core/browser/website_settings_registry.h"
21 #include "components/content_settings/core/common/pref_names.h" 23 #include "components/content_settings/core/common/pref_names.h"
24 #include "components/content_settings/core/test/content_settings_test_utils.h"
22 #include "components/prefs/pref_service.h" 25 #include "components/prefs/pref_service.h"
23 #include "components/prefs/scoped_user_pref_update.h" 26 #include "components/prefs/scoped_user_pref_update.h"
24 #include "components/syncable_prefs/testing_pref_service_syncable.h" 27 #include "components/syncable_prefs/testing_pref_service_syncable.h"
25 #include "content/public/test/test_browser_thread.h" 28 #include "content/public/test/test_browser_thread.h"
26 #include "net/base/static_cookie_policy.h" 29 #include "net/base/static_cookie_policy.h"
27 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
28 #include "url/gurl.h" 31 #include "url/gurl.h"
29 32
30 using content::BrowserThread; 33 using content::BrowserThread;
31 34
32 using ::testing::_; 35 using ::testing::_;
33 36
34 class HostContentSettingsMapTest : public testing::Test { 37 class HostContentSettingsMapTest : public testing::Test {
35 public: 38 public:
36 HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI, &message_loop_) { 39 HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI, &message_loop_) {
37 } 40 }
38 41
39 protected: 42 protected:
40 const std::string& GetPrefName(ContentSettingsType type) { 43 const std::string& GetPrefName(ContentSettingsType type) {
41 return content_settings::WebsiteSettingsRegistry::GetInstance() 44 return content_settings::WebsiteSettingsRegistry::GetInstance()
42 ->Get(type) 45 ->Get(type)
43 ->pref_name(); 46 ->pref_name();
44 } 47 }
45 48
46 base::MessageLoop message_loop_; 49 base::MessageLoop message_loop_;
47 content::TestBrowserThread ui_thread_; 50 content::TestBrowserThread ui_thread_;
48 }; 51 };
49 52
53 // Helper that wrap test boilerplates, and maintains a "current content type"
huangs 2016/04/11 21:37:33 Adding this layer of abstraction makes test much s
54 // state so caller needs not repeatedly specify content type.
55 class HostContentSettingsMapTester {
raymes 2016/04/12 04:52:47 Since these are specific to your tests, how about
huangs 2016/04/13 17:36:18 Changing this to just a wrapper: - Renaming to Tes
56 public:
57 HostContentSettingsMapTester() {
58 profile_.reset(new TestingProfile);
59 prefs_ = profile_->GetTestingPrefService();
60 host_content_settings_map_ =
61 HostContentSettingsMapFactory::GetForProfile(profile_.get());
62 }
63
64 // Chainable.
65 HostContentSettingsMapTester& SelectType(ContentSettingsType content_type) {
raymes 2016/04/12 04:52:47 nit: How about removing this function (and not tra
huangs 2016/04/12 14:19:13 The ability to select "cur" state once to establis
66 cur_content_type_ = content_type;
67 switch (cur_content_type_) {
68 case CONTENT_SETTINGS_TYPE_COOKIES:
69 cur_policy_default_setting_ = prefs::kManagedDefaultCookiesSetting;
70 break;
71 case CONTENT_SETTINGS_TYPE_POPUPS:
72 cur_policy_default_setting_ = prefs::kManagedDefaultPopupsSetting;
73 break;
74 default:
75 // Add support as needed.
76 NOTREACHED();
77 }
78 return *this;
79 }
80
81 void ClearPolicyDefault() {
82 prefs_->RemoveManagedPref(cur_policy_default_setting_);
83 }
84
85 void SetPolicyDefault(ContentSetting setting) {
86 prefs_->SetManagedPref(cur_policy_default_setting_,
87 new base::FundamentalValue(setting));
88 }
89
90 bool AreUserExceptionsAllowed() {
91 return host_content_settings_map_->AreUserExceptionsAllowedForType(
92 cur_content_type_);
93 }
94
95 void AddUserException(std::string exception,
96 ContentSetting content_settings) {
97 content_settings::TestUtils::AddUserException(exception, cur_content_type_,
98 content_settings, host_content_settings_map_);
99 }
100
101 // Wrapper to query GetWebsiteSetting(), and only return the source.
102 content_settings::SettingSource SourceMatch(
raymes 2016/04/12 04:52:47 nit: GetSettingSourceForURL?
huangs 2016/04/13 17:36:18 Done.
103 const std::string& url_str) {
104 GURL host(url_str);
Bernhard Bauer 2016/04/12 09:30:05 Should this be |url|?
huangs 2016/04/13 17:36:18 Done.
105 content_settings::SettingInfo setting_info;
106 scoped_ptr<base::Value> result =
107 host_content_settings_map_->GetWebsiteSetting(
108 host, host, cur_content_type_, std::string(),
109 &setting_info);
110 return setting_info.source;
111 };
112
113 private:
114 std::unique_ptr<TestingProfile> profile_;
115 syncable_prefs::TestingPrefServiceSyncable* prefs_ = nullptr;
116 HostContentSettingsMap* host_content_settings_map_ = nullptr;
117 ContentSettingsType cur_content_type_ = CONTENT_SETTINGS_TYPE_DEFAULT;
118 const char* cur_policy_default_setting_;
119
120 DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMapTester);
121 };
122
50 TEST_F(HostContentSettingsMapTest, DefaultValues) { 123 TEST_F(HostContentSettingsMapTest, DefaultValues) {
51 TestingProfile profile; 124 TestingProfile profile;
52 HostContentSettingsMap* host_content_settings_map = 125 HostContentSettingsMap* host_content_settings_map =
53 HostContentSettingsMapFactory::GetForProfile(&profile); 126 HostContentSettingsMapFactory::GetForProfile(&profile);
54 127
55 // Check setting defaults. 128 // Check setting defaults.
56 EXPECT_EQ(CONTENT_SETTING_ALLOW, 129 EXPECT_EQ(CONTENT_SETTING_ALLOW,
57 host_content_settings_map->GetDefaultContentSetting( 130 host_content_settings_map->GetDefaultContentSetting(
58 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL)); 131 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
59 host_content_settings_map->SetDefaultContentSetting( 132 host_content_settings_map->SetDefaultContentSetting(
(...skipping 706 matching lines...) Expand 10 before | Expand all | Expand 10 after
766 scoped_ptr<base::Value> stored_value = 839 scoped_ptr<base::Value> stored_value =
767 host_content_settings_map->GetWebsiteSetting( 840 host_content_settings_map->GetWebsiteSetting(
768 host, host, CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA, std::string(), 841 host, host, CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA, std::string(),
769 nullptr); 842 nullptr);
770 EXPECT_TRUE(stored_value && stored_value->Equals(&test_value)); 843 EXPECT_TRUE(stored_value && stored_value->Equals(&test_value));
771 EXPECT_EQ(nullptr, otr_map->GetWebsiteSetting( 844 EXPECT_EQ(nullptr, otr_map->GetWebsiteSetting(
772 host, host, CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA, 845 host, host, CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA,
773 std::string(), nullptr)); 846 std::string(), nullptr));
774 } 847 }
775 848
849 TEST_F(HostContentSettingsMapTest, AreUserExceptionsAllowedForType) {
850 ContentSettingsType kContentTypesToTest[] = {
851 CONTENT_SETTINGS_TYPE_COOKIES,
852 CONTENT_SETTINGS_TYPE_POPUPS,
853 };
854
855 HostContentSettingsMapTester tester;
Bernhard Bauer 2016/04/12 09:30:05 Move this into the loop to limit its scope?
huangs 2016/04/13 17:36:18 Done. Note that I moved TestingProfile outside, a
856
857 for (ContentSettingsType type : kContentTypesToTest) {
858 tester.SelectType(type);
859
860 // No settings: Yes.
861 tester.ClearPolicyDefault();
862 EXPECT_TRUE(tester.AreUserExceptionsAllowed());
863
864 // Policy enforces default value: No.
865 tester.SetPolicyDefault(CONTENT_SETTING_ALLOW);
866 EXPECT_FALSE(tester.AreUserExceptionsAllowed());
867 tester.SetPolicyDefault(CONTENT_SETTING_BLOCK);
868 EXPECT_FALSE(tester.AreUserExceptionsAllowed());
869
870 // Not calling tester.ClearPolicyDefault(); let it linger.
raymes 2016/04/12 04:52:47 Should we clean up the state?
huangs 2016/04/13 17:36:18 Done.
871 }
872 }
873
874 TEST_F(HostContentSettingsMapTest, PrefExceptionsOperation) {
875 using content_settings::SETTING_SOURCE_POLICY;
876 using content_settings::SETTING_SOURCE_USER;
877
878 const char kUrl1[] = "http://user_exception_allow.com";
879 const char kUrl2[] = "http://user_exception_block.com";
880 const char kUrl3[] = "http://non_exception.com";
881
882 HostContentSettingsMapTester tester;
883
884 // Arbitrarily using cookies as content type to test.
885 tester.SelectType(CONTENT_SETTINGS_TYPE_COOKIES);
886
887 // Add |kUrl1| and |kUrl2| only.
888 tester.AddUserException(kUrl1, CONTENT_SETTING_ALLOW);
889 tester.AddUserException(kUrl2, CONTENT_SETTING_BLOCK);
890
891 // No policy setting: follow users settings.
892 tester.ClearPolicyDefault();
893 // User exceptions.
894 EXPECT_EQ(SETTING_SOURCE_USER, tester.SourceMatch(kUrl1));
895 EXPECT_EQ(SETTING_SOURCE_USER, tester.SourceMatch(kUrl2));
896 // User default.
897 EXPECT_EQ(SETTING_SOURCE_USER, tester.SourceMatch(kUrl3));
898
899 // Policy overrides users always.
900 tester.SetPolicyDefault(CONTENT_SETTING_ALLOW);
901 EXPECT_EQ(SETTING_SOURCE_POLICY, tester.SourceMatch(kUrl1));
902 EXPECT_EQ(SETTING_SOURCE_POLICY, tester.SourceMatch(kUrl2));
903 EXPECT_EQ(SETTING_SOURCE_POLICY, tester.SourceMatch(kUrl3));
904 tester.SetPolicyDefault(CONTENT_SETTING_BLOCK);
905 EXPECT_EQ(SETTING_SOURCE_POLICY, tester.SourceMatch(kUrl1));
906 EXPECT_EQ(SETTING_SOURCE_POLICY, tester.SourceMatch(kUrl2));
907 EXPECT_EQ(SETTING_SOURCE_POLICY, tester.SourceMatch(kUrl3));
908 }
909
776 // For a single Unicode encoded pattern, check if it gets converted to punycode 910 // For a single Unicode encoded pattern, check if it gets converted to punycode
777 // and old pattern gets deleted. 911 // and old pattern gets deleted.
778 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) { 912 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) {
779 TestingProfile profile; 913 TestingProfile profile;
780 PrefService* prefs = profile.GetPrefs(); 914 PrefService* prefs = profile.GetPrefs();
781 915
782 // Set utf-8 data. 916 // Set utf-8 data.
783 { 917 {
784 DictionaryPrefUpdate update(prefs, 918 DictionaryPrefUpdate update(prefs,
785 GetPrefName(CONTENT_SETTINGS_TYPE_PLUGINS)); 919 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())); 1318 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string()));
1185 1319
1186 // After migrating old settings, changes to the setting works. 1320 // After migrating old settings, changes to the setting works.
1187 host_content_settings_map->SetContentSettingDefaultScope( 1321 host_content_settings_map->SetContentSettingDefaultScope(
1188 host, GURL(), CONTENT_SETTINGS_TYPE_KEYGEN, std::string(), 1322 host, GURL(), CONTENT_SETTINGS_TYPE_KEYGEN, std::string(),
1189 CONTENT_SETTING_BLOCK); 1323 CONTENT_SETTING_BLOCK);
1190 EXPECT_EQ(CONTENT_SETTING_BLOCK, 1324 EXPECT_EQ(CONTENT_SETTING_BLOCK,
1191 host_content_settings_map->GetContentSetting( 1325 host_content_settings_map->GetContentSetting(
1192 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string())); 1326 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string()));
1193 } 1327 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698