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 class ChromeNetworkDelegateSafeSearchTest
: public testing::Test
{
60 ChromeNetworkDelegateSafeSearchTest()
61 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
) {
62 #if defined(ENABLE_EXTENSIONS)
63 forwarder_
= new extensions::EventRouterForwarder();
67 void SetUp() override
{
68 ChromeNetworkDelegate::InitializePrefsOnUIThread(
71 &force_google_safe_search_
,
72 &force_youtube_safety_mode_
,
73 profile_
.GetTestingPrefService());
77 scoped_ptr
<net::NetworkDelegate
> CreateNetworkDelegate() {
78 scoped_ptr
<ChromeNetworkDelegate
> network_delegate(
79 new ChromeNetworkDelegate(forwarder(), &enable_referrers_
));
80 network_delegate
->set_force_google_safe_search(&force_google_safe_search_
);
81 network_delegate
->set_force_youtube_safety_mode(
82 &force_youtube_safety_mode_
);
83 return network_delegate
.Pass();
86 void SetSafeSearch(bool google_safe_search
,
87 bool youtube_safety_mode
) {
88 force_google_safe_search_
.SetValue(google_safe_search
);
89 force_youtube_safety_mode_
.SetValue(youtube_safety_mode
);
92 void SetDelegate(net::NetworkDelegate
* delegate
) {
93 network_delegate_
= delegate
;
94 context_
.set_network_delegate(network_delegate_
);
97 // Does a request to an arbitrary URL and verifies that the SafeSearch
98 // enforcement utility functions were called/not called as expected.
99 void QueryURL(bool expect_google_safe_search
,
100 bool expect_youtube_safety_mode
) {
101 safe_search_util::ClearForceGoogleSafeSearchCountForTesting();
102 safe_search_util::ClearForceYouTubeSafetyModeCountForTesting();
104 scoped_ptr
<net::URLRequest
> request(context_
.CreateRequest(
105 GURL("http://anyurl.com"), net::DEFAULT_PRIORITY
, &delegate_
));
108 base::MessageLoop::current()->RunUntilIdle();
110 EXPECT_EQ(expect_google_safe_search
? 1 : 0,
111 safe_search_util::GetForceGoogleSafeSearchCountForTesting());
112 EXPECT_EQ(expect_youtube_safety_mode
? 1 : 0,
113 safe_search_util::GetForceYouTubeSafetyModeCountForTesting());
117 extensions::EventRouterForwarder
* forwarder() {
118 #if defined(ENABLE_EXTENSIONS)
119 return forwarder_
.get();
125 content::TestBrowserThreadBundle thread_bundle_
;
126 #if defined(ENABLE_EXTENSIONS)
127 scoped_refptr
<extensions::EventRouterForwarder
> forwarder_
;
129 TestingProfile profile_
;
130 BooleanPrefMember enable_referrers_
;
131 BooleanPrefMember force_google_safe_search_
;
132 BooleanPrefMember force_youtube_safety_mode_
;
133 scoped_ptr
<net::URLRequest
> request_
;
134 net::TestURLRequestContext context_
;
135 net::NetworkDelegate
* network_delegate_
;
136 net::TestDelegate delegate_
;
139 TEST_F(ChromeNetworkDelegateSafeSearchTest
, SafeSearch
) {
140 scoped_ptr
<net::NetworkDelegate
> delegate(CreateNetworkDelegate());
141 SetDelegate(delegate
.get());
143 // Loop over all combinations of the two policies.
144 for (int i
= 0; i
< 4; i
++) {
145 bool google_safe_search
= i
% 2;
146 bool youtube_safety_mode
= i
/ 2;
147 SetSafeSearch(google_safe_search
, youtube_safety_mode
);
149 QueryURL(google_safe_search
, youtube_safety_mode
);
153 // Privacy Mode disables Channel Id if cookies are blocked (cr223191)
154 class ChromeNetworkDelegatePrivacyModeTest
: public testing::Test
{
156 ChromeNetworkDelegatePrivacyModeTest()
157 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
158 #if defined(ENABLE_EXTENSIONS)
159 forwarder_(new extensions::EventRouterForwarder()),
161 cookie_settings_(CookieSettings::Factory::GetForProfile(&profile_
)
163 kBlockedSite("http://ads.thirdparty.com"),
164 kAllowedSite("http://good.allays.com"),
165 kFirstPartySite("http://cool.things.com"),
166 kBlockedFirstPartySite("http://no.thirdparties.com") {}
168 void SetUp() override
{
169 ChromeNetworkDelegate::InitializePrefsOnUIThread(
170 &enable_referrers_
, NULL
, NULL
, NULL
,
171 profile_
.GetTestingPrefService());
175 scoped_ptr
<ChromeNetworkDelegate
> CreateNetworkDelegate() {
176 scoped_ptr
<ChromeNetworkDelegate
> network_delegate(
177 new ChromeNetworkDelegate(forwarder(), &enable_referrers_
));
178 network_delegate
->set_cookie_settings(cookie_settings_
);
179 return network_delegate
.Pass();
182 void SetDelegate(net::NetworkDelegate
* delegate
) {
183 network_delegate_
= delegate
;
184 context_
.set_network_delegate(network_delegate_
);
188 extensions::EventRouterForwarder
* forwarder() {
189 #if defined(ENABLE_EXTENSIONS)
190 return forwarder_
.get();
196 content::TestBrowserThreadBundle thread_bundle_
;
197 #if defined(ENABLE_EXTENSIONS)
198 scoped_refptr
<extensions::EventRouterForwarder
> forwarder_
;
200 TestingProfile profile_
;
201 CookieSettings
* cookie_settings_
;
202 BooleanPrefMember enable_referrers_
;
203 scoped_ptr
<net::URLRequest
> request_
;
204 net::TestURLRequestContext context_
;
205 net::NetworkDelegate
* network_delegate_
;
207 const GURL kBlockedSite
;
208 const GURL kAllowedSite
;
209 const GURL kEmptyFirstPartySite
;
210 const GURL kFirstPartySite
;
211 const GURL kBlockedFirstPartySite
;
214 TEST_F(ChromeNetworkDelegatePrivacyModeTest
, DisablePrivacyIfCookiesAllowed
) {
215 scoped_ptr
<ChromeNetworkDelegate
> delegate(CreateNetworkDelegate());
216 SetDelegate(delegate
.get());
218 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
219 kEmptyFirstPartySite
));
223 TEST_F(ChromeNetworkDelegatePrivacyModeTest
, EnablePrivacyIfCookiesBlocked
) {
224 scoped_ptr
<ChromeNetworkDelegate
> delegate(CreateNetworkDelegate());
225 SetDelegate(delegate
.get());
227 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kBlockedSite
,
228 kEmptyFirstPartySite
));
230 cookie_settings_
->SetCookieSetting(
231 ContentSettingsPattern::FromURL(kBlockedSite
),
232 ContentSettingsPattern::Wildcard(),
233 CONTENT_SETTING_BLOCK
);
234 EXPECT_TRUE(network_delegate_
->CanEnablePrivacyMode(kBlockedSite
,
235 kEmptyFirstPartySite
));
238 TEST_F(ChromeNetworkDelegatePrivacyModeTest
, EnablePrivacyIfThirdPartyBlocked
) {
239 scoped_ptr
<ChromeNetworkDelegate
> delegate(CreateNetworkDelegate());
240 SetDelegate(delegate
.get());
242 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
245 profile_
.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
, true);
246 EXPECT_TRUE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
248 profile_
.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
, false);
249 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
253 TEST_F(ChromeNetworkDelegatePrivacyModeTest
,
254 DisablePrivacyIfOnlyFirstPartyBlocked
) {
255 scoped_ptr
<ChromeNetworkDelegate
> delegate(CreateNetworkDelegate());
256 SetDelegate(delegate
.get());
258 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
259 kBlockedFirstPartySite
));
261 cookie_settings_
->SetCookieSetting(
262 ContentSettingsPattern::FromURL(kBlockedFirstPartySite
),
263 ContentSettingsPattern::Wildcard(),
264 CONTENT_SETTING_BLOCK
);
265 // Privacy mode is disabled as kAllowedSite is still getting cookies
266 EXPECT_FALSE(network_delegate_
->CanEnablePrivacyMode(kAllowedSite
,
267 kBlockedFirstPartySite
));