[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / net / test / local_test_server.cc
blob5e1e0e93765030d3ec3651465ec99e3cf0c20b71
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"
19 namespace net {
21 namespace {
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);
30 break;
31 case base::Value::TYPE_INTEGER: {
32 int value;
33 bool result = value_node.GetAsInteger(&value);
34 DCHECK(result);
35 command_line->AppendArg(argument_name + "=" + base::IntToString(value));
36 break;
38 case Value::TYPE_STRING: {
39 std::string value;
40 bool result = value_node.GetAsString(&value);
41 if (!result || value.empty())
42 return false;
43 command_line->AppendArg(argument_name + "=" + value);
44 break;
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:
51 default:
52 NOTREACHED() << "improper json type";
53 return false;
55 return true;
58 } // namespace
60 LocalTestServer::LocalTestServer(Type type,
61 const std::string& host,
62 const FilePath& document_root)
63 : BaseTestServer(type, host) {
64 if (!Init(document_root))
65 NOTREACHED();
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))
73 NOTREACHED();
76 LocalTestServer::~LocalTestServer() {
77 Stop();
80 // static
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";
86 return false;
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;
94 return true;
97 bool LocalTestServer::GetTestServerPath(FilePath* testserver_path) const {
98 if (!GetTestServerDirectory(testserver_path))
99 return false;
100 *testserver_path = testserver_path->Append(FILE_PATH_LITERAL(
101 "testserver.py"));
102 return true;
105 bool LocalTestServer::Start() {
106 // Get path to Python server script.
107 FilePath testserver_path;
108 if (!GetTestServerPath(&testserver_path))
109 return false;
111 if (!SetPythonPath())
112 return false;
114 if (!LaunchPython(testserver_path))
115 return false;
117 if (!WaitToStart()) {
118 Stop();
119 return false;
122 return SetupWhenServerStarted();
125 bool LocalTestServer::Stop() {
126 CleanUpWhenStoppingServer();
128 if (!process_handle_)
129 return true;
131 // First check if the process has already terminated.
132 bool ret = base::WaitForSingleProcess(process_handle_, base::TimeDelta());
133 if (!ret)
134 ret = base::KillProcess(process_handle_, 1, true);
136 if (ret) {
137 base::CloseProcessHandle(process_handle_);
138 process_handle_ = base::kNullProcessHandle;
139 } else {
140 VLOG(1) << "Kill failed?";
143 return ret;
146 bool LocalTestServer::Init(const FilePath& document_root) {
147 if (document_root.IsAbsolute())
148 return false;
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.
154 DCHECK(!GetPort());
155 process_handle_ = base::kNullProcessHandle;
157 FilePath src_dir;
158 if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_dir))
159 return false;
160 SetResourcePath(src_dir.Append(document_root),
161 src_dir.AppendASCII("net")
162 .AppendASCII("data")
163 .AppendASCII("ssl")
164 .AppendASCII("certificates"));
165 return true;
168 bool LocalTestServer::SetPythonPath() const {
169 return SetPythonPathStatic();
172 // static
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";
177 return false;
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"));
187 AppendToPythonPath(
188 third_party_dir.AppendASCII("pyftpdlib").AppendASCII("src"));
189 AppendToPythonPath(
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";
197 return true;
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"));
207 return true;
210 bool LocalTestServer::AddCommandLineArguments(CommandLine* command_line) const {
211 base::DictionaryValue arguments_dict;
212 if (!GenerateArguments(&arguments_dict))
213 return false;
215 // Serialize the argument dictionary into CommandLine.
216 for (DictionaryValue::Iterator it(arguments_dict); it.HasNext();
217 it.Advance()) {
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())
225 return false;
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))
229 return false;
231 } else if (!AppendArgumentFromJSONValue(key, value, command_line)) {
232 return false;
236 // Append the appropriate server type argument.
237 switch (type()) {
238 case TYPE_HTTP:
239 case TYPE_HTTPS:
240 // The default type is HTTP, no argument required.
241 break;
242 case TYPE_WS:
243 case TYPE_WSS:
244 command_line->AppendArg("--websocket");
245 break;
246 case TYPE_FTP:
247 command_line->AppendArg("-f");
248 break;
249 case TYPE_SYNC:
250 command_line->AppendArg("--sync");
251 break;
252 case TYPE_TCP_ECHO:
253 command_line->AppendArg("--tcp-echo");
254 break;
255 case TYPE_UDP_ECHO:
256 command_line->AppendArg("--udp-echo");
257 break;
258 case TYPE_BASIC_AUTH_PROXY:
259 command_line->AppendArg("--basic-auth-proxy");
260 break;
261 default:
262 NOTREACHED();
263 return false;
266 return true;
269 } // namespace net