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 "../lib/libcompat.h"
28 #include "check_error.h"
29 #include "check_list.h"
30 #include "check_impl.h"
31 #include "check_pack.h"
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) }
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.
52 /* typedef an unsigned int that has at least 4 bytes */
53 typedef uint32_t ck_uint32;
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);
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);
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);
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);
81 typedef int (*pfun) (char **, CheckMsg *);
82 typedef void (*upfun) (char **, CheckMsg *);
84 static pfun pftab[] = {
91 static upfun upftab[] = {
95 (upfun) upack_duration
99 pack (enum ck_msg_type type, char **buf, CheckMsg * msg)
106 check_type (type, __FILE__, __LINE__);
108 return pftab[type] (buf, msg);
112 upack (char *buf, CheckMsg * msg, enum ck_msg_type *type)
121 *type = upack_type (&buf);
123 check_type (*type, __FILE__, __LINE__);
125 upftab[*type] (&buf, msg);
131 pack_int (char **buf, int val)
133 unsigned char *ubuf = (unsigned char *) *buf;
134 ck_uint32 uval = val;
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);
145 upack_int (char **buf)
147 unsigned char *ubuf = (unsigned char *) *buf;
151 (ck_uint32) ((ubuf[0] << 24) | (ubuf[1] << 16) | (ubuf[2] << 8) |
160 pack_str (char **buf, const char *val)
167 strsz = strlen (val);
169 pack_int (buf, strsz);
172 memcpy (*buf, val, strsz);
178 upack_str (char **buf)
183 strsz = upack_int (buf);
186 val = (char *) emalloc (strsz + 1);
187 memcpy (val, *buf, strsz);
191 val = (char *) emalloc (1);
199 pack_type (char **buf, enum ck_msg_type type)
201 pack_int (buf, (int) type);
204 static enum ck_msg_type
205 upack_type (char **buf)
207 return (enum ck_msg_type) upack_int (buf);
212 pack_ctx (char **buf, CtxMsg * cmsg)
218 *buf = ptr = (char *) emalloc (len);
220 pack_type (&ptr, CK_MSG_CTX);
221 pack_int (&ptr, (int) cmsg->ctx);
227 upack_ctx (char **buf, CtxMsg * cmsg)
229 cmsg->ctx = (enum ck_result_ctx) upack_int (buf);
233 pack_duration (char **buf, DurationMsg * cmsg)
239 *buf = ptr = (char *) emalloc (len);
241 pack_type (&ptr, CK_MSG_DURATION);
242 pack_int (&ptr, cmsg->duration);
248 upack_duration (char **buf, DurationMsg * cmsg)
250 cmsg->duration = upack_int (buf);
254 pack_loc (char **buf, LocMsg * lmsg)
259 len = 4 + 4 + (lmsg->file ? strlen (lmsg->file) : 0) + 4;
260 *buf = ptr = (char *) emalloc (len);
262 pack_type (&ptr, CK_MSG_LOC);
263 pack_str (&ptr, lmsg->file);
264 pack_int (&ptr, lmsg->line);
270 upack_loc (char **buf, LocMsg * lmsg)
272 lmsg->file = upack_str (buf);
273 lmsg->line = upack_int (buf);
277 pack_fail (char **buf, FailMsg * fmsg)
282 len = 4 + 4 + (fmsg->msg ? strlen (fmsg->msg) : 0);
283 *buf = ptr = (char *) emalloc (len);
285 pack_type (&ptr, CK_MSG_FAIL);
286 pack_str (&ptr, fmsg->msg);
292 upack_fail (char **buf, FailMsg * fmsg)
294 fmsg->msg = upack_str (buf);
298 check_type (int type, const char *file, int line)
300 if (type < 0 || type >= CK_MSG_LAST)
301 eprintf ("Bad message type arg %d", file, line, type);
305 static pthread_mutex_t ck_mutex_lock = PTHREAD_MUTEX_INITIALIZER;
307 ppack_cleanup (void *mutex)
309 pthread_mutex_unlock ((pthread_mutex_t *) mutex);
314 ppack (FILE * fdes, enum ck_msg_type type, CheckMsg * msg)
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);
325 pthread_cleanup_push (ppack_cleanup, &ck_mutex_lock);
326 pthread_mutex_lock (&ck_mutex_lock);
327 r = fwrite (buf, 1, n, fdes);
329 pthread_mutex_unlock (&ck_mutex_lock);
330 pthread_cleanup_pop (0);
332 eprintf ("Error in call to fwrite:", __FILE__, __LINE__ - 2);
338 read_buf (FILE * fdes, int size, char *buf)
342 n = fread (buf, 1, size, fdes);
345 eprintf ("Error in call to fread:", __FILE__, __LINE__ - 4);
352 get_result (char *buf, RcvMsg * rmsg)
354 enum ck_msg_type type;
358 n = upack (buf, &msg, &type);
360 eprintf ("Error in call to upack", __FILE__, __LINE__ - 2);
362 if (type == CK_MSG_CTX) {
363 CtxMsg *cmsg = (CtxMsg *) & msg;
365 rcvmsg_update_ctx (rmsg, cmsg->ctx);
366 } else if (type == CK_MSG_LOC) {
367 LocMsg *lmsg = (LocMsg *) & msg;
369 if (rmsg->failctx == CK_CTX_INVALID) {
370 rcvmsg_update_loc (rmsg, lmsg->file, lmsg->line);
373 } else if (type == CK_MSG_FAIL) {
374 FailMsg *fmsg = (FailMsg *) & msg;
376 if (rmsg->msg == NULL) {
377 rmsg->msg = strdup (fmsg->msg);
378 rmsg->failctx = rmsg->lastctx;
380 /* Skip subsequent failure messages, only happens for CK_NOFORK */
383 } else if (type == CK_MSG_DURATION) {
384 DurationMsg *cmsg = (DurationMsg *) & msg;
386 rmsg->duration = cmsg->duration;
388 check_type (type, __FILE__, __LINE__);
394 reset_rcv_test (RcvMsg * rmsg)
396 rmsg->test_line = -1;
397 rmsg->test_file = NULL;
401 reset_rcv_fixture (RcvMsg * rmsg)
403 rmsg->fixture_line = -1;
404 rmsg->fixture_file = NULL;
412 rmsg = (RcvMsg *) emalloc (sizeof (RcvMsg));
413 rmsg->lastctx = CK_CTX_INVALID;
414 rmsg->failctx = CK_CTX_INVALID;
417 reset_rcv_test (rmsg);
418 reset_rcv_fixture (rmsg);
423 rcvmsg_free (RcvMsg * rmsg)
425 free (rmsg->fixture_file);
426 free (rmsg->test_file);
432 rcvmsg_update_ctx (RcvMsg * rmsg, enum ck_result_ctx ctx)
434 if (rmsg->lastctx != CK_CTX_INVALID) {
435 free (rmsg->fixture_file);
436 reset_rcv_fixture (rmsg);
442 rcvmsg_update_loc (RcvMsg * rmsg, const char *file, int line)
444 if (rmsg->lastctx == CK_CTX_TEST) {
445 free (rmsg->test_file);
446 rmsg->test_line = line;
447 rmsg->test_file = strdup (file);
449 free (rmsg->fixture_file);
450 rmsg->fixture_line = line;
451 rmsg->fixture_file = strdup (file);
456 punpack (FILE * fdes)
458 int nread, nparse, n;
462 rmsg = rcvmsg_create ();
464 /* Allcate 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);
469 /* While not all parsed */
471 /* Parse one message */
472 n = get_result (buf, rmsg);
474 /* Move remaining data in buffer to the beginning */
475 memmove (buf, buf + n, nparse);
476 /* If EOF has not been seen */
478 /* Read more data into empty space at end of the buffer */
479 nread = read_buf (fdes, n, buf + nparse);
485 if (rmsg->lastctx == CK_CTX_INVALID) {