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