| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 const uint8_t* server_private_key) { | 42 const uint8_t* server_private_key) { |
| 43 std::string serialized_report(GetUploadData(request)); | 43 std::string serialized_report(GetUploadData(request)); |
| 44 certificate_reporting::EncryptedCertLoggerRequest encrypted_request; | 44 certificate_reporting::EncryptedCertLoggerRequest encrypted_request; |
| 45 EXPECT_TRUE(encrypted_request.ParseFromString(serialized_report)); | 45 EXPECT_TRUE(encrypted_request.ParseFromString(serialized_report)); |
| 46 EXPECT_EQ(kServerPublicKeyTestVersion, | 46 EXPECT_EQ(kServerPublicKeyTestVersion, |
| 47 encrypted_request.server_public_key_version()); | 47 encrypted_request.server_public_key_version()); |
| 48 EXPECT_EQ(certificate_reporting::EncryptedCertLoggerRequest:: | 48 EXPECT_EQ(certificate_reporting::EncryptedCertLoggerRequest:: |
| 49 AEAD_ECDH_AES_128_CTR_HMAC_SHA256, | 49 AEAD_ECDH_AES_128_CTR_HMAC_SHA256, |
| 50 encrypted_request.algorithm()); | 50 encrypted_request.algorithm()); |
| 51 std::string decrypted_report; | 51 std::string decrypted_report; |
| 52 certificate_reporting::ErrorReporter::DecryptErrorReport( | 52 EXPECT_TRUE(certificate_reporting::ErrorReporter::DecryptErrorReport( |
| 53 server_private_key, encrypted_request, &decrypted_report); | 53 server_private_key, encrypted_request, &decrypted_report)); |
| 54 return decrypted_report; | 54 return decrypted_report; |
| 55 } | 55 } |
| 56 | 56 |
| 57 // Checks that the serialized reports in |observed_reports| have the same | |
| 58 // hostnames as |expected_hostnames|. | |
| 59 void CompareHostnames(const std::set<std::string>& expected_hostnames, | |
| 60 const std::set<std::string>& observed_reports, | |
| 61 const std::string& comparison_type) { | |
| 62 std::set<std::string> observed_hostnames; | |
| 63 for (const std::string& serialized_report : observed_reports) { | |
| 64 certificate_reporting::ErrorReport report; | |
| 65 ASSERT_TRUE(report.InitializeFromString(serialized_report)); | |
| 66 observed_hostnames.insert(report.hostname()); | |
| 67 } | |
| 68 EXPECT_EQ(expected_hostnames, observed_hostnames) | |
| 69 << "Comparison failed for " << comparison_type << " reports."; | |
| 70 } | |
| 71 | |
| 72 void WaitReports( | 57 void WaitReports( |
| 73 certificate_reporting_test_utils::RequestObserver* observer, | 58 certificate_reporting_test_utils::RequestObserver* observer, |
| 74 const certificate_reporting_test_utils::ReportExpectation& expectation) { | 59 const certificate_reporting_test_utils::ReportExpectation& expectation) { |
| 75 observer->Wait(expectation.num_reports()); | 60 observer->Wait(expectation.num_reports()); |
| 76 CompareHostnames(expectation.successful_reports, | 61 EXPECT_EQ(expectation.successful_reports, observer->successful_reports()); |
| 77 observer->successful_reports(), "successful"); | 62 EXPECT_EQ(expectation.failed_reports, observer->failed_reports()); |
| 78 CompareHostnames(expectation.failed_reports, observer->failed_reports(), | 63 EXPECT_EQ(expectation.delayed_reports, observer->delayed_reports()); |
| 79 "failed"); | |
| 80 CompareHostnames(expectation.delayed_reports, observer->delayed_reports(), | |
| 81 "delayed"); | |
| 82 observer->ClearObservedReports(); | 64 observer->ClearObservedReports(); |
| 83 } | 65 } |
| 84 | 66 |
| 85 } // namespace | 67 } // namespace |
| 86 | 68 |
| 87 namespace certificate_reporting_test_utils { | 69 namespace certificate_reporting_test_utils { |
| 88 | 70 |
| 89 RequestObserver::RequestObserver() | 71 RequestObserver::RequestObserver() |
| 90 : num_events_to_wait_for_(0u), num_received_events_(0u) {} | 72 : num_events_to_wait_for_(0u), num_received_events_(0u) {} |
| 91 RequestObserver::~RequestObserver() {} | 73 RequestObserver::~RequestObserver() {} |
| (...skipping 13 matching lines...) Expand all Loading... |
| 105 EXPECT_EQ(0u, num_events_to_wait_for_); | 87 EXPECT_EQ(0u, num_events_to_wait_for_); |
| 106 } else if (num_received_events_ == num_events_to_wait_for) { | 88 } else if (num_received_events_ == num_events_to_wait_for) { |
| 107 num_received_events_ = 0u; | 89 num_received_events_ = 0u; |
| 108 num_events_to_wait_for_ = 0u; | 90 num_events_to_wait_for_ = 0u; |
| 109 } | 91 } |
| 110 } | 92 } |
| 111 | 93 |
| 112 void RequestObserver::OnRequest(const std::string& serialized_report, | 94 void RequestObserver::OnRequest(const std::string& serialized_report, |
| 113 ReportSendingResult report_type) { | 95 ReportSendingResult report_type) { |
| 114 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 97 certificate_reporting::ErrorReport report; |
| 98 EXPECT_TRUE(report.InitializeFromString(serialized_report)); |
| 99 |
| 115 switch (report_type) { | 100 switch (report_type) { |
| 116 case REPORTS_SUCCESSFUL: | 101 case REPORTS_SUCCESSFUL: |
| 117 successful_reports_.insert(serialized_report); | 102 successful_reports_[report.hostname()] = |
| 103 report.is_retry_upload() ? RETRIED : NOT_RETRIED; |
| 118 break; | 104 break; |
| 119 case REPORTS_FAIL: | 105 case REPORTS_FAIL: |
| 120 failed_reports_.insert(serialized_report); | 106 failed_reports_[report.hostname()] = |
| 107 report.is_retry_upload() ? RETRIED : NOT_RETRIED; |
| 121 break; | 108 break; |
| 122 case REPORTS_DELAY: | 109 case REPORTS_DELAY: |
| 123 delayed_reports_.insert(serialized_report); | 110 delayed_reports_[report.hostname()] = |
| 111 report.is_retry_upload() ? RETRIED : NOT_RETRIED; |
| 124 break; | 112 break; |
| 125 } | 113 } |
| 126 | 114 |
| 127 num_received_events_++; | 115 num_received_events_++; |
| 128 if (!run_loop_) { | 116 if (!run_loop_) { |
| 129 return; | 117 return; |
| 130 } | 118 } |
| 131 ASSERT_LE(num_received_events_, num_events_to_wait_for_) | 119 ASSERT_LE(num_received_events_, num_events_to_wait_for_) |
| 132 << "Observed unexpected report"; | 120 << "Observed unexpected report"; |
| 133 | 121 |
| 134 if (num_received_events_ == num_events_to_wait_for_) { | 122 if (num_received_events_ == num_events_to_wait_for_) { |
| 135 num_events_to_wait_for_ = 0u; | 123 num_events_to_wait_for_ = 0u; |
| 136 num_received_events_ = 0u; | 124 num_received_events_ = 0u; |
| 137 run_loop_->Quit(); | 125 run_loop_->Quit(); |
| 138 } | 126 } |
| 139 } | 127 } |
| 140 | 128 |
| 141 const std::set<std::string>& RequestObserver::successful_reports() const { | 129 const ObservedReportMap& RequestObserver::successful_reports() const { |
| 142 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 130 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 143 return successful_reports_; | 131 return successful_reports_; |
| 144 } | 132 } |
| 145 | 133 |
| 146 const std::set<std::string>& RequestObserver::failed_reports() const { | 134 const ObservedReportMap& RequestObserver::failed_reports() const { |
| 147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 135 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 148 return failed_reports_; | 136 return failed_reports_; |
| 149 } | 137 } |
| 150 | 138 |
| 151 const std::set<std::string>& RequestObserver::delayed_reports() const { | 139 const ObservedReportMap& RequestObserver::delayed_reports() const { |
| 152 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 140 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 153 return delayed_reports_; | 141 return delayed_reports_; |
| 154 } | 142 } |
| 155 | 143 |
| 156 void RequestObserver::ClearObservedReports() { | 144 void RequestObserver::ClearObservedReports() { |
| 157 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 145 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 158 successful_reports_.clear(); | 146 successful_reports_.clear(); |
| 159 failed_reports_.clear(); | 147 failed_reports_.clear(); |
| 160 delayed_reports_.clear(); | 148 delayed_reports_.clear(); |
| 161 } | 149 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 server_private_key_(server_private_key), | 223 server_private_key_(server_private_key), |
| 236 weak_factory_(this) {} | 224 weak_factory_(this) {} |
| 237 | 225 |
| 238 CertReportJobInterceptor::~CertReportJobInterceptor() {} | 226 CertReportJobInterceptor::~CertReportJobInterceptor() {} |
| 239 | 227 |
| 240 net::URLRequestJob* CertReportJobInterceptor::MaybeInterceptRequest( | 228 net::URLRequestJob* CertReportJobInterceptor::MaybeInterceptRequest( |
| 241 net::URLRequest* request, | 229 net::URLRequest* request, |
| 242 net::NetworkDelegate* network_delegate) const { | 230 net::NetworkDelegate* network_delegate) const { |
| 243 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 231 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 244 | 232 |
| 245 const std::string uploaded_report = | 233 const std::string serialized_report = |
| 246 GetReportContents(request, server_private_key_); | 234 GetReportContents(request, server_private_key_); |
| 247 content::BrowserThread::PostTask( | 235 content::BrowserThread::PostTask( |
| 248 content::BrowserThread::UI, FROM_HERE, | 236 content::BrowserThread::UI, FROM_HERE, |
| 249 base::Bind(&CertReportJobInterceptor::RequestCreated, | 237 base::Bind(&CertReportJobInterceptor::RequestCreated, |
| 250 weak_factory_.GetWeakPtr(), uploaded_report, | 238 weak_factory_.GetWeakPtr(), serialized_report, |
| 251 expected_report_result_)); | 239 expected_report_result_)); |
| 252 | 240 |
| 253 if (expected_report_result_ == REPORTS_FAIL) { | 241 if (expected_report_result_ == REPORTS_FAIL) { |
| 254 return new DelayableCertReportURLRequestJob( | 242 return new DelayableCertReportURLRequestJob( |
| 255 false, true, request, network_delegate, | 243 false, true, request, network_delegate, |
| 256 base::Bind(&CertReportJobInterceptor::RequestDestructed, | 244 base::Bind(&CertReportJobInterceptor::RequestDestructed, |
| 257 base::Unretained(this), uploaded_report, | 245 base::Unretained(this), serialized_report, |
| 258 expected_report_result_)); | 246 expected_report_result_)); |
| 259 | 247 |
| 260 } else if (expected_report_result_ == REPORTS_DELAY) { | 248 } else if (expected_report_result_ == REPORTS_DELAY) { |
| 261 DCHECK(!delayed_request_) << "Supports only one delayed request at a time"; | 249 DCHECK(!delayed_request_) << "Supports only one delayed request at a time"; |
| 262 DelayableCertReportURLRequestJob* job = | 250 DelayableCertReportURLRequestJob* job = |
| 263 new DelayableCertReportURLRequestJob( | 251 new DelayableCertReportURLRequestJob( |
| 264 true, false, request, network_delegate, | 252 true, false, request, network_delegate, |
| 265 base::Bind(&CertReportJobInterceptor::RequestDestructed, | 253 base::Bind(&CertReportJobInterceptor::RequestDestructed, |
| 266 base::Unretained(this), uploaded_report, | 254 base::Unretained(this), serialized_report, |
| 267 expected_report_result_)); | 255 expected_report_result_)); |
| 268 delayed_request_ = job->GetWeakPtr(); | 256 delayed_request_ = job->GetWeakPtr(); |
| 269 return job; | 257 return job; |
| 270 } | 258 } |
| 271 // Successful url request job. | 259 // Successful url request job. |
| 272 return new DelayableCertReportURLRequestJob( | 260 return new DelayableCertReportURLRequestJob( |
| 273 false, false, request, network_delegate, | 261 false, false, request, network_delegate, |
| 274 base::Bind(&CertReportJobInterceptor::RequestDestructed, | 262 base::Bind(&CertReportJobInterceptor::RequestDestructed, |
| 275 base::Unretained(this), uploaded_report, | 263 base::Unretained(this), serialized_report, |
| 276 expected_report_result_)); | 264 expected_report_result_)); |
| 277 } | 265 } |
| 278 | 266 |
| 279 void CertReportJobInterceptor::SetFailureMode( | 267 void CertReportJobInterceptor::SetFailureMode( |
| 280 ReportSendingResult expected_report_result) { | 268 ReportSendingResult expected_report_result) { |
| 281 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 269 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 282 content::BrowserThread::PostTask( | 270 content::BrowserThread::PostTask( |
| 283 content::BrowserThread::IO, FROM_HERE, | 271 content::BrowserThread::IO, FROM_HERE, |
| 284 base::Bind(&CertReportJobInterceptor::SetFailureModeOnIOThread, | 272 base::Bind(&CertReportJobInterceptor::SetFailureModeOnIOThread, |
| 285 weak_factory_.GetWeakPtr(), expected_report_result)); | 273 weak_factory_.GetWeakPtr(), expected_report_result)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 310 } | 298 } |
| 311 | 299 |
| 312 void CertReportJobInterceptor::ResumeOnIOThread() { | 300 void CertReportJobInterceptor::ResumeOnIOThread() { |
| 313 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 301 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 314 EXPECT_EQ(REPORTS_DELAY, expected_report_result_); | 302 EXPECT_EQ(REPORTS_DELAY, expected_report_result_); |
| 315 if (delayed_request_) | 303 if (delayed_request_) |
| 316 delayed_request_->Resume(); | 304 delayed_request_->Resume(); |
| 317 } | 305 } |
| 318 | 306 |
| 319 void CertReportJobInterceptor::RequestCreated( | 307 void CertReportJobInterceptor::RequestCreated( |
| 320 const std::string& uploaded_report, | 308 const std::string& serialized_report, |
| 321 ReportSendingResult expected_report_result) const { | 309 ReportSendingResult expected_report_result) const { |
| 322 request_created_observer_.OnRequest(uploaded_report, expected_report_result); | 310 request_created_observer_.OnRequest(serialized_report, |
| 311 expected_report_result); |
| 323 } | 312 } |
| 324 | 313 |
| 325 void CertReportJobInterceptor::RequestDestructed( | 314 void CertReportJobInterceptor::RequestDestructed( |
| 326 const std::string& uploaded_report, | 315 const std::string& serialized_report, |
| 327 ReportSendingResult expected_report_result) const { | 316 ReportSendingResult expected_report_result) const { |
| 328 request_destroyed_observer_.OnRequest(uploaded_report, | 317 request_destroyed_observer_.OnRequest(serialized_report, |
| 329 expected_report_result); | 318 expected_report_result); |
| 330 } | 319 } |
| 331 | 320 |
| 332 ReportExpectation::ReportExpectation() {} | 321 ReportExpectation::ReportExpectation() {} |
| 333 | 322 |
| 334 ReportExpectation::ReportExpectation(const ReportExpectation& other) = default; | 323 ReportExpectation::ReportExpectation(const ReportExpectation& other) = default; |
| 335 | 324 |
| 336 ReportExpectation::~ReportExpectation() {} | 325 ReportExpectation::~ReportExpectation() {} |
| 337 | 326 |
| 338 // static | 327 // static |
| 339 ReportExpectation ReportExpectation::Successful( | 328 ReportExpectation ReportExpectation::Successful( |
| 340 const std::set<std::string>& reports) { | 329 const ObservedReportMap& reports) { |
| 341 ReportExpectation expectation; | 330 ReportExpectation expectation; |
| 342 expectation.successful_reports = reports; | 331 expectation.successful_reports = reports; |
| 343 return expectation; | 332 return expectation; |
| 344 } | 333 } |
| 345 | 334 |
| 346 // static | 335 // static |
| 347 ReportExpectation ReportExpectation::Failed( | 336 ReportExpectation ReportExpectation::Failed(const ObservedReportMap& reports) { |
| 348 const std::set<std::string>& reports) { | |
| 349 ReportExpectation expectation; | 337 ReportExpectation expectation; |
| 350 expectation.failed_reports = reports; | 338 expectation.failed_reports = reports; |
| 351 return expectation; | 339 return expectation; |
| 352 } | 340 } |
| 353 | 341 |
| 354 // static | 342 // static |
| 355 ReportExpectation ReportExpectation::Delayed( | 343 ReportExpectation ReportExpectation::Delayed(const ObservedReportMap& reports) { |
| 356 const std::set<std::string>& reports) { | |
| 357 ReportExpectation expectation; | 344 ReportExpectation expectation; |
| 358 expectation.delayed_reports = reports; | 345 expectation.delayed_reports = reports; |
| 359 return expectation; | 346 return expectation; |
| 360 } | 347 } |
| 361 | 348 |
| 362 int ReportExpectation::num_reports() const { | 349 int ReportExpectation::num_reports() const { |
| 363 return successful_reports.size() + failed_reports.size() + | 350 return successful_reports.size() + failed_reports.size() + |
| 364 delayed_reports.size(); | 351 delayed_reports.size(); |
| 365 } | 352 } |
| 366 | 353 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 | 438 |
| 452 if (service->GetReporterForTesting()) { | 439 if (service->GetReporterForTesting()) { |
| 453 // Reporter can be null if reporting is disabled. | 440 // Reporter can be null if reporting is disabled. |
| 454 EXPECT_EQ( | 441 EXPECT_EQ( |
| 455 0u, | 442 0u, |
| 456 service->GetReporterForTesting()->inflight_report_count_for_testing()); | 443 service->GetReporterForTesting()->inflight_report_count_for_testing()); |
| 457 } | 444 } |
| 458 } | 445 } |
| 459 | 446 |
| 460 } // namespace certificate_reporting_test_utils | 447 } // namespace certificate_reporting_test_utils |
| OLD | NEW |