Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1191)

Unified Diff: net/test/embedded_test_server/embedded_test_server.h

Issue 1376593007: SSL in EmbeddedTestServer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removing unused macros. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/test/embedded_test_server/embedded_test_server.h
diff --git a/net/test/embedded_test_server/embedded_test_server.h b/net/test/embedded_test_server/embedded_test_server.h
index 031ceaf4b75d6de6744437e930f56a548b8f347d..3cd47b59d168e88357f92ec252ba4aea22552b96 100644
--- a/net/test/embedded_test_server/embedded_test_server.h
+++ b/net/test/embedded_test_server/embedded_test_server.h
@@ -12,18 +12,21 @@
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
+#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/thread.h"
#include "base/threading/thread_checker.h"
+#include "crypto/rsa_private_key.h"
#include "net/base/address_list.h"
+#include "net/base/host_port_pair.h"
#include "net/base/ip_endpoint.h"
+#include "net/cert/x509_certificate.h"
+#include "net/socket/stream_socket.h"
+#include "net/socket/tcp_server_socket.h"
+#include "net/ssl/ssl_server_config.h"
#include "url/gurl.h"
-namespace base {
-class FilePath;
-}
-
namespace net {
class StreamSocket;
@@ -45,7 +48,7 @@ struct HttpRequest;
//
// void SetUp() {
// test_server_.reset(new EmbeddedTestServer());
-// ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady());
+// ASSERT_TRUE(test_server_.Start());
// test_server_->RegisterRequestHandler(
// base::Bind(&FooTest::HandleRequest, base::Unretained(this)));
// }
@@ -63,11 +66,10 @@ struct HttpRequest;
// }
//
// For a test that spawns another process such as browser_tests, it is
-// suggested to call InitializeAndWaitUntilReady in SetUpOnMainThread after
-// the process is spawned. If you have to do it before the process spawns,
-// you need to first setup the listen socket so that there is no no other
-// threads running while spawning the process. To do so, please follow
-// the following example:
+// suggested to call Start in SetUpOnMainThread after the process is spawned.
+// If you have to do it before the process spawns, you need to first setup the
+// listen socket so that there is no no other threads running while spawning
+// the process. To do so, please follow the following example:
//
// void SetUp() {
// ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
@@ -82,12 +84,19 @@ struct HttpRequest;
//
class EmbeddedTestServer {
public:
+ enum Type {
+ TYPE_HTTP,
+ TYPE_HTTPS,
+ };
+
typedef base::Callback<scoped_ptr<HttpResponse>(
const HttpRequest& request)> HandleRequestCallback;
- // Creates a http test server. InitializeAndWaitUntilReady() must be called
- // to start the server.
+ // Creates a http test server. Start() must be called to start the server.
+ // |is_using_ssl| indicates whether the server should use SSL for all
+ // connections.
EmbeddedTestServer();
+ explicit EmbeddedTestServer(const Type type);
mmenke 2015/10/19 18:07:40 is_using_ssl_ should be const, rather than the typ
svaldez 2015/10/19 21:56:14 Done.
~EmbeddedTestServer();
// Sets a connection listener, that would be notified when various connection
@@ -98,7 +107,11 @@ class EmbeddedTestServer {
// Initializes and waits until the server is ready to accept requests.
// This is the equivalent of calling InitializeAndListen() followed by
// StartAcceptingConnections().
- // Returns whether a listening socket has been succesfully created.
+ // Returns whether a listening socket has been successfully created.
+ bool Start();
mmenke 2015/10/19 18:07:40 WARN_UNUSED_RESULT?
svaldez 2015/10/19 21:56:14 As part of using Start instead of InitializeAndWai
+
+ // Deprecated method that calls Start().
+ // TODO(svaldez): Remove and replace with Start().
bool InitializeAndWaitUntilReady() WARN_UNUSED_RESULT;
// Starts listening for incoming connections but will not yet accept them.
@@ -116,6 +129,10 @@ class EmbeddedTestServer {
return listen_socket_.get() != NULL;
}
+ HostPortPair host_port_pair() const {
+ return HostPortPair::FromURL(base_url_);
+ }
+
// Returns the base URL to the server, which looks like
// http://127.0.0.1:<port>/, where <port> is the actual port number used by
// the server.
@@ -133,26 +150,56 @@ class EmbeddedTestServer {
const std::string& relative_url) const;
// Returns the address list needed to connect to the server.
- bool GetAddressList(net::AddressList* address_list) const WARN_UNUSED_RESULT;
+ bool GetAddressList(AddressList* address_list) const WARN_UNUSED_RESULT;
// Returns the port number used by the server.
uint16 port() const { return port_; }
+ // Returns whether we are using SSL.
+ bool is_using_ssl() const { return is_using_ssl_; }
+
+ void SetSSLConfig(const SSLServerConfig& ssl_config);
+
+ // Returns the file name of the certificate the server is using.
+ std::string GetCertificateName() const;
mmenke 2015/10/19 18:07:40 Does this need to be public? I don't see anything
svaldez 2015/10/19 21:56:14 Some of the STS tests call GetCertificateName. See
+
+ // Returns the certificate that the server is using.
+ scoped_refptr<X509Certificate> GetCertificate() const;
mmenke 2015/10/19 18:07:40 Again, I don't see this being called anywhere, and
svaldez 2015/10/19 21:56:14 Its exposed through the BaseTestServer interface f
+
// Registers request handler which serves files from |directory|.
// For instance, a request to "/foo.html" is served by "foo.html" under
// |directory|. Files under sub directories are also handled in the same way
// (i.e. "/foo/bar.html" is served by "foo/bar.html" under |directory|).
void ServeFilesFromDirectory(const base::FilePath& directory);
mmenke 2015/10/19 18:07:40 Add a TODO to remove this method? I don't think w
svaldez 2015/10/19 21:56:14 Done.
+ // Serves files relative to DIR_SOURCE_ROOT.
+ void ServeFilesFromSourceDirectory(const std::string relative);
mmenke 2015/10/19 18:07:40 const std::string&
svaldez 2015/10/19 21:56:14 Done.
+ void ServeFilesFromSourceDirectory(const base::FilePath& relative);
+
+ // Registers the default handlers and serve additional files from the
+ // |directory| directory, relative to DIR_SOURCE_ROOT.
+ void AddDefaultHandlers(const base::FilePath& directory);
+
// The most general purpose method. Any request processing can be added using
// this method. Takes ownership of the object. The |callback| is called
// on UI thread.
void RegisterRequestHandler(const HandleRequestCallback& callback);
+ // Adds default handlers to be tried after all other user-specified handlers
+ // have been tried.
mmenke 2015/10/19 18:07:40 Should also specify order relative to those added
svaldez 2015/10/19 21:56:14 Done.
+ void RegisterDefaultHandler(const HandleRequestCallback& callback);
+
private:
// Shuts down the server.
void ShutdownOnIOThread();
+ // Load the root for the certs being used in testing.
+ void LoadTestSSLRoot();
+ // Upgrade the TCP connection to one over SSL.
+ scoped_ptr<StreamSocket> DoSSLUpgrade(scoped_ptr<StreamSocket> connection);
+ // Handles async callback when we've finished performing the SSL handshake.
mmenke 2015/10/19 18:07:40 nit: Don't use "we" in comments.
svaldez 2015/10/19 21:56:14 Done.
+ void OnHandshakeDone(HttpConnection* connection, int rv);
+
// Begins accepting new client connections.
void DoAcceptLoop();
// Handles async callback when there is a new client socket. |rv| is the
@@ -184,6 +231,8 @@ class EmbeddedTestServer {
bool PostTaskToIOThreadAndWait(
const base::Closure& closure) WARN_UNUSED_RESULT;
+ bool is_using_ssl_;
+
scoped_ptr<base::Thread> io_thread_;
scoped_ptr<TCPServerSocket> listen_socket_;
@@ -197,15 +246,22 @@ class EmbeddedTestServer {
// Owns the HttpConnection objects.
std::map<StreamSocket*, HttpConnection*> connections_;
- // Vector of registered request handlers.
+ // Vector of registered and default request handlers.
std::vector<HandleRequestCallback> request_handlers_;
+ std::vector<HandleRequestCallback> default_request_handlers_;
base::ThreadChecker thread_checker_;
+ net::SSLServerConfig ssl_config_;
+
DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServer);
};
} // namespace test_server
+
+// TODO(svaldez): Refactor EmbeddedTestServer to be in the net namespace.
+using test_server::EmbeddedTestServer;
+
} // namespace net
#endif // NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_

Powered by Google App Engine
This is Rietveld 408576698