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/path_service.h" | |
12 #include "base/run_loop.h" | |
13 #include "base/thread_task_runner_handle.h" | |
14 #include "chrome/browser/net/cert_logger.pb.h" | |
15 #include "chrome/browser/net/url_request_mock_util.h" | |
16 #include "chrome/common/chrome_paths.h" | |
17 #include "content/public/browser/browser_thread.h" | |
18 #include "content/public/test/test_browser_thread_bundle.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"); | |
44 | |
45 SSLInfo GetSSLInfo() { | |
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) { | |
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(upload->GetElementReaders()->size(), 1u); | |
mmenke
2015/03/11 15:34:28
For EXPECT_EQ and ASSERT_EQ, the expected value sh
estark
2015/03/11 22:19:10
Done.
| |
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(uploaded_request.hostname(), kHostname); | |
90 EXPECT_EQ(uploaded_request.cert_chain(), GetPEMEncodedChain()); | |
91 EXPECT_EQ(uploaded_request.pin().size(), 1); | |
92 EXPECT_EQ(uploaded_request.pin().Get(0), kDummyFailureLog); | |
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 CertificateErrorReporterTestNetworkDelegate : public NetworkDelegateImpl { | |
estark
2015/03/11 05:58:34
Is this name too disgustingly long? I was trying t
mmenke
2015/03/11 15:34:28
I'm fine with long names. Maybe call it TestCerti
estark
2015/03/11 22:19:10
Done.
| |
101 public: | |
102 CertificateErrorReporterTestNetworkDelegate() | |
103 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), | |
104 num_requests_(0) {} | |
105 | |
106 ~CertificateErrorReporterTestNetworkDelegate() override {} | |
107 | |
108 int OnBeforeURLRequest(URLRequest* request, | |
109 const CompletionCallback& callback, | |
110 GURL* new_url) override { | |
111 num_requests_++; | |
112 EXPECT_EQ(request->url(), expected_url_); | |
113 EXPECT_EQ(request->method(), "POST"); | |
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 SetURLRequestDestroyedCallback(base::Closure callback) { | |
mmenke
2015/03/11 15:34:28
nit: const base::Closure&
estark
2015/03/11 22:19:10
Done.
| |
124 url_request_destroyed_callback_ = callback; | |
125 } | |
126 | |
127 void SetExpectedURL(const GURL& url) { expected_url_ = url; } | |
mmenke
2015/03/11 15:34:28
nit: set_expected_url. Also more comment to call
estark
2015/03/11 22:19:10
Done.
| |
128 | |
129 int num_requests() const { return num_requests_; } | |
130 | |
131 private: | |
132 base::Closure url_request_destroyed_callback_; | |
133 int num_requests_; | |
134 GURL expected_url_; | |
mmenke
2015/03/11 15:34:28
nit:
DISALLOW_COPY_AND_ASSIGN(CertificateErrorRep
estark
2015/03/11 22:19:10
Done.
| |
135 }; | |
136 | |
137 class CertificateErrorReporterTest : public ::testing::Test { | |
138 public: | |
139 CertificateErrorReporterTest() : context_(true) { | |
140 EnableUrlRequestMocks(true); | |
141 context_.set_network_delegate(&network_delegate_); | |
142 context_.Init(); | |
143 } | |
144 | |
145 ~CertificateErrorReporterTest() override { | |
146 EnableUrlRequestMocks(false); | |
147 chrome_browser_net::SetUrlRequestMocksEnabled(false); | |
mmenke
2015/03/11 15:34:28
This isn't needed - it does the same thing as the
estark
2015/03/11 22:19:10
Done.
| |
148 } | |
149 | |
150 CertificateErrorReporterTestNetworkDelegate* network_delegate() { | |
151 return &network_delegate_; | |
152 } | |
153 | |
154 TestURLRequestContext* context() { return &context_; } | |
155 | |
156 private: | |
157 content::TestBrowserThreadBundle thread_bundle_; | |
mmenke
2015/03/11 15:34:28
Do we still needs this? Think we can get away wit
estark
2015/03/11 22:19:10
Done.
| |
158 TestURLRequestContext context_; | |
159 CertificateErrorReporterTestNetworkDelegate network_delegate_; | |
mmenke
2015/03/11 15:34:28
The network delegate should be before the context,
estark
2015/03/11 22:19:10
Done.
| |
160 }; | |
161 | |
162 // Test that CertificateErrorReporter::SendReport creates a URLRequest | |
163 // for the endpoint and sends the expected data. | |
164 TEST_F(CertificateErrorReporterTest, SendReportSendsRequest) { | |
165 base::RunLoop run_loop; | |
166 network_delegate()->SetURLRequestDestroyedCallback(run_loop.QuitClosure()); | |
167 | |
168 GURL url = net::URLRequestMockHTTPJob::GetMockHttpsUrl( | |
169 base::FilePath(FILE_PATH_LITERAL("empty.html"))); | |
170 network_delegate()->SetExpectedURL(url); | |
171 | |
172 CertificateErrorReporter reporter(context(), url); | |
173 | |
174 EXPECT_EQ(0, network_delegate()->num_requests()); | |
175 | |
176 reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, | |
177 kHostname, GetSSLInfo()); | |
178 run_loop.Run(); | |
179 | |
180 EXPECT_EQ(1, network_delegate()->num_requests()); | |
181 } | |
182 | |
183 // Test that pending URLRequests get cleaned up when the reporter is | |
184 // deleted. | |
185 TEST_F(CertificateErrorReporterTest, PendingRequestGetsDeleted) { | |
186 base::RunLoop run_loop; | |
187 network_delegate()->SetURLRequestDestroyedCallback(run_loop.QuitClosure()); | |
188 | |
189 GURL url = net::URLRequestMockHTTPJob::GetMockUrlWithFailure( | |
190 base::FilePath(FILE_PATH_LITERAL("empty.html")), | |
191 net::URLRequestMockHTTPJob::START, net::ERR_IO_PENDING); | |
192 network_delegate()->SetExpectedURL(url); | |
193 | |
194 EXPECT_EQ(0, network_delegate()->num_requests()); | |
195 | |
196 scoped_ptr<CertificateErrorReporter> reporter( | |
197 new CertificateErrorReporter(context(), url)); | |
198 reporter->SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, | |
199 kHostname, GetSSLInfo()); | |
200 reporter.reset(); | |
201 | |
202 run_loop.Run(); | |
203 | |
204 EXPECT_EQ(1, network_delegate()->num_requests()); | |
205 } | |
206 | |
207 // Test that a request that returns an error gets cleaned up. | |
208 TEST_F(CertificateErrorReporterTest, ErroredRequestGetsDeleted) { | |
209 base::RunLoop run_loop; | |
210 network_delegate()->SetURLRequestDestroyedCallback(run_loop.QuitClosure()); | |
211 | |
212 GURL url = net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_FAILED); | |
213 network_delegate()->SetExpectedURL(url); | |
214 | |
215 EXPECT_EQ(0, network_delegate()->num_requests()); | |
216 | |
217 CertificateErrorReporter reporter(context(), url); | |
218 reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, | |
219 kHostname, GetSSLInfo()); | |
220 run_loop.Run(); | |
221 | |
222 EXPECT_EQ(1, network_delegate()->num_requests()); | |
223 } | |
224 | |
225 } // namespace | |
OLD | NEW |