run_testserver.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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/spawned_test_server/spawned_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  base::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::SpawnedTestServer::Type server_type;
50  if (command_line->HasSwitch("http")) {
51    server_type = net::SpawnedTestServer::TYPE_HTTP;
52  } else if (command_line->HasSwitch("https")) {
53    server_type = net::SpawnedTestServer::TYPE_HTTPS;
54  } else if (command_line->HasSwitch("ws")) {
55    server_type = net::SpawnedTestServer::TYPE_WS;
56  } else if (command_line->HasSwitch("wss")) {
57    server_type = net::SpawnedTestServer::TYPE_WSS;
58  } else if (command_line->HasSwitch("ftp")) {
59    server_type = net::SpawnedTestServer::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::SpawnedTestServer::TYPE_HTTPS;
65    else
66      server_type = net::SpawnedTestServer::TYPE_HTTP;
67  }
68
69  net::SpawnedTestServer::SSLOptions ssl_options;
70  if (command_line->HasSwitch("ssl-cert")) {
71    if (!net::SpawnedTestServer::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 =
79          net::SpawnedTestServer::SSLOptions::CERT_OK;
80    } else if (cert_option == "mismatched-name") {
81      ssl_options.server_certificate =
82          net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
83    } else if (cert_option == "expired") {
84      ssl_options.server_certificate =
85          net::SpawnedTestServer::SSLOptions::CERT_EXPIRED;
86    } else {
87      printf("Error: --ssl-cert has invalid value %s\n", cert_option.c_str());
88      PrintUsage();
89      return -1;
90    }
91  }
92
93  base::FilePath doc_root = command_line->GetSwitchValuePath("doc-root");
94  if (doc_root.empty()) {
95    printf("Error: --doc-root must be specified\n");
96    PrintUsage();
97    return -1;
98  }
99
100  scoped_ptr<net::SpawnedTestServer> test_server;
101  if (net::SpawnedTestServer::UsingSSL(server_type)) {
102    test_server.reset(
103        new net::SpawnedTestServer(server_type, ssl_options, doc_root));
104  } else {
105    test_server.reset(new net::SpawnedTestServer(
106                          server_type,
107                          net::SpawnedTestServer::kLocalhost,
108                          doc_root));
109  }
110
111  if (!test_server->Start()) {
112    printf("Error: failed to start test server. Exiting.\n");
113    return -1;
114  }
115
116  if (!file_util::DirectoryExists(test_server->document_root())) {
117    printf("Error: invalid doc root: \"%s\" does not exist!\n",
118        UTF16ToUTF8(test_server->document_root().LossyDisplayName()).c_str());
119    return -1;
120  }
121
122  printf("testserver running at %s (type ctrl+c to exit)\n",
123         test_server->host_port_pair().ToString().c_str());
124
125  message_loop.Run();
126  return 0;
127}
128