void tcase_fn_start (char *fname, char *file, int line)
{
- int msqid;
+ MsgSys *msgsys;
- msqid = get_send_msq();
- send_last_loc_msg (msqid, file, line);
+ msgsys = get_send_msgsys();
+ send_last_loc_msg (msgsys, file, line);
}
void _mark_point (char *file, int line)
{
- int msqid;
+ MsgSys *msgsys;
- msqid = get_send_msq();
- send_last_loc_msg (msqid, file, line);
+ msgsys = get_send_msgsys();
+ send_last_loc_msg (msgsys, file, line);
}
void _fail_unless (int result, char *file, int line, char * msg)
{
- int msqid;
+ MsgSys *msgsys;
- msqid = get_send_msq();
+ msgsys = get_send_msgsys();
if (line > MAXLINE)
eprintf ("Line number %d too large to use", line);
- send_last_loc_msg (msqid, file, line);
+ send_last_loc_msg (msgsys, file, line);
if (!result) {
- send_failure_msg (msqid, msg);
+ send_failure_msg (msgsys, msg);
exit(1);
}
}
#include "check.h"
#include "check_impl.h"
#include "check_msg.h"
-#include "check_run.h"
+
+
+typedef struct LastLocMsg {
+ long int message_type;
+ char msg[CMAXMSG]; /* Format: filename\nlineno\0 */
+} LastLocMsg;
+
+typedef struct FailureMsg {
+ long int message_type;
+ char msg[CMAXMSG];
+} FailureMsg;
+
+struct MsgSys
+{
+ int msqid;
+};
enum {
LASTLOCMSG = 1,
FAILUREMSG = 2
};
static LastLocMsg *create_last_loc_msg (char *file, int line);
+static char *last_loc_file (LastLocMsg *msg);
+static int last_loc_line (LastLocMsg *msg);
static FailureMsg *create_failure_msg (char *msg);
-static int get_msq (key_t key);
static char *ipcerrstr (int ipcerr);
+static int init_key (void);
+static int send_key (void);
+static int recv_key (void);
+
static FailureMsg *create_failure_msg (char *msg)
{
return m;
}
-char *last_loc_file (LastLocMsg *msg)
+static char *last_loc_file (LastLocMsg *msg)
{
int i;
char *rmsg = emalloc (CMAXMSG); /* caller responsible for freeing */
return rmsg;
}
-int last_loc_line (LastLocMsg *msg)
+static int last_loc_line (LastLocMsg *msg)
{
char *rmsg;
if (msg == NULL)
}
-void send_last_loc_msg (int msqid, char * file, int line)
+void send_last_loc_msg (MsgSys *msgsys, char *file, int line)
{
int rval;
LastLocMsg *rmsg = create_last_loc_msg(file, line);
- rval = msgsnd(msqid, (void *) rmsg, CMAXMSG, IPC_NOWAIT);
+ rval = msgsnd(msgsys->msqid, (void *) rmsg, CMAXMSG, IPC_NOWAIT);
if (rval == -1) {
- eprintf ("send_last_loc_msg:Failed to send message, msqid = %d:",msqid);
+ eprintf ("send_last_loc_msg:Failed to send message, msqid = %d:",
+ msgsys->msqid);
}
free(rmsg);
}
-int init_msq (void) {
- int msqid;
+MsgSys *init_msgsys (void) {
+ return create_msgsys_with_key(init_key());
+}
- msqid = msgget(init_key(), 0666 | IPC_CREAT);
- if (msqid == -1)
- eprintf ("Unable to create message queue (%s):", ipcerrstr(errno));
- return msqid;
+MsgSys *get_recv_msgsys (void)
+{
+ return get_msgsys_with_key (recv_key());
}
-int get_recv_msq (void)
+MsgSys *get_send_msgsys (void)
{
- return get_msq ((key_t) recv_key());
+ return get_msgsys_with_key (send_key());
}
-int get_send_msq (void)
+MsgSys *create_msgsys_with_key (int key)
{
- return get_msq ((key_t) send_key());
+ MsgSys *msgsys;
+
+ msgsys = emalloc(sizeof(MsgSys));
+
+ msgsys->msqid = msgget((key_t) key, 0666 | IPC_CREAT);
+ if (msgsys->msqid == -1)
+ eprintf ("Unable to create message queue (%s):", ipcerrstr(errno));
+ return msgsys;
}
-static int get_msq (key_t key)
+MsgSys *get_msgsys_with_key (int key)
{
- int msqid;
+ MsgSys *msgsys;
+
+ msgsys = emalloc(sizeof(MsgSys));
- msqid = msgget (key, 0666);
- if (msqid == -1)
+ msgsys->msqid = msgget ((key_t) key, 0666);
+ if (msgsys->msqid == -1)
eprintf ("Unable to get message queue (%s):", ipcerrstr(errno));
- return msqid;
+ return msgsys;
}
-void delete_msq (void)
+void delete_msgsys_with_key (int key)
{
- int msqid;
+ MsgSys *msgsys;
- msqid = get_msq((key_t) init_key());
- if (msgctl (msqid, IPC_RMID, NULL) == -1)
+ msgsys = get_msgsys_with_key ((key_t) key);
+ if (msgctl (msgsys->msqid, IPC_RMID, NULL) == -1)
eprintf ("Failed to free message queue:");
+ free(msgsys);
}
+void delete_msgsys (void)
+{
+ delete_msgsys_with_key(init_key());
+}
-void send_failure_msg (int msqid, char *msg)
+
+void send_failure_msg (MsgSys *msgsys, char *msg)
{
int rval;
FailureMsg *rmsg = create_failure_msg(msg);
- rval = msgsnd(msqid, (void *) rmsg, CMAXMSG, IPC_NOWAIT);
+ rval = msgsnd(msgsys->msqid, (void *) rmsg, CMAXMSG, IPC_NOWAIT);
if (rval == -1)
eprintf ("send_failure_msg:Failed to send message:");
free(rmsg);
}
-LastLocMsg *receive_last_loc_msg (int msqid)
+Loc *receive_last_loc_msg (MsgSys *msgsys)
{
- LastLocMsg *rmsg = emalloc(sizeof(LastLocMsg)); /* caller responsible for freeing */
+ LastLocMsg *lmsg;
+ Loc *loc;
+
+ lmsg = emalloc(sizeof(LastLocMsg)); /* caller responsible for freeing */
while (1) {
int rval;
- rval = msgrcv(msqid, (void *) rmsg, CMAXMSG, LASTLOCMSG, IPC_NOWAIT);
+ rval = msgrcv(msgsys->msqid,
+ (void *) lmsg, CMAXMSG, LASTLOCMSG, IPC_NOWAIT);
if (rval == -1) {
if (errno == ENOMSG)
break;
eprintf ("receive_last_loc_msg:Failed to receive message:");
}
}
- return rmsg;
+ loc = emalloc(sizeof(Loc));
+ loc->file = last_loc_file(lmsg);
+ loc->line = last_loc_line(lmsg);
+ free(lmsg);
+
+ return loc;
}
-FailureMsg *receive_failure_msg (int msqid)
+char *receive_failure_msg (MsgSys *msgsys)
{
- FailureMsg *rmsg = emalloc(sizeof(FailureMsg));
+ FailureMsg *fmsg;
+ char *msg;
int rval;
- rval = msgrcv(msqid, (void *) rmsg, CMAXMSG, FAILUREMSG, IPC_NOWAIT);
+
+ fmsg = emalloc(sizeof(FailureMsg));
+ rval = msgrcv(msgsys->msqid,
+ (void *) fmsg, CMAXMSG, FAILUREMSG, IPC_NOWAIT);
if (rval == -1) {
if (errno == ENOMSG)
return NULL;
eprintf ("receive_failure_msg:Failed to receive message:");
}
- return rmsg;
+ msg = emalloc(strlen(fmsg->msg) + 1);
+ strcpy(msg,fmsg->msg);
+ free(fmsg);
+ return msg;
}
int init_key(void)
/* Functions implementing messaging during test runs */
/* check.h must be included before this header */
+/* Abstract type for a messaging system
+ Hides the details (IPC msg vs. pipes vs...*/
+typedef struct MsgSys MsgSys;
-typedef struct LastLocMsg {
- long int message_type;
- char msg[CMAXMSG]; /* Format: filename\nlineno\0 */
-} LastLocMsg;
+typedef struct loc
+{
+ int line;
+ char *file;
+} Loc;
-typedef struct FailureMsg {
- long int message_type;
- char msg[CMAXMSG];
-} FailureMsg;
-
-
-void send_failure_msg (int msqid, char *msg);
-void send_last_loc_msg (int msqid, char * file, int line);
+void send_failure_msg (MsgSys *msgsys, char *fmsg);
+void send_last_loc_msg (MsgSys *msgsys, char * file, int line);
/* malloc'd return value which caller is responsible for
freeing in each of the next two functions */
-FailureMsg *receive_failure_msg (int msqid);
-LastLocMsg *receive_last_loc_msg (int msqid);
-
-/* file name contained in the LastLocMsg */
-/* return value is malloc'd, caller responsible for freeing */
-char *last_loc_file(LastLocMsg *msg);
-int last_loc_line(LastLocMsg *msg);
-
-int init_msq (void);
-void delete_msq (void);
-
-int get_recv_msq (void);
-int get_send_msq (void);
-int init_key (void);
-int send_key (void);
-int recv_key (void);
+char *receive_failure_msg (MsgSys *msgsys);
+Loc *receive_last_loc_msg (MsgSys *msgsys);
+
+
+MsgSys *init_msgsys (void);
+void delete_msgsys (void);
+
+MsgSys *get_recv_msgsys (void);
+MsgSys *get_send_msgsys (void);
+
+/* Used externally only for testing */
+MsgSys *create_msgsys_with_key(int key);
+void delete_msgsys_with_key(int key);
+MsgSys *get_msgsys_with_key(int key);
#endif /*CHECK_MSG_H */
#include "check_impl.h"
#include "check_msg.h"
#include "check_log.h"
-#include "check_run.h"
static void srunner_run_init (SRunner *sr, enum print_verbosity print_mode);
static void srunner_run_tcase (SRunner *sr, TCase *tc);
static void srunner_add_failure (SRunner *sr, TestResult *tf);
static TestResult *tfun_run (char *tcname, TF *tf);
-static TestResult *receive_result_info (int msqid, int status, char *tcname);
-static void receive_last_loc_info (int msqid, TestResult *tr);
-static void receive_failure_info (int msqid, int status, TestResult *tr);
+static TestResult *receive_result_info (MsgSys *msgsys, int status, char *tcname);
+static void receive_last_loc_info (MsgSys *msgsys, TestResult *tr);
+static void receive_failure_info (MsgSys *msgsys, int status, TestResult *tr);
static List *srunner_resultlst (SRunner *sr);
static char *signal_msg (int sig);
List *tfl;
TF *tfun;
TestResult *tr;
- int msqid;
+ MsgSys *msgsys;
- msqid = init_msq();
+ msgsys = init_msgsys();
if (tc->setup)
tc->setup();
}
if (tc->teardown)
tc->teardown();
- delete_msq();
+ delete_msgsys();
}
-static void receive_last_loc_info (int msqid, TestResult *tr)
+static void receive_last_loc_info (MsgSys *msgsys, TestResult *tr)
{
- LastLocMsg *lmsg;
- lmsg = receive_last_loc_msg (msqid);
- tr->file = last_loc_file (lmsg);
- tr->line = last_loc_line (lmsg);
- free (lmsg);
+ Loc *loc;
+ loc = receive_last_loc_msg (msgsys);
+ tr->file = loc->file;
+ tr->line = loc->line;
+ free (loc);
}
-static void receive_failure_info (int msqid, int status, TestResult *tr)
+static void receive_failure_info (MsgSys *msgsys, int status, TestResult *tr)
{
- FailureMsg *fmsg;
+ char *fmsg;
if (WIFSIGNALED(status)) {
tr->rtype = CRERROR;
}
else {
- fmsg = receive_failure_msg (msqid);
+ fmsg = receive_failure_msg (msgsys);
if (fmsg == NULL) { /* implies early exit */
tr->rtype = CRERROR;
tr->msg = exit_msg (WEXITSTATUS(status));
}
else {
tr->rtype = CRFAILURE;
- tr->msg = emalloc(strlen(fmsg->msg) + 1);
- strcpy (tr->msg, fmsg->msg);
+ tr->msg = emalloc(strlen(fmsg) + 1);
+ strcpy (tr->msg, fmsg);
free (fmsg);
}
}
}
}
-static TestResult *receive_result_info (int msqid, int status, char *tcname)
+static TestResult *receive_result_info (MsgSys *msgsys, int status, char *tcname)
{
TestResult *tr = emalloc (sizeof(TestResult));
- msqid = get_recv_msq();
+ msgsys = get_recv_msgsys();
tr->tcname = tcname;
- receive_last_loc_info (msqid, tr);
- receive_failure_info (msqid, status, tr);
+ receive_last_loc_info (msgsys, tr);
+ receive_failure_info (msgsys, status, tr);
return tr;
}
{
pid_t pid;
int status = 0;
- int msqid;
+ MsgSys *msgsys;
- msqid = get_recv_msq();
+ msgsys = get_recv_msgsys();
pid = fork();
if (pid == -1)
_exit(EXIT_SUCCESS);
}
(void) wait(&status);
- return receive_result_info(msqid, status, tcname);
+ return receive_result_info(msgsys, status, tcname);
}
int srunner_ntests_failed (SRunner *sr)
#include "check_msg.h"
#include "check_check.h"
-int msq;
+MsgSys *msgsys;
+
+enum {
+ KEY = 1
+};
+
static void msg_setup (void)
{
- msq = msgget(1, 0666 | IPC_CREAT);
- if (msq == -1)
- eprintf ("Unable to create message queue (%s):");
+ msgsys = create_msgsys_with_key(KEY);
}
static void msg_teardown (void)
{
- if (msgctl (msq, IPC_RMID, NULL) == -1)
- eprintf ("Failed to free message queue:");
+ delete_msgsys_with_key(KEY);
}
START_TEST(test_send_failure)
{
- FailureMsg *fmsg;
+ char *fmsg;
/* Failure message sending and receiving need to be in equal pairs
otherwise the world may fall off its axis...*/
- send_failure_msg (msq, "This is a test");
- fmsg = receive_failure_msg (msq);
- fail_unless (strcmp (fmsg->msg, "This is a test") == 0,
+ send_failure_msg (msgsys, "This is a test");
+ fmsg = receive_failure_msg (msgsys);
+ fail_unless (strcmp (fmsg, "This is a test") == 0,
"Didn't receive the correct message");
free(fmsg);
}
START_TEST(test_send_lastloc)
{
- LastLocMsg *lmsg;
- char *file;
+ Loc *loc;
- send_last_loc_msg (msq, "abc", 1);
- send_last_loc_msg (msq, "def", 2);
- lmsg = receive_last_loc_msg (msq);
- file = last_loc_file (lmsg);
- fail_unless (strcmp (file, "def") == 0,
+ send_last_loc_msg (msgsys, "abc", 1);
+ send_last_loc_msg (msgsys, "def", 2);
+ loc = receive_last_loc_msg (msgsys);
+ fail_unless (strcmp (loc->file, "def") == 0,
"Didn't receive the correct file name");
- fail_unless (last_loc_line(lmsg) == 2,
+ fail_unless (loc->line == 2,
"Didn't receive the correct line number");
- free(file);
- free(lmsg);
+ free(loc);
}
END_TEST