diff options
Diffstat (limited to '')
-rw-r--r-- | src/test/test-fd-util.c | 92 |
1 files changed, 57 insertions, 35 deletions
diff --git a/src/test/test-fd-util.c b/src/test/test-fd-util.c index 021d4b4..f2b65d4 100644 --- a/src/test/test-fd-util.c +++ b/src/test/test-fd-util.c @@ -138,6 +138,7 @@ TEST(rearrange_stdio) { if (r == 0) { _cleanup_free_ char *path = NULL; + int pipe_read_fd, pair[2]; char buffer[10]; /* Child */ @@ -145,6 +146,10 @@ TEST(rearrange_stdio) { safe_close(STDERR_FILENO); /* Let's close an fd < 2, to make it more interesting */ assert_se(rearrange_stdio(-EBADF, -EBADF, -EBADF) >= 0); + /* Reconfigure logging after rearranging stdout/stderr, so we still log to somewhere if the + * following tests fail, making it slightly less annoying to debug */ + log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); + log_open(); assert_se(fd_get_path(STDIN_FILENO, &path) >= 0); assert_se(path_equal(path, "/dev/null")); @@ -162,21 +167,20 @@ TEST(rearrange_stdio) { safe_close(STDOUT_FILENO); safe_close(STDERR_FILENO); - { - int pair[2]; - assert_se(pipe(pair) >= 0); - assert_se(pair[0] == 0); - assert_se(pair[1] == 1); - assert_se(fd_move_above_stdio(0) == 3); - } + assert_se(pipe(pair) >= 0); + assert_se(pair[0] == 0); + assert_se(pair[1] == 1); + pipe_read_fd = fd_move_above_stdio(0); + assert_se(pipe_read_fd >= 3); + assert_se(open("/dev/full", O_WRONLY|O_CLOEXEC) == 0); - assert_se(acquire_data_fd("foobar", 6, 0) == 2); + assert_se(acquire_data_fd("foobar") == 2); assert_se(rearrange_stdio(2, 0, 1) >= 0); assert_se(write(1, "x", 1) < 0 && errno == ENOSPC); assert_se(write(2, "z", 1) == 1); - assert_se(read(3, buffer, sizeof(buffer)) == 1); + assert_se(read(pipe_read_fd, buffer, sizeof(buffer)) == 1); assert_se(buffer[0] == 'z'); assert_se(read(0, buffer, sizeof(buffer)) == 6); assert_se(memcmp(buffer, "foobar", 6) == 0); @@ -184,7 +188,7 @@ TEST(rearrange_stdio) { assert_se(rearrange_stdio(-EBADF, 1, 2) >= 0); assert_se(write(1, "a", 1) < 0 && errno == ENOSPC); assert_se(write(2, "y", 1) == 1); - assert_se(read(3, buffer, sizeof(buffer)) == 1); + assert_se(read(pipe_read_fd, buffer, sizeof(buffer)) == 1); assert_se(buffer[0] == 'y'); assert_se(fd_get_path(0, &path) >= 0); @@ -393,11 +397,11 @@ TEST(close_all_fds) { } TEST(format_proc_fd_path) { - assert_se(streq_ptr(FORMAT_PROC_FD_PATH(0), "/proc/self/fd/0")); - assert_se(streq_ptr(FORMAT_PROC_FD_PATH(1), "/proc/self/fd/1")); - assert_se(streq_ptr(FORMAT_PROC_FD_PATH(2), "/proc/self/fd/2")); - assert_se(streq_ptr(FORMAT_PROC_FD_PATH(3), "/proc/self/fd/3")); - assert_se(streq_ptr(FORMAT_PROC_FD_PATH(2147483647), "/proc/self/fd/2147483647")); + ASSERT_STREQ(FORMAT_PROC_FD_PATH(0), "/proc/self/fd/0"); + ASSERT_STREQ(FORMAT_PROC_FD_PATH(1), "/proc/self/fd/1"); + ASSERT_STREQ(FORMAT_PROC_FD_PATH(2), "/proc/self/fd/2"); + ASSERT_STREQ(FORMAT_PROC_FD_PATH(3), "/proc/self/fd/3"); + ASSERT_STREQ(FORMAT_PROC_FD_PATH(2147483647), "/proc/self/fd/2147483647"); } TEST(fd_reopen) { @@ -409,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); @@ -424,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)); @@ -438,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)); @@ -453,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); @@ -465,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)); @@ -480,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)); @@ -497,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); @@ -646,6 +650,24 @@ TEST(dir_fd_is_root) { assert_se(dir_fd_is_root_or_cwd(fd) == 0); } +TEST(fds_are_same_mount) { + _cleanup_close_ int fd1 = -EBADF, fd2 = -EBADF, fd3 = -EBADF, fd4 = -EBADF; + + fd1 = open("/sys", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW); + fd2 = open("/proc", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW); + fd3 = open("/proc", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW); + fd4 = open("/", O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW); + + if (fd1 < 0 || fd2 < 0 || fd3 < 0 || fd4 < 0) + return (void) log_tests_skipped_errno(errno, "Failed to open /sys or /proc or /"); + + if (fds_are_same_mount(fd1, fd4) > 0 && fds_are_same_mount(fd2, fd4) > 0) + return (void) log_tests_skipped("Cannot test fds_are_same_mount() as /sys and /proc are not mounted"); + + assert_se(fds_are_same_mount(fd1, fd2) == 0); + assert_se(fds_are_same_mount(fd2, fd3) > 0); +} + TEST(fd_get_path) { _cleanup_(rm_rf_physical_and_freep) char *t = NULL; _cleanup_close_ int tfd = -EBADF, fd = -EBADF; @@ -654,7 +676,7 @@ TEST(fd_get_path) { tfd = mkdtemp_open(NULL, O_PATH, &t); assert_se(tfd >= 0); assert_se(fd_get_path(tfd, &p) >= 0); - assert_se(streq(p, t)); + ASSERT_STREQ(p, t); p = mfree(p); @@ -662,7 +684,7 @@ TEST(fd_get_path) { assert_se(chdir(t) >= 0); assert_se(fd_get_path(AT_FDCWD, &p) >= 0); - assert_se(streq(p, t)); + ASSERT_STREQ(p, t); p = mfree(p); @@ -675,7 +697,7 @@ TEST(fd_get_path) { fd = openat(tfd, "regular", O_CLOEXEC|O_PATH); assert_se(fd >= 0); assert_se(fd_get_path(fd, &p) >= 0); - assert_se(streq(p, q)); + ASSERT_STREQ(p, q); p = mfree(p); fd = safe_close(fd); @@ -683,7 +705,7 @@ TEST(fd_get_path) { fd = openat(AT_FDCWD, "regular", O_CLOEXEC|O_PATH); assert_se(fd >= 0); assert_se(fd_get_path(fd, &p) >= 0); - assert_se(streq(p, q)); + ASSERT_STREQ(p, q); p = mfree(p); fd = safe_close(fd); @@ -692,7 +714,7 @@ TEST(fd_get_path) { assert_se(fd >= 0); assert_se(fd_verify_regular(fd) >= 0); assert_se(fd_get_path(fd, &p) >= 0); - assert_se(streq(p, q)); + ASSERT_STREQ(p, q); p = mfree(p); fd = safe_close(fd); @@ -701,7 +723,7 @@ TEST(fd_get_path) { assert_se(fd >= 0); assert_se(fd_verify_regular(fd) >= 0); assert_se(fd_get_path(fd, &p) >= 0); - assert_se(streq(p, q)); + ASSERT_STREQ(p, q); p = mfree(p); fd = safe_close(fd); @@ -710,7 +732,7 @@ TEST(fd_get_path) { assert_se(fd >= 0); assert_se(fd_verify_regular(fd) >= 0); assert_se(fd_get_path(fd, &p) >= 0); - assert_se(streq(p, q)); + ASSERT_STREQ(p, q); p = mfree(p); fd = safe_close(fd); @@ -719,7 +741,7 @@ TEST(fd_get_path) { assert_se(fd >= 0); assert_se(fd_verify_regular(fd) >= 0); assert_se(fd_get_path(fd, &p) >= 0); - assert_se(streq(p, q)); + ASSERT_STREQ(p, q); p = mfree(p); q = mfree(q); @@ -730,7 +752,7 @@ TEST(fd_get_path) { assert_se(fd >= 0); assert_se(fd_verify_regular(fd) == -ELOOP); assert_se(fd_get_path(fd, &p) >= 0); - assert_se(streq(p, q)); + ASSERT_STREQ(p, q); p = mfree(p); fd = safe_close(fd); @@ -739,7 +761,7 @@ TEST(fd_get_path) { assert_se(fd >= 0); assert_se(fd_verify_regular(fd) == -ELOOP); assert_se(fd_get_path(fd, &p) >= 0); - assert_se(streq(p, q)); + ASSERT_STREQ(p, q); p = mfree(p); fd = safe_close(fd); @@ -748,7 +770,7 @@ TEST(fd_get_path) { assert_se(fd >= 0); assert_se(fd_verify_regular(fd) == -ELOOP); assert_se(fd_get_path(fd, &p) >= 0); - assert_se(streq(p, q)); + ASSERT_STREQ(p, q); p = mfree(p); fd = safe_close(fd); @@ -757,7 +779,7 @@ TEST(fd_get_path) { assert_se(fd >= 0); assert_se(fd_verify_regular(fd) == -ELOOP); assert_se(fd_get_path(fd, &p) >= 0); - assert_se(streq(p, q)); + ASSERT_STREQ(p, q); assert_se(chdir(saved_cwd) >= 0); } |