1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
// Copyright (c) 2012 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_network_delegate.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_member.h"
#include "chrome/browser/content_settings/cookie_settings.h"
#include "chrome/browser/net/safe_search_util.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/base/request_priority.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(ENABLE_EXTENSIONS)
#include "chrome/browser/extensions/event_router_forwarder.h"
#endif
#if defined(ENABLE_EXTENSIONS)
class ChromeNetworkDelegateTest : public testing::Test {
protected:
ChromeNetworkDelegateTest()
: forwarder_(new extensions::EventRouterForwarder()) {
}
void SetUp() override {
never_throttle_requests_original_value_ =
ChromeNetworkDelegate::g_never_throttle_requests_;
ChromeNetworkDelegate::g_never_throttle_requests_ = false;
}
void TearDown() override {
ChromeNetworkDelegate::g_never_throttle_requests_ =
never_throttle_requests_original_value_;
}
scoped_ptr<ChromeNetworkDelegate> CreateNetworkDelegate() {
return make_scoped_ptr(
new ChromeNetworkDelegate(forwarder_.get(), &pref_member_));
}
// Implementation moved here for access to private bits.
void NeverThrottleLogicImpl() {
scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate());
net::TestURLRequestContext context;
scoped_ptr<net::URLRequest> extension_request(context.CreateRequest(
GURL("http://example.com/"), net::DEFAULT_PRIORITY, NULL, NULL));
extension_request->set_first_party_for_cookies(
GURL("chrome-extension://abcdef/bingo.html"));
scoped_ptr<net::URLRequest> web_page_request(context.CreateRequest(
GURL("http://example.com/"), net::DEFAULT_PRIORITY, NULL, NULL));
web_page_request->set_first_party_for_cookies(
GURL("http://example.com/helloworld.html"));
ASSERT_TRUE(delegate->OnCanThrottleRequest(*extension_request));
ASSERT_FALSE(delegate->OnCanThrottleRequest(*web_page_request));
delegate->NeverThrottleRequests();
ASSERT_TRUE(ChromeNetworkDelegate::g_never_throttle_requests_);
ASSERT_FALSE(delegate->OnCanThrottleRequest(*extension_request));
ASSERT_FALSE(delegate->OnCanThrottleRequest(*web_page_request));
// Verify that the flag applies to later instances of the
// ChromeNetworkDelegate.
//
// We test the side effects of the flag rather than just the flag
// itself (which we did above) to help ensure that a changed
// implementation would show the same behavior, i.e. all instances
// of ChromeNetworkDelegate after the flag is set obey the flag.
scoped_ptr<ChromeNetworkDelegate> second_delegate(CreateNetworkDelegate());
ASSERT_FALSE(delegate->OnCanThrottleRequest(*extension_request));
ASSERT_FALSE(delegate->OnCanThrottleRequest(*web_page_request));
}
private:
bool never_throttle_requests_original_value_;
base::MessageLoopForIO message_loop_;
scoped_refptr<extensions::EventRouterForwarder> forwarder_;
BooleanPrefMember pref_member_;
};
TEST_F(ChromeNetworkDelegateTest, NeverThrottleLogic) {
NeverThrottleLogicImpl();
}
#endif // defined(ENABLE_EXTENSIONS)
class ChromeNetworkDelegateSafeSearchTest : public testing::Test {
public:
ChromeNetworkDelegateSafeSearchTest()
: thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
#if defined(ENABLE_EXTENSIONS)
forwarder_ = new extensions::EventRouterForwarder();
#endif
}
void SetUp() override {
ChromeNetworkDelegate::InitializePrefsOnUIThread(
&enable_referrers_,
NULL,
&force_safe_search_,
&force_google_safe_search_,
&force_youtube_safety_mode_,
profile_.GetTestingPrefService());
}
protected:
scoped_ptr<net::NetworkDelegate> CreateNetworkDelegate() {
scoped_ptr<ChromeNetworkDelegate> network_delegate(
new ChromeNetworkDelegate(forwarder(), &enable_referrers_));
network_delegate->set_force_safe_search(&force_safe_search_);
network_delegate->set_force_google_safe_search(&force_google_safe_search_);
network_delegate->set_force_youtube_safety_mode(
&force_youtube_safety_mode_);
return network_delegate.Pass();
}
void SetSafeSearch(bool safe_search,
bool google_safe_search,
bool youtube_safety_mode) {
force_safe_search_.SetValue(safe_search);
force_google_safe_search_.SetValue(google_safe_search);
force_youtube_safety_mode_.SetValue(youtube_safety_mode);
}
void SetDelegate(net::NetworkDelegate* delegate) {
network_delegate_ = delegate;
context_.set_network_delegate(network_delegate_);
}
// Does a request to an arbitrary URL and verifies that the SafeSearch
// enforcement utility functions were called/not called as expected.
void QueryURL(bool expect_google_safe_search,
bool expect_youtube_safety_mode) {
safe_search_util::ClearForceGoogleSafeSearchCountForTesting();
safe_search_util::ClearForceYouTubeSafetyModeCountForTesting();
scoped_ptr<net::URLRequest> request(context_.CreateRequest(
GURL("http://anyurl.com"), net::DEFAULT_PRIORITY, &delegate_, NULL));
request->Start();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(expect_google_safe_search ? 1 : 0,
safe_search_util::GetForceGoogleSafeSearchCountForTesting());
EXPECT_EQ(expect_youtube_safety_mode ? 1 : 0,
safe_search_util::GetForceYouTubeSafetyModeCountForTesting());
}
private:
extensions::EventRouterForwarder* forwarder() {
#if defined(ENABLE_EXTENSIONS)
return forwarder_.get();
#else
return NULL;
#endif
}
content::TestBrowserThreadBundle thread_bundle_;
#if defined(ENABLE_EXTENSIONS)
scoped_refptr<extensions::EventRouterForwarder> forwarder_;
#endif
TestingProfile profile_;
BooleanPrefMember enable_referrers_;
BooleanPrefMember force_safe_search_;
BooleanPrefMember force_google_safe_search_;
BooleanPrefMember force_youtube_safety_mode_;
scoped_ptr<net::URLRequest> request_;
net::TestURLRequestContext context_;
net::NetworkDelegate* network_delegate_;
net::TestDelegate delegate_;
};
TEST_F(ChromeNetworkDelegateSafeSearchTest, SafeSearch) {
scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
SetDelegate(delegate.get());
// Loop over all combinations of the three policies.
for (int i = 0; i < 8; i++) {
bool safe_search = i % 2;
bool google_safe_search = (i / 2) % 2;
bool youtube_safety_mode = i / 4;
SetSafeSearch(safe_search, google_safe_search, youtube_safety_mode);
// The old "SafeSearch" policy implies both Google and YouTube.
bool expect_google_safe_search = safe_search || google_safe_search;
bool expect_youtube_safety_mode = safe_search || youtube_safety_mode;
QueryURL(expect_google_safe_search, expect_youtube_safety_mode);
}
}
// Privacy Mode disables Channel Id if cookies are blocked (cr223191)
class ChromeNetworkDelegatePrivacyModeTest : public testing::Test {
public:
ChromeNetworkDelegatePrivacyModeTest()
: thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
#if defined(ENABLE_EXTENSIONS)
forwarder_(new extensions::EventRouterForwarder()),
#endif
cookie_settings_(CookieSettings::Factory::GetForProfile(&profile_)
.get()),
kBlockedSite("http://ads.thirdparty.com"),
kAllowedSite("http://good.allays.com"),
kFirstPartySite("http://cool.things.com"),
kBlockedFirstPartySite("http://no.thirdparties.com") {}
void SetUp() override {
ChromeNetworkDelegate::InitializePrefsOnUIThread(
&enable_referrers_, NULL, NULL, NULL, NULL,
profile_.GetTestingPrefService());
}
protected:
scoped_ptr<ChromeNetworkDelegate> CreateNetworkDelegate() {
scoped_ptr<ChromeNetworkDelegate> network_delegate(
new ChromeNetworkDelegate(forwarder(), &enable_referrers_));
network_delegate->set_cookie_settings(cookie_settings_);
return network_delegate.Pass();
}
void SetDelegate(net::NetworkDelegate* delegate) {
network_delegate_ = delegate;
context_.set_network_delegate(network_delegate_);
}
protected:
extensions::EventRouterForwarder* forwarder() {
#if defined(ENABLE_EXTENSIONS)
return forwarder_.get();
#else
return NULL;
#endif
}
content::TestBrowserThreadBundle thread_bundle_;
#if defined(ENABLE_EXTENSIONS)
scoped_refptr<extensions::EventRouterForwarder> forwarder_;
#endif
TestingProfile profile_;
CookieSettings* cookie_settings_;
BooleanPrefMember enable_referrers_;
scoped_ptr<net::URLRequest> request_;
net::TestURLRequestContext context_;
net::NetworkDelegate* network_delegate_;
const GURL kBlockedSite;
const GURL kAllowedSite;
const GURL kEmptyFirstPartySite;
const GURL kFirstPartySite;
const GURL kBlockedFirstPartySite;
};
TEST_F(ChromeNetworkDelegatePrivacyModeTest, DisablePrivacyIfCookiesAllowed) {
scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate());
SetDelegate(delegate.get());
EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
kEmptyFirstPartySite));
}
TEST_F(ChromeNetworkDelegatePrivacyModeTest, EnablePrivacyIfCookiesBlocked) {
scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate());
SetDelegate(delegate.get());
EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kBlockedSite,
kEmptyFirstPartySite));
cookie_settings_->SetCookieSetting(
ContentSettingsPattern::FromURL(kBlockedSite),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTING_BLOCK);
EXPECT_TRUE(network_delegate_->CanEnablePrivacyMode(kBlockedSite,
kEmptyFirstPartySite));
}
TEST_F(ChromeNetworkDelegatePrivacyModeTest, EnablePrivacyIfThirdPartyBlocked) {
scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate());
SetDelegate(delegate.get());
EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
kFirstPartySite));
profile_.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true);
EXPECT_TRUE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
kFirstPartySite));
profile_.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, false);
EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
kFirstPartySite));
}
TEST_F(ChromeNetworkDelegatePrivacyModeTest,
DisablePrivacyIfOnlyFirstPartyBlocked) {
scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate());
SetDelegate(delegate.get());
EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
kBlockedFirstPartySite));
cookie_settings_->SetCookieSetting(
ContentSettingsPattern::FromURL(kBlockedFirstPartySite),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTING_BLOCK);
// Privacy mode is disabled as kAllowedSite is still getting cookies
EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
kBlockedFirstPartySite));
}
|