| 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 #ifndef CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS_H_ | 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS_H_ |
| 6 #define CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS_H_ | 6 #define CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/threading/non_thread_safe.h" |
| 12 #include "chrome/browser/safe_browsing/certificate_reporting_service.h" | 13 #include "chrome/browser/safe_browsing/certificate_reporting_service.h" |
| 13 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "net/base/network_delegate_impl.h" | 16 #include "net/base/network_delegate_impl.h" |
| 16 #include "net/url_request/url_request_interceptor.h" | 17 #include "net/url_request/url_request_interceptor.h" |
| 17 #include "net/url_request/url_request_job.h" | 18 #include "net/url_request/url_request_job.h" |
| 18 | 19 |
| 19 namespace net { | 20 namespace net { |
| 20 class NetworkDelegate; | 21 class NetworkDelegate; |
| 21 } | 22 } |
| 22 | 23 |
| 23 namespace certificate_reporting_test_utils { | 24 namespace certificate_reporting_test_utils { |
| 24 | 25 |
| 26 // Syntactic sugar for wrapping report expectations in a more readable way. |
| 27 // Passed to WaitForRequestDeletions() as input. |
| 28 // Example: |
| 29 // The following expects report0 and report1 to be successfully sent and their |
| 30 // URL requests to be deleted: |
| 31 // WaitForRequestDeletions( |
| 32 // ReportExpectation::Successful("report0, report1")); |
| 33 struct ReportExpectation { |
| 34 ReportExpectation(); |
| 35 ReportExpectation(const ReportExpectation& other); |
| 36 ~ReportExpectation(); |
| 37 // Returns an expectation where all reports in |reports| succeed. |
| 38 static ReportExpectation Successful(const std::set<std::string>& reports); |
| 39 // Returns an expectation where all reports in |reports| fail. |
| 40 static ReportExpectation Failed(const std::set<std::string>& reports); |
| 41 // Returns an expectation where all reports in |reports| are delayed. |
| 42 static ReportExpectation Delayed(const std::set<std::string>& reports); |
| 43 // Total number of reports expected. |
| 44 int num_reports() const; |
| 45 std::set<std::string> successful_reports; |
| 46 std::set<std::string> failed_reports; |
| 47 std::set<std::string> delayed_reports; |
| 48 }; |
| 49 |
| 50 // Helper class to wait for a number of events (e.g. request destroyed, report |
| 51 // observed). |
| 52 class ReportWaitHelper { |
| 53 public: |
| 54 ReportWaitHelper(); |
| 55 ~ReportWaitHelper(); |
| 56 // Waits for |num_events_to_wait_for|. |
| 57 void Wait(int num_events_to_wait_for); |
| 58 // Must be called when an event is observed. |
| 59 void OnEvent(); |
| 60 |
| 61 private: |
| 62 int num_events_to_wait_for_; |
| 63 int num_received_events_; |
| 64 std::unique_ptr<base::RunLoop> run_loop_; |
| 65 }; |
| 66 |
| 25 // Failure mode of the report sending attempts. | 67 // Failure mode of the report sending attempts. |
| 26 enum ReportSendingResult { | 68 enum ReportSendingResult { |
| 27 // Report send attempts should be successful. | 69 // Report send attempts should be successful. |
| 28 REPORTS_SUCCESSFUL, | 70 REPORTS_SUCCESSFUL, |
| 29 // Report send attempts should fail. | 71 // Report send attempts should fail. |
| 30 REPORTS_FAIL, | 72 REPORTS_FAIL, |
| 31 // Report send attempts should hang until explicitly resumed. | 73 // Report send attempts should hang until explicitly resumed. |
| 32 REPORTS_DELAY, | 74 REPORTS_DELAY, |
| 33 }; | 75 }; |
| 34 | 76 |
| 35 // A URLRequestJob that can be delayed until Resume() is called. Returns an | 77 // A URLRequestJob that can be delayed until Resume() is called. Returns an |
| 36 // empty response. If Resume() is called before a request is made, then the | 78 // empty response. If Resume() is called before a request is made, then the |
| 37 // request will not be delayed. | 79 // request will not be delayed. |
| 38 class DelayableCertReportURLRequestJob : public net::URLRequestJob { | 80 class DelayableCertReportURLRequestJob : public net::URLRequestJob, |
| 81 public base::NonThreadSafe { |
| 39 public: | 82 public: |
| 40 DelayableCertReportURLRequestJob(net::URLRequest* request, | 83 DelayableCertReportURLRequestJob(net::URLRequest* request, |
| 41 net::NetworkDelegate* network_delegate); | 84 net::NetworkDelegate* network_delegate); |
| 42 ~DelayableCertReportURLRequestJob() override; | 85 ~DelayableCertReportURLRequestJob() override; |
| 43 | 86 |
| 44 base::WeakPtr<DelayableCertReportURLRequestJob> GetWeakPtr(); | 87 base::WeakPtr<DelayableCertReportURLRequestJob> GetWeakPtr(); |
| 45 | 88 |
| 46 // net::URLRequestJob methods: | 89 // net::URLRequestJob methods: |
| 47 void Start() override; | 90 void Start() override; |
| 48 int ReadRawData(net::IOBuffer* buf, int buf_size) override; | 91 int ReadRawData(net::IOBuffer* buf, int buf_size) override; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 70 const uint8_t* server_private_key); | 113 const uint8_t* server_private_key); |
| 71 ~CertReportJobInterceptor() override; | 114 ~CertReportJobInterceptor() override; |
| 72 | 115 |
| 73 // net::URLRequestInterceptor method: | 116 // net::URLRequestInterceptor method: |
| 74 net::URLRequestJob* MaybeInterceptRequest( | 117 net::URLRequestJob* MaybeInterceptRequest( |
| 75 net::URLRequest* request, | 118 net::URLRequest* request, |
| 76 net::NetworkDelegate* network_delegate) const override; | 119 net::NetworkDelegate* network_delegate) const override; |
| 77 | 120 |
| 78 // Sets the failure mode for reports. Must be called on the UI thread. | 121 // Sets the failure mode for reports. Must be called on the UI thread. |
| 79 void SetFailureMode(ReportSendingResult expected_report_result); | 122 void SetFailureMode(ReportSendingResult expected_report_result); |
| 80 // Resumes any hanging URL request. Must be called on the UI thread. | 123 // Resumes any hanging URL request and runs callback when the request |
| 81 void Resume(); | 124 // is resumed (i.e. response starts). Must be called on the UI thread. |
| 125 void Resume(const base::Closure& callback); |
| 82 | 126 |
| 83 // These must be called on the UI thread. | 127 // These must be called on the UI thread. |
| 84 const std::set<std::string>& successful_reports() const; | 128 const std::set<std::string>& successful_reports() const; |
| 85 const std::set<std::string>& failed_reports() const; | 129 const std::set<std::string>& failed_reports() const; |
| 86 const std::set<std::string>& delayed_reports() const; | 130 const std::set<std::string>& delayed_reports() const; |
| 87 void ClearObservedReports(); | 131 void ClearObservedReports(); |
| 88 | 132 |
| 133 // Waits for requests for |num_reports| reports to be created. Only used in |
| 134 // browser tests. Unit tests wait for requests to be destroyed instead. |
| 135 // Must be called on the UI thread. |
| 136 void WaitForReports(int num_reports); |
| 137 |
| 89 private: | 138 private: |
| 90 void SetFailureModeOnIOThread(ReportSendingResult expected_report_result); | 139 void SetFailureModeOnIOThread(ReportSendingResult expected_report_result); |
| 91 void ResumeOnIOThread(); | 140 void ResumeOnIOThread(); |
| 92 void RequestCreated(const std::string& uploaded_report, | 141 void RequestCreated(const std::string& uploaded_report, |
| 93 ReportSendingResult expected_report_result); | 142 ReportSendingResult expected_report_result); |
| 94 | 143 |
| 95 std::set<std::string> successful_reports_; | 144 std::set<std::string> successful_reports_; |
| 96 std::set<std::string> failed_reports_; | 145 std::set<std::string> failed_reports_; |
| 97 std::set<std::string> delayed_reports_; | 146 std::set<std::string> delayed_reports_; |
| 98 | 147 |
| 99 ReportSendingResult expected_report_result_; | 148 ReportSendingResult expected_report_result_; |
| 100 | 149 |
| 101 // Private key to decrypt certificate reports. | 150 // Private key to decrypt certificate reports. |
| 102 const uint8_t* server_private_key_; | 151 const uint8_t* server_private_key_; |
| 103 | 152 |
| 153 ReportWaitHelper wait_helper_; |
| 154 |
| 104 mutable base::WeakPtr<DelayableCertReportURLRequestJob> delayed_request_ = | 155 mutable base::WeakPtr<DelayableCertReportURLRequestJob> delayed_request_ = |
| 105 nullptr; | 156 nullptr; |
| 106 mutable base::WeakPtrFactory<CertReportJobInterceptor> weak_factory_; | 157 mutable base::WeakPtrFactory<CertReportJobInterceptor> weak_factory_; |
| 107 | 158 |
| 108 DISALLOW_COPY_AND_ASSIGN(CertReportJobInterceptor); | 159 DISALLOW_COPY_AND_ASSIGN(CertReportJobInterceptor); |
| 109 }; | 160 }; |
| 110 | 161 |
| 111 // A network delegate used to observe URL request destructions. The tests check | 162 // A network delegate used to observe URL request destructions. The tests check |
| 112 // that no outstanding URL request is present during tear down. | 163 // that no outstanding URL request is present during tear down. |
| 113 class CertificateReportingServiceTestNetworkDelegate | 164 class CertificateReportingServiceTestNetworkDelegate |
| 114 : public net::NetworkDelegateImpl { | 165 : public net::NetworkDelegateImpl { |
| 115 public: | 166 public: |
| 116 CertificateReportingServiceTestNetworkDelegate( | 167 CertificateReportingServiceTestNetworkDelegate( |
| 117 const base::Callback<void()>& url_request_destroyed_callback); | 168 const base::Callback<void()>& url_request_destroyed_callback); |
| 118 ~CertificateReportingServiceTestNetworkDelegate() override; | 169 ~CertificateReportingServiceTestNetworkDelegate() override; |
| 119 | 170 |
| 120 // net::NetworkDelegate method: | 171 // net::NetworkDelegate method: |
| 121 void OnURLRequestDestroyed(net::URLRequest* request) override; | 172 void OnURLRequestDestroyed(net::URLRequest* request) override; |
| 122 | 173 |
| 123 private: | 174 private: |
| 124 base::Callback<void()> url_request_destroyed_callback_; | 175 base::Callback<void()> url_request_destroyed_callback_; |
| 125 }; | 176 }; |
| 126 | 177 |
| 127 // Base class for CertificateReportingService tests. Sets up an interceptor to | 178 // Base class for CertificateReportingService tests. Sets up an interceptor to |
| 128 // keep track of reports that are being sent. | 179 // keep track of reports that are being sent. |
| 129 class CertificateReportingServiceTestBase { | 180 class CertificateReportingServiceTestHelper { |
| 130 protected: | 181 public: |
| 131 CertificateReportingServiceTestBase(); | 182 CertificateReportingServiceTestHelper(); |
| 132 virtual ~CertificateReportingServiceTestBase(); | 183 ~CertificateReportingServiceTestHelper(); |
| 133 | |
| 134 // Syntactic sugar for wrapping report expectations in a more readable way. | |
| 135 // Passed to WaitForRequestDeletions() as input. | |
| 136 // Example: | |
| 137 // The following expects report0 and report1 to be successfully sent and their | |
| 138 // URL requests to be deleted: | |
| 139 // WaitForRequestDeletions( | |
| 140 // ReportExpectation::Successful("report0, report1")); | |
| 141 struct ReportExpectation { | |
| 142 ReportExpectation(); | |
| 143 ReportExpectation(const ReportExpectation& other); | |
| 144 ~ReportExpectation(); | |
| 145 // Returns an expectation where all reports in |reports| succeed. | |
| 146 static ReportExpectation Successful(const std::set<std::string>& reports); | |
| 147 // Returns an expectation where all reports in |reports| fail. | |
| 148 static ReportExpectation Failed(const std::set<std::string>& reports); | |
| 149 // Returns an expectation where all reports in |reports| are delayed. | |
| 150 static ReportExpectation Delayed(const std::set<std::string>& reports); | |
| 151 std::set<std::string> successful_reports; | |
| 152 std::set<std::string> failed_reports; | |
| 153 std::set<std::string> delayed_reports; | |
| 154 }; | |
| 155 | 184 |
| 156 void SetUpInterceptor(); | 185 void SetUpInterceptor(); |
| 157 void TearDownInterceptor(); | |
| 158 | 186 |
| 159 // Changes the behavior of report uploads to fail, succeed or hang. | 187 // Changes the behavior of report uploads to fail, succeed or hang. |
| 160 void SetFailureMode(ReportSendingResult expected_report_result); | 188 void SetFailureMode(ReportSendingResult expected_report_result); |
| 161 | 189 |
| 162 // Resumes delayed report request. Failure mode should be REPORTS_DELAY when | 190 // Resumes delayed report request. Failure mode should be REPORTS_DELAY when |
| 163 // calling this method. | 191 // calling this method. |
| 164 void ResumeDelayedRequest(); | 192 void ResumeDelayedRequest(const base::Callback<void()>& callback); |
| 165 | |
| 166 // Waits for the URL requests for the expected reports to be destroyed. | |
| 167 // Doesn't block if all requests have already been destroyed. | |
| 168 void WaitForRequestsDestroyed(const ReportExpectation& expectation); | |
| 169 | 193 |
| 170 uint8_t* server_public_key(); | 194 uint8_t* server_public_key(); |
| 171 uint32_t server_public_key_version() const; | 195 uint32_t server_public_key_version() const; |
| 172 | 196 |
| 173 net::NetworkDelegate* network_delegate(); | |
| 174 | |
| 175 CertReportJobInterceptor* interceptor() { return url_request_interceptor_; } | 197 CertReportJobInterceptor* interceptor() { return url_request_interceptor_; } |
| 176 | 198 |
| 177 private: | 199 private: |
| 178 void SetUpInterceptorOnIOThread( | 200 void SetUpInterceptorOnIOThread(); |
| 179 std::unique_ptr<net::URLRequestInterceptor> url_request_interceptor); | |
| 180 void TearDownInterceptorOnIOThread(); | |
| 181 void OnURLRequestDestroyed(); | |
| 182 | 201 |
| 183 CertReportJobInterceptor* url_request_interceptor_; | 202 CertReportJobInterceptor* url_request_interceptor_; |
| 184 | 203 |
| 185 uint8_t server_public_key_[32]; | 204 uint8_t server_public_key_[32]; |
| 186 uint8_t server_private_key_[32]; | 205 uint8_t server_private_key_[32]; |
| 187 | 206 |
| 188 std::unique_ptr<CertificateReportingServiceTestNetworkDelegate> | 207 DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestHelper); |
| 189 network_delegate_; | |
| 190 | |
| 191 int num_request_deletions_to_wait_for_; | |
| 192 int num_deleted_requests_; | |
| 193 std::unique_ptr<base::RunLoop> run_loop_; | |
| 194 DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestBase); | |
| 195 }; | 208 }; |
| 196 | 209 |
| 197 } // namespace certificate_reporting_test_utils | 210 } // namespace certificate_reporting_test_utils |
| 198 | 211 |
| 199 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS
_H_ | 212 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS
_H_ |
| OLD | NEW |