12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <stdio.h>
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/at_exit.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/command_line.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/logging.h"
119ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
12a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch#include "base/process/launch.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_number_conversions.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/test/test_timeouts.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/test/python_utils.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "sync/test/local_sync_test_server.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static void PrintUsage() {
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  printf("run_sync_testserver [--port=<port>] [--xmpp-port=<xmpp_port>]\n");
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Launches the chromiumsync_test.py or xmppserver_test.py scripts, which test
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the sync HTTP and XMPP sever functionality respectively.
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static bool RunSyncTest(
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath::StringType& sync_test_script_name) {
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<syncer::LocalSyncTestServer> test_server(
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new syncer::LocalSyncTestServer());
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (!test_server->SetPythonPath()) {
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(ERROR) << "Error trying to set python path. Exiting.";
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath sync_test_script_path;
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!test_server->GetTestScriptPath(sync_test_script_name,
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                      &sync_test_script_path)) {
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(ERROR) << "Error trying to get path for test script "
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               << sync_test_script_name;
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CommandLine python_command(CommandLine::NO_PROGRAM);
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!GetPythonCommand(&python_command)) {
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(ERROR) << "Could not get python runtime command.";
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  python_command.AppendArgPath(sync_test_script_path);
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!base::LaunchProcess(python_command, base::LaunchOptions(), NULL)) {
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(ERROR) << "Failed to launch test script " << sync_test_script_name;
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Gets a port value from the switch with name |switch_name| and writes it to
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// |port|. Returns true if a port was provided and false otherwise.
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static bool GetPortFromSwitch(const std::string& switch_name, uint16* port) {
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(port != NULL) << "|port| is NULL";
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CommandLine* command_line = CommandLine::ForCurrentProcess();
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int port_int = 0;
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (command_line->HasSwitch(switch_name)) {
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::string port_str = command_line->GetSwitchValueASCII(switch_name);
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!base::StringToInt(port_str, &port_int)) {
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return false;
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  *port = static_cast<uint16>(port_int);
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)int main(int argc, const char* argv[]) {
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::AtExitManager at_exit_manager;
73b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  base::MessageLoopForIO message_loop;
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Process command line
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CommandLine::Init(argc, argv);
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CommandLine* command_line = CommandLine::ForCurrentProcess();
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
79eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  logging::LoggingSettings settings;
80eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  settings.logging_dest = logging::LOG_TO_ALL;
81eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  settings.log_file = FILE_PATH_LITERAL("sync_testserver.log");
82eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!logging::InitLogging(settings)) {
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    printf("Error: could not initialize logging. Exiting.\n");
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return -1;
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TestTimeouts::Initialize();
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (command_line->HasSwitch("help")) {
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PrintUsage();
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return 0;
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (command_line->HasSwitch("sync-test")) {
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return RunSyncTest(FILE_PATH_LITERAL("chromiumsync_test.py")) ? 0 : -1;
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (command_line->HasSwitch("xmpp-test")) {
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return RunSyncTest(FILE_PATH_LITERAL("xmppserver_test.py")) ? 0 : -1;
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  uint16 port = 0;
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetPortFromSwitch("port", &port);
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  uint16 xmpp_port = 0;
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetPortFromSwitch("xmpp-port", &xmpp_port);
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<syncer::LocalSyncTestServer> test_server(
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new syncer::LocalSyncTestServer(port, xmpp_port));
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!test_server->Start()) {
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    printf("Error: failed to start python sync test server. Exiting.\n");
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return -1;
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  printf("Python sync test server running at %s (type ctrl+c to exit)\n",
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         test_server->host_port_pair().ToString().c_str());
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  message_loop.Run();
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return 0;
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
121