OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "net/http/certificate_report_sender_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/macros.h" | |
10 #include "base/run_loop.h" | |
11 #include "base/thread_task_runner_handle.h" | |
12 #include "net/base/load_flags.h" | |
13 #include "net/base/network_delegate_impl.h" | |
14 #include "net/base/upload_bytes_element_reader.h" | |
15 #include "net/base/upload_data_stream.h" | |
16 #include "net/base/upload_element_reader.h" | |
17 #include "net/test/url_request/url_request_failed_job.h" | |
18 #include "net/test/url_request/url_request_mock_data_job.h" | |
19 #include "net/url_request/url_request_filter.h" | |
20 #include "net/url_request/url_request_test_util.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 | |
davidben
2015/07/23 00:09:42
This is a test in //net, so you can wrap the whole
estark
2015/07/23 02:41:22
Done.
| |
23 namespace { | |
24 | |
25 const char kDummyReport[] = "foo.test"; | |
26 const char kSecondDummyReport[] = "foo2.test"; | |
27 | |
28 void EnableUrlRequestMocks(bool enable) { | |
davidben
2015/07/23 00:09:42
Perhaps SetURLRequestMocksEnabled(bool enable) sin
estark
2015/07/23 02:41:21
Done.
| |
29 net::URLRequestFilter::GetInstance()->ClearHandlers(); | |
30 if (!enable) | |
31 return; | |
32 | |
33 net::URLRequestFailedJob::AddUrlHandler(); | |
34 net::URLRequestMockDataJob::AddUrlHandler(); | |
35 } | |
36 | |
37 // Check that data uploaded in the request matches the test report | |
38 // data. The sent reports will be erased from |expect_reports|. | |
39 void CheckUploadData(net::URLRequest* request, | |
40 std::set<std::string>* expect_reports) { | |
41 const net::UploadDataStream* upload = request->get_upload(); | |
42 ASSERT_TRUE(upload); | |
43 ASSERT_TRUE(upload->GetElementReaders()); | |
44 EXPECT_EQ(1u, upload->GetElementReaders()->size()); | |
davidben
2015/07/23 00:09:42
This probably should be ASSERT_EQ so you don't cra
estark
2015/07/23 02:41:22
Done.
| |
45 | |
46 const net::UploadBytesElementReader* reader = | |
47 (*upload->GetElementReaders())[0]->AsBytesReader(); | |
48 ASSERT_TRUE(reader); | |
49 std::string upload_data(reader->bytes(), reader->length()); | |
50 | |
51 EXPECT_EQ(1u, expect_reports->count(upload_data)); | |
52 expect_reports->erase(upload_data); | |
davidben
2015/07/23 00:09:42
This can be
EXPECT_EQ(1, expect_reports->erase(u
estark
2015/07/23 02:41:21
Done.
| |
53 } | |
54 | |
55 // A network delegate that lets tests check that a certificate report | |
56 // was sent. It counts the number of requests and lets tests register a | |
57 // callback to run when the request is destroyed. It also checks that | |
58 // the uploaded data is as expected. | |
59 class TestCertificateReportSenderNetworkDelegate | |
60 : public net::NetworkDelegateImpl { | |
61 public: | |
62 TestCertificateReportSenderNetworkDelegate() | |
63 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), | |
64 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)), | |
65 num_requests_(0), | |
66 expect_cookies_(false) {} | |
67 | |
68 ~TestCertificateReportSenderNetworkDelegate() override {} | |
69 | |
70 void ExpectReport(const std::string& report) { | |
71 expect_reports_.insert(report); | |
72 } | |
73 | |
74 void set_all_url_requests_destroyed_callback( | |
75 const base::Closure& all_url_requests_destroyed_callback) { | |
76 all_url_requests_destroyed_callback_ = all_url_requests_destroyed_callback; | |
77 } | |
78 | |
79 void set_url_request_destroyed_callback( | |
80 const base::Closure& url_request_destroyed_callback) { | |
81 url_request_destroyed_callback_ = url_request_destroyed_callback; | |
82 } | |
83 | |
84 void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; } | |
85 | |
86 size_t num_requests() const { return num_requests_; } | |
87 | |
88 // Sets whether cookies are expected to be sent on requests. | |
89 void set_expect_cookies(bool expect_cookies) { | |
90 expect_cookies_ = expect_cookies; | |
91 } | |
92 | |
93 // NetworkDelegateImpl implementation | |
94 int OnBeforeURLRequest(net::URLRequest* request, | |
95 const net::CompletionCallback& callback, | |
96 GURL* new_url) override { | |
97 num_requests_++; | |
98 EXPECT_EQ(expect_url_, request->url()); | |
99 EXPECT_STRCASEEQ("POST", request->method().data()); | |
100 | |
101 if (expect_cookies_) { | |
102 EXPECT_FALSE(request->load_flags() & net::LOAD_DO_NOT_SEND_COOKIES); | |
103 EXPECT_FALSE(request->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES); | |
104 } else { | |
105 EXPECT_TRUE(request->load_flags() & net::LOAD_DO_NOT_SEND_COOKIES); | |
106 EXPECT_TRUE(request->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES); | |
107 } | |
108 | |
109 CheckUploadData(request, &expect_reports_); | |
110 return net::OK; | |
111 } | |
112 | |
113 void OnURLRequestDestroyed(net::URLRequest* request) override { | |
114 url_request_destroyed_callback_.Run(); | |
115 if (expect_reports_.empty()) | |
116 all_url_requests_destroyed_callback_.Run(); | |
117 } | |
118 | |
119 private: | |
120 base::Closure url_request_destroyed_callback_; | |
121 base::Closure all_url_requests_destroyed_callback_; | |
122 size_t num_requests_; | |
123 GURL expect_url_; | |
124 std::set<std::string> expect_reports_; | |
125 bool expect_cookies_; | |
126 | |
127 DISALLOW_COPY_AND_ASSIGN(TestCertificateReportSenderNetworkDelegate); | |
128 }; | |
129 | |
130 class CertificateReportSenderImplTest : public ::testing::Test { | |
131 public: | |
132 CertificateReportSenderImplTest() : context_(true) { | |
133 EnableUrlRequestMocks(true); | |
134 context_.set_network_delegate(&network_delegate_); | |
135 context_.Init(); | |
136 } | |
137 | |
138 ~CertificateReportSenderImplTest() override { EnableUrlRequestMocks(false); } | |
139 | |
140 TestCertificateReportSenderNetworkDelegate* network_delegate() { | |
141 return &network_delegate_; | |
142 } | |
143 | |
144 net::TestURLRequestContext* context() { return &context_; } | |
145 | |
146 private: | |
147 TestCertificateReportSenderNetworkDelegate network_delegate_; | |
148 net::TestURLRequestContext context_; | |
149 }; | |
150 | |
151 void SendReport(net::CertificateReportSender* reporter, | |
152 TestCertificateReportSenderNetworkDelegate* network_delegate, | |
153 const std::string& report, | |
154 const GURL& url, | |
155 size_t request_sequence_number) { | |
davidben
2015/07/23 00:09:42
May as well make this a protected method of SendRe
estark
2015/07/23 02:41:21
Done.
| |
156 base::RunLoop run_loop; | |
157 network_delegate->set_url_request_destroyed_callback(run_loop.QuitClosure()); | |
158 | |
159 network_delegate->set_expect_url(url); | |
160 network_delegate->ExpectReport(report); | |
161 | |
162 EXPECT_EQ(request_sequence_number, network_delegate->num_requests()); | |
163 | |
164 reporter->Send(url, report); | |
165 | |
166 // The report is sent asynchronously, so wait for the report's | |
167 // URLRequest to be destroyed before checking that the report was | |
168 // sent. | |
169 run_loop.Run(); | |
170 | |
171 EXPECT_EQ(request_sequence_number + 1, network_delegate->num_requests()); | |
172 } | |
173 | |
174 // Test that CertificateReportSender::Send creates a URLRequest for the | |
175 // endpoint and sends the expected data. | |
176 TEST_F(CertificateReportSenderImplTest, SendsRequest) { | |
177 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
178 net::CertificateReportSenderImpl reporter( | |
179 context(), net::CertificateReportSenderImpl::DO_NOT_SEND_COOKIES); | |
180 SendReport(&reporter, network_delegate(), kDummyReport, url, 0); | |
181 } | |
182 | |
183 TEST_F(CertificateReportSenderImplTest, SendMultipleReportsSequentially) { | |
184 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
185 net::CertificateReportSenderImpl reporter( | |
186 context(), net::CertificateReportSenderImpl::DO_NOT_SEND_COOKIES); | |
187 SendReport(&reporter, network_delegate(), kDummyReport, url, 0); | |
188 SendReport(&reporter, network_delegate(), kDummyReport, url, 1); | |
189 } | |
190 | |
191 TEST_F(CertificateReportSenderImplTest, SendMultipleReportsSimultaneously) { | |
192 base::RunLoop run_loop; | |
193 network_delegate()->set_all_url_requests_destroyed_callback( | |
194 run_loop.QuitClosure()); | |
195 | |
196 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
197 network_delegate()->set_expect_url(url); | |
198 network_delegate()->ExpectReport(kDummyReport); | |
199 network_delegate()->ExpectReport(kSecondDummyReport); | |
200 | |
201 net::CertificateReportSenderImpl reporter( | |
202 context(), net::CertificateReportSenderImpl::DO_NOT_SEND_COOKIES); | |
203 | |
204 EXPECT_EQ(0u, network_delegate()->num_requests()); | |
205 | |
206 reporter.Send(url, kDummyReport); | |
207 reporter.Send(url, kSecondDummyReport); | |
208 | |
209 run_loop.Run(); | |
210 | |
211 EXPECT_EQ(2u, network_delegate()->num_requests()); | |
212 } | |
213 | |
214 // Test that pending URLRequests get cleaned up when the report sender | |
215 // is deleted. | |
216 TEST_F(CertificateReportSenderImplTest, PendingRequestGetsDeleted) { | |
217 base::RunLoop run_loop; | |
218 network_delegate()->set_url_request_destroyed_callback( | |
219 run_loop.QuitClosure()); | |
220 | |
221 GURL url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | |
222 net::URLRequestFailedJob::START, net::ERR_IO_PENDING); | |
223 network_delegate()->set_expect_url(url); | |
224 network_delegate()->ExpectReport(kDummyReport); | |
225 | |
226 EXPECT_EQ(0u, network_delegate()->num_requests()); | |
227 | |
228 scoped_ptr<net::CertificateReportSender> reporter( | |
229 new net::CertificateReportSenderImpl( | |
230 context(), net::CertificateReportSenderImpl::DO_NOT_SEND_COOKIES)); | |
231 reporter->Send(url, kDummyReport); | |
232 reporter.reset(); | |
233 | |
234 run_loop.Run(); | |
davidben
2015/07/23 00:09:42
This happens synchronously, so you could also set
estark
2015/07/23 02:41:21
Done.
| |
235 | |
236 EXPECT_EQ(1u, network_delegate()->num_requests()); | |
237 } | |
238 | |
239 // Test that a request that returns an error gets cleaned up. | |
240 TEST_F(CertificateReportSenderImplTest, ErroredRequestGetsDeleted) { | |
241 GURL url = net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_FAILED); | |
242 net::CertificateReportSenderImpl reporter( | |
243 context(), net::CertificateReportSenderImpl::DO_NOT_SEND_COOKIES); | |
davidben
2015/07/23 00:09:42
Probably want a comment so it's clear:
// SendRe
estark
2015/07/23 02:41:21
Done.
| |
244 SendReport(&reporter, network_delegate(), kDummyReport, url, 0); | |
245 } | |
246 | |
247 // Test that cookies are sent or not sent according to the error | |
248 // reporter's cookies preference. | |
249 | |
250 TEST_F(CertificateReportSenderImplTest, SendCookiesPreference) { | |
251 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
252 net::CertificateReportSenderImpl reporter( | |
253 context(), net::CertificateReportSenderImpl::SEND_COOKIES); | |
254 | |
255 network_delegate()->set_expect_cookies(true); | |
256 SendReport(&reporter, network_delegate(), kDummyReport, url, 0); | |
257 } | |
258 | |
259 TEST_F(CertificateReportSenderImplTest, DoNotSendCookiesPreference) { | |
260 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
261 net::CertificateReportSenderImpl reporter( | |
262 context(), net::CertificateReportSenderImpl::DO_NOT_SEND_COOKIES); | |
263 | |
264 network_delegate()->set_expect_cookies(false); | |
265 SendReport(&reporter, network_delegate(), kDummyReport, url, 0); | |
266 } | |
267 | |
268 } // namespace | |
davidben
2015/07/23 00:09:42
I believe the conclusion the last time this came u
estark
2015/07/23 02:41:21
Done.
| |
OLD | NEW |