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 "components/certificate_reporting/encrypted_cert_logger.pb.h" | 19 #include "components/certificate_reporting/encrypted_cert_logger.pb.h" |
| 20 #include "net/http/http_status_code.h" |
20 #include "net/test/url_request/url_request_failed_job.h" | 21 #include "net/test/url_request/url_request_failed_job.h" |
21 #include "net/test/url_request/url_request_mock_data_job.h" | 22 #include "net/test/url_request/url_request_mock_data_job.h" |
22 #include "net/url_request/report_sender.h" | 23 #include "net/url_request/report_sender.h" |
23 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 #include "third_party/boringssl/src/include/openssl/curve25519.h" | 26 #include "third_party/boringssl/src/include/openssl/curve25519.h" |
26 | 27 |
27 namespace certificate_reporting { | 28 namespace certificate_reporting { |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 const char kDummyHttpReportUri[] = "http://example.test"; | 32 const char kDummyHttpReportUri[] = "http://example.test"; |
32 const char kDummyHttpsReportUri[] = "https://example.test"; | 33 const char kDummyHttpsReportUri[] = "https://example.test"; |
33 const char kDummyReport[] = "a dummy report"; | 34 const char kDummyReport[] = "a dummy report"; |
34 const uint32_t kServerPublicKeyTestVersion = 16; | 35 const uint32_t kServerPublicKeyTestVersion = 16; |
35 | 36 |
36 void ErrorCallback(bool* called, const GURL& report_uri, int net_error) { | 37 void ErrorCallback(bool* called, |
| 38 const GURL& report_uri, |
| 39 int net_error, |
| 40 int http_response_code) { |
37 EXPECT_NE(net::OK, net_error); | 41 EXPECT_NE(net::OK, net_error); |
| 42 EXPECT_EQ(-1, http_response_code); |
38 *called = true; | 43 *called = true; |
39 } | 44 } |
40 | 45 |
41 void SuccessCallback(bool* called) { | 46 void SuccessCallback(bool* called) { |
42 *called = true; | 47 *called = true; |
43 } | 48 } |
44 | 49 |
45 // A mock ReportSender that keeps track of the last report | 50 // A mock ReportSender that keeps track of the last report |
46 // sent. | 51 // sent. |
47 class MockCertificateReportSender : public net::ReportSender { | 52 class MockCertificateReportSender : public net::ReportSender { |
48 public: | 53 public: |
49 MockCertificateReportSender() | 54 MockCertificateReportSender() |
50 : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} | 55 : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} |
51 ~MockCertificateReportSender() override {} | 56 ~MockCertificateReportSender() override {} |
52 | 57 |
53 void Send( | 58 void Send(const GURL& report_uri, |
54 const GURL& report_uri, | 59 base::StringPiece content_type, |
55 base::StringPiece content_type, | 60 base::StringPiece report, |
56 base::StringPiece report, | 61 const base::Callback<void()>& success_callback, |
57 const base::Callback<void()>& success_callback, | 62 const base::Callback<void(const GURL&, int, int)>& error_callback) |
58 const base::Callback<void(const GURL&, int)>& error_callback) override { | 63 override { |
59 latest_report_uri_ = report_uri; | 64 latest_report_uri_ = report_uri; |
60 report.CopyToString(&latest_report_); | 65 report.CopyToString(&latest_report_); |
61 content_type.CopyToString(&latest_content_type_); | 66 content_type.CopyToString(&latest_content_type_); |
62 } | 67 } |
63 | 68 |
64 const GURL& latest_report_uri() const { return latest_report_uri_; } | 69 const GURL& latest_report_uri() const { return latest_report_uri_; } |
65 | 70 |
66 const std::string& latest_report() const { return latest_report_; } | 71 const std::string& latest_report() const { return latest_report_; } |
67 | 72 |
68 const std::string& latest_content_type() const { | 73 const std::string& latest_content_type() const { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 // an encrypted or plaintext extended reporting report as appropriate. | 125 // an encrypted or plaintext extended reporting report as appropriate. |
121 TEST_F(ErrorReporterTest, ExtendedReportingSendReport) { | 126 TEST_F(ErrorReporterTest, ExtendedReportingSendReport) { |
122 // Data should not be encrypted when sent to an HTTPS URL. | 127 // Data should not be encrypted when sent to an HTTPS URL. |
123 MockCertificateReportSender* mock_report_sender = | 128 MockCertificateReportSender* mock_report_sender = |
124 new MockCertificateReportSender(); | 129 new MockCertificateReportSender(); |
125 GURL https_url(kDummyHttpsReportUri); | 130 GURL https_url(kDummyHttpsReportUri); |
126 ErrorReporter https_reporter(https_url, server_public_key_, | 131 ErrorReporter https_reporter(https_url, server_public_key_, |
127 kServerPublicKeyTestVersion, | 132 kServerPublicKeyTestVersion, |
128 base::WrapUnique(mock_report_sender)); | 133 base::WrapUnique(mock_report_sender)); |
129 https_reporter.SendExtendedReportingReport( | 134 https_reporter.SendExtendedReportingReport( |
130 kDummyReport, base::Closure(), base::Callback<void(const GURL&, int)>()); | 135 kDummyReport, base::Callback<void()>(), |
| 136 base::Callback<void(const GURL&, int, int)>()); |
131 EXPECT_EQ(mock_report_sender->latest_report_uri(), https_url); | 137 EXPECT_EQ(mock_report_sender->latest_report_uri(), https_url); |
132 EXPECT_EQ(mock_report_sender->latest_report(), kDummyReport); | 138 EXPECT_EQ(mock_report_sender->latest_report(), kDummyReport); |
133 | 139 |
134 // Data should be encrypted when sent to an HTTP URL. | 140 // Data should be encrypted when sent to an HTTP URL. |
135 MockCertificateReportSender* http_mock_report_sender = | 141 MockCertificateReportSender* http_mock_report_sender = |
136 new MockCertificateReportSender(); | 142 new MockCertificateReportSender(); |
137 GURL http_url(kDummyHttpReportUri); | 143 const GURL http_url(kDummyHttpReportUri); |
138 ErrorReporter http_reporter(http_url, server_public_key_, | 144 ErrorReporter http_reporter(http_url, server_public_key_, |
139 kServerPublicKeyTestVersion, | 145 kServerPublicKeyTestVersion, |
140 base::WrapUnique(http_mock_report_sender)); | 146 base::WrapUnique(http_mock_report_sender)); |
141 http_reporter.SendExtendedReportingReport( | 147 http_reporter.SendExtendedReportingReport( |
142 kDummyReport, base::Closure(), base::Callback<void(const GURL&, int)>()); | 148 kDummyReport, base::Callback<void()>(), |
| 149 base::Callback<void(const GURL&, int, int)>()); |
143 | 150 |
144 EXPECT_EQ(http_mock_report_sender->latest_report_uri(), http_url); | 151 EXPECT_EQ(http_mock_report_sender->latest_report_uri(), http_url); |
145 EXPECT_EQ("application/octet-stream", | 152 EXPECT_EQ("application/octet-stream", |
146 http_mock_report_sender->latest_content_type()); | 153 http_mock_report_sender->latest_content_type()); |
147 | 154 |
148 std::string uploaded_report; | 155 std::string uploaded_report; |
149 EncryptedCertLoggerRequest encrypted_request; | 156 EncryptedCertLoggerRequest encrypted_request; |
150 ASSERT_TRUE(encrypted_request.ParseFromString( | 157 ASSERT_TRUE(encrypted_request.ParseFromString( |
151 http_mock_report_sender->latest_report())); | 158 http_mock_report_sender->latest_report())); |
152 EXPECT_EQ(kServerPublicKeyTestVersion, | 159 EXPECT_EQ(kServerPublicKeyTestVersion, |
(...skipping 10 matching lines...) Expand all Loading... |
163 TEST_F(ErrorReporterTest, ErroredRequestCallsCallback) { | 170 TEST_F(ErrorReporterTest, ErroredRequestCallsCallback) { |
164 net::URLRequestFailedJob::AddUrlHandler(); | 171 net::URLRequestFailedJob::AddUrlHandler(); |
165 | 172 |
166 base::RunLoop run_loop; | 173 base::RunLoop run_loop; |
167 net::TestURLRequestContext context(true); | 174 net::TestURLRequestContext context(true); |
168 TestCertificateReporterNetworkDelegate test_delegate; | 175 TestCertificateReporterNetworkDelegate test_delegate; |
169 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); | 176 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); |
170 context.set_network_delegate(&test_delegate); | 177 context.set_network_delegate(&test_delegate); |
171 context.Init(); | 178 context.Init(); |
172 | 179 |
173 GURL report_uri( | 180 const GURL report_uri( |
174 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_FAILED)); | 181 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_FAILED)); |
175 ErrorReporter reporter(&context, report_uri, | 182 ErrorReporter reporter(&context, report_uri, |
176 net::ReportSender::DO_NOT_SEND_COOKIES); | 183 net::ReportSender::DO_NOT_SEND_COOKIES); |
177 | 184 |
178 bool error_callback_called = false; | 185 bool error_callback_called = false; |
179 bool success_callback_called = false; | 186 bool success_callback_called = false; |
180 reporter.SendExtendedReportingReport( | 187 reporter.SendExtendedReportingReport( |
181 kDummyReport, base::Bind(&SuccessCallback, &success_callback_called), | 188 kDummyReport, base::Bind(&SuccessCallback, &success_callback_called), |
182 base::Bind(&ErrorCallback, &error_callback_called)); | 189 base::Bind(&ErrorCallback, &error_callback_called)); |
183 run_loop.Run(); | 190 run_loop.Run(); |
184 | 191 |
185 EXPECT_TRUE(error_callback_called); | 192 EXPECT_TRUE(error_callback_called); |
186 EXPECT_FALSE(success_callback_called); | 193 EXPECT_FALSE(success_callback_called); |
187 } | 194 } |
188 | 195 |
189 // Tests that an UMA histogram is recorded if a report fails to send. | 196 // Tests that an UMA histogram is recorded if a report is successfully sent. |
190 TEST_F(ErrorReporterTest, SuccessfulRequestCallsCallback) { | 197 TEST_F(ErrorReporterTest, SuccessfulRequestCallsCallback) { |
191 net::URLRequestMockDataJob::AddUrlHandler(); | 198 net::URLRequestMockDataJob::AddUrlHandler(); |
192 | 199 |
193 base::RunLoop run_loop; | 200 base::RunLoop run_loop; |
194 net::TestURLRequestContext context(true); | 201 net::TestURLRequestContext context(true); |
195 TestCertificateReporterNetworkDelegate test_delegate; | 202 TestCertificateReporterNetworkDelegate test_delegate; |
196 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); | 203 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); |
197 context.set_network_delegate(&test_delegate); | 204 context.set_network_delegate(&test_delegate); |
198 context.Init(); | 205 context.Init(); |
199 | 206 |
200 GURL report_uri(net::URLRequestMockDataJob::GetMockHttpUrl("some data", 1)); | 207 const GURL report_uri( |
| 208 net::URLRequestMockDataJob::GetMockHttpUrl("some data", 1)); |
201 ErrorReporter reporter(&context, report_uri, | 209 ErrorReporter reporter(&context, report_uri, |
202 net::ReportSender::DO_NOT_SEND_COOKIES); | 210 net::ReportSender::DO_NOT_SEND_COOKIES); |
203 | 211 |
204 bool error_callback_called = false; | 212 bool error_callback_called = false; |
205 bool success_callback_called = false; | 213 bool success_callback_called = false; |
206 reporter.SendExtendedReportingReport( | 214 reporter.SendExtendedReportingReport( |
207 kDummyReport, base::Bind(&SuccessCallback, &success_callback_called), | 215 kDummyReport, base::Bind(&SuccessCallback, &success_callback_called), |
208 base::Bind(&ErrorCallback, &error_callback_called)); | 216 base::Bind(&ErrorCallback, &error_callback_called)); |
209 run_loop.Run(); | 217 run_loop.Run(); |
210 | 218 |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 ASSERT_TRUE(encrypted_request.ParseFromString( | 376 ASSERT_TRUE(encrypted_request.ParseFromString( |
369 std::string(reinterpret_cast<const char*>(kSerializedEncryptedReport), | 377 std::string(reinterpret_cast<const char*>(kSerializedEncryptedReport), |
370 sizeof(kSerializedEncryptedReport)))); | 378 sizeof(kSerializedEncryptedReport)))); |
371 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( | 379 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( |
372 server_private_key_, encrypted_request, &decrypted_serialized_report)); | 380 server_private_key_, encrypted_request, &decrypted_serialized_report)); |
373 } | 381 } |
374 | 382 |
375 } // namespace | 383 } // namespace |
376 | 384 |
377 } // namespace certificate_reporting | 385 } // namespace certificate_reporting |
OLD | NEW |