| 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 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 base::Closure url_request_destroyed_callback_; | 294 base::Closure url_request_destroyed_callback_; |
| 295 | 295 |
| 296 DISALLOW_COPY_AND_ASSIGN(TestExpectCTNetworkDelegate); | 296 DISALLOW_COPY_AND_ASSIGN(TestExpectCTNetworkDelegate); |
| 297 }; | 297 }; |
| 298 | 298 |
| 299 // A test fixture that allows tests to send a report and wait until the | 299 // A test fixture that allows tests to send a report and wait until the |
| 300 // net::URLRequest that sent the report is destroyed. | 300 // net::URLRequest that sent the report is destroyed. |
| 301 class ChromeExpectCTReporterWaitTest : public ::testing::Test { | 301 class ChromeExpectCTReporterWaitTest : public ::testing::Test { |
| 302 public: | 302 public: |
| 303 ChromeExpectCTReporterWaitTest() | 303 ChromeExpectCTReporterWaitTest() |
| 304 : context_(true), | 304 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 305 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 305 |
| 306 context_.set_network_delegate(&network_delegate_); | 306 void SetUp() override { |
| 307 context_.Init(); | 307 // Initializes URLRequestContext after the thread is set up. |
| 308 context_.reset(new net::TestURLRequestContext(true)); |
| 309 context_->set_network_delegate(&network_delegate_); |
| 310 context_->Init(); |
| 311 net::URLRequestFailedJob::AddUrlHandler(); |
| 308 } | 312 } |
| 309 | 313 |
| 310 void SetUp() override { net::URLRequestFailedJob::AddUrlHandler(); } | |
| 311 | |
| 312 void TearDown() override { | 314 void TearDown() override { |
| 313 net::URLRequestFilter::GetInstance()->ClearHandlers(); | 315 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
| 314 } | 316 } |
| 315 | 317 |
| 316 net::TestURLRequestContext* context() { return &context_; } | 318 net::TestURLRequestContext* context() { return context_.get(); } |
| 317 | 319 |
| 318 protected: | 320 protected: |
| 319 void SendReport(ChromeExpectCTReporter* reporter, | 321 void SendReport(ChromeExpectCTReporter* reporter, |
| 320 const net::HostPortPair& host_port, | 322 const net::HostPortPair& host_port, |
| 321 const GURL& report_uri, | 323 const GURL& report_uri, |
| 322 const net::SSLInfo& ssl_info) { | 324 const net::SSLInfo& ssl_info) { |
| 323 base::RunLoop run_loop; | 325 base::RunLoop run_loop; |
| 324 network_delegate_.set_url_request_destroyed_callback( | 326 network_delegate_.set_url_request_destroyed_callback( |
| 325 run_loop.QuitClosure()); | 327 run_loop.QuitClosure()); |
| 326 reporter->OnExpectCTFailed(host_port, report_uri, ssl_info); | 328 reporter->OnExpectCTFailed(host_port, report_uri, ssl_info); |
| 327 run_loop.Run(); | 329 run_loop.Run(); |
| 328 } | 330 } |
| 329 | 331 |
| 330 private: | 332 private: |
| 331 TestExpectCTNetworkDelegate network_delegate_; | 333 TestExpectCTNetworkDelegate network_delegate_; |
| 332 net::TestURLRequestContext context_; | 334 std::unique_ptr<net::TestURLRequestContext> context_; |
| 333 content::TestBrowserThreadBundle thread_bundle_; | 335 content::TestBrowserThreadBundle thread_bundle_; |
| 334 | 336 |
| 335 DISALLOW_COPY_AND_ASSIGN(ChromeExpectCTReporterWaitTest); | 337 DISALLOW_COPY_AND_ASSIGN(ChromeExpectCTReporterWaitTest); |
| 336 }; | 338 }; |
| 337 | 339 |
| 338 } // namespace | 340 } // namespace |
| 339 | 341 |
| 340 // Test that no report is sent when the feature is not enabled. | 342 // Test that no report is sent when the feature is not enabled. |
| 341 TEST(ChromeExpectCTReporterTest, FeatureDisabled) { | 343 TEST(ChromeExpectCTReporterTest, FeatureDisabled) { |
| 342 base::MessageLoop message_loop; | 344 base::MessageLoop message_loop; |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 EXPECT_EQ(report_uri, sender->latest_report_uri()); | 493 EXPECT_EQ(report_uri, sender->latest_report_uri()); |
| 492 EXPECT_FALSE(sender->latest_serialized_report().empty()); | 494 EXPECT_FALSE(sender->latest_serialized_report().empty()); |
| 493 EXPECT_EQ("application/json; charset=utf-8", sender->latest_content_type()); | 495 EXPECT_EQ("application/json; charset=utf-8", sender->latest_content_type()); |
| 494 ASSERT_NO_FATAL_FAILURE(CheckExpectCTReport( | 496 ASSERT_NO_FATAL_FAILURE(CheckExpectCTReport( |
| 495 sender->latest_serialized_report(), host_port, ssl_info)); | 497 sender->latest_serialized_report(), host_port, ssl_info)); |
| 496 | 498 |
| 497 histograms.ExpectTotalCount(kFailureHistogramName, 0); | 499 histograms.ExpectTotalCount(kFailureHistogramName, 0); |
| 498 histograms.ExpectTotalCount(kSendHistogramName, 1); | 500 histograms.ExpectTotalCount(kSendHistogramName, 1); |
| 499 histograms.ExpectBucketCount(kSendHistogramName, true, 1); | 501 histograms.ExpectBucketCount(kSendHistogramName, true, 1); |
| 500 } | 502 } |
| OLD | NEW |