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