| 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 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 298 : context_(true), |
| 299 context_(true) { | 299 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 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_; | |
| 326 TestExpectCTNetworkDelegate network_delegate_; | 325 TestExpectCTNetworkDelegate network_delegate_; |
| 327 net::TestURLRequestContext context_; | 326 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); | |
| 342 net::TestURLRequestContext context; | 340 net::TestURLRequestContext context; |
| 343 ChromeExpectCTReporter reporter(&context); | 341 ChromeExpectCTReporter reporter(&context); |
| 344 reporter.report_sender_.reset(sender); | 342 reporter.report_sender_.reset(sender); |
| 345 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 343 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 346 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 344 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 347 | 345 |
| 348 net::SSLInfo ssl_info; | 346 net::SSLInfo ssl_info; |
| 349 ssl_info.cert = | 347 ssl_info.cert = |
| 350 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); | 348 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); |
| 351 ssl_info.unverified_cert = net::ImportCertFromFile( | 349 ssl_info.unverified_cert = net::ImportCertFromFile( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 363 | 361 |
| 364 // 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. |
| 365 TEST(ChromeExpectCTReporterTest, EmptyReportURI) { | 363 TEST(ChromeExpectCTReporterTest, EmptyReportURI) { |
| 366 base::HistogramTester histograms; | 364 base::HistogramTester histograms; |
| 367 histograms.ExpectTotalCount(kSendHistogramName, 0); | 365 histograms.ExpectTotalCount(kSendHistogramName, 0); |
| 368 | 366 |
| 369 base::test::ScopedFeatureList scoped_feature_list; | 367 base::test::ScopedFeatureList scoped_feature_list; |
| 370 scoped_feature_list.InitAndEnableFeature(features::kExpectCTReporting); | 368 scoped_feature_list.InitAndEnableFeature(features::kExpectCTReporting); |
| 371 | 369 |
| 372 TestCertificateReportSender* sender = new TestCertificateReportSender(); | 370 TestCertificateReportSender* sender = new TestCertificateReportSender(); |
| 373 content::TestBrowserThreadBundle thread_bundle( | |
| 374 content::TestBrowserThreadBundle::IO_MAINLOOP); | |
| 375 net::TestURLRequestContext context; | 371 net::TestURLRequestContext context; |
| 376 ChromeExpectCTReporter reporter(&context); | 372 ChromeExpectCTReporter reporter(&context); |
| 377 reporter.report_sender_.reset(sender); | 373 reporter.report_sender_.reset(sender); |
| 378 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 374 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 379 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 375 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 380 | 376 |
| 381 reporter.OnExpectCTFailed(net::HostPortPair("example.test", 443), GURL(), | 377 reporter.OnExpectCTFailed(net::HostPortPair("example.test", 443), GURL(), |
| 382 net::SSLInfo()); | 378 net::SSLInfo()); |
| 383 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 379 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 384 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 380 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 base::test::ScopedFeatureList scoped_feature_list; | 421 base::test::ScopedFeatureList scoped_feature_list; |
| 426 scoped_feature_list.InitAndEnableFeature(features::kExpectCTReporting); | 422 scoped_feature_list.InitAndEnableFeature(features::kExpectCTReporting); |
| 427 | 423 |
| 428 TestCertificateReportSender* sender = new TestCertificateReportSender(); | 424 TestCertificateReportSender* sender = new TestCertificateReportSender(); |
| 429 content::TestBrowserThreadBundle thread_bundle( | |
| 430 content::TestBrowserThreadBundle::IO_MAINLOOP); | |
| 431 net::TestURLRequestContext context; | 425 net::TestURLRequestContext context; |
| 432 ChromeExpectCTReporter reporter(&context); | 426 ChromeExpectCTReporter reporter(&context); |
| 433 reporter.report_sender_.reset(sender); | 427 reporter.report_sender_.reset(sender); |
| 434 EXPECT_TRUE(sender->latest_report_uri().is_empty()); | 428 EXPECT_TRUE(sender->latest_report_uri().is_empty()); |
| 435 EXPECT_TRUE(sender->latest_serialized_report().empty()); | 429 EXPECT_TRUE(sender->latest_serialized_report().empty()); |
| 436 | 430 |
| 437 net::SSLInfo ssl_info; | 431 net::SSLInfo ssl_info; |
| 438 ssl_info.cert = | 432 ssl_info.cert = |
| 439 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); | 433 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); |
| 440 ssl_info.unverified_cert = net::ImportCertFromFile( | 434 ssl_info.unverified_cert = net::ImportCertFromFile( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 EXPECT_EQ(report_uri, sender->latest_report_uri()); | 482 EXPECT_EQ(report_uri, sender->latest_report_uri()); |
| 489 EXPECT_FALSE(sender->latest_serialized_report().empty()); | 483 EXPECT_FALSE(sender->latest_serialized_report().empty()); |
| 490 EXPECT_EQ("application/json; charset=utf-8", sender->latest_content_type()); | 484 EXPECT_EQ("application/json; charset=utf-8", sender->latest_content_type()); |
| 491 ASSERT_NO_FATAL_FAILURE(CheckExpectCTReport( | 485 ASSERT_NO_FATAL_FAILURE(CheckExpectCTReport( |
| 492 sender->latest_serialized_report(), host_port, ssl_info)); | 486 sender->latest_serialized_report(), host_port, ssl_info)); |
| 493 | 487 |
| 494 histograms.ExpectTotalCount(kFailureHistogramName, 0); | 488 histograms.ExpectTotalCount(kFailureHistogramName, 0); |
| 495 histograms.ExpectTotalCount(kSendHistogramName, 1); | 489 histograms.ExpectTotalCount(kSendHistogramName, 1); |
| 496 histograms.ExpectBucketCount(kSendHistogramName, true, 1); | 490 histograms.ExpectBucketCount(kSendHistogramName, true, 1); |
| 497 } | 491 } |
| OLD | NEW |