]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-fd-util.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-fd-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
0999c8ad
RC
2/***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
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.
11
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.
16
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/>.
19***/
20
21#include <fcntl.h>
22#include <unistd.h>
23
24#include "alloc-util.h"
25#include "fd-util.h"
26#include "fileio.h"
27#include "macro.h"
28
29static void test_close_many(void) {
30 int fds[3];
31 char name0[] = "/tmp/test-close-many.XXXXXX";
32 char name1[] = "/tmp/test-close-many.XXXXXX";
33 char name2[] = "/tmp/test-close-many.XXXXXX";
34
646853bd
TM
35 fds[0] = mkostemp_safe(name0);
36 fds[1] = mkostemp_safe(name1);
37 fds[2] = mkostemp_safe(name2);
0999c8ad
RC
38
39 close_many(fds, 2);
40
41 assert_se(fcntl(fds[0], F_GETFD) == -1);
42 assert_se(fcntl(fds[1], F_GETFD) == -1);
43 assert_se(fcntl(fds[2], F_GETFD) >= 0);
44
45 safe_close(fds[2]);
46
47 unlink(name0);
48 unlink(name1);
49 unlink(name2);
50}
51
52static void test_close_nointr(void) {
53 char name[] = "/tmp/test-test-close_nointr.XXXXXX";
54 int fd;
55
646853bd 56 fd = mkostemp_safe(name);
0999c8ad
RC
57 assert_se(fd >= 0);
58 assert_se(close_nointr(fd) >= 0);
59 assert_se(close_nointr(fd) < 0);
60
61 unlink(name);
62}
63
64static void test_same_fd(void) {
65 _cleanup_close_pair_ int p[2] = { -1, -1 };
66 _cleanup_close_ int a = -1, b = -1, c = -1;
67
68 assert_se(pipe2(p, O_CLOEXEC) >= 0);
69 assert_se((a = dup(p[0])) >= 0);
70 assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
71 assert_se((c = dup(a)) >= 0);
72
73 assert_se(same_fd(p[0], p[0]) > 0);
74 assert_se(same_fd(p[1], p[1]) > 0);
75 assert_se(same_fd(a, a) > 0);
76 assert_se(same_fd(b, b) > 0);
77
78 assert_se(same_fd(a, p[0]) > 0);
79 assert_se(same_fd(p[0], a) > 0);
80 assert_se(same_fd(c, p[0]) > 0);
81 assert_se(same_fd(p[0], c) > 0);
82 assert_se(same_fd(a, c) > 0);
83 assert_se(same_fd(c, a) > 0);
84
85 assert_se(same_fd(p[0], p[1]) == 0);
86 assert_se(same_fd(p[1], p[0]) == 0);
87 assert_se(same_fd(p[0], b) == 0);
88 assert_se(same_fd(b, p[0]) == 0);
89 assert_se(same_fd(p[1], a) == 0);
90 assert_se(same_fd(a, p[1]) == 0);
91 assert_se(same_fd(p[1], b) == 0);
92 assert_se(same_fd(b, p[1]) == 0);
93
94 assert_se(same_fd(a, b) == 0);
95 assert_se(same_fd(b, a) == 0);
96}
97
504afd7c
ZJS
98static void test_open_serialization_fd(void) {
99 _cleanup_close_ int fd = -1;
100
101 fd = open_serialization_fd("test");
102 assert_se(fd >= 0);
103
104 write(fd, "test\n", 5);
105}
106
0999c8ad
RC
107int main(int argc, char *argv[]) {
108 test_close_many();
109 test_close_nointr();
110 test_same_fd();
504afd7c 111 test_open_serialization_fd();
0999c8ad
RC
112
113 return 0;
114}