blob: f00f9f2d2b5730d59886b047c36f49a983ce55d5 [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"
tim@chromium.orgc9ff5422011-06-18 11:53:42 +000013#include "base/test/test_timeouts.h"
darin@chromium.orgcc65c352011-04-15 19:07:49 +000014#include "base/utf_string_conversions.h"
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +000015#include "net/test/python_utils.h"
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000016#include "net/test/test_server.h"
17
18static void PrintUsage() {
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000019 printf("run_testserver --doc-root=relpath [--http|--https|--ftp|--sync]\n"
20 " [--https-cert=ok|mismatched-name|expired]\n");
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000021 printf("(NOTE: relpath should be relative to the 'src' directory)\n");
22}
23
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +000024// Launches the chromiumsync_test script, testing the --sync functionality.
25static bool RunSyncTest() {
26 if (!net::TestServer::SetPythonPath()) {
27 LOG(ERROR) << "Error trying to set python path. Exiting.";
28 return false;
29 }
30
31 FilePath sync_test_path;
32 if (!net::TestServer::GetTestServerDirectory(&sync_test_path)) {
33 LOG(ERROR) << "Error trying to get python test server path.";
34 return false;
35 }
36
37 sync_test_path =
38 sync_test_path.Append(FILE_PATH_LITERAL("chromiumsync_test.py"));
39 FilePath python_runtime;
40 if (!GetPythonRunTime(&python_runtime)) {
41 LOG(ERROR) << "Could not get python runtime command.";
42 return false;
43 }
44
45 CommandLine python_command(python_runtime);
46 python_command.AppendArgPath(sync_test_path);
47 if (!base::LaunchProcess(python_command, base::LaunchOptions(), NULL)) {
48 LOG(ERROR) << "Failed to launch test script.";
49 return false;
50 }
51 return true;
52}
53
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000054int main(int argc, const char* argv[]) {
55 base::AtExitManager at_exit_manager;
56 MessageLoopForIO message_loop;
57
58 // Process command line
59 CommandLine::Init(argc, argv);
60 CommandLine* command_line = CommandLine::ForCurrentProcess();
61
akalin@chromium.orgc3062de2011-01-11 01:03:36 +000062 if (!logging::InitLogging(
63 FILE_PATH_LITERAL("testserver.log"),
64 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG,
65 logging::LOCK_LOG_FILE,
66 logging::APPEND_TO_OLD_LOG_FILE,
67 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS)) {
akalin@chromium.org3dfcd452010-11-18 22:29:38 +000068 printf("Error: could not initialize logging. Exiting.\n");
69 return -1;
70 }
71
tim@chromium.org8d38ca12011-06-23 23:56:38 +000072 TestTimeouts::Initialize();
73
msw@chromium.orgc7688b82011-07-13 21:46:32 +000074 if (command_line->GetSwitches().empty() || command_line->HasSwitch("help")) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000075 PrintUsage();
76 return -1;
77 }
78
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +000079 net::TestServer::Type server_type(net::TestServer::TYPE_HTTP);
80 if (command_line->HasSwitch("https")) {
81 server_type = net::TestServer::TYPE_HTTPS;
82 } else if (command_line->HasSwitch("ftp")) {
83 server_type = net::TestServer::TYPE_FTP;
akalin@chromium.org154bb132010-11-12 02:20:27 +000084 } else if (command_line->HasSwitch("sync")) {
85 server_type = net::TestServer::TYPE_SYNC;
tim@chromium.orgb5d4b2a2012-03-15 05:13:30 +000086 } else if (command_line->HasSwitch("sync-test")) {
87 return RunSyncTest() ? 0 : -1;
akalin@chromium.org154bb132010-11-12 02:20:27 +000088 }
89
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +000090 net::TestServer::HTTPSOptions https_options;
91 if (command_line->HasSwitch("https-cert")) {
92 server_type = net::TestServer::TYPE_HTTPS;
93 std::string cert_option = command_line->GetSwitchValueASCII("https-cert");
94 if (cert_option == "ok") {
95 https_options.server_certificate = net::TestServer::HTTPSOptions::CERT_OK;
96 } else if (cert_option == "mismatched-name") {
97 https_options.server_certificate =
98 net::TestServer::HTTPSOptions::CERT_MISMATCHED_NAME;
99 } else if (cert_option == "expired") {
100 https_options.server_certificate =
101 net::TestServer::HTTPSOptions::CERT_EXPIRED;
102 } else {
103 printf("Error: --https-cert has invalid value %s\n", cert_option.c_str());
104 PrintUsage();
105 return -1;
106 }
107 }
108
akalin@chromium.org154bb132010-11-12 02:20:27 +0000109 FilePath doc_root = command_line->GetSwitchValuePath("doc-root");
110 if ((server_type != net::TestServer::TYPE_SYNC) && doc_root.empty()) {
111 printf("Error: --doc-root must be specified\n");
112 PrintUsage();
113 return -1;
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000114 }
115
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000116 scoped_ptr<net::TestServer> test_server;
117 if (server_type == net::TestServer::TYPE_HTTPS)
118 test_server.reset(new net::TestServer(https_options, doc_root));
119 else
erikwright@chromium.orgb3c1a9b2012-02-24 23:05:30 +0000120 test_server.reset(new net::TestServer(server_type,
121 net::TestServer::kLocalhost,
122 doc_root));
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000123
124 if (!test_server->Start()) {
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000125 printf("Error: failed to start test server. Exiting.\n");
126 return -1;
127 }
128
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000129 if (!file_util::DirectoryExists(test_server->document_root())) {
darin@chromium.orgcc65c352011-04-15 19:07:49 +0000130 printf("Error: invalid doc root: \"%s\" does not exist!\n",
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000131 UTF16ToUTF8(test_server->document_root().LossyDisplayName()).c_str());
darin@chromium.orgcc65c352011-04-15 19:07:49 +0000132 return -1;
133 }
134
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000135 printf("testserver running at %s (type ctrl+c to exit)\n",
cbentzel@chromium.orgca6e57e2011-09-03 03:06:40 +0000136 test_server->host_port_pair().ToString().c_str());
phajdan.jr@chromium.orgefd74402010-09-03 23:54:36 +0000137
138 message_loop.Run();
139 return 0;
140}