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

Side by Side Diff: net/url_request/certificate_report_sender_unittest.cc

Issue 1212973002: Add net::CertificateReportSender for handling cert report sending (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more unit test fixes Created 5 years, 4 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
« no previous file with comments | « net/url_request/certificate_report_sender.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/url_request/certificate_report_sender.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/25 01:32:57 Oh, I actually meant this. You can nest anonymous
estark 2015/07/25 06:28:27 Ah, right, thanks. Done.
23 namespace {
24
25 const char kDummyReport[] = "foo.test";
26 const char kSecondDummyReport[] = "foo2.test";
27
28 void MarkURLRequestDestroyed(bool* url_request_destroyed) {
29 *url_request_destroyed = true;
30 }
31
32 void SetUrlRequestMocksEnabled(bool enable) {
33 net::URLRequestFilter::GetInstance()->ClearHandlers();
34 if (!enable)
35 return;
36
37 net::URLRequestFailedJob::AddUrlHandler();
38 net::URLRequestMockDataJob::AddUrlHandler();
39 }
40
41 // Check that data uploaded in the request matches the test report
42 // data. The sent reports will be erased from |expect_reports|.
43 void CheckUploadData(net::URLRequest* request,
44 std::set<std::string>* expect_reports) {
45 const net::UploadDataStream* upload = request->get_upload();
46 ASSERT_TRUE(upload);
47 ASSERT_TRUE(upload->GetElementReaders());
48 ASSERT_EQ(1u, upload->GetElementReaders()->size());
49
50 const net::UploadBytesElementReader* reader =
51 (*upload->GetElementReaders())[0]->AsBytesReader();
52 ASSERT_TRUE(reader);
53 std::string upload_data(reader->bytes(), reader->length());
54
55 EXPECT_EQ(1u, expect_reports->erase(upload_data));
56 }
57
58 // A network delegate that lets tests check that a certificate report
59 // was sent. It counts the number of requests and lets tests register a
60 // callback to run when the request is destroyed. It also checks that
61 // the uploaded data is as expected.
62 class TestCertificateReportSenderNetworkDelegate
63 : public net::NetworkDelegateImpl {
64 public:
65 TestCertificateReportSenderNetworkDelegate()
66 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)),
67 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)),
68 num_requests_(0),
69 expect_cookies_(false) {}
70
71 ~TestCertificateReportSenderNetworkDelegate() override {}
72
73 void ExpectReport(const std::string& report) {
74 expect_reports_.insert(report);
75 }
76
77 void set_all_url_requests_destroyed_callback(
78 const base::Closure& all_url_requests_destroyed_callback) {
79 all_url_requests_destroyed_callback_ = all_url_requests_destroyed_callback;
80 }
81
82 void set_url_request_destroyed_callback(
83 const base::Closure& url_request_destroyed_callback) {
84 url_request_destroyed_callback_ = url_request_destroyed_callback;
85 }
86
87 void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; }
88
89 size_t num_requests() const { return num_requests_; }
90
91 // Sets whether cookies are expected to be sent on requests.
92 void set_expect_cookies(bool expect_cookies) {
93 expect_cookies_ = expect_cookies;
94 }
95
96 // NetworkDelegateImpl implementation
97 int OnBeforeURLRequest(net::URLRequest* request,
98 const net::CompletionCallback& callback,
99 GURL* new_url) override {
100 num_requests_++;
101 EXPECT_EQ(expect_url_, request->url());
102 EXPECT_STRCASEEQ("POST", request->method().data());
103
104 if (expect_cookies_) {
105 EXPECT_FALSE(request->load_flags() & net::LOAD_DO_NOT_SEND_COOKIES);
106 EXPECT_FALSE(request->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES);
107 } else {
108 EXPECT_TRUE(request->load_flags() & net::LOAD_DO_NOT_SEND_COOKIES);
109 EXPECT_TRUE(request->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES);
110 }
111
112 CheckUploadData(request, &expect_reports_);
113 return net::OK;
114 }
115
116 void OnURLRequestDestroyed(net::URLRequest* request) override {
117 url_request_destroyed_callback_.Run();
118 if (expect_reports_.empty())
119 all_url_requests_destroyed_callback_.Run();
120 }
121
122 private:
123 base::Closure url_request_destroyed_callback_;
124 base::Closure all_url_requests_destroyed_callback_;
125 size_t num_requests_;
126 GURL expect_url_;
127 std::set<std::string> expect_reports_;
128 bool expect_cookies_;
129
130 DISALLOW_COPY_AND_ASSIGN(TestCertificateReportSenderNetworkDelegate);
131 };
132
133 class CertificateReportSenderTest : public ::testing::Test {
134 public:
135 CertificateReportSenderTest() : context_(true) {
136 SetUrlRequestMocksEnabled(true);
137 context_.set_network_delegate(&network_delegate_);
138 context_.Init();
139 }
140
141 ~CertificateReportSenderTest() override { SetUrlRequestMocksEnabled(false); }
142
143 TestCertificateReportSenderNetworkDelegate* network_delegate() {
144 return &network_delegate_;
145 }
146
147 net::TestURLRequestContext* context() { return &context_; }
148
149 protected:
150 void SendReport(net::CertificateReportSender* reporter,
151 const std::string& report,
152 const GURL& url,
153 size_t request_sequence_number) {
154 base::RunLoop run_loop;
155 network_delegate_.set_url_request_destroyed_callback(
156 run_loop.QuitClosure());
157
158 network_delegate_.set_expect_url(url);
159 network_delegate_.ExpectReport(report);
160
161 EXPECT_EQ(request_sequence_number, network_delegate_.num_requests());
162
163 reporter->Send(url, report);
164
165 // The report is sent asynchronously, so wait for the report's
166 // URLRequest to be destroyed before checking that the report was
167 // sent.
168 run_loop.Run();
169
170 EXPECT_EQ(request_sequence_number + 1, network_delegate_.num_requests());
171 }
172
173 private:
174 TestCertificateReportSenderNetworkDelegate network_delegate_;
175 net::TestURLRequestContext context_;
176 };
177
178 // Test that CertificateReportSender::Send creates a URLRequest for the
179 // endpoint and sends the expected data.
180 TEST_F(CertificateReportSenderTest, SendsRequest) {
181 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
182 net::CertificateReportSender reporter(
183 context(), net::CertificateReportSender::DO_NOT_SEND_COOKIES);
184 SendReport(&reporter, kDummyReport, url, 0);
185 }
186
187 TEST_F(CertificateReportSenderTest, SendMultipleReportsSequentially) {
188 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
189 net::CertificateReportSender reporter(
190 context(), net::CertificateReportSender::DO_NOT_SEND_COOKIES);
191 SendReport(&reporter, kDummyReport, url, 0);
192 SendReport(&reporter, kDummyReport, url, 1);
193 }
194
195 TEST_F(CertificateReportSenderTest, SendMultipleReportsSimultaneously) {
196 base::RunLoop run_loop;
197 network_delegate()->set_all_url_requests_destroyed_callback(
198 run_loop.QuitClosure());
199
200 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
201 network_delegate()->set_expect_url(url);
202 network_delegate()->ExpectReport(kDummyReport);
203 network_delegate()->ExpectReport(kSecondDummyReport);
204
205 net::CertificateReportSender reporter(
206 context(), net::CertificateReportSender::DO_NOT_SEND_COOKIES);
207
208 EXPECT_EQ(0u, network_delegate()->num_requests());
209
210 reporter.Send(url, kDummyReport);
211 reporter.Send(url, kSecondDummyReport);
212
213 run_loop.Run();
214
215 EXPECT_EQ(2u, network_delegate()->num_requests());
216 }
217
218 // Test that pending URLRequests get cleaned up when the report sender
219 // is deleted.
220 TEST_F(CertificateReportSenderTest, PendingRequestGetsDeleted) {
221 bool url_request_destroyed = false;
222 network_delegate()->set_url_request_destroyed_callback(base::Bind(
223 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed)));
224
225 GURL url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
226 net::URLRequestFailedJob::START, net::ERR_IO_PENDING);
227 network_delegate()->set_expect_url(url);
228 network_delegate()->ExpectReport(kDummyReport);
229
230 EXPECT_EQ(0u, network_delegate()->num_requests());
231
232 scoped_ptr<net::CertificateReportSender> reporter(
233 new net::CertificateReportSender(
234 context(), net::CertificateReportSender::DO_NOT_SEND_COOKIES));
235 reporter->Send(url, kDummyReport);
236 reporter.reset();
237
238 EXPECT_EQ(1u, network_delegate()->num_requests());
239 EXPECT_TRUE(url_request_destroyed);
240 }
241
242 // Test that a request that returns an error gets cleaned up.
243 TEST_F(CertificateReportSenderTest, ErroredRequestGetsDeleted) {
244 GURL url = net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_FAILED);
245 net::CertificateReportSender reporter(
246 context(), net::CertificateReportSender::DO_NOT_SEND_COOKIES);
247 // SendReport will block until the URLRequest is destroyed.
248 SendReport(&reporter, kDummyReport, url, 0);
249 }
250
251 // Test that cookies are sent or not sent according to the error
252 // reporter's cookies preference.
253
254 TEST_F(CertificateReportSenderTest, SendCookiesPreference) {
255 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
256 net::CertificateReportSender reporter(
257 context(), net::CertificateReportSender::SEND_COOKIES);
258
259 network_delegate()->set_expect_cookies(true);
260 SendReport(&reporter, kDummyReport, url, 0);
261 }
262
263 TEST_F(CertificateReportSenderTest, DoNotSendCookiesPreference) {
264 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
265 net::CertificateReportSender reporter(
266 context(), net::CertificateReportSender::DO_NOT_SEND_COOKIES);
267
268 network_delegate()->set_expect_cookies(false);
269 SendReport(&reporter, kDummyReport, url, 0);
270 }
271
272 } // namespace
OLDNEW
« no previous file with comments | « net/url_request/certificate_report_sender.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698