Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / net / chrome_network_delegate_unittest.cc
blob07dac2b19e965c50faf880b3e67b9b782f461b8d
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 "base/run_loop.h"
13 #include "base/test/histogram_tester.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/content_settings/cookie_settings_factory.h"
16 #include "chrome/browser/net/safe_search_util.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/test/base/testing_browser_process.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "chrome/test/base/testing_profile_manager.h"
21 #include "components/content_settings/core/browser/cookie_settings.h"
22 #include "components/content_settings/core/common/pref_names.h"
23 #include "components/syncable_prefs/testing_pref_service_syncable.h"
24 #include "content/public/browser/resource_request_info.h"
25 #include "content/public/common/content_switches.h"
26 #include "content/public/common/resource_type.h"
27 #include "content/public/test/test_browser_thread_bundle.h"
28 #include "net/base/request_priority.h"
29 #include "net/socket/socket_test_util.h"
30 #include "net/url_request/url_request.h"
31 #include "net/url_request/url_request_test_util.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33 #include "url/gurl.h"
35 #if defined(ENABLE_EXTENSIONS)
36 #include "chrome/browser/extensions/event_router_forwarder.h"
37 #endif
39 #if !defined(OS_IOS)
40 #include "components/data_use_measurement/core/data_use_user_data.h"
41 #endif
43 namespace {
45 // This function requests a URL, and makes it return a known response. If
46 // |from_user| is true, it attaches a ResourceRequestInfo to the URLRequest,
47 // because requests from users have this info. If |from_user| is false, the
48 // request is presumed to be from a service, and the service name is set in the
49 // request's user data. (As an example suggestions service tag is attached). if
50 // |redirect| is true, it adds necessary socket data to have it follow redirect
51 // before getting the final response.
52 void RequestURL(net::URLRequestContext* context,
53 net::MockClientSocketFactory* socket_factory,
54 bool from_user,
55 bool redirect) {
56 net::MockRead redirect_mock_reads[] = {
57 net::MockRead("HTTP/1.1 302 Found\r\n"
58 "Location: http://bar.com/\r\n\r\n"),
59 net::MockRead(net::SYNCHRONOUS, net::OK),
61 net::StaticSocketDataProvider redirect_socket_data_provider(
62 redirect_mock_reads, arraysize(redirect_mock_reads), nullptr, 0);
64 if (redirect)
65 socket_factory->AddSocketDataProvider(&redirect_socket_data_provider);
66 net::MockRead response_mock_reads[] = {
67 net::MockRead("HTTP/1.1 200 OK\r\n\r\n"), net::MockRead("response body"),
68 net::MockRead(net::SYNCHRONOUS, net::OK),
70 net::StaticSocketDataProvider response_socket_data_provider(
71 response_mock_reads, arraysize(response_mock_reads), nullptr, 0);
72 socket_factory->AddSocketDataProvider(&response_socket_data_provider);
73 net::TestDelegate test_delegate;
74 test_delegate.set_quit_on_complete(true);
75 scoped_ptr<net::URLRequest> request(context->CreateRequest(
76 GURL("http://example.com"), net::DEFAULT_PRIORITY, &test_delegate));
78 if (from_user) {
79 content::ResourceRequestInfo::AllocateForTesting(
80 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2,
81 true, false, true, true);
82 } else {
83 request->SetUserData(
84 data_use_measurement::DataUseUserData::kUserDataKey,
85 new data_use_measurement::DataUseUserData(
86 data_use_measurement::DataUseUserData::SUGGESTIONS));
88 request->Start();
89 base::RunLoop().RunUntilIdle();
92 } // namespace
94 class ChromeNetworkDelegateTest : public testing::Test {
95 public:
96 ChromeNetworkDelegateTest()
97 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
98 context_(new net::TestURLRequestContext(true)) {
99 #if defined(ENABLE_EXTENSIONS)
100 forwarder_ = new extensions::EventRouterForwarder();
101 #endif
104 void SetUp() override {
105 ChromeNetworkDelegate::InitializePrefsOnUIThread(
106 &enable_referrers_, nullptr, nullptr, nullptr,
107 profile_.GetTestingPrefService());
108 profile_manager_.reset(
109 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
110 ASSERT_TRUE(profile_manager_->SetUp());
113 void Initialize() {
114 network_delegate_.reset(
115 new ChromeNetworkDelegate(forwarder(), &enable_referrers_));
116 context_->set_client_socket_factory(&socket_factory_);
117 context_->set_network_delegate(network_delegate_.get());
118 context_->Init();
121 net::TestURLRequestContext* context() { return context_.get(); }
122 net::NetworkDelegate* network_delegate() { return network_delegate_.get(); }
123 net::MockClientSocketFactory* socket_factory() { return &socket_factory_; }
125 extensions::EventRouterForwarder* forwarder() {
126 #if defined(ENABLE_EXTENSIONS)
127 return forwarder_.get();
128 #else
129 return nullptr;
130 #endif
133 private:
134 scoped_ptr<TestingProfileManager> profile_manager_;
135 content::TestBrowserThreadBundle thread_bundle_;
136 #if defined(ENABLE_EXTENSIONS)
137 scoped_refptr<extensions::EventRouterForwarder> forwarder_;
138 #endif
139 TestingProfile profile_;
140 BooleanPrefMember enable_referrers_;
141 scoped_ptr<net::NetworkDelegate> network_delegate_;
142 net::MockClientSocketFactory socket_factory_;
143 scoped_ptr<net::TestURLRequestContext> context_;
146 // This function tests data use measurement for requests by services. it makes a
147 // query which is similar to a query of a service, so it should affect
148 // DataUse.TrafficSize.System.Dimensions and DataUse.MessageSize.ServiceName
149 // histograms. AppState and ConnectionType dimensions are always Foreground and
150 // NotCellular respectively.
151 #if !defined(OS_IOS)
152 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementServiceTest) {
153 Initialize();
154 base::HistogramTester histogram_tester;
156 // A query from a service without redirection.
157 RequestURL(context(), socket_factory(), false, false);
158 histogram_tester.ExpectTotalCount(
159 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 1);
160 histogram_tester.ExpectTotalCount(
161 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 1);
162 // One upload and one download message, so totalCount should be 2.
163 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2);
164 histogram_tester.ExpectTotalCount(
165 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 0);
166 histogram_tester.ExpectTotalCount(
167 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 0);
170 // This function tests data use measurement for requests by user.The query from
171 // a user should affect DataUse.TrafficSize.User.Dimensions histogram. AppState
172 // and ConnectionType dimensions are always Foreground and NotCellular
173 // respectively.
174 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementUserTest) {
175 Initialize();
176 base::HistogramTester histogram_tester;
178 // A query from user without redirection.
179 RequestURL(context(), socket_factory(), true, false);
180 histogram_tester.ExpectTotalCount(
181 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 1);
182 histogram_tester.ExpectTotalCount(
183 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 1);
184 histogram_tester.ExpectTotalCount(
185 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 0);
186 histogram_tester.ExpectTotalCount(
187 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 0);
188 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 0);
191 // This function tests data use measurement for requests by services in case the
192 // request is redirected once. it makes a query which is similar to a query of a
193 // service, so it should affect DataUse.TrafficSize.System.Dimensions and
194 // DataUse.MessageSize.ServiceName histograms. AppState and ConnectionType
195 // dimensions are always Foreground and NotCellular respectively.
196 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementServiceTestWithRedirect) {
197 Initialize();
198 base::HistogramTester histogram_tester;
200 // A query from user with one redirection.
201 RequestURL(context(), socket_factory(), false, true);
202 histogram_tester.ExpectTotalCount(
203 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 2);
204 histogram_tester.ExpectTotalCount(
205 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 2);
206 // Two uploads and two downloads message, so totalCount should be 4.
207 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 4);
208 histogram_tester.ExpectTotalCount(
209 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 0);
210 histogram_tester.ExpectTotalCount(
211 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 0);
214 // This function tests data use measurement for requests by user in case the
215 // request is redirected once.The query from a user should affect
216 // DataUse.TrafficSize.User.Dimensions histogram. AppState and ConnectionType
217 // dimensions are always Foreground and NotCellular respectively.
218 TEST_F(ChromeNetworkDelegateTest, DataUseMeasurementUserTestWithRedirect) {
219 Initialize();
220 base::HistogramTester histogram_tester;
222 // A query from user with one redirection.
223 RequestURL(context(), socket_factory(), true, true);
225 histogram_tester.ExpectTotalCount(
226 "DataUse.TrafficSize.User.Downstream.Foreground.NotCellular", 2);
227 histogram_tester.ExpectTotalCount(
228 "DataUse.TrafficSize.User.Upstream.Foreground.NotCellular", 2);
229 histogram_tester.ExpectTotalCount(
230 "DataUse.TrafficSize.System.Downstream.Foreground.NotCellular", 0);
231 histogram_tester.ExpectTotalCount(
232 "DataUse.TrafficSize.System.Upstream.Foreground.NotCellular", 0);
233 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 0);
236 #endif
238 TEST_F(ChromeNetworkDelegateTest, DisableFirstPartyOnlyCookiesIffFlagDisabled) {
239 Initialize();
240 EXPECT_FALSE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled());
243 TEST_F(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) {
244 base::CommandLine::ForCurrentProcess()->AppendSwitch(
245 switches::kEnableExperimentalWebPlatformFeatures);
246 Initialize();
247 EXPECT_TRUE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled());
250 class ChromeNetworkDelegateSafeSearchTest : public testing::Test {
251 public:
252 ChromeNetworkDelegateSafeSearchTest()
253 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
254 #if defined(ENABLE_EXTENSIONS)
255 forwarder_ = new extensions::EventRouterForwarder();
256 #endif
259 void SetUp() override {
260 ChromeNetworkDelegate::InitializePrefsOnUIThread(
261 &enable_referrers_,
262 NULL,
263 &force_google_safe_search_,
264 &force_youtube_safety_mode_,
265 profile_.GetTestingPrefService());
268 protected:
269 scoped_ptr<net::NetworkDelegate> CreateNetworkDelegate() {
270 scoped_ptr<ChromeNetworkDelegate> network_delegate(
271 new ChromeNetworkDelegate(forwarder(), &enable_referrers_));
272 network_delegate->set_force_google_safe_search(&force_google_safe_search_);
273 network_delegate->set_force_youtube_safety_mode(
274 &force_youtube_safety_mode_);
275 return network_delegate.Pass();
278 void SetSafeSearch(bool google_safe_search,
279 bool youtube_safety_mode) {
280 force_google_safe_search_.SetValue(google_safe_search);
281 force_youtube_safety_mode_.SetValue(youtube_safety_mode);
284 void SetDelegate(net::NetworkDelegate* delegate) {
285 network_delegate_ = delegate;
286 context_.set_network_delegate(network_delegate_);
289 // Does a request to an arbitrary URL and verifies that the SafeSearch
290 // enforcement utility functions were called/not called as expected.
291 void QueryURL(bool expect_google_safe_search,
292 bool expect_youtube_safety_mode) {
293 safe_search_util::ClearForceGoogleSafeSearchCountForTesting();
294 safe_search_util::ClearForceYouTubeSafetyModeCountForTesting();
296 scoped_ptr<net::URLRequest> request(context_.CreateRequest(
297 GURL("http://anyurl.com"), net::DEFAULT_PRIORITY, &delegate_));
299 request->Start();
300 base::MessageLoop::current()->RunUntilIdle();
302 EXPECT_EQ(expect_google_safe_search ? 1 : 0,
303 safe_search_util::GetForceGoogleSafeSearchCountForTesting());
304 EXPECT_EQ(expect_youtube_safety_mode ? 1 : 0,
305 safe_search_util::GetForceYouTubeSafetyModeCountForTesting());
308 private:
309 extensions::EventRouterForwarder* forwarder() {
310 #if defined(ENABLE_EXTENSIONS)
311 return forwarder_.get();
312 #else
313 return NULL;
314 #endif
317 content::TestBrowserThreadBundle thread_bundle_;
318 #if defined(ENABLE_EXTENSIONS)
319 scoped_refptr<extensions::EventRouterForwarder> forwarder_;
320 #endif
321 TestingProfile profile_;
322 BooleanPrefMember enable_referrers_;
323 BooleanPrefMember force_google_safe_search_;
324 BooleanPrefMember force_youtube_safety_mode_;
325 scoped_ptr<net::URLRequest> request_;
326 net::TestURLRequestContext context_;
327 net::NetworkDelegate* network_delegate_;
328 net::TestDelegate delegate_;
331 TEST_F(ChromeNetworkDelegateSafeSearchTest, SafeSearch) {
332 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
333 SetDelegate(delegate.get());
335 // Loop over all combinations of the two policies.
336 for (int i = 0; i < 4; i++) {
337 bool google_safe_search = i % 2;
338 bool youtube_safety_mode = i / 2;
339 SetSafeSearch(google_safe_search, youtube_safety_mode);
341 QueryURL(google_safe_search, youtube_safety_mode);
345 // Privacy Mode disables Channel Id if cookies are blocked (cr223191)
346 class ChromeNetworkDelegatePrivacyModeTest : public testing::Test {
347 public:
348 ChromeNetworkDelegatePrivacyModeTest()
349 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
350 #if defined(ENABLE_EXTENSIONS)
351 forwarder_(new extensions::EventRouterForwarder()),
352 #endif
353 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_).get()),
354 kBlockedSite("http://ads.thirdparty.com"),
355 kAllowedSite("http://good.allays.com"),
356 kFirstPartySite("http://cool.things.com"),
357 kBlockedFirstPartySite("http://no.thirdparties.com") {
360 void SetUp() override {
361 ChromeNetworkDelegate::InitializePrefsOnUIThread(
362 &enable_referrers_, NULL, NULL, NULL,
363 profile_.GetTestingPrefService());
366 protected:
367 scoped_ptr<ChromeNetworkDelegate> CreateNetworkDelegate() {
368 scoped_ptr<ChromeNetworkDelegate> network_delegate(
369 new ChromeNetworkDelegate(forwarder(), &enable_referrers_));
370 network_delegate->set_cookie_settings(cookie_settings_);
371 return network_delegate.Pass();
374 void SetDelegate(net::NetworkDelegate* delegate) {
375 network_delegate_ = delegate;
376 context_.set_network_delegate(network_delegate_);
379 protected:
380 extensions::EventRouterForwarder* forwarder() {
381 #if defined(ENABLE_EXTENSIONS)
382 return forwarder_.get();
383 #else
384 return NULL;
385 #endif
388 content::TestBrowserThreadBundle thread_bundle_;
389 #if defined(ENABLE_EXTENSIONS)
390 scoped_refptr<extensions::EventRouterForwarder> forwarder_;
391 #endif
392 TestingProfile profile_;
393 content_settings::CookieSettings* cookie_settings_;
394 BooleanPrefMember enable_referrers_;
395 scoped_ptr<net::URLRequest> request_;
396 net::TestURLRequestContext context_;
397 net::NetworkDelegate* network_delegate_;
399 const GURL kBlockedSite;
400 const GURL kAllowedSite;
401 const GURL kEmptyFirstPartySite;
402 const GURL kFirstPartySite;
403 const GURL kBlockedFirstPartySite;
406 TEST_F(ChromeNetworkDelegatePrivacyModeTest, DisablePrivacyIfCookiesAllowed) {
407 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate());
408 SetDelegate(delegate.get());
410 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
411 kEmptyFirstPartySite));
415 TEST_F(ChromeNetworkDelegatePrivacyModeTest, EnablePrivacyIfCookiesBlocked) {
416 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate());
417 SetDelegate(delegate.get());
419 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kBlockedSite,
420 kEmptyFirstPartySite));
422 cookie_settings_->SetCookieSetting(
423 ContentSettingsPattern::FromURL(kBlockedSite),
424 ContentSettingsPattern::Wildcard(),
425 CONTENT_SETTING_BLOCK);
426 EXPECT_TRUE(network_delegate_->CanEnablePrivacyMode(kBlockedSite,
427 kEmptyFirstPartySite));
430 TEST_F(ChromeNetworkDelegatePrivacyModeTest, EnablePrivacyIfThirdPartyBlocked) {
431 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate());
432 SetDelegate(delegate.get());
434 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
435 kFirstPartySite));
437 profile_.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true);
438 EXPECT_TRUE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
439 kFirstPartySite));
440 profile_.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, false);
441 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
442 kFirstPartySite));
445 TEST_F(ChromeNetworkDelegatePrivacyModeTest,
446 DisablePrivacyIfOnlyFirstPartyBlocked) {
447 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate());
448 SetDelegate(delegate.get());
450 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
451 kBlockedFirstPartySite));
453 cookie_settings_->SetCookieSetting(
454 ContentSettingsPattern::FromURL(kBlockedFirstPartySite),
455 ContentSettingsPattern::Wildcard(),
456 CONTENT_SETTING_BLOCK);
457 // Privacy mode is disabled as kAllowedSite is still getting cookies
458 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite,
459 kBlockedFirstPartySite));