diff options
author | Ronny Chevalier <chevalier.ronny@gmail.com> | 2014-06-14 22:10:45 +0200 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2014-06-16 20:38:22 +0200 |
commit | 106ecd769d57116ad81efdea2c27557fba062138 (patch) | |
tree | 569d31c5a64342594852f5e919c88a613f90499b /src/test/test-fdset.c | |
parent | c182135d3af7a662739d16353e3cc630199aac24 (diff) |
tests: add test-fdset
Diffstat (limited to 'src/test/test-fdset.c')
-rw-r--r-- | src/test/test-fdset.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/test/test-fdset.c b/src/test/test-fdset.c new file mode 100644 index 0000000000..ed83ee18de --- /dev/null +++ b/src/test/test-fdset.c @@ -0,0 +1,114 @@ +/*** + This file is part of systemd + + Copyright 2014 Ronny Chevalier + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <fcntl.h> +#include <unistd.h> + +#include "fdset.h" +#include "util.h" +#include "macro.h" + +static void test_fdset_new_fill(void) { + int fd = -1; + _cleanup_fdset_free_ FDSet *fdset = NULL; + char name[] = "/tmp/test-fdset_new_fill.XXXXXX"; + + fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); + assert_se(fd >= 0); + assert_se(fdset_new_fill(&fdset) >= 0); + assert_se(fdset_contains(fdset, fd)); + + unlink(name); +} + +static void test_fdset_put_dup(void) { + _cleanup_close_ int fd = -1; + int copyfd = -1; + _cleanup_fdset_free_ FDSet *fdset = NULL; + char name[] = "/tmp/test-fdset_put_dup.XXXXXX"; + + fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); + assert_se(fd >= 0); + + fdset = fdset_new(); + assert_se(fdset); + copyfd = fdset_put_dup(fdset, fd); + assert_se(copyfd >= 0 && copyfd != fd); + assert_se(fdset_contains(fdset, copyfd)); + assert_se(!fdset_contains(fdset, fd)); + + unlink(name); +} + +static void test_fdset_cloexec(void) { + int fd = -1; + _cleanup_fdset_free_ FDSet *fdset = NULL; + int flags = -1; + char name[] = "/tmp/test-fdset_cloexec.XXXXXX"; + + fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); + assert_se(fd >= 0); + + fdset = fdset_new(); + assert_se(fdset); + assert_se(fdset_put(fdset, fd)); + + assert_se(fdset_cloexec(fdset, false) >= 0); + flags = fcntl(fd, F_GETFD); + assert_se(flags >= 0); + assert_se(!(flags & FD_CLOEXEC)); + + assert_se(fdset_cloexec(fdset, true) >= 0); + flags = fcntl(fd, F_GETFD); + assert_se(flags >= 0); + assert_se(flags & FD_CLOEXEC); + + unlink(name); +} + +static void test_fdset_close_others(void) { + int fd = -1; + int copyfd = -1; + _cleanup_fdset_free_ FDSet *fdset = NULL; + int flags = -1; + char name[] = "/tmp/test-fdset_close_others.XXXXXX"; + + fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); + assert_se(fd >= 0); + + fdset = fdset_new(); + assert_se(fdset); + copyfd = fdset_put_dup(fdset, fd); + assert_se(copyfd >= 0); + + assert_se(fdset_close_others(fdset) >= 0); + flags = fcntl(fd, F_GETFD); + assert_se(flags < 0); + flags = fcntl(copyfd, F_GETFD); + assert_se(flags >= 0); + + unlink(name); +} + +int main(int argc, char *argv[]) { + test_fdset_new_fill(); + test_fdset_put_dup(); + test_fdset_cloexec(); + test_fdset_close_others(); +} |