| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/safe_browsing/certificate_reporting_service_test_utils.
h" | 5 #include "chrome/browser/safe_browsing/certificate_reporting_service_test_utils.
h" |
| 6 | 6 |
| 7 #include "base/threading/thread_task_runner_handle.h" | 7 #include "base/threading/thread_task_runner_handle.h" |
| 8 #include "components/certificate_reporting/encrypted_cert_logger.pb.h" | 8 #include "components/certificate_reporting/encrypted_cert_logger.pb.h" |
| 9 #include "components/certificate_reporting/error_report.h" | 9 #include "components/certificate_reporting/error_report.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 return; | 205 return; |
| 206 } | 206 } |
| 207 if (should_fail_) { | 207 if (should_fail_) { |
| 208 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 208 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 209 net::ERR_SSL_PROTOCOL_ERROR)); | 209 net::ERR_SSL_PROTOCOL_ERROR)); |
| 210 return; | 210 return; |
| 211 } | 211 } |
| 212 // Start reading asynchronously as would a normal network request. | 212 // Start reading asynchronously as would a normal network request. |
| 213 base::ThreadTaskRunnerHandle::Get()->PostTask( | 213 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 214 FROM_HERE, | 214 FROM_HERE, |
| 215 base::Bind(&DelayableCertReportURLRequestJob::NotifyHeadersComplete, | 215 base::BindOnce(&DelayableCertReportURLRequestJob::NotifyHeadersComplete, |
| 216 weak_factory_.GetWeakPtr())); | 216 weak_factory_.GetWeakPtr())); |
| 217 } | 217 } |
| 218 | 218 |
| 219 CertReportJobInterceptor::CertReportJobInterceptor( | 219 CertReportJobInterceptor::CertReportJobInterceptor( |
| 220 ReportSendingResult expected_report_result, | 220 ReportSendingResult expected_report_result, |
| 221 const uint8_t* server_private_key) | 221 const uint8_t* server_private_key) |
| 222 : expected_report_result_(expected_report_result), | 222 : expected_report_result_(expected_report_result), |
| 223 server_private_key_(server_private_key), | 223 server_private_key_(server_private_key), |
| 224 weak_factory_(this) {} | 224 weak_factory_(this) {} |
| 225 | 225 |
| 226 CertReportJobInterceptor::~CertReportJobInterceptor() {} | 226 CertReportJobInterceptor::~CertReportJobInterceptor() {} |
| 227 | 227 |
| 228 net::URLRequestJob* CertReportJobInterceptor::MaybeInterceptRequest( | 228 net::URLRequestJob* CertReportJobInterceptor::MaybeInterceptRequest( |
| 229 net::URLRequest* request, | 229 net::URLRequest* request, |
| 230 net::NetworkDelegate* network_delegate) const { | 230 net::NetworkDelegate* network_delegate) const { |
| 231 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 231 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 232 | 232 |
| 233 const std::string serialized_report = | 233 const std::string serialized_report = |
| 234 GetReportContents(request, server_private_key_); | 234 GetReportContents(request, server_private_key_); |
| 235 content::BrowserThread::PostTask( | 235 content::BrowserThread::PostTask( |
| 236 content::BrowserThread::UI, FROM_HERE, | 236 content::BrowserThread::UI, FROM_HERE, |
| 237 base::Bind(&CertReportJobInterceptor::RequestCreated, | 237 base::BindOnce(&CertReportJobInterceptor::RequestCreated, |
| 238 weak_factory_.GetWeakPtr(), serialized_report, | 238 weak_factory_.GetWeakPtr(), serialized_report, |
| 239 expected_report_result_)); | 239 expected_report_result_)); |
| 240 | 240 |
| 241 if (expected_report_result_ == REPORTS_FAIL) { | 241 if (expected_report_result_ == REPORTS_FAIL) { |
| 242 return new DelayableCertReportURLRequestJob( | 242 return new DelayableCertReportURLRequestJob( |
| 243 false, true, request, network_delegate, | 243 false, true, request, network_delegate, |
| 244 base::Bind(&CertReportJobInterceptor::RequestDestructed, | 244 base::Bind(&CertReportJobInterceptor::RequestDestructed, |
| 245 base::Unretained(this), serialized_report, | 245 base::Unretained(this), serialized_report, |
| 246 expected_report_result_)); | 246 expected_report_result_)); |
| 247 | 247 |
| 248 } else if (expected_report_result_ == REPORTS_DELAY) { | 248 } else if (expected_report_result_ == REPORTS_DELAY) { |
| 249 DCHECK(!delayed_request_) << "Supports only one delayed request at a time"; | 249 DCHECK(!delayed_request_) << "Supports only one delayed request at a time"; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 262 base::Bind(&CertReportJobInterceptor::RequestDestructed, | 262 base::Bind(&CertReportJobInterceptor::RequestDestructed, |
| 263 base::Unretained(this), serialized_report, | 263 base::Unretained(this), serialized_report, |
| 264 expected_report_result_)); | 264 expected_report_result_)); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void CertReportJobInterceptor::SetFailureMode( | 267 void CertReportJobInterceptor::SetFailureMode( |
| 268 ReportSendingResult expected_report_result) { | 268 ReportSendingResult expected_report_result) { |
| 269 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 269 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 270 content::BrowserThread::PostTask( | 270 content::BrowserThread::PostTask( |
| 271 content::BrowserThread::IO, FROM_HERE, | 271 content::BrowserThread::IO, FROM_HERE, |
| 272 base::Bind(&CertReportJobInterceptor::SetFailureModeOnIOThread, | 272 base::BindOnce(&CertReportJobInterceptor::SetFailureModeOnIOThread, |
| 273 weak_factory_.GetWeakPtr(), expected_report_result)); | 273 weak_factory_.GetWeakPtr(), expected_report_result)); |
| 274 } | 274 } |
| 275 | 275 |
| 276 void CertReportJobInterceptor::Resume() { | 276 void CertReportJobInterceptor::Resume() { |
| 277 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 277 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 278 content::BrowserThread::PostTask( | 278 content::BrowserThread::PostTask( |
| 279 content::BrowserThread::IO, FROM_HERE, | 279 content::BrowserThread::IO, FROM_HERE, |
| 280 base::Bind(&CertReportJobInterceptor::ResumeOnIOThread, | 280 base::BindOnce(&CertReportJobInterceptor::ResumeOnIOThread, |
| 281 base::Unretained(this))); | 281 base::Unretained(this))); |
| 282 } | 282 } |
| 283 | 283 |
| 284 RequestObserver* CertReportJobInterceptor::request_created_observer() const { | 284 RequestObserver* CertReportJobInterceptor::request_created_observer() const { |
| 285 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 285 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 286 return &request_created_observer_; | 286 return &request_created_observer_; |
| 287 } | 287 } |
| 288 | 288 |
| 289 RequestObserver* CertReportJobInterceptor::request_destroyed_observer() const { | 289 RequestObserver* CertReportJobInterceptor::request_destroyed_observer() const { |
| 290 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 290 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 291 return &request_destroyed_observer_; | 291 return &request_destroyed_observer_; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 | 381 |
| 382 CertificateReportingServiceTestHelper:: | 382 CertificateReportingServiceTestHelper:: |
| 383 ~CertificateReportingServiceTestHelper() {} | 383 ~CertificateReportingServiceTestHelper() {} |
| 384 | 384 |
| 385 void CertificateReportingServiceTestHelper::SetUpInterceptor() { | 385 void CertificateReportingServiceTestHelper::SetUpInterceptor() { |
| 386 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 386 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 387 url_request_interceptor_ = | 387 url_request_interceptor_ = |
| 388 new CertReportJobInterceptor(REPORTS_FAIL, server_private_key_); | 388 new CertReportJobInterceptor(REPORTS_FAIL, server_private_key_); |
| 389 content::BrowserThread::PostTask( | 389 content::BrowserThread::PostTask( |
| 390 content::BrowserThread::IO, FROM_HERE, | 390 content::BrowserThread::IO, FROM_HERE, |
| 391 base::Bind(&SetUpURLHandlersOnIOThread, | 391 base::BindOnce(&SetUpURLHandlersOnIOThread, |
| 392 base::Passed(std::unique_ptr<net::URLRequestInterceptor>( | 392 base::Passed(std::unique_ptr<net::URLRequestInterceptor>( |
| 393 url_request_interceptor_)))); | 393 url_request_interceptor_)))); |
| 394 } | 394 } |
| 395 | 395 |
| 396 void CertificateReportingServiceTestHelper::SetFailureMode( | 396 void CertificateReportingServiceTestHelper::SetFailureMode( |
| 397 ReportSendingResult expected_report_result) { | 397 ReportSendingResult expected_report_result) { |
| 398 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 398 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 399 url_request_interceptor_->SetFailureMode(expected_report_result); | 399 url_request_interceptor_->SetFailureMode(expected_report_result); |
| 400 } | 400 } |
| 401 | 401 |
| 402 void CertificateReportingServiceTestHelper::ResumeDelayedRequest() { | 402 void CertificateReportingServiceTestHelper::ResumeDelayedRequest() { |
| 403 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 403 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 | 438 |
| 439 if (service->GetReporterForTesting()) { | 439 if (service->GetReporterForTesting()) { |
| 440 // Reporter can be null if reporting is disabled. | 440 // Reporter can be null if reporting is disabled. |
| 441 EXPECT_EQ( | 441 EXPECT_EQ( |
| 442 0u, | 442 0u, |
| 443 service->GetReporterForTesting()->inflight_report_count_for_testing()); | 443 service->GetReporterForTesting()->inflight_report_count_for_testing()); |
| 444 } | 444 } |
| 445 } | 445 } |
| 446 | 446 |
| 447 } // namespace certificate_reporting_test_utils | 447 } // namespace certificate_reporting_test_utils |
| OLD | NEW |