| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/certificate_reporting_test_utils.h" | 5 #include "chrome/browser/ssl/certificate_reporting_test_utils.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/safe_browsing/ping_manager.h" | 15 #include "chrome/browser/safe_browsing/ping_manager.h" |
| 16 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 16 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 17 #include "chrome/browser/safe_browsing/ui_manager.h" | 17 #include "chrome/browser/safe_browsing/ui_manager.h" |
| 18 #include "chrome/browser/ssl/cert_report_helper.h" | 18 #include "chrome/browser/ssl/cert_report_helper.h" |
| 19 #include "chrome/browser/ssl/certificate_error_report.h" | 19 #include "chrome/browser/ssl/certificate_error_report.h" |
| 20 #include "chrome/browser/ssl/ssl_cert_reporter.h" | 20 #include "chrome/browser/ssl/ssl_cert_reporter.h" |
| 21 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 23 #include "components/variations/variations_associated_data.h" | 23 #include "components/variations/variations_associated_data.h" |
| 24 #include "net/url_request/certificate_report_sender.h" |
| 24 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 27 | 28 |
| 28 using chrome_browser_net::CertificateErrorReporter; | 29 using chrome_browser_net::CertificateErrorReporter; |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 void SetMockReporter(SafeBrowsingService* safe_browsing_service, | 33 void SetMockReporter(SafeBrowsingService* safe_browsing_service, |
| 33 scoped_ptr<CertificateErrorReporter> reporter) { | 34 scoped_ptr<CertificateErrorReporter> reporter) { |
| 34 safe_browsing_service->ping_manager()->SetCertificateErrorReporterForTesting( | 35 safe_browsing_service->ping_manager()->SetCertificateErrorReporterForTesting( |
| 35 reporter.Pass()); | 36 reporter.Pass()); |
| 36 } | 37 } |
| 37 | 38 |
| 38 } // namespace | 39 } // namespace |
| 39 | 40 |
| 40 namespace CertificateReportingTestUtils { | 41 namespace CertificateReportingTestUtils { |
| 41 | 42 |
| 42 // This class is used to test invalid certificate chain reporting when | 43 // This class is used to test invalid certificate chain reporting when |
| 43 // the user opts in to do so on the interstitial. It keeps track of the | 44 // the user opts in to do so on the interstitial. It keeps track of the |
| 44 // most recent hostname for which a report would have been sent over the | 45 // most recent hostname for which a report would have been sent over the |
| 45 // network. | 46 // network. |
| 46 class MockReporter : public chrome_browser_net::CertificateErrorReporter { | 47 class MockReporter : public chrome_browser_net::CertificateErrorReporter { |
| 47 public: | 48 public: |
| 48 MockReporter(net::URLRequestContext* request_context, | 49 MockReporter( |
| 49 const GURL& upload_url, | 50 net::URLRequestContext* request_context, |
| 50 CookiesPreference cookies_preference); | 51 const GURL& upload_url, |
| 52 net::CertificateReportSender::CookiesPreference cookies_preference); |
| 51 | 53 |
| 52 // CertificateErrorReporter implementation | 54 // CertificateErrorReporter implementation |
| 53 void SendReport(CertificateErrorReporter::ReportType type, | 55 void SendReport(CertificateErrorReporter::ReportType type, |
| 54 const std::string& serialized_report) override; | 56 const std::string& serialized_report) override; |
| 55 | 57 |
| 56 // Returns the hostname in the report for the last call to | 58 // Returns the hostname in the report for the last call to |
| 57 // |SendReport|. | 59 // |SendReport|. |
| 58 const std::string& latest_hostname_reported() { | 60 const std::string& latest_hostname_reported() { |
| 59 return latest_hostname_reported_; | 61 return latest_hostname_reported_; |
| 60 } | 62 } |
| 61 | 63 |
| 62 private: | 64 private: |
| 63 std::string latest_hostname_reported_; | 65 std::string latest_hostname_reported_; |
| 64 | 66 |
| 65 DISALLOW_COPY_AND_ASSIGN(MockReporter); | 67 DISALLOW_COPY_AND_ASSIGN(MockReporter); |
| 66 }; | 68 }; |
| 67 | 69 |
| 68 MockReporter::MockReporter(net::URLRequestContext* request_context, | 70 MockReporter::MockReporter( |
| 69 const GURL& upload_url, | 71 net::URLRequestContext* request_context, |
| 70 CookiesPreference cookies_preference) | 72 const GURL& upload_url, |
| 73 net::CertificateReportSender::CookiesPreference cookies_preference) |
| 71 : CertificateErrorReporter(request_context, | 74 : CertificateErrorReporter(request_context, |
| 72 upload_url, | 75 upload_url, |
| 73 cookies_preference) { | 76 cookies_preference) {} |
| 74 } | |
| 75 | 77 |
| 76 void MockReporter::SendReport(CertificateErrorReporter::ReportType type, | 78 void MockReporter::SendReport(CertificateErrorReporter::ReportType type, |
| 77 const std::string& serialized_report) { | 79 const std::string& serialized_report) { |
| 78 CertificateErrorReport report; | 80 CertificateErrorReport report; |
| 79 ASSERT_TRUE(report.InitializeFromString(serialized_report)); | 81 ASSERT_TRUE(report.InitializeFromString(serialized_report)); |
| 80 EXPECT_EQ(CertificateErrorReporter::REPORT_TYPE_EXTENDED_REPORTING, type); | 82 EXPECT_EQ(CertificateErrorReporter::REPORT_TYPE_EXTENDED_REPORTING, type); |
| 81 latest_hostname_reported_ = report.hostname(); | 83 latest_hostname_reported_ = report.hostname(); |
| 82 } | 84 } |
| 83 | 85 |
| 84 void CertificateReportingTest::SetUpMockReporter() { | 86 void CertificateReportingTest::SetUpMockReporter() { |
| 85 // Set up the mock reporter to track the hostnames that reports get | 87 // Set up the mock reporter to track the hostnames that reports get |
| 86 // sent for. The request_context argument is null here | 88 // sent for. The request_context argument is null here |
| 87 // because the MockReporter doesn't actually use a | 89 // because the MockReporter doesn't actually use a |
| 88 // request_context. (In order to pass a real request_context, the | 90 // request_context. (In order to pass a real request_context, the |
| 89 // reporter would have to be constructed on the IO thread.) | 91 // reporter would have to be constructed on the IO thread.) |
| 90 reporter_ = new MockReporter(nullptr, GURL("http://example.test"), | 92 reporter_ = |
| 91 MockReporter::DO_NOT_SEND_COOKIES); | 93 new MockReporter(nullptr, GURL("http://example.test"), |
| 94 net::CertificateReportSender::DO_NOT_SEND_COOKIES); |
| 92 | 95 |
| 93 scoped_refptr<SafeBrowsingService> safe_browsing_service = | 96 scoped_refptr<SafeBrowsingService> safe_browsing_service = |
| 94 g_browser_process->safe_browsing_service(); | 97 g_browser_process->safe_browsing_service(); |
| 95 ASSERT_TRUE(safe_browsing_service); | 98 ASSERT_TRUE(safe_browsing_service); |
| 96 | 99 |
| 97 content::BrowserThread::PostTask( | 100 content::BrowserThread::PostTask( |
| 98 content::BrowserThread::IO, FROM_HERE, | 101 content::BrowserThread::IO, FROM_HERE, |
| 99 base::Bind( | 102 base::Bind( |
| 100 SetMockReporter, safe_browsing_service, | 103 SetMockReporter, safe_browsing_service, |
| 101 base::Passed(scoped_ptr<CertificateErrorReporter>(reporter_)))); | 104 base::Passed(scoped_ptr<CertificateErrorReporter>(reporter_)))); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 if (!base::StringToDouble(param, &sendingThreshold)) | 179 if (!base::StringToDouble(param, &sendingThreshold)) |
| 177 return CERT_REPORT_NOT_EXPECTED; | 180 return CERT_REPORT_NOT_EXPECTED; |
| 178 | 181 |
| 179 if (sendingThreshold == 1.0) | 182 if (sendingThreshold == 1.0) |
| 180 return CertificateReportingTestUtils::CERT_REPORT_EXPECTED; | 183 return CertificateReportingTestUtils::CERT_REPORT_EXPECTED; |
| 181 } | 184 } |
| 182 return CertificateReportingTestUtils::CERT_REPORT_NOT_EXPECTED; | 185 return CertificateReportingTestUtils::CERT_REPORT_NOT_EXPECTED; |
| 183 } | 186 } |
| 184 | 187 |
| 185 } // namespace CertificateReportingTestUtils | 188 } // namespace CertificateReportingTestUtils |
| OLD | NEW |