run_testserver.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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/strings/utf_string_conversions.h"
13#include "base/test/test_timeouts.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  logging::LoggingSettings settings;
32  settings.logging_dest = logging::LOG_TO_ALL;
33  settings.log_file = FILE_PATH_LITERAL("testserver.log");
34  if (!logging::InitLogging(settings)) {
35    printf("Error: could not initialize logging. Exiting.\n");
36    return -1;
37  }
38
39  TestTimeouts::Initialize();
40
41  if (command_line->GetSwitches().empty() ||
42      command_line->HasSwitch("help")) {
43    PrintUsage();
44    return -1;
45  }
46
47  net::SpawnedTestServer::Type server_type;
48  if (command_line->HasSwitch("http")) {
49    server_type = net::SpawnedTestServer::TYPE_HTTP;
50  } else if (command_line->HasSwitch("https")) {
51    server_type = net::SpawnedTestServer::TYPE_HTTPS;
52  } else if (command_line->HasSwitch("ws")) {
53    server_type = net::SpawnedTestServer::TYPE_WS;
54  } else if (command_line->HasSwitch("wss")) {
55    server_type = net::SpawnedTestServer::TYPE_WSS;
56  } else if (command_line->HasSwitch("ftp")) {
57    server_type = net::SpawnedTestServer::TYPE_FTP;
58  } else {
59    // If no scheme switch is specified, select http or https scheme.
60    // TODO(toyoshim): Remove this estimation.
61    if (command_line->HasSwitch("ssl-cert"))
62      server_type = net::SpawnedTestServer::TYPE_HTTPS;
63    else
64      server_type = net::SpawnedTestServer::TYPE_HTTP;
65  }
66
67  net::SpawnedTestServer::SSLOptions ssl_options;
68  if (command_line->HasSwitch("ssl-cert")) {
69    if (!net::SpawnedTestServer::UsingSSL(server_type)) {
70      printf("Error: --ssl-cert is specified on non-secure scheme\n");
71      PrintUsage();
72      return -1;
73    }
74    std::string cert_option = command_line->GetSwitchValueASCII("ssl-cert");
75    if (cert_option == "ok") {
76      ssl_options.server_certificate =
77          net::SpawnedTestServer::SSLOptions::CERT_OK;
78    } else if (cert_option == "mismatched-name") {
79      ssl_options.server_certificate =
80          net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
81    } else if (cert_option == "expired") {
82      ssl_options.server_certificate =
83          net::SpawnedTestServer::SSLOptions::CERT_EXPIRED;
84    } else {
85      printf("Error: --ssl-cert has invalid value %s\n", cert_option.c_str());
86      PrintUsage();
87      return -1;
88    }
89  }
90
91  base::FilePath doc_root = command_line->GetSwitchValuePath("doc-root");
92  if (doc_root.empty()) {
93    printf("Error: --doc-root must be specified\n");
94    PrintUsage();
95    return -1;
96  }
97
98  scoped_ptr<net::SpawnedTestServer> test_server;
99  if (net::SpawnedTestServer::UsingSSL(server_type)) {
100    test_server.reset(
101        new net::SpawnedTestServer(server_type, ssl_options, doc_root));
102  } else {
103    test_server.reset(new net::SpawnedTestServer(
104                          server_type,
105                          net::SpawnedTestServer::kLocalhost,
106                          doc_root));
107  }
108
109  if (!test_server->Start()) {
110    printf("Error: failed to start test server. Exiting.\n");
111    return -1;
112  }
113
114  if (!file_util::DirectoryExists(test_server->document_root())) {
115    printf("Error: invalid doc root: \"%s\" does not exist!\n",
116        UTF16ToUTF8(test_server->document_root().LossyDisplayName()).c_str());
117    return -1;
118  }
119
120  printf("testserver running at %s (type ctrl+c to exit)\n",
121         test_server->host_port_pair().ToString().c_str());
122
123  message_loop.Run();
124  return 0;
125}
126