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

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: pare down reporter interface to just Send() Created 5 years, 5 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
(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
23 namespace net {
24
davidben 2015/07/24 18:54:12 namespace {
estark 2015/07/24 22:56:04 Done (made the whole thing anonymous).
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 URLRequestFilter::GetInstance()->ClearHandlers();
34 if (!enable)
35 return;
36
37 URLRequestFailedJob::AddUrlHandler();
38 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(URLRequest* request,
44 std::set<std::string>* expect_reports) {
45 const UploadDataStream* upload = request->get_upload();
46 ASSERT_TRUE(upload);
47 ASSERT_TRUE(upload->GetElementReaders());
48 ASSERT_EQ(1u, upload->GetElementReaders()->size());
49
50 const 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 : public NetworkDelegateImpl {
63 public:
64 TestCertificateReportSenderNetworkDelegate()
65 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)),
66 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)),
67 num_requests_(0),
68 expect_cookies_(false) {}
69
70 ~TestCertificateReportSenderNetworkDelegate() override {}
71
72 void ExpectReport(const std::string& report) {
73 expect_reports_.insert(report);
74 }
75
76 void set_all_url_requests_destroyed_callback(
77 const base::Closure& all_url_requests_destroyed_callback) {
78 all_url_requests_destroyed_callback_ = all_url_requests_destroyed_callback;
79 }
80
81 void set_url_request_destroyed_callback(
82 const base::Closure& url_request_destroyed_callback) {
83 url_request_destroyed_callback_ = url_request_destroyed_callback;
84 }
85
86 void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; }
87
88 size_t num_requests() const { return num_requests_; }
89
90 // Sets whether cookies are expected to be sent on requests.
91 void set_expect_cookies(bool expect_cookies) {
92 expect_cookies_ = expect_cookies;
93 }
94
95 // NetworkDelegateImpl implementation
96 int OnBeforeURLRequest(URLRequest* request,
97 const CompletionCallback& callback,
98 GURL* new_url) override {
99 num_requests_++;
100 EXPECT_EQ(expect_url_, request->url());
101 EXPECT_STRCASEEQ("POST", request->method().data());
102
103 if (expect_cookies_) {
104 EXPECT_FALSE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES);
105 EXPECT_FALSE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES);
106 } else {
107 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES);
108 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES);
109 }
110
111 CheckUploadData(request, &expect_reports_);
112 return OK;
113 }
114
115 void OnURLRequestDestroyed(URLRequest* request) override {
116 url_request_destroyed_callback_.Run();
117 if (expect_reports_.empty())
118 all_url_requests_destroyed_callback_.Run();
119 }
120
121 private:
122 base::Closure url_request_destroyed_callback_;
123 base::Closure all_url_requests_destroyed_callback_;
124 size_t num_requests_;
125 GURL expect_url_;
126 std::set<std::string> expect_reports_;
127 bool expect_cookies_;
128
129 DISALLOW_COPY_AND_ASSIGN(TestCertificateReportSenderNetworkDelegate);
130 };
davidben 2015/07/24 18:54:12 } // namespace
davidben 2015/07/24 19:09:08 Fascinating. Okay, apparently we're way less consi
estark 2015/07/24 22:56:03 Done.
131
132 class CertificateReportSenderTest : public ::testing::Test {
133 public:
134 CertificateReportSenderTest() : context_(true) {
135 SetUrlRequestMocksEnabled(true);
136 context_.set_network_delegate(&network_delegate_);
137 context_.Init();
138 }
139
140 ~CertificateReportSenderTest() override { SetUrlRequestMocksEnabled(false); }
141
142 TestCertificateReportSenderNetworkDelegate* network_delegate() {
143 return &network_delegate_;
144 }
145
146 TestURLRequestContext* context() { return &context_; }
147
148 protected:
149 void SendReport(CertificateReportSender* reporter,
150 const std::string& report,
151 const GURL& url,
152 size_t request_sequence_number) {
153 base::RunLoop run_loop;
154 network_delegate_.set_url_request_destroyed_callback(
155 run_loop.QuitClosure());
156
157 network_delegate_.set_expect_url(url);
158 network_delegate_.ExpectReport(report);
159
160 EXPECT_EQ(request_sequence_number, network_delegate_.num_requests());
161
162 reporter->Send(url, report);
163
164 // The report is sent asynchronously, so wait for the report's
165 // URLRequest to be destroyed before checking that the report was
166 // sent.
167 run_loop.Run();
168
169 EXPECT_EQ(request_sequence_number + 1, network_delegate_.num_requests());
170 }
171
172 private:
173 TestCertificateReportSenderNetworkDelegate network_delegate_;
174 TestURLRequestContext context_;
175 };
176
177 // Test that CertificateReportSender::Send creates a URLRequest for the
178 // endpoint and sends the expected data.
179 TEST_F(CertificateReportSenderTest, SendsRequest) {
180 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
181 CertificateReportSender reporter(
182 context(), CertificateReportSender::DO_NOT_SEND_COOKIES);
183 SendReport(&reporter, kDummyReport, url, 0);
184 }
185
186 TEST_F(CertificateReportSenderTest, SendMultipleReportsSequentially) {
187 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
188 CertificateReportSender reporter(
189 context(), CertificateReportSender::DO_NOT_SEND_COOKIES);
190 SendReport(&reporter, kDummyReport, url, 0);
191 SendReport(&reporter, kDummyReport, url, 1);
192 }
193
194 TEST_F(CertificateReportSenderTest, SendMultipleReportsSimultaneously) {
195 base::RunLoop run_loop;
196 network_delegate()->set_all_url_requests_destroyed_callback(
197 run_loop.QuitClosure());
198
199 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
200 network_delegate()->set_expect_url(url);
201 network_delegate()->ExpectReport(kDummyReport);
202 network_delegate()->ExpectReport(kSecondDummyReport);
203
204 CertificateReportSender reporter(
205 context(), CertificateReportSender::DO_NOT_SEND_COOKIES);
206
207 EXPECT_EQ(0u, network_delegate()->num_requests());
208
209 reporter.Send(url, kDummyReport);
210 reporter.Send(url, kSecondDummyReport);
211
212 run_loop.Run();
213
214 EXPECT_EQ(2u, network_delegate()->num_requests());
215 }
216
217 // Test that pending URLRequests get cleaned up when the report sender
218 // is deleted.
219 TEST_F(CertificateReportSenderTest, PendingRequestGetsDeleted) {
220 bool url_request_destroyed = false;
221 network_delegate()->set_url_request_destroyed_callback(base::Bind(
222 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed)));
223
224 GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
225 URLRequestFailedJob::START, ERR_IO_PENDING);
226 network_delegate()->set_expect_url(url);
227 network_delegate()->ExpectReport(kDummyReport);
228
229 EXPECT_EQ(0u, network_delegate()->num_requests());
230
231 scoped_ptr<CertificateReportSender> reporter(new CertificateReportSender(
232 context(), CertificateReportSender::DO_NOT_SEND_COOKIES));
233 reporter->Send(url, kDummyReport);
234 reporter.reset();
235
236 EXPECT_EQ(1u, network_delegate()->num_requests());
237 EXPECT_TRUE(url_request_destroyed);
238 }
239
240 // Test that a request that returns an error gets cleaned up.
241 TEST_F(CertificateReportSenderTest, ErroredRequestGetsDeleted) {
242 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED);
243 CertificateReportSender reporter(
244 context(), CertificateReportSender::DO_NOT_SEND_COOKIES);
245 // SendReport will block until the URLRequest is destroyed.
246 SendReport(&reporter, kDummyReport, url, 0);
247 }
248
249 // Test that cookies are sent or not sent according to the error
250 // reporter's cookies preference.
251
252 TEST_F(CertificateReportSenderTest, SendCookiesPreference) {
253 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
254 CertificateReportSender reporter(context(),
255 CertificateReportSender::SEND_COOKIES);
256
257 network_delegate()->set_expect_cookies(true);
258 SendReport(&reporter, kDummyReport, url, 0);
259 }
260
261 TEST_F(CertificateReportSenderTest, DoNotSendCookiesPreference) {
262 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
263 CertificateReportSender reporter(
264 context(), CertificateReportSender::DO_NOT_SEND_COOKIES);
265
266 network_delegate()->set_expect_cookies(false);
267 SendReport(&reporter, kDummyReport, url, 0);
268 }
269
270 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698