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/run_loop.h" |
| 18 #include "base/test/histogram_tester.h" |
17 #include "components/certificate_reporting/encrypted_cert_logger.pb.h" | 19 #include "components/certificate_reporting/encrypted_cert_logger.pb.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" |
18 #include "crypto/curve25519.h" | 21 #include "crypto/curve25519.h" |
| 22 #include "net/test/url_request/url_request_failed_job.h" |
19 #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" |
20 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
21 | 26 |
22 namespace certificate_reporting { | 27 namespace certificate_reporting { |
23 | 28 |
24 namespace { | 29 namespace { |
25 | 30 |
26 const char kDummyHttpReportUri[] = "http://example.test"; | 31 const char kDummyHttpReportUri[] = "http://example.test"; |
27 const char kDummyHttpsReportUri[] = "https://example.test"; | 32 const char kDummyHttpsReportUri[] = "https://example.test"; |
28 const char kDummyReport[] = "a dummy report"; | 33 const char kDummyReport[] = "a dummy report"; |
29 const uint32_t kServerPublicKeyTestVersion = 16; | 34 const uint32_t kServerPublicKeyTestVersion = 16; |
| 35 const char kFailureHistogramName[] = "SSL.CertificateErrorReportFailure"; |
30 | 36 |
31 // A mock ReportSender that keeps track of the last report | 37 // A mock ReportSender that keeps track of the last report |
32 // sent. | 38 // sent. |
33 class MockCertificateReportSender : public net::ReportSender { | 39 class MockCertificateReportSender : public net::ReportSender { |
34 public: | 40 public: |
35 MockCertificateReportSender() | 41 MockCertificateReportSender() |
36 : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} | 42 : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} |
37 ~MockCertificateReportSender() override {} | 43 ~MockCertificateReportSender() override {} |
38 | 44 |
39 void Send(const GURL& report_uri, const std::string& report) override { | 45 void Send(const GURL& report_uri, const std::string& report) override { |
40 latest_report_uri_ = report_uri; | 46 latest_report_uri_ = report_uri; |
41 latest_report_ = report; | 47 latest_report_ = report; |
42 } | 48 } |
43 | 49 |
44 const GURL& latest_report_uri() { return latest_report_uri_; } | 50 const GURL& latest_report_uri() { return latest_report_uri_; } |
45 | 51 |
46 const std::string& latest_report() { return latest_report_; } | 52 const std::string& latest_report() { return latest_report_; } |
47 | 53 |
48 private: | 54 private: |
49 GURL latest_report_uri_; | 55 GURL latest_report_uri_; |
50 std::string latest_report_; | 56 std::string latest_report_; |
51 | 57 |
52 DISALLOW_COPY_AND_ASSIGN(MockCertificateReportSender); | 58 DISALLOW_COPY_AND_ASSIGN(MockCertificateReportSender); |
53 }; | 59 }; |
54 | 60 |
| 61 // A test network delegate that allows the user to specify a callback to |
| 62 // be run whenever a net::URLRequest is destroyed. |
| 63 class TestCertificateReporterNetworkDelegate : public net::NetworkDelegateImpl { |
| 64 public: |
| 65 TestCertificateReporterNetworkDelegate() |
| 66 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)) {} |
| 67 |
| 68 void set_url_request_destroyed_callback(const base::Closure& callback) { |
| 69 url_request_destroyed_callback_ = callback; |
| 70 } |
| 71 |
| 72 // net::NetworkDelegateImpl: |
| 73 void OnURLRequestDestroyed(net::URLRequest* request) override { |
| 74 url_request_destroyed_callback_.Run(); |
| 75 } |
| 76 |
| 77 private: |
| 78 base::Closure url_request_destroyed_callback_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(TestCertificateReporterNetworkDelegate); |
| 81 }; |
| 82 |
55 class ErrorReporterTest : public ::testing::Test { | 83 class ErrorReporterTest : public ::testing::Test { |
56 public: | 84 public: |
57 ErrorReporterTest() { | 85 ErrorReporterTest() |
| 86 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
58 memset(server_private_key_, 1, sizeof(server_private_key_)); | 87 memset(server_private_key_, 1, sizeof(server_private_key_)); |
59 crypto::curve25519::ScalarBaseMult(server_private_key_, server_public_key_); | 88 crypto::curve25519::ScalarBaseMult(server_private_key_, server_public_key_); |
60 } | 89 } |
61 | 90 |
62 ~ErrorReporterTest() override {} | 91 ~ErrorReporterTest() override {} |
63 | 92 |
64 protected: | 93 protected: |
| 94 content::TestBrowserThreadBundle thread_bundle_; |
65 uint8_t server_public_key_[32]; | 95 uint8_t server_public_key_[32]; |
66 uint8_t server_private_key_[32]; | 96 uint8_t server_private_key_[32]; |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(ErrorReporterTest); |
67 }; | 99 }; |
68 | 100 |
69 // Test that ErrorReporter::SendExtendedReportingReport sends | 101 // Test that ErrorReporter::SendExtendedReportingReport sends |
70 // an encrypted or plaintext extended reporting report as appropriate. | 102 // an encrypted or plaintext extended reporting report as appropriate. |
71 TEST_F(ErrorReporterTest, ExtendedReportingSendReport) { | 103 TEST_F(ErrorReporterTest, ExtendedReportingSendReport) { |
72 // Data should not be encrypted when sent to an HTTPS URL. | 104 // Data should not be encrypted when sent to an HTTPS URL. |
73 MockCertificateReportSender* mock_report_sender = | 105 MockCertificateReportSender* mock_report_sender = |
74 new MockCertificateReportSender(); | 106 new MockCertificateReportSender(); |
75 GURL https_url(kDummyHttpsReportUri); | 107 GURL https_url(kDummyHttpsReportUri); |
76 ErrorReporter https_reporter(https_url, server_public_key_, | 108 ErrorReporter https_reporter(https_url, server_public_key_, |
(...skipping 21 matching lines...) Expand all Loading... |
98 EXPECT_EQ(kServerPublicKeyTestVersion, | 130 EXPECT_EQ(kServerPublicKeyTestVersion, |
99 encrypted_request.server_public_key_version()); | 131 encrypted_request.server_public_key_version()); |
100 EXPECT_EQ(EncryptedCertLoggerRequest::AEAD_ECDH_AES_128_CTR_HMAC_SHA256, | 132 EXPECT_EQ(EncryptedCertLoggerRequest::AEAD_ECDH_AES_128_CTR_HMAC_SHA256, |
101 encrypted_request.algorithm()); | 133 encrypted_request.algorithm()); |
102 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( | 134 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( |
103 server_private_key_, encrypted_request, &uploaded_report)); | 135 server_private_key_, encrypted_request, &uploaded_report)); |
104 | 136 |
105 EXPECT_EQ(kDummyReport, uploaded_report); | 137 EXPECT_EQ(kDummyReport, uploaded_report); |
106 } | 138 } |
107 | 139 |
| 140 // Tests that an UMA histogram is recorded if a report fails to send. |
| 141 TEST_F(ErrorReporterTest, UMAOnFailure) { |
| 142 net::URLRequestFailedJob::AddUrlHandler(); |
| 143 |
| 144 base::HistogramTester histograms; |
| 145 histograms.ExpectTotalCount(kFailureHistogramName, 0); |
| 146 |
| 147 base::RunLoop run_loop; |
| 148 net::TestURLRequestContext context(true); |
| 149 TestCertificateReporterNetworkDelegate test_delegate; |
| 150 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); |
| 151 context.set_network_delegate(&test_delegate); |
| 152 context.Init(); |
| 153 |
| 154 GURL report_uri( |
| 155 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_FAILED)); |
| 156 ErrorReporter reporter(&context, report_uri, |
| 157 net::ReportSender::DO_NOT_SEND_COOKIES); |
| 158 reporter.SendExtendedReportingReport(kDummyReport); |
| 159 run_loop.Run(); |
| 160 |
| 161 histograms.ExpectTotalCount(kFailureHistogramName, 1); |
| 162 histograms.ExpectBucketCount(kFailureHistogramName, |
| 163 -net::ERR_CONNECTION_FAILED, 1); |
| 164 } |
| 165 |
108 // This test decrypts a "known gold" report. It's intentionally brittle | 166 // This test decrypts a "known gold" report. It's intentionally brittle |
109 // in order to catch changes in report encryption that could cause the | 167 // in order to catch changes in report encryption that could cause the |
110 // server to no longer be able to decrypt reports that it receives from | 168 // server to no longer be able to decrypt reports that it receives from |
111 // Chrome. | 169 // Chrome. |
112 TEST_F(ErrorReporterTest, DecryptExampleReport) { | 170 TEST_F(ErrorReporterTest, DecryptExampleReport) { |
113 // This data should not be changed without also changing the | 171 // This data should not be changed without also changing the |
114 // corresponding server-side test. | 172 // corresponding server-side test. |
115 const unsigned char kSerializedEncryptedReport[] = { | 173 const unsigned char kSerializedEncryptedReport[] = { |
116 0x0A, 0xFB, 0x0C, 0xD5, 0x44, 0x21, 0x36, 0x4D, 0xFC, 0x29, 0x56, 0xBD, | 174 0x0A, 0xFB, 0x0C, 0xD5, 0x44, 0x21, 0x36, 0x4D, 0xFC, 0x29, 0x56, 0xBD, |
117 0x47, 0x18, 0xB1, 0x6F, 0x97, 0xF1, 0xF0, 0x3C, 0x31, 0x31, 0x1D, 0xD7, | 175 0x47, 0x18, 0xB1, 0x6F, 0x97, 0xF1, 0xF0, 0x3C, 0x31, 0x31, 0x1D, 0xD7, |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 ASSERT_TRUE(encrypted_request.ParseFromString( | 319 ASSERT_TRUE(encrypted_request.ParseFromString( |
262 std::string(reinterpret_cast<const char*>(kSerializedEncryptedReport), | 320 std::string(reinterpret_cast<const char*>(kSerializedEncryptedReport), |
263 sizeof(kSerializedEncryptedReport)))); | 321 sizeof(kSerializedEncryptedReport)))); |
264 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( | 322 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( |
265 server_private_key_, encrypted_request, &decrypted_serialized_report)); | 323 server_private_key_, encrypted_request, &decrypted_serialized_report)); |
266 } | 324 } |
267 | 325 |
268 } // namespace | 326 } // namespace |
269 | 327 |
270 } // namespace certificate_reporting | 328 } // namespace certificate_reporting |
OLD | NEW |