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