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

Side by Side 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 unified diff | Download patch
OLDNEW
(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 "chrome/browser/safe_browsing/certificate_reporting_service.h"
12 #include "net/url_request/url_request_interceptor.h"
13 #include "net/url_request/url_request_job.h"
14
15 namespace net {
16 class NetworkDelegate;
17 }
18
19 namespace certificate_reporting_test_utils {
20
21 // This class observes and keeps track of events from
22 // CertificateReportingService. It can also wait for |num_events_to_wait_for|
23 // number of events to arrive.
24 class ReportEventObserver : public CertificateReportingService::EventObserver {
25 public:
26 explicit ReportEventObserver(int num_events_to_wait_for);
27 ~ReportEventObserver() override;
28
29 // CertificateReportingService::EventObserver methods:
30 void OnSendAttempt(bool completed) override;
31 void OnSendComplete(int report_id, bool success) override;
32 void OnReset() override;
33
34 // Waits for |num_events_to_wait_for| number of events to arrive.
35 void WaitForEvents();
36
37 int num_cancelled_attempts() const { return num_cancelled_attempts_; }
38 int num_completed_attempts() const { return num_completed_attempts_; }
39 int num_resets() const { return num_resets_; }
40
41 // Returns the set of report ids that were successfully sent.
42 const std::set<int>& successful_report_ids() const {
43 return successful_report_ids_;
44 }
45 // Returns the set of report ids that were not sent.
46 const std::set<int>& failed_report_ids() const { return failed_report_ids_; }
47
48 private:
49 enum EventType {
50 EVENT_SEND_ATTEMPT = 0,
51 EVENT_SEND_COMPLETE,
52 EVENT_RESET,
53 };
54
55 void OnEventReceived(EventType event_type);
56
57 std::set<int> successful_report_ids_;
58 std::set<int> failed_report_ids_;
59
60 int num_cancelled_attempts_ = 0;
61 int num_completed_attempts_ = 0;
62 int num_resets_ = 0;
63
64 int num_events_received_ = 0;
65 int num_events_to_wait_for_ = 0;
66 bool waiting_ = false;
67
68 DISALLOW_COPY_AND_ASSIGN(ReportEventObserver);
69 };
70
71 // Failure mode of the report sending attempts.
72 enum ReportSendingResult {
73 // Report send attempts should be successful.
74 REPORTS_SUCCESSFUL = 0,
75 // Report send attempts should fail.
76 REPORTS_FAIL = 1,
77 // Report send attempts should hang until explicitly resumed.
78 REPORTS_DELAY = 2,
79 };
80
81 // A URLRequestJob that serves valid time server responses, but delays
82 // them until Resume() is called. If Resume() is called before a request
83 // is made, then the request will not be delayed.
84 class DelayableCertReportURLRequestJob : public net::URLRequestJob {
85 public:
86 DelayableCertReportURLRequestJob(net::URLRequest* request,
87 net::NetworkDelegate* network_delegate);
88 ~DelayableCertReportURLRequestJob() override;
89
90 base::WeakPtr<DelayableCertReportURLRequestJob> GetWeakPtr() {
91 return weak_factory_.GetWeakPtr();
92 }
93
94 // net::URLRequestJob methods:
95 void Start() override;
96 int ReadRawData(net::IOBuffer* buf, int buf_size) override;
97 int GetResponseCode() const override;
98 void GetResponseInfo(net::HttpResponseInfo* info) override;
99
100 // Resumes a previously started request that was delayed. If no
101 // request has been started yet, then when Start() is called it will
102 // not delay.
103 void Resume();
104
105 private:
106 bool delayed_ = true;
107 bool started_ = false;
108 base::WeakPtrFactory<DelayableCertReportURLRequestJob> weak_factory_;
109
110 DISALLOW_COPY_AND_ASSIGN(DelayableCertReportURLRequestJob);
111 };
112
113 // A job interceptor that returns a failed or succesful url request job. Used to
114 // simulate report uploads that fail or succeed. Two different instances of this
115 // class is used by the tests; one for failed reports and one for successful
116 // reports. Can keep track of the report ids it observes.
117 class CertReportJobInterceptor : public net::URLRequestInterceptor {
118 public:
119 explicit CertReportJobInterceptor(ReportSendingResult expected_report_result);
120 ~CertReportJobInterceptor() override;
121
122 // net::URLRequestInterceptor method:
123 net::URLRequestJob* MaybeInterceptRequest(
124 net::URLRequest* request,
125 net::NetworkDelegate* network_delegate) const override;
126
127 // Sets the failure mode for reports.
128 void SetFailureMode(ReportSendingResult expected_report_result);
129 // Resumes any hanging URL request.
130 void Resume();
131
132 private:
133 void SetFailureModeOnIOThread(ReportSendingResult expected_report_result);
134 void ResumeOnIOThread();
135
136 mutable base::WeakPtr<DelayableCertReportURLRequestJob> delayed_request_ =
137 nullptr;
138
139 ReportSendingResult expected_report_result_;
140 mutable base::WeakPtrFactory<CertReportJobInterceptor> weak_factory_;
141
142 DISALLOW_COPY_AND_ASSIGN(CertReportJobInterceptor);
143 };
144
145 class CertificateReportingServiceTestBase {
146 public:
147 CertificateReportingServiceTestBase();
148 virtual ~CertificateReportingServiceTestBase();
149
150 // Changes the behavior of report uploads to fail, succeed or hang.
151 void SetFailureMode(certificate_reporting_test_utils::ReportSendingResult
152 expected_report_result);
153
154 // Resumes delayed report request. Failure mode should be REPORTS_DELAY when
155 // calling this method.
156 void ResumeDelayedRequest();
157
158 protected:
159 void SetUpInterceptor();
160
161 private:
162 certificate_reporting_test_utils::CertReportJobInterceptor*
163 url_request_interceptor_;
164
165 DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestBase);
166 };
167
168 } // namespace certificate_reporting_test_utils
169
170 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS _H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698