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

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: Add SetCookieException to handle custom 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
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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 const GURL kFirstPartySite; 46 const GURL kFirstPartySite;
47 const GURL kBlockedFirstPartySite; 47 const GURL kBlockedFirstPartySite;
48 const GURL kChromeURL; 48 const GURL kChromeURL;
49 const GURL kExtensionURL; 49 const GURL kExtensionURL;
50 const GURL kHttpSite; 50 const GURL kHttpSite;
51 const GURL kHttpsSite; 51 const GURL kHttpsSite;
52 ContentSettingsPattern kAllHttpsSitesPattern; 52 ContentSettingsPattern kAllHttpsSitesPattern;
53 }; 53 };
54 54
55 TEST_F(CookieSettingsTest, TestWhitelistedScheme) { 55 TEST_F(CookieSettingsTest, TestWhitelistedScheme) {
56 cookie_settings_->SetCookieSetting(ContentSettingsPattern::Wildcard(), 56 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
57 ContentSettingsPattern::Wildcard(),
58 CONTENT_SETTING_BLOCK);
59 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(kHttpSite, kChromeURL)); 57 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(kHttpSite, kChromeURL));
60 EXPECT_TRUE(cookie_settings_->IsReadingCookieAllowed(kHttpsSite, kChromeURL)); 58 EXPECT_TRUE(cookie_settings_->IsReadingCookieAllowed(kHttpsSite, kChromeURL));
61 EXPECT_TRUE(cookie_settings_->IsReadingCookieAllowed(kChromeURL, kHttpSite)); 59 EXPECT_TRUE(cookie_settings_->IsReadingCookieAllowed(kChromeURL, kHttpSite));
62 #if defined(ENABLE_EXTENSIONS) 60 #if defined(ENABLE_EXTENSIONS)
63 EXPECT_TRUE( 61 EXPECT_TRUE(
64 cookie_settings_->IsReadingCookieAllowed(kExtensionURL, kExtensionURL)); 62 cookie_settings_->IsReadingCookieAllowed(kExtensionURL, kExtensionURL));
65 #else 63 #else
66 EXPECT_FALSE( 64 EXPECT_FALSE(
67 cookie_settings_->IsReadingCookieAllowed(kExtensionURL, kExtensionURL)); 65 cookie_settings_->IsReadingCookieAllowed(kExtensionURL, kExtensionURL));
68 #endif 66 #endif
69 EXPECT_FALSE( 67 EXPECT_FALSE(
70 cookie_settings_->IsReadingCookieAllowed(kExtensionURL, kHttpSite)); 68 cookie_settings_->IsReadingCookieAllowed(kExtensionURL, kHttpSite));
71 } 69 }
72 70
73 TEST_F(CookieSettingsTest, CookiesBlockSingle) { 71 TEST_F(CookieSettingsTest, CookiesBlockSingle) {
74 cookie_settings_->SetCookieSetting( 72 cookie_settings_->SetCookieSetting(kBlockedSite, CONTENT_SETTING_BLOCK);
75 ContentSettingsPattern::FromURL(kBlockedSite),
76 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK);
77 EXPECT_FALSE( 73 EXPECT_FALSE(
78 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kBlockedSite)); 74 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kBlockedSite));
79 } 75 }
80 76
81 TEST_F(CookieSettingsTest, CookiesBlockThirdParty) { 77 TEST_F(CookieSettingsTest, CookiesBlockThirdParty) {
82 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true); 78 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true);
83 EXPECT_FALSE( 79 EXPECT_FALSE(
84 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite)); 80 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
85 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kBlockedSite)); 81 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kBlockedSite));
86 EXPECT_FALSE( 82 EXPECT_FALSE(
87 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite)); 83 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
88 } 84 }
89 85
90 TEST_F(CookieSettingsTest, CookiesAllowThirdParty) { 86 TEST_F(CookieSettingsTest, CookiesAllowThirdParty) {
91 EXPECT_TRUE( 87 EXPECT_TRUE(
92 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite)); 88 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
93 EXPECT_TRUE( 89 EXPECT_TRUE(
94 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite)); 90 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
95 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kBlockedSite)); 91 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kBlockedSite));
96 } 92 }
97 93
98 TEST_F(CookieSettingsTest, CookiesExplicitBlockSingleThirdParty) { 94 TEST_F(CookieSettingsTest, CookiesExplicitBlockSingleThirdParty) {
99 cookie_settings_->SetCookieSetting( 95 cookie_settings_->SetCookieSetting(kBlockedSite, CONTENT_SETTING_BLOCK);
100 ContentSettingsPattern::FromURL(kBlockedSite),
101 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK);
102 EXPECT_FALSE( 96 EXPECT_FALSE(
103 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite)); 97 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
104 EXPECT_FALSE( 98 EXPECT_FALSE(
105 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite)); 99 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
106 EXPECT_TRUE( 100 EXPECT_TRUE(
107 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite)); 101 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
108 } 102 }
109 103
110 TEST_F(CookieSettingsTest, CookiesExplicitSessionOnly) { 104 TEST_F(CookieSettingsTest, CookiesExplicitSessionOnly) {
111 cookie_settings_->SetCookieSetting( 105 cookie_settings_->SetCookieSetting(kBlockedSite,
112 ContentSettingsPattern::FromURL(kBlockedSite), 106 CONTENT_SETTING_SESSION_ONLY);
113 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_SESSION_ONLY);
114 EXPECT_TRUE( 107 EXPECT_TRUE(
115 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite)); 108 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
116 EXPECT_TRUE( 109 EXPECT_TRUE(
117 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite)); 110 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
118 EXPECT_TRUE(cookie_settings_->IsCookieSessionOnly(kBlockedSite)); 111 EXPECT_TRUE(cookie_settings_->IsCookieSessionOnly(kBlockedSite));
119 112
120 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true); 113 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true);
121 EXPECT_TRUE( 114 EXPECT_TRUE(
122 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite)); 115 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
123 EXPECT_TRUE( 116 EXPECT_TRUE(
124 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite)); 117 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
125 EXPECT_TRUE(cookie_settings_->IsCookieSessionOnly(kBlockedSite)); 118 EXPECT_TRUE(cookie_settings_->IsCookieSessionOnly(kBlockedSite));
126 } 119 }
127 120
128 TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedExplicitAllow) { 121 TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedExplicitAllow) {
129 cookie_settings_->SetCookieSetting( 122 cookie_settings_->SetCookieSetting(kAllowedSite, CONTENT_SETTING_ALLOW);
130 ContentSettingsPattern::FromURL(kAllowedSite),
131 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW);
132 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true); 123 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true);
133 EXPECT_TRUE( 124 EXPECT_TRUE(
134 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite)); 125 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite));
135 EXPECT_TRUE( 126 EXPECT_TRUE(
136 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite)); 127 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
137 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite)); 128 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite));
138 129
139 // Extensions should always be allowed to use cookies. 130 // Extensions should always be allowed to use cookies.
140 EXPECT_TRUE( 131 EXPECT_TRUE(
141 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kExtensionURL)); 132 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kExtensionURL));
142 EXPECT_TRUE( 133 EXPECT_TRUE(
143 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kExtensionURL)); 134 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kExtensionURL));
144 } 135 }
145 136
146 TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedAllSitesAllowed) { 137 TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedAllSitesAllowed) {
147 cookie_settings_->SetCookieSetting( 138 cookie_settings_->SetCookieSetting(kAllowedSite, CONTENT_SETTING_ALLOW);
148 ContentSettingsPattern::FromURL(kAllowedSite),
149 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW);
150 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true); 139 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true);
151 // As an example for a pattern that matches all hosts but not all origins, 140 // As an example for a url that matches all hosts but not all origins,
152 // match all HTTPS sites. 141 // match all HTTPS sites.
153 cookie_settings_->SetCookieSetting(kAllHttpsSitesPattern, 142 settings_map_->SetContentSetting(
154 ContentSettingsPattern::Wildcard(), 143 kAllHttpsSitesPattern, ContentSettingsPattern::Wildcard(),
155 CONTENT_SETTING_ALLOW); 144 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_ALLOW);
156 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY); 145 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
157 146
158 // |kAllowedSite| should be allowed. 147 // |kAllowedSite| should be allowed.
159 EXPECT_TRUE( 148 EXPECT_TRUE(
160 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kBlockedSite)); 149 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kBlockedSite));
161 EXPECT_TRUE( 150 EXPECT_TRUE(
162 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kBlockedSite)); 151 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kBlockedSite));
163 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite)); 152 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite));
164 153
165 // HTTPS sites should be allowed in a first-party context. 154 // HTTPS sites should be allowed in a first-party context.
(...skipping 25 matching lines...) Expand all
191 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(kFirstPartySite, 180 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(kFirstPartySite,
192 kFirstPartySite)); 181 kFirstPartySite));
193 EXPECT_FALSE(cookie_settings_->IsSettingCookieAllowed(kFirstPartySite, 182 EXPECT_FALSE(cookie_settings_->IsSettingCookieAllowed(kFirstPartySite,
194 kFirstPartySite)); 183 kFirstPartySite));
195 EXPECT_FALSE( 184 EXPECT_FALSE(
196 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite)); 185 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
197 } 186 }
198 187
199 TEST_F(CookieSettingsTest, CookiesBlockEverythingExceptAllowed) { 188 TEST_F(CookieSettingsTest, CookiesBlockEverythingExceptAllowed) {
200 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); 189 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
201 cookie_settings_->SetCookieSetting( 190 cookie_settings_->SetCookieSetting(kAllowedSite, CONTENT_SETTING_ALLOW);
202 ContentSettingsPattern::FromURL(kAllowedSite),
203 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW);
204 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(kFirstPartySite, 191 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(kFirstPartySite,
205 kFirstPartySite)); 192 kFirstPartySite));
206 EXPECT_FALSE(cookie_settings_->IsSettingCookieAllowed(kFirstPartySite, 193 EXPECT_FALSE(cookie_settings_->IsSettingCookieAllowed(kFirstPartySite,
207 kFirstPartySite)); 194 kFirstPartySite));
208 EXPECT_TRUE( 195 EXPECT_TRUE(
209 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite)); 196 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite));
210 EXPECT_TRUE( 197 EXPECT_TRUE(
211 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite)); 198 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
212 EXPECT_TRUE( 199 EXPECT_TRUE(
213 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kAllowedSite)); 200 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kAllowedSite));
214 EXPECT_TRUE( 201 EXPECT_TRUE(
215 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kAllowedSite)); 202 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kAllowedSite));
216 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite)); 203 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite));
217 } 204 }
218 205
219 TEST_F(CookieSettingsTest, CookiesBlockSingleFirstParty) { 206 TEST_F(CookieSettingsTest, CookiesBlockSingleFirstParty) {
220 cookie_settings_->SetCookieSetting( 207 settings_map_->SetContentSetting(
221 ContentSettingsPattern::FromURL(kAllowedSite), 208 ContentSettingsPattern::FromURL(kAllowedSite),
222 ContentSettingsPattern::FromURL(kFirstPartySite), CONTENT_SETTING_ALLOW); 209 ContentSettingsPattern::FromURL(kFirstPartySite),
223 cookie_settings_->SetCookieSetting( 210 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_ALLOW);
211 settings_map_->SetContentSetting(
224 ContentSettingsPattern::FromURL(kAllowedSite), 212 ContentSettingsPattern::FromURL(kAllowedSite),
225 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), 213 ContentSettingsPattern::FromURL(kBlockedFirstPartySite),
226 CONTENT_SETTING_BLOCK); 214 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_BLOCK);
227 215
228 EXPECT_TRUE( 216 EXPECT_TRUE(
229 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite)); 217 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite));
230 EXPECT_TRUE( 218 EXPECT_TRUE(
231 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite)); 219 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
232 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite)); 220 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite));
233 221
234 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed( 222 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(
235 kAllowedSite, kBlockedFirstPartySite)); 223 kAllowedSite, kBlockedFirstPartySite));
236 EXPECT_FALSE(cookie_settings_->IsSettingCookieAllowed( 224 EXPECT_FALSE(cookie_settings_->IsSettingCookieAllowed(
(...skipping 16 matching lines...) Expand all
253 ContentSettingsPattern::FromURL(kAllowedSite), 241 ContentSettingsPattern::FromURL(kAllowedSite),
254 ContentSettingsPattern::FromURL(kFirstPartySite)); 242 ContentSettingsPattern::FromURL(kFirstPartySite));
255 243
256 EXPECT_FALSE( 244 EXPECT_FALSE(
257 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite)); 245 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite));
258 EXPECT_FALSE( 246 EXPECT_FALSE(
259 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite)); 247 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
260 } 248 }
261 249
262 TEST_F(CookieSettingsTest, ExtensionsRegularSettings) { 250 TEST_F(CookieSettingsTest, ExtensionsRegularSettings) {
263 cookie_settings_->SetCookieSetting( 251 cookie_settings_->SetCookieSetting(kBlockedSite, CONTENT_SETTING_BLOCK);
264 ContentSettingsPattern::FromURL(kBlockedSite),
265 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK);
266 252
267 // Regular cookie settings also apply to extensions. 253 // Regular cookie settings also apply to extensions.
268 EXPECT_FALSE( 254 EXPECT_FALSE(
269 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kExtensionURL)); 255 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kExtensionURL));
270 } 256 }
271 257
272 TEST_F(CookieSettingsTest, ExtensionsOwnCookies) { 258 TEST_F(CookieSettingsTest, ExtensionsOwnCookies) {
273 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); 259 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
274 260
275 #if defined(ENABLE_EXTENSIONS) 261 #if defined(ENABLE_EXTENSIONS)
(...skipping 13 matching lines...) Expand all
289 275
290 // XHRs stemming from extensions are exempt from third-party cookie blocking 276 // XHRs stemming from extensions are exempt from third-party cookie blocking
291 // rules (as the first party is always the extension's security origin). 277 // rules (as the first party is always the extension's security origin).
292 EXPECT_TRUE( 278 EXPECT_TRUE(
293 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kExtensionURL)); 279 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kExtensionURL));
294 } 280 }
295 281
296 } // namespace 282 } // namespace
297 283
298 } // namespace content_settings 284 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698