| 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..e4a33f17b6a153677fb18ee0185ecb184288e273
|
| --- /dev/null
|
| +++ b/chrome/browser/content_settings/cookie_settings_unittest.cc
|
| @@ -0,0 +1,227 @@
|
| +// 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 "base/json/json_reader.h"
|
| +#include "base/json/json_writer.h"
|
| +#include "chrome/browser/content_settings/content_settings_details.h"
|
| +#include "chrome/browser/content_settings/cookie_settings.h"
|
| +#include "chrome/browser/content_settings/host_content_settings_map.h"
|
| +#include "chrome/browser/content_settings/mock_settings_observer.h"
|
| +#include "chrome/browser/prefs/pref_service.h"
|
| +#include "chrome/browser/prefs/scoped_user_pref_update.h"
|
| +#include "chrome/common/chrome_switches.h"
|
| +#include "chrome/common/pref_names.h"
|
| +#include "chrome/common/url_constants.h"
|
| +#include "chrome/test/base/testing_browser_process_test.h"
|
| +#include "chrome/test/base/testing_pref_service.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"
|
| +// FIXME: sort these out
|
| +
|
| +using ::testing::_;
|
| +
|
| +namespace {
|
| +
|
| +// Tests for cookie content settings.
|
| +const GURL kBlockedSite = GURL("http://ads.thirdparty.com");
|
| +const GURL kAllowedSite = GURL("http://good.allays.com");
|
| +const GURL kFirstPartySite = GURL("http://cool.things.com");
|
| +const GURL kExtensionURL = GURL("chrome-extension://deadbeef");
|
| +
|
| +class CookieSettingsTest : public TestingBrowserProcessTest {
|
| + public:
|
| + CookieSettingsTest() : ui_thread_(BrowserThread::UI, &message_loop_) {
|
| + }
|
| +
|
| + protected:
|
| + MessageLoop message_loop_;
|
| + BrowserThread ui_thread_;
|
| +};
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesBlockSingle) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings =
|
| + profile.GetHostContentSettingsMap()->GetCookieSettings();
|
| +
|
| + cookie_settings->SetCookieAllowed(
|
| + ContentSettingsPattern::FromURL(kBlockedSite),
|
| + ContentSettingsPattern::Wildcard(),
|
| + false);
|
| +
|
| + EXPECT_FALSE(cookie_settings->IsCookieAllowed(
|
| + kBlockedSite, kBlockedSite, false));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesBlockThirdParty) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings =
|
| + profile.GetHostContentSettingsMap()->GetCookieSettings();
|
| + cookie_settings->SetBlockThirdPartyCookies(true);
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kBlockedSite, kFirstPartySite, false));
|
| + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kBlockedSite));
|
| + EXPECT_FALSE(cookie_settings->IsCookieAllowed(
|
| + kBlockedSite, kFirstPartySite, true));
|
| +
|
| + CommandLine* cmd = CommandLine::ForCurrentProcess();
|
| + AutoReset<CommandLine> auto_reset(cmd, *cmd);
|
| + cmd->AppendSwitch(switches::kBlockReadingThirdPartyCookies);
|
| +
|
| + EXPECT_FALSE(cookie_settings->IsCookieAllowed(
|
| + kBlockedSite, kFirstPartySite, false));
|
| + EXPECT_FALSE(cookie_settings->IsCookieAllowed(
|
| + kBlockedSite, kFirstPartySite, true));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesAllowThirdParty) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings =
|
| + profile.GetHostContentSettingsMap()->GetCookieSettings();
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kBlockedSite, kFirstPartySite, false));
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kBlockedSite, kFirstPartySite, true));
|
| + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kBlockedSite));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesExplicitBlockSingleThirdParty) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings =
|
| + profile.GetHostContentSettingsMap()->GetCookieSettings();
|
| + cookie_settings->SetCookieAllowed(
|
| + ContentSettingsPattern::FromURL(kBlockedSite),
|
| + ContentSettingsPattern::Wildcard(),
|
| + false);
|
| + EXPECT_FALSE(cookie_settings->IsCookieAllowed(
|
| + kBlockedSite, kFirstPartySite, false));
|
| + EXPECT_FALSE(cookie_settings->IsCookieAllowed(
|
| + kBlockedSite, kFirstPartySite, true));
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kAllowedSite, kFirstPartySite, true));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesExplicitSessionOnly) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings =
|
| + profile.GetHostContentSettingsMap()->GetCookieSettings();
|
| + cookie_settings->SetCookieSessionOnly(
|
| + ContentSettingsPattern::FromURL(kBlockedSite),
|
| + true);
|
| + cookie_settings->SetCookieAllowed(
|
| + ContentSettingsPattern::FromURL(kBlockedSite),
|
| + ContentSettingsPattern::Wildcard(),
|
| + true);
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kBlockedSite, kFirstPartySite, false));
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kBlockedSite, kFirstPartySite, true));
|
| + EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(kBlockedSite));
|
| +
|
| + cookie_settings->SetBlockThirdPartyCookies(true);
|
| + EXPECT_TRUE(cookie_settings->
|
| + IsCookieAllowed(kBlockedSite, kFirstPartySite, false));
|
| + EXPECT_TRUE(cookie_settings->
|
| + IsCookieAllowed(kBlockedSite, kFirstPartySite, true));
|
| + EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(kBlockedSite));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedExplicitAllow) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings =
|
| + profile.GetHostContentSettingsMap()->GetCookieSettings();
|
| + cookie_settings->SetCookieAllowed(
|
| + ContentSettingsPattern::FromURL(kAllowedSite),
|
| + ContentSettingsPattern::Wildcard(),
|
| + true);
|
| + cookie_settings->SetBlockThirdPartyCookies(true);
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kAllowedSite, kFirstPartySite, false));
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kAllowedSite, kFirstPartySite, true));
|
| + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite));
|
| +
|
| + // Extensions should always be allowed to use cookies.
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kAllowedSite, kExtensionURL, false));
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kAllowedSite, kExtensionURL, true));
|
| +
|
| + CommandLine* cmd = CommandLine::ForCurrentProcess();
|
| + AutoReset<CommandLine> auto_reset(cmd, *cmd);
|
| + cmd->AppendSwitch(switches::kBlockReadingThirdPartyCookies);
|
| +
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kAllowedSite, kFirstPartySite, false));
|
| + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite));
|
| +
|
| + // Extensions should always be allowed to use cookies.
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kAllowedSite, kExtensionURL, false));
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kAllowedSite, kExtensionURL, true));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesBlockEverything) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings =
|
| + profile.GetHostContentSettingsMap()->GetCookieSettings();
|
| + cookie_settings->SetDefaultSetting(CONTENT_SETTING_BLOCK);
|
| +
|
| + EXPECT_FALSE(cookie_settings->IsCookieAllowed(
|
| + kFirstPartySite, kFirstPartySite, false));
|
| + EXPECT_FALSE(cookie_settings->IsCookieAllowed(
|
| + kFirstPartySite, kFirstPartySite, true));
|
| + EXPECT_FALSE(cookie_settings->IsCookieAllowed(
|
| + kAllowedSite, kFirstPartySite, true));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, CookiesBlockEverythingExceptAllowed) {
|
| + TestingProfile profile;
|
| + CookieSettings* cookie_settings =
|
| + profile.GetHostContentSettingsMap()->GetCookieSettings();
|
| + cookie_settings->SetDefaultSetting(CONTENT_SETTING_BLOCK);
|
| + cookie_settings->SetCookieAllowed(
|
| + ContentSettingsPattern::FromURL(kAllowedSite),
|
| + ContentSettingsPattern::Wildcard(),
|
| + true);
|
| + EXPECT_FALSE(cookie_settings->IsCookieAllowed(
|
| + kFirstPartySite, kFirstPartySite, false));
|
| + EXPECT_FALSE(cookie_settings->IsCookieAllowed(
|
| + kFirstPartySite, kFirstPartySite, true));
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kAllowedSite, kFirstPartySite, false));
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kAllowedSite, kFirstPartySite, true));
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kAllowedSite, kAllowedSite, false));
|
| + EXPECT_TRUE(cookie_settings->IsCookieAllowed(
|
| + kAllowedSite, kAllowedSite, true));
|
| + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite));
|
| +}
|
| +
|
| +TEST_F(CookieSettingsTest, MigrateObsoletePrefs) {
|
| + // This feature is currently behind a flag.
|
| + CommandLine* cmd = CommandLine::ForCurrentProcess();
|
| + AutoReset<CommandLine> auto_reset(cmd, *cmd);
|
| + cmd->AppendSwitch(switches::kEnableResourceContentSettings);
|
| +
|
| + TestingProfile profile;
|
| + PrefService* prefs = profile.GetPrefs();
|
| +
|
| + // Set obsolete data.
|
| + prefs->SetInteger(prefs::kCookieBehavior,
|
| + net::StaticCookiePolicy::BLOCK_ALL_COOKIES);
|
| +
|
| + CookieSettings* cookie_settings =
|
| + profile.GetHostContentSettingsMap()->GetCookieSettings();
|
| +
|
| + EXPECT_EQ(CONTENT_SETTING_BLOCK,
|
| + cookie_settings->GetDefaultSetting());
|
| +}
|
| +
|
| +} // namespace
|
|
|