summaryrefslogtreecommitdiff
path: root/src/testing/mock_spawn_command.c
blob: e6c4dde98eddf307d92fa3d9b892abb6777e5936 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*++
/* NAME
/*	mock_spawn_command 3t
/* SUMMARY
/*	spawn_command() mocker
/* SYNOPSIS
/*	#include <mock_spawn_command.h>
/*
/*	typedef struct MOCK_SPAWN_CMD_REQ {
/*	struct spawn_args *want_args;
/*	int	out_status;
/*	const char *out_text;
P/*
/*	void	setup_mock_spawn_command(const MOCK_SPAWN_CMD_REQ *request)
/* DESCRIPTION
/*	setup_mock_spawn_command() instantiates a spawn_command() mocker,
/*	that given the expected inputs, will produce the specified
/*	outputs, without any additional dependencies or side effects.
/*
/*	This implementation supports only one instance of expectations
/*	and outputs, and makes a shallow copy of its inputs:
/* .IP want_args
/*	The inputs that the mock spawn_command() instance must
/*	receive. Otherwise, The mocker will terminate the test.
/* .IP out_status
/*	The spawn_command() result value.
/* .IP out_text
/*	Pointer to null-terminated text that will be written to the
/*	simulated command's standard error stream.
/* SEE ALSO
/*	spawn_command(3), command spawner
/* 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 <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>

 /*
  * Utility library.
  */
#include <msg.h>
#include <stringops.h>

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

 /*
  * Input expectations and outputs. For now we support only one instance.
  */
static MOCK_SPAWN_CMD_REQ mock_req;

void    setup_mock_spawn_command(const MOCK_SPAWN_CMD_REQ *request)
{

    /*
     * Validate the output requirements.
     */
    if (request->out_status == 0) {
	if (request->out_text != 0)
	    msg_fatal("setup_spawn_command: out_status is zero, but out_text "
		      "is not null");
    } else {
	if (request->out_text == 0 || *request->out_text == 0) {
	    msg_fatal("setup_spawn_command: out_status is not zero, but "
		      "out_text is null or empty");
	}
    }

    /*
     * Make a shallow copy of the arguments.
     */
    mock_req = *request;
}

 /*
  * Matchers. TODO(wietse)  factor out.
  */
typedef void PRINTFLIKE(1, 2) (*MATCH_WARN) (const char *,...);

#define STR_OR_NULL(s)          ((s) ? (s) : "(null)")
#define NOT_NULL_OR_NULL(arg)   ((arg) ? "(not null)" : "(null)")

/* match_decimal - match integers, report difference in decimal */

static bool match_decimal(const char *what, MATCH_WARN warn, int want, int got)
{
    if (want != got) {
	if (warn)
	    warn("%s: want %d, got %d", what, want, got);
	return (false);
    }
    return (true);
}

/* match_str - match null-terminated strings */

static int match_str(const char *what, MATCH_WARN warn,
		             const char *want, const char *got)
{
    if (want == 0 && got == 0)
	return (true);
    if (!want != !got) {
	if (warn)
	    warn("%s: want '%s', got '%s'", what, STR_OR_NULL(want),
		 STR_OR_NULL(got));
	return (false);
    }
    if (strcmp(got, want) != 0) {
	if (warn)
	    warn("%s: want %s, got %s", what, want, got);
	return (false);
    }
    return (true);
}

/* match_cpp - match (char **) instances */

static bool match_cpp(const char *what, MATCH_WARN warn,
		              char **want, char **got)
{
    char  **wpp, **gpp;

    if (want == 0 && got == 0)
	return (true);
    if (!want != !got) {
	if (warn)
	    warn("%s: want %s, got %s", what, NOT_NULL_OR_NULL(want),
		 NOT_NULL_OR_NULL(got));
	return (false);
    }
    for (wpp = want, gpp = got; /* void */ ; wpp++, gpp++) {
	if (*wpp == 0 && *gpp == 0) {
	    return (true);
	} else if (*wpp == 0 || *gpp == 0) {
	    if (warn)
		warn("%s: want '%s', got '%s'",
		     what, STR_OR_NULL(*wpp), STR_OR_NULL(*gpp));
	    return (false);
	} else if (!match_str(what, warn, *gpp, *wpp)) {
	    return (false);
	}
    }
    return (true);
}

/* match_spawn_args - compare expected against actual */

static bool match_spawn_args(MATCH_WARN warn, struct spawn_args *want,
			             struct spawn_args *got)
{
    return (match_cpp("argv", warn, want->argv, got->argv)
	    && match_str("command", warn,
			 want->command, got->command)
	    && match_decimal("uid", warn, want->uid, got->uid)
	    && match_cpp("env", warn, want->env, got->env)
	    && match_cpp("export", warn,
			 want->export, got->export)
	    && match_str("shell", warn, want->shell, got->shell)
	    && match_decimal("time_limit", warn,
			     want->time_limit, got->time_limit));
}

/* spawn_command - mock */

int     spawn_command(int key,...)
{
    struct spawn_args got_args;
    va_list ap;

    va_start(ap, key);
    get_spawn_args(&got_args, key, ap);
    va_end(ap);

    /*
     * Match spawn_command() arguments against expectations or terminate.
     */
    if (!match_spawn_args(msg_warn, &mock_req.want_args, &got_args))
	msg_fatal("spawn_command called with unexpected argument");

    /* Now output some info as if the command was executed. */
    if (mock_req.out_status != 0)
	write(got_args.stderr_fd, mock_req.out_text, strlen(mock_req.out_text));
    return (mock_req.out_status);
}