Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / content_settings / core / browser / cookie_settings_unittest.cc
blobbbdcbae5d291d607c4e43b38c2da1727f0e04fc5
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 "components/content_settings/core/browser/cookie_settings.h"
7 #include "components/content_settings/core/browser/host_content_settings_map.h"
8 #include "components/content_settings/core/common/content_settings_pattern.h"
9 #include "components/content_settings/core/common/pref_names.h"
10 #include "components/pref_registry/testing_pref_service_syncable.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "url/gurl.h"
14 namespace content_settings {
16 namespace {
18 class CookieSettingsTest : public testing::Test {
19 public:
20 CookieSettingsTest()
21 : kBlockedSite("http://ads.thirdparty.com"),
22 kAllowedSite("http://good.allays.com"),
23 kFirstPartySite("http://cool.things.com"),
24 kBlockedFirstPartySite("http://no.thirdparties.com"),
25 kExtensionURL("chrome-extension://deadbeef"),
26 kHttpsSite("https://example.com"),
27 kAllHttpsSitesPattern(ContentSettingsPattern::FromString("https://*")) {
28 CookieSettings::RegisterProfilePrefs(prefs_.registry());
29 HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry());
30 settings_map_ = new HostContentSettingsMap(&prefs_, false);
31 cookie_settings_ =
32 new CookieSettings(settings_map_.get(), &prefs_, "chrome-extension");
35 ~CookieSettingsTest() override { settings_map_->ShutdownOnUIThread(); }
37 protected:
38 user_prefs::TestingPrefServiceSyncable prefs_;
39 scoped_refptr<HostContentSettingsMap> settings_map_;
40 scoped_refptr<CookieSettings> cookie_settings_;
41 const GURL kBlockedSite;
42 const GURL kAllowedSite;
43 const GURL kFirstPartySite;
44 const GURL kBlockedFirstPartySite;
45 const GURL kExtensionURL;
46 const GURL kHttpsSite;
47 ContentSettingsPattern kAllHttpsSitesPattern;
50 TEST_F(CookieSettingsTest, CookiesBlockSingle) {
51 cookie_settings_->SetCookieSetting(
52 ContentSettingsPattern::FromURL(kBlockedSite),
53 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK);
54 EXPECT_FALSE(
55 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kBlockedSite));
58 TEST_F(CookieSettingsTest, CookiesBlockThirdParty) {
59 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true);
60 EXPECT_FALSE(
61 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
62 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kBlockedSite));
63 EXPECT_FALSE(
64 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
67 TEST_F(CookieSettingsTest, CookiesAllowThirdParty) {
68 EXPECT_TRUE(
69 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
70 EXPECT_TRUE(
71 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
72 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kBlockedSite));
75 TEST_F(CookieSettingsTest, CookiesExplicitBlockSingleThirdParty) {
76 cookie_settings_->SetCookieSetting(
77 ContentSettingsPattern::FromURL(kBlockedSite),
78 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK);
79 EXPECT_FALSE(
80 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
81 EXPECT_FALSE(
82 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
83 EXPECT_TRUE(
84 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
87 TEST_F(CookieSettingsTest, CookiesExplicitSessionOnly) {
88 cookie_settings_->SetCookieSetting(
89 ContentSettingsPattern::FromURL(kBlockedSite),
90 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_SESSION_ONLY);
91 EXPECT_TRUE(
92 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
93 EXPECT_TRUE(
94 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
95 EXPECT_TRUE(cookie_settings_->IsCookieSessionOnly(kBlockedSite));
97 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true);
98 EXPECT_TRUE(
99 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
100 EXPECT_TRUE(
101 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
102 EXPECT_TRUE(cookie_settings_->IsCookieSessionOnly(kBlockedSite));
105 TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedExplicitAllow) {
106 cookie_settings_->SetCookieSetting(
107 ContentSettingsPattern::FromURL(kAllowedSite),
108 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW);
109 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true);
110 EXPECT_TRUE(
111 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite));
112 EXPECT_TRUE(
113 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
114 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite));
116 // Extensions should always be allowed to use cookies.
117 EXPECT_TRUE(
118 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kExtensionURL));
119 EXPECT_TRUE(
120 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kExtensionURL));
123 TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedAllSitesAllowed) {
124 cookie_settings_->SetCookieSetting(
125 ContentSettingsPattern::FromURL(kAllowedSite),
126 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW);
127 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true);
128 // As an example for a pattern that matches all hosts but not all origins,
129 // match all HTTPS sites.
130 cookie_settings_->SetCookieSetting(kAllHttpsSitesPattern,
131 ContentSettingsPattern::Wildcard(),
132 CONTENT_SETTING_ALLOW);
133 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
135 // |kAllowedSite| should be allowed.
136 EXPECT_TRUE(
137 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kBlockedSite));
138 EXPECT_TRUE(
139 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kBlockedSite));
140 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite));
142 // HTTPS sites should be allowed in a first-party context.
143 EXPECT_TRUE(cookie_settings_->IsReadingCookieAllowed(kHttpsSite, kHttpsSite));
144 EXPECT_TRUE(cookie_settings_->IsSettingCookieAllowed(kHttpsSite, kHttpsSite));
145 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite));
147 // HTTP sites should be allowed, but session-only.
148 EXPECT_TRUE(cookie_settings_->IsReadingCookieAllowed(kFirstPartySite,
149 kFirstPartySite));
150 EXPECT_TRUE(cookie_settings_->IsSettingCookieAllowed(kFirstPartySite,
151 kFirstPartySite));
152 EXPECT_TRUE(cookie_settings_->IsCookieSessionOnly(kFirstPartySite));
154 // Third-party cookies should be blocked.
155 EXPECT_FALSE(
156 cookie_settings_->IsReadingCookieAllowed(kFirstPartySite, kBlockedSite));
157 EXPECT_FALSE(
158 cookie_settings_->IsSettingCookieAllowed(kFirstPartySite, kBlockedSite));
159 EXPECT_FALSE(
160 cookie_settings_->IsReadingCookieAllowed(kHttpsSite, kBlockedSite));
161 EXPECT_FALSE(
162 cookie_settings_->IsSettingCookieAllowed(kHttpsSite, kBlockedSite));
165 TEST_F(CookieSettingsTest, CookiesBlockEverything) {
166 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
168 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(kFirstPartySite,
169 kFirstPartySite));
170 EXPECT_FALSE(cookie_settings_->IsSettingCookieAllowed(kFirstPartySite,
171 kFirstPartySite));
172 EXPECT_FALSE(
173 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
176 TEST_F(CookieSettingsTest, CookiesBlockEverythingExceptAllowed) {
177 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
178 cookie_settings_->SetCookieSetting(
179 ContentSettingsPattern::FromURL(kAllowedSite),
180 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW);
181 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(kFirstPartySite,
182 kFirstPartySite));
183 EXPECT_FALSE(cookie_settings_->IsSettingCookieAllowed(kFirstPartySite,
184 kFirstPartySite));
185 EXPECT_TRUE(
186 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite));
187 EXPECT_TRUE(
188 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
189 EXPECT_TRUE(
190 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kAllowedSite));
191 EXPECT_TRUE(
192 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kAllowedSite));
193 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite));
196 TEST_F(CookieSettingsTest, CookiesBlockSingleFirstParty) {
197 cookie_settings_->SetCookieSetting(
198 ContentSettingsPattern::FromURL(kAllowedSite),
199 ContentSettingsPattern::FromURL(kFirstPartySite), CONTENT_SETTING_ALLOW);
200 cookie_settings_->SetCookieSetting(
201 ContentSettingsPattern::FromURL(kAllowedSite),
202 ContentSettingsPattern::FromURL(kBlockedFirstPartySite),
203 CONTENT_SETTING_BLOCK);
205 EXPECT_TRUE(
206 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite));
207 EXPECT_TRUE(
208 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
209 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite));
211 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(
212 kAllowedSite, kBlockedFirstPartySite));
213 EXPECT_FALSE(cookie_settings_->IsSettingCookieAllowed(
214 kAllowedSite, kBlockedFirstPartySite));
216 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
218 EXPECT_TRUE(
219 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite));
220 EXPECT_TRUE(
221 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
222 EXPECT_FALSE(cookie_settings_->IsCookieSessionOnly(kAllowedSite));
224 EXPECT_FALSE(cookie_settings_->IsReadingCookieAllowed(
225 kAllowedSite, kBlockedFirstPartySite));
226 EXPECT_FALSE(cookie_settings_->IsSettingCookieAllowed(
227 kAllowedSite, kBlockedFirstPartySite));
229 cookie_settings_->ResetCookieSetting(
230 ContentSettingsPattern::FromURL(kAllowedSite),
231 ContentSettingsPattern::FromURL(kFirstPartySite));
233 EXPECT_FALSE(
234 cookie_settings_->IsReadingCookieAllowed(kAllowedSite, kFirstPartySite));
235 EXPECT_FALSE(
236 cookie_settings_->IsSettingCookieAllowed(kAllowedSite, kFirstPartySite));
239 TEST_F(CookieSettingsTest, ExtensionsRegularSettings) {
240 cookie_settings_->SetCookieSetting(
241 ContentSettingsPattern::FromURL(kBlockedSite),
242 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK);
244 // Regular cookie settings also apply to extensions.
245 EXPECT_FALSE(
246 cookie_settings_->IsReadingCookieAllowed(kBlockedSite, kExtensionURL));
249 TEST_F(CookieSettingsTest, ExtensionsOwnCookies) {
250 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
252 #if defined(ENABLE_EXTENSIONS)
253 // Extensions can always use cookies (and site data) in their own origin.
254 EXPECT_TRUE(
255 cookie_settings_->IsReadingCookieAllowed(kExtensionURL, kExtensionURL));
256 #else
257 // Except if extensions are disabled. Then the extension-specific checks do
258 // not exist and the default setting is to block.
259 EXPECT_FALSE(
260 cookie_settings_->IsReadingCookieAllowed(kExtensionURL, kExtensionURL));
261 #endif
264 TEST_F(CookieSettingsTest, ExtensionsThirdParty) {
265 prefs_.SetBoolean(prefs::kBlockThirdPartyCookies, true);
267 // XHRs stemming from extensions are exempt from third-party cookie blocking
268 // rules (as the first party is always the extension's security origin).
269 EXPECT_TRUE(
270 cookie_settings_->IsSettingCookieAllowed(kBlockedSite, kExtensionURL));
273 } // namespace
275 } // namespace content_settings