Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / test / spawned_test_server / local_test_server_win.cc
blob0476b907caffb879ab100bd3d8199289227fc02f
1 // Copyright (c) 2012 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.
5 #include "net/test/spawned_test_server/local_test_server.h"
7 #include <windows.h>
9 #include "base/base_paths.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/environment.h"
13 #include "base/files/file_path.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/process/launch.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/test/test_timeouts.h"
20 #include "base/threading/thread.h"
21 #include "base/win/scoped_handle.h"
22 #include "net/test/python_utils.h"
24 namespace {
26 // Writes |size| bytes to |handle| and sets |*unblocked| to true.
27 // Used as a crude timeout mechanism by ReadData().
28 void UnblockPipe(HANDLE handle, DWORD size, bool* unblocked) {
29 std::string unblock_data(size, '\0');
30 // Unblock the ReadFile in LocalTestServer::WaitToStart by writing to the
31 // pipe. Make sure the call succeeded, otherwise we are very likely to hang.
32 DWORD bytes_written = 0;
33 LOG(WARNING) << "Timeout reached; unblocking pipe by writing "
34 << size << " bytes";
35 CHECK(WriteFile(handle, unblock_data.data(), size, &bytes_written,
36 NULL));
37 CHECK_EQ(size, bytes_written);
38 *unblocked = true;
41 // Given a file handle, reads into |buffer| until |bytes_max| bytes
42 // has been read or an error has been encountered. Returns
43 // true if the read was successful.
44 bool ReadData(HANDLE read_fd, HANDLE write_fd,
45 DWORD bytes_max, uint8* buffer) {
46 base::Thread thread("test_server_watcher");
47 if (!thread.Start())
48 return false;
50 // Prepare a timeout in case the server fails to start.
51 bool unblocked = false;
52 thread.message_loop()->PostDelayedTask(
53 FROM_HERE, base::Bind(UnblockPipe, write_fd, bytes_max, &unblocked),
54 TestTimeouts::action_max_timeout());
56 DWORD bytes_read = 0;
57 while (bytes_read < bytes_max) {
58 DWORD num_bytes;
59 if (!ReadFile(read_fd, buffer + bytes_read, bytes_max - bytes_read,
60 &num_bytes, NULL)) {
61 PLOG(ERROR) << "ReadFile failed";
62 return false;
64 if (num_bytes <= 0) {
65 LOG(ERROR) << "ReadFile returned invalid byte count: " << num_bytes;
66 return false;
68 bytes_read += num_bytes;
71 thread.Stop();
72 // If the timeout kicked in, abort.
73 if (unblocked) {
74 LOG(ERROR) << "Timeout exceeded for ReadData";
75 return false;
78 return true;
81 } // namespace
83 namespace net {
85 bool LocalTestServer::LaunchPython(const base::FilePath& testserver_path) {
86 base::CommandLine python_command(base::CommandLine::NO_PROGRAM);
87 if (!GetPythonCommand(&python_command))
88 return false;
90 python_command.AppendArgPath(testserver_path);
91 if (!AddCommandLineArguments(&python_command))
92 return false;
94 HANDLE child_read = NULL;
95 HANDLE child_write = NULL;
96 if (!CreatePipe(&child_read, &child_write, NULL, 0)) {
97 PLOG(ERROR) << "Failed to create pipe";
98 return false;
100 child_read_fd_.Set(child_read);
101 child_write_fd_.Set(child_write);
103 // Have the child inherit the write half.
104 if (!::DuplicateHandle(::GetCurrentProcess(), child_write,
105 ::GetCurrentProcess(), &child_write, 0, TRUE,
106 DUPLICATE_SAME_ACCESS)) {
107 PLOG(ERROR) << "Failed to enable pipe inheritance";
108 return false;
111 // Pass the handle on the command-line. Although HANDLE is a
112 // pointer, truncating it on 64-bit machines is okay. See
113 // http://msdn.microsoft.com/en-us/library/aa384203.aspx
115 // "64-bit versions of Windows use 32-bit handles for
116 // interoperability. When sharing a handle between 32-bit and 64-bit
117 // applications, only the lower 32 bits are significant, so it is
118 // safe to truncate the handle (when passing it from 64-bit to
119 // 32-bit) or sign-extend the handle (when passing it from 32-bit to
120 // 64-bit)."
121 python_command.AppendArg("--startup-pipe=" +
122 base::IntToString(reinterpret_cast<uintptr_t>(child_write)));
124 base::LaunchOptions launch_options;
125 launch_options.inherit_handles = true;
126 process_ = base::LaunchProcess(python_command, launch_options);
127 if (!process_.IsValid()) {
128 LOG(ERROR) << "Failed to launch " << python_command.GetCommandLineString();
129 ::CloseHandle(child_write);
130 return false;
133 ::CloseHandle(child_write);
134 return true;
137 bool LocalTestServer::WaitToStart() {
138 base::win::ScopedHandle read_fd(child_read_fd_.Take());
139 base::win::ScopedHandle write_fd(child_write_fd_.Take());
141 uint32 server_data_len = 0;
142 if (!ReadData(read_fd.Get(), write_fd.Get(), sizeof(server_data_len),
143 reinterpret_cast<uint8*>(&server_data_len))) {
144 LOG(ERROR) << "Could not read server_data_len";
145 return false;
147 std::string server_data(server_data_len, '\0');
148 if (!ReadData(read_fd.Get(), write_fd.Get(), server_data_len,
149 reinterpret_cast<uint8*>(&server_data[0]))) {
150 LOG(ERROR) << "Could not read server_data (" << server_data_len
151 << " bytes)";
152 return false;
155 if (!ParseServerData(server_data)) {
156 LOG(ERROR) << "Could not parse server_data: " << server_data;
157 return false;
160 return true;
163 } // namespace net