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"
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"
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 "
35 CHECK(WriteFile(handle
, unblock_data
.data(), size
, &bytes_written
,
37 CHECK_EQ(size
, bytes_written
);
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");
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());
57 while (bytes_read
< bytes_max
) {
59 if (!ReadFile(read_fd
, buffer
+ bytes_read
, bytes_max
- bytes_read
,
61 PLOG(ERROR
) << "ReadFile failed";
65 LOG(ERROR
) << "ReadFile returned invalid byte count: " << num_bytes
;
68 bytes_read
+= num_bytes
;
72 // If the timeout kicked in, abort.
74 LOG(ERROR
) << "Timeout exceeded for ReadData";
81 // Class that sets up a temporary path that includes the supplied path
84 // TODO(bratell): By making this more generic we can possibly reuse
85 // it at other places such as
86 // chrome/common/multi_process_lock_unittest.cc.
89 // Constructor which sets up the environment to include the path to
91 explicit ScopedPath(const base::FilePath
& path_to_add
);
93 // Destructor that restores the path that were active when the
94 // object was constructed.
98 // The PATH environment variable before it was changed or an empty
99 // string if there was no PATH environment variable.
100 std::string old_path_
;
102 // The helper object that allows us to read and set environment
103 // variables more easily.
104 scoped_ptr
<base::Environment
> environment_
;
106 // A flag saying if we have actually modified the environment.
109 DISALLOW_COPY_AND_ASSIGN(ScopedPath
);
112 ScopedPath::ScopedPath(const base::FilePath
& path_to_add
)
113 : environment_(base::Environment::Create()),
114 path_modified_(false) {
115 environment_
->GetVar("PATH", &old_path_
);
117 std::string new_value
= old_path_
;
118 if (!new_value
.empty())
121 new_value
+= base::WideToUTF8(path_to_add
.value());
123 path_modified_
= environment_
->SetVar("PATH", new_value
);
126 ScopedPath::~ScopedPath() {
129 if (old_path_
.empty())
130 environment_
->UnSetVar("PATH");
132 environment_
->SetVar("PATH", old_path_
);
139 bool LocalTestServer::LaunchPython(const base::FilePath
& testserver_path
) {
140 base::CommandLine
python_command(base::CommandLine::NO_PROGRAM
);
141 if (!GetPythonCommand(&python_command
))
144 python_command
.AppendArgPath(testserver_path
);
145 if (!AddCommandLineArguments(&python_command
))
148 HANDLE child_read
= NULL
;
149 HANDLE child_write
= NULL
;
150 if (!CreatePipe(&child_read
, &child_write
, NULL
, 0)) {
151 PLOG(ERROR
) << "Failed to create pipe";
154 child_read_fd_
.Set(child_read
);
155 child_write_fd_
.Set(child_write
);
157 // Have the child inherit the write half.
158 if (!SetHandleInformation(child_write
, HANDLE_FLAG_INHERIT
,
159 HANDLE_FLAG_INHERIT
)) {
160 PLOG(ERROR
) << "Failed to enable pipe inheritance";
164 // Pass the handle on the command-line. Although HANDLE is a
165 // pointer, truncating it on 64-bit machines is okay. See
166 // http://msdn.microsoft.com/en-us/library/aa384203.aspx
168 // "64-bit versions of Windows use 32-bit handles for
169 // interoperability. When sharing a handle between 32-bit and 64-bit
170 // applications, only the lower 32 bits are significant, so it is
171 // safe to truncate the handle (when passing it from 64-bit to
172 // 32-bit) or sign-extend the handle (when passing it from 32-bit to
174 python_command
.AppendArg("--startup-pipe=" +
175 base::IntToString(reinterpret_cast<uintptr_t>(child_write
)));
177 base::LaunchOptions launch_options
;
178 launch_options
.inherit_handles
= true;
179 process_
= base::LaunchProcess(python_command
, launch_options
);
180 if (!process_
.IsValid()) {
181 LOG(ERROR
) << "Failed to launch " << python_command
.GetCommandLineString();
188 bool LocalTestServer::WaitToStart() {
189 base::win::ScopedHandle
read_fd(child_read_fd_
.Take());
190 base::win::ScopedHandle
write_fd(child_write_fd_
.Take());
192 uint32 server_data_len
= 0;
193 if (!ReadData(read_fd
.Get(), write_fd
.Get(), sizeof(server_data_len
),
194 reinterpret_cast<uint8
*>(&server_data_len
))) {
195 LOG(ERROR
) << "Could not read server_data_len";
198 std::string
server_data(server_data_len
, '\0');
199 if (!ReadData(read_fd
.Get(), write_fd
.Get(), server_data_len
,
200 reinterpret_cast<uint8
*>(&server_data
[0]))) {
201 LOG(ERROR
) << "Could not read server_data (" << server_data_len
206 if (!ParseServerData(server_data
)) {
207 LOG(ERROR
) << "Could not parse server_data: " << server_data
;