OLD | NEW |
| (Empty) |
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 <stdint.h> | |
6 #include <stdio.h> | |
7 | |
8 #include "base/at_exit.h" | |
9 #include "base/command_line.h" | |
10 #include "base/files/file_path.h" | |
11 #include "base/logging.h" | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/process/launch.h" | |
14 #include "base/strings/string_number_conversions.h" | |
15 #include "base/test/test_timeouts.h" | |
16 #include "net/test/python_utils.h" | |
17 #include "sync/test/local_sync_test_server.h" | |
18 | |
19 static void PrintUsage() { | |
20 printf("run_sync_testserver [--port=<port>] [--xmpp-port=<xmpp_port>]\n"); | |
21 } | |
22 | |
23 // Launches the chromiumsync_test.py or xmppserver_test.py scripts, which test | |
24 // the sync HTTP and XMPP sever functionality respectively. | |
25 static bool RunSyncTest( | |
26 const base::FilePath::StringType& sync_test_script_name) { | |
27 std::unique_ptr<syncer::LocalSyncTestServer> test_server( | |
28 new syncer::LocalSyncTestServer()); | |
29 if (!test_server->SetPythonPath()) { | |
30 LOG(ERROR) << "Error trying to set python path. Exiting."; | |
31 return false; | |
32 } | |
33 | |
34 base::FilePath sync_test_script_path; | |
35 if (!test_server->GetTestScriptPath(sync_test_script_name, | |
36 &sync_test_script_path)) { | |
37 LOG(ERROR) << "Error trying to get path for test script " | |
38 << sync_test_script_name; | |
39 return false; | |
40 } | |
41 | |
42 base::CommandLine python_command(base::CommandLine::NO_PROGRAM); | |
43 if (!GetPythonCommand(&python_command)) { | |
44 LOG(ERROR) << "Could not get python runtime command."; | |
45 return false; | |
46 } | |
47 | |
48 python_command.AppendArgPath(sync_test_script_path); | |
49 if (!base::LaunchProcess(python_command, base::LaunchOptions()).IsValid()) { | |
50 LOG(ERROR) << "Failed to launch test script " << sync_test_script_name; | |
51 return false; | |
52 } | |
53 return true; | |
54 } | |
55 | |
56 // Gets a port value from the switch with name |switch_name| and writes it to | |
57 // |port|. Returns true if a port was provided and false otherwise. | |
58 static bool GetPortFromSwitch(const std::string& switch_name, uint16_t* port) { | |
59 DCHECK(port != NULL) << "|port| is NULL"; | |
60 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
61 int port_int = 0; | |
62 if (command_line->HasSwitch(switch_name)) { | |
63 std::string port_str = command_line->GetSwitchValueASCII(switch_name); | |
64 if (!base::StringToInt(port_str, &port_int)) { | |
65 return false; | |
66 } | |
67 } | |
68 *port = static_cast<uint16_t>(port_int); | |
69 return true; | |
70 } | |
71 | |
72 int main(int argc, const char* argv[]) { | |
73 base::AtExitManager at_exit_manager; | |
74 base::MessageLoopForIO message_loop; | |
75 | |
76 // Process command line | |
77 base::CommandLine::Init(argc, argv); | |
78 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
79 | |
80 logging::LoggingSettings settings; | |
81 settings.logging_dest = logging::LOG_TO_ALL; | |
82 settings.log_file = FILE_PATH_LITERAL("sync_testserver.log"); | |
83 if (!logging::InitLogging(settings)) { | |
84 printf("Error: could not initialize logging. Exiting.\n"); | |
85 return -1; | |
86 } | |
87 | |
88 TestTimeouts::Initialize(); | |
89 | |
90 if (command_line->HasSwitch("help")) { | |
91 PrintUsage(); | |
92 return 0; | |
93 } | |
94 | |
95 if (command_line->HasSwitch("sync-test")) { | |
96 return RunSyncTest(FILE_PATH_LITERAL("chromiumsync_test.py")) ? 0 : -1; | |
97 } | |
98 | |
99 if (command_line->HasSwitch("xmpp-test")) { | |
100 return RunSyncTest(FILE_PATH_LITERAL("xmppserver_test.py")) ? 0 : -1; | |
101 } | |
102 | |
103 uint16_t port = 0; | |
104 GetPortFromSwitch("port", &port); | |
105 | |
106 uint16_t xmpp_port = 0; | |
107 GetPortFromSwitch("xmpp-port", &xmpp_port); | |
108 | |
109 std::unique_ptr<syncer::LocalSyncTestServer> test_server( | |
110 new syncer::LocalSyncTestServer(port, xmpp_port)); | |
111 if (!test_server->Start()) { | |
112 printf("Error: failed to start python sync test server. Exiting.\n"); | |
113 return -1; | |
114 } | |
115 | |
116 printf("Python sync test server running at %s (type ctrl+c to exit)\n", | |
117 test_server->host_port_pair().ToString().c_str()); | |
118 | |
119 message_loop.Run(); | |
120 return 0; | |
121 } | |
OLD | NEW |