rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 2 | // 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.org | 9d09499 | 2013-02-24 05:40:52 +0000 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 10 | #include "base/logging.h" |
avi@chromium.org | 188cc45 | 2013-07-18 00:41:22 +0000 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
avi@chromium.org | 60a948a | 2013-06-07 18:41:05 +0000 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
tim@chromium.org | c9ff542 | 2011-06-18 11:53:42 +0000 | [diff] [blame] | 13 | #include "base/test/test_timeouts.h" |
phajdan.jr@chromium.org | afa35e1 | 2013-05-07 20:04:21 +0000 | [diff] [blame] | 14 | #include "net/test/spawned_test_server/spawned_test_server.h" |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 15 | |
| 16 | static void PrintUsage() { |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 17 | printf("run_testserver --doc-root=relpath\n" |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 18 | " [--http|--https|--ws|--wss|--ftp]\n" |
| 19 | " [--ssl-cert=ok|mismatched-name|expired]\n"); |
rsimha@chromium.org | 6a5525c | 2012-05-24 20:40:21 +0000 | [diff] [blame] | 20 | printf("(NOTE: relpath should be relative to the 'src' directory.\n"); |
rsimha@chromium.org | 6a5525c | 2012-05-24 20:40:21 +0000 | [diff] [blame] | 21 | } |
| 22 | |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 23 | int main(int argc, const char* argv[]) { |
| 24 | base::AtExitManager at_exit_manager; |
xhwang@chromium.org | 4742e37 | 2013-05-23 20:51:34 +0000 | [diff] [blame] | 25 | base::MessageLoopForIO message_loop; |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 26 | |
| 27 | // Process command line |
thestig@chromium.org | 94a4efc | 2014-06-03 00:01:07 +0000 | [diff] [blame] | 28 | base::CommandLine::Init(argc, argv); |
| 29 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 30 | |
akalin@chromium.org | 520e202 | 2013-06-21 21:15:33 +0000 | [diff] [blame] | 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)) { |
akalin@chromium.org | 3dfcd45 | 2010-11-18 22:29:38 +0000 | [diff] [blame] | 35 | printf("Error: could not initialize logging. Exiting.\n"); |
| 36 | return -1; |
| 37 | } |
| 38 | |
tim@chromium.org | 8d38ca1 | 2011-06-23 23:56:38 +0000 | [diff] [blame] | 39 | TestTimeouts::Initialize(); |
| 40 | |
rsimha@chromium.org | 6a5525c | 2012-05-24 20:40:21 +0000 | [diff] [blame] | 41 | if (command_line->GetSwitches().empty() || |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 42 | command_line->HasSwitch("help")) { |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 43 | PrintUsage(); |
| 44 | return -1; |
| 45 | } |
| 46 | |
phajdan.jr@chromium.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 47 | net::SpawnedTestServer::Type server_type; |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 48 | if (command_line->HasSwitch("http")) { |
phajdan.jr@chromium.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 49 | server_type = net::SpawnedTestServer::TYPE_HTTP; |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 50 | } else if (command_line->HasSwitch("https")) { |
phajdan.jr@chromium.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 51 | server_type = net::SpawnedTestServer::TYPE_HTTPS; |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 52 | } else if (command_line->HasSwitch("ws")) { |
phajdan.jr@chromium.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 53 | server_type = net::SpawnedTestServer::TYPE_WS; |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 54 | } else if (command_line->HasSwitch("wss")) { |
phajdan.jr@chromium.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 55 | server_type = net::SpawnedTestServer::TYPE_WSS; |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 56 | } else if (command_line->HasSwitch("ftp")) { |
phajdan.jr@chromium.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 57 | server_type = net::SpawnedTestServer::TYPE_FTP; |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 58 | } 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.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 62 | server_type = net::SpawnedTestServer::TYPE_HTTPS; |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 63 | else |
phajdan.jr@chromium.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 64 | server_type = net::SpawnedTestServer::TYPE_HTTP; |
akalin@chromium.org | 154bb13 | 2010-11-12 02:20:27 +0000 | [diff] [blame] | 65 | } |
| 66 | |
phajdan.jr@chromium.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 67 | net::SpawnedTestServer::SSLOptions ssl_options; |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 68 | if (command_line->HasSwitch("ssl-cert")) { |
phajdan.jr@chromium.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 69 | if (!net::SpawnedTestServer::UsingSSL(server_type)) { |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 70 | 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.org | ca6e57e | 2011-09-03 03:06:40 +0000 | [diff] [blame] | 75 | if (cert_option == "ok") { |
phajdan.jr@chromium.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 76 | ssl_options.server_certificate = |
| 77 | net::SpawnedTestServer::SSLOptions::CERT_OK; |
cbentzel@chromium.org | ca6e57e | 2011-09-03 03:06:40 +0000 | [diff] [blame] | 78 | } else if (cert_option == "mismatched-name") { |
toyoshim@chromium.org | b1546ce | 2012-08-23 01:05:12 +0000 | [diff] [blame] | 79 | ssl_options.server_certificate = |
phajdan.jr@chromium.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 80 | net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME; |
cbentzel@chromium.org | ca6e57e | 2011-09-03 03:06:40 +0000 | [diff] [blame] | 81 | } else if (cert_option == "expired") { |
toyoshim@chromium.org | b1546ce | 2012-08-23 01:05:12 +0000 | [diff] [blame] | 82 | ssl_options.server_certificate = |
phajdan.jr@chromium.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 83 | net::SpawnedTestServer::SSLOptions::CERT_EXPIRED; |
cbentzel@chromium.org | ca6e57e | 2011-09-03 03:06:40 +0000 | [diff] [blame] | 84 | } else { |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 85 | printf("Error: --ssl-cert has invalid value %s\n", cert_option.c_str()); |
cbentzel@chromium.org | ca6e57e | 2011-09-03 03:06:40 +0000 | [diff] [blame] | 86 | PrintUsage(); |
| 87 | return -1; |
| 88 | } |
| 89 | } |
| 90 | |
brettw@chromium.org | b1ccf59 | 2013-02-08 20:40:15 +0000 | [diff] [blame] | 91 | base::FilePath doc_root = command_line->GetSwitchValuePath("doc-root"); |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 92 | if (doc_root.empty()) { |
akalin@chromium.org | 154bb13 | 2010-11-12 02:20:27 +0000 | [diff] [blame] | 93 | printf("Error: --doc-root must be specified\n"); |
| 94 | PrintUsage(); |
| 95 | return -1; |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 96 | } |
| 97 | |
phajdan.jr@chromium.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 98 | scoped_ptr<net::SpawnedTestServer> test_server; |
| 99 | if (net::SpawnedTestServer::UsingSSL(server_type)) { |
| 100 | test_server.reset( |
| 101 | new net::SpawnedTestServer(server_type, ssl_options, doc_root)); |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 102 | } else { |
phajdan.jr@chromium.org | a866ea4 | 2013-05-03 18:57:22 +0000 | [diff] [blame] | 103 | test_server.reset(new net::SpawnedTestServer( |
| 104 | server_type, |
| 105 | net::SpawnedTestServer::kLocalhost, |
| 106 | doc_root)); |
rsimha@chromium.org | 6a5525c | 2012-05-24 20:40:21 +0000 | [diff] [blame] | 107 | } |
cbentzel@chromium.org | ca6e57e | 2011-09-03 03:06:40 +0000 | [diff] [blame] | 108 | |
| 109 | if (!test_server->Start()) { |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 110 | printf("Error: failed to start test server. Exiting.\n"); |
| 111 | return -1; |
| 112 | } |
| 113 | |
brettw@chromium.org | 5b77578 | 2013-07-15 20:18:09 +0000 | [diff] [blame] | 114 | if (!base::DirectoryExists(test_server->document_root())) { |
darin@chromium.org | cc65c35 | 2011-04-15 19:07:49 +0000 | [diff] [blame] | 115 | printf("Error: invalid doc root: \"%s\" does not exist!\n", |
avi@chromium.org | 029ec73 | 2013-12-25 18:18:01 +0000 | [diff] [blame] | 116 | base::UTF16ToUTF8( |
| 117 | test_server->document_root().LossyDisplayName()).c_str()); |
darin@chromium.org | cc65c35 | 2011-04-15 19:07:49 +0000 | [diff] [blame] | 118 | return -1; |
| 119 | } |
| 120 | |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 121 | printf("testserver running at %s (type ctrl+c to exit)\n", |
cbentzel@chromium.org | ca6e57e | 2011-09-03 03:06:40 +0000 | [diff] [blame] | 122 | test_server->host_port_pair().ToString().c_str()); |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 123 | |
| 124 | message_loop.Run(); |
| 125 | return 0; |
| 126 | } |