chiark / gitweb /
7f52748ccb45bb64902daba23ac4b739ef97b72b
[elogind.git] / src / test / test-fdset.c
1 /***
2   This file is part of systemd
3
4   Copyright 2014 Ronny Chevalier
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 #include "fdset.h"
24 #include "util.h"
25 #include "macro.h"
26
27 static void test_fdset_new_fill(void) {
28         int fd = -1;
29         _cleanup_fdset_free_ FDSet *fdset = NULL;
30         char name[] = "/tmp/test-fdset_new_fill.XXXXXX";
31
32         fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
33         assert_se(fd >= 0);
34         assert_se(fdset_new_fill(&fdset) >= 0);
35         assert_se(fdset_contains(fdset, fd));
36
37         unlink(name);
38 }
39
40 static void test_fdset_put_dup(void) {
41         _cleanup_close_ int fd = -1;
42         int copyfd = -1;
43         _cleanup_fdset_free_ FDSet *fdset = NULL;
44         char name[] = "/tmp/test-fdset_put_dup.XXXXXX";
45
46         fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
47         assert_se(fd >= 0);
48
49         fdset = fdset_new();
50         assert_se(fdset);
51         copyfd = fdset_put_dup(fdset, fd);
52         assert_se(copyfd >= 0 && copyfd != fd);
53         assert_se(fdset_contains(fdset, copyfd));
54         assert_se(!fdset_contains(fdset, fd));
55
56         unlink(name);
57 }
58
59 static void test_fdset_cloexec(void) {
60         int fd = -1;
61         _cleanup_fdset_free_ FDSet *fdset = NULL;
62         int flags = -1;
63         char name[] = "/tmp/test-fdset_cloexec.XXXXXX";
64
65         fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
66         assert_se(fd >= 0);
67
68         fdset = fdset_new();
69         assert_se(fdset);
70         assert_se(fdset_put(fdset, fd));
71
72         assert_se(fdset_cloexec(fdset, false) >= 0);
73         flags = fcntl(fd, F_GETFD);
74         assert_se(flags >= 0);
75         assert_se(!(flags & FD_CLOEXEC));
76
77         assert_se(fdset_cloexec(fdset, true) >= 0);
78         flags = fcntl(fd, F_GETFD);
79         assert_se(flags >= 0);
80         assert_se(flags & FD_CLOEXEC);
81
82         unlink(name);
83 }
84
85 static void test_fdset_close_others(void) {
86         int fd = -1;
87         int copyfd = -1;
88         _cleanup_fdset_free_ FDSet *fdset = NULL;
89         int flags = -1;
90         char name[] = "/tmp/test-fdset_close_others.XXXXXX";
91
92         fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
93         assert_se(fd >= 0);
94
95         fdset = fdset_new();
96         assert_se(fdset);
97         copyfd = fdset_put_dup(fdset, fd);
98         assert_se(copyfd >= 0);
99
100         assert_se(fdset_close_others(fdset) >= 0);
101         flags = fcntl(fd, F_GETFD);
102         assert_se(flags < 0);
103         flags = fcntl(copyfd, F_GETFD);
104         assert_se(flags >= 0);
105
106         unlink(name);
107 }
108
109 int main(int argc, char *argv[]) {
110         test_fdset_new_fill();
111         test_fdset_put_dup();
112         test_fdset_cloexec();
113         test_fdset_close_others();
114
115         return 0;
116 }