Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: net/test/test_server.h

Issue 3034038: GTTF: Move more test server code from net/url_request/url_request_unittest.h (Closed)
Patch Set: hopefully final Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/proxy/proxy_resolver_perftest.cc ('k') | net/tools/testserver/run_testserver.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #pragma once 7 #pragma once
8 8
9 #include "build/build_config.h" 9 #include "build/build_config.h"
10 10
11 #include <string> 11 #include <string>
12 12
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/file_path.h" 14 #include "base/file_path.h"
15 #include "base/process_util.h" 15 #include "base/process_util.h"
16 #include "base/ref_counted.h"
17 #include "base/string_util.h"
18 #include "googleurl/src/gurl.h"
16 19
17 #if defined(OS_WIN) 20 #if defined(OS_WIN)
18 #include "base/scoped_handle_win.h" 21 #include "base/scoped_handle_win.h"
19 #endif 22 #endif
20 23
21 #if defined(USE_NSS) 24 #if defined(USE_NSS)
22 #include "base/ref_counted.h"
23 #include "net/base/x509_certificate.h" 25 #include "net/base/x509_certificate.h"
24 #endif 26 #endif
25 27
26 namespace net { 28 namespace net {
27 29
30 const int kHTTPDefaultPort = 1337;
31 const int kFTPDefaultPort = 1338;
32
33 const char kDefaultHostName[] = "localhost";
34
28 // This object bounds the lifetime of an external python-based HTTP/HTTPS/FTP 35 // This object bounds the lifetime of an external python-based HTTP/HTTPS/FTP
29 // server that can provide various responses useful for testing. 36 // server that can provide various responses useful for testing.
30 // A few basic convenience methods are provided, but no
31 // URL handling methods (those belong at a higher layer, e.g. in
32 // url_request_unittest.h).
33
34 class TestServerLauncher { 37 class TestServerLauncher {
35 public: 38 public:
36 TestServerLauncher(); 39 TestServerLauncher();
37 virtual ~TestServerLauncher(); 40 virtual ~TestServerLauncher();
38 41
39 enum Protocol { 42 enum Protocol {
40 ProtoHTTP, ProtoFTP 43 ProtoHTTP, ProtoFTP
41 }; 44 };
42 45
43 // Load the test root cert, if it hasn't been loaded yet. 46 // Load the test root cert, if it hasn't been loaded yet.
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 127
125 #if defined(OS_WIN) 128 #if defined(OS_WIN)
126 // Launch test server as a job so that it is not orphaned if the test case is 129 // Launch test server as a job so that it is not orphaned if the test case is
127 // abnormally terminated. 130 // abnormally terminated.
128 bool LaunchTestServerAsJob(const std::wstring& cmdline, 131 bool LaunchTestServerAsJob(const std::wstring& cmdline,
129 bool start_hidden, 132 bool start_hidden,
130 base::ProcessHandle* process_handle, 133 base::ProcessHandle* process_handle,
131 ScopedHandle* job_handle); 134 ScopedHandle* job_handle);
132 #endif 135 #endif
133 136
137 // This object bounds the lifetime of an external python-based HTTP/FTP server
138 // that can provide various responses useful for testing.
139 class BaseTestServer : public base::RefCounted<BaseTestServer> {
140 protected:
141 BaseTestServer() {}
142
143 public:
144 bool WaitToFinish(int milliseconds) {
145 return launcher_.WaitToFinish(milliseconds);
146 }
147
148 bool Stop() {
149 return launcher_.Stop();
150 }
151
152 GURL TestServerPage(const std::string& base_address,
153 const std::string& path) {
154 return GURL(base_address + path);
155 }
156
157 GURL TestServerPage(const std::string& path) {
158 // TODO(phajdan.jr): Check for problems with IPv6.
159 return GURL(scheme_ + "://" + host_name_ + ":" + port_str_ + "/" + path);
160 }
161
162 GURL TestServerPage(const std::string& path,
163 const std::string& user,
164 const std::string& password) {
165 // TODO(phajdan.jr): Check for problems with IPv6.
166
167 if (password.empty())
168 return GURL(scheme_ + "://" + user + "@" +
169 host_name_ + ":" + port_str_ + "/" + path);
170
171 return GURL(scheme_ + "://" + user + ":" + password +
172 "@" + host_name_ + ":" + port_str_ + "/" + path);
173 }
174
175 FilePath GetDataDirectory() {
176 return launcher_.GetDocumentRootPath();
177 }
178
179 protected:
180 friend class base::RefCounted<BaseTestServer>;
181 virtual ~BaseTestServer() { }
182
183 bool Start(net::TestServerLauncher::Protocol protocol,
184 const std::string& host_name, int port,
185 const FilePath& document_root,
186 const FilePath& cert_path,
187 const std::wstring& file_root_url) {
188 if (!launcher_.Start(protocol,
189 host_name, port, document_root, cert_path, file_root_url))
190 return false;
191
192 if (protocol == net::TestServerLauncher::ProtoFTP)
193 scheme_ = "ftp";
194 else
195 scheme_ = "http";
196 if (!cert_path.empty())
197 scheme_.push_back('s');
198
199 host_name_ = host_name;
200 port_str_ = IntToString(port);
201 return true;
202 }
203
204 net::TestServerLauncher launcher_;
205 std::string scheme_;
206 std::string host_name_;
207 std::string port_str_;
208 };
209
210 class HTTPTestServer : public BaseTestServer {
211 protected:
212 HTTPTestServer() {}
213
214 public:
215 // Creates and returns a new HTTPTestServer.
216 static scoped_refptr<HTTPTestServer> CreateServer(
217 const std::wstring& document_root) {
218 return CreateServerWithFileRootURL(document_root, std::wstring());
219 }
220
221 static scoped_refptr<HTTPTestServer> CreateServerWithFileRootURL(
222 const std::wstring& document_root,
223 const std::wstring& file_root_url) {
224 scoped_refptr<HTTPTestServer> test_server(new HTTPTestServer());
225 FilePath no_cert;
226 FilePath docroot = FilePath::FromWStringHack(document_root);
227 if (!StartTestServer(test_server.get(), docroot, no_cert, file_root_url))
228 return NULL;
229 return test_server;
230 }
231
232 static bool StartTestServer(HTTPTestServer* server,
233 const FilePath& document_root,
234 const FilePath& cert_path,
235 const std::wstring& file_root_url) {
236 return server->Start(net::TestServerLauncher::ProtoHTTP, kDefaultHostName,
237 kHTTPDefaultPort, document_root, cert_path,
238 file_root_url);
239 }
240 };
241
242 class HTTPSTestServer : public HTTPTestServer {
243 protected:
244 HTTPSTestServer() {}
245
246 public:
247 // Create a server with a valid certificate
248 // TODO(dkegel): HTTPSTestServer should not require an instance to specify
249 // stock test certificates
250 static scoped_refptr<HTTPSTestServer> CreateGoodServer(
251 const std::wstring& document_root) {
252 scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer();
253 FilePath docroot = FilePath::FromWStringHack(document_root);
254 FilePath certpath = test_server->launcher_.GetOKCertPath();
255 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP,
256 net::TestServerLauncher::kHostName,
257 net::TestServerLauncher::kOKHTTPSPort,
258 docroot, certpath, std::wstring())) {
259 return NULL;
260 }
261 return test_server;
262 }
263
264 // Create a server with an up to date certificate for the wrong hostname
265 // for this host
266 static scoped_refptr<HTTPSTestServer> CreateMismatchedServer(
267 const std::wstring& document_root) {
268 scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer();
269 FilePath docroot = FilePath::FromWStringHack(document_root);
270 FilePath certpath = test_server->launcher_.GetOKCertPath();
271 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP,
272 net::TestServerLauncher::kMismatchedHostName,
273 net::TestServerLauncher::kOKHTTPSPort,
274 docroot, certpath, std::wstring())) {
275 return NULL;
276 }
277 return test_server;
278 }
279
280 // Create a server with an expired certificate
281 static scoped_refptr<HTTPSTestServer> CreateExpiredServer(
282 const std::wstring& document_root) {
283 scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer();
284 FilePath docroot = FilePath::FromWStringHack(document_root);
285 FilePath certpath = test_server->launcher_.GetExpiredCertPath();
286 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP,
287 net::TestServerLauncher::kHostName,
288 net::TestServerLauncher::kBadHTTPSPort,
289 docroot, certpath, std::wstring())) {
290 return NULL;
291 }
292 return test_server;
293 }
294
295 // Create a server with an arbitrary certificate
296 static scoped_refptr<HTTPSTestServer> CreateServer(
297 const std::string& host_name, int port,
298 const std::wstring& document_root,
299 const std::wstring& cert_path) {
300 scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer();
301 FilePath docroot = FilePath::FromWStringHack(document_root);
302 FilePath certpath = FilePath::FromWStringHack(cert_path);
303 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP,
304 host_name, port, docroot, certpath, std::wstring())) {
305 return NULL;
306 }
307 return test_server;
308 }
309
310 protected:
311 std::wstring cert_path_;
312
313 private:
314 virtual ~HTTPSTestServer() {}
315 };
316
317 class FTPTestServer : public BaseTestServer {
318 public:
319 FTPTestServer() {
320 }
321
322 static scoped_refptr<FTPTestServer> CreateServer(
323 const std::wstring& document_root) {
324 scoped_refptr<FTPTestServer> test_server = new FTPTestServer();
325 FilePath docroot = FilePath::FromWStringHack(document_root);
326 FilePath no_cert;
327 if (!test_server->Start(net::TestServerLauncher::ProtoFTP,
328 kDefaultHostName, kFTPDefaultPort, docroot, no_cert, std::wstring())) {
329 return NULL;
330 }
331 return test_server;
332 }
333
334 private:
335 ~FTPTestServer() {}
336 };
337
338
134 } // namespace net 339 } // namespace net
135 340
136 #endif // NET_TEST_TEST_SERVER_H_ 341 #endif // NET_TEST_TEST_SERVER_H_
OLDNEW
« no previous file with comments | « net/proxy/proxy_resolver_perftest.cc ('k') | net/tools/testserver/run_testserver.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698