Date: Fri, 22 Apr 2022 15:25:06 GMT From: Mark Johnston <markj@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Subject: git: cb613da9163c - stable/13 - libsysdecode: Add regression tests for sysdecode_cap_rights(3) Message-ID: <202204221525.23MFP68C082079@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=cb613da9163c9b92a2ee337db5590199ad5ef26e commit cb613da9163c9b92a2ee337db5590199ad5ef26e Author: Mark Johnston <markj@FreeBSD.org> AuthorDate: 2022-04-15 14:23:32 +0000 Commit: Mark Johnston <markj@FreeBSD.org> 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 <bsd.lib.mk> 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 <bsd.test.mk> 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 <sys/capsicum.h> +#include <stdio.h> +#include <stdlib.h> + +#include <atf-c.h> +#include <sysdecode.h> + +/* + * 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()); +}
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202204221525.23MFP68C082079>