xref: /aosp_15_r20/external/cronet/net/test/spawned_test_server/local_test_server.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
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 <optional>
9 #include <vector>
10 
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_file.h"
13 #include "base/process/process.h"
14 #include "build/build_config.h"
15 #include "net/test/spawned_test_server/base_test_server.h"
16 
17 #if BUILDFLAG(IS_WIN)
18 #include "base/win/scoped_handle.h"
19 #endif
20 
21 namespace base {
22 class CommandLine;
23 }
24 
25 namespace net {
26 
27 // The LocalTestServer runs an external Python-based test server in the
28 // same machine in which the LocalTestServer runs.
29 class LocalTestServer : public BaseTestServer {
30  public:
31   // Initialize a TestServer. |document_root| must be a relative path under the
32   // root tree.
33   LocalTestServer(Type type, const base::FilePath& document_root);
34 
35   // Initialize a TestServer with a specific set of SSLOptions.
36   // |document_root| must be a relative path under the root tree.
37   LocalTestServer(Type type,
38                   const SSLOptions& ssl_options,
39                   const base::FilePath& document_root);
40 
41   LocalTestServer(const LocalTestServer&) = delete;
42   LocalTestServer& operator=(const LocalTestServer&) = delete;
43 
44   ~LocalTestServer() override;
45 
46   // BaseTestServer overrides.
47   [[nodiscard]] bool StartInBackground() override;
48   [[nodiscard]] bool BlockUntilStarted() override;
49 
50   // Stop the server started by Start().
51   bool Stop();
52 
53   // Returns the directories to use as the PYTHONPATH, or nullopt on error.
54   virtual std::optional<std::vector<base::FilePath>> GetPythonPath() const;
55 
56   // Returns true if the base::FilePath for the testserver python script is
57   // successfully stored  in |*testserver_path|.
58   [[nodiscard]] virtual bool GetTestServerPath(
59       base::FilePath* testserver_path) const;
60 
61   // Adds the command line arguments for the Python test server to
62   // |command_line|. Returns true on success.
63   [[nodiscard]] virtual bool AddCommandLineArguments(
64       base::CommandLine* command_line) const;
65 
66   // Returns the actual path of document root for test cases. This function
67   // should be called by test cases to retrieve the actual document root path.
GetDocumentRoot()68   base::FilePath GetDocumentRoot() const { return document_root(); }
69 
70  private:
71   bool Init(const base::FilePath& document_root);
72 
73   // Launches the Python test server. Returns true on success. |testserver_path|
74   // is the path to the test server script. |python_path| is the list of
75   // directories to use as the PYTHONPATH environment variable.
76   [[nodiscard]] bool LaunchPython(
77       const base::FilePath& testserver_path,
78       const std::vector<base::FilePath>& python_path);
79 
80   // Waits for the server to start. Returns true on success.
81   [[nodiscard]] bool WaitToStart();
82 
83   // The Python process running the test server.
84   base::Process process_;
85 
86 #if BUILDFLAG(IS_WIN)
87   // The pipe file handle we read from.
88   base::win::ScopedHandle child_read_fd_;
89 
90   // The pipe file handle the child and we write to.
91   base::win::ScopedHandle child_write_fd_;
92 #endif
93 
94 #if BUILDFLAG(IS_POSIX)
95   // The file descriptor the child writes to when it starts.
96   base::ScopedFD child_fd_;
97 #endif
98 };
99 
100 }  // namespace net
101 
102 #endif  // NET_TEST_SPAWNED_TEST_SERVER_LOCAL_TEST_SERVER_H_
103