Chromium Code Reviews| 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_set> |
|
estark
2017/01/13 23:26:22
looks like you're using normal <set> below; did yo
meacer
2017/01/17 22:24:27
Changed to 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 // Struct that wraps information about observed reports. | |
| 27 struct ObservedReport { | |
| 28 std::string hostname; | |
| 29 bool is_retried; | |
| 30 | |
| 31 ObservedReport(const std::string& hostname, bool is_retried); | |
| 32 | |
| 33 // For std::set: | |
| 34 bool operator<(const ObservedReport& other) const; | |
| 35 bool operator==(const ObservedReport& other) const; | |
| 36 | |
| 37 static ObservedReport Retried(const std::string& hostname); | |
| 38 static ObservedReport NotRetried(const std::string& hostname); | |
| 39 }; | |
| 40 | |
| 26 // Syntactic sugar for wrapping report expectations in a more readable way. | 41 // Syntactic sugar for wrapping report expectations in a more readable way. |
| 27 // Passed to WaitForRequestDeletions() as input. | 42 // Passed to WaitForRequestDeletions() as input. |
| 28 // Example: | 43 // Example: |
| 29 // The following expects report0 and report1 to be successfully sent and their | 44 // The following expects report0 and report1 to be successfully sent and their |
| 30 // URL requests to be deleted: | 45 // URL requests to be deleted: |
| 31 // WaitForRequestsDestroyed( | 46 // WaitForRequestsDestroyed( |
| 32 // ReportExpectation::Successful("report0, report1")); | 47 // ReportExpectation::Successful("report0, report1")); |
| 33 struct ReportExpectation { | 48 struct ReportExpectation { |
| 34 ReportExpectation(); | 49 ReportExpectation(); |
| 35 ReportExpectation(const ReportExpectation& other); | 50 ReportExpectation(const ReportExpectation& other); |
| 36 ~ReportExpectation(); | 51 ~ReportExpectation(); |
| 37 // Returns an expectation where all reports in |reports| succeed. | 52 // Returns an expectation where all reports in |reports| succeed. |
| 38 static ReportExpectation Successful(const std::set<std::string>& reports); | 53 static ReportExpectation Successful(const std::set<ObservedReport>& reports); |
| 39 // Returns an expectation where all reports in |reports| fail. | 54 // Returns an expectation where all reports in |reports| fail. |
| 40 static ReportExpectation Failed(const std::set<std::string>& reports); | 55 static ReportExpectation Failed(const std::set<ObservedReport>& reports); |
| 41 // Returns an expectation where all reports in |reports| are delayed. | 56 // Returns an expectation where all reports in |reports| are delayed. |
| 42 static ReportExpectation Delayed(const std::set<std::string>& reports); | 57 static ReportExpectation Delayed(const std::set<ObservedReport>& reports); |
| 43 // Total number of reports expected. | 58 // Total number of reports expected. |
| 44 int num_reports() const; | 59 int num_reports() const; |
| 45 std::set<std::string> successful_reports; | 60 std::set<ObservedReport> successful_reports; |
| 46 std::set<std::string> failed_reports; | 61 std::set<ObservedReport> failed_reports; |
| 47 std::set<std::string> delayed_reports; | 62 std::set<ObservedReport> delayed_reports; |
| 48 }; | 63 }; |
| 49 | 64 |
| 50 // Failure mode of the report sending attempts. | 65 // Failure mode of the report sending attempts. |
| 51 enum ReportSendingResult { | 66 enum ReportSendingResult { |
| 52 // Report send attempts should be successful. | 67 // Report send attempts should be successful. |
| 53 REPORTS_SUCCESSFUL, | 68 REPORTS_SUCCESSFUL, |
| 54 // Report send attempts should fail. | 69 // Report send attempts should fail. |
| 55 REPORTS_FAIL, | 70 REPORTS_FAIL, |
| 56 // Report send attempts should hang until explicitly resumed. | 71 // Report send attempts should hang until explicitly resumed. |
| 57 REPORTS_DELAY, | 72 REPORTS_DELAY, |
| 58 }; | 73 }; |
| 59 | 74 |
| 60 // Helper class to wait for a number of events (e.g. request destroyed, report | 75 // Helper class to wait for a number of events (e.g. request destroyed, report |
| 61 // observed). | 76 // observed). |
| 62 class RequestObserver { | 77 class RequestObserver { |
| 63 public: | 78 public: |
| 64 RequestObserver(); | 79 RequestObserver(); |
| 65 ~RequestObserver(); | 80 ~RequestObserver(); |
| 66 | 81 |
| 67 // Waits for |num_request| requests to be created or destroyed, depending on | 82 // Waits for |num_request| requests to be created or destroyed, depending on |
| 68 // whichever one this class observes. | 83 // whichever one this class observes. |
| 69 void Wait(unsigned int num_events_to_wait_for); | 84 void Wait(unsigned int num_events_to_wait_for); |
| 70 | 85 |
| 71 // Called when a request created or destroyed, depending on whichever one this | 86 // Called when a request created or destroyed, depending on whichever one this |
| 72 // class observes. | 87 // class observes. |
| 73 void OnRequest(const std::string& serialized_report, | 88 void OnRequest(const ObservedReport& observed_report, |
| 74 ReportSendingResult report_type); | 89 ReportSendingResult report_type); |
| 75 | 90 |
| 76 // These must be called on the UI thread. | 91 // These must be called on the UI thread. |
| 77 const std::set<std::string>& successful_reports() const; | 92 const std::set<ObservedReport>& successful_reports() const; |
| 78 const std::set<std::string>& failed_reports() const; | 93 const std::set<ObservedReport>& failed_reports() const; |
| 79 const std::set<std::string>& delayed_reports() const; | 94 const std::set<ObservedReport>& delayed_reports() const; |
| 80 void ClearObservedReports(); | 95 void ClearObservedReports(); |
| 81 | 96 |
| 82 private: | 97 private: |
| 83 unsigned int num_events_to_wait_for_; | 98 unsigned int num_events_to_wait_for_; |
| 84 unsigned int num_received_events_; | 99 unsigned int num_received_events_; |
| 85 std::unique_ptr<base::RunLoop> run_loop_; | 100 std::unique_ptr<base::RunLoop> run_loop_; |
| 86 | 101 |
| 87 std::set<std::string> successful_reports_; | 102 std::set<ObservedReport> successful_reports_; |
| 88 std::set<std::string> failed_reports_; | 103 std::set<ObservedReport> failed_reports_; |
| 89 std::set<std::string> delayed_reports_; | 104 std::set<ObservedReport> delayed_reports_; |
| 90 }; | 105 }; |
| 91 | 106 |
| 92 // A URLRequestJob that can be delayed until Resume() is called. Returns an | 107 // 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 | 108 // 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 | 109 // request will not be delayed. If not delayed, it can return a failed or a |
| 95 // successful URL request job. | 110 // successful URL request job. |
| 96 class DelayableCertReportURLRequestJob : public net::URLRequestJob, | 111 class DelayableCertReportURLRequestJob : public net::URLRequestJob, |
| 97 public base::NonThreadSafe { | 112 public base::NonThreadSafe { |
| 98 public: | 113 public: |
| 99 DelayableCertReportURLRequestJob( | 114 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 | 160 // 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. | 161 // is resumed (i.e. response starts). Must be called on the UI thread. |
| 147 void Resume(); | 162 void Resume(); |
| 148 | 163 |
| 149 RequestObserver* request_created_observer() const; | 164 RequestObserver* request_created_observer() const; |
| 150 RequestObserver* request_destroyed_observer() const; | 165 RequestObserver* request_destroyed_observer() const; |
| 151 | 166 |
| 152 private: | 167 private: |
| 153 void SetFailureModeOnIOThread(ReportSendingResult expected_report_result); | 168 void SetFailureModeOnIOThread(ReportSendingResult expected_report_result); |
| 154 void ResumeOnIOThread(); | 169 void ResumeOnIOThread(); |
| 155 void RequestCreated(const std::string& uploaded_report, | 170 void RequestCreated(const std::string& serialized_report, |
| 156 ReportSendingResult expected_report_result) const; | 171 ReportSendingResult expected_report_result) const; |
| 157 void RequestDestructed(const std::string& uploaded_report, | 172 void RequestDestructed(const std::string& serialized_report, |
| 158 ReportSendingResult expected_report_result) const; | 173 ReportSendingResult expected_report_result) const; |
| 159 | 174 |
| 160 mutable std::set<std::string> successful_reports_; | 175 mutable std::set<std::string> successful_reports_; |
| 161 mutable std::set<std::string> failed_reports_; | 176 mutable std::set<std::string> failed_reports_; |
| 162 mutable std::set<std::string> delayed_reports_; | 177 mutable std::set<std::string> delayed_reports_; |
| 163 | 178 |
| 164 ReportSendingResult expected_report_result_; | 179 ReportSendingResult expected_report_result_; |
| 165 | 180 |
| 166 // Private key to decrypt certificate reports. | 181 // Private key to decrypt certificate reports. |
| 167 const uint8_t* server_private_key_; | 182 const uint8_t* server_private_key_; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 | 245 |
| 231 uint8_t server_public_key_[32]; | 246 uint8_t server_public_key_[32]; |
| 232 uint8_t server_private_key_[32]; | 247 uint8_t server_private_key_[32]; |
| 233 | 248 |
| 234 DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestHelper); | 249 DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestHelper); |
| 235 }; | 250 }; |
| 236 | 251 |
| 237 } // namespace certificate_reporting_test_utils | 252 } // namespace certificate_reporting_test_utils |
| 238 | 253 |
| 239 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS _H_ | 254 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS _H_ |
| OLD | NEW |