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

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

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 #include "chrome/browser/safe_browsing/certificate_reporting_service_test_utils. h"
6
7 #include "base/threading/thread_task_runner_handle.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/test/test_utils.h"
10 #include "net/test/url_request/url_request_failed_job.h"
11 #include "net/test/url_request/url_request_mock_data_job.h"
12 #include "net/url_request/url_request_filter.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16 void SetUpURLHandlersOnIOThread(
17 std::unique_ptr<net::URLRequestInterceptor> url_request_interceptor) {
18 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
19 filter->AddUrlInterceptor(
20 GURL(CertificateReportingService::kExtendedReportingUploadUrlInsecure),
21 std::move(url_request_interceptor));
22 }
23 }
24
25 namespace certificate_reporting_test_utils {
26
27 ReportEventObserver::ReportEventObserver(int num_events_to_wait_for)
28 : num_events_to_wait_for_(num_events_to_wait_for) {}
29
30 ReportEventObserver::~ReportEventObserver() {}
31
32 void ReportEventObserver::OnSendAttempt(bool completed) {
33 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
34 if (completed) {
35 num_completed_attempts_++;
36 } else {
37 num_cancelled_attempts_++;
38 }
39 OnEventReceived(EVENT_SEND_ATTEMPT);
40 }
41
42 void ReportEventObserver::OnSendComplete(int report_id, bool success) {
43 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
44 if (success) {
45 successful_report_ids_.insert(report_id);
46 } else {
47 failed_report_ids_.insert(report_id);
48 }
49 OnEventReceived(EVENT_SEND_COMPLETE);
50 }
51
52 void ReportEventObserver::OnReset() {
53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
54 num_resets_++;
55 OnEventReceived(EVENT_RESET);
56 }
57
58 void ReportEventObserver::WaitForEvents() {
59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
60 if (num_events_received_ < num_events_to_wait_for_) {
61 waiting_ = true;
62 content::RunMessageLoop();
63 EXPECT_FALSE(waiting_);
64 }
65 EXPECT_EQ(num_events_to_wait_for_, num_events_received_);
66 }
67
68 void ReportEventObserver::OnEventReceived(EventType event_type) {
69 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
70 num_events_received_++;
71 EXPECT_LE(num_events_received_, num_events_to_wait_for_)
72 << "An unexpected event arrived of type " << event_type;
73 if (waiting_ && num_events_received_ == num_events_to_wait_for_) {
74 waiting_ = false;
75 base::MessageLoop::current()->QuitWhenIdle();
76 }
77 }
78
79 DelayableCertReportURLRequestJob::DelayableCertReportURLRequestJob(
80 net::URLRequest* request,
81 net::NetworkDelegate* network_delegate)
82 : net::URLRequestJob(request, network_delegate), weak_factory_(this) {}
83
84 DelayableCertReportURLRequestJob::~DelayableCertReportURLRequestJob() {}
85
86 void DelayableCertReportURLRequestJob::Start() {
87 started_ = true;
88 if (delayed_) {
89 // Do nothing until Resume() is called.
90 return;
91 }
92 Resume();
93 }
94
95 int DelayableCertReportURLRequestJob::ReadRawData(net::IOBuffer* buf,
96 int buf_size) {
97 return 0;
98 }
99
100 int DelayableCertReportURLRequestJob::GetResponseCode() const {
101 return 200;
102 }
103
104 void DelayableCertReportURLRequestJob::GetResponseInfo(
105 net::HttpResponseInfo* info) {}
106
107 void DelayableCertReportURLRequestJob::Resume() {
108 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
109 DCHECK(delayed_);
110 if (!started_) {
111 // If Start() hasn't been called yet, then unset |delayed_| so
112 // that when Start() is called, the request will begin
113 // immediately.
114 delayed_ = false;
115 return;
116 }
117 // Start reading asynchronously as would a normal network request.
118 base::ThreadTaskRunnerHandle::Get()->PostTask(
119 FROM_HERE,
120 base::Bind(&DelayableCertReportURLRequestJob::NotifyHeadersComplete,
121 weak_factory_.GetWeakPtr()));
122 }
123
124 CertReportJobInterceptor::CertReportJobInterceptor(
125 ReportSendingResult expected_report_result)
126 : expected_report_result_(expected_report_result), weak_factory_(this) {}
127
128 CertReportJobInterceptor::~CertReportJobInterceptor() {}
129
130 net::URLRequestJob* CertReportJobInterceptor::MaybeInterceptRequest(
131 net::URLRequest* request,
132 net::NetworkDelegate* network_delegate) const {
133 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
134 if (expected_report_result_ == REPORTS_FAIL) {
135 return new net::URLRequestFailedJob(request, network_delegate,
136 net::ERR_SSL_PROTOCOL_ERROR);
137 } else if (expected_report_result_ == REPORTS_DELAY) {
138 DCHECK(!delayed_request_) << "Supports only one delayed request at a time";
139 DelayableCertReportURLRequestJob* job =
140 new DelayableCertReportURLRequestJob(request, network_delegate);
141 delayed_request_ = job->GetWeakPtr();
142 return job;
143 }
144 // Successful url request job.
145 return new net::URLRequestMockDataJob(request, network_delegate, "some data",
146 1, false);
147 }
148
149 void CertReportJobInterceptor::SetFailureMode(
150 ReportSendingResult expected_report_result) {
151 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
152 content::BrowserThread::PostTask(
153 content::BrowserThread::IO, FROM_HERE,
154 base::Bind(&CertReportJobInterceptor::SetFailureModeOnIOThread,
155 weak_factory_.GetWeakPtr(), expected_report_result));
156 }
157
158 void CertReportJobInterceptor::Resume() {
159 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
160 content::BrowserThread::PostTask(
161 content::BrowserThread::IO, FROM_HERE,
162 base::Bind(&CertReportJobInterceptor::ResumeOnIOThread,
163 weak_factory_.GetWeakPtr()));
164 }
165
166 void CertReportJobInterceptor::SetFailureModeOnIOThread(
167 ReportSendingResult expected_report_result) {
168 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
169 expected_report_result_ = expected_report_result;
170 }
171
172 void CertReportJobInterceptor::ResumeOnIOThread() {
173 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
174 EXPECT_EQ(REPORTS_DELAY, expected_report_result_);
175 if (delayed_request_)
176 delayed_request_->Resume();
177 }
178
179 CertificateReportingServiceTestBase::CertificateReportingServiceTestBase() {}
180
181 CertificateReportingServiceTestBase::~CertificateReportingServiceTestBase() {}
182
183 void CertificateReportingServiceTestBase::SetUpInterceptor() {
184 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
185 url_request_interceptor_ =
186 new certificate_reporting_test_utils::CertReportJobInterceptor(
187 certificate_reporting_test_utils::REPORTS_FAIL);
188 content::BrowserThread::PostTask(
189 content::BrowserThread::IO, FROM_HERE,
190 base::Bind(&SetUpURLHandlersOnIOThread,
191 base::Passed(std::unique_ptr<net::URLRequestInterceptor>(
192 url_request_interceptor_))));
193 }
194
195 // Changes the behavior of report uploads to fail or succeed.
196 void CertificateReportingServiceTestBase::SetFailureMode(
197 certificate_reporting_test_utils::ReportSendingResult
198 expected_report_result) {
199 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
200 url_request_interceptor_->SetFailureMode(expected_report_result);
201 }
202
203 void CertificateReportingServiceTestBase::ResumeDelayedRequest() {
204 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
205 url_request_interceptor_->Resume();
206 }
207
208 } // namespace certificate_reporting_test_utils
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698