| 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 <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
#include <stdio.h> |
| 26 |
|
| 27 |
#include "internal-check.h" |
| 28 |
#include "check_error.h" |
| 29 |
#include "check_list.h" |
| 30 |
#include "check_impl.h" |
| 31 |
#include "check_pack.h" |
| 32 |
|
| 33 |
#ifndef HAVE_PTHREAD |
| 34 |
#define pthread_mutex_lock(arg) |
| 35 |
#define pthread_mutex_unlock(arg) |
| 36 |
#define pthread_cleanup_push(f,a) { |
| 37 |
#define pthread_cleanup_pop(e) } |
| 38 |
#endif |
| 39 |
|
| 40 |
/* Maximum size for one message in the message stream. */ |
| 41 |
#define CK_MAX_MSG_SIZE 8192 |
| 42 |
/* This is used to implement a sliding window on the receiving |
| 43 |
* side. When sending messages, we assure that no single message |
| 44 |
* is bigger than this (actually we check against CK_MAX_MSG_SIZE/2). |
| 45 |
* The usual size for a message is less than 80 bytes. |
| 46 |
* All this is done instead of the previous approach to allocate (actually |
| 47 |
* continuously reallocate) one big chunk for the whole message stream. |
| 48 |
* Problems were seen in the wild with up to 4 GB reallocations. |
| 49 |
*/ |
| 50 |
|
| 51 |
|
| 52 |
/* typedef an unsigned int that has at least 4 bytes */ |
| 53 |
typedef uint32_t ck_uint32; |
| 54 |
|
| 55 |
|
| 56 |
static void pack_int (char **buf, int val); |
| 57 |
static int upack_int (char **buf); |
| 58 |
static void pack_str (char **buf, const char *str); |
| 59 |
static char *upack_str (char **buf); |
| 60 |
|
| 61 |
static int pack_ctx (char **buf, CtxMsg * cmsg); |
| 62 |
static int pack_loc (char **buf, LocMsg * lmsg); |
| 63 |
static int pack_fail (char **buf, FailMsg * fmsg); |
| 64 |
static int pack_duration (char **buf, DurationMsg * fmsg); |
| 65 |
static void upack_ctx (char **buf, CtxMsg * cmsg); |
| 66 |
static void upack_loc (char **buf, LocMsg * lmsg); |
| 67 |
static void upack_fail (char **buf, FailMsg * fmsg); |
| 68 |
static void upack_duration (char **buf, DurationMsg * fmsg); |
| 69 |
|
| 70 |
static void check_type (int type, const char *file, int line); |
| 71 |
static enum ck_msg_type upack_type (char **buf); |
| 72 |
static void pack_type (char **buf, enum ck_msg_type type); |
| 73 |
|
| 74 |
static int read_buf (FILE * fdes, int size, char *buf); |
| 75 |
static int get_result (char *buf, RcvMsg * rmsg); |
| 76 |
static void rcvmsg_update_ctx (RcvMsg * rmsg, enum ck_result_ctx ctx); |
| 77 |
static void rcvmsg_update_loc (RcvMsg * rmsg, const char *file, int line); |
| 78 |
static RcvMsg *rcvmsg_create (void); |
| 79 |
void rcvmsg_free (RcvMsg * rmsg); |
| 80 |
|
| 81 |
typedef int (*pfun) (char **, CheckMsg *); |
| 82 |
typedef void (*upfun) (char **, CheckMsg *); |
| 83 |
|
| 84 |
static pfun pftab[] = { |
| 85 |
(pfun) pack_ctx, |
| 86 |
(pfun) pack_fail, |
| 87 |
(pfun) pack_loc, |
| 88 |
(pfun) pack_duration |
| 89 |
}; |
| 90 |
|
| 91 |
static upfun upftab[] = { |
| 92 |
(upfun) upack_ctx, |
| 93 |
(upfun) upack_fail, |
| 94 |
(upfun) upack_loc, |
| 95 |
(upfun) upack_duration |
| 96 |
}; |
| 97 |
|
| 98 |
int |
| 99 |
pack (enum ck_msg_type type, char **buf, CheckMsg * msg) |
| 100 |
{ |
| 101 |
if (buf == NULL) |
| 102 |
return -1; |
| 103 |
if (msg == NULL) |
| 104 |
return 0; |
| 105 |
|
| 106 |
check_type (type, __FILE__, __LINE__); |
| 107 |
|
| 108 |
return pftab[type] (buf, msg); |
| 109 |
} |
| 110 |
|
| 111 |
int |
| 112 |
upack (char *buf, CheckMsg * msg, enum ck_msg_type *type) |
| 113 |
{ |
| 114 |
char *obuf; |
| 115 |
|
| 116 |
if (buf == NULL) |
| 117 |
return -1; |
| 118 |
|
| 119 |
obuf = buf; |
| 120 |
|
| 121 |
*type = upack_type (&buf); |
| 122 |
|
| 123 |
check_type (*type, __FILE__, __LINE__); |
| 124 |
|
| 125 |
upftab[*type] (&buf, msg); |
| 126 |
|
| 127 |
return buf - obuf; |
| 128 |
} |
| 129 |
|
| 130 |
static void |
| 131 |
pack_int (char **buf, int val) |
| 132 |
{ |
| 133 |
unsigned char *ubuf = (unsigned char *) *buf; |
| 134 |
ck_uint32 uval = val; |
| 135 |
|
| 136 |
ubuf[0] = (unsigned char) ((uval >> 24) & 0xFF); |
| 137 |
ubuf[1] = (unsigned char) ((uval >> 16) & 0xFF); |
| 138 |
ubuf[2] = (unsigned char) ((uval >> 8) & 0xFF); |
| 139 |
ubuf[3] = (unsigned char) (uval & 0xFF); |
| 140 |
|
| 141 |
*buf += 4; |
| 142 |
} |
| 143 |
|
| 144 |
static int |
| 145 |
upack_int (char **buf) |
| 146 |
{ |
| 147 |
unsigned char *ubuf = (unsigned char *) *buf; |
| 148 |
ck_uint32 uval; |
| 149 |
|
| 150 |
uval = |
| 151 |
(ck_uint32) ((ubuf[0] << 24) | (ubuf[1] << 16) | (ubuf[2] << 8) | |
| 152 |
ubuf[3]); |
| 153 |
|
| 154 |
*buf += 4; |
| 155 |
|
| 156 |
return (int) uval; |
| 157 |
} |
| 158 |
|
| 159 |
static void |
| 160 |
pack_str (char **buf, const char *val) |
| 161 |
{ |
| 162 |
int strsz; |
| 163 |
|
| 164 |
if (val == NULL) |
| 165 |
strsz = 0; |
| 166 |
else |
| 167 |
strsz = strlen (val); |
| 168 |
|
| 169 |
pack_int (buf, strsz); |
| 170 |
|
| 171 |
if (strsz > 0) { |
| 172 |
memcpy (*buf, val, strsz); |
| 173 |
*buf += strsz; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
static char * |
| 178 |
upack_str (char **buf) |
| 179 |
{ |
| 180 |
char *val; |
| 181 |
int strsz; |
| 182 |
|
| 183 |
strsz = upack_int (buf); |
| 184 |
|
| 185 |
if (strsz > 0) { |
| 186 |
val = (char *) emalloc (strsz + 1); |
| 187 |
memcpy (val, *buf, strsz); |
| 188 |
val[strsz] = 0; |
| 189 |
*buf += strsz; |
| 190 |
} else { |
| 191 |
val = (char *) emalloc (1); |
| 192 |
*val = 0; |
| 193 |
} |
| 194 |
|
| 195 |
return val; |
| 196 |
} |
| 197 |
|
| 198 |
static void |
| 199 |
pack_type (char **buf, enum ck_msg_type type) |
| 200 |
{ |
| 201 |
pack_int (buf, (int) type); |
| 202 |
} |
| 203 |
|
| 204 |
static enum ck_msg_type |
| 205 |
upack_type (char **buf) |
| 206 |
{ |
| 207 |
return (enum ck_msg_type) upack_int (buf); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
static int |
| 212 |
pack_ctx (char **buf, CtxMsg * cmsg) |
| 213 |
{ |
| 214 |
char *ptr; |
| 215 |
int len; |
| 216 |
|
| 217 |
len = 4 + 4; |
| 218 |
*buf = ptr = (char *) emalloc (len); |
| 219 |
|
| 220 |
pack_type (&ptr, CK_MSG_CTX); |
| 221 |
pack_int (&ptr, (int) cmsg->ctx); |
| 222 |
|
| 223 |
return len; |
| 224 |
} |
| 225 |
|
| 226 |
static void |
| 227 |
upack_ctx (char **buf, CtxMsg * cmsg) |
| 228 |
{ |
| 229 |
cmsg->ctx = (enum ck_result_ctx) upack_int (buf); |
| 230 |
} |
| 231 |
|
| 232 |
static int |
| 233 |
pack_duration (char **buf, DurationMsg * cmsg) |
| 234 |
{ |
| 235 |
char *ptr; |
| 236 |
int len; |
| 237 |
|
| 238 |
len = 4 + 4; |
| 239 |
*buf = ptr = (char *) emalloc (len); |
| 240 |
|
| 241 |
pack_type (&ptr, CK_MSG_DURATION); |
| 242 |
pack_int (&ptr, cmsg->duration); |
| 243 |
|
| 244 |
return len; |
| 245 |
} |
| 246 |
|
| 247 |
static void |
| 248 |
upack_duration (char **buf, DurationMsg * cmsg) |
| 249 |
{ |
| 250 |
cmsg->duration = upack_int (buf); |
| 251 |
} |
| 252 |
|
| 253 |
static int |
| 254 |
pack_loc (char **buf, LocMsg * lmsg) |
| 255 |
{ |
| 256 |
char *ptr; |
| 257 |
int len; |
| 258 |
|
| 259 |
len = 4 + 4 + (lmsg->file ? strlen (lmsg->file) : 0) + 4; |
| 260 |
*buf = ptr = (char *) emalloc (len); |
| 261 |
|
| 262 |
pack_type (&ptr, CK_MSG_LOC); |
| 263 |
pack_str (&ptr, lmsg->file); |
| 264 |
pack_int (&ptr, lmsg->line); |
| 265 |
|
| 266 |
return len; |
| 267 |
} |
| 268 |
|
| 269 |
static void |
| 270 |
upack_loc (char **buf, LocMsg * lmsg) |
| 271 |
{ |
| 272 |
lmsg->file = upack_str (buf); |
| 273 |
lmsg->line = upack_int (buf); |
| 274 |
} |
| 275 |
|
| 276 |
static int |
| 277 |
pack_fail (char **buf, FailMsg * fmsg) |
| 278 |
{ |
| 279 |
char *ptr; |
| 280 |
int len; |
| 281 |
|
| 282 |
len = 4 + 4 + (fmsg->msg ? strlen (fmsg->msg) : 0); |
| 283 |
*buf = ptr = (char *) emalloc (len); |
| 284 |
|
| 285 |
pack_type (&ptr, CK_MSG_FAIL); |
| 286 |
pack_str (&ptr, fmsg->msg); |
| 287 |
|
| 288 |
return len; |
| 289 |
} |
| 290 |
|
| 291 |
static void |
| 292 |
upack_fail (char **buf, FailMsg * fmsg) |
| 293 |
{ |
| 294 |
fmsg->msg = upack_str (buf); |
| 295 |
} |
| 296 |
|
| 297 |
static void |
| 298 |
check_type (int type, const char *file, int line) |
| 299 |
{ |
| 300 |
if (type < 0 || type >= CK_MSG_LAST) |
| 301 |
eprintf ("Bad message type arg %d", file, line, type); |
| 302 |
} |
| 303 |
|
| 304 |
#ifdef HAVE_PTHREAD |
| 305 |
static pthread_mutex_t ck_mutex_lock = PTHREAD_MUTEX_INITIALIZER; |
| 306 |
static void |
| 307 |
ppack_cleanup (void *mutex) |
| 308 |
{ |
| 309 |
pthread_mutex_unlock ((pthread_mutex_t *) mutex); |
| 310 |
} |
| 311 |
#endif |
| 312 |
|
| 313 |
void |
| 314 |
ppack (FILE * fdes, enum ck_msg_type type, CheckMsg * msg) |
| 315 |
{ |
| 316 |
char *buf = NULL; |
| 317 |
int n; |
| 318 |
ssize_t r; |
| 319 |
|
| 320 |
n = pack (type, &buf, msg); |
| 321 |
/* Keep it on the safe side to not send too much data. */ |
| 322 |
if (n > (CK_MAX_MSG_SIZE / 2)) |
| 323 |
eprintf ("Message string too long", __FILE__, __LINE__ - 2); |
| 324 |
|
| 325 |
pthread_cleanup_push (ppack_cleanup, &ck_mutex_lock); |
| 326 |
pthread_mutex_lock (&ck_mutex_lock); |
| 327 |
r = fwrite (buf, 1, n, fdes); |
| 328 |
fflush (fdes); |
| 329 |
pthread_mutex_unlock (&ck_mutex_lock); |
| 330 |
pthread_cleanup_pop (0); |
| 331 |
if (r != n) |
| 332 |
eprintf ("Error in call to fwrite:", __FILE__, __LINE__ - 2); |
| 333 |
|
| 334 |
free (buf); |
| 335 |
} |
| 336 |
|
| 337 |
static int |
| 338 |
read_buf (FILE * fdes, int size, char *buf) |
| 339 |
{ |
| 340 |
int n; |
| 341 |
|
| 342 |
n = fread (buf, 1, size, fdes); |
| 343 |
|
| 344 |
if (ferror (fdes)) { |
| 345 |
eprintf ("Error in call to fread:", __FILE__, __LINE__ - 4); |
| 346 |
} |
| 347 |
|
| 348 |
return n; |
| 349 |
} |
| 350 |
|
| 351 |
static int |
| 352 |
get_result (char *buf, RcvMsg * rmsg) |
| 353 |
{ |
| 354 |
enum ck_msg_type type; |
| 355 |
CheckMsg msg; |
| 356 |
int n; |
| 357 |
|
| 358 |
n = upack (buf, &msg, &type); |
| 359 |
if (n == -1) |
| 360 |
eprintf ("Error in call to upack", __FILE__, __LINE__ - 2); |
| 361 |
|
| 362 |
if (type == CK_MSG_CTX) { |
| 363 |
CtxMsg *cmsg = (CtxMsg *) & msg; |
| 364 |
|
| 365 |
rcvmsg_update_ctx (rmsg, cmsg->ctx); |
| 366 |
} else if (type == CK_MSG_LOC) { |
| 367 |
LocMsg *lmsg = (LocMsg *) & msg; |
| 368 |
|
| 369 |
if (rmsg->failctx == CK_CTX_INVALID) { |
| 370 |
rcvmsg_update_loc (rmsg, lmsg->file, lmsg->line); |
| 371 |
} |
| 372 |
free (lmsg->file); |
| 373 |
} else if (type == CK_MSG_FAIL) { |
| 374 |
FailMsg *fmsg = (FailMsg *) & msg; |
| 375 |
|
| 376 |
if (rmsg->msg == NULL) { |
| 377 |
rmsg->msg = strdup (fmsg->msg); |
| 378 |
rmsg->failctx = rmsg->lastctx; |
| 379 |
} else { |
| 380 |
/* Skip subsequent failure messages, only happens for CK_NOFORK */ |
| 381 |
} |
| 382 |
free (fmsg->msg); |
| 383 |
} else if (type == CK_MSG_DURATION) { |
| 384 |
DurationMsg *cmsg = (DurationMsg *) & msg; |
| 385 |
|
| 386 |
rmsg->duration = cmsg->duration; |
| 387 |
} else |
| 388 |
check_type (type, __FILE__, __LINE__); |
| 389 |
|
| 390 |
return n; |
| 391 |
} |
| 392 |
|
| 393 |
static void |
| 394 |
reset_rcv_test (RcvMsg * rmsg) |
| 395 |
{ |
| 396 |
rmsg->test_line = -1; |
| 397 |
rmsg->test_file = NULL; |
| 398 |
} |
| 399 |
|
| 400 |
static void |
| 401 |
reset_rcv_fixture (RcvMsg * rmsg) |
| 402 |
{ |
| 403 |
rmsg->fixture_line = -1; |
| 404 |
rmsg->fixture_file = NULL; |
| 405 |
} |
| 406 |
|
| 407 |
static RcvMsg * |
| 408 |
rcvmsg_create (void) |
| 409 |
{ |
| 410 |
RcvMsg *rmsg; |
| 411 |
|
| 412 |
rmsg = (RcvMsg *) emalloc (sizeof (RcvMsg)); |
| 413 |
rmsg->lastctx = CK_CTX_INVALID; |
| 414 |
rmsg->failctx = CK_CTX_INVALID; |
| 415 |
rmsg->msg = NULL; |
| 416 |
rmsg->duration = -1; |
| 417 |
reset_rcv_test (rmsg); |
| 418 |
reset_rcv_fixture (rmsg); |
| 419 |
return rmsg; |
| 420 |
} |
| 421 |
|
| 422 |
void |
| 423 |
rcvmsg_free (RcvMsg * rmsg) |
| 424 |
{ |
| 425 |
free (rmsg->fixture_file); |
| 426 |
free (rmsg->test_file); |
| 427 |
free (rmsg->msg); |
| 428 |
free (rmsg); |
| 429 |
} |
| 430 |
|
| 431 |
static void |
| 432 |
rcvmsg_update_ctx (RcvMsg * rmsg, enum ck_result_ctx ctx) |
| 433 |
{ |
| 434 |
if (rmsg->lastctx != CK_CTX_INVALID) { |
| 435 |
free (rmsg->fixture_file); |
| 436 |
reset_rcv_fixture (rmsg); |
| 437 |
} |
| 438 |
rmsg->lastctx = ctx; |
| 439 |
} |
| 440 |
|
| 441 |
static void |
| 442 |
rcvmsg_update_loc (RcvMsg * rmsg, const char *file, int line) |
| 443 |
{ |
| 444 |
if (rmsg->lastctx == CK_CTX_TEST) { |
| 445 |
free (rmsg->test_file); |
| 446 |
rmsg->test_line = line; |
| 447 |
rmsg->test_file = strdup (file); |
| 448 |
} else { |
| 449 |
free (rmsg->fixture_file); |
| 450 |
rmsg->fixture_line = line; |
| 451 |
rmsg->fixture_file = strdup (file); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
RcvMsg * |
| 456 |
punpack (FILE * fdes) |
| 457 |
{ |
| 458 |
int nread, nparse, n; |
| 459 |
char *buf; |
| 460 |
RcvMsg *rmsg; |
| 461 |
|
| 462 |
rmsg = rcvmsg_create (); |
| 463 |
|
| 464 |
/* Allocate a buffer */ |
| 465 |
buf = (char *) emalloc (CK_MAX_MSG_SIZE); |
| 466 |
/* Fill the buffer from the file */ |
| 467 |
nread = read_buf (fdes, CK_MAX_MSG_SIZE, buf); |
| 468 |
nparse = nread; |
| 469 |
/* While not all parsed */ |
| 470 |
while (nparse > 0) { |
| 471 |
/* Parse one message */ |
| 472 |
n = get_result (buf, rmsg); |
| 473 |
nparse -= n; |
| 474 |
if (nparse < 0) |
| 475 |
eprintf ("Error in call to get_result", __FILE__, __LINE__ - 3); |
| 476 |
/* Move remaining data in buffer to the beginning */ |
| 477 |
memmove (buf, buf + n, nparse); |
| 478 |
/* If EOF has not been seen */ |
| 479 |
if (nread > 0) { |
| 480 |
/* Read more data into empty space at end of the buffer */ |
| 481 |
nread = read_buf (fdes, n, buf + nparse); |
| 482 |
nparse += nread; |
| 483 |
} |
| 484 |
} |
| 485 |
free (buf); |
| 486 |
|
| 487 |
if (rmsg->lastctx == CK_CTX_INVALID) { |
| 488 |
free (rmsg); |
| 489 |
rmsg = NULL; |
| 490 |
} |
| 491 |
|
| 492 |
return rmsg; |
| 493 |
} |