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/content_settings_pattern.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/pref_names.h" |
| 12 #include "chrome/test/base/testing_profile.h" |
| 13 #include "googleurl/src/gurl.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 // Tests for cookie content settings. |
| 19 const GURL kBlockedSite = GURL("http://ads.thirdparty.com"); |
| 20 const GURL kAllowedSite = GURL("http://good.allays.com"); |
| 21 const GURL kFirstPartySite = GURL("http://cool.things.com"); |
| 22 const GURL kExtensionURL = GURL("chrome-extension://deadbeef"); |
| 23 |
| 24 class CookieSettingsTest : public testing::Test { |
| 25 public: |
| 26 CookieSettingsTest() : ui_thread_(BrowserThread::UI, &message_loop_) { |
| 27 } |
| 28 |
| 29 protected: |
| 30 MessageLoop message_loop_; |
| 31 BrowserThread ui_thread_; |
| 32 }; |
| 33 |
| 34 TEST_F(CookieSettingsTest, CookiesBlockSingle) { |
| 35 TestingProfile profile; |
| 36 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 37 |
| 38 cookie_settings->SetCookieSetting( |
| 39 ContentSettingsPattern::FromURL(kBlockedSite), |
| 40 CONTENT_SETTING_BLOCK); |
| 41 |
| 42 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 43 kBlockedSite, kBlockedSite)); |
| 44 } |
| 45 |
| 46 TEST_F(CookieSettingsTest, CookiesBlockThirdParty) { |
| 47 TestingProfile profile; |
| 48 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 49 profile.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); |
| 50 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 51 kBlockedSite, kFirstPartySite)); |
| 52 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); |
| 53 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 54 kBlockedSite, kFirstPartySite)); |
| 55 |
| 56 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 57 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 58 cmd->AppendSwitch(switches::kBlockReadingThirdPartyCookies); |
| 59 |
| 60 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 61 kBlockedSite, kFirstPartySite)); |
| 62 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 63 kBlockedSite, kFirstPartySite)); |
| 64 } |
| 65 |
| 66 TEST_F(CookieSettingsTest, CookiesAllowThirdParty) { |
| 67 TestingProfile profile; |
| 68 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 69 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 70 kBlockedSite, kFirstPartySite)); |
| 71 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 72 kBlockedSite, kFirstPartySite)); |
| 73 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); |
| 74 } |
| 75 |
| 76 TEST_F(CookieSettingsTest, CookiesExplicitBlockSingleThirdParty) { |
| 77 TestingProfile profile; |
| 78 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 79 cookie_settings->SetCookieSetting( |
| 80 ContentSettingsPattern::FromURL(kBlockedSite), |
| 81 CONTENT_SETTING_BLOCK); |
| 82 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 83 kBlockedSite, kFirstPartySite)); |
| 84 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 85 kBlockedSite, kFirstPartySite)); |
| 86 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 87 kAllowedSite, kFirstPartySite)); |
| 88 } |
| 89 |
| 90 TEST_F(CookieSettingsTest, CookiesExplicitSessionOnly) { |
| 91 TestingProfile profile; |
| 92 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 93 cookie_settings->SetCookieSetting( |
| 94 ContentSettingsPattern::FromURL(kBlockedSite), |
| 95 CONTENT_SETTING_SESSION_ONLY); |
| 96 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 97 kBlockedSite, kFirstPartySite)); |
| 98 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 99 kBlockedSite, kFirstPartySite)); |
| 100 EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); |
| 101 |
| 102 profile.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); |
| 103 EXPECT_TRUE(cookie_settings-> |
| 104 IsReadingCookieAllowed(kBlockedSite, kFirstPartySite)); |
| 105 EXPECT_TRUE(cookie_settings-> |
| 106 IsSettingCookieAllowed(kBlockedSite, kFirstPartySite)); |
| 107 EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); |
| 108 } |
| 109 |
| 110 TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedExplicitAllow) { |
| 111 TestingProfile profile; |
| 112 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 113 cookie_settings->SetCookieSetting( |
| 114 ContentSettingsPattern::FromURL(kAllowedSite), |
| 115 CONTENT_SETTING_ALLOW); |
| 116 profile.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); |
| 117 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 118 kAllowedSite, kFirstPartySite)); |
| 119 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 120 kAllowedSite, kFirstPartySite)); |
| 121 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); |
| 122 |
| 123 // Extensions should always be allowed to use cookies. |
| 124 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 125 kAllowedSite, kExtensionURL)); |
| 126 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 127 kAllowedSite, kExtensionURL)); |
| 128 |
| 129 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 130 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 131 cmd->AppendSwitch(switches::kBlockReadingThirdPartyCookies); |
| 132 |
| 133 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 134 kAllowedSite, kFirstPartySite)); |
| 135 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); |
| 136 |
| 137 // Extensions should always be allowed to use cookies. |
| 138 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 139 kAllowedSite, kExtensionURL)); |
| 140 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 141 kAllowedSite, kExtensionURL)); |
| 142 } |
| 143 |
| 144 TEST_F(CookieSettingsTest, CookiesBlockEverything) { |
| 145 TestingProfile profile; |
| 146 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 147 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); |
| 148 |
| 149 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 150 kFirstPartySite, kFirstPartySite)); |
| 151 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 152 kFirstPartySite, kFirstPartySite)); |
| 153 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 154 kAllowedSite, kFirstPartySite)); |
| 155 } |
| 156 |
| 157 TEST_F(CookieSettingsTest, CookiesBlockEverythingExceptAllowed) { |
| 158 TestingProfile profile; |
| 159 CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); |
| 160 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); |
| 161 cookie_settings->SetCookieSetting( |
| 162 ContentSettingsPattern::FromURL(kAllowedSite), |
| 163 CONTENT_SETTING_ALLOW); |
| 164 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( |
| 165 kFirstPartySite, kFirstPartySite)); |
| 166 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( |
| 167 kFirstPartySite, kFirstPartySite)); |
| 168 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 169 kAllowedSite, kFirstPartySite)); |
| 170 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 171 kAllowedSite, kFirstPartySite)); |
| 172 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( |
| 173 kAllowedSite, kAllowedSite)); |
| 174 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( |
| 175 kAllowedSite, kAllowedSite)); |
| 176 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); |
| 177 } |
| 178 |
| 179 } // namespace |
OLD | NEW |