Index: chrome/browser/safe_browsing/certificate_reporting_service_test_utils.h |
diff --git a/chrome/browser/safe_browsing/certificate_reporting_service_test_utils.h b/chrome/browser/safe_browsing/certificate_reporting_service_test_utils.h |
index 1d2f6ccac11c43ce734c460cf6cf9f4d0f39da8d..7e6dc91531f95bf040df4e66388408c2a84146f6 100644 |
--- a/chrome/browser/safe_browsing/certificate_reporting_service_test_utils.h |
+++ b/chrome/browser/safe_browsing/certificate_reporting_service_test_utils.h |
@@ -9,6 +9,7 @@ |
#include "base/macros.h" |
#include "base/run_loop.h" |
+#include "base/threading/non_thread_safe.h" |
#include "chrome/browser/safe_browsing/certificate_reporting_service.h" |
#include "content/public/test/test_browser_thread.h" |
#include "content/public/test/test_browser_thread_bundle.h" |
@@ -22,6 +23,47 @@ class NetworkDelegate; |
namespace certificate_reporting_test_utils { |
+// Syntactic sugar for wrapping report expectations in a more readable way. |
+// Passed to WaitForRequestDeletions() as input. |
+// Example: |
+// The following expects report0 and report1 to be successfully sent and their |
+// URL requests to be deleted: |
+// WaitForRequestDeletions( |
+// ReportExpectation::Successful("report0, report1")); |
+struct ReportExpectation { |
+ ReportExpectation(); |
+ ReportExpectation(const ReportExpectation& other); |
+ ~ReportExpectation(); |
+ // Returns an expectation where all reports in |reports| succeed. |
+ static ReportExpectation Successful(const std::set<std::string>& reports); |
+ // Returns an expectation where all reports in |reports| fail. |
+ static ReportExpectation Failed(const std::set<std::string>& reports); |
+ // Returns an expectation where all reports in |reports| are delayed. |
+ static ReportExpectation Delayed(const std::set<std::string>& reports); |
+ // Total number of reports expected. |
+ int num_reports() const; |
+ std::set<std::string> successful_reports; |
+ std::set<std::string> failed_reports; |
+ std::set<std::string> delayed_reports; |
+}; |
+ |
+// Helper class to wait for a number of events (e.g. request destroyed, report |
+// observed). |
+class ReportWaitHelper { |
+ public: |
+ ReportWaitHelper(); |
+ ~ReportWaitHelper(); |
+ // Waits for |num_events_to_wait_for|. |
+ void Wait(int num_events_to_wait_for); |
+ // Must be called when an event is observed. |
+ void OnEvent(); |
+ |
+ private: |
+ int num_events_to_wait_for_; |
+ int num_received_events_; |
+ std::unique_ptr<base::RunLoop> run_loop_; |
+}; |
+ |
// Failure mode of the report sending attempts. |
enum ReportSendingResult { |
// Report send attempts should be successful. |
@@ -35,7 +77,8 @@ enum ReportSendingResult { |
// A URLRequestJob that can be delayed until Resume() is called. Returns an |
// empty response. If Resume() is called before a request is made, then the |
// request will not be delayed. |
-class DelayableCertReportURLRequestJob : public net::URLRequestJob { |
+class DelayableCertReportURLRequestJob : public net::URLRequestJob, |
+ public base::NonThreadSafe { |
public: |
DelayableCertReportURLRequestJob(net::URLRequest* request, |
net::NetworkDelegate* network_delegate); |
@@ -77,8 +120,9 @@ class CertReportJobInterceptor : public net::URLRequestInterceptor { |
// Sets the failure mode for reports. Must be called on the UI thread. |
void SetFailureMode(ReportSendingResult expected_report_result); |
- // Resumes any hanging URL request. Must be called on the UI thread. |
- void Resume(); |
+ // Resumes any hanging URL request and runs callback when the request |
+ // is resumed (i.e. response starts). Must be called on the UI thread. |
+ void Resume(const base::Closure& callback); |
// These must be called on the UI thread. |
const std::set<std::string>& successful_reports() const; |
@@ -86,6 +130,11 @@ class CertReportJobInterceptor : public net::URLRequestInterceptor { |
const std::set<std::string>& delayed_reports() const; |
void ClearObservedReports(); |
+ // Waits for requests for |num_reports| reports to be created. Only used in |
+ // browser tests. Unit tests wait for requests to be destroyed instead. |
+ // Must be called on the UI thread. |
+ void WaitForReports(int num_reports); |
+ |
private: |
void SetFailureModeOnIOThread(ReportSendingResult expected_report_result); |
void ResumeOnIOThread(); |
@@ -101,6 +150,8 @@ class CertReportJobInterceptor : public net::URLRequestInterceptor { |
// Private key to decrypt certificate reports. |
const uint8_t* server_private_key_; |
+ ReportWaitHelper wait_helper_; |
+ |
mutable base::WeakPtr<DelayableCertReportURLRequestJob> delayed_request_ = |
nullptr; |
mutable base::WeakPtrFactory<CertReportJobInterceptor> weak_factory_; |
@@ -131,66 +182,28 @@ class CertificateReportingServiceTestBase { |
CertificateReportingServiceTestBase(); |
virtual ~CertificateReportingServiceTestBase(); |
- // Syntactic sugar for wrapping report expectations in a more readable way. |
- // Passed to WaitForRequestDeletions() as input. |
- // Example: |
- // The following expects report0 and report1 to be successfully sent and their |
- // URL requests to be deleted: |
- // WaitForRequestDeletions( |
- // ReportExpectation::Successful("report0, report1")); |
- struct ReportExpectation { |
- ReportExpectation(); |
- ReportExpectation(const ReportExpectation& other); |
- ~ReportExpectation(); |
- // Returns an expectation where all reports in |reports| succeed. |
- static ReportExpectation Successful(const std::set<std::string>& reports); |
- // Returns an expectation where all reports in |reports| fail. |
- static ReportExpectation Failed(const std::set<std::string>& reports); |
- // Returns an expectation where all reports in |reports| are delayed. |
- static ReportExpectation Delayed(const std::set<std::string>& reports); |
- std::set<std::string> successful_reports; |
- std::set<std::string> failed_reports; |
- std::set<std::string> delayed_reports; |
- }; |
- |
void SetUpInterceptor(); |
- void TearDownInterceptor(); |
// Changes the behavior of report uploads to fail, succeed or hang. |
void SetFailureMode(ReportSendingResult expected_report_result); |
// Resumes delayed report request. Failure mode should be REPORTS_DELAY when |
// calling this method. |
- void ResumeDelayedRequest(); |
- |
- // Waits for the URL requests for the expected reports to be destroyed. |
- // Doesn't block if all requests have already been destroyed. |
- void WaitForRequestsDestroyed(const ReportExpectation& expectation); |
+ void ResumeDelayedRequest(const base::Callback<void()>& callback); |
uint8_t* server_public_key(); |
uint32_t server_public_key_version() const; |
- net::NetworkDelegate* network_delegate(); |
- |
CertReportJobInterceptor* interceptor() { return url_request_interceptor_; } |
private: |
- void SetUpInterceptorOnIOThread( |
- std::unique_ptr<net::URLRequestInterceptor> url_request_interceptor); |
- void TearDownInterceptorOnIOThread(); |
- void OnURLRequestDestroyed(); |
+ void SetUpInterceptorOnIOThread(); |
CertReportJobInterceptor* url_request_interceptor_; |
uint8_t server_public_key_[32]; |
uint8_t server_private_key_[32]; |
- std::unique_ptr<CertificateReportingServiceTestNetworkDelegate> |
- network_delegate_; |
- |
- int num_request_deletions_to_wait_for_; |
- int num_deleted_requests_; |
- std::unique_ptr<base::RunLoop> run_loop_; |
DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestBase); |
}; |