| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/base_test_server.h" | 5 #include "net/test/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" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "net/base/net_log.h" | 21 #include "net/base/net_log.h" |
| 22 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
| 23 #include "net/base/test_completion_callback.h" | 23 #include "net/base/test_completion_callback.h" |
| 24 #include "net/base/test_root_certs.h" | 24 #include "net/base/test_root_certs.h" |
| 25 | 25 |
| 26 namespace net { | 26 namespace net { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 std::string GetHostname(BaseTestServer::Type type, | 30 std::string GetHostname(BaseTestServer::Type type, |
| 31 const BaseTestServer::HTTPSOptions& options) { | 31 const BaseTestServer::SSLOptions& options) { |
| 32 if (type == BaseTestServer::TYPE_HTTPS && | 32 if ((type == BaseTestServer::TYPE_HTTPS || |
| 33 type == BaseTestServer::TYPE_WSS) && |
| 33 options.server_certificate == | 34 options.server_certificate == |
| 34 BaseTestServer::HTTPSOptions::CERT_MISMATCHED_NAME) { | 35 BaseTestServer::SSLOptions::CERT_MISMATCHED_NAME) { |
| 35 // Return a different hostname string that resolves to the same hostname. | 36 // Return a different hostname string that resolves to the same hostname. |
| 36 return "localhost"; | 37 return "localhost"; |
| 37 } | 38 } |
| 38 | 39 |
| 39 // Use the 127.0.0.1 as default. | 40 // Use the 127.0.0.1 as default. |
| 40 return BaseTestServer::kLocalhost; | 41 return BaseTestServer::kLocalhost; |
| 41 } | 42 } |
| 42 | 43 |
| 43 void GetCiphersList(int cipher, base::ListValue* values) { | 44 void GetCiphersList(int cipher, base::ListValue* values) { |
| 44 if (cipher & BaseTestServer::HTTPSOptions::BULK_CIPHER_RC4) | 45 if (cipher & BaseTestServer::SSLOptions::BULK_CIPHER_RC4) |
| 45 values->Append(base::Value::CreateStringValue("rc4")); | 46 values->Append(base::Value::CreateStringValue("rc4")); |
| 46 if (cipher & BaseTestServer::HTTPSOptions::BULK_CIPHER_AES128) | 47 if (cipher & BaseTestServer::SSLOptions::BULK_CIPHER_AES128) |
| 47 values->Append(base::Value::CreateStringValue("aes128")); | 48 values->Append(base::Value::CreateStringValue("aes128")); |
| 48 if (cipher & BaseTestServer::HTTPSOptions::BULK_CIPHER_AES256) | 49 if (cipher & BaseTestServer::SSLOptions::BULK_CIPHER_AES256) |
| 49 values->Append(base::Value::CreateStringValue("aes256")); | 50 values->Append(base::Value::CreateStringValue("aes256")); |
| 50 if (cipher & BaseTestServer::HTTPSOptions::BULK_CIPHER_3DES) | 51 if (cipher & BaseTestServer::SSLOptions::BULK_CIPHER_3DES) |
| 51 values->Append(base::Value::CreateStringValue("3des")); | 52 values->Append(base::Value::CreateStringValue("3des")); |
| 52 } | 53 } |
| 53 | 54 |
| 54 } // namespace | 55 } // namespace |
| 55 | 56 |
| 56 BaseTestServer::HTTPSOptions::HTTPSOptions() | 57 BaseTestServer::SSLOptions::SSLOptions() |
| 57 : server_certificate(CERT_OK), | 58 : server_certificate(CERT_OK), |
| 58 ocsp_status(OCSP_OK), | 59 ocsp_status(OCSP_OK), |
| 59 request_client_certificate(false), | 60 request_client_certificate(false), |
| 60 bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY), | 61 bulk_ciphers(SSLOptions::BULK_CIPHER_ANY), |
| 61 record_resume(false), | 62 record_resume(false), |
| 62 tls_intolerant(TLS_INTOLERANT_NONE) {} | 63 tls_intolerant(TLS_INTOLERANT_NONE) {} |
| 63 | 64 |
| 64 BaseTestServer::HTTPSOptions::HTTPSOptions( | 65 BaseTestServer::SSLOptions::SSLOptions( |
| 65 BaseTestServer::HTTPSOptions::ServerCertificate cert) | 66 BaseTestServer::SSLOptions::ServerCertificate cert) |
| 66 : server_certificate(cert), | 67 : server_certificate(cert), |
| 67 request_client_certificate(false), | 68 request_client_certificate(false), |
| 68 bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY), | 69 bulk_ciphers(SSLOptions::BULK_CIPHER_ANY), |
| 69 record_resume(false), | 70 record_resume(false), |
| 70 tls_intolerant(TLS_INTOLERANT_NONE) {} | 71 tls_intolerant(TLS_INTOLERANT_NONE) {} |
| 71 | 72 |
| 72 BaseTestServer::HTTPSOptions::~HTTPSOptions() {} | 73 BaseTestServer::SSLOptions::~SSLOptions() {} |
| 73 | 74 |
| 74 FilePath BaseTestServer::HTTPSOptions::GetCertificateFile() const { | 75 FilePath BaseTestServer::SSLOptions::GetCertificateFile() const { |
| 75 switch (server_certificate) { | 76 switch (server_certificate) { |
| 76 case CERT_OK: | 77 case CERT_OK: |
| 77 case CERT_MISMATCHED_NAME: | 78 case CERT_MISMATCHED_NAME: |
| 78 return FilePath(FILE_PATH_LITERAL("ok_cert.pem")); | 79 return FilePath(FILE_PATH_LITERAL("ok_cert.pem")); |
| 79 case CERT_EXPIRED: | 80 case CERT_EXPIRED: |
| 80 return FilePath(FILE_PATH_LITERAL("expired_cert.pem")); | 81 return FilePath(FILE_PATH_LITERAL("expired_cert.pem")); |
| 81 case CERT_CHAIN_WRONG_ROOT: | 82 case CERT_CHAIN_WRONG_ROOT: |
| 82 // This chain uses its own dedicated test root certificate to avoid | 83 // This chain uses its own dedicated test root certificate to avoid |
| 83 // side-effects that may affect testing. | 84 // side-effects that may affect testing. |
| 84 return FilePath(FILE_PATH_LITERAL("redundant-server-chain.pem")); | 85 return FilePath(FILE_PATH_LITERAL("redundant-server-chain.pem")); |
| 85 case CERT_AUTO: | 86 case CERT_AUTO: |
| 86 return FilePath(); | 87 return FilePath(); |
| 87 default: | 88 default: |
| 88 NOTREACHED(); | 89 NOTREACHED(); |
| 89 } | 90 } |
| 90 return FilePath(); | 91 return FilePath(); |
| 91 } | 92 } |
| 92 | 93 |
| 93 std::string BaseTestServer::HTTPSOptions::GetOCSPArgument() const { | 94 std::string BaseTestServer::SSLOptions::GetOCSPArgument() const { |
| 94 if (server_certificate != CERT_AUTO) | 95 if (server_certificate != CERT_AUTO) |
| 95 return ""; | 96 return ""; |
| 96 | 97 |
| 97 switch (ocsp_status) { | 98 switch (ocsp_status) { |
| 98 case OCSP_OK: | 99 case OCSP_OK: |
| 99 return "ok"; | 100 return "ok"; |
| 100 case OCSP_REVOKED: | 101 case OCSP_REVOKED: |
| 101 return "revoked"; | 102 return "revoked"; |
| 102 case OCSP_INVALID: | 103 case OCSP_INVALID: |
| 103 return "invalid"; | 104 return "invalid"; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 114 const char BaseTestServer::kLocalhost[] = "127.0.0.1"; | 115 const char BaseTestServer::kLocalhost[] = "127.0.0.1"; |
| 115 const char BaseTestServer::kGDataAuthToken[] = "testtoken"; | 116 const char BaseTestServer::kGDataAuthToken[] = "testtoken"; |
| 116 | 117 |
| 117 BaseTestServer::BaseTestServer(Type type, const std::string& host) | 118 BaseTestServer::BaseTestServer(Type type, const std::string& host) |
| 118 : type_(type), | 119 : type_(type), |
| 119 started_(false), | 120 started_(false), |
| 120 log_to_console_(false) { | 121 log_to_console_(false) { |
| 121 Init(host); | 122 Init(host); |
| 122 } | 123 } |
| 123 | 124 |
| 124 BaseTestServer::BaseTestServer(const HTTPSOptions& https_options) | 125 BaseTestServer::BaseTestServer(Type type, const SSLOptions& ssl_options) |
| 125 : https_options_(https_options), | 126 : ssl_options_(ssl_options), |
| 126 type_(TYPE_HTTPS), | 127 type_(type), |
| 127 started_(false), | 128 started_(false), |
| 128 log_to_console_(false) { | 129 log_to_console_(false) { |
| 129 Init(GetHostname(TYPE_HTTPS, https_options)); | 130 DCHECK(type == TYPE_HTTPS || type == TYPE_WSS); |
| 131 Init(GetHostname(type, ssl_options)); |
| 130 } | 132 } |
| 131 | 133 |
| 132 BaseTestServer::~BaseTestServer() {} | 134 BaseTestServer::~BaseTestServer() {} |
| 133 | 135 |
| 134 const HostPortPair& BaseTestServer::host_port_pair() const { | 136 const HostPortPair& BaseTestServer::host_port_pair() const { |
| 135 DCHECK(started_); | 137 DCHECK(started_); |
| 136 return host_port_pair_; | 138 return host_port_pair_; |
| 137 } | 139 } |
| 138 | 140 |
| 139 const DictionaryValue& BaseTestServer::server_data() const { | 141 const DictionaryValue& BaseTestServer::server_data() const { |
| 140 DCHECK(started_); | 142 DCHECK(started_); |
| 141 DCHECK(server_data_.get()); | 143 DCHECK(server_data_.get()); |
| 142 return *server_data_; | 144 return *server_data_; |
| 143 } | 145 } |
| 144 | 146 |
| 145 std::string BaseTestServer::GetScheme() const { | 147 std::string BaseTestServer::GetScheme() const { |
| 146 switch (type_) { | 148 switch (type_) { |
| 147 case TYPE_FTP: | 149 case TYPE_FTP: |
| 148 return "ftp"; | 150 return "ftp"; |
| 149 case TYPE_GDATA: | 151 case TYPE_GDATA: |
| 150 case TYPE_HTTP: | 152 case TYPE_HTTP: |
| 151 case TYPE_SYNC: | 153 case TYPE_SYNC: |
| 152 return "http"; | 154 return "http"; |
| 153 case TYPE_HTTPS: | 155 case TYPE_HTTPS: |
| 154 return "https"; | 156 return "https"; |
| 157 case TYPE_WS: |
| 158 return "ws"; |
| 159 case TYPE_WSS: |
| 160 return "wss"; |
| 155 case TYPE_TCP_ECHO: | 161 case TYPE_TCP_ECHO: |
| 156 case TYPE_UDP_ECHO: | 162 case TYPE_UDP_ECHO: |
| 157 default: | 163 default: |
| 158 NOTREACHED(); | 164 NOTREACHED(); |
| 159 } | 165 } |
| 160 return std::string(); | 166 return std::string(); |
| 161 } | 167 } |
| 162 | 168 |
| 163 bool BaseTestServer::GetAddressList(AddressList* address_list) const { | 169 bool BaseTestServer::GetAddressList(AddressList* address_list) const { |
| 164 DCHECK(address_list); | 170 DCHECK(address_list); |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 root_certificate_path = src_dir.Append(certificates_dir_); | 302 root_certificate_path = src_dir.Append(certificates_dir_); |
| 297 } | 303 } |
| 298 | 304 |
| 299 return root_certs->AddFromFile( | 305 return root_certs->AddFromFile( |
| 300 root_certificate_path.AppendASCII("root_ca_cert.crt")); | 306 root_certificate_path.AppendASCII("root_ca_cert.crt")); |
| 301 } | 307 } |
| 302 | 308 |
| 303 bool BaseTestServer::SetupWhenServerStarted() { | 309 bool BaseTestServer::SetupWhenServerStarted() { |
| 304 DCHECK(host_port_pair_.port()); | 310 DCHECK(host_port_pair_.port()); |
| 305 | 311 |
| 306 if (type_ == TYPE_HTTPS && !LoadTestRootCert()) | 312 if ((type_ == TYPE_HTTPS || type_ == TYPE_WSS) && !LoadTestRootCert()) |
| 307 return false; | 313 return false; |
| 308 | 314 |
| 309 started_ = true; | 315 started_ = true; |
| 310 allowed_port_.reset(new ScopedPortException(host_port_pair_.port())); | 316 allowed_port_.reset(new ScopedPortException(host_port_pair_.port())); |
| 311 return true; | 317 return true; |
| 312 } | 318 } |
| 313 | 319 |
| 314 void BaseTestServer::CleanUpWhenStoppingServer() { | 320 void BaseTestServer::CleanUpWhenStoppingServer() { |
| 315 TestRootCerts* root_certs = TestRootCerts::GetInstance(); | 321 TestRootCerts* root_certs = TestRootCerts::GetInstance(); |
| 316 root_certs->Clear(); | 322 root_certs->Clear(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 332 arguments->SetString("data-dir", document_root_.value()); | 338 arguments->SetString("data-dir", document_root_.value()); |
| 333 | 339 |
| 334 if (VLOG_IS_ON(1) || log_to_console_) | 340 if (VLOG_IS_ON(1) || log_to_console_) |
| 335 arguments->Set("log-to-console", base::Value::CreateNullValue()); | 341 arguments->Set("log-to-console", base::Value::CreateNullValue()); |
| 336 | 342 |
| 337 if (type_ == TYPE_HTTPS) { | 343 if (type_ == TYPE_HTTPS) { |
| 338 arguments->Set("https", base::Value::CreateNullValue()); | 344 arguments->Set("https", base::Value::CreateNullValue()); |
| 339 | 345 |
| 340 // Check the certificate arguments of the HTTPS server. | 346 // Check the certificate arguments of the HTTPS server. |
| 341 FilePath certificate_path(certificates_dir_); | 347 FilePath certificate_path(certificates_dir_); |
| 342 FilePath certificate_file(https_options_.GetCertificateFile()); | 348 FilePath certificate_file(ssl_options_.GetCertificateFile()); |
| 343 if (!certificate_file.value().empty()) { | 349 if (!certificate_file.value().empty()) { |
| 344 certificate_path = certificate_path.Append(certificate_file); | 350 certificate_path = certificate_path.Append(certificate_file); |
| 345 if (certificate_path.IsAbsolute() && | 351 if (certificate_path.IsAbsolute() && |
| 346 !file_util::PathExists(certificate_path)) { | 352 !file_util::PathExists(certificate_path)) { |
| 347 LOG(ERROR) << "Certificate path " << certificate_path.value() | 353 LOG(ERROR) << "Certificate path " << certificate_path.value() |
| 348 << " doesn't exist. Can't launch https server."; | 354 << " doesn't exist. Can't launch https server."; |
| 349 return false; | 355 return false; |
| 350 } | 356 } |
| 351 arguments->SetString("cert-and-key-file", certificate_path.value()); | 357 arguments->SetString("cert-and-key-file", certificate_path.value()); |
| 352 } | 358 } |
| 353 | 359 |
| 354 std::string ocsp_arg = https_options_.GetOCSPArgument(); | 360 std::string ocsp_arg = ssl_options_.GetOCSPArgument(); |
| 355 if (!ocsp_arg.empty()) | 361 if (!ocsp_arg.empty()) |
| 356 arguments->SetString("ocsp", ocsp_arg); | 362 arguments->SetString("ocsp", ocsp_arg); |
| 357 | 363 |
| 358 // Check the client certificate related arguments. | 364 // Check the client certificate related arguments. |
| 359 if (https_options_.request_client_certificate) | 365 if (ssl_options_.request_client_certificate) |
| 360 arguments->Set("ssl-client-auth", base::Value::CreateNullValue()); | 366 arguments->Set("ssl-client-auth", base::Value::CreateNullValue()); |
| 361 scoped_ptr<base::ListValue> ssl_client_certs(new base::ListValue()); | 367 scoped_ptr<base::ListValue> ssl_client_certs(new base::ListValue()); |
| 362 | 368 |
| 363 std::vector<FilePath>::const_iterator it; | 369 std::vector<FilePath>::const_iterator it; |
| 364 for (it = https_options_.client_authorities.begin(); | 370 for (it = ssl_options_.client_authorities.begin(); |
| 365 it != https_options_.client_authorities.end(); ++it) { | 371 it != ssl_options_.client_authorities.end(); ++it) { |
| 366 if (it->IsAbsolute() && !file_util::PathExists(*it)) { | 372 if (it->IsAbsolute() && !file_util::PathExists(*it)) { |
| 367 LOG(ERROR) << "Client authority path " << it->value() | 373 LOG(ERROR) << "Client authority path " << it->value() |
| 368 << " doesn't exist. Can't launch https server."; | 374 << " doesn't exist. Can't launch https server."; |
| 369 return false; | 375 return false; |
| 370 } | 376 } |
| 371 ssl_client_certs->Append(base::Value::CreateStringValue(it->value())); | 377 ssl_client_certs->Append(base::Value::CreateStringValue(it->value())); |
| 372 } | 378 } |
| 373 | 379 |
| 374 if (ssl_client_certs->GetSize()) | 380 if (ssl_client_certs->GetSize()) |
| 375 arguments->Set("ssl-client-ca", ssl_client_certs.release()); | 381 arguments->Set("ssl-client-ca", ssl_client_certs.release()); |
| 376 | 382 |
| 377 // Check bulk cipher argument. | 383 // Check bulk cipher argument. |
| 378 scoped_ptr<base::ListValue> bulk_cipher_values(new base::ListValue()); | 384 scoped_ptr<base::ListValue> bulk_cipher_values(new base::ListValue()); |
| 379 GetCiphersList(https_options_.bulk_ciphers, bulk_cipher_values.get()); | 385 GetCiphersList(ssl_options_.bulk_ciphers, bulk_cipher_values.get()); |
| 380 if (bulk_cipher_values->GetSize()) | 386 if (bulk_cipher_values->GetSize()) |
| 381 arguments->Set("ssl-bulk-cipher", bulk_cipher_values.release()); | 387 arguments->Set("ssl-bulk-cipher", bulk_cipher_values.release()); |
| 382 if (https_options_.record_resume) | 388 if (ssl_options_.record_resume) |
| 383 arguments->Set("https-record-resume", base::Value::CreateNullValue()); | 389 arguments->Set("https-record-resume", base::Value::CreateNullValue()); |
| 384 if (https_options_.tls_intolerant != HTTPSOptions::TLS_INTOLERANT_NONE) { | 390 if (ssl_options_.tls_intolerant != SSLOptions::TLS_INTOLERANT_NONE) { |
| 385 arguments->Set("tls-intolerant", | 391 arguments->Set("tls-intolerant", |
| 386 base::Value::CreateIntegerValue(https_options_.tls_intolerant)); | 392 base::Value::CreateIntegerValue(ssl_options_.tls_intolerant)); |
| 387 } | 393 } |
| 388 } | 394 } |
| 389 return true; | 395 return true; |
| 390 } | 396 } |
| 391 | 397 |
| 392 } // namespace net | 398 } // namespace net |
| OLD | NEW |