| 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/json/json_reader.h" | 20 #include "base/json/json_reader.h" |
| 21 #include "base/file_util.h" | 21 #include "base/file_util.h" |
| 22 #include "base/logging.h" | 22 #include "base/logging.h" |
| 23 #include "base/path_service.h" | 23 #include "base/path_service.h" |
| 24 #include "base/scoped_ptr.h" | 24 #include "base/scoped_ptr.h" |
| 25 #include "base/string_number_conversions.h" | 25 #include "base/string_number_conversions.h" |
| 26 #include "base/utf_string_conversions.h" | 26 #include "base/utf_string_conversions.h" |
| 27 #include "base/values.h" | 27 #include "base/values.h" |
| 28 #include "googleurl/src/gurl.h" | 28 #include "googleurl/src/gurl.h" |
| 29 #include "net/base/cert_test_util.h" | |
| 30 #include "net/base/host_port_pair.h" | 29 #include "net/base/host_port_pair.h" |
| 31 #include "net/base/host_resolver.h" | 30 #include "net/base/host_resolver.h" |
| 32 #include "net/base/test_completion_callback.h" | 31 #include "net/base/test_completion_callback.h" |
| 32 #include "net/base/test_root_certs.h" |
| 33 #include "net/socket/tcp_client_socket.h" | 33 #include "net/socket/tcp_client_socket.h" |
| 34 #include "net/test/python_utils.h" | 34 #include "net/test/python_utils.h" |
| 35 #include "testing/platform_test.h" | 35 #include "testing/platform_test.h" |
| 36 | 36 |
| 37 namespace net { | 37 namespace net { |
| 38 | 38 |
| 39 namespace { | 39 namespace { |
| 40 | 40 |
| 41 // Number of connection attempts for tests. | 41 // Number of connection attempts for tests. |
| 42 const int kServerConnectionAttempts = 10; | 42 const int kServerConnectionAttempts = 10; |
| 43 | 43 |
| 44 // Connection timeout in milliseconds for tests. | 44 // Connection timeout in milliseconds for tests. |
| 45 const int kServerConnectionTimeoutMs = 1000; | 45 const int kServerConnectionTimeoutMs = 1000; |
| 46 | 46 |
| 47 std::string GetHostname(TestServer::Type type, | 47 std::string GetHostname(TestServer::Type type, |
| 48 const TestServer::HTTPSOptions& options) { | 48 const TestServer::HTTPSOptions& options) { |
| 49 if (type == TestServer::TYPE_HTTPS && | 49 if (type == TestServer::TYPE_HTTPS && |
| 50 options.server_certificate == | 50 options.server_certificate == |
| 51 TestServer::HTTPSOptions::CERT_MISMATCHED_NAME) { | 51 TestServer::HTTPSOptions::CERT_MISMATCHED_NAME) { |
| 52 // Return a different hostname string that resolves to the same hostname. | 52 // Return a different hostname string that resolves to the same hostname. |
| 53 return "localhost"; | 53 return "localhost"; |
| 54 } | 54 } |
| 55 | 55 |
| 56 return "127.0.0.1"; | 56 return "127.0.0.1"; |
| 57 } | 57 } |
| 58 | 58 |
| 59 } // namespace | 59 } // namespace |
| 60 | 60 |
| 61 #if defined(OS_MACOSX) | |
| 62 void SetMacTestCertificate(X509Certificate* cert); | |
| 63 #endif | |
| 64 | |
| 65 TestServer::HTTPSOptions::HTTPSOptions() | 61 TestServer::HTTPSOptions::HTTPSOptions() |
| 66 : server_certificate(CERT_OK), | 62 : server_certificate(CERT_OK), |
| 67 request_client_certificate(false), | 63 request_client_certificate(false), |
| 68 bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY) {} | 64 bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY) {} |
| 69 | 65 |
| 70 TestServer::HTTPSOptions::HTTPSOptions( | 66 TestServer::HTTPSOptions::HTTPSOptions( |
| 71 TestServer::HTTPSOptions::ServerCertificate cert) | 67 TestServer::HTTPSOptions::ServerCertificate cert) |
| 72 : server_certificate(cert), | 68 : server_certificate(cert), |
| 73 request_client_certificate(false), | 69 request_client_certificate(false), |
| 74 bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY) {} | 70 bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY) {} |
| (...skipping 21 matching lines...) Expand all Loading... |
| 96 | 92 |
| 97 TestServer::TestServer(const HTTPSOptions& https_options, | 93 TestServer::TestServer(const HTTPSOptions& https_options, |
| 98 const FilePath& document_root) | 94 const FilePath& document_root) |
| 99 : https_options_(https_options), | 95 : https_options_(https_options), |
| 100 type_(TYPE_HTTPS), | 96 type_(TYPE_HTTPS), |
| 101 started_(false) { | 97 started_(false) { |
| 102 Init(document_root); | 98 Init(document_root); |
| 103 } | 99 } |
| 104 | 100 |
| 105 TestServer::~TestServer() { | 101 TestServer::~TestServer() { |
| 106 #if defined(OS_MACOSX) | 102 TestRootCerts* root_certs = TestRootCerts::GetInstance(); |
| 107 SetMacTestCertificate(NULL); | 103 root_certs->Clear(); |
| 108 #endif | |
| 109 Stop(); | 104 Stop(); |
| 110 } | 105 } |
| 111 | 106 |
| 112 void TestServer::Init(const FilePath& document_root) { | 107 void TestServer::Init(const FilePath& document_root) { |
| 113 // At this point, the port that the testserver will listen on is unknown. | 108 // At this point, the port that the testserver will listen on is unknown. |
| 114 // The testserver will listen on an ephemeral port, and write the port | 109 // The testserver will listen on an ephemeral port, and write the port |
| 115 // number out over a pipe that this TestServer object will read from. Once | 110 // number out over a pipe that this TestServer object will read from. Once |
| 116 // that is complete, the host_port_pair_ will contain the actual port. | 111 // that is complete, the host_port_pair_ will contain the actual port. |
| 117 host_port_pair_ = HostPortPair(GetHostname(type_, https_options_), 0); | 112 host_port_pair_ = HostPortPair(GetHostname(type_, https_options_), 0); |
| 118 process_handle_ = base::kNullProcessHandle; | 113 process_handle_ = base::kNullProcessHandle; |
| 119 | 114 |
| 120 FilePath src_dir; | 115 FilePath src_dir; |
| 121 PathService::Get(base::DIR_SOURCE_ROOT, &src_dir); | 116 PathService::Get(base::DIR_SOURCE_ROOT, &src_dir); |
| 122 | 117 |
| 123 document_root_ = src_dir.Append(document_root); | 118 document_root_ = src_dir.Append(document_root); |
| 124 | 119 |
| 125 certificates_dir_ = src_dir.Append(FILE_PATH_LITERAL("net")) | 120 certificates_dir_ = src_dir.Append(FILE_PATH_LITERAL("net")) |
| 126 .Append(FILE_PATH_LITERAL("data")) | 121 .Append(FILE_PATH_LITERAL("data")) |
| 127 .Append(FILE_PATH_LITERAL("ssl")) | 122 .Append(FILE_PATH_LITERAL("ssl")) |
| 128 .Append(FILE_PATH_LITERAL("certificates")); | 123 .Append(FILE_PATH_LITERAL("certificates")); |
| 129 } | 124 } |
| 130 | 125 |
| 131 bool TestServer::Start() { | 126 bool TestServer::Start() { |
| 132 if (type_ == TYPE_HTTPS) { | 127 if (type_ == TYPE_HTTPS) { |
| 133 if (!LoadTestRootCert()) | 128 if (!LoadTestRootCert()) |
| 134 return false; | 129 return false; |
| 135 if (!CheckCATrusted()) | |
| 136 return false; | |
| 137 } | 130 } |
| 138 | 131 |
| 139 // Get path to python server script | 132 // Get path to python server script |
| 140 FilePath testserver_path; | 133 FilePath testserver_path; |
| 141 if (!PathService::Get(base::DIR_SOURCE_ROOT, &testserver_path)) { | 134 if (!PathService::Get(base::DIR_SOURCE_ROOT, &testserver_path)) { |
| 142 LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT"; | 135 LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT"; |
| 143 return false; | 136 return false; |
| 144 } | 137 } |
| 145 testserver_path = testserver_path | 138 testserver_path = testserver_path |
| 146 .Append(FILE_PATH_LITERAL("net")) | 139 .Append(FILE_PATH_LITERAL("net")) |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 FILE_PATH_LITERAL("device_management_pb"))); | 304 FILE_PATH_LITERAL("device_management_pb"))); |
| 312 | 305 |
| 313 return true; | 306 return true; |
| 314 } | 307 } |
| 315 | 308 |
| 316 FilePath TestServer::GetRootCertificatePath() { | 309 FilePath TestServer::GetRootCertificatePath() { |
| 317 return certificates_dir_.AppendASCII("root_ca_cert.crt"); | 310 return certificates_dir_.AppendASCII("root_ca_cert.crt"); |
| 318 } | 311 } |
| 319 | 312 |
| 320 bool TestServer::LoadTestRootCert() { | 313 bool TestServer::LoadTestRootCert() { |
| 321 #if defined(USE_OPENSSL) || defined(USE_NSS) | 314 TestRootCerts* root_certs = TestRootCerts::GetInstance(); |
| 322 if (cert_) | 315 return root_certs->AddFromFile(GetRootCertificatePath()); |
| 323 return true; | |
| 324 | |
| 325 // TODO(dkegel): figure out how to get this to only happen once? | |
| 326 | |
| 327 // This currently leaks a little memory. | |
| 328 // TODO(dkegel): fix the leak and remove the entry in | |
| 329 // tools/valgrind/memcheck/suppressions.txt | |
| 330 ANNOTATE_SCOPED_MEMORY_LEAK; // Tell heap checker about the leak. | |
| 331 cert_ = LoadTemporaryRootCert(GetRootCertificatePath()); | |
| 332 return (cert_ != NULL); | |
| 333 #elif defined(OS_MACOSX) | |
| 334 X509Certificate* cert = LoadTemporaryRootCert(GetRootCertificatePath()); | |
| 335 if (!cert) | |
| 336 return false; | |
| 337 SetMacTestCertificate(cert); | |
| 338 return true; | |
| 339 #else | |
| 340 return true; | |
| 341 #endif | |
| 342 } | 316 } |
| 343 | 317 |
| 344 bool TestServer::AddCommandLineArguments(CommandLine* command_line) const { | 318 bool TestServer::AddCommandLineArguments(CommandLine* command_line) const { |
| 345 command_line->AppendSwitchASCII("port", | 319 command_line->AppendSwitchASCII("port", |
| 346 base::IntToString(host_port_pair_.port())); | 320 base::IntToString(host_port_pair_.port())); |
| 347 command_line->AppendSwitchPath("data-dir", document_root_); | 321 command_line->AppendSwitchPath("data-dir", document_root_); |
| 348 | 322 |
| 349 if (type_ == TYPE_FTP) { | 323 if (type_ == TYPE_FTP) { |
| 350 command_line->AppendArg("-f"); | 324 command_line->AppendArg("-f"); |
| 351 } else if (type_ == TYPE_SYNC) { | 325 } else if (type_ == TYPE_SYNC) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 } | 382 } |
| 409 if ((port <= 0) || (port > kuint16max)) { | 383 if ((port <= 0) || (port > kuint16max)) { |
| 410 LOG(ERROR) << "Invalid port value: " << port; | 384 LOG(ERROR) << "Invalid port value: " << port; |
| 411 return false; | 385 return false; |
| 412 } | 386 } |
| 413 host_port_pair_.set_port(port); | 387 host_port_pair_.set_port(port); |
| 414 return true; | 388 return true; |
| 415 } | 389 } |
| 416 | 390 |
| 417 } // namespace net | 391 } // namespace net |
| OLD | NEW |