From nobody Fri Dec 15 16:51:00 2023 X-Original-To: dev-commits-src-branches@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 4SsFbS4v0jz53nWp; Fri, 15 Dec 2023 16:51:00 +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 4SsFbS4JVBz4b44; Fri, 15 Dec 2023 16:51:00 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1702659060; 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=Q9Vp9VuJFEi1cneieOWhI3g1TM8jF84Tbwg32b2RLbw=; b=A+L+NK8cg4M6lOQjZbTIbkd89xM0v62If0LHCRcZMv+WGhvbXTsepLompjyIcxBGdQWl/c cmBPkBKq/MlO+U1TCMiav2/w9byqNHgeGe5YynUL1K8OlDRCAIfWaNWxA5cqXuarsIWAf1 wbcZIjTqazt0A+AUs/M+RDE4xBj7DwAoOSxYWnuCa3DFEWNeK+JGlUBRdlNLaQmDjaIBwP 00oM3ZUqmLo7u4KZzqm4kaUo/JC7PqvfUkwPHJWcRTBJ5TswEw+fwyyQOQMuxemFO6VZ+T UPG6ec17dLLtQy4qHLa2rm2v59PhCAcgzLPbHfFmhK+FGxSfFDlKuIkchzERgQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1702659060; a=rsa-sha256; cv=none; b=qIa1WDoHJYrKC6KwwI8PqkZ1+zlP2KyAzpkKDbh98uQsELYIJchFBXTtHdg2Hi3nXl8Zc1 ICAkguPfbLb5Kk6KlWiSLkqLnoeVOv/0ml/zZLDxqw3b9nQXly9fy6p+VTqtqh8wsLFKZW 1Y2Toalodjq5iLqfAjtXr3a7jXIfYUE+hugCjLYlp1zUfQz8Y1FkUd/MbyPJqDeCifHQPX P00/dFomPqck6eiSHgmFTrhwqC1RwefubTVeykSOKQ+p9TkPnQxjPCRf0Y3JLcqfgGqjYi lul2IZEj+4/yQtkeEynlMEJmNTDiYj1HSI91tl2+U5pQVusoh2+EemiZiYwEjw== 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=1702659060; 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=Q9Vp9VuJFEi1cneieOWhI3g1TM8jF84Tbwg32b2RLbw=; b=qlMrUuROMxN+SefzcdTOKiANJLBp2b9UxSgvbwXMyFojQR62p5WxQE6ulmGty0AD59Zd+/ IdeM7QOJ3Usr9RXJ9cwZNucafmycJN53LZRKBHz1rn/76RErP+SfnK4EvgEvLrOh2L/6iZ 5vyuvyzHSZ2ZDtFMdu8LS3GzPEqp51lvKM+CZ/LnlBkseqglJdM1N6OmW6R7UUtZJN9dpf N2LE9iTcyishZMDtlV6cUIGYzL7cNJ8pQuxq2xPV4bq+K/4U1bFuS9bth7xJ8i/lLvZXYi iUL/JYSXGmXjRD4Xia8LkQ/0+yRIrsUvv/PB3rMgNivrw35oHOFCjR3CrphmJg== 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 4SsFbS3Prtz3yv; Fri, 15 Dec 2023 16:51:00 +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 3BFGp0G3011925; Fri, 15 Dec 2023 16:51:00 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BFGp0vg011922; Fri, 15 Dec 2023 16:51:00 GMT (envelope-from git) Date: Fri, 15 Dec 2023 16:51:00 GMT Message-Id: <202312151651.3BFGp0vg011922@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: 40754cd21e70 - stable/14 - lockf: add some tests List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@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/stable/14 X-Git-Reftype: branch X-Git-Commit: 40754cd21e70b35341f4f3f9a2d09008fe370f9d Auto-Submitted: auto-generated The branch stable/14 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=40754cd21e70b35341f4f3f9a2d09008fe370f9d commit 40754cd21e70b35341f4f3f9a2d09008fe370f9d Author: Kyle Evans AuthorDate: 2023-11-22 05:09:11 +0000 Commit: Kyle Evans CommitDate: 2023-12-15 00:58:25 +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. (cherry picked from commit 296a5a4db1fc8203f5f6aa8f68321e6ab4652b56) --- 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 04dc24d772c8..613da00e277d 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -1081,6 +1081,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 +}