1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7
8 #include "data-fd-util.h"
9 #include "fd-util.h"
10 #include "memory-util.h"
11 #include "process-util.h"
12 #include "tests.h"
13 #include "random-util.h"
14
test_acquire_data_fd_one(unsigned flags)15 static void test_acquire_data_fd_one(unsigned flags) {
16 char wbuffer[196*1024 - 7];
17 char rbuffer[sizeof(wbuffer)];
18 int fd;
19
20 fd = acquire_data_fd("foo", 3, flags);
21 assert_se(fd >= 0);
22
23 zero(rbuffer);
24 assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 3);
25 assert_se(streq(rbuffer, "foo"));
26
27 fd = safe_close(fd);
28
29 fd = acquire_data_fd("", 0, flags);
30 assert_se(fd >= 0);
31
32 zero(rbuffer);
33 assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 0);
34 assert_se(streq(rbuffer, ""));
35
36 fd = safe_close(fd);
37
38 random_bytes(wbuffer, sizeof(wbuffer));
39
40 fd = acquire_data_fd(wbuffer, sizeof(wbuffer), flags);
41 assert_se(fd >= 0);
42
43 zero(rbuffer);
44 assert_se(read(fd, rbuffer, sizeof(rbuffer)) == sizeof(rbuffer));
45 assert_se(memcmp(rbuffer, wbuffer, sizeof(rbuffer)) == 0);
46
47 fd = safe_close(fd);
48 }
49
TEST(acquire_data_fd)50 TEST(acquire_data_fd) {
51 test_acquire_data_fd_one(0);
52 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL);
53 test_acquire_data_fd_one(ACQUIRE_NO_MEMFD);
54 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD);
55 test_acquire_data_fd_one(ACQUIRE_NO_PIPE);
56 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_PIPE);
57 test_acquire_data_fd_one(ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE);
58 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE);
59 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE|ACQUIRE_NO_TMPFILE);
60 }
61
assert_equal_fd(int fd1,int fd2)62 static void assert_equal_fd(int fd1, int fd2) {
63 for (;;) {
64 uint8_t a[4096], b[4096];
65 ssize_t x, y;
66
67 x = read(fd1, a, sizeof(a));
68 assert_se(x >= 0);
69
70 y = read(fd2, b, sizeof(b));
71 assert_se(y >= 0);
72
73 assert_se(x == y);
74
75 if (x == 0)
76 break;
77
78 assert_se(memcmp(a, b, x) == 0);
79 }
80 }
81
TEST(copy_data_fd)82 TEST(copy_data_fd) {
83 _cleanup_close_ int fd1 = -1, fd2 = -1;
84 _cleanup_(close_pairp) int sfd[2] = { -1, -1 };
85 _cleanup_(sigkill_waitp) pid_t pid = -1;
86 int r;
87
88 fd1 = open("/etc/fstab", O_RDONLY|O_CLOEXEC);
89 if (fd1 >= 0) {
90
91 fd2 = copy_data_fd(fd1);
92 assert_se(fd2 >= 0);
93
94 assert_se(lseek(fd1, 0, SEEK_SET) == 0);
95 assert_equal_fd(fd1, fd2);
96 }
97
98 fd1 = safe_close(fd1);
99 fd2 = safe_close(fd2);
100
101 fd1 = acquire_data_fd("hallo", 6, 0);
102 assert_se(fd1 >= 0);
103
104 fd2 = copy_data_fd(fd1);
105 assert_se(fd2 >= 0);
106
107 safe_close(fd1);
108 fd1 = acquire_data_fd("hallo", 6, 0);
109 assert_se(fd1 >= 0);
110
111 assert_equal_fd(fd1, fd2);
112
113 fd1 = safe_close(fd1);
114 fd2 = safe_close(fd2);
115
116 assert_se(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, sfd) >= 0);
117
118 r = safe_fork("(sd-pipe)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
119 assert_se(r >= 0);
120
121 if (r == 0) {
122 /* child */
123
124 sfd[0] = safe_close(sfd[0]);
125
126 for (uint64_t i = 0; i < 1536*1024 / sizeof(uint64_t); i++)
127 assert_se(write(sfd[1], &i, sizeof(i)) == sizeof(i));
128
129 sfd[1] = safe_close(sfd[1]);
130
131 _exit(EXIT_SUCCESS);
132 }
133
134 sfd[1] = safe_close(sfd[1]);
135
136 fd2 = copy_data_fd(sfd[0]);
137 assert_se(fd2 >= 0);
138
139 uint64_t j;
140 for (uint64_t i = 0; i < 1536*1024 / sizeof(uint64_t); i++) {
141 assert_se(read(fd2, &j, sizeof(j)) == sizeof(j));
142 assert_se(i == j);
143 }
144
145 assert_se(read(fd2, &j, sizeof(j)) == 0);
146 }
147
148 DEFINE_TEST_MAIN(LOG_DEBUG);
149