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
3 // found in the LICENSE file.
5 #include "base/auto_reset.h"
6 #include "base/command_line.h"
7 #include "base/message_loop.h"
8 #include "chrome/browser/content_settings/cookie_settings.h"
9 #include "chrome/browser/prefs/pref_service.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/common/content_settings_pattern.h"
12 #include "chrome/common/pref_names.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/test/test_browser_thread.h"
15 #include "googleurl/src/gurl.h"
16 #include "net/base/static_cookie_policy.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using content::BrowserThread
;
23 class CookieSettingsTest
: public testing::Test
{
25 CookieSettingsTest() :
26 ui_thread_(BrowserThread::UI
, &message_loop_
),
27 cookie_settings_(CookieSettings::Factory::GetForProfile(&profile_
)),
28 kBlockedSite("http://ads.thirdparty.com"),
29 kAllowedSite("http://good.allays.com"),
30 kFirstPartySite("http://cool.things.com"),
31 kBlockedFirstPartySite("http://no.thirdparties.com"),
32 kExtensionURL("chrome-extension://deadbeef"),
33 kHttpsSite("https://example.com"),
34 kAllHttpsSitesPattern(ContentSettingsPattern::FromString("https://*")) {
38 MessageLoop message_loop_
;
39 content::TestBrowserThread ui_thread_
;
40 TestingProfile profile_
;
41 CookieSettings
* cookie_settings_
;
42 const GURL kBlockedSite
;
43 const GURL kAllowedSite
;
44 const GURL kFirstPartySite
;
45 const GURL kBlockedFirstPartySite
;
46 const GURL kExtensionURL
;
47 const GURL kHttpsSite
;
48 ContentSettingsPattern kAllHttpsSitesPattern
;
51 TEST_F(CookieSettingsTest
, CookiesBlockSingle
) {
52 cookie_settings_
->SetCookieSetting(
53 ContentSettingsPattern::FromURL(kBlockedSite
),
54 ContentSettingsPattern::Wildcard(),
55 CONTENT_SETTING_BLOCK
);
56 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(
57 kBlockedSite
, kBlockedSite
));
60 TEST_F(CookieSettingsTest
, CookiesBlockThirdParty
) {
61 profile_
.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
, true);
62 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(
63 kBlockedSite
, kFirstPartySite
));
64 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kBlockedSite
));
65 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(
66 kBlockedSite
, kFirstPartySite
));
68 CommandLine
* cmd
= CommandLine::ForCurrentProcess();
69 AutoReset
<CommandLine
> auto_reset(cmd
, *cmd
);
70 cmd
->AppendSwitch(switches::kOnlyBlockSettingThirdPartyCookies
);
72 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(
73 kBlockedSite
, kFirstPartySite
));
74 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(
75 kBlockedSite
, kFirstPartySite
));
78 TEST_F(CookieSettingsTest
, CookiesAllowThirdParty
) {
79 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(
80 kBlockedSite
, kFirstPartySite
));
81 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(
82 kBlockedSite
, kFirstPartySite
));
83 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kBlockedSite
));
86 TEST_F(CookieSettingsTest
, CookiesExplicitBlockSingleThirdParty
) {
87 cookie_settings_
->SetCookieSetting(
88 ContentSettingsPattern::FromURL(kBlockedSite
),
89 ContentSettingsPattern::Wildcard(),
90 CONTENT_SETTING_BLOCK
);
91 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(
92 kBlockedSite
, kFirstPartySite
));
93 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(
94 kBlockedSite
, kFirstPartySite
));
95 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(
96 kAllowedSite
, kFirstPartySite
));
99 TEST_F(CookieSettingsTest
, CookiesExplicitSessionOnly
) {
100 cookie_settings_
->SetCookieSetting(
101 ContentSettingsPattern::FromURL(kBlockedSite
),
102 ContentSettingsPattern::Wildcard(),
103 CONTENT_SETTING_SESSION_ONLY
);
104 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(
105 kBlockedSite
, kFirstPartySite
));
106 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(
107 kBlockedSite
, kFirstPartySite
));
108 EXPECT_TRUE(cookie_settings_
->IsCookieSessionOnly(kBlockedSite
));
110 profile_
.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
, true);
111 EXPECT_TRUE(cookie_settings_
->
112 IsReadingCookieAllowed(kBlockedSite
, kFirstPartySite
));
113 EXPECT_TRUE(cookie_settings_
->
114 IsSettingCookieAllowed(kBlockedSite
, kFirstPartySite
));
115 EXPECT_TRUE(cookie_settings_
->IsCookieSessionOnly(kBlockedSite
));
118 TEST_F(CookieSettingsTest
, CookiesThirdPartyBlockedExplicitAllow
) {
119 cookie_settings_
->SetCookieSetting(
120 ContentSettingsPattern::FromURL(kAllowedSite
),
121 ContentSettingsPattern::Wildcard(),
122 CONTENT_SETTING_ALLOW
);
123 profile_
.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
, true);
124 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(
125 kAllowedSite
, kFirstPartySite
));
126 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(
127 kAllowedSite
, kFirstPartySite
));
128 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kAllowedSite
));
130 // Extensions should always be allowed to use cookies.
131 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(
132 kAllowedSite
, kExtensionURL
));
133 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(
134 kAllowedSite
, kExtensionURL
));
136 // Extensions should always be allowed to use cookies.
137 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(
138 kAllowedSite
, kExtensionURL
));
139 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(
140 kAllowedSite
, kExtensionURL
));
143 TEST_F(CookieSettingsTest
, CookiesThirdPartyBlockedAllSitesAllowed
) {
144 cookie_settings_
->SetCookieSetting(
145 ContentSettingsPattern::FromURL(kAllowedSite
),
146 ContentSettingsPattern::Wildcard(),
147 CONTENT_SETTING_ALLOW
);
148 profile_
.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
, true);
149 // As an example for a pattern that matches all hosts but not all origins,
150 // match all HTTPS sites.
151 cookie_settings_
->SetCookieSetting(
152 kAllHttpsSitesPattern
,
153 ContentSettingsPattern::Wildcard(),
154 CONTENT_SETTING_ALLOW
);
155 cookie_settings_
->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
157 // |kAllowedSite| should be allowed.
158 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(
159 kAllowedSite
, kBlockedSite
));
160 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(
161 kAllowedSite
, kBlockedSite
));
162 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kAllowedSite
));
164 // HTTPS sites should be allowed in a first-party context.
165 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(
166 kHttpsSite
, kHttpsSite
));
167 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(
168 kHttpsSite
, kHttpsSite
));
169 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kAllowedSite
));
171 // HTTP sites should be allowed, but session-only.
172 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(
173 kFirstPartySite
, kFirstPartySite
));
174 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(
175 kFirstPartySite
, kFirstPartySite
));
176 EXPECT_TRUE(cookie_settings_
->IsCookieSessionOnly(kFirstPartySite
));
178 // Third-party cookies should be blocked.
179 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(
180 kFirstPartySite
, kBlockedSite
));
181 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(
182 kFirstPartySite
, kBlockedSite
));
183 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(
184 kHttpsSite
, kBlockedSite
));
185 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(
186 kHttpsSite
, kBlockedSite
));
189 TEST_F(CookieSettingsTest
, CookiesBlockEverything
) {
190 cookie_settings_
->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK
);
192 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(
193 kFirstPartySite
, kFirstPartySite
));
194 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(
195 kFirstPartySite
, kFirstPartySite
));
196 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(
197 kAllowedSite
, kFirstPartySite
));
200 TEST_F(CookieSettingsTest
, CookiesBlockEverythingExceptAllowed
) {
201 cookie_settings_
->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK
);
202 cookie_settings_
->SetCookieSetting(
203 ContentSettingsPattern::FromURL(kAllowedSite
),
204 ContentSettingsPattern::Wildcard(),
205 CONTENT_SETTING_ALLOW
);
206 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(
207 kFirstPartySite
, kFirstPartySite
));
208 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(
209 kFirstPartySite
, kFirstPartySite
));
210 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(
211 kAllowedSite
, kFirstPartySite
));
212 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(
213 kAllowedSite
, kFirstPartySite
));
214 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(
215 kAllowedSite
, kAllowedSite
));
216 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(
217 kAllowedSite
, kAllowedSite
));
218 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kAllowedSite
));
221 TEST_F(CookieSettingsTest
, CookiesBlockSingleFirstParty
) {
222 cookie_settings_
->SetCookieSetting(
223 ContentSettingsPattern::FromURL(kAllowedSite
),
224 ContentSettingsPattern::FromURL(kFirstPartySite
),
225 CONTENT_SETTING_ALLOW
);
226 cookie_settings_
->SetCookieSetting(
227 ContentSettingsPattern::FromURL(kAllowedSite
),
228 ContentSettingsPattern::FromURL(kBlockedFirstPartySite
),
229 CONTENT_SETTING_BLOCK
);
231 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(
232 kAllowedSite
, kFirstPartySite
));
233 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(
234 kAllowedSite
, kFirstPartySite
));
235 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kAllowedSite
));
237 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(
238 kAllowedSite
, kBlockedFirstPartySite
));
239 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(
240 kAllowedSite
, kBlockedFirstPartySite
));
242 cookie_settings_
->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK
);
244 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(
245 kAllowedSite
, kFirstPartySite
));
246 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(
247 kAllowedSite
, kFirstPartySite
));
248 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kAllowedSite
));
250 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(
251 kAllowedSite
, kBlockedFirstPartySite
));
252 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(
253 kAllowedSite
, kBlockedFirstPartySite
));
255 cookie_settings_
->ResetCookieSetting(
256 ContentSettingsPattern::FromURL(kAllowedSite
),
257 ContentSettingsPattern::FromURL(kFirstPartySite
));
259 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(
260 kAllowedSite
, kFirstPartySite
));
261 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(
262 kAllowedSite
, kFirstPartySite
));
265 TEST_F(CookieSettingsTest
, ExtensionsRegularSettings
) {
266 cookie_settings_
->SetCookieSetting(
267 ContentSettingsPattern::FromURL(kBlockedSite
),
268 ContentSettingsPattern::Wildcard(),
269 CONTENT_SETTING_BLOCK
);
271 // Regular cookie settings also apply to extensions.
272 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(
273 kBlockedSite
, kExtensionURL
));
276 TEST_F(CookieSettingsTest
, ExtensionsOwnCookies
) {
277 cookie_settings_
->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK
);
279 // Extensions can always use cookies (and site data) in their own origin.
280 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(
281 kExtensionURL
, kExtensionURL
));
284 TEST_F(CookieSettingsTest
, ExtensionsThirdParty
) {
285 profile_
.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
, true);
287 // XHRs stemming from extensions are exempt from third-party cookie blocking
288 // rules (as the first party is always the extension's security origin).
289 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(
290 kBlockedSite
, kExtensionURL
));