OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/net/chrome_network_delegate.h" | 5 #include "chrome/browser/net/chrome_network_delegate.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include "base/command_line.h" | 9 #include "base/command_line.h" |
8 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
11 #include "base/prefs/pref_member.h" | 13 #include "base/prefs/pref_member.h" |
12 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
13 #include "base/test/histogram_tester.h" | 15 #include "base/test/histogram_tester.h" |
14 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/content_settings/cookie_settings_factory.h" | 17 #include "chrome/browser/content_settings/cookie_settings_factory.h" |
16 #include "chrome/browser/net/safe_search_util.h" | 18 #include "chrome/browser/net/safe_search_util.h" |
17 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
18 #include "chrome/test/base/testing_browser_process.h" | 20 #include "chrome/test/base/testing_browser_process.h" |
19 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
20 #include "chrome/test/base/testing_profile_manager.h" | 22 #include "chrome/test/base/testing_profile_manager.h" |
21 #include "components/content_settings/core/browser/cookie_settings.h" | 23 #include "components/content_settings/core/browser/cookie_settings.h" |
22 #include "components/content_settings/core/common/pref_names.h" | 24 #include "components/content_settings/core/common/pref_names.h" |
| 25 #include "components/data_usage/core/data_use_aggregator.h" |
23 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 26 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
24 #include "content/public/browser/resource_request_info.h" | 27 #include "content/public/browser/resource_request_info.h" |
25 #include "content/public/common/content_switches.h" | 28 #include "content/public/common/content_switches.h" |
26 #include "content/public/common/resource_type.h" | 29 #include "content/public/common/resource_type.h" |
27 #include "content/public/test/test_browser_thread_bundle.h" | 30 #include "content/public/test/test_browser_thread_bundle.h" |
28 #include "net/base/request_priority.h" | 31 #include "net/base/request_priority.h" |
29 #include "net/socket/socket_test_util.h" | 32 #include "net/socket/socket_test_util.h" |
30 #include "net/url_request/url_request.h" | 33 #include "net/url_request/url_request.h" |
31 #include "net/url_request/url_request_test_util.h" | 34 #include "net/url_request/url_request_test_util.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
33 #include "url/gurl.h" | 36 #include "url/gurl.h" |
34 | 37 |
35 #if defined(ENABLE_EXTENSIONS) | 38 #if defined(ENABLE_EXTENSIONS) |
36 #include "chrome/browser/extensions/event_router_forwarder.h" | 39 #include "chrome/browser/extensions/event_router_forwarder.h" |
37 #endif | 40 #endif |
38 | 41 |
39 #if !defined(OS_IOS) | 42 #if !defined(OS_IOS) |
40 #include "components/data_use_measurement/core/data_use_user_data.h" | 43 #include "components/data_use_measurement/core/data_use_user_data.h" |
41 #endif | 44 #endif |
42 | 45 |
43 namespace { | 46 namespace { |
44 | 47 |
45 // This function requests a URL, and makes it return a known response. If | 48 // 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, | 49 // |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 | 50 // 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 | 51 // 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 | 52 // 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 | 53 // |redirect| is true, it adds necessary socket data to have it follow redirect |
51 // before getting the final response. | 54 // before getting the final response. |
52 void RequestURL(net::URLRequestContext* context, | 55 scoped_ptr<net::URLRequest> RequestURL( |
53 net::MockClientSocketFactory* socket_factory, | 56 net::URLRequestContext* context, |
54 bool from_user, | 57 net::MockClientSocketFactory* socket_factory, |
55 bool redirect) { | 58 bool from_user, |
| 59 bool redirect) { |
56 net::MockRead redirect_mock_reads[] = { | 60 net::MockRead redirect_mock_reads[] = { |
57 net::MockRead("HTTP/1.1 302 Found\r\n" | 61 net::MockRead("HTTP/1.1 302 Found\r\n" |
58 "Location: http://bar.com/\r\n\r\n"), | 62 "Location: http://bar.com/\r\n\r\n"), |
59 net::MockRead(net::SYNCHRONOUS, net::OK), | 63 net::MockRead(net::SYNCHRONOUS, net::OK), |
60 }; | 64 }; |
61 net::StaticSocketDataProvider redirect_socket_data_provider( | 65 net::StaticSocketDataProvider redirect_socket_data_provider( |
62 redirect_mock_reads, arraysize(redirect_mock_reads), nullptr, 0); | 66 redirect_mock_reads, arraysize(redirect_mock_reads), nullptr, 0); |
63 | 67 |
64 if (redirect) | 68 if (redirect) |
65 socket_factory->AddSocketDataProvider(&redirect_socket_data_provider); | 69 socket_factory->AddSocketDataProvider(&redirect_socket_data_provider); |
(...skipping 14 matching lines...) Expand all Loading... |
80 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2, | 84 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, -2, -2, -2, |
81 true, false, true, true); | 85 true, false, true, true); |
82 } else { | 86 } else { |
83 request->SetUserData( | 87 request->SetUserData( |
84 data_use_measurement::DataUseUserData::kUserDataKey, | 88 data_use_measurement::DataUseUserData::kUserDataKey, |
85 new data_use_measurement::DataUseUserData( | 89 new data_use_measurement::DataUseUserData( |
86 data_use_measurement::DataUseUserData::SUGGESTIONS)); | 90 data_use_measurement::DataUseUserData::SUGGESTIONS)); |
87 } | 91 } |
88 request->Start(); | 92 request->Start(); |
89 base::RunLoop().RunUntilIdle(); | 93 base::RunLoop().RunUntilIdle(); |
| 94 return request.Pass(); |
90 } | 95 } |
91 | 96 |
| 97 // A fake DataUseAggregator for testing that only counts how many times its |
| 98 // respective methods have been called. |
| 99 class FakeDataUseAggregator : public data_usage::DataUseAggregator { |
| 100 public: |
| 101 FakeDataUseAggregator() |
| 102 : on_the_record_tx_bytes_(0), |
| 103 on_the_record_rx_bytes_(0), |
| 104 off_the_record_tx_bytes_(0), |
| 105 off_the_record_rx_bytes_(0) {} |
| 106 ~FakeDataUseAggregator() override {} |
| 107 |
| 108 void ReportDataUse(const net::URLRequest& request, |
| 109 int32_t tab_id, |
| 110 int64_t tx_bytes, |
| 111 int64_t rx_bytes) override { |
| 112 on_the_record_tx_bytes_ += tx_bytes; |
| 113 on_the_record_rx_bytes_ += rx_bytes; |
| 114 } |
| 115 |
| 116 void ReportOffTheRecordDataUse(int64_t tx_bytes, int64_t rx_bytes) override { |
| 117 off_the_record_tx_bytes_ += tx_bytes; |
| 118 off_the_record_rx_bytes_ += rx_bytes; |
| 119 } |
| 120 |
| 121 int64_t on_the_record_tx_bytes() const { return on_the_record_tx_bytes_; } |
| 122 int64_t on_the_record_rx_bytes() const { return on_the_record_rx_bytes_; } |
| 123 int64_t off_the_record_tx_bytes() const { return off_the_record_tx_bytes_; } |
| 124 int64_t off_the_record_rx_bytes() const { return off_the_record_rx_bytes_; } |
| 125 |
| 126 private: |
| 127 int64_t on_the_record_tx_bytes_; |
| 128 int64_t on_the_record_rx_bytes_; |
| 129 int64_t off_the_record_tx_bytes_; |
| 130 int64_t off_the_record_rx_bytes_; |
| 131 }; |
| 132 |
92 } // namespace | 133 } // namespace |
93 | 134 |
94 class ChromeNetworkDelegateTest : public testing::Test { | 135 class ChromeNetworkDelegateTest : public testing::Test { |
95 public: | 136 public: |
96 ChromeNetworkDelegateTest() | 137 ChromeNetworkDelegateTest() |
97 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 138 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
98 context_(new net::TestURLRequestContext(true)) { | 139 context_(new net::TestURLRequestContext(true)) { |
99 #if defined(ENABLE_EXTENSIONS) | 140 #if defined(ENABLE_EXTENSIONS) |
100 forwarder_ = new extensions::EventRouterForwarder(); | 141 forwarder_ = new extensions::EventRouterForwarder(); |
101 #endif | 142 #endif |
(...skipping 13 matching lines...) Expand all Loading... |
115 new ChromeNetworkDelegate(forwarder(), &enable_referrers_)); | 156 new ChromeNetworkDelegate(forwarder(), &enable_referrers_)); |
116 context_->set_client_socket_factory(&socket_factory_); | 157 context_->set_client_socket_factory(&socket_factory_); |
117 context_->set_network_delegate(network_delegate_.get()); | 158 context_->set_network_delegate(network_delegate_.get()); |
118 context_->Init(); | 159 context_->Init(); |
119 } | 160 } |
120 | 161 |
121 net::TestURLRequestContext* context() { return context_.get(); } | 162 net::TestURLRequestContext* context() { return context_.get(); } |
122 net::NetworkDelegate* network_delegate() { return network_delegate_.get(); } | 163 net::NetworkDelegate* network_delegate() { return network_delegate_.get(); } |
123 net::MockClientSocketFactory* socket_factory() { return &socket_factory_; } | 164 net::MockClientSocketFactory* socket_factory() { return &socket_factory_; } |
124 | 165 |
| 166 ChromeNetworkDelegate* chrome_network_delegate() { |
| 167 return network_delegate_.get(); |
| 168 } |
| 169 |
125 extensions::EventRouterForwarder* forwarder() { | 170 extensions::EventRouterForwarder* forwarder() { |
126 #if defined(ENABLE_EXTENSIONS) | 171 #if defined(ENABLE_EXTENSIONS) |
127 return forwarder_.get(); | 172 return forwarder_.get(); |
128 #else | 173 #else |
129 return nullptr; | 174 return nullptr; |
130 #endif | 175 #endif |
131 } | 176 } |
132 | 177 |
133 private: | 178 private: |
134 scoped_ptr<TestingProfileManager> profile_manager_; | 179 scoped_ptr<TestingProfileManager> profile_manager_; |
135 content::TestBrowserThreadBundle thread_bundle_; | 180 content::TestBrowserThreadBundle thread_bundle_; |
136 #if defined(ENABLE_EXTENSIONS) | 181 #if defined(ENABLE_EXTENSIONS) |
137 scoped_refptr<extensions::EventRouterForwarder> forwarder_; | 182 scoped_refptr<extensions::EventRouterForwarder> forwarder_; |
138 #endif | 183 #endif |
139 TestingProfile profile_; | 184 TestingProfile profile_; |
140 BooleanPrefMember enable_referrers_; | 185 BooleanPrefMember enable_referrers_; |
141 scoped_ptr<net::NetworkDelegate> network_delegate_; | 186 scoped_ptr<ChromeNetworkDelegate> network_delegate_; |
142 net::MockClientSocketFactory socket_factory_; | 187 net::MockClientSocketFactory socket_factory_; |
143 scoped_ptr<net::TestURLRequestContext> context_; | 188 scoped_ptr<net::TestURLRequestContext> context_; |
144 }; | 189 }; |
145 | 190 |
146 // This function tests data use measurement for requests by services. it makes a | 191 // 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 | 192 // query which is similar to a query of a service, so it should affect |
148 // DataUse.TrafficSize.System.Dimensions and DataUse.MessageSize.ServiceName | 193 // DataUse.TrafficSize.System.Dimensions and DataUse.MessageSize.ServiceName |
149 // histograms. AppState and ConnectionType dimensions are always Foreground and | 194 // histograms. AppState and ConnectionType dimensions are always Foreground and |
150 // NotCellular respectively. | 195 // NotCellular respectively. |
151 #if !defined(OS_IOS) | 196 #if !defined(OS_IOS) |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 EXPECT_FALSE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled()); | 285 EXPECT_FALSE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled()); |
241 } | 286 } |
242 | 287 |
243 TEST_F(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) { | 288 TEST_F(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) { |
244 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 289 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
245 switches::kEnableExperimentalWebPlatformFeatures); | 290 switches::kEnableExperimentalWebPlatformFeatures); |
246 Initialize(); | 291 Initialize(); |
247 EXPECT_TRUE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled()); | 292 EXPECT_TRUE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled()); |
248 } | 293 } |
249 | 294 |
| 295 TEST_F(ChromeNetworkDelegateTest, ReportDataUseToAggregator) { |
| 296 FakeDataUseAggregator fake_aggregator; |
| 297 Initialize(); |
| 298 |
| 299 chrome_network_delegate()->set_data_use_aggregator( |
| 300 &fake_aggregator, false /* is_data_usage_off_the_record */); |
| 301 |
| 302 scoped_ptr<net::URLRequest> request = |
| 303 RequestURL(context(), socket_factory(), true, false); |
| 304 EXPECT_EQ(request->GetTotalSentBytes(), |
| 305 fake_aggregator.on_the_record_tx_bytes()); |
| 306 EXPECT_EQ(request->GetTotalReceivedBytes(), |
| 307 fake_aggregator.on_the_record_rx_bytes()); |
| 308 EXPECT_EQ(0, fake_aggregator.off_the_record_tx_bytes()); |
| 309 EXPECT_EQ(0, fake_aggregator.off_the_record_rx_bytes()); |
| 310 } |
| 311 |
| 312 TEST_F(ChromeNetworkDelegateTest, ReportOffTheRecordDataUseToAggregator) { |
| 313 FakeDataUseAggregator fake_aggregator; |
| 314 Initialize(); |
| 315 |
| 316 chrome_network_delegate()->set_data_use_aggregator( |
| 317 &fake_aggregator, true /* is_data_usage_off_the_record */); |
| 318 scoped_ptr<net::URLRequest> request = |
| 319 RequestURL(context(), socket_factory(), true, false); |
| 320 |
| 321 EXPECT_EQ(0, fake_aggregator.on_the_record_tx_bytes()); |
| 322 EXPECT_EQ(0, fake_aggregator.on_the_record_rx_bytes()); |
| 323 EXPECT_EQ(request->GetTotalSentBytes(), |
| 324 fake_aggregator.off_the_record_tx_bytes()); |
| 325 EXPECT_EQ(request->GetTotalReceivedBytes(), |
| 326 fake_aggregator.off_the_record_rx_bytes()); |
| 327 } |
| 328 |
250 class ChromeNetworkDelegateSafeSearchTest : public testing::Test { | 329 class ChromeNetworkDelegateSafeSearchTest : public testing::Test { |
251 public: | 330 public: |
252 ChromeNetworkDelegateSafeSearchTest() | 331 ChromeNetworkDelegateSafeSearchTest() |
253 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 332 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
254 #if defined(ENABLE_EXTENSIONS) | 333 #if defined(ENABLE_EXTENSIONS) |
255 forwarder_ = new extensions::EventRouterForwarder(); | 334 forwarder_ = new extensions::EventRouterForwarder(); |
256 #endif | 335 #endif |
257 } | 336 } |
258 | 337 |
259 void SetUp() override { | 338 void SetUp() override { |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 kBlockedFirstPartySite)); | 530 kBlockedFirstPartySite)); |
452 | 531 |
453 cookie_settings_->SetCookieSetting( | 532 cookie_settings_->SetCookieSetting( |
454 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), | 533 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), |
455 ContentSettingsPattern::Wildcard(), | 534 ContentSettingsPattern::Wildcard(), |
456 CONTENT_SETTING_BLOCK); | 535 CONTENT_SETTING_BLOCK); |
457 // Privacy mode is disabled as kAllowedSite is still getting cookies | 536 // Privacy mode is disabled as kAllowedSite is still getting cookies |
458 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, | 537 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, |
459 kBlockedFirstPartySite)); | 538 kBlockedFirstPartySite)); |
460 } | 539 } |
OLD | NEW |