| 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" | |
| 12 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 13 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 14 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
| 14 #include "base/test/scoped_feature_list.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/common/chrome_features.h" | 16 #include "chrome/common/chrome_features.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "net/cert/signed_certificate_timestamp_and_status.h" | 18 #include "net/cert/signed_certificate_timestamp_and_status.h" |
| 19 #include "net/test/cert_test_util.h" | 19 #include "net/test/cert_test_util.h" |
| 20 #include "net/test/test_data_directory.h" | 20 #include "net/test/test_data_directory.h" |
| 21 #include "net/test/url_request/url_request_failed_job.h" | 21 #include "net/test/url_request/url_request_failed_job.h" |
| 22 #include "net/url_request/report_sender.h" | 22 #include "net/url_request/report_sender.h" |
| 23 #include "net/url_request/url_request_filter.h" | 23 #include "net/url_request/url_request_filter.h" |
| 24 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 } | 322 } |
| 323 | 323 |
| 324 private: | 324 private: |
| 325 TestExpectCTNetworkDelegate network_delegate_; | 325 TestExpectCTNetworkDelegate network_delegate_; |
| 326 net::TestURLRequestContext context_; | 326 net::TestURLRequestContext context_; |
| 327 content::TestBrowserThreadBundle thread_bundle_; | 327 content::TestBrowserThreadBundle thread_bundle_; |
| 328 | 328 |
| 329 DISALLOW_COPY_AND_ASSIGN(ChromeExpectCTReporterWaitTest); | 329 DISALLOW_COPY_AND_ASSIGN(ChromeExpectCTReporterWaitTest); |
| 330 }; | 330 }; |
| 331 | 331 |
| 332 void EnableFeature() { | |
| 333 base::FeatureList::ClearInstanceForTesting(); | |
| 334 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
| 335 feature_list->InitializeFromCommandLine(features::kExpectCTReporting.name, | |
| 336 ""); | |
| 337 base::FeatureList::SetInstance(std::move(feature_list)); | |
| 338 } | |
| 339 | |
| 340 } // namespace | 332 } // namespace |
| 341 | 333 |
| 342 // Test that no report is sent when the feature is not enabled. | 334 // Test that no report is sent when the feature is not enabled. |
| 343 TEST(ChromeExpectCTReporterTest, FeatureDisabled) { | 335 TEST(ChromeExpectCTReporterTest, FeatureDisabled) { |
| 344 base::HistogramTester histograms; | 336 base::HistogramTester histograms; |
| 345 histograms.ExpectTotalCount(kSendHistogramName, 0); | 337 histograms.ExpectTotalCount(kSendHistogramName, 0); |
| 346 | 338 |
| 347 TestCertificateReportSender* sender = new TestCertificateReportSender(); | 339 TestCertificateReportSender* sender = new TestCertificateReportSender(); |
| 348 net::TestURLRequestContext context; | 340 net::TestURLRequestContext context; |
| 349 ChromeExpectCTReporter reporter(&context); | 341 ChromeExpectCTReporter reporter(&context); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 365 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 357 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 366 | 358 |
| 367 histograms.ExpectTotalCount(kSendHistogramName, 0); | 359 histograms.ExpectTotalCount(kSendHistogramName, 0); |
| 368 } | 360 } |
| 369 | 361 |
| 370 // Test that no report is sent if the report URI is empty. | 362 // Test that no report is sent if the report URI is empty. |
| 371 TEST(ChromeExpectCTReporterTest, EmptyReportURI) { | 363 TEST(ChromeExpectCTReporterTest, EmptyReportURI) { |
| 372 base::HistogramTester histograms; | 364 base::HistogramTester histograms; |
| 373 histograms.ExpectTotalCount(kSendHistogramName, 0); | 365 histograms.ExpectTotalCount(kSendHistogramName, 0); |
| 374 | 366 |
| 375 EnableFeature(); | 367 base::test::ScopedFeatureList scoped_feature_list; |
| 368 scoped_feature_list.InitAndEnableFeature(features::kExpectCTReporting); |
| 369 |
| 376 TestCertificateReportSender* sender = new TestCertificateReportSender(); | 370 TestCertificateReportSender* sender = new TestCertificateReportSender(); |
| 377 net::TestURLRequestContext context; | 371 net::TestURLRequestContext context; |
| 378 ChromeExpectCTReporter reporter(&context); | 372 ChromeExpectCTReporter reporter(&context); |
| 379 reporter.report_sender_.reset(sender); | 373 reporter.report_sender_.reset(sender); |
| 380 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 374 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 381 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 375 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 382 | 376 |
| 383 reporter.OnExpectCTFailed(net::HostPortPair("example.test", 443), GURL(), | 377 reporter.OnExpectCTFailed(net::HostPortPair("example.test", 443), GURL(), |
| 384 net::SSLInfo()); | 378 net::SSLInfo()); |
| 385 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 379 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 386 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 380 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 387 | 381 |
| 388 histograms.ExpectTotalCount(kSendHistogramName, 0); | 382 histograms.ExpectTotalCount(kSendHistogramName, 0); |
| 389 } | 383 } |
| 390 | 384 |
| 391 // Test that if a report fails to send, the UMA metric is recorded. | 385 // Test that if a report fails to send, the UMA metric is recorded. |
| 392 TEST_F(ChromeExpectCTReporterWaitTest, SendReportFailure) { | 386 TEST_F(ChromeExpectCTReporterWaitTest, SendReportFailure) { |
| 393 EnableFeature(); | 387 base::test::ScopedFeatureList scoped_feature_list; |
| 388 scoped_feature_list.InitAndEnableFeature(features::kExpectCTReporting); |
| 389 |
| 394 base::HistogramTester histograms; | 390 base::HistogramTester histograms; |
| 395 histograms.ExpectTotalCount(kFailureHistogramName, 0); | 391 histograms.ExpectTotalCount(kFailureHistogramName, 0); |
| 396 histograms.ExpectTotalCount(kSendHistogramName, 0); | 392 histograms.ExpectTotalCount(kSendHistogramName, 0); |
| 397 | 393 |
| 398 ChromeExpectCTReporter reporter(context()); | 394 ChromeExpectCTReporter reporter(context()); |
| 399 | 395 |
| 400 net::SSLInfo ssl_info; | 396 net::SSLInfo ssl_info; |
| 401 ssl_info.cert = | 397 ssl_info.cert = |
| 402 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); | 398 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); |
| 403 ssl_info.unverified_cert = net::ImportCertFromFile( | 399 ssl_info.unverified_cert = net::ImportCertFromFile( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 415 histograms.ExpectTotalCount(kSendHistogramName, 1); | 411 histograms.ExpectTotalCount(kSendHistogramName, 1); |
| 416 histograms.ExpectBucketCount(kSendHistogramName, true, 1); | 412 histograms.ExpectBucketCount(kSendHistogramName, true, 1); |
| 417 } | 413 } |
| 418 | 414 |
| 419 // Test that a sent report has the right format. | 415 // Test that a sent report has the right format. |
| 420 TEST(ChromeExpectCTReporterTest, SendReport) { | 416 TEST(ChromeExpectCTReporterTest, SendReport) { |
| 421 base::HistogramTester histograms; | 417 base::HistogramTester histograms; |
| 422 histograms.ExpectTotalCount(kFailureHistogramName, 0); | 418 histograms.ExpectTotalCount(kFailureHistogramName, 0); |
| 423 histograms.ExpectTotalCount(kSendHistogramName, 0); | 419 histograms.ExpectTotalCount(kSendHistogramName, 0); |
| 424 | 420 |
| 425 EnableFeature(); | 421 base::test::ScopedFeatureList scoped_feature_list; |
| 422 scoped_feature_list.InitAndEnableFeature(features::kExpectCTReporting); |
| 423 |
| 426 TestCertificateReportSender* sender = new TestCertificateReportSender(); | 424 TestCertificateReportSender* sender = new TestCertificateReportSender(); |
| 427 net::TestURLRequestContext context; | 425 net::TestURLRequestContext context; |
| 428 ChromeExpectCTReporter reporter(&context); | 426 ChromeExpectCTReporter reporter(&context); |
| 429 reporter.report_sender_.reset(sender); | 427 reporter.report_sender_.reset(sender); |
| 430 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 428 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 431 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 429 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 432 | 430 |
| 433 net::SSLInfo ssl_info; | 431 net::SSLInfo ssl_info; |
| 434 ssl_info.cert = | 432 ssl_info.cert = |
| 435 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); | 433 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 EXPECT_EQ(report_uri, sender->latest_report_uri()); | 482 EXPECT_EQ(report_uri, sender->latest_report_uri()); |
| 485 EXPECT_FALSE(sender->latest_serialized_report().empty()); | 483 EXPECT_FALSE(sender->latest_serialized_report().empty()); |
| 486 EXPECT_EQ("application/json; charset=utf-8", sender->latest_content_type()); | 484 EXPECT_EQ("application/json; charset=utf-8", sender->latest_content_type()); |
| 487 ASSERT_NO_FATAL_FAILURE(CheckExpectCTReport( | 485 ASSERT_NO_FATAL_FAILURE(CheckExpectCTReport( |
| 488 sender->latest_serialized_report(), host_port, ssl_info)); | 486 sender->latest_serialized_report(), host_port, ssl_info)); |
| 489 | 487 |
| 490 histograms.ExpectTotalCount(kFailureHistogramName, 0); | 488 histograms.ExpectTotalCount(kFailureHistogramName, 0); |
| 491 histograms.ExpectTotalCount(kSendHistogramName, 1); | 489 histograms.ExpectTotalCount(kSendHistogramName, 1); |
| 492 histograms.ExpectBucketCount(kSendHistogramName, true, 1); | 490 histograms.ExpectBucketCount(kSendHistogramName, true, 1); |
| 493 } | 491 } |
| OLD | NEW |