Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1588)

Unified Diff: chrome/browser/safe_browsing/certificate_reporting_service_test_utils.h

Issue 2543523002: Implement main CertificateReportingService code and add unit tests. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..127c37ec90b711336e35de37f787a4ff8c05b2ac
--- /dev/null
+++ b/chrome/browser/safe_browsing/certificate_reporting_service_test_utils.h
@@ -0,0 +1,170 @@
+// 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 "chrome/browser/safe_browsing/certificate_reporting_service.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 {
+
+// This class observes and keeps track of events from
+// CertificateReportingService. It can also wait for |num_events_to_wait_for|
+// number of events to arrive.
+class ReportEventObserver : public CertificateReportingService::EventObserver {
+ public:
+ explicit ReportEventObserver(int num_events_to_wait_for);
+ ~ReportEventObserver() override;
+
+ // CertificateReportingService::EventObserver methods:
+ void OnSendAttempt(bool completed) override;
+ void OnSendComplete(int report_id, bool success) override;
+ void OnReset() override;
+
+ // Waits for |num_events_to_wait_for| number of events to arrive.
+ void WaitForEvents();
+
+ int num_cancelled_attempts() const { return num_cancelled_attempts_; }
+ int num_completed_attempts() const { return num_completed_attempts_; }
+ int num_resets() const { return num_resets_; }
+
+ // Returns the set of report ids that were successfully sent.
+ const std::set<int>& successful_report_ids() const {
+ return successful_report_ids_;
+ }
+ // Returns the set of report ids that were not sent.
+ const std::set<int>& failed_report_ids() const { return failed_report_ids_; }
+
+ private:
+ enum EventType {
+ EVENT_SEND_ATTEMPT = 0,
+ EVENT_SEND_COMPLETE,
+ EVENT_RESET,
+ };
+
+ void OnEventReceived(EventType event_type);
+
+ std::set<int> successful_report_ids_;
+ std::set<int> failed_report_ids_;
+
+ int num_cancelled_attempts_ = 0;
+ int num_completed_attempts_ = 0;
+ int num_resets_ = 0;
+
+ int num_events_received_ = 0;
+ int num_events_to_wait_for_ = 0;
+ bool waiting_ = false;
+
+ DISALLOW_COPY_AND_ASSIGN(ReportEventObserver);
+};
+
+// Failure mode of the report sending attempts.
+enum ReportSendingResult {
+ // Report send attempts should be successful.
+ REPORTS_SUCCESSFUL = 0,
+ // 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
+// 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() {
+ 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
+// reports. Can keep track of the report ids it observes.
+class CertReportJobInterceptor : public net::URLRequestInterceptor {
+ public:
+ explicit CertReportJobInterceptor(ReportSendingResult expected_report_result);
+ ~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();
+
+ private:
+ void SetFailureModeOnIOThread(ReportSendingResult expected_report_result);
+ void ResumeOnIOThread();
+
+ mutable base::WeakPtr<DelayableCertReportURLRequestJob> delayed_request_ =
+ nullptr;
+
+ ReportSendingResult expected_report_result_;
+ mutable base::WeakPtrFactory<CertReportJobInterceptor> weak_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(CertReportJobInterceptor);
+};
+
+class CertificateReportingServiceTestBase {
+ public:
+ CertificateReportingServiceTestBase();
+ virtual ~CertificateReportingServiceTestBase();
+
+ // Changes the behavior of report uploads to fail, succeed or hang.
+ void SetFailureMode(certificate_reporting_test_utils::ReportSendingResult
+ expected_report_result);
+
+ // Resumes delayed report request. Failure mode should be REPORTS_DELAY when
+ // calling this method.
+ void ResumeDelayedRequest();
+
+ protected:
+ void SetUpInterceptor();
+
+ private:
+ certificate_reporting_test_utils::CertReportJobInterceptor*
+ url_request_interceptor_;
+
+ DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestBase);
+};
+
+} // namespace certificate_reporting_test_utils
+
+#endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS_H_

Powered by Google App Engine
This is Rietveld 408576698