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/cookie_settings_factory.h" |
| 12 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 13 #include "chrome/browser/content_settings/mock_settings_observer.h" |
| 14 #include "chrome/browser/prefs/pref_service.h" |
| 15 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 16 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/common/url_constants.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 testing::Test { |
| 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 CookieSettingsFactory::GetForProfile(&profile); |
| 50 |
| 51 cookie_settings->SetCookieSetting( |
| 52 ContentSettingsPattern::FromURL(kBlockedSite), |
| 53 CONTENT_SETTING_BLOCK); |
| 54 |
| 55 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 56 kBlockedSite, kBlockedSite)); |
| 57 } |
| 58 |
| 59 TEST_F(CookieSettingsTest, CookiesBlockThirdParty) { |
| 60 TestingProfile profile; |
| 61 CookieSettings* cookie_settings = |
| 62 CookieSettingsFactory::GetForProfile(&profile); |
| 63 cookie_settings->SetBlockThirdPartyCookies(true); |
| 64 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 65 kBlockedSite, kFirstPartySite)); |
| 66 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); |
| 67 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 68 kBlockedSite, kFirstPartySite)); |
| 69 |
| 70 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 71 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 72 cmd->AppendSwitch(switches::kBlockReadingThirdPartyCookies); |
| 73 |
| 74 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 75 kBlockedSite, kFirstPartySite)); |
| 76 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 77 kBlockedSite, kFirstPartySite)); |
| 78 } |
| 79 |
| 80 TEST_F(CookieSettingsTest, CookiesAllowThirdParty) { |
| 81 TestingProfile profile; |
| 82 CookieSettings* cookie_settings = |
| 83 CookieSettingsFactory::GetForProfile(&profile); |
| 84 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 85 kBlockedSite, kFirstPartySite)); |
| 86 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 87 kBlockedSite, kFirstPartySite)); |
| 88 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); |
| 89 } |
| 90 |
| 91 TEST_F(CookieSettingsTest, CookiesExplicitBlockSingleThirdParty) { |
| 92 TestingProfile profile; |
| 93 CookieSettings* cookie_settings = |
| 94 CookieSettingsFactory::GetForProfile(&profile); |
| 95 cookie_settings->SetCookieSetting( |
| 96 ContentSettingsPattern::FromURL(kBlockedSite), |
| 97 CONTENT_SETTING_BLOCK); |
| 98 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 99 kBlockedSite, kFirstPartySite)); |
| 100 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 101 kBlockedSite, kFirstPartySite)); |
| 102 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 103 kAllowedSite, kFirstPartySite)); |
| 104 } |
| 105 |
| 106 TEST_F(CookieSettingsTest, CookiesExplicitSessionOnly) { |
| 107 TestingProfile profile; |
| 108 CookieSettings* cookie_settings = |
| 109 CookieSettingsFactory::GetForProfile(&profile); |
| 110 cookie_settings->SetCookieSetting( |
| 111 ContentSettingsPattern::FromURL(kBlockedSite), |
| 112 CONTENT_SETTING_SESSION_ONLY); |
| 113 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 114 kBlockedSite, kFirstPartySite)); |
| 115 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 116 kBlockedSite, kFirstPartySite)); |
| 117 EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); |
| 118 |
| 119 cookie_settings->SetBlockThirdPartyCookies(true); |
| 120 EXPECT_TRUE(cookie_settings-> |
| 121 IsReadingCookieAllowed(kBlockedSite, kFirstPartySite)); |
| 122 EXPECT_TRUE(cookie_settings-> |
| 123 IsSettingCookieAllowed(kBlockedSite, kFirstPartySite)); |
| 124 EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); |
| 125 } |
| 126 |
| 127 TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedExplicitAllow) { |
| 128 TestingProfile profile; |
| 129 CookieSettings* cookie_settings = |
| 130 CookieSettingsFactory::GetForProfile(&profile); |
| 131 cookie_settings->SetCookieSetting( |
| 132 ContentSettingsPattern::FromURL(kAllowedSite), |
| 133 CONTENT_SETTING_ALLOW); |
| 134 cookie_settings->SetBlockThirdPartyCookies(true); |
| 135 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 136 kAllowedSite, kFirstPartySite)); |
| 137 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 138 kAllowedSite, kFirstPartySite)); |
| 139 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); |
| 140 |
| 141 // Extensions should always be allowed to use cookies. |
| 142 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 143 kAllowedSite, kExtensionURL)); |
| 144 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 145 kAllowedSite, kExtensionURL)); |
| 146 |
| 147 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 148 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 149 cmd->AppendSwitch(switches::kBlockReadingThirdPartyCookies); |
| 150 |
| 151 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 152 kAllowedSite, kFirstPartySite)); |
| 153 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); |
| 154 |
| 155 // Extensions should always be allowed to use cookies. |
| 156 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 157 kAllowedSite, kExtensionURL)); |
| 158 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 159 kAllowedSite, kExtensionURL)); |
| 160 } |
| 161 |
| 162 TEST_F(CookieSettingsTest, CookiesBlockEverything) { |
| 163 TestingProfile profile; |
| 164 CookieSettings* cookie_settings = |
| 165 CookieSettingsFactory::GetForProfile(&profile); |
| 166 cookie_settings->SetDefaultSetting(CONTENT_SETTING_BLOCK); |
| 167 |
| 168 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 169 kFirstPartySite, kFirstPartySite)); |
| 170 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 171 kFirstPartySite, kFirstPartySite)); |
| 172 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 173 kAllowedSite, kFirstPartySite)); |
| 174 } |
| 175 |
| 176 TEST_F(CookieSettingsTest, CookiesBlockEverythingExceptAllowed) { |
| 177 TestingProfile profile; |
| 178 CookieSettings* cookie_settings = |
| 179 CookieSettingsFactory::GetForProfile(&profile); |
| 180 cookie_settings->SetDefaultSetting(CONTENT_SETTING_BLOCK); |
| 181 cookie_settings->SetCookieSetting( |
| 182 ContentSettingsPattern::FromURL(kAllowedSite), |
| 183 CONTENT_SETTING_ALLOW); |
| 184 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 185 kFirstPartySite, kFirstPartySite)); |
| 186 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 187 kFirstPartySite, kFirstPartySite)); |
| 188 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 189 kAllowedSite, kFirstPartySite)); |
| 190 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 191 kAllowedSite, kFirstPartySite)); |
| 192 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 193 kAllowedSite, kAllowedSite)); |
| 194 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 195 kAllowedSite, kAllowedSite)); |
| 196 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); |
| 197 } |
| 198 |
| 199 } // namespace |
OLD | NEW |