From nobody Thu Aug 31 13:55:37 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Rc2k15vN4z4sC09; Thu, 31 Aug 2023 13:55:37 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Rc2k15KWDz3YsF; Thu, 31 Aug 2023 13:55:37 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1693490137; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nVXhHfJWwvsCragnb8diTCg3X0BZyZki5d5tMv74qLg=; b=tM2RVyCVXrhs+H4iGHxpU//AiY/ba/bpHn6e6HNgjHU9fB9HhfHvHMmsoskjSPDUaRClaC cAkvFESRDUfn4H0SkwhFSn4zvZZ2mU0bRVHc55polTVlBo4ZJ9OXMAReEsNmoU5VAkRv5D qDaW7nUPyW87JUkMLduVPLhRM0bhoNEI3/Yf6EFPf2yflY6JBSzlRErWzyXPkH1nnK1KN7 bqBCS0SpY5cGT3Zo5P77oCapl0OpPFPt2Vtv9Svqtd9Bh6Fvjor6eOYahg+ea8pV3q9KdW J8nu3cMG97ZkhEi+jVrGgwu1w/RYIHHv45M7ggRi26GyEfBAY9TEezvMrdb48g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1693490137; a=rsa-sha256; cv=none; b=MMQlg0XSgbIaRJUA29FFVDriBLsyhSK9UzYHSoGhtnLh4XqW4N0keonW7UMYcpofmbGcr2 ZLiHWorxjulja4PYNPrP+vuhoU/6afL3jhnXHEiVm66HAiB3+1zAfa6h+5+3pcBK86Tkj3 XxyCCMnA1JTDVa7+kgHlvB/hlg2jSYAZd02HmJlkaBK1cAG0RP9nn1lTpahjoTqSPMOT1h t99FX1YsiGSeIGrKtR680rHqzU5jD8dXy8kTo7nas5e83wrf2gBTD33m9NkH7BYQUbzle6 pkfx+74mwKwU9PHbi5mXRUKck7Ebusw3Pt2LqS4j7NCcRsV+G4JLdF1TjK1ZjA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1693490137; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nVXhHfJWwvsCragnb8diTCg3X0BZyZki5d5tMv74qLg=; b=U8WQMYmqDU0ty57YthUbeFwxGr5h4d+WY6cADjEYX7/2SbC/BOH/AZEikT2Vlqvhf/UEUd KGuh3JcAZXNRaNXCKOSTECyR4UI1mLmjvt6RbxdffNLFzKyAR+kIOSdpfUmYLsN/pU6Q96 LV/xYsl3VAUYY8uaYgtcrNfvgQlKZCBz87R/hsk7h5VWkHfKhQaezeoownPwm6RVfkjhKT 8iunnNaKqkUd+2I0xH6gMbyI4N8pDMA5bB4dY5HKEer+lMUg4E1nk2MoK1PGLrr098h4jm no8cEKKvun1yKfasbTYUtDXdlq/kPzY7VSlZI/srUv6vAnvppZ5wi0yoGAeSvQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Rc2k14N38z5td; Thu, 31 Aug 2023 13:55:37 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 37VDtbmZ083344; Thu, 31 Aug 2023 13:55:37 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 37VDtbLP083341; Thu, 31 Aug 2023 13:55:37 GMT (envelope-from git) Date: Thu, 31 Aug 2023 13:55:37 GMT Message-Id: <202308311355.37VDtbLP083341@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 77740df27c10 - stable/13 - syslogd: Add some basic regression tests List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 77740df27c10c6ae7f51de7c80f1c597716952f3 Auto-Submitted: auto-generated The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=77740df27c10c6ae7f51de7c80f1c597716952f3 commit 77740df27c10c6ae7f51de7c80f1c597716952f3 Author: Jake Freeland AuthorDate: 2023-06-22 21:19:51 +0000 Commit: Mark Johnston CommitDate: 2023-08-31 13:55:19 +0000 syslogd: Add some basic regression tests Reviewed by: markj MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D41403 (cherry picked from commit fcace2906ddfae6bee6064b63a6df44be1bdf04b) --- etc/mtree/BSD.tests.dist | 2 + usr.sbin/syslogd/Makefile | 3 + usr.sbin/syslogd/tests/Makefile | 3 + usr.sbin/syslogd/tests/syslogd_test.sh | 299 +++++++++++++++++++++++++++++++++ 4 files changed, 307 insertions(+) diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index fc04b5b02d23..39d1e7801619 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -1153,6 +1153,8 @@ .. sa .. + syslogd + .. .. .. diff --git a/usr.sbin/syslogd/Makefile b/usr.sbin/syslogd/Makefile index 8bdc107dd1c1..803ec8c93056 100644 --- a/usr.sbin/syslogd/Makefile +++ b/usr.sbin/syslogd/Makefile @@ -44,4 +44,7 @@ PPPPACKAGE= ppp CFLAGS+= -I${SRCTOP}/usr.bin/wall +HAS_TESTS= +SUBDIR.${MK_TESTS}+= tests + .include diff --git a/usr.sbin/syslogd/tests/Makefile b/usr.sbin/syslogd/tests/Makefile new file mode 100644 index 000000000000..9f7e151a3938 --- /dev/null +++ b/usr.sbin/syslogd/tests/Makefile @@ -0,0 +1,3 @@ +ATF_TESTS_SH+= syslogd_test + +.include diff --git a/usr.sbin/syslogd/tests/syslogd_test.sh b/usr.sbin/syslogd/tests/syslogd_test.sh new file mode 100644 index 000000000000..e4927868e47f --- /dev/null +++ b/usr.sbin/syslogd/tests/syslogd_test.sh @@ -0,0 +1,299 @@ +#- +# SPDX-License-Identifier: BSD-2-Clause +# +# Copyright (c) 2021, 2023 The FreeBSD Foundation +# +# This software was developed by Mark Johnston under sponsorship from +# the FreeBSD Foundation. +# +# This software was developed by Jake Freeland under sponsorship from +# the FreeBSD Foundation. +# + +# Tests to-do: +# actions: hostname, users + +readonly SYSLOGD_UDP_PORT="5140" +readonly SYSLOGD_CONFIG="${PWD}/syslog.conf" +readonly SYSLOGD_LOCAL_SOCKET="${PWD}/log.sock" +readonly SYSLOGD_PIDFILE="${PWD}/syslogd.pid" +readonly SYSLOGD_LOCAL_PRIVSOCKET="${PWD}/logpriv.sock" + +# Start a private syslogd instance. +syslogd_start() +{ + syslogd \ + -b ":${SYSLOGD_UDP_PORT}" \ + -C \ + -d \ + -f "${SYSLOGD_CONFIG}" \ + -H \ + -p "${SYSLOGD_LOCAL_SOCKET}" \ + -P "${SYSLOGD_PIDFILE}" \ + -S "${SYSLOGD_LOCAL_PRIVSOCKET}" \ + $@ \ + & + + # Give syslogd a bit of time to spin up. + while [ "$((i+=1))" -le 20 ]; do + [ -S "${SYSLOGD_LOCAL_SOCKET}" ] && return + sleep 0.1 + done + atf_fail "timed out waiting for syslogd to start" +} + +# Simple logger(1) wrapper. +syslogd_log() +{ + atf_check -s exit:0 -o empty -e empty logger $* +} + +# Make syslogd reload its configuration file. +syslogd_reload() +{ + pkill -HUP -F "${SYSLOGD_PIDFILE}" +} + +# Stop a private syslogd instance. +syslogd_stop() +{ + pid=$(cat "${SYSLOGD_PIDFILE}") + if pkill -F "${SYSLOGD_PIDFILE}"; then + wait "${pid}" + rm -f "${SYSLOGD_PIDFILE}" "${SYSLOGD_LOCAL_SOCKET}" \ + "${SYSLOGD_LOCAL_PRIVSOCKET}" + fi +} + +atf_test_case "basic" "cleanup" +basic_head() +{ + atf_set descr "Messages are logged via supported transports" +} +basic_body() +{ + logfile="${PWD}/basic.log" + printf "user.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}" + syslogd_start + + syslogd_log -p user.debug -t basic -h "${SYSLOGD_LOCAL_SOCKET}" \ + "hello, world (unix)" + atf_check -s exit:0 -o match:"basic: hello, world \(unix\)" \ + tail -n 1 "${logfile}" + + # Grab kernel configuration file. + sysctl kern.conftxt > conf.txt + + # We have INET transport; make sure we can use it. + if grep -qw "INET" conf.txt; then + syslogd_log -4 -p user.debug -t basic -h 127.0.0.1 -P "${SYSLOGD_UDP_PORT}" \ + "hello, world (v4)" + atf_check -s exit:0 -o match:"basic: hello, world \(v4\)" \ + tail -n 1 "${logfile}" + fi + # We have INET6 transport; make sure we can use it. + if grep -qw "INET6" conf.txt; then + syslogd_log -6 -p user.debug -t basic -h ::1 -P "${SYSLOGD_UDP_PORT}" \ + "hello, world (v6)" + atf_check -s exit:0 -o match:"basic: hello, world \(v6\)" \ + tail -n 1 "${logfile}" + fi +} +basic_cleanup() +{ + syslogd_stop +} + +atf_test_case "reload" "cleanup" +reload_head() +{ + atf_set descr "SIGHUP correctly refreshes configuration" +} +reload_body() +{ + logfile="${PWD}/reload.log" + printf "user.debug\t/${logfile}\n" > "${SYSLOGD_CONFIG}" + syslogd_start + + syslogd_log -p user.debug -t reload -h "${SYSLOGD_LOCAL_SOCKET}" \ + "pre-reload" + atf_check -s exit:0 -o match:"reload: pre-reload" tail -n 1 "${logfile}" + + # Override the old rule. + truncate -s 0 "${logfile}" + printf "news.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}" + syslogd_reload + + syslogd_log -p user.debug -t reload -h "${SYSLOGD_LOCAL_SOCKET}" \ + "post-reload user" + syslogd_log -p news.debug -t reload -h "${SYSLOGD_LOCAL_SOCKET}" \ + "post-reload news" + atf_check -s exit:0 -o not-match:"reload: post-reload user" cat ${logfile} + atf_check -s exit:0 -o match:"reload: post-reload news" cat ${logfile} +} +reload_cleanup() +{ + syslogd_stop +} + +atf_test_case "prog_filter" "cleanup" +prog_filter_head() +{ + atf_set descr "Messages are only received from programs in the filter" +} +prog_filter_body() +{ + logfile="${PWD}/prog_filter.log" + printf "!prog1,prog2\nuser.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}" + syslogd_start + + for i in 1 2 3; do + syslogd_log -p user.debug -t "prog${i}" -h "${SYSLOGD_LOCAL_SOCKET}" \ + "hello this is prog${i}" + done + atf_check -s exit:0 -o match:"prog1: hello this is prog1" cat "${logfile}" + atf_check -s exit:0 -o match:"prog2: hello this is prog2" cat "${logfile}" + atf_check -s exit:0 -o not-match:"prog3: hello this is prog3" cat "${logfile}" + + # Override the old rule. + truncate -s 0 ${logfile} + printf "!-prog1,prog2\nuser.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}" + syslogd_reload + + for i in 1 2 3; do + syslogd_log -p user.debug -t "prog${i}" -h "${SYSLOGD_LOCAL_SOCKET}" \ + "hello this is prog${i}" + done + atf_check -s exit:0 -o not-match:"prog1: hello this is prog1" cat "${logfile}" + atf_check -s exit:0 -o not-match:"prog2: hello this is prog2" cat "${logfile}" + atf_check -s exit:0 -o match:"prog3: hello this is prog3" cat "${logfile}" +} +prog_filter_cleanup() +{ + syslogd_stop +} + +atf_test_case "host_filter" "cleanup" +host_filter_head() +{ + atf_set descr "Messages are only received from hostnames in the filter" +} +host_filter_body() +{ + logfile="${PWD}/host_filter.log" + printf "+host1,host2\nuser.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}" + syslogd_start + + for i in 1 2 3; do + syslogd_log -p user.debug -t "host${i}" -H "host${i}" \ + -h "${SYSLOGD_LOCAL_SOCKET}" "hello this is host${i}" + done + atf_check -s exit:0 -o match:"host1: hello this is host1" cat "${logfile}" + atf_check -s exit:0 -o match:"host2: hello this is host2" cat "${logfile}" + atf_check -s exit:0 -o not-match:"host3: hello this is host3" cat "${logfile}" + + # Override the old rule. + truncate -s 0 ${logfile} + printf "\-host1,host2\nuser.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}" + syslogd_reload + + for i in 1 2 3; do + syslogd_log -p user.debug -t "host${i}" -H "host${i}" \ + -h "${SYSLOGD_LOCAL_SOCKET}" "hello this is host${i}" + done + atf_check -s exit:0 -o not-match:"host1: hello this is host1" cat "${logfile}" + atf_check -s exit:0 -o not-match:"host2: hello this is host2" cat "${logfile}" + atf_check -s exit:0 -o match:"host3: hello this is host3" cat "${logfile}" +} +host_filter_cleanup() +{ + syslogd_stop +} + +atf_test_case "prop_filter" "cleanup" +prop_filter_head() +{ + atf_set descr "Messages are received based on conditions in the propery based filter" +} +prop_filter_body() +{ + logfile="${PWD}/prop_filter.log" + printf ":msg,contains,\"FreeBSD\"\nuser.debug\t${logfile}\n" \ + > "${SYSLOGD_CONFIG}" + syslogd_start + + syslogd_log -p user.debug -t "prop1" -h "${SYSLOGD_LOCAL_SOCKET}" "FreeBSD" + syslogd_log -p user.debug -t "prop2" -h "${SYSLOGD_LOCAL_SOCKET}" "freebsd" + atf_check -s exit:0 -o match:"prop1: FreeBSD" cat "${logfile}" + atf_check -s exit:0 -o not-match:"prop2: freebsd" cat "${logfile}" + + truncate -s 0 ${logfile} + printf ":msg,!contains,\"FreeBSD\"\nuser.debug\t${logfile}\n" \ + > "${SYSLOGD_CONFIG}" + syslogd_reload + + syslogd_log -p user.debug -t "prop1" -h "${SYSLOGD_LOCAL_SOCKET}" "FreeBSD" + syslogd_log -p user.debug -t "prop2" -h "${SYSLOGD_LOCAL_SOCKET}" "freebsd" + atf_check -s exit:0 -o not-match:"prop1: FreeBSD" cat "${logfile}" + atf_check -s exit:0 -o match:"prop2: freebsd" cat "${logfile}" + + truncate -s 0 ${logfile} + printf ":msg,icase_contains,\"FreeBSD\"\nuser.debug\t${logfile}\n" \ + > "${SYSLOGD_CONFIG}" + syslogd_reload + + syslogd_log -p user.debug -t "prop1" -h "${SYSLOGD_LOCAL_SOCKET}" "FreeBSD" + syslogd_log -p user.debug -t "prop2" -h "${SYSLOGD_LOCAL_SOCKET}" "freebsd" + atf_check -s exit:0 -o match:"prop1: FreeBSD" cat "${logfile}" + atf_check -s exit:0 -o match:"prop2: freebsd" cat "${logfile}" + + truncate -s 0 ${logfile} + printf ":msg,!icase_contains,\"FreeBSD\"\nuser.debug\t${logfile}\n" \ + > "${SYSLOGD_CONFIG}" + syslogd_reload + + syslogd_log -p user.debug -t "prop1" -h "${SYSLOGD_LOCAL_SOCKET}" "FreeBSD" + syslogd_log -p user.debug -t "prop2" -h "${SYSLOGD_LOCAL_SOCKET}" "freebsd" + syslogd_log -p user.debug -t "prop3" -h "${SYSLOGD_LOCAL_SOCKET}" "Solaris" + atf_check -s exit:0 -o not-match:"prop1: FreeBSD" cat "${logfile}" + atf_check -s exit:0 -o not-match:"prop2: freebsd" cat "${logfile}" + atf_check -s exit:0 -o match:"prop3: Solaris" cat "${logfile}" +} +prop_filter_cleanup() +{ + syslogd_stop +} + +atf_test_case "pipe_action" "cleanup" +pipe_action_head() +{ + atf_set descr "The pipe action evaluates provided command in sh(1)" +} +pipe_action_body() +{ + logfile="${PWD}/pipe_action.log" + printf "\"While I'm digging in the tunnel, the elves will often come to me \ + with solutions to my problem.\"\n-Saymore Crey" > ${logfile} + + printf "!pipe\nuser.debug\t| sed -i '' -e 's/Saymore Crey/Seymour Cray/g' \ + ${logfile}\n" > "${SYSLOGD_CONFIG}" + syslogd_start + + syslogd_log -p user.debug -t "pipe" -h "${SYSLOGD_LOCAL_SOCKET}" \ + "fix spelling error" + atf_check -s exit:0 -o match:"Seymour Cray" cat "${logfile}" +} +pipe_action_cleanup() +{ + syslogd_stop +} + +atf_init_test_cases() +{ + atf_add_test_case "basic" + atf_add_test_case "reload" + atf_add_test_case "prog_filter" + atf_add_test_case "host_filter" + atf_add_test_case "prop_filter" + atf_add_test_case "pipe_action" +}