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 | #pragma warning(push, 0)
|
---|
42 | #include <windows.h>
|
---|
43 | #include <tchar.h>
|
---|
44 | #include <stdio.h>
|
---|
45 | #include <strsafe.h>
|
---|
46 | #pragma warning(pop)
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 | /** Maximum size of stdout we are able to capture. */
|
---|
51 | #define OUTPUT_BUFFER_SIZE 8192
|
---|
52 |
|
---|
53 | /** Maximum command-line length. */
|
---|
54 | #define PCUT_COMMAND_LINE_BUFFER_SIZE 256
|
---|
55 |
|
---|
56 | /** Buffer for assertion and other error messages. */
|
---|
57 | static char error_message_buffer[OUTPUT_BUFFER_SIZE];
|
---|
58 |
|
---|
59 | /** Buffer for stdout from the test. */
|
---|
60 | static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
|
---|
61 |
|
---|
62 | /** Prepare for a new test.
|
---|
63 | *
|
---|
64 | * @param test Test that is about to be run.
|
---|
65 | */
|
---|
66 | static void before_test_start(pcut_item_t *test) {
|
---|
67 | pcut_report_test_start(test);
|
---|
68 |
|
---|
69 | memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE);
|
---|
70 | memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /** Report that a certain function failed.
|
---|
74 | *
|
---|
75 | * @param test Current test.
|
---|
76 | * @param failed_function_name Name of the failed function.
|
---|
77 | */
|
---|
78 | static void report_func_fail(pcut_item_t *test, const char *failed_function_name) {
|
---|
79 | /* TODO: get error description. */
|
---|
80 | pcut_snprintf(error_message_buffer, OUTPUT_BUFFER_SIZE - 1,
|
---|
81 | "%s failed: %s.", failed_function_name, "unknown reason");
|
---|
82 | pcut_report_test_done(test, PCUT_OUTCOME_INTERNAL_ERROR, error_message_buffer, NULL, NULL);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** Read full buffer from given file descriptor.
|
---|
86 | *
|
---|
87 | * This function exists to overcome the possibility that read() may
|
---|
88 | * not fill the full length of the provided buffer even when EOF is
|
---|
89 | * not reached.
|
---|
90 | *
|
---|
91 | * @param fd Opened file descriptor.
|
---|
92 | * @param buffer Buffer to store data into.
|
---|
93 | * @param buffer_size Size of the @p buffer in bytes.
|
---|
94 | * @return Number of actually read bytes.
|
---|
95 | */
|
---|
96 | static size_t read_all(HANDLE fd, char *buffer, size_t buffer_size) {
|
---|
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 |
|
---|
123 | struct test_output_data {
|
---|
124 | HANDLE pipe_stdout;
|
---|
125 | HANDLE pipe_stderr;
|
---|
126 | char *output_buffer;
|
---|
127 | size_t output_buffer_size;
|
---|
128 | };
|
---|
129 |
|
---|
130 | static DWORD WINAPI read_test_output_on_background(LPVOID test_output_data_ptr) {
|
---|
131 | size_t stderr_size = 0;
|
---|
132 | struct test_output_data *test_output_data = (struct test_output_data *) test_output_data_ptr;
|
---|
133 |
|
---|
134 | stderr_size = read_all(test_output_data->pipe_stderr,
|
---|
135 | test_output_data->output_buffer,
|
---|
136 | test_output_data->output_buffer_size - 1);
|
---|
137 | read_all(test_output_data->pipe_stdout,
|
---|
138 | test_output_data->output_buffer,
|
---|
139 | test_output_data->output_buffer_size - 1 - stderr_size);
|
---|
140 |
|
---|
141 | return 0;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /** Run the test as a new process and report the result.
|
---|
145 | *
|
---|
146 | * @param self_path Path to itself, that is to current binary.
|
---|
147 | * @param test Test to be run.
|
---|
148 | */
|
---|
149 | int pcut_run_test_forking(const char *self_path, pcut_item_t *test) {
|
---|
150 | /* TODO: clean-up if something goes wrong "in the middle" */
|
---|
151 | BOOL okay = FALSE;
|
---|
152 | DWORD rc;
|
---|
153 | int outcome;
|
---|
154 | int timed_out;
|
---|
155 | int time_out_millis;
|
---|
156 | SECURITY_ATTRIBUTES security_attributes;
|
---|
157 | HANDLE link_stdout[2] = { NULL, NULL };
|
---|
158 | HANDLE link_stderr[2] = { NULL, NULL };
|
---|
159 | HANDLE link_stdin[2] = { NULL, NULL };
|
---|
160 | PROCESS_INFORMATION process_info;
|
---|
161 | STARTUPINFO start_info;
|
---|
162 | char command[PCUT_COMMAND_LINE_BUFFER_SIZE];
|
---|
163 | struct test_output_data test_output_data;
|
---|
164 | HANDLE test_output_thread_reader;
|
---|
165 |
|
---|
166 |
|
---|
167 | before_test_start(test);
|
---|
168 |
|
---|
169 | /* Pipe handles are inherited. */
|
---|
170 | security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
|
---|
171 | security_attributes.bInheritHandle = TRUE;
|
---|
172 | security_attributes.lpSecurityDescriptor = NULL;
|
---|
173 |
|
---|
174 | /* Create pipe for stdout, make sure it is not inherited. */
|
---|
175 | okay = CreatePipe(&link_stdout[0], &link_stdout[1], &security_attributes, 0);
|
---|
176 | if (!okay) {
|
---|
177 | report_func_fail(test, "CreatePipe(/* stdout */)");
|
---|
178 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
179 | }
|
---|
180 | okay = SetHandleInformation(link_stdout[0], HANDLE_FLAG_INHERIT, 0);
|
---|
181 | if (!okay) {
|
---|
182 | report_func_fail(test, "SetHandleInformation(/* stdout */)");
|
---|
183 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* Create pipe for stderr, make sure it is not inherited. */
|
---|
187 | okay = CreatePipe(&link_stderr[0], &link_stderr[1], &security_attributes, 0);
|
---|
188 | if (!okay) {
|
---|
189 | report_func_fail(test, "CreatePipe(/* stderr */)");
|
---|
190 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
191 | }
|
---|
192 | okay = SetHandleInformation(link_stderr[0], HANDLE_FLAG_INHERIT, 0);
|
---|
193 | if (!okay) {
|
---|
194 | report_func_fail(test, "SetHandleInformation(/* stderr */)");
|
---|
195 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /* Create pipe for stdin, make sure it is not inherited. */
|
---|
199 | okay = CreatePipe(&link_stdin[0], &link_stdin[1], &security_attributes, 0);
|
---|
200 | if (!okay) {
|
---|
201 | report_func_fail(test, "CreatePipe(/* stdin */)");
|
---|
202 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
203 | }
|
---|
204 | okay = SetHandleInformation(link_stdin[1], HANDLE_FLAG_INHERIT, 0);
|
---|
205 | if (!okay) {
|
---|
206 | report_func_fail(test, "SetHandleInformation(/* stdin */)");
|
---|
207 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /* Prepare information for the child process. */
|
---|
211 | ZeroMemory(&process_info, sizeof(PROCESS_INFORMATION));
|
---|
212 | ZeroMemory(&start_info, sizeof(STARTUPINFO));
|
---|
213 | start_info.cb = sizeof(STARTUPINFO);
|
---|
214 | start_info.hStdError = link_stderr[1];
|
---|
215 | start_info.hStdOutput = link_stdout[1];
|
---|
216 | start_info.hStdInput = link_stdin[0];
|
---|
217 | start_info.dwFlags |= STARTF_USESTDHANDLES;
|
---|
218 |
|
---|
219 | /* Format the command line. */
|
---|
220 | pcut_snprintf(command, PCUT_COMMAND_LINE_BUFFER_SIZE - 1,
|
---|
221 | "\"%s\" -t%d", self_path, test->id);
|
---|
222 |
|
---|
223 | /* Run the process. */
|
---|
224 | okay = CreateProcess(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL,
|
---|
225 | &start_info, &process_info);
|
---|
226 |
|
---|
227 | if (!okay) {
|
---|
228 | report_func_fail(test, "CreateProcess()");
|
---|
229 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
230 | }
|
---|
231 |
|
---|
232 | // FIXME: kill the process on error
|
---|
233 |
|
---|
234 | /* Close handles to the first thread. */
|
---|
235 | CloseHandle(process_info.hThread);
|
---|
236 |
|
---|
237 | /* Close the other ends of the pipes. */
|
---|
238 | okay = CloseHandle(link_stdout[1]);
|
---|
239 | if (!okay) {
|
---|
240 | report_func_fail(test, "CloseHandle(/* stdout */)");
|
---|
241 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
242 | }
|
---|
243 | okay = CloseHandle(link_stderr[1]);
|
---|
244 | if (!okay) {
|
---|
245 | report_func_fail(test, "CloseHandle(/* stderr */)");
|
---|
246 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
247 | }
|
---|
248 | okay = CloseHandle(link_stdin[0]);
|
---|
249 | if (!okay) {
|
---|
250 | report_func_fail(test, "CloseHandle(/* stdin */)");
|
---|
251 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
252 | }
|
---|
253 |
|
---|
254 | /*
|
---|
255 | * Read data from stdout and stderr.
|
---|
256 | * We need to do this in a separate thread to allow the
|
---|
257 | * time-out to work correctly.
|
---|
258 | * Probably, this can be done with asynchronous I/O but
|
---|
259 | * this works for now pretty well.
|
---|
260 | */
|
---|
261 | test_output_data.pipe_stderr = link_stderr[0];
|
---|
262 | test_output_data.pipe_stdout = link_stdout[0];
|
---|
263 | test_output_data.output_buffer = extra_output_buffer;
|
---|
264 | test_output_data.output_buffer_size = OUTPUT_BUFFER_SIZE;
|
---|
265 |
|
---|
266 | test_output_thread_reader = CreateThread(NULL, 0,
|
---|
267 | read_test_output_on_background, &test_output_data,
|
---|
268 | 0, NULL);
|
---|
269 |
|
---|
270 | if (test_output_thread_reader == NULL) {
|
---|
271 | report_func_fail(test, "CreateThread(/* read test stdout */)");
|
---|
272 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /* Wait for the process to terminate. */
|
---|
276 | timed_out = 0;
|
---|
277 | time_out_millis = pcut_get_test_timeout(test) * 1000;
|
---|
278 | rc = WaitForSingleObject(process_info.hProcess, time_out_millis);
|
---|
279 | PCUT_DEBUG("Waiting for test %s (%dms) returned %d.", test->name, time_out_millis, rc);
|
---|
280 | if (rc == WAIT_TIMEOUT) {
|
---|
281 | /* We timed-out: kill the process and wait for its termination again. */
|
---|
282 | timed_out = 1;
|
---|
283 | okay = TerminateProcess(process_info.hProcess, 5);
|
---|
284 | if (!okay) {
|
---|
285 | report_func_fail(test, "TerminateProcess(/* PROCESS_INFORMATION.hProcess */)");
|
---|
286 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
287 | }
|
---|
288 | rc = WaitForSingleObject(process_info.hProcess, INFINITE);
|
---|
289 | }
|
---|
290 | if (rc != WAIT_OBJECT_0) {
|
---|
291 | report_func_fail(test, "WaitForSingleObject(/* PROCESS_INFORMATION.hProcess */)");
|
---|
292 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /* Get the return code and convert it to outcome. */
|
---|
296 | okay = GetExitCodeProcess(process_info.hProcess, &rc);
|
---|
297 | if (!okay) {
|
---|
298 | report_func_fail(test, "GetExitCodeProcess()");
|
---|
299 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
300 | }
|
---|
301 |
|
---|
302 | if (rc == 0) {
|
---|
303 | outcome = PCUT_OUTCOME_PASS;
|
---|
304 | } else if ((rc > 0) && (rc < 10) && !timed_out) {
|
---|
305 | outcome = PCUT_OUTCOME_FAIL;
|
---|
306 | } else {
|
---|
307 | outcome = PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /* Wait for the reader thread (shall be terminated by now). */
|
---|
311 | rc = WaitForSingleObject(test_output_thread_reader, INFINITE);
|
---|
312 | if (rc != WAIT_OBJECT_0) {
|
---|
313 | report_func_fail(test, "WaitForSingleObject(/* stdout reader thread */)");
|
---|
314 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
315 | }
|
---|
316 |
|
---|
317 | pcut_report_test_done_unparsed(test, outcome, extra_output_buffer, OUTPUT_BUFFER_SIZE);
|
---|
318 |
|
---|
319 | return outcome;
|
---|
320 | }
|
---|
321 |
|
---|
322 | void pcut_hook_before_test(pcut_item_t *test) {
|
---|
323 | PCUT_UNUSED(test);
|
---|
324 |
|
---|
325 | /*
|
---|
326 | * Prevent displaying the dialog informing the user that the
|
---|
327 | * program unexpectedly failed.
|
---|
328 | */
|
---|
329 | SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
|
---|
330 | }
|
---|