summaryrefslogtreecommitdiffstats
path: root/sync/tools/testserver/run_sync_testserver.cc
blob: 2057db2295c416b1917e7e097916450fc77de7bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdint.h>
#include <stdio.h>

#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/process/launch.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/test_timeouts.h"
#include "net/test/python_utils.h"
#include "sync/test/local_sync_test_server.h"

static void PrintUsage() {
  printf("run_sync_testserver [--port=<port>] [--xmpp-port=<xmpp_port>]\n");
}

// Launches the chromiumsync_test.py or xmppserver_test.py scripts, which test
// the sync HTTP and XMPP sever functionality respectively.
static bool RunSyncTest(
    const base::FilePath::StringType& sync_test_script_name) {
  scoped_ptr<syncer::LocalSyncTestServer> test_server(
      new syncer::LocalSyncTestServer());
  if (!test_server->SetPythonPath()) {
    LOG(ERROR) << "Error trying to set python path. Exiting.";
    return false;
  }

  base::FilePath sync_test_script_path;
  if (!test_server->GetTestScriptPath(sync_test_script_name,
                                      &sync_test_script_path)) {
    LOG(ERROR) << "Error trying to get path for test script "
               << sync_test_script_name;
    return false;
  }

  base::CommandLine python_command(base::CommandLine::NO_PROGRAM);
  if (!GetPythonCommand(&python_command)) {
    LOG(ERROR) << "Could not get python runtime command.";
    return false;
  }

  python_command.AppendArgPath(sync_test_script_path);
  if (!base::LaunchProcess(python_command, base::LaunchOptions()).IsValid()) {
    LOG(ERROR) << "Failed to launch test script " << sync_test_script_name;
    return false;
  }
  return true;
}

// Gets a port value from the switch with name |switch_name| and writes it to
// |port|. Returns true if a port was provided and false otherwise.
static bool GetPortFromSwitch(const std::string& switch_name, uint16_t* port) {
  DCHECK(port != NULL) << "|port| is NULL";
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  int port_int = 0;
  if (command_line->HasSwitch(switch_name)) {
    std::string port_str = command_line->GetSwitchValueASCII(switch_name);
    if (!base::StringToInt(port_str, &port_int)) {
      return false;
    }
  }
  *port = static_cast<uint16_t>(port_int);
  return true;
}

int main(int argc, const char* argv[]) {
  base::AtExitManager at_exit_manager;
  base::MessageLoopForIO message_loop;

  // Process command line
  base::CommandLine::Init(argc, argv);
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();

  logging::LoggingSettings settings;
  settings.logging_dest = logging::LOG_TO_ALL;
  settings.log_file = FILE_PATH_LITERAL("sync_testserver.log");
  if (!logging::InitLogging(settings)) {
    printf("Error: could not initialize logging. Exiting.\n");
    return -1;
  }

  TestTimeouts::Initialize();

  if (command_line->HasSwitch("help")) {
    PrintUsage();
    return 0;
  }

  if (command_line->HasSwitch("sync-test")) {
    return RunSyncTest(FILE_PATH_LITERAL("chromiumsync_test.py")) ? 0 : -1;
  }

  if (command_line->HasSwitch("xmpp-test")) {
    return RunSyncTest(FILE_PATH_LITERAL("xmppserver_test.py")) ? 0 : -1;
  }

  uint16_t port = 0;
  GetPortFromSwitch("port", &port);

  uint16_t xmpp_port = 0;
  GetPortFromSwitch("xmpp-port", &xmpp_port);

  scoped_ptr<syncer::LocalSyncTestServer> test_server(
      new syncer::LocalSyncTestServer(port, xmpp_port));
  if (!test_server->Start()) {
    printf("Error: failed to start python sync test server. Exiting.\n");
    return -1;
  }

  printf("Python sync test server running at %s (type ctrl+c to exit)\n",
         test_server->host_port_pair().ToString().c_str());

  message_loop.Run();
  return 0;
}