2009-07-16 17:39:16 +00:00
|
|
|
/*
|
|
|
|
* Check: a unit test framework for C
|
|
|
|
* Copyright (C) 2001, 2002 Arien Malec
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 20:44:48 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2009-07-16 17:39:16 +00:00
|
|
|
*/
|
|
|
|
|
check: Apply GStreamer-specific patches
Reintroduced patches:
* Make sure that fail_if(1) actually fails
from commit 9f99d056a263e71a5e6181224829def906cf0226
New patches due to updated libcheck (based on 0.9.14):
* Checks in m4/check-checks.m4 to cater for new dependencies
* Conditional compile-time compat POSIX fallbacks for libcheck
* Avoid relative paths for libcheck header files
* Make timer_create() usage depend on posix timers, not librt
* Rely on default AX_PTHREAD behavior to allow HAVE_PTHREAD to be used
when checking for types and functions (like clock_gettime())
* Avoid double declaration of clock_gettime() when availabe outside of
librt by making compat clock_gettime() declaration conditional
* check 0.9.9 renamed _fail_unless() and 0.9.12 later renamed it again
to _ck_assert_failed(), so ASSERT_{CRITICAL,WARNING}() now calls this
function
* Remove libcheck fallback infrastructure for malloc(), realloc(),
gettimeofday() and snprintf() since either they appear to be
available or they introduce even more dependencies.
The result is an embedded check in gstreamer that has been tested by
running check tests in core, -base, -good, -bad, -ugly and rtsp-server
on Linux, OSX and Windows.
Fixes https://bugzilla.gnome.org/show_bug.cgi?id=727826
2014-11-15 12:26:47 +00:00
|
|
|
#include "libcompat.h"
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-12-14 11:54:32 +00:00
|
|
|
#include "internal-check.h"
|
2009-07-16 17:39:16 +00:00
|
|
|
#include "check_error.h"
|
|
|
|
#include "check_list.h"
|
|
|
|
#include "check_impl.h"
|
|
|
|
#include "check_pack.h"
|
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
#ifndef HAVE_PTHREAD
|
2009-10-12 11:50:51 +00:00
|
|
|
#define pthread_mutex_lock(arg)
|
|
|
|
#define pthread_mutex_unlock(arg)
|
2014-11-15 11:53:32 +00:00
|
|
|
#define pthread_cleanup_push(f,a) {
|
|
|
|
#define pthread_cleanup_pop(e) }
|
2009-10-12 11:50:51 +00:00
|
|
|
#endif
|
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
/* Maximum size for one message in the message stream. */
|
|
|
|
#define CK_MAX_MSG_SIZE 8192
|
|
|
|
/* This is used to implement a sliding window on the receiving
|
|
|
|
* side. When sending messages, we assure that no single message
|
|
|
|
* is bigger than this (actually we check against CK_MAX_MSG_SIZE/2).
|
|
|
|
* The usual size for a message is less than 80 bytes.
|
|
|
|
* All this is done instead of the previous approach to allocate (actually
|
|
|
|
* continuously reallocate) one big chunk for the whole message stream.
|
|
|
|
* Problems were seen in the wild with up to 4 GB reallocations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-07-16 17:39:16 +00:00
|
|
|
/* typedef an unsigned int that has at least 4 bytes */
|
|
|
|
typedef uint32_t ck_uint32;
|
|
|
|
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void pack_int (char **buf, int val);
|
|
|
|
static int upack_int (char **buf);
|
|
|
|
static void pack_str (char **buf, const char *str);
|
|
|
|
static char *upack_str (char **buf);
|
|
|
|
|
|
|
|
static int pack_ctx (char **buf, CtxMsg * cmsg);
|
|
|
|
static int pack_loc (char **buf, LocMsg * lmsg);
|
|
|
|
static int pack_fail (char **buf, FailMsg * fmsg);
|
2014-11-15 11:53:32 +00:00
|
|
|
static int pack_duration (char **buf, DurationMsg * fmsg);
|
2009-10-12 11:49:35 +00:00
|
|
|
static void upack_ctx (char **buf, CtxMsg * cmsg);
|
|
|
|
static void upack_loc (char **buf, LocMsg * lmsg);
|
|
|
|
static void upack_fail (char **buf, FailMsg * fmsg);
|
2014-11-15 11:53:32 +00:00
|
|
|
static void upack_duration (char **buf, DurationMsg * fmsg);
|
2009-10-12 11:49:35 +00:00
|
|
|
|
|
|
|
static void check_type (int type, const char *file, int line);
|
2009-07-16 17:39:16 +00:00
|
|
|
static enum ck_msg_type upack_type (char **buf);
|
2009-10-12 11:49:35 +00:00
|
|
|
static void pack_type (char **buf, enum ck_msg_type type);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
static int read_buf (FILE * fdes, int size, char *buf);
|
2009-10-12 11:49:35 +00:00
|
|
|
static int get_result (char *buf, RcvMsg * rmsg);
|
|
|
|
static void rcvmsg_update_ctx (RcvMsg * rmsg, enum ck_result_ctx ctx);
|
|
|
|
static void rcvmsg_update_loc (RcvMsg * rmsg, const char *file, int line);
|
2009-07-16 17:39:16 +00:00
|
|
|
static RcvMsg *rcvmsg_create (void);
|
2009-10-12 11:49:35 +00:00
|
|
|
void rcvmsg_free (RcvMsg * rmsg);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
typedef int (*pfun) (char **, CheckMsg *);
|
2009-07-16 17:39:16 +00:00
|
|
|
typedef void (*upfun) (char **, CheckMsg *);
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static pfun pftab[] = {
|
2009-07-16 17:39:16 +00:00
|
|
|
(pfun) pack_ctx,
|
|
|
|
(pfun) pack_fail,
|
2014-11-15 11:53:32 +00:00
|
|
|
(pfun) pack_loc,
|
|
|
|
(pfun) pack_duration
|
2009-07-16 17:39:16 +00:00
|
|
|
};
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static upfun upftab[] = {
|
2009-07-16 17:39:16 +00:00
|
|
|
(upfun) upack_ctx,
|
|
|
|
(upfun) upack_fail,
|
2014-11-15 11:53:32 +00:00
|
|
|
(upfun) upack_loc,
|
|
|
|
(upfun) upack_duration
|
2009-07-16 17:39:16 +00:00
|
|
|
};
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
int
|
|
|
|
pack (enum ck_msg_type type, char **buf, CheckMsg * msg)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (buf == NULL)
|
|
|
|
return -1;
|
|
|
|
if (msg == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
check_type (type, __FILE__, __LINE__);
|
|
|
|
|
|
|
|
return pftab[type] (buf, msg);
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
int
|
|
|
|
upack (char *buf, CheckMsg * msg, enum ck_msg_type *type)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
char *obuf;
|
|
|
|
|
|
|
|
if (buf == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
obuf = buf;
|
|
|
|
|
|
|
|
*type = upack_type (&buf);
|
|
|
|
|
|
|
|
check_type (*type, __FILE__, __LINE__);
|
2009-10-12 11:49:35 +00:00
|
|
|
|
2009-07-16 17:39:16 +00:00
|
|
|
upftab[*type] (&buf, msg);
|
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
return buf - obuf;
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
pack_int (char **buf, int val)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
unsigned char *ubuf = (unsigned char *) *buf;
|
|
|
|
ck_uint32 uval = val;
|
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
ubuf[0] = (unsigned char) ((uval >> 24) & 0xFF);
|
|
|
|
ubuf[1] = (unsigned char) ((uval >> 16) & 0xFF);
|
|
|
|
ubuf[2] = (unsigned char) ((uval >> 8) & 0xFF);
|
|
|
|
ubuf[3] = (unsigned char) (uval & 0xFF);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
*buf += 4;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static int
|
|
|
|
upack_int (char **buf)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
unsigned char *ubuf = (unsigned char *) *buf;
|
|
|
|
ck_uint32 uval;
|
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
uval =
|
|
|
|
(ck_uint32) ((ubuf[0] << 24) | (ubuf[1] << 16) | (ubuf[2] << 8) |
|
|
|
|
ubuf[3]);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
*buf += 4;
|
|
|
|
|
|
|
|
return (int) uval;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
pack_str (char **buf, const char *val)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
int strsz;
|
|
|
|
|
|
|
|
if (val == NULL)
|
|
|
|
strsz = 0;
|
|
|
|
else
|
|
|
|
strsz = strlen (val);
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
pack_int (buf, strsz);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
if (strsz > 0) {
|
|
|
|
memcpy (*buf, val, strsz);
|
|
|
|
*buf += strsz;
|
2009-10-12 11:49:35 +00:00
|
|
|
}
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static char *
|
|
|
|
upack_str (char **buf)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
char *val;
|
|
|
|
int strsz;
|
|
|
|
|
|
|
|
strsz = upack_int (buf);
|
|
|
|
|
|
|
|
if (strsz > 0) {
|
2014-11-15 11:53:32 +00:00
|
|
|
val = (char *) emalloc (strsz + 1);
|
2009-07-16 17:39:16 +00:00
|
|
|
memcpy (val, *buf, strsz);
|
|
|
|
val[strsz] = 0;
|
|
|
|
*buf += strsz;
|
|
|
|
} else {
|
2014-11-15 11:53:32 +00:00
|
|
|
val = (char *) emalloc (1);
|
2009-07-16 17:39:16 +00:00
|
|
|
*val = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
pack_type (char **buf, enum ck_msg_type type)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
pack_int (buf, (int) type);
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static enum ck_msg_type
|
|
|
|
upack_type (char **buf)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
return (enum ck_msg_type) upack_int (buf);
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
pack_ctx (char **buf, CtxMsg * cmsg)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = 4 + 4;
|
2014-11-15 11:53:32 +00:00
|
|
|
*buf = ptr = (char *) emalloc (len);
|
2009-10-12 11:49:35 +00:00
|
|
|
|
2009-07-16 17:39:16 +00:00
|
|
|
pack_type (&ptr, CK_MSG_CTX);
|
|
|
|
pack_int (&ptr, (int) cmsg->ctx);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
upack_ctx (char **buf, CtxMsg * cmsg)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
2014-11-15 11:53:32 +00:00
|
|
|
cmsg->ctx = (enum ck_result_ctx) upack_int (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pack_duration (char **buf, DurationMsg * cmsg)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = 4 + 4;
|
|
|
|
*buf = ptr = (char *) emalloc (len);
|
|
|
|
|
|
|
|
pack_type (&ptr, CK_MSG_DURATION);
|
|
|
|
pack_int (&ptr, cmsg->duration);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
upack_duration (char **buf, DurationMsg * cmsg)
|
|
|
|
{
|
|
|
|
cmsg->duration = upack_int (buf);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static int
|
|
|
|
pack_loc (char **buf, LocMsg * lmsg)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = 4 + 4 + (lmsg->file ? strlen (lmsg->file) : 0) + 4;
|
2014-11-15 11:53:32 +00:00
|
|
|
*buf = ptr = (char *) emalloc (len);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
pack_type (&ptr, CK_MSG_LOC);
|
|
|
|
pack_str (&ptr, lmsg->file);
|
|
|
|
pack_int (&ptr, lmsg->line);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
upack_loc (char **buf, LocMsg * lmsg)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
lmsg->file = upack_str (buf);
|
|
|
|
lmsg->line = upack_int (buf);
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static int
|
|
|
|
pack_fail (char **buf, FailMsg * fmsg)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = 4 + 4 + (fmsg->msg ? strlen (fmsg->msg) : 0);
|
2014-11-15 11:53:32 +00:00
|
|
|
*buf = ptr = (char *) emalloc (len);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
pack_type (&ptr, CK_MSG_FAIL);
|
|
|
|
pack_str (&ptr, fmsg->msg);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
upack_fail (char **buf, FailMsg * fmsg)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
fmsg->msg = upack_str (buf);
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
check_type (int type, const char *file, int line)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (type < 0 || type >= CK_MSG_LAST)
|
2009-10-12 11:50:51 +00:00
|
|
|
eprintf ("Bad message type arg %d", file, line, type);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 20:09:48 +00:00
|
|
|
#ifdef HAVE_PTHREAD
|
2014-11-15 11:53:32 +00:00
|
|
|
static pthread_mutex_t ck_mutex_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static void
|
|
|
|
ppack_cleanup (void *mutex)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock ((pthread_mutex_t *) mutex);
|
|
|
|
}
|
2009-10-12 11:50:51 +00:00
|
|
|
#endif
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
2014-11-15 11:53:32 +00:00
|
|
|
ppack (FILE * fdes, enum ck_msg_type type, CheckMsg * msg)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
2014-11-15 11:53:32 +00:00
|
|
|
char *buf = NULL;
|
2009-07-16 17:39:16 +00:00
|
|
|
int n;
|
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
n = pack (type, &buf, msg);
|
2014-11-15 11:53:32 +00:00
|
|
|
/* Keep it on the safe side to not send too much data. */
|
|
|
|
if (n > (CK_MAX_MSG_SIZE / 2))
|
|
|
|
eprintf ("Message string too long", __FILE__, __LINE__ - 2);
|
|
|
|
|
|
|
|
pthread_cleanup_push (ppack_cleanup, &ck_mutex_lock);
|
|
|
|
pthread_mutex_lock (&ck_mutex_lock);
|
|
|
|
r = fwrite (buf, 1, n, fdes);
|
|
|
|
fflush (fdes);
|
|
|
|
pthread_mutex_unlock (&ck_mutex_lock);
|
|
|
|
pthread_cleanup_pop (0);
|
|
|
|
if (r != n)
|
|
|
|
eprintf ("Error in call to fwrite:", __FILE__, __LINE__ - 2);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
free (buf);
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static int
|
2014-11-15 11:53:32 +00:00
|
|
|
read_buf (FILE * fdes, int size, char *buf)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
int n;
|
2014-11-15 11:53:32 +00:00
|
|
|
|
|
|
|
n = fread (buf, 1, size, fdes);
|
|
|
|
|
|
|
|
if (ferror (fdes)) {
|
|
|
|
eprintf ("Error in call to fread:", __FILE__, __LINE__ - 4);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
return n;
|
2009-10-12 11:49:35 +00:00
|
|
|
}
|
2009-07-16 17:39:16 +00:00
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static int
|
|
|
|
get_result (char *buf, RcvMsg * rmsg)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
enum ck_msg_type type;
|
|
|
|
CheckMsg msg;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = upack (buf, &msg, &type);
|
|
|
|
if (n == -1)
|
|
|
|
eprintf ("Error in call to upack", __FILE__, __LINE__ - 2);
|
2009-10-12 11:49:35 +00:00
|
|
|
|
2009-07-16 17:39:16 +00:00
|
|
|
if (type == CK_MSG_CTX) {
|
2009-10-12 11:49:35 +00:00
|
|
|
CtxMsg *cmsg = (CtxMsg *) & msg;
|
2014-11-15 11:53:32 +00:00
|
|
|
|
2009-07-16 17:39:16 +00:00
|
|
|
rcvmsg_update_ctx (rmsg, cmsg->ctx);
|
|
|
|
} else if (type == CK_MSG_LOC) {
|
2009-10-12 11:49:35 +00:00
|
|
|
LocMsg *lmsg = (LocMsg *) & msg;
|
2014-11-15 11:53:32 +00:00
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
if (rmsg->failctx == CK_CTX_INVALID) {
|
2009-07-16 17:39:16 +00:00
|
|
|
rcvmsg_update_loc (rmsg, lmsg->file, lmsg->line);
|
|
|
|
}
|
|
|
|
free (lmsg->file);
|
2009-10-12 11:49:35 +00:00
|
|
|
} else if (type == CK_MSG_FAIL) {
|
|
|
|
FailMsg *fmsg = (FailMsg *) & msg;
|
2014-11-15 11:53:32 +00:00
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
if (rmsg->msg == NULL) {
|
2014-11-15 11:53:32 +00:00
|
|
|
rmsg->msg = strdup (fmsg->msg);
|
2009-07-16 17:39:16 +00:00
|
|
|
rmsg->failctx = rmsg->lastctx;
|
2009-10-12 11:49:35 +00:00
|
|
|
} else {
|
2009-07-16 17:39:16 +00:00
|
|
|
/* Skip subsequent failure messages, only happens for CK_NOFORK */
|
|
|
|
}
|
|
|
|
free (fmsg->msg);
|
2014-11-15 11:53:32 +00:00
|
|
|
} else if (type == CK_MSG_DURATION) {
|
|
|
|
DurationMsg *cmsg = (DurationMsg *) & msg;
|
|
|
|
|
|
|
|
rmsg->duration = cmsg->duration;
|
2009-07-16 17:39:16 +00:00
|
|
|
} else
|
|
|
|
check_type (type, __FILE__, __LINE__);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
reset_rcv_test (RcvMsg * rmsg)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
rmsg->test_line = -1;
|
|
|
|
rmsg->test_file = NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
reset_rcv_fixture (RcvMsg * rmsg)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
rmsg->fixture_line = -1;
|
|
|
|
rmsg->fixture_file = NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static RcvMsg *
|
|
|
|
rcvmsg_create (void)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
RcvMsg *rmsg;
|
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
rmsg = (RcvMsg *) emalloc (sizeof (RcvMsg));
|
2009-07-16 17:39:16 +00:00
|
|
|
rmsg->lastctx = CK_CTX_INVALID;
|
|
|
|
rmsg->failctx = CK_CTX_INVALID;
|
|
|
|
rmsg->msg = NULL;
|
2014-11-15 11:53:32 +00:00
|
|
|
rmsg->duration = -1;
|
2009-07-16 17:39:16 +00:00
|
|
|
reset_rcv_test (rmsg);
|
|
|
|
reset_rcv_fixture (rmsg);
|
|
|
|
return rmsg;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
|
|
|
rcvmsg_free (RcvMsg * rmsg)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
2009-10-12 11:49:35 +00:00
|
|
|
free (rmsg->fixture_file);
|
|
|
|
free (rmsg->test_file);
|
|
|
|
free (rmsg->msg);
|
|
|
|
free (rmsg);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
rcvmsg_update_ctx (RcvMsg * rmsg, enum ck_result_ctx ctx)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
2009-10-12 11:49:35 +00:00
|
|
|
if (rmsg->lastctx != CK_CTX_INVALID) {
|
|
|
|
free (rmsg->fixture_file);
|
2009-07-16 17:39:16 +00:00
|
|
|
reset_rcv_fixture (rmsg);
|
|
|
|
}
|
|
|
|
rmsg->lastctx = ctx;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
rcvmsg_update_loc (RcvMsg * rmsg, const char *file, int line)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (rmsg->lastctx == CK_CTX_TEST) {
|
2009-10-12 11:49:35 +00:00
|
|
|
free (rmsg->test_file);
|
2009-07-16 17:39:16 +00:00
|
|
|
rmsg->test_line = line;
|
2014-11-15 11:53:32 +00:00
|
|
|
rmsg->test_file = strdup (file);
|
2009-07-16 17:39:16 +00:00
|
|
|
} else {
|
2009-10-12 11:49:35 +00:00
|
|
|
free (rmsg->fixture_file);
|
2009-07-16 17:39:16 +00:00
|
|
|
rmsg->fixture_line = line;
|
2014-11-15 11:53:32 +00:00
|
|
|
rmsg->fixture_file = strdup (file);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-12 11:49:35 +00:00
|
|
|
|
|
|
|
RcvMsg *
|
2014-11-15 11:53:32 +00:00
|
|
|
punpack (FILE * fdes)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
2014-11-15 11:53:32 +00:00
|
|
|
int nread, nparse, n;
|
2009-07-16 17:39:16 +00:00
|
|
|
char *buf;
|
|
|
|
RcvMsg *rmsg;
|
|
|
|
|
|
|
|
rmsg = rcvmsg_create ();
|
2009-10-12 11:49:35 +00:00
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
/* Allcate a buffer */
|
|
|
|
buf = (char *) emalloc (CK_MAX_MSG_SIZE);
|
|
|
|
/* Fill the buffer from the file */
|
|
|
|
nread = read_buf (fdes, CK_MAX_MSG_SIZE, buf);
|
|
|
|
nparse = nread;
|
|
|
|
/* While not all parsed */
|
|
|
|
while (nparse > 0) {
|
|
|
|
/* Parse one message */
|
2009-07-16 17:39:16 +00:00
|
|
|
n = get_result (buf, rmsg);
|
2014-11-15 11:53:32 +00:00
|
|
|
nparse -= n;
|
|
|
|
/* Move remaining data in buffer to the beginning */
|
|
|
|
memmove (buf, buf + n, nparse);
|
|
|
|
/* If EOF has not been seen */
|
|
|
|
if (nread > 0) {
|
|
|
|
/* Read more data into empty space at end of the buffer */
|
|
|
|
nread = read_buf (fdes, n, buf + nparse);
|
|
|
|
nparse += nread;
|
|
|
|
}
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
2014-11-15 11:53:32 +00:00
|
|
|
free (buf);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
if (rmsg->lastctx == CK_CTX_INVALID) {
|
|
|
|
free (rmsg);
|
|
|
|
rmsg = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rmsg;
|
|
|
|
}
|