summaryrefslogtreecommitdiff
path: root/src/testing/mock_stat.c
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/mock_stat.c
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/mock_stat.c')
-rw-r--r--src/testing/mock_stat.c113
1 files changed, 113 insertions, 0 deletions
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);
+ }
+}