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/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" 17 18 static void PrintUsage() { 19 printf("run_sync_testserver [--port=<port>] [--xmpp-port=<xmpp_port>]\n"); 20 } 21 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; 31 } 32 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; 39 } 40 41 CommandLine python_command(CommandLine::NO_PROGRAM); 42 if (!GetPythonCommand(&python_command)) { 43 LOG(ERROR) << "Could not get python runtime command."; 44 return false; 45 } 46 47 python_command.AppendArgPath(sync_test_script_path); 48 if (!base::LaunchProcess(python_command, base::LaunchOptions(), NULL)) { 49 LOG(ERROR) << "Failed to launch test script " << sync_test_script_name; 50 return false; 51 } 52 return true; 53 } 54 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 CommandLine* command_line = 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; 65 } 66 } 67 *port = static_cast<uint16>(port_int); 68 return true; 69 } 70 71 int main(int argc, const char* argv[]) { 72 base::AtExitManager at_exit_manager; 73 base::MessageLoopForIO message_loop; 74 75 // Process command line 76 CommandLine::Init(argc, argv); 77 CommandLine* command_line = CommandLine::ForCurrentProcess(); 78 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; 85 } 86 87 TestTimeouts::Initialize(); 88 89 if (command_line->HasSwitch("help")) { 90 PrintUsage(); 91 return 0; 92 } 93 94 if (command_line->HasSwitch("sync-test")) { 95 return RunSyncTest(FILE_PATH_LITERAL("chromiumsync_test.py")) ? 0 : -1; 96 } 97 98 if (command_line->HasSwitch("xmpp-test")) { 99 return RunSyncTest(FILE_PATH_LITERAL("xmppserver_test.py")) ? 0 : -1; 100 } 101 102 uint16 port = 0; 103 GetPortFromSwitch("port", &port); 104 105 uint16 xmpp_port = 0; 106 GetPortFromSwitch("xmpp-port", &xmpp_port); 107 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; 113 } 114 115 printf("Python sync test server running at %s (type ctrl+c to exit)\n", 116 test_server->host_port_pair().ToString().c_str()); 117 118 message_loop.Run(); 119 return 0; 120 } 121