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

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: Cleanup 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 "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 = 0,
estark 2016/12/07 00:42:55 nit: explicit values aren't necessary (unless you'
meacer 2016/12/07 21:37:33 Done.
29 // Report send attempts should fail.
30 REPORTS_FAIL = 1,
31 // Report send attempts should hang until explicitly resumed.
32 REPORTS_DELAY = 2,
33 };
34
35 // 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
36 // them until Resume() is called. If Resume() is called before a request
37 // is made, then the 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() {
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.
45 return weak_factory_.GetWeakPtr();
46 }
47
48 // net::URLRequestJob methods:
49 void Start() override;
50 int ReadRawData(net::IOBuffer* buf, int buf_size) override;
51 int GetResponseCode() const override;
52 void GetResponseInfo(net::HttpResponseInfo* info) override;
53
54 // Resumes a previously started request that was delayed. If no
55 // request has been started yet, then when Start() is called it will
56 // not delay.
57 void Resume();
58
59 private:
60 bool delayed_ = true;
61 bool started_ = false;
62 base::WeakPtrFactory<DelayableCertReportURLRequestJob> weak_factory_;
63
64 DISALLOW_COPY_AND_ASSIGN(DelayableCertReportURLRequestJob);
65 };
66
67 // A job interceptor that returns a failed or succesful url request job. Used to
68 // simulate report uploads that fail or succeed. Two different instances of this
69 // 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
70 // reports. Can keep track of the report ids it observes.
71 class CertReportJobInterceptor : public net::URLRequestInterceptor {
72 public:
73 CertReportJobInterceptor(ReportSendingResult expected_report_result,
74 const uint8_t* server_private_key);
75 ~CertReportJobInterceptor() override;
76
77 // net::URLRequestInterceptor method:
78 net::URLRequestJob* MaybeInterceptRequest(
79 net::URLRequest* request,
80 net::NetworkDelegate* network_delegate) const override;
81
82 // Sets the failure mode for reports.
83 void SetFailureMode(ReportSendingResult expected_report_result);
84 // Resumes any hanging URL request.
85 void Resume();
86
87 void WaitForRequests(int requests_to_wait_for);
88
89 const std::set<std::string>& successful_reports() const;
90 const std::set<std::string>& failed_reports() const;
91 const std::set<std::string>& delayed_reports() const;
92
93 void Clear();
94
95 private:
96 void SetFailureModeOnIOThread(ReportSendingResult expected_report_result);
97 void ResumeOnIOThread();
98 void RequestCreated(const std::string& uploaded_report,
99 ReportSendingResult expected_report_result);
100
101 std::set<std::string> successful_reports_;
102 std::set<std::string> failed_reports_;
103 std::set<std::string> delayed_reports_;
104
105 ReportSendingResult expected_report_result_;
106
107 // Private key to decrypt certificate reports.
108 const uint8_t* server_private_key_;
109
110 mutable base::WeakPtr<DelayableCertReportURLRequestJob> delayed_request_ =
111 nullptr;
112 mutable base::WeakPtrFactory<CertReportJobInterceptor> weak_factory_;
113
114 DISALLOW_COPY_AND_ASSIGN(CertReportJobInterceptor);
115 };
116
117 class CertificateReportingServiceTestNetworkDelegate
estark 2016/12/07 00:42:55 document
meacer 2016/12/07 21:37:33 Done.
118 : public net::NetworkDelegateImpl {
119 public:
120 CertificateReportingServiceTestNetworkDelegate(
121 const base::Callback<void(net::URLRequest* request)>&
122 url_request_destroyed_callback);
123 ~CertificateReportingServiceTestNetworkDelegate() override;
124
125 void OnURLRequestDestroyed(net::URLRequest* request) override;
126
127 private:
128 base::Callback<void(net::URLRequest* request)>
129 url_request_destroyed_callback_;
130 };
131
132 class CertificateReportingServiceTestBase {
estark 2016/12/07 00:42:55 document
meacer 2016/12/07 21:37:33 Done.
133 public:
134 CertificateReportingServiceTestBase();
135 virtual ~CertificateReportingServiceTestBase();
136
137 protected:
138 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.
139 ReportExpectation();
140 ~ReportExpectation();
141 ReportExpectation& Successful(const std::set<std::string>& reports);
142 ReportExpectation& Failed(const std::set<std::string>& reports);
143 ReportExpectation& Delayed(const std::set<std::string>& reports);
144 std::set<std::string> successful_reports;
145 std::set<std::string> failed_reports;
146 std::set<std::string> delayed_reports;
147 };
148
149 void SetUpInterceptor();
150
151 // Changes the behavior of report uploads to fail, succeed or hang.
152 void SetFailureMode(ReportSendingResult 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 uint8_t* server_public_key() { return server_public_key_; }
159 uint32_t server_public_key_version() const;
160
161 net::NetworkDelegate* network_delegate() { return &network_delegate_; }
162
163 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.
164
165 CertReportJobInterceptor* interceptor() { return url_request_interceptor_; }
166
167 private:
168 void WaitForReports_(const std::set<std::string>& reports);
169
170 void OnRequestDeleted(net::URLRequest* request);
171 void ClearObservedReports();
172
173 CertReportJobInterceptor* url_request_interceptor_;
174
175 uint8_t server_public_key_[32];
176 uint8_t server_private_key_[32];
177
178 CertificateReportingServiceTestNetworkDelegate network_delegate_;
179
180 int num_request_deletions_to_wait_for_;
181 int num_deleted_requests_;
182 std::unique_ptr<base::RunLoop> run_loop_;
183 DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceTestBase);
184 };
185
186 } // namespace certificate_reporting_test_utils
187
188 #endif // CHROME_BROWSER_SAFE_BROWSING_CERTIFICATE_REPORTING_SERVICE_TEST_UTILS _H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698