blob: 6da829d9f315968d25e7bf3e87557887fe035517 [file] [log] [blame]
rsimha@chromium.org99a6f172013-01-20 01:10:24 +00001// Copyright 2013 The Chromium Authors. All rights reserved.
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <stdio.h>
6
7#include "base/at_exit.h"
8#include "base/command_line.h"
brettw@chromium.org9d094992013-02-24 05:40:52 +00009#include "base/files/file_path.h"
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000010#include "base/logging.h"
avi@chromium.org188cc452013-07-18 00:41:22 +000011#include "base/message_loop/message_loop.h"
avi@chromium.org60a948a2013-06-07 18:41:05 +000012#include "base/strings/utf_string_conversions.h"
tim@chromium.orgc9ff5422011-06-18 11:53:42 +000013#include "base/test/test_timeouts.h"
phajdan.jr@chromium.orgafa35e12013-05-07 20:04:21 +000014#include "net/test/spawned_test_server/spawned_test_server.h"
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000015
16static void PrintUsage() {
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000017 printf("run_testserver --doc-root=relpath\n"
rsimha@chromium.org99a6f172013-01-20 01:10:24 +000018 " [--http|--https|--ws|--wss|--ftp]\n"
19 " [--ssl-cert=ok|mismatched-name|expired]\n");
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000020 printf("(NOTE: relpath should be relative to the 'src' directory.\n");
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000021}
22
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000023int main(int argc, const char* argv[]) {
24 base::AtExitManager at_exit_manager;
xhwang@chromium.org4742e372013-05-23 20:51:34 +000025 base::MessageLoopForIO message_loop;
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000026
27 // Process command line
thestig@chromium.org94a4efc2014-06-03 00:01:07 +000028 base::CommandLine::Init(argc, argv);
29 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000030
akalin@chromium.org520e2022013-06-21 21:15:33 +000031 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)) {
akalin@chromium.org3dfcd452010-11-18 22:29:38 +000035 printf("Error: could not initialize logging. Exiting.\n");
36 return -1;
37 }
38
tim@chromium.org8d38ca12011-06-23 23:56:38 +000039 TestTimeouts::Initialize();
40
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000041 if (command_line->GetSwitches().empty() ||
rsimha@chromium.org99a6f172013-01-20 01:10:24 +000042 command_line->HasSwitch("help")) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000043 PrintUsage();
44 return -1;
45 }
46
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000047 net::SpawnedTestServer::Type server_type;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000048 if (command_line->HasSwitch("http")) {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000049 server_type = net::SpawnedTestServer::TYPE_HTTP;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000050 } else if (command_line->HasSwitch("https")) {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000051 server_type = net::SpawnedTestServer::TYPE_HTTPS;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000052 } else if (command_line->HasSwitch("ws")) {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000053 server_type = net::SpawnedTestServer::TYPE_WS;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000054 } else if (command_line->HasSwitch("wss")) {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000055 server_type = net::SpawnedTestServer::TYPE_WSS;
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000056 } else if (command_line->HasSwitch("ftp")) {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000057 server_type = net::SpawnedTestServer::TYPE_FTP;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000058 } else {
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"))
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000062 server_type = net::SpawnedTestServer::TYPE_HTTPS;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000063 else
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000064 server_type = net::SpawnedTestServer::TYPE_HTTP;
akalin@chromium.org154bb132010-11-12 02:20:27 +000065 }
66
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000067 net::SpawnedTestServer::SSLOptions ssl_options;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000068 if (command_line->HasSwitch("ssl-cert")) {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000069 if (!net::SpawnedTestServer::UsingSSL(server_type)) {
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000070 printf("Error: --ssl-cert is specified on non-secure scheme\n");
71 PrintUsage();
72 return -1;
73 }
74 std::string cert_option = command_line->GetSwitchValueASCII("ssl-cert");
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000075 if (cert_option == "ok") {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000076 ssl_options.server_certificate =
77 net::SpawnedTestServer::SSLOptions::CERT_OK;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000078 } else if (cert_option == "mismatched-name") {
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +000079 ssl_options.server_certificate =
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000080 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000081 } else if (cert_option == "expired") {
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +000082 ssl_options.server_certificate =
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000083 net::SpawnedTestServer::SSLOptions::CERT_EXPIRED;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000084 } else {
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000085 printf("Error: --ssl-cert has invalid value %s\n", cert_option.c_str());
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000086 PrintUsage();
87 return -1;
88 }
89 }
90
brettw@chromium.orgb1ccf592013-02-08 20:40:15 +000091 base::FilePath doc_root = command_line->GetSwitchValuePath("doc-root");
rsimha@chromium.org99a6f172013-01-20 01:10:24 +000092 if (doc_root.empty()) {
akalin@chromium.org154bb132010-11-12 02:20:27 +000093 printf("Error: --doc-root must be specified\n");
94 PrintUsage();
95 return -1;
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000096 }
97
danakj33b49432016-04-18 15:28:08 -070098 std::unique_ptr<net::SpawnedTestServer> test_server;
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000099 if (net::SpawnedTestServer::UsingSSL(server_type)) {
100 test_server.reset(
101 new net::SpawnedTestServer(server_type, ssl_options, doc_root));
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +0000102 } else {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +0000103 test_server.reset(new net::SpawnedTestServer(
104 server_type,
105 net::SpawnedTestServer::kLocalhost,
106 doc_root));
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +0000107 }
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000108
109 if (!test_server->Start()) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000110 printf("Error: failed to start test server. Exiting.\n");
111 return -1;
112 }
113
brettw@chromium.org5b775782013-07-15 20:18:09 +0000114 if (!base::DirectoryExists(test_server->document_root())) {
darin@chromium.orgcc65c352011-04-15 19:07:49 +0000115 printf("Error: invalid doc root: \"%s\" does not exist!\n",
avi@chromium.org029ec732013-12-25 18:18:01 +0000116 base::UTF16ToUTF8(
117 test_server->document_root().LossyDisplayName()).c_str());
darin@chromium.orgcc65c352011-04-15 19:07:49 +0000118 return -1;
119 }
120
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000121 printf("testserver running at %s (type ctrl+c to exit)\n",
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000122 test_server->host_port_pair().ToString().c_str());
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000123
124 message_loop.Run();
125 return 0;
126}