| 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 #ifndef NET_TEST_TEST_SERVER_H_ | 5 #ifndef NET_TEST_TEST_SERVER_H_ |
| 6 #define NET_TEST_TEST_SERVER_H_ | 6 #define NET_TEST_TEST_SERVER_H_ |
| 7 | 7 |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/compiler_specific.h" |
| 12 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 13 #include "base/process_util.h" | 14 #include "base/process_util.h" |
| 14 | 15 |
| 15 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
| 16 #include "base/scoped_handle_win.h" | 17 #include "base/scoped_handle_win.h" |
| 17 #endif | 18 #endif |
| 18 | 19 |
| 19 #if defined(USE_NSS) | 20 #if defined(USE_NSS) |
| 20 #include "base/ref_counted.h" | 21 #include "base/ref_counted.h" |
| 21 #include "net/base/x509_certificate.h" | 22 #include "net/base/x509_certificate.h" |
| 22 #endif | 23 #endif |
| 23 | 24 |
| 24 namespace net { | 25 namespace net { |
| 25 | 26 |
| 26 // This object bounds the lifetime of an external python-based HTTP/HTTPS/FTP | 27 // This object bounds the lifetime of an external python-based HTTP/HTTPS/FTP |
| 27 // server that can provide various responses useful for testing. | 28 // server that can provide various responses useful for testing. |
| 28 // A few basic convenience methods are provided, but no | 29 // A few basic convenience methods are provided, but no |
| 29 // URL handling methods (those belong at a higher layer, e.g. in | 30 // URL handling methods (those belong at a higher layer, e.g. in |
| 30 // url_request_unittest.h). | 31 // url_request_unittest.h). |
| 31 | 32 |
| 32 class TestServerLauncher { | 33 class TestServerLauncher { |
| 33 public: | 34 public: |
| 34 TestServerLauncher(); | 35 TestServerLauncher(); |
| 35 TestServerLauncher(int connection_attempts, int connection_timeout); | |
| 36 | |
| 37 virtual ~TestServerLauncher(); | 36 virtual ~TestServerLauncher(); |
| 38 | 37 |
| 39 enum Protocol { | 38 enum Protocol { |
| 40 ProtoHTTP, ProtoFTP | 39 ProtoHTTP, ProtoFTP |
| 41 }; | 40 }; |
| 42 | 41 |
| 43 // Load the test root cert, if it hasn't been loaded yet. | 42 // Load the test root cert, if it hasn't been loaded yet. |
| 44 bool LoadTestRootCert(); | 43 bool LoadTestRootCert() WARN_UNUSED_RESULT; |
| 45 | |
| 46 // Tells the server to enable/disable servicing each request | |
| 47 // in a separate process. Takes effect only if called before Start. | |
| 48 void set_forking(bool forking) { forking_ = forking; } | |
| 49 | 44 |
| 50 // Start src/net/tools/testserver/testserver.py and | 45 // Start src/net/tools/testserver/testserver.py and |
| 51 // ask it to serve the given protocol. | 46 // ask it to serve the given protocol. |
| 52 // If protocol is HTTP, and cert_path is not empty, serves HTTPS. | 47 // If protocol is HTTP, and cert_path is not empty, serves HTTPS. |
| 53 // file_root_url specifies the root url on the server that documents will be | 48 // file_root_url specifies the root url on the server that documents will be |
| 54 // served out of. This is /files/ by default. | 49 // served out of. This is /files/ by default. |
| 55 // Returns true on success, false if files not found or root cert | 50 // Returns true on success, false if files not found or root cert |
| 56 // not trusted. | 51 // not trusted. |
| 57 bool Start(net::TestServerLauncher::Protocol protocol, | 52 bool Start(net::TestServerLauncher::Protocol protocol, |
| 58 const std::string& host_name, int port, | 53 const std::string& host_name, int port, |
| 59 const FilePath& document_root, | 54 const FilePath& document_root, |
| 60 const FilePath& cert_path, | 55 const FilePath& cert_path, |
| 61 const std::wstring& file_root_url); | 56 const std::wstring& file_root_url) WARN_UNUSED_RESULT; |
| 62 | 57 |
| 63 // Stop the server started by Start(). | 58 // Stop the server started by Start(). |
| 64 bool Stop(); | 59 bool Stop(); |
| 65 | 60 |
| 66 // If you access the server's Kill url, it will exit by itself | 61 // If you access the server's Kill url, it will exit by itself |
| 67 // without a call to Stop(). | 62 // without a call to Stop(). |
| 68 // WaitToFinish is handy in that case. | 63 // WaitToFinish is handy in that case. |
| 69 // It returns true if the server exited cleanly. | 64 // It returns true if the server exited cleanly. |
| 70 bool WaitToFinish(int milliseconds); | 65 bool WaitToFinish(int milliseconds) WARN_UNUSED_RESULT; |
| 71 | 66 |
| 72 // Paths to a good, an expired, and an invalid server certificate | 67 // Paths to a good, an expired, and an invalid server certificate |
| 73 // (use as arguments to Start()). | 68 // (use as arguments to Start()). |
| 74 FilePath GetOKCertPath(); | 69 FilePath GetOKCertPath(); |
| 75 FilePath GetExpiredCertPath(); | 70 FilePath GetExpiredCertPath(); |
| 76 | 71 |
| 77 FilePath GetDocumentRootPath() { return document_root_dir_; } | 72 FilePath GetDocumentRootPath() { return document_root_dir_; } |
| 78 | 73 |
| 79 // Issuer name of the root cert that should be trusted for the test to work. | 74 // Issuer name of the root cert that should be trusted for the test to work. |
| 80 static const wchar_t kCertIssuerName[]; | 75 static const wchar_t kCertIssuerName[]; |
| 81 | 76 |
| 82 // Hostname to use for test server | 77 // Hostname to use for test server |
| 83 static const char kHostName[]; | 78 static const char kHostName[]; |
| 84 | 79 |
| 85 // Different hostname to use for test server (that still resolves to same IP) | 80 // Different hostname to use for test server (that still resolves to same IP) |
| 86 static const char kMismatchedHostName[]; | 81 static const char kMismatchedHostName[]; |
| 87 | 82 |
| 88 // Port to use for test server | 83 // Port to use for test server |
| 89 static const int kOKHTTPSPort; | 84 static const int kOKHTTPSPort; |
| 90 | 85 |
| 91 // Port to use for bad test server | 86 // Port to use for bad test server |
| 92 static const int kBadHTTPSPort; | 87 static const int kBadHTTPSPort; |
| 93 | 88 |
| 94 private: | 89 private: |
| 95 // Wait a while for the server to start, return whether | 90 // Wait a while for the server to start, return whether |
| 96 // we were able to make a connection to it. | 91 // we were able to make a connection to it. |
| 97 bool WaitToStart(const std::string& host_name, int port); | 92 bool WaitToStart(const std::string& host_name, int port) WARN_UNUSED_RESULT; |
| 98 | 93 |
| 99 // Append to PYTHONPATH so Python can find pyftpdlib and tlslite. | 94 // Append to PYTHONPATH so Python can find pyftpdlib and tlslite. |
| 100 void SetPythonPath(); | 95 void SetPythonPath(); |
| 101 | 96 |
| 102 // Path to our test root certificate. | 97 // Path to our test root certificate. |
| 103 FilePath GetRootCertPath(); | 98 FilePath GetRootCertPath(); |
| 104 | 99 |
| 105 // Returns false if our test root certificate is not trusted. | 100 // Returns false if our test root certificate is not trusted. |
| 106 bool CheckCATrusted(); | 101 bool CheckCATrusted() WARN_UNUSED_RESULT; |
| 107 | 102 |
| 108 // Initilize the certificate path. | 103 // Initilize the certificate path. |
| 109 void InitCertPath(); | 104 void InitCertPath(); |
| 110 | 105 |
| 111 FilePath document_root_dir_; | 106 FilePath document_root_dir_; |
| 112 | 107 |
| 113 FilePath cert_dir_; | 108 FilePath cert_dir_; |
| 114 | 109 |
| 115 FilePath python_runtime_; | |
| 116 | |
| 117 base::ProcessHandle process_handle_; | 110 base::ProcessHandle process_handle_; |
| 118 | 111 |
| 119 #if defined(OS_WIN) | 112 #if defined(OS_WIN) |
| 120 // JobObject used to clean up orphaned child processes. | 113 // JobObject used to clean up orphaned child processes. |
| 121 ScopedHandle job_handle_; | 114 ScopedHandle job_handle_; |
| 122 #endif | 115 #endif |
| 123 | 116 |
| 124 // True if the server should handle each request in a separate process. | |
| 125 bool forking_; | |
| 126 | |
| 127 // Number of tries and timeout for each try used for WaitToStart. | |
| 128 int connection_attempts_; | |
| 129 int connection_timeout_; | |
| 130 | |
| 131 #if defined(USE_NSS) | 117 #if defined(USE_NSS) |
| 132 scoped_refptr<X509Certificate> cert_; | 118 scoped_refptr<X509Certificate> cert_; |
| 133 #endif | 119 #endif |
| 134 | 120 |
| 135 DISALLOW_COPY_AND_ASSIGN(TestServerLauncher); | 121 DISALLOW_COPY_AND_ASSIGN(TestServerLauncher); |
| 136 }; | 122 }; |
| 137 | 123 |
| 138 #if defined(OS_WIN) | 124 #if defined(OS_WIN) |
| 139 // Launch test server as a job so that it is not orphaned if the test case is | 125 // Launch test server as a job so that it is not orphaned if the test case is |
| 140 // abnormally terminated. | 126 // abnormally terminated. |
| 141 bool LaunchTestServerAsJob(const std::wstring& cmdline, | 127 bool LaunchTestServerAsJob(const std::wstring& cmdline, |
| 142 bool start_hidden, | 128 bool start_hidden, |
| 143 base::ProcessHandle* process_handle, | 129 base::ProcessHandle* process_handle, |
| 144 ScopedHandle* job_handle); | 130 ScopedHandle* job_handle); |
| 145 #endif | 131 #endif |
| 146 | 132 |
| 147 } // namespace net | 133 } // namespace net |
| 148 | 134 |
| 149 #endif // NET_TEST_TEST_SERVER_H_ | 135 #endif // NET_TEST_TEST_SERVER_H_ |
| OLD | NEW |