Enables compositing support for webview.
[chromium-blink-merge.git] / net / test / remote_test_server.cc
blob3782cf2fb40e32ad469d4ac202b130daec692d59
1 // Copyright (c) 2012 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.
5 #include "net/test/remote_test_server.h"
7 #include <vector>
9 #include "base/base_paths.h"
10 #include "base/file_path.h"
11 #include "base/file_util.h"
12 #include "base/json/json_writer.h"
13 #include "base/logging.h"
14 #include "base/path_service.h"
15 #include "base/string_number_conversions.h"
16 #include "base/string_split.h"
17 #include "base/values.h"
18 #include "googleurl/src/gurl.h"
19 #include "net/base/host_port_pair.h"
20 #include "net/base/net_errors.h"
21 #include "net/test/spawner_communicator.h"
23 namespace net {
25 namespace {
27 // To reduce the running time of tests, tests may be sharded across several
28 // devices. This means that it may be necessary to support multiple instances
29 // of the test server spawner and the Python test server simultaneously on the
30 // same host. Each pair of (test server spawner, Python test server) correspond
31 // to a single testing device.
32 // The mapping between the test server spawner and the individual Python test
33 // servers is written to a file on the device prior to executing any tests.
34 FilePath GetTestServerPortInfoFile() {
35 #if !defined(OS_ANDROID)
36 return FilePath("/tmp/net-test-server-ports");
37 #else
38 FilePath test_data_dir;
39 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &test_data_dir);
40 return test_data_dir.Append("net-test-server-ports");
41 #endif
44 // Please keep it sync with dictionary SERVER_TYPES in testserver.py
45 std::string GetServerTypeString(BaseTestServer::Type type) {
46 switch (type) {
47 case BaseTestServer::TYPE_FTP:
48 return "ftp";
49 case BaseTestServer::TYPE_HTTP:
50 case BaseTestServer::TYPE_HTTPS:
51 return "http";
52 case BaseTestServer::TYPE_WS:
53 case BaseTestServer::TYPE_WSS:
54 return "ws";
55 case BaseTestServer::TYPE_SYNC:
56 return "sync";
57 case BaseTestServer::TYPE_TCP_ECHO:
58 return "tcpecho";
59 case BaseTestServer::TYPE_UDP_ECHO:
60 return "udpecho";
61 default:
62 NOTREACHED();
64 return std::string();
67 } // namespace
69 RemoteTestServer::RemoteTestServer(Type type,
70 const std::string& host,
71 const FilePath& document_root)
72 : BaseTestServer(type, host),
73 spawner_server_port_(0) {
74 if (!Init(document_root))
75 NOTREACHED();
78 RemoteTestServer::RemoteTestServer(Type type,
79 const SSLOptions& ssl_options,
80 const FilePath& document_root)
81 : BaseTestServer(type, ssl_options),
82 spawner_server_port_(0) {
83 if (!Init(document_root))
84 NOTREACHED();
87 RemoteTestServer::~RemoteTestServer() {
88 Stop();
91 bool RemoteTestServer::Start() {
92 if (spawner_communicator_.get())
93 return true;
94 spawner_communicator_.reset(new SpawnerCommunicator(spawner_server_port_));
96 base::DictionaryValue arguments_dict;
97 if (!GenerateArguments(&arguments_dict))
98 return false;
100 // Append the 'server-type' argument which is used by spawner server to
101 // pass right server type to Python test server.
102 arguments_dict.SetString("server-type", GetServerTypeString(type()));
104 // Generate JSON-formatted argument string.
105 std::string arguments_string;
106 base::JSONWriter::Write(&arguments_dict, &arguments_string);
107 if (arguments_string.empty())
108 return false;
110 // Start the Python test server on the remote machine.
111 uint16 test_server_port;
112 if (!spawner_communicator_->StartServer(arguments_string,
113 &test_server_port)) {
114 return false;
116 if (0 == test_server_port)
117 return false;
119 // Construct server data to initialize BaseTestServer::server_data_.
120 base::DictionaryValue server_data_dict;
121 // At this point, the test server should be spawned on the host. Update the
122 // local port to real port of Python test server, which will be forwarded to
123 // the remote server.
124 server_data_dict.SetInteger("port", test_server_port);
125 std::string server_data;
126 base::JSONWriter::Write(&server_data_dict, &server_data);
127 if (server_data.empty() || !ParseServerData(server_data)) {
128 LOG(ERROR) << "Could not parse server_data: " << server_data;
129 return false;
132 return SetupWhenServerStarted();
135 bool RemoteTestServer::Stop() {
136 if (!spawner_communicator_.get())
137 return true;
138 CleanUpWhenStoppingServer();
139 bool stopped = spawner_communicator_->StopServer();
140 // Explicitly reset |spawner_communicator_| to avoid reusing the stopped one.
141 spawner_communicator_.reset(NULL);
142 return stopped;
145 // On Android, the document root in the device is not the same as the document
146 // root in the host machine where the test server is launched. So prepend
147 // DIR_SOURCE_ROOT here to get the actual path of document root on the Android
148 // device.
149 FilePath RemoteTestServer::GetDocumentRoot() const {
150 FilePath src_dir;
151 PathService::Get(base::DIR_SOURCE_ROOT, &src_dir);
152 return src_dir.Append(document_root());
155 bool RemoteTestServer::Init(const FilePath& document_root) {
156 if (document_root.IsAbsolute())
157 return false;
159 // Gets ports information used by test server spawner and Python test server.
160 int test_server_port = 0;
162 // Parse file to extract the ports information.
163 std::string port_info;
164 if (!file_util::ReadFileToString(GetTestServerPortInfoFile(),
165 &port_info) ||
166 port_info.empty()) {
167 return false;
170 std::vector<std::string> ports;
171 base::SplitString(port_info, ':', &ports);
172 if (ports.size() != 2u)
173 return false;
175 // Verify the ports information.
176 base::StringToInt(ports[0], &spawner_server_port_);
177 if (!spawner_server_port_ ||
178 static_cast<uint32>(spawner_server_port_) >= kuint16max)
179 return false;
181 // Allow the test_server_port to be 0, which means the test server spawner
182 // will pick up a random port to run the test server.
183 base::StringToInt(ports[1], &test_server_port);
184 if (static_cast<uint32>(test_server_port) >= kuint16max)
185 return false;
186 SetPort(test_server_port);
188 SetResourcePath(document_root, FilePath().AppendASCII("net")
189 .AppendASCII("data")
190 .AppendASCII("ssl")
191 .AppendASCII("certificates"));
192 return true;
195 } // namespace net