OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/data_reduction_proxy/core/browser/data_reduction_proxy_usag
e_stats.h" | 5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_bypa
ss_stats.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/prefs/testing_pref_service.h" | 14 #include "base/prefs/testing_pref_service.h" |
15 #include "base/test/histogram_tester.h" | 15 #include "base/test/histogram_tester.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 namespace data_reduction_proxy { | 50 namespace data_reduction_proxy { |
51 | 51 |
52 namespace { | 52 namespace { |
53 | 53 |
54 const std::string kBody = "hello"; | 54 const std::string kBody = "hello"; |
55 const std::string kNextBody = "hello again"; | 55 const std::string kNextBody = "hello again"; |
56 const std::string kErrorBody = "bad"; | 56 const std::string kErrorBody = "bad"; |
57 | 57 |
58 } // namespace | 58 } // namespace |
59 | 59 |
60 class DataReductionProxyUsageStatsTest : public testing::Test { | 60 class DataReductionProxyBypassStatsTest : public testing::Test { |
61 public: | 61 public: |
62 DataReductionProxyUsageStatsTest() | 62 DataReductionProxyBypassStatsTest() : context_(true) { |
63 : context_(true) { | |
64 context_.Init(); | 63 context_.Init(); |
65 | 64 |
66 // The |test_job_factory_| takes ownership of the interceptor. | 65 // The |test_job_factory_| takes ownership of the interceptor. |
67 test_job_interceptor_ = new net::TestJobInterceptor(); | 66 test_job_interceptor_ = new net::TestJobInterceptor(); |
68 EXPECT_TRUE(test_job_factory_.SetProtocolHandler(url::kHttpScheme, | 67 EXPECT_TRUE(test_job_factory_.SetProtocolHandler(url::kHttpScheme, |
69 test_job_interceptor_)); | 68 test_job_interceptor_)); |
70 | 69 |
71 context_.set_job_factory(&test_job_factory_); | 70 context_.set_job_factory(&test_job_factory_); |
72 | 71 |
73 test_context_ = | 72 test_context_ = |
(...skipping 30 matching lines...) Expand all Loading... |
104 | 103 |
105 EXPECT_TRUE(fake_request->response_headers() != NULL); | 104 EXPECT_TRUE(fake_request->response_headers() != NULL); |
106 return fake_request.Pass(); | 105 return fake_request.Pass(); |
107 } | 106 } |
108 | 107 |
109 bool IsUnreachable() const { | 108 bool IsUnreachable() const { |
110 return test_context_->settings()->IsDataReductionProxyUnreachable(); | 109 return test_context_->settings()->IsDataReductionProxyUnreachable(); |
111 } | 110 } |
112 | 111 |
113 protected: | 112 protected: |
114 scoped_ptr<DataReductionProxyUsageStats> BuildUsageStats() { | 113 scoped_ptr<DataReductionProxyBypassStats> BuildBypassStats() { |
115 return make_scoped_ptr( | 114 return make_scoped_ptr( |
116 new DataReductionProxyUsageStats( | 115 new DataReductionProxyBypassStats( |
117 test_context_->config(), | 116 test_context_->config(), |
118 test_context_->unreachable_callback(), | 117 test_context_->unreachable_callback(), |
119 test_context_->task_runner())).Pass(); | 118 test_context_->task_runner())).Pass(); |
120 } | 119 } |
121 | 120 |
122 net::URLRequest* url_request() { | 121 net::URLRequest* url_request() { |
123 return mock_url_request_.get(); | 122 return mock_url_request_.get(); |
124 } | 123 } |
125 | 124 |
126 MockDataReductionProxyConfig* config() const { | 125 MockDataReductionProxyConfig* config() const { |
127 return test_context_->mock_config(); | 126 return test_context_->mock_config(); |
128 } | 127 } |
129 | 128 |
130 void RunUntilIdle() { | 129 void RunUntilIdle() { |
131 test_context_->RunUntilIdle(); | 130 test_context_->RunUntilIdle(); |
132 } | 131 } |
133 | 132 |
134 private: | 133 private: |
135 net::TestURLRequestContext context_; | 134 net::TestURLRequestContext context_; |
136 net::TestDelegate delegate_; | 135 net::TestDelegate delegate_; |
137 scoped_ptr<net::URLRequest> mock_url_request_; | 136 scoped_ptr<net::URLRequest> mock_url_request_; |
138 // |test_job_interceptor_| is owned by |test_job_factory_|. | 137 // |test_job_interceptor_| is owned by |test_job_factory_|. |
139 net::TestJobInterceptor* test_job_interceptor_; | 138 net::TestJobInterceptor* test_job_interceptor_; |
140 net::URLRequestJobFactoryImpl test_job_factory_; | 139 net::URLRequestJobFactoryImpl test_job_factory_; |
141 scoped_ptr<DataReductionProxyTestContext> test_context_; | 140 scoped_ptr<DataReductionProxyTestContext> test_context_; |
142 }; | 141 }; |
143 | 142 |
144 TEST_F(DataReductionProxyUsageStatsTest, IsDataReductionProxyUnreachable) { | 143 TEST_F(DataReductionProxyBypassStatsTest, IsDataReductionProxyUnreachable) { |
145 net::ProxyServer fallback_proxy_server = | 144 net::ProxyServer fallback_proxy_server = |
146 net::ProxyServer::FromURI("foo.com", net::ProxyServer::SCHEME_HTTP); | 145 net::ProxyServer::FromURI("foo.com", net::ProxyServer::SCHEME_HTTP); |
147 data_reduction_proxy::DataReductionProxyTypeInfo proxy_info; | 146 data_reduction_proxy::DataReductionProxyTypeInfo proxy_info; |
148 struct TestCase { | 147 struct TestCase { |
149 bool fallback_proxy_server_is_data_reduction_proxy; | 148 bool fallback_proxy_server_is_data_reduction_proxy; |
150 bool was_proxy_used; | 149 bool was_proxy_used; |
151 bool is_unreachable; | 150 bool is_unreachable; |
152 }; | 151 }; |
153 const TestCase test_cases[] = { | 152 const TestCase test_cases[] = { |
154 { | 153 { |
(...skipping 16 matching lines...) Expand all Loading... |
171 false, | 170 false, |
172 true | 171 true |
173 } | 172 } |
174 }; | 173 }; |
175 for (size_t i = 0; i < arraysize(test_cases); ++i) { | 174 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
176 TestCase test_case = test_cases[i]; | 175 TestCase test_case = test_cases[i]; |
177 | 176 |
178 EXPECT_CALL(*config(), IsDataReductionProxy(testing::_, testing::_)) | 177 EXPECT_CALL(*config(), IsDataReductionProxy(testing::_, testing::_)) |
179 .WillRepeatedly(testing::Return( | 178 .WillRepeatedly(testing::Return( |
180 test_case.fallback_proxy_server_is_data_reduction_proxy)); | 179 test_case.fallback_proxy_server_is_data_reduction_proxy)); |
181 EXPECT_CALL(*config(), | 180 EXPECT_CALL(*config(), WasDataReductionProxyUsed(url_request(), testing::_)) |
182 WasDataReductionProxyUsed(url_request(), testing::_)) | |
183 .WillRepeatedly(testing::Return(test_case.was_proxy_used)); | 181 .WillRepeatedly(testing::Return(test_case.was_proxy_used)); |
184 | 182 |
185 scoped_ptr<DataReductionProxyUsageStats> usage_stats = BuildUsageStats(); | 183 scoped_ptr<DataReductionProxyBypassStats> bypass_stats = BuildBypassStats(); |
186 | 184 |
187 usage_stats->OnProxyFallback(fallback_proxy_server, | 185 bypass_stats->OnProxyFallback(fallback_proxy_server, |
188 net::ERR_PROXY_CONNECTION_FAILED); | 186 net::ERR_PROXY_CONNECTION_FAILED); |
189 usage_stats->OnUrlRequestCompleted(url_request(), false); | 187 bypass_stats->OnUrlRequestCompleted(url_request(), false); |
190 RunUntilIdle(); | 188 RunUntilIdle(); |
191 | 189 |
192 EXPECT_EQ(test_case.is_unreachable, IsUnreachable()); | 190 EXPECT_EQ(test_case.is_unreachable, IsUnreachable()); |
193 } | 191 } |
194 } | 192 } |
195 | 193 |
196 TEST_F(DataReductionProxyUsageStatsTest, ProxyUnreachableThenReachable) { | 194 TEST_F(DataReductionProxyBypassStatsTest, ProxyUnreachableThenReachable) { |
197 net::ProxyServer fallback_proxy_server = | 195 net::ProxyServer fallback_proxy_server = |
198 net::ProxyServer::FromURI("foo.com", net::ProxyServer::SCHEME_HTTP); | 196 net::ProxyServer::FromURI("foo.com", net::ProxyServer::SCHEME_HTTP); |
199 scoped_ptr<DataReductionProxyUsageStats> usage_stats = BuildUsageStats(); | 197 scoped_ptr<DataReductionProxyBypassStats> bypass_stats = BuildBypassStats(); |
200 EXPECT_CALL(*config(), IsDataReductionProxy(testing::_, testing::_)) | 198 EXPECT_CALL(*config(), IsDataReductionProxy(testing::_, testing::_)) |
201 .WillOnce(testing::Return(true)); | 199 .WillOnce(testing::Return(true)); |
202 EXPECT_CALL(*config(), | 200 EXPECT_CALL(*config(), WasDataReductionProxyUsed(url_request(), testing::_)) |
203 WasDataReductionProxyUsed(url_request(), testing::_)) | |
204 .WillOnce(testing::Return(true)); | 201 .WillOnce(testing::Return(true)); |
205 | 202 |
206 // proxy falls back | 203 // proxy falls back |
207 usage_stats->OnProxyFallback(fallback_proxy_server, | 204 bypass_stats->OnProxyFallback(fallback_proxy_server, |
208 net::ERR_PROXY_CONNECTION_FAILED); | 205 net::ERR_PROXY_CONNECTION_FAILED); |
209 RunUntilIdle(); | 206 RunUntilIdle(); |
210 EXPECT_TRUE(IsUnreachable()); | 207 EXPECT_TRUE(IsUnreachable()); |
211 | 208 |
212 // proxy succeeds | 209 // proxy succeeds |
213 usage_stats->OnUrlRequestCompleted(url_request(), false); | 210 bypass_stats->OnUrlRequestCompleted(url_request(), false); |
214 RunUntilIdle(); | 211 RunUntilIdle(); |
215 EXPECT_FALSE(IsUnreachable()); | 212 EXPECT_FALSE(IsUnreachable()); |
216 } | 213 } |
217 | 214 |
218 TEST_F(DataReductionProxyUsageStatsTest, ProxyReachableThenUnreachable) { | 215 TEST_F(DataReductionProxyBypassStatsTest, ProxyReachableThenUnreachable) { |
219 net::ProxyServer fallback_proxy_server = | 216 net::ProxyServer fallback_proxy_server = |
220 net::ProxyServer::FromURI("foo.com", net::ProxyServer::SCHEME_HTTP); | 217 net::ProxyServer::FromURI("foo.com", net::ProxyServer::SCHEME_HTTP); |
221 scoped_ptr<DataReductionProxyUsageStats> usage_stats = BuildUsageStats(); | 218 scoped_ptr<DataReductionProxyBypassStats> bypass_stats = BuildBypassStats(); |
222 EXPECT_CALL(*config(), | 219 EXPECT_CALL(*config(), WasDataReductionProxyUsed(url_request(), testing::_)) |
223 WasDataReductionProxyUsed(url_request(), testing::_)) | |
224 .WillOnce(testing::Return(true)); | 220 .WillOnce(testing::Return(true)); |
225 EXPECT_CALL(*config(), IsDataReductionProxy(testing::_, testing::_)) | 221 EXPECT_CALL(*config(), IsDataReductionProxy(testing::_, testing::_)) |
226 .WillRepeatedly(testing::Return(true)); | 222 .WillRepeatedly(testing::Return(true)); |
227 | 223 |
228 // Proxy succeeds. | 224 // Proxy succeeds. |
229 usage_stats->OnUrlRequestCompleted(url_request(), false); | 225 bypass_stats->OnUrlRequestCompleted(url_request(), false); |
230 RunUntilIdle(); | 226 RunUntilIdle(); |
231 EXPECT_FALSE(IsUnreachable()); | 227 EXPECT_FALSE(IsUnreachable()); |
232 | 228 |
233 // Then proxy falls back indefinitely. | 229 // Then proxy falls back indefinitely. |
234 usage_stats->OnProxyFallback(fallback_proxy_server, | 230 bypass_stats->OnProxyFallback(fallback_proxy_server, |
235 net::ERR_PROXY_CONNECTION_FAILED); | 231 net::ERR_PROXY_CONNECTION_FAILED); |
236 usage_stats->OnProxyFallback(fallback_proxy_server, | 232 bypass_stats->OnProxyFallback(fallback_proxy_server, |
237 net::ERR_PROXY_CONNECTION_FAILED); | 233 net::ERR_PROXY_CONNECTION_FAILED); |
238 usage_stats->OnProxyFallback(fallback_proxy_server, | 234 bypass_stats->OnProxyFallback(fallback_proxy_server, |
239 net::ERR_PROXY_CONNECTION_FAILED); | 235 net::ERR_PROXY_CONNECTION_FAILED); |
240 usage_stats->OnProxyFallback(fallback_proxy_server, | 236 bypass_stats->OnProxyFallback(fallback_proxy_server, |
241 net::ERR_PROXY_CONNECTION_FAILED); | 237 net::ERR_PROXY_CONNECTION_FAILED); |
242 RunUntilIdle(); | 238 RunUntilIdle(); |
243 EXPECT_TRUE(IsUnreachable()); | 239 EXPECT_TRUE(IsUnreachable()); |
244 } | 240 } |
245 | 241 |
246 TEST_F(DataReductionProxyUsageStatsTest, | 242 TEST_F(DataReductionProxyBypassStatsTest, |
247 DetectAndRecordMissingViaHeaderResponseCode) { | 243 DetectAndRecordMissingViaHeaderResponseCode) { |
248 const std::string kPrimaryHistogramName = | 244 const std::string kPrimaryHistogramName = |
249 "DataReductionProxy.MissingViaHeader.ResponseCode.Primary"; | 245 "DataReductionProxy.MissingViaHeader.ResponseCode.Primary"; |
250 const std::string kFallbackHistogramName = | 246 const std::string kFallbackHistogramName = |
251 "DataReductionProxy.MissingViaHeader.ResponseCode.Fallback"; | 247 "DataReductionProxy.MissingViaHeader.ResponseCode.Fallback"; |
252 | 248 |
253 struct TestCase { | 249 struct TestCase { |
254 bool is_primary; | 250 bool is_primary; |
255 const char* headers; | 251 const char* headers; |
256 int expected_primary_sample; // -1 indicates no expected sample. | 252 int expected_primary_sample; // -1 indicates no expected sample. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 } | 305 } |
310 }; | 306 }; |
311 | 307 |
312 for (size_t i = 0; i < arraysize(test_cases); ++i) { | 308 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
313 base::HistogramTester histogram_tester; | 309 base::HistogramTester histogram_tester; |
314 std::string raw_headers(test_cases[i].headers); | 310 std::string raw_headers(test_cases[i].headers); |
315 HeadersToRaw(&raw_headers); | 311 HeadersToRaw(&raw_headers); |
316 scoped_refptr<net::HttpResponseHeaders> headers( | 312 scoped_refptr<net::HttpResponseHeaders> headers( |
317 new net::HttpResponseHeaders(raw_headers)); | 313 new net::HttpResponseHeaders(raw_headers)); |
318 | 314 |
319 DataReductionProxyUsageStats::DetectAndRecordMissingViaHeaderResponseCode( | 315 DataReductionProxyBypassStats::DetectAndRecordMissingViaHeaderResponseCode( |
320 test_cases[i].is_primary, headers.get()); | 316 test_cases[i].is_primary, headers.get()); |
321 | 317 |
322 if (test_cases[i].expected_primary_sample == -1) { | 318 if (test_cases[i].expected_primary_sample == -1) { |
323 histogram_tester.ExpectTotalCount(kPrimaryHistogramName, 0); | 319 histogram_tester.ExpectTotalCount(kPrimaryHistogramName, 0); |
324 } else { | 320 } else { |
325 histogram_tester.ExpectUniqueSample( | 321 histogram_tester.ExpectUniqueSample( |
326 kPrimaryHistogramName, test_cases[i].expected_primary_sample, 1); | 322 kPrimaryHistogramName, test_cases[i].expected_primary_sample, 1); |
327 } | 323 } |
328 | 324 |
329 if (test_cases[i].expected_fallback_sample == -1) { | 325 if (test_cases[i].expected_fallback_sample == -1) { |
330 histogram_tester.ExpectTotalCount(kFallbackHistogramName, 0); | 326 histogram_tester.ExpectTotalCount(kFallbackHistogramName, 0); |
331 } else { | 327 } else { |
332 histogram_tester.ExpectUniqueSample( | 328 histogram_tester.ExpectUniqueSample( |
333 kFallbackHistogramName, test_cases[i].expected_fallback_sample, 1); | 329 kFallbackHistogramName, test_cases[i].expected_fallback_sample, 1); |
334 } | 330 } |
335 } | 331 } |
336 } | 332 } |
337 | 333 |
338 TEST_F(DataReductionProxyUsageStatsTest, RecordMissingViaHeaderBytes) { | 334 TEST_F(DataReductionProxyBypassStatsTest, RecordMissingViaHeaderBytes) { |
339 const std::string k4xxHistogramName = | 335 const std::string k4xxHistogramName = |
340 "DataReductionProxy.MissingViaHeader.Bytes.4xx"; | 336 "DataReductionProxy.MissingViaHeader.Bytes.4xx"; |
341 const std::string kOtherHistogramName = | 337 const std::string kOtherHistogramName = |
342 "DataReductionProxy.MissingViaHeader.Bytes.Other"; | 338 "DataReductionProxy.MissingViaHeader.Bytes.Other"; |
343 const int64 kResponseContentLength = 100; | 339 const int64 kResponseContentLength = 100; |
344 | 340 |
345 struct TestCase { | 341 struct TestCase { |
346 bool was_proxy_used; | 342 bool was_proxy_used; |
347 const char* headers; | 343 const char* headers; |
348 bool is_4xx_sample_expected; | 344 bool is_4xx_sample_expected; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 { | 410 { |
415 true, | 411 true, |
416 "HTTP/1.1 500 Internal Server Error\n", | 412 "HTTP/1.1 500 Internal Server Error\n", |
417 false, | 413 false, |
418 true | 414 true |
419 } | 415 } |
420 }; | 416 }; |
421 | 417 |
422 for (size_t i = 0; i < arraysize(test_cases); ++i) { | 418 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
423 base::HistogramTester histogram_tester; | 419 base::HistogramTester histogram_tester; |
424 scoped_ptr<DataReductionProxyUsageStats> usage_stats = BuildUsageStats(); | 420 scoped_ptr<DataReductionProxyBypassStats> bypass_stats = BuildBypassStats(); |
425 | 421 |
426 std::string raw_headers(test_cases[i].headers); | 422 std::string raw_headers(test_cases[i].headers); |
427 HeadersToRaw(&raw_headers); | 423 HeadersToRaw(&raw_headers); |
428 | 424 |
429 scoped_ptr<net::URLRequest> fake_request( | 425 scoped_ptr<net::URLRequest> fake_request( |
430 CreateURLRequestWithResponseHeaders(GURL("http://www.google.com/"), | 426 CreateURLRequestWithResponseHeaders(GURL("http://www.google.com/"), |
431 raw_headers)); | 427 raw_headers)); |
432 fake_request->set_received_response_content_length(kResponseContentLength); | 428 fake_request->set_received_response_content_length(kResponseContentLength); |
433 | 429 |
434 EXPECT_CALL(*config(), | 430 EXPECT_CALL(*config(), |
435 WasDataReductionProxyUsed(fake_request.get(), testing::_)) | 431 WasDataReductionProxyUsed(fake_request.get(), testing::_)) |
436 .WillRepeatedly(Return(test_cases[i].was_proxy_used)); | 432 .WillRepeatedly(Return(test_cases[i].was_proxy_used)); |
437 | 433 |
438 usage_stats->RecordMissingViaHeaderBytes(*fake_request); | 434 bypass_stats->RecordMissingViaHeaderBytes(*fake_request); |
439 | 435 |
440 if (test_cases[i].is_4xx_sample_expected) { | 436 if (test_cases[i].is_4xx_sample_expected) { |
441 histogram_tester.ExpectUniqueSample(k4xxHistogramName, | 437 histogram_tester.ExpectUniqueSample(k4xxHistogramName, |
442 kResponseContentLength, 1); | 438 kResponseContentLength, 1); |
443 } else { | 439 } else { |
444 histogram_tester.ExpectTotalCount(k4xxHistogramName, 0); | 440 histogram_tester.ExpectTotalCount(k4xxHistogramName, 0); |
445 } | 441 } |
446 | 442 |
447 if (test_cases[i].is_other_sample_expected) { | 443 if (test_cases[i].is_other_sample_expected) { |
448 histogram_tester.ExpectUniqueSample(kOtherHistogramName, | 444 histogram_tester.ExpectUniqueSample(kOtherHistogramName, |
449 kResponseContentLength, 1); | 445 kResponseContentLength, 1); |
450 } else { | 446 } else { |
451 histogram_tester.ExpectTotalCount(kOtherHistogramName, 0); | 447 histogram_tester.ExpectTotalCount(kOtherHistogramName, 0); |
452 } | 448 } |
453 } | 449 } |
454 } | 450 } |
455 | 451 |
456 TEST_F(DataReductionProxyUsageStatsTest, RequestCompletionErrorCodes) { | 452 TEST_F(DataReductionProxyBypassStatsTest, RequestCompletionErrorCodes) { |
457 const std::string kPrimaryHistogramName = | 453 const std::string kPrimaryHistogramName = |
458 "DataReductionProxy.RequestCompletionErrorCodes.Primary"; | 454 "DataReductionProxy.RequestCompletionErrorCodes.Primary"; |
459 const std::string kFallbackHistogramName = | 455 const std::string kFallbackHistogramName = |
460 "DataReductionProxy.RequestCompletionErrorCodes.Fallback"; | 456 "DataReductionProxy.RequestCompletionErrorCodes.Fallback"; |
461 const std::string kPrimaryMainFrameHistogramName = | 457 const std::string kPrimaryMainFrameHistogramName = |
462 "DataReductionProxy.RequestCompletionErrorCodes.MainFrame.Primary"; | 458 "DataReductionProxy.RequestCompletionErrorCodes.MainFrame.Primary"; |
463 const std::string kFallbackMainFrameHistogramName = | 459 const std::string kFallbackMainFrameHistogramName = |
464 "DataReductionProxy.RequestCompletionErrorCodes.MainFrame.Fallback"; | 460 "DataReductionProxy.RequestCompletionErrorCodes.MainFrame.Fallback"; |
465 | 461 |
466 struct TestCase { | 462 struct TestCase { |
467 bool was_proxy_used; | 463 bool was_proxy_used; |
468 bool is_load_bypass_proxy; | 464 bool is_load_bypass_proxy; |
469 bool is_fallback; | 465 bool is_fallback; |
470 bool is_main_frame; | 466 bool is_main_frame; |
471 net::Error net_error; | 467 net::Error net_error; |
472 }; | 468 }; |
473 | 469 |
474 const TestCase test_cases[] = { | 470 const TestCase test_cases[] = { |
475 {false, true, false, true, net::OK}, | 471 { false, true, false, true, net::OK }, |
476 {false, true, false, false, net::ERR_TOO_MANY_REDIRECTS}, | 472 { false, true, false, false, net::ERR_TOO_MANY_REDIRECTS }, |
477 {false, false, false, true, net::OK}, | 473 { false, false, false, true, net::OK }, |
478 {false, false, false, false, net::ERR_TOO_MANY_REDIRECTS}, | 474 { false, false, false, false, net::ERR_TOO_MANY_REDIRECTS }, |
479 {true, false, false, true, net::OK}, | 475 { true, false, false, true, net::OK }, |
480 {true, false, false, true, net::ERR_TOO_MANY_REDIRECTS}, | 476 { true, false, false, true, net::ERR_TOO_MANY_REDIRECTS }, |
481 {true, false, false, false, net::OK}, | 477 { true, false, false, false, net::OK }, |
482 {true, false, false, false, net::ERR_TOO_MANY_REDIRECTS}, | 478 { true, false, false, false, net::ERR_TOO_MANY_REDIRECTS }, |
483 {true, false, true, true, net::OK}, | 479 { true, false, true, true, net::OK }, |
484 {true, false, true, true, net::ERR_TOO_MANY_REDIRECTS}, | 480 { true, false, true, true, net::ERR_TOO_MANY_REDIRECTS }, |
485 {true, false, true, false, net::OK}, | 481 { true, false, true, false, net::OK }, |
486 {true, false, true, false, net::ERR_TOO_MANY_REDIRECTS} | 482 { true, false, true, false, net::ERR_TOO_MANY_REDIRECTS } |
487 }; | 483 }; |
488 | 484 |
489 for (size_t i = 0; i < arraysize(test_cases); ++i) { | 485 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
490 base::HistogramTester histogram_tester; | 486 base::HistogramTester histogram_tester; |
491 scoped_ptr<DataReductionProxyUsageStats> usage_stats = BuildUsageStats(); | 487 scoped_ptr<DataReductionProxyBypassStats> bypass_stats = BuildBypassStats(); |
492 | 488 |
493 std::string raw_headers("HTTP/1.1 200 OK\n" | 489 std::string raw_headers("HTTP/1.1 200 OK\n" |
494 "Via: 1.1 Chrome-Compression-Proxy\n"); | 490 "Via: 1.1 Chrome-Compression-Proxy\n"); |
495 HeadersToRaw(&raw_headers); | 491 HeadersToRaw(&raw_headers); |
496 scoped_ptr<net::URLRequest> fake_request( | 492 scoped_ptr<net::URLRequest> fake_request( |
497 CreateURLRequestWithResponseHeaders(GURL("http://www.google.com/"), | 493 CreateURLRequestWithResponseHeaders(GURL("http://www.google.com/"), |
498 raw_headers)); | 494 raw_headers)); |
499 if (test_cases[i].is_load_bypass_proxy) { | 495 if (test_cases[i].is_load_bypass_proxy) { |
500 fake_request->SetLoadFlags(fake_request->load_flags() | | 496 fake_request->SetLoadFlags(fake_request->load_flags() | |
501 net::LOAD_BYPASS_PROXY); | 497 net::LOAD_BYPASS_PROXY); |
502 } | 498 } |
503 if (test_cases[i].is_main_frame) { | 499 if (test_cases[i].is_main_frame) { |
504 fake_request->SetLoadFlags(fake_request->load_flags() | | 500 fake_request->SetLoadFlags(fake_request->load_flags() | |
505 net::LOAD_MAIN_FRAME); | 501 net::LOAD_MAIN_FRAME); |
506 } | 502 } |
507 | 503 |
508 int net_error_int = static_cast<int>(test_cases[i].net_error); | 504 int net_error_int = static_cast<int>(test_cases[i].net_error); |
509 if (test_cases[i].net_error != net::OK) { | 505 if (test_cases[i].net_error != net::OK) { |
510 fake_request->CancelWithError(net_error_int); | 506 fake_request->CancelWithError(net_error_int); |
511 } | 507 } |
512 | 508 |
513 DataReductionProxyTypeInfo proxy_info; | 509 DataReductionProxyTypeInfo proxy_info; |
514 proxy_info.is_fallback = test_cases[i].is_fallback; | 510 proxy_info.is_fallback = test_cases[i].is_fallback; |
515 EXPECT_CALL(*config(), WasDataReductionProxyUsed(fake_request.get(), | 511 EXPECT_CALL(*config(), WasDataReductionProxyUsed(fake_request.get(), |
516 testing::NotNull())) | 512 testing::NotNull())) |
517 .WillRepeatedly(testing::DoAll(testing::SetArgPointee<1>(proxy_info), | 513 .WillRepeatedly(testing::DoAll(testing::SetArgPointee<1>(proxy_info), |
518 Return(test_cases[i].was_proxy_used))); | 514 Return(test_cases[i].was_proxy_used))); |
519 | 515 |
520 usage_stats->OnUrlRequestCompleted(fake_request.get(), false); | 516 bypass_stats->OnUrlRequestCompleted(fake_request.get(), false); |
521 | 517 |
522 if (test_cases[i].was_proxy_used && !test_cases[i].is_load_bypass_proxy && | 518 if (test_cases[i].was_proxy_used && !test_cases[i].is_load_bypass_proxy && |
523 !test_cases[i].is_fallback) { | 519 !test_cases[i].is_fallback) { |
524 histogram_tester.ExpectUniqueSample( | 520 histogram_tester.ExpectUniqueSample( |
525 kPrimaryHistogramName, -net_error_int, 1); | 521 kPrimaryHistogramName, -net_error_int, 1); |
526 } else { | 522 } else { |
527 histogram_tester.ExpectTotalCount(kPrimaryHistogramName, 0); | 523 histogram_tester.ExpectTotalCount(kPrimaryHistogramName, 0); |
528 } | 524 } |
529 if (test_cases[i].was_proxy_used && !test_cases[i].is_load_bypass_proxy && | 525 if (test_cases[i].was_proxy_used && !test_cases[i].is_load_bypass_proxy && |
530 test_cases[i].is_fallback) { | 526 test_cases[i].is_fallback) { |
(...skipping 13 matching lines...) Expand all Loading... |
544 test_cases[i].is_fallback && test_cases[i].is_main_frame) { | 540 test_cases[i].is_fallback && test_cases[i].is_main_frame) { |
545 histogram_tester.ExpectUniqueSample( | 541 histogram_tester.ExpectUniqueSample( |
546 kFallbackMainFrameHistogramName, -net_error_int, 1); | 542 kFallbackMainFrameHistogramName, -net_error_int, 1); |
547 } else { | 543 } else { |
548 histogram_tester.ExpectTotalCount(kFallbackMainFrameHistogramName, 0); | 544 histogram_tester.ExpectTotalCount(kFallbackMainFrameHistogramName, 0); |
549 } | 545 } |
550 } | 546 } |
551 } | 547 } |
552 | 548 |
553 // End-to-end tests for the DataReductionProxy.BypassedBytes histograms. | 549 // End-to-end tests for the DataReductionProxy.BypassedBytes histograms. |
554 class DataReductionProxyUsageStatsEndToEndTest : public testing::Test { | 550 class DataReductionProxyBypassStatsEndToEndTest : public testing::Test { |
555 public: | 551 public: |
556 DataReductionProxyUsageStatsEndToEndTest() | 552 DataReductionProxyBypassStatsEndToEndTest() |
557 : context_(true), context_storage_(&context_) {} | 553 : context_(true), context_storage_(&context_) {} |
558 | 554 |
559 ~DataReductionProxyUsageStatsEndToEndTest() override { | 555 ~DataReductionProxyBypassStatsEndToEndTest() override { |
560 drp_test_context_->io_data()->ShutdownOnUIThread(); | 556 drp_test_context_->io_data()->ShutdownOnUIThread(); |
561 drp_test_context_->RunUntilIdle(); | 557 drp_test_context_->RunUntilIdle(); |
562 } | 558 } |
563 | 559 |
564 void SetUp() override { | 560 void SetUp() override { |
565 // Only use the primary data reduction proxy in order to make it easier to | 561 // Only use the primary data reduction proxy in order to make it easier to |
566 // test bypassed bytes due to proxy fallbacks. This way, a test just needs | 562 // test bypassed bytes due to proxy fallbacks. This way, a test just needs |
567 // to cause one proxy fallback in order for the data reduction proxy to be | 563 // to cause one proxy fallback in order for the data reduction proxy to be |
568 // fully bypassed. | 564 // fully bypassed. |
569 drp_test_context_ = | 565 drp_test_context_ = |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 } | 703 } |
708 | 704 |
709 private: | 705 private: |
710 net::TestDelegate delegate_; | 706 net::TestDelegate delegate_; |
711 net::MockClientSocketFactory mock_socket_factory_; | 707 net::MockClientSocketFactory mock_socket_factory_; |
712 net::TestURLRequestContext context_; | 708 net::TestURLRequestContext context_; |
713 net::URLRequestContextStorage context_storage_; | 709 net::URLRequestContextStorage context_storage_; |
714 scoped_ptr<DataReductionProxyTestContext> drp_test_context_; | 710 scoped_ptr<DataReductionProxyTestContext> drp_test_context_; |
715 }; | 711 }; |
716 | 712 |
717 TEST_F(DataReductionProxyUsageStatsEndToEndTest, BypassedBytesNoRetry) { | 713 TEST_F(DataReductionProxyBypassStatsEndToEndTest, BypassedBytesNoRetry) { |
718 struct TestCase { | 714 struct TestCase { |
719 GURL url; | 715 GURL url; |
720 const char* histogram_name; | 716 const char* histogram_name; |
721 const char* initial_response_headers; | 717 const char* initial_response_headers; |
722 }; | 718 }; |
723 const TestCase test_cases[] = { | 719 const TestCase test_cases[] = { |
724 { GURL("http://foo.com"), | 720 { GURL("http://foo.com"), |
725 "DataReductionProxy.BypassedBytes.NotBypassed", | 721 "DataReductionProxy.BypassedBytes.NotBypassed", |
726 "HTTP/1.1 200 OK\r\n" | 722 "HTTP/1.1 200 OK\r\n" |
727 "Via: 1.1 Chrome-Compression-Proxy\r\n\r\n", | 723 "Via: 1.1 Chrome-Compression-Proxy\r\n\r\n", |
(...skipping 15 matching lines...) Expand all Loading... |
743 CreateAndExecuteRequest(test_case.url, test_case.initial_response_headers, | 739 CreateAndExecuteRequest(test_case.url, test_case.initial_response_headers, |
744 kBody.c_str(), nullptr, nullptr); | 740 kBody.c_str(), nullptr, nullptr); |
745 | 741 |
746 histogram_tester.ExpectUniqueSample(test_case.histogram_name, kBody.size(), | 742 histogram_tester.ExpectUniqueSample(test_case.histogram_name, kBody.size(), |
747 1); | 743 1); |
748 ExpectOtherBypassedBytesHistogramsEmpty(histogram_tester, | 744 ExpectOtherBypassedBytesHistogramsEmpty(histogram_tester, |
749 test_case.histogram_name); | 745 test_case.histogram_name); |
750 } | 746 } |
751 } | 747 } |
752 | 748 |
753 TEST_F(DataReductionProxyUsageStatsEndToEndTest, BypassedBytesProxyOverridden) { | 749 TEST_F(DataReductionProxyBypassStatsEndToEndTest, |
| 750 BypassedBytesProxyOverridden) { |
754 scoped_ptr<net::ProxyService> proxy_service( | 751 scoped_ptr<net::ProxyService> proxy_service( |
755 net::ProxyService::CreateFixed("http://test.com:80")); | 752 net::ProxyService::CreateFixed("http://test.com:80")); |
756 set_proxy_service(proxy_service.get()); | 753 set_proxy_service(proxy_service.get()); |
757 InitializeContext(); | 754 InitializeContext(); |
758 | 755 |
759 base::HistogramTester histogram_tester; | 756 base::HistogramTester histogram_tester; |
760 CreateAndExecuteRequest(GURL("http://foo.com"), "HTTP/1.1 200 OK\r\n\r\n", | 757 CreateAndExecuteRequest(GURL("http://foo.com"), "HTTP/1.1 200 OK\r\n\r\n", |
761 kBody.c_str(), nullptr, nullptr); | 758 kBody.c_str(), nullptr, nullptr); |
762 | 759 |
763 histogram_tester.ExpectUniqueSample( | 760 histogram_tester.ExpectUniqueSample( |
764 "DataReductionProxy.BypassedBytes.ProxyOverridden", kBody.size(), 1); | 761 "DataReductionProxy.BypassedBytes.ProxyOverridden", kBody.size(), 1); |
765 ExpectOtherBypassedBytesHistogramsEmpty( | 762 ExpectOtherBypassedBytesHistogramsEmpty( |
766 histogram_tester, "DataReductionProxy.BypassedBytes.ProxyOverridden"); | 763 histogram_tester, "DataReductionProxy.BypassedBytes.ProxyOverridden"); |
767 } | 764 } |
768 | 765 |
769 TEST_F(DataReductionProxyUsageStatsEndToEndTest, BypassedBytesCurrent) { | 766 TEST_F(DataReductionProxyBypassStatsEndToEndTest, BypassedBytesCurrent) { |
770 InitializeContext(); | 767 InitializeContext(); |
771 base::HistogramTester histogram_tester; | 768 base::HistogramTester histogram_tester; |
772 CreateAndExecuteRequest(GURL("http://foo.com"), | 769 CreateAndExecuteRequest(GURL("http://foo.com"), |
773 "HTTP/1.1 502 Bad Gateway\r\n" | 770 "HTTP/1.1 502 Bad Gateway\r\n" |
774 "Via: 1.1 Chrome-Compression-Proxy\r\n" | 771 "Via: 1.1 Chrome-Compression-Proxy\r\n" |
775 "Chrome-Proxy: block-once\r\n\r\n", | 772 "Chrome-Proxy: block-once\r\n\r\n", |
776 kErrorBody.c_str(), "HTTP/1.1 200 OK\r\n\r\n", | 773 kErrorBody.c_str(), "HTTP/1.1 200 OK\r\n\r\n", |
777 kBody.c_str()); | 774 kBody.c_str()); |
778 | 775 |
779 histogram_tester.ExpectUniqueSample( | 776 histogram_tester.ExpectUniqueSample( |
780 "DataReductionProxy.BypassedBytes.Current", kBody.size(), 1); | 777 "DataReductionProxy.BypassedBytes.Current", kBody.size(), 1); |
781 ExpectOtherBypassedBytesHistogramsEmpty( | 778 ExpectOtherBypassedBytesHistogramsEmpty( |
782 histogram_tester, "DataReductionProxy.BypassedBytes.Current"); | 779 histogram_tester, "DataReductionProxy.BypassedBytes.Current"); |
783 } | 780 } |
784 | 781 |
785 TEST_F(DataReductionProxyUsageStatsEndToEndTest, BypassedBytesShortAudioVideo) { | 782 TEST_F(DataReductionProxyBypassStatsEndToEndTest, |
| 783 BypassedBytesShortAudioVideo) { |
786 InitializeContext(); | 784 InitializeContext(); |
787 base::HistogramTester histogram_tester; | 785 base::HistogramTester histogram_tester; |
788 CreateAndExecuteRequest(GURL("http://foo.com"), | 786 CreateAndExecuteRequest(GURL("http://foo.com"), |
789 "HTTP/1.1 502 Bad Gateway\r\n" | 787 "HTTP/1.1 502 Bad Gateway\r\n" |
790 "Via: 1.1 Chrome-Compression-Proxy\r\n" | 788 "Via: 1.1 Chrome-Compression-Proxy\r\n" |
791 "Chrome-Proxy: block=1\r\n\r\n", | 789 "Chrome-Proxy: block=1\r\n\r\n", |
792 kErrorBody.c_str(), | 790 kErrorBody.c_str(), |
793 "HTTP/1.1 200 OK\r\n" | 791 "HTTP/1.1 200 OK\r\n" |
794 "Content-Type: video/mp4\r\n\r\n", | 792 "Content-Type: video/mp4\r\n\r\n", |
795 kBody.c_str()); | 793 kBody.c_str()); |
796 | 794 |
797 histogram_tester.ExpectUniqueSample( | 795 histogram_tester.ExpectUniqueSample( |
798 "DataReductionProxy.BypassedBytes.ShortAudioVideo", kBody.size(), 1); | 796 "DataReductionProxy.BypassedBytes.ShortAudioVideo", kBody.size(), 1); |
799 ExpectOtherBypassedBytesHistogramsEmpty( | 797 ExpectOtherBypassedBytesHistogramsEmpty( |
800 histogram_tester, "DataReductionProxy.BypassedBytes.ShortAudioVideo"); | 798 histogram_tester, "DataReductionProxy.BypassedBytes.ShortAudioVideo"); |
801 } | 799 } |
802 | 800 |
803 TEST_F(DataReductionProxyUsageStatsEndToEndTest, BypassedBytesExplicitBypass) { | 801 TEST_F(DataReductionProxyBypassStatsEndToEndTest, BypassedBytesExplicitBypass) { |
804 struct TestCase { | 802 struct TestCase { |
805 const char* triggering_histogram_name; | 803 const char* triggering_histogram_name; |
806 const char* all_histogram_name; | 804 const char* all_histogram_name; |
807 const char* initial_response_headers; | 805 const char* initial_response_headers; |
808 }; | 806 }; |
809 const TestCase test_cases[] = { | 807 const TestCase test_cases[] = { |
810 { "DataReductionProxy.BypassedBytes.ShortTriggeringRequest", | 808 { "DataReductionProxy.BypassedBytes.ShortTriggeringRequest", |
811 "DataReductionProxy.BypassedBytes.ShortAll", | 809 "DataReductionProxy.BypassedBytes.ShortAll", |
812 "HTTP/1.1 502 Bad Gateway\r\n" | 810 "HTTP/1.1 502 Bad Gateway\r\n" |
813 "Via: 1.1 Chrome-Compression-Proxy\r\n" | 811 "Via: 1.1 Chrome-Compression-Proxy\r\n" |
(...skipping 29 matching lines...) Expand all Loading... |
843 histogram_tester.ExpectUniqueSample(test_case.triggering_histogram_name, | 841 histogram_tester.ExpectUniqueSample(test_case.triggering_histogram_name, |
844 kBody.size(), 1); | 842 kBody.size(), 1); |
845 histogram_tester.ExpectUniqueSample(test_case.all_histogram_name, | 843 histogram_tester.ExpectUniqueSample(test_case.all_histogram_name, |
846 kNextBody.size(), 1); | 844 kNextBody.size(), 1); |
847 ExpectOtherBypassedBytesHistogramsEmpty(histogram_tester, | 845 ExpectOtherBypassedBytesHistogramsEmpty(histogram_tester, |
848 test_case.triggering_histogram_name, | 846 test_case.triggering_histogram_name, |
849 test_case.all_histogram_name); | 847 test_case.all_histogram_name); |
850 } | 848 } |
851 } | 849 } |
852 | 850 |
853 TEST_F(DataReductionProxyUsageStatsEndToEndTest, | 851 TEST_F(DataReductionProxyBypassStatsEndToEndTest, |
854 BypassedBytesClientSideFallback) { | 852 BypassedBytesClientSideFallback) { |
855 struct TestCase { | 853 struct TestCase { |
856 const char* histogram_name; | 854 const char* histogram_name; |
857 const char* initial_response_headers; | 855 const char* initial_response_headers; |
858 }; | 856 }; |
859 const TestCase test_cases[] = { | 857 const TestCase test_cases[] = { |
860 { "DataReductionProxy.BypassedBytes.MissingViaHeader4xx", | 858 { "DataReductionProxy.BypassedBytes.MissingViaHeader4xx", |
861 "HTTP/1.1 414 Request-URI Too Long\r\n\r\n", | 859 "HTTP/1.1 414 Request-URI Too Long\r\n\r\n", |
862 }, | 860 }, |
863 { "DataReductionProxy.BypassedBytes.MissingViaHeaderOther", | 861 { "DataReductionProxy.BypassedBytes.MissingViaHeaderOther", |
(...skipping 29 matching lines...) Expand all Loading... |
893 histogram_tester.ExpectTotalCount(test_case.histogram_name, 2); | 891 histogram_tester.ExpectTotalCount(test_case.histogram_name, 2); |
894 histogram_tester.ExpectBucketCount(test_case.histogram_name, kBody.size(), | 892 histogram_tester.ExpectBucketCount(test_case.histogram_name, kBody.size(), |
895 1); | 893 1); |
896 histogram_tester.ExpectBucketCount(test_case.histogram_name, | 894 histogram_tester.ExpectBucketCount(test_case.histogram_name, |
897 kNextBody.size(), 1); | 895 kNextBody.size(), 1); |
898 ExpectOtherBypassedBytesHistogramsEmpty(histogram_tester, | 896 ExpectOtherBypassedBytesHistogramsEmpty(histogram_tester, |
899 test_case.histogram_name); | 897 test_case.histogram_name); |
900 } | 898 } |
901 } | 899 } |
902 | 900 |
903 TEST_F(DataReductionProxyUsageStatsEndToEndTest, BypassedBytesNetErrorOther) { | 901 TEST_F(DataReductionProxyBypassStatsEndToEndTest, BypassedBytesNetErrorOther) { |
904 // Make the data reduction proxy host fail to resolve. | 902 // Make the data reduction proxy host fail to resolve. |
905 scoped_ptr<net::MockHostResolver> host_resolver(new net::MockHostResolver()); | 903 scoped_ptr<net::MockHostResolver> host_resolver(new net::MockHostResolver()); |
906 host_resolver->rules()->AddSimulatedFailure( | 904 host_resolver->rules()->AddSimulatedFailure( |
907 config()->test_params()->origin().host_port_pair().host()); | 905 config()->test_params()->origin().host_port_pair().host()); |
908 set_host_resolver(host_resolver.get()); | 906 set_host_resolver(host_resolver.get()); |
909 InitializeContext(); | 907 InitializeContext(); |
910 | 908 |
911 base::HistogramTester histogram_tester; | 909 base::HistogramTester histogram_tester; |
912 CreateAndExecuteRequest(GURL("http://foo.com"), "HTTP/1.1 200 OK\r\n\r\n", | 910 CreateAndExecuteRequest(GURL("http://foo.com"), "HTTP/1.1 200 OK\r\n\r\n", |
913 kBody.c_str(), nullptr, nullptr); | 911 kBody.c_str(), nullptr, nullptr); |
914 | 912 |
915 histogram_tester.ExpectUniqueSample( | 913 histogram_tester.ExpectUniqueSample( |
916 "DataReductionProxy.BypassedBytes.NetworkErrorOther", kBody.size(), 1); | 914 "DataReductionProxy.BypassedBytes.NetworkErrorOther", kBody.size(), 1); |
917 ExpectOtherBypassedBytesHistogramsEmpty( | 915 ExpectOtherBypassedBytesHistogramsEmpty( |
918 histogram_tester, "DataReductionProxy.BypassedBytes.NetworkErrorOther"); | 916 histogram_tester, "DataReductionProxy.BypassedBytes.NetworkErrorOther"); |
919 histogram_tester.ExpectUniqueSample( | 917 histogram_tester.ExpectUniqueSample( |
920 "DataReductionProxy.BypassOnNetworkErrorPrimary", | 918 "DataReductionProxy.BypassOnNetworkErrorPrimary", |
921 -net::ERR_PROXY_CONNECTION_FAILED, 1); | 919 -net::ERR_PROXY_CONNECTION_FAILED, 1); |
922 } | 920 } |
923 | 921 |
924 } // namespace data_reduction_proxy | 922 } // namespace data_reduction_proxy |
OLD | NEW |