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_MACOSX) | 13 #if defined(OS_MACOSX) |
14 #include "net/base/x509_certificate.h" | 14 #include "net/base/x509_certificate.h" |
15 #endif | 15 #endif |
16 | 16 |
17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
18 #include "base/debug/leak_annotations.h" | 18 #include "base/debug/leak_annotations.h" |
19 #include "base/file_util.h" | 19 #include "base/file_util.h" |
20 #include "base/logging.h" | 20 #include "base/logging.h" |
21 #include "base/path_service.h" | 21 #include "base/path_service.h" |
22 #include "base/string_number_conversions.h" | 22 #include "base/string_number_conversions.h" |
23 #include "base/utf_string_conversions.h" | 23 #include "base/utf_string_conversions.h" |
24 #include "googleurl/src/gurl.h" | 24 #include "googleurl/src/gurl.h" |
25 #include "net/base/cert_test_util.h" | |
26 #include "net/base/host_port_pair.h" | 25 #include "net/base/host_port_pair.h" |
27 #include "net/base/host_resolver.h" | 26 #include "net/base/host_resolver.h" |
27 #include "net/base/temporary_root_certs.h" | |
28 #include "net/base/test_completion_callback.h" | 28 #include "net/base/test_completion_callback.h" |
29 #include "net/socket/tcp_client_socket.h" | 29 #include "net/socket/tcp_client_socket.h" |
30 #include "net/test/python_utils.h" | 30 #include "net/test/python_utils.h" |
31 #include "testing/platform_test.h" | 31 #include "testing/platform_test.h" |
32 | 32 |
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. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 TestServer::HTTPSOptions::CERT_MISMATCHED_NAME) { | 100 TestServer::HTTPSOptions::CERT_MISMATCHED_NAME) { |
101 // Return a different hostname string that resolves to the same hostname. | 101 // Return a different hostname string that resolves to the same hostname. |
102 return "localhost"; | 102 return "localhost"; |
103 } | 103 } |
104 | 104 |
105 return "127.0.0.1"; | 105 return "127.0.0.1"; |
106 } | 106 } |
107 | 107 |
108 } // namespace | 108 } // namespace |
109 | 109 |
110 #if defined(OS_MACOSX) | |
111 void SetMacTestCertificate(X509Certificate* cert); | |
112 #endif | |
113 | |
114 TestServer::HTTPSOptions::HTTPSOptions() | 110 TestServer::HTTPSOptions::HTTPSOptions() |
115 : server_certificate(CERT_OK), | 111 : server_certificate(CERT_OK), |
116 request_client_certificate(false), | 112 request_client_certificate(false), |
117 bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY) {} | 113 bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY) {} |
118 | 114 |
119 TestServer::HTTPSOptions::HTTPSOptions( | 115 TestServer::HTTPSOptions::HTTPSOptions( |
120 TestServer::HTTPSOptions::ServerCertificate cert) | 116 TestServer::HTTPSOptions::ServerCertificate cert) |
121 : server_certificate(cert), | 117 : server_certificate(cert), |
122 request_client_certificate(false), | 118 request_client_certificate(false), |
123 bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY) {} | 119 bulk_ciphers(HTTPSOptions::BULK_CIPHER_ANY) {} |
(...skipping 18 matching lines...) Expand all Loading... | |
142 Init(document_root); | 138 Init(document_root); |
143 } | 139 } |
144 | 140 |
145 TestServer::TestServer(const HTTPSOptions& https_options, | 141 TestServer::TestServer(const HTTPSOptions& https_options, |
146 const FilePath& document_root) | 142 const FilePath& document_root) |
147 : https_options_(https_options), type_(TYPE_HTTPS) { | 143 : https_options_(https_options), type_(TYPE_HTTPS) { |
148 Init(document_root); | 144 Init(document_root); |
149 } | 145 } |
150 | 146 |
151 TestServer::~TestServer() { | 147 TestServer::~TestServer() { |
152 #if defined(OS_MACOSX) | 148 TemporaryRootCerts* root_certs = TemporaryRootCerts::GetInstance(); |
153 SetMacTestCertificate(NULL); | 149 root_certs->RemoveFromFile(GetRootCertificatePath()); |
154 #endif | |
155 Stop(); | 150 Stop(); |
156 } | 151 } |
157 | 152 |
158 void TestServer::Init(const FilePath& document_root) { | 153 void TestServer::Init(const FilePath& document_root) { |
159 host_port_pair_ = HostPortPair(GetHostname(type_, https_options_), | 154 host_port_pair_ = HostPortPair(GetHostname(type_, https_options_), |
160 GetPort(type_, https_options_)); | 155 GetPort(type_, https_options_)); |
161 process_handle_ = base::kNullProcessHandle; | 156 process_handle_ = base::kNullProcessHandle; |
162 | 157 |
163 FilePath src_dir; | 158 FilePath src_dir; |
164 PathService::Get(base::DIR_SOURCE_ROOT, &src_dir); | 159 PathService::Get(base::DIR_SOURCE_ROOT, &src_dir); |
165 | 160 |
166 document_root_ = src_dir.Append(document_root); | 161 document_root_ = src_dir.Append(document_root); |
167 | 162 |
168 certificates_dir_ = src_dir.Append(FILE_PATH_LITERAL("net")) | 163 certificates_dir_ = src_dir.Append(FILE_PATH_LITERAL("net")) |
169 .Append(FILE_PATH_LITERAL("data")) | 164 .Append(FILE_PATH_LITERAL("data")) |
170 .Append(FILE_PATH_LITERAL("ssl")) | 165 .Append(FILE_PATH_LITERAL("ssl")) |
171 .Append(FILE_PATH_LITERAL("certificates")); | 166 .Append(FILE_PATH_LITERAL("certificates")); |
172 } | 167 } |
173 | 168 |
174 bool TestServer::Start() { | 169 bool TestServer::Start() { |
175 if (type_ == TYPE_HTTPS) { | 170 if (type_ == TYPE_HTTPS) { |
176 if (!LoadTestRootCert()) | 171 if (!LoadTestRootCert()) |
177 return false; | 172 return false; |
178 if (!CheckCATrusted()) | |
179 return false; | |
180 } | 173 } |
181 | 174 |
182 // Get path to python server script | 175 // Get path to python server script |
183 FilePath testserver_path; | 176 FilePath testserver_path; |
184 if (!PathService::Get(base::DIR_SOURCE_ROOT, &testserver_path)) { | 177 if (!PathService::Get(base::DIR_SOURCE_ROOT, &testserver_path)) { |
185 LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT"; | 178 LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT"; |
186 return false; | 179 return false; |
187 } | 180 } |
188 testserver_path = testserver_path | 181 testserver_path = testserver_path |
189 .Append(FILE_PATH_LITERAL("net")) | 182 .Append(FILE_PATH_LITERAL("net")) |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
313 Append(FILE_PATH_LITERAL("sync_pb"))); | 306 Append(FILE_PATH_LITERAL("sync_pb"))); |
314 | 307 |
315 return true; | 308 return true; |
316 } | 309 } |
317 | 310 |
318 FilePath TestServer::GetRootCertificatePath() { | 311 FilePath TestServer::GetRootCertificatePath() { |
319 return certificates_dir_.AppendASCII("root_ca_cert.crt"); | 312 return certificates_dir_.AppendASCII("root_ca_cert.crt"); |
320 } | 313 } |
321 | 314 |
322 bool TestServer::LoadTestRootCert() { | 315 bool TestServer::LoadTestRootCert() { |
323 #if defined(USE_NSS) | |
324 if (cert_) | |
325 return true; | |
326 | |
327 // TODO(dkegel): figure out how to get this to only happen once? | 316 // TODO(dkegel): figure out how to get this to only happen once? |
bulach
2010/11/09 16:21:09
afaict, this is no longer necessary as TemporaryRo
| |
328 | 317 TemporaryRootCerts* root_certs = TemporaryRootCerts::GetInstance(); |
329 // This currently leaks a little memory. | 318 return root_certs->AddFromFile(GetRootCertificatePath()); |
330 // TODO(dkegel): fix the leak and remove the entry in | |
331 // tools/valgrind/memcheck/suppressions.txt | |
332 ANNOTATE_SCOPED_MEMORY_LEAK; // Tell heap checker about the leak. | |
333 cert_ = LoadTemporaryRootCert(GetRootCertificatePath()); | |
334 return (cert_ != NULL); | |
335 #elif defined(OS_MACOSX) | |
336 X509Certificate* cert = LoadTemporaryRootCert(GetRootCertificatePath()); | |
337 if (!cert) | |
338 return false; | |
339 SetMacTestCertificate(cert); | |
340 return true; | |
341 #else | |
342 return true; | |
343 #endif | |
344 } | 319 } |
345 | 320 |
346 bool TestServer::AddCommandLineArguments(CommandLine* command_line) const { | 321 bool TestServer::AddCommandLineArguments(CommandLine* command_line) const { |
347 command_line->AppendSwitchASCII("port", | 322 command_line->AppendSwitchASCII("port", |
348 base::IntToString(host_port_pair_.port())); | 323 base::IntToString(host_port_pair_.port())); |
349 command_line->AppendSwitchPath("data-dir", document_root_); | 324 command_line->AppendSwitchPath("data-dir", document_root_); |
350 | 325 |
351 if (type_ == TYPE_FTP) { | 326 if (type_ == TYPE_FTP) { |
352 command_line->AppendArg("-f"); | 327 command_line->AppendArg("-f"); |
353 } else if (type_ == TYPE_HTTPS) { | 328 } else if (type_ == TYPE_HTTPS) { |
(...skipping 30 matching lines...) Expand all Loading... | |
384 if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_AES256) | 359 if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_AES256) |
385 command_line->AppendSwitchASCII(kBulkCipherSwitch, "aes256"); | 360 command_line->AppendSwitchASCII(kBulkCipherSwitch, "aes256"); |
386 if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_3DES) | 361 if (https_options_.bulk_ciphers & HTTPSOptions::BULK_CIPHER_3DES) |
387 command_line->AppendSwitchASCII(kBulkCipherSwitch, "3des"); | 362 command_line->AppendSwitchASCII(kBulkCipherSwitch, "3des"); |
388 } | 363 } |
389 | 364 |
390 return true; | 365 return true; |
391 } | 366 } |
392 | 367 |
393 } // namespace net | 368 } // namespace net |
OLD | NEW |