diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2021-06-21 22:54:12 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2021-06-24 10:05:22 +0200 |
commit | 6a818c3cb4cba768dc42efa84db2fbd938f5def0 (patch) | |
tree | deeb8ffe7151f9445aaf703e9efc596d59a7040c /src/test/test-fd-util.c | |
parent | 37350b81b5ce7c94636ea637a84af479132b6726 (diff) | |
download | systemd-6a818c3cb4cba768dc42efa84db2fbd938f5def0.tar.gz |
basic: move acquire_data_fd() and fd_duplicate_data_fd() to new data-fd-util.c
fd_duplicate_data_fd() is renamed to copy_data_fd(). This makes
the two functions have nicely similar names.
Now fd-util.[ch] is again about low-level file descriptor manipulations.
copy_data_fd() is a complex function that internally wraps the other
functions in copy.c. I want to move copy.c and the whole cluster of
related code from basic/ to shared/ later on, and this is a preparatory
step for that.
Diffstat (limited to 'src/test/test-fd-util.c')
-rw-r--r-- | src/test/test-fd-util.c | 138 |
1 files changed, 1 insertions, 137 deletions
diff --git a/src/test/test-fd-util.c b/src/test/test-fd-util.c index bece89aef2..1cd3bdfbbe 100644 --- a/src/test/test-fd-util.c +++ b/src/test/test-fd-util.c @@ -4,6 +4,7 @@ #include <unistd.h> #include "alloc-util.h" +#include "data-fd-util.h" #include "fd-util.h" #include "fileio.h" #include "macro.h" @@ -95,54 +96,6 @@ static void test_open_serialization_fd(void) { assert_se(write(fd, "test\n", 5) == 5); } -static void test_acquire_data_fd_one(unsigned flags) { - char wbuffer[196*1024 - 7]; - char rbuffer[sizeof(wbuffer)]; - int fd; - - fd = acquire_data_fd("foo", 3, flags); - assert_se(fd >= 0); - - zero(rbuffer); - assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 3); - assert_se(streq(rbuffer, "foo")); - - fd = safe_close(fd); - - fd = acquire_data_fd("", 0, flags); - assert_se(fd >= 0); - - zero(rbuffer); - assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 0); - assert_se(streq(rbuffer, "")); - - fd = safe_close(fd); - - random_bytes(wbuffer, sizeof(wbuffer)); - - fd = acquire_data_fd(wbuffer, sizeof(wbuffer), flags); - assert_se(fd >= 0); - - zero(rbuffer); - assert_se(read(fd, rbuffer, sizeof(rbuffer)) == sizeof(rbuffer)); - assert_se(memcmp(rbuffer, wbuffer, sizeof(rbuffer)) == 0); - - fd = safe_close(fd); -} - -static void test_acquire_data_fd(void) { - - test_acquire_data_fd_one(0); - test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL); - test_acquire_data_fd_one(ACQUIRE_NO_MEMFD); - test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD); - test_acquire_data_fd_one(ACQUIRE_NO_PIPE); - test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_PIPE); - test_acquire_data_fd_one(ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE); - test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE); - test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE|ACQUIRE_NO_TMPFILE); -} - static void test_fd_move_above_stdio(void) { int original_stdin, new_fd; @@ -227,93 +180,6 @@ static void test_rearrange_stdio(void) { } } -static void assert_equal_fd(int fd1, int fd2) { - - for (;;) { - uint8_t a[4096], b[4096]; - ssize_t x, y; - - x = read(fd1, a, sizeof(a)); - assert_se(x >= 0); - - y = read(fd2, b, sizeof(b)); - assert_se(y >= 0); - - assert_se(x == y); - - if (x == 0) - break; - - assert_se(memcmp(a, b, x) == 0); - } -} - -static void test_fd_duplicate_data_fd(void) { - _cleanup_close_ int fd1 = -1, fd2 = -1; - _cleanup_(close_pairp) int sfd[2] = { -1, -1 }; - _cleanup_(sigkill_waitp) pid_t pid = -1; - uint64_t i, j; - int r; - - fd1 = open("/etc/fstab", O_RDONLY|O_CLOEXEC); - if (fd1 >= 0) { - - fd2 = fd_duplicate_data_fd(fd1); - assert_se(fd2 >= 0); - - assert_se(lseek(fd1, 0, SEEK_SET) == 0); - assert_equal_fd(fd1, fd2); - } - - fd1 = safe_close(fd1); - fd2 = safe_close(fd2); - - fd1 = acquire_data_fd("hallo", 6, 0); - assert_se(fd1 >= 0); - - fd2 = fd_duplicate_data_fd(fd1); - assert_se(fd2 >= 0); - - safe_close(fd1); - fd1 = acquire_data_fd("hallo", 6, 0); - assert_se(fd1 >= 0); - - assert_equal_fd(fd1, fd2); - - fd1 = safe_close(fd1); - fd2 = safe_close(fd2); - - assert_se(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, sfd) >= 0); - - r = safe_fork("(sd-pipe)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid); - assert_se(r >= 0); - - if (r == 0) { - /* child */ - - sfd[0] = safe_close(sfd[0]); - - for (i = 0; i < 1536*1024 / sizeof(uint64_t); i++) - assert_se(write(sfd[1], &i, sizeof(i)) == sizeof(i)); - - sfd[1] = safe_close(sfd[1]); - - _exit(EXIT_SUCCESS); - } - - sfd[1] = safe_close(sfd[1]); - - fd2 = fd_duplicate_data_fd(sfd[0]); - assert_se(fd2 >= 0); - - for (i = 0; i < 1536*1024 / sizeof(uint64_t); i++) { - assert_se(read(fd2, &j, sizeof(j)) == sizeof(j)); - assert_se(i == j); - } - - assert_se(read(fd2, &j, sizeof(j)) == 0); -} - static void test_read_nr_open(void) { log_info("nr-open: %i", read_nr_open()); } @@ -420,10 +286,8 @@ int main(int argc, char *argv[]) { test_close_nointr(); test_same_fd(); test_open_serialization_fd(); - test_acquire_data_fd(); test_fd_move_above_stdio(); test_rearrange_stdio(); - test_fd_duplicate_data_fd(); test_read_nr_open(); test_close_all_fds(); |