1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/at_exit.h"
8 #include "base/command_line.h"
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/test/test_timeouts.h"
14 #include "net/test/spawned_test_server/spawned_test_server.h"
16 static void PrintUsage() {
17 printf("run_testserver --doc-root=relpath\n"
18 " [--http|--https|--ws|--wss|--ftp]\n"
19 " [--ssl-cert=ok|mismatched-name|expired]\n");
20 printf("(NOTE: relpath should be relative to the 'src' directory.\n");
23 int main(int argc
, const char* argv
[]) {
24 base::AtExitManager at_exit_manager
;
25 base::MessageLoopForIO message_loop
;
27 // Process command line
28 base::CommandLine::Init(argc
, argv
);
29 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
31 logging::LoggingSettings settings
;
32 settings
.logging_dest
= logging::LOG_TO_ALL
;
33 settings
.log_file
= FILE_PATH_LITERAL("testserver.log");
34 if (!logging::InitLogging(settings
)) {
35 printf("Error: could not initialize logging. Exiting.\n");
39 TestTimeouts::Initialize();
41 if (command_line
->GetSwitches().empty() ||
42 command_line
->HasSwitch("help")) {
47 net::SpawnedTestServer::Type server_type
;
48 if (command_line
->HasSwitch("http")) {
49 server_type
= net::SpawnedTestServer::TYPE_HTTP
;
50 } else if (command_line
->HasSwitch("https")) {
51 server_type
= net::SpawnedTestServer::TYPE_HTTPS
;
52 } else if (command_line
->HasSwitch("ws")) {
53 server_type
= net::SpawnedTestServer::TYPE_WS
;
54 } else if (command_line
->HasSwitch("wss")) {
55 server_type
= net::SpawnedTestServer::TYPE_WSS
;
56 } else if (command_line
->HasSwitch("ftp")) {
57 server_type
= net::SpawnedTestServer::TYPE_FTP
;
59 // If no scheme switch is specified, select http or https scheme.
60 // TODO(toyoshim): Remove this estimation.
61 if (command_line
->HasSwitch("ssl-cert"))
62 server_type
= net::SpawnedTestServer::TYPE_HTTPS
;
64 server_type
= net::SpawnedTestServer::TYPE_HTTP
;
67 net::SpawnedTestServer::SSLOptions ssl_options
;
68 if (command_line
->HasSwitch("ssl-cert")) {
69 if (!net::SpawnedTestServer::UsingSSL(server_type
)) {
70 printf("Error: --ssl-cert is specified on non-secure scheme\n");
74 std::string cert_option
= command_line
->GetSwitchValueASCII("ssl-cert");
75 if (cert_option
== "ok") {
76 ssl_options
.server_certificate
=
77 net::SpawnedTestServer::SSLOptions::CERT_OK
;
78 } else if (cert_option
== "mismatched-name") {
79 ssl_options
.server_certificate
=
80 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME
;
81 } else if (cert_option
== "expired") {
82 ssl_options
.server_certificate
=
83 net::SpawnedTestServer::SSLOptions::CERT_EXPIRED
;
85 printf("Error: --ssl-cert has invalid value %s\n", cert_option
.c_str());
91 base::FilePath doc_root
= command_line
->GetSwitchValuePath("doc-root");
92 if (doc_root
.empty()) {
93 printf("Error: --doc-root must be specified\n");
98 scoped_ptr
<net::SpawnedTestServer
> test_server
;
99 if (net::SpawnedTestServer::UsingSSL(server_type
)) {
101 new net::SpawnedTestServer(server_type
, ssl_options
, doc_root
));
103 test_server
.reset(new net::SpawnedTestServer(
105 net::SpawnedTestServer::kLocalhost
,
109 if (!test_server
->Start()) {
110 printf("Error: failed to start test server. Exiting.\n");
114 if (!base::DirectoryExists(test_server
->document_root())) {
115 printf("Error: invalid doc root: \"%s\" does not exist!\n",
117 test_server
->document_root().LossyDisplayName()).c_str());
121 printf("testserver running at %s (type ctrl+c to exit)\n",
122 test_server
->host_port_pair().ToString().c_str());