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/local_test_server.h"
7 #include "base/command_line.h"
8 #include "base/json/json_reader.h"
9 #include "base/logging.h"
10 #include "base/path_service.h"
11 #include "base/process_util.h"
12 #include "base/string_number_conversions.h"
13 #include "base/values.h"
14 #include "googleurl/src/gurl.h"
15 #include "net/base/host_port_pair.h"
16 #include "net/base/net_errors.h"
17 #include "net/test/python_utils.h"
23 bool AppendArgumentFromJSONValue(const std::string
& key
,
24 const base::Value
& value_node
,
25 CommandLine
* command_line
) {
26 std::string argument_name
= "--" + key
;
27 switch (value_node
.GetType()) {
28 case base::Value::TYPE_NULL
:
29 command_line
->AppendArg(argument_name
);
31 case base::Value::TYPE_INTEGER
: {
33 bool result
= value_node
.GetAsInteger(&value
);
35 command_line
->AppendArg(argument_name
+ "=" + base::IntToString(value
));
38 case Value::TYPE_STRING
: {
40 bool result
= value_node
.GetAsString(&value
);
41 if (!result
|| value
.empty())
43 command_line
->AppendArg(argument_name
+ "=" + value
);
46 case base::Value::TYPE_BOOLEAN
:
47 case base::Value::TYPE_DOUBLE
:
48 case base::Value::TYPE_LIST
:
49 case base::Value::TYPE_DICTIONARY
:
50 case base::Value::TYPE_BINARY
:
52 NOTREACHED() << "improper json type";
60 LocalTestServer::LocalTestServer(Type type
,
61 const std::string
& host
,
62 const FilePath
& document_root
)
63 : BaseTestServer(type
, host
) {
64 if (!Init(document_root
))
68 LocalTestServer::LocalTestServer(Type type
,
69 const SSLOptions
& ssl_options
,
70 const FilePath
& document_root
)
71 : BaseTestServer(type
, ssl_options
) {
72 if (!Init(document_root
))
76 LocalTestServer::~LocalTestServer() {
81 bool LocalTestServer::GetTestServerDirectory(FilePath
* directory
) {
82 // Get path to python server script.
83 FilePath testserver_dir
;
84 if (!PathService::Get(base::DIR_SOURCE_ROOT
, &testserver_dir
)) {
85 LOG(ERROR
) << "Failed to get DIR_SOURCE_ROOT";
89 testserver_dir
= testserver_dir
90 .Append(FILE_PATH_LITERAL("net"))
91 .Append(FILE_PATH_LITERAL("tools"))
92 .Append(FILE_PATH_LITERAL("testserver"));
93 *directory
= testserver_dir
;
97 bool LocalTestServer::GetTestServerPath(FilePath
* testserver_path
) const {
98 if (!GetTestServerDirectory(testserver_path
))
100 *testserver_path
= testserver_path
->Append(FILE_PATH_LITERAL(
105 bool LocalTestServer::Start() {
106 // Get path to Python server script.
107 FilePath testserver_path
;
108 if (!GetTestServerPath(&testserver_path
))
111 if (!SetPythonPath())
114 if (!LaunchPython(testserver_path
))
117 if (!WaitToStart()) {
122 return SetupWhenServerStarted();
125 bool LocalTestServer::Stop() {
126 CleanUpWhenStoppingServer();
128 if (!process_handle_
)
131 // First check if the process has already terminated.
132 bool ret
= base::WaitForSingleProcess(process_handle_
, base::TimeDelta());
134 ret
= base::KillProcess(process_handle_
, 1, true);
137 base::CloseProcessHandle(process_handle_
);
138 process_handle_
= base::kNullProcessHandle
;
140 VLOG(1) << "Kill failed?";
146 bool LocalTestServer::Init(const FilePath
& document_root
) {
147 if (document_root
.IsAbsolute())
150 // At this point, the port that the test server will listen on is unknown.
151 // The test server will listen on an ephemeral port, and write the port
152 // number out over a pipe that this TestServer object will read from. Once
153 // that is complete, the host port pair will contain the actual port.
155 process_handle_
= base::kNullProcessHandle
;
158 if (!PathService::Get(base::DIR_SOURCE_ROOT
, &src_dir
))
160 SetResourcePath(src_dir
.Append(document_root
),
161 src_dir
.AppendASCII("net")
164 .AppendASCII("certificates"));
168 bool LocalTestServer::SetPythonPath() const {
169 return SetPythonPathStatic();
173 bool LocalTestServer::SetPythonPathStatic() {
174 FilePath third_party_dir
;
175 if (!PathService::Get(base::DIR_SOURCE_ROOT
, &third_party_dir
)) {
176 LOG(ERROR
) << "Failed to get DIR_SOURCE_ROOT";
179 third_party_dir
= third_party_dir
.AppendASCII("third_party");
181 // For simplejson. (simplejson, unlike all the other Python modules
182 // we include, doesn't have an extra 'simplejson' directory, so we
183 // need to include its parent directory, i.e. third_party_dir).
184 AppendToPythonPath(third_party_dir
);
186 AppendToPythonPath(third_party_dir
.AppendASCII("tlslite"));
188 third_party_dir
.AppendASCII("pyftpdlib").AppendASCII("src"));
190 third_party_dir
.AppendASCII("pywebsocket").AppendASCII("src"));
192 // Locate the Python code generated by the protocol buffers compiler.
193 FilePath pyproto_dir
;
194 if (!GetPyProtoPath(&pyproto_dir
)) {
195 LOG(WARNING
) << "Cannot find pyproto dir for generated code. "
196 << "Testserver features that rely on it will not work";
200 AppendToPythonPath(pyproto_dir
);
201 AppendToPythonPath(pyproto_dir
.AppendASCII("sync").AppendASCII("protocol"));
202 AppendToPythonPath(pyproto_dir
.AppendASCII("chrome")
203 .AppendASCII("browser")
204 .AppendASCII("policy")
205 .AppendASCII("proto"));
210 bool LocalTestServer::AddCommandLineArguments(CommandLine
* command_line
) const {
211 base::DictionaryValue arguments_dict
;
212 if (!GenerateArguments(&arguments_dict
))
215 // Serialize the argument dictionary into CommandLine.
216 for (DictionaryValue::Iterator
it(arguments_dict
); it
.HasNext();
218 const base::Value
& value
= it
.value();
219 const std::string
& key
= it
.key();
221 // Add arguments from a list.
222 if (value
.IsType(Value::TYPE_LIST
)) {
223 const base::ListValue
* list
= NULL
;
224 if (!value
.GetAsList(&list
) || !list
|| list
->empty())
226 for (base::ListValue::const_iterator list_it
= list
->begin();
227 list_it
!= list
->end(); ++list_it
) {
228 if (!AppendArgumentFromJSONValue(key
, *(*list_it
), command_line
))
231 } else if (!AppendArgumentFromJSONValue(key
, value
, command_line
)) {
236 // Append the appropriate server type argument.
240 // The default type is HTTP, no argument required.
244 command_line
->AppendArg("--websocket");
247 command_line
->AppendArg("-f");
250 command_line
->AppendArg("--sync");
253 command_line
->AppendArg("--tcp-echo");
256 command_line
->AppendArg("--udp-echo");
258 case TYPE_BASIC_AUTH_PROXY
:
259 command_line
->AppendArg("--basic-auth-proxy");