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