2 * Check: a unit test framework for C
3 * Copyright (C) 2001 2002, Arien Malec
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #include "libcompat.h"
23 #include <sys/types.h>
28 #include "check_error.h"
29 #include "internal-check.h"
30 #include "check_list.h"
31 #include "check_impl.h"
32 #include "check_msg.h"
33 #include "check_pack.h"
34 #include "check_str.h"
37 /* 'Pipe' is implemented as a temporary file to overcome message
38 * volume limitations outlined in bug #482012. This scheme works well
39 * with the existing usage wherein the parent does not begin reading
40 * until the child has done writing and exited.
43 * - The parent creates a tmpfile().
44 * - The fork() call has the effect of duplicating the file descriptor
45 * and copying (on write) the FILE* data structures.
46 * - The child writes to the file, and its dup'ed file descriptor and
47 * data structures are cleaned up on child process exit.
48 * - Before reading, the parent rewind()'s the file to reset both
49 * FILE* and underlying file descriptor location data.
50 * - When finished, the parent fclose()'s the FILE*, deleting the
51 * temporary file, per tmpfile()'s semantics.
53 * This scheme may break down if the usage changes to asynchronous
54 * reading and writing.
57 static FILE *send_file1;
58 static char *send_file1_name;
59 static FILE *send_file2;
60 static char *send_file2_name;
62 static FILE *get_pipe (void);
63 static void setup_pipe (void);
64 static void teardown_pipe (void);
65 static TestResult *construct_test_result (RcvMsg * rmsg, int waserror);
66 static void tr_set_loc_by_ctx (TestResult * tr, enum ck_result_ctx ctx,
71 if (send_file2 != 0) {
75 if (send_file1 != 0) {
79 eprintf ("No messaging setup", __FILE__, __LINE__);
85 send_failure_info (const char *msg)
89 fmsg.msg = strdup (msg);
90 ppack (get_pipe (), CK_MSG_FAIL, (CheckMsg *) & fmsg);
95 send_duration_info (int duration)
99 dmsg.duration = duration;
100 ppack (get_pipe (), CK_MSG_DURATION, (CheckMsg *) & dmsg);
104 send_loc_info (const char *file, int line)
108 lmsg.file = strdup (file);
110 ppack (get_pipe (), CK_MSG_LOC, (CheckMsg *) & lmsg);
115 send_ctx_info (enum ck_result_ctx ctx)
120 ppack (get_pipe (), CK_MSG_CTX, (CheckMsg *) & cmsg);
124 receive_test_result (int waserror)
132 eprintf ("Error in call to get_pipe", __FILE__, __LINE__ - 2);
139 eprintf ("Error in call to punpack", __FILE__, __LINE__ - 4);
145 result = construct_test_result (rmsg, waserror);
151 tr_set_loc_by_ctx (TestResult * tr, enum ck_result_ctx ctx, RcvMsg * rmsg)
153 if (ctx == CK_CTX_TEST) {
154 tr->file = rmsg->test_file;
155 tr->line = rmsg->test_line;
156 rmsg->test_file = NULL;
157 rmsg->test_line = -1;
159 tr->file = rmsg->fixture_file;
160 tr->line = rmsg->fixture_line;
161 rmsg->fixture_file = NULL;
162 rmsg->fixture_line = -1;
167 construct_test_result (RcvMsg * rmsg, int waserror)
176 if (rmsg->msg != NULL || waserror) {
177 tr->ctx = rmsg->lastctx;
180 tr_set_loc_by_ctx (tr, tr->ctx, rmsg);
181 } else if (rmsg->lastctx == CK_CTX_SETUP) {
182 tr->ctx = CK_CTX_SETUP;
184 tr_set_loc_by_ctx (tr, CK_CTX_SETUP, rmsg);
186 tr->ctx = CK_CTX_TEST;
188 tr->duration = rmsg->duration;
189 tr_set_loc_by_ctx (tr, CK_CTX_TEST, rmsg);
196 setup_messaging (void)
202 teardown_messaging (void)
208 * Open a temporary file.
210 * If the file could be unlinked upon creation, the name
211 * of the file is not returned via 'name'. However, if the
212 * file could not be unlinked, the name is returned,
213 * expecting the caller to both delete the file and
214 * free the 'name' field after the file is closed.
217 open_tmp_file (char **name)
224 /* Windows does not like tmpfile(). This is likely because tmpfile()
225 * call unlink() on the file before returning it, to make sure the
226 * file is deleted when it is closed. The unlink() call also fails
227 * on Windows if the file is still open. */
228 /* also note that mkstemp is apparently a C90 replacement for tmpfile */
229 /* perhaps all we need to do on Windows is set TMPDIR to whatever is
230 stored in TEMP for tmpfile to work */
231 /* and finally, the "b" from "w+b" is ignored on OS X, not sure about WIN32 */
235 char *tmp = getenv ("TEMP");
236 char *tmp_file = tempnam (tmp, "check_");
239 * Note, tempnam is not enough to get a unique name. Between
240 * getting the name and opening the file, something else also
241 * calling tempnam() could get the same name. It has been observed
242 * on MinGW-w64 builds on Wine that this exact thing happens
243 * if multiple instances of a unit tests are running concurrently.
244 * To prevent two concurrent unit tests from getting the same file,
245 * we append the pid to the file. The pid should be unique on the
248 char *uniq_tmp_file = ck_strdup_printf ("%s.%d", tmp_file, getpid ());
250 file = fopen (uniq_tmp_file, "w+b");
251 *name = uniq_tmp_file;
256 const char *tmp_dir = getenv ("TEMP");
260 *name = ck_strdup_printf ("%s/check_XXXXXX", tmp_dir);
261 if (-1 < (fd = mkstemp (*name))) {
262 file = fdopen (fd, "w+b");
263 if (0 == unlink (*name) || NULL == file) {
275 if (send_file1 == NULL) {
276 send_file1 = open_tmp_file (&send_file1_name);
279 if (send_file2 == NULL) {
280 send_file2 = open_tmp_file (&send_file2_name);
283 eprintf ("Only one nesting of suite runs supported", __FILE__, __LINE__);
289 if (send_file2 != 0) {
292 if (send_file2_name != NULL) {
293 unlink (send_file2_name);
294 free (send_file2_name);
295 send_file2_name = NULL;
297 } else if (send_file1 != 0) {
300 if (send_file1_name != NULL) {
301 unlink (send_file1_name);
302 free (send_file1_name);
303 send_file1_name = NULL;
306 eprintf ("No messaging setup", __FILE__, __LINE__);