This source file includes following definitions.
- GetTestServerPortInfoFile
- GetServerTypeString
- spawner_server_port_
- spawner_server_port_
- Start
- StartInBackground
- BlockUntilStarted
- Stop
- GetDocumentRoot
- Init
#include "net/test/spawned_test_server/remote_test_server.h"
#include <vector>
#include "base/base_paths.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/values.h"
#include "net/base/host_port_pair.h"
#include "net/base/net_errors.h"
#include "net/test/spawned_test_server/spawner_communicator.h"
#include "url/gurl.h"
namespace net {
namespace {
base::FilePath GetTestServerPortInfoFile() {
#if !defined(OS_ANDROID)
return base::FilePath("/tmp/net-test-server-ports");
#else
base::FilePath test_data_dir;
PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &test_data_dir);
return test_data_dir.Append("net-test-server-ports");
#endif
}
std::string GetServerTypeString(BaseTestServer::Type type) {
switch (type) {
case BaseTestServer::TYPE_FTP:
return "ftp";
case BaseTestServer::TYPE_HTTP:
case BaseTestServer::TYPE_HTTPS:
return "http";
case BaseTestServer::TYPE_WS:
case BaseTestServer::TYPE_WSS:
return "ws";
case BaseTestServer::TYPE_TCP_ECHO:
return "tcpecho";
case BaseTestServer::TYPE_UDP_ECHO:
return "udpecho";
default:
NOTREACHED();
}
return std::string();
}
}
RemoteTestServer::RemoteTestServer(Type type,
const std::string& host,
const base::FilePath& document_root)
: BaseTestServer(type, host),
spawner_server_port_(0) {
if (!Init(document_root))
NOTREACHED();
}
RemoteTestServer::RemoteTestServer(Type type,
const SSLOptions& ssl_options,
const base::FilePath& document_root)
: BaseTestServer(type, ssl_options),
spawner_server_port_(0) {
if (!Init(document_root))
NOTREACHED();
}
RemoteTestServer::~RemoteTestServer() {
Stop();
}
bool RemoteTestServer::Start() {
if (spawner_communicator_.get())
return true;
spawner_communicator_.reset(new SpawnerCommunicator(spawner_server_port_));
base::DictionaryValue arguments_dict;
if (!GenerateArguments(&arguments_dict))
return false;
arguments_dict.SetString("server-type", GetServerTypeString(type()));
std::string arguments_string;
base::JSONWriter::Write(&arguments_dict, &arguments_string);
if (arguments_string.empty())
return false;
uint16 test_server_port;
if (!spawner_communicator_->StartServer(arguments_string,
&test_server_port)) {
return false;
}
if (0 == test_server_port)
return false;
base::DictionaryValue server_data_dict;
server_data_dict.SetInteger("port", test_server_port);
std::string server_data;
base::JSONWriter::Write(&server_data_dict, &server_data);
if (server_data.empty() || !ParseServerData(server_data)) {
LOG(ERROR) << "Could not parse server_data: " << server_data;
return false;
}
return SetupWhenServerStarted();
}
bool RemoteTestServer::StartInBackground() {
NOTIMPLEMENTED();
return false;
}
bool RemoteTestServer::BlockUntilStarted() {
NOTIMPLEMENTED();
return false;
}
bool RemoteTestServer::Stop() {
if (!spawner_communicator_.get())
return true;
CleanUpWhenStoppingServer();
bool stopped = spawner_communicator_->StopServer();
spawner_communicator_.reset(NULL);
return stopped;
}
base::FilePath RemoteTestServer::GetDocumentRoot() const {
base::FilePath src_dir;
PathService::Get(base::DIR_SOURCE_ROOT, &src_dir);
return src_dir.Append(document_root());
}
bool RemoteTestServer::Init(const base::FilePath& document_root) {
if (document_root.IsAbsolute())
return false;
int test_server_port = 0;
std::string port_info;
if (!base::ReadFileToString(GetTestServerPortInfoFile(), &port_info) ||
port_info.empty()) {
return false;
}
std::vector<std::string> ports;
base::SplitString(port_info, ':', &ports);
if (ports.size() != 2u)
return false;
base::StringToInt(ports[0], &spawner_server_port_);
if (!spawner_server_port_ ||
static_cast<uint32>(spawner_server_port_) >= kuint16max)
return false;
base::StringToInt(ports[1], &test_server_port);
if (static_cast<uint32>(test_server_port) >= kuint16max)
return false;
SetPort(test_server_port);
SetResourcePath(document_root, base::FilePath().AppendASCII("net")
.AppendASCII("data")
.AppendASCII("ssl")
.AppendASCII("certificates"));
return true;
}
}