xref: /freebsd/usr.sbin/syslogd/tests/syslogd_forwarded_format_test.sh (revision 8cc248fb92bfae1e6ef4e2b48a0064377c4b3c11)
1ee75c991SMichal Scigocki#-
2ee75c991SMichal Scigocki# SPDX-License-Identifier: BSD-2-Clause
3ee75c991SMichal Scigocki#
4ee75c991SMichal Scigocki# Copyright (c) 2024 Michal Scigocki <michal.os@hotmail.com>
5ee75c991SMichal Scigocki#
6ee75c991SMichal Scigocki
7ee75c991SMichal Scigocki. $(atf_get_srcdir)/syslogd_format_test_common.sh
8ee75c991SMichal Scigocki
9ee75c991SMichal ScigockiSERVER_1_PORT="5140"
10ee75c991SMichal ScigockiSERVER_2_PORT="5141"
11ee75c991SMichal Scigocki
12ee75c991SMichal Scigocki# Forwarded Message Tests
13ee75c991SMichal Scigocki# Two servers, one sending syslog messages to the other over UDP
14ee75c991SMichal Scigockisetup_forwarded_format_test()
15ee75c991SMichal Scigocki{
16ee75c991SMichal Scigocki    local format="$1"
17ee75c991SMichal Scigocki    local logfile="$2"
18ee75c991SMichal Scigocki    local pcapfile="$3"
19ee75c991SMichal Scigocki
20ee75c991SMichal Scigocki    confirm_INET_support_or_skip
21ee75c991SMichal Scigocki
22ee75c991SMichal Scigocki    # Begin packet capture for single packet
23ee75c991SMichal Scigocki    tcpdump --immediate-mode -c 1 -i lo0 -w "${pcapfile}" \
24ee75c991SMichal Scigocki        dst port "${SERVER_1_PORT}" &
25ee75c991SMichal Scigocki    tcpdump_pid="$!"
26ee75c991SMichal Scigocki
27ee75c991SMichal Scigocki    # Start first server: receive UDP, log to file
28ee75c991SMichal Scigocki    printf "user.debug\t${logfile}\n" > "$(config_filename ${SERVER_1_PORT})"
29ee75c991SMichal Scigocki    syslogd_start_on_port "${SERVER_1_PORT}" -O "${format}"
30ee75c991SMichal Scigocki
31ee75c991SMichal Scigocki    # Start second server: send UDP, log to first server
32ee75c991SMichal Scigocki    printf "user.debug\t@127.0.0.1:${SERVER_1_PORT}\n" \
33ee75c991SMichal Scigocki        > "$(config_filename ${SERVER_2_PORT})"
34ee75c991SMichal Scigocki    syslogd_start_on_port "${SERVER_2_PORT}" -O "${format}"
35ee75c991SMichal Scigocki
36ee75c991SMichal Scigocki    # Send test syslog message
37ee75c991SMichal Scigocki    syslogd_log -4 -p user.debug -t "${TAG}" -h 127.0.0.1 \
38ee75c991SMichal Scigocki        -P "${SERVER_2_PORT}" -H "${HOSTNAME}" "${MSG}"
39ee75c991SMichal Scigocki
40ee75c991SMichal Scigocki    wait "${tcpdump_pid}" # Wait for packet capture to finish
41ee75c991SMichal Scigocki}
42ee75c991SMichal Scigocki
43ee75c991SMichal Scigockiatf_test_case "O_flag_bsd_forwarded" "cleanup"
44ee75c991SMichal ScigockiO_flag_bsd_forwarded_head()
45ee75c991SMichal Scigocki{
46ee75c991SMichal Scigocki    atf_set descr "bsd format test on a forwarded syslog message"
47ee75c991SMichal Scigocki    set_common_atf_metadata
48ee75c991SMichal Scigocki}
49ee75c991SMichal ScigockiO_flag_bsd_forwarded_body()
50ee75c991SMichal Scigocki{
51ee75c991SMichal Scigocki    local format="bsd"
52ee75c991SMichal Scigocki    local logfile="${PWD}/${format}_forwarded.log"
53ee75c991SMichal Scigocki    local pcapfile="${PWD}/${format}_forwarded.pcap"
54ee75c991SMichal Scigocki
55ee75c991SMichal Scigocki    setup_forwarded_format_test "${format}" "${logfile}" "${pcapfile}"
56ee75c991SMichal Scigocki
57ee75c991SMichal Scigocki    atf_expect_fail \
58ee75c991SMichal Scigocki        "PR 220246 syslog -O bsd deviates from RFC 3164 recommendations"
59ee75c991SMichal Scigocki    atf_check -s exit:0 -o match:"${REGEX_RFC3164_LOGFILE}" cat "${logfile}"
60ee75c991SMichal Scigocki    atf_check -s exit:0 -e ignore -o match:"${REGEX_RFC3164_PAYLOAD}" \
61ee75c991SMichal Scigocki        tcpdump -A -r "${pcapfile}"
62ee75c991SMichal Scigocki}
63ee75c991SMichal ScigockiO_flag_bsd_forwarded_cleanup()
64ee75c991SMichal Scigocki{
65ee75c991SMichal Scigocki    syslogd_stop_on_ports \
66ee75c991SMichal Scigocki        "${SERVER_1_PORT}" \
67ee75c991SMichal Scigocki        "${SERVER_2_PORT}"
68ee75c991SMichal Scigocki}
69ee75c991SMichal Scigocki
70ee75c991SMichal Scigockiatf_test_case "O_flag_rfc3164_forwarded" "cleanup"
71ee75c991SMichal ScigockiO_flag_rfc3164_forwarded_head()
72ee75c991SMichal Scigocki{
73ee75c991SMichal Scigocki    atf_set descr "rfc3164 format test on a forwarded syslog message"
74ee75c991SMichal Scigocki    set_common_atf_metadata
75ee75c991SMichal Scigocki}
76ee75c991SMichal ScigockiO_flag_rfc3164_forwarded_body()
77ee75c991SMichal Scigocki{
78ee75c991SMichal Scigocki    local format="rfc3164"
79ee75c991SMichal Scigocki    local logfile="${PWD}/${format}_forwarded.log"
80ee75c991SMichal Scigocki    local pcapfile="${PWD}/${format}_forwarded.pcap"
81ee75c991SMichal Scigocki
82ee75c991SMichal Scigocki    setup_forwarded_format_test "${format}" "${logfile}" "${pcapfile}"
83ee75c991SMichal Scigocki
84ee75c991SMichal Scigocki    atf_expect_fail \
85ee75c991SMichal Scigocki        "PR 220246 syslog -O rfc3164 deviates from RFC 3164 recommendations"
86ee75c991SMichal Scigocki    atf_check -s exit:0 -o match:"${REGEX_RFC3164_LOGFILE}" cat "${logfile}"
87ee75c991SMichal Scigocki    atf_check -s exit:0 -e ignore -o match:"${REGEX_RFC3164_PAYLOAD}" \
88ee75c991SMichal Scigocki        tcpdump -A -r "${pcapfile}"
89ee75c991SMichal Scigocki}
90ee75c991SMichal ScigockiO_flag_rfc3164_forwarded_cleanup()
91ee75c991SMichal Scigocki{
92ee75c991SMichal Scigocki    syslogd_stop_on_ports \
93ee75c991SMichal Scigocki        "${SERVER_1_PORT}" \
94ee75c991SMichal Scigocki        "${SERVER_2_PORT}"
95ee75c991SMichal Scigocki}
96ee75c991SMichal Scigocki
97*8cc248fbSMichal Scigockiatf_test_case "O_flag_rfc3164strict_forwarded" "cleanup"
98*8cc248fbSMichal ScigockiO_flag_rfc3164strict_forwarded_head()
99*8cc248fbSMichal Scigocki{
100*8cc248fbSMichal Scigocki    atf_set descr "rfc3164-strict format test on a forwarded syslog message"
101*8cc248fbSMichal Scigocki    set_common_atf_metadata
102*8cc248fbSMichal Scigocki}
103*8cc248fbSMichal ScigockiO_flag_rfc3164strict_forwarded_body()
104*8cc248fbSMichal Scigocki{
105*8cc248fbSMichal Scigocki    local format="rfc3164-strict"
106*8cc248fbSMichal Scigocki    local logfile="${PWD}/${format}_forwarded.log"
107*8cc248fbSMichal Scigocki    local pcapfile="${PWD}/${format}_forwarded.pcap"
108*8cc248fbSMichal Scigocki
109*8cc248fbSMichal Scigocki    setup_forwarded_format_test "${format}" "${logfile}" "${pcapfile}"
110*8cc248fbSMichal Scigocki
111*8cc248fbSMichal Scigocki    atf_check -s exit:0 -o match:"${REGEX_RFC3164_LOGFILE}" cat "${logfile}"
112*8cc248fbSMichal Scigocki    atf_check -s exit:0 -e ignore -o match:"${REGEX_RFC3164_PAYLOAD}" \
113*8cc248fbSMichal Scigocki        tcpdump -A -r "${pcapfile}"
114*8cc248fbSMichal Scigocki}
115*8cc248fbSMichal ScigockiO_flag_rfc3164strict_forwarded_cleanup()
116*8cc248fbSMichal Scigocki{
117*8cc248fbSMichal Scigocki    syslogd_stop_on_ports \
118*8cc248fbSMichal Scigocki        "${SYSLOGD_UDP_PORT_1}" \
119*8cc248fbSMichal Scigocki        "${SYSLOGD_UDP_PORT_2}"
120*8cc248fbSMichal Scigocki}
121*8cc248fbSMichal Scigocki
122ee75c991SMichal Scigockiatf_test_case "O_flag_syslog_forwarded" "cleanup"
123ee75c991SMichal ScigockiO_flag_syslog_forwarded_head()
124ee75c991SMichal Scigocki{
125ee75c991SMichal Scigocki    atf_set descr "syslog format test on a forwarded syslog message"
126ee75c991SMichal Scigocki    set_common_atf_metadata
127ee75c991SMichal Scigocki}
128ee75c991SMichal ScigockiO_flag_syslog_forwarded_body()
129ee75c991SMichal Scigocki{
130ee75c991SMichal Scigocki    local format="syslog"
131ee75c991SMichal Scigocki    local logfile="${PWD}/${format}_forwarded.log"
132ee75c991SMichal Scigocki    local pcapfile="${PWD}/${format}_forwarded.pcap"
133ee75c991SMichal Scigocki
134ee75c991SMichal Scigocki    setup_forwarded_format_test "${format}" "${logfile}" "${pcapfile}"
135ee75c991SMichal Scigocki
136ee75c991SMichal Scigocki    atf_check -s exit:0 -o match:"${REGEX_RFC5424_LOGFILE}" cat "${logfile}"
137ee75c991SMichal Scigocki    atf_check -s exit:0 -e ignore -o match:"${REGEX_RFC5424_PAYLOAD}" \
138ee75c991SMichal Scigocki        tcpdump -A -r "${pcapfile}"
139ee75c991SMichal Scigocki}
140ee75c991SMichal ScigockiO_flag_syslog_forwarded_cleanup()
141ee75c991SMichal Scigocki{
142ee75c991SMichal Scigocki    syslogd_stop_on_ports \
143ee75c991SMichal Scigocki        "${SERVER_1_PORT}" \
144ee75c991SMichal Scigocki        "${SERVER_2_PORT}"
145ee75c991SMichal Scigocki}
146ee75c991SMichal Scigocki
147ee75c991SMichal Scigockiatf_test_case "O_flag_rfc5424_forwarded" "cleanup"
148ee75c991SMichal ScigockiO_flag_rfc5424_forwarded_head()
149ee75c991SMichal Scigocki{
150ee75c991SMichal Scigocki    atf_set descr "rfc5424 format test on a forwarded syslog message"
151ee75c991SMichal Scigocki    set_common_atf_metadata
152ee75c991SMichal Scigocki}
153ee75c991SMichal ScigockiO_flag_rfc5424_forwarded_body()
154ee75c991SMichal Scigocki{
155ee75c991SMichal Scigocki    local format="rfc5424"
156ee75c991SMichal Scigocki    local logfile="${PWD}/${format}_forwarded.log"
157ee75c991SMichal Scigocki    local pcapfile="${PWD}/${format}_forwarded.pcap"
158ee75c991SMichal Scigocki
159ee75c991SMichal Scigocki    setup_forwarded_format_test "${format}" "${logfile}" "${pcapfile}"
160ee75c991SMichal Scigocki
161ee75c991SMichal Scigocki    atf_check -s exit:0 -o match:"${REGEX_RFC5424_LOGFILE}" cat "${logfile}"
162ee75c991SMichal Scigocki    atf_check -s exit:0 -e ignore -o match:"${REGEX_RFC5424_PAYLOAD}" \
163ee75c991SMichal Scigocki        tcpdump -A -r "${pcapfile}"
164ee75c991SMichal Scigocki}
165ee75c991SMichal ScigockiO_flag_rfc5424_forwarded_cleanup()
166ee75c991SMichal Scigocki{
167ee75c991SMichal Scigocki    syslogd_stop_on_ports \
168ee75c991SMichal Scigocki        "${SERVER_1_PORT}" \
169ee75c991SMichal Scigocki        "${SERVER_2_PORT}"
170ee75c991SMichal Scigocki}
171ee75c991SMichal Scigocki
172ee75c991SMichal Scigocki# Legacy bsd/rfc3164 format tests
173ee75c991SMichal Scigocki# The legacy syntax was introduced in FreeBSD PR 7055, circa 1998
174ee75c991SMichal Scigockiatf_test_case "O_flag_bsd_forwarded_legacy" "cleanup"
175ee75c991SMichal ScigockiO_flag_bsd_forwarded_legacy_head()
176ee75c991SMichal Scigocki{
177ee75c991SMichal Scigocki    atf_set descr "legacy bsd format test on a forwarded syslog message"
178ee75c991SMichal Scigocki    set_common_atf_metadata
179ee75c991SMichal Scigocki}
180ee75c991SMichal ScigockiO_flag_bsd_forwarded_legacy_body()
181ee75c991SMichal Scigocki{
182ee75c991SMichal Scigocki    local format="bsd"
183ee75c991SMichal Scigocki    local logfile="${PWD}/${format}_forwarded_legacy.log"
184ee75c991SMichal Scigocki    local pcapfile="${PWD}/${format}_forwarded.pcap"
185ee75c991SMichal Scigocki
186ee75c991SMichal Scigocki    setup_forwarded_format_test "${format}" "${logfile}" "${pcapfile}"
187ee75c991SMichal Scigocki
188ee75c991SMichal Scigocki    atf_check -s exit:0 -o match:"${REGEX_RFC3164_LEGACY_LOGFILE}" \
189ee75c991SMichal Scigocki        cat "${logfile}"
190ee75c991SMichal Scigocki    atf_check -s exit:0 -e ignore \
191ee75c991SMichal Scigocki        -o match:"${REGEX_RFC3164_LEGACY_PAYLOAD}" \
192ee75c991SMichal Scigocki        tcpdump -A -r "${pcapfile}"
193ee75c991SMichal Scigocki}
194ee75c991SMichal ScigockiO_flag_bsd_forwarded_legacy_cleanup()
195ee75c991SMichal Scigocki{
196ee75c991SMichal Scigocki    syslogd_stop_on_ports \
197ee75c991SMichal Scigocki        "${SERVER_1_PORT}" \
198ee75c991SMichal Scigocki        "${SERVER_2_PORT}"
199ee75c991SMichal Scigocki}
200ee75c991SMichal Scigocki
201ee75c991SMichal Scigockiatf_test_case "O_flag_rfc3164_forwarded_legacy" "cleanup"
202ee75c991SMichal ScigockiO_flag_rfc3164_forwarded_legacy_head()
203ee75c991SMichal Scigocki{
204ee75c991SMichal Scigocki    atf_set descr \
205ee75c991SMichal Scigocki        "legacy rfc3164 format test on a forwarded syslog message"
206ee75c991SMichal Scigocki    set_common_atf_metadata
207ee75c991SMichal Scigocki}
208ee75c991SMichal ScigockiO_flag_rfc3164_forwarded_legacy_body()
209ee75c991SMichal Scigocki{
210ee75c991SMichal Scigocki    local format="rfc3164"
211ee75c991SMichal Scigocki    local logfile="${PWD}/${format}_forwarded_legacy.log"
212ee75c991SMichal Scigocki    local pcapfile="${PWD}/${format}_forwarded.pcap"
213ee75c991SMichal Scigocki
214ee75c991SMichal Scigocki    setup_forwarded_format_test "${format}" "${logfile}" "${pcapfile}"
215ee75c991SMichal Scigocki
216ee75c991SMichal Scigocki    atf_check -s exit:0 -o match:"${REGEX_RFC3164_LEGACY_LOGFILE}" \
217ee75c991SMichal Scigocki        cat "${logfile}"
218ee75c991SMichal Scigocki    atf_check -s exit:0 -e ignore \
219ee75c991SMichal Scigocki        -o match:"${REGEX_RFC3164_LEGACY_PAYLOAD}" \
220ee75c991SMichal Scigocki        tcpdump -A -r "${pcapfile}"
221ee75c991SMichal Scigocki}
222ee75c991SMichal ScigockiO_flag_rfc3164_forwarded_legacy_cleanup()
223ee75c991SMichal Scigocki{
224ee75c991SMichal Scigocki    syslogd_stop_on_ports \
225ee75c991SMichal Scigocki        "${SERVER_1_PORT}" \
226ee75c991SMichal Scigocki        "${SERVER_2_PORT}"
227ee75c991SMichal Scigocki}
228ee75c991SMichal Scigocki
229ee75c991SMichal Scigockiatf_init_test_cases()
230ee75c991SMichal Scigocki{
231ee75c991SMichal Scigocki    atf_add_test_case "O_flag_bsd_forwarded"
232ee75c991SMichal Scigocki    atf_add_test_case "O_flag_rfc3164_forwarded"
233*8cc248fbSMichal Scigocki    atf_add_test_case "O_flag_rfc3164strict_forwarded"
234ee75c991SMichal Scigocki    atf_add_test_case "O_flag_syslog_forwarded"
235ee75c991SMichal Scigocki    atf_add_test_case "O_flag_rfc5424_forwarded"
236ee75c991SMichal Scigocki
237ee75c991SMichal Scigocki    atf_add_test_case "O_flag_bsd_forwarded_legacy"
238ee75c991SMichal Scigocki    atf_add_test_case "O_flag_rfc3164_forwarded_legacy"
239ee75c991SMichal Scigocki}
240