Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 23 Dec 2024 15:49:52 GMT
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 66a022a30dd9 - main - syslogd: Add syslog basic message format tests
Message-ID:  <202412231549.4BNFnqRJ011939@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=66a022a30dd968631c64b73e473dd16589592fad

commit 66a022a30dd968631c64b73e473dd16589592fad
Author:     Michal Scigocki <michal.os@hotmail.com>
AuthorDate: 2024-12-23 02:53:51 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-12-23 15:45:12 +0000

    syslogd: Add syslog basic message format tests
    
    RFC 3164 and RFC 5424 syslog message format tests and minor refactor of
    existing tests to reuse components in new tests.
    
    Reviewed by:    markj
    MFC after:      3 weeks
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/1536
---
 usr.sbin/syslogd/tests/Makefile                    |  11 +-
 .../syslogd/tests/syslogd_basic_format_test.sh     | 111 +++++++++++++++++++
 .../syslogd/tests/syslogd_format_test_common.sh    |  32 ++++++
 usr.sbin/syslogd/tests/syslogd_test.sh             | 105 +-----------------
 usr.sbin/syslogd/tests/syslogd_test_common.sh      | 117 +++++++++++++++++++++
 5 files changed, 270 insertions(+), 106 deletions(-)

diff --git a/usr.sbin/syslogd/tests/Makefile b/usr.sbin/syslogd/tests/Makefile
index c60709b0531d..65d08d75fcd1 100644
--- a/usr.sbin/syslogd/tests/Makefile
+++ b/usr.sbin/syslogd/tests/Makefile
@@ -1,7 +1,14 @@
-ATF_TESTS_SH=	syslogd_test
+PACKAGE=	tests
+
+ATF_TESTS_SH=	syslogd_basic_format_test \
+		syslogd_test
+
 # Run in jail with new vnet so we don't need to worry about address conflicts
 TEST_METADATA=	execenv="jail" \
-		execenv_jail_params="vnet=new"
+		execenv_jail_params="vnet=new" \
 		timeout=20
 
+${PACKAGE}FILES=syslogd_format_test_common.sh \
+		syslogd_test_common.sh
+
 .include <bsd.test.mk>
diff --git a/usr.sbin/syslogd/tests/syslogd_basic_format_test.sh b/usr.sbin/syslogd/tests/syslogd_basic_format_test.sh
new file mode 100644
index 000000000000..7ee7cd0e1e91
--- /dev/null
+++ b/usr.sbin/syslogd/tests/syslogd_basic_format_test.sh
@@ -0,0 +1,111 @@
+#-
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2024 Michal Scigocki <michal.os@hotmail.com>
+#
+
+. $(atf_get_srcdir)/syslogd_test_common.sh
+. $(atf_get_srcdir)/syslogd_format_test_common.sh
+
+# Basic format tests
+# Single server, logging to local socket (inet disabled)
+setup_basic_format_test()
+{
+    local format="$1"
+    local logfile="$2"
+
+    printf "user.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}"
+
+    syslogd_start \
+        -O "${format}" \
+        -N \
+        -ss
+
+    syslogd_log -p user.debug -t "${TAG}" \
+        -h "${SYSLOGD_LOCAL_SOCKET}" \
+        -H "${HOSTNAME}" "${MSG}"
+}
+
+atf_test_case "O_flag_bsd_basic" "cleanup"
+O_flag_bsd_basic_head()
+{
+    atf_set descr "bsd format test on local syslog message"
+}
+O_flag_bsd_basic_body()
+{
+    local format="bsd"
+    local logfile="${PWD}/O_flag_${format}_basic.log"
+
+    setup_basic_format_test "${format}" "${logfile}"
+
+    atf_check -s exit:0 -o match:"${REGEX_RFC3164_LOGFILE}" cat "${logfile}"
+}
+O_flag_bsd_basic_cleanup()
+{
+    syslogd_stop
+}
+
+atf_test_case "O_flag_rfc3164_basic" "cleanup"
+O_flag_rfc3164_basic_head()
+{
+    atf_set descr "rfc3164 format test on local syslog message"
+}
+O_flag_rfc3164_basic_body()
+{
+    local format="rfc3164"
+    local logfile="${PWD}/O_flag_${format}_basic.log"
+
+    setup_basic_format_test "${format}" "${logfile}"
+
+    atf_check -s exit:0 -o match:"${REGEX_RFC3164_LOGFILE}" cat "${logfile}"
+}
+O_flag_rfc3164_basic_cleanup()
+{
+    syslogd_stop
+}
+
+atf_test_case "O_flag_syslog_basic" "cleanup"
+O_flag_syslog_basic_head()
+{
+    atf_set descr "syslog format test on local syslog message"
+}
+O_flag_syslog_basic_body()
+{
+    local format="syslog"
+    local logfile="${PWD}/O_flag_${format}_basic.log"
+
+    setup_basic_format_test "${format}" "${logfile}"
+
+    atf_check -s exit:0 -o match:"${REGEX_RFC5424_LOGFILE}" cat "${logfile}"
+}
+O_flag_syslog_basic_cleanup()
+{
+    syslogd_stop
+}
+
+atf_test_case "O_flag_rfc5424_basic" "cleanup"
+O_flag_rfc5424_basic_head()
+{
+    atf_set descr "rfc5424 format test on local syslog message"
+}
+O_flag_rfc5424_basic_body()
+{
+    local format="rfc5424"
+    local logfile="${PWD}/O_flag_${format}_basic.log"
+
+    setup_basic_format_test "${format}" "${logfile}"
+
+    atf_check -s exit:0 -o match:"${REGEX_RFC5424_LOGFILE}" cat "${logfile}"
+}
+O_flag_rfc5424_basic_cleanup()
+{
+    syslogd_stop
+}
+
+atf_init_test_cases()
+{
+    atf_add_test_case "O_flag_bsd_basic"
+    atf_add_test_case "O_flag_rfc3164_basic"
+    atf_add_test_case "O_flag_syslog_basic"
+    atf_add_test_case "O_flag_rfc5424_basic"
+}
diff --git a/usr.sbin/syslogd/tests/syslogd_format_test_common.sh b/usr.sbin/syslogd/tests/syslogd_format_test_common.sh
new file mode 100644
index 000000000000..199fb746839c
--- /dev/null
+++ b/usr.sbin/syslogd/tests/syslogd_format_test_common.sh
@@ -0,0 +1,32 @@
+#-
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2024 Michal Scigocki <michal.os@hotmail.com>
+#
+
+# REGEX Components
+readonly PRI="<15>"
+readonly VERSION="1"
+readonly DATE_RFC3164="[A-Z][a-z]{2} [ 1-3][0-9]"
+readonly TIMESPEC_RFC5424="([:TZ0-9\.\+\-]{20,32}|\-)" # Simplified TIMESPEC
+readonly TIME_RFC3164="([0-9]{2}:){2}[0-9]{2}"
+readonly HOSTNAME="example.test"
+readonly HOSTNAME_REGEX="example\.test"
+readonly TAG="test_tag"
+readonly MSG="test_log_message"
+
+# Test REGEX
+# Dec  2 15:55:00 example.test test_tag: test_log_message
+readonly REGEX_RFC3164="${DATE_RFC3164} ${TIME_RFC3164} ${HOSTNAME_REGEX} ${TAG}: ${MSG}"
+readonly REGEX_RFC3164_LOGFILE="^${REGEX_RFC3164}$"
+readonly REGEX_RFC3164_PAYLOAD="${PRI}${REGEX_RFC3164}$"
+
+# Dec  2 15:55:00 Forwarded from example.test: test_tag: test_log_message
+readonly REGEX_RFC3164_LEGACY="${DATE_RFC3164} ${TIME_RFC3164} Forwarded from ${HOSTNAME_REGEX}: ${TAG}: ${MSG}"
+readonly REGEX_RFC3164_LEGACY_LOGFILE="^${REGEX_RFC3164_LEGACY}$"
+readonly REGEX_RFC3164_LEGACY_PAYLOAD="${PRI}${REGEX_RFC3164_LEGACY}$"
+
+# <15>1 2024-12-02T15:55:00.000000+00:00 example.test test_tag - - - test_log_message
+readonly REGEX_RFC5424="${PRI}${VERSION} ${TIMESPEC_RFC5424} ${HOSTNAME_REGEX} ${TAG} - - - ${MSG}"
+readonly REGEX_RFC5424_LOGFILE="^${REGEX_RFC5424}$"
+readonly REGEX_RFC5424_PAYLOAD="${REGEX_RFC5424}$"
diff --git a/usr.sbin/syslogd/tests/syslogd_test.sh b/usr.sbin/syslogd/tests/syslogd_test.sh
index e75c85d5995a..51c3a5fca1c6 100644
--- a/usr.sbin/syslogd/tests/syslogd_test.sh
+++ b/usr.sbin/syslogd/tests/syslogd_test.sh
@@ -13,110 +13,7 @@
 # Tests to-do:
 # actions: users
 
-readonly SYSLOGD_UDP_PORT="5140"
-readonly SYSLOGD_CONFIG="${PWD}/syslog.conf"
-readonly SYSLOGD_LOCAL_SOCKET="${PWD}/log.sock"
-readonly SYSLOGD_PIDFILE="${PWD}/syslogd.pid"
-readonly SYSLOGD_LOCAL_PRIVSOCKET="${PWD}/logpriv.sock"
-
-# Start a private syslogd instance.
-syslogd_start()
-{
-    local jail bind_addr conf_file pid_file socket privsocket
-    local opt next other_args
-
-    # Setup loopback so we can deliver messages to ourself.
-    atf_check ifconfig lo0 inet 127.0.0.1/16
-
-    OPTIND=1
-    while getopts ":b:f:j:P:p:S:" opt; do
-        case "${opt}" in
-        b)
-            bind_addr="${OPTARG}"
-            ;;
-        f)
-            conf_file="${OPTARG}"
-            ;;
-        j)
-            jail="jexec ${OPTARG}"
-            ;;
-        P)
-            pid_file="${OPTARG}"
-            ;;
-        p)
-            socket="${OPTARG}"
-            ;;
-        S)
-            privsocket="${OPTARG}"
-            ;;
-        ?)
-            opt="${OPTARG}"
-            next="$(eval echo \${${OPTIND}})"
-
-            case "${next}" in
-            -* | "")
-                other_args="${other_args} -${opt}"
-                shift $((OPTIND - 1))
-                ;;
-            *)
-                other_args="${other_args} -${opt} ${next}"
-                shift ${OPTIND}
-                ;;
-            esac
-
-            # Tell getopts to continue parsing.
-            OPTIND=1
-            ;;
-        :)
-            atf_fail "The -${OPTARG} flag requires an argument"
-            ;;
-        esac
-    done
-
-    $jail syslogd \
-        -b "${bind_addr:-":${SYSLOGD_UDP_PORT}"}" \
-        -C \
-        -d \
-        -f "${conf_file:-${SYSLOGD_CONFIG}}" \
-        -H \
-        -P "${pid_file:-${SYSLOGD_PIDFILE}}" \
-        -p "${socket:-${SYSLOGD_LOCAL_SOCKET}}" \
-        -S "${privsocket:-${SYSLOGD_LOCAL_PRIVSOCKET}}" \
-        ${other_args} \
-        &
-
-    # Give syslogd a bit of time to spin up.
-    while [ "$((i+=1))" -le 20 ]; do
-        [ -S "${socket:-${SYSLOGD_LOCAL_SOCKET}}" ] && return
-        sleep 0.1
-    done
-    atf_fail "timed out waiting for syslogd to start"
-}
-
-# Simple logger(1) wrapper.
-syslogd_log()
-{
-    atf_check -s exit:0 -o empty -e empty logger $*
-}
-
-# Make syslogd reload its configuration file.
-syslogd_reload()
-{
-    pkill -HUP -F "${1:-${SYSLOGD_PIDFILE}}"
-}
-
-# Stop a private syslogd instance.
-syslogd_stop()
-{
-    local pid_file="${1:-${SYSLOGD_PIDFILE}}"
-
-    pid=$(cat "${pid_file}")
-    if pkill -F "${pid_file}"; then
-        wait "${pid}"
-        rm -f "${pid_file}" "${2:-${SYSLOGD_LOCAL_SOCKET}}" \
-            "${3:-${SYSLOGD_LOCAL_PRIVSOCKET}}"
-    fi
-}
+. $(atf_get_srcdir)/syslogd_test_common.sh
 
 atf_test_case "unix" "cleanup"
 unix_head()
diff --git a/usr.sbin/syslogd/tests/syslogd_test_common.sh b/usr.sbin/syslogd/tests/syslogd_test_common.sh
new file mode 100644
index 000000000000..a764ebfb0e24
--- /dev/null
+++ b/usr.sbin/syslogd/tests/syslogd_test_common.sh
@@ -0,0 +1,117 @@
+#-
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2021, 2023 The FreeBSD Foundation
+#
+# This software was developed by Mark Johnston under sponsorship from
+# the FreeBSD Foundation.
+#
+# This software was developed by Jake Freeland under sponsorship from
+# the FreeBSD Foundation.
+#
+
+readonly SYSLOGD_UDP_PORT="5140"
+readonly SYSLOGD_CONFIG="${PWD}/syslog.conf"
+readonly SYSLOGD_LOCAL_SOCKET="${PWD}/log.sock"
+readonly SYSLOGD_PIDFILE="${PWD}/syslogd.pid"
+readonly SYSLOGD_LOCAL_PRIVSOCKET="${PWD}/logpriv.sock"
+
+# Start a private syslogd instance.
+syslogd_start()
+{
+    local jail bind_addr conf_file pid_file socket privsocket
+    local opt next other_args
+
+    # Setup loopback so we can deliver messages to ourself.
+    atf_check ifconfig lo0 inet 127.0.0.1/16
+
+    OPTIND=1
+    while getopts ":b:f:j:P:p:S:" opt; do
+        case "${opt}" in
+        b)
+            bind_addr="${OPTARG}"
+            ;;
+        f)
+            conf_file="${OPTARG}"
+            ;;
+        j)
+            jail="jexec ${OPTARG}"
+            ;;
+        P)
+            pid_file="${OPTARG}"
+            ;;
+        p)
+            socket="${OPTARG}"
+            ;;
+        S)
+            privsocket="${OPTARG}"
+            ;;
+        ?)
+            opt="${OPTARG}"
+            next="$(eval echo \${${OPTIND}})"
+
+            case "${next}" in
+            -* | "")
+                other_args="${other_args} -${opt}"
+                shift $((OPTIND - 1))
+                ;;
+            *)
+                other_args="${other_args} -${opt} ${next}"
+                shift ${OPTIND}
+                ;;
+            esac
+
+            # Tell getopts to continue parsing.
+            OPTIND=1
+            ;;
+        :)
+            atf_fail "The -${OPTARG} flag requires an argument"
+            ;;
+        esac
+    done
+
+    $jail syslogd \
+        -b "${bind_addr:-":${SYSLOGD_UDP_PORT}"}" \
+        -C \
+        -d \
+        -f "${conf_file:-${SYSLOGD_CONFIG}}" \
+        -H \
+        -P "${pid_file:-${SYSLOGD_PIDFILE}}" \
+        -p "${socket:-${SYSLOGD_LOCAL_SOCKET}}" \
+        -S "${privsocket:-${SYSLOGD_LOCAL_PRIVSOCKET}}" \
+        ${other_args} \
+        &
+
+    # Give syslogd a bit of time to spin up.
+    while [ "$((i+=1))" -le 20 ]; do
+        [ -S "${socket:-${SYSLOGD_LOCAL_SOCKET}}" ] && return
+        sleep 0.1
+    done
+    atf_fail "timed out waiting for syslogd to start"
+}
+
+# Simple logger(1) wrapper.
+syslogd_log()
+{
+    atf_check -s exit:0 -o empty -e empty logger $*
+}
+
+# Make syslogd reload its configuration file.
+syslogd_reload()
+{
+    pkill -HUP -F "${1:-${SYSLOGD_PIDFILE}}"
+}
+
+# Stop a private syslogd instance.
+syslogd_stop()
+{
+    local pid_file="${1:-${SYSLOGD_PIDFILE}}"
+    local socket_file="${2:-${SYSLOGD_LOCAL_SOCKET}}"
+    local privsocket_file="${3:-${SYSLOGD_LOCAL_PRIVSOCKET}}"
+
+    pid=$(cat "${pid_file}")
+    if pkill -F "${pid_file}"; then
+        wait "${pid}"
+        rm -f "${pid_file}" "${socket_file}" "${privsocket_file}"
+    fi
+}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202412231549.4BNFnqRJ011939>