| 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 "components/certificate_reporting/error_reporter.h" | 5 #include "components/certificate_reporting/error_reporter.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <set> | 10 #include <set> |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 } | 44 } |
| 45 | 45 |
| 46 void SuccessCallback(bool* called) { | 46 void SuccessCallback(bool* called) { |
| 47 *called = true; | 47 *called = true; |
| 48 } | 48 } |
| 49 | 49 |
| 50 // A mock ReportSender that keeps track of the last report | 50 // A mock ReportSender that keeps track of the last report |
| 51 // sent. | 51 // sent. |
| 52 class MockCertificateReportSender : public net::ReportSender { | 52 class MockCertificateReportSender : public net::ReportSender { |
| 53 public: | 53 public: |
| 54 MockCertificateReportSender() | 54 MockCertificateReportSender() : net::ReportSender(nullptr) {} |
| 55 : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} | |
| 56 ~MockCertificateReportSender() override {} | 55 ~MockCertificateReportSender() override {} |
| 57 | 56 |
| 58 void Send(const GURL& report_uri, | 57 void Send(const GURL& report_uri, |
| 59 base::StringPiece content_type, | 58 base::StringPiece content_type, |
| 60 base::StringPiece report, | 59 base::StringPiece report, |
| 61 const base::Callback<void()>& success_callback, | 60 const base::Callback<void()>& success_callback, |
| 62 const base::Callback<void(const GURL&, int, int)>& error_callback) | 61 const base::Callback<void(const GURL&, int, int)>& error_callback) |
| 63 override { | 62 override { |
| 64 latest_report_uri_ = report_uri; | 63 latest_report_uri_ = report_uri; |
| 65 report.CopyToString(&latest_report_); | 64 report.CopyToString(&latest_report_); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 | 171 |
| 173 base::RunLoop run_loop; | 172 base::RunLoop run_loop; |
| 174 net::TestURLRequestContext context(true); | 173 net::TestURLRequestContext context(true); |
| 175 TestCertificateReporterNetworkDelegate test_delegate; | 174 TestCertificateReporterNetworkDelegate test_delegate; |
| 176 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); | 175 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); |
| 177 context.set_network_delegate(&test_delegate); | 176 context.set_network_delegate(&test_delegate); |
| 178 context.Init(); | 177 context.Init(); |
| 179 | 178 |
| 180 const GURL report_uri( | 179 const GURL report_uri( |
| 181 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_FAILED)); | 180 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_FAILED)); |
| 182 ErrorReporter reporter(&context, report_uri, | 181 ErrorReporter reporter(&context, report_uri); |
| 183 net::ReportSender::DO_NOT_SEND_COOKIES); | |
| 184 | 182 |
| 185 bool error_callback_called = false; | 183 bool error_callback_called = false; |
| 186 bool success_callback_called = false; | 184 bool success_callback_called = false; |
| 187 reporter.SendExtendedReportingReport( | 185 reporter.SendExtendedReportingReport( |
| 188 kDummyReport, base::Bind(&SuccessCallback, &success_callback_called), | 186 kDummyReport, base::Bind(&SuccessCallback, &success_callback_called), |
| 189 base::Bind(&ErrorCallback, &error_callback_called)); | 187 base::Bind(&ErrorCallback, &error_callback_called)); |
| 190 run_loop.Run(); | 188 run_loop.Run(); |
| 191 | 189 |
| 192 EXPECT_TRUE(error_callback_called); | 190 EXPECT_TRUE(error_callback_called); |
| 193 EXPECT_FALSE(success_callback_called); | 191 EXPECT_FALSE(success_callback_called); |
| 194 } | 192 } |
| 195 | 193 |
| 196 // Tests that an UMA histogram is recorded if a report is successfully sent. | 194 // Tests that an UMA histogram is recorded if a report is successfully sent. |
| 197 TEST_F(ErrorReporterTest, SuccessfulRequestCallsCallback) { | 195 TEST_F(ErrorReporterTest, SuccessfulRequestCallsCallback) { |
| 198 net::URLRequestMockDataJob::AddUrlHandler(); | 196 net::URLRequestMockDataJob::AddUrlHandler(); |
| 199 | 197 |
| 200 base::RunLoop run_loop; | 198 base::RunLoop run_loop; |
| 201 net::TestURLRequestContext context(true); | 199 net::TestURLRequestContext context(true); |
| 202 TestCertificateReporterNetworkDelegate test_delegate; | 200 TestCertificateReporterNetworkDelegate test_delegate; |
| 203 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); | 201 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); |
| 204 context.set_network_delegate(&test_delegate); | 202 context.set_network_delegate(&test_delegate); |
| 205 context.Init(); | 203 context.Init(); |
| 206 | 204 |
| 207 const GURL report_uri( | 205 const GURL report_uri( |
| 208 net::URLRequestMockDataJob::GetMockHttpUrl("some data", 1)); | 206 net::URLRequestMockDataJob::GetMockHttpUrl("some data", 1)); |
| 209 ErrorReporter reporter(&context, report_uri, | 207 ErrorReporter reporter(&context, report_uri); |
| 210 net::ReportSender::DO_NOT_SEND_COOKIES); | |
| 211 | 208 |
| 212 bool error_callback_called = false; | 209 bool error_callback_called = false; |
| 213 bool success_callback_called = false; | 210 bool success_callback_called = false; |
| 214 reporter.SendExtendedReportingReport( | 211 reporter.SendExtendedReportingReport( |
| 215 kDummyReport, base::Bind(&SuccessCallback, &success_callback_called), | 212 kDummyReport, base::Bind(&SuccessCallback, &success_callback_called), |
| 216 base::Bind(&ErrorCallback, &error_callback_called)); | 213 base::Bind(&ErrorCallback, &error_callback_called)); |
| 217 run_loop.Run(); | 214 run_loop.Run(); |
| 218 | 215 |
| 219 EXPECT_FALSE(error_callback_called); | 216 EXPECT_FALSE(error_callback_called); |
| 220 EXPECT_TRUE(success_callback_called); | 217 EXPECT_TRUE(success_callback_called); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 ASSERT_TRUE(encrypted_request.ParseFromString( | 373 ASSERT_TRUE(encrypted_request.ParseFromString( |
| 377 std::string(reinterpret_cast<const char*>(kSerializedEncryptedReport), | 374 std::string(reinterpret_cast<const char*>(kSerializedEncryptedReport), |
| 378 sizeof(kSerializedEncryptedReport)))); | 375 sizeof(kSerializedEncryptedReport)))); |
| 379 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( | 376 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( |
| 380 server_private_key_, encrypted_request, &decrypted_serialized_report)); | 377 server_private_key_, encrypted_request, &decrypted_serialized_report)); |
| 381 } | 378 } |
| 382 | 379 |
| 383 } // namespace | 380 } // namespace |
| 384 | 381 |
| 385 } // namespace certificate_reporting | 382 } // namespace certificate_reporting |
| OLD | NEW |