diff options
author | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-11 21:19:33 +0000 |
---|---|---|
committer | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-11 21:19:33 +0000 |
commit | 658959349622d3350a4cdaebac6bc3a1a02c58ec (patch) | |
tree | 0a012a044696be1729d30819a1585eade97033aa | |
parent | 1305034e8302fe722ee5048df1cd1a7fa4a6dedf (diff) | |
download | chromium_src-658959349622d3350a4cdaebac6bc3a1a02c58ec.zip chromium_src-658959349622d3350a4cdaebac6bc3a1a02c58ec.tar.gz chromium_src-658959349622d3350a4cdaebac6bc3a1a02c58ec.tar.bz2 |
Add a unit test for ChromeCookiePolicy.
BUG=none
TEST=yup
Review URL: http://codereview.chromium.org/7003011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85044 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/net/chrome_cookie_policy.h | 1 | ||||
-rw-r--r-- | chrome/browser/net/chrome_cookie_policy_unittest.cc | 149 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 |
3 files changed, 151 insertions, 0 deletions
diff --git a/chrome/browser/net/chrome_cookie_policy.h b/chrome/browser/net/chrome_cookie_policy.h index e050d4b..907fb5e 100644 --- a/chrome/browser/net/chrome_cookie_policy.h +++ b/chrome/browser/net/chrome_cookie_policy.h @@ -37,6 +37,7 @@ class ChromeCookiePolicy : public net::CookiePolicy { // True if blocking third-party cookies also applies to reading them. bool strict_third_party_blocking_; + friend class ChromeCookiePolicyTest; DISALLOW_COPY_AND_ASSIGN(ChromeCookiePolicy); }; diff --git a/chrome/browser/net/chrome_cookie_policy_unittest.cc b/chrome/browser/net/chrome_cookie_policy_unittest.cc new file mode 100644 index 0000000..4721076 --- /dev/null +++ b/chrome/browser/net/chrome_cookie_policy_unittest.cc @@ -0,0 +1,149 @@ +// 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 "chrome/browser/net/chrome_cookie_policy.h" + +#include "chrome/browser/content_settings/content_settings_pattern.h" +#include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/common/content_settings.h" +#include "chrome/common/content_settings_types.h" +#include "chrome/test/testing_profile.h" +#include "content/browser/browser_thread.h" +#include "googleurl/src/gurl.h" +#include "net/base/net_errors.h" +#include "testing/gtest/include/gtest/gtest.h" + +static const GURL kBlockedSite = GURL("http://ads.thirdparty.com"); +static const GURL kAllowedSite = GURL("http://good.allays.com"); +static const GURL kFirstPartySite = GURL("http://cool.things.com"); + +class ChromeCookiePolicyTest : public testing::Test { + public: + ChromeCookiePolicyTest() + : ui_thread_(BrowserThread::UI, &message_loop_), + io_thread_(BrowserThread::IO, &message_loop_), + cookie_policy_(settings()) { + } + + TestingProfile* profile() { return &profile_; } + + HostContentSettingsMap* settings() { + return profile()->GetHostContentSettingsMap(); + } + + ChromeCookiePolicy* policy() { return &cookie_policy_; } + + void set_strict_third_party_blocking(bool flag) { + policy()->strict_third_party_blocking_ = flag; + } + + void SetException(const GURL& url, ContentSetting setting) { + settings()->AddExceptionForURL(url, CONTENT_SETTINGS_TYPE_COOKIES, "", + setting); + } + + private: + // HostContentSettingsMap can only operate and be deleted on the UI thread. + // Give it a fake one. + MessageLoop message_loop_; + BrowserThread ui_thread_; + BrowserThread io_thread_; + + TestingProfile profile_; + ChromeCookiePolicy cookie_policy_; +}; + +namespace { + +TEST_F(ChromeCookiePolicyTest, BlockSingle) { + SetException(kBlockedSite, CONTENT_SETTING_BLOCK); + EXPECT_EQ(net::ERR_ACCESS_DENIED, + policy()->CanGetCookies(kBlockedSite, kBlockedSite)); +} + +TEST_F(ChromeCookiePolicyTest, BlockThirdParty) { + settings()->SetBlockThirdPartyCookies(true); + EXPECT_EQ(net::OK, + policy()->CanGetCookies(kBlockedSite, kFirstPartySite)); + EXPECT_EQ(net::ERR_ACCESS_DENIED, + policy()->CanSetCookie(kBlockedSite, kFirstPartySite, "")); + + set_strict_third_party_blocking(true); + EXPECT_EQ(net::ERR_ACCESS_DENIED, + policy()->CanGetCookies(kBlockedSite, kFirstPartySite)); +} + +TEST_F(ChromeCookiePolicyTest, AllowThirdParty) { + settings()->SetBlockThirdPartyCookies(false); + EXPECT_EQ(net::OK, policy()->CanGetCookies(kBlockedSite, kFirstPartySite)); + EXPECT_EQ(net::OK, policy()->CanSetCookie(kBlockedSite, kFirstPartySite, "")); +} + +TEST_F(ChromeCookiePolicyTest, ExplicitBlockSingleThirdParty) { + SetException(kBlockedSite, CONTENT_SETTING_BLOCK); + settings()->SetBlockThirdPartyCookies(false); + + EXPECT_EQ(net::ERR_ACCESS_DENIED, + policy()->CanGetCookies(kBlockedSite, kFirstPartySite)); + EXPECT_EQ(net::ERR_ACCESS_DENIED, + policy()->CanSetCookie(kBlockedSite, kFirstPartySite, "")); + EXPECT_EQ(net::OK, + policy()->CanSetCookie(kAllowedSite, kFirstPartySite, "")); +} + +TEST_F(ChromeCookiePolicyTest, ExplicitSessionOnly) { + SetException(kBlockedSite, CONTENT_SETTING_SESSION_ONLY); + + settings()->SetBlockThirdPartyCookies(false); + EXPECT_EQ(net::OK, policy()->CanGetCookies(kBlockedSite, kFirstPartySite)); + EXPECT_EQ(net::OK_FOR_SESSION_ONLY, + policy()->CanSetCookie(kBlockedSite, kFirstPartySite, "")); + + settings()->SetBlockThirdPartyCookies(true); + EXPECT_EQ(net::OK, policy()->CanGetCookies(kBlockedSite, kFirstPartySite)); + EXPECT_EQ(net::ERR_ACCESS_DENIED, + policy()->CanSetCookie(kBlockedSite, kFirstPartySite, "")); +} + +TEST_F(ChromeCookiePolicyTest, ThirdPartyAlwaysBlocked) { + SetException(kAllowedSite, CONTENT_SETTING_ALLOW); + + settings()->SetBlockThirdPartyCookies(true); + EXPECT_EQ(net::OK, + policy()->CanGetCookies(kAllowedSite, kFirstPartySite)); + EXPECT_EQ(net::ERR_ACCESS_DENIED, + policy()->CanSetCookie(kAllowedSite, kFirstPartySite, "")); + + set_strict_third_party_blocking(true); + EXPECT_EQ(net::ERR_ACCESS_DENIED, + policy()->CanGetCookies(kAllowedSite, kFirstPartySite)); +} + +TEST_F(ChromeCookiePolicyTest, BlockEverything) { + settings()->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, + CONTENT_SETTING_BLOCK); + EXPECT_EQ(net::ERR_ACCESS_DENIED, + policy()->CanGetCookies(kFirstPartySite, kFirstPartySite)); + EXPECT_EQ(net::ERR_ACCESS_DENIED, + policy()->CanSetCookie(kFirstPartySite, kFirstPartySite, "")); + EXPECT_EQ(net::ERR_ACCESS_DENIED, + policy()->CanSetCookie(kAllowedSite, kFirstPartySite, "")); +} + +TEST_F(ChromeCookiePolicyTest, BlockEverythingExceptAllowed) { + settings()->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, + CONTENT_SETTING_BLOCK); + SetException(kAllowedSite, CONTENT_SETTING_ALLOW); + + EXPECT_EQ(net::ERR_ACCESS_DENIED, + policy()->CanGetCookies(kFirstPartySite, kFirstPartySite)); + EXPECT_EQ(net::ERR_ACCESS_DENIED, + policy()->CanSetCookie(kFirstPartySite, kFirstPartySite, "")); + EXPECT_EQ(net::OK, policy()->CanGetCookies(kAllowedSite, kFirstPartySite)); + EXPECT_EQ(net::OK, policy()->CanSetCookie(kAllowedSite, kFirstPartySite, "")); + EXPECT_EQ(net::OK, policy()->CanGetCookies(kAllowedSite, kAllowedSite)); + EXPECT_EQ(net::OK, policy()->CanSetCookie(kAllowedSite, kAllowedSite, "")); +} + +} diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 216d7f1..2996876 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1395,6 +1395,7 @@ 'browser/mock_keychain_mac.h', 'browser/mock_plugin_exceptions_table_model.cc', 'browser/mock_plugin_exceptions_table_model.h', + 'browser/net/chrome_cookie_policy_unittest.cc', 'browser/net/chrome_net_log_unittest.cc', 'browser/net/connection_tester_unittest.cc', 'browser/net/gaia/token_service_unittest.cc', |