From owner-dev-commits-src-all@freebsd.org Fri Aug 27 08:52:47 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id ED68965B359; Fri, 27 Aug 2021 08:52:47 +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 4GwtlM2qZ1z4r3K; Fri, 27 Aug 2021 08:52:47 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 36C232051E; Fri, 27 Aug 2021 08:52:47 +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 17R8qlRG051158; Fri, 27 Aug 2021 08:52:47 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 17R8qlXE051157; Fri, 27 Aug 2021 08:52:47 GMT (envelope-from git) Date: Fri, 27 Aug 2021 08:52:47 GMT Message-Id: <202108270852.17R8qlXE051157@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Gleb Popov Subject: git: f8b88be850a5 - main - tests/sys/acl: Add ATF C test for newly added acl_* functions. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: arrowd X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: f8b88be850a51eb982a476b27b74686a445e9a38 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 27 Aug 2021 08:52:48 -0000 The branch main has been updated by arrowd (ports committer): URL: https://cgit.FreeBSD.org/src/commit/?id=f8b88be850a51eb982a476b27b74686a445e9a38 commit f8b88be850a51eb982a476b27b74686a445e9a38 Author: Gleb Popov AuthorDate: 2021-01-23 10:00:47 +0000 Commit: Gleb Popov CommitDate: 2021-08-27 08:52:21 +0000 tests/sys/acl: Add ATF C test for newly added acl_* functions. Reviewed by: kib, debdrup, gbe Approved by: kib Differential Revision: https://reviews.freebsd.org/D28255 --- tests/sys/acl/Makefile | 2 + tests/sys/acl/acl-api-test.c | 194 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) diff --git a/tests/sys/acl/Makefile b/tests/sys/acl/Makefile index bd548f06951e..abc505efe90b 100644 --- a/tests/sys/acl/Makefile +++ b/tests/sys/acl/Makefile @@ -14,6 +14,8 @@ ${PACKAGE}FILES+= tools-posix.test SCRIPTS+= run +ATF_TESTS_C+= acl-api-test + TAP_TESTS_SH+= 00 TAP_TESTS_SH+= 01 TAP_TESTS_SH+= 02 diff --git a/tests/sys/acl/acl-api-test.c b/tests/sys/acl/acl-api-test.c new file mode 100644 index 000000000000..3bd288313fed --- /dev/null +++ b/tests/sys/acl/acl-api-test.c @@ -0,0 +1,194 @@ +/*- + * Copyright (c) 2021 Gleb Popov + * All rights reserved. + * + * 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 + +#include + +/* Compatibility shim to make it possible to run this test on Linux + * gcc -I/path/to/atf/include -L/path/to/atf/lib -latf-c -lacl acl-api-test.c + */ +#ifdef __linux__ +#include +#define acl_from_mode_np acl_from_mode +#define acl_equiv_mode_np acl_equiv_mode +#define acl_cmp_np acl_cmp +#endif + +static const mode_t all_modes[] = { + S_IRUSR, + S_IWUSR, + S_IXUSR, + S_IRGRP, + S_IWGRP, + S_IXGRP, + S_IROTH, + S_IWOTH, + S_IXOTH +}; + +static mode_t gen_random_mode(void) +{ + mode_t mode = 0; + + for (unsigned i = 0; i < sizeof(all_modes) / sizeof(mode_t); i++) { + if (rand() % 2) + mode |= all_modes[i]; + } + + return (mode); +} + +/* Generate a random mode_t, produce an acl_t from it, + * then use acl_equiv_mode_np to produce a mode_t again. + * The call should succeed and mode_t's should be equal + */ +ATF_TC_WITHOUT_HEAD(acl_mode_roundup); +ATF_TC_BODY(acl_mode_roundup, tc) +{ + int num_tests = 100; + + while (num_tests--) { + mode_t src_mode, equiv_mode; + acl_t acl; + + src_mode = gen_random_mode(); + + acl = acl_from_mode_np(src_mode); + ATF_REQUIRE(acl != NULL); + + ATF_CHECK_EQ(0, acl_equiv_mode_np(acl, &equiv_mode)); + ATF_CHECK_EQ(src_mode, equiv_mode); + + acl_free(acl); + } +} + +/* Successfull acl_equiv_mode_np calls are tested in acl_mode_roundup. + * Here some specific cases are tested. + */ +ATF_TC_WITHOUT_HEAD(acl_equiv_mode_test); +ATF_TC_BODY(acl_equiv_mode_test, tc) +{ + acl_t acl; + acl_entry_t entry; + mode_t mode; + int uid = 0; + + acl = acl_init(1); + ATF_REQUIRE(acl != NULL); + + /* empty acl maps to 0000 UNIX mode */ + ATF_CHECK_EQ(0, acl_equiv_mode_np(acl, &mode)); + ATF_CHECK_EQ(0, mode); + +#ifndef __linux__ + /* NFS-branded acl's can't be converted to UNIX mode */ + ATF_REQUIRE_EQ(0, acl_create_entry(&acl, &entry)); + ATF_REQUIRE_EQ(0, acl_set_tag_type(entry, ACL_EVERYONE)); + ATF_CHECK_EQ(1, acl_equiv_mode_np(acl, &mode)); +#endif + + /* acl's with qualified user entries can't be converted to UNIX mode */ + acl_free(acl); + acl = acl_init(1); + ATF_REQUIRE(acl != NULL); + ATF_REQUIRE_EQ(0, acl_create_entry(&acl, &entry)); + ATF_REQUIRE_EQ(0, acl_set_tag_type(entry, ACL_USER)); + ATF_REQUIRE_EQ(0, acl_set_qualifier(entry, &uid)); + ATF_CHECK_EQ(1, acl_equiv_mode_np(acl, &mode)); + + /* passing NULL causes EINVAL */ + ATF_CHECK_ERRNO(EINVAL, acl_equiv_mode_np(NULL, &mode)); +} + +ATF_TC_WITHOUT_HEAD(acl_cmp_test); +ATF_TC_BODY(acl_cmp_test, tc) +{ + acl_t empty_acl, acl1, acl2; + acl_entry_t entry; + acl_permset_t perms; + + empty_acl = acl_init(1); + ATF_REQUIRE(empty_acl != NULL); + + acl1 = acl_init(3); + ATF_REQUIRE(acl1 != NULL); + + /* first, check that two empty acls are equal */ + ATF_CHECK_EQ(0, acl_cmp_np(acl1, empty_acl)); + + /* now create an entry and compare against empty acl */ + ATF_REQUIRE_EQ(0, acl_create_entry(&acl1, &entry)); + ATF_REQUIRE_EQ(0, acl_set_tag_type(entry, ACL_USER_OBJ)); + ATF_REQUIRE_EQ(0, acl_get_permset(entry, &perms)); + ATF_REQUIRE_EQ(0, acl_clear_perms(perms)); + ATF_REQUIRE_EQ(0, acl_add_perm(perms, ACL_READ)); + ATF_CHECK_EQ(1, acl_cmp_np(empty_acl, acl1)); + + /* make a dup of non-empty acl and check that they are equal */ + acl2 = acl_dup(acl1); + ATF_REQUIRE(acl2 != NULL); + ATF_CHECK_EQ(0, acl_cmp_np(acl1, acl2)); + + /* change the tag type and compare */ + ATF_REQUIRE_EQ(1, acl_get_entry(acl1, ACL_FIRST_ENTRY, &entry)); + ATF_REQUIRE_EQ(0, acl_set_tag_type(entry, ACL_GROUP_OBJ)); + ATF_CHECK_EQ(1, acl_cmp_np(acl1, acl2)); + + /* change the permset and compare */ + acl_free(acl2); + acl2 = acl_dup(acl1); + ATF_REQUIRE(acl2 != NULL); + ATF_REQUIRE_EQ(1, acl_get_entry(acl1, ACL_FIRST_ENTRY, &entry)); + ATF_REQUIRE_EQ(0, acl_get_permset(entry, &perms)); + ATF_REQUIRE_EQ(0, acl_clear_perms(perms)); + ATF_CHECK_EQ(1, acl_cmp_np(acl1, acl2)); + + /* check that passing NULL yields EINVAL */ + ATF_CHECK_ERRNO(EINVAL, acl_cmp_np(NULL, NULL)); + ATF_CHECK_ERRNO(EINVAL, acl_cmp_np(acl1, NULL)); + ATF_CHECK_ERRNO(EINVAL, acl_cmp_np(NULL, acl1)); + + acl_free(empty_acl); + acl_free(acl1); + acl_free(acl2); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, acl_mode_roundup); + ATF_TP_ADD_TC(tp, acl_equiv_mode_test); + ATF_TP_ADD_TC(tp, acl_cmp_test); + + return (atf_no_error()); +}