blob: 58abe34dec3c670ef4edbef6af2eec17d1ef030e [file] [log] [blame]
erikwright@chromium.orgb3c1a9b2012-02-24 23:05:30 +00001// Copyright (c) 2012 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"
akalin@chromium.org3dfcd452010-11-18 22:29:38 +00009#include "base/file_path.h"
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000010#include "base/logging.h"
11#include "base/message_loop.h"
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +000012#include "base/process_util.h"
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000013#include "base/string_number_conversions.h"
tim@chromium.orgc9ff5422011-06-18 11:53:42 +000014#include "base/test/test_timeouts.h"
darin@chromium.orgcc65c352011-04-15 19:07:49 +000015#include "base/utf_string_conversions.h"
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000016#include "net/test/local_sync_test_server.h"
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +000017#include "net/test/python_utils.h"
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000018#include "net/test/test_server.h"
19
20static void PrintUsage() {
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +000021 printf("run_testserver --doc-root=relpath\n"
22 " [--http|--https|--ws|--wss|--ftp|--sync]\n"
23 " [--ssl-cert=ok|mismatched-name|expired]\n"
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000024 " [--port=<port>] [--xmpp-port=<xmpp_port>]\n");
25 printf("(NOTE: relpath should be relative to the 'src' directory.\n");
26 printf(" --port and --xmpp-port only work with the --sync flag.)\n");
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000027}
28
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +000029// Launches the chromiumsync_test script, testing the --sync functionality.
30static bool RunSyncTest() {
mattm@chromium.org6be1b5e2012-09-05 01:24:29 +000031 if (!net::TestServer::SetPythonPath()) {
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +000032 LOG(ERROR) << "Error trying to set python path. Exiting.";
33 return false;
34 }
35
36 FilePath sync_test_path;
37 if (!net::TestServer::GetTestServerDirectory(&sync_test_path)) {
38 LOG(ERROR) << "Error trying to get python test server path.";
39 return false;
40 }
41
42 sync_test_path =
43 sync_test_path.Append(FILE_PATH_LITERAL("chromiumsync_test.py"));
rsimha@google.comd4f91742012-09-12 21:53:15 +000044
45 CommandLine python_command(CommandLine::NO_PROGRAM);
46 if (!GetPythonCommand(&python_command)) {
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +000047 LOG(ERROR) << "Could not get python runtime command.";
48 return false;
49 }
50
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +000051 python_command.AppendArgPath(sync_test_path);
52 if (!base::LaunchProcess(python_command, base::LaunchOptions(), NULL)) {
53 LOG(ERROR) << "Failed to launch test script.";
54 return false;
55 }
56 return true;
57}
58
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000059// Gets a port value from the switch with name |switch_name| and writes it to
60// |port|. Returns true if successful and false otherwise.
61static bool GetPortFromSwitch(const std::string& switch_name, uint16* port) {
62 DCHECK(port != NULL) << "|port| is NULL";
63 CommandLine* command_line = CommandLine::ForCurrentProcess();
64 int port_int = 0;
65 if (command_line->HasSwitch(switch_name)) {
66 std::string port_str = command_line->GetSwitchValueASCII(switch_name);
67 if (!base::StringToInt(port_str, &port_int)) {
68 LOG(WARNING) << "Could not extract port from switch " << switch_name;
69 return false;
70 }
71 }
72 *port = static_cast<uint16>(port_int);
73 return true;
74}
75
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000076int main(int argc, const char* argv[]) {
77 base::AtExitManager at_exit_manager;
78 MessageLoopForIO message_loop;
79
80 // Process command line
81 CommandLine::Init(argc, argv);
82 CommandLine* command_line = CommandLine::ForCurrentProcess();
83
akalin@chromium.orgc3062de2011-01-11 01:03:36 +000084 if (!logging::InitLogging(
85 FILE_PATH_LITERAL("testserver.log"),
86 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG,
87 logging::LOCK_LOG_FILE,
88 logging::APPEND_TO_OLD_LOG_FILE,
89 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS)) {
akalin@chromium.org3dfcd452010-11-18 22:29:38 +000090 printf("Error: could not initialize logging. Exiting.\n");
91 return -1;
92 }
93
tim@chromium.org8d38ca12011-06-23 23:56:38 +000094 TestTimeouts::Initialize();
95
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000096 if (command_line->GetSwitches().empty() ||
97 command_line->HasSwitch("help") ||
98 ((command_line->HasSwitch("port") ||
99 command_line->HasSwitch("xmpp-port")) &&
100 !command_line->HasSwitch("sync"))) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000101 PrintUsage();
102 return -1;
103 }
104
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +0000105 net::TestServer::Type server_type;
106 if (command_line->HasSwitch("http")) {
107 server_type = net::TestServer::TYPE_HTTP;
108 } else if (command_line->HasSwitch("https")) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000109 server_type = net::TestServer::TYPE_HTTPS;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +0000110 } else if (command_line->HasSwitch("ws")) {
111 server_type = net::TestServer::TYPE_WS;
112 } else if (command_line->HasSwitch("wss")) {
113 server_type = net::TestServer::TYPE_WSS;
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000114 } else if (command_line->HasSwitch("ftp")) {
115 server_type = net::TestServer::TYPE_FTP;
akalin@chromium.org154bb132010-11-12 02:20:27 +0000116 } else if (command_line->HasSwitch("sync")) {
117 server_type = net::TestServer::TYPE_SYNC;
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +0000118 } else if (command_line->HasSwitch("sync-test")) {
119 return RunSyncTest() ? 0 : -1;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +0000120 } else {
121 // If no scheme switch is specified, select http or https scheme.
122 // TODO(toyoshim): Remove this estimation.
123 if (command_line->HasSwitch("ssl-cert"))
124 server_type = net::TestServer::TYPE_HTTPS;
125 else
126 server_type = net::TestServer::TYPE_HTTP;
akalin@chromium.org154bb132010-11-12 02:20:27 +0000127 }
128
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +0000129 net::TestServer::SSLOptions ssl_options;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +0000130 if (command_line->HasSwitch("ssl-cert")) {
131 if (!net::TestServer::UsingSSL(server_type)) {
132 printf("Error: --ssl-cert is specified on non-secure scheme\n");
133 PrintUsage();
134 return -1;
135 }
136 std::string cert_option = command_line->GetSwitchValueASCII("ssl-cert");
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000137 if (cert_option == "ok") {
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +0000138 ssl_options.server_certificate = net::TestServer::SSLOptions::CERT_OK;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000139 } else if (cert_option == "mismatched-name") {
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +0000140 ssl_options.server_certificate =
141 net::TestServer::SSLOptions::CERT_MISMATCHED_NAME;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000142 } else if (cert_option == "expired") {
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +0000143 ssl_options.server_certificate =
144 net::TestServer::SSLOptions::CERT_EXPIRED;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000145 } else {
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +0000146 printf("Error: --ssl-cert has invalid value %s\n", cert_option.c_str());
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000147 PrintUsage();
148 return -1;
149 }
150 }
151
akalin@chromium.org154bb132010-11-12 02:20:27 +0000152 FilePath doc_root = command_line->GetSwitchValuePath("doc-root");
153 if ((server_type != net::TestServer::TYPE_SYNC) && doc_root.empty()) {
154 printf("Error: --doc-root must be specified\n");
155 PrintUsage();
156 return -1;
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000157 }
158
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000159 scoped_ptr<net::TestServer> test_server;
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +0000160 if (net::TestServer::UsingSSL(server_type)) {
161 test_server.reset(new net::TestServer(server_type, ssl_options, doc_root));
162 } else if (server_type == net::TestServer::TYPE_SYNC) {
163 uint16 port = 0;
164 uint16 xmpp_port = 0;
165 if (!GetPortFromSwitch("port", &port) ||
166 !GetPortFromSwitch("xmpp-port", &xmpp_port)) {
167 printf("Error: Could not extract --port and/or --xmpp-port.\n");
168 return -1;
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +0000169 }
toyoshim@chromium.orgaa1b6e72012-10-09 03:43:19 +0000170 test_server.reset(new net::LocalSyncTestServer(port, xmpp_port));
171 } else {
172 test_server.reset(new net::TestServer(server_type,
173 net::TestServer::kLocalhost,
174 doc_root));
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +0000175 }
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000176
177 if (!test_server->Start()) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000178 printf("Error: failed to start test server. Exiting.\n");
179 return -1;
180 }
181
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000182 if (!file_util::DirectoryExists(test_server->document_root())) {
darin@chromium.orgcc65c352011-04-15 19:07:49 +0000183 printf("Error: invalid doc root: \"%s\" does not exist!\n",
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000184 UTF16ToUTF8(test_server->document_root().LossyDisplayName()).c_str());
darin@chromium.orgcc65c352011-04-15 19:07:49 +0000185 return -1;
186 }
187
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000188 printf("testserver running at %s (type ctrl+c to exit)\n",
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000189 test_server->host_port_pair().ToString().c_str());
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000190
191 message_loop.Run();
192 return 0;
193}