xref: /freebsd/usr.sbin/syslogd/tests/syslogd_test.sh (revision 66a022a30dd968631c64b73e473dd16589592fad)
1#-
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2021, 2023 The FreeBSD Foundation
5#
6# This software was developed by Mark Johnston under sponsorship from
7# the FreeBSD Foundation.
8#
9# This software was developed by Jake Freeland under sponsorship from
10# the FreeBSD Foundation.
11#
12
13# Tests to-do:
14# actions: users
15
16. $(atf_get_srcdir)/syslogd_test_common.sh
17
18atf_test_case "unix" "cleanup"
19unix_head()
20{
21    atf_set descr "Messages are logged over UNIX transport"
22}
23unix_body()
24{
25    local logfile="${PWD}/unix.log"
26
27    printf "user.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}"
28    syslogd_start
29
30    syslogd_log -p user.debug -t unix -h "${SYSLOGD_LOCAL_SOCKET}" \
31        "hello, world (unix)"
32    atf_check -s exit:0 -o match:"unix: hello, world \(unix\)" \
33        tail -n 1 "${logfile}"
34}
35unix_cleanup()
36{
37    syslogd_stop
38}
39
40atf_test_case "inet" "cleanup"
41inet_head()
42{
43    atf_set descr "Messages are logged over INET transport"
44}
45inet_body()
46{
47    local logfile="${PWD}/inet.log"
48
49    [ "$(sysctl -n kern.features.inet)" != "1" ] &&
50        atf_skip "Kernel does not support INET"
51
52    printf "user.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}"
53    syslogd_start
54
55    # We have INET transport; make sure we can use it.
56    syslogd_log -4 -p user.debug -t inet -h 127.0.0.1 -P "${SYSLOGD_UDP_PORT}" \
57        "hello, world (v4)"
58    atf_check -s exit:0 -o match:"inet: hello, world \(v4\)" \
59        tail -n 1 "${logfile}"
60}
61inet_cleanup()
62{
63    syslogd_stop
64}
65
66atf_test_case "inet6" "cleanup"
67inet6_head()
68{
69    atf_set descr "Messages are logged over INET6 transport"
70}
71inet6_body()
72{
73    local logfile="${PWD}/inet6.log"
74
75    [ "$(sysctl -n kern.features.inet6)" != "1" ] &&
76        atf_skip "Kernel does not support INET6"
77
78    printf "user.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}"
79    syslogd_start
80
81    # We have INET6 transport; make sure we can use it.
82    syslogd_log -6 -p user.debug -t unix -h ::1 -P "${SYSLOGD_UDP_PORT}" \
83        "hello, world (v6)"
84    atf_check -s exit:0 -o match:"unix: hello, world \(v6\)" \
85        tail -n 1 "${logfile}"
86}
87inet6_cleanup()
88{
89    syslogd_stop
90}
91
92atf_test_case "reload" "cleanup"
93reload_head()
94{
95    atf_set descr "SIGHUP correctly refreshes configuration"
96}
97reload_body()
98{
99    logfile="${PWD}/reload.log"
100    printf "user.debug\t/${logfile}\n" > "${SYSLOGD_CONFIG}"
101    syslogd_start
102
103    syslogd_log -p user.debug -t reload -h "${SYSLOGD_LOCAL_SOCKET}" \
104        "pre-reload"
105    atf_check -s exit:0 -o match:"reload: pre-reload" tail -n 1 "${logfile}"
106
107    # Override the old rule.
108    truncate -s 0 "${logfile}"
109    printf "news.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}"
110    syslogd_reload
111
112    syslogd_log -p user.debug -t reload -h "${SYSLOGD_LOCAL_SOCKET}" \
113        "post-reload user"
114    syslogd_log -p news.debug -t reload -h "${SYSLOGD_LOCAL_SOCKET}" \
115        "post-reload news"
116    atf_check -s exit:0 -o not-match:"reload: post-reload user" cat ${logfile}
117    atf_check -s exit:0 -o match:"reload: post-reload news" cat ${logfile}
118}
119reload_cleanup()
120{
121    syslogd_stop
122}
123
124atf_test_case "prog_filter" "cleanup"
125prog_filter_head()
126{
127    atf_set descr "Messages are only received from programs in the filter"
128}
129prog_filter_body()
130{
131    logfile="${PWD}/prog_filter.log"
132    printf "!prog1,prog2\nuser.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}"
133    syslogd_start
134
135    for i in 1 2 3; do
136        syslogd_log -p user.debug -t "prog${i}" -h "${SYSLOGD_LOCAL_SOCKET}" \
137            "hello this is prog${i}"
138    done
139    atf_check -s exit:0 -o match:"prog1: hello this is prog1" cat "${logfile}"
140    atf_check -s exit:0 -o match:"prog2: hello this is prog2" cat "${logfile}"
141    atf_check -s exit:0 -o not-match:"prog3: hello this is prog3" cat "${logfile}"
142
143    # Override the old rule.
144    truncate -s 0 ${logfile}
145    printf "!-prog1,prog2\nuser.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}"
146    syslogd_reload
147
148    for i in 1 2 3; do
149        syslogd_log -p user.debug -t "prog${i}" -h "${SYSLOGD_LOCAL_SOCKET}" \
150            "hello this is prog${i}"
151    done
152    atf_check -s exit:0 -o not-match:"prog1: hello this is prog1" cat "${logfile}"
153    atf_check -s exit:0 -o not-match:"prog2: hello this is prog2" cat "${logfile}"
154    atf_check -s exit:0 -o match:"prog3: hello this is prog3" cat "${logfile}"
155}
156prog_filter_cleanup()
157{
158    syslogd_stop
159}
160
161atf_test_case "host_filter" "cleanup"
162host_filter_head()
163{
164    atf_set descr "Messages are only received from hostnames in the filter"
165}
166host_filter_body()
167{
168    logfile="${PWD}/host_filter.log"
169    printf "+host1,host2\nuser.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}"
170    syslogd_start
171
172    for i in 1 2 3; do
173        syslogd_log -p user.debug -t "host${i}" -H "host${i}" \
174            -h "${SYSLOGD_LOCAL_SOCKET}" "hello this is host${i}"
175    done
176    atf_check -s exit:0 -o match:"host1: hello this is host1" cat "${logfile}"
177    atf_check -s exit:0 -o match:"host2: hello this is host2" cat "${logfile}"
178    atf_check -s exit:0 -o not-match:"host3: hello this is host3" cat "${logfile}"
179
180    # Override the old rule.
181    truncate -s 0 ${logfile}
182    printf "\-host1,host2\nuser.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}"
183    syslogd_reload
184
185    for i in 1 2 3; do
186        syslogd_log -p user.debug -t "host${i}" -H "host${i}" \
187        -h "${SYSLOGD_LOCAL_SOCKET}" "hello this is host${i}"
188    done
189    atf_check -s exit:0 -o not-match:"host1: hello this is host1" cat "${logfile}"
190    atf_check -s exit:0 -o not-match:"host2: hello this is host2" cat "${logfile}"
191    atf_check -s exit:0 -o match:"host3: hello this is host3" cat "${logfile}"
192}
193host_filter_cleanup()
194{
195    syslogd_stop
196}
197
198atf_test_case "prop_filter" "cleanup"
199prop_filter_head()
200{
201    atf_set descr "Messages are received based on conditions in the propery based filter"
202}
203prop_filter_body()
204{
205    logfile="${PWD}/prop_filter.log"
206    printf ":msg,contains,\"FreeBSD\"\nuser.debug\t${logfile}\n" \
207        > "${SYSLOGD_CONFIG}"
208    syslogd_start
209
210    syslogd_log -p user.debug -t "prop1" -h "${SYSLOGD_LOCAL_SOCKET}" "FreeBSD"
211    syslogd_log -p user.debug -t "prop2" -h "${SYSLOGD_LOCAL_SOCKET}" "freebsd"
212    atf_check -s exit:0 -o match:"prop1: FreeBSD" cat "${logfile}"
213    atf_check -s exit:0 -o not-match:"prop2: freebsd" cat "${logfile}"
214
215    truncate -s 0 ${logfile}
216    printf ":msg,!contains,\"FreeBSD\"\nuser.debug\t${logfile}\n" \
217        > "${SYSLOGD_CONFIG}"
218    syslogd_reload
219
220    syslogd_log -p user.debug -t "prop1" -h "${SYSLOGD_LOCAL_SOCKET}" "FreeBSD"
221    syslogd_log -p user.debug -t "prop2" -h "${SYSLOGD_LOCAL_SOCKET}" "freebsd"
222    atf_check -s exit:0 -o not-match:"prop1: FreeBSD" cat "${logfile}"
223    atf_check -s exit:0 -o match:"prop2: freebsd" cat "${logfile}"
224
225    truncate -s 0 ${logfile}
226    printf ":msg,icase_contains,\"FreeBSD\"\nuser.debug\t${logfile}\n" \
227        > "${SYSLOGD_CONFIG}"
228    syslogd_reload
229
230    syslogd_log -p user.debug -t "prop1" -h "${SYSLOGD_LOCAL_SOCKET}" "FreeBSD"
231    syslogd_log -p user.debug -t "prop2" -h "${SYSLOGD_LOCAL_SOCKET}" "freebsd"
232    atf_check -s exit:0 -o match:"prop1: FreeBSD" cat "${logfile}"
233    atf_check -s exit:0 -o match:"prop2: freebsd" cat "${logfile}"
234
235    truncate -s 0 ${logfile}
236    printf ":msg,!icase_contains,\"FreeBSD\"\nuser.debug\t${logfile}\n" \
237        > "${SYSLOGD_CONFIG}"
238    syslogd_reload
239
240    syslogd_log -p user.debug -t "prop1" -h "${SYSLOGD_LOCAL_SOCKET}" "FreeBSD"
241    syslogd_log -p user.debug -t "prop2" -h "${SYSLOGD_LOCAL_SOCKET}" "freebsd"
242    syslogd_log -p user.debug -t "prop3" -h "${SYSLOGD_LOCAL_SOCKET}" "Solaris"
243    atf_check -s exit:0 -o not-match:"prop1: FreeBSD" cat "${logfile}"
244    atf_check -s exit:0 -o not-match:"prop2: freebsd" cat "${logfile}"
245    atf_check -s exit:0 -o match:"prop3: Solaris" cat "${logfile}"
246}
247prop_filter_cleanup()
248{
249    syslogd_stop
250}
251
252atf_test_case "host_action" "cleanup"
253host_action_head()
254{
255    atf_set descr "Sends a message to a specified host"
256}
257host_action_body()
258{
259    local addr="192.0.2.100"
260    local logfile="${PWD}/host_action.log"
261
262    atf_check ifconfig lo1 create
263    atf_check ifconfig lo1 inet "${addr}/24"
264    atf_check ifconfig lo1 up
265
266    printf "user.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}"
267    syslogd_start -b "${addr}"
268
269    printf "user.debug\t@${addr}\n" > "${SYSLOGD_CONFIG}.2"
270    syslogd_start \
271        -f "${SYSLOGD_CONFIG}.2" \
272        -P "${SYSLOGD_PIDFILE}.2" \
273        -p "${SYSLOGD_LOCAL_SOCKET}.2" \
274        -S "${SYSLOGD_LOCAL_PRIVSOCKET}.2"
275
276    syslogd_log -p user.debug -t "test" -h "${SYSLOGD_LOCAL_SOCKET}.2" \
277        "message from syslogd2"
278    atf_check -s exit:0 -o match:"test: message from syslogd2" \
279        cat "${logfile}"
280}
281host_action_cleanup()
282{
283    syslogd_stop
284    syslogd_stop \
285        "${SYSLOGD_PIDFILE}.2" \
286        "${SYSLOGD_LOCAL_SOCKET}.2" \
287        "${SYSLOGD_LOCAL_PRIVSOCKET}.2"
288    atf_check ifconfig lo1 destroy
289}
290
291atf_test_case "pipe_action" "cleanup"
292pipe_action_head()
293{
294    atf_set descr "The pipe action evaluates provided command in sh(1)"
295}
296pipe_action_body()
297{
298    logfile="${PWD}/pipe_action.log"
299    printf "\"While I'm digging in the tunnel, the elves will often come to me \
300        with solutions to my problem.\"\n-Saymore Crey" > ${logfile}
301
302    printf "!pipe\nuser.debug\t| sed -i '' -e 's/Saymore Crey/Seymour Cray/g' \
303        ${logfile}\n" > "${SYSLOGD_CONFIG}"
304    syslogd_start
305
306    syslogd_log -p user.debug -t "pipe" -h "${SYSLOGD_LOCAL_SOCKET}" \
307        "fix spelling error"
308    atf_check -s exit:0 -o match:"Seymour Cray" cat "${logfile}"
309}
310pipe_action_cleanup()
311{
312    syslogd_stop
313}
314
315atf_test_case "jail_noinet" "cleanup"
316jail_noinet_head()
317{
318    atf_set descr "syslogd -ss can be run in a jail without INET support"
319    atf_set require.user root
320}
321jail_noinet_body()
322{
323    local logfile
324
325    atf_check jail -c name=syslogd_noinet persist
326
327    logfile="${PWD}/jail_noinet.log"
328    printf "user.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}"
329    syslogd_start -j syslogd_noinet -s -s
330
331    syslogd_log -p user.debug -t "test" -h "${SYSLOGD_LOCAL_SOCKET}" \
332        "hello, world"
333    atf_check -s exit:0 -o match:"test: hello, world" cat "${logfile}"
334}
335jail_noinet_cleanup()
336{
337    jail -r syslogd_noinet
338}
339
340atf_init_test_cases()
341{
342    atf_add_test_case "unix"
343    atf_add_test_case "inet"
344    atf_add_test_case "inet6"
345    atf_add_test_case "reload"
346    atf_add_test_case "prog_filter"
347    atf_add_test_case "host_filter"
348    atf_add_test_case "prop_filter"
349    atf_add_test_case "host_action"
350    atf_add_test_case "pipe_action"
351    atf_add_test_case "jail_noinet"
352}
353