| Index: chrome/browser/content_settings/cookie_settings_unittest.cc
|
| diff --git a/chrome/browser/content_settings/cookie_settings_unittest.cc b/chrome/browser/content_settings/cookie_settings_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..bc94ede3c1d78c30a59c7d693522c03edd1bfd4c
|
| --- /dev/null
|
| +++ b/chrome/browser/content_settings/cookie_settings_unittest.cc
|
| @@ -0,0 +1,238 @@
|
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/auto_reset.h"
|
| +#include "base/command_line.h"
|
| +#include "chrome/browser/content_settings/cookie_settings.h"
|
| +#include "chrome/browser/prefs/pref_service.h"
|
| +#include "chrome/common/chrome_switches.h"
|
| +#include "chrome/common/content_settings_pattern.h"
|
| +#include "chrome/common/pref_names.h"
|
| +#include "chrome/test/base/testing_profile.h"
|
| +#include "googleurl/src/gurl.h"
|
| +#include "net/base/static_cookie_policy.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace {
|
| +
|
| +class CookieSettingsTest : public testing::Test {
|
| + public:
|
| + CookieSettingsTest() :
|
| + ui_thread_(BrowserThread::UI, &message_loop_),
|
| + kBlockedSite("http://ads.thirdparty.com"),
|
| + kAllowedSite("http://good.allays.com"),
|
| + kFirstPartySite("http://cool.things.com"),
|
| + kBlockedFirstPartySite("http://no.thirdparties.com"),
|
| + kExtensionURL("chrome-extension://deadbeef") {}
|
| +
|
| + protected:
|
| + MessageLoop message_loop_;
|
| + BrowserThread ui_thread_;
|
| + const GURL kBlockedSite;
|
| + const GURL kAllowedSite;
|
| + const GURL kFirstPartySite;
|
| + const GURL kBlockedFirstPartySite;
|
| + const GURL kExtensionURL;
|
| +};
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesBlockSingle) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile);
|
| + cookie_settings->SetCookieSetting(
|
| + ContentSettingsPattern::FromURL(kBlockedSite),
|
| + CONTENT_SETTING_BLOCK);
|
| + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed(
|
| + kBlockedSite, kBlockedSite));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesBlockThirdParty) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile);
|
| + profile.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true);
|
| + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(
|
| + kBlockedSite, kFirstPartySite));
|
| + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kBlockedSite));
|
| + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed(
|
| + kBlockedSite, kFirstPartySite));
|
| +
|
| + CommandLine* cmd = CommandLine::ForCurrentProcess();
|
| + AutoReset<CommandLine> auto_reset(cmd, *cmd);
|
| + cmd->AppendSwitch(switches::kBlockReadingThirdPartyCookies);
|
| +
|
| + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed(
|
| + kBlockedSite, kFirstPartySite));
|
| + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed(
|
| + kBlockedSite, kFirstPartySite));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesAllowThirdParty) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile);
|
| + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(
|
| + kBlockedSite, kFirstPartySite));
|
| + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
|
| + kBlockedSite, kFirstPartySite));
|
| + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kBlockedSite));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesExplicitBlockSingleThirdParty) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile);
|
| + cookie_settings->SetCookieSetting(
|
| + ContentSettingsPattern::FromURL(kBlockedSite),
|
| + CONTENT_SETTING_BLOCK);
|
| + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed(
|
| + kBlockedSite, kFirstPartySite));
|
| + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed(
|
| + kBlockedSite, kFirstPartySite));
|
| + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
|
| + kAllowedSite, kFirstPartySite));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesExplicitSessionOnly) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile);
|
| + cookie_settings->SetCookieSetting(
|
| + ContentSettingsPattern::FromURL(kBlockedSite),
|
| + CONTENT_SETTING_SESSION_ONLY);
|
| + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(
|
| + kBlockedSite, kFirstPartySite));
|
| + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
|
| + kBlockedSite, kFirstPartySite));
|
| + EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(kBlockedSite));
|
| +
|
| + profile.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true);
|
| + EXPECT_TRUE(cookie_settings->
|
| + IsReadingCookieAllowed(kBlockedSite, kFirstPartySite));
|
| + EXPECT_TRUE(cookie_settings->
|
| + IsSettingCookieAllowed(kBlockedSite, kFirstPartySite));
|
| + EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(kBlockedSite));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedExplicitAllow) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile);
|
| + cookie_settings->SetCookieSetting(
|
| + ContentSettingsPattern::FromURL(kAllowedSite),
|
| + CONTENT_SETTING_ALLOW);
|
| + profile.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true);
|
| + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(
|
| + kAllowedSite, kFirstPartySite));
|
| + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
|
| + kAllowedSite, kFirstPartySite));
|
| + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite));
|
| +
|
| + // Extensions should always be allowed to use cookies.
|
| + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(
|
| + kAllowedSite, kExtensionURL));
|
| + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
|
| + kAllowedSite, kExtensionURL));
|
| +
|
| + CommandLine* cmd = CommandLine::ForCurrentProcess();
|
| + AutoReset<CommandLine> auto_reset(cmd, *cmd);
|
| + cmd->AppendSwitch(switches::kBlockReadingThirdPartyCookies);
|
| +
|
| + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(
|
| + kAllowedSite, kFirstPartySite));
|
| + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite));
|
| +
|
| + // Extensions should always be allowed to use cookies.
|
| + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(
|
| + kAllowedSite, kExtensionURL));
|
| + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
|
| + kAllowedSite, kExtensionURL));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesBlockEverything) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile);
|
| + cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
|
| +
|
| + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed(
|
| + kFirstPartySite, kFirstPartySite));
|
| + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed(
|
| + kFirstPartySite, kFirstPartySite));
|
| + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed(
|
| + kAllowedSite, kFirstPartySite));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesBlockEverythingExceptAllowed) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile);
|
| + cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
|
| + cookie_settings->SetCookieSetting(
|
| + ContentSettingsPattern::FromURL(kAllowedSite),
|
| + CONTENT_SETTING_ALLOW);
|
| + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed(
|
| + kFirstPartySite, kFirstPartySite));
|
| + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed(
|
| + kFirstPartySite, kFirstPartySite));
|
| + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(
|
| + kAllowedSite, kFirstPartySite));
|
| + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
|
| + kAllowedSite, kFirstPartySite));
|
| + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(
|
| + kAllowedSite, kAllowedSite));
|
| + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
|
| + kAllowedSite, kAllowedSite));
|
| + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, MigrateObsoleteCookiePrefs) {
|
| + TestingProfile profile;
|
| + PrefService* prefs = profile.GetPrefs();
|
| + prefs->SetInteger(prefs::kCookieBehavior,
|
| + net::StaticCookiePolicy::BLOCK_ALL_COOKIES);
|
| + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile);
|
| + EXPECT_EQ(CONTENT_SETTING_BLOCK,
|
| + cookie_settings->GetDefaultCookieSetting(NULL));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesBlockSingleFirstParty) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile);
|
| + cookie_settings->SetCookieSetting(
|
| + ContentSettingsPattern::FromURL(kAllowedSite),
|
| + ContentSettingsPattern::FromURL(kFirstPartySite),
|
| + CONTENT_SETTING_ALLOW);
|
| + cookie_settings->SetCookieSetting(
|
| + ContentSettingsPattern::FromURL(kAllowedSite),
|
| + ContentSettingsPattern::FromURL(kBlockedFirstPartySite),
|
| + CONTENT_SETTING_BLOCK);
|
| +
|
| + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(
|
| + kAllowedSite, kFirstPartySite));
|
| + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
|
| + kAllowedSite, kFirstPartySite));
|
| + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite));
|
| +
|
| + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed(
|
| + kAllowedSite, kBlockedFirstPartySite));
|
| + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed(
|
| + kAllowedSite, kBlockedFirstPartySite));
|
| +
|
| + cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
|
| +
|
| + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(
|
| + kAllowedSite, kFirstPartySite));
|
| + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
|
| + kAllowedSite, kFirstPartySite));
|
| + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite));
|
| +
|
| + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed(
|
| + kAllowedSite, kBlockedFirstPartySite));
|
| + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed(
|
| + kAllowedSite, kBlockedFirstPartySite));
|
| +
|
| + cookie_settings->ResetCookieSetting(
|
| + ContentSettingsPattern::FromURL(kAllowedSite),
|
| + ContentSettingsPattern::FromURL(kFirstPartySite));
|
| +
|
| + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed(
|
| + kAllowedSite, kFirstPartySite));
|
| + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed(
|
| + kAllowedSite, kFirstPartySite));
|
| +}
|
| +
|
| +} // namespace
|
|
|