From nobody Fri Apr 15 14:28:06 2022 X-Original-To: dev-commits-src-main@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 E9D632D92FC; Fri, 15 Apr 2022 14:28:06 +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 4KfzFf4PYkz53bK; Fri, 15 Apr 2022 14:28:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1650032886; 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=/UHW+F2h2vv4FOrXHumkce2Xw/oDxlrfqHzYN8ppRVs=; b=Ahoomol9vTr4I9wy770fr8Hz7SC0Glo8Tfg2EwxarrSeWQA+pvIL2az2afKJaR1LSF09rn Y/2wSJEwSmxV13KDPQ2MILmJfGnbONHDbjZPuFKJbVdJvYdRDZdxzV4t7Q0ThPBDb+Bgow Mham7L5d3pIiPkWypzLFM9JaMTu637rkbEW+bdXqmd48noiGx6GTaxPYsjckXO/oNURman in055HzDidzoErwrS3kSAMRQA512Tt8E+Kh1ASQUJbaaDtB3p00GXOPb2FcUYutwOk9KoA R6mZ73/4udo3LQj0q3ohmR903Um9KftoK8FRw6lSTpoWldH6fhvMwa5ofIqNFw== 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 6AC6D17008; Fri, 15 Apr 2022 14:28:06 +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 23FES6lY063386; Fri, 15 Apr 2022 14:28:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 23FES60l063385; Fri, 15 Apr 2022 14:28:06 GMT (envelope-from git) Date: Fri, 15 Apr 2022 14:28:06 GMT Message-Id: <202204151428.23FES60l063385@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: d0f245d21f47 - main - libsysdecode: Add regression tests for sysdecode_cap_rights(3) List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: d0f245d21f47c55ed40b34a17d5caf08aba1952f Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1650032886; 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=/UHW+F2h2vv4FOrXHumkce2Xw/oDxlrfqHzYN8ppRVs=; b=Bs9no3ZXDFpA0T6MGBNsTAXt+AqXdMM15lqiDkQWX4eK7cPNrYa1FjfM/nUqbAj94sER+q xYSQ6PHjPVpiuLMl1yjcitsekg3mG7sfEO5mYYXvwy+TKKPLr2cl9QzDpU0LJlvkQCVCYy e801ZggPvOOt0GlELEhZ5uWoxMfFAKBcaAR95GxK50Hq8/UmbILN8pn+PICz8x7ox6l8QG nbw0Wj8P3xEhAtHxTePttUtk0CF0tFy0Yg8PnaPVX2ZXt83c4u4XMS1BTm3ZhreFONwP4l d3krL8b5ckhxJOrrMpqi8zK+jfJ/29pcd92yN8ADG5IrJ3jp1DvudTWXLhZVgw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1650032886; a=rsa-sha256; cv=none; b=NZrlTxHx2Sa+eswIDd1T+T3MIsd61Cw+gciZwfQ1SMyBRmOMAuD+toDk1Tje4VrvCR4Iu0 yNPqGsHeFA5skNjiIsOetqDGnIoGDqq8FJJv3JA/TxRKaUMRvBTHAWZVBB5nj9RkwK3SW2 xj1rONSEl/vRJiBqCv1GUlKNxxuw5712OoVmE+DoJPuRjox83gslJQVIYKuodw8xXo/tOd D3zrCPtxyV7XWOSncywvVZ+vhq0JPQIMjsYWfa3aRudJym16o+isomQkwYQkHf6y68Bo46 pvszKMrkG5uuPQTkpkXcCsdwNVC3nACGva6lm16B1yI75WjTOduSxJ6TuDChNg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=d0f245d21f47c55ed40b34a17d5caf08aba1952f commit d0f245d21f47c55ed40b34a17d5caf08aba1952f Author: Mark Johnston AuthorDate: 2022-04-15 14:23:32 +0000 Commit: Mark Johnston CommitDate: 2022-04-15 14:23:32 +0000 libsysdecode: Add regression tests for sysdecode_cap_rights(3) Reviewed by: jhb, emaste MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D34900 --- 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 05890a266a08..32620e849df2 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()); +}