xref: /freebsd/usr.sbin/syslogd/tests/syslogd_relayed_format_test.sh (revision 8cc248fb92bfae1e6ef4e2b48a0064377c4b3c11)
165c61840SMichal Scigocki#-
265c61840SMichal Scigocki# SPDX-License-Identifier: BSD-2-Clause
365c61840SMichal Scigocki#
465c61840SMichal Scigocki# Copyright (c) 2024 Michal Scigocki <michal.os@hotmail.com>
565c61840SMichal Scigocki#
665c61840SMichal Scigocki
765c61840SMichal Scigocki. $(atf_get_srcdir)/syslogd_format_test_common.sh
865c61840SMichal Scigocki
965c61840SMichal Scigockireadonly SERVER_1_PORT="5140"
1065c61840SMichal Scigockireadonly SERVER_2_PORT="5141"
1165c61840SMichal Scigockireadonly SERVER_3_PORT="5142"
1265c61840SMichal Scigocki
1365c61840SMichal Scigocki# Relayed messages tests
1465c61840SMichal Scigocki# [Host] ---UDP--> [Relay] ---UDP--> [Central]
1565c61840SMichal Scigockisetup_relayed_format_test()
1665c61840SMichal Scigocki{
1765c61840SMichal Scigocki    local format="$1"
1865c61840SMichal Scigocki    local logfile="$2"
1965c61840SMichal Scigocki    local pcapfile="$3"
2065c61840SMichal Scigocki
2165c61840SMichal Scigocki    confirm_INET_support_or_skip
2265c61840SMichal Scigocki
2365c61840SMichal Scigocki    # Begin packet capture for single packet
2465c61840SMichal Scigocki    tcpdump --immediate-mode -c 1 -i lo0 -w "${pcapfile}" dst port \
2565c61840SMichal Scigocki        "${SERVER_1_PORT}" &
2665c61840SMichal Scigocki    tcpdump_pid="$!"
2765c61840SMichal Scigocki
2865c61840SMichal Scigocki    # Start first (central) server: receive UDP, log to file
2965c61840SMichal Scigocki    printf "user.debug\t${logfile}\n" \
3065c61840SMichal Scigocki        > "$(config_filename ${SERVER_1_PORT})"
3165c61840SMichal Scigocki    syslogd_start_on_port "${SERVER_1_PORT}" -O "${format}"
3265c61840SMichal Scigocki
3365c61840SMichal Scigocki    # Start second (relay) server: send UDP, log to central server
3465c61840SMichal Scigocki    printf "user.debug\t@127.0.0.1:${SERVER_1_PORT}\n" \
3565c61840SMichal Scigocki        > "$(config_filename ${SERVER_2_PORT})"
3665c61840SMichal Scigocki    syslogd_start_on_port "${SERVER_2_PORT}" -O "${format}"
3765c61840SMichal Scigocki
3865c61840SMichal Scigocki    # Start third logging host: send UDP, log to relay server
3965c61840SMichal Scigocki    printf "user.debug\t@127.0.0.1:${SERVER_2_PORT}\n" \
4065c61840SMichal Scigocki        > "$(config_filename ${SERVER_3_PORT})"
4165c61840SMichal Scigocki    syslogd_start_on_port "${SERVER_3_PORT}" -O "${format}"
4265c61840SMichal Scigocki
4365c61840SMichal Scigocki    # Send test syslog message
4465c61840SMichal Scigocki    syslogd_log -4 -p user.debug -t "${TAG}" -h 127.0.0.1 \
4565c61840SMichal Scigocki        -P "${SERVER_3_PORT}" -H "${HOSTNAME}" "${MSG}"
4665c61840SMichal Scigocki
4765c61840SMichal Scigocki    wait "${tcpdump_pid}" # Wait for packet capture to finish
4865c61840SMichal Scigocki}
4965c61840SMichal Scigocki
5065c61840SMichal Scigockiatf_test_case "O_flag_bsd_relayed" "cleanup"
5165c61840SMichal ScigockiO_flag_bsd_relayed_head()
5265c61840SMichal Scigocki{
5365c61840SMichal Scigocki    atf_set descr "bsd format test on a relayed syslog message"
5465c61840SMichal Scigocki    set_common_atf_metadata
5565c61840SMichal Scigocki}
5665c61840SMichal ScigockiO_flag_bsd_relayed_body()
5765c61840SMichal Scigocki{
5865c61840SMichal Scigocki    local format="bsd"
5965c61840SMichal Scigocki    local logfile="${PWD}/${format}_relayed.log"
6065c61840SMichal Scigocki    local pcapfile="${PWD}/${format}_relayed.pcap"
6165c61840SMichal Scigocki
6265c61840SMichal Scigocki    setup_relayed_format_test "${format}" "${logfile}" "${pcapfile}"
6365c61840SMichal Scigocki
6465c61840SMichal Scigocki    atf_expect_fail "PR 220246 issue with the legacy bsd format"
6565c61840SMichal Scigocki    atf_check -s exit:0 -o match:"${REGEX_RFC3164_LOGFILE}" cat "${logfile}"
6665c61840SMichal Scigocki    atf_check -s exit:0 -e ignore -o match:"${REGEX_RFC3164_PAYLOAD}" \
6765c61840SMichal Scigocki        tcpdump -A -r "${pcapfile}"
6865c61840SMichal Scigocki}
6965c61840SMichal ScigockiO_flag_bsd_relayed_cleanup()
7065c61840SMichal Scigocki{
7165c61840SMichal Scigocki    syslogd_stop_on_ports \
7265c61840SMichal Scigocki        "${SERVER_1_PORT}" \
7365c61840SMichal Scigocki        "${SERVER_2_PORT}" \
7465c61840SMichal Scigocki        "${SERVER_3_PORT}"
7565c61840SMichal Scigocki}
7665c61840SMichal Scigocki
7765c61840SMichal Scigockiatf_test_case "O_flag_rfc3164_relayed" "cleanup"
7865c61840SMichal ScigockiO_flag_rfc3164_relayed_head()
7965c61840SMichal Scigocki{
8065c61840SMichal Scigocki    atf_set descr "rfc3164 format test on a relayed syslog message"
8165c61840SMichal Scigocki    set_common_atf_metadata
8265c61840SMichal Scigocki}
8365c61840SMichal ScigockiO_flag_rfc3164_relayed_body()
8465c61840SMichal Scigocki{
8565c61840SMichal Scigocki    local format="rfc3164"
8665c61840SMichal Scigocki    local logfile="${PWD}/${format}_relayed.log"
8765c61840SMichal Scigocki    local pcapfile="${PWD}/${format}_relayed.pcap"
8865c61840SMichal Scigocki
8965c61840SMichal Scigocki    setup_relayed_format_test "${format}" "${logfile}" "${pcapfile}"
9065c61840SMichal Scigocki
9165c61840SMichal Scigocki    atf_expect_fail "PR 220246 issue with the legacy rfc3164 format"
9265c61840SMichal Scigocki    atf_check -s exit:0 -o match:"${REGEX_RFC3164_LOGFILE}" cat "${logfile}"
9365c61840SMichal Scigocki    atf_check -s exit:0 -e ignore -o match:"${REGEX_RFC3164_PAYLOAD}" \
9465c61840SMichal Scigocki        tcpdump -A -r "${pcapfile}"
9565c61840SMichal Scigocki}
9665c61840SMichal ScigockiO_flag_rfc3164_relayed_cleanup()
9765c61840SMichal Scigocki{
9865c61840SMichal Scigocki    syslogd_stop_on_ports \
9965c61840SMichal Scigocki        "${SERVER_1_PORT}" \
10065c61840SMichal Scigocki        "${SERVER_2_PORT}" \
10165c61840SMichal Scigocki        "${SERVER_3_PORT}"
10265c61840SMichal Scigocki}
10365c61840SMichal Scigocki
104*8cc248fbSMichal Scigockiatf_test_case "O_flag_rfc3164strict_relayed" "cleanup"
105*8cc248fbSMichal ScigockiO_flag_rfc3164strict_relayed_head()
106*8cc248fbSMichal Scigocki{
107*8cc248fbSMichal Scigocki    atf_set descr "rfc3164-strict format test on a relayed syslog message"
108*8cc248fbSMichal Scigocki    set_common_atf_metadata
109*8cc248fbSMichal Scigocki}
110*8cc248fbSMichal ScigockiO_flag_rfc3164strict_relayed_body()
111*8cc248fbSMichal Scigocki{
112*8cc248fbSMichal Scigocki    local format="rfc3164-strict"
113*8cc248fbSMichal Scigocki    local logfile="${PWD}/${format}_relayed.log"
114*8cc248fbSMichal Scigocki    local pcapfile="${PWD}/${format}_relayed.pcap"
115*8cc248fbSMichal Scigocki
116*8cc248fbSMichal Scigocki    setup_relayed_format_test "${format}" "${logfile}" "${pcapfile}"
117*8cc248fbSMichal Scigocki
118*8cc248fbSMichal Scigocki    atf_check -s exit:0 -o match:"${REGEX_RFC3164_LOGFILE}" cat "${logfile}"
119*8cc248fbSMichal Scigocki    atf_check -s exit:0 -e ignore -o match:"${REGEX_RFC3164_PAYLOAD}" \
120*8cc248fbSMichal Scigocki        tcpdump -A -r "${pcapfile}"
121*8cc248fbSMichal Scigocki}
122*8cc248fbSMichal ScigockiO_flag_rfc3164strict_relayed_cleanup()
123*8cc248fbSMichal Scigocki{
124*8cc248fbSMichal Scigocki    syslogd_stop_on_ports \
125*8cc248fbSMichal Scigocki        "${SYSLOGD_UDP_PORT_1}" \
126*8cc248fbSMichal Scigocki        "${SYSLOGD_UDP_PORT_2}" \
127*8cc248fbSMichal Scigocki        "${SYSLOGD_UDP_PORT_3}"
128*8cc248fbSMichal Scigocki}
129*8cc248fbSMichal Scigocki
13065c61840SMichal Scigockiatf_test_case "O_flag_syslog_relayed" "cleanup"
13165c61840SMichal ScigockiO_flag_syslog_relayed_head()
13265c61840SMichal Scigocki{
13365c61840SMichal Scigocki    atf_set descr "syslog format test on a relayed syslog message"
13465c61840SMichal Scigocki    set_common_atf_metadata
13565c61840SMichal Scigocki}
13665c61840SMichal ScigockiO_flag_syslog_relayed_body()
13765c61840SMichal Scigocki{
13865c61840SMichal Scigocki    local format="syslog"
13965c61840SMichal Scigocki    local logfile="${PWD}/${format}_relayed.log"
14065c61840SMichal Scigocki    local pcapfile="${PWD}/${format}_relayed.pcap"
14165c61840SMichal Scigocki
14265c61840SMichal Scigocki    setup_relayed_format_test "${format}" "${logfile}" "${pcapfile}"
14365c61840SMichal Scigocki
14465c61840SMichal Scigocki    atf_check -s exit:0 -o match:"${REGEX_RFC5424_LOGFILE}" cat "${logfile}"
14565c61840SMichal Scigocki    atf_check -s exit:0 -e ignore -o match:"${REGEX_RFC5424_PAYLOAD}" \
14665c61840SMichal Scigocki        tcpdump -A -r "${pcapfile}"
14765c61840SMichal Scigocki}
14865c61840SMichal ScigockiO_flag_syslog_relayed_cleanup()
14965c61840SMichal Scigocki{
15065c61840SMichal Scigocki    syslogd_stop_on_ports \
15165c61840SMichal Scigocki        "${SERVER_1_PORT}" \
15265c61840SMichal Scigocki        "${SERVER_2_PORT}" \
15365c61840SMichal Scigocki        "${SERVER_3_PORT}"
15465c61840SMichal Scigocki}
15565c61840SMichal Scigocki
15665c61840SMichal Scigockiatf_test_case "O_flag_rfc5424_relayed" "cleanup"
15765c61840SMichal ScigockiO_flag_rfc5424_relayed_head()
15865c61840SMichal Scigocki{
15965c61840SMichal Scigocki    atf_set descr "rfc5424 format test on a relayed syslog message"
16065c61840SMichal Scigocki    set_common_atf_metadata
16165c61840SMichal Scigocki}
16265c61840SMichal ScigockiO_flag_rfc5424_relayed_body()
16365c61840SMichal Scigocki{
16465c61840SMichal Scigocki    local format="rfc5424"
16565c61840SMichal Scigocki    local logfile="${PWD}/${format}_relayed.log"
16665c61840SMichal Scigocki    local pcapfile="${PWD}/${format}_relayed.pcap"
16765c61840SMichal Scigocki
16865c61840SMichal Scigocki    setup_relayed_format_test "${format}" "${logfile}" "${pcapfile}"
16965c61840SMichal Scigocki
17065c61840SMichal Scigocki    atf_check -s exit:0 -o match:"${REGEX_RFC5424_LOGFILE}" cat "${logfile}"
17165c61840SMichal Scigocki    atf_check -s exit:0 -e ignore -o match:"${REGEX_RFC5424_PAYLOAD}" \
17265c61840SMichal Scigocki        tcpdump -A -r "${pcapfile}"
17365c61840SMichal Scigocki}
17465c61840SMichal ScigockiO_flag_rfc5424_relayed_cleanup()
17565c61840SMichal Scigocki{
17665c61840SMichal Scigocki    syslogd_stop_on_ports \
17765c61840SMichal Scigocki        "${SERVER_1_PORT}" \
17865c61840SMichal Scigocki        "${SERVER_2_PORT}" \
17965c61840SMichal Scigocki        "${SERVER_3_PORT}"
18065c61840SMichal Scigocki}
18165c61840SMichal Scigocki
18265c61840SMichal Scigockiatf_init_test_cases()
18365c61840SMichal Scigocki{
18465c61840SMichal Scigocki    atf_add_test_case "O_flag_bsd_relayed"
18565c61840SMichal Scigocki    atf_add_test_case "O_flag_rfc3164_relayed"
186*8cc248fbSMichal Scigocki    atf_add_test_case "O_flag_rfc3164strict_relayed"
18765c61840SMichal Scigocki    atf_add_test_case "O_flag_syslog_relayed"
18865c61840SMichal Scigocki    atf_add_test_case "O_flag_rfc5424_relayed"
18965c61840SMichal Scigocki}
190