This source file includes following definitions.
- UnblockPipe
- ReadData
- path_modified_
- LaunchPython
- WaitToStart
#include "net/test/spawned_test_server/local_test_server.h"
#include <windows.h>
#include <wincrypt.h>
#include "base/base_paths.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/environment.h"
#include "base/files/file_path.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h"
#include "base/win/scoped_handle.h"
#include "net/test/python_utils.h"
#pragma comment(lib, "crypt32.lib")
namespace {
void UnblockPipe(HANDLE handle, DWORD size, bool* unblocked) {
std::string unblock_data(size, '\0');
DWORD bytes_written = 0;
LOG(WARNING) << "Timeout reached; unblocking pipe by writing "
<< size << " bytes";
CHECK(WriteFile(handle, unblock_data.data(), size, &bytes_written,
NULL));
CHECK_EQ(size, bytes_written);
*unblocked = true;
}
bool ReadData(HANDLE read_fd, HANDLE write_fd,
DWORD bytes_max, uint8* buffer) {
base::Thread thread("test_server_watcher");
if (!thread.Start())
return false;
bool unblocked = false;
thread.message_loop()->PostDelayedTask(
FROM_HERE, base::Bind(UnblockPipe, write_fd, bytes_max, &unblocked),
TestTimeouts::action_max_timeout());
DWORD bytes_read = 0;
while (bytes_read < bytes_max) {
DWORD num_bytes;
if (!ReadFile(read_fd, buffer + bytes_read, bytes_max - bytes_read,
&num_bytes, NULL)) {
PLOG(ERROR) << "ReadFile failed";
return false;
}
if (num_bytes <= 0) {
LOG(ERROR) << "ReadFile returned invalid byte count: " << num_bytes;
return false;
}
bytes_read += num_bytes;
}
thread.Stop();
if (unblocked) {
LOG(ERROR) << "Timeout exceeded for ReadData";
return false;
}
return true;
}
class ScopedPath {
public:
explicit ScopedPath(const base::FilePath& path_to_add);
~ScopedPath();
private:
std::string old_path_;
scoped_ptr<base::Environment> environment_;
bool path_modified_;
DISALLOW_COPY_AND_ASSIGN(ScopedPath);
};
ScopedPath::ScopedPath(const base::FilePath& path_to_add)
: environment_(base::Environment::Create()),
path_modified_(false) {
environment_->GetVar("PATH", &old_path_);
std::string new_value = old_path_;
if (!new_value.empty())
new_value += ";";
new_value += base::WideToUTF8(path_to_add.value());
path_modified_ = environment_->SetVar("PATH", new_value);
}
ScopedPath::~ScopedPath() {
if (!path_modified_)
return;
if (old_path_.empty())
environment_->UnSetVar("PATH");
else
environment_->SetVar("PATH", old_path_);
}
}
namespace net {
bool LocalTestServer::LaunchPython(const base::FilePath& testserver_path) {
base::CommandLine python_command(base::CommandLine::NO_PROGRAM);
if (!GetPythonCommand(&python_command))
return false;
python_command.AppendArgPath(testserver_path);
if (!AddCommandLineArguments(&python_command))
return false;
HANDLE child_read = NULL;
HANDLE child_write = NULL;
if (!CreatePipe(&child_read, &child_write, NULL, 0)) {
PLOG(ERROR) << "Failed to create pipe";
return false;
}
child_read_fd_.Set(child_read);
child_write_fd_.Set(child_write);
if (!SetHandleInformation(child_write, HANDLE_FLAG_INHERIT,
HANDLE_FLAG_INHERIT)) {
PLOG(ERROR) << "Failed to enable pipe inheritance";
return false;
}
python_command.AppendArg("--startup-pipe=" +
base::IntToString(reinterpret_cast<uintptr_t>(child_write)));
job_handle_.Set(CreateJobObject(NULL, NULL));
if (!job_handle_.IsValid()) {
LOG(ERROR) << "Could not create JobObject.";
return false;
}
if (!base::SetJobObjectLimitFlags(job_handle_.Get(),
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)) {
LOG(ERROR) << "Could not SetJobObjectLimitFlags.";
return false;
}
base::LaunchOptions launch_options;
launch_options.inherit_handles = true;
launch_options.job_handle = job_handle_.Get();
if (!base::LaunchProcess(python_command, launch_options, &process_handle_)) {
LOG(ERROR) << "Failed to launch " << python_command.GetCommandLineString();
return false;
}
return true;
}
bool LocalTestServer::WaitToStart() {
base::win::ScopedHandle read_fd(child_read_fd_.Take());
base::win::ScopedHandle write_fd(child_write_fd_.Take());
uint32 server_data_len = 0;
if (!ReadData(read_fd.Get(), write_fd.Get(), sizeof(server_data_len),
reinterpret_cast<uint8*>(&server_data_len))) {
LOG(ERROR) << "Could not read server_data_len";
return false;
}
std::string server_data(server_data_len, '\0');
if (!ReadData(read_fd.Get(), write_fd.Get(), server_data_len,
reinterpret_cast<uint8*>(&server_data[0]))) {
LOG(ERROR) << "Could not read server_data (" << server_data_len
<< " bytes)";
return false;
}
if (!ParseServerData(server_data)) {
LOG(ERROR) << "Could not parse server_data: " << server_data;
return false;
}
return true;
}
}