blob: 378d5b0982264143dd200479dff2b72cc56f4cb5 [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"
fdorayfdd01b72016-08-25 08:36:37 -070012#include "base/run_loop.h"
avi@chromium.org60a948a2013-06-07 18:41:05 +000013#include "base/strings/utf_string_conversions.h"
tim@chromium.orgc9ff5422011-06-18 11:53:42 +000014#include "base/test/test_timeouts.h"
phajdan.jr@chromium.orgafa35e12013-05-07 20:04:21 +000015#include "net/test/spawned_test_server/spawned_test_server.h"
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000016
17static void PrintUsage() {
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000018 printf("run_testserver --doc-root=relpath\n"
rsimha@chromium.org99a6f172013-01-20 01:10:24 +000019 " [--http|--https|--ws|--wss|--ftp]\n"
20 " [--ssl-cert=ok|mismatched-name|expired]\n");
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000021 printf("(NOTE: relpath should be relative to the 'src' directory.\n");
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000022}
23
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000024int main(int argc, const char* argv[]) {
25 base::AtExitManager at_exit_manager;
xhwang@chromium.org4742e372013-05-23 20:51:34 +000026 base::MessageLoopForIO message_loop;
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000027
28 // Process command line
thestig@chromium.org94a4efc2014-06-03 00:01:07 +000029 base::CommandLine::Init(argc, argv);
30 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000031
akalin@chromium.org520e2022013-06-21 21:15:33 +000032 logging::LoggingSettings settings;
33 settings.logging_dest = logging::LOG_TO_ALL;
34 settings.log_file = FILE_PATH_LITERAL("testserver.log");
35 if (!logging::InitLogging(settings)) {
akalin@chromium.org3dfcd452010-11-18 22:29:38 +000036 printf("Error: could not initialize logging. Exiting.\n");
37 return -1;
38 }
39
tim@chromium.org8d38ca12011-06-23 23:56:38 +000040 TestTimeouts::Initialize();
41
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000042 if (command_line->GetSwitches().empty() ||
rsimha@chromium.org99a6f172013-01-20 01:10:24 +000043 command_line->HasSwitch("help")) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000044 PrintUsage();
45 return -1;
46 }
47
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000048 net::SpawnedTestServer::Type server_type;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000049 if (command_line->HasSwitch("http")) {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000050 server_type = net::SpawnedTestServer::TYPE_HTTP;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000051 } else if (command_line->HasSwitch("https")) {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000052 server_type = net::SpawnedTestServer::TYPE_HTTPS;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000053 } else if (command_line->HasSwitch("ws")) {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000054 server_type = net::SpawnedTestServer::TYPE_WS;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000055 } else if (command_line->HasSwitch("wss")) {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000056 server_type = net::SpawnedTestServer::TYPE_WSS;
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000057 } else if (command_line->HasSwitch("ftp")) {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000058 server_type = net::SpawnedTestServer::TYPE_FTP;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000059 } else {
60 // If no scheme switch is specified, select http or https scheme.
61 // TODO(toyoshim): Remove this estimation.
62 if (command_line->HasSwitch("ssl-cert"))
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000063 server_type = net::SpawnedTestServer::TYPE_HTTPS;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000064 else
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000065 server_type = net::SpawnedTestServer::TYPE_HTTP;
akalin@chromium.org154bb132010-11-12 02:20:27 +000066 }
67
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000068 net::SpawnedTestServer::SSLOptions ssl_options;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000069 if (command_line->HasSwitch("ssl-cert")) {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000070 if (!net::SpawnedTestServer::UsingSSL(server_type)) {
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000071 printf("Error: --ssl-cert is specified on non-secure scheme\n");
72 PrintUsage();
73 return -1;
74 }
75 std::string cert_option = command_line->GetSwitchValueASCII("ssl-cert");
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000076 if (cert_option == "ok") {
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000077 ssl_options.server_certificate =
78 net::SpawnedTestServer::SSLOptions::CERT_OK;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000079 } else if (cert_option == "mismatched-name") {
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +000080 ssl_options.server_certificate =
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000081 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000082 } else if (cert_option == "expired") {
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +000083 ssl_options.server_certificate =
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +000084 net::SpawnedTestServer::SSLOptions::CERT_EXPIRED;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000085 } else {
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000086 printf("Error: --ssl-cert has invalid value %s\n", cert_option.c_str());
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000087 PrintUsage();
88 return -1;
89 }
90 }
91
brettw@chromium.orgb1ccf592013-02-08 20:40:15 +000092 base::FilePath doc_root = command_line->GetSwitchValuePath("doc-root");
rsimha@chromium.org99a6f172013-01-20 01:10:24 +000093 if (doc_root.empty()) {
akalin@chromium.org154bb132010-11-12 02:20:27 +000094 printf("Error: --doc-root must be specified\n");
95 PrintUsage();
96 return -1;
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000097 }
98
danakj33b49432016-04-18 15:28:08 -070099 std::unique_ptr<net::SpawnedTestServer> test_server;
phajdan.jr@chromium.orga866ea42013-05-03 18:57:22 +0000100 if (net::SpawnedTestServer::UsingSSL(server_type)) {
101 test_server.reset(
102 new net::SpawnedTestServer(server_type, ssl_options, doc_root));
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +0000103 } else {
Sergey Ulanov3f98d672017-08-14 22:12:58 +0000104 test_server.reset(new net::SpawnedTestServer(server_type, doc_root));
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +0000105 }
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000106
107 if (!test_server->Start()) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000108 printf("Error: failed to start test server. Exiting.\n");
109 return -1;
110 }
111
brettw@chromium.org5b775782013-07-15 20:18:09 +0000112 if (!base::DirectoryExists(test_server->document_root())) {
darin@chromium.orgcc65c352011-04-15 19:07:49 +0000113 printf("Error: invalid doc root: \"%s\" does not exist!\n",
avi@chromium.org029ec732013-12-25 18:18:01 +0000114 base::UTF16ToUTF8(
115 test_server->document_root().LossyDisplayName()).c_str());
darin@chromium.orgcc65c352011-04-15 19:07:49 +0000116 return -1;
117 }
118
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000119 printf("testserver running at %s (type ctrl+c to exit)\n",
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000120 test_server->host_port_pair().ToString().c_str());
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000121
fdorayfdd01b72016-08-25 08:36:37 -0700122 base::RunLoop().Run();
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000123 return 0;
124}