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_factory.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 "components/content_settings/core/browser/cookie_settings.h"
18 #include "components/content_settings/core/common/pref_names.h"
19 #include "content/public/common/content_switches.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "net/base/request_priority.h"
22 #include "net/url_request/url_request.h"
23 #include "net/url_request/url_request_test_util.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 #if defined(ENABLE_EXTENSIONS)
27 #include "chrome/browser/extensions/event_router_forwarder.h"
30 TEST(ChromeNetworkDelegateTest
, DisableFirstPartyOnlyCookiesIffFlagDisabled
) {
31 BooleanPrefMember pref_member_
;
32 scoped_ptr
<ChromeNetworkDelegate
> delegate
;
34 #if defined(ENABLE_EXTENSIONS)
35 scoped_refptr
<extensions::EventRouterForwarder
> forwarder
=
36 new extensions::EventRouterForwarder();
37 delegate
.reset(new ChromeNetworkDelegate(forwarder
.get(), &pref_member_
));
39 delegate
.reset(new ChromeNetworkDelegate(nullptr, &pref_member_
));
41 EXPECT_FALSE(delegate
->FirstPartyOnlyCookieExperimentEnabled());
44 TEST(ChromeNetworkDelegateTest
, EnableFirstPartyOnlyCookiesIffFlagEnabled
) {
45 base::CommandLine::ForCurrentProcess()->AppendSwitch(
46 switches::kEnableExperimentalWebPlatformFeatures
);
47 BooleanPrefMember pref_member_
;
48 scoped_ptr
<ChromeNetworkDelegate
> delegate
;
50 #if defined(ENABLE_EXTENSIONS)
51 scoped_refptr
<extensions::EventRouterForwarder
> forwarder
=
52 new extensions::EventRouterForwarder();
53 delegate
.reset(new ChromeNetworkDelegate(forwarder
.get(), &pref_member_
));
55 delegate
.reset(new ChromeNetworkDelegate(nullptr, &pref_member_
));
57 EXPECT_TRUE(delegate
->FirstPartyOnlyCookieExperimentEnabled());
60 class ChromeNetworkDelegateSafeSearchTest
: public testing::Test
{
62 ChromeNetworkDelegateSafeSearchTest()
63 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
) {
64 #if defined(ENABLE_EXTENSIONS)
65 forwarder_
= new extensions::EventRouterForwarder();
69 void SetUp() override
{
70 ChromeNetworkDelegate::InitializePrefsOnUIThread(
73 &force_google_safe_search_
,
74 &force_youtube_safety_mode_
,
75 profile_
.GetTestingPrefService());
79 scoped_ptr
<net::NetworkDelegate
> CreateNetworkDelegate() {
80 scoped_ptr
<ChromeNetworkDelegate
> network_delegate(
81 new ChromeNetworkDelegate(forwarder(), &enable_referrers_
));
82 network_delegate
->set_force_google_safe_search(&force_google_safe_search_
);
83 network_delegate
->set_force_youtube_safety_mode(
84 &force_youtube_safety_mode_
);
85 return network_delegate
.Pass();
88 void SetSafeSearch(bool google_safe_search
,
89 bool youtube_safety_mode
) {
90 force_google_safe_search_
.SetValue(google_safe_search
);
91 force_youtube_safety_mode_
.SetValue(youtube_safety_mode
);
94 void SetDelegate(net::NetworkDelegate
* delegate
) {
95 network_delegate_
= delegate
;
96 context_
.set_network_delegate(network_delegate_
);
99 // Does a request to an arbitrary URL and verifies that the SafeSearch
100 // enforcement utility functions were called/not called as expected.
101 void QueryURL(bool expect_google_safe_search
,
102 bool expect_youtube_safety_mode
) {
103 safe_search_util::ClearForceGoogleSafeSearchCountForTesting();
104 safe_search_util::ClearForceYouTubeSafetyModeCountForTesting();
106 scoped_ptr
<net::URLRequest
> request(context_
.CreateRequest(
107 GURL("http://anyurl.com"), net::DEFAULT_PRIORITY
, &delegate_
));
110 base::MessageLoop::current()->RunUntilIdle();
112 EXPECT_EQ(expect_google_safe_search
? 1 : 0,
113 safe_search_util::GetForceGoogleSafeSearchCountForTesting());
114 EXPECT_EQ(expect_youtube_safety_mode
? 1 : 0,
115 safe_search_util::GetForceYouTubeSafetyModeCountForTesting());
119 extensions::EventRouterForwarder
* forwarder() {
120 #if defined(ENABLE_EXTENSIONS)
121 return forwarder_
.get();
127 content::TestBrowserThreadBundle thread_bundle_
;
128 #if defined(ENABLE_EXTENSIONS)
129 scoped_refptr
<extensions::EventRouterForwarder
> forwarder_
;
131 TestingProfile profile_
;
132 BooleanPrefMember enable_referrers_
;
133 BooleanPrefMember force_google_safe_search_
;
134 BooleanPrefMember force_youtube_safety_mode_
;
135 scoped_ptr
<net::URLRequest
> request_
;
136 net::TestURLRequestContext context_
;
137 net::NetworkDelegate
* network_delegate_
;
138 net::TestDelegate delegate_
;
141 TEST_F(ChromeNetworkDelegateSafeSearchTest
, SafeSearch
) {
142 scoped_ptr
<net::NetworkDelegate
> delegate(CreateNetworkDelegate());
143 SetDelegate(delegate
.get());
145 // Loop over all combinations of the two policies.
146 for (int i
= 0; i
< 4; i
++) {
147 bool google_safe_search
= i
% 2;
148 bool youtube_safety_mode
= i
/ 2;
149 SetSafeSearch(google_safe_search
, youtube_safety_mode
);
151 QueryURL(google_safe_search
, youtube_safety_mode
);
155 // Privacy Mode disables Channel Id if cookies are blocked (cr223191)
156 class ChromeNetworkDelegatePrivacyModeTest
: public testing::Test
{
158 ChromeNetworkDelegatePrivacyModeTest()
159 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
160 #if defined(ENABLE_EXTENSIONS)
161 forwarder_(new extensions::EventRouterForwarder()),
163 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_
).get()),
164 kBlockedSite("http://ads.thirdparty.com"),
165 kAllowedSite("http://good.allays.com"),
166 kFirstPartySite("http://cool.things.com"),
167 kBlockedFirstPartySite("http://no.thirdparties.com") {
170 void SetUp() override
{
171 ChromeNetworkDelegate::InitializePrefsOnUIThread(
172 &enable_referrers_
, NULL
, NULL
, NULL
,
173 profile_
.GetTestingPrefService());
177 scoped_ptr
<ChromeNetworkDelegate
> CreateNetworkDelegate() {
178 scoped_ptr
<ChromeNetworkDelegate
> network_delegate(
179 new ChromeNetworkDelegate(forwarder(), &enable_referrers_
));
180 network_delegate
->set_cookie_settings(cookie_settings_
);
181 return network_delegate
.Pass();
184 void SetDelegate(net::NetworkDelegate
* delegate
) {
185 network_delegate_
= delegate
;
186 context_
.set_network_delegate(network_delegate_
);
190 extensions::EventRouterForwarder
* forwarder() {
191 #if defined(ENABLE_EXTENSIONS)
192 return forwarder_
.get();
198 content::TestBrowserThreadBundle thread_bundle_
;
199 #if defined(ENABLE_EXTENSIONS)
200 scoped_refptr
<extensions::EventRouterForwarder
> forwarder_
;
202 TestingProfile profile_
;
203 content_settings::CookieSettings
* cookie_settings_
;
204 BooleanPrefMember enable_referrers_
;
205 scoped_ptr
<net::URLRequest
> request_
;
206 net::TestURLRequestContext context_
;
207 net::NetworkDelegate
* network_delegate_
;
209 const GURL kBlockedSite
;
210 const GURL kAllowedSite
;
211 const GURL kEmptyFirstPartySite
;
212 const GURL kFirstPartySite
;
213 const GURL kBlockedFirstPartySite
;
216 TEST_F(ChromeNetworkDelegatePrivacyModeTest
, DisablePrivacyIfCookiesAllowed
) {
217 scoped_ptr
<ChromeNetworkDelegate
> delegate(CreateNetworkDelegate());
218 SetDelegate(delegate
.get());
220 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
221 kEmptyFirstPartySite
));
225 TEST_F(ChromeNetworkDelegatePrivacyModeTest
, EnablePrivacyIfCookiesBlocked
) {
226 scoped_ptr
<ChromeNetworkDelegate
> delegate(CreateNetworkDelegate());
227 SetDelegate(delegate
.get());
229 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kBlockedSite
,
230 kEmptyFirstPartySite
));
232 cookie_settings_
->SetCookieSetting(
233 ContentSettingsPattern::FromURL(kBlockedSite
),
234 ContentSettingsPattern::Wildcard(),
235 CONTENT_SETTING_BLOCK
);
236 EXPECT_TRUE(network_delegate_
->CanEnablePrivacyMode(kBlockedSite
,
237 kEmptyFirstPartySite
));
240 TEST_F(ChromeNetworkDelegatePrivacyModeTest
, EnablePrivacyIfThirdPartyBlocked
) {
241 scoped_ptr
<ChromeNetworkDelegate
> delegate(CreateNetworkDelegate());
242 SetDelegate(delegate
.get());
244 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
247 profile_
.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
, true);
248 EXPECT_TRUE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
250 profile_
.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
, false);
251 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
255 TEST_F(ChromeNetworkDelegatePrivacyModeTest
,
256 DisablePrivacyIfOnlyFirstPartyBlocked
) {
257 scoped_ptr
<ChromeNetworkDelegate
> delegate(CreateNetworkDelegate());
258 SetDelegate(delegate
.get());
260 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
261 kBlockedFirstPartySite
));
263 cookie_settings_
->SetCookieSetting(
264 ContentSettingsPattern::FromURL(kBlockedFirstPartySite
),
265 ContentSettingsPattern::Wildcard(),
266 CONTENT_SETTING_BLOCK
);
267 // Privacy mode is disabled as kAllowedSite is still getting cookies
268 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
269 kBlockedFirstPartySite
));