|
Packit |
0b5880 |
/*
|
|
Packit |
0b5880 |
* Check: a unit test framework for C
|
|
Packit |
0b5880 |
* Copyright (C) 2001, 2002 Arien Malec
|
|
Packit |
0b5880 |
*
|
|
Packit |
0b5880 |
* This library is free software; you can redistribute it and/or
|
|
Packit |
0b5880 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
0b5880 |
* License as published by the Free Software Foundation; either
|
|
Packit |
0b5880 |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
0b5880 |
*
|
|
Packit |
0b5880 |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
0b5880 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
0b5880 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
0b5880 |
* Lesser General Public License for more details.
|
|
Packit |
0b5880 |
*
|
|
Packit |
0b5880 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
0b5880 |
* License along with this library; if not, write to the
|
|
Packit |
0b5880 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
|
Packit |
0b5880 |
* MA 02110-1301, USA.
|
|
Packit |
0b5880 |
*/
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
#include "../lib/libcompat.h"
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
#include <sys/types.h>
|
|
Packit |
0b5880 |
#include <stdlib.h>
|
|
Packit |
0b5880 |
#include <fcntl.h>
|
|
Packit |
0b5880 |
#include <stdio.h>
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
#include "check_error.h"
|
|
Packit |
0b5880 |
#include "check.h"
|
|
Packit |
0b5880 |
#include "check_list.h"
|
|
Packit |
0b5880 |
#include "check_impl.h"
|
|
Packit |
0b5880 |
#include "check_msg.h"
|
|
Packit |
0b5880 |
#include "check_pack.h"
|
|
Packit |
0b5880 |
#include "check_str.h"
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
/* 'Pipe' is implemented as a temporary file to overcome message
|
|
Packit |
0b5880 |
* volume limitations outlined in bug #482012. This scheme works well
|
|
Packit |
0b5880 |
* with the existing usage wherein the parent does not begin reading
|
|
Packit |
0b5880 |
* until the child has done writing and exited.
|
|
Packit |
0b5880 |
*
|
|
Packit |
0b5880 |
* Pipe life cycle:
|
|
Packit |
0b5880 |
* - The parent creates a tmpfile().
|
|
Packit |
0b5880 |
* - The fork() call has the effect of duplicating the file descriptor
|
|
Packit |
0b5880 |
* and copying (on write) the FILE* data structures.
|
|
Packit |
0b5880 |
* - The child writes to the file, and its dup'ed file descriptor and
|
|
Packit |
0b5880 |
* data structures are cleaned up on child process exit.
|
|
Packit |
0b5880 |
* - Before reading, the parent rewind()'s the file to reset both
|
|
Packit |
0b5880 |
* FILE* and underlying file descriptor location data.
|
|
Packit |
0b5880 |
* - When finished, the parent fclose()'s the FILE*, deleting the
|
|
Packit |
0b5880 |
* temporary file, per tmpfile()'s semantics.
|
|
Packit |
0b5880 |
*
|
|
Packit |
0b5880 |
* This scheme may break down if the usage changes to asynchronous
|
|
Packit |
0b5880 |
* reading and writing.
|
|
Packit |
0b5880 |
*/
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
static FILE *send_file1;
|
|
Packit |
0b5880 |
static char *send_file1_name;
|
|
Packit |
0b5880 |
static FILE *send_file2;
|
|
Packit |
0b5880 |
static char *send_file2_name;
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
static FILE *get_pipe(void);
|
|
Packit |
0b5880 |
static void setup_pipe(void);
|
|
Packit |
0b5880 |
static void teardown_pipe(void);
|
|
Packit |
0b5880 |
static TestResult *construct_test_result(RcvMsg * rmsg, int waserror);
|
|
Packit |
0b5880 |
static void tr_set_loc_by_ctx(TestResult * tr, enum ck_result_ctx ctx,
|
|
Packit |
0b5880 |
RcvMsg * rmsg);
|
|
Packit |
0b5880 |
static FILE *get_pipe(void)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
if(send_file2 != 0)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
return send_file2;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
if(send_file1 != 0)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
return send_file1;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
eprintf("Unable to report test progress or a failure; was an ck_assert or ck_abort function called while not running tests?", __FILE__, __LINE__);
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
return NULL;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
void send_failure_info(const char *msg)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
FailMsg fmsg;
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
fmsg.msg = strdup(msg);
|
|
Packit |
0b5880 |
ppack(get_pipe(), CK_MSG_FAIL, (CheckMsg *) & fmsg);
|
|
Packit |
0b5880 |
free(fmsg.msg);
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
void send_duration_info(int duration)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
DurationMsg dmsg;
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
dmsg.duration = duration;
|
|
Packit |
0b5880 |
ppack(get_pipe(), CK_MSG_DURATION, (CheckMsg *) & dmsg);
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
void send_loc_info(const char *file, int line)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
LocMsg lmsg;
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
lmsg.file = strdup(file);
|
|
Packit |
0b5880 |
lmsg.line = line;
|
|
Packit |
0b5880 |
ppack(get_pipe(), CK_MSG_LOC, (CheckMsg *) & lmsg);
|
|
Packit |
0b5880 |
free(lmsg.file);
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
void send_ctx_info(enum ck_result_ctx ctx)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
CtxMsg cmsg;
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
cmsg.ctx = ctx;
|
|
Packit |
0b5880 |
ppack(get_pipe(), CK_MSG_CTX, (CheckMsg *) & cmsg);
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
TestResult *receive_test_result(int waserror)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
FILE *fp;
|
|
Packit |
0b5880 |
RcvMsg *rmsg;
|
|
Packit |
0b5880 |
TestResult *result;
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
fp = get_pipe();
|
|
Packit |
0b5880 |
if(fp == NULL)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
eprintf("Error in call to get_pipe", __FILE__, __LINE__ - 2);
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
rewind(fp);
|
|
Packit |
0b5880 |
rmsg = punpack(fp);
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
if(rmsg == NULL)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
eprintf("Error in call to punpack", __FILE__, __LINE__ - 4);
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
teardown_pipe();
|
|
Packit |
0b5880 |
setup_pipe();
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
result = construct_test_result(rmsg, waserror);
|
|
Packit |
0b5880 |
rcvmsg_free(rmsg);
|
|
Packit |
0b5880 |
return result;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
static void tr_set_loc_by_ctx(TestResult * tr, enum ck_result_ctx ctx,
|
|
Packit |
0b5880 |
RcvMsg * rmsg)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
if(ctx == CK_CTX_TEST)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
tr->file = rmsg->test_file;
|
|
Packit |
0b5880 |
tr->line = rmsg->test_line;
|
|
Packit |
0b5880 |
rmsg->test_file = NULL;
|
|
Packit |
0b5880 |
rmsg->test_line = -1;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
else
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
tr->file = rmsg->fixture_file;
|
|
Packit |
0b5880 |
tr->line = rmsg->fixture_line;
|
|
Packit |
0b5880 |
rmsg->fixture_file = NULL;
|
|
Packit |
0b5880 |
rmsg->fixture_line = -1;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
static TestResult *construct_test_result(RcvMsg * rmsg, int waserror)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
TestResult *tr;
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
if(rmsg == NULL)
|
|
Packit |
0b5880 |
return NULL;
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
tr = tr_create();
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
if(rmsg->msg != NULL || waserror)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
if(rmsg->failctx != CK_CTX_INVALID)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
tr->ctx = rmsg->failctx;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
else
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
tr->ctx = rmsg->lastctx;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
tr->msg = rmsg->msg;
|
|
Packit |
0b5880 |
rmsg->msg = NULL;
|
|
Packit |
0b5880 |
tr_set_loc_by_ctx(tr, tr->ctx, rmsg);
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
else if(rmsg->lastctx == CK_CTX_SETUP)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
tr->ctx = CK_CTX_SETUP;
|
|
Packit |
0b5880 |
tr->msg = NULL;
|
|
Packit |
0b5880 |
tr_set_loc_by_ctx(tr, CK_CTX_SETUP, rmsg);
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
else
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
tr->ctx = CK_CTX_TEST;
|
|
Packit |
0b5880 |
tr->msg = NULL;
|
|
Packit |
0b5880 |
tr->duration = rmsg->duration;
|
|
Packit |
0b5880 |
tr_set_loc_by_ctx(tr, CK_CTX_TEST, rmsg);
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
return tr;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
void setup_messaging(void)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
setup_pipe();
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
void teardown_messaging(void)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
teardown_pipe();
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
/**
|
|
Packit |
0b5880 |
* Open a temporary file.
|
|
Packit |
0b5880 |
*
|
|
Packit |
0b5880 |
* If the file could be unlinked upon creation, the name
|
|
Packit |
0b5880 |
* of the file is not returned via 'name'. However, if the
|
|
Packit |
0b5880 |
* file could not be unlinked, the name is returned,
|
|
Packit |
0b5880 |
* expecting the caller to both delete the file and
|
|
Packit |
0b5880 |
* free the 'name' field after the file is closed.
|
|
Packit |
0b5880 |
*/
|
|
Packit |
0b5880 |
FILE *open_tmp_file(char **name)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
FILE *file = NULL;
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
*name = NULL;
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
#if !HAVE_MKSTEMP
|
|
Packit |
0b5880 |
/* Windows does not like tmpfile(). This is likely because tmpfile()
|
|
Packit |
0b5880 |
* call unlink() on the file before returning it, to make sure the
|
|
Packit |
0b5880 |
* file is deleted when it is closed. The unlink() call also fails
|
|
Packit |
0b5880 |
* on Windows if the file is still open. */
|
|
Packit |
0b5880 |
/* also note that mkstemp is apparently a C90 replacement for tmpfile */
|
|
Packit |
0b5880 |
/* perhaps all we need to do on Windows is set TMPDIR to whatever is
|
|
Packit |
0b5880 |
stored in TEMP for tmpfile to work */
|
|
Packit |
0b5880 |
/* and finally, the "b" from "w+b" is ignored on OS X, not sure about WIN32 */
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
file = tmpfile();
|
|
Packit |
0b5880 |
if(file == NULL)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
/*
|
|
Packit |
0b5880 |
* The heuristic for selecting a temporary folder is as follows:
|
|
Packit |
0b5880 |
* 1) If the TEMP environment variable is defined, use that directory.
|
|
Packit |
0b5880 |
* 2) If the P_tmpdir macro is defined, use that directory.
|
|
Packit |
0b5880 |
* 3) If the TMPDIR environment variable is defined, use that directory.
|
|
Packit |
0b5880 |
* 4) Use the platform defined temporary directory, or the current directory.
|
|
Packit |
0b5880 |
*/
|
|
Packit |
0b5880 |
char *tmp = getenv("TEMP");
|
|
Packit |
0b5880 |
char *tmp_file = tempnam(tmp, "check_");
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
/*
|
|
Packit |
0b5880 |
* Note, tempnam is not enough to get a unique name. Between
|
|
Packit |
0b5880 |
* getting the name and opening the file, something else also
|
|
Packit |
0b5880 |
* calling tempnam() could get the same name. It has been observed
|
|
Packit |
0b5880 |
* on MinGW-w64 builds on Wine that this exact thing happens
|
|
Packit |
0b5880 |
* if multiple instances of a unit tests are running concurrently.
|
|
Packit |
0b5880 |
* To prevent two concurrent unit tests from getting the same file,
|
|
Packit |
0b5880 |
* we append the pid to the file. The pid should be unique on the
|
|
Packit |
0b5880 |
* system.
|
|
Packit |
0b5880 |
*/
|
|
Packit |
0b5880 |
char *uniq_tmp_file = ck_strdup_printf("%s.%d", tmp_file, getpid());
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
file = fopen(uniq_tmp_file, "w+b");
|
|
Packit |
0b5880 |
*name = uniq_tmp_file;
|
|
Packit |
0b5880 |
free(tmp_file);
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
#else
|
|
Packit |
0b5880 |
/*
|
|
Packit |
0b5880 |
* The heuristic for selecting a temporary folder is as follows:
|
|
Packit |
0b5880 |
* 1) If the TEMP environment variable is defined, use that directory.
|
|
Packit |
0b5880 |
* 2) If the P_tmpdir macro is defined, use that directory.
|
|
Packit |
0b5880 |
* 3) If the TMPDIR environment variable is defined, use that directory.
|
|
Packit |
0b5880 |
* 4) Use the current directory
|
|
Packit |
0b5880 |
*/
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
int fd = -1;
|
|
Packit |
0b5880 |
const char *tmp_dir = getenv ("TEMP");
|
|
Packit |
0b5880 |
#ifdef P_tmpdir
|
|
Packit |
0b5880 |
if (tmp_dir == NULL)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
tmp_dir = P_tmpdir;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
#endif /*P_tmpdir*/
|
|
Packit |
0b5880 |
if (tmp_dir == NULL)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
tmp_dir = getenv ("TMPDIR");
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
if (tmp_dir == NULL)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
tmp_dir = ".";
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
*name = ck_strdup_printf ("%s/check_XXXXXX", tmp_dir);
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
if (-1 < (fd = mkstemp (*name)))
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
file = fdopen (fd, "w+b");
|
|
Packit |
0b5880 |
if (0 == unlink (*name) || NULL == file)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
free (*name);
|
|
Packit |
0b5880 |
*name = NULL;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
#endif
|
|
Packit |
0b5880 |
return file;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
static void setup_pipe(void)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
if(send_file1 == NULL)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
send_file1 = open_tmp_file(&send_file1_name);
|
|
Packit |
0b5880 |
if(send_file1 == NULL)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
eprintf("Unable to create temporary file for communication; may not have permissions to do so", __FILE__, __LINE__ -3);
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
return;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
if(send_file2 == NULL)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
send_file2 = open_tmp_file(&send_file2_name);
|
|
Packit |
0b5880 |
if(send_file2 == NULL)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
eprintf("Unable to create temporary file for communication; may not have permissions to do so", __FILE__, __LINE__ -3);
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
return;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
eprintf("Only one nesting of suite runs supported", __FILE__, __LINE__);
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
|
|
Packit |
0b5880 |
static void teardown_pipe(void)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
if(send_file2 != 0)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
fclose(send_file2);
|
|
Packit |
0b5880 |
send_file2 = 0;
|
|
Packit |
0b5880 |
if(send_file2_name != NULL)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
unlink(send_file2_name);
|
|
Packit |
0b5880 |
free(send_file2_name);
|
|
Packit |
0b5880 |
send_file2_name = NULL;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
else if(send_file1 != 0)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
fclose(send_file1);
|
|
Packit |
0b5880 |
send_file1 = 0;
|
|
Packit |
0b5880 |
if(send_file1_name != NULL)
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
unlink(send_file1_name);
|
|
Packit |
0b5880 |
free(send_file1_name);
|
|
Packit |
0b5880 |
send_file1_name = NULL;
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
else
|
|
Packit |
0b5880 |
{
|
|
Packit |
0b5880 |
eprintf("No messaging setup", __FILE__, __LINE__);
|
|
Packit |
0b5880 |
}
|
|
Packit |
0b5880 |
}
|