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 "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
16 #include "net/base/request_priority.h" | 16 #include "net/base/request_priority.h" |
17 #include "net/base/test_data_directory.h" | 17 #include "net/base/test_data_directory.h" |
18 #include "net/cert/x509_certificate.h" | 18 #include "net/cert/x509_certificate.h" |
19 #include "net/http/transport_security_state.h" | 19 #include "net/http/transport_security_state.h" |
20 #include "net/ssl/ssl_info.h" | 20 #include "net/ssl/ssl_info.h" |
21 #include "net/test/cert_test_util.h" | 21 #include "net/test/cert_test_util.h" |
22 #include "net/url_request/fraudulent_certificate_reporter.h" | 22 #include "net/url_request/fraudulent_certificate_reporter.h" |
23 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
| 24 #include "net/url_request/url_request_context.h" |
24 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 | 27 |
27 using content::BrowserThread; | 28 using content::BrowserThread; |
28 using net::SSLInfo; | 29 using net::SSLInfo; |
29 | 30 |
30 namespace chrome_browser_net { | 31 namespace chrome_browser_net { |
31 | 32 |
32 // Builds an SSLInfo from an invalid cert chain. In this case, the cert is | 33 // 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 // expired; what matters is that the cert would not pass even a normal |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 // Google pinned property. | 109 // Google pinned property. |
109 virtual void SendReport(const std::string& hostname, | 110 virtual void SendReport(const std::string& hostname, |
110 const SSLInfo& ssl_info, | 111 const SSLInfo& ssl_info, |
111 bool sni_available) OVERRIDE { | 112 bool sni_available) OVERRIDE { |
112 EXPECT_FALSE(IsGoodSSLInfo(ssl_info)); | 113 EXPECT_FALSE(IsGoodSSLInfo(ssl_info)); |
113 EXPECT_FALSE(net::TransportSecurityState::IsGooglePinnedProperty( | 114 EXPECT_FALSE(net::TransportSecurityState::IsGooglePinnedProperty( |
114 hostname, sni_available)); | 115 hostname, sni_available)); |
115 } | 116 } |
116 }; | 117 }; |
117 | 118 |
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. | |
122 // | |
123 // This class doesn't do anything now, but in near future versions it will. | |
124 class MockURLRequest : public net::URLRequest { | |
125 public: | |
126 explicit MockURLRequest(net::URLRequestContext* context) | |
127 : net::URLRequest(GURL(std::string()), | |
128 net::DEFAULT_PRIORITY, | |
129 NULL, | |
130 context) {} | |
131 | |
132 private: | |
133 }; | |
134 | |
135 // A ChromeFraudulentCertificateReporter that uses a MockURLRequest, but is | 119 // A ChromeFraudulentCertificateReporter that uses a MockURLRequest, but is |
136 // otherwise normal: reports are constructed and sent in the usual way. | 120 // otherwise normal: reports are constructed and sent in the usual way. |
137 class MockReporter : public ChromeFraudulentCertificateReporter { | 121 class MockReporter : public ChromeFraudulentCertificateReporter { |
138 public: | 122 public: |
139 explicit MockReporter(net::URLRequestContext* request_context) | 123 explicit MockReporter(net::URLRequestContext* request_context) |
140 : ChromeFraudulentCertificateReporter(request_context) {} | 124 : ChromeFraudulentCertificateReporter(request_context) {} |
141 | 125 |
142 virtual scoped_ptr<net::URLRequest> CreateURLRequest( | 126 virtual scoped_ptr<net::URLRequest> CreateURLRequest( |
143 net::URLRequestContext* context) OVERRIDE { | 127 net::URLRequestContext* context) OVERRIDE { |
144 return scoped_ptr<net::URLRequest>(new MockURLRequest(context)); | 128 return context->CreateRequest(GURL(std::string()), |
| 129 net::DEFAULT_PRIORITY, |
| 130 NULL, |
| 131 NULL); |
145 } | 132 } |
146 | 133 |
147 virtual void SendReport( | 134 virtual void SendReport( |
148 const std::string& hostname, | 135 const std::string& hostname, |
149 const net::SSLInfo& ssl_info, | 136 const net::SSLInfo& ssl_info, |
150 bool sni_available) OVERRIDE { | 137 bool sni_available) OVERRIDE { |
151 DCHECK(!hostname.empty()); | 138 DCHECK(!hostname.empty()); |
152 DCHECK(ssl_info.is_valid()); | 139 DCHECK(ssl_info.is_valid()); |
153 ChromeFraudulentCertificateReporter::SendReport(hostname, ssl_info, | 140 ChromeFraudulentCertificateReporter::SendReport(hostname, ssl_info, |
154 sni_available); | 141 sni_available); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 } | 186 } |
200 | 187 |
201 TEST(ChromeFraudulentCertificateReporterTest, ReportIsNotSent) { | 188 TEST(ChromeFraudulentCertificateReporterTest, ReportIsNotSent) { |
202 base::MessageLoopForIO loop; | 189 base::MessageLoopForIO loop; |
203 content::TestBrowserThread io_thread(BrowserThread::IO, &loop); | 190 content::TestBrowserThread io_thread(BrowserThread::IO, &loop); |
204 loop.PostTask(FROM_HERE, base::Bind(&DoReportIsNotSent)); | 191 loop.PostTask(FROM_HERE, base::Bind(&DoReportIsNotSent)); |
205 loop.RunUntilIdle(); | 192 loop.RunUntilIdle(); |
206 } | 193 } |
207 | 194 |
208 } // namespace chrome_browser_net | 195 } // namespace chrome_browser_net |
OLD | NEW |