OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "base/auto_reset.h" |
| 6 #include "base/command_line.h" |
| 7 #include "chrome/browser/content_settings/cookie_settings.h" |
| 8 #include "chrome/browser/prefs/pref_service.h" |
| 9 #include "chrome/common/chrome_switches.h" |
| 10 #include "chrome/common/content_settings_pattern.h" |
| 11 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/test/base/testing_profile.h" |
| 13 #include "googleurl/src/gurl.h" |
| 14 #include "net/base/static_cookie_policy.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 class CookieSettingsTest : public testing::Test { |
| 20 public: |
| 21 CookieSettingsTest() : |
| 22 ui_thread_(BrowserThread::UI, &message_loop_), |
| 23 kBlockedSite("http://ads.thirdparty.com"), |
| 24 kAllowedSite("http://good.allays.com"), |
| 25 kFirstPartySite("http://cool.things.com"), |
| 26 kBlockedFirstPartySite("http://no.thirdparties.com"), |
| 27 kExtensionURL("chrome-extension://deadbeef") {} |
| 28 |
| 29 protected: |
| 30 MessageLoop message_loop_; |
| 31 BrowserThread ui_thread_; |
| 32 const GURL kBlockedSite; |
| 33 const GURL kAllowedSite; |
| 34 const GURL kFirstPartySite; |
| 35 const GURL kBlockedFirstPartySite; |
| 36 const GURL kExtensionURL; |
| 37 }; |
| 38 |
| 39 TEST_F(CookieSettingsTest, CookiesBlockSingle) { |
| 40 TestingProfile profile; |
| 41 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 42 cookie_settings->SetCookieSetting( |
| 43 ContentSettingsPattern::FromURL(kBlockedSite), |
| 44 CONTENT_SETTING_BLOCK); |
| 45 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 46 kBlockedSite, kBlockedSite)); |
| 47 } |
| 48 |
| 49 TEST_F(CookieSettingsTest, CookiesBlockThirdParty) { |
| 50 TestingProfile profile; |
| 51 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 52 profile.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); |
| 53 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 54 kBlockedSite, kFirstPartySite)); |
| 55 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); |
| 56 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 57 kBlockedSite, kFirstPartySite)); |
| 58 |
| 59 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 60 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 61 cmd->AppendSwitch(switches::kBlockReadingThirdPartyCookies); |
| 62 |
| 63 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 64 kBlockedSite, kFirstPartySite)); |
| 65 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 66 kBlockedSite, kFirstPartySite)); |
| 67 } |
| 68 |
| 69 TEST_F(CookieSettingsTest, CookiesAllowThirdParty) { |
| 70 TestingProfile profile; |
| 71 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 72 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 73 kBlockedSite, kFirstPartySite)); |
| 74 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 75 kBlockedSite, kFirstPartySite)); |
| 76 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); |
| 77 } |
| 78 |
| 79 TEST_F(CookieSettingsTest, CookiesExplicitBlockSingleThirdParty) { |
| 80 TestingProfile profile; |
| 81 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 82 cookie_settings->SetCookieSetting( |
| 83 ContentSettingsPattern::FromURL(kBlockedSite), |
| 84 CONTENT_SETTING_BLOCK); |
| 85 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 86 kBlockedSite, kFirstPartySite)); |
| 87 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 88 kBlockedSite, kFirstPartySite)); |
| 89 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 90 kAllowedSite, kFirstPartySite)); |
| 91 } |
| 92 |
| 93 TEST_F(CookieSettingsTest, CookiesExplicitSessionOnly) { |
| 94 TestingProfile profile; |
| 95 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 96 cookie_settings->SetCookieSetting( |
| 97 ContentSettingsPattern::FromURL(kBlockedSite), |
| 98 CONTENT_SETTING_SESSION_ONLY); |
| 99 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 100 kBlockedSite, kFirstPartySite)); |
| 101 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 102 kBlockedSite, kFirstPartySite)); |
| 103 EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); |
| 104 |
| 105 profile.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); |
| 106 EXPECT_TRUE(cookie_settings-> |
| 107 IsReadingCookieAllowed(kBlockedSite, kFirstPartySite)); |
| 108 EXPECT_TRUE(cookie_settings-> |
| 109 IsSettingCookieAllowed(kBlockedSite, kFirstPartySite)); |
| 110 EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); |
| 111 } |
| 112 |
| 113 TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedExplicitAllow) { |
| 114 TestingProfile profile; |
| 115 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 116 cookie_settings->SetCookieSetting( |
| 117 ContentSettingsPattern::FromURL(kAllowedSite), |
| 118 CONTENT_SETTING_ALLOW); |
| 119 profile.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); |
| 120 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 121 kAllowedSite, kFirstPartySite)); |
| 122 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 123 kAllowedSite, kFirstPartySite)); |
| 124 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); |
| 125 |
| 126 // Extensions should always be allowed to use cookies. |
| 127 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 128 kAllowedSite, kExtensionURL)); |
| 129 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 130 kAllowedSite, kExtensionURL)); |
| 131 |
| 132 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 133 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 134 cmd->AppendSwitch(switches::kBlockReadingThirdPartyCookies); |
| 135 |
| 136 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 137 kAllowedSite, kFirstPartySite)); |
| 138 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); |
| 139 |
| 140 // Extensions should always be allowed to use cookies. |
| 141 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 142 kAllowedSite, kExtensionURL)); |
| 143 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 144 kAllowedSite, kExtensionURL)); |
| 145 } |
| 146 |
| 147 TEST_F(CookieSettingsTest, CookiesBlockEverything) { |
| 148 TestingProfile profile; |
| 149 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 150 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); |
| 151 |
| 152 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 153 kFirstPartySite, kFirstPartySite)); |
| 154 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 155 kFirstPartySite, kFirstPartySite)); |
| 156 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 157 kAllowedSite, kFirstPartySite)); |
| 158 } |
| 159 |
| 160 TEST_F(CookieSettingsTest, CookiesBlockEverythingExceptAllowed) { |
| 161 TestingProfile profile; |
| 162 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 163 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); |
| 164 cookie_settings->SetCookieSetting( |
| 165 ContentSettingsPattern::FromURL(kAllowedSite), |
| 166 CONTENT_SETTING_ALLOW); |
| 167 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 168 kFirstPartySite, kFirstPartySite)); |
| 169 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 170 kFirstPartySite, kFirstPartySite)); |
| 171 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 172 kAllowedSite, kFirstPartySite)); |
| 173 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 174 kAllowedSite, kFirstPartySite)); |
| 175 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 176 kAllowedSite, kAllowedSite)); |
| 177 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 178 kAllowedSite, kAllowedSite)); |
| 179 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); |
| 180 } |
| 181 |
| 182 TEST_F(CookieSettingsTest, MigrateObsoleteCookiePrefs) { |
| 183 TestingProfile profile; |
| 184 PrefService* prefs = profile.GetPrefs(); |
| 185 prefs->SetInteger(prefs::kCookieBehavior, |
| 186 net::StaticCookiePolicy::BLOCK_ALL_COOKIES); |
| 187 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 188 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 189 cookie_settings->GetDefaultCookieSetting(NULL)); |
| 190 } |
| 191 |
| 192 TEST_F(CookieSettingsTest, CookiesBlockSingleFirstParty) { |
| 193 TestingProfile profile; |
| 194 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 195 cookie_settings->SetCookieSetting( |
| 196 ContentSettingsPattern::FromURL(kAllowedSite), |
| 197 ContentSettingsPattern::FromURL(kFirstPartySite), |
| 198 CONTENT_SETTING_ALLOW); |
| 199 cookie_settings->SetCookieSetting( |
| 200 ContentSettingsPattern::FromURL(kAllowedSite), |
| 201 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), |
| 202 CONTENT_SETTING_BLOCK); |
| 203 |
| 204 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 205 kAllowedSite, kFirstPartySite)); |
| 206 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 207 kAllowedSite, kFirstPartySite)); |
| 208 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); |
| 209 |
| 210 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 211 kAllowedSite, kBlockedFirstPartySite)); |
| 212 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 213 kAllowedSite, kBlockedFirstPartySite)); |
| 214 |
| 215 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); |
| 216 |
| 217 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 218 kAllowedSite, kFirstPartySite)); |
| 219 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 220 kAllowedSite, kFirstPartySite)); |
| 221 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); |
| 222 |
| 223 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 224 kAllowedSite, kBlockedFirstPartySite)); |
| 225 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 226 kAllowedSite, kBlockedFirstPartySite)); |
| 227 |
| 228 cookie_settings->ResetCookieSetting( |
| 229 ContentSettingsPattern::FromURL(kAllowedSite), |
| 230 ContentSettingsPattern::FromURL(kFirstPartySite)); |
| 231 |
| 232 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 233 kAllowedSite, kFirstPartySite)); |
| 234 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 235 kAllowedSite, kFirstPartySite)); |
| 236 } |
| 237 |
| 238 } // namespace |
OLD | NEW |