run_testserver.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright 2013 The Chromium Authors. All rights reserved.
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"
9#include "base/files/file_path.h"
10#include "base/logging.h"
11#include "base/message_loop.h"
12#include "base/test/test_timeouts.h"
13#include "base/utf_string_conversions.h"
14#include "net/test/test_server.h"
15
16static void PrintUsage() {
17  printf("run_testserver --doc-root=relpath\n"
18         "               [--http|--https|--ws|--wss|--ftp]\n"
19         "               [--ssl-cert=ok|mismatched-name|expired]\n");
20  printf("(NOTE: relpath should be relative to the 'src' directory.\n");
21}
22
23int main(int argc, const char* argv[]) {
24  base::AtExitManager at_exit_manager;
25  MessageLoopForIO message_loop;
26
27  // Process command line
28  CommandLine::Init(argc, argv);
29  CommandLine* command_line = CommandLine::ForCurrentProcess();
30
31  if (!logging::InitLogging(
32          FILE_PATH_LITERAL("testserver.log"),
33          logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG,
34          logging::LOCK_LOG_FILE,
35          logging::APPEND_TO_OLD_LOG_FILE,
36          logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS)) {
37    printf("Error: could not initialize logging. Exiting.\n");
38    return -1;
39  }
40
41  TestTimeouts::Initialize();
42
43  if (command_line->GetSwitches().empty() ||
44      command_line->HasSwitch("help")) {
45    PrintUsage();
46    return -1;
47  }
48
49  net::TestServer::Type server_type;
50  if (command_line->HasSwitch("http")) {
51    server_type = net::TestServer::TYPE_HTTP;
52  } else if (command_line->HasSwitch("https")) {
53    server_type = net::TestServer::TYPE_HTTPS;
54  } else if (command_line->HasSwitch("ws")) {
55    server_type = net::TestServer::TYPE_WS;
56  } else if (command_line->HasSwitch("wss")) {
57    server_type = net::TestServer::TYPE_WSS;
58  } else if (command_line->HasSwitch("ftp")) {
59    server_type = net::TestServer::TYPE_FTP;
60  } else {
61    // If no scheme switch is specified, select http or https scheme.
62    // TODO(toyoshim): Remove this estimation.
63    if (command_line->HasSwitch("ssl-cert"))
64      server_type = net::TestServer::TYPE_HTTPS;
65    else
66      server_type = net::TestServer::TYPE_HTTP;
67  }
68
69  net::TestServer::SSLOptions ssl_options;
70  if (command_line->HasSwitch("ssl-cert")) {
71    if (!net::TestServer::UsingSSL(server_type)) {
72      printf("Error: --ssl-cert is specified on non-secure scheme\n");
73      PrintUsage();
74      return -1;
75    }
76    std::string cert_option = command_line->GetSwitchValueASCII("ssl-cert");
77    if (cert_option == "ok") {
78      ssl_options.server_certificate = net::TestServer::SSLOptions::CERT_OK;
79    } else if (cert_option == "mismatched-name") {
80      ssl_options.server_certificate =
81          net::TestServer::SSLOptions::CERT_MISMATCHED_NAME;
82    } else if (cert_option == "expired") {
83      ssl_options.server_certificate =
84          net::TestServer::SSLOptions::CERT_EXPIRED;
85    } else {
86      printf("Error: --ssl-cert has invalid value %s\n", cert_option.c_str());
87      PrintUsage();
88      return -1;
89    }
90  }
91
92  base::FilePath doc_root = command_line->GetSwitchValuePath("doc-root");
93  if (doc_root.empty()) {
94    printf("Error: --doc-root must be specified\n");
95    PrintUsage();
96    return -1;
97  }
98
99  scoped_ptr<net::TestServer> test_server;
100  if (net::TestServer::UsingSSL(server_type)) {
101    test_server.reset(new net::TestServer(server_type, ssl_options, doc_root));
102  } else {
103    test_server.reset(new net::TestServer(server_type,
104                                          net::TestServer::kLocalhost,
105                                          doc_root));
106  }
107
108  if (!test_server->Start()) {
109    printf("Error: failed to start test server. Exiting.\n");
110    return -1;
111  }
112
113  if (!file_util::DirectoryExists(test_server->document_root())) {
114    printf("Error: invalid doc root: \"%s\" does not exist!\n",
115        UTF16ToUTF8(test_server->document_root().LossyDisplayName()).c_str());
116    return -1;
117  }
118
119  printf("testserver running at %s (type ctrl+c to exit)\n",
120         test_server->host_port_pair().ToString().c_str());
121
122  message_loop.Run();
123  return 0;
124}
125