From nobody Fri Apr 22 15:25:06 2022 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 E6D7E1999A9A; Fri, 22 Apr 2022 15:25:07 +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 4KlJBC2yQqz4SMX; Fri, 22 Apr 2022 15:25:07 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1650641107; 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=XcEK74hh4S1wGFDjGlj6Ed6YQ4wZiv210F+EONaYbss=; b=lDDBg6XmvIF5fVBFnroYZVblovJs2T8bV3AbX5TGlbuPz1iGH8rx0COl4Andagve6nD03q VqP12b8CRVxUBF9WK58VAkXl4QdER5AeE8LrvWWpNk9qN9I4cLtbNZBHeHBdx0J1ceYSkq W24xhM+sOtMHXYO4Zlkr/En83PwRmOM02bc57GqDNYgmmDG9mtOpm0yam+AE8CKP3uGDgS iCa4HTmEkRvaSYi1Surhp7TwLwfVoBy/pRqLB9f7xb/5SnRz9zM1MRH9TflKkfKBr+tqnW C/hmAO1jXwwnaYz/LjP6NaPCT6Oqkj8n3PzIGBuzpiF2qSAXQ3DLLuOyew2qGA== 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 129791FD54; Fri, 22 Apr 2022 15:25:07 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 23MFP6Y8082080; Fri, 22 Apr 2022 15:25:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 23MFP68C082079; Fri, 22 Apr 2022 15:25:06 GMT (envelope-from git) Date: Fri, 22 Apr 2022 15:25:06 GMT Message-Id: <202204221525.23MFP68C082079@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: cb613da9163c - stable/13 - libsysdecode: Add regression tests for sysdecode_cap_rights(3) 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: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: cb613da9163c9b92a2ee337db5590199ad5ef26e Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1650641107; 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=XcEK74hh4S1wGFDjGlj6Ed6YQ4wZiv210F+EONaYbss=; b=S5ygbpV14MoqIsbbAQZ9KsrRU/W/U9jU6J1OWJ3ug+bGWqBPfhOFrVIsA7PKrI7SSId5c2 jQLKOAZfA1zdTcqX3K1tBgGtRxbzMv62a58CxX/U+1taXIujvZUw8mPkAN3NhLXavw3hY6 /M4M2pwFB/kbmrtwBIjxioU4aBvmhnEtMqb4CeSvXP6j8uuvVOO2jE5udXb0Bidnft53A6 8x3HxOn8dc09pEA/JC1W1/jIs2fwCE1yZChRuvWySK8dcM9gnQ27FUn+nBerUvtLyxE3Oy +CX3fIWas8IuUct4bpiICnEJ76kGmiG9c8epIPb8oQ4Hlftfj1PdujARkD2RLg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1650641107; a=rsa-sha256; cv=none; b=XXPI45v4rl8jWMxWUXaH0PeMziN+5uFG1vXju4/sNGhXnu0DRLzjV1JLnD2twen6y+TGaW kNvIXoSpBLhlc4jK0BHUvX69zDylG5Z/iU2wH/hRtaDm4vfQ5qpyDSJQsrlWJ/cCbug35v ++bRZCdkzqt4DgGh19HCSQwu0hzGaBzqVsGWCi1mKj3mOVbZlNmg5kYVCFPPYVjJBvm68l k3dyZVZezhODe4gmwyrhAVDmBAH3hRdxOupSHDOCXvjUhTPAuINeOkQ7LstRwPIdeG5nFY +GywToPnNtQQhjiNKA4twx7WW2ecA9eGmjPWkX3jfiaDIBEyUqn8hAxy9U5i0g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=cb613da9163c9b92a2ee337db5590199ad5ef26e commit cb613da9163c9b92a2ee337db5590199ad5ef26e Author: Mark Johnston AuthorDate: 2022-04-15 14:23:32 +0000 Commit: Mark Johnston CommitDate: 2022-04-22 14:36:37 +0000 libsysdecode: Add regression tests for sysdecode_cap_rights(3) Reviewed by: jhb, emaste Sponsored by: The FreeBSD Foundation (cherry picked from commit d0f245d21f47c55ed40b34a17d5caf08aba1952f) --- etc/mtree/BSD.tests.dist | 2 + lib/libsysdecode/Makefile | 3 + lib/libsysdecode/tests/Makefile | 5 ++ lib/libsysdecode/tests/sysdecode_test.c | 152 ++++++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+) diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index 75fbc64dce9c..736fb95f9440 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -404,6 +404,8 @@ .. libsbuf .. + libsysdecode + .. libthr dlopen .. diff --git a/lib/libsysdecode/Makefile b/lib/libsysdecode/Makefile index d58858086bc9..671c23d95a70 100644 --- a/lib/libsysdecode/Makefile +++ b/lib/libsysdecode/Makefile @@ -141,4 +141,7 @@ ioctl.c: ioctl.c.tmp beforedepend: ioctl.c tables.h +HAS_TESTS= +SUBDIR.${MK_TESTS}+= tests + .include diff --git a/lib/libsysdecode/tests/Makefile b/lib/libsysdecode/tests/Makefile new file mode 100644 index 000000000000..3d75b9b32ba7 --- /dev/null +++ b/lib/libsysdecode/tests/Makefile @@ -0,0 +1,5 @@ +ATF_TESTS_C+= sysdecode_test + +LIBADD= sysdecode + +.include diff --git a/lib/libsysdecode/tests/sysdecode_test.c b/lib/libsysdecode/tests/sysdecode_test.c new file mode 100644 index 000000000000..c41fdae68927 --- /dev/null +++ b/lib/libsysdecode/tests/sysdecode_test.c @@ -0,0 +1,152 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2022 The FreeBSD Foundation + * + * This software was developed by Mark Johnston under sponsorship from + * the FreeBSD Foundation. + * + * 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. + */ + +#include +#include +#include + +#include +#include + +/* + * Take a comma-separated list of capability rights and verify that all rights + * are present in the specified table, and that all rights in the table are + * present in the list. + */ +static void +check_sysdecode_cap_rights(FILE *fp, char **bufp, size_t *szp, + cap_rights_t *rightsp, const char *tab[]) +{ + const char *next, *tok; + char *buf; + int i; + + sysdecode_cap_rights(fp, rightsp); + + ATF_REQUIRE(fflush(fp) == 0); + (*bufp)[*szp] = '\0'; + + buf = strdup(*bufp); + for (tok = buf; (next = strsep(&buf, ",")), tok != NULL; tok = next) { + for (i = 0; tab[i] != NULL; i++) { + if (strcmp(tok, tab[i]) == 0) + break; + } + ATF_REQUIRE_MSG(tab[i] != NULL, + "did not find '%s' in table", tok); + } + free(buf); + + for (i = 0; tab[i] != NULL; i++) { + buf = strdup(*bufp); + for (tok = buf; (next = strsep(&buf, ",")), tok != NULL; + tok = next) { + if (strcmp(tok, tab[i]) == 0) + break; + } + free(buf); + ATF_REQUIRE_MSG(tok != NULL, + "did not find '%s' in output stream", tab[i]); + } + + ATF_REQUIRE(fseek(fp, 0, SEEK_SET) == 0); +} + +/* + * Regression tests for sysdecode_cap_rights(3). + */ +ATF_TC_WITHOUT_HEAD(cap_rights); +ATF_TC_BODY(cap_rights, tc) +{ + char *buf; + FILE *fp; + size_t sz; + cap_rights_t rights; + + fp = open_memstream(&buf, &sz); + ATF_REQUIRE(fp != NULL); + + /* + * libsysdecode emits a pseudo-right, CAP_NONE, when no rights are + * present. + */ + check_sysdecode_cap_rights(fp, &buf, &sz, + cap_rights_init(&rights), + (const char *[]){ "CAP_NONE", NULL, }); + + check_sysdecode_cap_rights(fp, &buf, &sz, + cap_rights_init(&rights, CAP_READ, CAP_SEEK), + (const char *[]){ "CAP_PREAD", NULL, }); + + check_sysdecode_cap_rights(fp, &buf, &sz, + cap_rights_init(&rights, CAP_READ, CAP_MMAP, CAP_SEEK_TELL), + (const char *[]){ "CAP_READ", "CAP_MMAP", "CAP_SEEK_TELL", NULL, }); + + check_sysdecode_cap_rights(fp, &buf, &sz, + cap_rights_init(&rights, CAP_MMAP, CAP_READ, CAP_WRITE, CAP_SEEK), + (const char *[]){ "CAP_MMAP_RW", NULL, }); + + check_sysdecode_cap_rights(fp, &buf, &sz, + cap_rights_init(&rights, CAP_READ, CAP_MMAP_X), + (const char *[]){ "CAP_MMAP_RX", NULL, }); + + /* Aliases map back to the main definition. */ + check_sysdecode_cap_rights(fp, &buf, &sz, + cap_rights_init(&rights, CAP_RECV, CAP_SEND), + (const char *[]){ "CAP_READ", "CAP_WRITE", NULL, }); + + /* This set straddles both indices. */ + check_sysdecode_cap_rights(fp, &buf, &sz, + cap_rights_init(&rights, CAP_READ, CAP_KQUEUE), + (const char *[]){ "CAP_READ", "CAP_KQUEUE", NULL, }); + + /* Create a rights set with an unnamed flag. */ + cap_rights_init(&rights, CAP_SEEK); + cap_rights_clear(&rights, CAP_SEEK_TELL); + check_sysdecode_cap_rights(fp, &buf, &sz, + &rights, + (const char *[]){ "CAP_NONE", "unknown rights", NULL, }); + + cap_rights_init(&rights, CAP_SEEK, CAP_KQUEUE_CHANGE); + cap_rights_clear(&rights, CAP_SEEK_TELL); + check_sysdecode_cap_rights(fp, &buf, &sz, + &rights, + (const char *[]){ "CAP_KQUEUE_CHANGE", "unknown rights", NULL, }); + + ATF_REQUIRE(fclose(fp) == 0); + free(buf); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, cap_rights); + + return (atf_no_error()); +}