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 "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
10 #include "content/public/test/test_utils.h" | 11 #include "content/public/test/test_utils.h" |
11 #include "crypto/curve25519.h" | 12 #include "crypto/curve25519.h" |
12 #include "net/base/upload_bytes_element_reader.h" | 13 #include "net/base/upload_bytes_element_reader.h" |
13 #include "net/base/upload_data_stream.h" | 14 #include "net/base/upload_data_stream.h" |
14 #include "net/test/url_request/url_request_failed_job.h" | |
15 #include "net/test/url_request/url_request_mock_data_job.h" | |
16 #include "net/url_request/url_request_filter.h" | 15 #include "net/url_request/url_request_filter.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
18 | 17 |
19 namespace { | 18 namespace { |
20 | 19 |
21 const uint32_t kServerPublicKeyTestVersion = 16; | 20 const uint32_t kServerPublicKeyTestVersion = 16; |
22 | 21 |
23 void SetUpURLHandlersOnIOThread( | 22 void SetUpURLHandlersOnIOThread( |
24 std::unique_ptr<net::URLRequestInterceptor> url_request_interceptor) { | 23 std::unique_ptr<net::URLRequestInterceptor> url_request_interceptor) { |
25 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 24 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
(...skipping 22 matching lines...) Expand all Loading... | |
48 encrypted_request.server_public_key_version()); | 47 encrypted_request.server_public_key_version()); |
49 EXPECT_EQ(certificate_reporting::EncryptedCertLoggerRequest:: | 48 EXPECT_EQ(certificate_reporting::EncryptedCertLoggerRequest:: |
50 AEAD_ECDH_AES_128_CTR_HMAC_SHA256, | 49 AEAD_ECDH_AES_128_CTR_HMAC_SHA256, |
51 encrypted_request.algorithm()); | 50 encrypted_request.algorithm()); |
52 std::string decrypted_report; | 51 std::string decrypted_report; |
53 certificate_reporting::ErrorReporter::DecryptErrorReport( | 52 certificate_reporting::ErrorReporter::DecryptErrorReport( |
54 server_private_key, encrypted_request, &decrypted_report); | 53 server_private_key, encrypted_request, &decrypted_report); |
55 return decrypted_report; | 54 return decrypted_report; |
56 } | 55 } |
57 | 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( | |
73 certificate_reporting_test_utils::RequestObserver* observer, | |
74 const certificate_reporting_test_utils::ReportExpectation& expectation) { | |
75 observer->Wait(expectation.num_reports()); | |
76 CompareHostnames(expectation.successful_reports, | |
77 observer->successful_reports(), "successful"); | |
78 CompareHostnames(expectation.failed_reports, observer->failed_reports(), | |
79 "failed"); | |
80 CompareHostnames(expectation.delayed_reports, observer->delayed_reports(), | |
81 "delayed"); | |
82 observer->ClearObservedReports(); | |
83 } | |
84 | |
58 } // namespace | 85 } // namespace |
59 | 86 |
60 namespace certificate_reporting_test_utils { | 87 namespace certificate_reporting_test_utils { |
61 | 88 |
89 RequestObserver::RequestObserver() | |
90 : num_events_to_wait_for_(0), num_received_events_(0) {} | |
91 RequestObserver::~RequestObserver() {} | |
92 | |
93 void RequestObserver::Wait(int num_events_to_wait_for) { | |
94 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
95 DCHECK(!run_loop_); | |
96 ASSERT_LE(num_received_events_, num_events_to_wait_for) | |
97 << "Observed unexpected report"; | |
98 | |
99 if (num_received_events_ < num_events_to_wait_for) { | |
100 num_events_to_wait_for_ = num_events_to_wait_for; | |
101 run_loop_.reset(new base::RunLoop()); | |
102 run_loop_->Run(); | |
103 run_loop_.reset(nullptr); | |
104 EXPECT_EQ(0, num_received_events_); | |
105 EXPECT_EQ(0, num_events_to_wait_for_); | |
106 } else if (num_received_events_ == num_events_to_wait_for) { | |
107 num_received_events_ = 0; | |
108 num_events_to_wait_for_ = 0; | |
109 } | |
110 } | |
111 | |
112 void RequestObserver::OnRequest(const std::string& serialized_report, | |
113 ReportSendingResult report_type) { | |
114 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
115 switch (report_type) { | |
116 case REPORTS_SUCCESSFUL: | |
117 successful_reports_.insert(serialized_report); | |
118 break; | |
119 case REPORTS_FAIL: | |
120 failed_reports_.insert(serialized_report); | |
121 break; | |
122 case REPORTS_DELAY: | |
123 delayed_reports_.insert(serialized_report); | |
124 break; | |
125 } | |
126 | |
127 num_received_events_++; | |
128 if (!run_loop_) { | |
129 return; | |
130 } | |
131 ASSERT_LE(num_received_events_, num_events_to_wait_for_) | |
132 << "Observed unexpected report"; | |
133 | |
134 if (num_received_events_ == num_events_to_wait_for_) { | |
135 num_events_to_wait_for_ = 0; | |
136 num_received_events_ = 0; | |
137 run_loop_->Quit(); | |
138 } | |
139 } | |
140 | |
141 const std::set<std::string>& RequestObserver::successful_reports() const { | |
142 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
143 return successful_reports_; | |
144 } | |
145 | |
146 const std::set<std::string>& RequestObserver::failed_reports() const { | |
147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
148 return failed_reports_; | |
149 } | |
150 | |
151 const std::set<std::string>& RequestObserver::delayed_reports() const { | |
152 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
153 return delayed_reports_; | |
154 } | |
155 | |
156 void RequestObserver::ClearObservedReports() { | |
157 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
158 successful_reports_.clear(); | |
159 failed_reports_.clear(); | |
160 delayed_reports_.clear(); | |
161 } | |
162 | |
62 DelayableCertReportURLRequestJob::DelayableCertReportURLRequestJob( | 163 DelayableCertReportURLRequestJob::DelayableCertReportURLRequestJob( |
164 bool delayed, | |
165 bool should_fail, | |
63 net::URLRequest* request, | 166 net::URLRequest* request, |
64 net::NetworkDelegate* network_delegate) | 167 net::NetworkDelegate* network_delegate, |
65 : net::URLRequestJob(request, network_delegate), weak_factory_(this) {} | 168 const base::Callback<void()>& destruction_callback) |
169 : net::URLRequestJob(request, network_delegate), | |
170 delayed_(delayed), | |
171 should_fail_(should_fail), | |
172 started_(false), | |
173 destruction_callback_(destruction_callback), | |
174 weak_factory_(this) {} | |
66 | 175 |
67 DelayableCertReportURLRequestJob::~DelayableCertReportURLRequestJob() {} | 176 DelayableCertReportURLRequestJob::~DelayableCertReportURLRequestJob() { |
177 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
178 destruction_callback_); | |
179 } | |
68 | 180 |
69 base::WeakPtr<DelayableCertReportURLRequestJob> | 181 base::WeakPtr<DelayableCertReportURLRequestJob> |
70 DelayableCertReportURLRequestJob::GetWeakPtr() { | 182 DelayableCertReportURLRequestJob::GetWeakPtr() { |
71 return weak_factory_.GetWeakPtr(); | 183 return weak_factory_.GetWeakPtr(); |
72 } | 184 } |
73 | 185 |
74 void DelayableCertReportURLRequestJob::Start() { | 186 void DelayableCertReportURLRequestJob::Start() { |
75 started_ = true; | 187 started_ = true; |
76 if (delayed_) { | 188 if (delayed_) { |
77 // Do nothing until Resume() is called. | 189 // Do nothing until Resume() is called. |
(...skipping 13 matching lines...) Expand all Loading... | |
91 return 200; | 203 return 200; |
92 } | 204 } |
93 | 205 |
94 void DelayableCertReportURLRequestJob::GetResponseInfo( | 206 void DelayableCertReportURLRequestJob::GetResponseInfo( |
95 net::HttpResponseInfo* info) { | 207 net::HttpResponseInfo* info) { |
96 // Report sender ignores responses. Return empty response. | 208 // Report sender ignores responses. Return empty response. |
97 } | 209 } |
98 | 210 |
99 void DelayableCertReportURLRequestJob::Resume() { | 211 void DelayableCertReportURLRequestJob::Resume() { |
100 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 212 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
101 DCHECK(delayed_); | |
102 if (!started_) { | 213 if (!started_) { |
103 // If Start() hasn't been called yet, then unset |delayed_| so | 214 // If Start() hasn't been called yet, then unset |delayed_| so that when |
104 // that when Start() is called, the request will begin | 215 // Start() is called, the request will begin immediately. |
105 // immediately. | |
106 delayed_ = false; | 216 delayed_ = false; |
107 return; | 217 return; |
108 } | 218 } |
219 if (should_fail_) { | |
220 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
221 net::ERR_SSL_PROTOCOL_ERROR)); | |
222 return; | |
223 } | |
109 // Start reading asynchronously as would a normal network request. | 224 // Start reading asynchronously as would a normal network request. |
110 base::ThreadTaskRunnerHandle::Get()->PostTask( | 225 base::ThreadTaskRunnerHandle::Get()->PostTask( |
111 FROM_HERE, | 226 FROM_HERE, |
112 base::Bind(&DelayableCertReportURLRequestJob::NotifyHeadersComplete, | 227 base::Bind(&DelayableCertReportURLRequestJob::NotifyHeadersComplete, |
113 weak_factory_.GetWeakPtr())); | 228 weak_factory_.GetWeakPtr())); |
114 } | 229 } |
115 | 230 |
116 CertReportJobInterceptor::CertReportJobInterceptor( | 231 CertReportJobInterceptor::CertReportJobInterceptor( |
117 ReportSendingResult expected_report_result, | 232 ReportSendingResult expected_report_result, |
118 const uint8_t* server_private_key) | 233 const uint8_t* server_private_key) |
(...skipping 10 matching lines...) Expand all Loading... | |
129 | 244 |
130 const std::string uploaded_report = | 245 const std::string uploaded_report = |
131 GetReportContents(request, server_private_key_); | 246 GetReportContents(request, server_private_key_); |
132 content::BrowserThread::PostTask( | 247 content::BrowserThread::PostTask( |
133 content::BrowserThread::UI, FROM_HERE, | 248 content::BrowserThread::UI, FROM_HERE, |
134 base::Bind(&CertReportJobInterceptor::RequestCreated, | 249 base::Bind(&CertReportJobInterceptor::RequestCreated, |
135 weak_factory_.GetWeakPtr(), uploaded_report, | 250 weak_factory_.GetWeakPtr(), uploaded_report, |
136 expected_report_result_)); | 251 expected_report_result_)); |
137 | 252 |
138 if (expected_report_result_ == REPORTS_FAIL) { | 253 if (expected_report_result_ == REPORTS_FAIL) { |
139 return new net::URLRequestFailedJob(request, network_delegate, | 254 return new DelayableCertReportURLRequestJob( |
140 net::ERR_SSL_PROTOCOL_ERROR); | 255 false, true, request, network_delegate, |
256 base::Bind(&CertReportJobInterceptor::RequestDestructed, | |
257 base::Unretained(this), uploaded_report, | |
258 expected_report_result_)); | |
259 | |
141 } else if (expected_report_result_ == REPORTS_DELAY) { | 260 } else if (expected_report_result_ == REPORTS_DELAY) { |
142 DCHECK(!delayed_request_) << "Supports only one delayed request at a time"; | 261 DCHECK(!delayed_request_) << "Supports only one delayed request at a time"; |
143 DelayableCertReportURLRequestJob* job = | 262 DelayableCertReportURLRequestJob* job = |
144 new DelayableCertReportURLRequestJob(request, network_delegate); | 263 new DelayableCertReportURLRequestJob( |
264 true, false, request, network_delegate, | |
265 base::Bind(&CertReportJobInterceptor::RequestDestructed, | |
266 base::Unretained(this), uploaded_report, | |
267 expected_report_result_)); | |
145 delayed_request_ = job->GetWeakPtr(); | 268 delayed_request_ = job->GetWeakPtr(); |
146 return job; | 269 return job; |
147 } | 270 } |
148 // Successful url request job. | 271 // Successful url request job. |
149 return new net::URLRequestMockDataJob(request, network_delegate, "some data", | 272 return new DelayableCertReportURLRequestJob( |
150 1, false); | 273 false, false, request, network_delegate, |
274 base::Bind(&CertReportJobInterceptor::RequestDestructed, | |
275 base::Unretained(this), uploaded_report, | |
276 expected_report_result_)); | |
151 } | 277 } |
152 | 278 |
153 void CertReportJobInterceptor::SetFailureMode( | 279 void CertReportJobInterceptor::SetFailureMode( |
154 ReportSendingResult expected_report_result) { | 280 ReportSendingResult expected_report_result) { |
155 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 281 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
156 content::BrowserThread::PostTask( | 282 content::BrowserThread::PostTask( |
157 content::BrowserThread::IO, FROM_HERE, | 283 content::BrowserThread::IO, FROM_HERE, |
158 base::Bind(&CertReportJobInterceptor::SetFailureModeOnIOThread, | 284 base::Bind(&CertReportJobInterceptor::SetFailureModeOnIOThread, |
159 weak_factory_.GetWeakPtr(), expected_report_result)); | 285 weak_factory_.GetWeakPtr(), expected_report_result)); |
160 } | 286 } |
161 | 287 |
162 void CertReportJobInterceptor::Resume(const base::Callback<void()>& callback) { | 288 void CertReportJobInterceptor::Resume() { |
163 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 289 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
164 content::BrowserThread::PostTaskAndReply( | 290 content::BrowserThread::PostTask( |
165 content::BrowserThread::IO, FROM_HERE, | 291 content::BrowserThread::IO, FROM_HERE, |
166 base::Bind(&CertReportJobInterceptor::ResumeOnIOThread, | 292 base::Bind(&CertReportJobInterceptor::ResumeOnIOThread, |
167 base::Unretained(this)), | 293 base::Unretained(this))); |
168 callback); | |
169 } | 294 } |
170 | 295 |
171 const std::set<std::string>& CertReportJobInterceptor::successful_reports() | 296 RequestObserver* CertReportJobInterceptor::request_created_observer() const { |
172 const { | |
173 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 297 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
174 return successful_reports_; | 298 return &request_created_observer_; |
175 } | 299 } |
176 | 300 |
177 const std::set<std::string>& CertReportJobInterceptor::failed_reports() const { | 301 RequestObserver* CertReportJobInterceptor::request_destroyed_observer() const { |
178 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 302 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
179 return failed_reports_; | 303 return &request_destroyed_observer_; |
180 } | |
181 | |
182 const std::set<std::string>& CertReportJobInterceptor::delayed_reports() const { | |
183 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
184 return delayed_reports_; | |
185 } | |
186 | |
187 void CertReportJobInterceptor::ClearObservedReports() { | |
188 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
189 successful_reports_.clear(); | |
190 failed_reports_.clear(); | |
191 delayed_reports_.clear(); | |
192 } | |
193 | |
194 void CertReportJobInterceptor::WaitForReports(int num_reports) { | |
195 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
196 wait_helper_.Wait(num_reports); | |
197 } | 304 } |
198 | 305 |
199 void CertReportJobInterceptor::SetFailureModeOnIOThread( | 306 void CertReportJobInterceptor::SetFailureModeOnIOThread( |
200 ReportSendingResult expected_report_result) { | 307 ReportSendingResult expected_report_result) { |
201 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 308 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
202 expected_report_result_ = expected_report_result; | 309 expected_report_result_ = expected_report_result; |
203 } | 310 } |
204 | 311 |
205 void CertReportJobInterceptor::ResumeOnIOThread() { | 312 void CertReportJobInterceptor::ResumeOnIOThread() { |
206 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 313 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
207 EXPECT_EQ(REPORTS_DELAY, expected_report_result_); | 314 EXPECT_EQ(REPORTS_DELAY, expected_report_result_); |
208 if (delayed_request_) | 315 if (delayed_request_) |
209 delayed_request_->Resume(); | 316 delayed_request_->Resume(); |
210 } | 317 } |
211 | 318 |
212 void CertReportJobInterceptor::RequestCreated( | 319 void CertReportJobInterceptor::RequestCreated( |
213 const std::string& uploaded_report, | 320 const std::string& uploaded_report, |
214 ReportSendingResult expected_report_result) { | 321 ReportSendingResult expected_report_result) const { |
215 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 322 request_created_observer_.OnRequest(uploaded_report, expected_report_result); |
216 switch (expected_report_result) { | 323 } |
217 case REPORTS_SUCCESSFUL: | 324 |
218 successful_reports_.insert(uploaded_report); | 325 void CertReportJobInterceptor::RequestDestructed( |
219 break; | 326 const std::string& uploaded_report, |
220 case REPORTS_FAIL: | 327 ReportSendingResult expected_report_result) const { |
221 failed_reports_.insert(uploaded_report); | 328 request_destroyed_observer_.OnRequest(uploaded_report, |
222 break; | 329 expected_report_result); |
223 case REPORTS_DELAY: | |
224 delayed_reports_.insert(uploaded_report); | |
225 break; | |
226 } | |
227 wait_helper_.OnEvent(); | |
228 } | 330 } |
229 | 331 |
230 ReportExpectation::ReportExpectation() {} | 332 ReportExpectation::ReportExpectation() {} |
231 | 333 |
232 ReportExpectation::ReportExpectation(const ReportExpectation& other) = default; | 334 ReportExpectation::ReportExpectation(const ReportExpectation& other) = default; |
233 | 335 |
234 ReportExpectation::~ReportExpectation() {} | 336 ReportExpectation::~ReportExpectation() {} |
235 | 337 |
236 // static | 338 // static |
237 ReportExpectation ReportExpectation::Successful( | 339 ReportExpectation ReportExpectation::Successful( |
(...skipping 17 matching lines...) Expand all Loading... | |
255 ReportExpectation expectation; | 357 ReportExpectation expectation; |
256 expectation.delayed_reports = reports; | 358 expectation.delayed_reports = reports; |
257 return expectation; | 359 return expectation; |
258 } | 360 } |
259 | 361 |
260 int ReportExpectation::num_reports() const { | 362 int ReportExpectation::num_reports() const { |
261 return successful_reports.size() + failed_reports.size() + | 363 return successful_reports.size() + failed_reports.size() + |
262 delayed_reports.size(); | 364 delayed_reports.size(); |
263 } | 365 } |
264 | 366 |
367 CertificateReportingServiceObserver::CertificateReportingServiceObserver() {} | |
368 | |
369 CertificateReportingServiceObserver::~CertificateReportingServiceObserver() {} | |
370 | |
371 void CertificateReportingServiceObserver::Clear() { | |
372 did_reset_ = false; | |
373 } | |
374 | |
375 void CertificateReportingServiceObserver::WaitForReset() { | |
376 DCHECK(!run_loop_); | |
377 if (did_reset_) | |
378 return; | |
379 run_loop_.reset(new base::RunLoop()); | |
380 run_loop_->Run(); | |
381 run_loop_.reset(); | |
382 } | |
383 | |
384 void CertificateReportingServiceObserver::OnServiceReset() { | |
385 did_reset_ = true; | |
386 if (run_loop_) | |
387 run_loop_->Quit(); | |
388 } | |
389 | |
265 CertificateReportingServiceTestHelper::CertificateReportingServiceTestHelper() { | 390 CertificateReportingServiceTestHelper::CertificateReportingServiceTestHelper() { |
266 memset(server_private_key_, 1, sizeof(server_private_key_)); | 391 memset(server_private_key_, 1, sizeof(server_private_key_)); |
267 crypto::curve25519::ScalarBaseMult(server_private_key_, server_public_key_); | 392 crypto::curve25519::ScalarBaseMult(server_private_key_, server_public_key_); |
268 } | 393 } |
269 | 394 |
270 CertificateReportingServiceTestHelper:: | 395 CertificateReportingServiceTestHelper:: |
271 ~CertificateReportingServiceTestHelper() {} | 396 ~CertificateReportingServiceTestHelper() {} |
272 | 397 |
273 void CertificateReportingServiceTestHelper::SetUpInterceptor() { | 398 void CertificateReportingServiceTestHelper::SetUpInterceptor() { |
274 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 399 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
275 url_request_interceptor_ = | 400 url_request_interceptor_ = |
276 new CertReportJobInterceptor(REPORTS_FAIL, server_private_key_); | 401 new CertReportJobInterceptor(REPORTS_FAIL, server_private_key_); |
277 content::BrowserThread::PostTask( | 402 content::BrowserThread::PostTask( |
278 content::BrowserThread::IO, FROM_HERE, | 403 content::BrowserThread::IO, FROM_HERE, |
279 base::Bind(&SetUpURLHandlersOnIOThread, | 404 base::Bind(&SetUpURLHandlersOnIOThread, |
280 base::Passed(std::unique_ptr<net::URLRequestInterceptor>( | 405 base::Passed(std::unique_ptr<net::URLRequestInterceptor>( |
281 url_request_interceptor_)))); | 406 url_request_interceptor_)))); |
282 } | 407 } |
283 | 408 |
284 // Changes the behavior of report uploads to fail or succeed. | |
estark
2017/01/05 16:42:29
Did you mean to delete this?
meacer
2017/01/05 20:15:46
Yes, this is already documented in the header.
| |
285 void CertificateReportingServiceTestHelper::SetFailureMode( | 409 void CertificateReportingServiceTestHelper::SetFailureMode( |
286 ReportSendingResult expected_report_result) { | 410 ReportSendingResult expected_report_result) { |
287 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 411 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
288 url_request_interceptor_->SetFailureMode(expected_report_result); | 412 url_request_interceptor_->SetFailureMode(expected_report_result); |
289 } | 413 } |
290 | 414 |
291 void CertificateReportingServiceTestHelper::ResumeDelayedRequest( | 415 void CertificateReportingServiceTestHelper::ResumeDelayedRequest() { |
292 const base::Callback<void()>& callback) { | |
293 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 416 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
294 url_request_interceptor_->Resume(callback); | 417 url_request_interceptor_->Resume(); |
295 } | 418 } |
296 | 419 |
297 uint8_t* CertificateReportingServiceTestHelper::server_public_key() { | 420 uint8_t* CertificateReportingServiceTestHelper::server_public_key() { |
298 return server_public_key_; | 421 return server_public_key_; |
299 } | 422 } |
300 | 423 |
301 uint32_t CertificateReportingServiceTestHelper::server_public_key_version() | 424 uint32_t CertificateReportingServiceTestHelper::server_public_key_version() |
302 const { | 425 const { |
303 return kServerPublicKeyTestVersion; | 426 return kServerPublicKeyTestVersion; |
304 } | 427 } |
305 | 428 |
306 ReportWaitHelper::ReportWaitHelper() | 429 void CertificateReportingServiceTestHelper::WaitForRequestsCreated( |
307 : num_events_to_wait_for_(0), num_received_events_(0) {} | 430 const ReportExpectation& expectation) { |
308 ReportWaitHelper::~ReportWaitHelper() {} | 431 WaitReports(interceptor()->request_created_observer(), expectation); |
432 } | |
309 | 433 |
310 void ReportWaitHelper::Wait(int num_events_to_wait_for) { | 434 void CertificateReportingServiceTestHelper::WaitForRequestsDestroyed( |
435 const ReportExpectation& expectation) { | |
436 WaitReports(interceptor()->request_destroyed_observer(), expectation); | |
437 } | |
438 | |
439 // Checks that there are no remaining successful and failed reports observed | |
440 // by the interceptor. | |
441 void CertificateReportingServiceTestHelper::ExpectNoRequests( | |
442 CertificateReportingService* service) { | |
311 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 443 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
312 DCHECK(!run_loop_); | 444 // Check that all requests have been destroyed. |
313 ASSERT_LE(num_received_events_, num_events_to_wait_for) | 445 EXPECT_TRUE(interceptor() |
314 << "Observed unexpected report"; | 446 ->request_destroyed_observer() |
447 ->successful_reports() | |
448 .empty()); | |
449 EXPECT_TRUE( | |
450 interceptor()->request_destroyed_observer()->failed_reports().empty()); | |
451 EXPECT_TRUE( | |
452 interceptor()->request_destroyed_observer()->delayed_reports().empty()); | |
315 | 453 |
316 if (num_received_events_ < num_events_to_wait_for) { | 454 if (service->GetReporterForTesting()) { |
317 num_events_to_wait_for_ = num_events_to_wait_for; | 455 // Reporter can be null if reporting is disabled. |
318 run_loop_.reset(new base::RunLoop()); | 456 EXPECT_EQ( |
319 run_loop_->Run(); | 457 0u, |
320 run_loop_.reset(nullptr); | 458 service->GetReporterForTesting()->inflight_report_count_for_testing()); |
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 } | 459 } |
327 } | 460 } |
328 | 461 |
329 void ReportWaitHelper::OnEvent() { | |
330 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
331 num_received_events_++; | |
332 if (!run_loop_) { | |
333 return; | |
334 } | |
335 ASSERT_LE(num_received_events_, num_events_to_wait_for_) | |
336 << "Observed unexpected report"; | |
337 if (num_received_events_ == num_events_to_wait_for_) { | |
338 num_events_to_wait_for_ = 0; | |
339 num_received_events_ = 0; | |
340 run_loop_->Quit(); | |
341 } | |
342 } | |
343 | |
344 } // namespace certificate_reporting_test_utils | 462 } // namespace certificate_reporting_test_utils |
OLD | NEW |