summaryrefslogtreecommitdiff
path: root/src/testing/msg_capture.c
blob: 38e7c38f13aa41406b2dd29ff898bf8b663b560a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*++
/* NAME
/*	msg_capture 3
/* SUMMARY
/*	message capture support
/* SYNOPSIS
/*	#include <msg_capture.h>
/*
/*	typedef struct MSG_CAPTURE MSG_CAPTURE;
/*
/*	MSG_CAPTURE *msg_capt_create(ssize_t init_size)
/*
/*	void	msg_capt_start(MSG_CAPTURE *mp)
/*
/*	void	msg_capt_stop(MSG_CAPTURE *mp)
/*
/*	const char *msg_capt_expose(MSG_CAPTURE *mp)
/*
/*	void	msg_capt_free(MSG_CAPTURE *mp)
/* DESCRIPTION
/*	This module captures msg(3) logging by temporarily redirecting
/*	VSTREAM_ER to a buffer.
/*
/*	msg_capt_create() creates a MSG_CAPTURE instance.
/*
/*	msg_capt_start() starts "recording" msg(3) output. It is an
/*	error to call this function while a recording is in progress.
/*
/*	msg_capt_stop() stops a "recording". It is an error to call this
/*	function while no recording is in progress.
/*
/*	msg_capt_expose() exposes the contents of a recording as a
/*	null-terminated string. It is an error to call this function
/*	while a recording is in progress.
/*
/*	msg_capt_free() destroys a MSG_CAPTURE instance.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	porcupine.org
/*--*/

 /*
  * System library.
  */
#include <sys_defs.h>
#include <string.h>

 /*
  * Utility library.
  */
#include <msg.h>
#include <mymalloc.h>
#include <vstream.h>
#include <vstring.h>

 /*
  * Testing library.
  */
#include <msg_capture.h>

 /*
  * TODO(wietse) make this a proper VSTREAM interface or test helper API.
  */

/* vstream_swap - capture output for testing */

static void vstream_swap(VSTREAM *one, VSTREAM *two)
{
    VSTREAM save;

    save = *one;
    *one = *two;
    *two = save;
}

struct MSG_CAPTURE {
    VSTRING *buffer;
    VSTREAM *stream;
};

MSG_CAPTURE *msg_capt_create(ssize_t init_size)
{
    MSG_CAPTURE *mp;

    mp = (MSG_CAPTURE *) mymalloc(sizeof(*mp));
    mp->buffer = vstring_alloc(init_size);
    mp->stream = 0;
    return (mp);
}

void    msg_capt_start(MSG_CAPTURE *mp)
{
    if (mp->stream != 0)
	msg_panic("%s: capture is in progress", __func__);
    VSTRING_RESET(mp->buffer);
    VSTRING_TERMINATE(mp->buffer);
    if ((mp->stream = vstream_memopen(mp->buffer, O_WRONLY)) == 0)
	msg_fatal("open memory stream: %m");
    vstream_swap(VSTREAM_ERR, mp->stream);
}

void    msg_capt_stop(MSG_CAPTURE *mp)
{
    if (mp->stream == 0)
	msg_panic("%s: no capture in progress", __func__);
    vstream_swap(mp->stream, VSTREAM_ERR);
    if (vstream_fclose(mp->stream))
	msg_fatal("close memory stream: %m");
    mp->stream = 0;
    VSTRING_TERMINATE(mp->buffer);
}

const char *msg_capt_expose(MSG_CAPTURE *mp)
{
    if (mp->stream != 0)
	msg_panic("%s: capture is in progress", __func__);
    return (vstring_str(mp->buffer));
}

void    msg_capt_free(MSG_CAPTURE *mp)
{
    if (mp->stream != 0)
	msg_capt_stop(mp);
    vstring_free(mp->buffer);
    myfree((void *) mp);
}