OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/test/test_server.h" | 5 #include "net/test/test_server.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 | 12 |
13 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
14 #include <windows.h> | 14 #include <windows.h> |
15 #include <wincrypt.h> | 15 #include <wincrypt.h> |
16 #elif defined(OS_MACOSX) | 16 #elif defined(OS_MACOSX) |
17 #include "net/base/x509_certificate.h" | 17 #include "net/base/x509_certificate.h" |
18 #endif | 18 #endif |
19 | 19 |
20 #include "base/file_util.h" | 20 #include "base/file_util.h" |
21 #include "base/leak_annotations.h" | 21 #include "base/leak_annotations.h" |
22 #include "base/logging.h" | 22 #include "base/logging.h" |
23 #include "base/path_service.h" | 23 #include "base/path_service.h" |
24 #include "base/utf_string_conversions.h" | 24 #include "base/utf_string_conversions.h" |
25 #include "net/base/cert_test_util.h" | 25 #include "net/base/cert_test_util.h" |
26 #include "net/base/host_resolver.h" | 26 #include "net/base/host_resolver.h" |
27 #include "net/base/net_test_constants.h" | |
28 #include "net/base/test_completion_callback.h" | 27 #include "net/base/test_completion_callback.h" |
29 #include "net/socket/tcp_client_socket.h" | 28 #include "net/socket/tcp_client_socket.h" |
30 #include "net/socket/tcp_pinger.h" | 29 #include "net/socket/tcp_pinger.h" |
31 #include "testing/platform_test.h" | 30 #include "testing/platform_test.h" |
32 | 31 |
33 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
34 #pragma comment(lib, "crypt32.lib") | 33 #pragma comment(lib, "crypt32.lib") |
35 #endif | 34 #endif |
36 | 35 |
| 36 namespace { |
| 37 |
| 38 // Number of connection attempts for tests. |
| 39 const int kServerConnectionAttempts = 10; |
| 40 |
| 41 // Connection timeout in milliseconds for tests. |
| 42 const int kServerConnectionTimeoutMs = 1000; |
| 43 |
| 44 } // namespace |
| 45 |
37 namespace net { | 46 namespace net { |
38 | 47 |
39 #if defined(OS_MACOSX) | 48 #if defined(OS_MACOSX) |
40 void SetMacTestCertificate(X509Certificate* cert); | 49 void SetMacTestCertificate(X509Certificate* cert); |
41 #endif | 50 #endif |
42 | 51 |
43 // static | 52 // static |
44 const char TestServerLauncher::kHostName[] = "127.0.0.1"; | 53 const char TestServerLauncher::kHostName[] = "127.0.0.1"; |
45 const char TestServerLauncher::kMismatchedHostName[] = "localhost"; | 54 const char TestServerLauncher::kMismatchedHostName[] = "localhost"; |
46 const int TestServerLauncher::kOKHTTPSPort = 9443; | 55 const int TestServerLauncher::kOKHTTPSPort = 9443; |
47 const int TestServerLauncher::kBadHTTPSPort = 9666; | 56 const int TestServerLauncher::kBadHTTPSPort = 9666; |
48 | 57 |
49 // The issuer name of the cert that should be trusted for the test to work. | 58 // The issuer name of the cert that should be trusted for the test to work. |
50 const wchar_t TestServerLauncher::kCertIssuerName[] = L"Test CA"; | 59 const wchar_t TestServerLauncher::kCertIssuerName[] = L"Test CA"; |
51 | 60 |
52 TestServerLauncher::TestServerLauncher() : process_handle_( | 61 TestServerLauncher::TestServerLauncher() |
53 base::kNullProcessHandle), | 62 : process_handle_(base::kNullProcessHandle) { |
54 forking_(false), | |
55 connection_attempts_(kDefaultTestConnectionAttempts), | |
56 connection_timeout_(kDefaultTestConnectionTimeout) | |
57 { | |
58 InitCertPath(); | 63 InitCertPath(); |
59 } | 64 } |
60 | 65 |
61 TestServerLauncher::TestServerLauncher(int connection_attempts, | |
62 int connection_timeout) | |
63 : process_handle_(base::kNullProcessHandle), | |
64 forking_(false), | |
65 connection_attempts_(connection_attempts), | |
66 connection_timeout_(connection_timeout) | |
67 { | |
68 InitCertPath(); | |
69 } | |
70 | |
71 void TestServerLauncher::InitCertPath() { | 66 void TestServerLauncher::InitCertPath() { |
72 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); | 67 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); |
73 cert_dir_ = cert_dir_.Append(FILE_PATH_LITERAL("net")) | 68 cert_dir_ = cert_dir_.Append(FILE_PATH_LITERAL("net")) |
74 .Append(FILE_PATH_LITERAL("data")) | 69 .Append(FILE_PATH_LITERAL("data")) |
75 .Append(FILE_PATH_LITERAL("ssl")) | 70 .Append(FILE_PATH_LITERAL("ssl")) |
76 .Append(FILE_PATH_LITERAL("certificates")); | 71 .Append(FILE_PATH_LITERAL("certificates")); |
77 } | 72 } |
78 | 73 |
79 namespace { | 74 namespace { |
80 | 75 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 .Append(FILE_PATH_LITERAL("testserver")) | 145 .Append(FILE_PATH_LITERAL("testserver")) |
151 .Append(FILE_PATH_LITERAL("testserver.py")); | 146 .Append(FILE_PATH_LITERAL("testserver.py")); |
152 | 147 |
153 PathService::Get(base::DIR_SOURCE_ROOT, &document_root_dir_); | 148 PathService::Get(base::DIR_SOURCE_ROOT, &document_root_dir_); |
154 document_root_dir_ = document_root_dir_.Append(document_root); | 149 document_root_dir_ = document_root_dir_.Append(document_root); |
155 | 150 |
156 SetPythonPath(); | 151 SetPythonPath(); |
157 | 152 |
158 #if defined(OS_WIN) | 153 #if defined(OS_WIN) |
159 // Get path to python interpreter | 154 // Get path to python interpreter |
160 if (!PathService::Get(base::DIR_SOURCE_ROOT, &python_runtime_)) | 155 FilePath python_exe; |
| 156 if (!PathService::Get(base::DIR_SOURCE_ROOT, &python_exe)) |
161 return false; | 157 return false; |
162 python_runtime_ = python_runtime_ | 158 python_exe = python_exe |
163 .Append(FILE_PATH_LITERAL("third_party")) | 159 .Append(FILE_PATH_LITERAL("third_party")) |
164 .Append(FILE_PATH_LITERAL("python_24")) | 160 .Append(FILE_PATH_LITERAL("python_24")) |
165 .Append(FILE_PATH_LITERAL("python.exe")); | 161 .Append(FILE_PATH_LITERAL("python.exe")); |
166 | 162 |
167 std::wstring command_line = | 163 std::wstring command_line = |
168 L"\"" + python_runtime_.ToWStringHack() + L"\" " + | 164 L"\"" + python_exe.ToWStringHack() + L"\" " + |
169 L"\"" + testserver_path.ToWStringHack() + | 165 L"\"" + testserver_path.ToWStringHack() + |
170 L"\" --port=" + UTF8ToWide(port_str) + | 166 L"\" --port=" + UTF8ToWide(port_str) + |
171 L" --data-dir=\"" + document_root_dir_.ToWStringHack() + L"\""; | 167 L" --data-dir=\"" + document_root_dir_.ToWStringHack() + L"\""; |
172 if (protocol == ProtoFTP) | 168 if (protocol == ProtoFTP) |
173 command_line.append(L" -f"); | 169 command_line.append(L" -f"); |
174 if (!cert_path.value().empty()) { | 170 if (!cert_path.value().empty()) { |
175 command_line.append(L" --https=\""); | 171 command_line.append(L" --https=\""); |
176 command_line.append(cert_path.ToWStringHack()); | 172 command_line.append(cert_path.ToWStringHack()); |
177 command_line.append(L"\""); | 173 command_line.append(L"\""); |
178 } | 174 } |
179 if (!file_root_url.empty()) { | 175 if (!file_root_url.empty()) { |
180 command_line.append(L" --file-root-url=\""); | 176 command_line.append(L" --file-root-url=\""); |
181 command_line.append(file_root_url); | 177 command_line.append(file_root_url); |
182 command_line.append(L"\""); | 178 command_line.append(L"\""); |
183 } | 179 } |
184 // Deliberately do not pass the --forking flag. It breaks the tests | |
185 // on Windows. | |
186 | 180 |
187 if (!LaunchTestServerAsJob(command_line, | 181 if (!LaunchTestServerAsJob(command_line, |
188 true, | 182 true, |
189 &process_handle_, | 183 &process_handle_, |
190 &job_handle_)) { | 184 &job_handle_)) { |
191 LOG(ERROR) << "Failed to launch " << command_line; | 185 LOG(ERROR) << "Failed to launch " << command_line; |
192 return false; | 186 return false; |
193 } | 187 } |
194 #elif defined(OS_POSIX) | 188 #elif defined(OS_POSIX) |
195 std::vector<std::string> command_line; | 189 std::vector<std::string> command_line; |
196 command_line.push_back("python"); | 190 command_line.push_back("python"); |
197 command_line.push_back(testserver_path.value()); | 191 command_line.push_back(testserver_path.value()); |
198 command_line.push_back("--port=" + port_str); | 192 command_line.push_back("--port=" + port_str); |
199 command_line.push_back("--data-dir=" + document_root_dir_.value()); | 193 command_line.push_back("--data-dir=" + document_root_dir_.value()); |
200 if (protocol == ProtoFTP) | 194 if (protocol == ProtoFTP) |
201 command_line.push_back("-f"); | 195 command_line.push_back("-f"); |
202 if (!cert_path.value().empty()) | 196 if (!cert_path.value().empty()) |
203 command_line.push_back("--https=" + cert_path.value()); | 197 command_line.push_back("--https=" + cert_path.value()); |
204 if (forking_) | |
205 command_line.push_back("--forking"); | |
206 | 198 |
207 base::file_handle_mapping_vector no_mappings; | 199 base::file_handle_mapping_vector no_mappings; |
208 LOG(INFO) << "Trying to launch " << command_line[0] << " ..."; | 200 LOG(INFO) << "Trying to launch " << command_line[0] << " ..."; |
209 if (!base::LaunchApp(command_line, no_mappings, false, &process_handle_)) { | 201 if (!base::LaunchApp(command_line, no_mappings, false, &process_handle_)) { |
210 LOG(ERROR) << "Failed to launch " << command_line[0] << " ..."; | 202 LOG(ERROR) << "Failed to launch " << command_line[0] << " ..."; |
211 return false; | 203 return false; |
212 } | 204 } |
213 #endif | 205 #endif |
214 | 206 |
215 // Let the server start, then verify that it's up. | 207 // Let the server start, then verify that it's up. |
(...skipping 14 matching lines...) Expand all Loading... |
230 // Otherwise tests can fail if they run faster than Python can start. | 222 // Otherwise tests can fail if they run faster than Python can start. |
231 net::AddressList addr; | 223 net::AddressList addr; |
232 scoped_refptr<net::HostResolver> resolver( | 224 scoped_refptr<net::HostResolver> resolver( |
233 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism)); | 225 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism)); |
234 net::HostResolver::RequestInfo info(host_name, port); | 226 net::HostResolver::RequestInfo info(host_name, port); |
235 int rv = resolver->Resolve(info, &addr, NULL, NULL, BoundNetLog()); | 227 int rv = resolver->Resolve(info, &addr, NULL, NULL, BoundNetLog()); |
236 if (rv != net::OK) | 228 if (rv != net::OK) |
237 return false; | 229 return false; |
238 | 230 |
239 net::TCPPinger pinger(addr); | 231 net::TCPPinger pinger(addr); |
240 rv = pinger.Ping(base::TimeDelta::FromMilliseconds(connection_timeout_), | 232 rv = pinger.Ping( |
241 connection_attempts_); | 233 base::TimeDelta::FromMilliseconds(kServerConnectionTimeoutMs), |
| 234 kServerConnectionAttempts); |
242 return rv == net::OK; | 235 return rv == net::OK; |
243 } | 236 } |
244 | 237 |
245 bool TestServerLauncher::WaitToFinish(int timeout_ms) { | 238 bool TestServerLauncher::WaitToFinish(int timeout_ms) { |
246 if (!process_handle_) | 239 if (!process_handle_) |
247 return true; | 240 return true; |
248 | 241 |
249 bool ret = base::WaitForSingleProcess(process_handle_, timeout_ms); | 242 bool ret = base::WaitForSingleProcess(process_handle_, timeout_ms); |
250 if (ret) { | 243 if (ret) { |
251 base::CloseProcessHandle(process_handle_); | 244 base::CloseProcessHandle(process_handle_); |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 process_info.hProcess)) { | 398 process_info.hProcess)) { |
406 LOG(ERROR) << "Could not AssignProcessToObject."; | 399 LOG(ERROR) << "Could not AssignProcessToObject."; |
407 return false; | 400 return false; |
408 } | 401 } |
409 } | 402 } |
410 return true; | 403 return true; |
411 } | 404 } |
412 #endif | 405 #endif |
413 | 406 |
414 } // namespace net | 407 } // namespace net |
OLD | NEW |