erikwright@chromium.org | b3c1a9b | 2012-02-24 23:05:30 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 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" |
akalin@chromium.org | 3dfcd45 | 2010-11-18 22:29:38 +0000 | [diff] [blame] | 9 | #include "base/file_path.h" |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 10 | #include "base/logging.h" |
| 11 | #include "base/message_loop.h" |
tim@chromium.org | b5d4b2a | 2012-03-15 05:13:30 +0000 | [diff] [blame^] | 12 | #include "base/process_util.h" |
tim@chromium.org | c9ff542 | 2011-06-18 11:53:42 +0000 | [diff] [blame] | 13 | #include "base/test/test_timeouts.h" |
darin@chromium.org | cc65c35 | 2011-04-15 19:07:49 +0000 | [diff] [blame] | 14 | #include "base/utf_string_conversions.h" |
tim@chromium.org | b5d4b2a | 2012-03-15 05:13:30 +0000 | [diff] [blame^] | 15 | #include "net/test/python_utils.h" |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 16 | #include "net/test/test_server.h" |
| 17 | |
| 18 | static void PrintUsage() { |
cbentzel@chromium.org | ca6e57e | 2011-09-03 03:06:40 +0000 | [diff] [blame] | 19 | printf("run_testserver --doc-root=relpath [--http|--https|--ftp|--sync]\n" |
| 20 | " [--https-cert=ok|mismatched-name|expired]\n"); |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 21 | printf("(NOTE: relpath should be relative to the 'src' directory)\n"); |
| 22 | } |
| 23 | |
tim@chromium.org | b5d4b2a | 2012-03-15 05:13:30 +0000 | [diff] [blame^] | 24 | // Launches the chromiumsync_test script, testing the --sync functionality. |
| 25 | static 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.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 54 | int 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.org | c3062de | 2011-01-11 01:03:36 +0000 | [diff] [blame] | 62 | 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.org | 3dfcd45 | 2010-11-18 22:29:38 +0000 | [diff] [blame] | 68 | printf("Error: could not initialize logging. Exiting.\n"); |
| 69 | return -1; |
| 70 | } |
| 71 | |
tim@chromium.org | 8d38ca1 | 2011-06-23 23:56:38 +0000 | [diff] [blame] | 72 | TestTimeouts::Initialize(); |
| 73 | |
msw@chromium.org | c7688b8 | 2011-07-13 21:46:32 +0000 | [diff] [blame] | 74 | if (command_line->GetSwitches().empty() || command_line->HasSwitch("help")) { |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 75 | PrintUsage(); |
| 76 | return -1; |
| 77 | } |
| 78 | |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 79 | 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.org | 154bb13 | 2010-11-12 02:20:27 +0000 | [diff] [blame] | 84 | } else if (command_line->HasSwitch("sync")) { |
| 85 | server_type = net::TestServer::TYPE_SYNC; |
tim@chromium.org | b5d4b2a | 2012-03-15 05:13:30 +0000 | [diff] [blame^] | 86 | } else if (command_line->HasSwitch("sync-test")) { |
| 87 | return RunSyncTest() ? 0 : -1; |
akalin@chromium.org | 154bb13 | 2010-11-12 02:20:27 +0000 | [diff] [blame] | 88 | } |
| 89 | |
cbentzel@chromium.org | ca6e57e | 2011-09-03 03:06:40 +0000 | [diff] [blame] | 90 | 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.org | 154bb13 | 2010-11-12 02:20:27 +0000 | [diff] [blame] | 109 | 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.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 114 | } |
| 115 | |
cbentzel@chromium.org | ca6e57e | 2011-09-03 03:06:40 +0000 | [diff] [blame] | 116 | 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.org | b3c1a9b | 2012-02-24 23:05:30 +0000 | [diff] [blame] | 120 | test_server.reset(new net::TestServer(server_type, |
| 121 | net::TestServer::kLocalhost, |
| 122 | doc_root)); |
cbentzel@chromium.org | ca6e57e | 2011-09-03 03:06:40 +0000 | [diff] [blame] | 123 | |
| 124 | if (!test_server->Start()) { |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 125 | printf("Error: failed to start test server. Exiting.\n"); |
| 126 | return -1; |
| 127 | } |
| 128 | |
cbentzel@chromium.org | ca6e57e | 2011-09-03 03:06:40 +0000 | [diff] [blame] | 129 | if (!file_util::DirectoryExists(test_server->document_root())) { |
darin@chromium.org | cc65c35 | 2011-04-15 19:07:49 +0000 | [diff] [blame] | 130 | printf("Error: invalid doc root: \"%s\" does not exist!\n", |
cbentzel@chromium.org | ca6e57e | 2011-09-03 03:06:40 +0000 | [diff] [blame] | 131 | UTF16ToUTF8(test_server->document_root().LossyDisplayName()).c_str()); |
darin@chromium.org | cc65c35 | 2011-04-15 19:07:49 +0000 | [diff] [blame] | 132 | return -1; |
| 133 | } |
| 134 | |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 135 | printf("testserver running at %s (type ctrl+c to exit)\n", |
cbentzel@chromium.org | ca6e57e | 2011-09-03 03:06:40 +0000 | [diff] [blame] | 136 | test_server->host_port_pair().ToString().c_str()); |
phajdan.jr@chromium.org | efd7440 | 2010-09-03 23:54:36 +0000 | [diff] [blame] | 137 | |
| 138 | message_loop.Run(); |
| 139 | return 0; |
| 140 | } |