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
3 // found in the LICENSE file.
5 #ifndef NET_TEST_SPAWNED_TEST_SERVER_BASE_TEST_SERVER_H_
6 #define NET_TEST_SPAWNED_TEST_SERVER_BASE_TEST_SERVER_H_
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "net/base/host_port_pair.h"
16 #include "net/ssl/ssl_client_cert_type.h"
21 class DictionaryValue
;
27 class ScopedPortException
;
29 // The base class of Test server implementation.
30 class BaseTestServer
{
32 typedef std::pair
<std::string
, std::string
> StringPair
;
34 // Following types represent protocol schemes. See also
35 // http://www.iana.org/assignments/uri-schemes.html
37 TYPE_BASIC_AUTH_PROXY
,
47 // Container for various options to control how the HTTPS or WSS server is
50 enum ServerCertificate
{
53 // CERT_AUTO causes the testserver to generate a test certificate issued
54 // by "Testing CA" (see net/data/ssl/certificates/ocsp-test-root.pem).
59 // Cross-signed certificate to test PKIX path building. Contains an
60 // intermediate cross-signed by an unknown root, while the client (via
61 // TestRootStore) is expected to have a self-signed version of the
63 CERT_CHAIN_WRONG_ROOT
,
66 // OCSPStatus enumerates the types of OCSP response that the testserver
76 // Bitmask of key exchange algorithms that the test server supports and that
77 // can be selectively enabled or disabled.
79 // Special value used to indicate that any algorithm the server supports
80 // is acceptable. Preferred over explicitly OR-ing all key exchange
84 KEY_EXCHANGE_RSA
= (1 << 0),
85 KEY_EXCHANGE_DHE_RSA
= (1 << 1),
88 // Bitmask of bulk encryption algorithms that the test server supports
89 // and that can be selectively enabled or disabled.
91 // Special value used to indicate that any algorithm the server supports
92 // is acceptable. Preferred over explicitly OR-ing all ciphers.
95 BULK_CIPHER_RC4
= (1 << 0),
96 BULK_CIPHER_AES128
= (1 << 1),
97 BULK_CIPHER_AES256
= (1 << 2),
99 // NOTE: 3DES support in the Python test server has external
100 // dependencies and not be available on all machines. Clients may not
101 // be able to connect if only 3DES is specified.
102 BULK_CIPHER_3DES
= (1 << 3),
105 // NOTE: the values of these enumerators are passed to the the Python test
106 // server. Do not change them.
107 enum TLSIntolerantLevel
{
108 TLS_INTOLERANT_NONE
= 0,
109 TLS_INTOLERANT_ALL
= 1, // Intolerant of all TLS versions.
110 TLS_INTOLERANT_TLS1_1
= 2, // Intolerant of TLS 1.1 or higher.
111 TLS_INTOLERANT_TLS1_2
= 3, // Intolerant of TLS 1.2 or higher.
114 // Values which control how the server reacts in response to a ClientHello
115 // it is intolerant of.
116 enum TLSIntoleranceType
{
117 TLS_INTOLERANCE_ALERT
= 0, // Send a handshake_failure alert.
118 TLS_INTOLERANCE_CLOSE
= 1, // Close the connection.
119 TLS_INTOLERANCE_RESET
= 2, // Send a TCP reset.
122 // Initialize a new SSLOptions using CERT_OK as the certificate.
125 // Initialize a new SSLOptions that will use the specified certificate.
126 explicit SSLOptions(ServerCertificate cert
);
129 // Returns the relative filename of the file that contains the
130 // |server_certificate|.
131 base::FilePath
GetCertificateFile() const;
133 // GetOCSPArgument returns the value of any OCSP argument to testserver or
134 // the empty string if there is none.
135 std::string
GetOCSPArgument() const;
137 // The certificate to use when serving requests.
138 ServerCertificate server_certificate
;
140 // If |server_certificate==CERT_AUTO| then this determines the type of OCSP
141 // response returned.
142 OCSPStatus ocsp_status
;
144 // If not zero, |cert_serial| will be the serial number of the
145 // auto-generated leaf certificate when |server_certificate==CERT_AUTO|.
148 // True if a CertificateRequest should be sent to the client during
150 bool request_client_certificate
;
152 // If |request_client_certificate| is true, an optional list of files,
153 // each containing a single, PEM-encoded X.509 certificates. The subject
154 // from each certificate will be added to the certificate_authorities
155 // field of the CertificateRequest.
156 std::vector
<base::FilePath
> client_authorities
;
158 // If |request_client_certificate| is true, an optional list of
159 // SSLClientCertType values to populate the certificate_types field of the
160 // CertificateRequest.
161 std::vector
<SSLClientCertType
> client_cert_types
;
163 // A bitwise-OR of KeyExchnage that should be used by the
164 // HTTPS server, or KEY_EXCHANGE_ANY to indicate that all implemented
165 // key exchange algorithms are acceptable.
168 // A bitwise-OR of BulkCipher that should be used by the
169 // HTTPS server, or BULK_CIPHER_ANY to indicate that all implemented
170 // ciphers are acceptable.
173 // If true, pass the --https-record-resume argument to testserver.py which
174 // causes it to log session cache actions and echo the log on
175 // /ssl-session-cache.
178 // If not TLS_INTOLERANT_NONE, the server will abort any handshake that
179 // negotiates an intolerant TLS version in order to test version fallback.
180 TLSIntolerantLevel tls_intolerant
;
182 // If |tls_intolerant| is not TLS_INTOLERANT_NONE, how the server reacts to
183 // an intolerant TLS version.
184 TLSIntoleranceType tls_intolerance_type
;
186 // fallback_scsv_enabled, if true, causes the server to process the
187 // TLS_FALLBACK_SCSV cipher suite. This cipher suite is sent by Chrome
188 // when performing TLS version fallback in response to an SSL handshake
189 // failure. If this option is enabled then the server will reject fallback
191 bool fallback_scsv_enabled
;
193 // Temporary glue for testing: validation of SCTs is application-controlled
194 // and can be appropriately mocked out, so sending fake data here does not
195 // affect handshaking behaviour.
196 // TODO(ekasper): replace with valid SCT files for test certs.
197 // (Fake) SignedCertificateTimestampList (as a raw binary string) to send in
199 std::string signed_cert_timestamps_tls_ext
;
201 // Whether to staple the OCSP response.
202 bool staple_ocsp_response
;
204 // Whether to enable NPN support.
207 // Whether to disable TLS session caching. When session caching is
208 // disabled, the server will use an empty session ID in the
210 bool disable_session_cache
;
213 // Pass as the 'host' parameter during construction to server on 127.0.0.1
214 static const char kLocalhost
[];
216 // Initialize a TestServer listening on a specific host (IP or hostname).
217 BaseTestServer(Type type
, const std::string
& host
);
219 // Initialize a TestServer with a specific set of SSLOptions for HTTPS or WSS.
220 BaseTestServer(Type type
, const SSLOptions
& ssl_options
);
222 // Returns the host port pair used by current Python based test server only
223 // if the server is started.
224 const HostPortPair
& host_port_pair() const;
226 const base::FilePath
& document_root() const { return document_root_
; }
227 const base::DictionaryValue
& server_data() const;
228 std::string
GetScheme() const;
229 bool GetAddressList(AddressList
* address_list
) const WARN_UNUSED_RESULT
;
231 GURL
GetURL(const std::string
& path
) const;
233 GURL
GetURLWithUser(const std::string
& path
,
234 const std::string
& user
) const;
236 GURL
GetURLWithUserAndPassword(const std::string
& path
,
237 const std::string
& user
,
238 const std::string
& password
) const;
240 static bool GetFilePathWithReplacements(
241 const std::string
& original_path
,
242 const std::vector
<StringPair
>& text_to_replace
,
243 std::string
* replacement_path
);
245 static bool UsingSSL(Type type
) {
246 return type
== BaseTestServer::TYPE_HTTPS
||
247 type
== BaseTestServer::TYPE_WSS
;
250 // Enable HTTP basic authentication. Currently this only works for TYPE_WS and
252 void set_websocket_basic_auth(bool ws_basic_auth
) {
253 ws_basic_auth_
= ws_basic_auth
;
257 virtual ~BaseTestServer();
258 Type
type() const { return type_
; }
260 // Gets port currently assigned to host_port_pair_ without checking
261 // whether it's available (server started) or not.
264 // Sets |port| as the actual port used by Python based test server.
265 void SetPort(uint16 port
);
267 // Set up internal status when the server is started.
268 bool SetupWhenServerStarted() WARN_UNUSED_RESULT
;
270 // Clean up internal status when starting to stop server.
271 void CleanUpWhenStoppingServer();
273 // Set path of test resources.
274 void SetResourcePath(const base::FilePath
& document_root
,
275 const base::FilePath
& certificates_dir
);
277 // Parses the server data read from the test server. Returns true
279 bool ParseServerData(const std::string
& server_data
) WARN_UNUSED_RESULT
;
281 // Generates a DictionaryValue with the arguments for launching the external
282 // Python test server.
283 bool GenerateArguments(base::DictionaryValue
* arguments
) const
286 // Subclasses can override this to add arguments that are specific to their
288 virtual bool GenerateAdditionalArguments(
289 base::DictionaryValue
* arguments
) const WARN_UNUSED_RESULT
;
292 void Init(const std::string
& host
);
294 // Marks the root certificate of an HTTPS test server as trusted for
295 // the duration of tests.
296 bool LoadTestRootCert() const WARN_UNUSED_RESULT
;
298 // Document root of the test server.
299 base::FilePath document_root_
;
301 // Directory that contains the SSL certificates.
302 base::FilePath certificates_dir_
;
304 // Address the test server listens on.
305 HostPortPair host_port_pair_
;
307 // Holds the data sent from the server (e.g., port number).
308 scoped_ptr
<base::DictionaryValue
> server_data_
;
310 // If |type_| is TYPE_HTTPS or TYPE_WSS, the TLS settings to use for the test
312 SSLOptions ssl_options_
;
316 // Has the server been started?
319 // Enables logging of the server to the console.
320 bool log_to_console_
;
322 // Is WebSocket basic HTTP authentication enabled?
325 scoped_ptr
<ScopedPortException
> allowed_port_
;
327 DISALLOW_COPY_AND_ASSIGN(BaseTestServer
);
332 #endif // NET_TEST_SPAWNED_TEST_SERVER_BASE_TEST_SERVER_H_