blob: 9ad0f7d98757c559dae78e1dbf3c9c4d5918036a [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() {
peter@chromium.orgb3de03a2012-09-14 09:38:25 +000021 printf("run_testserver --doc-root=relpath [--http|--https|--ftp|--sync]\n"
22 " [--https-cert=ok|mismatched-name|expired]\n"
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000023 " [--port=<port>] [--xmpp-port=<xmpp_port>]\n");
24 printf("(NOTE: relpath should be relative to the 'src' directory.\n");
25 printf(" --port and --xmpp-port only work with the --sync flag.)\n");
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000026}
27
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +000028// Launches the chromiumsync_test script, testing the --sync functionality.
29static bool RunSyncTest() {
mattm@chromium.org6be1b5e2012-09-05 01:24:29 +000030 if (!net::TestServer::SetPythonPath()) {
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +000031 LOG(ERROR) << "Error trying to set python path. Exiting.";
32 return false;
33 }
34
35 FilePath sync_test_path;
36 if (!net::TestServer::GetTestServerDirectory(&sync_test_path)) {
37 LOG(ERROR) << "Error trying to get python test server path.";
38 return false;
39 }
40
41 sync_test_path =
42 sync_test_path.Append(FILE_PATH_LITERAL("chromiumsync_test.py"));
rsimha@google.comd4f91742012-09-12 21:53:15 +000043
44 CommandLine python_command(CommandLine::NO_PROGRAM);
45 if (!GetPythonCommand(&python_command)) {
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +000046 LOG(ERROR) << "Could not get python runtime command.";
47 return false;
48 }
49
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +000050 python_command.AppendArgPath(sync_test_path);
51 if (!base::LaunchProcess(python_command, base::LaunchOptions(), NULL)) {
52 LOG(ERROR) << "Failed to launch test script.";
53 return false;
54 }
55 return true;
56}
57
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000058// Gets a port value from the switch with name |switch_name| and writes it to
59// |port|. Returns true if successful and false otherwise.
60static bool GetPortFromSwitch(const std::string& switch_name, uint16* port) {
61 DCHECK(port != NULL) << "|port| is NULL";
62 CommandLine* command_line = CommandLine::ForCurrentProcess();
63 int port_int = 0;
64 if (command_line->HasSwitch(switch_name)) {
65 std::string port_str = command_line->GetSwitchValueASCII(switch_name);
66 if (!base::StringToInt(port_str, &port_int)) {
67 LOG(WARNING) << "Could not extract port from switch " << switch_name;
68 return false;
69 }
70 }
71 *port = static_cast<uint16>(port_int);
72 return true;
73}
74
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000075int main(int argc, const char* argv[]) {
76 base::AtExitManager at_exit_manager;
77 MessageLoopForIO message_loop;
78
79 // Process command line
80 CommandLine::Init(argc, argv);
81 CommandLine* command_line = CommandLine::ForCurrentProcess();
82
akalin@chromium.orgc3062de2011-01-11 01:03:36 +000083 if (!logging::InitLogging(
84 FILE_PATH_LITERAL("testserver.log"),
85 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG,
86 logging::LOCK_LOG_FILE,
87 logging::APPEND_TO_OLD_LOG_FILE,
88 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS)) {
akalin@chromium.org3dfcd452010-11-18 22:29:38 +000089 printf("Error: could not initialize logging. Exiting.\n");
90 return -1;
91 }
92
tim@chromium.org8d38ca12011-06-23 23:56:38 +000093 TestTimeouts::Initialize();
94
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +000095 if (command_line->GetSwitches().empty() ||
96 command_line->HasSwitch("help") ||
97 ((command_line->HasSwitch("port") ||
98 command_line->HasSwitch("xmpp-port")) &&
99 !command_line->HasSwitch("sync"))) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000100 PrintUsage();
101 return -1;
102 }
103
peter@chromium.orgb3de03a2012-09-14 09:38:25 +0000104 net::TestServer::Type server_type(net::TestServer::TYPE_HTTP);
105 if (command_line->HasSwitch("https")) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000106 server_type = net::TestServer::TYPE_HTTPS;
107 } else if (command_line->HasSwitch("ftp")) {
108 server_type = net::TestServer::TYPE_FTP;
akalin@chromium.org154bb132010-11-12 02:20:27 +0000109 } else if (command_line->HasSwitch("sync")) {
110 server_type = net::TestServer::TYPE_SYNC;
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +0000111 } else if (command_line->HasSwitch("sync-test")) {
112 return RunSyncTest() ? 0 : -1;
akalin@chromium.org154bb132010-11-12 02:20:27 +0000113 }
114
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +0000115 net::TestServer::SSLOptions ssl_options;
peter@chromium.orgb3de03a2012-09-14 09:38:25 +0000116 if (command_line->HasSwitch("https-cert")) {
117 server_type = net::TestServer::TYPE_HTTPS;
118 std::string cert_option = command_line->GetSwitchValueASCII("https-cert");
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000119 if (cert_option == "ok") {
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +0000120 ssl_options.server_certificate = net::TestServer::SSLOptions::CERT_OK;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000121 } else if (cert_option == "mismatched-name") {
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +0000122 ssl_options.server_certificate =
123 net::TestServer::SSLOptions::CERT_MISMATCHED_NAME;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000124 } else if (cert_option == "expired") {
toyoshim@chromium.orgb1546ce2012-08-23 01:05:12 +0000125 ssl_options.server_certificate =
126 net::TestServer::SSLOptions::CERT_EXPIRED;
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000127 } else {
peter@chromium.orgb3de03a2012-09-14 09:38:25 +0000128 printf("Error: --https-cert has invalid value %s\n", cert_option.c_str());
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000129 PrintUsage();
130 return -1;
131 }
132 }
133
akalin@chromium.org154bb132010-11-12 02:20:27 +0000134 FilePath doc_root = command_line->GetSwitchValuePath("doc-root");
135 if ((server_type != net::TestServer::TYPE_SYNC) && doc_root.empty()) {
136 printf("Error: --doc-root must be specified\n");
137 PrintUsage();
138 return -1;
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000139 }
140
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000141 scoped_ptr<net::TestServer> test_server;
peter@chromium.orgb3de03a2012-09-14 09:38:25 +0000142 switch (server_type) {
143 case net::TestServer::TYPE_HTTPS: {
144 test_server.reset(new net::TestServer(server_type,
145 ssl_options,
146 doc_root));
147 break;
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +0000148 }
peter@chromium.orgb3de03a2012-09-14 09:38:25 +0000149 case net::TestServer::TYPE_SYNC: {
150 uint16 port = 0;
151 uint16 xmpp_port = 0;
152 if (!GetPortFromSwitch("port", &port) ||
153 !GetPortFromSwitch("xmpp-port", &xmpp_port)) {
154 printf("Error: Could not extract --port and/or --xmpp-port.\n");
155 return -1;
156 }
157 test_server.reset(new net::LocalSyncTestServer(port, xmpp_port));
158 break;
159 }
160 default: {
161 test_server.reset(new net::TestServer(server_type,
162 net::TestServer::kLocalhost,
163 doc_root));
164 break;
165 }
rsimha@chromium.org6a5525c2012-05-24 20:40:21 +0000166 }
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000167
168 if (!test_server->Start()) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000169 printf("Error: failed to start test server. Exiting.\n");
170 return -1;
171 }
172
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000173 if (!file_util::DirectoryExists(test_server->document_root())) {
darin@chromium.orgcc65c352011-04-15 19:07:49 +0000174 printf("Error: invalid doc root: \"%s\" does not exist!\n",
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000175 UTF16ToUTF8(test_server->document_root().LossyDisplayName()).c_str());
darin@chromium.orgcc65c352011-04-15 19:07:49 +0000176 return -1;
177 }
178
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000179 printf("testserver running at %s (type ctrl+c to exit)\n",
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000180 test_server->host_port_pair().ToString().c_str());
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000181
182 message_loop.Run();
183 return 0;
184}