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