Chromium Code Reviews| Index: chrome/browser/safe_browsing/certificate_reporting_service_test_utils.h |
| diff --git a/chrome/browser/safe_browsing/certificate_reporting_service_test_utils.h b/chrome/browser/safe_browsing/certificate_reporting_service_test_utils.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..296a77685557e3dd05d65b2b78ba8394a0d50067 |
| --- /dev/null |
| +++ b/chrome/browser/safe_browsing/certificate_reporting_service_test_utils.h |
| @@ -0,0 +1,188 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS_H_ |
| +#define CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS_H_ |
| + |
| +#include <set> |
| + |
| +#include "base/macros.h" |
| +#include "base/run_loop.h" |
| +#include "chrome/browser/safe_browsing/certificate_reporting_service.h" |
| +#include "content/public/test/test_browser_thread.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "net/base/network_delegate_impl.h" |
| +#include "net/url_request/url_request_interceptor.h" |
| +#include "net/url_request/url_request_job.h" |
| + |
| +namespace net { |
| +class NetworkDelegate; |
| +} |
| + |
| +namespace certificate_reporting_test_utils { |
| + |
| +// Failure mode of the report sending attempts. |
| +enum ReportSendingResult { |
| + // Report send attempts should be successful. |
| + REPORTS_SUCCESSFUL = 0, |
|
estark
2016/12/07 00:42:55
nit: explicit values aren't necessary (unless you'
meacer
2016/12/07 21:37:33
Done.
|
| + // Report send attempts should fail. |
| + REPORTS_FAIL = 1, |
| + // Report send attempts should hang until explicitly resumed. |
| + REPORTS_DELAY = 2, |
| +}; |
| + |
| +// A URLRequestJob that serves valid time server responses, but delays |
|
estark
2016/12/07 00:42:55
time server => report server
I can tell where you
meacer
2016/12/07 21:37:33
I tried to erase all traces but apparently failed
|
| +// them until Resume() is called. If Resume() is called before a request |
| +// is made, then the request will not be delayed. |
| +class DelayableCertReportURLRequestJob : public net::URLRequestJob { |
| + public: |
| + DelayableCertReportURLRequestJob(net::URLRequest* request, |
| + net::NetworkDelegate* network_delegate); |
| + ~DelayableCertReportURLRequestJob() override; |
| + |
| + base::WeakPtr<DelayableCertReportURLRequestJob> GetWeakPtr() { |
|
estark
2016/12/07 00:42:55
nit: maybe don't inline this since nothing else is
meacer
2016/12/07 21:37:33
Done.
|
| + return weak_factory_.GetWeakPtr(); |
| + } |
| + |
| + // net::URLRequestJob methods: |
| + void Start() override; |
| + int ReadRawData(net::IOBuffer* buf, int buf_size) override; |
| + int GetResponseCode() const override; |
| + void GetResponseInfo(net::HttpResponseInfo* info) override; |
| + |
| + // Resumes a previously started request that was delayed. If no |
| + // request has been started yet, then when Start() is called it will |
| + // not delay. |
| + void Resume(); |
| + |
| + private: |
| + bool delayed_ = true; |
| + bool started_ = false; |
| + base::WeakPtrFactory<DelayableCertReportURLRequestJob> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DelayableCertReportURLRequestJob); |
| +}; |
| + |
| +// A job interceptor that returns a failed or succesful url request job. Used to |
| +// simulate report uploads that fail or succeed. Two different instances of this |
| +// class is used by the tests; one for failed reports and one for successful |
|
estark
2016/12/07 00:42:55
nit: is => are
meacer
2016/12/07 21:37:33
Old comment, removed. There is a single instance o
|
| +// reports. Can keep track of the report ids it observes. |
| +class CertReportJobInterceptor : public net::URLRequestInterceptor { |
| + public: |
| + CertReportJobInterceptor(ReportSendingResult expected_report_result, |
| + const uint8_t* server_private_key); |
| + ~CertReportJobInterceptor() override; |
| + |
| + // net::URLRequestInterceptor method: |
| + net::URLRequestJob* MaybeInterceptRequest( |
| + net::URLRequest* request, |
| + net::NetworkDelegate* network_delegate) const override; |
| + |
| + // Sets the failure mode for reports. |
| + void SetFailureMode(ReportSendingResult expected_report_result); |
| + // Resumes any hanging URL request. |
| + void Resume(); |
| + |
| + void WaitForRequests(int requests_to_wait_for); |
| + |
| + const std::set<std::string>& successful_reports() const; |
| + const std::set<std::string>& failed_reports() const; |
| + const std::set<std::string>& delayed_reports() const; |
| + |
| + void Clear(); |
| + |
| + private: |
| + void SetFailureModeOnIOThread(ReportSendingResult expected_report_result); |
| + void ResumeOnIOThread(); |
| + void RequestCreated(const std::string& uploaded_report, |
| + ReportSendingResult expected_report_result); |
| + |
| + std::set<std::string> successful_reports_; |
| + std::set<std::string> failed_reports_; |
| + std::set<std::string> delayed_reports_; |
| + |
| + ReportSendingResult expected_report_result_; |
| + |
| + // Private key to decrypt certificate reports. |
| + const uint8_t* server_private_key_; |
| + |
| + mutable base::WeakPtr<DelayableCertReportURLRequestJob> delayed_request_ = |
| + nullptr; |
| + mutable base::WeakPtrFactory<CertReportJobInterceptor> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CertReportJobInterceptor); |
| +}; |
| + |
| +class CertificateReportingServiceTestNetworkDelegate |
|
estark
2016/12/07 00:42:55
document
meacer
2016/12/07 21:37:33
Done.
|
| + : public net::NetworkDelegateImpl { |
| + public: |
| + CertificateReportingServiceTestNetworkDelegate( |
| + const base::Callback<void(net::URLRequest* request)>& |
| + url_request_destroyed_callback); |
| + ~CertificateReportingServiceTestNetworkDelegate() override; |
| + |
| + void OnURLRequestDestroyed(net::URLRequest* request) override; |
| + |
| + private: |
| + base::Callback<void(net::URLRequest* request)> |
| + url_request_destroyed_callback_; |
| +}; |
| + |
| +class CertificateReportingServiceTestBase { |
|
estark
2016/12/07 00:42:55
document
meacer
2016/12/07 21:37:33
Done.
|
| + public: |
| + CertificateReportingServiceTestBase(); |
| + virtual ~CertificateReportingServiceTestBase(); |
| + |
| + protected: |
| + struct ReportExpectation { |
|
estark
2016/12/07 00:42:55
document plz
(In particular I can't figure out fro
meacer
2016/12/07 21:37:33
Done.
|
| + ReportExpectation(); |
| + ~ReportExpectation(); |
| + ReportExpectation& Successful(const std::set<std::string>& reports); |
| + ReportExpectation& Failed(const std::set<std::string>& reports); |
| + ReportExpectation& Delayed(const std::set<std::string>& reports); |
| + std::set<std::string> successful_reports; |
| + std::set<std::string> failed_reports; |
| + std::set<std::string> delayed_reports; |
| + }; |
| + |
| + void SetUpInterceptor(); |
| + |
| + // Changes the behavior of report uploads to fail, succeed or hang. |
| + void SetFailureMode(ReportSendingResult expected_report_result); |
| + |
| + // Resumes delayed report request. Failure mode should be REPORTS_DELAY when |
| + // calling this method. |
| + void ResumeDelayedRequest(); |
| + |
| + uint8_t* server_public_key() { return server_public_key_; } |
| + uint32_t server_public_key_version() const; |
| + |
| + net::NetworkDelegate* network_delegate() { return &network_delegate_; } |
| + |
| + void WaitForRequestDeletions(const ReportExpectation& expectation); |
|
estark
2016/12/07 00:42:55
nit: document that this handles the case where all
meacer
2016/12/07 21:37:33
Done.
|
| + |
| + CertReportJobInterceptor* interceptor() { return url_request_interceptor_; } |
| + |
| + private: |
| + void WaitForReports_(const std::set<std::string>& reports); |
| + |
| + void OnRequestDeleted(net::URLRequest* request); |
| + void ClearObservedReports(); |
| + |
| + CertReportJobInterceptor* url_request_interceptor_; |
| + |
| + uint8_t server_public_key_[32]; |
| + uint8_t server_private_key_[32]; |
| + |
| + CertificateReportingServiceTestNetworkDelegate network_delegate_; |
| + |
| + int num_request_deletions_to_wait_for_; |
| + int num_deleted_requests_; |
| + std::unique_ptr<base::RunLoop> run_loop_; |
| + DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestBase); |
| +}; |
| + |
| +} // namespace certificate_reporting_test_utils |
| + |
| +#endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS_H_ |