| 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> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/test/histogram_tester.h" | |
| 20 #include "components/certificate_reporting/encrypted_cert_logger.pb.h" | 19 #include "components/certificate_reporting/encrypted_cert_logger.pb.h" |
| 21 #include "crypto/curve25519.h" | 20 #include "crypto/curve25519.h" |
| 22 #include "net/test/url_request/url_request_failed_job.h" | 21 #include "net/test/url_request/url_request_failed_job.h" |
| 22 #include "net/test/url_request/url_request_mock_data_job.h" |
| 23 #include "net/url_request/report_sender.h" | 23 #include "net/url_request/report_sender.h" |
| 24 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 26 |
| 27 namespace certificate_reporting { | 27 namespace certificate_reporting { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 const char kDummyHttpReportUri[] = "http://example.test"; | 31 const char kDummyHttpReportUri[] = "http://example.test"; |
| 32 const char kDummyHttpsReportUri[] = "https://example.test"; | 32 const char kDummyHttpsReportUri[] = "https://example.test"; |
| 33 const char kDummyReport[] = "a dummy report"; | 33 const char kDummyReport[] = "a dummy report"; |
| 34 const uint32_t kServerPublicKeyTestVersion = 16; | 34 const uint32_t kServerPublicKeyTestVersion = 16; |
| 35 const char kFailureHistogramName[] = "SSL.CertificateErrorReportFailure"; | 35 |
| 36 void ErrorCallback(bool* called, const GURL& report_uri, int net_error) { |
| 37 EXPECT_NE(net::OK, net_error); |
| 38 *called = true; |
| 39 } |
| 40 |
| 41 void SuccessCallback(bool* called) { |
| 42 *called = true; |
| 43 } |
| 36 | 44 |
| 37 // A mock ReportSender that keeps track of the last report | 45 // A mock ReportSender that keeps track of the last report |
| 38 // sent. | 46 // sent. |
| 39 class MockCertificateReportSender : public net::ReportSender { | 47 class MockCertificateReportSender : public net::ReportSender { |
| 40 public: | 48 public: |
| 41 MockCertificateReportSender() | 49 MockCertificateReportSender() |
| 42 : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} | 50 : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} |
| 43 ~MockCertificateReportSender() override {} | 51 ~MockCertificateReportSender() override {} |
| 44 | 52 |
| 45 void Send( | 53 void Send( |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 // Test that ErrorReporter::SendExtendedReportingReport sends | 119 // Test that ErrorReporter::SendExtendedReportingReport sends |
| 112 // an encrypted or plaintext extended reporting report as appropriate. | 120 // an encrypted or plaintext extended reporting report as appropriate. |
| 113 TEST_F(ErrorReporterTest, ExtendedReportingSendReport) { | 121 TEST_F(ErrorReporterTest, ExtendedReportingSendReport) { |
| 114 // Data should not be encrypted when sent to an HTTPS URL. | 122 // Data should not be encrypted when sent to an HTTPS URL. |
| 115 MockCertificateReportSender* mock_report_sender = | 123 MockCertificateReportSender* mock_report_sender = |
| 116 new MockCertificateReportSender(); | 124 new MockCertificateReportSender(); |
| 117 GURL https_url(kDummyHttpsReportUri); | 125 GURL https_url(kDummyHttpsReportUri); |
| 118 ErrorReporter https_reporter(https_url, server_public_key_, | 126 ErrorReporter https_reporter(https_url, server_public_key_, |
| 119 kServerPublicKeyTestVersion, | 127 kServerPublicKeyTestVersion, |
| 120 base::WrapUnique(mock_report_sender)); | 128 base::WrapUnique(mock_report_sender)); |
| 121 https_reporter.SendExtendedReportingReport(kDummyReport); | 129 https_reporter.SendExtendedReportingReport( |
| 130 kDummyReport, base::Closure(), base::Callback<void(const GURL&, int)>()); |
| 122 EXPECT_EQ(mock_report_sender->latest_report_uri(), https_url); | 131 EXPECT_EQ(mock_report_sender->latest_report_uri(), https_url); |
| 123 EXPECT_EQ(mock_report_sender->latest_report(), kDummyReport); | 132 EXPECT_EQ(mock_report_sender->latest_report(), kDummyReport); |
| 124 | 133 |
| 125 // Data should be encrypted when sent to an HTTP URL. | 134 // Data should be encrypted when sent to an HTTP URL. |
| 126 MockCertificateReportSender* http_mock_report_sender = | 135 MockCertificateReportSender* http_mock_report_sender = |
| 127 new MockCertificateReportSender(); | 136 new MockCertificateReportSender(); |
| 128 GURL http_url(kDummyHttpReportUri); | 137 GURL http_url(kDummyHttpReportUri); |
| 129 ErrorReporter http_reporter(http_url, server_public_key_, | 138 ErrorReporter http_reporter(http_url, server_public_key_, |
| 130 kServerPublicKeyTestVersion, | 139 kServerPublicKeyTestVersion, |
| 131 base::WrapUnique(http_mock_report_sender)); | 140 base::WrapUnique(http_mock_report_sender)); |
| 132 http_reporter.SendExtendedReportingReport(kDummyReport); | 141 http_reporter.SendExtendedReportingReport( |
| 142 kDummyReport, base::Closure(), base::Callback<void(const GURL&, int)>()); |
| 133 | 143 |
| 134 EXPECT_EQ(http_mock_report_sender->latest_report_uri(), http_url); | 144 EXPECT_EQ(http_mock_report_sender->latest_report_uri(), http_url); |
| 135 EXPECT_EQ("application/octet-stream", | 145 EXPECT_EQ("application/octet-stream", |
| 136 http_mock_report_sender->latest_content_type()); | 146 http_mock_report_sender->latest_content_type()); |
| 137 | 147 |
| 138 std::string uploaded_report; | 148 std::string uploaded_report; |
| 139 EncryptedCertLoggerRequest encrypted_request; | 149 EncryptedCertLoggerRequest encrypted_request; |
| 140 ASSERT_TRUE(encrypted_request.ParseFromString( | 150 ASSERT_TRUE(encrypted_request.ParseFromString( |
| 141 http_mock_report_sender->latest_report())); | 151 http_mock_report_sender->latest_report())); |
| 142 EXPECT_EQ(kServerPublicKeyTestVersion, | 152 EXPECT_EQ(kServerPublicKeyTestVersion, |
| 143 encrypted_request.server_public_key_version()); | 153 encrypted_request.server_public_key_version()); |
| 144 EXPECT_EQ(EncryptedCertLoggerRequest::AEAD_ECDH_AES_128_CTR_HMAC_SHA256, | 154 EXPECT_EQ(EncryptedCertLoggerRequest::AEAD_ECDH_AES_128_CTR_HMAC_SHA256, |
| 145 encrypted_request.algorithm()); | 155 encrypted_request.algorithm()); |
| 146 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( | 156 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( |
| 147 server_private_key_, encrypted_request, &uploaded_report)); | 157 server_private_key_, encrypted_request, &uploaded_report)); |
| 148 | 158 |
| 149 EXPECT_EQ(kDummyReport, uploaded_report); | 159 EXPECT_EQ(kDummyReport, uploaded_report); |
| 150 } | 160 } |
| 151 | 161 |
| 152 // Tests that an UMA histogram is recorded if a report fails to send. | 162 // Tests that an UMA histogram is recorded if a report fails to send. |
| 153 TEST_F(ErrorReporterTest, UMAOnFailure) { | 163 TEST_F(ErrorReporterTest, ErroredRequestCallsCallback) { |
| 154 net::URLRequestFailedJob::AddUrlHandler(); | 164 net::URLRequestFailedJob::AddUrlHandler(); |
| 155 | 165 |
| 156 base::HistogramTester histograms; | |
| 157 histograms.ExpectTotalCount(kFailureHistogramName, 0); | |
| 158 | |
| 159 base::RunLoop run_loop; | 166 base::RunLoop run_loop; |
| 160 net::TestURLRequestContext context(true); | 167 net::TestURLRequestContext context(true); |
| 161 TestCertificateReporterNetworkDelegate test_delegate; | 168 TestCertificateReporterNetworkDelegate test_delegate; |
| 162 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); | 169 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); |
| 163 context.set_network_delegate(&test_delegate); | 170 context.set_network_delegate(&test_delegate); |
| 164 context.Init(); | 171 context.Init(); |
| 165 | 172 |
| 166 GURL report_uri( | 173 GURL report_uri( |
| 167 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_FAILED)); | 174 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_FAILED)); |
| 168 ErrorReporter reporter(&context, report_uri, | 175 ErrorReporter reporter(&context, report_uri, |
| 169 net::ReportSender::DO_NOT_SEND_COOKIES); | 176 net::ReportSender::DO_NOT_SEND_COOKIES); |
| 170 reporter.SendExtendedReportingReport(kDummyReport); | 177 |
| 178 bool error_callback_called = false; |
| 179 bool success_callback_called = false; |
| 180 reporter.SendExtendedReportingReport( |
| 181 kDummyReport, base::Bind(&SuccessCallback, &success_callback_called), |
| 182 base::Bind(&ErrorCallback, &error_callback_called)); |
| 171 run_loop.Run(); | 183 run_loop.Run(); |
| 172 | 184 |
| 173 histograms.ExpectTotalCount(kFailureHistogramName, 1); | 185 EXPECT_TRUE(error_callback_called); |
| 174 histograms.ExpectBucketCount(kFailureHistogramName, | 186 EXPECT_FALSE(success_callback_called); |
| 175 -net::ERR_CONNECTION_FAILED, 1); | 187 } |
| 188 |
| 189 // Tests that an UMA histogram is recorded if a report fails to send. |
| 190 TEST_F(ErrorReporterTest, SuccessfulRequestCallsCallback) { |
| 191 net::URLRequestMockDataJob::AddUrlHandler(); |
| 192 |
| 193 base::RunLoop run_loop; |
| 194 net::TestURLRequestContext context(true); |
| 195 TestCertificateReporterNetworkDelegate test_delegate; |
| 196 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); |
| 197 context.set_network_delegate(&test_delegate); |
| 198 context.Init(); |
| 199 |
| 200 GURL report_uri(net::URLRequestMockDataJob::GetMockHttpUrl("some data", 1)); |
| 201 ErrorReporter reporter(&context, report_uri, |
| 202 net::ReportSender::DO_NOT_SEND_COOKIES); |
| 203 |
| 204 bool error_callback_called = false; |
| 205 bool success_callback_called = false; |
| 206 reporter.SendExtendedReportingReport( |
| 207 kDummyReport, base::Bind(&SuccessCallback, &success_callback_called), |
| 208 base::Bind(&ErrorCallback, &error_callback_called)); |
| 209 run_loop.Run(); |
| 210 |
| 211 EXPECT_FALSE(error_callback_called); |
| 212 EXPECT_TRUE(success_callback_called); |
| 176 } | 213 } |
| 177 | 214 |
| 178 // This test decrypts a "known gold" report. It's intentionally brittle | 215 // This test decrypts a "known gold" report. It's intentionally brittle |
| 179 // in order to catch changes in report encryption that could cause the | 216 // in order to catch changes in report encryption that could cause the |
| 180 // server to no longer be able to decrypt reports that it receives from | 217 // server to no longer be able to decrypt reports that it receives from |
| 181 // Chrome. | 218 // Chrome. |
| 182 TEST_F(ErrorReporterTest, DecryptExampleReport) { | 219 TEST_F(ErrorReporterTest, DecryptExampleReport) { |
| 183 // This data should not be changed without also changing the | 220 // This data should not be changed without also changing the |
| 184 // corresponding server-side test. | 221 // corresponding server-side test. |
| 185 const unsigned char kSerializedEncryptedReport[] = { | 222 const unsigned char kSerializedEncryptedReport[] = { |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 ASSERT_TRUE(encrypted_request.ParseFromString( | 368 ASSERT_TRUE(encrypted_request.ParseFromString( |
| 332 std::string(reinterpret_cast<const char*>(kSerializedEncryptedReport), | 369 std::string(reinterpret_cast<const char*>(kSerializedEncryptedReport), |
| 333 sizeof(kSerializedEncryptedReport)))); | 370 sizeof(kSerializedEncryptedReport)))); |
| 334 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( | 371 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( |
| 335 server_private_key_, encrypted_request, &decrypted_serialized_report)); | 372 server_private_key_, encrypted_request, &decrypted_serialized_report)); |
| 336 } | 373 } |
| 337 | 374 |
| 338 } // namespace | 375 } // namespace |
| 339 | 376 |
| 340 } // namespace certificate_reporting | 377 } // namespace certificate_reporting |
| OLD | NEW |