| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/url_request/certificate_report_sender.h" | 5 #include "net/url_request/report_sender.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
| 13 #include "net/base/network_delegate_impl.h" | 13 #include "net/base/network_delegate_impl.h" |
| 14 #include "net/base/upload_bytes_element_reader.h" | 14 #include "net/base/upload_bytes_element_reader.h" |
| 15 #include "net/base/upload_data_stream.h" | 15 #include "net/base/upload_data_stream.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 EXPECT_EQ(1u, expect_reports->erase(upload_data)); | 47 EXPECT_EQ(1u, expect_reports->erase(upload_data)); |
| 48 } | 48 } |
| 49 | 49 |
| 50 // Provides an error callback for report sending that sets |called| to | 50 // Provides an error callback for report sending that sets |called| to |
| 51 // true. | 51 // true. |
| 52 void ErrorCallback(bool* called, const GURL& report_uri, int net_error) { | 52 void ErrorCallback(bool* called, const GURL& report_uri, int net_error) { |
| 53 EXPECT_NE(OK, net_error); | 53 EXPECT_NE(OK, net_error); |
| 54 *called = true; | 54 *called = true; |
| 55 } | 55 } |
| 56 | 56 |
| 57 // A network delegate that lets tests check that a certificate report | 57 // A network delegate that lets tests check that a report |
| 58 // was sent. It counts the number of requests and lets tests register a | 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 | 59 // callback to run when the request is destroyed. It also checks that |
| 60 // the uploaded data is as expected. | 60 // the uploaded data is as expected. |
| 61 class TestCertificateReportSenderNetworkDelegate : public NetworkDelegateImpl { | 61 class TestReportSenderNetworkDelegate : public NetworkDelegateImpl { |
| 62 public: | 62 public: |
| 63 TestCertificateReportSenderNetworkDelegate() | 63 TestReportSenderNetworkDelegate() |
| 64 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), | 64 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), |
| 65 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)), | 65 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)), |
| 66 num_requests_(0), | 66 num_requests_(0), |
| 67 expect_cookies_(false) {} | 67 expect_cookies_(false) {} |
| 68 | 68 |
| 69 void ExpectReport(const std::string& report) { | 69 void ExpectReport(const std::string& report) { |
| 70 expect_reports_.insert(report); | 70 expect_reports_.insert(report); |
| 71 } | 71 } |
| 72 | 72 |
| 73 void set_all_url_requests_destroyed_callback(const base::Closure& callback) { | 73 void set_all_url_requests_destroyed_callback(const base::Closure& callback) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 } | 117 } |
| 118 | 118 |
| 119 private: | 119 private: |
| 120 base::Closure url_request_destroyed_callback_; | 120 base::Closure url_request_destroyed_callback_; |
| 121 base::Closure all_url_requests_destroyed_callback_; | 121 base::Closure all_url_requests_destroyed_callback_; |
| 122 size_t num_requests_; | 122 size_t num_requests_; |
| 123 GURL expect_url_; | 123 GURL expect_url_; |
| 124 std::set<std::string> expect_reports_; | 124 std::set<std::string> expect_reports_; |
| 125 bool expect_cookies_; | 125 bool expect_cookies_; |
| 126 | 126 |
| 127 DISALLOW_COPY_AND_ASSIGN(TestCertificateReportSenderNetworkDelegate); | 127 DISALLOW_COPY_AND_ASSIGN(TestReportSenderNetworkDelegate); |
| 128 }; | 128 }; |
| 129 | 129 |
| 130 class CertificateReportSenderTest : public ::testing::Test { | 130 class ReportSenderTest : public ::testing::Test { |
| 131 public: | 131 public: |
| 132 CertificateReportSenderTest() : context_(true) { | 132 ReportSenderTest() : context_(true) { |
| 133 context_.set_network_delegate(&network_delegate_); | 133 context_.set_network_delegate(&network_delegate_); |
| 134 context_.Init(); | 134 context_.Init(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void SetUp() override { | 137 void SetUp() override { |
| 138 URLRequestFailedJob::AddUrlHandler(); | 138 URLRequestFailedJob::AddUrlHandler(); |
| 139 URLRequestMockDataJob::AddUrlHandler(); | 139 URLRequestMockDataJob::AddUrlHandler(); |
| 140 } | 140 } |
| 141 | 141 |
| 142 void TearDown() override { URLRequestFilter::GetInstance()->ClearHandlers(); } | 142 void TearDown() override { URLRequestFilter::GetInstance()->ClearHandlers(); } |
| 143 | 143 |
| 144 TestURLRequestContext* context() { return &context_; } | 144 TestURLRequestContext* context() { return &context_; } |
| 145 | 145 |
| 146 protected: | 146 protected: |
| 147 void SendReport(CertificateReportSender* reporter, | 147 void SendReport(ReportSender* reporter, |
| 148 const std::string& report, | 148 const std::string& report, |
| 149 const GURL& url, | 149 const GURL& url, |
| 150 size_t request_sequence_number) { | 150 size_t request_sequence_number) { |
| 151 base::RunLoop run_loop; | 151 base::RunLoop run_loop; |
| 152 network_delegate_.set_url_request_destroyed_callback( | 152 network_delegate_.set_url_request_destroyed_callback( |
| 153 run_loop.QuitClosure()); | 153 run_loop.QuitClosure()); |
| 154 | 154 |
| 155 network_delegate_.set_expect_url(url); | 155 network_delegate_.set_expect_url(url); |
| 156 network_delegate_.ExpectReport(report); | 156 network_delegate_.ExpectReport(report); |
| 157 | 157 |
| 158 EXPECT_EQ(request_sequence_number, network_delegate_.num_requests()); | 158 EXPECT_EQ(request_sequence_number, network_delegate_.num_requests()); |
| 159 | 159 |
| 160 reporter->Send(url, report); | 160 reporter->Send(url, report); |
| 161 | 161 |
| 162 // The report is sent asynchronously, so wait for the report's | 162 // The report is sent asynchronously, so wait for the report's |
| 163 // URLRequest to be destroyed before checking that the report was | 163 // URLRequest to be destroyed before checking that the report was |
| 164 // sent. | 164 // sent. |
| 165 run_loop.Run(); | 165 run_loop.Run(); |
| 166 | 166 |
| 167 EXPECT_EQ(request_sequence_number + 1, network_delegate_.num_requests()); | 167 EXPECT_EQ(request_sequence_number + 1, network_delegate_.num_requests()); |
| 168 } | 168 } |
| 169 | 169 |
| 170 TestCertificateReportSenderNetworkDelegate network_delegate_; | 170 TestReportSenderNetworkDelegate network_delegate_; |
| 171 | 171 |
| 172 private: | 172 private: |
| 173 TestURLRequestContext context_; | 173 TestURLRequestContext context_; |
| 174 }; | 174 }; |
| 175 | 175 |
| 176 // Test that CertificateReportSender::Send creates a URLRequest for the | 176 // Test that ReportSender::Send creates a URLRequest for the |
| 177 // endpoint and sends the expected data. | 177 // endpoint and sends the expected data. |
| 178 TEST_F(CertificateReportSenderTest, SendsRequest) { | 178 TEST_F(ReportSenderTest, SendsRequest) { |
| 179 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 179 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
| 180 CertificateReportSender reporter( | 180 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); |
| 181 context(), CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 182 SendReport(&reporter, kDummyReport, url, 0); | 181 SendReport(&reporter, kDummyReport, url, 0); |
| 183 } | 182 } |
| 184 | 183 |
| 185 TEST_F(CertificateReportSenderTest, SendMultipleReportsSequentially) { | 184 TEST_F(ReportSenderTest, SendMultipleReportsSequentially) { |
| 186 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 185 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
| 187 CertificateReportSender reporter( | 186 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); |
| 188 context(), CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 189 SendReport(&reporter, kDummyReport, url, 0); | 187 SendReport(&reporter, kDummyReport, url, 0); |
| 190 SendReport(&reporter, kDummyReport, url, 1); | 188 SendReport(&reporter, kDummyReport, url, 1); |
| 191 } | 189 } |
| 192 | 190 |
| 193 TEST_F(CertificateReportSenderTest, SendMultipleReportsSimultaneously) { | 191 TEST_F(ReportSenderTest, SendMultipleReportsSimultaneously) { |
| 194 base::RunLoop run_loop; | 192 base::RunLoop run_loop; |
| 195 network_delegate_.set_all_url_requests_destroyed_callback( | 193 network_delegate_.set_all_url_requests_destroyed_callback( |
| 196 run_loop.QuitClosure()); | 194 run_loop.QuitClosure()); |
| 197 | 195 |
| 198 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 196 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
| 199 network_delegate_.set_expect_url(url); | 197 network_delegate_.set_expect_url(url); |
| 200 network_delegate_.ExpectReport(kDummyReport); | 198 network_delegate_.ExpectReport(kDummyReport); |
| 201 network_delegate_.ExpectReport(kSecondDummyReport); | 199 network_delegate_.ExpectReport(kSecondDummyReport); |
| 202 | 200 |
| 203 CertificateReportSender reporter( | 201 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); |
| 204 context(), CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 205 | 202 |
| 206 EXPECT_EQ(0u, network_delegate_.num_requests()); | 203 EXPECT_EQ(0u, network_delegate_.num_requests()); |
| 207 | 204 |
| 208 reporter.Send(url, kDummyReport); | 205 reporter.Send(url, kDummyReport); |
| 209 reporter.Send(url, kSecondDummyReport); | 206 reporter.Send(url, kSecondDummyReport); |
| 210 | 207 |
| 211 run_loop.Run(); | 208 run_loop.Run(); |
| 212 | 209 |
| 213 EXPECT_EQ(2u, network_delegate_.num_requests()); | 210 EXPECT_EQ(2u, network_delegate_.num_requests()); |
| 214 } | 211 } |
| 215 | 212 |
| 216 // Test that pending URLRequests get cleaned up when the report sender | 213 // Test that pending URLRequests get cleaned up when the report sender |
| 217 // is deleted. | 214 // is deleted. |
| 218 TEST_F(CertificateReportSenderTest, PendingRequestGetsDeleted) { | 215 TEST_F(ReportSenderTest, PendingRequestGetsDeleted) { |
| 219 bool url_request_destroyed = false; | 216 bool url_request_destroyed = false; |
| 220 network_delegate_.set_url_request_destroyed_callback(base::Bind( | 217 network_delegate_.set_url_request_destroyed_callback(base::Bind( |
| 221 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed))); | 218 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed))); |
| 222 | 219 |
| 223 GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 220 GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
| 224 URLRequestFailedJob::START, ERR_IO_PENDING); | 221 URLRequestFailedJob::START, ERR_IO_PENDING); |
| 225 network_delegate_.set_expect_url(url); | 222 network_delegate_.set_expect_url(url); |
| 226 network_delegate_.ExpectReport(kDummyReport); | 223 network_delegate_.ExpectReport(kDummyReport); |
| 227 | 224 |
| 228 EXPECT_EQ(0u, network_delegate_.num_requests()); | 225 EXPECT_EQ(0u, network_delegate_.num_requests()); |
| 229 | 226 |
| 230 std::unique_ptr<CertificateReportSender> reporter(new CertificateReportSender( | 227 std::unique_ptr<ReportSender> reporter( |
| 231 context(), CertificateReportSender::DO_NOT_SEND_COOKIES)); | 228 new ReportSender(context(), ReportSender::DO_NOT_SEND_COOKIES)); |
| 232 reporter->Send(url, kDummyReport); | 229 reporter->Send(url, kDummyReport); |
| 233 reporter.reset(); | 230 reporter.reset(); |
| 234 | 231 |
| 235 EXPECT_EQ(1u, network_delegate_.num_requests()); | 232 EXPECT_EQ(1u, network_delegate_.num_requests()); |
| 236 EXPECT_TRUE(url_request_destroyed); | 233 EXPECT_TRUE(url_request_destroyed); |
| 237 } | 234 } |
| 238 | 235 |
| 239 // Test that a request that returns an error gets cleaned up. | 236 // Test that a request that returns an error gets cleaned up. |
| 240 TEST_F(CertificateReportSenderTest, ErroredRequestGetsDeleted) { | 237 TEST_F(ReportSenderTest, ErroredRequestGetsDeleted) { |
| 241 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); | 238 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); |
| 242 CertificateReportSender reporter( | 239 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); |
| 243 context(), CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 244 // SendReport will block until the URLRequest is destroyed. | 240 // SendReport will block until the URLRequest is destroyed. |
| 245 SendReport(&reporter, kDummyReport, url, 0); | 241 SendReport(&reporter, kDummyReport, url, 0); |
| 246 } | 242 } |
| 247 | 243 |
| 248 // Test that the error callback, if provided, gets called when a request | 244 // Test that the error callback, if provided, gets called when a request |
| 249 // returns an error. | 245 // returns an error. |
| 250 TEST_F(CertificateReportSenderTest, ErroredRequestCallsCallback) { | 246 TEST_F(ReportSenderTest, ErroredRequestCallsCallback) { |
| 251 bool error_callback_called = false; | 247 bool error_callback_called = false; |
| 252 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); | 248 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); |
| 253 CertificateReportSender reporter( | 249 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES, |
| 254 context(), CertificateReportSender::DO_NOT_SEND_COOKIES, | 250 base::Bind(ErrorCallback, &error_callback_called)); |
| 255 base::Bind(ErrorCallback, &error_callback_called)); | |
| 256 // SendReport will block until the URLRequest is destroyed. | 251 // SendReport will block until the URLRequest is destroyed. |
| 257 SendReport(&reporter, kDummyReport, url, 0); | 252 SendReport(&reporter, kDummyReport, url, 0); |
| 258 EXPECT_TRUE(error_callback_called); | 253 EXPECT_TRUE(error_callback_called); |
| 259 } | 254 } |
| 260 | 255 |
| 261 // Test that the error callback does not get called when a request | 256 // Test that the error callback does not get called when a request |
| 262 // does not return an error. | 257 // does not return an error. |
| 263 TEST_F(CertificateReportSenderTest, SuccessfulRequestDoesNotCallErrorCallback) { | 258 TEST_F(ReportSenderTest, SuccessfulRequestDoesNotCallErrorCallback) { |
| 264 bool error_callback_called = false; | 259 bool error_callback_called = false; |
| 265 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 260 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
| 266 CertificateReportSender reporter( | 261 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES, |
| 267 context(), CertificateReportSender::DO_NOT_SEND_COOKIES, | 262 base::Bind(ErrorCallback, &error_callback_called)); |
| 268 base::Bind(ErrorCallback, &error_callback_called)); | |
| 269 SendReport(&reporter, kDummyReport, url, 0); | 263 SendReport(&reporter, kDummyReport, url, 0); |
| 270 EXPECT_FALSE(error_callback_called); | 264 EXPECT_FALSE(error_callback_called); |
| 271 } | 265 } |
| 272 | 266 |
| 273 // Test that cookies are sent or not sent according to the error | 267 // Test that cookies are sent or not sent according to the error |
| 274 // reporter's cookies preference. | 268 // reporter's cookies preference. |
| 275 | 269 |
| 276 TEST_F(CertificateReportSenderTest, SendCookiesPreference) { | 270 TEST_F(ReportSenderTest, SendCookiesPreference) { |
| 277 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 271 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
| 278 CertificateReportSender reporter(context(), | 272 ReportSender reporter(context(), ReportSender::SEND_COOKIES); |
| 279 CertificateReportSender::SEND_COOKIES); | |
| 280 | 273 |
| 281 network_delegate_.set_expect_cookies(true); | 274 network_delegate_.set_expect_cookies(true); |
| 282 SendReport(&reporter, kDummyReport, url, 0); | 275 SendReport(&reporter, kDummyReport, url, 0); |
| 283 } | 276 } |
| 284 | 277 |
| 285 TEST_F(CertificateReportSenderTest, DoNotSendCookiesPreference) { | 278 TEST_F(ReportSenderTest, DoNotSendCookiesPreference) { |
| 286 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 279 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
| 287 CertificateReportSender reporter( | 280 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); |
| 288 context(), CertificateReportSender::DO_NOT_SEND_COOKIES); | |
| 289 | 281 |
| 290 network_delegate_.set_expect_cookies(false); | 282 network_delegate_.set_expect_cookies(false); |
| 291 SendReport(&reporter, kDummyReport, url, 0); | 283 SendReport(&reporter, kDummyReport, url, 0); |
| 292 } | 284 } |
| 293 | 285 |
| 294 } // namespace | 286 } // namespace |
| 295 } // namespace net | 287 } // namespace net |
| OLD | NEW |