| 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" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
| 19 #include "chrome/browser/net/cert_logger.pb.h" | 19 #include "chrome/browser/net/cert_logger.pb.h" |
| 20 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
| 21 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 22 #include "crypto/curve25519.h" | 22 #include "crypto/curve25519.h" |
| 23 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
| 24 #include "net/base/network_delegate_impl.h" | 24 #include "net/base/network_delegate_impl.h" |
| 25 #include "net/base/test_data_directory.h" | 25 #include "net/base/test_data_directory.h" |
| 26 #include "net/base/upload_bytes_element_reader.h" | 26 #include "net/base/upload_bytes_element_reader.h" |
| 27 #include "net/base/upload_data_stream.h" | 27 #include "net/base/upload_data_stream.h" |
| 28 #include "net/base/upload_element_reader.h" | 28 #include "net/base/upload_element_reader.h" |
| 29 #include "net/cert/cert_status_flags.h" |
| 29 #include "net/test/cert_test_util.h" | 30 #include "net/test/cert_test_util.h" |
| 30 #include "net/test/url_request/url_request_failed_job.h" | 31 #include "net/test/url_request/url_request_failed_job.h" |
| 31 #include "net/test/url_request/url_request_mock_data_job.h" | 32 #include "net/test/url_request/url_request_mock_data_job.h" |
| 32 #include "net/test/url_request/url_request_mock_http_job.h" | 33 #include "net/test/url_request/url_request_mock_http_job.h" |
| 33 #include "net/url_request/url_request_filter.h" | 34 #include "net/url_request/url_request_filter.h" |
| 34 #include "net/url_request/url_request_test_util.h" | 35 #include "net/url_request/url_request_test_util.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
| 36 | 37 |
| 37 using chrome_browser_net::CertificateErrorReporter; | 38 using chrome_browser_net::CertificateErrorReporter; |
| 38 using content::BrowserThread; | 39 using content::BrowserThread; |
| 40 using net::CertStatus; |
| 39 using net::CompletionCallback; | 41 using net::CompletionCallback; |
| 40 using net::SSLInfo; | 42 using net::SSLInfo; |
| 41 using net::NetworkDelegateImpl; | 43 using net::NetworkDelegateImpl; |
| 42 using net::TestURLRequestContext; | 44 using net::TestURLRequestContext; |
| 43 using net::URLRequest; | 45 using net::URLRequest; |
| 44 | 46 |
| 45 namespace { | 47 namespace { |
| 46 | 48 |
| 47 const char kHostname[] = "test.mail.google.com"; | 49 const char kHostname[] = "test.mail.google.com"; |
| 48 const char kSecondRequestHostname[] = "test2.mail.google.com"; | 50 const char kSecondRequestHostname[] = "test2.mail.google.com"; |
| 49 const char kDummyFailureLog[] = "dummy failure log"; | 51 const char kDummyFailureLog[] = "dummy failure log"; |
| 50 const char kTestCertFilename[] = "test_mail_google_com.pem"; | 52 const char kTestCertFilename[] = "test_mail_google_com.pem"; |
| 51 const uint32 kServerPublicKeyVersion = 1; | 53 const uint32 kServerPublicKeyVersion = 1; |
| 54 const CertStatus kCertStatus = |
| 55 net::CERT_STATUS_COMMON_NAME_INVALID | net::CERT_STATUS_REVOKED; |
| 56 const size_t kNumCertErrors = 2; |
| 57 const chrome_browser_net::CertLoggerRequest::CertError kFirstReportedCertError = |
| 58 chrome_browser_net::CertLoggerRequest::ERR_CERT_COMMON_NAME_INVALID; |
| 59 const chrome_browser_net::CertLoggerRequest::CertError |
| 60 kSecondReportedCertError = |
| 61 chrome_browser_net::CertLoggerRequest::ERR_CERT_REVOKED; |
| 52 | 62 |
| 53 SSLInfo GetTestSSLInfo() { | 63 SSLInfo GetTestSSLInfo() { |
| 54 SSLInfo info; | 64 SSLInfo info; |
| 55 info.cert = | 65 info.cert = |
| 56 net::ImportCertFromFile(net::GetTestCertsDirectory(), kTestCertFilename); | 66 net::ImportCertFromFile(net::GetTestCertsDirectory(), kTestCertFilename); |
| 57 info.is_issued_by_known_root = true; | 67 info.is_issued_by_known_root = true; |
| 68 info.cert_status = kCertStatus; |
| 58 info.pinning_failure_log = kDummyFailureLog; | 69 info.pinning_failure_log = kDummyFailureLog; |
| 59 return info; | 70 return info; |
| 60 } | 71 } |
| 61 | 72 |
| 62 std::string GetPEMEncodedChain() { | 73 std::string GetPEMEncodedChain() { |
| 63 base::FilePath cert_path = | 74 base::FilePath cert_path = |
| 64 net::GetTestCertsDirectory().AppendASCII(kTestCertFilename); | 75 net::GetTestCertsDirectory().AppendASCII(kTestCertFilename); |
| 65 std::string cert_data; | 76 std::string cert_data; |
| 66 EXPECT_TRUE(base::ReadFileToString(cert_path, &cert_data)); | 77 EXPECT_TRUE(base::ReadFileToString(cert_path, &cert_data)); |
| 67 return cert_data; | 78 return cert_data; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 #else | 129 #else |
| 119 ASSERT_TRUE(uploaded_request.ParseFromString(upload_data)); | 130 ASSERT_TRUE(uploaded_request.ParseFromString(upload_data)); |
| 120 #endif | 131 #endif |
| 121 | 132 |
| 122 EXPECT_EQ(1u, expect_hostnames->count(uploaded_request.hostname())); | 133 EXPECT_EQ(1u, expect_hostnames->count(uploaded_request.hostname())); |
| 123 expect_hostnames->erase(uploaded_request.hostname()); | 134 expect_hostnames->erase(uploaded_request.hostname()); |
| 124 | 135 |
| 125 EXPECT_EQ(GetPEMEncodedChain(), uploaded_request.cert_chain()); | 136 EXPECT_EQ(GetPEMEncodedChain(), uploaded_request.cert_chain()); |
| 126 EXPECT_EQ(1, uploaded_request.pin().size()); | 137 EXPECT_EQ(1, uploaded_request.pin().size()); |
| 127 EXPECT_EQ(kDummyFailureLog, uploaded_request.pin().Get(0)); | 138 EXPECT_EQ(kDummyFailureLog, uploaded_request.pin().Get(0)); |
| 139 EXPECT_EQ(2, uploaded_request.cert_error().size()); |
| 140 |
| 141 std::set<chrome_browser_net::CertLoggerRequest::CertError> reported_errors; |
| 142 reported_errors.insert( |
| 143 static_cast<chrome_browser_net::CertLoggerRequest::CertError>( |
| 144 uploaded_request.cert_error().Get(0))); |
| 145 reported_errors.insert( |
| 146 static_cast<chrome_browser_net::CertLoggerRequest::CertError>( |
| 147 uploaded_request.cert_error().Get(1))); |
| 148 EXPECT_EQ(kNumCertErrors, reported_errors.size()); |
| 149 EXPECT_EQ(1u, reported_errors.count(kFirstReportedCertError)); |
| 150 EXPECT_EQ(1u, reported_errors.count(kSecondReportedCertError)); |
| 128 } | 151 } |
| 129 | 152 |
| 130 // A network delegate that lets tests check that a certificate error | 153 // A network delegate that lets tests check that a certificate error |
| 131 // report was sent. It counts the number of requests and lets tests | 154 // 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 | 155 // register a callback to run when the request is destroyed. It also |
| 133 // checks that the uploaded data is as expected (a report for | 156 // checks that the uploaded data is as expected (a report for |
| 134 // |kHostname| and |GetTestSSLInfo()|). | 157 // |kHostname| and |GetTestSSLInfo()|). |
| 135 class TestCertificateErrorReporterNetworkDelegate : public NetworkDelegateImpl { | 158 class TestCertificateErrorReporterNetworkDelegate : public NetworkDelegateImpl { |
| 136 public: | 159 public: |
| 137 TestCertificateErrorReporterNetworkDelegate() | 160 TestCertificateErrorReporterNetworkDelegate() |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 404 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
| 382 CertificateErrorReporter reporter( | 405 CertificateErrorReporter reporter( |
| 383 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); | 406 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); |
| 384 | 407 |
| 385 network_delegate()->set_expect_cookies(false); | 408 network_delegate()->set_expect_cookies(false); |
| 386 SendReport(&reporter, network_delegate(), kHostname, url, 0, | 409 SendReport(&reporter, network_delegate(), kHostname, url, 0, |
| 387 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); | 410 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); |
| 388 } | 411 } |
| 389 | 412 |
| 390 } // namespace | 413 } // namespace |
| OLD | NEW |