1 // Copyright (c) 2012 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/net/chrome_fraudulent_certificate_reporter.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/threading/thread.h"
15 #include "chrome/browser/net/chrome_url_request_context.h"
16 #include "content/public/test/test_browser_thread.h"
17 #include "net/base/request_priority.h"
18 #include "net/base/test_data_directory.h"
19 #include "net/cert/x509_certificate.h"
20 #include "net/http/transport_security_state.h"
21 #include "net/ssl/ssl_info.h"
22 #include "net/test/cert_test_util.h"
23 #include "net/url_request/fraudulent_certificate_reporter.h"
24 #include "net/url_request/url_request.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 using content::BrowserThread;
30 namespace chrome_browser_net {
32 // Builds an SSLInfo from an invalid cert chain. In this case, the cert is
33 // expired; what matters is that the cert would not pass even a normal
34 // sanity check. We test that we DO NOT send a fraudulent certificate report
36 static SSLInfo GetBadSSLInfo() {
39 info.cert = net::ImportCertFromFile(net::GetTestCertsDirectory(),
41 info.cert_status = net::CERT_STATUS_DATE_INVALID;
42 info.is_issued_by_known_root = false;
47 // Builds an SSLInfo from a "good" cert chain, as defined by IsGoodSSLInfo,
48 // but which does not pass DomainState::IsChainOfPublicKeysPermitted. In this
49 // case, the certificate is for mail.google.com, signed by our Chrome test
50 // CA. During testing, Chrome believes this CA is part of the root system
51 // store. But, this CA is not in the pin list; we test that we DO send a
52 // fraudulent certicate report in this case.
53 static SSLInfo GetGoodSSLInfo() {
56 info.cert = net::ImportCertFromFile(net::GetTestCertsDirectory(),
57 "test_mail_google_com.pem");
58 info.is_issued_by_known_root = true;
63 // Checks that |info| is good as required by the SSL checks performed in
64 // URLRequestHttpJob::OnStartCompleted, which are enough to trigger pin
65 // checking but not sufficient to pass
66 // DomainState::IsChainOfPublicKeysPermitted.
67 static bool IsGoodSSLInfo(const SSLInfo& info) {
68 return info.is_valid() && info.is_issued_by_known_root;
71 class TestReporter : public ChromeFraudulentCertificateReporter {
73 explicit TestReporter(net::URLRequestContext* request_context)
74 : ChromeFraudulentCertificateReporter(request_context) {}
77 class SendingTestReporter : public TestReporter {
79 explicit SendingTestReporter(net::URLRequestContext* request_context)
80 : TestReporter(request_context), passed_(false) {}
82 // Passes if invoked with a good SSLInfo and for a hostname that is a Google
84 virtual void SendReport(const std::string& hostname,
85 const SSLInfo& ssl_info,
86 bool sni_available) OVERRIDE {
87 EXPECT_TRUE(IsGoodSSLInfo(ssl_info));
88 EXPECT_TRUE(net::TransportSecurityState::IsGooglePinnedProperty(
89 hostname, sni_available));
93 virtual ~SendingTestReporter() {
94 // If the object is destroyed without having its SendReport method invoked,
102 class NotSendingTestReporter : public TestReporter {
104 explicit NotSendingTestReporter(net::URLRequestContext* request_context)
105 : TestReporter(request_context) {}
107 // Passes if invoked with a bad SSLInfo and for a hostname that is not a
108 // Google pinned property.
109 virtual void SendReport(const std::string& hostname,
110 const SSLInfo& ssl_info,
111 bool sni_available) OVERRIDE {
112 EXPECT_FALSE(IsGoodSSLInfo(ssl_info));
113 EXPECT_FALSE(net::TransportSecurityState::IsGooglePinnedProperty(
114 hostname, sni_available));
118 // For the first version of the feature, sending reports is "fire and forget".
119 // Therefore, we test only that the Reporter tried to send a request at all.
120 // In the future, when we have more sophisticated (i.e., any) error handling
121 // and re-tries, we will need more sopisticated tests as well.
123 // This class doesn't do anything now, but in near future versions it will.
124 class MockURLRequest : public net::URLRequest {
126 explicit MockURLRequest(net::URLRequestContext* context)
127 : net::URLRequest(GURL(std::string()),
128 net::DEFAULT_PRIORITY,
135 // A ChromeFraudulentCertificateReporter that uses a MockURLRequest, but is
136 // otherwise normal: reports are constructed and sent in the usual way.
137 class MockReporter : public ChromeFraudulentCertificateReporter {
139 explicit MockReporter(net::URLRequestContext* request_context)
140 : ChromeFraudulentCertificateReporter(request_context) {}
142 virtual scoped_ptr<net::URLRequest> CreateURLRequest(
143 net::URLRequestContext* context) OVERRIDE {
144 return scoped_ptr<net::URLRequest>(new MockURLRequest(context));
147 virtual void SendReport(
148 const std::string& hostname,
149 const net::SSLInfo& ssl_info,
150 bool sni_available) OVERRIDE {
151 DCHECK(!hostname.empty());
152 DCHECK(ssl_info.is_valid());
153 ChromeFraudulentCertificateReporter::SendReport(hostname, ssl_info,
158 static void DoReportIsSent() {
159 ChromeURLRequestContext context(ChromeURLRequestContext::CONTEXT_TYPE_MAIN,
161 SendingTestReporter reporter(&context);
162 SSLInfo info = GetGoodSSLInfo();
163 reporter.SendReport("mail.google.com", info, true);
166 static void DoReportIsNotSent() {
167 ChromeURLRequestContext context(ChromeURLRequestContext::CONTEXT_TYPE_MAIN,
169 NotSendingTestReporter reporter(&context);
170 SSLInfo info = GetBadSSLInfo();
171 reporter.SendReport("www.example.com", info, true);
174 static void DoMockReportIsSent() {
175 ChromeURLRequestContext context(ChromeURLRequestContext::CONTEXT_TYPE_MAIN,
177 MockReporter reporter(&context);
178 SSLInfo info = GetGoodSSLInfo();
179 reporter.SendReport("mail.google.com", info, true);
182 TEST(ChromeFraudulentCertificateReporterTest, GoodBadInfo) {
183 SSLInfo good = GetGoodSSLInfo();
184 EXPECT_TRUE(IsGoodSSLInfo(good));
186 SSLInfo bad = GetBadSSLInfo();
187 EXPECT_FALSE(IsGoodSSLInfo(bad));
190 TEST(ChromeFraudulentCertificateReporterTest, ReportIsSent) {
191 base::MessageLoop loop(base::MessageLoop::TYPE_IO);
192 content::TestBrowserThread io_thread(BrowserThread::IO, &loop);
193 loop.PostTask(FROM_HERE, base::Bind(&DoReportIsSent));
197 TEST(ChromeFraudulentCertificateReporterTest, MockReportIsSent) {
198 base::MessageLoop loop(base::MessageLoop::TYPE_IO);
199 content::TestBrowserThread io_thread(BrowserThread::IO, &loop);
200 loop.PostTask(FROM_HERE, base::Bind(&DoMockReportIsSent));
204 TEST(ChromeFraudulentCertificateReporterTest, ReportIsNotSent) {
205 base::MessageLoop loop(base::MessageLoop::TYPE_IO);
206 content::TestBrowserThread io_thread(BrowserThread::IO, &loop);
207 loop.PostTask(FROM_HERE, base::Bind(&DoReportIsNotSent));
211 } // namespace chrome_browser_net