| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/test_server.h" | 5 #include "net/test/test_server.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 | 12 |
| 13 #if defined(OS_MACOSX) | 13 #if defined(OS_MACOSX) |
| 14 #include "net/base/x509_certificate.h" | 14 #include "net/base/x509_certificate.h" |
| 15 #endif | 15 #endif |
| 16 | 16 |
| 17 #include "base/base64.h" | 17 #include "base/base64.h" |
| 18 #include "base/command_line.h" | 18 #include "base/command_line.h" |
| 19 #include "base/debug/leak_annotations.h" | 19 #include "base/debug/leak_annotations.h" |
| 20 #include "base/file_util.h" | 20 #include "base/file_util.h" |
| 21 #include "base/logging.h" | 21 #include "base/logging.h" |
| 22 #include "base/path_service.h" | 22 #include "base/path_service.h" |
| 23 #include "base/string_number_conversions.h" | 23 #include "base/string_number_conversions.h" |
| 24 #include "base/utf_string_conversions.h" | 24 #include "base/utf_string_conversions.h" |
| 25 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
| 26 #include "net/base/cert_test_util.h" | |
| 27 #include "net/base/host_port_pair.h" | 26 #include "net/base/host_port_pair.h" |
| 28 #include "net/base/host_resolver.h" | 27 #include "net/base/host_resolver.h" |
| 29 #include "net/base/test_completion_callback.h" | 28 #include "net/base/test_completion_callback.h" |
| 29 #include "net/base/test_root_certs.h" |
| 30 #include "net/socket/tcp_client_socket.h" | 30 #include "net/socket/tcp_client_socket.h" |
| 31 #include "net/test/python_utils.h" | 31 #include "net/test/python_utils.h" |
| 32 #include "testing/platform_test.h" | 32 #include "testing/platform_test.h" |
| 33 | 33 |
| 34 namespace net { | 34 namespace net { |
| 35 | 35 |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 // Number of connection attempts for tests. | 38 // Number of connection attempts for tests. |
| 39 const int kServerConnectionAttempts = 10; | 39 const int kServerConnectionAttempts = 10; |
| 40 | 40 |
| 41 // Connection timeout in milliseconds for tests. | 41 // Connection timeout in milliseconds for tests. |
| 42 const int kServerConnectionTimeoutMs = 1000; | 42 const int kServerConnectionTimeoutMs = 1000; |
| 43 | 43 |
| 44 std::string GetHostname(TestServer::Type type, | 44 std::string GetHostname(TestServer::Type type, |
| 45 const TestServer::HTTPSOptions& options) { | 45 const TestServer::HTTPSOptions& options) { |
| 46 if (type == TestServer::TYPE_HTTPS && | 46 if (type == TestServer::TYPE_HTTPS && |
| 47 options.server_certificate == | 47 options.server_certificate == |
| 48 TestServer::HTTPSOptions::CERT_MISMATCHED_NAME) { | 48 TestServer::HTTPSOptions::CERT_MISMATCHED_NAME) { |
| 49 // Return a different hostname string that resolves to the same hostname. | 49 // Return a different hostname string that resolves to the same hostname. |
| 50 return "localhost"; | 50 return "localhost"; |
| 51 } | 51 } |
| 52 | 52 |
| 53 return "127.0.0.1"; | 53 return "127.0.0.1"; |
| 54 } | 54 } |
| 55 | 55 |
| 56 } // namespace | 56 } // namespace |
| 57 | 57 |
| 58 #if defined(OS_MACOSX) | |
| 59 void SetMacTestCertificate(X509Certificate* cert); | |
| 60 #endif | |
| 61 | |
| 62 TestServer::HTTPSOptions::HTTPSOptions() | 58 TestServer::HTTPSOptions::HTTPSOptions() |
| 63 : server_certificate(CERT_OK), | 59 : server_certificate(CERT_OK), |
| 64 request_client_certificate(false), | 60 request_client_certificate(false), |
| 65 bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY) {} | 61 bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY) {} |
| 66 | 62 |
| 67 TestServer::HTTPSOptions::HTTPSOptions( | 63 TestServer::HTTPSOptions::HTTPSOptions( |
| 68 TestServer::HTTPSOptions::ServerCertificate cert) | 64 TestServer::HTTPSOptions::ServerCertificate cert) |
| 69 : server_certificate(cert), | 65 : server_certificate(cert), |
| 70 request_client_certificate(false), | 66 request_client_certificate(false), |
| 71 bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY) {} | 67 bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY) {} |
| (...skipping 21 matching lines...) Expand all Loading... |
| 93 | 89 |
| 94 TestServer::TestServer(const HTTPSOptions& https_options, | 90 TestServer::TestServer(const HTTPSOptions& https_options, |
| 95 const FilePath& document_root) | 91 const FilePath& document_root) |
| 96 : https_options_(https_options), | 92 : https_options_(https_options), |
| 97 type_(TYPE_HTTPS), | 93 type_(TYPE_HTTPS), |
| 98 started_(false) { | 94 started_(false) { |
| 99 Init(document_root); | 95 Init(document_root); |
| 100 } | 96 } |
| 101 | 97 |
| 102 TestServer::~TestServer() { | 98 TestServer::~TestServer() { |
| 103 #if defined(OS_MACOSX) | 99 TestRootCerts* root_certs = TestRootCerts::GetInstance(); |
| 104 SetMacTestCertificate(NULL); | 100 root_certs->Clear(); |
| 105 #endif | |
| 106 Stop(); | 101 Stop(); |
| 107 } | 102 } |
| 108 | 103 |
| 109 void TestServer::Init(const FilePath& document_root) { | 104 void TestServer::Init(const FilePath& document_root) { |
| 110 // At this point, the port that the testserver will listen on is unknown. | 105 // At this point, the port that the testserver will listen on is unknown. |
| 111 // The testserver will listen on an ephemeral port, and write the port | 106 // The testserver will listen on an ephemeral port, and write the port |
| 112 // number out over a pipe that this TestServer object will read from. Once | 107 // number out over a pipe that this TestServer object will read from. Once |
| 113 // that is complete, the host_port_pair_ will contain the actual port. | 108 // that is complete, the host_port_pair_ will contain the actual port. |
| 114 host_port_pair_ = HostPortPair(GetHostname(type_, https_options_), 0); | 109 host_port_pair_ = HostPortPair(GetHostname(type_, https_options_), 0); |
| 115 process_handle_ = base::kNullProcessHandle; | 110 process_handle_ = base::kNullProcessHandle; |
| 116 | 111 |
| 117 FilePath src_dir; | 112 FilePath src_dir; |
| 118 PathService::Get(base::DIR_SOURCE_ROOT, &src_dir); | 113 PathService::Get(base::DIR_SOURCE_ROOT, &src_dir); |
| 119 | 114 |
| 120 document_root_ = src_dir.Append(document_root); | 115 document_root_ = src_dir.Append(document_root); |
| 121 | 116 |
| 122 certificates_dir_ = src_dir.Append(FILE_PATH_LITERAL("net")) | 117 certificates_dir_ = src_dir.Append(FILE_PATH_LITERAL("net")) |
| 123 .Append(FILE_PATH_LITERAL("data")) | 118 .Append(FILE_PATH_LITERAL("data")) |
| 124 .Append(FILE_PATH_LITERAL("ssl")) | 119 .Append(FILE_PATH_LITERAL("ssl")) |
| 125 .Append(FILE_PATH_LITERAL("certificates")); | 120 .Append(FILE_PATH_LITERAL("certificates")); |
| 126 } | 121 } |
| 127 | 122 |
| 128 bool TestServer::Start() { | 123 bool TestServer::Start() { |
| 129 if (type_ == TYPE_HTTPS) { | 124 if (type_ == TYPE_HTTPS) { |
| 130 if (!LoadTestRootCert()) | 125 if (!LoadTestRootCert()) |
| 131 return false; | 126 return false; |
| 132 if (!CheckCATrusted()) | |
| 133 return false; | |
| 134 } | 127 } |
| 135 | 128 |
| 136 // Get path to python server script | 129 // Get path to python server script |
| 137 FilePath testserver_path; | 130 FilePath testserver_path; |
| 138 if (!PathService::Get(base::DIR_SOURCE_ROOT, &testserver_path)) { | 131 if (!PathService::Get(base::DIR_SOURCE_ROOT, &testserver_path)) { |
| 139 LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT"; | 132 LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT"; |
| 140 return false; | 133 return false; |
| 141 } | 134 } |
| 142 testserver_path = testserver_path | 135 testserver_path = testserver_path |
| 143 .Append(FILE_PATH_LITERAL("net")) | 136 .Append(FILE_PATH_LITERAL("net")) |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 FILE_PATH_LITERAL("device_management_pb"))); | 287 FILE_PATH_LITERAL("device_management_pb"))); |
| 295 | 288 |
| 296 return true; | 289 return true; |
| 297 } | 290 } |
| 298 | 291 |
| 299 FilePath TestServer::GetRootCertificatePath() { | 292 FilePath TestServer::GetRootCertificatePath() { |
| 300 return certificates_dir_.AppendASCII("root_ca_cert.crt"); | 293 return certificates_dir_.AppendASCII("root_ca_cert.crt"); |
| 301 } | 294 } |
| 302 | 295 |
| 303 bool TestServer::LoadTestRootCert() { | 296 bool TestServer::LoadTestRootCert() { |
| 304 #if defined(USE_NSS) | 297 TestRootCerts* root_certs = TestRootCerts::GetInstance(); |
| 305 if (cert_) | 298 return root_certs->AddFromFile(GetRootCertificatePath()); |
| 306 return true; | |
| 307 | |
| 308 // TODO(dkegel): figure out how to get this to only happen once? | |
| 309 | |
| 310 // This currently leaks a little memory. | |
| 311 // TODO(dkegel): fix the leak and remove the entry in | |
| 312 // tools/valgrind/memcheck/suppressions.txt | |
| 313 ANNOTATE_SCOPED_MEMORY_LEAK; // Tell heap checker about the leak. | |
| 314 cert_ = LoadTemporaryRootCert(GetRootCertificatePath()); | |
| 315 return (cert_ != NULL); | |
| 316 #elif defined(OS_MACOSX) | |
| 317 X509Certificate* cert = LoadTemporaryRootCert(GetRootCertificatePath()); | |
| 318 if (!cert) | |
| 319 return false; | |
| 320 SetMacTestCertificate(cert); | |
| 321 return true; | |
| 322 #else | |
| 323 return true; | |
| 324 #endif | |
| 325 } | 299 } |
| 326 | 300 |
| 327 bool TestServer::AddCommandLineArguments(CommandLine* command_line) const { | 301 bool TestServer::AddCommandLineArguments(CommandLine* command_line) const { |
| 328 command_line->AppendSwitchASCII("port", | 302 command_line->AppendSwitchASCII("port", |
| 329 base::IntToString(host_port_pair_.port())); | 303 base::IntToString(host_port_pair_.port())); |
| 330 command_line->AppendSwitchPath("data-dir", document_root_); | 304 command_line->AppendSwitchPath("data-dir", document_root_); |
| 331 | 305 |
| 332 if (type_ == TYPE_FTP) { | 306 if (type_ == TYPE_FTP) { |
| 333 command_line->AppendArg("-f"); | 307 command_line->AppendArg("-f"); |
| 334 } else if (type_ == TYPE_SYNC) { | 308 } else if (type_ == TYPE_SYNC) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_AES256) | 341 if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_AES256) |
| 368 command_line->AppendSwitchASCII(kBulkCipherSwitch, "aes256"); | 342 command_line->AppendSwitchASCII(kBulkCipherSwitch, "aes256"); |
| 369 if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_3DES) | 343 if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_3DES) |
| 370 command_line->AppendSwitchASCII(kBulkCipherSwitch, "3des"); | 344 command_line->AppendSwitchASCII(kBulkCipherSwitch, "3des"); |
| 371 } | 345 } |
| 372 | 346 |
| 373 return true; | 347 return true; |
| 374 } | 348 } |
| 375 | 349 |
| 376 } // namespace net | 350 } // namespace net |
| OLD | NEW |