| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/ssl/chrome_expect_ct_reporter.h" | 5 #include "chrome/browser/ssl/chrome_expect_ct_reporter.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/feature_list.h" |
| 11 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_features.h" |
| 14 #include "net/base/test_data_directory.h" | 15 #include "net/base/test_data_directory.h" |
| 15 #include "net/ssl/signed_certificate_timestamp_and_status.h" | 16 #include "net/ssl/signed_certificate_timestamp_and_status.h" |
| 16 #include "net/test/cert_test_util.h" | 17 #include "net/test/cert_test_util.h" |
| 17 #include "net/url_request/certificate_report_sender.h" | 18 #include "net/url_request/certificate_report_sender.h" |
| 18 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 const base::ListValue* report_invalid_scts = nullptr; | 243 const base::ListValue* report_invalid_scts = nullptr; |
| 243 ASSERT_TRUE(report_dict->GetList("invalid-scts", &report_invalid_scts)); | 244 ASSERT_TRUE(report_dict->GetList("invalid-scts", &report_invalid_scts)); |
| 244 const base::ListValue* report_valid_scts = nullptr; | 245 const base::ListValue* report_valid_scts = nullptr; |
| 245 ASSERT_TRUE(report_dict->GetList("valid-scts", &report_valid_scts)); | 246 ASSERT_TRUE(report_dict->GetList("valid-scts", &report_valid_scts)); |
| 246 | 247 |
| 247 ASSERT_NO_FATAL_FAILURE(CheckReportSCTs( | 248 ASSERT_NO_FATAL_FAILURE(CheckReportSCTs( |
| 248 ssl_info.signed_certificate_timestamps, *report_unknown_scts, | 249 ssl_info.signed_certificate_timestamps, *report_unknown_scts, |
| 249 *report_invalid_scts, *report_valid_scts)); | 250 *report_invalid_scts, *report_valid_scts)); |
| 250 } | 251 } |
| 251 | 252 |
| 253 void EnableFeature() { |
| 254 base::FeatureList::ClearInstanceForTesting(); |
| 255 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 256 feature_list->InitializeFromCommandLine(features::kExpectCTReporting.name, |
| 257 ""); |
| 258 base::FeatureList::SetInstance(std::move(feature_list)); |
| 259 } |
| 260 |
| 252 } // namespace | 261 } // namespace |
| 253 | 262 |
| 254 // Test that no report is sent when the command line switch is not | 263 // Test that no report is sent when the feature is not enabled. |
| 255 // enabled. | 264 TEST(ChromeExpectCTReporterTest, FeatureDisabled) { |
| 256 TEST(ChromeExpectCTReporterTest, NoCommandLineSwitch) { | |
| 257 TestCertificateReportSender* sender = new TestCertificateReportSender(); | 265 TestCertificateReportSender* sender = new TestCertificateReportSender(); |
| 258 net::TestURLRequestContext context; | 266 net::TestURLRequestContext context; |
| 259 ChromeExpectCTReporter reporter(&context); | 267 ChromeExpectCTReporter reporter(&context); |
| 260 reporter.report_sender_.reset(sender); | 268 reporter.report_sender_.reset(sender); |
| 261 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 269 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 262 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 270 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 263 | 271 |
| 264 net::SSLInfo ssl_info; | 272 net::SSLInfo ssl_info; |
| 265 ssl_info.cert = | 273 ssl_info.cert = |
| 266 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); | 274 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); |
| 267 ssl_info.unverified_cert = net::ImportCertFromFile( | 275 ssl_info.unverified_cert = net::ImportCertFromFile( |
| 268 net::GetTestCertsDirectory(), "localhost_cert.pem"); | 276 net::GetTestCertsDirectory(), "localhost_cert.pem"); |
| 269 | 277 |
| 270 net::HostPortPair host_port("example.test", 443); | 278 net::HostPortPair host_port("example.test", 443); |
| 271 GURL report_uri("http://example-report.test"); | 279 GURL report_uri("http://example-report.test"); |
| 272 | 280 |
| 273 reporter.OnExpectCTFailed(host_port, report_uri, ssl_info); | 281 reporter.OnExpectCTFailed(host_port, report_uri, ssl_info); |
| 274 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 282 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 275 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 283 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 276 } | 284 } |
| 277 | 285 |
| 278 // Test that no report is sent if the report URI is empty. | 286 // Test that no report is sent if the report URI is empty. |
| 279 TEST(ChromeExpectCTReporterTest, EmptyReportURI) { | 287 TEST(ChromeExpectCTReporterTest, EmptyReportURI) { |
| 280 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 288 EnableFeature(); |
| 281 switches::kEnableExpectCTReporting); | |
| 282 TestCertificateReportSender* sender = new TestCertificateReportSender(); | 289 TestCertificateReportSender* sender = new TestCertificateReportSender(); |
| 283 net::TestURLRequestContext context; | 290 net::TestURLRequestContext context; |
| 284 ChromeExpectCTReporter reporter(&context); | 291 ChromeExpectCTReporter reporter(&context); |
| 285 reporter.report_sender_.reset(sender); | 292 reporter.report_sender_.reset(sender); |
| 286 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 293 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 287 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 294 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 288 | 295 |
| 289 reporter.OnExpectCTFailed(net::HostPortPair("example.test", 443), GURL(), | 296 reporter.OnExpectCTFailed(net::HostPortPair("example.test", 443), GURL(), |
| 290 net::SSLInfo()); | 297 net::SSLInfo()); |
| 291 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 298 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 292 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 299 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 293 } | 300 } |
| 294 | 301 |
| 295 // Test that a sent report has the right format. | 302 // Test that a sent report has the right format. |
| 296 TEST(ChromeExpectCTReporterTest, SendReport) { | 303 TEST(ChromeExpectCTReporterTest, SendReport) { |
| 297 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 304 EnableFeature(); |
| 298 switches::kEnableExpectCTReporting); | |
| 299 TestCertificateReportSender* sender = new TestCertificateReportSender(); | 305 TestCertificateReportSender* sender = new TestCertificateReportSender(); |
| 300 net::TestURLRequestContext context; | 306 net::TestURLRequestContext context; |
| 301 ChromeExpectCTReporter reporter(&context); | 307 ChromeExpectCTReporter reporter(&context); |
| 302 reporter.report_sender_.reset(sender); | 308 reporter.report_sender_.reset(sender); |
| 303 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 309 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 304 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 310 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 305 | 311 |
| 306 net::SSLInfo ssl_info; | 312 net::SSLInfo ssl_info; |
| 307 ssl_info.cert = | 313 ssl_info.cert = |
| 308 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); | 314 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 net::HostPortPair host_port("example.test", 443); | 350 net::HostPortPair host_port("example.test", 443); |
| 345 GURL report_uri("http://example-report.test"); | 351 GURL report_uri("http://example-report.test"); |
| 346 | 352 |
| 347 // Check that the report is sent and contains the correct information. | 353 // Check that the report is sent and contains the correct information. |
| 348 reporter.OnExpectCTFailed(host_port, report_uri, ssl_info); | 354 reporter.OnExpectCTFailed(host_port, report_uri, ssl_info); |
| 349 EXPECT_EQ(report_uri, sender->latest_report_uri()); | 355 EXPECT_EQ(report_uri, sender->latest_report_uri()); |
| 350 EXPECT_FALSE(sender->latest_serialized_report().empty()); | 356 EXPECT_FALSE(sender->latest_serialized_report().empty()); |
| 351 ASSERT_NO_FATAL_FAILURE(CheckExpectCTReport( | 357 ASSERT_NO_FATAL_FAILURE(CheckExpectCTReport( |
| 352 sender->latest_serialized_report(), host_port, ssl_info)); | 358 sender->latest_serialized_report(), host_port, ssl_info)); |
| 353 } | 359 } |
| OLD | NEW |