local_sync_test_server.cc revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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 "sync/test/local_sync_test_server.h"
6
7#include "base/command_line.h"
8#include "base/path_service.h"
9#include "base/string_number_conversions.h"
10#include "base/values.h"
11#include "net/test/python_utils.h"
12#include "net/test/spawned_test_server/spawned_test_server.h"
13
14namespace syncer {
15
16LocalSyncTestServer::LocalSyncTestServer()
17    : LocalTestServer(
18        net::SpawnedTestServer::TYPE_HTTP,  // Sync uses the HTTP scheme.
19        net::SpawnedTestServer::kLocalhost,
20        base::FilePath()),
21      xmpp_port_(0) {}
22
23LocalSyncTestServer::LocalSyncTestServer(uint16 port, uint16 xmpp_port)
24    : LocalTestServer(
25        net::SpawnedTestServer::TYPE_HTTP,  // Sync uses the HTTP scheme.
26        net::SpawnedTestServer::kLocalhost,
27        base::FilePath()),
28      xmpp_port_(xmpp_port) {
29  SetPort(port);
30}
31
32LocalSyncTestServer::~LocalSyncTestServer() {}
33
34bool LocalSyncTestServer::AddCommandLineArguments(
35    CommandLine* command_line) const {
36  if (!LocalTestServer::AddCommandLineArguments(command_line))
37    return false;
38  if (xmpp_port_ != 0) {
39    std::string xmpp_port_str = base::IntToString(xmpp_port_);
40    command_line->AppendArg("--xmpp-port=" + xmpp_port_str);
41  }
42  return true;
43}
44
45bool LocalSyncTestServer::GetTestServerPath(
46    base::FilePath* testserver_path) const {
47  base::FilePath testserver_dir;
48  if (!PathService::Get(base::DIR_SOURCE_ROOT, &testserver_dir)) {
49    LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT";
50    return false;
51  }
52  testserver_dir = testserver_dir.Append(FILE_PATH_LITERAL("sync"))
53                                 .Append(FILE_PATH_LITERAL("tools"))
54                                 .Append(FILE_PATH_LITERAL("testserver"));
55
56  *testserver_path =
57      testserver_dir.Append(FILE_PATH_LITERAL("sync_testserver.py"));
58  return true;
59}
60
61bool LocalSyncTestServer::GetTestScriptPath(
62    const base::FilePath::StringType& test_script_name,
63    base::FilePath* test_script_path) const {
64  base::FilePath testserver_path;
65  if (!GetTestServerPath(&testserver_path))
66    return false;
67  *test_script_path = testserver_path.DirName().Append(test_script_name);
68  return true;
69}
70
71bool LocalSyncTestServer::SetPythonPath() const {
72  if (!LocalTestServer::SetPythonPath())
73    return false;
74
75  // Add the net/tools/testserver directory to the path, so that testserver_base
76  // can be imported.
77  base::FilePath net_testserver_path;
78  if (!LocalTestServer::GetTestServerPath(&net_testserver_path)) {
79    LOG(ERROR) << "Failed to get net testserver path.";
80    return false;
81  }
82  AppendToPythonPath(net_testserver_path.DirName());
83
84  // Locate the Python code generated by the sync protocol buffers compiler.
85  base::FilePath pyproto_dir;
86  if (!GetPyProtoPath(&pyproto_dir)) {
87    LOG(WARNING) << "Cannot find pyproto dir for generated code. "
88                 << "Testserver features that rely on it will not work";
89    return true;
90  }
91  AppendToPythonPath(pyproto_dir.AppendASCII("sync").AppendASCII("protocol"));
92  return true;
93}
94
95}  // namespace syncer
96