]>
git.proxmox.com Git - systemd.git/blob - src/test/test-fd-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "alloc-util.h"
28 #include "random-util.h"
29 #include "string-util.h"
32 static void test_close_many(void) {
34 char name0
[] = "/tmp/test-close-many.XXXXXX";
35 char name1
[] = "/tmp/test-close-many.XXXXXX";
36 char name2
[] = "/tmp/test-close-many.XXXXXX";
38 fds
[0] = mkostemp_safe(name0
);
39 fds
[1] = mkostemp_safe(name1
);
40 fds
[2] = mkostemp_safe(name2
);
44 assert_se(fcntl(fds
[0], F_GETFD
) == -1);
45 assert_se(fcntl(fds
[1], F_GETFD
) == -1);
46 assert_se(fcntl(fds
[2], F_GETFD
) >= 0);
55 static void test_close_nointr(void) {
56 char name
[] = "/tmp/test-test-close_nointr.XXXXXX";
59 fd
= mkostemp_safe(name
);
61 assert_se(close_nointr(fd
) >= 0);
62 assert_se(close_nointr(fd
) < 0);
67 static void test_same_fd(void) {
68 _cleanup_close_pair_
int p
[2] = { -1, -1 };
69 _cleanup_close_
int a
= -1, b
= -1, c
= -1;
71 assert_se(pipe2(p
, O_CLOEXEC
) >= 0);
72 assert_se((a
= dup(p
[0])) >= 0);
73 assert_se((b
= open("/dev/null", O_RDONLY
|O_CLOEXEC
)) >= 0);
74 assert_se((c
= dup(a
)) >= 0);
76 assert_se(same_fd(p
[0], p
[0]) > 0);
77 assert_se(same_fd(p
[1], p
[1]) > 0);
78 assert_se(same_fd(a
, a
) > 0);
79 assert_se(same_fd(b
, b
) > 0);
81 assert_se(same_fd(a
, p
[0]) > 0);
82 assert_se(same_fd(p
[0], a
) > 0);
83 assert_se(same_fd(c
, p
[0]) > 0);
84 assert_se(same_fd(p
[0], c
) > 0);
85 assert_se(same_fd(a
, c
) > 0);
86 assert_se(same_fd(c
, a
) > 0);
88 assert_se(same_fd(p
[0], p
[1]) == 0);
89 assert_se(same_fd(p
[1], p
[0]) == 0);
90 assert_se(same_fd(p
[0], b
) == 0);
91 assert_se(same_fd(b
, p
[0]) == 0);
92 assert_se(same_fd(p
[1], a
) == 0);
93 assert_se(same_fd(a
, p
[1]) == 0);
94 assert_se(same_fd(p
[1], b
) == 0);
95 assert_se(same_fd(b
, p
[1]) == 0);
97 assert_se(same_fd(a
, b
) == 0);
98 assert_se(same_fd(b
, a
) == 0);
101 static void test_open_serialization_fd(void) {
102 _cleanup_close_
int fd
= -1;
104 fd
= open_serialization_fd("test");
107 assert_se(write(fd
, "test\n", 5) == 5);
110 static void test_acquire_data_fd_one(unsigned flags
) {
111 char wbuffer
[196*1024 - 7];
112 char rbuffer
[sizeof(wbuffer
)];
115 fd
= acquire_data_fd("foo", 3, flags
);
119 assert_se(read(fd
, rbuffer
, sizeof(rbuffer
)) == 3);
120 assert_se(streq(rbuffer
, "foo"));
124 fd
= acquire_data_fd("", 0, flags
);
128 assert_se(read(fd
, rbuffer
, sizeof(rbuffer
)) == 0);
129 assert_se(streq(rbuffer
, ""));
133 random_bytes(wbuffer
, sizeof(wbuffer
));
135 fd
= acquire_data_fd(wbuffer
, sizeof(wbuffer
), flags
);
139 assert_se(read(fd
, rbuffer
, sizeof(rbuffer
)) == sizeof(rbuffer
));
140 assert_se(memcmp(rbuffer
, wbuffer
, sizeof(rbuffer
)) == 0);
145 static void test_acquire_data_fd(void) {
147 test_acquire_data_fd_one(0);
148 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL
);
149 test_acquire_data_fd_one(ACQUIRE_NO_MEMFD
);
150 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL
|ACQUIRE_NO_MEMFD
);
151 test_acquire_data_fd_one(ACQUIRE_NO_PIPE
);
152 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL
|ACQUIRE_NO_PIPE
);
153 test_acquire_data_fd_one(ACQUIRE_NO_MEMFD
|ACQUIRE_NO_PIPE
);
154 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL
|ACQUIRE_NO_MEMFD
|ACQUIRE_NO_PIPE
);
155 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL
|ACQUIRE_NO_MEMFD
|ACQUIRE_NO_PIPE
|ACQUIRE_NO_TMPFILE
);
158 int main(int argc
, char *argv
[]) {
162 test_open_serialization_fd();
163 test_acquire_data_fd();