12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "net/test/spawned_test_server/base_test_server.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base64.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/json/json_reader.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/address_list.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/host_port_pair.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_log.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_util.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/test_completion_callback.h"
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/cert/test_root_certs.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/dns/host_resolver.h"
247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string GetHostname(BaseTestServer::Type type,
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        const BaseTestServer::SSLOptions& options) {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (BaseTestServer::UsingSSL(type) &&
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      options.server_certificate ==
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          BaseTestServer::SSLOptions::CERT_MISMATCHED_NAME) {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Return a different hostname string that resolves to the same hostname.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return "localhost";
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use the 127.0.0.1 as default.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return BaseTestServer::kLocalhost;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)std::string GetClientCertType(SSLClientCertType type) {
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  switch (type) {
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case CLIENT_CERT_RSA_SIGN:
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return "rsa_sign";
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case CLIENT_CERT_DSS_SIGN:
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return "dss_sign";
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case CLIENT_CERT_ECDSA_SIGN:
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return "ecdsa_sign";
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    default:
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      NOTREACHED();
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return "";
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid GetKeyExchangesList(int key_exchange, base::ListValue* values) {
580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (key_exchange & BaseTestServer::SSLOptions::KEY_EXCHANGE_RSA)
590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    values->Append(new base::StringValue("rsa"));
600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (key_exchange & BaseTestServer::SSLOptions::KEY_EXCHANGE_DHE_RSA)
610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    values->Append(new base::StringValue("dhe_rsa"));
620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GetCiphersList(int cipher, base::ListValue* values) {
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (cipher & BaseTestServer::SSLOptions::BULK_CIPHER_RC4)
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    values->Append(new base::StringValue("rc4"));
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (cipher & BaseTestServer::SSLOptions::BULK_CIPHER_AES128)
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    values->Append(new base::StringValue("aes128"));
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (cipher & BaseTestServer::SSLOptions::BULK_CIPHER_AES256)
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    values->Append(new base::StringValue("aes256"));
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (cipher & BaseTestServer::SSLOptions::BULK_CIPHER_3DES)
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    values->Append(new base::StringValue("3des"));
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbase::StringValue* GetTLSIntoleranceType(
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    BaseTestServer::SSLOptions::TLSIntoleranceType type) {
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  switch (type) {
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case BaseTestServer::SSLOptions::TLS_INTOLERANCE_ALERT:
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return new base::StringValue("alert");
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case BaseTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE:
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return new base::StringValue("close");
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case BaseTestServer::SSLOptions::TLS_INTOLERANCE_RESET:
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return new base::StringValue("reset");
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    default:
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      NOTREACHED();
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return new base::StringValue("");
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BaseTestServer::SSLOptions::SSLOptions()
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : server_certificate(CERT_OK),
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ocsp_status(OCSP_OK),
95a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      cert_serial(0),
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      request_client_certificate(false),
970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      key_exchanges(SSLOptions::KEY_EXCHANGE_ANY),
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bulk_ciphers(SSLOptions::BULK_CIPHER_ANY),
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      record_resume(false),
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      tls_intolerant(TLS_INTOLERANT_NONE),
101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      tls_intolerance_type(TLS_INTOLERANCE_ALERT),
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      fallback_scsv_enabled(false),
1030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      staple_ocsp_response(false),
1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      enable_npn(false),
1055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      disable_session_cache(false) {
1065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BaseTestServer::SSLOptions::SSLOptions(
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    BaseTestServer::SSLOptions::ServerCertificate cert)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : server_certificate(cert),
111a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      ocsp_status(OCSP_OK),
112a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      cert_serial(0),
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      request_client_certificate(false),
1140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      key_exchanges(SSLOptions::KEY_EXCHANGE_ANY),
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bulk_ciphers(SSLOptions::BULK_CIPHER_ANY),
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      record_resume(false),
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      tls_intolerant(TLS_INTOLERANT_NONE),
118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      tls_intolerance_type(TLS_INTOLERANCE_ALERT),
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      fallback_scsv_enabled(false),
1200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      staple_ocsp_response(false),
1215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      enable_npn(false),
1225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      disable_session_cache(false) {
1235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BaseTestServer::SSLOptions::~SSLOptions() {}
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)base::FilePath BaseTestServer::SSLOptions::GetCertificateFile() const {
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (server_certificate) {
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CERT_OK:
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CERT_MISMATCHED_NAME:
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return base::FilePath(FILE_PATH_LITERAL("ok_cert.pem"));
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CERT_EXPIRED:
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return base::FilePath(FILE_PATH_LITERAL("expired_cert.pem"));
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CERT_CHAIN_WRONG_ROOT:
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // This chain uses its own dedicated test root certificate to avoid
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // side-effects that may affect testing.
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return base::FilePath(FILE_PATH_LITERAL("redundant-server-chain.pem"));
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CERT_AUTO:
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return base::FilePath();
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return base::FilePath();
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string BaseTestServer::SSLOptions::GetOCSPArgument() const {
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (server_certificate != CERT_AUTO)
148c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return std::string();
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (ocsp_status) {
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case OCSP_OK:
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "ok";
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case OCSP_REVOKED:
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "revoked";
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case OCSP_INVALID:
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "invalid";
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case OCSP_UNAUTHORIZED:
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "unauthorized";
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case OCSP_UNKNOWN:
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "unknown";
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return std::string();
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char BaseTestServer::kLocalhost[] = "127.0.0.1";
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BaseTestServer::BaseTestServer(Type type, const std::string& host)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : type_(type),
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      started_(false),
1725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      log_to_console_(false),
1735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      ws_basic_auth_(false) {
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Init(host);
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BaseTestServer::BaseTestServer(Type type, const SSLOptions& ssl_options)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : ssl_options_(ssl_options),
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      type_(type),
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      started_(false),
1815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      log_to_console_(false),
1825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      ws_basic_auth_(false) {
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(UsingSSL(type));
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Init(GetHostname(type, ssl_options));
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BaseTestServer::~BaseTestServer() {}
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const HostPortPair& BaseTestServer::host_port_pair() const {
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(started_);
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return host_port_pair_;
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const base::DictionaryValue& BaseTestServer::server_data() const {
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(started_);
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(server_data_.get());
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return *server_data_;
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string BaseTestServer::GetScheme() const {
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (type_) {
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case TYPE_FTP:
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "ftp";
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case TYPE_HTTP:
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "http";
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case TYPE_HTTPS:
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "https";
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case TYPE_WS:
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "ws";
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case TYPE_WSS:
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "wss";
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case TYPE_TCP_ECHO:
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case TYPE_UDP_ECHO:
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return std::string();
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BaseTestServer::GetAddressList(AddressList* address_list) const {
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(address_list);
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<HostResolver> resolver(HostResolver::CreateDefaultResolver(NULL));
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HostResolver::RequestInfo info(host_port_pair_);
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestCompletionCallback callback;
2263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  int rv = resolver->Resolve(info,
2273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                             DEFAULT_PRIORITY,
2283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                             address_list,
2293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                             callback.callback(),
2303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                             NULL,
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             BoundNetLog());
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (rv == ERR_IO_PENDING)
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    rv = callback.WaitForResult();
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (rv != net::OK) {
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Failed to resolve hostname: " << host_port_pair_.host();
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)uint16 BaseTestServer::GetPort() {
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return host_port_pair_.port();
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void BaseTestServer::SetPort(uint16 port) {
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_port_pair_.set_port(port);
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GURL BaseTestServer::GetURL(const std::string& path) const {
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GURL(GetScheme() + "://" + host_port_pair_.ToString() + "/" + path);
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GURL BaseTestServer::GetURLWithUser(const std::string& path,
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                const std::string& user) const {
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GURL(GetScheme() + "://" + user + "@" + host_port_pair_.ToString() +
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              "/" + path);
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GURL BaseTestServer::GetURLWithUserAndPassword(const std::string& path,
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           const std::string& user,
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           const std::string& password) const {
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GURL(GetScheme() + "://" + user + ":" + password + "@" +
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              host_port_pair_.ToString() + "/" + path);
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BaseTestServer::GetFilePathWithReplacements(
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& original_file_path,
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::vector<StringPair>& text_to_replace,
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string* replacement_path) {
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string new_file_path = original_file_path;
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool first_query_parameter = true;
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::vector<StringPair>::const_iterator end = text_to_replace.end();
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (std::vector<StringPair>::const_iterator it = text_to_replace.begin();
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       it != end;
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       ++it) {
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& old_text = it->first;
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& new_text = it->second;
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string base64_old;
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string base64_new;
281a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::Base64Encode(old_text, &base64_old);
282a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::Base64Encode(new_text, &base64_new);
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (first_query_parameter) {
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_file_path += "?";
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      first_query_parameter = false;
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_file_path += "&";
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_file_path += "replace_text=";
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_file_path += base64_old;
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_file_path += ":";
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_file_path += base64_new;
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *replacement_path = new_file_path;
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void BaseTestServer::Init(const std::string& host) {
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_port_pair_ = HostPortPair(host, 0);
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(battre) Remove this after figuring out why the TestServer is flaky.
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://crbug.com/96594
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  log_to_console_ = true;
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void BaseTestServer::SetResourcePath(const base::FilePath& document_root,
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     const base::FilePath& certificates_dir) {
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This method shouldn't get called twice.
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(certificates_dir_.empty());
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  document_root_ = document_root;
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  certificates_dir_ = certificates_dir;
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!certificates_dir_.empty());
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BaseTestServer::ParseServerData(const std::string& server_data) {
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VLOG(1) << "Server data: " << server_data;
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::JSONReader json_reader;
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<base::Value> value(json_reader.ReadToValue(server_data));
3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!value.get() || !value->IsType(base::Value::TYPE_DICTIONARY)) {
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Could not parse server data: "
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               << json_reader.GetErrorMessage();
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  server_data_.reset(static_cast<base::DictionaryValue*>(value.release()));
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int port = 0;
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!server_data_->GetInteger("port", &port)) {
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Could not find port value";
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if ((port <= 0) || (port > kuint16max)) {
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Invalid port value: " << port;
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_port_pair_.set_port(port);
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BaseTestServer::LoadTestRootCert() const {
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestRootCerts* root_certs = TestRootCerts::GetInstance();
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!root_certs)
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should always use absolute path to load the root certificate.
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath root_certificate_path = certificates_dir_;
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!certificates_dir_.IsAbsolute()) {
3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath src_dir;
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_dir))
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    root_certificate_path = src_dir.Append(certificates_dir_);
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return root_certs->AddFromFile(
356eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      root_certificate_path.AppendASCII("root_ca_cert.pem"));
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BaseTestServer::SetupWhenServerStarted() {
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(host_port_pair_.port());
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (UsingSSL(type_) && !LoadTestRootCert())
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  started_ = true;
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  allowed_port_.reset(new ScopedPortException(host_port_pair_.port()));
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void BaseTestServer::CleanUpWhenStoppingServer() {
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestRootCerts* root_certs = TestRootCerts::GetInstance();
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  root_certs->Clear();
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_port_pair_.set_port(0);
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  allowed_port_.reset();
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  started_ = false;
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Generates a dictionary of arguments to pass to the Python test server via
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the test server spawner, in the form of
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// { argument-name: argument-value, ... }
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns false if an invalid configuration is specified.
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BaseTestServer::GenerateArguments(base::DictionaryValue* arguments) const {
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(arguments);
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  arguments->SetString("host", host_port_pair_.host());
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  arguments->SetInteger("port", host_port_pair_.port());
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  arguments->SetString("data-dir", document_root_.value());
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (VLOG_IS_ON(1) || log_to_console_)
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    arguments->Set("log-to-console", base::Value::CreateNullValue());
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (ws_basic_auth_) {
3945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    DCHECK(type_ == TYPE_WS || type_ == TYPE_WSS);
3955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    arguments->Set("ws-basic-auth", base::Value::CreateNullValue());
3965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
3975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (UsingSSL(type_)) {
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Check the certificate arguments of the HTTPS server.
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath certificate_path(certificates_dir_);
4012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath certificate_file(ssl_options_.GetCertificateFile());
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!certificate_file.value().empty()) {
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      certificate_path = certificate_path.Append(certificate_file);
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (certificate_path.IsAbsolute() &&
4057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch          !base::PathExists(certificate_path)) {
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        LOG(ERROR) << "Certificate path " << certificate_path.value()
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   << " doesn't exist. Can't launch https server.";
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return false;
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      arguments->SetString("cert-and-key-file", certificate_path.value());
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Check the client certificate related arguments.
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (ssl_options_.request_client_certificate)
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      arguments->Set("ssl-client-auth", base::Value::CreateNullValue());
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<base::ListValue> ssl_client_certs(new base::ListValue());
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::vector<base::FilePath>::const_iterator it;
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (it = ssl_options_.client_authorities.begin();
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         it != ssl_options_.client_authorities.end(); ++it) {
4217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (it->IsAbsolute() && !base::PathExists(*it)) {
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        LOG(ERROR) << "Client authority path " << it->value()
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   << " doesn't exist. Can't launch https server.";
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return false;
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ssl_client_certs->Append(new base::StringValue(it->value()));
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (ssl_client_certs->GetSize())
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      arguments->Set("ssl-client-ca", ssl_client_certs.release());
431cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
432cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    scoped_ptr<base::ListValue> client_cert_types(new base::ListValue());
433cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    for (size_t i = 0; i < ssl_options_.client_cert_types.size(); i++) {
434cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      client_cert_types->Append(new base::StringValue(
435cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          GetClientCertType(ssl_options_.client_cert_types[i])));
436cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
437cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (client_cert_types->GetSize())
438cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      arguments->Set("ssl-client-cert-type", client_cert_types.release());
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (type_ == TYPE_HTTPS) {
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    arguments->Set("https", base::Value::CreateNullValue());
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string ocsp_arg = ssl_options_.GetOCSPArgument();
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!ocsp_arg.empty())
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      arguments->SetString("ocsp", ocsp_arg);
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
448a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    if (ssl_options_.cert_serial != 0) {
4495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      arguments->SetInteger("cert-serial", ssl_options_.cert_serial);
450a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    }
451a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
4520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    // Check key exchange argument.
4530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    scoped_ptr<base::ListValue> key_exchange_values(new base::ListValue());
4540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    GetKeyExchangesList(ssl_options_.key_exchanges, key_exchange_values.get());
4550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (key_exchange_values->GetSize())
4560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      arguments->Set("ssl-key-exchange", key_exchange_values.release());
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Check bulk cipher argument.
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<base::ListValue> bulk_cipher_values(new base::ListValue());
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GetCiphersList(ssl_options_.bulk_ciphers, bulk_cipher_values.get());
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (bulk_cipher_values->GetSize())
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      arguments->Set("ssl-bulk-cipher", bulk_cipher_values.release());
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (ssl_options_.record_resume)
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      arguments->Set("https-record-resume", base::Value::CreateNullValue());
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (ssl_options_.tls_intolerant != SSLOptions::TLS_INTOLERANT_NONE) {
4655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      arguments->SetInteger("tls-intolerant", ssl_options_.tls_intolerant);
466116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      arguments->Set("tls-intolerance-type", GetTLSIntoleranceType(
467116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          ssl_options_.tls_intolerance_type));
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (ssl_options_.fallback_scsv_enabled)
4705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      arguments->Set("fallback-scsv", base::Value::CreateNullValue());
4715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!ssl_options_.signed_cert_timestamps_tls_ext.empty()) {
4725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      std::string b64_scts_tls_ext;
4735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Base64Encode(ssl_options_.signed_cert_timestamps_tls_ext,
4745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         &b64_scts_tls_ext);
4755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      arguments->SetString("signed-cert-timestamps-tls-ext", b64_scts_tls_ext);
476a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
4775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (ssl_options_.staple_ocsp_response)
4785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      arguments->Set("staple-ocsp-response", base::Value::CreateNullValue());
4790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (ssl_options_.enable_npn)
4800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      arguments->Set("enable-npn", base::Value::CreateNullValue());
4815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    if (ssl_options_.disable_session_cache)
4825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      arguments->Set("disable-session-cache", base::Value::CreateNullValue());
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GenerateAdditionalArguments(arguments);
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BaseTestServer::GenerateAdditionalArguments(
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::DictionaryValue* arguments) const {
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
494