source: mainline/uspace/lib/pcut/src/os/windows.c@ f7aaffe0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f7aaffe0 was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 10.3 KB
RevLine 
[9b20126]1/*
2 * Copyright (c) 2014 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file
30 *
31 * Windows-specific functions for test execution via the popen() system call.
32 */
33
34/*
35 * Code inspired by Creating a Child Process with Redirected Input and Output:
36 * http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx
37 */
38
39#include "../internal.h"
40
41#include <windows.h>
42#include <tchar.h>
43#include <stdio.h>
44#include <strsafe.h>
45
46
47/** Maximum size of stdout we are able to capture. */
48#define OUTPUT_BUFFER_SIZE 8192
49
50/** Maximum command-line length. */
51#define PCUT_COMMAND_LINE_BUFFER_SIZE 256
52
53/** Buffer for assertion and other error messages. */
54static char error_message_buffer[OUTPUT_BUFFER_SIZE];
55
56/** Buffer for stdout from the test. */
57static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
58
59/** Prepare for a new test.
60 *
61 * @param test Test that is about to be run.
62 */
[1433ecda]63static void before_test_start(pcut_item_t *test)
64{
[9b20126]65 pcut_report_test_start(test);
66
67 memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE);
68 memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE);
69}
70
71/** Report that a certain function failed.
72 *
73 * @param test Current test.
74 * @param failed_function_name Name of the failed function.
75 */
[1433ecda]76static void report_func_fail(pcut_item_t *test, const char *failed_function_name)
77{
[9b20126]78 /* TODO: get error description. */
79 sprintf_s(error_message_buffer, OUTPUT_BUFFER_SIZE - 1,
[1433ecda]80 "%s failed: %s.", failed_function_name, "unknown reason");
[9b20126]81 pcut_report_test_done(test, TEST_OUTCOME_ERROR, error_message_buffer, NULL, NULL);
82}
83
84/** Read full buffer from given file descriptor.
85 *
86 * This function exists to overcome the possibility that read() may
87 * not fill the full length of the provided buffer even when EOF is
88 * not reached.
89 *
90 * @param fd Opened file descriptor.
91 * @param buffer Buffer to store data into.
92 * @param buffer_size Size of the @p buffer in bytes.
93 * @return Number of actually read bytes.
94 */
[1433ecda]95static size_t read_all(HANDLE fd, char *buffer, size_t buffer_size)
96{
[9b20126]97 DWORD actually_read;
98 char *buffer_start = buffer;
99 BOOL okay = FALSE;
100
101 do {
102 okay = ReadFile(fd, buffer, buffer_size, &actually_read, NULL);
103 if (!okay) {
104 break;
105 }
106 if (actually_read > 0) {
107 buffer += actually_read;
108 buffer_size -= actually_read;
109 if (buffer_size == 0) {
110 break;
111 }
112 }
113 } while (actually_read > 0);
114 if (buffer_start != buffer) {
115 if (*(buffer - 1) == 10) {
116 *(buffer - 1) = 0;
117 buffer--;
118 }
119 }
120 return buffer - buffer_start;
121}
122
123struct test_output_data {
124 HANDLE pipe_stdout;
125 HANDLE pipe_stderr;
126 char *output_buffer;
127 size_t output_buffer_size;
128};
129
[1433ecda]130static DWORD WINAPI read_test_output_on_background(LPVOID test_output_data_ptr)
131{
[9b20126]132 size_t stderr_size = 0;
133 struct test_output_data *test_output_data = (struct test_output_data *) test_output_data_ptr;
134
135 stderr_size = read_all(test_output_data->pipe_stderr,
[1433ecda]136 test_output_data->output_buffer,
137 test_output_data->output_buffer_size - 1);
[9b20126]138 read_all(test_output_data->pipe_stdout,
[1433ecda]139 test_output_data->output_buffer,
140 test_output_data->output_buffer_size - 1 - stderr_size);
[9b20126]141
142 return 0;
143}
144
145/** Run the test as a new process and report the result.
146 *
147 * @param self_path Path to itself, that is to current binary.
148 * @param test Test to be run.
149 */
[1433ecda]150int pcut_run_test_forking(const char *self_path, pcut_item_t *test)
151{
[9b20126]152 /* TODO: clean-up if something goes wrong "in the middle" */
153 BOOL okay = FALSE;
154 DWORD rc;
155 int outcome;
156 int timed_out;
157 int time_out_millis;
158 SECURITY_ATTRIBUTES security_attributes;
159 HANDLE link_stdout[2] = { NULL, NULL };
160 HANDLE link_stderr[2] = { NULL, NULL };
161 HANDLE link_stdin[2] = { NULL, NULL };
162 PROCESS_INFORMATION process_info;
163 STARTUPINFO start_info;
164 char command[PCUT_COMMAND_LINE_BUFFER_SIZE];
165 struct test_output_data test_output_data;
166 HANDLE test_output_thread_reader;
167
168
169 before_test_start(test);
170
171 /* Pipe handles are inherited. */
172 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
173 security_attributes.bInheritHandle = TRUE;
174 security_attributes.lpSecurityDescriptor = NULL;
175
176 /* Create pipe for stdout, make sure it is not inherited. */
177 okay = CreatePipe(&link_stdout[0], &link_stdout[1], &security_attributes, 0);
178 if (!okay) {
179 report_func_fail(test, "CreatePipe(/* stdout */)");
[9eb1ff5]180 return PCUT_OUTCOME_INTERNAL_ERROR;
[9b20126]181 }
182 okay = SetHandleInformation(link_stdout[0], HANDLE_FLAG_INHERIT, 0);
183 if (!okay) {
184 report_func_fail(test, "SetHandleInformation(/* stdout */)");
[9eb1ff5]185 return PCUT_OUTCOME_INTERNAL_ERROR;
[9b20126]186 }
187
188 /* Create pipe for stderr, make sure it is not inherited. */
189 okay = CreatePipe(&link_stderr[0], &link_stderr[1], &security_attributes, 0);
190 if (!okay) {
191 report_func_fail(test, "CreatePipe(/* stderr */)");
[9eb1ff5]192 return PCUT_OUTCOME_INTERNAL_ERROR;
[9b20126]193 }
194 okay = SetHandleInformation(link_stderr[0], HANDLE_FLAG_INHERIT, 0);
195 if (!okay) {
196 report_func_fail(test, "SetHandleInformation(/* stderr */)");
[9eb1ff5]197 return PCUT_OUTCOME_INTERNAL_ERROR;
[9b20126]198 }
199
200 /* Create pipe for stdin, make sure it is not inherited. */
201 okay = CreatePipe(&link_stdin[0], &link_stdin[1], &security_attributes, 0);
202 if (!okay) {
203 report_func_fail(test, "CreatePipe(/* stdin */)");
[9eb1ff5]204 return PCUT_OUTCOME_INTERNAL_ERROR;
[9b20126]205 }
206 okay = SetHandleInformation(link_stdin[1], HANDLE_FLAG_INHERIT, 0);
207 if (!okay) {
208 report_func_fail(test, "SetHandleInformation(/* stdin */)");
[9eb1ff5]209 return PCUT_OUTCOME_INTERNAL_ERROR;
[9b20126]210 }
211
212 /* Prepare information for the child process. */
213 ZeroMemory(&process_info, sizeof(PROCESS_INFORMATION));
214 ZeroMemory(&start_info, sizeof(STARTUPINFO));
215 start_info.cb = sizeof(STARTUPINFO);
216 start_info.hStdError = link_stderr[1];
217 start_info.hStdOutput = link_stdout[1];
218 start_info.hStdInput = link_stdin[0];
219 start_info.dwFlags |= STARTF_USESTDHANDLES;
220
221 /* Format the command line. */
222 sprintf_s(command, PCUT_COMMAND_LINE_BUFFER_SIZE - 1,
[1433ecda]223 "\"%s\" -t%d", self_path, test->id);
[9b20126]224
225 /* Run the process. */
226 okay = CreateProcess(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL,
[1433ecda]227 &start_info, &process_info);
[9b20126]228
229 if (!okay) {
230 report_func_fail(test, "CreateProcess()");
[9eb1ff5]231 return PCUT_OUTCOME_INTERNAL_ERROR;
[9b20126]232 }
233
234 // FIXME: kill the process on error
235
236 /* Close handles to the first thread. */
237 CloseHandle(process_info.hThread);
238
239 /* Close the other ends of the pipes. */
240 okay = CloseHandle(link_stdout[1]);
241 if (!okay) {
242 report_func_fail(test, "CloseHandle(/* stdout */)");
[9eb1ff5]243 return PCUT_OUTCOME_INTERNAL_ERROR;
[9b20126]244 }
245 okay = CloseHandle(link_stderr[1]);
246 if (!okay) {
247 report_func_fail(test, "CloseHandle(/* stderr */)");
[9eb1ff5]248 return PCUT_OUTCOME_INTERNAL_ERROR;
[9b20126]249 }
250 okay = CloseHandle(link_stdin[0]);
251 if (!okay) {
252 report_func_fail(test, "CloseHandle(/* stdin */)");
[9eb1ff5]253 return PCUT_OUTCOME_INTERNAL_ERROR;
[9b20126]254 }
255
256 /*
257 * Read data from stdout and stderr.
258 * We need to do this in a separate thread to allow the
259 * time-out to work correctly.
260 * Probably, this can be done with asynchronous I/O but
261 * this works for now pretty well.
262 */
263 test_output_data.pipe_stderr = link_stderr[0];
264 test_output_data.pipe_stdout = link_stdout[0];
265 test_output_data.output_buffer = extra_output_buffer;
266 test_output_data.output_buffer_size = OUTPUT_BUFFER_SIZE;
267
268 test_output_thread_reader = CreateThread(NULL, 0,
[1433ecda]269 read_test_output_on_background, &test_output_data,
270 0, NULL);
[9b20126]271
272 if (test_output_thread_reader == NULL) {
273 report_func_fail(test, "CreateThread(/* read test stdout */)");
[9eb1ff5]274 return PCUT_OUTCOME_INTERNAL_ERROR;
[9b20126]275 }
276
277 /* Wait for the process to terminate. */
278 timed_out = 0;
279 time_out_millis = pcut_get_test_timeout(test) * 1000;
280 rc = WaitForSingleObject(process_info.hProcess, time_out_millis);
281 PCUT_DEBUG("Waiting for test %s (%dms) returned %d.", test->name, time_out_millis, rc);
282 if (rc == WAIT_TIMEOUT) {
283 /* We timed-out: kill the process and wait for its termination again. */
284 timed_out = 1;
285 okay = TerminateProcess(process_info.hProcess, 5);
286 if (!okay) {
287 report_func_fail(test, "TerminateProcess(/* PROCESS_INFORMATION.hProcess */)");
[9eb1ff5]288 return PCUT_ERROR_INTERNAL_FAILURE;
[9b20126]289 }
290 rc = WaitForSingleObject(process_info.hProcess, INFINITE);
291 }
292 if (rc != WAIT_OBJECT_0) {
293 report_func_fail(test, "WaitForSingleObject(/* PROCESS_INFORMATION.hProcess */)");
[9eb1ff5]294 return PCUT_OUTCOME_INTERNAL_ERROR;
[9b20126]295 }
296
297 /* Get the return code and convert it to outcome. */
298 okay = GetExitCodeProcess(process_info.hProcess, &rc);
299 if (!okay) {
300 report_func_fail(test, "GetExitCodeProcess()");
[9eb1ff5]301 return PCUT_OUTCOME_INTERNAL_ERROR;
[9b20126]302 }
303
304 if (rc == 0) {
[9eb1ff5]305 outcome = PCUT_OUTCOME_PASS;
[9b20126]306 } else if ((rc > 0) && (rc < 10) && !timed_out) {
[9eb1ff5]307 outcome = PCUT_OUTCOME_FAIL;
[9b20126]308 } else {
[9eb1ff5]309 outcome = PCUT_OUTCOME_INTERNAL_ERROR;
[9b20126]310 }
311
312 /* Wait for the reader thread (shall be terminated by now). */
313 rc = WaitForSingleObject(test_output_thread_reader, INFINITE);
314 if (rc != WAIT_OBJECT_0) {
315 report_func_fail(test, "WaitForSingleObject(/* stdout reader thread */)");
[9eb1ff5]316 return PCUT_ERROR_INTERNAL_FAILURE;
[9b20126]317 }
318
319 pcut_report_test_done_unparsed(test, outcome, extra_output_buffer, OUTPUT_BUFFER_SIZE);
[9eb1ff5]320
321 return outcome;
[9b20126]322}
323
[1433ecda]324void pcut_hook_before_test(pcut_item_t *test)
325{
[9b20126]326 PCUT_UNUSED(test);
327
328 /*
329 * Prevent displaying the dialog informing the user that the
330 * program unexpectedly failed.
331 */
332 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
333}
Note: See TracBrowser for help on using the repository browser.