[sql] Remove _HAS_EXCEPTIONS=0 from build info.
[chromium-blink-merge.git] / chrome / browser / ssl / certificate_reporting_test_utils.cc
blob74e9fa82def7a19fd61fd8aefd7187498593f85f
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ssl/certificate_reporting_test_utils.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/safe_browsing/ping_manager.h"
16 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
17 #include "chrome/browser/safe_browsing/ui_manager.h"
18 #include "chrome/browser/ssl/cert_report_helper.h"
19 #include "chrome/browser/ssl/certificate_error_report.h"
20 #include "chrome/browser/ssl/ssl_cert_reporter.h"
21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/common/pref_names.h"
23 #include "components/variations/variations_associated_data.h"
24 #include "net/url_request/certificate_report_sender.h"
25 #include "net/url_request/url_request_context.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "url/gurl.h"
29 using chrome_browser_net::CertificateErrorReporter;
31 namespace {
33 void SetMockReporter(SafeBrowsingService* safe_browsing_service,
34 scoped_ptr<CertificateErrorReporter> reporter) {
35 safe_browsing_service->ping_manager()->SetCertificateErrorReporterForTesting(
36 reporter.Pass());
39 } // namespace
41 namespace CertificateReportingTestUtils {
43 // This class is used to test invalid certificate chain reporting when
44 // the user opts in to do so on the interstitial. It keeps track of the
45 // most recent hostname for which a report would have been sent over the
46 // network.
47 class MockReporter : public chrome_browser_net::CertificateErrorReporter {
48 public:
49 MockReporter(
50 net::URLRequestContext* request_context,
51 const GURL& upload_url,
52 net::CertificateReportSender::CookiesPreference cookies_preference);
54 // CertificateErrorReporter implementation
55 void SendReport(CertificateErrorReporter::ReportType type,
56 const std::string& serialized_report) override;
58 // Returns the hostname in the report for the last call to
59 // |SendReport|.
60 const std::string& latest_hostname_reported() {
61 return latest_hostname_reported_;
64 private:
65 std::string latest_hostname_reported_;
67 DISALLOW_COPY_AND_ASSIGN(MockReporter);
70 MockReporter::MockReporter(
71 net::URLRequestContext* request_context,
72 const GURL& upload_url,
73 net::CertificateReportSender::CookiesPreference cookies_preference)
74 : CertificateErrorReporter(request_context,
75 upload_url,
76 cookies_preference) {}
78 void MockReporter::SendReport(CertificateErrorReporter::ReportType type,
79 const std::string& serialized_report) {
80 CertificateErrorReport report;
81 ASSERT_TRUE(report.InitializeFromString(serialized_report));
82 EXPECT_EQ(CertificateErrorReporter::REPORT_TYPE_EXTENDED_REPORTING, type);
83 latest_hostname_reported_ = report.hostname();
86 void CertificateReportingTest::SetUpMockReporter() {
87 // Set up the mock reporter to track the hostnames that reports get
88 // sent for. The request_context argument is null here
89 // because the MockReporter doesn't actually use a
90 // request_context. (In order to pass a real request_context, the
91 // reporter would have to be constructed on the IO thread.)
92 reporter_ =
93 new MockReporter(nullptr, GURL("http://example.test"),
94 net::CertificateReportSender::DO_NOT_SEND_COOKIES);
96 scoped_refptr<SafeBrowsingService> safe_browsing_service =
97 g_browser_process->safe_browsing_service();
98 ASSERT_TRUE(safe_browsing_service);
100 content::BrowserThread::PostTask(
101 content::BrowserThread::IO, FROM_HERE,
102 base::Bind(
103 SetMockReporter, safe_browsing_service,
104 base::Passed(scoped_ptr<CertificateErrorReporter>(reporter_))));
107 const std::string& CertificateReportingTest::GetLatestHostnameReported() const {
108 return reporter_->latest_hostname_reported();
111 // This is a test implementation of the interface that blocking pages
112 // use to send certificate reports. It checks that the blocking page
113 // calls or does not call the report method when a report should or
114 // should not be sent, respectively.
115 class MockSSLCertReporter : public SSLCertReporter {
116 public:
117 MockSSLCertReporter(
118 const scoped_refptr<SafeBrowsingUIManager>& safe_browsing_ui_manager,
119 const base::Closure& report_sent_callback)
120 : safe_browsing_ui_manager_(safe_browsing_ui_manager),
121 reported_(false),
122 expect_report_(false),
123 report_sent_callback_(report_sent_callback) {}
125 ~MockSSLCertReporter() override { EXPECT_EQ(expect_report_, reported_); }
127 // SSLCertReporter implementation
128 void ReportInvalidCertificateChain(
129 const std::string& serialized_report) override {
130 reported_ = true;
131 if (expect_report_) {
132 safe_browsing_ui_manager_->ReportInvalidCertificateChain(
133 serialized_report, report_sent_callback_);
137 void set_expect_report(bool expect_report) { expect_report_ = expect_report; }
139 private:
140 const scoped_refptr<SafeBrowsingUIManager> safe_browsing_ui_manager_;
141 bool reported_;
142 bool expect_report_;
143 base::Closure report_sent_callback_;
146 void SetCertReportingOptIn(Browser* browser, OptIn opt_in) {
147 browser->profile()->GetPrefs()->SetBoolean(
148 prefs::kSafeBrowsingExtendedReportingEnabled,
149 opt_in == EXTENDED_REPORTING_OPT_IN);
152 scoped_ptr<SSLCertReporter> SetUpMockSSLCertReporter(
153 base::RunLoop* run_loop,
154 ExpectReport expect_report) {
155 // Set up a MockSSLCertReporter to keep track of when the blocking
156 // page invokes the cert reporter.
157 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service();
158 EXPECT_TRUE(sb_service);
159 if (!sb_service)
160 return nullptr;
162 scoped_ptr<MockSSLCertReporter> ssl_cert_reporter(new MockSSLCertReporter(
163 sb_service->ui_manager(), expect_report == CERT_REPORT_EXPECTED
164 ? run_loop->QuitClosure()
165 : base::Bind(&base::DoNothing)));
166 ssl_cert_reporter->set_expect_report(expect_report == CERT_REPORT_EXPECTED);
167 return ssl_cert_reporter.Pass();
170 ExpectReport GetReportExpectedFromFinch() {
171 const std::string group_name = base::FieldTrialList::FindFullName(
172 CertReportHelper::kFinchExperimentName);
174 if (group_name == CertReportHelper::kFinchGroupShowPossiblySend) {
175 const std::string param = variations::GetVariationParamValue(
176 CertReportHelper::kFinchExperimentName,
177 CertReportHelper::kFinchParamName);
178 double sendingThreshold;
179 if (!base::StringToDouble(param, &sendingThreshold))
180 return CERT_REPORT_NOT_EXPECTED;
182 if (sendingThreshold == 1.0)
183 return CertificateReportingTestUtils::CERT_REPORT_EXPECTED;
185 return CertificateReportingTestUtils::CERT_REPORT_NOT_EXPECTED;
188 } // namespace CertificateReportingTestUtils