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