| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/embedded_test_server/embedded_test_server.h" | 5 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 | 45 |
| 46 EmbeddedTestServer::EmbeddedTestServer(Type type) | 46 EmbeddedTestServer::EmbeddedTestServer(Type type) |
| 47 : is_using_ssl_(type == TYPE_HTTPS), | 47 : is_using_ssl_(type == TYPE_HTTPS), |
| 48 connection_listener_(nullptr), | 48 connection_listener_(nullptr), |
| 49 port_(0), | 49 port_(0), |
| 50 cert_(CERT_OK), | 50 cert_(CERT_OK), |
| 51 weak_factory_(this) { | 51 weak_factory_(this) { |
| 52 DCHECK(thread_checker_.CalledOnValidThread()); | 52 DCHECK(thread_checker_.CalledOnValidThread()); |
| 53 | 53 |
| 54 if (is_using_ssl_) { | 54 if (is_using_ssl_) { |
| 55 base::ThreadRestrictions::ScopedAllowIO allow_io_for_importing_test_cert; |
| 55 TestRootCerts* root_certs = TestRootCerts::GetInstance(); | 56 TestRootCerts* root_certs = TestRootCerts::GetInstance(); |
| 56 base::FilePath certs_dir(GetTestCertsDirectory()); | 57 base::FilePath certs_dir(GetTestCertsDirectory()); |
| 57 root_certs->AddFromFile(certs_dir.AppendASCII("root_ca_cert.pem")); | 58 root_certs->AddFromFile(certs_dir.AppendASCII("root_ca_cert.pem")); |
| 58 } | 59 } |
| 59 } | 60 } |
| 60 | 61 |
| 61 EmbeddedTestServer::~EmbeddedTestServer() { | 62 EmbeddedTestServer::~EmbeddedTestServer() { |
| 62 DCHECK(thread_checker_.CalledOnValidThread()); | 63 DCHECK(thread_checker_.CalledOnValidThread()); |
| 63 | 64 |
| 64 if (Started() && !ShutdownAndWaitUntilComplete()) { | 65 if (Started() && !ShutdownAndWaitUntilComplete()) { |
| 65 LOG(ERROR) << "EmbeddedTestServer failed to shut down."; | 66 LOG(ERROR) << "EmbeddedTestServer failed to shut down."; |
| 66 } | 67 } |
| 68 |
| 69 { |
| 70 // Thread::Join induced by test code should cause an assert. |
| 71 base::ThreadRestrictions::ScopedAllowIO allow_io_for_thread_join; |
| 72 |
| 73 io_thread_.reset(); |
| 74 } |
| 67 } | 75 } |
| 68 | 76 |
| 69 void EmbeddedTestServer::SetConnectionListener( | 77 void EmbeddedTestServer::SetConnectionListener( |
| 70 EmbeddedTestServerConnectionListener* listener) { | 78 EmbeddedTestServerConnectionListener* listener) { |
| 71 DCHECK(!Started()); | 79 DCHECK(!Started()); |
| 72 connection_listener_ = listener; | 80 connection_listener_ = listener; |
| 73 } | 81 } |
| 74 | 82 |
| 75 bool EmbeddedTestServer::Start() { | 83 bool EmbeddedTestServer::Start() { |
| 76 bool success = InitializeAndListen(); | 84 bool success = InitializeAndListen(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 port_ = local_endpoint_.port(); | 119 port_ = local_endpoint_.port(); |
| 112 | 120 |
| 113 listen_socket_->DetachFromThread(); | 121 listen_socket_->DetachFromThread(); |
| 114 | 122 |
| 115 if (is_using_ssl_) | 123 if (is_using_ssl_) |
| 116 InitializeSSLServerContext(); | 124 InitializeSSLServerContext(); |
| 117 return true; | 125 return true; |
| 118 } | 126 } |
| 119 | 127 |
| 120 void EmbeddedTestServer::InitializeSSLServerContext() { | 128 void EmbeddedTestServer::InitializeSSLServerContext() { |
| 129 base::ThreadRestrictions::ScopedAllowIO allow_io_for_ssl_initialization; |
| 121 base::FilePath certs_dir(GetTestCertsDirectory()); | 130 base::FilePath certs_dir(GetTestCertsDirectory()); |
| 122 std::string cert_name = GetCertificateName(); | 131 std::string cert_name = GetCertificateName(); |
| 123 | 132 |
| 124 base::FilePath key_path = certs_dir.AppendASCII(cert_name); | 133 base::FilePath key_path = certs_dir.AppendASCII(cert_name); |
| 125 std::string key_string; | 134 std::string key_string; |
| 126 CHECK(base::ReadFileToString(key_path, &key_string)); | 135 CHECK(base::ReadFileToString(key_path, &key_string)); |
| 127 std::vector<std::string> headers; | 136 std::vector<std::string> headers; |
| 128 headers.push_back("PRIVATE KEY"); | 137 headers.push_back("PRIVATE KEY"); |
| 129 PEMTokenizer pem_tokenizer(key_string, headers); | 138 PEMTokenizer pem_tokenizer(key_string, headers); |
| 130 pem_tokenizer.GetNext(); | 139 pem_tokenizer.GetNext(); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 case CERT_BAD_VALIDITY: | 260 case CERT_BAD_VALIDITY: |
| 252 return "bad_validity.pem"; | 261 return "bad_validity.pem"; |
| 253 } | 262 } |
| 254 | 263 |
| 255 return "ok_cert.pem"; | 264 return "ok_cert.pem"; |
| 256 } | 265 } |
| 257 | 266 |
| 258 scoped_refptr<X509Certificate> EmbeddedTestServer::GetCertificate() const { | 267 scoped_refptr<X509Certificate> EmbeddedTestServer::GetCertificate() const { |
| 259 DCHECK(is_using_ssl_); | 268 DCHECK(is_using_ssl_); |
| 260 base::FilePath certs_dir(GetTestCertsDirectory()); | 269 base::FilePath certs_dir(GetTestCertsDirectory()); |
| 270 |
| 271 base::ThreadRestrictions::ScopedAllowIO allow_io_for_importing_test_cert; |
| 261 return ImportCertFromFile(certs_dir, GetCertificateName()); | 272 return ImportCertFromFile(certs_dir, GetCertificateName()); |
| 262 } | 273 } |
| 263 | 274 |
| 264 void EmbeddedTestServer::ServeFilesFromDirectory( | 275 void EmbeddedTestServer::ServeFilesFromDirectory( |
| 265 const base::FilePath& directory) { | 276 const base::FilePath& directory) { |
| 266 RegisterRequestHandler(base::Bind(&HandleFileRequest, directory)); | 277 RegisterRequestHandler(base::Bind(&HandleFileRequest, directory)); |
| 267 } | 278 } |
| 268 | 279 |
| 269 void EmbeddedTestServer::ServeFilesFromSourceDirectory( | 280 void EmbeddedTestServer::ServeFilesFromSourceDirectory( |
| 270 const std::string& relative) { | 281 const std::string& relative) { |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 run_loop.QuitClosure())) { | 463 run_loop.QuitClosure())) { |
| 453 return false; | 464 return false; |
| 454 } | 465 } |
| 455 run_loop.Run(); | 466 run_loop.Run(); |
| 456 | 467 |
| 457 return true; | 468 return true; |
| 458 } | 469 } |
| 459 | 470 |
| 460 } // namespace test_server | 471 } // namespace test_server |
| 461 } // namespace net | 472 } // namespace net |
| OLD | NEW |