1 // Copyright (c) 2012 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.
5 #include "chrome/browser/net/chrome_network_delegate.h"
7 #include "base/command_line.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/pref_member.h"
12 #include "chrome/browser/content_settings/cookie_settings.h"
13 #include "chrome/browser/net/safe_search_util.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/testing_pref_service_syncable.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "content/public/common/content_switches.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "net/base/request_priority.h"
20 #include "net/url_request/url_request.h"
21 #include "net/url_request/url_request_test_util.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 #if defined(ENABLE_EXTENSIONS)
25 #include "chrome/browser/extensions/event_router_forwarder.h"
28 TEST(ChromeNetworkDelegateTest
, DisableFirstPartyOnlyCookiesIffFlagDisabled
) {
29 BooleanPrefMember pref_member_
;
30 scoped_ptr
<ChromeNetworkDelegate
> delegate
;
32 #if defined(ENABLE_EXTENSIONS)
33 scoped_refptr
<extensions::EventRouterForwarder
> forwarder
=
34 new extensions::EventRouterForwarder();
35 delegate
.reset(new ChromeNetworkDelegate(forwarder
.get(), &pref_member_
));
37 delegate
.reset(new ChromeNetworkDelegate(nullptr, &pref_member_
));
39 EXPECT_FALSE(delegate
->FirstPartyOnlyCookieExperimentEnabled());
42 TEST(ChromeNetworkDelegateTest
, EnableFirstPartyOnlyCookiesIffFlagEnabled
) {
43 base::CommandLine::ForCurrentProcess()->AppendSwitch(
44 switches::kEnableExperimentalWebPlatformFeatures
);
45 BooleanPrefMember pref_member_
;
46 scoped_ptr
<ChromeNetworkDelegate
> delegate
;
48 #if defined(ENABLE_EXTENSIONS)
49 scoped_refptr
<extensions::EventRouterForwarder
> forwarder
=
50 new extensions::EventRouterForwarder();
51 delegate
.reset(new ChromeNetworkDelegate(forwarder
.get(), &pref_member_
));
53 delegate
.reset(new ChromeNetworkDelegate(nullptr, &pref_member_
));
55 EXPECT_TRUE(delegate
->FirstPartyOnlyCookieExperimentEnabled());
58 #if defined(ENABLE_EXTENSIONS)
59 class ChromeNetworkDelegateThrottlingTest
: public testing::Test
{
61 ChromeNetworkDelegateThrottlingTest()
62 : forwarder_(new extensions::EventRouterForwarder()) {}
64 void SetUp() override
{
65 never_throttle_requests_original_value_
=
66 ChromeNetworkDelegate::g_never_throttle_requests_
;
67 ChromeNetworkDelegate::g_never_throttle_requests_
= false;
70 void TearDown() override
{
71 ChromeNetworkDelegate::g_never_throttle_requests_
=
72 never_throttle_requests_original_value_
;
75 scoped_ptr
<ChromeNetworkDelegate
> CreateNetworkDelegate() {
76 return make_scoped_ptr(
77 new ChromeNetworkDelegate(forwarder(), &pref_member_
));
80 // Implementation moved here for access to private bits.
81 void NeverThrottleLogicImpl() {
82 scoped_ptr
<ChromeNetworkDelegate
> delegate(CreateNetworkDelegate());
84 net::TestURLRequestContext context
;
85 scoped_ptr
<net::URLRequest
> extension_request(context
.CreateRequest(
86 GURL("http://example.com/"), net::DEFAULT_PRIORITY
, NULL
));
87 extension_request
->set_first_party_for_cookies(
88 GURL("chrome-extension://abcdef/bingo.html"));
89 scoped_ptr
<net::URLRequest
> web_page_request(context
.CreateRequest(
90 GURL("http://example.com/"), net::DEFAULT_PRIORITY
, NULL
));
91 web_page_request
->set_first_party_for_cookies(
92 GURL("http://example.com/helloworld.html"));
94 ASSERT_TRUE(delegate
->OnCanThrottleRequest(*extension_request
));
95 ASSERT_FALSE(delegate
->OnCanThrottleRequest(*web_page_request
));
97 delegate
->NeverThrottleRequests();
98 ASSERT_TRUE(ChromeNetworkDelegate::g_never_throttle_requests_
);
99 ASSERT_FALSE(delegate
->OnCanThrottleRequest(*extension_request
));
100 ASSERT_FALSE(delegate
->OnCanThrottleRequest(*web_page_request
));
102 // Verify that the flag applies to later instances of the
103 // ChromeNetworkDelegate.
105 // We test the side effects of the flag rather than just the flag
106 // itself (which we did above) to help ensure that a changed
107 // implementation would show the same behavior, i.e. all instances
108 // of ChromeNetworkDelegate after the flag is set obey the flag.
109 scoped_ptr
<ChromeNetworkDelegate
> second_delegate(CreateNetworkDelegate());
110 ASSERT_FALSE(delegate
->OnCanThrottleRequest(*extension_request
));
111 ASSERT_FALSE(delegate
->OnCanThrottleRequest(*web_page_request
));
115 extensions::EventRouterForwarder
* forwarder() { return forwarder_
.get(); }
117 bool never_throttle_requests_original_value_
;
118 base::MessageLoopForIO message_loop_
;
120 scoped_refptr
<extensions::EventRouterForwarder
> forwarder_
;
121 BooleanPrefMember pref_member_
;
124 TEST_F(ChromeNetworkDelegateThrottlingTest
, NeverThrottleLogic
) {
125 NeverThrottleLogicImpl();
127 #endif // defined(ENABLE_EXTENSIONS)
129 class ChromeNetworkDelegateSafeSearchTest
: public testing::Test
{
131 ChromeNetworkDelegateSafeSearchTest()
132 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
) {
133 #if defined(ENABLE_EXTENSIONS)
134 forwarder_
= new extensions::EventRouterForwarder();
138 void SetUp() override
{
139 ChromeNetworkDelegate::InitializePrefsOnUIThread(
143 &force_google_safe_search_
,
144 &force_youtube_safety_mode_
,
145 profile_
.GetTestingPrefService());
149 scoped_ptr
<net::NetworkDelegate
> CreateNetworkDelegate() {
150 scoped_ptr
<ChromeNetworkDelegate
> network_delegate(
151 new ChromeNetworkDelegate(forwarder(), &enable_referrers_
));
152 network_delegate
->set_force_safe_search(&force_safe_search_
);
153 network_delegate
->set_force_google_safe_search(&force_google_safe_search_
);
154 network_delegate
->set_force_youtube_safety_mode(
155 &force_youtube_safety_mode_
);
156 return network_delegate
.Pass();
159 void SetSafeSearch(bool safe_search
,
160 bool google_safe_search
,
161 bool youtube_safety_mode
) {
162 force_safe_search_
.SetValue(safe_search
);
163 force_google_safe_search_
.SetValue(google_safe_search
);
164 force_youtube_safety_mode_
.SetValue(youtube_safety_mode
);
167 void SetDelegate(net::NetworkDelegate
* delegate
) {
168 network_delegate_
= delegate
;
169 context_
.set_network_delegate(network_delegate_
);
172 // Does a request to an arbitrary URL and verifies that the SafeSearch
173 // enforcement utility functions were called/not called as expected.
174 void QueryURL(bool expect_google_safe_search
,
175 bool expect_youtube_safety_mode
) {
176 safe_search_util::ClearForceGoogleSafeSearchCountForTesting();
177 safe_search_util::ClearForceYouTubeSafetyModeCountForTesting();
179 scoped_ptr
<net::URLRequest
> request(context_
.CreateRequest(
180 GURL("http://anyurl.com"), net::DEFAULT_PRIORITY
, &delegate_
));
183 base::MessageLoop::current()->RunUntilIdle();
185 EXPECT_EQ(expect_google_safe_search
? 1 : 0,
186 safe_search_util::GetForceGoogleSafeSearchCountForTesting());
187 EXPECT_EQ(expect_youtube_safety_mode
? 1 : 0,
188 safe_search_util::GetForceYouTubeSafetyModeCountForTesting());
192 extensions::EventRouterForwarder
* forwarder() {
193 #if defined(ENABLE_EXTENSIONS)
194 return forwarder_
.get();
200 content::TestBrowserThreadBundle thread_bundle_
;
201 #if defined(ENABLE_EXTENSIONS)
202 scoped_refptr
<extensions::EventRouterForwarder
> forwarder_
;
204 TestingProfile profile_
;
205 BooleanPrefMember enable_referrers_
;
206 BooleanPrefMember force_safe_search_
;
207 BooleanPrefMember force_google_safe_search_
;
208 BooleanPrefMember force_youtube_safety_mode_
;
209 scoped_ptr
<net::URLRequest
> request_
;
210 net::TestURLRequestContext context_
;
211 net::NetworkDelegate
* network_delegate_
;
212 net::TestDelegate delegate_
;
215 TEST_F(ChromeNetworkDelegateSafeSearchTest
, SafeSearch
) {
216 scoped_ptr
<net::NetworkDelegate
> delegate(CreateNetworkDelegate());
217 SetDelegate(delegate
.get());
219 // Loop over all combinations of the three policies.
220 for (int i
= 0; i
< 8; i
++) {
221 bool safe_search
= i
% 2;
222 bool google_safe_search
= (i
/ 2) % 2;
223 bool youtube_safety_mode
= i
/ 4;
224 SetSafeSearch(safe_search
, google_safe_search
, youtube_safety_mode
);
226 // The old "SafeSearch" policy implies both Google and YouTube.
227 bool expect_google_safe_search
= safe_search
|| google_safe_search
;
228 bool expect_youtube_safety_mode
= safe_search
|| youtube_safety_mode
;
229 QueryURL(expect_google_safe_search
, expect_youtube_safety_mode
);
233 // Privacy Mode disables Channel Id if cookies are blocked (cr223191)
234 class ChromeNetworkDelegatePrivacyModeTest
: public testing::Test
{
236 ChromeNetworkDelegatePrivacyModeTest()
237 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
238 #if defined(ENABLE_EXTENSIONS)
239 forwarder_(new extensions::EventRouterForwarder()),
241 cookie_settings_(CookieSettings::Factory::GetForProfile(&profile_
)
243 kBlockedSite("http://ads.thirdparty.com"),
244 kAllowedSite("http://good.allays.com"),
245 kFirstPartySite("http://cool.things.com"),
246 kBlockedFirstPartySite("http://no.thirdparties.com") {}
248 void SetUp() override
{
249 ChromeNetworkDelegate::InitializePrefsOnUIThread(
250 &enable_referrers_
, NULL
, NULL
, NULL
, NULL
,
251 profile_
.GetTestingPrefService());
255 scoped_ptr
<ChromeNetworkDelegate
> CreateNetworkDelegate() {
256 scoped_ptr
<ChromeNetworkDelegate
> network_delegate(
257 new ChromeNetworkDelegate(forwarder(), &enable_referrers_
));
258 network_delegate
->set_cookie_settings(cookie_settings_
);
259 return network_delegate
.Pass();
262 void SetDelegate(net::NetworkDelegate
* delegate
) {
263 network_delegate_
= delegate
;
264 context_
.set_network_delegate(network_delegate_
);
268 extensions::EventRouterForwarder
* forwarder() {
269 #if defined(ENABLE_EXTENSIONS)
270 return forwarder_
.get();
276 content::TestBrowserThreadBundle thread_bundle_
;
277 #if defined(ENABLE_EXTENSIONS)
278 scoped_refptr
<extensions::EventRouterForwarder
> forwarder_
;
280 TestingProfile profile_
;
281 CookieSettings
* cookie_settings_
;
282 BooleanPrefMember enable_referrers_
;
283 scoped_ptr
<net::URLRequest
> request_
;
284 net::TestURLRequestContext context_
;
285 net::NetworkDelegate
* network_delegate_
;
287 const GURL kBlockedSite
;
288 const GURL kAllowedSite
;
289 const GURL kEmptyFirstPartySite
;
290 const GURL kFirstPartySite
;
291 const GURL kBlockedFirstPartySite
;
294 TEST_F(ChromeNetworkDelegatePrivacyModeTest
, DisablePrivacyIfCookiesAllowed
) {
295 scoped_ptr
<ChromeNetworkDelegate
> delegate(CreateNetworkDelegate());
296 SetDelegate(delegate
.get());
298 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
299 kEmptyFirstPartySite
));
303 TEST_F(ChromeNetworkDelegatePrivacyModeTest
, EnablePrivacyIfCookiesBlocked
) {
304 scoped_ptr
<ChromeNetworkDelegate
> delegate(CreateNetworkDelegate());
305 SetDelegate(delegate
.get());
307 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kBlockedSite
,
308 kEmptyFirstPartySite
));
310 cookie_settings_
->SetCookieSetting(
311 ContentSettingsPattern::FromURL(kBlockedSite
),
312 ContentSettingsPattern::Wildcard(),
313 CONTENT_SETTING_BLOCK
);
314 EXPECT_TRUE(network_delegate_
->CanEnablePrivacyMode(kBlockedSite
,
315 kEmptyFirstPartySite
));
318 TEST_F(ChromeNetworkDelegatePrivacyModeTest
, EnablePrivacyIfThirdPartyBlocked
) {
319 scoped_ptr
<ChromeNetworkDelegate
> delegate(CreateNetworkDelegate());
320 SetDelegate(delegate
.get());
322 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
325 profile_
.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
, true);
326 EXPECT_TRUE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
328 profile_
.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
, false);
329 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
333 TEST_F(ChromeNetworkDelegatePrivacyModeTest
,
334 DisablePrivacyIfOnlyFirstPartyBlocked
) {
335 scoped_ptr
<ChromeNetworkDelegate
> delegate(CreateNetworkDelegate());
336 SetDelegate(delegate
.get());
338 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
339 kBlockedFirstPartySite
));
341 cookie_settings_
->SetCookieSetting(
342 ContentSettingsPattern::FromURL(kBlockedFirstPartySite
),
343 ContentSettingsPattern::Wildcard(),
344 CONTENT_SETTING_BLOCK
);
345 // Privacy mode is disabled as kAllowedSite is still getting cookies
346 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
347 kBlockedFirstPartySite
));