| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "net/test/spawned_test_server/base_test_server.h" | 5 #include "net/test/spawned_test_server/base_test_server.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "net/base/address_list.h" | 16 #include "net/base/address_list.h" |
| 17 #include "net/base/host_port_pair.h" | 17 #include "net/base/host_port_pair.h" |
| 18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "net/base/port_util.h" | 19 #include "net/base/port_util.h" |
| 20 #include "net/base/test_completion_callback.h" | 20 #include "net/base/test_completion_callback.h" |
| 21 #include "net/cert/test_root_certs.h" | 21 #include "net/cert/test_root_certs.h" |
| 22 #include "net/cert/x509_certificate.h" |
| 22 #include "net/dns/host_resolver.h" | 23 #include "net/dns/host_resolver.h" |
| 23 #include "net/log/net_log.h" | 24 #include "net/log/net_log.h" |
| 24 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 25 | 26 |
| 26 namespace net { | 27 namespace net { |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 std::string GetHostname(BaseTestServer::Type type, | 31 std::string GetHostname(BaseTestServer::Type type, |
| 31 const BaseTestServer::SSLOptions& options) { | 32 const BaseTestServer::SSLOptions& options) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 case BaseTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE: | 89 case BaseTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE: |
| 89 return new base::StringValue("close"); | 90 return new base::StringValue("close"); |
| 90 case BaseTestServer::SSLOptions::TLS_INTOLERANCE_RESET: | 91 case BaseTestServer::SSLOptions::TLS_INTOLERANCE_RESET: |
| 91 return new base::StringValue("reset"); | 92 return new base::StringValue("reset"); |
| 92 default: | 93 default: |
| 93 NOTREACHED(); | 94 NOTREACHED(); |
| 94 return new base::StringValue(""); | 95 return new base::StringValue(""); |
| 95 } | 96 } |
| 96 } | 97 } |
| 97 | 98 |
| 99 bool GetLocalCertificatesDir(const base::FilePath& certificates_dir, |
| 100 base::FilePath* local_certificates_dir) { |
| 101 if (certificates_dir.IsAbsolute()) { |
| 102 *local_certificates_dir = certificates_dir; |
| 103 return true; |
| 104 } |
| 105 |
| 106 base::FilePath src_dir; |
| 107 if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)) |
| 108 return false; |
| 109 |
| 110 *local_certificates_dir = src_dir.Append(certificates_dir); |
| 111 return true; |
| 112 } |
| 113 |
| 98 } // namespace | 114 } // namespace |
| 99 | 115 |
| 100 BaseTestServer::SSLOptions::SSLOptions() | 116 BaseTestServer::SSLOptions::SSLOptions() |
| 101 : server_certificate(CERT_OK), | 117 : server_certificate(CERT_OK), |
| 102 ocsp_status(OCSP_OK), | 118 ocsp_status(OCSP_OK), |
| 103 cert_serial(0), | 119 cert_serial(0), |
| 104 request_client_certificate(false), | 120 request_client_certificate(false), |
| 105 key_exchanges(SSLOptions::KEY_EXCHANGE_ANY), | 121 key_exchanges(SSLOptions::KEY_EXCHANGE_ANY), |
| 106 bulk_ciphers(SSLOptions::BULK_CIPHER_ANY), | 122 bulk_ciphers(SSLOptions::BULK_CIPHER_ANY), |
| 107 record_resume(false), | 123 record_resume(false), |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 *replacement_path = new_file_path; | 332 *replacement_path = new_file_path; |
| 317 return true; | 333 return true; |
| 318 } | 334 } |
| 319 | 335 |
| 320 bool BaseTestServer::LoadTestRootCert() const { | 336 bool BaseTestServer::LoadTestRootCert() const { |
| 321 TestRootCerts* root_certs = TestRootCerts::GetInstance(); | 337 TestRootCerts* root_certs = TestRootCerts::GetInstance(); |
| 322 if (!root_certs) | 338 if (!root_certs) |
| 323 return false; | 339 return false; |
| 324 | 340 |
| 325 // Should always use absolute path to load the root certificate. | 341 // Should always use absolute path to load the root certificate. |
| 326 base::FilePath root_certificate_path = certificates_dir_; | 342 base::FilePath root_certificate_path; |
| 327 if (!certificates_dir_.IsAbsolute()) { | 343 if (!GetLocalCertificatesDir(certificates_dir_, &root_certificate_path)) |
| 328 base::FilePath src_dir; | 344 return false; |
| 329 if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)) | |
| 330 return false; | |
| 331 root_certificate_path = src_dir.Append(certificates_dir_); | |
| 332 } | |
| 333 | 345 |
| 334 return root_certs->AddFromFile( | 346 return root_certs->AddFromFile( |
| 335 root_certificate_path.AppendASCII("root_ca_cert.pem")); | 347 root_certificate_path.AppendASCII("root_ca_cert.pem")); |
| 336 } | 348 } |
| 337 | 349 |
| 350 scoped_refptr<X509Certificate> BaseTestServer::GetCertificate() const { |
| 351 base::FilePath certificate_path; |
| 352 if (!GetLocalCertificatesDir(certificates_dir_, &certificate_path)) |
| 353 return nullptr; |
| 354 |
| 355 base::FilePath certificate_file(ssl_options_.GetCertificateFile()); |
| 356 if (certificate_file.value().empty()) |
| 357 return nullptr; |
| 358 |
| 359 certificate_path = certificate_path.Append(certificate_file); |
| 360 |
| 361 std::string cert_data; |
| 362 if (!base::ReadFileToString(certificate_path, &cert_data)) |
| 363 return nullptr; |
| 364 |
| 365 CertificateList certs_in_file = |
| 366 X509Certificate::CreateCertificateListFromBytes( |
| 367 cert_data.data(), cert_data.size(), |
| 368 X509Certificate::FORMAT_PEM_CERT_SEQUENCE); |
| 369 if (certs_in_file.empty()) |
| 370 return nullptr; |
| 371 return certs_in_file[0]; |
| 372 } |
| 373 |
| 338 void BaseTestServer::Init(const std::string& host) { | 374 void BaseTestServer::Init(const std::string& host) { |
| 339 host_port_pair_ = HostPortPair(host, 0); | 375 host_port_pair_ = HostPortPair(host, 0); |
| 340 | 376 |
| 341 // TODO(battre) Remove this after figuring out why the TestServer is flaky. | 377 // TODO(battre) Remove this after figuring out why the TestServer is flaky. |
| 342 // http://crbug.com/96594 | 378 // http://crbug.com/96594 |
| 343 log_to_console_ = true; | 379 log_to_console_ = true; |
| 344 } | 380 } |
| 345 | 381 |
| 346 void BaseTestServer::SetResourcePath(const base::FilePath& document_root, | 382 void BaseTestServer::SetResourcePath(const base::FilePath& document_root, |
| 347 const base::FilePath& certificates_dir) { | 383 const base::FilePath& certificates_dir) { |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 | 550 |
| 515 return GenerateAdditionalArguments(arguments); | 551 return GenerateAdditionalArguments(arguments); |
| 516 } | 552 } |
| 517 | 553 |
| 518 bool BaseTestServer::GenerateAdditionalArguments( | 554 bool BaseTestServer::GenerateAdditionalArguments( |
| 519 base::DictionaryValue* arguments) const { | 555 base::DictionaryValue* arguments) const { |
| 520 return true; | 556 return true; |
| 521 } | 557 } |
| 522 | 558 |
| 523 } // namespace net | 559 } // namespace net |
| OLD | NEW |