git: 65c61840eeb7 - main - syslogd: Add syslog relayed message format tests

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 23 Dec 2024 15:49:54 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=65c61840eeb794ca1505bc7e076f664c8d985f72

commit 65c61840eeb794ca1505bc7e076f664c8d985f72
Author:     Michal Scigocki <michal.os@hotmail.com>
AuthorDate: 2024-12-23 02:57:37 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-12-23 15:45:59 +0000

    syslogd: Add syslog relayed message format tests
    
    Reviewed by:    markj
    MFC after:      3 weeks
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/1536
---
 usr.sbin/syslogd/tests/Makefile                    |   1 +
 .../syslogd/tests/syslogd_relayed_format_test.sh   | 162 +++++++++++++++++++++
 2 files changed, 163 insertions(+)

diff --git a/usr.sbin/syslogd/tests/Makefile b/usr.sbin/syslogd/tests/Makefile
index 36b7798d317c..fcc0679f3274 100644
--- a/usr.sbin/syslogd/tests/Makefile
+++ b/usr.sbin/syslogd/tests/Makefile
@@ -2,6 +2,7 @@ PACKAGE=	tests
 
 ATF_TESTS_SH=	syslogd_basic_format_test \
 		syslogd_forwarded_format_test \
+		syslogd_relayed_format_test \
 		syslogd_test
 
 # Run in jail with new vnet so we don't need to worry about address conflicts
diff --git a/usr.sbin/syslogd/tests/syslogd_relayed_format_test.sh b/usr.sbin/syslogd/tests/syslogd_relayed_format_test.sh
new file mode 100644
index 000000000000..21481823068d
--- /dev/null
+++ b/usr.sbin/syslogd/tests/syslogd_relayed_format_test.sh
@@ -0,0 +1,162 @@
+#-
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2024 Michal Scigocki <michal.os@hotmail.com>
+#
+
+. $(atf_get_srcdir)/syslogd_format_test_common.sh
+
+readonly SERVER_1_PORT="5140"
+readonly SERVER_2_PORT="5141"
+readonly SERVER_3_PORT="5142"
+
+# Relayed messages tests
+# [Host] ---UDP--> [Relay] ---UDP--> [Central]
+setup_relayed_format_test()
+{
+    local format="$1"
+    local logfile="$2"
+    local pcapfile="$3"
+
+    confirm_INET_support_or_skip
+
+    # Begin packet capture for single packet
+    tcpdump --immediate-mode -c 1 -i lo0 -w "${pcapfile}" dst port \
+        "${SERVER_1_PORT}" &
+    tcpdump_pid="$!"
+
+    # Start first (central) server: receive UDP, log to file
+    printf "user.debug\t${logfile}\n" \
+        > "$(config_filename ${SERVER_1_PORT})"
+    syslogd_start_on_port "${SERVER_1_PORT}" -O "${format}"
+
+    # Start second (relay) server: send UDP, log to central server
+    printf "user.debug\t@127.0.0.1:${SERVER_1_PORT}\n" \
+        > "$(config_filename ${SERVER_2_PORT})"
+    syslogd_start_on_port "${SERVER_2_PORT}" -O "${format}"
+
+    # Start third logging host: send UDP, log to relay server
+    printf "user.debug\t@127.0.0.1:${SERVER_2_PORT}\n" \
+        > "$(config_filename ${SERVER_3_PORT})"
+    syslogd_start_on_port "${SERVER_3_PORT}" -O "${format}"
+
+    # Send test syslog message
+    syslogd_log -4 -p user.debug -t "${TAG}" -h 127.0.0.1 \
+        -P "${SERVER_3_PORT}" -H "${HOSTNAME}" "${MSG}"
+
+    wait "${tcpdump_pid}" # Wait for packet capture to finish
+}
+
+atf_test_case "O_flag_bsd_relayed" "cleanup"
+O_flag_bsd_relayed_head()
+{
+    atf_set descr "bsd format test on a relayed syslog message"
+    set_common_atf_metadata
+}
+O_flag_bsd_relayed_body()
+{
+    local format="bsd"
+    local logfile="${PWD}/${format}_relayed.log"
+    local pcapfile="${PWD}/${format}_relayed.pcap"
+
+    setup_relayed_format_test "${format}" "${logfile}" "${pcapfile}"
+
+    atf_expect_fail "PR 220246 issue with the legacy bsd format"
+    atf_check -s exit:0 -o match:"${REGEX_RFC3164_LOGFILE}" cat "${logfile}"
+    atf_check -s exit:0 -e ignore -o match:"${REGEX_RFC3164_PAYLOAD}" \
+        tcpdump -A -r "${pcapfile}"
+}
+O_flag_bsd_relayed_cleanup()
+{
+    syslogd_stop_on_ports \
+        "${SERVER_1_PORT}" \
+        "${SERVER_2_PORT}" \
+        "${SERVER_3_PORT}"
+}
+
+atf_test_case "O_flag_rfc3164_relayed" "cleanup"
+O_flag_rfc3164_relayed_head()
+{
+    atf_set descr "rfc3164 format test on a relayed syslog message"
+    set_common_atf_metadata
+}
+O_flag_rfc3164_relayed_body()
+{
+    local format="rfc3164"
+    local logfile="${PWD}/${format}_relayed.log"
+    local pcapfile="${PWD}/${format}_relayed.pcap"
+
+    setup_relayed_format_test "${format}" "${logfile}" "${pcapfile}"
+
+    atf_expect_fail "PR 220246 issue with the legacy rfc3164 format"
+    atf_check -s exit:0 -o match:"${REGEX_RFC3164_LOGFILE}" cat "${logfile}"
+    atf_check -s exit:0 -e ignore -o match:"${REGEX_RFC3164_PAYLOAD}" \
+        tcpdump -A -r "${pcapfile}"
+}
+O_flag_rfc3164_relayed_cleanup()
+{
+    syslogd_stop_on_ports \
+        "${SERVER_1_PORT}" \
+        "${SERVER_2_PORT}" \
+        "${SERVER_3_PORT}"
+}
+
+atf_test_case "O_flag_syslog_relayed" "cleanup"
+O_flag_syslog_relayed_head()
+{
+    atf_set descr "syslog format test on a relayed syslog message"
+    set_common_atf_metadata
+}
+O_flag_syslog_relayed_body()
+{
+    local format="syslog"
+    local logfile="${PWD}/${format}_relayed.log"
+    local pcapfile="${PWD}/${format}_relayed.pcap"
+
+    setup_relayed_format_test "${format}" "${logfile}" "${pcapfile}"
+
+    atf_check -s exit:0 -o match:"${REGEX_RFC5424_LOGFILE}" cat "${logfile}"
+    atf_check -s exit:0 -e ignore -o match:"${REGEX_RFC5424_PAYLOAD}" \
+        tcpdump -A -r "${pcapfile}"
+}
+O_flag_syslog_relayed_cleanup()
+{
+    syslogd_stop_on_ports \
+        "${SERVER_1_PORT}" \
+        "${SERVER_2_PORT}" \
+        "${SERVER_3_PORT}"
+}
+
+atf_test_case "O_flag_rfc5424_relayed" "cleanup"
+O_flag_rfc5424_relayed_head()
+{
+    atf_set descr "rfc5424 format test on a relayed syslog message"
+    set_common_atf_metadata
+}
+O_flag_rfc5424_relayed_body()
+{
+    local format="rfc5424"
+    local logfile="${PWD}/${format}_relayed.log"
+    local pcapfile="${PWD}/${format}_relayed.pcap"
+
+    setup_relayed_format_test "${format}" "${logfile}" "${pcapfile}"
+
+    atf_check -s exit:0 -o match:"${REGEX_RFC5424_LOGFILE}" cat "${logfile}"
+    atf_check -s exit:0 -e ignore -o match:"${REGEX_RFC5424_PAYLOAD}" \
+        tcpdump -A -r "${pcapfile}"
+}
+O_flag_rfc5424_relayed_cleanup()
+{
+    syslogd_stop_on_ports \
+        "${SERVER_1_PORT}" \
+        "${SERVER_2_PORT}" \
+        "${SERVER_3_PORT}"
+}
+
+atf_init_test_cases()
+{
+    atf_add_test_case "O_flag_bsd_relayed"
+    atf_add_test_case "O_flag_rfc3164_relayed"
+    atf_add_test_case "O_flag_syslog_relayed"
+    atf_add_test_case "O_flag_rfc5424_relayed"
+}