| 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/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/threading/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 namespace { | |
| 25 | |
| 26 const char kDummyReport[] = "foo.test"; | |
| 27 const char kSecondDummyReport[] = "foo2.test"; | |
| 28 | |
| 29 void MarkURLRequestDestroyed(bool* url_request_destroyed) { | |
| 30 *url_request_destroyed = true; | |
| 31 } | |
| 32 | |
| 33 // Checks that data uploaded in the request matches the test report | |
| 34 // data. Erases the sent reports from |expect_reports|. | |
| 35 void CheckUploadData(const URLRequest& request, | |
| 36 std::set<std::string>* expect_reports) { | |
| 37 const UploadDataStream* upload = request.get_upload(); | |
| 38 ASSERT_TRUE(upload); | |
| 39 ASSERT_TRUE(upload->GetElementReaders()); | |
| 40 ASSERT_EQ(1u, upload->GetElementReaders()->size()); | |
| 41 | |
| 42 const UploadBytesElementReader* reader = | |
| 43 (*upload->GetElementReaders())[0]->AsBytesReader(); | |
| 44 ASSERT_TRUE(reader); | |
| 45 std::string upload_data(reader->bytes(), reader->length()); | |
| 46 | |
| 47 EXPECT_EQ(1u, expect_reports->erase(upload_data)); | |
| 48 } | |
| 49 | |
| 50 // Provides an error callback for report sending that sets |called| to | |
| 51 // true. | |
| 52 void ErrorCallback(bool* called, const GURL& report_uri, int net_error) { | |
| 53 EXPECT_NE(OK, net_error); | |
| 54 *called = true; | |
| 55 } | |
| 56 | |
| 57 // A network delegate that lets tests check that a certificate report | |
| 58 // was sent. It counts the number of requests and lets tests register a | |
| 59 // callback to run when the request is destroyed. It also checks that | |
| 60 // the uploaded data is as expected. | |
| 61 class TestCertificateReportSenderNetworkDelegate : public NetworkDelegateImpl { | |
| 62 public: | |
| 63 TestCertificateReportSenderNetworkDelegate() | |
| 64 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), | |
| 65 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)), | |
| 66 num_requests_(0), | |
| 67 expect_cookies_(false) {} | |
| 68 | |
| 69 void ExpectReport(const std::string& report) { | |
| 70 expect_reports_.insert(report); | |
| 71 } | |
| 72 | |
| 73 void set_all_url_requests_destroyed_callback(const base::Closure& callback) { | |
| 74 all_url_requests_destroyed_callback_ = callback; | |
| 75 } | |
| 76 | |
| 77 void set_url_request_destroyed_callback(const base::Closure& callback) { | |
| 78 url_request_destroyed_callback_ = callback; | |
| 79 } | |
| 80 | |
| 81 void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; } | |
| 82 | |
| 83 size_t num_requests() const { return num_requests_; } | |
| 84 | |
| 85 // Sets whether cookies are expected to be sent on requests. | |
| 86 void set_expect_cookies(bool expect_cookies) { | |
| 87 expect_cookies_ = expect_cookies; | |
| 88 } | |
| 89 | |
| 90 // NetworkDelegateImpl implementation. | |
| 91 int OnBeforeURLRequest(URLRequest* request, | |
| 92 const CompletionCallback& callback, | |
| 93 GURL* new_url) override { | |
| 94 num_requests_++; | |
| 95 EXPECT_EQ(expect_url_, request->url()); | |
| 96 EXPECT_STRCASEEQ("POST", request->method().data()); | |
| 97 | |
| 98 if (expect_cookies_) { | |
| 99 EXPECT_FALSE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES); | |
| 100 EXPECT_FALSE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES); | |
| 101 } else { | |
| 102 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES); | |
| 103 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES); | |
| 104 } | |
| 105 | |
| 106 CheckUploadData(*request, &expect_reports_); | |
| 107 | |
| 108 // Unconditionally return OK, since the sender ignores the results | |
| 109 // anyway. | |
| 110 return OK; | |
| 111 } | |
| 112 | |
| 113 void OnURLRequestDestroyed(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 CertificateReportSenderTest : public ::testing::Test { | |
| 131 public: | |
| 132 CertificateReportSenderTest() : context_(true) { | |
| 133 context_.set_network_delegate(&network_delegate_); | |
| 134 context_.Init(); | |
| 135 } | |
| 136 | |
| 137 void SetUp() override { | |
| 138 URLRequestFailedJob::AddUrlHandler(); | |
| 139 URLRequestMockDataJob::AddUrlHandler(); | |
| 140 } | |
| 141 | |
| 142 void TearDown() override { URLRequestFilter::GetInstance()->ClearHandlers(); } | |
| 143 | |
| 144 TestURLRequestContext* context() { return &context_; } | |
| 145 | |
| 146 protected: | |
| 147 void SendReport(CertificateReportSender* reporter, | |
| 148 const std::string& report, | |
| 149 const GURL& url, | |
| 150 size_t request_sequence_number) { | |
| 151 base::RunLoop run_loop; | |
| 152 network_delegate_.set_url_request_destroyed_callback( | |
| 153 run_loop.QuitClosure()); | |
| 154 | |
| 155 network_delegate_.set_expect_url(url); | |
| 156 network_delegate_.ExpectReport(report); | |
| 157 | |
| 158 EXPECT_EQ(request_sequence_number, network_delegate_.num_requests()); | |
| 159 | |
| 160 reporter->Send(url, report); | |
| 161 | |
| 162 // The report is sent asynchronously, so wait for the report's | |
| 163 // URLRequest to be destroyed before checking that the report was | |
| 164 // sent. | |
| 165 run_loop.Run(); | |
| 166 | |
| 167 EXPECT_EQ(request_sequence_number + 1, network_delegate_.num_requests()); | |
| 168 } | |
| 169 | |
| 170 TestCertificateReportSenderNetworkDelegate network_delegate_; | |
| 171 | |
| 172 private: | |
| 173 TestURLRequestContext context_; | |
| 174 }; | |
| 175 | |
| 176 // Test that CertificateReportSender::Send creates a URLRequest for the | |
| 177 // endpoint and sends the expected data. | |
| 178 TEST_F(CertificateReportSenderTest, SendsRequest) { | |
| 179 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
| 180 CertificateReportSender reporter( | |
| 181 context(), CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 182 SendReport(&reporter, kDummyReport, url, 0); | |
| 183 } | |
| 184 | |
| 185 TEST_F(CertificateReportSenderTest, SendMultipleReportsSequentially) { | |
| 186 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
| 187 CertificateReportSender reporter( | |
| 188 context(), CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 189 SendReport(&reporter, kDummyReport, url, 0); | |
| 190 SendReport(&reporter, kDummyReport, url, 1); | |
| 191 } | |
| 192 | |
| 193 TEST_F(CertificateReportSenderTest, SendMultipleReportsSimultaneously) { | |
| 194 base::RunLoop run_loop; | |
| 195 network_delegate_.set_all_url_requests_destroyed_callback( | |
| 196 run_loop.QuitClosure()); | |
| 197 | |
| 198 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
| 199 network_delegate_.set_expect_url(url); | |
| 200 network_delegate_.ExpectReport(kDummyReport); | |
| 201 network_delegate_.ExpectReport(kSecondDummyReport); | |
| 202 | |
| 203 CertificateReportSender reporter( | |
| 204 context(), CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 205 | |
| 206 EXPECT_EQ(0u, network_delegate_.num_requests()); | |
| 207 | |
| 208 reporter.Send(url, kDummyReport); | |
| 209 reporter.Send(url, kSecondDummyReport); | |
| 210 | |
| 211 run_loop.Run(); | |
| 212 | |
| 213 EXPECT_EQ(2u, network_delegate_.num_requests()); | |
| 214 } | |
| 215 | |
| 216 // Test that pending URLRequests get cleaned up when the report sender | |
| 217 // is deleted. | |
| 218 TEST_F(CertificateReportSenderTest, PendingRequestGetsDeleted) { | |
| 219 bool url_request_destroyed = false; | |
| 220 network_delegate_.set_url_request_destroyed_callback(base::Bind( | |
| 221 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed))); | |
| 222 | |
| 223 GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | |
| 224 URLRequestFailedJob::START, ERR_IO_PENDING); | |
| 225 network_delegate_.set_expect_url(url); | |
| 226 network_delegate_.ExpectReport(kDummyReport); | |
| 227 | |
| 228 EXPECT_EQ(0u, network_delegate_.num_requests()); | |
| 229 | |
| 230 std::unique_ptr<CertificateReportSender> reporter(new CertificateReportSender( | |
| 231 context(), CertificateReportSender::DO_NOT_SEND_COOKIES)); | |
| 232 reporter->Send(url, kDummyReport); | |
| 233 reporter.reset(); | |
| 234 | |
| 235 EXPECT_EQ(1u, network_delegate_.num_requests()); | |
| 236 EXPECT_TRUE(url_request_destroyed); | |
| 237 } | |
| 238 | |
| 239 // Test that a request that returns an error gets cleaned up. | |
| 240 TEST_F(CertificateReportSenderTest, ErroredRequestGetsDeleted) { | |
| 241 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); | |
| 242 CertificateReportSender reporter( | |
| 243 context(), CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 244 // SendReport will block until the URLRequest is destroyed. | |
| 245 SendReport(&reporter, kDummyReport, url, 0); | |
| 246 } | |
| 247 | |
| 248 // Test that the error callback, if provided, gets called when a request | |
| 249 // returns an error. | |
| 250 TEST_F(CertificateReportSenderTest, ErroredRequestCallsCallback) { | |
| 251 bool error_callback_called = false; | |
| 252 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); | |
| 253 CertificateReportSender reporter( | |
| 254 context(), CertificateReportSender::DO_NOT_SEND_COOKIES, | |
| 255 base::Bind(ErrorCallback, &error_callback_called)); | |
| 256 // SendReport will block until the URLRequest is destroyed. | |
| 257 SendReport(&reporter, kDummyReport, url, 0); | |
| 258 EXPECT_TRUE(error_callback_called); | |
| 259 } | |
| 260 | |
| 261 // Test that the error callback does not get called when a request | |
| 262 // does not return an error. | |
| 263 TEST_F(CertificateReportSenderTest, SuccessfulRequestDoesNotCallErrorCallback) { | |
| 264 bool error_callback_called = false; | |
| 265 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
| 266 CertificateReportSender reporter( | |
| 267 context(), CertificateReportSender::DO_NOT_SEND_COOKIES, | |
| 268 base::Bind(ErrorCallback, &error_callback_called)); | |
| 269 SendReport(&reporter, kDummyReport, url, 0); | |
| 270 EXPECT_FALSE(error_callback_called); | |
| 271 } | |
| 272 | |
| 273 // Test that cookies are sent or not sent according to the error | |
| 274 // reporter's cookies preference. | |
| 275 | |
| 276 TEST_F(CertificateReportSenderTest, SendCookiesPreference) { | |
| 277 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
| 278 CertificateReportSender reporter(context(), | |
| 279 CertificateReportSender::SEND_COOKIES); | |
| 280 | |
| 281 network_delegate_.set_expect_cookies(true); | |
| 282 SendReport(&reporter, kDummyReport, url, 0); | |
| 283 } | |
| 284 | |
| 285 TEST_F(CertificateReportSenderTest, DoNotSendCookiesPreference) { | |
| 286 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
| 287 CertificateReportSender reporter( | |
| 288 context(), CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 289 | |
| 290 network_delegate_.set_expect_cookies(false); | |
| 291 SendReport(&reporter, kDummyReport, url, 0); | |
| 292 } | |
| 293 | |
| 294 } // namespace | |
| 295 } // namespace net | |
| OLD | NEW |