OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/net/certificate_error_reporter.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/files/file_path.h" | |
10 #include "base/files/file_util.h" | |
11 #include "base/macros.h" | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/path_service.h" | |
14 #include "base/run_loop.h" | |
15 #include "base/thread_task_runner_handle.h" | |
16 #include "chrome/browser/net/cert_logger.pb.h" | |
17 #include "chrome/common/chrome_paths.h" | |
18 #include "content/public/browser/browser_thread.h" | |
19 #include "net/base/network_delegate_impl.h" | |
20 #include "net/base/test_data_directory.h" | |
21 #include "net/base/upload_bytes_element_reader.h" | |
22 #include "net/base/upload_data_stream.h" | |
23 #include "net/base/upload_element_reader.h" | |
24 #include "net/test/cert_test_util.h" | |
25 #include "net/test/url_request/url_request_failed_job.h" | |
26 #include "net/test/url_request/url_request_mock_http_job.h" | |
27 #include "net/url_request/url_request_filter.h" | |
28 #include "net/url_request/url_request_test_util.h" | |
29 #include "testing/gtest/include/gtest/gtest.h" | |
30 | |
31 using chrome_browser_net::CertificateErrorReporter; | |
32 using content::BrowserThread; | |
33 using net::CompletionCallback; | |
34 using net::SSLInfo; | |
35 using net::NetworkDelegateImpl; | |
36 using net::TestURLRequestContext; | |
37 using net::URLRequest; | |
38 | |
39 namespace { | |
40 | |
41 const char kHostname[] = "test.mail.google.com"; | |
42 const char kDummyFailureLog[] = "dummy failure log"; | |
43 const char kTestCertFilename[] = FILE_PATH_LITERAL("test_mail_google_com.pem"); | |
Ryan Sleevi
2015/03/12 05:15:05
You need to have this use base::FilePath::CharType
estark
2015/03/12 14:21:48
Done.
| |
44 | |
45 SSLInfo GetSSLInfo() { | |
Ryan Sleevi
2015/03/12 05:15:05
pedantic nit: GetTestSSLInfo()?
estark
2015/03/12 14:21:48
Done.
| |
46 SSLInfo info; | |
47 info.cert = | |
48 net::ImportCertFromFile(net::GetTestCertsDirectory(), kTestCertFilename); | |
49 info.is_issued_by_known_root = true; | |
50 info.pinning_failure_log = kDummyFailureLog; | |
51 return info; | |
52 } | |
53 | |
54 std::string GetPEMEncodedChain() { | |
55 base::FilePath cert_path = | |
56 net::GetTestCertsDirectory().AppendASCII(kTestCertFilename); | |
57 std::string cert_data; | |
58 EXPECT_TRUE(base::ReadFileToString(cert_path, &cert_data)); | |
59 return cert_data; | |
60 } | |
61 | |
62 void EnableUrlRequestMocks(bool enable) { | |
63 net::URLRequestFilter::GetInstance()->ClearHandlers(); | |
64 if (enable) { | |
Ryan Sleevi
2015/03/12 05:15:05
if (!enable)
return
base::FilePath root_http;
.
estark
2015/03/12 14:21:48
Done.
| |
65 base::FilePath root_http; | |
66 net::URLRequestFailedJob::AddUrlHandler(); | |
67 PathService::Get(chrome::DIR_TEST_DATA, &root_http); | |
68 net::URLRequestMockHTTPJob::AddUrlHandlers( | |
69 root_http, BrowserThread::GetBlockingPool()); | |
70 } | |
71 } | |
72 | |
73 // Check that data uploaded in the request matches the test data (an | |
74 // SSL report for |kHostname| as returned by |GetSSLInfo()|). | |
75 void CheckUploadData(URLRequest* request) { | |
76 const net::UploadDataStream* upload = request->get_upload(); | |
77 ASSERT_TRUE(upload); | |
78 ASSERT_TRUE(upload->GetElementReaders()); | |
79 EXPECT_EQ(1u, upload->GetElementReaders()->size()); | |
80 | |
81 const net::UploadBytesElementReader* reader = | |
82 (*upload->GetElementReaders())[0]->AsBytesReader(); | |
83 ASSERT_TRUE(reader); | |
84 std::string upload_data(reader->bytes(), reader->length()); | |
85 chrome_browser_net::CertLoggerRequest uploaded_request; | |
86 | |
87 uploaded_request.ParseFromString(upload_data); | |
88 | |
89 EXPECT_EQ(kHostname, uploaded_request.hostname()); | |
90 EXPECT_EQ(GetPEMEncodedChain(), uploaded_request.cert_chain()); | |
91 EXPECT_EQ(1, uploaded_request.pin().size()); | |
92 EXPECT_EQ(kDummyFailureLog, uploaded_request.pin().Get(0)); | |
93 } | |
94 | |
95 // A network delegate that lets tests check that a certificate error | |
96 // report was sent. It counts the number of requests and lets tests | |
97 // register a callback to run when the request is destroyed. It also | |
98 // checks that the uploaded data is as expected (a report for | |
99 // |kHostname| and |GetSSLInfo()|). | |
100 class TestCertificateErrorReporterNetworkDelegate : public NetworkDelegateImpl { | |
101 public: | |
102 TestCertificateErrorReporterNetworkDelegate() | |
103 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), | |
104 num_requests_(0) {} | |
105 | |
106 ~TestCertificateErrorReporterNetworkDelegate() override {} | |
107 | |
108 int OnBeforeURLRequest(URLRequest* request, | |
109 const CompletionCallback& callback, | |
110 GURL* new_url) override { | |
111 num_requests_++; | |
112 EXPECT_EQ(expected_url_, request->url()); | |
113 EXPECT_EQ("POST", request->method()); | |
114 | |
115 CheckUploadData(request); | |
116 return net::OK; | |
117 } | |
118 | |
119 void OnURLRequestDestroyed(URLRequest* request) override { | |
120 url_request_destroyed_callback_.Run(); | |
121 } | |
122 | |
123 void set_url_request_destroyed_callback( | |
124 const base::Closure& url_request_destroyed_callback) { | |
125 url_request_destroyed_callback_ = url_request_destroyed_callback; | |
126 } | |
127 | |
128 void set_expected_url(const GURL& expected_url) { | |
129 expected_url_ = expected_url; | |
130 } | |
131 | |
132 int num_requests() const { return num_requests_; } | |
133 | |
134 private: | |
135 base::Closure url_request_destroyed_callback_; | |
136 int num_requests_; | |
137 GURL expected_url_; | |
138 | |
139 DISALLOW_COPY_AND_ASSIGN(TestCertificateErrorReporterNetworkDelegate); | |
140 }; | |
141 | |
142 class CertificateErrorReporterTest : public ::testing::Test { | |
143 public: | |
144 CertificateErrorReporterTest() : context_(true) { | |
145 EnableUrlRequestMocks(true); | |
146 context_.set_network_delegate(&network_delegate_); | |
147 context_.Init(); | |
148 } | |
149 | |
150 ~CertificateErrorReporterTest() override { EnableUrlRequestMocks(false); } | |
151 | |
152 TestCertificateErrorReporterNetworkDelegate* network_delegate() { | |
153 return &network_delegate_; | |
154 } | |
155 | |
156 TestURLRequestContext* context() { return &context_; } | |
157 | |
158 private: | |
159 base::MessageLoop message_loop_; | |
160 TestCertificateErrorReporterNetworkDelegate network_delegate_; | |
161 TestURLRequestContext context_; | |
162 }; | |
163 | |
164 // Test that CertificateErrorReporter::SendReport creates a URLRequest | |
165 // for the endpoint and sends the expected data. | |
166 TEST_F(CertificateErrorReporterTest, SendReportSendsRequest) { | |
167 base::RunLoop run_loop; | |
168 network_delegate()->set_url_request_destroyed_callback( | |
169 run_loop.QuitClosure()); | |
170 | |
171 GURL url = net::URLRequestMockHTTPJob::GetMockHttpsUrl( | |
172 base::FilePath(FILE_PATH_LITERAL("empty.html"))); | |
173 network_delegate()->set_expected_url(url); | |
174 | |
175 CertificateErrorReporter reporter(context(), url); | |
176 | |
177 EXPECT_EQ(0, network_delegate()->num_requests()); | |
178 | |
179 reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, | |
180 kHostname, GetSSLInfo()); | |
181 run_loop.Run(); | |
182 | |
183 EXPECT_EQ(1, network_delegate()->num_requests()); | |
184 } | |
185 | |
186 // Test that pending URLRequests get cleaned up when the reporter is | |
187 // deleted. | |
188 TEST_F(CertificateErrorReporterTest, PendingRequestGetsDeleted) { | |
189 base::RunLoop run_loop; | |
190 network_delegate()->set_url_request_destroyed_callback( | |
191 run_loop.QuitClosure()); | |
192 | |
193 GURL url = net::URLRequestMockHTTPJob::GetMockUrlWithFailure( | |
194 base::FilePath(FILE_PATH_LITERAL("empty.html")), | |
195 net::URLRequestMockHTTPJob::START, net::ERR_IO_PENDING); | |
196 network_delegate()->set_expected_url(url); | |
197 | |
198 EXPECT_EQ(0, network_delegate()->num_requests()); | |
199 | |
200 scoped_ptr<CertificateErrorReporter> reporter( | |
201 new CertificateErrorReporter(context(), url)); | |
202 reporter->SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, | |
203 kHostname, GetSSLInfo()); | |
204 reporter.reset(); | |
205 | |
206 run_loop.Run(); | |
207 | |
208 EXPECT_EQ(1, network_delegate()->num_requests()); | |
209 } | |
210 | |
211 // Test that a request that returns an error gets cleaned up. | |
212 TEST_F(CertificateErrorReporterTest, ErroredRequestGetsDeleted) { | |
213 base::RunLoop run_loop; | |
214 network_delegate()->set_url_request_destroyed_callback( | |
215 run_loop.QuitClosure()); | |
216 | |
217 GURL url = net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_FAILED); | |
218 network_delegate()->set_expected_url(url); | |
219 | |
220 EXPECT_EQ(0, network_delegate()->num_requests()); | |
221 | |
222 CertificateErrorReporter reporter(context(), url); | |
223 reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, | |
224 kHostname, GetSSLInfo()); | |
225 run_loop.Run(); | |
226 | |
227 EXPECT_EQ(1, network_delegate()->num_requests()); | |
228 } | |
229 | |
230 } // namespace | |
OLD | NEW |