| 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 28 matching lines...) Expand all Loading... |
| 39 } | 39 } |
| 40 | 40 |
| 41 void SuccessCallback(bool* called) { | 41 void SuccessCallback(bool* called) { |
| 42 *called = true; | 42 *called = true; |
| 43 } | 43 } |
| 44 | 44 |
| 45 // A mock ReportSender that keeps track of the last report | 45 // A mock ReportSender that keeps track of the last report |
| 46 // sent. | 46 // sent. |
| 47 class MockCertificateReportSender : public net::ReportSender { | 47 class MockCertificateReportSender : public net::ReportSender { |
| 48 public: | 48 public: |
| 49 MockCertificateReportSender() | 49 MockCertificateReportSender() : net::ReportSender(nullptr) {} |
| 50 : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} | |
| 51 ~MockCertificateReportSender() override {} | 50 ~MockCertificateReportSender() override {} |
| 52 | 51 |
| 53 void Send( | 52 void Send( |
| 54 const GURL& report_uri, | 53 const GURL& report_uri, |
| 55 base::StringPiece content_type, | 54 base::StringPiece content_type, |
| 56 base::StringPiece report, | 55 base::StringPiece report, |
| 57 const base::Callback<void()>& success_callback, | 56 const base::Callback<void()>& success_callback, |
| 58 const base::Callback<void(const GURL&, int)>& error_callback) override { | 57 const base::Callback<void(const GURL&, int)>& error_callback) override { |
| 59 latest_report_uri_ = report_uri; | 58 latest_report_uri_ = report_uri; |
| 60 report.CopyToString(&latest_report_); | 59 report.CopyToString(&latest_report_); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 TEST_F(ErrorReporterTest, ErroredRequestCallsCallback) { | 162 TEST_F(ErrorReporterTest, ErroredRequestCallsCallback) { |
| 164 net::URLRequestFailedJob::AddUrlHandler(); | 163 net::URLRequestFailedJob::AddUrlHandler(); |
| 165 | 164 |
| 166 base::RunLoop run_loop; | 165 base::RunLoop run_loop; |
| 167 net::TestURLRequestContext context(true); | 166 net::TestURLRequestContext context(true); |
| 168 TestCertificateReporterNetworkDelegate test_delegate; | 167 TestCertificateReporterNetworkDelegate test_delegate; |
| 169 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); | 168 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); |
| 170 context.set_network_delegate(&test_delegate); | 169 context.set_network_delegate(&test_delegate); |
| 171 context.Init(); | 170 context.Init(); |
| 172 | 171 |
| 173 GURL report_uri( | 172 const GURL report_uri( |
| 174 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_FAILED)); | 173 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_FAILED)); |
| 175 ErrorReporter reporter(&context, report_uri, | 174 ErrorReporter reporter(&context, report_uri); |
| 176 net::ReportSender::DO_NOT_SEND_COOKIES); | |
| 177 | 175 |
| 178 bool error_callback_called = false; | 176 bool error_callback_called = false; |
| 179 bool success_callback_called = false; | 177 bool success_callback_called = false; |
| 180 reporter.SendExtendedReportingReport( | 178 reporter.SendExtendedReportingReport( |
| 181 kDummyReport, base::Bind(&SuccessCallback, &success_callback_called), | 179 kDummyReport, base::Bind(&SuccessCallback, &success_callback_called), |
| 182 base::Bind(&ErrorCallback, &error_callback_called)); | 180 base::Bind(&ErrorCallback, &error_callback_called)); |
| 183 run_loop.Run(); | 181 run_loop.Run(); |
| 184 | 182 |
| 185 EXPECT_TRUE(error_callback_called); | 183 EXPECT_TRUE(error_callback_called); |
| 186 EXPECT_FALSE(success_callback_called); | 184 EXPECT_FALSE(success_callback_called); |
| 187 } | 185 } |
| 188 | 186 |
| 189 // Tests that an UMA histogram is recorded if a report fails to send. | 187 // Tests that an UMA histogram is recorded if a report fails to send. |
| 190 TEST_F(ErrorReporterTest, SuccessfulRequestCallsCallback) { | 188 TEST_F(ErrorReporterTest, SuccessfulRequestCallsCallback) { |
| 191 net::URLRequestMockDataJob::AddUrlHandler(); | 189 net::URLRequestMockDataJob::AddUrlHandler(); |
| 192 | 190 |
| 193 base::RunLoop run_loop; | 191 base::RunLoop run_loop; |
| 194 net::TestURLRequestContext context(true); | 192 net::TestURLRequestContext context(true); |
| 195 TestCertificateReporterNetworkDelegate test_delegate; | 193 TestCertificateReporterNetworkDelegate test_delegate; |
| 196 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); | 194 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); |
| 197 context.set_network_delegate(&test_delegate); | 195 context.set_network_delegate(&test_delegate); |
| 198 context.Init(); | 196 context.Init(); |
| 199 | 197 |
| 200 GURL report_uri(net::URLRequestMockDataJob::GetMockHttpUrl("some data", 1)); | 198 const GURL report_uri( |
| 201 ErrorReporter reporter(&context, report_uri, | 199 net::URLRequestMockDataJob::GetMockHttpUrl("some data", 1)); |
| 202 net::ReportSender::DO_NOT_SEND_COOKIES); | 200 ErrorReporter reporter(&context, report_uri); |
| 203 | 201 |
| 204 bool error_callback_called = false; | 202 bool error_callback_called = false; |
| 205 bool success_callback_called = false; | 203 bool success_callback_called = false; |
| 206 reporter.SendExtendedReportingReport( | 204 reporter.SendExtendedReportingReport( |
| 207 kDummyReport, base::Bind(&SuccessCallback, &success_callback_called), | 205 kDummyReport, base::Bind(&SuccessCallback, &success_callback_called), |
| 208 base::Bind(&ErrorCallback, &error_callback_called)); | 206 base::Bind(&ErrorCallback, &error_callback_called)); |
| 209 run_loop.Run(); | 207 run_loop.Run(); |
| 210 | 208 |
| 211 EXPECT_FALSE(error_callback_called); | 209 EXPECT_FALSE(error_callback_called); |
| 212 EXPECT_TRUE(success_callback_called); | 210 EXPECT_TRUE(success_callback_called); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 ASSERT_TRUE(encrypted_request.ParseFromString( | 366 ASSERT_TRUE(encrypted_request.ParseFromString( |
| 369 std::string(reinterpret_cast<const char*>(kSerializedEncryptedReport), | 367 std::string(reinterpret_cast<const char*>(kSerializedEncryptedReport), |
| 370 sizeof(kSerializedEncryptedReport)))); | 368 sizeof(kSerializedEncryptedReport)))); |
| 371 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( | 369 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( |
| 372 server_private_key_, encrypted_request, &decrypted_serialized_report)); | 370 server_private_key_, encrypted_request, &decrypted_serialized_report)); |
| 373 } | 371 } |
| 374 | 372 |
| 375 } // namespace | 373 } // namespace |
| 376 | 374 |
| 377 } // namespace certificate_reporting | 375 } // namespace certificate_reporting |
| OLD | NEW |