| 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 <unordered_map> |
| 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 "base/threading/non_thread_safe.h" |
| 13 #include "chrome/browser/safe_browsing/certificate_reporting_service.h" | 13 #include "chrome/browser/safe_browsing/certificate_reporting_service.h" |
| 14 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread.h" |
| 15 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "net/base/network_delegate_impl.h" | 16 #include "net/base/network_delegate_impl.h" |
| 17 #include "net/url_request/url_request_interceptor.h" | 17 #include "net/url_request/url_request_interceptor.h" |
| 18 #include "net/url_request/url_request_job.h" | 18 #include "net/url_request/url_request_job.h" |
| 19 | 19 |
| 20 namespace net { | 20 namespace net { |
| 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 enum RetryStatus { |
| 27 NOT_RETRIED = 0, |
| 28 RETRIED = 1, |
| 29 }; |
| 30 |
| 31 typedef std::unordered_map<std::string, RetryStatus> ObservedReportMap; |
| 32 |
| 26 // Syntactic sugar for wrapping report expectations in a more readable way. | 33 // Syntactic sugar for wrapping report expectations in a more readable way. |
| 27 // Passed to WaitForRequestDeletions() as input. | 34 // Passed to WaitForRequestDeletions() as input. |
| 28 // Example: | 35 // Example: |
| 29 // The following expects report0 and report1 to be successfully sent and their | 36 // The following expects report0 and report1 to be successfully sent and their |
| 30 // URL requests to be deleted: | 37 // URL requests to be deleted: |
| 31 // WaitForRequestsDestroyed( | 38 // WaitForRequestsDestroyed( |
| 32 // ReportExpectation::Successful("report0, report1")); | 39 // ReportExpectation::Successful("report0, report1")); |
| 33 struct ReportExpectation { | 40 struct ReportExpectation { |
| 34 ReportExpectation(); | 41 ReportExpectation(); |
| 35 ReportExpectation(const ReportExpectation& other); | 42 ReportExpectation(const ReportExpectation& other); |
| 36 ~ReportExpectation(); | 43 ~ReportExpectation(); |
| 37 // Returns an expectation where all reports in |reports| succeed. | 44 // Returns an expectation where all reports in |reports| succeed. |
| 38 static ReportExpectation Successful(const std::set<std::string>& reports); | 45 static ReportExpectation Successful(const ObservedReportMap& reports); |
| 39 // Returns an expectation where all reports in |reports| fail. | 46 // Returns an expectation where all reports in |reports| fail. |
| 40 static ReportExpectation Failed(const std::set<std::string>& reports); | 47 static ReportExpectation Failed(const ObservedReportMap& reports); |
| 41 // Returns an expectation where all reports in |reports| are delayed. | 48 // Returns an expectation where all reports in |reports| are delayed. |
| 42 static ReportExpectation Delayed(const std::set<std::string>& reports); | 49 static ReportExpectation Delayed(const ObservedReportMap& reports); |
| 43 // Total number of reports expected. | 50 // Total number of reports expected. |
| 44 int num_reports() const; | 51 int num_reports() const; |
| 45 std::set<std::string> successful_reports; | 52 ObservedReportMap successful_reports; |
| 46 std::set<std::string> failed_reports; | 53 ObservedReportMap failed_reports; |
| 47 std::set<std::string> delayed_reports; | 54 ObservedReportMap delayed_reports; |
| 48 }; | 55 }; |
| 49 | 56 |
| 50 // Failure mode of the report sending attempts. | 57 // Failure mode of the report sending attempts. |
| 51 enum ReportSendingResult { | 58 enum ReportSendingResult { |
| 52 // Report send attempts should be successful. | 59 // Report send attempts should be successful. |
| 53 REPORTS_SUCCESSFUL, | 60 REPORTS_SUCCESSFUL, |
| 54 // Report send attempts should fail. | 61 // Report send attempts should fail. |
| 55 REPORTS_FAIL, | 62 REPORTS_FAIL, |
| 56 // Report send attempts should hang until explicitly resumed. | 63 // Report send attempts should hang until explicitly resumed. |
| 57 REPORTS_DELAY, | 64 REPORTS_DELAY, |
| 58 }; | 65 }; |
| 59 | 66 |
| 60 // Helper class to wait for a number of events (e.g. request destroyed, report | 67 // Helper class to wait for a number of events (e.g. request destroyed, report |
| 61 // observed). | 68 // observed). |
| 62 class RequestObserver { | 69 class RequestObserver { |
| 63 public: | 70 public: |
| 64 RequestObserver(); | 71 RequestObserver(); |
| 65 ~RequestObserver(); | 72 ~RequestObserver(); |
| 66 | 73 |
| 67 // Waits for |num_request| requests to be created or destroyed, depending on | 74 // Waits for |num_request| requests to be created or destroyed, depending on |
| 68 // whichever one this class observes. | 75 // whichever one this class observes. |
| 69 void Wait(unsigned int num_events_to_wait_for); | 76 void Wait(unsigned int num_events_to_wait_for); |
| 70 | 77 |
| 71 // Called when a request created or destroyed, depending on whichever one this | 78 // Called when a request created or destroyed, depending on whichever one this |
| 72 // class observes. | 79 // class observes. |
| 73 void OnRequest(const std::string& serialized_report, | 80 void OnRequest(const std::string& serialized_report, |
| 74 ReportSendingResult report_type); | 81 ReportSendingResult report_type); |
| 75 | 82 |
| 76 // These must be called on the UI thread. | 83 // These must be called on the UI thread. |
| 77 const std::set<std::string>& successful_reports() const; | 84 const ObservedReportMap& successful_reports() const; |
| 78 const std::set<std::string>& failed_reports() const; | 85 const ObservedReportMap& failed_reports() const; |
| 79 const std::set<std::string>& delayed_reports() const; | 86 const ObservedReportMap& delayed_reports() const; |
| 80 void ClearObservedReports(); | 87 void ClearObservedReports(); |
| 81 | 88 |
| 82 private: | 89 private: |
| 83 unsigned int num_events_to_wait_for_; | 90 unsigned int num_events_to_wait_for_; |
| 84 unsigned int num_received_events_; | 91 unsigned int num_received_events_; |
| 85 std::unique_ptr<base::RunLoop> run_loop_; | 92 std::unique_ptr<base::RunLoop> run_loop_; |
| 86 | 93 |
| 87 std::set<std::string> successful_reports_; | 94 ObservedReportMap successful_reports_; |
| 88 std::set<std::string> failed_reports_; | 95 ObservedReportMap failed_reports_; |
| 89 std::set<std::string> delayed_reports_; | 96 ObservedReportMap delayed_reports_; |
| 90 }; | 97 }; |
| 91 | 98 |
| 92 // A URLRequestJob that can be delayed until Resume() is called. Returns an | 99 // A URLRequestJob that can be delayed until Resume() is called. Returns an |
| 93 // empty response. If Resume() is called before a request is made, then the | 100 // empty response. If Resume() is called before a request is made, then the |
| 94 // request will not be delayed. If not delayed, it can return a failed or a | 101 // request will not be delayed. If not delayed, it can return a failed or a |
| 95 // successful URL request job. | 102 // successful URL request job. |
| 96 class DelayableCertReportURLRequestJob : public net::URLRequestJob, | 103 class DelayableCertReportURLRequestJob : public net::URLRequestJob, |
| 97 public base::NonThreadSafe { | 104 public base::NonThreadSafe { |
| 98 public: | 105 public: |
| 99 DelayableCertReportURLRequestJob( | 106 DelayableCertReportURLRequestJob( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 // Resumes any hanging URL request and runs callback when the request | 152 // Resumes any hanging URL request and runs callback when the request |
| 146 // is resumed (i.e. response starts). Must be called on the UI thread. | 153 // is resumed (i.e. response starts). Must be called on the UI thread. |
| 147 void Resume(); | 154 void Resume(); |
| 148 | 155 |
| 149 RequestObserver* request_created_observer() const; | 156 RequestObserver* request_created_observer() const; |
| 150 RequestObserver* request_destroyed_observer() const; | 157 RequestObserver* request_destroyed_observer() const; |
| 151 | 158 |
| 152 private: | 159 private: |
| 153 void SetFailureModeOnIOThread(ReportSendingResult expected_report_result); | 160 void SetFailureModeOnIOThread(ReportSendingResult expected_report_result); |
| 154 void ResumeOnIOThread(); | 161 void ResumeOnIOThread(); |
| 155 void RequestCreated(const std::string& uploaded_report, | 162 void RequestCreated(const std::string& serialized_report, |
| 156 ReportSendingResult expected_report_result) const; | 163 ReportSendingResult expected_report_result) const; |
| 157 void RequestDestructed(const std::string& uploaded_report, | 164 void RequestDestructed(const std::string& serialized_report, |
| 158 ReportSendingResult expected_report_result) const; | 165 ReportSendingResult expected_report_result) const; |
| 159 | 166 |
| 160 mutable std::set<std::string> successful_reports_; | 167 mutable std::set<std::string> successful_reports_; |
| 161 mutable std::set<std::string> failed_reports_; | 168 mutable std::set<std::string> failed_reports_; |
| 162 mutable std::set<std::string> delayed_reports_; | 169 mutable std::set<std::string> delayed_reports_; |
| 163 | 170 |
| 164 ReportSendingResult expected_report_result_; | 171 ReportSendingResult expected_report_result_; |
| 165 | 172 |
| 166 // Private key to decrypt certificate reports. | 173 // Private key to decrypt certificate reports. |
| 167 const uint8_t* server_private_key_; | 174 const uint8_t* server_private_key_; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 | 237 |
| 231 uint8_t server_public_key_[32]; | 238 uint8_t server_public_key_[32]; |
| 232 uint8_t server_private_key_[32]; | 239 uint8_t server_private_key_[32]; |
| 233 | 240 |
| 234 DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestHelper); | 241 DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestHelper); |
| 235 }; | 242 }; |
| 236 | 243 |
| 237 } // namespace certificate_reporting_test_utils | 244 } // namespace certificate_reporting_test_utils |
| 238 | 245 |
| 239 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS
_H_ | 246 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS
_H_ |
| OLD | NEW |