Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 2 Feb 2021 10:08:28 GMT
From:      Alex Richardson <arichardson@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 40407d3998d1 - main - tests/sys/audit: Skip extattr tests if extattrs are not supported
Message-ID:  <202102021008.112A8Skf069912@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by arichardson:

URL: https://cgit.FreeBSD.org/src/commit/?id=40407d3998d1a12cbe929721f4dbe72b4be478a6

commit 40407d3998d1a12cbe929721f4dbe72b4be478a6
Author:     Alex Richardson <arichardson@FreeBSD.org>
AuthorDate: 2021-02-02 09:55:18 +0000
Commit:     Alex Richardson <arichardson@FreeBSD.org>
CommitDate: 2021-02-02 09:55:19 +0000

    tests/sys/audit: Skip extattr tests if extattrs are not supported
    
    In the CheriBSD CI, we run the testsuite with /tmp as tmpfs. This causes
    the extattr audit tests to fail since tmpfs does not (yet) support
    extattrs. Skip those tests if the target path is on a file system that
    does not support extended file attributes.
    
    While touching these two files also convert the ATF_REQUIRE_EQ(-1, ...)
    checks to use ATF_REQURIE_ERRNO().
    
    Reviewed By:    asomers
    Differential Revision: https://reviews.freebsd.org/D28392
---
 tests/sys/audit/file-attribute-access.c | 129 ++++++++++++++++++--------------
 tests/sys/audit/file-attribute-modify.c | 106 +++++++++++++++-----------
 tests/sys/audit/utils.c                 |  18 +++++
 tests/sys/audit/utils.h                 |  21 ++++++
 4 files changed, 173 insertions(+), 101 deletions(-)

diff --git a/tests/sys/audit/file-attribute-access.c b/tests/sys/audit/file-attribute-access.c
index 016d5772b933..9f4514bc3fde 100644
--- a/tests/sys/audit/file-attribute-access.c
+++ b/tests/sys/audit/file-attribute-access.c
@@ -27,13 +27,15 @@
 
 #include <sys/param.h>
 #include <sys/extattr.h>
-#include <sys/ucred.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
 #include <sys/syscall.h>
+#include <sys/ucred.h>
 
 #include <atf-c.h>
+#include <errno.h>
 #include <fcntl.h>
+#include <string.h>
 #include <unistd.h>
 
 #include "utils.h"
@@ -89,7 +91,7 @@ ATF_TC_BODY(stat_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, stat(errpath, &statbuff));
+	ATF_REQUIRE_ERRNO(ENOENT, stat(errpath, &statbuff) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -132,7 +134,7 @@ ATF_TC_BODY(lstat_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: symbolic link does not exist */
-	ATF_REQUIRE_EQ(-1, lstat(errpath, &statbuff));
+	ATF_REQUIRE_ERRNO(ENOENT, lstat(errpath, &statbuff) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -180,7 +182,7 @@ ATF_TC_BODY(fstat_failure, tc)
 	FILE *pipefd = setup(fds, auclass);
 	const char *regex = "fstat.*return,failure : Bad file descriptor";
 	/* Failure reason: bad file descriptor */
-	ATF_REQUIRE_EQ(-1, fstat(-1, &statbuff));
+	ATF_REQUIRE_ERRNO(EBADF, fstat(-1, &statbuff) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -224,8 +226,8 @@ ATF_TC_BODY(fstatat_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: symbolic link does not exist */
-	ATF_REQUIRE_EQ(-1, fstatat(AT_FDCWD, path, &statbuff,
-		AT_SYMLINK_NOFOLLOW));
+	ATF_REQUIRE_ERRNO(ENOENT,
+	    fstatat(AT_FDCWD, path, &statbuff, AT_SYMLINK_NOFOLLOW) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -269,7 +271,7 @@ ATF_TC_BODY(statfs_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, statfs(errpath, &statfsbuff));
+	ATF_REQUIRE_ERRNO(ENOENT, statfs(errpath, &statfsbuff) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -319,7 +321,7 @@ ATF_TC_BODY(fstatfs_failure, tc)
 	FILE *pipefd = setup(fds, auclass);
 	const char *regex = "fstatfs.*return,failure : Bad file descriptor";
 	/* Failure reason: bad file descriptor */
-	ATF_REQUIRE_EQ(-1, fstatfs(-1, &statfsbuff));
+	ATF_REQUIRE_ERRNO(EBADF, fstatfs(-1, &statfsbuff) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -364,7 +366,7 @@ ATF_TC_BODY(getfsstat_failure, tc)
 	const char *regex = "getfsstat.*return,failure : Invalid argument";
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: Invalid value for mode */
-	ATF_REQUIRE_EQ(-1, getfsstat(NULL, 0, -1));
+	ATF_REQUIRE_ERRNO(EINVAL, getfsstat(NULL, 0, -1) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -409,7 +411,7 @@ ATF_TC_BODY(lgetfh_failure, tc)
 	const char *regex = "lgetfh.*return,failure";
 	FILE *pipefd = setup(fds, "fa");
 	/* Failure reason: symbolic link does not exist */
-	ATF_REQUIRE_EQ(-1, lgetfh(errpath, &fht));
+	ATF_REQUIRE_ERRNO(ENOENT, lgetfh(errpath, &fht) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -465,7 +467,7 @@ ATF_TC_BODY(fhopen_failure, tc)
 	 * Failure reason: NULL does not represent any file handle
 	 * and O_CREAT is not allowed as the flag for fhopen(2)
 	 */
-	ATF_REQUIRE_EQ(-1, fhopen(NULL, O_CREAT));
+	ATF_REQUIRE_ERRNO(EINVAL, fhopen(NULL, O_CREAT) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -516,7 +518,7 @@ ATF_TC_BODY(fhstat_failure, tc)
 	const char *regex = "fhstat.*return,failure : Bad address";
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: NULL does not represent any file handle */
-	ATF_REQUIRE_EQ(-1, fhstat(NULL, NULL));
+	ATF_REQUIRE_ERRNO(EFAULT, fhstat(NULL, NULL) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -567,7 +569,7 @@ ATF_TC_BODY(fhstatfs_failure, tc)
 	const char *regex = "fhstatfs.*return,failure : Bad address";
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: NULL does not represent any file handle */
-	ATF_REQUIRE_EQ(-1, fhstatfs(NULL, NULL));
+	ATF_REQUIRE_ERRNO(EFAULT, fhstatfs(NULL, NULL) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -611,7 +613,7 @@ ATF_TC_BODY(access_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, access(errpath, F_OK));
+	ATF_REQUIRE_ERRNO(ENOENT, access(errpath, F_OK) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -655,7 +657,7 @@ ATF_TC_BODY(eaccess_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, eaccess(errpath, F_OK));
+	ATF_REQUIRE_ERRNO(ENOENT, eaccess(errpath, F_OK) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -699,7 +701,8 @@ ATF_TC_BODY(faccessat_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, faccessat(AT_FDCWD, errpath, F_OK, AT_EACCESS));
+	ATF_REQUIRE_ERRNO(ENOENT,
+	    faccessat(AT_FDCWD, errpath, F_OK, AT_EACCESS) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -744,7 +747,7 @@ ATF_TC_BODY(pathconf_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, pathconf(errpath, _PC_NAME_MAX));
+	ATF_REQUIRE_ERRNO(ENOENT, pathconf(errpath, _PC_NAME_MAX) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -788,7 +791,7 @@ ATF_TC_BODY(lpathconf_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: symbolic link does not exist */
-	ATF_REQUIRE_EQ(-1, lpathconf(errpath, _PC_SYMLINK_MAX));
+	ATF_REQUIRE_ERRNO(ENOENT, lpathconf(errpath, _PC_SYMLINK_MAX) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -837,7 +840,7 @@ ATF_TC_BODY(fpathconf_failure, tc)
 	FILE *pipefd = setup(fds, auclass);
 	const char *regex = "fpathconf.*return,failure : Bad file descriptor";
 	/* Failure reason: Bad file descriptor */
-	ATF_REQUIRE_EQ(-1, fpathconf(-1, _PC_NAME_MAX));
+	ATF_REQUIRE_ERRNO(EBADF, fpathconf(-1, _PC_NAME_MAX) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -858,17 +861,20 @@ ATF_TC_BODY(extattr_get_file_success, tc)
 {
 	/* File needs to exist to call extattr_get_file(2) */
 	ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
+	skip_if_extattr_not_supported(path);
+
 	/* Set an extended attribute to be retrieved later on */
-	ATF_REQUIRE_EQ(sizeof(buff), extattr_set_file(path,
-		EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
+	REQUIRE_EXTATTR_RESULT(sizeof(buff),
+	    extattr_set_file(path, EXTATTR_NAMESPACE_USER, name, buff,
+		sizeof(buff)));
 
 	/* Prepare the regex to be checked in the audit record */
 	snprintf(extregex, sizeof(extregex),
 		"extattr_get_file.*%s.*%s.*return,success", path, name);
 
 	FILE *pipefd = setup(fds, auclass);
-	ATF_REQUIRE_EQ(sizeof(buff), extattr_get_file(path,
-		EXTATTR_NAMESPACE_USER, name, NULL, 0));
+	REQUIRE_EXTATTR_RESULT(sizeof(buff),
+	    extattr_get_file(path, EXTATTR_NAMESPACE_USER, name, NULL, 0));
 	check_audit(fds, extregex, pipefd);
 	close(filedesc);
 }
@@ -894,8 +900,9 @@ ATF_TC_BODY(extattr_get_file_failure, tc)
 
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, extattr_get_file(path,
-		EXTATTR_NAMESPACE_USER, name, NULL, 0));
+	ATF_REQUIRE_ERRNO(ENOENT,
+	    extattr_get_file(path, EXTATTR_NAMESPACE_USER, name, NULL, 0) ==
+		-1);
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -916,17 +923,20 @@ ATF_TC_BODY(extattr_get_fd_success, tc)
 {
 	/* File needs to exist to call extattr_get_fd(2) */
 	ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
+	skip_if_extattr_not_supported(path);
+
 	/* Set an extended attribute to be retrieved later on */
-	ATF_REQUIRE_EQ(sizeof(buff), extattr_set_file(path,
-		EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
+	REQUIRE_EXTATTR_RESULT(sizeof(buff),
+	    extattr_set_file(path, EXTATTR_NAMESPACE_USER, name, buff,
+		sizeof(buff)));
 
 	/* Prepare the regex to be checked in the audit record */
 	snprintf(extregex, sizeof(extregex),
 		"extattr_get_fd.*%s.*return,success", name);
 
 	FILE *pipefd = setup(fds, auclass);
-	ATF_REQUIRE_EQ(sizeof(buff), extattr_get_fd(filedesc,
-		EXTATTR_NAMESPACE_USER, name, NULL, 0));
+	REQUIRE_EXTATTR_RESULT(sizeof(buff),
+	    extattr_get_fd(filedesc, EXTATTR_NAMESPACE_USER, name, NULL, 0));
 	check_audit(fds, extregex, pipefd);
 	close(filedesc);
 }
@@ -952,8 +962,8 @@ ATF_TC_BODY(extattr_get_fd_failure, tc)
 
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: Invalid file descriptor */
-	ATF_REQUIRE_EQ(-1, extattr_get_fd(-1,
-		EXTATTR_NAMESPACE_USER, name, NULL, 0));
+	ATF_REQUIRE_ERRNO(EBADF,
+	    extattr_get_fd(-1, EXTATTR_NAMESPACE_USER, name, NULL, 0) == -1);
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -974,17 +984,20 @@ ATF_TC_BODY(extattr_get_link_success, tc)
 {
 	/* Symbolic link needs to exist to call extattr_get_link(2) */
 	ATF_REQUIRE_EQ(0, symlink("symlink", path));
+	skip_if_extattr_not_supported(".");
+
 	/* Set an extended attribute to be retrieved later on */
-	ATF_REQUIRE_EQ(sizeof(buff), extattr_set_link(path,
-		EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
+	REQUIRE_EXTATTR_RESULT(sizeof(buff),
+	    extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, buff,
+		sizeof(buff)));
 
 	/* Prepare the regex to be checked in the audit record */
 	snprintf(extregex, sizeof(extregex),
 		"extattr_get_link.*%s.*%s.*return,success", path, name);
 
 	FILE *pipefd = setup(fds, auclass);
-	ATF_REQUIRE_EQ(sizeof(buff), extattr_get_link(path,
-		EXTATTR_NAMESPACE_USER, name, NULL, 0));
+	REQUIRE_EXTATTR_RESULT(sizeof(buff),
+	    extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, NULL, 0));
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -1008,8 +1021,8 @@ ATF_TC_BODY(extattr_get_link_failure, tc)
 		"extattr_get_link.*%s.*%s.*failure", path, name);
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: symbolic link does not exist */
-	ATF_REQUIRE_EQ(-1, extattr_get_link(path,
-		EXTATTR_NAMESPACE_USER, name, NULL, 0));
+	ATF_REQUIRE_ERRNO(ENOENT,
+	    extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, NULL, 0));
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -1028,16 +1041,17 @@ ATF_TC_HEAD(extattr_list_file_success, tc)
 
 ATF_TC_BODY(extattr_list_file_success, tc)
 {
-	int readbuff;
+	ssize_t readbuff;
 	/* File needs to exist to call extattr_list_file(2) */
 	ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
+	skip_if_extattr_not_supported(path);
 
 	FILE *pipefd = setup(fds, auclass);
-	ATF_REQUIRE((readbuff = extattr_list_file(path,
-		EXTATTR_NAMESPACE_USER, NULL, 0)) != -1);
+	readbuff = REQUIRE_EXTATTR_SUCCESS(
+	    extattr_list_file(path, EXTATTR_NAMESPACE_USER, NULL, 0));
 	/* Prepare the regex to be checked in the audit record */
 	snprintf(extregex, sizeof(extregex),
-		"extattr_list_file.*%s.*return,success,%d", path, readbuff);
+	    "extattr_list_file.*%s.*return,success,%zd", path, readbuff);
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -1062,8 +1076,8 @@ ATF_TC_BODY(extattr_list_file_failure, tc)
 
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, extattr_list_file(path,
-		EXTATTR_NAMESPACE_USER, NULL, 0));
+	ATF_REQUIRE_ERRNO(ENOENT,
+	    extattr_list_file(path, EXTATTR_NAMESPACE_USER, NULL, 0));
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -1082,16 +1096,17 @@ ATF_TC_HEAD(extattr_list_fd_success, tc)
 
 ATF_TC_BODY(extattr_list_fd_success, tc)
 {
-	int readbuff;
+	ssize_t readbuff;
 	/* File needs to exist to call extattr_list_fd(2) */
 	ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
+	skip_if_extattr_not_supported(path);
 
 	FILE *pipefd = setup(fds, auclass);
-	ATF_REQUIRE((readbuff = extattr_list_fd(filedesc,
-		EXTATTR_NAMESPACE_USER, NULL, 0)) != -1);
+	readbuff = REQUIRE_EXTATTR_SUCCESS(
+	    extattr_list_fd(filedesc, EXTATTR_NAMESPACE_USER, NULL, 0));
 	/* Prepare the regex to be checked in the audit record */
 	snprintf(extregex, sizeof(extregex),
-		"extattr_list_fd.*return,success,%d", readbuff);
+	    "extattr_list_fd.*return,success,%zd", readbuff);
 	check_audit(fds, extregex, pipefd);
 	close(filedesc);
 }
@@ -1117,8 +1132,8 @@ ATF_TC_BODY(extattr_list_fd_failure, tc)
 
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: Invalid file descriptor */
-	ATF_REQUIRE_EQ(-1,
-		extattr_list_fd(-1, EXTATTR_NAMESPACE_USER, NULL, 0));
+	ATF_REQUIRE_ERRNO(EBADF,
+	    extattr_list_fd(-1, EXTATTR_NAMESPACE_USER, NULL, 0) == -1);
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -1137,16 +1152,17 @@ ATF_TC_HEAD(extattr_list_link_success, tc)
 
 ATF_TC_BODY(extattr_list_link_success, tc)
 {
-	int readbuff;
+	ssize_t readbuff;
 	/* Symbolic link needs to exist to call extattr_list_link(2) */
 	ATF_REQUIRE_EQ(0, symlink("symlink", path));
-	FILE *pipefd = setup(fds, auclass);
+	skip_if_extattr_not_supported(".");
 
-	ATF_REQUIRE((readbuff = extattr_list_link(path,
-		EXTATTR_NAMESPACE_USER, NULL, 0)) != -1);
+	FILE *pipefd = setup(fds, auclass);
+	readbuff = REQUIRE_EXTATTR_SUCCESS(
+	    extattr_list_link(path, EXTATTR_NAMESPACE_USER, NULL, 0));
 	/* Prepare the regex to be checked in the audit record */
 	snprintf(extregex, sizeof(extregex),
-		"extattr_list_link.*%s.*return,success,%d", path, readbuff);
+	    "extattr_list_link.*%s.*return,success,%zd", path, readbuff);
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -1170,8 +1186,8 @@ ATF_TC_BODY(extattr_list_link_failure, tc)
 		"extattr_list_link.*%s.*failure", path);
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: symbolic link does not exist */
-	ATF_REQUIRE_EQ(-1, extattr_list_link(path,
-		EXTATTR_NAMESPACE_USER, NULL, 0));
+	ATF_REQUIRE_ERRNO(ENOENT,
+	    extattr_list_link(path, EXTATTR_NAMESPACE_USER, NULL, 0) == -1);
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -1236,6 +1252,5 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, extattr_list_fd_failure);
 	ATF_TP_ADD_TC(tp, extattr_list_link_success);
 	ATF_TP_ADD_TC(tp, extattr_list_link_failure);
-
 	return (atf_no_error());
 }
diff --git a/tests/sys/audit/file-attribute-modify.c b/tests/sys/audit/file-attribute-modify.c
index 8df1d6d2d95f..689d7a0b8d61 100644
--- a/tests/sys/audit/file-attribute-modify.c
+++ b/tests/sys/audit/file-attribute-modify.c
@@ -33,6 +33,7 @@
 #include <sys/time.h>
 
 #include <atf-c.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <stdint.h>
 #include <unistd.h>
@@ -92,7 +93,7 @@ ATF_TC_BODY(flock_failure, tc)
 {
 	const char *regex = "flock.*return,failure : Bad file descriptor";
 	FILE *pipefd = setup(fds, auclass);
-	ATF_REQUIRE_EQ(-1, flock(-1, LOCK_SH));
+	ATF_REQUIRE_ERRNO(EBADF, flock(-1, LOCK_SH) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -141,7 +142,7 @@ ATF_TC_BODY(fcntl_failure, tc)
 {
 	const char *regex = "fcntl.*return,failure : Bad file descriptor";
 	FILE *pipefd = setup(fds, auclass);
-	ATF_REQUIRE_EQ(-1, fcntl(-1, F_GETFL, 0));
+	ATF_REQUIRE_ERRNO(EBADF, fcntl(-1, F_GETFL, 0) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -189,7 +190,7 @@ ATF_TC_BODY(fsync_failure, tc)
 	const char *regex = "fsync.*return,failure : Bad file descriptor";
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: Invalid file descriptor */
-	ATF_REQUIRE_EQ(-1, fsync(-1));
+	ATF_REQUIRE_ERRNO(EBADF, fsync(-1) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -233,7 +234,7 @@ ATF_TC_BODY(chmod_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, chmod(errpath, mode));
+	ATF_REQUIRE_ERRNO(ENOENT, chmod(errpath, mode) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -281,7 +282,7 @@ ATF_TC_BODY(fchmod_failure, tc)
 	const char *regex = "fchmod.*return,failure : Bad file descriptor";
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: Invalid file descriptor */
-	ATF_REQUIRE_EQ(-1, fchmod(-1, mode));
+	ATF_REQUIRE_ERRNO(EBADF, fchmod(-1, mode) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -324,7 +325,7 @@ ATF_TC_BODY(lchmod_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, lchmod(errpath, mode));
+	ATF_REQUIRE_ERRNO(ENOENT, lchmod(errpath, mode) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -368,7 +369,7 @@ ATF_TC_BODY(fchmodat_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, fchmodat(AT_FDCWD, errpath, mode, 0));
+	ATF_REQUIRE_ERRNO(ENOENT, fchmodat(AT_FDCWD, errpath, mode, 0) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -412,7 +413,7 @@ ATF_TC_BODY(chown_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, chown(errpath, uid, gid));
+	ATF_REQUIRE_ERRNO(ENOENT, chown(errpath, uid, gid) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -460,7 +461,7 @@ ATF_TC_BODY(fchown_failure, tc)
 	const char *regex = "fchown.*return,failure : Bad file descriptor";
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: Invalid file descriptor */
-	ATF_REQUIRE_EQ(-1, fchown(-1, uid, gid));
+	ATF_REQUIRE_ERRNO(EBADF, fchown(-1, uid, gid) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -503,7 +504,7 @@ ATF_TC_BODY(lchown_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: Symbolic link does not exist */
-	ATF_REQUIRE_EQ(-1, lchown(errpath, uid, gid));
+	ATF_REQUIRE_ERRNO(ENOENT, lchown(errpath, uid, gid) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -547,7 +548,8 @@ ATF_TC_BODY(fchownat_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, fchownat(AT_FDCWD, errpath, uid, gid, 0));
+	ATF_REQUIRE_ERRNO(ENOENT,
+	    fchownat(AT_FDCWD, errpath, uid, gid, 0) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -591,7 +593,7 @@ ATF_TC_BODY(chflags_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, chflags(errpath, UF_OFFLINE));
+	ATF_REQUIRE_ERRNO(ENOENT, chflags(errpath, UF_OFFLINE) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -639,7 +641,7 @@ ATF_TC_BODY(fchflags_failure, tc)
 	const char *regex = "fchflags.*return,failure : Bad file descriptor";
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: Invalid file descriptor */
-	ATF_REQUIRE_EQ(-1, fchflags(-1, UF_OFFLINE));
+	ATF_REQUIRE_ERRNO(EBADF, fchflags(-1, UF_OFFLINE) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -682,7 +684,7 @@ ATF_TC_BODY(lchflags_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: Symbolic link does not exist */
-	ATF_REQUIRE_EQ(-1, lchflags(errpath, UF_OFFLINE));
+	ATF_REQUIRE_ERRNO(ENOENT, lchflags(errpath, UF_OFFLINE) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -726,7 +728,8 @@ ATF_TC_BODY(chflagsat_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, chflagsat(AT_FDCWD, errpath, UF_OFFLINE, 0));
+	ATF_REQUIRE_ERRNO(ENOENT,
+	    chflagsat(AT_FDCWD, errpath, UF_OFFLINE, 0) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -770,7 +773,7 @@ ATF_TC_BODY(utimes_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, utimes(errpath, NULL));
+	ATF_REQUIRE_ERRNO(ENOENT, utimes(errpath, NULL) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -818,7 +821,7 @@ ATF_TC_BODY(futimes_failure, tc)
 	const char *regex = "futimes.*return,failure : Bad file descriptor";
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: Invalid file descriptor */
-	ATF_REQUIRE_EQ(-1, futimes(-1, NULL));
+	ATF_REQUIRE_ERRNO(EBADF, futimes(-1, NULL) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -861,7 +864,7 @@ ATF_TC_BODY(lutimes_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: symbolic link does not exist */
-	ATF_REQUIRE_EQ(-1, lutimes(errpath, NULL));
+	ATF_REQUIRE_ERRNO(ENOENT, lutimes(errpath, NULL) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -905,7 +908,7 @@ ATF_TC_BODY(futimesat_failure, tc)
 {
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, futimesat(AT_FDCWD, errpath, NULL));
+	ATF_REQUIRE_ERRNO(ENOENT, futimesat(AT_FDCWD, errpath, NULL) == -1);
 	check_audit(fds, failurereg, pipefd);
 }
 
@@ -949,7 +952,8 @@ ATF_TC_BODY(mprotect_failure, tc)
 {
 	const char *regex = "mprotect.*return,failure : Invalid argument";
 	FILE *pipefd = setup(fds, auclass);
-	ATF_REQUIRE_EQ(-1, mprotect((void *)SIZE_MAX, -1, PROT_NONE));
+	ATF_REQUIRE_ERRNO(EINVAL,
+	    mprotect((void *)SIZE_MAX, -1, PROT_NONE) == -1);
 	check_audit(fds, regex, pipefd);
 }
 
@@ -977,7 +981,7 @@ ATF_TC_BODY(undelete_failure, tc)
 
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: File does not exist */
-	ATF_REQUIRE_EQ(-1, undelete(errpath));
+	ATF_REQUIRE_ERRNO(ENOENT, undelete(errpath) == -1);
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -998,12 +1002,14 @@ ATF_TC_BODY(extattr_set_file_success, tc)
 {
 	/* File needs to exist to call extattr_set_file(2) */
 	ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
+	skip_if_extattr_not_supported(path);
+
 	/* Prepare the regex to be checked in the audit record */
 	snprintf(extregex, sizeof(extregex),
 		"extattr_set_file.*%s.*%s.*return,success", path, name);
 
 	FILE *pipefd = setup(fds, auclass);
-	ATF_REQUIRE_EQ(sizeof(buff), extattr_set_file(path,
+	REQUIRE_EXTATTR_RESULT(sizeof(buff), extattr_set_file(path,
 		EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
 	check_audit(fds, extregex, pipefd);
 	close(filedesc);
@@ -1030,8 +1036,9 @@ ATF_TC_BODY(extattr_set_file_failure, tc)
 
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, extattr_set_file(path,
-		EXTATTR_NAMESPACE_USER, name, NULL, 0));
+	ATF_REQUIRE_ERRNO(ENOENT,
+	    extattr_set_file(path, EXTATTR_NAMESPACE_USER, name, NULL, 0) ==
+		-1);
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -1052,13 +1059,14 @@ ATF_TC_BODY(extattr_set_fd_success, tc)
 {
 	/* File needs to exist to call extattr_set_fd(2) */
 	ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
+	skip_if_extattr_not_supported(path);
 
 	/* Prepare the regex to be checked in the audit record */
 	snprintf(extregex, sizeof(extregex),
 		"extattr_set_fd.*%s.*return,success", name);
 
 	FILE *pipefd = setup(fds, auclass);
-	ATF_REQUIRE_EQ(sizeof(buff), extattr_set_fd(filedesc,
+	REQUIRE_EXTATTR_RESULT(sizeof(buff), extattr_set_fd(filedesc,
 		EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
 	check_audit(fds, extregex, pipefd);
 	close(filedesc);
@@ -1085,8 +1093,8 @@ ATF_TC_BODY(extattr_set_fd_failure, tc)
 
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: Invalid file descriptor */
-	ATF_REQUIRE_EQ(-1, extattr_set_fd(-1,
-		EXTATTR_NAMESPACE_USER, name, NULL, 0));
+	ATF_REQUIRE_ERRNO(EBADF,
+	    extattr_set_fd(-1, EXTATTR_NAMESPACE_USER, name, NULL, 0) == -1);
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -1107,12 +1115,14 @@ ATF_TC_BODY(extattr_set_link_success, tc)
 {
 	/* Symbolic link needs to exist to call extattr_set_link(2) */
 	ATF_REQUIRE_EQ(0, symlink("symlink", path));
+	skip_if_extattr_not_supported(".");
+
 	/* Prepare the regex to be checked in the audit record */
 	snprintf(extregex, sizeof(extregex),
 		"extattr_set_link.*%s.*%s.*return,success", path, name);
 
 	FILE *pipefd = setup(fds, auclass);
-	ATF_REQUIRE_EQ(sizeof(buff), extattr_set_link(path,
+	REQUIRE_EXTATTR_RESULT(sizeof(buff), extattr_set_link(path,
 		EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
 
 	check_audit(fds, extregex, pipefd);
@@ -1138,8 +1148,9 @@ ATF_TC_BODY(extattr_set_link_failure, tc)
 		"extattr_set_link.*%s.*%s.*failure", path, name);
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: symbolic link does not exist */
-	ATF_REQUIRE_EQ(-1, extattr_set_link(path,
-		EXTATTR_NAMESPACE_USER, name, NULL, 0));
+	ATF_REQUIRE_ERRNO(ENOENT,
+	    extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, NULL, 0) ==
+		-1);
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -1160,12 +1171,14 @@ ATF_TC_BODY(extattr_delete_file_success, tc)
 {
 	/* File needs to exist to call extattr_delete_file(2) */
 	ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
-	ATF_REQUIRE_EQ(sizeof(buff), extattr_set_file(path,
+	skip_if_extattr_not_supported(path);
+
+	REQUIRE_EXTATTR_RESULT(sizeof(buff), extattr_set_file(path,
 		EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
 
 	FILE *pipefd = setup(fds, auclass);
-	ATF_REQUIRE((retval = extattr_delete_file(path,
-		EXTATTR_NAMESPACE_USER, name)) != -1);
+	retval = REQUIRE_EXTATTR_SUCCESS(
+	    extattr_delete_file(path, EXTATTR_NAMESPACE_USER, name));
 	/* Prepare the regex to be checked in the audit record */
 	snprintf(extregex, sizeof(extregex),
 	"extattr_delete_file.*%s.*return,success,%d", path, retval);
@@ -1194,8 +1207,8 @@ ATF_TC_BODY(extattr_delete_file_failure, tc)
 
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: file does not exist */
-	ATF_REQUIRE_EQ(-1, extattr_delete_file(path,
-		EXTATTR_NAMESPACE_USER, name));
+	ATF_REQUIRE_ERRNO(ENOENT,
+	    extattr_delete_file(path, EXTATTR_NAMESPACE_USER, name) == -1);
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -1216,12 +1229,14 @@ ATF_TC_BODY(extattr_delete_fd_success, tc)
 {
 	/* File needs to exist to call extattr_delete_fd(2) */
 	ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
-	ATF_REQUIRE_EQ(sizeof(buff), extattr_set_file(path,
+	skip_if_extattr_not_supported(path);
+
+	REQUIRE_EXTATTR_RESULT(sizeof(buff), extattr_set_file(path,
 		EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
 
 	FILE *pipefd = setup(fds, auclass);
-	ATF_REQUIRE((retval = extattr_delete_fd(filedesc,
-		EXTATTR_NAMESPACE_USER, name)) != -1);
+	retval = REQUIRE_EXTATTR_SUCCESS(extattr_delete_fd(filedesc,
+		EXTATTR_NAMESPACE_USER, name));
 	/* Prepare the regex to be checked in the audit record */
 	snprintf(extregex, sizeof(extregex),
 		"extattr_delete_fd.*return,success,%d", retval);
@@ -1250,7 +1265,8 @@ ATF_TC_BODY(extattr_delete_fd_failure, tc)
 
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: Invalid file descriptor */
-	ATF_REQUIRE_EQ(-1, extattr_delete_fd(-1, EXTATTR_NAMESPACE_USER, name));
+	ATF_REQUIRE_ERRNO(EBADF,
+	    extattr_delete_fd(-1, EXTATTR_NAMESPACE_USER, name) == -1);
 	check_audit(fds, extregex, pipefd);
 }
 
@@ -1271,12 +1287,14 @@ ATF_TC_BODY(extattr_delete_link_success, tc)
 {
 	/* Symbolic link needs to exist to call extattr_delete_link(2) */
 	ATF_REQUIRE_EQ(0, symlink("symlink", path));
-	ATF_REQUIRE_EQ(sizeof(buff), extattr_set_link(path,
+	skip_if_extattr_not_supported(".");
+
+	REQUIRE_EXTATTR_RESULT(sizeof(buff), extattr_set_link(path,
 		EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
 
 	FILE *pipefd = setup(fds, auclass);
-	ATF_REQUIRE((retval = extattr_delete_link(path,
-		EXTATTR_NAMESPACE_USER, name)) != -1);
+	retval = REQUIRE_EXTATTR_SUCCESS(extattr_delete_link(path,
+		EXTATTR_NAMESPACE_USER, name));
 	/* Prepare the regex to be checked in the audit record */
 	snprintf(extregex, sizeof(extregex),
 	"extattr_delete_link.*%s.*return,success,%d", path, retval);
@@ -1303,8 +1321,8 @@ ATF_TC_BODY(extattr_delete_link_failure, tc)
 		"extattr_delete_link.*%s.*failure", path);
 	FILE *pipefd = setup(fds, auclass);
 	/* Failure reason: symbolic link does not exist */
-	ATF_REQUIRE_EQ(-1, extattr_delete_link(path,
-		EXTATTR_NAMESPACE_USER, name));
+	ATF_REQUIRE_ERRNO(ENOENT,
+	    extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name) == -1);
 	check_audit(fds, extregex, pipefd);
 }
 
diff --git a/tests/sys/audit/utils.c b/tests/sys/audit/utils.c
index d7a1e6792b1c..be31f4138412 100644
--- a/tests/sys/audit/utils.c
+++ b/tests/sys/audit/utils.c
@@ -25,6 +25,8 @@
  * $FreeBSD$
  */
 
+#include <sys/types.h>
+#include <sys/extattr.h>
 #include <sys/ioctl.h>
 
 #include <bsm/libbsm.h>
@@ -204,6 +206,22 @@ check_audit(struct pollfd fd[], const char *auditrgx, FILE *pipestream) {
 	ATF_REQUIRE_EQ(0, fclose(pipestream));
 }
 
+void
+skip_if_extattr_not_supported(const char *path)
+{
+	ssize_t result;
+
+	/*
+	 * Some file systems (e.g. tmpfs) do not support extattr, so we need
+	 * skip tests that use extattrs. To detect this we can check whether
+	 * the extattr_list_file returns EOPNOTSUPP.
+	 */
+	result = extattr_list_file(path, EXTATTR_NAMESPACE_USER, NULL, 0);
+	if (result == -1 && errno == EOPNOTSUPP) {
+		atf_tc_skip("File system does not support extattrs.");
+	}
+}
+
 FILE
 *setup(struct pollfd fd[], const char *name)
 {
diff --git a/tests/sys/audit/utils.h b/tests/sys/audit/utils.h
index 51001148c269..2f7a9e5cebb2 100644
--- a/tests/sys/audit/utils.h
+++ b/tests/sys/audit/utils.h
@@ -37,5 +37,26 @@
 void check_audit(struct pollfd [], const char *, FILE *);
 FILE *setup(struct pollfd [], const char *);
 void cleanup(void);
+void skip_if_extattr_not_supported(const char *);
+
+#define REQUIRE_EXTATTR_SUCCESS(call)						\
+	({									\
+		errno = 0; /* Reset errno before call */			\
+		ssize_t result = (call);					\
+		if (result == -1) {						\
+			atf_tc_fail_requirement(__FILE__, __LINE__,		\
+			    "%s failed with errno %d (%s)", #call, errno,	\
+			    strerror(errno));					\
+		}								\
+		result;								\
+	})
+
+#define REQUIRE_EXTATTR_RESULT(_expected, expr)					\
+	do {									\
+		ssize_t expected = (_expected);					\
+		ssize_t _result = REQUIRE_EXTATTR_SUCCESS(expr);		\
+		ATF_REQUIRE_EQ_MSG(expected, _result, "%s: %zd != %zd", #expr,	\
+		    expected, _result);						\
+	} while (0)
 
 #endif  /* _SETUP_H_ */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202102021008.112A8Skf069912>