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
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include "check.h"
|
|
|
|
#include "check_error.h"
|
|
|
|
#include "check_list.h"
|
|
|
|
#include "check_impl.h"
|
|
|
|
#include "check_msg.h"
|
|
|
|
#include "check_log.h"
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
enum rinfo
|
|
|
|
{
|
2009-07-16 17:39:16 +00:00
|
|
|
CK_R_SIG,
|
|
|
|
CK_R_PASS,
|
|
|
|
CK_R_EXIT,
|
|
|
|
CK_R_FAIL_TEST,
|
|
|
|
CK_R_FAIL_FIXTURE
|
|
|
|
};
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
enum tf_type
|
|
|
|
{
|
2009-07-16 17:39:16 +00:00
|
|
|
CK_FORK_TEST,
|
|
|
|
CK_NOFORK_TEST,
|
|
|
|
CK_NOFORK_FIXTURE
|
|
|
|
};
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void srunner_run_init (SRunner * sr, enum print_output print_mode);
|
|
|
|
static void srunner_run_end (SRunner * sr, enum print_output print_mode);
|
|
|
|
static void srunner_iterate_suites (SRunner * sr, enum print_output print_mode);
|
|
|
|
static void srunner_run_tcase (SRunner * sr, TCase * tc);
|
|
|
|
static int srunner_run_unchecked_setup (SRunner * sr, TCase * tc);
|
|
|
|
static void srunner_run_unchecked_teardown (SRunner * sr, TCase * tc);
|
|
|
|
static TestResult *tcase_run_checked_setup (SRunner * sr, TCase * tc);
|
|
|
|
static void tcase_run_checked_teardown (TCase * tc);
|
|
|
|
static void srunner_iterate_tcase_tfuns (SRunner * sr, TCase * tc);
|
|
|
|
static void srunner_add_failure (SRunner * sr, TestResult * tf);
|
|
|
|
static TestResult *tcase_run_tfun_fork (SRunner * sr, TCase * tc, TF * tf,
|
|
|
|
int i);
|
|
|
|
static TestResult *tcase_run_tfun_nofork (SRunner * sr, TCase * tc, TF * tf,
|
|
|
|
int i);
|
2009-07-16 17:39:16 +00:00
|
|
|
static TestResult *receive_result_info_fork (const char *tcname,
|
2009-10-12 11:49:35 +00:00
|
|
|
const char *tname, int iter, int status, int expected_signal);
|
2009-07-16 17:39:16 +00:00
|
|
|
static TestResult *receive_result_info_nofork (const char *tcname,
|
2009-10-12 11:49:35 +00:00
|
|
|
const char *tname, int iter);
|
|
|
|
static void set_fork_info (TestResult * tr, int status, int expected_signal);
|
|
|
|
static void set_nofork_info (TestResult * tr);
|
2009-07-16 17:39:16 +00:00
|
|
|
static char *signal_msg (int sig);
|
|
|
|
static char *signal_error_msg (int signal_received, int signal_expected);
|
|
|
|
static char *pass_msg (void);
|
|
|
|
static char *exit_msg (int exitstatus);
|
|
|
|
static int waserror (int status, int expected_signal);
|
|
|
|
|
|
|
|
#define MSG_LEN 100
|
|
|
|
|
|
|
|
static int alarm_received;
|
|
|
|
static pid_t group_pid;
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
sig_handler (int sig_nr)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
switch (sig_nr) {
|
2009-10-12 11:49:35 +00:00
|
|
|
case SIGALRM:
|
|
|
|
alarm_received = 1;
|
|
|
|
killpg (group_pid, SIGKILL);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
eprintf ("Unhandled signal: %d", __FILE__, __LINE__, sig_nr);
|
|
|
|
break;
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
srunner_run_init (SRunner * sr, enum print_output print_mode)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
2009-10-12 11:49:35 +00:00
|
|
|
set_fork_status (srunner_fork_status (sr));
|
|
|
|
setup_messaging ();
|
2009-07-16 17:39:16 +00:00
|
|
|
srunner_init_logging (sr, print_mode);
|
|
|
|
log_srunner_start (sr);
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
srunner_run_end (SRunner * sr, enum print_output print_mode)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
log_srunner_end (sr);
|
|
|
|
srunner_end_logging (sr);
|
2009-10-12 11:49:35 +00:00
|
|
|
teardown_messaging ();
|
|
|
|
set_fork_status (CK_FORK);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
srunner_iterate_suites (SRunner * sr, enum print_output print_mode)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
List *slst;
|
|
|
|
List *tcl;
|
|
|
|
TCase *tc;
|
|
|
|
|
|
|
|
slst = sr->slst;
|
2009-10-12 11:49:35 +00:00
|
|
|
|
|
|
|
for (list_front (slst); !list_at_end (slst); list_advance (slst)) {
|
|
|
|
Suite *s = list_val (slst);
|
|
|
|
|
2009-07-16 17:39:16 +00:00
|
|
|
log_suite_start (sr, s);
|
|
|
|
|
|
|
|
tcl = s->tclst;
|
2009-10-12 11:49:35 +00:00
|
|
|
|
|
|
|
for (list_front (tcl); !list_at_end (tcl); list_advance (tcl)) {
|
2009-07-16 17:39:16 +00:00
|
|
|
tc = list_val (tcl);
|
|
|
|
srunner_run_tcase (sr, tc);
|
|
|
|
}
|
2009-10-12 11:49:35 +00:00
|
|
|
|
2009-07-16 17:39:16 +00:00
|
|
|
log_suite_end (sr, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
|
|
|
srunner_run_all (SRunner * sr, enum print_output print_mode)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
struct sigaction old_action;
|
|
|
|
struct sigaction new_action;
|
2009-10-12 11:49:35 +00:00
|
|
|
|
2009-07-16 17:39:16 +00:00
|
|
|
if (sr == NULL)
|
|
|
|
return;
|
2009-10-12 11:49:35 +00:00
|
|
|
if (print_mode >= CK_LAST) {
|
|
|
|
eprintf ("Bad print_mode argument to srunner_run_all: %d",
|
|
|
|
__FILE__, __LINE__, print_mode);
|
|
|
|
}
|
|
|
|
memset (&new_action, 0, sizeof new_action);
|
2009-07-16 17:39:16 +00:00
|
|
|
new_action.sa_handler = sig_handler;
|
2009-10-12 11:49:35 +00:00
|
|
|
sigaction (SIGALRM, &new_action, &old_action);
|
2009-07-16 17:39:16 +00:00
|
|
|
srunner_run_init (sr, print_mode);
|
|
|
|
srunner_iterate_suites (sr, print_mode);
|
|
|
|
srunner_run_end (sr, print_mode);
|
2009-10-12 11:49:35 +00:00
|
|
|
sigaction (SIGALRM, &old_action, NULL);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
srunner_add_failure (SRunner * sr, TestResult * tr)
|
|
|
|
{
|
2009-07-16 17:39:16 +00:00
|
|
|
list_add_end (sr->resultlst, tr);
|
2009-10-12 11:49:35 +00:00
|
|
|
sr->stats->n_checked++; /* count checks during setup, test, and teardown */
|
2009-07-16 17:39:16 +00:00
|
|
|
if (tr->rtype == CK_FAILURE)
|
|
|
|
sr->stats->n_failed++;
|
|
|
|
else if (tr->rtype == CK_ERROR)
|
|
|
|
sr->stats->n_errors++;
|
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 void
|
|
|
|
srunner_iterate_tcase_tfuns (SRunner * sr, TCase * tc)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
List *tfl;
|
|
|
|
TF *tfun;
|
|
|
|
TestResult *tr = NULL;
|
|
|
|
|
|
|
|
tfl = tc->tflst;
|
2009-10-12 11:49:35 +00:00
|
|
|
|
|
|
|
for (list_front (tfl); !list_at_end (tfl); list_advance (tfl)) {
|
2009-07-16 17:39:16 +00:00
|
|
|
int i;
|
|
|
|
tfun = list_val (tfl);
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
for (i = tfun->loop_start; i < tfun->loop_end; i++) {
|
|
|
|
switch (srunner_fork_status (sr)) {
|
|
|
|
case CK_FORK:
|
|
|
|
tr = tcase_run_tfun_fork (sr, tc, tfun, i);
|
|
|
|
break;
|
|
|
|
case CK_NOFORK:
|
|
|
|
tr = tcase_run_tfun_nofork (sr, tc, tfun, i);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
eprintf ("Bad fork status in SRunner", __FILE__, __LINE__);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
srunner_add_failure (sr, tr);
|
2009-10-12 11:49:35 +00:00
|
|
|
log_test_end (sr, tr);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
srunner_run_unchecked_setup (SRunner * sr, TCase * tc)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
TestResult *tr;
|
|
|
|
List *l;
|
|
|
|
Fixture *f;
|
|
|
|
int rval = 1;
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
set_fork_status (CK_NOFORK);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
l = tc->unch_sflst;
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
for (list_front (l); !list_at_end (l); list_advance (l)) {
|
|
|
|
send_ctx_info (CK_CTX_SETUP);
|
|
|
|
f = list_val (l);
|
|
|
|
f->fun ();
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
tr = receive_result_info_nofork (tc->name, "unchecked_setup", 0);
|
|
|
|
|
|
|
|
if (tr->rtype != CK_PASS) {
|
2009-10-12 11:49:35 +00:00
|
|
|
srunner_add_failure (sr, tr);
|
2009-07-16 17:39:16 +00:00
|
|
|
rval = 0;
|
|
|
|
break;
|
|
|
|
}
|
2009-10-12 11:49:35 +00:00
|
|
|
free (tr->file);
|
|
|
|
free (tr->msg);
|
|
|
|
free (tr);
|
|
|
|
}
|
2009-07-16 17:39:16 +00:00
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
set_fork_status (srunner_fork_status (sr));
|
2009-07-16 17:39:16 +00:00
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static TestResult *
|
|
|
|
tcase_run_checked_setup (SRunner * sr, TCase * tc)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
TestResult *tr = NULL;
|
|
|
|
List *l;
|
|
|
|
Fixture *f;
|
2009-10-12 11:49:35 +00:00
|
|
|
enum fork_status fstat = srunner_fork_status (sr);
|
|
|
|
|
2009-07-16 17:39:16 +00:00
|
|
|
l = tc->ch_sflst;
|
|
|
|
if (fstat == CK_FORK) {
|
2009-10-12 11:49:35 +00:00
|
|
|
send_ctx_info (CK_CTX_SETUP);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
2009-10-12 11:49:35 +00:00
|
|
|
|
|
|
|
for (list_front (l); !list_at_end (l); list_advance (l)) {
|
2009-07-16 17:39:16 +00:00
|
|
|
if (fstat == CK_NOFORK) {
|
2009-10-12 11:49:35 +00:00
|
|
|
send_ctx_info (CK_CTX_SETUP);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
2009-10-12 11:49:35 +00:00
|
|
|
f = list_val (l);
|
|
|
|
f->fun ();
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
/* Stop the setup and return the failure if nofork mode. */
|
|
|
|
if (fstat == CK_NOFORK) {
|
|
|
|
tr = receive_result_info_nofork (tc->name, "checked_setup", 0);
|
|
|
|
if (tr->rtype != CK_PASS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
free (tr->file);
|
|
|
|
free (tr->msg);
|
|
|
|
free (tr);
|
2009-07-16 17:39:16 +00:00
|
|
|
tr = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tr;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
tcase_run_checked_teardown (TCase * tc)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
List *l;
|
|
|
|
Fixture *f;
|
|
|
|
|
|
|
|
l = tc->ch_tflst;
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
send_ctx_info (CK_CTX_TEARDOWN);
|
|
|
|
|
|
|
|
for (list_front (l); !list_at_end (l); list_advance (l)) {
|
|
|
|
f = list_val (l);
|
|
|
|
f->fun ();
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
srunner_run_unchecked_teardown (SRunner * sr, TCase * tc)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
List *l;
|
|
|
|
Fixture *f;
|
2009-10-12 11:49:35 +00:00
|
|
|
|
|
|
|
set_fork_status (CK_NOFORK);
|
2009-07-16 17:39:16 +00:00
|
|
|
l = tc->unch_tflst;
|
2009-10-12 11:49:35 +00:00
|
|
|
|
|
|
|
for (list_front (l); !list_at_end (l); list_advance (l)) {
|
|
|
|
|
|
|
|
f = list_val (l);
|
|
|
|
send_ctx_info (CK_CTX_TEARDOWN);
|
2009-07-16 17:39:16 +00:00
|
|
|
f->fun ();
|
|
|
|
}
|
2009-10-12 11:49:35 +00:00
|
|
|
set_fork_status (srunner_fork_status (sr));
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
srunner_run_tcase (SRunner * sr, TCase * tc)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
2009-10-12 11:49:35 +00:00
|
|
|
if (srunner_run_unchecked_setup (sr, tc)) {
|
|
|
|
srunner_iterate_tcase_tfuns (sr, tc);
|
|
|
|
srunner_run_unchecked_teardown (sr, tc);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static TestResult *
|
|
|
|
receive_result_info_fork (const char *tcname,
|
|
|
|
const char *tname, int iter, int status, int expected_signal)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
TestResult *tr;
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
tr = receive_test_result (waserror (status, expected_signal));
|
2009-07-16 17:39:16 +00:00
|
|
|
if (tr == NULL)
|
2009-10-12 11:49:35 +00:00
|
|
|
eprintf ("Failed to receive test result", __FILE__, __LINE__);
|
2009-07-16 17:39:16 +00:00
|
|
|
tr->tcname = tcname;
|
|
|
|
tr->tname = tname;
|
|
|
|
tr->iter = iter;
|
2009-10-12 11:49:35 +00:00
|
|
|
set_fork_info (tr, status, expected_signal);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
return tr;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static TestResult *
|
|
|
|
receive_result_info_nofork (const char *tcname, const char *tname, int iter)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
TestResult *tr;
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
tr = receive_test_result (0);
|
2009-07-16 17:39:16 +00:00
|
|
|
if (tr == NULL)
|
2009-10-12 11:49:35 +00:00
|
|
|
eprintf ("Failed to receive test result", __FILE__, __LINE__);
|
2009-07-16 17:39:16 +00:00
|
|
|
tr->tcname = tcname;
|
|
|
|
tr->tname = tname;
|
|
|
|
tr->iter = iter;
|
2009-10-12 11:49:35 +00:00
|
|
|
set_nofork_info (tr);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
return tr;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
set_fork_info (TestResult * tr, int status, int signal_expected)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
2009-10-12 11:49:35 +00:00
|
|
|
int was_sig = WIFSIGNALED (status);
|
|
|
|
int was_exit = WIFEXITED (status);
|
|
|
|
int exit_status = WEXITSTATUS (status);
|
|
|
|
int signal_received = WTERMSIG (status);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
if (was_sig) {
|
|
|
|
if (signal_expected == signal_received) {
|
|
|
|
if (alarm_received) {
|
|
|
|
/* Got alarm instead of signal */
|
|
|
|
tr->rtype = CK_ERROR;
|
2009-10-12 11:49:35 +00:00
|
|
|
tr->msg = signal_error_msg (signal_received, signal_expected);
|
2009-07-16 17:39:16 +00:00
|
|
|
} else {
|
|
|
|
tr->rtype = CK_PASS;
|
2009-10-12 11:49:35 +00:00
|
|
|
tr->msg = pass_msg ();
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
} else if (signal_expected != 0) {
|
|
|
|
/* signal received, but not the expected one */
|
|
|
|
tr->rtype = CK_ERROR;
|
2009-10-12 11:49:35 +00:00
|
|
|
tr->msg = signal_error_msg (signal_received, signal_expected);
|
2009-07-16 17:39:16 +00:00
|
|
|
} else {
|
|
|
|
/* signal received and none expected */
|
|
|
|
tr->rtype = CK_ERROR;
|
2009-10-12 11:49:35 +00:00
|
|
|
tr->msg = signal_msg (signal_received);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
} else if (signal_expected == 0) {
|
|
|
|
if (was_exit && exit_status == 0) {
|
|
|
|
tr->rtype = CK_PASS;
|
2009-10-12 11:49:35 +00:00
|
|
|
tr->msg = pass_msg ();
|
2009-07-16 17:39:16 +00:00
|
|
|
} else if (was_exit && exit_status != 0) {
|
2009-10-12 11:49:35 +00:00
|
|
|
if (tr->msg == NULL) { /* early exit */
|
2009-07-16 17:39:16 +00:00
|
|
|
tr->rtype = CK_ERROR;
|
2009-10-12 11:49:35 +00:00
|
|
|
tr->msg = exit_msg (exit_status);
|
2009-07-16 17:39:16 +00:00
|
|
|
} else {
|
|
|
|
tr->rtype = CK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2009-10-12 11:49:35 +00:00
|
|
|
} else { /* a signal was expected and none raised */
|
2009-07-16 17:39:16 +00:00
|
|
|
if (was_exit) {
|
2009-10-12 11:49:35 +00:00
|
|
|
tr->msg = exit_msg (exit_status);
|
2009-07-16 17:39:16 +00:00
|
|
|
if (exit_status == 0)
|
2009-10-12 11:49:35 +00:00
|
|
|
tr->rtype = CK_FAILURE; /* normal exit status */
|
2009-07-16 17:39:16 +00:00
|
|
|
else
|
2009-10-12 11:49:35 +00:00
|
|
|
tr->rtype = CK_FAILURE; /* early exit */
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
set_nofork_info (TestResult * tr)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (tr->msg == NULL) {
|
|
|
|
tr->rtype = CK_PASS;
|
2009-10-12 11:49:35 +00:00
|
|
|
tr->msg = pass_msg ();
|
2009-07-16 17:39:16 +00:00
|
|
|
} else {
|
|
|
|
tr->rtype = CK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static TestResult *
|
|
|
|
tcase_run_tfun_nofork (SRunner * sr, TCase * tc, TF * tfun, int i)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
TestResult *tr;
|
2009-10-12 11:49:35 +00:00
|
|
|
|
|
|
|
tr = tcase_run_checked_setup (sr, tc);
|
2009-07-16 17:39:16 +00:00
|
|
|
if (tr == NULL) {
|
2009-10-12 11:49:35 +00:00
|
|
|
tfun->fn (i);
|
|
|
|
tcase_run_checked_teardown (tc);
|
|
|
|
return receive_result_info_nofork (tc->name, tfun->name, i);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
2009-10-12 11:49:35 +00:00
|
|
|
|
2009-07-16 17:39:16 +00:00
|
|
|
return tr;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
|
|
|
|
static TestResult *
|
|
|
|
tcase_run_tfun_fork (SRunner * sr, TCase * tc, TF * tfun, int i)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
pid_t pid_w;
|
|
|
|
pid_t pid;
|
|
|
|
int status = 0;
|
2009-10-12 11:49:35 +00:00
|
|
|
pid = fork ();
|
2009-07-16 17:39:16 +00:00
|
|
|
if (pid == -1)
|
2009-10-12 11:49:35 +00:00
|
|
|
eprintf ("Error in call to fork:", __FILE__, __LINE__ - 2);
|
2009-07-16 17:39:16 +00:00
|
|
|
if (pid == 0) {
|
2009-10-12 11:49:35 +00:00
|
|
|
setpgid (0, 0);
|
|
|
|
group_pid = getpgrp ();
|
|
|
|
tcase_run_checked_setup (sr, tc);
|
|
|
|
tfun->fn (i);
|
|
|
|
tcase_run_checked_teardown (tc);
|
|
|
|
exit (EXIT_SUCCESS);
|
2009-07-16 17:39:16 +00:00
|
|
|
} else {
|
|
|
|
group_pid = pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
alarm_received = 0;
|
2009-10-12 11:49:35 +00:00
|
|
|
alarm (tc->timeout);
|
2009-07-16 17:39:16 +00:00
|
|
|
do {
|
2009-10-12 11:49:35 +00:00
|
|
|
pid_w = waitpid (pid, &status, 0);
|
2009-07-16 17:39:16 +00:00
|
|
|
} while (pid_w == -1);
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
killpg (pid, SIGKILL); /* Kill remaining processes. */
|
|
|
|
|
|
|
|
return receive_result_info_fork (tc->name, tfun->name, i, status,
|
|
|
|
tfun->signal);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static char *
|
|
|
|
signal_error_msg (int signal_received, int signal_expected)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
char *sig_r_str;
|
|
|
|
char *sig_e_str;
|
2009-10-12 11:49:35 +00:00
|
|
|
char *msg = emalloc (MSG_LEN); /* free'd by caller */
|
|
|
|
sig_r_str = strdup (strsignal (signal_received));
|
|
|
|
sig_e_str = strdup (strsignal (signal_expected));
|
2009-07-16 17:39:16 +00:00
|
|
|
if (alarm_received) {
|
|
|
|
snprintf (msg, MSG_LEN, "Test timeout expired, expected signal %d (%s)",
|
2009-10-12 11:49:35 +00:00
|
|
|
signal_expected, sig_e_str);
|
2009-07-16 17:39:16 +00:00
|
|
|
} else {
|
|
|
|
snprintf (msg, MSG_LEN, "Received signal %d (%s), expected %d (%s)",
|
2009-10-12 11:49:35 +00:00
|
|
|
signal_received, sig_r_str, signal_expected, sig_e_str);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
2009-10-12 11:49:35 +00:00
|
|
|
free (sig_r_str);
|
|
|
|
free (sig_e_str);
|
2009-07-16 17:39:16 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static char *
|
|
|
|
signal_msg (int signal)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
2009-10-12 11:49:35 +00:00
|
|
|
char *msg = emalloc (MSG_LEN); /* free'd by caller */
|
2009-07-16 17:39:16 +00:00
|
|
|
if (alarm_received) {
|
2009-10-12 11:49:35 +00:00
|
|
|
snprintf (msg, MSG_LEN, "Test timeout expired");
|
2009-07-16 17:39:16 +00:00
|
|
|
} else {
|
2009-10-12 11:49:35 +00:00
|
|
|
snprintf (msg, MSG_LEN, "Received signal %d (%s)",
|
|
|
|
signal, strsignal (signal));
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static char *
|
|
|
|
exit_msg (int exitval)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
2009-10-12 11:49:35 +00:00
|
|
|
char *msg = emalloc (MSG_LEN); /* free'd by caller */
|
|
|
|
snprintf (msg, MSG_LEN, "Early exit with return value %d", exitval);
|
2009-07-16 17:39:16 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static char *
|
|
|
|
pass_msg (void)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
2009-10-12 11:49:35 +00:00
|
|
|
char *msg = emalloc (sizeof ("Passed"));
|
2009-07-16 17:39:16 +00:00
|
|
|
strcpy (msg, "Passed");
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
enum fork_status
|
|
|
|
srunner_fork_status (SRunner * sr)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (sr->fstat == CK_FORK_GETENV) {
|
|
|
|
char *env = getenv ("CK_FORK");
|
|
|
|
if (env == NULL)
|
|
|
|
return CK_FORK;
|
2009-10-12 11:49:35 +00:00
|
|
|
if (strcmp (env, "no") == 0)
|
2009-07-16 17:39:16 +00:00
|
|
|
return CK_NOFORK;
|
|
|
|
else
|
|
|
|
return CK_FORK;
|
|
|
|
} else
|
|
|
|
return sr->fstat;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
|
|
|
srunner_set_fork_status (SRunner * sr, enum fork_status fstat)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
sr->fstat = fstat;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
pid_t
|
|
|
|
check_fork (void)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
2009-10-12 11:49:35 +00:00
|
|
|
pid_t pid = fork ();
|
2009-07-16 17:39:16 +00:00
|
|
|
/* Set the process to a process group to be able to kill it easily. */
|
2009-10-12 11:49:35 +00:00
|
|
|
setpgid (pid, group_pid);
|
2009-07-16 17:39:16 +00:00
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
|
|
|
check_waitpid_and_exit (pid_t pid)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
pid_t pid_w;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (pid > 0) {
|
|
|
|
do {
|
2009-10-12 11:49:35 +00:00
|
|
|
pid_w = waitpid (pid, &status, 0);
|
2009-07-16 17:39:16 +00:00
|
|
|
} while (pid_w == -1);
|
2009-10-12 11:49:35 +00:00
|
|
|
if (waserror (status, 0))
|
|
|
|
exit (EXIT_FAILURE);
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
2009-10-12 11:49:35 +00:00
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
2009-07-16 17:39:16 +00:00
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static int
|
|
|
|
waserror (int status, int signal_expected)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
int was_sig = WIFSIGNALED (status);
|
|
|
|
int was_exit = WIFEXITED (status);
|
|
|
|
int exit_status = WEXITSTATUS (status);
|
2009-10-12 11:49:35 +00:00
|
|
|
int signal_received = WTERMSIG (status);
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
return ((was_sig && (signal_received != signal_expected)) ||
|
2009-10-12 11:49:35 +00:00
|
|
|
(was_exit && exit_status != 0));
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|