Introduce ASSERT_OK_ERRNO()

ASSERT_OK() is for functions that return negative errno. Let's
introduce ASSERT_OK_ERRNO() for syscalls that return -1 and store
the error in errno.
This commit is contained in:
Daan De Meyer 2024-04-10 13:31:58 +02:00
parent 29b27d59d5
commit 681e39c1dc
8 changed files with 41 additions and 25 deletions

View file

@ -215,6 +215,16 @@ static inline int run_test_table(void) {
} \
})
#define ASSERT_OK_ERRNO(expr) \
({ \
typeof(expr) _result = (expr); \
if (_result < 0) { \
log_error_errno(errno, "%s:%i: Assertion failed: expected \"%s\" to succeed but got the following error: %m", \
PROJECT_FILE, __LINE__, #expr); \
abort(); \
} \
})
#define ASSERT_TRUE(expr) \
({ \
if (!(expr)) { \

View file

@ -95,7 +95,7 @@ TEST_RET(fd_acl_make_read_only) {
/* make it more exciting */
(void) fd_add_uid_acl_permission(fd, 1, ACL_READ|ACL_WRITE|ACL_EXECUTE);
assert_se(fstat(fd, &st) >= 0);
ASSERT_OK_ERRNO(fstat(fd, &st));
assert_se(FLAGS_SET(st.st_mode, 0200));
cmd = strjoina("getfacl -p ", fn);
@ -107,7 +107,7 @@ TEST_RET(fd_acl_make_read_only) {
log_info("read-only");
assert_se(fd_acl_make_read_only(fd));
assert_se(fstat(fd, &st) >= 0);
ASSERT_OK_ERRNO(fstat(fd, &st));
assert_se((st.st_mode & 0222) == 0000);
cmd = strjoina("getfacl -p ", fn);
@ -119,7 +119,7 @@ TEST_RET(fd_acl_make_read_only) {
log_info("writable");
assert_se(fd_acl_make_writable(fd));
assert_se(fstat(fd, &st) >= 0);
ASSERT_OK_ERRNO(fstat(fd, &st));
assert_se((st.st_mode & 0222) == 0200);
cmd = strjoina("getfacl -p ", fn);
@ -131,7 +131,7 @@ TEST_RET(fd_acl_make_read_only) {
log_info("read-only");
assert_se(fd_acl_make_read_only(fd));
assert_se(fstat(fd, &st) >= 0);
ASSERT_OK_ERRNO(fstat(fd, &st));
assert_se((st.st_mode & 0222) == 0000);
cmd = strjoina("getfacl -p ", fn);

View file

@ -444,7 +444,7 @@ TEST_RET(copy_holes) {
return log_tests_skipped("Filesystem doesn't support hole punching");
assert_se(r >= 0);
assert_se(fstat(fd, &stat) >= 0);
ASSERT_OK_ERRNO(fstat(fd, &stat));
blksz = stat.st_blksize;
buf = alloca_safe(blksz);
memset(buf, 1, blksz);
@ -469,7 +469,7 @@ TEST_RET(copy_holes) {
assert_se(lseek(fd_copy, 2 * blksz, SEEK_DATA) < 0 && errno == ENXIO);
/* Test that the copied file has the correct size. */
assert_se(fstat(fd_copy, &stat) >= 0);
ASSERT_OK_ERRNO(fstat(fd_copy, &stat));
assert_se(stat.st_size == 3 * blksz);
close(fd);
@ -490,7 +490,7 @@ TEST_RET(copy_holes_with_gaps) {
assert_se((fd = openat(tfd, "src", O_CREAT | O_RDWR, 0600)) >= 0);
assert_se((fd_copy = openat(tfd, "dst", O_CREAT | O_WRONLY, 0600)) >= 0);
assert_se(fstat(fd, &st) >= 0);
ASSERT_OK_ERRNO(fstat(fd, &st));
blksz = st.st_blksize;
buf = alloca_safe(blksz);
memset(buf, 1, blksz);
@ -519,7 +519,7 @@ TEST_RET(copy_holes_with_gaps) {
/* Copy to the start of the second hole */
assert_se(copy_bytes(fd, fd_copy, 3 * blksz, COPY_HOLES) >= 0);
assert_se(fstat(fd_copy, &st) >= 0);
ASSERT_OK_ERRNO(fstat(fd_copy, &st));
assert_se(st.st_size == 3 * blksz);
/* Copy to the middle of the second hole */
@ -527,7 +527,7 @@ TEST_RET(copy_holes_with_gaps) {
assert_se(lseek(fd_copy, 0, SEEK_SET) >= 0);
assert_se(ftruncate(fd_copy, 0) >= 0);
assert_se(copy_bytes(fd, fd_copy, 4 * blksz, COPY_HOLES) >= 0);
assert_se(fstat(fd_copy, &st) >= 0);
ASSERT_OK_ERRNO(fstat(fd_copy, &st));
assert_se(st.st_size == 4 * blksz);
/* Copy to the end of the second hole */
@ -535,7 +535,7 @@ TEST_RET(copy_holes_with_gaps) {
assert_se(lseek(fd_copy, 0, SEEK_SET) >= 0);
assert_se(ftruncate(fd_copy, 0) >= 0);
assert_se(copy_bytes(fd, fd_copy, 5 * blksz, COPY_HOLES) >= 0);
assert_se(fstat(fd_copy, &st) >= 0);
ASSERT_OK_ERRNO(fstat(fd_copy, &st));
assert_se(st.st_size == 5 * blksz);
/* Copy everything */
@ -543,7 +543,7 @@ TEST_RET(copy_holes_with_gaps) {
assert_se(lseek(fd_copy, 0, SEEK_SET) >= 0);
assert_se(ftruncate(fd_copy, 0) >= 0);
assert_se(copy_bytes(fd, fd_copy, UINT64_MAX, COPY_HOLES) >= 0);
assert_se(fstat(fd_copy, &st) >= 0);
ASSERT_OK_ERRNO(fstat(fd_copy, &st));
assert_se(st.st_size == 6 * blksz);
return 0;

View file

@ -413,7 +413,7 @@ TEST(fd_reopen) {
fd1 = open("/proc", O_DIRECTORY|O_PATH|O_CLOEXEC);
assert_se(fd1 >= 0);
assert_se(fstat(fd1, &st1) >= 0);
ASSERT_OK_ERRNO(fstat(fd1, &st1));
assert_se(S_ISDIR(st1.st_mode));
fl = fcntl(fd1, F_GETFL);
@ -428,7 +428,7 @@ TEST(fd_reopen) {
fd2 = fd_reopen(fd1, O_RDONLY|O_DIRECTORY|O_CLOEXEC); /* drop the O_PATH */
assert_se(fd2 >= 0);
assert_se(fstat(fd2, &st2) >= 0);
ASSERT_OK_ERRNO(fstat(fd2, &st2));
assert_se(S_ISDIR(st2.st_mode));
assert_se(stat_inode_same(&st1, &st2));
@ -442,7 +442,7 @@ TEST(fd_reopen) {
fd1 = fd_reopen(fd2, O_DIRECTORY|O_PATH|O_CLOEXEC); /* reacquire the O_PATH */
assert_se(fd1 >= 0);
assert_se(fstat(fd1, &st1) >= 0);
ASSERT_OK_ERRNO(fstat(fd1, &st1));
assert_se(S_ISDIR(st1.st_mode));
assert_se(stat_inode_same(&st1, &st2));
@ -457,7 +457,7 @@ TEST(fd_reopen) {
fd1 = open("/proc/version", O_PATH|O_CLOEXEC);
assert_se(fd1 >= 0);
assert_se(fstat(fd1, &st1) >= 0);
ASSERT_OK_ERRNO(fstat(fd1, &st1));
assert_se(S_ISREG(st1.st_mode));
fl = fcntl(fd1, F_GETFL);
@ -469,7 +469,7 @@ TEST(fd_reopen) {
fd2 = fd_reopen(fd1, O_RDONLY|O_CLOEXEC); /* drop the O_PATH */
assert_se(fd2 >= 0);
assert_se(fstat(fd2, &st2) >= 0);
ASSERT_OK_ERRNO(fstat(fd2, &st2));
assert_se(S_ISREG(st2.st_mode));
assert_se(stat_inode_same(&st1, &st2));
@ -484,7 +484,7 @@ TEST(fd_reopen) {
fd1 = fd_reopen(fd2, O_PATH|O_CLOEXEC); /* reacquire the O_PATH */
assert_se(fd1 >= 0);
assert_se(fstat(fd1, &st1) >= 0);
ASSERT_OK_ERRNO(fstat(fd1, &st1));
assert_se(S_ISREG(st1.st_mode));
assert_se(stat_inode_same(&st1, &st2));
@ -501,12 +501,12 @@ TEST(fd_reopen) {
/* Validate what happens if we reopen a symlink */
fd1 = open("/proc/self", O_PATH|O_CLOEXEC|O_NOFOLLOW);
assert_se(fd1 >= 0);
assert_se(fstat(fd1, &st1) >= 0);
ASSERT_OK_ERRNO(fstat(fd1, &st1));
assert_se(S_ISLNK(st1.st_mode));
fd2 = fd_reopen(fd1, O_PATH|O_CLOEXEC);
assert_se(fd2 >= 0);
assert_se(fstat(fd2, &st2) >= 0);
ASSERT_OK_ERRNO(fstat(fd2, &st2));
assert_se(S_ISLNK(st2.st_mode));
assert_se(stat_inode_same(&st1, &st2));
fd2 = safe_close(fd2);

View file

@ -275,14 +275,14 @@ TEST(unlinkat_deallocate) {
assert_se(write(fd, "hallo\n", 6) == 6);
assert_se(fstat(fd, &st) >= 0);
ASSERT_OK_ERRNO(fstat(fd, &st));
assert_se(st.st_size == 6);
assert_se(st.st_blocks > 0);
assert_se(st.st_nlink == 1);
assert_se(unlinkat_deallocate(AT_FDCWD, p, UNLINK_ERASE) >= 0);
assert_se(fstat(fd, &st) >= 0);
ASSERT_OK_ERRNO(fstat(fd, &st));
assert_se(IN_SET(st.st_size, 0, 6)); /* depending on whether hole punching worked the size will be 6
(it worked) or 0 (we had to resort to truncation) */
assert_se(st.st_blocks == 0);
@ -557,13 +557,13 @@ TEST(open_mkdir_at) {
fd = open_mkdir_at(AT_FDCWD, "/", O_CLOEXEC, 0);
assert_se(fd >= 0);
assert_se(stat("/", &sta) >= 0);
assert_se(fstat(fd, &stb) >= 0);
ASSERT_OK_ERRNO(fstat(fd, &stb));
assert_se(stat_inode_same(&sta, &stb));
fd = safe_close(fd);
fd = open_mkdir_at(AT_FDCWD, ".", O_CLOEXEC, 0);
assert_se(stat(".", &sta) >= 0);
assert_se(fstat(fd, &stb) >= 0);
ASSERT_OK_ERRNO(fstat(fd, &stb));
assert_se(stat_inode_same(&sta, &stb));
fd = safe_close(fd);

View file

@ -1114,6 +1114,12 @@ TEST(ASSERT) {
ASSERT_SIGNAL(ASSERT_OK(-1), SIGABRT);
ASSERT_SIGNAL(ASSERT_OK(-ENOANO), SIGABRT);
ASSERT_OK_ERRNO(0 >= 0);
ASSERT_OK_ERRNO(255 >= 0);
ASSERT_OK_ERRNO(printf("Hello world\n"));
ASSERT_SIGNAL(ASSERT_OK_ERRNO(-1), SIGABRT);
ASSERT_SIGNAL(ASSERT_OK_ERRNO(-ENOANO), SIGABRT);
ASSERT_TRUE(true);
ASSERT_TRUE(255);
ASSERT_TRUE(getpid());

View file

@ -205,7 +205,7 @@ TEST(anonymous_inode) {
/* Verify that we handle anonymous inodes correctly, i.e. those which have no file type */
struct stat st;
assert_se(fstat(fd, &st) >= 0);
ASSERT_OK_ERRNO(fstat(fd, &st));
assert_se((st.st_mode & S_IFMT) == 0);
assert_se(!inode_type_to_string(st.st_mode));

View file

@ -155,7 +155,7 @@ TEST(get_ctty) {
}
/* In almost all cases STDIN will match our controlling TTY. Let's verify that and then compare paths */
assert_se(fstat(STDIN_FILENO, &st) >= 0);
ASSERT_OK_ERRNO(fstat(STDIN_FILENO, &st));
if (S_ISCHR(st.st_mode) && st.st_rdev == devnr) {
_cleanup_free_ char *stdin_name = NULL;