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

Side by Side Diff: chrome/browser/safe_browsing/certificate_reporting_service_test_utils.h

Issue 2605403002: Fix flaky CertificateReportingService browser tests. (Closed)
Patch Set: Fix broken tests Created 3 years, 11 months 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
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 <set>
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 // Syntactic sugar for wrapping report expectations in a more readable way. 26 // Syntactic sugar for wrapping report expectations in a more readable way.
27 // Passed to WaitForRequestDeletions() as input.
28 // Example:
29 // The following expects report0 and report1 to be successfully sent and their
30 // URL requests to be deleted:
31 // WaitForRequestDeletions(
estark 2017/01/05 16:42:29 This example still seems useful, except it should
meacer 2017/01/05 20:15:46 Done.
32 // ReportExpectation::Successful("report0, report1"));
33 struct ReportExpectation { 27 struct ReportExpectation {
34 ReportExpectation(); 28 ReportExpectation();
35 ReportExpectation(const ReportExpectation& other); 29 ReportExpectation(const ReportExpectation& other);
36 ~ReportExpectation(); 30 ~ReportExpectation();
37 // Returns an expectation where all reports in |reports| succeed. 31 // Returns an expectation where all reports in |reports| succeed.
38 static ReportExpectation Successful(const std::set<std::string>& reports); 32 static ReportExpectation Successful(const std::set<std::string>& reports);
39 // Returns an expectation where all reports in |reports| fail. 33 // Returns an expectation where all reports in |reports| fail.
40 static ReportExpectation Failed(const std::set<std::string>& reports); 34 static ReportExpectation Failed(const std::set<std::string>& reports);
41 // Returns an expectation where all reports in |reports| are delayed. 35 // Returns an expectation where all reports in |reports| are delayed.
42 static ReportExpectation Delayed(const std::set<std::string>& reports); 36 static ReportExpectation Delayed(const std::set<std::string>& reports);
43 // Total number of reports expected. 37 // Total number of reports expected.
44 int num_reports() const; 38 int num_reports() const;
45 std::set<std::string> successful_reports; 39 std::set<std::string> successful_reports;
46 std::set<std::string> failed_reports; 40 std::set<std::string> failed_reports;
47 std::set<std::string> delayed_reports; 41 std::set<std::string> delayed_reports;
48 }; 42 };
49 43
50 // Helper class to wait for a number of events (e.g. request destroyed, report
51 // observed).
52 class ReportWaitHelper {
53 public:
54 ReportWaitHelper();
55 ~ReportWaitHelper();
56 // Waits for |num_events_to_wait_for|.
57 void Wait(int num_events_to_wait_for);
58 // Must be called when an event is observed.
59 void OnEvent();
60
61 private:
62 int num_events_to_wait_for_;
63 int num_received_events_;
64 std::unique_ptr<base::RunLoop> run_loop_;
65 };
66
67 // Failure mode of the report sending attempts. 44 // Failure mode of the report sending attempts.
68 enum ReportSendingResult { 45 enum ReportSendingResult {
69 // Report send attempts should be successful. 46 // Report send attempts should be successful.
70 REPORTS_SUCCESSFUL, 47 REPORTS_SUCCESSFUL,
71 // Report send attempts should fail. 48 // Report send attempts should fail.
72 REPORTS_FAIL, 49 REPORTS_FAIL,
73 // Report send attempts should hang until explicitly resumed. 50 // Report send attempts should hang until explicitly resumed.
74 REPORTS_DELAY, 51 REPORTS_DELAY,
75 }; 52 };
76 53
54 // Helper class to wait for a number of events (e.g. request destroyed, report
55 // observed).
56 class RequestObserver {
57 public:
58 RequestObserver();
59 ~RequestObserver();
60
61 // Waits for |num_request| requests to be created or destroyed, depending on
62 // whichever one this class observes.
63 void Wait(int num_events_to_wait_for);
estark 2017/01/05 16:42:29 nit: unsigned?
meacer 2017/01/05 20:15:46 Done.
64
65 // Called when a request created or destroyed, depending on whichever one this
66 // class observes.
67 void OnRequest(const std::string& serialized_report,
68 ReportSendingResult report_type);
69
70 // These must be called on the UI thread.
71 const std::set<std::string>& successful_reports() const;
72 const std::set<std::string>& failed_reports() const;
73 const std::set<std::string>& delayed_reports() const;
74 void ClearObservedReports();
75
76 private:
77 int num_events_to_wait_for_;
estark 2017/01/05 16:42:29 nit: unsigned and on line 78 too
meacer 2017/01/05 20:15:46 Done.
78 int num_received_events_;
79 std::unique_ptr<base::RunLoop> run_loop_;
80
81 std::set<std::string> successful_reports_;
82 std::set<std::string> failed_reports_;
83 std::set<std::string> delayed_reports_;
84 };
85
77 // A URLRequestJob that can be delayed until Resume() is called. Returns an 86 // A URLRequestJob that can be delayed until Resume() is called. Returns an
78 // empty response. If Resume() is called before a request is made, then the 87 // empty response. If Resume() is called before a request is made, then the
79 // request will not be delayed. 88 // request will not be delayed. If not delayed, it can return a failed or a
89 // successful URL request job.
80 class DelayableCertReportURLRequestJob : public net::URLRequestJob, 90 class DelayableCertReportURLRequestJob : public net::URLRequestJob,
81 public base::NonThreadSafe { 91 public base::NonThreadSafe {
82 public: 92 public:
83 DelayableCertReportURLRequestJob(net::URLRequest* request, 93 DelayableCertReportURLRequestJob(
84 net::NetworkDelegate* network_delegate); 94 bool delayed,
95 bool should_fail,
96 net::URLRequest* request,
97 net::NetworkDelegate* network_delegate,
98 const base::Callback<void()>& destruction_callback);
85 ~DelayableCertReportURLRequestJob() override; 99 ~DelayableCertReportURLRequestJob() override;
86 100
87 base::WeakPtr<DelayableCertReportURLRequestJob> GetWeakPtr(); 101 base::WeakPtr<DelayableCertReportURLRequestJob> GetWeakPtr();
88 102
89 // net::URLRequestJob methods: 103 // net::URLRequestJob methods:
90 void Start() override; 104 void Start() override;
91 int ReadRawData(net::IOBuffer* buf, int buf_size) override; 105 int ReadRawData(net::IOBuffer* buf, int buf_size) override;
92 int GetResponseCode() const override; 106 int GetResponseCode() const override;
93 void GetResponseInfo(net::HttpResponseInfo* info) override; 107 void GetResponseInfo(net::HttpResponseInfo* info) override;
94 108
95 // Resumes a previously started request that was delayed. If no 109 // Resumes a previously started request that was delayed. If no
96 // request has been started yet, then when Start() is called it will 110 // request has been started yet, then when Start() is called it will
97 // not delay. 111 // not delay.
98 void Resume(); 112 void Resume();
99 113
100 private: 114 private:
101 bool delayed_ = true; 115 bool delayed_;
102 bool started_ = false; 116 bool should_fail_;
117 bool started_;
118 base::Callback<void()> destruction_callback_;
103 base::WeakPtrFactory<DelayableCertReportURLRequestJob> weak_factory_; 119 base::WeakPtrFactory<DelayableCertReportURLRequestJob> weak_factory_;
104 120
105 DISALLOW_COPY_AND_ASSIGN(DelayableCertReportURLRequestJob); 121 DISALLOW_COPY_AND_ASSIGN(DelayableCertReportURLRequestJob);
106 }; 122 };
107 123
108 // A job interceptor that returns a failed, succesful or delayed request job. 124 // A job interceptor that returns a failed, succesful or delayed request job.
109 // Used to simulate report uploads that fail, succeed or hang. 125 // Used to simulate report uploads that fail, succeed or hang.
110 class CertReportJobInterceptor : public net::URLRequestInterceptor { 126 class CertReportJobInterceptor : public net::URLRequestInterceptor {
111 public: 127 public:
112 CertReportJobInterceptor(ReportSendingResult expected_report_result, 128 CertReportJobInterceptor(ReportSendingResult expected_report_result,
113 const uint8_t* server_private_key); 129 const uint8_t* server_private_key);
114 ~CertReportJobInterceptor() override; 130 ~CertReportJobInterceptor() override;
115 131
116 // net::URLRequestInterceptor method: 132 // net::URLRequestInterceptor method:
117 net::URLRequestJob* MaybeInterceptRequest( 133 net::URLRequestJob* MaybeInterceptRequest(
118 net::URLRequest* request, 134 net::URLRequest* request,
119 net::NetworkDelegate* network_delegate) const override; 135 net::NetworkDelegate* network_delegate) const override;
120 136
121 // Sets the failure mode for reports. Must be called on the UI thread. 137 // Sets the failure mode for reports. Must be called on the UI thread.
122 void SetFailureMode(ReportSendingResult expected_report_result); 138 void SetFailureMode(ReportSendingResult expected_report_result);
123 // Resumes any hanging URL request and runs callback when the request 139 // Resumes any hanging URL request and runs callback when the request
124 // is resumed (i.e. response starts). Must be called on the UI thread. 140 // is resumed (i.e. response starts). Must be called on the UI thread.
125 void Resume(const base::Closure& callback); 141 void Resume();
126 142
127 // These must be called on the UI thread. 143 RequestObserver* request_created_observer() const;
128 const std::set<std::string>& successful_reports() const; 144 RequestObserver* request_destroyed_observer() const;
129 const std::set<std::string>& failed_reports() const;
130 const std::set<std::string>& delayed_reports() const;
131 void ClearObservedReports();
132
133 // Waits for requests for |num_reports| reports to be created. Only used in
134 // browser tests. Unit tests wait for requests to be destroyed instead.
135 // Must be called on the UI thread.
136 void WaitForReports(int num_reports);
137 145
138 private: 146 private:
139 void SetFailureModeOnIOThread(ReportSendingResult expected_report_result); 147 void SetFailureModeOnIOThread(ReportSendingResult expected_report_result);
140 void ResumeOnIOThread(); 148 void ResumeOnIOThread();
141 void RequestCreated(const std::string& uploaded_report, 149 void RequestCreated(const std::string& uploaded_report,
142 ReportSendingResult expected_report_result); 150 ReportSendingResult expected_report_result) const;
151 void RequestDestructed(const std::string& uploaded_report,
152 ReportSendingResult expected_report_result) const;
143 153
144 std::set<std::string> successful_reports_; 154 mutable std::set<std::string> successful_reports_;
145 std::set<std::string> failed_reports_; 155 mutable std::set<std::string> failed_reports_;
146 std::set<std::string> delayed_reports_; 156 mutable std::set<std::string> delayed_reports_;
147 157
148 ReportSendingResult expected_report_result_; 158 ReportSendingResult expected_report_result_;
149 159
150 // Private key to decrypt certificate reports. 160 // Private key to decrypt certificate reports.
151 const uint8_t* server_private_key_; 161 const uint8_t* server_private_key_;
152 162
153 ReportWaitHelper wait_helper_; 163 mutable RequestObserver request_created_observer_;
164 mutable RequestObserver request_destroyed_observer_;
154 165
155 mutable base::WeakPtr<DelayableCertReportURLRequestJob> delayed_request_ = 166 mutable base::WeakPtr<DelayableCertReportURLRequestJob> delayed_request_ =
156 nullptr; 167 nullptr;
157 mutable base::WeakPtrFactory<CertReportJobInterceptor> weak_factory_; 168 mutable base::WeakPtrFactory<CertReportJobInterceptor> weak_factory_;
158 169
159 DISALLOW_COPY_AND_ASSIGN(CertReportJobInterceptor); 170 DISALLOW_COPY_AND_ASSIGN(CertReportJobInterceptor);
160 }; 171 };
161 172
162 // A network delegate used to observe URL request destructions. The tests check 173 // Class to wait for the CertificateReportingService to reset.
163 // that no outstanding URL request is present during tear down. 174 class CertificateReportingServiceObserver {
164 class CertificateReportingServiceTestNetworkDelegate
165 : public net::NetworkDelegateImpl {
166 public: 175 public:
167 CertificateReportingServiceTestNetworkDelegate( 176 CertificateReportingServiceObserver();
168 const base::Callback<void()>& url_request_destroyed_callback); 177 ~CertificateReportingServiceObserver();
169 ~CertificateReportingServiceTestNetworkDelegate() override;
170 178
171 // net::NetworkDelegate method: 179 // Clears the state of the observer. Must be called before waiting each time.
172 void OnURLRequestDestroyed(net::URLRequest* request) override; 180 void Clear();
181
182 // Waits for the service to reset.
183 void WaitForReset();
184
185 // Must be called when the service is reset.
186 void OnServiceReset();
173 187
174 private: 188 private:
175 base::Callback<void()> url_request_destroyed_callback_; 189 bool did_reset_ = false;
190 std::unique_ptr<base::RunLoop> run_loop_;
176 }; 191 };
177 192
178 // Base class for CertificateReportingService tests. Sets up an interceptor to 193 // Base class for CertificateReportingService tests. Sets up an interceptor to
179 // keep track of reports that are being sent. 194 // keep track of reports that are being sent.
180 class CertificateReportingServiceTestHelper { 195 class CertificateReportingServiceTestHelper {
181 public: 196 public:
182 CertificateReportingServiceTestHelper(); 197 CertificateReportingServiceTestHelper();
183 ~CertificateReportingServiceTestHelper(); 198 ~CertificateReportingServiceTestHelper();
184 199
185 void SetUpInterceptor(); 200 void SetUpInterceptor();
186 201
187 // Changes the behavior of report uploads to fail, succeed or hang. 202 // Changes the behavior of report uploads to fail, succeed or hang.
188 void SetFailureMode(ReportSendingResult expected_report_result); 203 void SetFailureMode(ReportSendingResult expected_report_result);
189 204
190 // Resumes delayed report request. Failure mode should be REPORTS_DELAY when 205 // Resumes delayed report request. Failure mode should be REPORTS_DELAY when
191 // calling this method. 206 // calling this method.
192 void ResumeDelayedRequest(const base::Callback<void()>& callback); 207 void ResumeDelayedRequest();
208
209 void WaitForRequestsCreated(const ReportExpectation& expectation);
210 void WaitForRequestsDestroyed(const ReportExpectation& expectation);
211
212 // Checks that all requests are destroyed and that there are no in-flight
213 // reports in |service|.
214 void ExpectNoRequests(CertificateReportingService* service);
193 215
194 uint8_t* server_public_key(); 216 uint8_t* server_public_key();
195 uint32_t server_public_key_version() const; 217 uint32_t server_public_key_version() const;
196 218
219 private:
197 CertReportJobInterceptor* interceptor() { return url_request_interceptor_; } 220 CertReportJobInterceptor* interceptor() { return url_request_interceptor_; }
198
199 private:
200 void SetUpInterceptorOnIOThread(); 221 void SetUpInterceptorOnIOThread();
201 222
202 CertReportJobInterceptor* url_request_interceptor_; 223 CertReportJobInterceptor* url_request_interceptor_;
203 224
204 uint8_t server_public_key_[32]; 225 uint8_t server_public_key_[32];
205 uint8_t server_private_key_[32]; 226 uint8_t server_private_key_[32];
206 227
207 DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestHelper); 228 DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestHelper);
208 }; 229 };
209 230
210 } // namespace certificate_reporting_test_utils 231 } // namespace certificate_reporting_test_utils
211 232
212 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS _H_ 233 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS _H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698