local_test_server.h revision a3f7b4e666c476898878fa745f637129375cd889
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.
4
5#ifndef NET_TEST_SPAWNED_TEST_SERVER_LOCAL_TEST_SERVER_H_
6#define NET_TEST_SPAWNED_TEST_SERVER_LOCAL_TEST_SERVER_H_
7
8#include <string>
9
10#include "base/file_util.h"
11#include "base/process/process_handle.h"
12#include "net/test/spawned_test_server/base_test_server.h"
13
14#if defined(OS_WIN)
15#include "base/win/scoped_handle.h"
16#endif
17
18class CommandLine;
19
20namespace net {
21
22// The LocalTestServer runs an external Python-based test server in the
23// same machine in which the LocalTestServer runs.
24class LocalTestServer : public BaseTestServer {
25 public:
26  // Initialize a TestServer listening on a specific host (IP or hostname).
27  // |document_root| must be a relative path under the root tree.
28  LocalTestServer(Type type,
29                  const std::string& host,
30                  const base::FilePath& document_root);
31
32  // Initialize a TestServer with a specific set of SSLOptions.
33  // |document_root| must be a relative path under the root tree.
34  LocalTestServer(Type type,
35                  const SSLOptions& ssl_options,
36                  const base::FilePath& document_root);
37
38  virtual ~LocalTestServer();
39
40  // Start the test server and block until it's ready. Returns true on success.
41  bool Start() WARN_UNUSED_RESULT;
42
43  // Start the test server without blocking. Use this if you need multiple test
44  // servers (such as WebSockets and HTTP, or HTTP and HTTPS). You must call
45  // BlockUntilStarted on all servers your test requires before executing the
46  // test. For example:
47  //
48  //   // Start the servers in parallel.
49  //   ASSERT_TRUE(http_server.StartInBackground());
50  //   ASSERT_TRUE(websocket_server.StartInBackground());
51  //   // Wait for both servers to be ready.
52  //   ASSERT_TRUE(http_server.BlockUntilStarted());
53  //   ASSERT_TRUE(websocket_server.BlockUntilStarted());
54  //   RunMyTest();
55  //
56  // Returns true on success.
57  bool StartInBackground() WARN_UNUSED_RESULT;
58
59  // Block until ths test server is ready. Returns true on success. See
60  // StartInBackground() documentation for more information.
61  bool BlockUntilStarted() WARN_UNUSED_RESULT;
62
63  // Stop the server started by Start().
64  bool Stop();
65
66  // Modify PYTHONPATH to contain libraries we need.
67  virtual bool SetPythonPath() const WARN_UNUSED_RESULT;
68
69  // Returns true if the base::FilePath for the testserver python script is
70  // successfully stored  in |*testserver_path|.
71  virtual bool GetTestServerPath(base::FilePath* testserver_path) const
72      WARN_UNUSED_RESULT;
73
74  // Adds the command line arguments for the Python test server to
75  // |command_line|. Returns true on success.
76  virtual bool AddCommandLineArguments(CommandLine* command_line) const
77      WARN_UNUSED_RESULT;
78
79  // Returns the actual path of document root for test cases. This function
80  // should be called by test cases to retrieve the actual document root path.
81  base::FilePath GetDocumentRoot() const { return document_root(); };
82
83 private:
84  bool Init(const base::FilePath& document_root);
85
86  // Launches the Python test server. Returns true on success.
87  bool LaunchPython(const base::FilePath& testserver_path) WARN_UNUSED_RESULT;
88
89  // Waits for the server to start. Returns true on success.
90  bool WaitToStart() WARN_UNUSED_RESULT;
91
92  // Handle of the Python process running the test server.
93  base::ProcessHandle process_handle_;
94
95#if defined(OS_WIN)
96  // JobObject used to clean up orphaned child processes.
97  base::win::ScopedHandle job_handle_;
98
99  // The pipe file handle we read from.
100  base::win::ScopedHandle child_read_fd_;
101
102  // The pipe file handle the child and we write to.
103  base::win::ScopedHandle child_write_fd_;
104#endif
105
106#if defined(OS_POSIX)
107  // The file descriptor the child writes to when it starts.
108  int child_fd_;
109  file_util::ScopedFD child_fd_closer_;
110#endif
111
112  DISALLOW_COPY_AND_ASSIGN(LocalTestServer);
113};
114
115}  // namespace net
116
117#endif  // NET_TEST_SPAWNED_TEST_SERVER_LOCAL_TEST_SERVER_H_
118