OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/test/test_server.h" | 5 #include "net/test/test_server.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <wincrypt.h> | 8 #include <wincrypt.h> |
9 | 9 |
10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 static const char kUnblock[] = "UNBLOCK"; | 79 static const char kUnblock[] = "UNBLOCK"; |
80 // Unblock the ReadFile in TestServer::WaitToStart by writing to the pipe. | 80 // Unblock the ReadFile in TestServer::WaitToStart by writing to the pipe. |
81 // Make sure the call succeeded, otherwise we are very likely to hang. | 81 // Make sure the call succeeded, otherwise we are very likely to hang. |
82 DWORD bytes_written = 0; | 82 DWORD bytes_written = 0; |
83 CHECK(WriteFile(handle, kUnblock, arraysize(kUnblock), &bytes_written, | 83 CHECK(WriteFile(handle, kUnblock, arraysize(kUnblock), &bytes_written, |
84 NULL)); | 84 NULL)); |
85 CHECK_EQ(arraysize(kUnblock), bytes_written); | 85 CHECK_EQ(arraysize(kUnblock), bytes_written); |
86 *unblocked = true; | 86 *unblocked = true; |
87 } | 87 } |
88 | 88 |
| 89 // Given a file handle, reads into |buffer| until |bytes_max| bytes |
| 90 // has been read or an error has been encountered (which includes |
| 91 // |unblocked| being set to true; see UnblockPipe() above). Returns |
| 92 // true if the read was successful. |
| 93 bool ReadData(bool* unblocked, HANDLE fd, DWORD bytes_max, uint8* buffer) { |
| 94 DWORD bytes_read = 0; |
| 95 while (bytes_read < bytes_max) { |
| 96 DWORD num_bytes; |
| 97 if (!ReadFile(fd, buffer + bytes_read, bytes_max - bytes_read, |
| 98 &num_bytes, NULL)) |
| 99 return false; |
| 100 if (num_bytes <= 0) |
| 101 return false; |
| 102 if (*unblocked) |
| 103 return false; |
| 104 bytes_read += num_bytes; |
| 105 } |
| 106 return true; |
| 107 } |
| 108 |
89 } // namespace | 109 } // namespace |
90 | 110 |
91 namespace net { | 111 namespace net { |
92 | 112 |
93 bool TestServer::LaunchPython(const FilePath& testserver_path) { | 113 bool TestServer::LaunchPython(const FilePath& testserver_path) { |
94 FilePath python_exe; | 114 FilePath python_exe; |
95 if (!PathService::Get(base::DIR_SOURCE_ROOT, &python_exe)) | 115 if (!PathService::Get(base::DIR_SOURCE_ROOT, &python_exe)) |
96 return false; | 116 return false; |
97 python_exe = python_exe | 117 python_exe = python_exe |
98 .Append(FILE_PATH_LITERAL("third_party")) | 118 .Append(FILE_PATH_LITERAL("third_party")) |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 &process_handle_, | 159 &process_handle_, |
140 &job_handle_)) { | 160 &job_handle_)) { |
141 LOG(ERROR) << "Failed to launch " << python_command.command_line_string(); | 161 LOG(ERROR) << "Failed to launch " << python_command.command_line_string(); |
142 return false; | 162 return false; |
143 } | 163 } |
144 | 164 |
145 return true; | 165 return true; |
146 } | 166 } |
147 | 167 |
148 bool TestServer::WaitToStart() { | 168 bool TestServer::WaitToStart() { |
| 169 ScopedHandle read_fd(child_read_fd_.Take()); |
| 170 ScopedHandle write_fd(child_write_fd_.Take()); |
| 171 |
149 base::Thread thread("test_server_watcher"); | 172 base::Thread thread("test_server_watcher"); |
150 if (!thread.Start()) | 173 if (!thread.Start()) |
151 return false; | 174 return false; |
152 | 175 |
153 // Prepare a timeout in case the server fails to start. | 176 // Prepare a timeout in case the server fails to start. |
154 bool unblocked = false; | 177 bool unblocked = false; |
155 thread.message_loop()->PostDelayedTask(FROM_HERE, | 178 thread.message_loop()->PostDelayedTask(FROM_HERE, |
156 NewRunnableFunction(UnblockPipe, child_write_fd_.Get(), &unblocked), | 179 NewRunnableFunction(UnblockPipe, write_fd.Get(), &unblocked), |
157 TestTimeouts::action_max_timeout_ms()); | 180 TestTimeouts::action_max_timeout_ms()); |
158 | 181 |
159 // Try to read two bytes from the pipe indicating the ephemeral port number. | 182 uint32 server_data_len = 0; |
160 uint16 port; | 183 if (!ReadData(&unblocked, read_fd.Get(), sizeof(server_data_len), |
161 uint8* buffer = reinterpret_cast<uint8*>(&port); | 184 reinterpret_cast<uint8*>(&server_data_len))) |
162 DWORD bytes_read = 0; | 185 return false; |
163 DWORD bytes_max = sizeof(port); | 186 std::string server_data(server_data_len, '\0'); |
164 while (bytes_read < bytes_max) { | 187 if (!ReadData(&unblocked, read_fd.Get(), server_data_len, |
165 DWORD num_bytes; | 188 reinterpret_cast<uint8*>(&server_data[0]))) |
166 if (!ReadFile(child_read_fd_, buffer + bytes_read, bytes_max - bytes_read, | |
167 &num_bytes, NULL)) | |
168 break; | |
169 if (num_bytes <= 0) | |
170 break; | |
171 bytes_read += num_bytes; | |
172 } | |
173 thread.Stop(); | |
174 child_read_fd_.Close(); | |
175 child_write_fd_.Close(); | |
176 | |
177 // If we hit the timeout, fail. | |
178 if (unblocked) | |
179 return false; | 189 return false; |
180 | 190 |
181 // If not enough bytes were read, fail. | 191 return ParseServerData(server_data); |
182 if (bytes_read < bytes_max) | |
183 return false; | |
184 | |
185 host_port_pair_.set_port(port); | |
186 return true; | |
187 } | 192 } |
188 | 193 |
189 bool TestServer::CheckCATrusted() { | 194 bool TestServer::CheckCATrusted() { |
190 HCERTSTORE cert_store = CertOpenSystemStore(NULL, L"ROOT"); | 195 HCERTSTORE cert_store = CertOpenSystemStore(NULL, L"ROOT"); |
191 if (!cert_store) { | 196 if (!cert_store) { |
192 LOG(ERROR) << " could not open trusted root CA store"; | 197 LOG(ERROR) << " could not open trusted root CA store"; |
193 return false; | 198 return false; |
194 } | 199 } |
195 PCCERT_CONTEXT cert = | 200 PCCERT_CONTEXT cert = |
196 CertFindCertificateInStore(cert_store, | 201 CertFindCertificateInStore(cert_store, |
(...skipping 11 matching lines...) Expand all Loading... |
208 "certificate to your trusted roots for this test to work. " | 213 "certificate to your trusted roots for this test to work. " |
209 "For more info visit:\n" | 214 "For more info visit:\n" |
210 "http://dev.chromium.org/developers/testing\n"; | 215 "http://dev.chromium.org/developers/testing\n"; |
211 return false; | 216 return false; |
212 } | 217 } |
213 | 218 |
214 return true; | 219 return true; |
215 } | 220 } |
216 | 221 |
217 } // namespace net | 222 } // namespace net |
OLD | NEW |