From nobody Sun Nov 26 04:10:51 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 4SdFd759mmz52lJx; Sun, 26 Nov 2023 04:10:51 +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 4SdFd74cfsz3KHq; Sun, 26 Nov 2023 04:10:51 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1700971851; 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=eMq+VxJFRs87HKvDxbnqzF82IMSWIhMZWOHdUkZblUM=; b=SJ19q4T8G2HHCCeWEa3fXZ2Cq9uy4kgGuaWabQL3rB3F0CSdxrtMbIBmUZm5mYo93psEIl NLDvSULaRqjTGJAnalSMyjaURrHpYdD6WdvPRHa3Ty6K19IelzQM0WNsuTQnGNScxm7CBa EwYKkA63D+gTQvmZrdZWsqbmacFrB2AiPH6SCedsKJ/8+9V4ucc+e72tr5jSz7ZsnmUW0Z lghtJmMs9IBr+3NUjX14RSEZ5Wk6qhJRMioKojHMmefHlcUNlX4VfFVc4hB6tD5WYXoE91 jM6swCbG//RQc5gHua1it//P2FzZMB8MJruCkCe26fQ/SG+a1cOI2GRrXYXe8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1700971851; 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=eMq+VxJFRs87HKvDxbnqzF82IMSWIhMZWOHdUkZblUM=; b=qOHxwwa4zUJf+yKvnqSPOSReSXXmwsjfyBCmAh0R6h/LYN5+XYeiXUjvlaawEi7ymV2zhk 3q/woxe/RbJhtHMiCqUHXB3GNwDm8V0Sq/G9VenLmh6kGfcjBoOj6YlVCQr8lrKZma8Asp Q9uuyivqM+EgcAR8HWq4mtYtBeoOmYUZOwLJJiwBm+dYhZ+TrvQpuG+tne0HJGQ2w6EGP1 haJ7cuM71exBStlvt+hm2sHF/gt4tmJqN63qKn55iN4zg0dReaPay6iir+wkN38bby/Iy/ QnsnzoP90WRsYVIIvmdKnZCpbZaLCV3DY1urQciP+TVurw8803ZzHn8i5NfB7w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1700971851; a=rsa-sha256; cv=none; b=C8H9IBINvL3FQkL1rgQ8ewvUNJfTBGKvoo4+pj10BmcKTXTq7RwjXUVRMNzTQ/N1shDZ0o DdLxio6ZNVPO5ooTcJtU5Myw8tSsMZb5d3oHk638rX1ZojUx504Zr41KUZnBRX+z8BBnPs psYW9/QJIko5p/GlN0bsc77IPqzxOuDMeQpFF/3xxoAuiWx4axOuUPygO/tV73xtTOxJxE MYA7z0MAT2P7p1wAt531GDFE7xFbyK6KM2DT2ByTfpnTb/tNVqXgGH08hBmuPKPbRQWQKZ HmBz983k06Mgpj5W4/QNoXKdoeWbeWQ0rXSvAHpi4LMD/sTjIA2NuCsZhi3ghA== 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 4SdFd73ZXFzgsm; Sun, 26 Nov 2023 04:10:51 +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 3AQ4ApKD038161; Sun, 26 Nov 2023 04:10:51 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3AQ4AphP038158; Sun, 26 Nov 2023 04:10:51 GMT (envelope-from git) Date: Sun, 26 Nov 2023 04:10:51 GMT Message-Id: <202311260410.3AQ4AphP038158@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: 296a5a4db1fc - main - lockf: add some 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: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 296a5a4db1fc8203f5f6aa8f68321e6ab4652b56 Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=296a5a4db1fc8203f5f6aa8f68321e6ab4652b56 commit 296a5a4db1fc8203f5f6aa8f68321e6ab4652b56 Author: Kyle Evans AuthorDate: 2023-11-22 05:09:11 +0000 Commit: Kyle Evans CommitDate: 2023-11-26 04:09:58 +0000 lockf: add some tests Provide basic coverage for the existing options, nothing deeper (e.g., pipe closing behavior) is tested in this set. Reviewed by: allanjude Feedback from: des Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D42714 --- etc/mtree/BSD.tests.dist | 2 + usr.bin/lockf/Makefile | 5 ++ usr.bin/lockf/tests/Makefile | 6 ++ usr.bin/lockf/tests/lockf_test.sh | 148 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+) diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index 4239de67478a..bc3dcda60913 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -1083,6 +1083,8 @@ .. locale .. + lockf + .. m4 .. mkimg diff --git a/usr.bin/lockf/Makefile b/usr.bin/lockf/Makefile index d1905a4510ba..b7d916f20d13 100644 --- a/usr.bin/lockf/Makefile +++ b/usr.bin/lockf/Makefile @@ -1,4 +1,9 @@ +.include + PROG= lockf +HAS_TESTS= +SUBDIR.${MK_TESTS}+= tests + .include diff --git a/usr.bin/lockf/tests/Makefile b/usr.bin/lockf/tests/Makefile new file mode 100644 index 000000000000..88a936cc583d --- /dev/null +++ b/usr.bin/lockf/tests/Makefile @@ -0,0 +1,6 @@ + +PACKAGE= tests + +ATF_TESTS_SH+= lockf_test + +.include diff --git a/usr.bin/lockf/tests/lockf_test.sh b/usr.bin/lockf/tests/lockf_test.sh new file mode 100644 index 000000000000..8696ab82a996 --- /dev/null +++ b/usr.bin/lockf/tests/lockf_test.sh @@ -0,0 +1,148 @@ +# +# SPDX-License-Identifier: BSD-2-Clause +# +# Copyright (c) 2023 Klara, Inc. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# + +# sysexits(3) +: ${EX_USAGE:=64} +: ${EX_UNAVAILABLE:=69} +: ${EX_CANTCREAT:=73} +: ${EX_TEMPFAIL:=75} + +atf_test_case badargs +badargs_body() +{ + atf_check -s exit:${EX_USAGE} -e not-empty lockf + atf_check -s exit:${EX_USAGE} -e not-empty lockf "testlock" +} + +atf_test_case basic +basic_body() +{ + # Something innocent so that it does eventually go away without our + # intervention. + lockf "testlock" sleep 10 & + lpid=$! + + # Make sure that the lock exists... + atf_check test -e "testlock" + + # Attempt both verbose and silent re-lock + atf_check -s exit:${EX_TEMPFAIL} -e not-empty \ + lockf -t 0 "testlock" sleep 0 + atf_check -s exit:${EX_TEMPFAIL} -e empty \ + lockf -t 0 -s "testlock" sleep 0 + + # Make sure it cleans up after the initial sleep 10 is over. + wait "$lpid" + atf_check test ! -e "testlock" +} + +atf_test_case keep +keep_body() +{ + lockf -k "testlock" sleep 10 & + lpid=$! + + # Make sure that the lock exists now... + while ! test -e "testlock"; do + sleep 0.5 + done + + kill "$lpid" + wait "$lpid" + + # And it still exits after the lock has been relinquished. + atf_check test -e "testlock" +} + +atf_test_case needfile +needfile_body() +{ + # Hopefully the clock doesn't jump. + start=$(date +"%s") + + # Should fail if the lockfile does not yet exist. + atf_check -s exit:"${EX_UNAVAILABLE}" lockf -sn "testlock" sleep 30 + + # It's hard to guess how quickly we should have finished that; one would + # hope that it exits fast, but to be safe we specified a sleep 30 under + # lock so that we have a good margin below that duration that we can + # safely test to make sure we didn't actually execute the program, more + # or less. + now=$(date +"%s") + tpass=$((now - start)) + atf_check test "$tpass" -lt 10 +} + +atf_test_case timeout +timeout_body() +{ + lockf "testlock" sleep 30 & + lpid=$! + + while ! test -e "testlock"; do + sleep 0.5 + done + + start=$(date +"%s") + timeout=2 + atf_check -s exit:${EX_TEMPFAIL} lockf -st "$timeout" "testlock" sleep 0 + + # We should have taken no less than our timeout, at least. + now=$(date +"%s") + tpass=$((now - start)) + atf_check test "$tpass" -ge "$timeout" + + kill "$lpid" + wait "$lpid" || true +} + +atf_test_case wrlock +wrlock_head() +{ + atf_set "require.user" "unprivileged" +} +wrlock_body() +{ + touch "testlock" + chmod -w "testlock" + + # Demonstrate that we can lock the file normally, but -w fails if we + # can't write. + atf_check lockf -kt 0 "testlock" sleep 0 + atf_check -s exit:${EX_CANTCREAT} -e not-empty \ + lockf -wt 0 "testlock" sleep 0 +} + +atf_init_test_cases() +{ + atf_add_test_case badargs + atf_add_test_case basic + atf_add_test_case keep + atf_add_test_case needfile + atf_add_test_case timeout + atf_add_test_case wrlock +}