]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fdset.c
tests: add test for fdset_iterate
[thirdparty/systemd.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 static void test_fdset_remove(void) {
110 _cleanup_close_ int fd = -1;
111 FDSet *fdset = NULL;
112 char name[] = "/tmp/test-fdset_remove.XXXXXX";
113
114 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
115 assert_se(fd >= 0);
116
117 fdset = fdset_new();
118 assert_se(fdset);
119 assert_se(fdset_put(fdset, fd) >= 0);
120 assert_se(fdset_remove(fdset, fd) >= 0);
121 assert_se(!fdset_contains(fdset, fd));
122 fdset_free(fdset);
123
124 assert_se(fcntl(fd, F_GETFD) >= 0);
125
126 unlink(name);
127 }
128
129 static void test_fdset_iterate(void) {
130 int fd = -1;
131 FDSet *fdset = NULL;
132 char name[] = "/tmp/test-fdset_iterate.XXXXXX";
133 Iterator i;
134 int c = 0;
135 int a;
136
137 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
138 assert_se(fd >= 0);
139
140 fdset = fdset_new();
141 assert_se(fdset);
142 assert_se(fdset_put(fdset, fd) >= 0);
143 assert_se(fdset_put(fdset, fd) >= 0);
144 assert_se(fdset_put(fdset, fd) >= 0);
145
146 FDSET_FOREACH(a, fdset, i) {
147 c++;
148 assert_se(a == fd);
149 }
150 assert_se(c == 1);
151
152 fdset_free(fdset);
153
154 unlink(name);
155 }
156
157 int main(int argc, char *argv[]) {
158 test_fdset_new_fill();
159 test_fdset_put_dup();
160 test_fdset_cloexec();
161 test_fdset_close_others();
162 test_fdset_remove();
163 test_fdset_iterate();
164
165 return 0;
166 }