Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: components/certificate_reporting/error_reporter_unittest.cc

Issue 2483993002: Add report_id to ErrorReporter interface. (Closed)
Patch Set: Same as patchset #2 but with the proper diff Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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,
37 int expected_report_id,
38 const GURL& report_uri,
39 int net_error,
40 int report_id) {
41 EXPECT_NE(net::OK, net_error);
42 *called = true;
43 EXPECT_EQ(expected_report_id, report_id);
44 }
45
46 void SuccessCallback(bool* called, int expected_report_id, int report_id) {
47 *called = true;
48 EXPECT_EQ(expected_report_id, report_id);
49 }
36 50
37 // A mock ReportSender that keeps track of the last report 51 // A mock ReportSender that keeps track of the last report
38 // sent. 52 // sent.
39 class MockCertificateReportSender : public net::ReportSender { 53 class MockCertificateReportSender : public net::ReportSender {
40 public: 54 public:
41 MockCertificateReportSender() 55 MockCertificateReportSender()
42 : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} 56 : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {}
43 ~MockCertificateReportSender() override {} 57 ~MockCertificateReportSender() override {}
44 58
45 void Send(const GURL& report_uri, 59 void Send(const GURL& report_uri,
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 // Test that ErrorReporter::SendExtendedReportingReport sends 127 // Test that ErrorReporter::SendExtendedReportingReport sends
114 // an encrypted or plaintext extended reporting report as appropriate. 128 // an encrypted or plaintext extended reporting report as appropriate.
115 TEST_F(ErrorReporterTest, ExtendedReportingSendReport) { 129 TEST_F(ErrorReporterTest, ExtendedReportingSendReport) {
116 // Data should not be encrypted when sent to an HTTPS URL. 130 // Data should not be encrypted when sent to an HTTPS URL.
117 MockCertificateReportSender* mock_report_sender = 131 MockCertificateReportSender* mock_report_sender =
118 new MockCertificateReportSender(); 132 new MockCertificateReportSender();
119 GURL https_url(kDummyHttpsReportUri); 133 GURL https_url(kDummyHttpsReportUri);
120 ErrorReporter https_reporter(https_url, server_public_key_, 134 ErrorReporter https_reporter(https_url, server_public_key_,
121 kServerPublicKeyTestVersion, 135 kServerPublicKeyTestVersion,
122 base::WrapUnique(mock_report_sender)); 136 base::WrapUnique(mock_report_sender));
123 https_reporter.SendExtendedReportingReport(kDummyReport); 137 https_reporter.SendExtendedReportingReport(kDummyReport, 0);
124 EXPECT_EQ(mock_report_sender->latest_report_uri(), https_url); 138 EXPECT_EQ(mock_report_sender->latest_report_uri(), https_url);
125 EXPECT_EQ(mock_report_sender->latest_report(), kDummyReport); 139 EXPECT_EQ(mock_report_sender->latest_report(), kDummyReport);
126 140
127 // Data should be encrypted when sent to an HTTP URL. 141 // Data should be encrypted when sent to an HTTP URL.
128 MockCertificateReportSender* http_mock_report_sender = 142 MockCertificateReportSender* http_mock_report_sender =
129 new MockCertificateReportSender(); 143 new MockCertificateReportSender();
130 GURL http_url(kDummyHttpReportUri); 144 GURL http_url(kDummyHttpReportUri);
131 ErrorReporter http_reporter(http_url, server_public_key_, 145 ErrorReporter http_reporter(http_url, server_public_key_,
132 kServerPublicKeyTestVersion, 146 kServerPublicKeyTestVersion,
133 base::WrapUnique(http_mock_report_sender)); 147 base::WrapUnique(http_mock_report_sender));
134 http_reporter.SendExtendedReportingReport(kDummyReport); 148 http_reporter.SendExtendedReportingReport(kDummyReport, 0);
135 149
136 EXPECT_EQ(http_mock_report_sender->latest_report_uri(), http_url); 150 EXPECT_EQ(http_mock_report_sender->latest_report_uri(), http_url);
137 EXPECT_EQ("application/octet-stream", 151 EXPECT_EQ("application/octet-stream",
138 http_mock_report_sender->latest_content_type()); 152 http_mock_report_sender->latest_content_type());
139 EXPECT_EQ(0, http_mock_report_sender->latest_report_id()); 153 EXPECT_EQ(0, http_mock_report_sender->latest_report_id());
140 154
141 std::string uploaded_report; 155 std::string uploaded_report;
142 EncryptedCertLoggerRequest encrypted_request; 156 EncryptedCertLoggerRequest encrypted_request;
143 ASSERT_TRUE(encrypted_request.ParseFromString( 157 ASSERT_TRUE(encrypted_request.ParseFromString(
144 http_mock_report_sender->latest_report())); 158 http_mock_report_sender->latest_report()));
145 EXPECT_EQ(kServerPublicKeyTestVersion, 159 EXPECT_EQ(kServerPublicKeyTestVersion,
146 encrypted_request.server_public_key_version()); 160 encrypted_request.server_public_key_version());
147 EXPECT_EQ(EncryptedCertLoggerRequest::AEAD_ECDH_AES_128_CTR_HMAC_SHA256, 161 EXPECT_EQ(EncryptedCertLoggerRequest::AEAD_ECDH_AES_128_CTR_HMAC_SHA256,
148 encrypted_request.algorithm()); 162 encrypted_request.algorithm());
149 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( 163 ASSERT_TRUE(ErrorReporter::DecryptErrorReport(
150 server_private_key_, encrypted_request, &uploaded_report)); 164 server_private_key_, encrypted_request, &uploaded_report));
151 165
152 EXPECT_EQ(kDummyReport, uploaded_report); 166 EXPECT_EQ(kDummyReport, uploaded_report);
153 } 167 }
154 168
155 // Tests that an UMA histogram is recorded if a report fails to send. 169 // Tests that an UMA histogram is recorded if a report fails to send.
156 TEST_F(ErrorReporterTest, UMAOnFailure) { 170 TEST_F(ErrorReporterTest, ErroredRequestCallsCallback) {
157 net::URLRequestFailedJob::AddUrlHandler(); 171 net::URLRequestFailedJob::AddUrlHandler();
158 172
159 base::HistogramTester histograms;
160 histograms.ExpectTotalCount(kFailureHistogramName, 0);
161
162 base::RunLoop run_loop; 173 base::RunLoop run_loop;
163 net::TestURLRequestContext context(true); 174 net::TestURLRequestContext context(true);
164 TestCertificateReporterNetworkDelegate test_delegate; 175 TestCertificateReporterNetworkDelegate test_delegate;
165 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure()); 176 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure());
166 context.set_network_delegate(&test_delegate); 177 context.set_network_delegate(&test_delegate);
167 context.Init(); 178 context.Init();
168 179
169 GURL report_uri( 180 GURL report_uri(
170 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_FAILED)); 181 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_FAILED));
171 ErrorReporter reporter(&context, report_uri, 182 ErrorReporter reporter(&context, report_uri,
172 net::ReportSender::DO_NOT_SEND_COOKIES); 183 net::ReportSender::DO_NOT_SEND_COOKIES);
173 reporter.SendExtendedReportingReport(kDummyReport); 184
185 bool error_callback_called = false;
186 bool success_callback_called = false;
187 const int report_id = 42;
188 reporter.SetErrorCallback(
189 base::Bind(&ErrorCallback, &error_callback_called, report_id));
190 reporter.SetSuccessCallback(
191 base::Bind(&SuccessCallback, &success_callback_called, report_id));
192 reporter.SendExtendedReportingReport(kDummyReport, report_id);
174 run_loop.Run(); 193 run_loop.Run();
175 194
176 histograms.ExpectTotalCount(kFailureHistogramName, 1); 195 EXPECT_TRUE(error_callback_called);
177 histograms.ExpectBucketCount(kFailureHistogramName, 196 EXPECT_FALSE(success_callback_called);
178 -net::ERR_CONNECTION_FAILED, 1); 197 }
198
199 // Tests that an UMA histogram is recorded if a report fails to send.
200 TEST_F(ErrorReporterTest, SuccessfulRequestCallsCallback) {
201 net::URLRequestMockDataJob::AddUrlHandler();
202
203 base::RunLoop run_loop;
204 net::TestURLRequestContext context(true);
205 TestCertificateReporterNetworkDelegate test_delegate;
206 test_delegate.set_url_request_destroyed_callback(run_loop.QuitClosure());
207 context.set_network_delegate(&test_delegate);
208 context.Init();
209
210 GURL report_uri(net::URLRequestMockDataJob::GetMockHttpUrl("some data", 1));
211 ErrorReporter reporter(&context, report_uri,
212 net::ReportSender::DO_NOT_SEND_COOKIES);
213
214 bool error_callback_called = false;
215 bool success_callback_called = false;
216 const int report_id = 42;
217 reporter.SetErrorCallback(
218 base::Bind(&ErrorCallback, &error_callback_called, report_id));
219 reporter.SetSuccessCallback(
220 base::Bind(&SuccessCallback, &success_callback_called, report_id));
221 reporter.SendExtendedReportingReport(kDummyReport, report_id);
222 run_loop.Run();
223
224 EXPECT_FALSE(error_callback_called);
225 EXPECT_TRUE(success_callback_called);
179 } 226 }
180 227
181 // This test decrypts a "known gold" report. It's intentionally brittle 228 // This test decrypts a "known gold" report. It's intentionally brittle
182 // in order to catch changes in report encryption that could cause the 229 // in order to catch changes in report encryption that could cause the
183 // server to no longer be able to decrypt reports that it receives from 230 // server to no longer be able to decrypt reports that it receives from
184 // Chrome. 231 // Chrome.
185 TEST_F(ErrorReporterTest, DecryptExampleReport) { 232 TEST_F(ErrorReporterTest, DecryptExampleReport) {
186 // This data should not be changed without also changing the 233 // This data should not be changed without also changing the
187 // corresponding server-side test. 234 // corresponding server-side test.
188 const unsigned char kSerializedEncryptedReport[] = { 235 const unsigned char kSerializedEncryptedReport[] = {
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 ASSERT_TRUE(encrypted_request.ParseFromString( 381 ASSERT_TRUE(encrypted_request.ParseFromString(
335 std::string(reinterpret_cast<const char*>(kSerializedEncryptedReport), 382 std::string(reinterpret_cast<const char*>(kSerializedEncryptedReport),
336 sizeof(kSerializedEncryptedReport)))); 383 sizeof(kSerializedEncryptedReport))));
337 ASSERT_TRUE(ErrorReporter::DecryptErrorReport( 384 ASSERT_TRUE(ErrorReporter::DecryptErrorReport(
338 server_private_key_, encrypted_request, &decrypted_serialized_report)); 385 server_private_key_, encrypted_request, &decrypted_serialized_report));
339 } 386 }
340 387
341 } // namespace 388 } // namespace
342 389
343 } // namespace certificate_reporting 390 } // namespace certificate_reporting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698