| 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" |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 base::Closure url_request_destroyed_callback_; | 288 base::Closure url_request_destroyed_callback_; |
| 289 | 289 |
| 290 DISALLOW_COPY_AND_ASSIGN(TestExpectCTNetworkDelegate); | 290 DISALLOW_COPY_AND_ASSIGN(TestExpectCTNetworkDelegate); |
| 291 }; | 291 }; |
| 292 | 292 |
| 293 // A test fixture that allows tests to send a report and wait until the | 293 // A test fixture that allows tests to send a report and wait until the |
| 294 // net::URLRequest that sent the report is destroyed. | 294 // net::URLRequest that sent the report is destroyed. |
| 295 class ChromeExpectCTReporterWaitTest : public ::testing::Test { | 295 class ChromeExpectCTReporterWaitTest : public ::testing::Test { |
| 296 public: | 296 public: |
| 297 ChromeExpectCTReporterWaitTest() | 297 ChromeExpectCTReporterWaitTest() |
| 298 : context_(true), | 298 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 299 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 299 context_(true) { |
| 300 context_.set_network_delegate(&network_delegate_); | 300 context_.set_network_delegate(&network_delegate_); |
| 301 context_.Init(); | 301 context_.Init(); |
| 302 } | 302 } |
| 303 | 303 |
| 304 void SetUp() override { net::URLRequestFailedJob::AddUrlHandler(); } | 304 void SetUp() override { net::URLRequestFailedJob::AddUrlHandler(); } |
| 305 | 305 |
| 306 void TearDown() override { | 306 void TearDown() override { |
| 307 net::URLRequestFilter::GetInstance()->ClearHandlers(); | 307 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
| 308 } | 308 } |
| 309 | 309 |
| 310 net::TestURLRequestContext* context() { return &context_; } | 310 net::TestURLRequestContext* context() { return &context_; } |
| 311 | 311 |
| 312 protected: | 312 protected: |
| 313 void SendReport(ChromeExpectCTReporter* reporter, | 313 void SendReport(ChromeExpectCTReporter* reporter, |
| 314 const net::HostPortPair& host_port, | 314 const net::HostPortPair& host_port, |
| 315 const GURL& report_uri, | 315 const GURL& report_uri, |
| 316 const net::SSLInfo& ssl_info) { | 316 const net::SSLInfo& ssl_info) { |
| 317 base::RunLoop run_loop; | 317 base::RunLoop run_loop; |
| 318 network_delegate_.set_url_request_destroyed_callback( | 318 network_delegate_.set_url_request_destroyed_callback( |
| 319 run_loop.QuitClosure()); | 319 run_loop.QuitClosure()); |
| 320 reporter->OnExpectCTFailed(host_port, report_uri, ssl_info); | 320 reporter->OnExpectCTFailed(host_port, report_uri, ssl_info); |
| 321 run_loop.Run(); | 321 run_loop.Run(); |
| 322 } | 322 } |
| 323 | 323 |
| 324 private: | 324 private: |
| 325 content::TestBrowserThreadBundle thread_bundle_; |
| 325 TestExpectCTNetworkDelegate network_delegate_; | 326 TestExpectCTNetworkDelegate network_delegate_; |
| 326 net::TestURLRequestContext context_; | 327 net::TestURLRequestContext context_; |
| 327 content::TestBrowserThreadBundle thread_bundle_; | |
| 328 | 328 |
| 329 DISALLOW_COPY_AND_ASSIGN(ChromeExpectCTReporterWaitTest); | 329 DISALLOW_COPY_AND_ASSIGN(ChromeExpectCTReporterWaitTest); |
| 330 }; | 330 }; |
| 331 | 331 |
| 332 } // namespace | 332 } // namespace |
| 333 | 333 |
| 334 // 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. |
| 335 TEST(ChromeExpectCTReporterTest, FeatureDisabled) { | 335 TEST(ChromeExpectCTReporterTest, FeatureDisabled) { |
| 336 base::HistogramTester histograms; | 336 base::HistogramTester histograms; |
| 337 histograms.ExpectTotalCount(kSendHistogramName, 0); | 337 histograms.ExpectTotalCount(kSendHistogramName, 0); |
| 338 | 338 |
| 339 TestCertificateReportSender* sender = new TestCertificateReportSender(); | 339 TestCertificateReportSender* sender = new TestCertificateReportSender(); |
| 340 content::TestBrowserThreadBundle thread_bundle( |
| 341 content::TestBrowserThreadBundle::IO_MAINLOOP); |
| 340 net::TestURLRequestContext context; | 342 net::TestURLRequestContext context; |
| 341 ChromeExpectCTReporter reporter(&context); | 343 ChromeExpectCTReporter reporter(&context); |
| 342 reporter.report_sender_.reset(sender); | 344 reporter.report_sender_.reset(sender); |
| 343 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 345 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 344 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 346 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 345 | 347 |
| 346 net::SSLInfo ssl_info; | 348 net::SSLInfo ssl_info; |
| 347 ssl_info.cert = | 349 ssl_info.cert = |
| 348 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); | 350 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); |
| 349 ssl_info.unverified_cert = net::ImportCertFromFile( | 351 ssl_info.unverified_cert = net::ImportCertFromFile( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 361 | 363 |
| 362 // Test that no report is sent if the report URI is empty. | 364 // Test that no report is sent if the report URI is empty. |
| 363 TEST(ChromeExpectCTReporterTest, EmptyReportURI) { | 365 TEST(ChromeExpectCTReporterTest, EmptyReportURI) { |
| 364 base::HistogramTester histograms; | 366 base::HistogramTester histograms; |
| 365 histograms.ExpectTotalCount(kSendHistogramName, 0); | 367 histograms.ExpectTotalCount(kSendHistogramName, 0); |
| 366 | 368 |
| 367 base::test::ScopedFeatureList scoped_feature_list; | 369 base::test::ScopedFeatureList scoped_feature_list; |
| 368 scoped_feature_list.InitAndEnableFeature(features::kExpectCTReporting); | 370 scoped_feature_list.InitAndEnableFeature(features::kExpectCTReporting); |
| 369 | 371 |
| 370 TestCertificateReportSender* sender = new TestCertificateReportSender(); | 372 TestCertificateReportSender* sender = new TestCertificateReportSender(); |
| 373 content::TestBrowserThreadBundle thread_bundle( |
| 374 content::TestBrowserThreadBundle::IO_MAINLOOP); |
| 371 net::TestURLRequestContext context; | 375 net::TestURLRequestContext context; |
| 372 ChromeExpectCTReporter reporter(&context); | 376 ChromeExpectCTReporter reporter(&context); |
| 373 reporter.report_sender_.reset(sender); | 377 reporter.report_sender_.reset(sender); |
| 374 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 378 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 375 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 379 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 376 | 380 |
| 377 reporter.OnExpectCTFailed(net::HostPortPair("example.test", 443), GURL(), | 381 reporter.OnExpectCTFailed(net::HostPortPair("example.test", 443), GURL(), |
| 378 net::SSLInfo()); | 382 net::SSLInfo()); |
| 379 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 383 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 380 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 384 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 // Test that a sent report has the right format. | 419 // Test that a sent report has the right format. |
| 416 TEST(ChromeExpectCTReporterTest, SendReport) { | 420 TEST(ChromeExpectCTReporterTest, SendReport) { |
| 417 base::HistogramTester histograms; | 421 base::HistogramTester histograms; |
| 418 histograms.ExpectTotalCount(kFailureHistogramName, 0); | 422 histograms.ExpectTotalCount(kFailureHistogramName, 0); |
| 419 histograms.ExpectTotalCount(kSendHistogramName, 0); | 423 histograms.ExpectTotalCount(kSendHistogramName, 0); |
| 420 | 424 |
| 421 base::test::ScopedFeatureList scoped_feature_list; | 425 base::test::ScopedFeatureList scoped_feature_list; |
| 422 scoped_feature_list.InitAndEnableFeature(features::kExpectCTReporting); | 426 scoped_feature_list.InitAndEnableFeature(features::kExpectCTReporting); |
| 423 | 427 |
| 424 TestCertificateReportSender* sender = new TestCertificateReportSender(); | 428 TestCertificateReportSender* sender = new TestCertificateReportSender(); |
| 429 content::TestBrowserThreadBundle thread_bundle( |
| 430 content::TestBrowserThreadBundle::IO_MAINLOOP); |
| 425 net::TestURLRequestContext context; | 431 net::TestURLRequestContext context; |
| 426 ChromeExpectCTReporter reporter(&context); | 432 ChromeExpectCTReporter reporter(&context); |
| 427 reporter.report_sender_.reset(sender); | 433 reporter.report_sender_.reset(sender); |
| 428 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 434 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 429 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 435 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 430 | 436 |
| 431 net::SSLInfo ssl_info; | 437 net::SSLInfo ssl_info; |
| 432 ssl_info.cert = | 438 ssl_info.cert = |
| 433 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); | 439 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); |
| 434 ssl_info.unverified_cert = net::ImportCertFromFile( | 440 ssl_info.unverified_cert = net::ImportCertFromFile( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 EXPECT_EQ(report_uri, sender->latest_report_uri()); | 488 EXPECT_EQ(report_uri, sender->latest_report_uri()); |
| 483 EXPECT_FALSE(sender->latest_serialized_report().empty()); | 489 EXPECT_FALSE(sender->latest_serialized_report().empty()); |
| 484 EXPECT_EQ("application/json; charset=utf-8", sender->latest_content_type()); | 490 EXPECT_EQ("application/json; charset=utf-8", sender->latest_content_type()); |
| 485 ASSERT_NO_FATAL_FAILURE(CheckExpectCTReport( | 491 ASSERT_NO_FATAL_FAILURE(CheckExpectCTReport( |
| 486 sender->latest_serialized_report(), host_port, ssl_info)); | 492 sender->latest_serialized_report(), host_port, ssl_info)); |
| 487 | 493 |
| 488 histograms.ExpectTotalCount(kFailureHistogramName, 0); | 494 histograms.ExpectTotalCount(kFailureHistogramName, 0); |
| 489 histograms.ExpectTotalCount(kSendHistogramName, 1); | 495 histograms.ExpectTotalCount(kSendHistogramName, 1); |
| 490 histograms.ExpectBucketCount(kSendHistogramName, true, 1); | 496 histograms.ExpectBucketCount(kSendHistogramName, true, 1); |
| 491 } | 497 } |
| OLD | NEW |