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

Side by Side Diff: components/content_settings/core/browser/cookie_settings_unittest.cc

Issue 1694063002: Use GURLS instead of patterns in SetCookieSetting() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@scoping_set_content_setting
Patch Set: remove unused patterns Created 4 years, 9 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
« no previous file with comments | « components/content_settings/core/browser/cookie_settings.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "components/content_settings/core/browser/cookie_settings.h" 5 #include "components/content_settings/core/browser/cookie_settings.h"
6 6
7 #include "components/content_settings/core/browser/host_content_settings_map.h" 7 #include "components/content_settings/core/browser/host_content_settings_map.h"
8 #include "components/content_settings/core/common/content_settings_pattern.h" 8 #include "components/content_settings/core/common/content_settings_pattern.h"
9 #include "components/content_settings/core/common/pref_names.h" 9 #include "components/content_settings/core/common/pref_names.h"
10 #include "components/pref_registry/testing_pref_service_syncable.h" 10 #include "components/pref_registry/testing_pref_service_syncable.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 const GURL kAllowedSite; 44 const GURL kAllowedSite;
45 const GURL kFirstPartySite; 45 const GURL kFirstPartySite;
46 const GURL kChromeURL; 46 const GURL kChromeURL;
47 const GURL kExtensionURL; 47 const GURL kExtensionURL;
48 const GURL kHttpSite; 48 const GURL kHttpSite;
49 const GURL kHttpsSite; 49 const GURL kHttpsSite;
50 ContentSettingsPattern kAllHttpsSitesPattern; 50 ContentSettingsPattern kAllHttpsSitesPattern;
51 }; 51 };
52 52
53 TEST_F(CookieSettingsTest, TestWhitelistedScheme) { 53 TEST_F(CookieSettingsTest, TestWhitelistedScheme) {
54 cookie_settings_->SetCookieSetting(ContentSettingsPattern::Wildcard(), 54 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
55 ContentSettingsPattern::Wildcard(),
56 CONTENT_SETTING_BLOCK);
57 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(kHttpSite, kChromeURL)); 55 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(kHttpSite, kChromeURL));
58 EXPECT_TRUE(cookie_settings_->IsReadingCookieAllowed(kHttpsSite, kChromeURL)); 56 EXPECT_TRUE(cookie_settings_->IsReadingCookieAllowed(kHttpsSite, kChromeURL));
59 EXPECT_TRUE(cookie_settings_->IsReadingCookieAllowed(kChromeURL, kHttpSite)); 57 EXPECT_TRUE(cookie_settings_->IsReadingCookieAllowed(kChromeURL, kHttpSite));
60 #if defined(ENABLE_EXTENSIONS) 58 #if defined(ENABLE_EXTENSIONS)
61 EXPECT_TRUE( 59 EXPECT_TRUE(
62 cookie_settings_->IsReadingCookieAllowed(kExtensionURL, kExtensionURL)); 60 cookie_settings_->IsReadingCookieAllowed(kExtensionURL, kExtensionURL));
63 #else 61 #else
64 EXPECT_FALSE( 62 EXPECT_FALSE(
65 cookie_settings_->IsReadingCookieAllowed(kExtensionURL, kExtensionURL)); 63 cookie_settings_->IsReadingCookieAllowed(kExtensionURL, kExtensionURL));
66 #endif 64 #endif
67 EXPECT_FALSE( 65 EXPECT_FALSE(
68 cookie_settings_->IsReadingCookieAllowed(kExtensionURL, kHttpSite)); 66 cookie_settings_->IsReadingCookieAllowed(kExtensionURL, kHttpSite));
69 } 67 }
70 68
71 TEST_F(CookieSettingsTest, CookiesBlockSingle) { 69 TEST_F(CookieSettingsTest, CookiesBlockSingle) {
72 cookie_settings_->SetCookieSetting( 70 cookie_settings_->SetCookieSetting(kBlockedSite, CONTENT_SETTING_BLOCK);
73 ContentSettingsPattern::FromURL(kBlockedSite),
74 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK);
75 EXPECT_FALSE( 71 EXPECT_FALSE(
76 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kBlockedSite)); 72 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kBlockedSite));
77 } 73 }
78 74
79 TEST_F(CookieSettingsTest, CookiesBlockThirdParty) { 75 TEST_F(CookieSettingsTest, CookiesBlockThirdParty) {
80 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true); 76 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true);
81 EXPECT_FALSE( 77 EXPECT_FALSE(
82 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite)); 78 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
83 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kBlockedSite)); 79 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kBlockedSite));
84 EXPECT_FALSE( 80 EXPECT_FALSE(
85 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite)); 81 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
86 } 82 }
87 83
88 TEST_F(CookieSettingsTest, CookiesAllowThirdParty) { 84 TEST_F(CookieSettingsTest, CookiesAllowThirdParty) {
89 EXPECT_TRUE( 85 EXPECT_TRUE(
90 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite)); 86 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
91 EXPECT_TRUE( 87 EXPECT_TRUE(
92 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite)); 88 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
93 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kBlockedSite)); 89 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kBlockedSite));
94 } 90 }
95 91
96 TEST_F(CookieSettingsTest, CookiesExplicitBlockSingleThirdParty) { 92 TEST_F(CookieSettingsTest, CookiesExplicitBlockSingleThirdParty) {
97 cookie_settings_->SetCookieSetting( 93 cookie_settings_->SetCookieSetting(kBlockedSite, CONTENT_SETTING_BLOCK);
98 ContentSettingsPattern::FromURL(kBlockedSite),
99 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK);
100 EXPECT_FALSE( 94 EXPECT_FALSE(
101 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite)); 95 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
102 EXPECT_FALSE( 96 EXPECT_FALSE(
103 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite)); 97 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
104 EXPECT_TRUE( 98 EXPECT_TRUE(
105 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite)); 99 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
106 } 100 }
107 101
108 TEST_F(CookieSettingsTest, CookiesExplicitSessionOnly) { 102 TEST_F(CookieSettingsTest, CookiesExplicitSessionOnly) {
109 cookie_settings_->SetCookieSetting( 103 cookie_settings_->SetCookieSetting(kBlockedSite,
110 ContentSettingsPattern::FromURL(kBlockedSite), 104 CONTENT_SETTING_SESSION_ONLY);
111 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_SESSION_ONLY);
112 EXPECT_TRUE( 105 EXPECT_TRUE(
113 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite)); 106 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
114 EXPECT_TRUE( 107 EXPECT_TRUE(
115 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite)); 108 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
116 EXPECT_TRUE(cookie_settings_->IsCookieSessionOnly(kBlockedSite)); 109 EXPECT_TRUE(cookie_settings_->IsCookieSessionOnly(kBlockedSite));
117 110
118 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true); 111 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true);
119 EXPECT_TRUE( 112 EXPECT_TRUE(
120 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite)); 113 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
121 EXPECT_TRUE( 114 EXPECT_TRUE(
122 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite)); 115 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
123 EXPECT_TRUE(cookie_settings_->IsCookieSessionOnly(kBlockedSite)); 116 EXPECT_TRUE(cookie_settings_->IsCookieSessionOnly(kBlockedSite));
124 } 117 }
125 118
126 TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedExplicitAllow) { 119 TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedExplicitAllow) {
127 cookie_settings_->SetCookieSetting( 120 cookie_settings_->SetCookieSetting(kAllowedSite, CONTENT_SETTING_ALLOW);
128 ContentSettingsPattern::FromURL(kAllowedSite),
129 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW);
130 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true); 121 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true);
131 EXPECT_TRUE( 122 EXPECT_TRUE(
132 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite)); 123 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite));
133 EXPECT_TRUE( 124 EXPECT_TRUE(
134 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite)); 125 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
135 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite)); 126 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite));
136 127
137 // Extensions should always be allowed to use cookies. 128 // Extensions should always be allowed to use cookies.
138 EXPECT_TRUE( 129 EXPECT_TRUE(
139 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kExtensionURL)); 130 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kExtensionURL));
140 EXPECT_TRUE( 131 EXPECT_TRUE(
141 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kExtensionURL)); 132 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kExtensionURL));
142 } 133 }
143 134
144 TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedAllSitesAllowed) { 135 TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedAllSitesAllowed) {
145 cookie_settings_->SetCookieSetting( 136 cookie_settings_->SetCookieSetting(kAllowedSite, CONTENT_SETTING_ALLOW);
146 ContentSettingsPattern::FromURL(kAllowedSite),
147 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW);
148 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true); 137 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true);
149 // As an example for a pattern that matches all hosts but not all origins, 138 // As an example for a url that matches all hosts but not all origins,
150 // match all HTTPS sites. 139 // match all HTTPS sites.
151 cookie_settings_->SetCookieSetting(kAllHttpsSitesPattern, 140 settings_map_->SetContentSetting(
152 ContentSettingsPattern::Wildcard(), 141 kAllHttpsSitesPattern, ContentSettingsPattern::Wildcard(),
153 CONTENT_SETTING_ALLOW); 142 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_ALLOW);
154 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY); 143 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
155 144
156 // |kAllowedSite| should be allowed. 145 // |kAllowedSite| should be allowed.
157 EXPECT_TRUE( 146 EXPECT_TRUE(
158 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kBlockedSite)); 147 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kBlockedSite));
159 EXPECT_TRUE( 148 EXPECT_TRUE(
160 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kBlockedSite)); 149 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kBlockedSite));
161 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite)); 150 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite));
162 151
163 // HTTPS sites should be allowed in a first-party context. 152 // HTTPS sites should be allowed in a first-party context.
(...skipping 25 matching lines...) Expand all
189 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(kFirstPartySite, 178 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(kFirstPartySite,
190 kFirstPartySite)); 179 kFirstPartySite));
191 EXPECT_FALSE(cookie_settings_->IsSettingCookieAllowed(kFirstPartySite, 180 EXPECT_FALSE(cookie_settings_->IsSettingCookieAllowed(kFirstPartySite,
192 kFirstPartySite)); 181 kFirstPartySite));
193 EXPECT_FALSE( 182 EXPECT_FALSE(
194 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite)); 183 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
195 } 184 }
196 185
197 TEST_F(CookieSettingsTest, CookiesBlockEverythingExceptAllowed) { 186 TEST_F(CookieSettingsTest, CookiesBlockEverythingExceptAllowed) {
198 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); 187 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
199 cookie_settings_->SetCookieSetting( 188 cookie_settings_->SetCookieSetting(kAllowedSite, CONTENT_SETTING_ALLOW);
200 ContentSettingsPattern::FromURL(kAllowedSite),
201 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW);
202 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(kFirstPartySite, 189 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(kFirstPartySite,
203 kFirstPartySite)); 190 kFirstPartySite));
204 EXPECT_FALSE(cookie_settings_->IsSettingCookieAllowed(kFirstPartySite, 191 EXPECT_FALSE(cookie_settings_->IsSettingCookieAllowed(kFirstPartySite,
205 kFirstPartySite)); 192 kFirstPartySite));
206 EXPECT_TRUE( 193 EXPECT_TRUE(
207 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite)); 194 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite));
208 EXPECT_TRUE( 195 EXPECT_TRUE(
209 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite)); 196 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
210 EXPECT_TRUE( 197 EXPECT_TRUE(
211 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kAllowedSite)); 198 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kAllowedSite));
212 EXPECT_TRUE( 199 EXPECT_TRUE(
213 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kAllowedSite)); 200 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kAllowedSite));
214 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite)); 201 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite));
215 } 202 }
216 203
217 TEST_F(CookieSettingsTest, ExtensionsRegularSettings) { 204 TEST_F(CookieSettingsTest, ExtensionsRegularSettings) {
218 cookie_settings_->SetCookieSetting( 205 cookie_settings_->SetCookieSetting(kBlockedSite, CONTENT_SETTING_BLOCK);
219 ContentSettingsPattern::FromURL(kBlockedSite),
220 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK);
221 206
222 // Regular cookie settings also apply to extensions. 207 // Regular cookie settings also apply to extensions.
223 EXPECT_FALSE( 208 EXPECT_FALSE(
224 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kExtensionURL)); 209 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kExtensionURL));
225 } 210 }
226 211
227 TEST_F(CookieSettingsTest, ExtensionsOwnCookies) { 212 TEST_F(CookieSettingsTest, ExtensionsOwnCookies) {
228 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); 213 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
229 214
230 #if defined(ENABLE_EXTENSIONS) 215 #if defined(ENABLE_EXTENSIONS)
(...skipping 13 matching lines...) Expand all
244 229
245 // XHRs stemming from extensions are exempt from third-party cookie blocking 230 // XHRs stemming from extensions are exempt from third-party cookie blocking
246 // rules (as the first party is always the extension's security origin). 231 // rules (as the first party is always the extension's security origin).
247 EXPECT_TRUE( 232 EXPECT_TRUE(
248 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kExtensionURL)); 233 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kExtensionURL));
249 } 234 }
250 235
251 } // namespace 236 } // namespace
252 237
253 } // namespace content_settings 238 } // namespace content_settings
OLDNEW
« no previous file with comments | « components/content_settings/core/browser/cookie_settings.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698