| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS_H_ |
| 6 #define CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS_H_ |
| 7 |
| 8 #include <set> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "base/run_loop.h" |
| 12 #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_bundle.h" |
| 15 #include "net/base/network_delegate_impl.h" |
| 16 #include "net/url_request/url_request_interceptor.h" |
| 17 #include "net/url_request/url_request_job.h" |
| 18 |
| 19 namespace net { |
| 20 class NetworkDelegate; |
| 21 } |
| 22 |
| 23 namespace certificate_reporting_test_utils { |
| 24 |
| 25 // Failure mode of the report sending attempts. |
| 26 enum ReportSendingResult { |
| 27 // Report send attempts should be successful. |
| 28 REPORTS_SUCCESSFUL, |
| 29 // Report send attempts should fail. |
| 30 REPORTS_FAIL, |
| 31 // Report send attempts should hang until explicitly resumed. |
| 32 REPORTS_DELAY, |
| 33 }; |
| 34 |
| 35 // 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 |
| 37 // request will not be delayed. |
| 38 class DelayableCertReportURLRequestJob : public net::URLRequestJob { |
| 39 public: |
| 40 DelayableCertReportURLRequestJob(net::URLRequest* request, |
| 41 net::NetworkDelegate* network_delegate); |
| 42 ~DelayableCertReportURLRequestJob() override; |
| 43 |
| 44 base::WeakPtr<DelayableCertReportURLRequestJob> GetWeakPtr(); |
| 45 |
| 46 // net::URLRequestJob methods: |
| 47 void Start() override; |
| 48 int ReadRawData(net::IOBuffer* buf, int buf_size) override; |
| 49 int GetResponseCode() const override; |
| 50 void GetResponseInfo(net::HttpResponseInfo* info) override; |
| 51 |
| 52 // Resumes a previously started request that was delayed. If no |
| 53 // request has been started yet, then when Start() is called it will |
| 54 // not delay. |
| 55 void Resume(); |
| 56 |
| 57 private: |
| 58 bool delayed_ = true; |
| 59 bool started_ = false; |
| 60 base::WeakPtrFactory<DelayableCertReportURLRequestJob> weak_factory_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(DelayableCertReportURLRequestJob); |
| 63 }; |
| 64 |
| 65 // A job interceptor that returns a failed, succesful or delayed request job. |
| 66 // Used to simulate report uploads that fail, succeed or hang. |
| 67 class CertReportJobInterceptor : public net::URLRequestInterceptor { |
| 68 public: |
| 69 CertReportJobInterceptor(ReportSendingResult expected_report_result, |
| 70 const uint8_t* server_private_key); |
| 71 ~CertReportJobInterceptor() override; |
| 72 |
| 73 // net::URLRequestInterceptor method: |
| 74 net::URLRequestJob* MaybeInterceptRequest( |
| 75 net::URLRequest* request, |
| 76 net::NetworkDelegate* network_delegate) const override; |
| 77 |
| 78 // Sets the failure mode for reports. Must be called on the UI thread. |
| 79 void SetFailureMode(ReportSendingResult expected_report_result); |
| 80 // Resumes any hanging URL request. Must be called on the UI thread. |
| 81 void Resume(); |
| 82 |
| 83 // These must be called on the UI thread. |
| 84 const std::set<std::string>& successful_reports() const; |
| 85 const std::set<std::string>& failed_reports() const; |
| 86 const std::set<std::string>& delayed_reports() const; |
| 87 void ClearObservedReports(); |
| 88 |
| 89 private: |
| 90 void SetFailureModeOnIOThread(ReportSendingResult expected_report_result); |
| 91 void ResumeOnIOThread(); |
| 92 void RequestCreated(const std::string& uploaded_report, |
| 93 ReportSendingResult expected_report_result); |
| 94 |
| 95 std::set<std::string> successful_reports_; |
| 96 std::set<std::string> failed_reports_; |
| 97 std::set<std::string> delayed_reports_; |
| 98 |
| 99 ReportSendingResult expected_report_result_; |
| 100 |
| 101 // Private key to decrypt certificate reports. |
| 102 const uint8_t* server_private_key_; |
| 103 |
| 104 mutable base::WeakPtr<DelayableCertReportURLRequestJob> delayed_request_ = |
| 105 nullptr; |
| 106 mutable base::WeakPtrFactory<CertReportJobInterceptor> weak_factory_; |
| 107 |
| 108 DISALLOW_COPY_AND_ASSIGN(CertReportJobInterceptor); |
| 109 }; |
| 110 |
| 111 // A network delegate used to observe URL request destructions. The tests check |
| 112 // that no outstanding URL request is present during tear down. |
| 113 class CertificateReportingServiceTestNetworkDelegate |
| 114 : public net::NetworkDelegateImpl { |
| 115 public: |
| 116 CertificateReportingServiceTestNetworkDelegate( |
| 117 const base::Callback<void()>& url_request_destroyed_callback); |
| 118 ~CertificateReportingServiceTestNetworkDelegate() override; |
| 119 |
| 120 // net::NetworkDelegate method: |
| 121 void OnURLRequestDestroyed(net::URLRequest* request) override; |
| 122 |
| 123 private: |
| 124 base::Callback<void()> url_request_destroyed_callback_; |
| 125 }; |
| 126 |
| 127 // Base class for CertificateReportingService tests. Sets up an interceptor to |
| 128 // keep track of reports that are being sent. |
| 129 class CertificateReportingServiceTestBase { |
| 130 protected: |
| 131 CertificateReportingServiceTestBase(); |
| 132 virtual ~CertificateReportingServiceTestBase(); |
| 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 |
| 156 void SetUpInterceptor(); |
| 157 void TearDownInterceptor(); |
| 158 |
| 159 // Changes the behavior of report uploads to fail, succeed or hang. |
| 160 void SetFailureMode(ReportSendingResult expected_report_result); |
| 161 |
| 162 // Resumes delayed report request. Failure mode should be REPORTS_DELAY when |
| 163 // calling this method. |
| 164 void ResumeDelayedRequest(); |
| 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 |
| 170 uint8_t* server_public_key(); |
| 171 uint32_t server_public_key_version() const; |
| 172 |
| 173 net::NetworkDelegate* network_delegate(); |
| 174 |
| 175 CertReportJobInterceptor* interceptor() { return url_request_interceptor_; } |
| 176 |
| 177 private: |
| 178 void SetUpInterceptorOnIOThread( |
| 179 std::unique_ptr<net::URLRequestInterceptor> url_request_interceptor); |
| 180 void TearDownInterceptorOnIOThread(); |
| 181 void OnURLRequestDestroyed(); |
| 182 |
| 183 CertReportJobInterceptor* url_request_interceptor_; |
| 184 |
| 185 uint8_t server_public_key_[32]; |
| 186 uint8_t server_private_key_[32]; |
| 187 |
| 188 std::unique_ptr<CertificateReportingServiceTestNetworkDelegate> |
| 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 }; |
| 196 |
| 197 } // namespace certificate_reporting_test_utils |
| 198 |
| 199 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS
_H_ |
| OLD | NEW |