1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/test/test_server.h"
#include <algorithm>
#include <string>
#include <vector>
#include "build/build_config.h"
#if defined(OS_MACOSX)
#include "net/base/x509_certificate.h"
#endif
#include "base/command_line.h"
#include "base/debug/leak_annotations.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "googleurl/src/gurl.h"
#include "net/base/cert_test_util.h"
#include "net/base/host_port_pair.h"
#include "net/base/host_resolver.h"
#include "net/base/test_completion_callback.h"
#include "net/socket/tcp_client_socket.h"
#include "net/test/python_utils.h"
#include "testing/platform_test.h"
namespace net {
namespace {
// Number of connection attempts for tests.
const int kServerConnectionAttempts = 10;
// Connection timeout in milliseconds for tests.
const int kServerConnectionTimeoutMs = 1000;
const char kTestServerShardFlag[] = "test-server-shard";
int GetHTTPSPortBase(const TestServer::HTTPSOptions& options) {
if (options.request_client_certificate)
return 9543;
switch (options.server_certificate) {
case TestServer::HTTPSOptions::CERT_OK:
return 9443;
case TestServer::HTTPSOptions::CERT_MISMATCHED_NAME:
return 9643;
case TestServer::HTTPSOptions::CERT_EXPIRED:
// TODO(phajdan.jr): Some tests rely on this hardcoded value.
// Some uses of this are actually in .html/.js files.
return 9666;
default:
NOTREACHED();
}
return -1;
}
int GetPortBase(TestServer::Type type,
const TestServer::HTTPSOptions& options) {
switch (type) {
case TestServer::TYPE_FTP:
return 3117;
case TestServer::TYPE_HTTP:
return 1337;
case TestServer::TYPE_HTTPS:
return GetHTTPSPortBase(options);
default:
NOTREACHED();
}
return -1;
}
int GetPort(TestServer::Type type,
const TestServer::HTTPSOptions& options) {
int port = GetPortBase(type, options);
if (CommandLine::ForCurrentProcess()->HasSwitch(kTestServerShardFlag)) {
std::string shard_str(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
kTestServerShardFlag));
int shard = -1;
if (base::StringToInt(shard_str, &shard)) {
port += shard;
} else {
LOG(FATAL) << "Got invalid " << kTestServerShardFlag << " flag value. "
<< "An integer is expected.";
}
}
return port;
}
std::string GetHostname(TestServer::Type type,
const TestServer::HTTPSOptions& options) {
if (type == TestServer::TYPE_HTTPS &&
options.server_certificate ==
TestServer::HTTPSOptions::CERT_MISMATCHED_NAME) {
// Return a different hostname string that resolves to the same hostname.
return "localhost";
}
return "127.0.0.1";
}
} // namespace
#if defined(OS_MACOSX)
void SetMacTestCertificate(X509Certificate* cert);
#endif
TestServer::HTTPSOptions::HTTPSOptions()
: server_certificate(CERT_OK),
request_client_certificate(false),
bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY) {}
TestServer::HTTPSOptions::HTTPSOptions(
TestServer::HTTPSOptions::ServerCertificate cert)
: server_certificate(cert),
request_client_certificate(false),
bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY) {}
TestServer::HTTPSOptions::~HTTPSOptions() {}
FilePath TestServer::HTTPSOptions::GetCertificateFile() const {
switch (server_certificate) {
case CERT_OK:
case CERT_MISMATCHED_NAME:
return FilePath(FILE_PATH_LITERAL("ok_cert.pem"));
case CERT_EXPIRED:
return FilePath(FILE_PATH_LITERAL("expired_cert.pem"));
default:
NOTREACHED();
}
return FilePath();
}
TestServer::TestServer(Type type, const FilePath& document_root)
: type_(type) {
Init(document_root);
}
TestServer::TestServer(const HTTPSOptions& https_options,
const FilePath& document_root)
: https_options_(https_options), type_(TYPE_HTTPS) {
Init(document_root);
}
TestServer::~TestServer() {
#if defined(OS_MACOSX)
SetMacTestCertificate(NULL);
#endif
Stop();
}
void TestServer::Init(const FilePath& document_root) {
host_port_pair_ = HostPortPair(GetHostname(type_, https_options_),
GetPort(type_, https_options_));
process_handle_ = base::kNullProcessHandle;
FilePath src_dir;
PathService::Get(base::DIR_SOURCE_ROOT, &src_dir);
document_root_ = src_dir.Append(document_root);
certificates_dir_ = src_dir.Append(FILE_PATH_LITERAL("net"))
.Append(FILE_PATH_LITERAL("data"))
.Append(FILE_PATH_LITERAL("ssl"))
.Append(FILE_PATH_LITERAL("certificates"));
}
bool TestServer::Start() {
if (type_ == TYPE_HTTPS) {
if (!LoadTestRootCert())
return false;
if (!CheckCATrusted())
return false;
}
// Get path to python server script
FilePath testserver_path;
if (!PathService::Get(base::DIR_SOURCE_ROOT, &testserver_path)) {
LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT";
return false;
}
testserver_path = testserver_path
.Append(FILE_PATH_LITERAL("net"))
.Append(FILE_PATH_LITERAL("tools"))
.Append(FILE_PATH_LITERAL("testserver"))
.Append(FILE_PATH_LITERAL("testserver.py"));
if (!SetPythonPath())
return false;
if (!LaunchPython(testserver_path))
return false;
if (!WaitToStart()) {
Stop();
return false;
}
return true;
}
bool TestServer::Stop() {
if (!process_handle_)
return true;
// First check if the process has already terminated.
bool ret = base::WaitForSingleProcess(process_handle_, 0);
if (!ret)
ret = base::KillProcess(process_handle_, 1, true);
if (ret) {
base::CloseProcessHandle(process_handle_);
process_handle_ = base::kNullProcessHandle;
} else {
VLOG(1) << "Kill failed?";
}
return ret;
}
std::string TestServer::GetScheme() const {
switch (type_) {
case TYPE_FTP:
return "ftp";
case TYPE_HTTP:
return "http";
case TYPE_HTTPS:
return "https";
default:
NOTREACHED();
}
return std::string();
}
bool TestServer::GetAddressList(AddressList* address_list) const {
DCHECK(address_list);
scoped_ptr<HostResolver> resolver(
CreateSystemHostResolver(HostResolver::kDefaultParallelism, NULL));
HostResolver::RequestInfo info(host_port_pair_);
int rv = resolver->Resolve(info, address_list, NULL, NULL, BoundNetLog());
if (rv != net::OK) {
LOG(ERROR) << "Failed to resolve hostname: " << host_port_pair_.host();
return false;
}
return true;
}
GURL TestServer::GetURL(const std::string& path) {
return GURL(GetScheme() + "://" + host_port_pair_.ToString() +
"/" + path);
}
GURL TestServer::GetURLWithUser(const std::string& path,
const std::string& user) {
return GURL(GetScheme() + "://" + user + "@" +
host_port_pair_.ToString() +
"/" + path);
}
GURL TestServer::GetURLWithUserAndPassword(const std::string& path,
const std::string& user,
const std::string& password) {
return GURL(GetScheme() + "://" + user + ":" + password +
"@" + host_port_pair_.ToString() +
"/" + path);
}
bool TestServer::SetPythonPath() {
FilePath third_party_dir;
if (!PathService::Get(base::DIR_SOURCE_ROOT, &third_party_dir)) {
LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT";
return false;
}
third_party_dir = third_party_dir.Append(FILE_PATH_LITERAL("third_party"));
AppendToPythonPath(third_party_dir.Append(FILE_PATH_LITERAL("tlslite")));
AppendToPythonPath(third_party_dir.Append(FILE_PATH_LITERAL("pyftpdlib")));
// Locate the Python code generated by the protocol buffers compiler.
FilePath generated_code_dir;
if (!PathService::Get(base::DIR_EXE, &generated_code_dir)) {
LOG(ERROR) << "Failed to get DIR_EXE";
return false;
}
static const FilePath kPyProto(FILE_PATH_LITERAL("pyproto"));
#if defined(OS_MACOSX)
// On Mac, DIR_EXE might be pointing deep into the Release/ (or Debug/)
// directory and we can't depend on how far down it goes. So we walk upwards
// from DIR_EXE until we find a likely looking spot.
while (!file_util::DirectoryExists(generated_code_dir.Append(kPyProto))) {
FilePath parent = generated_code_dir.DirName();
if (parent == generated_code_dir) {
// We hit the root directory. Maybe we didn't build any targets which
// produced Python protocol buffers.
PathService::Get(base::DIR_EXE, &generated_code_dir);
break;
}
generated_code_dir = parent;
}
#endif
AppendToPythonPath(generated_code_dir.Append(kPyProto));
AppendToPythonPath(generated_code_dir.Append(kPyProto).
Append(FILE_PATH_LITERAL("sync_pb")));
return true;
}
FilePath TestServer::GetRootCertificatePath() {
return certificates_dir_.AppendASCII("root_ca_cert.crt");
}
bool TestServer::LoadTestRootCert() {
#if defined(USE_NSS)
if (cert_)
return true;
// TODO(dkegel): figure out how to get this to only happen once?
// This currently leaks a little memory.
// TODO(dkegel): fix the leak and remove the entry in
// tools/valgrind/memcheck/suppressions.txt
ANNOTATE_SCOPED_MEMORY_LEAK; // Tell heap checker about the leak.
cert_ = LoadTemporaryRootCert(GetRootCertificatePath());
return (cert_ != NULL);
#elif defined(OS_MACOSX)
X509Certificate* cert = LoadTemporaryRootCert(GetRootCertificatePath());
if (!cert)
return false;
SetMacTestCertificate(cert);
return true;
#else
return true;
#endif
}
bool TestServer::AddCommandLineArguments(CommandLine* command_line) const {
command_line->AppendSwitchASCII("port",
base::IntToString(host_port_pair_.port()));
command_line->AppendSwitchPath("data-dir", document_root_);
if (type_ == TYPE_FTP) {
command_line->AppendArg("-f");
} else if (type_ == TYPE_HTTPS) {
FilePath certificate_path(certificates_dir_);
certificate_path = certificate_path.Append(
https_options_.GetCertificateFile());
if (!file_util::PathExists(certificate_path)) {
LOG(ERROR) << "Certificate path " << certificate_path.value()
<< " doesn't exist. Can't launch https server.";
return false;
}
command_line->AppendSwitchPath("https", certificate_path);
if (https_options_.request_client_certificate)
command_line->AppendSwitch("ssl-client-auth");
for (std::vector<FilePath>::const_iterator it =
https_options_.client_authorities.begin();
it != https_options_.client_authorities.end(); ++it) {
if (!file_util::PathExists(*it)) {
LOG(ERROR) << "Client authority path " << it->value()
<< " doesn't exist. Can't launch https server.";
return false;
}
command_line->AppendSwitchPath("ssl-client-ca", *it);
}
const char kBulkCipherSwitch[] = "ssl-bulk-cipher";
if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_RC4)
command_line->AppendSwitchASCII(kBulkCipherSwitch, "rc4");
if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_AES128)
command_line->AppendSwitchASCII(kBulkCipherSwitch, "aes128");
if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_AES256)
command_line->AppendSwitchASCII(kBulkCipherSwitch, "aes256");
if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_3DES)
command_line->AppendSwitchASCII(kBulkCipherSwitch, "3des");
}
return true;
}
} // namespace net
|