summaryrefslogtreecommitdiff
path: root/src/testing
diff options
context:
space:
mode:
authorbeatrice <beatrice@dangofactory.net>2026-11-22 19:19:03 +0900
committerbeatrice <beatrice@dangofactory.net>2026-11-22 19:19:03 +0900
commitbd4611555e18fca7c66afaef0f094e15038b0459 (patch)
tree1b1a9109567b61ac259ead146f4bef21a0f886df /src/testing
parent2324a9e97a727bf95690c9fb8fdca0f52301940e (diff)
downloadpostfix-master.tar.gz
postfix-master.zip
Release tarball postfix-3.11.7.tar.gz (dist-generated files / local differences from tag)HEADlocal/postfixmaster
Diffstat (limited to 'src/testing')
l---------src/testing/.indent.pro1
-rw-r--r--src/testing/Makefile.in141
-rw-r--r--src/testing/dict_test_helper.c266
-rw-r--r--src/testing/dict_test_helper.h45
-rw-r--r--src/testing/mock_dict.c116
-rw-r--r--src/testing/mock_dict.h27
-rw-r--r--src/testing/mock_open_as.c102
-rw-r--r--src/testing/mock_open_as.h45
-rw-r--r--src/testing/mock_spawn_command.c198
-rw-r--r--src/testing/mock_spawn_command.h39
-rw-r--r--src/testing/mock_stat.c113
-rw-r--r--src/testing/mock_stat.h39
-rw-r--r--src/testing/msg_capture.c130
-rw-r--r--src/testing/msg_capture.h34
-rw-r--r--src/testing/nosleep.c29
15 files changed, 1325 insertions, 0 deletions
diff --git a/src/testing/.indent.pro b/src/testing/.indent.pro
new file mode 120000
index 000000000..5c837eca6
--- /dev/null
+++ b/src/testing/.indent.pro
@@ -0,0 +1 @@
+../../.indent.pro \ No newline at end of file
diff --git a/src/testing/Makefile.in b/src/testing/Makefile.in
new file mode 100644
index 000000000..4534831c4
--- /dev/null
+++ b/src/testing/Makefile.in
@@ -0,0 +1,141 @@
+SHELL = /bin/sh
+SRCS = nosleep.c dict_test_helper.c mock_open_as.c mock_spawn_command.c \
+ mock_stat.c msg_capture.c mock_dict.c
+LIB_OBJ = dict_test_helper.o mock_open_as.o mock_spawn_command.o \
+ mock_stat.o msg_capture.o mock_dict.o
+MOCK_OBJ=
+TEST_OBJ=
+HDRS = dict_test_helper.h mock_open_as.h mock_spawn_command.h \
+ mock_stat.h msg_capture.h mock_dict.h
+TESTSRC =
+DEFS = -I. -I$(INC_DIR) -D$(SYSTYPE)
+CFLAGS = $(DEBUG) $(OPT) $(DEFS)
+INCL =
+LIB = libtesting.a
+LIB_SO = nosleep.so
+TESTPROG=
+
+LIBS = ../../lib/lib$(LIB_PREFIX)global$(LIB_SUFFIX) \
+ ../../lib/lib$(LIB_PREFIX)util$(LIB_SUFFIX)
+LIB_DIR = ../../lib
+INC_DIR = ../../include
+MAKES =
+
+.c.o:; $(CC) $(SHLIB_CFLAGS) $(CFLAGS) -c $*.c
+
+all: $(LIB_SO) $(LIB) $(MOCK_OBJ)
+
+$(LIB_OBJ) $(MOCK_OBJ) $(TEST_OBJ): ../../conf/makedefs.out
+
+Makefile: Makefile.in
+ cat ../../conf/makedefs.out $? >$@
+
+test: $(TESTPROG)
+
+$(LIB): $(LIB_OBJ)
+ $(_AR) $(ARFL) $(LIB) $?
+ $(_RANLIB) $(LIB)
+
+$(LIB_DIR)/$(LIB): $(LIB)
+ cp $(LIB) $(LIB_DIR)
+ $(_RANLIB) $(LIB_DIR)/$(LIB)
+
+nosleep.so: nosleep.c
+ $(CC) $(CFLAGS) -fPIC -shared -o $@ $?
+
+update: lib_so_update lib_update
+
+lib_so_update: $(LIB_SO)
+ for i in $(LIB_SO); do \
+ cmp -s $$i $(LIB_DIR)/$$i 2>/dev/null || cp $$i $(LIB_DIR); \
+ done
+
+lib_update: $(LIB_DIR)/$(LIB) $(HDRS) $(MOCK_OBJ)
+ -for i in $(HDRS); \
+ do \
+ cmp -s $$i $(INC_DIR)/$$i 2>/dev/null || cp $$i $(INC_DIR); \
+ done
+ (cd $(INC_DIR); chmod 644 $(HDRS))
+ -for i in $(MOCK_OBJ); \
+ do \
+ cmp -s $$i $(LIB_DIR)/$$i 2>/dev/null || cp $$i $(LIB_DIR); \
+ done
+
+tests:
+
+clean:
+ rm -f $(LIB_SO) *.o
+
+tidy: clean
+
+depend: $(MAKES)
+ (sed '1,/^# do not edit/!d' Makefile.in; \
+ set -e; for i in [a-z][a-z0-9]*.c; do \
+ $(CC) -E $(DEFS) $(INCL) $$i | grep -v '[<>]' | sed -n -e '/^# *1 *"\([^"]*\)".*/{' \
+ -e 's//'`echo $$i|sed 's/c$$/o/'`': \1/' \
+ -e 's/o: \.\//o: /' -e p -e '}' ; \
+ done | LANG=C sort -u) | grep -v '[.][o][:][ ][/]' >$$$$ && mv $$$$ Makefile.in
+ @$(EXPORT) make -f Makefile.in Makefile 1>&2
+
+# do not edit below this line - it is generated by 'make depend'
+dict_test_helper.o: ../../include/argv.h
+dict_test_helper.o: ../../include/check_arg.h
+dict_test_helper.o: ../../include/dict.h
+dict_test_helper.o: ../../include/msg.h
+dict_test_helper.o: ../../include/myflock.h
+dict_test_helper.o: ../../include/stringops.h
+dict_test_helper.o: ../../include/sys_defs.h
+dict_test_helper.o: ../../include/vbuf.h
+dict_test_helper.o: ../../include/vstream.h
+dict_test_helper.o: ../../include/vstring.h
+dict_test_helper.o: dict_test_helper.c
+dict_test_helper.o: dict_test_helper.h
+mock_dict.o: ../../include/argv.h
+mock_dict.o: ../../include/check_arg.h
+mock_dict.o: ../../include/dict.h
+mock_dict.o: ../../include/dict_cdb.h
+mock_dict.o: ../../include/dict_inline.h
+mock_dict.o: ../../include/dict_lmdb.h
+mock_dict.o: ../../include/mkmap.h
+mock_dict.o: ../../include/myflock.h
+mock_dict.o: ../../include/mymalloc.h
+mock_dict.o: ../../include/sys_defs.h
+mock_dict.o: ../../include/vbuf.h
+mock_dict.o: ../../include/vstream.h
+mock_dict.o: ../../include/vstring.h
+mock_dict.o: mock_dict.c
+mock_dict.o: mock_dict.h
+mock_open_as.o: ../../include/check_arg.h
+mock_open_as.o: ../../include/msg.h
+mock_open_as.o: ../../include/open_as.h
+mock_open_as.o: ../../include/sys_defs.h
+mock_open_as.o: ../../include/vbuf.h
+mock_open_as.o: ../../include/vstream.h
+mock_open_as.o: ../../include/vstring.h
+mock_open_as.o: mock_open_as.c
+mock_open_as.o: mock_open_as.h
+mock_spawn_command.o: ../../include/check_arg.h
+mock_spawn_command.o: ../../include/msg.h
+mock_spawn_command.o: ../../include/spawn_command.h
+mock_spawn_command.o: ../../include/stringops.h
+mock_spawn_command.o: ../../include/sys_defs.h
+mock_spawn_command.o: ../../include/vbuf.h
+mock_spawn_command.o: ../../include/vstring.h
+mock_spawn_command.o: mock_spawn_command.c
+mock_spawn_command.o: mock_spawn_command.h
+mock_stat.o: ../../include/htable.h
+mock_stat.o: ../../include/msg.h
+mock_stat.o: ../../include/sys_defs.h
+mock_stat.o: ../../include/wrap_stat.h
+mock_stat.o: mock_stat.c
+mock_stat.o: mock_stat.h
+msg_capture.o: ../../include/check_arg.h
+msg_capture.o: ../../include/msg.h
+msg_capture.o: ../../include/mymalloc.h
+msg_capture.o: ../../include/sys_defs.h
+msg_capture.o: ../../include/vbuf.h
+msg_capture.o: ../../include/vstream.h
+msg_capture.o: ../../include/vstring.h
+msg_capture.o: msg_capture.c
+msg_capture.o: msg_capture.h
+nosleep.o: nosleep.c
diff --git a/src/testing/dict_test_helper.c b/src/testing/dict_test_helper.c
new file mode 100644
index 000000000..e46c24f1a
--- /dev/null
+++ b/src/testing/dict_test_helper.c
@@ -0,0 +1,266 @@
+/*++
+/* NAME
+/* dict_test_helper 3
+/* SUMMARY
+/* dictionary test helpers
+/* SYNOPSIS
+/* #include <dict_test_helper.h>
+/*
+/* DICT *dict_open_and_capture_msg(
+/* const char *type_name,
+/* int open_flags,
+/* int dict_flags,
+/* VSTRING *out_msg_buf)
+/*
+/* char *dict_compose_spec(
+/* const char *dict_type,
+/* const char **component_specs,
+/* int open_flags,
+/* int dict_flags,
+/* VSTRING *out_composite_spec,
+/* ARGV *out_reg_component_specs)
+/*
+/* chat char *dict_get_and_capture_msg(
+/* DICT *dict,
+/* const char *key,
+/* VSTRING *out_msg_buf)
+/*
+/* struct dict_get_verify_data {
+/* .in +4
+/* const char *key;
+/* const char *want_value;
+/* int want_error;
+/* const char *want_msg;
+/* .in -4
+/* }
+/*
+/* int dict_get_and_verify(
+/* DICT *dict,
+/* const char *key,
+/* const char *want_value,
+/* int want_error,
+/* const char *want_msg)
+/*
+/* int dict_get_and_verify_bulk(
+/* DICT *dict,
+/* const struct dict_get_verify_data *data)
+/* DESCRIPTION
+/* This module contains common code for dictionary tests.
+/*
+/* All functions that capture msg(3) output clear the output
+/* VSTRING buffer first.
+/*
+/* dict_open_and_capture_msg() invokes dict_open() while
+/* capturing msg(3) output to a VSTRING buffer.
+/*
+/* dict_compose_spec() constructs a composite dictionary spec with
+/* the form "\fIdict_type:{component_specs[0],...}\fR". It records in
+/* out_reg_component_specs the names under which the component_specs
+/* dictionaries will be registered with dict_register(), with each
+/* name having the form "\fItype:name(open_flags,dict_flags). The
+/* result value is the out_composite_spec string value.
+/*
+/* dict_get_and_capture_msg() invokes dict_get() while capturing
+/* msg(3) output to a VSTRING buffer.
+/*
+/* dict_get_and_verify() invokes dict_get() and verifies that
+/* expectations are met. The want_value argument requires an exact
+/* match; specify null if the expected lookup result is "not found".
+/* The want_error argument requires an exact match; specify zero
+/* (DICT_ERR_NONE) or one of the other expected DICT_ERR_*
+/* values. The want_msg argument requires a substring match;
+/* specify null if no msg(3) output is expected. The result value
+/* is PASS or FAIL.
+/*
+/* dict_get_and_verify_bulk() provides a convenient interface for
+/* multiple lookup tests.
+/* LICENSE
+/* 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>
+
+ /*
+ * Utility library.
+ */
+#include <argv.h>
+#include <dict.h>
+#include <dict_test_helper.h>
+#include <msg.h>
+#include <stringops.h>
+#include <vstream.h>
+#include <vstring.h>
+
+#define LEN(x) VSTRING_LEN(x)
+#define STR(x) vstring_str(x)
+
+ /*
+ * TODO(wietse) factor this out to common testing header file.
+ */
+#define PASS 1
+#define FAIL 0
+
+ /*
+ * TODO(wietse) make this a proper VSTREAM interface or test helper API.
+ */
+
+/* vstream_swap - capture msg(3) output for testing */
+
+static void vstream_swap(VSTREAM *one, VSTREAM *two)
+{
+ VSTREAM save;
+
+ save = *one;
+ *one = *two;
+ *two = save;
+}
+
+/* dict_open_and_capture_msg - open dictionary and capture msg(3) output */
+
+DICT *dict_open_and_capture_msg(const char *type_name, int open_flags,
+ int dict_flags, VSTRING *out_msg_buf)
+{
+ VSTREAM *memory_stream;
+ DICT *dict;
+
+ VSTRING_RESET(out_msg_buf);
+ VSTRING_TERMINATE(out_msg_buf);
+ if ((memory_stream = vstream_memopen(out_msg_buf, O_WRONLY)) == 0)
+ msg_fatal("open memory stream: %m");
+ vstream_swap(VSTREAM_ERR, memory_stream);
+ dict = dict_open(type_name, open_flags, dict_flags);
+ vstream_swap(memory_stream, VSTREAM_ERR);
+ (void) vstream_fclose(memory_stream);
+ return (dict);
+}
+
+/* dict_compose_spec - compose aggregate spec and component registered specs */
+
+char *dict_compose_spec(const char *dict_type,
+ const char **component_specs,
+ int open_flags, int dict_flags,
+ VSTRING *out_composite_spec,
+ ARGV *out_reg_component_specs)
+{
+ VSTRING *reg_spec = vstring_alloc(100);
+ const char **cpp;
+
+ /*
+ * A dictionary spec is formatted as "type:name", and a dictionary is
+ * registered with dict_register() as "type:name(open_flags,dict_flags)".
+ * The latter form is used to share dictionary instances that have the
+ * exact same properties.
+ *
+ * Build the composite dictionary spec from the dict_type and component
+ * dictionary specs, and build the list of component specs decorated with
+ * open_flags and initial dict_flags such as locking.
+ *
+ * Normally, these decorated specs are used for registering tables with
+ * dict_register() and for looking them up with dict_handle(). For
+ * testing, we need those names to determine whether a component
+ * dictionary is registered.
+ *
+ * The dict_flags in a registered component spec may differ from actual
+ * dictionary flags: when a dictionary is opened, it may add dict_flags
+ * that describe its own properties such as whether the table's left-hand
+ * side is a fixed string or a pattern.
+ */
+ argv_truncate(out_reg_component_specs, 0);
+ vstring_strcpy(out_composite_spec, dict_type);
+ vstring_strcat(out_composite_spec, ":{");
+ for (cpp = component_specs; *cpp; cpp++) {
+ vstring_strcat(out_composite_spec, *cpp);
+ if (cpp[1])
+ vstring_strcat(out_composite_spec, ",");
+ dict_make_registered_name(reg_spec, *cpp, open_flags, dict_flags);
+ argv_add(out_reg_component_specs, STR(reg_spec), (char *) 0);
+ }
+ vstring_strcat(out_composite_spec, "}");
+ vstring_free(reg_spec);
+ return (STR(out_composite_spec));
+}
+
+/* dict_get_and_capture_msg - deploy dict_get() and capture msg(3) output */
+
+const char *dict_get_and_capture_msg(DICT *dict, const char *key,
+ VSTRING *out_msg_buf)
+{
+ VSTREAM *memory_stream;
+ const char *value;
+
+ VSTRING_RESET(out_msg_buf);
+ VSTRING_TERMINATE(out_msg_buf);
+ if ((memory_stream = vstream_memopen(out_msg_buf, O_WRONLY)) == 0)
+ msg_fatal("open memory stream: %m");
+ vstream_swap(VSTREAM_ERR, memory_stream);
+ value = dict_get(dict, key);
+ vstream_swap(memory_stream, VSTREAM_ERR);
+ (void) vstream_fclose(memory_stream);
+ return (value);
+}
+
+/* dict_get_and_verify - deploy dict_get() and verify results */
+
+int dict_get_and_verify(DICT *dict, const char *key, const char *want_value,
+ int want_error, const char *want_msg)
+{
+ VSTRING *msg_buf = vstring_alloc(100);
+ int ret = PASS;
+ const char *got;
+
+ got = dict_get_and_capture_msg(dict, key, msg_buf);
+ if (LEN(msg_buf) > 0 && want_msg == 0) {
+ msg_warn("unexpected error message: '%s'", STR(msg_buf));
+ ret = FAIL;
+ } else if (want_msg != 0 && strstr(STR(msg_buf), want_msg) == 0) {
+ msg_warn("unexpected error message: got '%s', want '%s'",
+ STR(msg_buf), want_msg);
+ ret = FAIL;
+ } else if (dict->error != want_error) {
+ msg_warn("unexpected lookup error for '%s': got '%d', want '%d",
+ key, dict->error, want_error);
+ ret = FAIL;
+ } else if (got == 0) {
+ if (want_value != 0) {
+ msg_warn("unexpected lookup result for '%s': got '%s', want '%s'",
+ key, "NOTFOUND", want_value);
+ ret = FAIL;
+ }
+ } else {
+ if (want_value == 0) {
+ msg_warn("unexpected lookup result for '%s': got '%s', want '%s'",
+ key, got, "NOTFOUND");
+ ret = FAIL;
+ } else if (strcmp(got, want_value) != 0) {
+ msg_warn("unexpected lookup result for '%s': got '%s', want '%s'",
+ key, got, want_value);
+ ret = FAIL;
+ }
+ }
+ vstring_free(msg_buf);
+ return (ret);
+}
+
+/* dict_get_and_verify_bulk - dict_get_and_verify() wrapper for bulk usage */
+
+int dict_get_and_verify_bulk(DICT *dict,
+ const struct dict_get_verify_data * data)
+{
+ int ret = PASS;
+ const struct dict_get_verify_data *dp;
+
+ for (dp = data; dp->key; dp++) {
+ if (dict_get_and_verify(dict, dp->key, dp->want_value,
+ dp->want_error, dp->want_msg) == FAIL)
+ ret = FAIL;
+ }
+ return (ret);
+}
diff --git a/src/testing/dict_test_helper.h b/src/testing/dict_test_helper.h
new file mode 100644
index 000000000..044dec05a
--- /dev/null
+++ b/src/testing/dict_test_helper.h
@@ -0,0 +1,45 @@
+#ifndef _DICT_TEST_HELPER_H_INCLUDED_
+#define _DICT_TEST_HELPER_H_INCLUDED_
+
+/*++
+/* NAME
+/* dict_test_helper 3h
+/* SUMMARY
+/* dictionary test helpers
+/* SYNOPSIS
+/* #include <dict_test_helper.h>
+/* DESCRIPTION
+/* .nf
+
+ /*
+ * Utility library.
+ */
+#include <argv.h>
+#include <dict.h>
+#include <vstring.h>
+
+ /*
+ * External interface.
+ */
+extern DICT *dict_open_and_capture_msg(const char *, int, int, VSTRING *);
+extern char *dict_compose_spec(const char *, const char **, int, int, VSTRING *, ARGV *);
+extern const char *dict_get_and_capture_msg(DICT *, const char *, VSTRING *);
+
+struct dict_get_verify_data {
+ const char *key;
+ const char *want_value;
+ int want_error;
+ const char *want_msg;
+};
+
+extern int dict_get_and_verify(DICT *, const char *, const char *, int, const char *);
+extern int dict_get_and_verify_bulk(DICT *, const struct dict_get_verify_data *);
+
+/* LICENSE
+/* The Secure Mailer license must be distributed with this software.
+/* AUTHOR(S)
+/* Wietse Venema
+/* porcupine.org
+/*--*/
+
+#endif
diff --git a/src/testing/mock_dict.c b/src/testing/mock_dict.c
new file mode 100644
index 000000000..d3f56b700
--- /dev/null
+++ b/src/testing/mock_dict.c
@@ -0,0 +1,116 @@
+/*++
+/* NAME
+/* mock_dict 3
+/* SUMMARY
+/* mock tables
+/* SYNOPSIS
+/* $include <mock_dict.h>
+/*
+/* setup_mock_cdb(const char *name_payload)
+/*
+/* setup_mock_lmdb(const char *name_payload)
+/* DESCRIPTION
+/* setup_mock_cdb (or setup_mock_lmdb) instantiates a mock cdb
+/* (or lmdb) table with a name_payload that uses inline: syntax.
+/* 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>
+
+ /*
+ * Utility library.
+ */
+#include <dict.h>
+#include <dict_cdb.h>
+#include <dict_inline.h>
+#include <dict_lmdb.h>
+#include <mkmap.h>
+#include <mymalloc.h>
+
+ /*
+ * Global library.
+ */
+
+ /*
+ * Testing library.
+ */
+#include <mock_dict.h>
+
+static char *mock_cdb_name_payload;
+
+static DICT *mock_dict_cdb_open(const char *dict_name, int open_flags,
+ int dict_flags)
+{
+ return (dict_open3(DICT_TYPE_INLINE, mock_cdb_name_payload,
+ open_flags, dict_flags));
+}
+
+static MKMAP *mock_mkmap_cdb_open(const char *path)
+{
+ MKMAP *mkmap = (MKMAP *) mymalloc(sizeof(*mkmap));
+
+ mkmap->open = mock_dict_cdb_open;
+ mkmap->after_open = 0;
+ mkmap->after_close = 0;
+ return (mkmap);
+}
+
+static const DICT_OPEN_INFO mock_cdb_info = {
+ "cdb", mock_dict_cdb_open, mock_mkmap_cdb_open,
+};
+
+void setup_mock_cdb(const char *name_payload)
+{
+ if (mock_cdb_name_payload != 0)
+ myfree(mock_cdb_name_payload);
+
+ mock_cdb_name_payload = mystrdup(name_payload);
+
+ if (dict_open_lookup(mock_cdb_info.type))
+ dict_open_unregister(mock_cdb_info.type);
+ dict_open_register(&mock_cdb_info);
+}
+
+static char *mock_lmdb_name_payload;
+
+static DICT *mock_dict_lmdb_open(const char *dict_name, int open_flags,
+ int dict_flags)
+{
+ return (dict_open3(DICT_TYPE_INLINE, mock_lmdb_name_payload,
+ open_flags, dict_flags));
+}
+
+static MKMAP *mock_mkmap_lmdb_open(const char *path)
+{
+ MKMAP *mkmap = (MKMAP *) mymalloc(sizeof(*mkmap));
+
+ mkmap->open = mock_dict_lmdb_open;
+ mkmap->after_open = 0;
+ mkmap->after_close = 0;
+ return (mkmap);
+}
+
+static const DICT_OPEN_INFO mock_lmdb_info = {
+ "lmdb", mock_dict_lmdb_open, mock_mkmap_lmdb_open,
+};
+
+void setup_mock_lmdb(const char *name_payload)
+{
+ if (mock_lmdb_name_payload != 0)
+ myfree(mock_lmdb_name_payload);
+
+ mock_lmdb_name_payload = mystrdup(name_payload);
+
+ if (dict_open_lookup(mock_lmdb_info.type))
+ dict_open_unregister(mock_lmdb_info.type);
+ dict_open_register(&mock_lmdb_info);
+}
diff --git a/src/testing/mock_dict.h b/src/testing/mock_dict.h
new file mode 100644
index 000000000..8ec0db902
--- /dev/null
+++ b/src/testing/mock_dict.h
@@ -0,0 +1,27 @@
+#ifndef _MOCK_DICT_H_INCLUDED_
+#define _MOCK_DICT_H_INCLUDED_
+
+/*++
+/* NAME
+/* mock_dict 3h
+/* SUMMARY
+/* mock tables
+/* SYNOPSIS
+/* #include <mock_dict.h>
+/* DESCRIPTION
+/* .nf
+
+ /*
+ * External interface.
+ */
+extern void setup_mock_cdb(const char *);
+extern void setup_mock_lmdb(const char *);
+
+/* LICENSE
+/* The Secure Mailer license must be distributed with this software.
+/* AUTHOR(S)
+/* Wietse Venema
+/* porcupine.org
+/*--*/
+
+#endif
diff --git a/src/testing/mock_open_as.c b/src/testing/mock_open_as.c
new file mode 100644
index 000000000..3a6a70a85
--- /dev/null
+++ b/src/testing/mock_open_as.c
@@ -0,0 +1,102 @@
+/*++
+/* NAME
+/* mock_open_as 3
+/* SUMMARY
+/* test scaffolding
+/* SYNOPSIS
+/* #include <mock_open_as.h>
+/*
+/* typedef struct MOCK_OPEN_AS_REQ {
+/* in +4
+/* const char *want_path,
+/* uid_t want_uid,
+/* gid_t want_gid,
+/* const char *out_data,
+/* int out_errno)
+/* .in -4
+/* } MOCK_OPEN_AS_REQ;
+/*
+/* void setup_mock_vstream_fopen_as(const MOCK_OPEN_AS_REQ *request)
+/* DESCRIPTION
+/* This module provides a mock vstream_fopen_as() implementation
+/* that returns static results for expected calls.
+/*
+/* setup_mock_vstream_fopen_as() makes a shallow copy of its input:
+/* .IP want_path, want_uid, want_gid
+/* The expected vstream_fopen_as() arguments. If the mock
+/* vstream_fopen_as() is called with an unexpected argument, the
+/* mock terminates the test with a fatal error.
+/* .IP out_data
+/* The data that can be read from the stream returned by
+/* vstream_fopen_as().
+/* .IP out_errno
+/* If not NULL, vstream_fopen_as() will return NULL and set errno
+/* to the value of this argument.
+/* SEE ALSO
+/* open_as(3), the real thing
+/* 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 <errno.h>
+#include <string.h>
+
+ /*
+ * Utility library.
+ */
+#include <msg.h>
+#include <open_as.h>
+#include <vstring.h>
+
+ /*
+ * Application-specific.
+ */
+#include <mock_open_as.h>
+
+static MOCK_OPEN_AS_REQ mock_req;
+
+/* setup_mock_vstream_fopen_as - set up expectation */
+
+void setup_mock_vstream_fopen_as(const MOCK_OPEN_AS_REQ *request)
+{
+ mock_req = *request;
+}
+
+/* vstream_fopen_as - fake implementation */
+
+VSTREAM *vstream_fopen_as(const char *path, int flags, mode_t mode,
+ uid_t uid, gid_t gid)
+{
+ VSTREAM *stream;
+ VSTRING *data_buffer;
+
+ /* TODO(wietse) replace with match_str() and match_decimal(). */
+ if (strcmp(path, mock_req.want_path) != 0)
+ msg_fatal("vstream_fopen: got path '%s', want '%s'",
+ path, mock_req.want_path);
+ if (uid != mock_req.want_uid)
+ msg_fatal("vstream_fopen: got uid '%d', want '%d'",
+ (int) uid, (int) mock_req.want_uid);
+ if (gid != mock_req.want_gid)
+ msg_fatal("vstream_fopen: got gid '%d', want '%d'",
+ (int) gid, (int) mock_req.want_gid);
+
+ if (mock_req.out_errno != 0) {
+ errno = mock_req.out_errno;
+ return (0);
+ }
+ data_buffer = vstring_alloc(strlen(mock_req.out_data));
+ vstring_strcpy(data_buffer, mock_req.out_data);
+ stream = vstream_memopen(data_buffer, flags);
+ vstream_control(stream, CA_VSTREAM_CTL_OWN_VSTRING, CA_VSTREAM_CTL_END);
+ return (stream);
+}
diff --git a/src/testing/mock_open_as.h b/src/testing/mock_open_as.h
new file mode 100644
index 000000000..daba39186
--- /dev/null
+++ b/src/testing/mock_open_as.h
@@ -0,0 +1,45 @@
+#ifndef _MOCK_OPEN_AS_H_INCLUDED_
+#define _MOCK_OPEN_AS_H_INCLUDED_
+
+/*++
+/* NAME
+/* mock_open_as 3h
+/* SUMMARY
+/* fake open_as
+/* SYNOPSIS
+/* #include <mock_open_as.h>
+/* DESCRIPTION
+/* .nf
+
+ /*
+ * System library.
+ */
+#include <unistd.h>
+
+ /*
+ * Utility library.
+ */
+#include <open_as.h>
+#include <vstream.h>
+
+ /*
+ * External interface.
+ */
+typedef struct MOCK_OPEN_AS_REQ {
+ const char *want_path;
+ uid_t want_uid;
+ gid_t want_gid;
+ const char *out_data;
+ int out_errno;
+} MOCK_OPEN_AS_REQ;
+
+extern void setup_mock_vstream_fopen_as(const MOCK_OPEN_AS_REQ *);
+
+/* LICENSE
+/* The Secure Mailer license must be distributed with this software.
+/* AUTHOR(S)
+/* Wietse Venema
+/* porcupine.org
+/*--*/
+
+#endif
diff --git a/src/testing/mock_spawn_command.c b/src/testing/mock_spawn_command.c
new file mode 100644
index 000000000..e6c4dde98
--- /dev/null
+++ b/src/testing/mock_spawn_command.c
@@ -0,0 +1,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);
+}
diff --git a/src/testing/mock_spawn_command.h b/src/testing/mock_spawn_command.h
new file mode 100644
index 000000000..d1ff39d95
--- /dev/null
+++ b/src/testing/mock_spawn_command.h
@@ -0,0 +1,39 @@
+#ifndef _MOCK_SPAWN_COMMAND_H_INCLUDED_
+#define _MOCK_SPAWN_COMMAND_H_INCLUDED_
+
+/*++
+/* NAME
+/* mock_spawn_command.h 3h
+/* SUMMARY
+/* spawn_command() mocker
+/* SYNOPSIS
+/* #include <mock_spawn_command.h>
+/* DESCRIPTION
+/* .nf
+
+ /*
+ * Utility library.
+ */
+#include <spawn_command.h>
+
+ /*
+ * External API.
+ */
+typedef struct MOCK_SPAWN_CMD_REQ {
+ struct spawn_args want_args;
+ int out_status;
+ const char *out_text;
+} MOCK_SPAWN_CMD_REQ;
+
+extern void setup_mock_spawn_command(const MOCK_SPAWN_CMD_REQ *);
+
+/* LICENSE
+/* .ad
+/* .fi
+/* The Secure Mailer license must be distributed with this software.
+/* AUTHOR(S)
+/* Wietse Venema
+/* porcupine.org
+/*--*/
+
+#endif
diff --git a/src/testing/mock_stat.c b/src/testing/mock_stat.c
new file mode 100644
index 000000000..8690d2195
--- /dev/null
+++ b/src/testing/mock_stat.c
@@ -0,0 +1,113 @@
+/*++
+/* NAME
+/* mock_stat 3
+/* SUMMARY
+/* test scaffolding
+/* SYNOPSIS
+/* #include <mock_stat.h>
+/*
+/* typedef struct MOCK_STAT_REQ {
+/* .in +4
+/* const char *want_path;
+/* int out_errno;
+/* struct stat st;
+/* .in -4
+/* } MOCK_STAT_REQ;
+/*
+/* void setup_mock_stat(const MOCK_STAT_REQ *request)
+/*
+/* void teardown_mock_stat(void)
+/* DESCRIPTION
+/* This module provides a mock stat() implementation that returns
+/* static results for expected calls.
+/*
+/* setup_mock_stat() adds a mapping from pathname to struct
+/* stat_info pointer. It is an error to enter multiple mappings
+/* for one pathname. If makes a shallow copy ig its input:
+/* .IP want_path
+/* The lookup key for file status information.
+/* .IP out_errno
+/* If non-zero, mock stat() will return -1 and set the global
+/* errno variable.
+/* .IP st
+/* File status information, ignored when out_errno is non-zero.
+/* .PP
+/* teardown_mock_stat() destroys all mappings entered with
+/* setup_mock_stat().
+/*
+/* The mock stat() function looks up the MOCK_STAT_REQ information
+/* for a pathname and terminates the test if the pathname is not
+/* expected. If the mapping contains a non-zero out_errno value,
+/* it simulates an error, sets the global errno value, and returns
+/* -1. Otherwise it copies the stat_info.st value to the storage
+/* provided with the st argument.
+/* SEE ALSO
+/* stat(2), the real thing
+/* 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 <errno.h>
+#include <sys/stat.h>
+
+ /*
+ * Utility library.
+ */
+#include <htable.h>
+#include <msg.h>
+#include <wrap_stat.h>
+
+ /*
+ * Test library.
+ */
+#include <mock_stat.h>
+
+ /*
+ * Application-specific.
+ */
+static HTABLE *mock_stat_table;
+
+/* setup_mock_stat - add path-to-status mapping */
+
+void setup_mock_stat(const MOCK_STAT_REQ *request)
+{
+ if (mock_stat_table == 0)
+ mock_stat_table = htable_create(10);
+ htable_enter(mock_stat_table, request->want_path, (void *) request);
+}
+
+/* teardown_mock_stat - destroy all path-to-status mappings */
+
+void teardown_mock_stat(void)
+{
+ if (mock_stat_table) {
+ htable_free(mock_stat_table, (void (*) (void *)) 0);
+ mock_stat_table = 0;
+ }
+}
+
+/* stat - mock stat() implementation */
+
+int stat(const char *path, struct stat *st) {
+ MOCK_STAT_REQ *mock_info;
+
+ if ((mock_info = (MOCK_STAT_REQ *) htable_find(mock_stat_table, path)) == 0)
+ msg_fatal("mock stat: unexpected pathname: '%s'", path);
+
+ if (mock_info->out_errno != 0) {
+ errno = mock_info->out_errno;
+ return (-1);
+ } else {
+ *st = mock_info->out_st;
+ return (0);
+ }
+}
diff --git a/src/testing/mock_stat.h b/src/testing/mock_stat.h
new file mode 100644
index 000000000..a7d2ed197
--- /dev/null
+++ b/src/testing/mock_stat.h
@@ -0,0 +1,39 @@
+#ifndef _MOCK_STAT_H_INCLUDED_
+#define _MOCK_STAT_H_INCLUDED_
+
+/*++
+/* NAME
+/* mock_stat 3h
+/* SUMMARY
+/* mock stat
+/* SYNOPSIS
+/* #include <mock_stat.h>
+/* DESCRIPTION
+/* .nf
+
+ /*
+ * System library.
+ */
+#include <sys/stat.h>
+#include <unistd.h>
+
+ /*
+ * External interface.
+ */
+typedef struct MOCK_STAT_REQ {
+ const char *want_path;
+ struct stat out_st;
+ int out_errno;
+} MOCK_STAT_REQ;
+
+extern void setup_mock_stat(const MOCK_STAT_REQ *);
+extern void teardown_mock_stat(void);
+
+/* LICENSE
+/* The Secure Mailer license must be distributed with this software.
+/* AUTHOR(S)
+/* Wietse Venema
+/* porcupine.org
+/*--*/
+
+#endif
diff --git a/src/testing/msg_capture.c b/src/testing/msg_capture.c
new file mode 100644
index 000000000..38e7c38f1
--- /dev/null
+++ b/src/testing/msg_capture.c
@@ -0,0 +1,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);
+}
diff --git a/src/testing/msg_capture.h b/src/testing/msg_capture.h
new file mode 100644
index 000000000..164e3b1c5
--- /dev/null
+++ b/src/testing/msg_capture.h
@@ -0,0 +1,34 @@
+#ifndef _MSB_CAPTURE_H_INCLUDED_
+#define _MSB_CAPTURE_H_INCLUDED_
+
+/*++
+/* NAME
+/* msg_capture 3h
+/* SUMMARY
+/* message capture support
+/* SYNOPSIS
+/* #include <msg_capture.h>
+/* DESCRIPTION
+/* .nf
+
+ /*
+ * External API.
+ */
+typedef struct MSG_CAPTURE MSG_CAPTURE;
+
+extern MSG_CAPTURE *msg_capt_create(ssize_t init_size);
+extern void msg_capt_start(MSG_CAPTURE *mp);
+extern void msg_capt_stop(MSG_CAPTURE *mp);
+extern const char *msg_capt_expose(MSG_CAPTURE *mp);
+extern void msg_capt_free(MSG_CAPTURE *mp);
+
+/* LICENSE
+/* .ad
+/* .fi
+/* The Secure Mailer license must be distributed with this software.
+/* AUTHOR(S)
+/* Wietse Venema
+/* porcupine.org
+/*--*/
+
+#endif
diff --git a/src/testing/nosleep.c b/src/testing/nosleep.c
new file mode 100644
index 000000000..dbbe40baf
--- /dev/null
+++ b/src/testing/nosleep.c
@@ -0,0 +1,29 @@
+/*++
+/* NAME
+/* nosleep 3
+/* SUMMARY
+/* override the system sleep(3) implementation
+/* SYNOPSIS
+/* LD_PRELOAD=/path/to/nosleep.so command....
+/*
+/* int sleep(unsigned int seconds)
+/* DESCRIPTION
+/* This sleep(3) implementation returns zero immediately. The purpose
+/* is to speed up error handler tests by skipping the delay between
+/* reporting a fatal error and before terminating the process.
+/* LICENSE
+/* .ad
+/* .fi
+/* The Secure Mailer license must be distributed with this software.
+/* AUTHOR(S)
+/* Wietse Venema
+/* porcupine.org
+/*--*/
+
+#include <unistd.h>
+
+unsigned int sleep(unsigned int seconds)
+{
+ /* Don't sleep. */
+ return (0);
+}