| 1 |
/* |
| 2 |
* Check: a unit test framework for C |
| 3 |
* Copyright (C) 2001, 2002 Arien Malec |
| 4 |
* |
| 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. |
| 9 |
* |
| 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. |
| 14 |
* |
| 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, Boston, |
| 18 |
* MA 02110-1301, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "libcompat/libcompat.h" |
| 22 |
|
| 23 |
#include <sys/types.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <fcntl.h> |
| 26 |
#include <stdio.h> |
| 27 |
|
| 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" |
| 35 |
|
| 36 |
|
| 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. |
| 41 |
* |
| 42 |
* Pipe life cycle: |
| 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. |
| 52 |
* |
| 53 |
* This scheme may break down if the usage changes to asynchronous |
| 54 |
* reading and writing. |
| 55 |
*/ |
| 56 |
|
| 57 |
static FILE *send_file1; |
| 58 |
static char *send_file1_name; |
| 59 |
static FILE *send_file2; |
| 60 |
static char *send_file2_name; |
| 61 |
|
| 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, |
| 67 |
RcvMsg * rmsg); |
| 68 |
static FILE * |
| 69 |
get_pipe (void) |
| 70 |
{ |
| 71 |
if (send_file2 != 0) { |
| 72 |
return send_file2; |
| 73 |
} |
| 74 |
|
| 75 |
if (send_file1 != 0) { |
| 76 |
return send_file1; |
| 77 |
} |
| 78 |
|
| 79 |
eprintf ("No messaging setup", __FILE__, __LINE__); |
| 80 |
|
| 81 |
return NULL; |
| 82 |
} |
| 83 |
|
| 84 |
void |
| 85 |
send_failure_info (const char *msg) |
| 86 |
{ |
| 87 |
FailMsg fmsg; |
| 88 |
|
| 89 |
fmsg.msg = strdup (msg); |
| 90 |
ppack (get_pipe (), CK_MSG_FAIL, (CheckMsg *) & fmsg); |
| 91 |
free (fmsg.msg); |
| 92 |
} |
| 93 |
|
| 94 |
void |
| 95 |
send_duration_info (int duration) |
| 96 |
{ |
| 97 |
DurationMsg dmsg; |
| 98 |
|
| 99 |
dmsg.duration = duration; |
| 100 |
ppack (get_pipe (), CK_MSG_DURATION, (CheckMsg *) & dmsg); |
| 101 |
} |
| 102 |
|
| 103 |
void |
| 104 |
send_loc_info (const char *file, int line) |
| 105 |
{ |
| 106 |
LocMsg lmsg; |
| 107 |
|
| 108 |
lmsg.file = strdup (file); |
| 109 |
lmsg.line = line; |
| 110 |
ppack (get_pipe (), CK_MSG_LOC, (CheckMsg *) & lmsg); |
| 111 |
free (lmsg.file); |
| 112 |
} |
| 113 |
|
| 114 |
void |
| 115 |
send_ctx_info (enum ck_result_ctx ctx) |
| 116 |
{ |
| 117 |
CtxMsg cmsg; |
| 118 |
|
| 119 |
cmsg.ctx = ctx; |
| 120 |
ppack (get_pipe (), CK_MSG_CTX, (CheckMsg *) & cmsg); |
| 121 |
} |
| 122 |
|
| 123 |
TestResult * |
| 124 |
receive_test_result (int waserror) |
| 125 |
{ |
| 126 |
FILE *fp; |
| 127 |
RcvMsg *rmsg; |
| 128 |
TestResult *result; |
| 129 |
|
| 130 |
fp = get_pipe (); |
| 131 |
if (fp == NULL) { |
| 132 |
eprintf ("Error in call to get_pipe", __FILE__, __LINE__ - 2); |
| 133 |
} |
| 134 |
|
| 135 |
rewind (fp); |
| 136 |
rmsg = punpack (fp); |
| 137 |
|
| 138 |
if (rmsg == NULL) { |
| 139 |
eprintf ("Error in call to punpack", __FILE__, __LINE__ - 4); |
| 140 |
} |
| 141 |
|
| 142 |
teardown_pipe (); |
| 143 |
setup_pipe (); |
| 144 |
|
| 145 |
result = construct_test_result (rmsg, waserror); |
| 146 |
rcvmsg_free (rmsg); |
| 147 |
return result; |
| 148 |
} |
| 149 |
|
| 150 |
static void |
| 151 |
tr_set_loc_by_ctx (TestResult * tr, enum ck_result_ctx ctx, RcvMsg * rmsg) |
| 152 |
{ |
| 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; |
| 158 |
} else { |
| 159 |
tr->file = rmsg->fixture_file; |
| 160 |
tr->line = rmsg->fixture_line; |
| 161 |
rmsg->fixture_file = NULL; |
| 162 |
rmsg->fixture_line = -1; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
static TestResult * |
| 167 |
construct_test_result (RcvMsg * rmsg, int waserror) |
| 168 |
{ |
| 169 |
TestResult *tr; |
| 170 |
|
| 171 |
if (rmsg == NULL) |
| 172 |
return NULL; |
| 173 |
|
| 174 |
tr = tr_create (); |
| 175 |
|
| 176 |
if (rmsg->msg != NULL || waserror) { |
| 177 |
if (rmsg->failctx != CK_CTX_INVALID) { |
| 178 |
tr->ctx = rmsg->failctx; |
| 179 |
} else { |
| 180 |
tr->ctx = rmsg->lastctx; |
| 181 |
} |
| 182 |
|
| 183 |
tr->msg = rmsg->msg; |
| 184 |
rmsg->msg = NULL; |
| 185 |
tr_set_loc_by_ctx (tr, tr->ctx, rmsg); |
| 186 |
} else if (rmsg->lastctx == CK_CTX_SETUP) { |
| 187 |
tr->ctx = CK_CTX_SETUP; |
| 188 |
tr->msg = NULL; |
| 189 |
tr_set_loc_by_ctx (tr, CK_CTX_SETUP, rmsg); |
| 190 |
} else { |
| 191 |
tr->ctx = CK_CTX_TEST; |
| 192 |
tr->msg = NULL; |
| 193 |
tr->duration = rmsg->duration; |
| 194 |
tr_set_loc_by_ctx (tr, CK_CTX_TEST, rmsg); |
| 195 |
} |
| 196 |
|
| 197 |
return tr; |
| 198 |
} |
| 199 |
|
| 200 |
void |
| 201 |
setup_messaging (void) |
| 202 |
{ |
| 203 |
setup_pipe (); |
| 204 |
} |
| 205 |
|
| 206 |
void |
| 207 |
teardown_messaging (void) |
| 208 |
{ |
| 209 |
teardown_pipe (); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Open a temporary file. |
| 214 |
* |
| 215 |
* If the file could be unlinked upon creation, the name |
| 216 |
* of the file is not returned via 'name'. However, if the |
| 217 |
* file could not be unlinked, the name is returned, |
| 218 |
* expecting the caller to both delete the file and |
| 219 |
* free the 'name' field after the file is closed. |
| 220 |
*/ |
| 221 |
FILE * |
| 222 |
open_tmp_file (char **name) |
| 223 |
{ |
| 224 |
FILE *file = NULL; |
| 225 |
|
| 226 |
*name = NULL; |
| 227 |
|
| 228 |
#if !HAVE_MKSTEMP |
| 229 |
/* Windows does not like tmpfile(). This is likely because tmpfile() |
| 230 |
* call unlink() on the file before returning it, to make sure the |
| 231 |
* file is deleted when it is closed. The unlink() call also fails |
| 232 |
* on Windows if the file is still open. */ |
| 233 |
/* also note that mkstemp is apparently a C90 replacement for tmpfile */ |
| 234 |
/* perhaps all we need to do on Windows is set TMPDIR to whatever is |
| 235 |
stored in TEMP for tmpfile to work */ |
| 236 |
/* and finally, the "b" from "w+b" is ignored on OS X, not sure about WIN32 */ |
| 237 |
|
| 238 |
file = tmpfile (); |
| 239 |
if (file == NULL) { |
| 240 |
char *tmp = getenv ("TEMP"); |
| 241 |
char *tmp_file = tempnam (tmp, "check_"); |
| 242 |
|
| 243 |
/* |
| 244 |
* Note, tempnam is not enough to get a unique name. Between |
| 245 |
* getting the name and opening the file, something else also |
| 246 |
* calling tempnam() could get the same name. It has been observed |
| 247 |
* on MinGW-w64 builds on Wine that this exact thing happens |
| 248 |
* if multiple instances of a unit tests are running concurrently. |
| 249 |
* To prevent two concurrent unit tests from getting the same file, |
| 250 |
* we append the pid to the file. The pid should be unique on the |
| 251 |
* system. |
| 252 |
*/ |
| 253 |
char *uniq_tmp_file = ck_strdup_printf ("%s.%d", tmp_file, getpid ()); |
| 254 |
|
| 255 |
file = fopen (uniq_tmp_file, "w+b"); |
| 256 |
*name = uniq_tmp_file; |
| 257 |
free (tmp_file); |
| 258 |
} |
| 259 |
#else |
| 260 |
int fd = -1; |
| 261 |
const char *tmp_dir = getenv ("TEMP"); |
| 262 |
if (!tmp_dir) { |
| 263 |
tmp_dir = "."; |
| 264 |
} |
| 265 |
|
| 266 |
*name = ck_strdup_printf ("%s/check_XXXXXX", tmp_dir); |
| 267 |
|
| 268 |
if (-1 < (fd = mkstemp (*name))) { |
| 269 |
file = fdopen (fd, "w+b"); |
| 270 |
if (0 == unlink (*name) || NULL == file) { |
| 271 |
free (*name); |
| 272 |
*name = NULL; |
| 273 |
} |
| 274 |
} |
| 275 |
#endif |
| 276 |
return file; |
| 277 |
} |
| 278 |
|
| 279 |
static void |
| 280 |
setup_pipe (void) |
| 281 |
{ |
| 282 |
if (send_file1 == NULL) { |
| 283 |
send_file1 = open_tmp_file (&send_file1_name); |
| 284 |
return; |
| 285 |
} |
| 286 |
if (send_file2 == NULL) { |
| 287 |
send_file2 = open_tmp_file (&send_file2_name); |
| 288 |
return; |
| 289 |
} |
| 290 |
eprintf ("Only one nesting of suite runs supported", __FILE__, __LINE__); |
| 291 |
} |
| 292 |
|
| 293 |
static void |
| 294 |
teardown_pipe (void) |
| 295 |
{ |
| 296 |
if (send_file2 != 0) { |
| 297 |
fclose (send_file2); |
| 298 |
send_file2 = 0; |
| 299 |
if (send_file2_name != NULL) { |
| 300 |
unlink (send_file2_name); |
| 301 |
free (send_file2_name); |
| 302 |
send_file2_name = NULL; |
| 303 |
} |
| 304 |
} else if (send_file1 != 0) { |
| 305 |
fclose (send_file1); |
| 306 |
send_file1 = 0; |
| 307 |
if (send_file1_name != NULL) { |
| 308 |
unlink (send_file1_name); |
| 309 |
free (send_file1_name); |
| 310 |
send_file1_name = NULL; |
| 311 |
} |
| 312 |
} else { |
| 313 |
eprintf ("No messaging setup", __FILE__, __LINE__); |
| 314 |
} |
| 315 |
} |