Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / sync / tools / testserver / run_sync_testserver.cc
blob3ae89fc645dcd6bf1387c0f0985ebd0c9fbf9479
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.
5 #include <stdio.h>
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/message_loop.h"
12 #include "base/process/launch.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/test/test_timeouts.h"
15 #include "net/test/python_utils.h"
16 #include "sync/test/local_sync_test_server.h"
18 static void PrintUsage() {
19 printf("run_sync_testserver [--port=<port>] [--xmpp-port=<xmpp_port>]\n");
22 // Launches the chromiumsync_test.py or xmppserver_test.py scripts, which test
23 // the sync HTTP and XMPP sever functionality respectively.
24 static bool RunSyncTest(
25 const base::FilePath::StringType& sync_test_script_name) {
26 scoped_ptr<syncer::LocalSyncTestServer> test_server(
27 new syncer::LocalSyncTestServer());
28 if (!test_server->SetPythonPath()) {
29 LOG(ERROR) << "Error trying to set python path. Exiting.";
30 return false;
33 base::FilePath sync_test_script_path;
34 if (!test_server->GetTestScriptPath(sync_test_script_name,
35 &sync_test_script_path)) {
36 LOG(ERROR) << "Error trying to get path for test script "
37 << sync_test_script_name;
38 return false;
41 base::CommandLine python_command(base::CommandLine::NO_PROGRAM);
42 if (!GetPythonCommand(&python_command)) {
43 LOG(ERROR) << "Could not get python runtime command.";
44 return false;
47 python_command.AppendArgPath(sync_test_script_path);
48 if (!base::LaunchProcess(python_command, base::LaunchOptions()).IsValid()) {
49 LOG(ERROR) << "Failed to launch test script " << sync_test_script_name;
50 return false;
52 return true;
55 // Gets a port value from the switch with name |switch_name| and writes it to
56 // |port|. Returns true if a port was provided and false otherwise.
57 static bool GetPortFromSwitch(const std::string& switch_name, uint16* port) {
58 DCHECK(port != NULL) << "|port| is NULL";
59 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
60 int port_int = 0;
61 if (command_line->HasSwitch(switch_name)) {
62 std::string port_str = command_line->GetSwitchValueASCII(switch_name);
63 if (!base::StringToInt(port_str, &port_int)) {
64 return false;
67 *port = static_cast<uint16>(port_int);
68 return true;
71 int main(int argc, const char* argv[]) {
72 base::AtExitManager at_exit_manager;
73 base::MessageLoopForIO message_loop;
75 // Process command line
76 base::CommandLine::Init(argc, argv);
77 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
79 logging::LoggingSettings settings;
80 settings.logging_dest = logging::LOG_TO_ALL;
81 settings.log_file = FILE_PATH_LITERAL("sync_testserver.log");
82 if (!logging::InitLogging(settings)) {
83 printf("Error: could not initialize logging. Exiting.\n");
84 return -1;
87 TestTimeouts::Initialize();
89 if (command_line->HasSwitch("help")) {
90 PrintUsage();
91 return 0;
94 if (command_line->HasSwitch("sync-test")) {
95 return RunSyncTest(FILE_PATH_LITERAL("chromiumsync_test.py")) ? 0 : -1;
98 if (command_line->HasSwitch("xmpp-test")) {
99 return RunSyncTest(FILE_PATH_LITERAL("xmppserver_test.py")) ? 0 : -1;
102 uint16 port = 0;
103 GetPortFromSwitch("port", &port);
105 uint16 xmpp_port = 0;
106 GetPortFromSwitch("xmpp-port", &xmpp_port);
108 scoped_ptr<syncer::LocalSyncTestServer> test_server(
109 new syncer::LocalSyncTestServer(port, xmpp_port));
110 if (!test_server->Start()) {
111 printf("Error: failed to start python sync test server. Exiting.\n");
112 return -1;
115 printf("Python sync test server running at %s (type ctrl+c to exit)\n",
116 test_server->host_port_pair().ToString().c_str());
118 message_loop.Run();
119 return 0;