| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
|
| 5 |
#include "fd-util.h" |
| 6 |
#include "fdset.h" |
| 7 |
#include "tests.h" |
| 8 |
#include "tmpfile-util.h" |
| 9 |
|
| 10 |
TEST(fdset_new_fill) { |
| 11 |
_cleanup_fdset_free_ FDSet *fdset = NULL; |
| 12 |
int fd = -EBADF, flags; |
| 13 |
|
| 14 |
log_close(); |
| 15 |
log_set_open_when_needed(true); |
| 16 |
|
| 17 |
fd = open("/dev/null", O_CLOEXEC|O_RDONLY); |
| 18 |
assert_se(fd >= 0); |
| 19 |
|
| 20 |
assert_se(fdset_new_fill(/* filter_cloexec= */ -1, &fdset) >= 0); |
| 21 |
assert_se(fdset_contains(fdset, fd)); |
| 22 |
fdset = fdset_free(fdset); |
| 23 |
assert_se(fd_validate(fd) == -EBADF); |
| 24 |
|
| 25 |
fd = open("/dev/null", O_CLOEXEC|O_RDONLY); |
| 26 |
assert_se(fd >= 0); |
| 27 |
|
| 28 |
assert_se(fdset_new_fill(/* filter_cloexec= */ 0, &fdset) >= 0); |
| 29 |
assert_se(!fdset_contains(fdset, fd)); |
| 30 |
fdset = fdset_free(fdset); |
| 31 |
assert_se(fd_validate(fd) >= 0); |
| 32 |
|
| 33 |
assert_se(fdset_new_fill(/* filter_cloexec= */ 1, &fdset) >= 0); |
| 34 |
assert_se(fdset_contains(fdset, fd)); |
| 35 |
fdset = fdset_free(fdset); |
| 36 |
assert_se(fd_validate(fd) == -EBADF); |
| 37 |
|
| 38 |
fd = open("/dev/null", O_RDONLY); |
| 39 |
assert_se(fd >= 0); |
| 40 |
|
| 41 |
assert_se(fdset_new_fill(/* filter_cloexec= */ 1, &fdset) >= 0); |
| 42 |
assert_se(!fdset_contains(fdset, fd)); |
| 43 |
fdset = fdset_free(fdset); |
| 44 |
assert_se(fd_validate(fd) >= 0); |
| 45 |
|
| 46 |
assert_se(fdset_new_fill(/* filter_cloexec= */ 0, &fdset) >= 0); |
| 47 |
assert_se(fdset_contains(fdset, fd)); |
| 48 |
flags = fcntl(fd, F_GETFD); |
| 49 |
assert_se(flags >= 0); |
| 50 |
assert_se(FLAGS_SET(flags, FD_CLOEXEC)); |
| 51 |
fdset = fdset_free(fdset); |
| 52 |
assert_se(fd_validate(fd) == -EBADF); |
| 53 |
|
| 54 |
log_open(); |
| 55 |
} |
| 56 |
|
| 57 |
TEST(fdset_put_dup) { |
| 58 |
_cleanup_close_ int fd = -EBADF; |
| 59 |
int copyfd = -EBADF; |
| 60 |
_cleanup_fdset_free_ FDSet *fdset = NULL; |
| 61 |
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_put_dup.XXXXXX"; |
| 62 |
|
| 63 |
fd = mkostemp_safe(name); |
| 64 |
assert_se(fd >= 0); |
| 65 |
|
| 66 |
fdset = fdset_new(); |
| 67 |
assert_se(fdset); |
| 68 |
copyfd = fdset_put_dup(fdset, fd); |
| 69 |
assert_se(copyfd >= 0 && copyfd != fd); |
| 70 |
assert_se(fdset_contains(fdset, copyfd)); |
| 71 |
assert_se(!fdset_contains(fdset, fd)); |
| 72 |
} |
| 73 |
|
| 74 |
TEST(fdset_cloexec) { |
| 75 |
int fd = -EBADF; |
| 76 |
_cleanup_fdset_free_ FDSet *fdset = NULL; |
| 77 |
int flags = -1; |
| 78 |
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_cloexec.XXXXXX"; |
| 79 |
|
| 80 |
fd = mkostemp_safe(name); |
| 81 |
assert_se(fd >= 0); |
| 82 |
|
| 83 |
fdset = fdset_new(); |
| 84 |
assert_se(fdset); |
| 85 |
assert_se(fdset_put(fdset, fd)); |
| 86 |
|
| 87 |
assert_se(fdset_cloexec(fdset, false) >= 0); |
| 88 |
flags = fcntl(fd, F_GETFD); |
| 89 |
assert_se(flags >= 0); |
| 90 |
assert_se(!(flags & FD_CLOEXEC)); |
| 91 |
|
| 92 |
assert_se(fdset_cloexec(fdset, true) >= 0); |
| 93 |
flags = fcntl(fd, F_GETFD); |
| 94 |
assert_se(flags >= 0); |
| 95 |
assert_se(flags & FD_CLOEXEC); |
| 96 |
} |
| 97 |
|
| 98 |
TEST(fdset_close_others) { |
| 99 |
int fd = -EBADF, copyfd = -EBADF; |
| 100 |
_cleanup_fdset_free_ FDSet *fdset = NULL; |
| 101 |
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_close_others.XXXXXX"; |
| 102 |
|
| 103 |
fd = mkostemp_safe(name); |
| 104 |
assert_se(fd >= 0); |
| 105 |
|
| 106 |
fdset = fdset_new(); |
| 107 |
assert_se(fdset); |
| 108 |
copyfd = fdset_put_dup(fdset, fd); |
| 109 |
assert_se(copyfd >= 0); |
| 110 |
|
| 111 |
/* fdset_close_others() will close any logging file descriptors as well, so close them beforehand |
| 112 |
* and reopen them again afterwards. */ |
| 113 |
log_close(); |
| 114 |
assert_se(fdset_close_others(fdset) >= 0); |
| 115 |
|
| 116 |
assert_se(fd_validate(fd) == -EBADF); |
| 117 |
|
| 118 |
/* Open log again after checking that fd is invalid, since reopening the log might make fd a valid |
| 119 |
* file descriptor again. */ |
| 120 |
(void) log_open(); |
| 121 |
|
| 122 |
assert_se(fd_validate(copyfd) >= 0); |
| 123 |
} |
| 124 |
|
| 125 |
TEST(fdset_remove) { |
| 126 |
_cleanup_close_ int fd = -EBADF; |
| 127 |
_cleanup_fdset_free_ FDSet *fdset = NULL; |
| 128 |
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_remove.XXXXXX"; |
| 129 |
|
| 130 |
fd = mkostemp_safe(name); |
| 131 |
assert_se(fd >= 0); |
| 132 |
|
| 133 |
fdset = fdset_new(); |
| 134 |
assert_se(fdset); |
| 135 |
assert_se(fdset_put(fdset, fd) >= 0); |
| 136 |
assert_se(fdset_remove(fdset, fd) >= 0); |
| 137 |
assert_se(!fdset_contains(fdset, fd)); |
| 138 |
|
| 139 |
assert_se(fd_validate(fd) >= 0); |
| 140 |
} |
| 141 |
|
| 142 |
TEST(fdset_iterate) { |
| 143 |
int fd = -EBADF; |
| 144 |
_cleanup_fdset_free_ FDSet *fdset = NULL; |
| 145 |
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_iterate.XXXXXX"; |
| 146 |
int c = 0; |
| 147 |
int a; |
| 148 |
|
| 149 |
fd = mkostemp_safe(name); |
| 150 |
assert_se(fd >= 0); |
| 151 |
|
| 152 |
fdset = fdset_new(); |
| 153 |
assert_se(fdset); |
| 154 |
assert_se(fdset_put(fdset, fd) >= 0); |
| 155 |
assert_se(fdset_put(fdset, fd) >= 0); |
| 156 |
assert_se(fdset_put(fdset, fd) >= 0); |
| 157 |
|
| 158 |
FDSET_FOREACH(a, fdset) { |
| 159 |
c++; |
| 160 |
assert_se(a == fd); |
| 161 |
} |
| 162 |
assert_se(c == 1); |
| 163 |
} |
| 164 |
|
| 165 |
TEST(fdset_isempty) { |
| 166 |
int fd; |
| 167 |
_cleanup_fdset_free_ FDSet *fdset = NULL; |
| 168 |
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_isempty.XXXXXX"; |
| 169 |
|
| 170 |
fd = mkostemp_safe(name); |
| 171 |
assert_se(fd >= 0); |
| 172 |
|
| 173 |
fdset = fdset_new(); |
| 174 |
assert_se(fdset); |
| 175 |
|
| 176 |
assert_se(fdset_isempty(fdset)); |
| 177 |
assert_se(fdset_put(fdset, fd) >= 0); |
| 178 |
assert_se(!fdset_isempty(fdset)); |
| 179 |
} |
| 180 |
|
| 181 |
TEST(fdset_steal_first) { |
| 182 |
int fd; |
| 183 |
_cleanup_fdset_free_ FDSet *fdset = NULL; |
| 184 |
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_steal_first.XXXXXX"; |
| 185 |
|
| 186 |
fd = mkostemp_safe(name); |
| 187 |
assert_se(fd >= 0); |
| 188 |
|
| 189 |
fdset = fdset_new(); |
| 190 |
assert_se(fdset); |
| 191 |
|
| 192 |
assert_se(fdset_steal_first(fdset) < 0); |
| 193 |
assert_se(fdset_put(fdset, fd) >= 0); |
| 194 |
assert_se(fdset_steal_first(fdset) == fd); |
| 195 |
assert_se(fdset_steal_first(fdset) < 0); |
| 196 |
assert_se(fdset_put(fdset, fd) >= 0); |
| 197 |
} |
| 198 |
|
| 199 |
TEST(fdset_new_array) { |
| 200 |
int fds[] = {10, 11, 12, 13}; |
| 201 |
_cleanup_fdset_free_ FDSet *fdset = NULL; |
| 202 |
|
| 203 |
assert_se(fdset_new_array(&fdset, fds, 4) >= 0); |
| 204 |
assert_se(fdset_size(fdset) == 4); |
| 205 |
assert_se(fdset_contains(fdset, 10)); |
| 206 |
assert_se(fdset_contains(fdset, 11)); |
| 207 |
assert_se(fdset_contains(fdset, 12)); |
| 208 |
assert_se(fdset_contains(fdset, 13)); |
| 209 |
} |
| 210 |
|
| 211 |
DEFINE_TEST_MAIN(LOG_INFO); |