Chromium Code Reviews| 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 |
| 71 ObservedReport::ObservedReport(const std::string& hostname, bool is_retried) | |
| 72 : hostname(hostname), is_retried(is_retried) {} | |
| 73 | |
| 74 // static | |
| 75 ObservedReport ObservedReport::Retried(const std::string& hostname) { | |
| 76 return ObservedReport(hostname, true); | |
| 77 } | |
| 78 | |
| 79 // static | |
| 80 ObservedReport ObservedReport::NotRetried(const std::string& hostname) { | |
| 81 return ObservedReport(hostname, false); | |
| 82 } | |
| 83 | |
| 84 bool ObservedReport::operator<(const ObservedReport& other) const { | |
|
estark
2017/01/13 23:26:22
Shouldn't these compare is_retried as well? (If th
meacer
2017/01/17 22:24:27
This was keying on the hostname, leaving is_retrie
| |
| 85 return hostname < other.hostname; | |
| 86 } | |
| 87 | |
| 88 bool ObservedReport::operator==(const ObservedReport& other) const { | |
| 89 return hostname == other.hostname; | |
| 90 } | |
| 91 | |
| 89 RequestObserver::RequestObserver() | 92 RequestObserver::RequestObserver() |
| 90 : num_events_to_wait_for_(0u), num_received_events_(0u) {} | 93 : num_events_to_wait_for_(0u), num_received_events_(0u) {} |
| 91 RequestObserver::~RequestObserver() {} | 94 RequestObserver::~RequestObserver() {} |
| 92 | 95 |
| 93 void RequestObserver::Wait(unsigned int num_events_to_wait_for) { | 96 void RequestObserver::Wait(unsigned int num_events_to_wait_for) { |
| 94 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 97 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 95 DCHECK(!run_loop_); | 98 DCHECK(!run_loop_); |
| 96 ASSERT_LE(num_received_events_, num_events_to_wait_for) | 99 ASSERT_LE(num_received_events_, num_events_to_wait_for) |
| 97 << "Observed unexpected report"; | 100 << "Observed unexpected report"; |
| 98 | 101 |
| 99 if (num_received_events_ < num_events_to_wait_for) { | 102 if (num_received_events_ < num_events_to_wait_for) { |
| 100 num_events_to_wait_for_ = num_events_to_wait_for; | 103 num_events_to_wait_for_ = num_events_to_wait_for; |
| 101 run_loop_.reset(new base::RunLoop()); | 104 run_loop_.reset(new base::RunLoop()); |
| 102 run_loop_->Run(); | 105 run_loop_->Run(); |
| 103 run_loop_.reset(nullptr); | 106 run_loop_.reset(nullptr); |
| 104 EXPECT_EQ(0u, num_received_events_); | 107 EXPECT_EQ(0u, num_received_events_); |
| 105 EXPECT_EQ(0u, num_events_to_wait_for_); | 108 EXPECT_EQ(0u, num_events_to_wait_for_); |
| 106 } else if (num_received_events_ == num_events_to_wait_for) { | 109 } else if (num_received_events_ == num_events_to_wait_for) { |
| 107 num_received_events_ = 0u; | 110 num_received_events_ = 0u; |
| 108 num_events_to_wait_for_ = 0u; | 111 num_events_to_wait_for_ = 0u; |
| 109 } | 112 } |
| 110 } | 113 } |
| 111 | 114 |
| 112 void RequestObserver::OnRequest(const std::string& serialized_report, | 115 void RequestObserver::OnRequest(const ObservedReport& observed_report, |
| 113 ReportSendingResult report_type) { | 116 ReportSendingResult report_type) { |
| 114 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 117 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 115 switch (report_type) { | 118 switch (report_type) { |
| 116 case REPORTS_SUCCESSFUL: | 119 case REPORTS_SUCCESSFUL: |
| 117 successful_reports_.insert(serialized_report); | 120 successful_reports_.insert(observed_report); |
| 118 break; | 121 break; |
| 119 case REPORTS_FAIL: | 122 case REPORTS_FAIL: |
| 120 failed_reports_.insert(serialized_report); | 123 failed_reports_.insert(observed_report); |
| 121 break; | 124 break; |
| 122 case REPORTS_DELAY: | 125 case REPORTS_DELAY: |
| 123 delayed_reports_.insert(serialized_report); | 126 delayed_reports_.insert(observed_report); |
| 124 break; | 127 break; |
| 125 } | 128 } |
| 126 | 129 |
| 127 num_received_events_++; | 130 num_received_events_++; |
| 128 if (!run_loop_) { | 131 if (!run_loop_) { |
| 129 return; | 132 return; |
| 130 } | 133 } |
| 131 ASSERT_LE(num_received_events_, num_events_to_wait_for_) | 134 ASSERT_LE(num_received_events_, num_events_to_wait_for_) |
| 132 << "Observed unexpected report"; | 135 << "Observed unexpected report"; |
| 133 | 136 |
| 134 if (num_received_events_ == num_events_to_wait_for_) { | 137 if (num_received_events_ == num_events_to_wait_for_) { |
| 135 num_events_to_wait_for_ = 0u; | 138 num_events_to_wait_for_ = 0u; |
| 136 num_received_events_ = 0u; | 139 num_received_events_ = 0u; |
| 137 run_loop_->Quit(); | 140 run_loop_->Quit(); |
| 138 } | 141 } |
| 139 } | 142 } |
| 140 | 143 |
| 141 const std::set<std::string>& RequestObserver::successful_reports() const { | 144 const std::set<ObservedReport>& RequestObserver::successful_reports() const { |
| 142 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 145 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 143 return successful_reports_; | 146 return successful_reports_; |
| 144 } | 147 } |
| 145 | 148 |
| 146 const std::set<std::string>& RequestObserver::failed_reports() const { | 149 const std::set<ObservedReport>& RequestObserver::failed_reports() const { |
| 147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 150 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 148 return failed_reports_; | 151 return failed_reports_; |
| 149 } | 152 } |
| 150 | 153 |
| 151 const std::set<std::string>& RequestObserver::delayed_reports() const { | 154 const std::set<ObservedReport>& RequestObserver::delayed_reports() const { |
| 152 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 155 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 153 return delayed_reports_; | 156 return delayed_reports_; |
| 154 } | 157 } |
| 155 | 158 |
| 156 void RequestObserver::ClearObservedReports() { | 159 void RequestObserver::ClearObservedReports() { |
| 157 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 160 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 158 successful_reports_.clear(); | 161 successful_reports_.clear(); |
| 159 failed_reports_.clear(); | 162 failed_reports_.clear(); |
| 160 delayed_reports_.clear(); | 163 delayed_reports_.clear(); |
| 161 } | 164 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 server_private_key_(server_private_key), | 238 server_private_key_(server_private_key), |
| 236 weak_factory_(this) {} | 239 weak_factory_(this) {} |
| 237 | 240 |
| 238 CertReportJobInterceptor::~CertReportJobInterceptor() {} | 241 CertReportJobInterceptor::~CertReportJobInterceptor() {} |
| 239 | 242 |
| 240 net::URLRequestJob* CertReportJobInterceptor::MaybeInterceptRequest( | 243 net::URLRequestJob* CertReportJobInterceptor::MaybeInterceptRequest( |
| 241 net::URLRequest* request, | 244 net::URLRequest* request, |
| 242 net::NetworkDelegate* network_delegate) const { | 245 net::NetworkDelegate* network_delegate) const { |
| 243 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 246 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 244 | 247 |
| 245 const std::string uploaded_report = | 248 const std::string serialized_report = |
| 246 GetReportContents(request, server_private_key_); | 249 GetReportContents(request, server_private_key_); |
| 247 content::BrowserThread::PostTask( | 250 content::BrowserThread::PostTask( |
| 248 content::BrowserThread::UI, FROM_HERE, | 251 content::BrowserThread::UI, FROM_HERE, |
| 249 base::Bind(&CertReportJobInterceptor::RequestCreated, | 252 base::Bind(&CertReportJobInterceptor::RequestCreated, |
| 250 weak_factory_.GetWeakPtr(), uploaded_report, | 253 weak_factory_.GetWeakPtr(), serialized_report, |
| 251 expected_report_result_)); | 254 expected_report_result_)); |
| 252 | 255 |
| 253 if (expected_report_result_ == REPORTS_FAIL) { | 256 if (expected_report_result_ == REPORTS_FAIL) { |
| 254 return new DelayableCertReportURLRequestJob( | 257 return new DelayableCertReportURLRequestJob( |
| 255 false, true, request, network_delegate, | 258 false, true, request, network_delegate, |
| 256 base::Bind(&CertReportJobInterceptor::RequestDestructed, | 259 base::Bind(&CertReportJobInterceptor::RequestDestructed, |
| 257 base::Unretained(this), uploaded_report, | 260 base::Unretained(this), serialized_report, |
| 258 expected_report_result_)); | 261 expected_report_result_)); |
| 259 | 262 |
| 260 } else if (expected_report_result_ == REPORTS_DELAY) { | 263 } else if (expected_report_result_ == REPORTS_DELAY) { |
| 261 DCHECK(!delayed_request_) << "Supports only one delayed request at a time"; | 264 DCHECK(!delayed_request_) << "Supports only one delayed request at a time"; |
| 262 DelayableCertReportURLRequestJob* job = | 265 DelayableCertReportURLRequestJob* job = |
| 263 new DelayableCertReportURLRequestJob( | 266 new DelayableCertReportURLRequestJob( |
| 264 true, false, request, network_delegate, | 267 true, false, request, network_delegate, |
| 265 base::Bind(&CertReportJobInterceptor::RequestDestructed, | 268 base::Bind(&CertReportJobInterceptor::RequestDestructed, |
| 266 base::Unretained(this), uploaded_report, | 269 base::Unretained(this), serialized_report, |
| 267 expected_report_result_)); | 270 expected_report_result_)); |
| 268 delayed_request_ = job->GetWeakPtr(); | 271 delayed_request_ = job->GetWeakPtr(); |
| 269 return job; | 272 return job; |
| 270 } | 273 } |
| 271 // Successful url request job. | 274 // Successful url request job. |
| 272 return new DelayableCertReportURLRequestJob( | 275 return new DelayableCertReportURLRequestJob( |
| 273 false, false, request, network_delegate, | 276 false, false, request, network_delegate, |
| 274 base::Bind(&CertReportJobInterceptor::RequestDestructed, | 277 base::Bind(&CertReportJobInterceptor::RequestDestructed, |
| 275 base::Unretained(this), uploaded_report, | 278 base::Unretained(this), serialized_report, |
| 276 expected_report_result_)); | 279 expected_report_result_)); |
| 277 } | 280 } |
| 278 | 281 |
| 279 void CertReportJobInterceptor::SetFailureMode( | 282 void CertReportJobInterceptor::SetFailureMode( |
| 280 ReportSendingResult expected_report_result) { | 283 ReportSendingResult expected_report_result) { |
| 281 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 284 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 282 content::BrowserThread::PostTask( | 285 content::BrowserThread::PostTask( |
| 283 content::BrowserThread::IO, FROM_HERE, | 286 content::BrowserThread::IO, FROM_HERE, |
| 284 base::Bind(&CertReportJobInterceptor::SetFailureModeOnIOThread, | 287 base::Bind(&CertReportJobInterceptor::SetFailureModeOnIOThread, |
| 285 weak_factory_.GetWeakPtr(), expected_report_result)); | 288 weak_factory_.GetWeakPtr(), expected_report_result)); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 310 } | 313 } |
| 311 | 314 |
| 312 void CertReportJobInterceptor::ResumeOnIOThread() { | 315 void CertReportJobInterceptor::ResumeOnIOThread() { |
| 313 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 316 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 314 EXPECT_EQ(REPORTS_DELAY, expected_report_result_); | 317 EXPECT_EQ(REPORTS_DELAY, expected_report_result_); |
| 315 if (delayed_request_) | 318 if (delayed_request_) |
| 316 delayed_request_->Resume(); | 319 delayed_request_->Resume(); |
| 317 } | 320 } |
| 318 | 321 |
| 319 void CertReportJobInterceptor::RequestCreated( | 322 void CertReportJobInterceptor::RequestCreated( |
| 320 const std::string& uploaded_report, | 323 const std::string& serialized_report, |
| 321 ReportSendingResult expected_report_result) const { | 324 ReportSendingResult expected_report_result) const { |
| 322 request_created_observer_.OnRequest(uploaded_report, expected_report_result); | 325 certificate_reporting::ErrorReport error_report; |
| 326 EXPECT_TRUE(error_report.InitializeFromString(serialized_report)); | |
| 327 request_created_observer_.OnRequest( | |
| 328 ObservedReport(error_report.hostname(), error_report.is_retry_upload()), | |
| 329 expected_report_result); | |
| 323 } | 330 } |
| 324 | 331 |
| 325 void CertReportJobInterceptor::RequestDestructed( | 332 void CertReportJobInterceptor::RequestDestructed( |
| 326 const std::string& uploaded_report, | 333 const std::string& serialized_report, |
| 327 ReportSendingResult expected_report_result) const { | 334 ReportSendingResult expected_report_result) const { |
| 328 request_destroyed_observer_.OnRequest(uploaded_report, | 335 certificate_reporting::ErrorReport error_report; |
| 329 expected_report_result); | 336 EXPECT_TRUE(error_report.InitializeFromString(serialized_report)); |
| 337 request_destroyed_observer_.OnRequest( | |
| 338 ObservedReport(error_report.hostname(), error_report.is_retry_upload()), | |
| 339 expected_report_result); | |
| 330 } | 340 } |
| 331 | 341 |
| 332 ReportExpectation::ReportExpectation() {} | 342 ReportExpectation::ReportExpectation() {} |
| 333 | 343 |
| 334 ReportExpectation::ReportExpectation(const ReportExpectation& other) = default; | 344 ReportExpectation::ReportExpectation(const ReportExpectation& other) = default; |
| 335 | 345 |
| 336 ReportExpectation::~ReportExpectation() {} | 346 ReportExpectation::~ReportExpectation() {} |
| 337 | 347 |
| 338 // static | 348 // static |
| 339 ReportExpectation ReportExpectation::Successful( | 349 ReportExpectation ReportExpectation::Successful( |
| 340 const std::set<std::string>& reports) { | 350 const std::set<ObservedReport>& reports) { |
| 341 ReportExpectation expectation; | 351 ReportExpectation expectation; |
| 342 expectation.successful_reports = reports; | 352 expectation.successful_reports = reports; |
| 343 return expectation; | 353 return expectation; |
| 344 } | 354 } |
| 345 | 355 |
| 346 // static | 356 // static |
| 347 ReportExpectation ReportExpectation::Failed( | 357 ReportExpectation ReportExpectation::Failed( |
| 348 const std::set<std::string>& reports) { | 358 const std::set<ObservedReport>& reports) { |
| 349 ReportExpectation expectation; | 359 ReportExpectation expectation; |
| 350 expectation.failed_reports = reports; | 360 expectation.failed_reports = reports; |
| 351 return expectation; | 361 return expectation; |
| 352 } | 362 } |
| 353 | 363 |
| 354 // static | 364 // static |
| 355 ReportExpectation ReportExpectation::Delayed( | 365 ReportExpectation ReportExpectation::Delayed( |
| 356 const std::set<std::string>& reports) { | 366 const std::set<ObservedReport>& reports) { |
| 357 ReportExpectation expectation; | 367 ReportExpectation expectation; |
| 358 expectation.delayed_reports = reports; | 368 expectation.delayed_reports = reports; |
| 359 return expectation; | 369 return expectation; |
| 360 } | 370 } |
| 361 | 371 |
| 362 int ReportExpectation::num_reports() const { | 372 int ReportExpectation::num_reports() const { |
| 363 return successful_reports.size() + failed_reports.size() + | 373 return successful_reports.size() + failed_reports.size() + |
| 364 delayed_reports.size(); | 374 delayed_reports.size(); |
| 365 } | 375 } |
| 366 | 376 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 451 | 461 |
| 452 if (service->GetReporterForTesting()) { | 462 if (service->GetReporterForTesting()) { |
| 453 // Reporter can be null if reporting is disabled. | 463 // Reporter can be null if reporting is disabled. |
| 454 EXPECT_EQ( | 464 EXPECT_EQ( |
| 455 0u, | 465 0u, |
| 456 service->GetReporterForTesting()->inflight_report_count_for_testing()); | 466 service->GetReporterForTesting()->inflight_report_count_for_testing()); |
| 457 } | 467 } |
| 458 } | 468 } |
| 459 | 469 |
| 460 } // namespace certificate_reporting_test_utils | 470 } // namespace certificate_reporting_test_utils |
| OLD | NEW |