OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/net/chrome_fraudulent_certificate_reporter.h" | 5 #include "chrome/browser/net/chrome_fraudulent_certificate_reporter.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "chrome/browser/net/certificate_error_reporter.h" |
15 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
16 #include "net/base/request_priority.h" | 17 #include "net/base/request_priority.h" |
17 #include "net/base/test_data_directory.h" | 18 #include "net/base/test_data_directory.h" |
18 #include "net/cert/x509_certificate.h" | 19 #include "net/cert/x509_certificate.h" |
19 #include "net/http/transport_security_state.h" | 20 #include "net/http/transport_security_state.h" |
20 #include "net/ssl/ssl_info.h" | 21 #include "net/ssl/ssl_info.h" |
21 #include "net/test/cert_test_util.h" | 22 #include "net/test/cert_test_util.h" |
22 #include "net/url_request/fraudulent_certificate_reporter.h" | 23 #include "net/url_request/fraudulent_certificate_reporter.h" |
23 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
24 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 106 |
106 // Passes if invoked with a bad SSLInfo and for a hostname that is not a | 107 // Passes if invoked with a bad SSLInfo and for a hostname that is not a |
107 // Google pinned property. | 108 // Google pinned property. |
108 void SendReport(const std::string& hostname, | 109 void SendReport(const std::string& hostname, |
109 const SSLInfo& ssl_info) override { | 110 const SSLInfo& ssl_info) override { |
110 EXPECT_FALSE(IsGoodSSLInfo(ssl_info)); | 111 EXPECT_FALSE(IsGoodSSLInfo(ssl_info)); |
111 EXPECT_FALSE(net::TransportSecurityState::IsGooglePinnedProperty(hostname)); | 112 EXPECT_FALSE(net::TransportSecurityState::IsGooglePinnedProperty(hostname)); |
112 } | 113 } |
113 }; | 114 }; |
114 | 115 |
115 // A ChromeFraudulentCertificateReporter that uses a MockURLRequest, but is | 116 // A CertificateErrorReporter that uses a MockURLRequest, but is |
116 // otherwise normal: reports are constructed and sent in the usual way. | 117 // otherwise normal: reports are constructed and sent in the usual way. |
117 class MockReporter : public ChromeFraudulentCertificateReporter { | 118 class MockReporter : public CertificateErrorReporter { |
118 public: | 119 public: |
119 explicit MockReporter(net::URLRequestContext* request_context) | 120 explicit MockReporter(net::URLRequestContext* request_context) |
120 : ChromeFraudulentCertificateReporter(request_context) {} | 121 : CertificateErrorReporter(request_context, GURL("http://example.com")) {} |
121 | 122 |
| 123 void SendReport(ReportType type, |
| 124 const std::string& hostname, |
| 125 const net::SSLInfo& ssl_info) override { |
| 126 EXPECT_EQ(type, REPORT_TYPE_PINNING_VIOLATION); |
| 127 EXPECT_FALSE(hostname.empty()); |
| 128 EXPECT_TRUE(ssl_info.is_valid()); |
| 129 CertificateErrorReporter::SendReport(type, hostname, ssl_info); |
| 130 } |
| 131 |
| 132 private: |
122 scoped_ptr<net::URLRequest> CreateURLRequest( | 133 scoped_ptr<net::URLRequest> CreateURLRequest( |
123 net::URLRequestContext* context) override { | 134 net::URLRequestContext* context) override { |
124 return context->CreateRequest(GURL(std::string()), | 135 return context->CreateRequest(GURL(std::string()), |
125 net::DEFAULT_PRIORITY, | 136 net::DEFAULT_PRIORITY, |
126 NULL, | 137 NULL, |
127 NULL); | 138 NULL); |
128 } | 139 } |
129 | |
130 void SendReport(const std::string& hostname, | |
131 const net::SSLInfo& ssl_info) override { | |
132 DCHECK(!hostname.empty()); | |
133 DCHECK(ssl_info.is_valid()); | |
134 ChromeFraudulentCertificateReporter::SendReport(hostname, ssl_info); | |
135 } | |
136 }; | 140 }; |
137 | 141 |
138 static void DoReportIsSent() { | 142 static void DoReportIsSent() { |
139 net::TestURLRequestContext context; | 143 net::TestURLRequestContext context; |
140 SendingTestReporter reporter(&context); | 144 SendingTestReporter reporter(&context); |
141 SSLInfo info = GetGoodSSLInfo(); | 145 SSLInfo info = GetGoodSSLInfo(); |
142 reporter.SendReport("mail.google.com", info); | 146 reporter.SendReport("mail.google.com", info); |
143 } | 147 } |
144 | 148 |
145 static void DoReportIsNotSent() { | 149 static void DoReportIsNotSent() { |
146 net::TestURLRequestContext context; | 150 net::TestURLRequestContext context; |
147 NotSendingTestReporter reporter(&context); | 151 NotSendingTestReporter reporter(&context); |
148 SSLInfo info = GetBadSSLInfo(); | 152 SSLInfo info = GetBadSSLInfo(); |
149 reporter.SendReport("www.example.com", info); | 153 reporter.SendReport("www.example.com", info); |
150 } | 154 } |
151 | 155 |
152 static void DoMockReportIsSent() { | 156 static void DoMockReportIsSent() { |
153 net::TestURLRequestContext context; | 157 net::TestURLRequestContext context; |
154 MockReporter reporter(&context); | 158 scoped_ptr<MockReporter> error_reporter(new MockReporter(&context)); |
| 159 ChromeFraudulentCertificateReporter reporter(error_reporter.Pass()); |
155 SSLInfo info = GetGoodSSLInfo(); | 160 SSLInfo info = GetGoodSSLInfo(); |
156 reporter.SendReport("mail.google.com", info); | 161 reporter.SendReport("mail.google.com", info); |
157 } | 162 } |
158 | 163 |
159 TEST(ChromeFraudulentCertificateReporterTest, GoodBadInfo) { | 164 TEST(ChromeFraudulentCertificateReporterTest, GoodBadInfo) { |
160 SSLInfo good = GetGoodSSLInfo(); | 165 SSLInfo good = GetGoodSSLInfo(); |
161 EXPECT_TRUE(IsGoodSSLInfo(good)); | 166 EXPECT_TRUE(IsGoodSSLInfo(good)); |
162 | 167 |
163 SSLInfo bad = GetBadSSLInfo(); | 168 SSLInfo bad = GetBadSSLInfo(); |
164 EXPECT_FALSE(IsGoodSSLInfo(bad)); | 169 EXPECT_FALSE(IsGoodSSLInfo(bad)); |
(...skipping 14 matching lines...) Expand all Loading... |
179 } | 184 } |
180 | 185 |
181 TEST(ChromeFraudulentCertificateReporterTest, ReportIsNotSent) { | 186 TEST(ChromeFraudulentCertificateReporterTest, ReportIsNotSent) { |
182 base::MessageLoopForIO loop; | 187 base::MessageLoopForIO loop; |
183 content::TestBrowserThread io_thread(BrowserThread::IO, &loop); | 188 content::TestBrowserThread io_thread(BrowserThread::IO, &loop); |
184 loop.PostTask(FROM_HERE, base::Bind(&DoReportIsNotSent)); | 189 loop.PostTask(FROM_HERE, base::Bind(&DoReportIsNotSent)); |
185 loop.RunUntilIdle(); | 190 loop.RunUntilIdle(); |
186 } | 191 } |
187 | 192 |
188 } // namespace chrome_browser_net | 193 } // namespace chrome_browser_net |
OLD | NEW |