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