xref: /aosp_15_r20/external/cronet/net/test/spawned_test_server/remote_test_server.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 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_REMOTE_TEST_SERVER_H_
6 #define NET_TEST_SPAWNED_TEST_SERVER_REMOTE_TEST_SERVER_H_
7 
8 #include <string>
9 
10 #include "base/threading/thread.h"
11 #include "net/test/spawned_test_server/base_test_server.h"
12 
13 namespace net {
14 
15 class RemoteTestServerSpawnerRequest;
16 
17 // The RemoteTestServer runs an external Python-based test server in another
18 // machine that is different from the machine that executes the tests. It is
19 // necessary because it's not always possible to run the test server on the same
20 // machine (it doesn't run on Android and Fuchsia because it's written in
21 // Python).
22 //
23 // The actual test server is executed on the host machine, while the unit tests
24 // themselves continue running on the device. To control the test server on the
25 // host machine, a second HTTP server is started, the spawner server, which
26 // controls the life cycle of remote test servers. Calls to start/kill the
27 // SpawnedTestServer are then redirected to the spawner server via
28 // this spawner communicator. The spawner is implemented in
29 // build/util/lib/common/chrome_test_server_spawner.py .
30 //
31 // On Fuchsia, the URL for the spawner server is passed to a test via the
32 // --remote-test-server-spawner-url-base switch on the command line. On other
33 // platforms, the URL is discovered by reading config file that's expected to be
34 // written on the test device by the test scrips. Location of the config
35 // dependends on platform:
36 //   - Android: DIR_ANDROID_EXTERNAL_STORAGE/net-test-server-config
37 //   - other: DIR_TEMP/net-test-server-config
38 //
39 // The config file must be stored in the following format:
40 //   {
41 //     'spawner_url_base': 'http://localhost:5000'
42 //   }
43 //
44 // 'spawner_url_base' specifies base URL for the spawner.
45 //
46 // Currently the following two commands are supported by spawner.
47 //
48 // (1) Start Python test server, format is:
49 // Path: "/start".
50 // Method: "POST".
51 // Data to server: all arguments needed to launch the Python test server, in
52 //   JSON format.
53 // Data from server: a JSON dict includes the following two field if success,
54 //   "port": the port the Python test server actually listen on that.
55 //   "message": must be "started".
56 //
57 // (2) Kill Python test server, format is:
58 // Path: "/kill".
59 // Method: "GET".
60 // Data to server: port=<server_port>.
61 // Data from server: String "killed" returned if success.
62 //
63 // The internal I/O thread is required by net stack to perform net I/O.
64 // The Start/StopServer methods block the caller thread until result is
65 // fetched from spawner server or timed-out.
66 class RemoteTestServer : public BaseTestServer {
67  public:
68   // Initialize a TestServer. |document_root| must be a relative path under the
69   // root tree.
70   RemoteTestServer(Type type, const base::FilePath& document_root);
71 
72   // Initialize a TestServer with a specific set of SSLOptions.
73   // |document_root| must be a relative path under the root tree.
74   RemoteTestServer(Type type,
75                    const SSLOptions& ssl_options,
76                    const base::FilePath& document_root);
77 
78   RemoteTestServer(const RemoteTestServer&) = delete;
79   RemoteTestServer& operator=(const RemoteTestServer&) = delete;
80 
81   ~RemoteTestServer() override;
82 
83   // BaseTestServer overrides.
84   [[nodiscard]] bool StartInBackground() override;
85   [[nodiscard]] bool BlockUntilStarted() override;
86 
87   // Stops the Python test server that is running on the host machine.
88   bool Stop();
89 
90   // Returns the actual path of document root for the test cases. This function
91   // should be called by test cases to retrieve the actual document root path
92   // on the Android device, otherwise document_root() function is used to get
93   // the document root.
94   base::FilePath GetDocumentRoot() const;
95 
96  private:
97   bool Init(const base::FilePath& document_root);
98 
99   // Returns URL for the specified spawner |command|.
100   GURL GetSpawnerUrl(const std::string& command) const;
101 
102   // URL of the test server spawner. Read from the config file during
103   // initialization.
104   std::string spawner_url_base_;
105 
106   // Thread used to run all IO activity in RemoteTestServerSpawnerRequest and
107   // |ocsp_proxy_|.
108   base::Thread io_thread_;
109 
110   std::unique_ptr<RemoteTestServerSpawnerRequest> start_request_;
111 
112   // Server port. Non-zero when the server is running.
113   int remote_port_ = 0;
114 };
115 
116 }  // namespace net
117 
118 #endif  // NET_TEST_SPAWNED_TEST_SERVER_REMOTE_TEST_SERVER_H_
119