| 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" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 class NetworkDelegate; | 21 class NetworkDelegate; |
| 22 } | 22 } |
| 23 | 23 |
| 24 namespace certificate_reporting_test_utils { | 24 namespace certificate_reporting_test_utils { |
| 25 | 25 |
| 26 // Syntactic sugar for wrapping report expectations in a more readable way. | 26 // Syntactic sugar for wrapping report expectations in a more readable way. |
| 27 // Passed to WaitForRequestDeletions() as input. | 27 // Passed to WaitForRequestDeletions() as input. |
| 28 // Example: | 28 // Example: |
| 29 // The following expects report0 and report1 to be successfully sent and their | 29 // The following expects report0 and report1 to be successfully sent and their |
| 30 // URL requests to be deleted: | 30 // URL requests to be deleted: |
| 31 // WaitForRequestDeletions( | 31 // WaitForRequestsDestroyed( |
| 32 // ReportExpectation::Successful("report0, report1")); | 32 // ReportExpectation::Successful("report0, report1")); |
| 33 struct ReportExpectation { | 33 struct ReportExpectation { |
| 34 ReportExpectation(); | 34 ReportExpectation(); |
| 35 ReportExpectation(const ReportExpectation& other); | 35 ReportExpectation(const ReportExpectation& other); |
| 36 ~ReportExpectation(); | 36 ~ReportExpectation(); |
| 37 // Returns an expectation where all reports in |reports| succeed. | 37 // Returns an expectation where all reports in |reports| succeed. |
| 38 static ReportExpectation Successful(const std::set<std::string>& reports); | 38 static ReportExpectation Successful(const std::set<std::string>& reports); |
| 39 // Returns an expectation where all reports in |reports| fail. | 39 // Returns an expectation where all reports in |reports| fail. |
| 40 static ReportExpectation Failed(const std::set<std::string>& reports); | 40 static ReportExpectation Failed(const std::set<std::string>& reports); |
| 41 // Returns an expectation where all reports in |reports| are delayed. | 41 // Returns an expectation where all reports in |reports| are delayed. |
| 42 static ReportExpectation Delayed(const std::set<std::string>& reports); | 42 static ReportExpectation Delayed(const std::set<std::string>& reports); |
| 43 // Total number of reports expected. | 43 // Total number of reports expected. |
| 44 int num_reports() const; | 44 int num_reports() const; |
| 45 std::set<std::string> successful_reports; | 45 std::set<std::string> successful_reports; |
| 46 std::set<std::string> failed_reports; | 46 std::set<std::string> failed_reports; |
| 47 std::set<std::string> delayed_reports; | 47 std::set<std::string> delayed_reports; |
| 48 }; | 48 }; |
| 49 | 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 | |
| 67 // Failure mode of the report sending attempts. | 50 // Failure mode of the report sending attempts. |
| 68 enum ReportSendingResult { | 51 enum ReportSendingResult { |
| 69 // Report send attempts should be successful. | 52 // Report send attempts should be successful. |
| 70 REPORTS_SUCCESSFUL, | 53 REPORTS_SUCCESSFUL, |
| 71 // Report send attempts should fail. | 54 // Report send attempts should fail. |
| 72 REPORTS_FAIL, | 55 REPORTS_FAIL, |
| 73 // Report send attempts should hang until explicitly resumed. | 56 // Report send attempts should hang until explicitly resumed. |
| 74 REPORTS_DELAY, | 57 REPORTS_DELAY, |
| 75 }; | 58 }; |
| 76 | 59 |
| 60 // Helper class to wait for a number of events (e.g. request destroyed, report |
| 61 // observed). |
| 62 class RequestObserver { |
| 63 public: |
| 64 RequestObserver(); |
| 65 ~RequestObserver(); |
| 66 |
| 67 // Waits for |num_request| requests to be created or destroyed, depending on |
| 68 // whichever one this class observes. |
| 69 void Wait(unsigned int num_events_to_wait_for); |
| 70 |
| 71 // Called when a request created or destroyed, depending on whichever one this |
| 72 // class observes. |
| 73 void OnRequest(const std::string& serialized_report, |
| 74 ReportSendingResult report_type); |
| 75 |
| 76 // These must be called on the UI thread. |
| 77 const std::set<std::string>& successful_reports() const; |
| 78 const std::set<std::string>& failed_reports() const; |
| 79 const std::set<std::string>& delayed_reports() const; |
| 80 void ClearObservedReports(); |
| 81 |
| 82 private: |
| 83 unsigned int num_events_to_wait_for_; |
| 84 unsigned int num_received_events_; |
| 85 std::unique_ptr<base::RunLoop> run_loop_; |
| 86 |
| 87 std::set<std::string> successful_reports_; |
| 88 std::set<std::string> failed_reports_; |
| 89 std::set<std::string> delayed_reports_; |
| 90 }; |
| 91 |
| 77 // A URLRequestJob that can be delayed until Resume() is called. Returns an | 92 // A URLRequestJob that can be delayed until Resume() is called. Returns an |
| 78 // empty response. If Resume() is called before a request is made, then the | 93 // empty response. If Resume() is called before a request is made, then the |
| 79 // request will not be delayed. | 94 // request will not be delayed. If not delayed, it can return a failed or a |
| 95 // successful URL request job. |
| 80 class DelayableCertReportURLRequestJob : public net::URLRequestJob, | 96 class DelayableCertReportURLRequestJob : public net::URLRequestJob, |
| 81 public base::NonThreadSafe { | 97 public base::NonThreadSafe { |
| 82 public: | 98 public: |
| 83 DelayableCertReportURLRequestJob(net::URLRequest* request, | 99 DelayableCertReportURLRequestJob( |
| 84 net::NetworkDelegate* network_delegate); | 100 bool delayed, |
| 101 bool should_fail, |
| 102 net::URLRequest* request, |
| 103 net::NetworkDelegate* network_delegate, |
| 104 const base::Callback<void()>& destruction_callback); |
| 85 ~DelayableCertReportURLRequestJob() override; | 105 ~DelayableCertReportURLRequestJob() override; |
| 86 | 106 |
| 87 base::WeakPtr<DelayableCertReportURLRequestJob> GetWeakPtr(); | 107 base::WeakPtr<DelayableCertReportURLRequestJob> GetWeakPtr(); |
| 88 | 108 |
| 89 // net::URLRequestJob methods: | 109 // net::URLRequestJob methods: |
| 90 void Start() override; | 110 void Start() override; |
| 91 int ReadRawData(net::IOBuffer* buf, int buf_size) override; | 111 int ReadRawData(net::IOBuffer* buf, int buf_size) override; |
| 92 int GetResponseCode() const override; | 112 int GetResponseCode() const override; |
| 93 void GetResponseInfo(net::HttpResponseInfo* info) override; | 113 void GetResponseInfo(net::HttpResponseInfo* info) override; |
| 94 | 114 |
| 95 // Resumes a previously started request that was delayed. If no | 115 // Resumes a previously started request that was delayed. If no |
| 96 // request has been started yet, then when Start() is called it will | 116 // request has been started yet, then when Start() is called it will |
| 97 // not delay. | 117 // not delay. |
| 98 void Resume(); | 118 void Resume(); |
| 99 | 119 |
| 100 private: | 120 private: |
| 101 bool delayed_ = true; | 121 bool delayed_; |
| 102 bool started_ = false; | 122 bool should_fail_; |
| 123 bool started_; |
| 124 base::Callback<void()> destruction_callback_; |
| 103 base::WeakPtrFactory<DelayableCertReportURLRequestJob> weak_factory_; | 125 base::WeakPtrFactory<DelayableCertReportURLRequestJob> weak_factory_; |
| 104 | 126 |
| 105 DISALLOW_COPY_AND_ASSIGN(DelayableCertReportURLRequestJob); | 127 DISALLOW_COPY_AND_ASSIGN(DelayableCertReportURLRequestJob); |
| 106 }; | 128 }; |
| 107 | 129 |
| 108 // A job interceptor that returns a failed, succesful or delayed request job. | 130 // A job interceptor that returns a failed, succesful or delayed request job. |
| 109 // Used to simulate report uploads that fail, succeed or hang. | 131 // Used to simulate report uploads that fail, succeed or hang. |
| 110 class CertReportJobInterceptor : public net::URLRequestInterceptor { | 132 class CertReportJobInterceptor : public net::URLRequestInterceptor { |
| 111 public: | 133 public: |
| 112 CertReportJobInterceptor(ReportSendingResult expected_report_result, | 134 CertReportJobInterceptor(ReportSendingResult expected_report_result, |
| 113 const uint8_t* server_private_key); | 135 const uint8_t* server_private_key); |
| 114 ~CertReportJobInterceptor() override; | 136 ~CertReportJobInterceptor() override; |
| 115 | 137 |
| 116 // net::URLRequestInterceptor method: | 138 // net::URLRequestInterceptor method: |
| 117 net::URLRequestJob* MaybeInterceptRequest( | 139 net::URLRequestJob* MaybeInterceptRequest( |
| 118 net::URLRequest* request, | 140 net::URLRequest* request, |
| 119 net::NetworkDelegate* network_delegate) const override; | 141 net::NetworkDelegate* network_delegate) const override; |
| 120 | 142 |
| 121 // Sets the failure mode for reports. Must be called on the UI thread. | 143 // Sets the failure mode for reports. Must be called on the UI thread. |
| 122 void SetFailureMode(ReportSendingResult expected_report_result); | 144 void SetFailureMode(ReportSendingResult expected_report_result); |
| 123 // Resumes any hanging URL request and runs callback when the request | 145 // Resumes any hanging URL request and runs callback when the request |
| 124 // is resumed (i.e. response starts). Must be called on the UI thread. | 146 // is resumed (i.e. response starts). Must be called on the UI thread. |
| 125 void Resume(const base::Closure& callback); | 147 void Resume(); |
| 126 | 148 |
| 127 // These must be called on the UI thread. | 149 RequestObserver* request_created_observer() const; |
| 128 const std::set<std::string>& successful_reports() const; | 150 RequestObserver* request_destroyed_observer() const; |
| 129 const std::set<std::string>& failed_reports() const; | |
| 130 const std::set<std::string>& delayed_reports() const; | |
| 131 void ClearObservedReports(); | |
| 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 | 151 |
| 138 private: | 152 private: |
| 139 void SetFailureModeOnIOThread(ReportSendingResult expected_report_result); | 153 void SetFailureModeOnIOThread(ReportSendingResult expected_report_result); |
| 140 void ResumeOnIOThread(); | 154 void ResumeOnIOThread(); |
| 141 void RequestCreated(const std::string& uploaded_report, | 155 void RequestCreated(const std::string& uploaded_report, |
| 142 ReportSendingResult expected_report_result); | 156 ReportSendingResult expected_report_result) const; |
| 157 void RequestDestructed(const std::string& uploaded_report, |
| 158 ReportSendingResult expected_report_result) const; |
| 143 | 159 |
| 144 std::set<std::string> successful_reports_; | 160 mutable std::set<std::string> successful_reports_; |
| 145 std::set<std::string> failed_reports_; | 161 mutable std::set<std::string> failed_reports_; |
| 146 std::set<std::string> delayed_reports_; | 162 mutable std::set<std::string> delayed_reports_; |
| 147 | 163 |
| 148 ReportSendingResult expected_report_result_; | 164 ReportSendingResult expected_report_result_; |
| 149 | 165 |
| 150 // Private key to decrypt certificate reports. | 166 // Private key to decrypt certificate reports. |
| 151 const uint8_t* server_private_key_; | 167 const uint8_t* server_private_key_; |
| 152 | 168 |
| 153 ReportWaitHelper wait_helper_; | 169 mutable RequestObserver request_created_observer_; |
| 170 mutable RequestObserver request_destroyed_observer_; |
| 154 | 171 |
| 155 mutable base::WeakPtr<DelayableCertReportURLRequestJob> delayed_request_ = | 172 mutable base::WeakPtr<DelayableCertReportURLRequestJob> delayed_request_ = |
| 156 nullptr; | 173 nullptr; |
| 157 mutable base::WeakPtrFactory<CertReportJobInterceptor> weak_factory_; | 174 mutable base::WeakPtrFactory<CertReportJobInterceptor> weak_factory_; |
| 158 | 175 |
| 159 DISALLOW_COPY_AND_ASSIGN(CertReportJobInterceptor); | 176 DISALLOW_COPY_AND_ASSIGN(CertReportJobInterceptor); |
| 160 }; | 177 }; |
| 161 | 178 |
| 162 // A network delegate used to observe URL request destructions. The tests check | 179 // Class to wait for the CertificateReportingService to reset. |
| 163 // that no outstanding URL request is present during tear down. | 180 class CertificateReportingServiceObserver { |
| 164 class CertificateReportingServiceTestNetworkDelegate | |
| 165 : public net::NetworkDelegateImpl { | |
| 166 public: | 181 public: |
| 167 CertificateReportingServiceTestNetworkDelegate( | 182 CertificateReportingServiceObserver(); |
| 168 const base::Callback<void()>& url_request_destroyed_callback); | 183 ~CertificateReportingServiceObserver(); |
| 169 ~CertificateReportingServiceTestNetworkDelegate() override; | |
| 170 | 184 |
| 171 // net::NetworkDelegate method: | 185 // Clears the state of the observer. Must be called before waiting each time. |
| 172 void OnURLRequestDestroyed(net::URLRequest* request) override; | 186 void Clear(); |
| 187 |
| 188 // Waits for the service to reset. |
| 189 void WaitForReset(); |
| 190 |
| 191 // Must be called when the service is reset. |
| 192 void OnServiceReset(); |
| 173 | 193 |
| 174 private: | 194 private: |
| 175 base::Callback<void()> url_request_destroyed_callback_; | 195 bool did_reset_ = false; |
| 196 std::unique_ptr<base::RunLoop> run_loop_; |
| 176 }; | 197 }; |
| 177 | 198 |
| 178 // Base class for CertificateReportingService tests. Sets up an interceptor to | 199 // Base class for CertificateReportingService tests. Sets up an interceptor to |
| 179 // keep track of reports that are being sent. | 200 // keep track of reports that are being sent. |
| 180 class CertificateReportingServiceTestHelper { | 201 class CertificateReportingServiceTestHelper { |
| 181 public: | 202 public: |
| 182 CertificateReportingServiceTestHelper(); | 203 CertificateReportingServiceTestHelper(); |
| 183 ~CertificateReportingServiceTestHelper(); | 204 ~CertificateReportingServiceTestHelper(); |
| 184 | 205 |
| 185 void SetUpInterceptor(); | 206 void SetUpInterceptor(); |
| 186 | 207 |
| 187 // Changes the behavior of report uploads to fail, succeed or hang. | 208 // Changes the behavior of report uploads to fail, succeed or hang. |
| 188 void SetFailureMode(ReportSendingResult expected_report_result); | 209 void SetFailureMode(ReportSendingResult expected_report_result); |
| 189 | 210 |
| 190 // Resumes delayed report request. Failure mode should be REPORTS_DELAY when | 211 // Resumes delayed report request. Failure mode should be REPORTS_DELAY when |
| 191 // calling this method. | 212 // calling this method. |
| 192 void ResumeDelayedRequest(const base::Callback<void()>& callback); | 213 void ResumeDelayedRequest(); |
| 214 |
| 215 void WaitForRequestsCreated(const ReportExpectation& expectation); |
| 216 void WaitForRequestsDestroyed(const ReportExpectation& expectation); |
| 217 |
| 218 // Checks that all requests are destroyed and that there are no in-flight |
| 219 // reports in |service|. |
| 220 void ExpectNoRequests(CertificateReportingService* service); |
| 193 | 221 |
| 194 uint8_t* server_public_key(); | 222 uint8_t* server_public_key(); |
| 195 uint32_t server_public_key_version() const; | 223 uint32_t server_public_key_version() const; |
| 196 | 224 |
| 225 private: |
| 197 CertReportJobInterceptor* interceptor() { return url_request_interceptor_; } | 226 CertReportJobInterceptor* interceptor() { return url_request_interceptor_; } |
| 198 | |
| 199 private: | |
| 200 void SetUpInterceptorOnIOThread(); | 227 void SetUpInterceptorOnIOThread(); |
| 201 | 228 |
| 202 CertReportJobInterceptor* url_request_interceptor_; | 229 CertReportJobInterceptor* url_request_interceptor_; |
| 203 | 230 |
| 204 uint8_t server_public_key_[32]; | 231 uint8_t server_public_key_[32]; |
| 205 uint8_t server_private_key_[32]; | 232 uint8_t server_private_key_[32]; |
| 206 | 233 |
| 207 DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestHelper); | 234 DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestHelper); |
| 208 }; | 235 }; |
| 209 | 236 |
| 210 } // namespace certificate_reporting_test_utils | 237 } // namespace certificate_reporting_test_utils |
| 211 | 238 |
| 212 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS
_H_ | 239 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS
_H_ |
| OLD | NEW |