| 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 "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/test/test_utils.h" | 10 #include "content/public/test/test_utils.h" |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 152 |
| 153 void CertReportJobInterceptor::SetFailureMode( | 153 void CertReportJobInterceptor::SetFailureMode( |
| 154 ReportSendingResult expected_report_result) { | 154 ReportSendingResult expected_report_result) { |
| 155 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 155 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 156 content::BrowserThread::PostTask( | 156 content::BrowserThread::PostTask( |
| 157 content::BrowserThread::IO, FROM_HERE, | 157 content::BrowserThread::IO, FROM_HERE, |
| 158 base::Bind(&CertReportJobInterceptor::SetFailureModeOnIOThread, | 158 base::Bind(&CertReportJobInterceptor::SetFailureModeOnIOThread, |
| 159 weak_factory_.GetWeakPtr(), expected_report_result)); | 159 weak_factory_.GetWeakPtr(), expected_report_result)); |
| 160 } | 160 } |
| 161 | 161 |
| 162 void CertReportJobInterceptor::Resume() { | 162 void CertReportJobInterceptor::Resume(const base::Callback<void()>& callback) { |
| 163 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 163 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 164 content::BrowserThread::PostTask( | 164 content::BrowserThread::PostTaskAndReply( |
| 165 content::BrowserThread::IO, FROM_HERE, | 165 content::BrowserThread::IO, FROM_HERE, |
| 166 base::Bind(&CertReportJobInterceptor::ResumeOnIOThread, | 166 base::Bind(&CertReportJobInterceptor::ResumeOnIOThread, |
| 167 base::Unretained(this))); | 167 base::Unretained(this)), |
| 168 callback); |
| 168 } | 169 } |
| 169 | 170 |
| 170 const std::set<std::string>& CertReportJobInterceptor::successful_reports() | 171 const std::set<std::string>& CertReportJobInterceptor::successful_reports() |
| 171 const { | 172 const { |
| 172 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 173 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 173 return successful_reports_; | 174 return successful_reports_; |
| 174 } | 175 } |
| 175 | 176 |
| 176 const std::set<std::string>& CertReportJobInterceptor::failed_reports() const { | 177 const std::set<std::string>& CertReportJobInterceptor::failed_reports() const { |
| 177 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 178 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 178 return failed_reports_; | 179 return failed_reports_; |
| 179 } | 180 } |
| 180 | 181 |
| 181 const std::set<std::string>& CertReportJobInterceptor::delayed_reports() const { | 182 const std::set<std::string>& CertReportJobInterceptor::delayed_reports() const { |
| 182 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 183 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 183 return delayed_reports_; | 184 return delayed_reports_; |
| 184 } | 185 } |
| 185 | 186 |
| 186 void CertReportJobInterceptor::ClearObservedReports() { | 187 void CertReportJobInterceptor::ClearObservedReports() { |
| 187 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 188 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 188 successful_reports_.clear(); | 189 successful_reports_.clear(); |
| 189 failed_reports_.clear(); | 190 failed_reports_.clear(); |
| 190 delayed_reports_.clear(); | 191 delayed_reports_.clear(); |
| 191 } | 192 } |
| 192 | 193 |
| 194 void CertReportJobInterceptor::WaitForReports(int num_reports) { |
| 195 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 196 wait_helper_.Wait(num_reports); |
| 197 } |
| 198 |
| 193 void CertReportJobInterceptor::SetFailureModeOnIOThread( | 199 void CertReportJobInterceptor::SetFailureModeOnIOThread( |
| 194 ReportSendingResult expected_report_result) { | 200 ReportSendingResult expected_report_result) { |
| 195 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 201 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 196 expected_report_result_ = expected_report_result; | 202 expected_report_result_ = expected_report_result; |
| 197 } | 203 } |
| 198 | 204 |
| 199 void CertReportJobInterceptor::ResumeOnIOThread() { | 205 void CertReportJobInterceptor::ResumeOnIOThread() { |
| 200 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 206 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 201 EXPECT_EQ(REPORTS_DELAY, expected_report_result_); | 207 EXPECT_EQ(REPORTS_DELAY, expected_report_result_); |
| 202 if (delayed_request_) | 208 if (delayed_request_) |
| 203 delayed_request_->Resume(); | 209 delayed_request_->Resume(); |
| 204 } | 210 } |
| 205 | 211 |
| 206 void CertReportJobInterceptor::RequestCreated( | 212 void CertReportJobInterceptor::RequestCreated( |
| 207 const std::string& uploaded_report, | 213 const std::string& uploaded_report, |
| 208 ReportSendingResult expected_report_result) { | 214 ReportSendingResult expected_report_result) { |
| 209 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 215 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 210 switch (expected_report_result) { | 216 switch (expected_report_result) { |
| 211 case REPORTS_SUCCESSFUL: | 217 case REPORTS_SUCCESSFUL: |
| 212 successful_reports_.insert(uploaded_report); | 218 successful_reports_.insert(uploaded_report); |
| 213 break; | 219 break; |
| 214 case REPORTS_FAIL: | 220 case REPORTS_FAIL: |
| 215 failed_reports_.insert(uploaded_report); | 221 failed_reports_.insert(uploaded_report); |
| 216 break; | 222 break; |
| 217 case REPORTS_DELAY: | 223 case REPORTS_DELAY: |
| 218 delayed_reports_.insert(uploaded_report); | 224 delayed_reports_.insert(uploaded_report); |
| 219 break; | 225 break; |
| 220 } | 226 } |
| 227 wait_helper_.OnEvent(); |
| 221 } | 228 } |
| 222 | 229 |
| 223 CertificateReportingServiceTestNetworkDelegate:: | 230 ReportExpectation::ReportExpectation() {} |
| 224 CertificateReportingServiceTestNetworkDelegate( | |
| 225 const base::Callback<void()>& url_request_destroyed_callback) | |
| 226 : url_request_destroyed_callback_(url_request_destroyed_callback) {} | |
| 227 | 231 |
| 228 CertificateReportingServiceTestNetworkDelegate:: | 232 ReportExpectation::ReportExpectation(const ReportExpectation& other) = default; |
| 229 ~CertificateReportingServiceTestNetworkDelegate() {} | |
| 230 | 233 |
| 231 void CertificateReportingServiceTestNetworkDelegate::OnURLRequestDestroyed( | 234 ReportExpectation::~ReportExpectation() {} |
| 232 net::URLRequest* request) { | |
| 233 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 234 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 235 url_request_destroyed_callback_); | |
| 236 } | |
| 237 | |
| 238 CertificateReportingServiceTestBase::ReportExpectation::ReportExpectation() {} | |
| 239 | |
| 240 CertificateReportingServiceTestBase::ReportExpectation::ReportExpectation( | |
| 241 const ReportExpectation& other) = default; | |
| 242 | |
| 243 CertificateReportingServiceTestBase::ReportExpectation::~ReportExpectation() {} | |
| 244 | 235 |
| 245 // static | 236 // static |
| 246 CertificateReportingServiceTestBase::ReportExpectation | 237 ReportExpectation ReportExpectation::Successful( |
| 247 CertificateReportingServiceTestBase::ReportExpectation::Successful( | |
| 248 const std::set<std::string>& reports) { | 238 const std::set<std::string>& reports) { |
| 249 ReportExpectation expectation; | 239 ReportExpectation expectation; |
| 250 expectation.successful_reports = reports; | 240 expectation.successful_reports = reports; |
| 251 return expectation; | 241 return expectation; |
| 252 } | 242 } |
| 253 | 243 |
| 254 // static | 244 // static |
| 255 CertificateReportingServiceTestBase::ReportExpectation | 245 ReportExpectation ReportExpectation::Failed( |
| 256 CertificateReportingServiceTestBase::ReportExpectation::Failed( | |
| 257 const std::set<std::string>& reports) { | 246 const std::set<std::string>& reports) { |
| 258 ReportExpectation expectation; | 247 ReportExpectation expectation; |
| 259 expectation.failed_reports = reports; | 248 expectation.failed_reports = reports; |
| 260 return expectation; | 249 return expectation; |
| 261 } | 250 } |
| 262 | 251 |
| 263 // static | 252 // static |
| 264 CertificateReportingServiceTestBase::ReportExpectation | 253 ReportExpectation ReportExpectation::Delayed( |
| 265 CertificateReportingServiceTestBase::ReportExpectation::Delayed( | |
| 266 const std::set<std::string>& reports) { | 254 const std::set<std::string>& reports) { |
| 267 ReportExpectation expectation; | 255 ReportExpectation expectation; |
| 268 expectation.delayed_reports = reports; | 256 expectation.delayed_reports = reports; |
| 269 return expectation; | 257 return expectation; |
| 270 } | 258 } |
| 271 | 259 |
| 272 CertificateReportingServiceTestBase::CertificateReportingServiceTestBase() | 260 int ReportExpectation::num_reports() const { |
| 273 : num_request_deletions_to_wait_for_(0), num_deleted_requests_(0) { | 261 return successful_reports.size() + failed_reports.size() + |
| 262 delayed_reports.size(); |
| 263 } |
| 264 |
| 265 CertificateReportingServiceTestHelper::CertificateReportingServiceTestHelper() { |
| 274 memset(server_private_key_, 1, sizeof(server_private_key_)); | 266 memset(server_private_key_, 1, sizeof(server_private_key_)); |
| 275 crypto::curve25519::ScalarBaseMult(server_private_key_, server_public_key_); | 267 crypto::curve25519::ScalarBaseMult(server_private_key_, server_public_key_); |
| 276 } | 268 } |
| 277 | 269 |
| 278 CertificateReportingServiceTestBase::~CertificateReportingServiceTestBase() {} | 270 CertificateReportingServiceTestHelper:: |
| 271 ~CertificateReportingServiceTestHelper() {} |
| 279 | 272 |
| 280 void CertificateReportingServiceTestBase::SetUpInterceptor() { | 273 void CertificateReportingServiceTestHelper::SetUpInterceptor() { |
| 281 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 274 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 282 url_request_interceptor_ = | 275 url_request_interceptor_ = |
| 283 new CertReportJobInterceptor(REPORTS_FAIL, server_private_key_); | 276 new CertReportJobInterceptor(REPORTS_FAIL, server_private_key_); |
| 284 content::BrowserThread::PostTask( | 277 content::BrowserThread::PostTask( |
| 285 content::BrowserThread::IO, FROM_HERE, | 278 content::BrowserThread::IO, FROM_HERE, |
| 286 base::Bind( | 279 base::Bind(&SetUpURLHandlersOnIOThread, |
| 287 &CertificateReportingServiceTestBase::SetUpInterceptorOnIOThread, | 280 base::Passed(std::unique_ptr<net::URLRequestInterceptor>( |
| 288 base::Unretained(this), | 281 url_request_interceptor_)))); |
| 289 base::Passed(std::unique_ptr<net::URLRequestInterceptor>( | |
| 290 url_request_interceptor_)))); | |
| 291 } | |
| 292 | |
| 293 void CertificateReportingServiceTestBase::TearDownInterceptor() { | |
| 294 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 295 content::BrowserThread::PostTask( | |
| 296 content::BrowserThread::IO, FROM_HERE, | |
| 297 base::Bind( | |
| 298 &CertificateReportingServiceTestBase::TearDownInterceptorOnIOThread, | |
| 299 base::Unretained(this))); | |
| 300 } | 282 } |
| 301 | 283 |
| 302 // Changes the behavior of report uploads to fail or succeed. | 284 // Changes the behavior of report uploads to fail or succeed. |
| 303 void CertificateReportingServiceTestBase::SetFailureMode( | 285 void CertificateReportingServiceTestHelper::SetFailureMode( |
| 304 ReportSendingResult expected_report_result) { | 286 ReportSendingResult expected_report_result) { |
| 305 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 287 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 306 url_request_interceptor_->SetFailureMode(expected_report_result); | 288 url_request_interceptor_->SetFailureMode(expected_report_result); |
| 307 } | 289 } |
| 308 | 290 |
| 309 void CertificateReportingServiceTestBase::ResumeDelayedRequest() { | 291 void CertificateReportingServiceTestHelper::ResumeDelayedRequest( |
| 292 const base::Callback<void()>& callback) { |
| 310 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 293 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 311 url_request_interceptor_->Resume(); | 294 url_request_interceptor_->Resume(callback); |
| 312 } | 295 } |
| 313 | 296 |
| 314 void CertificateReportingServiceTestBase::WaitForRequestsDestroyed( | 297 uint8_t* CertificateReportingServiceTestHelper::server_public_key() { |
| 315 const ReportExpectation& expectation) { | |
| 316 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 317 DCHECK(!run_loop_); | |
| 318 | |
| 319 const int num_request_deletions_to_wait_for = | |
| 320 expectation.successful_reports.size() + | |
| 321 expectation.failed_reports.size() + expectation.delayed_reports.size(); | |
| 322 | |
| 323 ASSERT_LE(num_deleted_requests_, num_request_deletions_to_wait_for) | |
| 324 << "Observed unexpected report"; | |
| 325 if (num_deleted_requests_ < num_request_deletions_to_wait_for) { | |
| 326 num_request_deletions_to_wait_for_ = num_request_deletions_to_wait_for; | |
| 327 run_loop_.reset(new base::RunLoop()); | |
| 328 run_loop_->Run(); | |
| 329 run_loop_.reset(nullptr); | |
| 330 EXPECT_EQ(0, num_deleted_requests_); | |
| 331 EXPECT_EQ(0, num_request_deletions_to_wait_for_); | |
| 332 } else if (num_deleted_requests_ == num_request_deletions_to_wait_for) { | |
| 333 num_deleted_requests_ = 0; | |
| 334 num_request_deletions_to_wait_for_ = 0; | |
| 335 } | |
| 336 EXPECT_EQ(expectation.successful_reports, | |
| 337 url_request_interceptor_->successful_reports()); | |
| 338 EXPECT_EQ(expectation.failed_reports, | |
| 339 url_request_interceptor_->failed_reports()); | |
| 340 EXPECT_EQ(expectation.delayed_reports, | |
| 341 url_request_interceptor_->delayed_reports()); | |
| 342 url_request_interceptor_->ClearObservedReports(); | |
| 343 } | |
| 344 | |
| 345 uint8_t* CertificateReportingServiceTestBase::server_public_key() { | |
| 346 return server_public_key_; | 298 return server_public_key_; |
| 347 } | 299 } |
| 348 | 300 |
| 349 uint32_t CertificateReportingServiceTestBase::server_public_key_version() | 301 uint32_t CertificateReportingServiceTestHelper::server_public_key_version() |
| 350 const { | 302 const { |
| 351 return kServerPublicKeyTestVersion; | 303 return kServerPublicKeyTestVersion; |
| 352 } | 304 } |
| 353 | 305 |
| 354 net::NetworkDelegate* CertificateReportingServiceTestBase::network_delegate() { | 306 ReportWaitHelper::ReportWaitHelper() |
| 355 return network_delegate_.get(); | 307 : num_events_to_wait_for_(0), num_received_events_(0) {} |
| 308 ReportWaitHelper::~ReportWaitHelper() {} |
| 309 |
| 310 void ReportWaitHelper::Wait(int num_events_to_wait_for) { |
| 311 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 312 DCHECK(!run_loop_); |
| 313 ASSERT_LE(num_received_events_, num_events_to_wait_for) |
| 314 << "Observed unexpected report"; |
| 315 |
| 316 if (num_received_events_ < num_events_to_wait_for) { |
| 317 num_events_to_wait_for_ = num_events_to_wait_for; |
| 318 run_loop_.reset(new base::RunLoop()); |
| 319 run_loop_->Run(); |
| 320 run_loop_.reset(nullptr); |
| 321 EXPECT_EQ(0, num_received_events_); |
| 322 EXPECT_EQ(0, num_events_to_wait_for_); |
| 323 } else if (num_received_events_ == num_events_to_wait_for) { |
| 324 num_received_events_ = 0; |
| 325 num_events_to_wait_for_ = 0; |
| 326 } |
| 356 } | 327 } |
| 357 | 328 |
| 358 void CertificateReportingServiceTestBase::SetUpInterceptorOnIOThread( | 329 void ReportWaitHelper::OnEvent() { |
| 359 std::unique_ptr<net::URLRequestInterceptor> url_request_interceptor) { | |
| 360 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 361 network_delegate_.reset(new CertificateReportingServiceTestNetworkDelegate( | |
| 362 base::Bind(&CertificateReportingServiceTestBase::OnURLRequestDestroyed, | |
| 363 base::Unretained(this)))); | |
| 364 SetUpURLHandlersOnIOThread(std::move(url_request_interceptor)); | |
| 365 } | |
| 366 | |
| 367 void CertificateReportingServiceTestBase::TearDownInterceptorOnIOThread() { | |
| 368 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 369 network_delegate_.reset(nullptr); | |
| 370 } | |
| 371 | |
| 372 void CertificateReportingServiceTestBase::OnURLRequestDestroyed() { | |
| 373 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 330 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 374 num_deleted_requests_++; | 331 num_received_events_++; |
| 375 if (!run_loop_) { | 332 if (!run_loop_) { |
| 376 return; | 333 return; |
| 377 } | 334 } |
| 378 EXPECT_LE(num_deleted_requests_, num_request_deletions_to_wait_for_); | 335 ASSERT_LE(num_received_events_, num_events_to_wait_for_) |
| 379 if (num_deleted_requests_ == num_request_deletions_to_wait_for_) { | 336 << "Observed unexpected report"; |
| 380 num_request_deletions_to_wait_for_ = 0; | 337 if (num_received_events_ == num_events_to_wait_for_) { |
| 381 num_deleted_requests_ = 0; | 338 num_events_to_wait_for_ = 0; |
| 339 num_received_events_ = 0; |
| 382 run_loop_->Quit(); | 340 run_loop_->Quit(); |
| 383 } | 341 } |
| 384 } | 342 } |
| 385 | 343 |
| 386 } // namespace certificate_reporting_test_utils | 344 } // namespace certificate_reporting_test_utils |
| OLD | NEW |