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
,
65 // Causes the testserver to use a hostname that is a domain
67 CERT_COMMON_NAME_IS_DOMAIN
,
70 // OCSPStatus enumerates the types of OCSP response that the testserver
80 // Bitmask of key exchange algorithms that the test server supports and that
81 // can be selectively enabled or disabled.
83 // Special value used to indicate that any algorithm the server supports
84 // is acceptable. Preferred over explicitly OR-ing all key exchange
88 KEY_EXCHANGE_RSA
= (1 << 0),
89 KEY_EXCHANGE_DHE_RSA
= (1 << 1),
90 KEY_EXCHANGE_ECDHE_RSA
= (1 << 2),
93 // Bitmask of bulk encryption algorithms that the test server supports
94 // and that can be selectively enabled or disabled.
96 // Special value used to indicate that any algorithm the server supports
97 // is acceptable. Preferred over explicitly OR-ing all ciphers.
100 BULK_CIPHER_RC4
= (1 << 0),
101 BULK_CIPHER_AES128
= (1 << 1),
102 BULK_CIPHER_AES256
= (1 << 2),
104 // NOTE: 3DES support in the Python test server has external
105 // dependencies and not be available on all machines. Clients may not
106 // be able to connect if only 3DES is specified.
107 BULK_CIPHER_3DES
= (1 << 3),
109 BULK_CIPHER_AES128GCM
= (1 << 4),
112 // NOTE: the values of these enumerators are passed to the the Python test
113 // server. Do not change them.
114 enum TLSIntolerantLevel
{
115 TLS_INTOLERANT_NONE
= 0,
116 TLS_INTOLERANT_ALL
= 1, // Intolerant of all TLS versions.
117 TLS_INTOLERANT_TLS1_1
= 2, // Intolerant of TLS 1.1 or higher.
118 TLS_INTOLERANT_TLS1_2
= 3, // Intolerant of TLS 1.2 or higher.
121 // Values which control how the server reacts in response to a ClientHello
122 // it is intolerant of.
123 enum TLSIntoleranceType
{
124 TLS_INTOLERANCE_ALERT
= 0, // Send a handshake_failure alert.
125 TLS_INTOLERANCE_CLOSE
= 1, // Close the connection.
126 TLS_INTOLERANCE_RESET
= 2, // Send a TCP reset.
129 // Initialize a new SSLOptions using CERT_OK as the certificate.
132 // Initialize a new SSLOptions that will use the specified certificate.
133 explicit SSLOptions(ServerCertificate cert
);
136 // Returns the relative filename of the file that contains the
137 // |server_certificate|.
138 base::FilePath
GetCertificateFile() const;
140 // GetOCSPArgument returns the value of any OCSP argument to testserver or
141 // the empty string if there is none.
142 std::string
GetOCSPArgument() const;
144 // The certificate to use when serving requests.
145 ServerCertificate server_certificate
;
147 // If |server_certificate==CERT_AUTO| then this determines the type of OCSP
148 // response returned.
149 OCSPStatus ocsp_status
;
151 // If not zero, |cert_serial| will be the serial number of the
152 // auto-generated leaf certificate when |server_certificate==CERT_AUTO|.
155 // True if a CertificateRequest should be sent to the client during
157 bool request_client_certificate
;
159 // If |request_client_certificate| is true, an optional list of files,
160 // each containing a single, PEM-encoded X.509 certificates. The subject
161 // from each certificate will be added to the certificate_authorities
162 // field of the CertificateRequest.
163 std::vector
<base::FilePath
> client_authorities
;
165 // If |request_client_certificate| is true, an optional list of
166 // SSLClientCertType values to populate the certificate_types field of the
167 // CertificateRequest.
168 std::vector
<SSLClientCertType
> client_cert_types
;
170 // A bitwise-OR of KeyExchnage that should be used by the
171 // HTTPS server, or KEY_EXCHANGE_ANY to indicate that all implemented
172 // key exchange algorithms are acceptable.
175 // A bitwise-OR of BulkCipher that should be used by the
176 // HTTPS server, or BULK_CIPHER_ANY to indicate that all implemented
177 // ciphers are acceptable.
180 // If true, pass the --https-record-resume argument to testserver.py which
181 // causes it to log session cache actions and echo the log on
182 // /ssl-session-cache.
185 // If not TLS_INTOLERANT_NONE, the server will abort any handshake that
186 // negotiates an intolerant TLS version in order to test version fallback.
187 TLSIntolerantLevel tls_intolerant
;
189 // If |tls_intolerant| is not TLS_INTOLERANT_NONE, how the server reacts to
190 // an intolerant TLS version.
191 TLSIntoleranceType tls_intolerance_type
;
193 // fallback_scsv_enabled, if true, causes the server to process the
194 // TLS_FALLBACK_SCSV cipher suite. This cipher suite is sent by Chrome
195 // when performing TLS version fallback in response to an SSL handshake
196 // failure. If this option is enabled then the server will reject fallback
198 bool fallback_scsv_enabled
;
200 // Temporary glue for testing: validation of SCTs is application-controlled
201 // and can be appropriately mocked out, so sending fake data here does not
202 // affect handshaking behaviour.
203 // TODO(ekasper): replace with valid SCT files for test certs.
204 // (Fake) SignedCertificateTimestampList (as a raw binary string) to send in
206 std::string signed_cert_timestamps_tls_ext
;
208 // Whether to staple the OCSP response.
209 bool staple_ocsp_response
;
211 // Whether to make the OCSP server unavailable. This does not affect the
212 // stapled OCSP response.
213 bool ocsp_server_unavailable
;
215 // Whether to enable NPN support.
218 // Whether to send a fatal alert immediately after completing the handshake.
219 bool alert_after_handshake
;
222 // Pass as the 'host' parameter during construction to server on 127.0.0.1
223 static const char kLocalhost
[];
225 // Initialize a TestServer listening on a specific host (IP or hostname).
226 BaseTestServer(Type type
, const std::string
& host
);
228 // Initialize a TestServer with a specific set of SSLOptions for HTTPS or WSS.
229 BaseTestServer(Type type
, const SSLOptions
& ssl_options
);
231 // Returns the host port pair used by current Python based test server only
232 // if the server is started.
233 const HostPortPair
& host_port_pair() const;
235 const base::FilePath
& document_root() const { return document_root_
; }
236 const base::DictionaryValue
& server_data() const;
237 std::string
GetScheme() const;
238 bool GetAddressList(AddressList
* address_list
) const WARN_UNUSED_RESULT
;
240 GURL
GetURL(const std::string
& path
) const;
242 GURL
GetURLWithUser(const std::string
& path
,
243 const std::string
& user
) const;
245 GURL
GetURLWithUserAndPassword(const std::string
& path
,
246 const std::string
& user
,
247 const std::string
& password
) const;
249 static bool GetFilePathWithReplacements(
250 const std::string
& original_path
,
251 const std::vector
<StringPair
>& text_to_replace
,
252 std::string
* replacement_path
);
254 static bool UsingSSL(Type type
) {
255 return type
== BaseTestServer::TYPE_HTTPS
||
256 type
== BaseTestServer::TYPE_WSS
;
259 // Enable HTTP basic authentication. Currently this only works for TYPE_WS and
261 void set_websocket_basic_auth(bool ws_basic_auth
) {
262 ws_basic_auth_
= ws_basic_auth
;
265 // Disable creation of anonymous FTP user.
266 void set_no_anonymous_ftp_user(bool no_anonymous_ftp_user
) {
267 no_anonymous_ftp_user_
= no_anonymous_ftp_user
;
270 // Marks the root certificate of an HTTPS test server as trusted for
271 // the duration of tests.
272 bool LoadTestRootCert() const WARN_UNUSED_RESULT
;
275 virtual ~BaseTestServer();
276 Type
type() const { return type_
; }
278 // Gets port currently assigned to host_port_pair_ without checking
279 // whether it's available (server started) or not.
282 // Sets |port| as the actual port used by Python based test server.
283 void SetPort(uint16 port
);
285 // Set up internal status when the server is started.
286 bool SetupWhenServerStarted() WARN_UNUSED_RESULT
;
288 // Clean up internal status when starting to stop server.
289 void CleanUpWhenStoppingServer();
291 // Set path of test resources.
292 void SetResourcePath(const base::FilePath
& document_root
,
293 const base::FilePath
& certificates_dir
);
295 // Parses the server data read from the test server. Returns true
297 bool ParseServerData(const std::string
& server_data
) WARN_UNUSED_RESULT
;
299 // Generates a DictionaryValue with the arguments for launching the external
300 // Python test server.
301 bool GenerateArguments(base::DictionaryValue
* arguments
) const
304 // Subclasses can override this to add arguments that are specific to their
306 virtual bool GenerateAdditionalArguments(
307 base::DictionaryValue
* arguments
) const WARN_UNUSED_RESULT
;
310 void Init(const std::string
& host
);
312 // Document root of the test server.
313 base::FilePath document_root_
;
315 // Directory that contains the SSL certificates.
316 base::FilePath certificates_dir_
;
318 // Address the test server listens on.
319 HostPortPair host_port_pair_
;
321 // Holds the data sent from the server (e.g., port number).
322 scoped_ptr
<base::DictionaryValue
> server_data_
;
324 // If |type_| is TYPE_HTTPS or TYPE_WSS, the TLS settings to use for the test
326 SSLOptions ssl_options_
;
330 // Has the server been started?
333 // Enables logging of the server to the console.
334 bool log_to_console_
;
336 // Is WebSocket basic HTTP authentication enabled?
339 // Disable creation of anonymous FTP user?
340 bool no_anonymous_ftp_user_
;
342 scoped_ptr
<ScopedPortException
> allowed_port_
;
344 DISALLOW_COPY_AND_ASSIGN(BaseTestServer
);
349 #endif // NET_TEST_SPAWNED_TEST_SERVER_BASE_TEST_SERVER_H_