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