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 |
(...skipping 22 matching lines...) Expand all Loading... |
33 namespace net { | 33 namespace net { |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 // Number of connection attempts for tests. | 37 // Number of connection attempts for tests. |
38 const int kServerConnectionAttempts = 10; | 38 const int kServerConnectionAttempts = 10; |
39 | 39 |
40 // Connection timeout in milliseconds for tests. | 40 // Connection timeout in milliseconds for tests. |
41 const int kServerConnectionTimeoutMs = 1000; | 41 const int kServerConnectionTimeoutMs = 1000; |
42 | 42 |
43 const char kTestServerShardFlag[] = "test-server-shard"; | |
44 | |
45 int GetHTTPSPortBase(const TestServer::HTTPSOptions& options) { | |
46 if (options.request_client_certificate) | |
47 return 9543; | |
48 | |
49 switch (options.server_certificate) { | |
50 case TestServer::HTTPSOptions::CERT_OK: | |
51 return 9443; | |
52 case TestServer::HTTPSOptions::CERT_MISMATCHED_NAME: | |
53 return 9643; | |
54 case TestServer::HTTPSOptions::CERT_EXPIRED: | |
55 // TODO(phajdan.jr): Some tests rely on this hardcoded value. | |
56 // Some uses of this are actually in .html/.js files. | |
57 return 9666; | |
58 default: | |
59 NOTREACHED(); | |
60 } | |
61 return -1; | |
62 } | |
63 | |
64 int GetPortBase(TestServer::Type type, | |
65 const TestServer::HTTPSOptions& options) { | |
66 switch (type) { | |
67 case TestServer::TYPE_FTP: | |
68 return 3117; | |
69 case TestServer::TYPE_HTTP: | |
70 return 1337; | |
71 case TestServer::TYPE_HTTPS: | |
72 return GetHTTPSPortBase(options); | |
73 default: | |
74 NOTREACHED(); | |
75 } | |
76 return -1; | |
77 } | |
78 | |
79 int GetPort(TestServer::Type type, | |
80 const TestServer::HTTPSOptions& options) { | |
81 int port = GetPortBase(type, options); | |
82 if (CommandLine::ForCurrentProcess()->HasSwitch(kTestServerShardFlag)) { | |
83 std::string shard_str(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
84 kTestServerShardFlag)); | |
85 int shard = -1; | |
86 if (base::StringToInt(shard_str, &shard)) { | |
87 port += shard; | |
88 } else { | |
89 LOG(FATAL) << "Got invalid " << kTestServerShardFlag << " flag value. " | |
90 << "An integer is expected."; | |
91 } | |
92 } | |
93 return port; | |
94 } | |
95 | |
96 std::string GetHostname(TestServer::Type type, | 43 std::string GetHostname(TestServer::Type type, |
97 const TestServer::HTTPSOptions& options) { | 44 const TestServer::HTTPSOptions& options) { |
98 if (type == TestServer::TYPE_HTTPS && | 45 if (type == TestServer::TYPE_HTTPS && |
99 options.server_certificate == | 46 options.server_certificate == |
100 TestServer::HTTPSOptions::CERT_MISMATCHED_NAME) { | 47 TestServer::HTTPSOptions::CERT_MISMATCHED_NAME) { |
101 // Return a different hostname string that resolves to the same hostname. | 48 // Return a different hostname string that resolves to the same hostname. |
102 return "localhost"; | 49 return "localhost"; |
103 } | 50 } |
104 | 51 |
105 return "127.0.0.1"; | 52 return "127.0.0.1"; |
(...skipping 25 matching lines...) Expand all Loading... |
131 return FilePath(FILE_PATH_LITERAL("ok_cert.pem")); | 78 return FilePath(FILE_PATH_LITERAL("ok_cert.pem")); |
132 case CERT_EXPIRED: | 79 case CERT_EXPIRED: |
133 return FilePath(FILE_PATH_LITERAL("expired_cert.pem")); | 80 return FilePath(FILE_PATH_LITERAL("expired_cert.pem")); |
134 default: | 81 default: |
135 NOTREACHED(); | 82 NOTREACHED(); |
136 } | 83 } |
137 return FilePath(); | 84 return FilePath(); |
138 } | 85 } |
139 | 86 |
140 TestServer::TestServer(Type type, const FilePath& document_root) | 87 TestServer::TestServer(Type type, const FilePath& document_root) |
141 : type_(type) { | 88 : type_(type), |
| 89 started_(false) { |
142 Init(document_root); | 90 Init(document_root); |
143 } | 91 } |
144 | 92 |
145 TestServer::TestServer(const HTTPSOptions& https_options, | 93 TestServer::TestServer(const HTTPSOptions& https_options, |
146 const FilePath& document_root) | 94 const FilePath& document_root) |
147 : https_options_(https_options), type_(TYPE_HTTPS) { | 95 : https_options_(https_options), |
| 96 type_(TYPE_HTTPS), |
| 97 started_(false) { |
148 Init(document_root); | 98 Init(document_root); |
149 } | 99 } |
150 | 100 |
151 TestServer::~TestServer() { | 101 TestServer::~TestServer() { |
152 #if defined(OS_MACOSX) | 102 #if defined(OS_MACOSX) |
153 SetMacTestCertificate(NULL); | 103 SetMacTestCertificate(NULL); |
154 #endif | 104 #endif |
155 Stop(); | 105 Stop(); |
156 } | 106 } |
157 | 107 |
158 void TestServer::Init(const FilePath& document_root) { | 108 void TestServer::Init(const FilePath& document_root) { |
159 host_port_pair_ = HostPortPair(GetHostname(type_, https_options_), | 109 // At this point, the port that the testserver will listen on is unknown. |
160 GetPort(type_, https_options_)); | 110 // The testserver will listen on an ephemeral port, and write the port |
| 111 // number out over a pipe that this TestServer object will read from. Once |
| 112 // that is complete, the host_port_pair_ will contain the actual port. |
| 113 host_port_pair_ = HostPortPair(GetHostname(type_, https_options_), 0); |
161 process_handle_ = base::kNullProcessHandle; | 114 process_handle_ = base::kNullProcessHandle; |
162 | 115 |
163 FilePath src_dir; | 116 FilePath src_dir; |
164 PathService::Get(base::DIR_SOURCE_ROOT, &src_dir); | 117 PathService::Get(base::DIR_SOURCE_ROOT, &src_dir); |
165 | 118 |
166 document_root_ = src_dir.Append(document_root); | 119 document_root_ = src_dir.Append(document_root); |
167 | 120 |
168 certificates_dir_ = src_dir.Append(FILE_PATH_LITERAL("net")) | 121 certificates_dir_ = src_dir.Append(FILE_PATH_LITERAL("net")) |
169 .Append(FILE_PATH_LITERAL("data")) | 122 .Append(FILE_PATH_LITERAL("data")) |
170 .Append(FILE_PATH_LITERAL("ssl")) | 123 .Append(FILE_PATH_LITERAL("ssl")) |
(...skipping 24 matching lines...) Expand all Loading... |
195 return false; | 148 return false; |
196 | 149 |
197 if (!LaunchPython(testserver_path)) | 150 if (!LaunchPython(testserver_path)) |
198 return false; | 151 return false; |
199 | 152 |
200 if (!WaitToStart()) { | 153 if (!WaitToStart()) { |
201 Stop(); | 154 Stop(); |
202 return false; | 155 return false; |
203 } | 156 } |
204 | 157 |
| 158 started_ = true; |
205 return true; | 159 return true; |
206 } | 160 } |
207 | 161 |
208 bool TestServer::Stop() { | 162 bool TestServer::Stop() { |
209 if (!process_handle_) | 163 if (!process_handle_) |
210 return true; | 164 return true; |
211 | 165 |
| 166 started_ = false; |
| 167 |
212 // First check if the process has already terminated. | 168 // First check if the process has already terminated. |
213 bool ret = base::WaitForSingleProcess(process_handle_, 0); | 169 bool ret = base::WaitForSingleProcess(process_handle_, 0); |
214 if (!ret) | 170 if (!ret) |
215 ret = base::KillProcess(process_handle_, 1, true); | 171 ret = base::KillProcess(process_handle_, 1, true); |
216 | 172 |
217 if (ret) { | 173 if (ret) { |
218 base::CloseProcessHandle(process_handle_); | 174 base::CloseProcessHandle(process_handle_); |
219 process_handle_ = base::kNullProcessHandle; | 175 process_handle_ = base::kNullProcessHandle; |
220 } else { | 176 } else { |
221 VLOG(1) << "Kill failed?"; | 177 VLOG(1) << "Kill failed?"; |
222 } | 178 } |
223 | 179 |
224 return ret; | 180 return ret; |
225 } | 181 } |
226 | 182 |
| 183 const HostPortPair& TestServer::host_port_pair() const { |
| 184 DCHECK(started_); |
| 185 return host_port_pair_; |
| 186 } |
| 187 |
227 std::string TestServer::GetScheme() const { | 188 std::string TestServer::GetScheme() const { |
228 switch (type_) { | 189 switch (type_) { |
229 case TYPE_FTP: | 190 case TYPE_FTP: |
230 return "ftp"; | 191 return "ftp"; |
231 case TYPE_HTTP: | 192 case TYPE_HTTP: |
232 return "http"; | 193 return "http"; |
233 case TYPE_HTTPS: | 194 case TYPE_HTTPS: |
234 return "https"; | 195 return "https"; |
235 default: | 196 default: |
236 NOTREACHED(); | 197 NOTREACHED(); |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_AES256) | 345 if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_AES256) |
385 command_line->AppendSwitchASCII(kBulkCipherSwitch, "aes256"); | 346 command_line->AppendSwitchASCII(kBulkCipherSwitch, "aes256"); |
386 if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_3DES) | 347 if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_3DES) |
387 command_line->AppendSwitchASCII(kBulkCipherSwitch, "3des"); | 348 command_line->AppendSwitchASCII(kBulkCipherSwitch, "3des"); |
388 } | 349 } |
389 | 350 |
390 return true; | 351 return true; |
391 } | 352 } |
392 | 353 |
393 } // namespace net | 354 } // namespace net |
OLD | NEW |