1 | /*
|
---|
2 | * Copyright (c) 2013 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 | * Unix-specific functions for test execution via the fork() system call.
|
---|
32 | */
|
---|
33 |
|
---|
34 | /** We need _POSIX_SOURCE because of kill(). */
|
---|
35 | #define _POSIX_SOURCE
|
---|
36 | /** We need _BSD_SOURCE because of snprintf() when compiling under C89. */
|
---|
37 | #define _BSD_SOURCE
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <unistd.h>
|
---|
40 | #include <sys/types.h>
|
---|
41 | #include <signal.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <assert.h>
|
---|
44 | #include <sys/wait.h>
|
---|
45 | #include <stdio.h>
|
---|
46 | #include <string.h>
|
---|
47 | #include "../internal.h"
|
---|
48 |
|
---|
49 | /** Maximum size of stdout we are able to capture. */
|
---|
50 | #define OUTPUT_BUFFER_SIZE 8192
|
---|
51 |
|
---|
52 | /** Buffer for assertion and other error messages. */
|
---|
53 | static char error_message_buffer[OUTPUT_BUFFER_SIZE];
|
---|
54 |
|
---|
55 | /** Buffer for stdout from the test. */
|
---|
56 | static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
|
---|
57 |
|
---|
58 | /** Prepare for a new test.
|
---|
59 | *
|
---|
60 | * @param test Test that is about to be run.
|
---|
61 | */
|
---|
62 | static void before_test_start(pcut_item_t *test) {
|
---|
63 | pcut_report_test_start(test);
|
---|
64 |
|
---|
65 | memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE);
|
---|
66 | memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** PID of the forked process running the actual test. */
|
---|
70 | static pid_t child_pid;
|
---|
71 |
|
---|
72 | /** Signal handler that kills the child.
|
---|
73 | *
|
---|
74 | * @param sig Signal number.
|
---|
75 | */
|
---|
76 | static void kill_child_on_alarm(int sig) {
|
---|
77 | PCUT_UNUSED(sig);
|
---|
78 | kill(child_pid, SIGKILL);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** Read full buffer from given file descriptor.
|
---|
82 | *
|
---|
83 | * This function exists to overcome the possibility that read() may
|
---|
84 | * not fill the full length of the provided buffer even when EOF is
|
---|
85 | * not reached.
|
---|
86 | *
|
---|
87 | * @param fd Opened file descriptor.
|
---|
88 | * @param buffer Buffer to store data into.
|
---|
89 | * @param buffer_size Size of the @p buffer in bytes.
|
---|
90 | * @return Number of actually read bytes.
|
---|
91 | */
|
---|
92 | static size_t read_all(int fd, char *buffer, size_t buffer_size) {
|
---|
93 | ssize_t actually_read;
|
---|
94 | char *buffer_start = buffer;
|
---|
95 | do {
|
---|
96 | actually_read = read(fd, buffer, buffer_size);
|
---|
97 | if (actually_read > 0) {
|
---|
98 | buffer += actually_read;
|
---|
99 | buffer_size -= actually_read;
|
---|
100 | if (buffer_size == 0) {
|
---|
101 | break;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | } while (actually_read > 0);
|
---|
105 | if (buffer_start != buffer) {
|
---|
106 | if (*(buffer - 1) == 10) {
|
---|
107 | *(buffer - 1) = 0;
|
---|
108 | buffer--;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | return buffer - buffer_start;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /** Convert program exit code to test outcome.
|
---|
115 | *
|
---|
116 | * @param status Status value from the wait() function.
|
---|
117 | * @return Test outcome code.
|
---|
118 | */
|
---|
119 | static int convert_wait_status_to_outcome(int status) {
|
---|
120 | if (WIFEXITED(status)) {
|
---|
121 | if (WEXITSTATUS(status) != 0) {
|
---|
122 | return TEST_OUTCOME_FAIL;
|
---|
123 | } else {
|
---|
124 | return TEST_OUTCOME_PASS;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (WIFSIGNALED(status)) {
|
---|
129 | return TEST_OUTCOME_ERROR;
|
---|
130 | }
|
---|
131 |
|
---|
132 | return status;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** Run the test in a forked environment and report the result.
|
---|
136 | *
|
---|
137 | * @param self_path Ignored.
|
---|
138 | * @param test Test to be run.
|
---|
139 | */
|
---|
140 | void pcut_run_test_forking(const char *self_path, pcut_item_t *test) {
|
---|
141 | int link_stdout[2], link_stderr[2];
|
---|
142 | int rc, status;
|
---|
143 | size_t stderr_size;
|
---|
144 |
|
---|
145 | PCUT_UNUSED(self_path);
|
---|
146 |
|
---|
147 | before_test_start(test);
|
---|
148 |
|
---|
149 |
|
---|
150 | rc = pipe(link_stdout);
|
---|
151 | if (rc == -1) {
|
---|
152 | snprintf(error_message_buffer, OUTPUT_BUFFER_SIZE - 1,
|
---|
153 | "pipe() failed: %s.", strerror(rc));
|
---|
154 | pcut_report_test_done(test, TEST_OUTCOME_ERROR, error_message_buffer, NULL, NULL);
|
---|
155 | return;
|
---|
156 | }
|
---|
157 | rc = pipe(link_stderr);
|
---|
158 | if (rc == -1) {
|
---|
159 | snprintf(error_message_buffer, OUTPUT_BUFFER_SIZE - 1,
|
---|
160 | "pipe() failed: %s.", strerror(rc));
|
---|
161 | pcut_report_test_done(test, TEST_OUTCOME_ERROR, error_message_buffer, NULL, NULL);
|
---|
162 | return;
|
---|
163 | }
|
---|
164 |
|
---|
165 | child_pid = fork();
|
---|
166 | if (child_pid == (pid_t)-1) {
|
---|
167 | snprintf(error_message_buffer, OUTPUT_BUFFER_SIZE - 1,
|
---|
168 | "fork() failed: %s.", strerror(rc));
|
---|
169 | rc = TEST_OUTCOME_ERROR;
|
---|
170 | goto leave_close_pipes;
|
---|
171 | }
|
---|
172 |
|
---|
173 | if (child_pid == 0) {
|
---|
174 | /* We are the child. */
|
---|
175 | dup2(link_stdout[1], STDOUT_FILENO);
|
---|
176 | close(link_stdout[0]);
|
---|
177 | dup2(link_stderr[1], STDERR_FILENO);
|
---|
178 | close(link_stderr[0]);
|
---|
179 |
|
---|
180 | rc = pcut_run_test_forked(test);
|
---|
181 |
|
---|
182 | exit(rc);
|
---|
183 | }
|
---|
184 |
|
---|
185 | close(link_stdout[1]);
|
---|
186 | close(link_stderr[1]);
|
---|
187 |
|
---|
188 | signal(SIGALRM, kill_child_on_alarm);
|
---|
189 | alarm(pcut_get_test_timeout(test));
|
---|
190 |
|
---|
191 | stderr_size = read_all(link_stderr[0], extra_output_buffer, OUTPUT_BUFFER_SIZE - 1);
|
---|
192 | read_all(link_stdout[0], extra_output_buffer, OUTPUT_BUFFER_SIZE - 1 - stderr_size);
|
---|
193 |
|
---|
194 | wait(&status);
|
---|
195 | alarm(0);
|
---|
196 |
|
---|
197 | rc = convert_wait_status_to_outcome(status);
|
---|
198 |
|
---|
199 | goto leave_close_parent_pipe;
|
---|
200 |
|
---|
201 | leave_close_pipes:
|
---|
202 | close(link_stdout[1]);
|
---|
203 | close(link_stderr[1]);
|
---|
204 | leave_close_parent_pipe:
|
---|
205 | close(link_stdout[0]);
|
---|
206 | close(link_stderr[0]);
|
---|
207 |
|
---|
208 | pcut_report_test_done_unparsed(test, rc, extra_output_buffer, OUTPUT_BUFFER_SIZE);
|
---|
209 | }
|
---|
210 |
|
---|
211 | void pcut_hook_before_test(pcut_item_t *test) {
|
---|
212 | PCUT_UNUSED(test);
|
---|
213 |
|
---|
214 | /* Do nothing. */
|
---|
215 | }
|
---|
216 |
|
---|