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)#ifndef NET_TEST_SPAWNED_TEST_SERVER_LOCAL_TEST_SERVER_H_
6b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#define NET_TEST_SPAWNED_TEST_SERVER_LOCAL_TEST_SERVER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/files/scoped_file.h"
12a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch#include "base/process/process_handle.h"
13b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "net/test/spawned_test_server/base_test_server.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/scoped_handle.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace base {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CommandLine;
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The LocalTestServer runs an external Python-based test server in the
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// same machine in which the LocalTestServer runs.
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class LocalTestServer : public BaseTestServer {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initialize a TestServer listening on a specific host (IP or hostname).
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |document_root| must be a relative path under the root tree.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LocalTestServer(Type type,
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  const std::string& host,
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  const base::FilePath& document_root);
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initialize a TestServer with a specific set of SSLOptions.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |document_root| must be a relative path under the root tree.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LocalTestServer(Type type,
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  const SSLOptions& ssl_options,
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  const base::FilePath& document_root);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~LocalTestServer();
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Start the test server and block until it's ready. Returns true on success.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Start() WARN_UNUSED_RESULT;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Start the test server without blocking. Use this if you need multiple test
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // servers (such as WebSockets and HTTP, or HTTP and HTTPS). You must call
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // BlockUntilStarted on all servers your test requires before executing the
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // test. For example:
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   // Start the servers in parallel.
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   ASSERT_TRUE(http_server.StartInBackground());
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   ASSERT_TRUE(websocket_server.StartInBackground());
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   // Wait for both servers to be ready.
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   ASSERT_TRUE(http_server.BlockUntilStarted());
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   ASSERT_TRUE(websocket_server.BlockUntilStarted());
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   RunMyTest();
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true on success.
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool StartInBackground() WARN_UNUSED_RESULT;
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Block until ths test server is ready. Returns true on success. See
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // StartInBackground() documentation for more information.
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool BlockUntilStarted() WARN_UNUSED_RESULT;
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Stop the server started by Start().
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Stop();
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Modify PYTHONPATH to contain libraries we need.
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool SetPythonPath() const WARN_UNUSED_RESULT;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the base::FilePath for the testserver python script is
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // successfully stored  in |*testserver_path|.
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool GetTestServerPath(base::FilePath* testserver_path) const
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      WARN_UNUSED_RESULT;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds the command line arguments for the Python test server to
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |command_line|. Returns true on success.
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual bool AddCommandLineArguments(base::CommandLine* command_line) const
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      WARN_UNUSED_RESULT;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the actual path of document root for test cases. This function
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // should be called by test cases to retrieve the actual document root path.
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath GetDocumentRoot() const { return document_root(); };
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool Init(const base::FilePath& document_root);
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Launches the Python test server. Returns true on success.
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool LaunchPython(const base::FilePath& testserver_path) WARN_UNUSED_RESULT;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Waits for the server to start. Returns true on success.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool WaitToStart() WARN_UNUSED_RESULT;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Handle of the Python process running the test server.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::ProcessHandle process_handle_;
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The pipe file handle we read from.
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::win::ScopedHandle child_read_fd_;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The pipe file handle the child and we write to.
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::win::ScopedHandle child_write_fd_;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The file descriptor the child writes to when it starts.
108a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::ScopedFD child_fd_;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(LocalTestServer);
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
116b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#endif  // NET_TEST_SPAWNED_TEST_SERVER_LOCAL_TEST_SERVER_H_
117