OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/certificate_error_reporter.h" | 5 #include "chrome/browser/net/certificate_error_reporter.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/files/file_path.h" | |
13 #include "base/files/file_util.h" | |
14 #include "base/macros.h" | 12 #include "base/macros.h" |
15 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
16 #include "base/path_service.h" | |
17 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
18 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
19 #include "chrome/browser/net/cert_logger.pb.h" | 16 #include "chrome/browser/net/encrypted_cert_logger.pb.h" |
20 #include "chrome/common/chrome_paths.h" | 17 #include "chrome/common/chrome_paths.h" |
21 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
22 #include "crypto/curve25519.h" | 19 #include "crypto/curve25519.h" |
23 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" |
24 #include "net/base/network_delegate_impl.h" | 21 #include "net/base/network_delegate_impl.h" |
25 #include "net/base/test_data_directory.h" | |
26 #include "net/base/upload_bytes_element_reader.h" | 22 #include "net/base/upload_bytes_element_reader.h" |
27 #include "net/base/upload_data_stream.h" | 23 #include "net/base/upload_data_stream.h" |
28 #include "net/base/upload_element_reader.h" | 24 #include "net/base/upload_element_reader.h" |
29 #include "net/test/cert_test_util.h" | |
30 #include "net/test/url_request/url_request_failed_job.h" | 25 #include "net/test/url_request/url_request_failed_job.h" |
31 #include "net/test/url_request/url_request_mock_data_job.h" | 26 #include "net/test/url_request/url_request_mock_data_job.h" |
32 #include "net/test/url_request/url_request_mock_http_job.h" | 27 #include "net/test/url_request/url_request_mock_http_job.h" |
33 #include "net/url_request/url_request_filter.h" | 28 #include "net/url_request/url_request_filter.h" |
34 #include "net/url_request/url_request_test_util.h" | 29 #include "net/url_request/url_request_test_util.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
36 | 31 |
37 using chrome_browser_net::CertificateErrorReporter; | 32 using chrome_browser_net::CertificateErrorReporter; |
38 using content::BrowserThread; | 33 using content::BrowserThread; |
39 using net::CompletionCallback; | 34 using net::CompletionCallback; |
40 using net::SSLInfo; | |
41 using net::NetworkDelegateImpl; | 35 using net::NetworkDelegateImpl; |
42 using net::TestURLRequestContext; | 36 using net::TestURLRequestContext; |
43 using net::URLRequest; | 37 using net::URLRequest; |
44 | 38 |
45 namespace { | 39 namespace { |
46 | 40 |
47 const char kHostname[] = "test.mail.google.com"; | 41 const char kDummyReport[] = "test.mail.google.com"; |
48 const char kSecondRequestHostname[] = "test2.mail.google.com"; | 42 const char kSecondDummyReport[] = "test2.mail.google.com"; |
49 const char kDummyFailureLog[] = "dummy failure log"; | |
50 const char kTestCertFilename[] = "test_mail_google_com.pem"; | |
51 const uint32 kServerPublicKeyVersion = 1; | 43 const uint32 kServerPublicKeyVersion = 1; |
52 | 44 |
53 SSLInfo GetTestSSLInfo() { | |
54 SSLInfo info; | |
55 info.cert = | |
56 net::ImportCertFromFile(net::GetTestCertsDirectory(), kTestCertFilename); | |
57 info.is_issued_by_known_root = true; | |
58 info.pinning_failure_log = kDummyFailureLog; | |
59 return info; | |
60 } | |
61 | |
62 std::string GetPEMEncodedChain() { | |
63 base::FilePath cert_path = | |
64 net::GetTestCertsDirectory().AppendASCII(kTestCertFilename); | |
65 std::string cert_data; | |
66 EXPECT_TRUE(base::ReadFileToString(cert_path, &cert_data)); | |
67 return cert_data; | |
68 } | |
69 | |
70 void EnableUrlRequestMocks(bool enable) { | 45 void EnableUrlRequestMocks(bool enable) { |
71 net::URLRequestFilter::GetInstance()->ClearHandlers(); | 46 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
72 if (!enable) | 47 if (!enable) |
73 return; | 48 return; |
74 | 49 |
75 base::FilePath root_http; | 50 base::FilePath root_http; |
76 net::URLRequestFailedJob::AddUrlHandler(); | 51 net::URLRequestFailedJob::AddUrlHandler(); |
77 PathService::Get(chrome::DIR_TEST_DATA, &root_http); | 52 PathService::Get(chrome::DIR_TEST_DATA, &root_http); |
78 net::URLRequestMockHTTPJob::AddUrlHandlers(root_http, | 53 net::URLRequestMockHTTPJob::AddUrlHandlers(root_http, |
79 BrowserThread::GetBlockingPool()); | 54 BrowserThread::GetBlockingPool()); |
80 net::URLRequestMockDataJob::AddUrlHandler(); | 55 net::URLRequestMockDataJob::AddUrlHandler(); |
81 } | 56 } |
82 | 57 |
83 // Check that data uploaded in the request matches the test data (an SSL | 58 // Check that data uploaded in the request matches the test report |
84 // report for one of the given hostnames, with the info returned by | 59 // data. The sent reports will be erased from |expect_reports|. |
85 // |GetTestSSLInfo()|). The hostname sent in the report will be erased | |
86 // from |expect_hostnames|. | |
87 void CheckUploadData(URLRequest* request, | 60 void CheckUploadData(URLRequest* request, |
88 std::set<std::string>* expect_hostnames, | 61 std::set<std::string>* expect_reports, |
89 bool encrypted, | 62 bool encrypted, |
90 const uint8* server_private_key) { | 63 const uint8* server_private_key) { |
91 const net::UploadDataStream* upload = request->get_upload(); | 64 const net::UploadDataStream* upload = request->get_upload(); |
92 ASSERT_TRUE(upload); | 65 ASSERT_TRUE(upload); |
93 ASSERT_TRUE(upload->GetElementReaders()); | 66 ASSERT_TRUE(upload->GetElementReaders()); |
94 EXPECT_EQ(1u, upload->GetElementReaders()->size()); | 67 EXPECT_EQ(1u, upload->GetElementReaders()->size()); |
95 | 68 |
96 const net::UploadBytesElementReader* reader = | 69 const net::UploadBytesElementReader* reader = |
97 (*upload->GetElementReaders())[0]->AsBytesReader(); | 70 (*upload->GetElementReaders())[0]->AsBytesReader(); |
98 ASSERT_TRUE(reader); | 71 ASSERT_TRUE(reader); |
99 std::string upload_data(reader->bytes(), reader->length()); | 72 std::string upload_data(reader->bytes(), reader->length()); |
100 | 73 |
101 chrome_browser_net::CertLoggerRequest uploaded_request; | 74 std::string uploaded_report; |
102 #if defined(USE_OPENSSL) | 75 #if defined(USE_OPENSSL) |
103 if (encrypted) { | 76 if (encrypted) { |
104 chrome_browser_net::EncryptedCertLoggerRequest encrypted_request; | 77 chrome_browser_net::EncryptedCertLoggerRequest encrypted_request; |
105 encrypted_request.ParseFromString(upload_data); | 78 encrypted_request.ParseFromString(upload_data); |
106 EXPECT_EQ(kServerPublicKeyVersion, | 79 EXPECT_EQ(kServerPublicKeyVersion, |
107 encrypted_request.server_public_key_version()); | 80 encrypted_request.server_public_key_version()); |
108 EXPECT_EQ(chrome_browser_net::EncryptedCertLoggerRequest:: | 81 EXPECT_EQ(chrome_browser_net::EncryptedCertLoggerRequest:: |
109 AEAD_ECDH_AES_128_CTR_HMAC_SHA256, | 82 AEAD_ECDH_AES_128_CTR_HMAC_SHA256, |
110 encrypted_request.algorithm()); | 83 encrypted_request.algorithm()); |
111 ASSERT_TRUE( | 84 ASSERT_TRUE(CertificateErrorReporter::DecryptCertificateErrorReport( |
112 chrome_browser_net::CertificateErrorReporter:: | 85 server_private_key, encrypted_request, &uploaded_report)); |
113 DecryptCertificateErrorReport(server_private_key, encrypted_request, | |
114 &uploaded_request)); | |
115 } else { | 86 } else { |
116 ASSERT_TRUE(uploaded_request.ParseFromString(upload_data)); | 87 uploaded_report = upload_data; |
117 } | 88 } |
118 #else | 89 #else |
119 ASSERT_TRUE(uploaded_request.ParseFromString(upload_data)); | 90 uploaded_report = upload_data; |
120 #endif | 91 #endif |
121 | 92 |
122 EXPECT_EQ(1u, expect_hostnames->count(uploaded_request.hostname())); | 93 EXPECT_EQ(1u, expect_reports->count(uploaded_report)); |
123 expect_hostnames->erase(uploaded_request.hostname()); | 94 expect_reports->erase(uploaded_report); |
124 | |
125 EXPECT_EQ(GetPEMEncodedChain(), uploaded_request.cert_chain()); | |
126 EXPECT_EQ(1, uploaded_request.pin().size()); | |
127 EXPECT_EQ(kDummyFailureLog, uploaded_request.pin().Get(0)); | |
128 } | 95 } |
129 | 96 |
130 // A network delegate that lets tests check that a certificate error | 97 // A network delegate that lets tests check that a certificate error |
131 // report was sent. It counts the number of requests and lets tests | 98 // report was sent. It counts the number of requests and lets tests |
132 // register a callback to run when the request is destroyed. It also | 99 // register a callback to run when the request is destroyed. It also |
133 // checks that the uploaded data is as expected (a report for | 100 // checks that the uploaded data is as expected. |
134 // |kHostname| and |GetTestSSLInfo()|). | |
135 class TestCertificateErrorReporterNetworkDelegate : public NetworkDelegateImpl { | 101 class TestCertificateErrorReporterNetworkDelegate : public NetworkDelegateImpl { |
136 public: | 102 public: |
137 TestCertificateErrorReporterNetworkDelegate() | 103 TestCertificateErrorReporterNetworkDelegate() |
138 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), | 104 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), |
139 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)), | 105 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)), |
140 num_requests_(0), | 106 num_requests_(0), |
141 expect_cookies_(false), | 107 expect_cookies_(false), |
142 expect_request_encrypted_(false) { | 108 expect_request_encrypted_(false) { |
143 memset(server_private_key_, 1, sizeof(server_private_key_)); | 109 memset(server_private_key_, 1, sizeof(server_private_key_)); |
144 crypto::curve25519::ScalarBaseMult(server_private_key_, server_public_key_); | 110 crypto::curve25519::ScalarBaseMult(server_private_key_, server_public_key_); |
145 } | 111 } |
146 | 112 |
147 ~TestCertificateErrorReporterNetworkDelegate() override {} | 113 ~TestCertificateErrorReporterNetworkDelegate() override {} |
148 | 114 |
149 void ExpectHostname(const std::string& hostname) { | 115 void ExpectReport(const std::string& report) { |
150 expect_hostnames_.insert(hostname); | 116 expect_reports_.insert(report); |
151 } | 117 } |
152 | 118 |
153 void set_all_url_requests_destroyed_callback( | 119 void set_all_url_requests_destroyed_callback( |
154 const base::Closure& all_url_requests_destroyed_callback) { | 120 const base::Closure& all_url_requests_destroyed_callback) { |
155 all_url_requests_destroyed_callback_ = all_url_requests_destroyed_callback; | 121 all_url_requests_destroyed_callback_ = all_url_requests_destroyed_callback; |
156 } | 122 } |
157 | 123 |
158 void set_url_request_destroyed_callback( | 124 void set_url_request_destroyed_callback( |
159 const base::Closure& url_request_destroyed_callback) { | 125 const base::Closure& url_request_destroyed_callback) { |
160 url_request_destroyed_callback_ = url_request_destroyed_callback; | 126 url_request_destroyed_callback_ = url_request_destroyed_callback; |
(...skipping 23 matching lines...) Expand all Loading... |
184 EXPECT_EQ("POST", request->method()); | 150 EXPECT_EQ("POST", request->method()); |
185 | 151 |
186 if (expect_cookies_) { | 152 if (expect_cookies_) { |
187 EXPECT_FALSE(request->load_flags() & net::LOAD_DO_NOT_SEND_COOKIES); | 153 EXPECT_FALSE(request->load_flags() & net::LOAD_DO_NOT_SEND_COOKIES); |
188 EXPECT_FALSE(request->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES); | 154 EXPECT_FALSE(request->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES); |
189 } else { | 155 } else { |
190 EXPECT_TRUE(request->load_flags() & net::LOAD_DO_NOT_SEND_COOKIES); | 156 EXPECT_TRUE(request->load_flags() & net::LOAD_DO_NOT_SEND_COOKIES); |
191 EXPECT_TRUE(request->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES); | 157 EXPECT_TRUE(request->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES); |
192 } | 158 } |
193 | 159 |
194 std::string uploaded_request_hostname; | 160 CheckUploadData(request, &expect_reports_, expect_request_encrypted_, |
195 CheckUploadData(request, &expect_hostnames_, expect_request_encrypted_, | |
196 server_private_key_); | 161 server_private_key_); |
197 expect_hostnames_.erase(uploaded_request_hostname); | |
198 return net::OK; | 162 return net::OK; |
199 } | 163 } |
200 | 164 |
201 void OnURLRequestDestroyed(URLRequest* request) override { | 165 void OnURLRequestDestroyed(URLRequest* request) override { |
202 url_request_destroyed_callback_.Run(); | 166 url_request_destroyed_callback_.Run(); |
203 if (expect_hostnames_.empty()) | 167 if (expect_reports_.empty()) |
204 all_url_requests_destroyed_callback_.Run(); | 168 all_url_requests_destroyed_callback_.Run(); |
205 } | 169 } |
206 | 170 |
207 const uint8* server_public_key() { return server_public_key_; } | 171 const uint8* server_public_key() { return server_public_key_; } |
208 | 172 |
209 private: | 173 private: |
210 base::Closure url_request_destroyed_callback_; | 174 base::Closure url_request_destroyed_callback_; |
211 base::Closure all_url_requests_destroyed_callback_; | 175 base::Closure all_url_requests_destroyed_callback_; |
212 int num_requests_; | 176 int num_requests_; |
213 GURL expect_url_; | 177 GURL expect_url_; |
214 std::set<std::string> expect_hostnames_; | 178 std::set<std::string> expect_reports_; |
215 bool expect_cookies_; | 179 bool expect_cookies_; |
216 bool expect_request_encrypted_; | 180 bool expect_request_encrypted_; |
217 | 181 |
218 uint8 server_public_key_[32]; | 182 uint8 server_public_key_[32]; |
219 uint8 server_private_key_[32]; | 183 uint8 server_private_key_[32]; |
220 | 184 |
221 DISALLOW_COPY_AND_ASSIGN(TestCertificateErrorReporterNetworkDelegate); | 185 DISALLOW_COPY_AND_ASSIGN(TestCertificateErrorReporterNetworkDelegate); |
222 }; | 186 }; |
223 | 187 |
224 class CertificateErrorReporterTest : public ::testing::Test { | 188 class CertificateErrorReporterTest : public ::testing::Test { |
(...skipping 13 matching lines...) Expand all Loading... |
238 TestURLRequestContext* context() { return &context_; } | 202 TestURLRequestContext* context() { return &context_; } |
239 | 203 |
240 private: | 204 private: |
241 base::MessageLoop message_loop_; | 205 base::MessageLoop message_loop_; |
242 TestCertificateErrorReporterNetworkDelegate network_delegate_; | 206 TestCertificateErrorReporterNetworkDelegate network_delegate_; |
243 TestURLRequestContext context_; | 207 TestURLRequestContext context_; |
244 }; | 208 }; |
245 | 209 |
246 void SendReport(CertificateErrorReporter* reporter, | 210 void SendReport(CertificateErrorReporter* reporter, |
247 TestCertificateErrorReporterNetworkDelegate* network_delegate, | 211 TestCertificateErrorReporterNetworkDelegate* network_delegate, |
248 const std::string& report_hostname, | 212 const std::string& report, |
249 const GURL& url, | 213 const GURL& url, |
250 int request_sequence_number, | 214 int request_sequence_number, |
251 CertificateErrorReporter::ReportType type) { | 215 CertificateErrorReporter::ReportType type) { |
252 base::RunLoop run_loop; | 216 base::RunLoop run_loop; |
253 network_delegate->set_url_request_destroyed_callback(run_loop.QuitClosure()); | 217 network_delegate->set_url_request_destroyed_callback(run_loop.QuitClosure()); |
254 | 218 |
255 network_delegate->set_expect_url(url); | 219 network_delegate->set_expect_url(url); |
256 network_delegate->ExpectHostname(report_hostname); | 220 network_delegate->ExpectReport(report); |
257 | 221 |
258 EXPECT_EQ(request_sequence_number, network_delegate->num_requests()); | 222 EXPECT_EQ(request_sequence_number, network_delegate->num_requests()); |
259 | 223 |
260 reporter->SendReport(type, report_hostname, GetTestSSLInfo()); | 224 reporter->SendReport(type, report); |
261 run_loop.Run(); | 225 run_loop.Run(); |
262 | 226 |
263 EXPECT_EQ(request_sequence_number + 1, network_delegate->num_requests()); | 227 EXPECT_EQ(request_sequence_number + 1, network_delegate->num_requests()); |
264 } | 228 } |
265 | 229 |
266 // Test that CertificateErrorReporter::SendReport creates a URLRequest | 230 // Test that CertificateErrorReporter::SendReport creates a URLRequest |
267 // for the endpoint and sends the expected data. | 231 // for the endpoint and sends the expected data. |
268 TEST_F(CertificateErrorReporterTest, PinningViolationSendReportSendsRequest) { | 232 TEST_F(CertificateErrorReporterTest, PinningViolationSendReportSendsRequest) { |
269 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 233 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
270 CertificateErrorReporter reporter( | 234 CertificateErrorReporter reporter( |
271 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); | 235 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); |
272 SendReport(&reporter, network_delegate(), kHostname, url, 0, | 236 SendReport(&reporter, network_delegate(), kDummyReport, url, 0, |
273 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); | 237 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); |
274 } | 238 } |
275 | 239 |
276 TEST_F(CertificateErrorReporterTest, ExtendedReportingSendReportSendsRequest) { | 240 TEST_F(CertificateErrorReporterTest, ExtendedReportingSendReportSendsRequest) { |
277 // Data should not be encrypted when sent to an HTTPS URL. | 241 // Data should not be encrypted when sent to an HTTPS URL. |
278 GURL https_url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 242 GURL https_url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
279 CertificateErrorReporter https_reporter( | 243 CertificateErrorReporter https_reporter( |
280 context(), https_url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); | 244 context(), https_url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); |
281 network_delegate()->set_expect_request_encrypted(false); | 245 network_delegate()->set_expect_request_encrypted(false); |
282 SendReport(&https_reporter, network_delegate(), kHostname, https_url, 0, | 246 SendReport(&https_reporter, network_delegate(), kDummyReport, https_url, 0, |
283 CertificateErrorReporter::REPORT_TYPE_EXTENDED_REPORTING); | 247 CertificateErrorReporter::REPORT_TYPE_EXTENDED_REPORTING); |
284 | 248 |
285 // Data should be encrypted when sent to an HTTP URL. | 249 // Data should be encrypted when sent to an HTTP URL. |
286 if (CertificateErrorReporter::IsHttpUploadUrlSupported()) { | 250 if (CertificateErrorReporter::IsHttpUploadUrlSupported()) { |
287 GURL http_url = net::URLRequestMockDataJob::GetMockHttpUrl("dummy data", 1); | 251 GURL http_url = net::URLRequestMockDataJob::GetMockHttpUrl("dummy data", 1); |
288 CertificateErrorReporter http_reporter( | 252 CertificateErrorReporter http_reporter( |
289 context(), http_url, CertificateErrorReporter::DO_NOT_SEND_COOKIES, | 253 context(), http_url, CertificateErrorReporter::DO_NOT_SEND_COOKIES, |
290 network_delegate()->server_public_key(), kServerPublicKeyVersion); | 254 network_delegate()->server_public_key(), kServerPublicKeyVersion); |
291 network_delegate()->set_expect_request_encrypted(true); | 255 network_delegate()->set_expect_request_encrypted(true); |
292 SendReport(&http_reporter, network_delegate(), kHostname, http_url, 1, | 256 SendReport(&http_reporter, network_delegate(), kDummyReport, http_url, 1, |
293 CertificateErrorReporter::REPORT_TYPE_EXTENDED_REPORTING); | 257 CertificateErrorReporter::REPORT_TYPE_EXTENDED_REPORTING); |
294 } | 258 } |
295 } | 259 } |
296 | 260 |
297 TEST_F(CertificateErrorReporterTest, SendMultipleReportsSequentially) { | 261 TEST_F(CertificateErrorReporterTest, SendMultipleReportsSequentially) { |
298 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 262 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
299 CertificateErrorReporter reporter( | 263 CertificateErrorReporter reporter( |
300 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); | 264 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); |
301 SendReport(&reporter, network_delegate(), kHostname, url, 0, | 265 SendReport(&reporter, network_delegate(), kDummyReport, url, 0, |
302 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); | 266 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); |
303 SendReport(&reporter, network_delegate(), kSecondRequestHostname, url, 1, | 267 SendReport(&reporter, network_delegate(), kDummyReport, url, 1, |
304 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); | 268 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); |
305 } | 269 } |
306 | 270 |
307 TEST_F(CertificateErrorReporterTest, SendMultipleReportsSimultaneously) { | 271 TEST_F(CertificateErrorReporterTest, SendMultipleReportsSimultaneously) { |
308 base::RunLoop run_loop; | 272 base::RunLoop run_loop; |
309 network_delegate()->set_all_url_requests_destroyed_callback( | 273 network_delegate()->set_all_url_requests_destroyed_callback( |
310 run_loop.QuitClosure()); | 274 run_loop.QuitClosure()); |
311 | 275 |
312 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 276 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
313 network_delegate()->set_expect_url(url); | 277 network_delegate()->set_expect_url(url); |
314 network_delegate()->ExpectHostname(kHostname); | 278 network_delegate()->ExpectReport(kDummyReport); |
315 network_delegate()->ExpectHostname(kSecondRequestHostname); | 279 network_delegate()->ExpectReport(kSecondDummyReport); |
316 | 280 |
317 CertificateErrorReporter reporter( | 281 CertificateErrorReporter reporter( |
318 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); | 282 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); |
319 | 283 |
320 EXPECT_EQ(0, network_delegate()->num_requests()); | 284 EXPECT_EQ(0, network_delegate()->num_requests()); |
321 | 285 |
322 reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, | 286 reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, |
323 kHostname, GetTestSSLInfo()); | 287 kDummyReport); |
324 reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, | 288 reporter.SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, |
325 kSecondRequestHostname, GetTestSSLInfo()); | 289 kSecondDummyReport); |
326 | 290 |
327 run_loop.Run(); | 291 run_loop.Run(); |
328 | 292 |
329 EXPECT_EQ(2, network_delegate()->num_requests()); | 293 EXPECT_EQ(2, network_delegate()->num_requests()); |
330 } | 294 } |
331 | 295 |
332 // Test that pending URLRequests get cleaned up when the reporter is | 296 // Test that pending URLRequests get cleaned up when the reporter is |
333 // deleted. | 297 // deleted. |
334 TEST_F(CertificateErrorReporterTest, PendingRequestGetsDeleted) { | 298 TEST_F(CertificateErrorReporterTest, PendingRequestGetsDeleted) { |
335 base::RunLoop run_loop; | 299 base::RunLoop run_loop; |
336 network_delegate()->set_url_request_destroyed_callback( | 300 network_delegate()->set_url_request_destroyed_callback( |
337 run_loop.QuitClosure()); | 301 run_loop.QuitClosure()); |
338 | 302 |
339 GURL url = net::URLRequestMockHTTPJob::GetMockUrlWithFailure( | 303 GURL url = net::URLRequestMockHTTPJob::GetMockUrlWithFailure( |
340 base::FilePath(FILE_PATH_LITERAL("empty.html")), | 304 base::FilePath(FILE_PATH_LITERAL("empty.html")), |
341 net::URLRequestMockHTTPJob::START, net::ERR_IO_PENDING); | 305 net::URLRequestMockHTTPJob::START, net::ERR_IO_PENDING); |
342 network_delegate()->set_expect_url(url); | 306 network_delegate()->set_expect_url(url); |
343 network_delegate()->ExpectHostname(kHostname); | 307 network_delegate()->ExpectReport(kDummyReport); |
344 | 308 |
345 EXPECT_EQ(0, network_delegate()->num_requests()); | 309 EXPECT_EQ(0, network_delegate()->num_requests()); |
346 | 310 |
347 scoped_ptr<CertificateErrorReporter> reporter(new CertificateErrorReporter( | 311 scoped_ptr<CertificateErrorReporter> reporter(new CertificateErrorReporter( |
348 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES)); | 312 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES)); |
349 reporter->SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, | 313 reporter->SendReport(CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION, |
350 kHostname, GetTestSSLInfo()); | 314 kDummyReport); |
351 reporter.reset(); | 315 reporter.reset(); |
352 | 316 |
353 run_loop.Run(); | 317 run_loop.Run(); |
354 | 318 |
355 EXPECT_EQ(1, network_delegate()->num_requests()); | 319 EXPECT_EQ(1, network_delegate()->num_requests()); |
356 } | 320 } |
357 | 321 |
358 // Test that a request that returns an error gets cleaned up. | 322 // Test that a request that returns an error gets cleaned up. |
359 TEST_F(CertificateErrorReporterTest, ErroredRequestGetsDeleted) { | 323 TEST_F(CertificateErrorReporterTest, ErroredRequestGetsDeleted) { |
360 GURL url = net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_FAILED); | 324 GURL url = net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_FAILED); |
361 CertificateErrorReporter reporter( | 325 CertificateErrorReporter reporter( |
362 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); | 326 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); |
363 SendReport(&reporter, network_delegate(), kHostname, url, 0, | 327 SendReport(&reporter, network_delegate(), kDummyReport, url, 0, |
364 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); | 328 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); |
365 } | 329 } |
366 | 330 |
367 // Test that cookies are sent or not sent according to the error | 331 // Test that cookies are sent or not sent according to the error |
368 // reporter's cookies preference. | 332 // reporter's cookies preference. |
369 | 333 |
370 TEST_F(CertificateErrorReporterTest, SendCookiesPreference) { | 334 TEST_F(CertificateErrorReporterTest, SendCookiesPreference) { |
371 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 335 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
372 CertificateErrorReporter reporter(context(), url, | 336 CertificateErrorReporter reporter(context(), url, |
373 CertificateErrorReporter::SEND_COOKIES); | 337 CertificateErrorReporter::SEND_COOKIES); |
374 | 338 |
375 network_delegate()->set_expect_cookies(true); | 339 network_delegate()->set_expect_cookies(true); |
376 SendReport(&reporter, network_delegate(), kHostname, url, 0, | 340 SendReport(&reporter, network_delegate(), kDummyReport, url, 0, |
377 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); | 341 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); |
378 } | 342 } |
379 | 343 |
380 TEST_F(CertificateErrorReporterTest, DoNotSendCookiesPreference) { | 344 TEST_F(CertificateErrorReporterTest, DoNotSendCookiesPreference) { |
381 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 345 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
382 CertificateErrorReporter reporter( | 346 CertificateErrorReporter reporter( |
383 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); | 347 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); |
384 | 348 |
385 network_delegate()->set_expect_cookies(false); | 349 network_delegate()->set_expect_cookies(false); |
386 SendReport(&reporter, network_delegate(), kHostname, url, 0, | 350 SendReport(&reporter, network_delegate(), kDummyReport, url, 0, |
387 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); | 351 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); |
388 } | 352 } |
389 | 353 |
390 } // namespace | 354 } // namespace |
OLD | NEW |