From nobody Fri Dec 15 18:57:31 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 4SsJPR6CvCz53wsW; Fri, 15 Dec 2023 18:57:31 +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 4SsJPR55ztz3MCP; Fri, 15 Dec 2023 18:57:31 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1702666651; 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=jB79hKzHVCXCAzNT5WaTVTJFtRdLVUyvl6RAvcL1zws=; b=Wccl5BQpiM91bSf/RBitzbHMdZpb9/YvOucp792SWTTkblJoQnoPV03Q9bzOf5oO86bLPc JVgSzDkJPeOtmqs88afRReBqvtYmYmMLJNFOI99Hz/myPZ9mZx9KE/x0t4RW8l8Ueq4cro 4Qba6WTmrrLi2OJNqmzmzCVhyF8vRik4pf4ck4nZJ4BGwibVrEsWDA9idUNze06P2qu/Qq sHRI9fL0vtFpT2eeVibGkOwDkgyE+stCq+BzLSAaSMwicrlFrgilITVFUNRaVFA7RyPR3d v/A2FU/rIIFIP3JR1UcFrYuObdE+1zXdovd9AmO61K3EWuIcb1I5xbaoxOZ5xw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1702666651; a=rsa-sha256; cv=none; b=G/xkgbLwuJmX6wEQ+48HF3ql2OI+uwYheaU46SH010nkt0xu7urX1H4Ijn/XdkEHXwueVV pofdKsdy0r/E5I5U3vBpvhmjuQZdHiPwkplsmPLraoiXwn8gvAOT2Jj6xpXi+mv6FyVlCy lEuGa4mYgd+9p/ID1b/JRKhswpfLVVVUkc9A5dqMgJyzQzLXGno/hVHod7DFfNR22U/G/8 N8qnsuDYtY0toCtyaiee+GnT41KqVtjfUDHJ/Ig/zIxetsR+o/nV5V7eVilH0zX2SpsN+W Q0uilRQWIDyOugcysFOzhoqT4hBAqsQGwn+T60c248HYjhYDokt1TTW0hDd1Hw== 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=1702666651; 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=jB79hKzHVCXCAzNT5WaTVTJFtRdLVUyvl6RAvcL1zws=; b=Vy8wPOFzTHz5QckOZA5wKTTmnlbcmXc7rhPJYDoSV4NZ07mqK92JnR7vhxtQPESUSySNcK j3+9Wgu1NI76Rxwb+xPP6XgjisGCT6W0+zi0GJBZyrUq2wL0oNh43c84bcStRHTzIe6khw IXY4uhjbMlFTYPK0I1aN5g+vjUA1PXYIqcnM4u0eC9L4l7c0AeYZIF/3naw4L5BI5bw+0J 9J7IDkwk3b1JSurxY5oBdN7dpx0LzycGLFHUBRD8yYRb4XVIrNefdg2/9YJF0QnBt0gA3f /rwUZ4M7/X0TWcMyfwf5+mocy7XBcvZyCX8dFLXhU2acHhRVFJrkj0hTPEDRQA== 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 4SsJPR4CKXz7cm; Fri, 15 Dec 2023 18:57:31 +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 3BFIvVVE018355; Fri, 15 Dec 2023 18:57:31 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BFIvVFV018352; Fri, 15 Dec 2023 18:57:31 GMT (envelope-from git) Date: Fri, 15 Dec 2023 18:57:31 GMT Message-Id: <202312151857.3BFIvVFV018352@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: fbd1d0bdff1d - stable/13 - 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/13 X-Git-Reftype: branch X-Git-Commit: fbd1d0bdff1d97068c148e0a200ea786eb29fe69 Auto-Submitted: auto-generated The branch stable/13 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=fbd1d0bdff1d97068c148e0a200ea786eb29fe69 commit fbd1d0bdff1d97068c148e0a200ea786eb29fe69 Author: Kyle Evans AuthorDate: 2023-11-22 05:09:11 +0000 Commit: Kyle Evans CommitDate: 2023-12-15 00:59:21 +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 46a305aec62d..f9a551bbc39b 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -1055,6 +1055,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 +}